diff --git a/src/presentation/artifacteditormodel.h b/src/presentation/artifacteditormodel.h --- a/src/presentation/artifacteditormodel.h +++ b/src/presentation/artifacteditormodel.h @@ -92,6 +92,7 @@ void setStartDate(const QDateTime &start); void setDueDate(const QDateTime &due); void delegate(const QString &name, const QString &email); + void openAttachment(const QModelIndex &index); void setEditingInProgress(bool editingInProgress); diff --git a/src/presentation/artifacteditormodel.cpp b/src/presentation/artifacteditormodel.cpp --- a/src/presentation/artifacteditormodel.cpp +++ b/src/presentation/artifacteditormodel.cpp @@ -25,7 +25,10 @@ #include "artifacteditormodel.h" #include +#include +#include #include +#include #include #include @@ -291,6 +294,25 @@ m_delegateFunction(task, delegate); } +void ArtifactEditorModel::openAttachment(const QModelIndex &index) +{ + auto task = m_artifact.objectCast(); + Q_ASSERT(task); + auto attachment = task->attachments().at(index.row()); + + auto uri = attachment.uri(); + if (!attachment.isUri()) { + auto tempFile = new QTemporaryFile(QDir::tempPath() + QStringLiteral("/zanshin_attachment_XXXXXX"), this); + tempFile->open(); + tempFile->setPermissions(QFile::ReadUser); + tempFile->write(attachment.data()); + tempFile->close(); + uri = QUrl::fromLocalFile(tempFile->fileName()); + } + + QDesktopServices::openUrl(uri); +} + void ArtifactEditorModel::setEditingInProgress(bool editing) { m_editingInProgress = editing; diff --git a/src/widgets/editorview.h b/src/widgets/editorview.h --- a/src/widgets/editorview.h +++ b/src/widgets/editorview.h @@ -83,6 +83,8 @@ void onStartTodayClicked(); void onDelegateEntered(); + void onAttachmentDoubleClicked(const QModelIndex &index); + private: QObject *m_model; diff --git a/src/widgets/editorview.cpp b/src/widgets/editorview.cpp --- a/src/widgets/editorview.cpp +++ b/src/widgets/editorview.cpp @@ -83,6 +83,7 @@ connect(ui->dueDateEdit, &KPIM::KDateEdit::dateEntered, this, &EditorView::onDueEditEntered); connect(ui->doneButton, &QAbstractButton::toggled, this, &EditorView::onDoneButtonChanged); connect(ui->startTodayButton, &QAbstractButton::clicked, this, &EditorView::onStartTodayClicked); + connect(ui->attachmentList, &QAbstractItemView::doubleClicked, this, &EditorView::onAttachmentDoubleClicked); connect(m_delegateEdit, &KLineEdit::returnPressed, this, &EditorView::onDelegateEntered); setEnabled(false); @@ -278,3 +279,11 @@ m_delegateEdit->clear(); } } + +void EditorView::onAttachmentDoubleClicked(const QModelIndex &index) +{ + if (!m_model) + return; + + QMetaObject::invokeMethod(m_model, "openAttachment", Q_ARG(QModelIndex, index)); +}