From acf2a53fea8fdbe366627e45fffe87434e3c2e7e Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Wed, 24 May 2017 12:59:23 +0200 Subject: Attachment open and download buttons We'll need proper icons though. --- framework/qml/AttachmentDelegate.qml | 33 ++++++++++++++-------- framework/qml/MailViewer.qml | 10 +++---- framework/src/domain/mime/attachmentmodel.cpp | 40 +++++++++++++++++++-------- framework/src/domain/mime/messageparser.h | 1 + 4 files changed, 56 insertions(+), 28 deletions(-) (limited to 'framework') diff --git a/framework/qml/AttachmentDelegate.qml b/framework/qml/AttachmentDelegate.qml index 994afd8c..081baf5d 100644 --- a/framework/qml/AttachmentDelegate.qml +++ b/framework/qml/AttachmentDelegate.qml @@ -17,8 +17,6 @@ */ import QtQuick 2.7 -import QtQuick.Layouts 1.1 -import org.kde.kirigami 1.0 as Kirigami import org.kube.framework 1.0 as Kube Item { @@ -26,9 +24,11 @@ Item { property string name property string icon + signal open; + signal download; - width: content.width + Kirigami.Units.gridUnit / 2 - height: content.height + Kirigami.Units.gridUnit / 2 + width: content.width + Kube.Units.gridUnit / 2 + height: content.height + Kube.Units.gridUnit / 2 Rectangle { anchors.fill: parent @@ -37,26 +37,25 @@ Item { color: Kube.Colors.disabledTextColor } - RowLayout { + Row { id: content anchors.centerIn: parent - spacing: Kirigami.Units.smallSpacing + spacing: Kube.Units.smallSpacing Rectangle { id: mimetype - height: Kirigami.Units.gridUnit - width: Kirigami.Units.gridUnit + height: Kube.Units.gridUnit + width: Kube.Units.gridUnit color: Kube.Colors.backgroundColor - Kirigami.Icon { + Kube.Icon { height: parent.height width: height - - source: root.icon + iconName: root.icon } } @@ -64,5 +63,17 @@ Item { text: root.name color: Kube.Colors.backgroundColor } + Kube.IconButton { + height: parent.height + width: height + iconName: Kube.Icons.goDown + onClicked: root.download() + } + Kube.IconButton { + height: parent.height + width: height + iconName: Kube.Icons.edit + onClicked: root.open() + } } } diff --git a/framework/qml/MailViewer.qml b/framework/qml/MailViewer.qml index d2c0a3be..14c4bc8a 100644 --- a/framework/qml/MailViewer.qml +++ b/framework/qml/MailViewer.qml @@ -286,11 +286,11 @@ Rectangle { icon: model.iconName clip: true - - //TODO size encrypted signed type - MouseArea { - anchors.fill: parent - onClicked: messageParser.attachments.saveAttachmentToDisk(messageParser.attachments.index(index, 0)) + onDownload: { + messageParser.attachments.saveAttachmentToDisk(messageParser.attachments.index(index, 0)) + } + onOpen: { + messageParser.attachments.openAttachment(messageParser.attachments.index(index, 0)) } } } diff --git a/framework/src/domain/mime/attachmentmodel.cpp b/framework/src/domain/mime/attachmentmodel.cpp index 714177e1..99745796 100644 --- a/framework/src/domain/mime/attachmentmodel.cpp +++ b/framework/src/domain/mime/attachmentmodel.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include QString sizeHuman(const Content::Ptr &content) @@ -129,34 +130,49 @@ QVariant AttachmentModel::data(const QModelIndex &index, int role) const return QVariant(); } -bool AttachmentModel::saveAttachmentToDisk(const QModelIndex &index) +static QString saveAttachmentToDisk(const QModelIndex &index, const QString &path) { if (index.internalPointer()) { const auto entry = static_cast(index.internalPointer()); const auto content = entry->content().at(0); auto filename = entry->mailMime()->filename(); auto data = content->mailMime()->decodedContent(); - - auto downloadDir = QStandardPaths::writableLocation(QStandardPaths::DownloadLocation); - if (downloadDir.isEmpty()) { - downloadDir = "~"; - } - downloadDir += "/kube/"; - QDir{}.mkpath(downloadDir); - - auto fname = downloadDir + filename; - if (content->mailMime()->isText() && !data.isEmpty()) { // convert CRLF to LF before writing text attachments to disk data = KMime::CRLFtoLF(data); } + auto fname = path + filename; QFile f(fname); if (!f.open(QIODevice::ReadWrite)) { qWarning() << "Failed to write attachment to file:" << fname << " Error: " << f.errorString(); - return false; + return {}; } f.write(data); qInfo() << "Wrote attachment to file: " << fname; + return fname; + } + return {}; + +} + +bool AttachmentModel::saveAttachmentToDisk(const QModelIndex &index) +{ + auto downloadDir = QStandardPaths::writableLocation(QStandardPaths::DownloadLocation); + if (downloadDir.isEmpty()) { + downloadDir = "~"; + } + downloadDir += "/kube/"; + QDir{}.mkpath(downloadDir); + return !::saveAttachmentToDisk(index, downloadDir).isEmpty(); +} + +bool AttachmentModel::openAttachment(const QModelIndex &index) +{ + auto downloadDir = QStandardPaths::writableLocation(QStandardPaths::TempLocation)+ "/kube/"; + QDir{}.mkpath(downloadDir); + const auto filePath = ::saveAttachmentToDisk(index, downloadDir); + if (!filePath.isEmpty()) { + QDesktopServices::openUrl(QUrl("file://" + filePath)); return true; } return false; diff --git a/framework/src/domain/mime/messageparser.h b/framework/src/domain/mime/messageparser.h index 1f44b296..de72a025 100644 --- a/framework/src/domain/mime/messageparser.h +++ b/framework/src/domain/mime/messageparser.h @@ -112,6 +112,7 @@ public: int columnCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE; Q_INVOKABLE bool saveAttachmentToDisk(const QModelIndex &parent); + Q_INVOKABLE bool openAttachment(const QModelIndex &index); private: std::unique_ptr d; -- cgit v1.2.3