From c39898dad95ef711e8abfb92961cef941a697b86 Mon Sep 17 00:00:00 2001 From: Felix Geyer Date: Wed, 3 Dec 2014 21:50:17 +0100 Subject: [PATCH] Support opening attachments directly. --- src/gui/entry/EditEntryWidget.cpp | 39 ++++++++++++++++++++++++ src/gui/entry/EditEntryWidget.h | 2 ++ src/gui/entry/EditEntryWidgetAdvanced.ui | 9 +++++- 3 files changed, 49 insertions(+), 1 deletion(-) diff --git a/src/gui/entry/EditEntryWidget.cpp b/src/gui/entry/EditEntryWidget.cpp index 465f5d401..fcb53d6a0 100644 --- a/src/gui/entry/EditEntryWidget.cpp +++ b/src/gui/entry/EditEntryWidget.cpp @@ -25,6 +25,7 @@ #include #include #include +#include #include "core/Config.h" #include "core/Database.h" @@ -107,7 +108,9 @@ void EditEntryWidget::setupAdvanced() m_attachmentsModel->setEntryAttachments(m_entryAttachments); m_advancedUi->attachmentsView->setModel(m_attachmentsModel); + connect(m_advancedUi->attachmentsView, SIGNAL(doubleClicked(QModelIndex)), SLOT(openAttachment(QModelIndex))); connect(m_advancedUi->saveAttachmentButton, SIGNAL(clicked()), SLOT(saveCurrentAttachment())); + connect(m_advancedUi->openAttachmentButton, SIGNAL(clicked()), SLOT(openCurrentAttachment())); connect(m_advancedUi->addAttachmentButton, SIGNAL(clicked()), SLOT(insertAttachment())); connect(m_advancedUi->removeAttachmentButton, SIGNAL(clicked()), SLOT(removeCurrentAttachment())); @@ -636,6 +639,42 @@ void EditEntryWidget::saveCurrentAttachment() } } +void EditEntryWidget::openAttachment(const QModelIndex& index) +{ + if (!index.isValid()) { + Q_ASSERT(false); + return; + } + + QString filename = m_attachmentsModel->keyByIndex(index); + QByteArray attachmentData = m_entryAttachments->value(filename); + + // tmp file will be removed once the database (or the application) has been closed + QString tmpFileTemplate = QDir::temp().absoluteFilePath(filename); + QTemporaryFile* file = new QTemporaryFile(tmpFileTemplate, this); + + if (!file->open()) { + MessageBox::warning(this, tr("Error"), + tr("Unable to save the attachment:\n").append(file->errorString())); + return; + } + + if (file->write(attachmentData) != attachmentData.size()) { + MessageBox::warning(this, tr("Error"), + tr("Unable to save the attachment:\n").append(file->errorString())); + return; + } + + QDesktopServices::openUrl(QUrl::fromLocalFile(file->fileName())); +} + +void EditEntryWidget::openCurrentAttachment() +{ + QModelIndex index = m_advancedUi->attachmentsView->currentIndex(); + + openAttachment(index); +} + void EditEntryWidget::removeCurrentAttachment() { Q_ASSERT(!m_history); diff --git a/src/gui/entry/EditEntryWidget.h b/src/gui/entry/EditEntryWidget.h index 4e3d82efa..903029872 100644 --- a/src/gui/entry/EditEntryWidget.h +++ b/src/gui/entry/EditEntryWidget.h @@ -76,6 +76,8 @@ private Q_SLOTS: void updateCurrentAttribute(); void insertAttachment(); void saveCurrentAttachment(); + void openAttachment(const QModelIndex& index); + void openCurrentAttachment(); void removeCurrentAttachment(); void updateAutoTypeEnabled(); void insertAutoTypeAssoc(); diff --git a/src/gui/entry/EditEntryWidgetAdvanced.ui b/src/gui/entry/EditEntryWidgetAdvanced.ui index ff7ccbb73..551ea4afe 100644 --- a/src/gui/entry/EditEntryWidgetAdvanced.ui +++ b/src/gui/entry/EditEntryWidgetAdvanced.ui @@ -7,7 +7,7 @@ 0 0 400 - 299 + 315 @@ -106,6 +106,13 @@ + + + + Open + + +