From 026278f1cf0c692b8ba134c84c26da5bb8274d67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sandro=20Knau=C3=9F?= Date: Tue, 8 Nov 2016 16:11:02 +0100 Subject: Add AttachmentModel for viewer --- framework/domain/CMakeLists.txt | 1 + framework/domain/attachmentmodel.cpp | 141 +++++++++++++++++++++++++++++++++ framework/domain/messageparser.cpp | 7 ++ framework/domain/messageparser.h | 28 +++++++ framework/domain/messageparser_new.cpp | 3 +- 5 files changed, 179 insertions(+), 1 deletion(-) create mode 100644 framework/domain/attachmentmodel.cpp (limited to 'framework') diff --git a/framework/domain/CMakeLists.txt b/framework/domain/CMakeLists.txt index a31bb016..094eac04 100644 --- a/framework/domain/CMakeLists.txt +++ b/framework/domain/CMakeLists.txt @@ -1,4 +1,5 @@ set(mailplugin_SRCS + attachmentmodel.cpp mailplugin.cpp maillistmodel.cpp folderlistmodel.cpp diff --git a/framework/domain/attachmentmodel.cpp b/framework/domain/attachmentmodel.cpp new file mode 100644 index 00000000..35e94e2e --- /dev/null +++ b/framework/domain/attachmentmodel.cpp @@ -0,0 +1,141 @@ +/* + Copyright (c) 2016 Sandro Knauß + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by + the Free Software Foundation; either version 2 of the License, or (at your + option) any later version. + + This library is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public + License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to the + Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. +*/ + +#include "messageparser.h" +#include "mimetreeparser/interface.h" + +#include + +QString sizeHuman(const Content::Ptr &content) +{ + float num = content->content().size(); + QStringList list; + list << "KB" << "MB" << "GB" << "TB"; + + QStringListIterator i(list); + QString unit("Bytes"); + + while(num >= 1024.0 && i.hasNext()) + { + unit = i.next(); + num /= 1024.0; + } + + if (unit == "Bytes") { + return QString().setNum(num) + " " + unit; + } else { + return QString().setNum(num,'f',2)+" "+unit; + } +} + +class AttachmentModelPrivate +{ +public: + AttachmentModelPrivate(AttachmentModel *q_ptr, const std::shared_ptr &parser); + + AttachmentModel *q; + std::shared_ptr mParser; + QVector mAttachments; +}; + +AttachmentModelPrivate::AttachmentModelPrivate(AttachmentModel* q_ptr, const std::shared_ptr& parser) + : q(q_ptr) + , mParser(parser) +{ + mAttachments = mParser->collectAttachmentParts(); +} + +AttachmentModel::AttachmentModel(std::shared_ptr parser) + : d(std::unique_ptr(new AttachmentModelPrivate(this, parser))) +{ +} + +AttachmentModel::~AttachmentModel() +{ +} + +QHash AttachmentModel::roleNames() const +{ + QHash roles; + roles[TypeRole] = "type"; + roles[NameRole] = "name"; + roles[SizeRole] = "size"; + roles[IsEncryptedRole] = "encrypted"; + roles[IsSignedRole] = "signed"; + return roles; +} + +QModelIndex AttachmentModel::index(int row, int column, const QModelIndex &parent) const +{ + if (row < 0 || column != 0) { + return QModelIndex(); + } + + if (row < d->mAttachments.size()) { + return createIndex(row, column, d->mAttachments.at(row).get()); + } + return QModelIndex(); +} + +QVariant AttachmentModel::data(const QModelIndex &index, int role) const +{ + if (!index.isValid()) { + switch (role) { + case Qt::DisplayRole: + return QString("root"); + } + return QVariant(); + } + + if (index.internalPointer()) { + const auto entry = static_cast(index.internalPointer()); + const auto content = entry->content().at(0); + switch(role) { + case TypeRole: + return content->mailMime()->mimetype().name(); + case NameRole: + return entry->mailMime()->filename(); + case SizeRole: + return sizeHuman(content); + case IsEncryptedRole: + return content->encryptions().size() > 0; + case IsSignedRole: + return content->signatures().size() > 0; + } + } + return QVariant(); +} + +QModelIndex AttachmentModel::parent(const QModelIndex &index) const +{ + return QModelIndex(); +} + +int AttachmentModel::rowCount(const QModelIndex &parent) const +{ + if (!parent.isValid()) { + return d->mAttachments.size(); + } + return 0; +} + +int AttachmentModel::columnCount(const QModelIndex &parent) const +{ + return 1; +} diff --git a/framework/domain/messageparser.cpp b/framework/domain/messageparser.cpp index ec79d50b..2b8d3afe 100644 --- a/framework/domain/messageparser.cpp +++ b/framework/domain/messageparser.cpp @@ -105,3 +105,10 @@ QAbstractItemModel *MessageParser::newTree() const new ModelTest(model, model); return model; } + +QAbstractItemModel *MessageParser::attachments() const +{ + const auto model = new AttachmentModel(d->mParser); + new ModelTest(model, model); + return model; +} diff --git a/framework/domain/messageparser.h b/framework/domain/messageparser.h index 559fa6f9..203f7576 100644 --- a/framework/domain/messageparser.h +++ b/framework/domain/messageparser.h @@ -41,6 +41,7 @@ typedef std::shared_ptr ContentPtr; class MessagePartPrivate; class NewModelPrivate; +class AttachmentModelPrivate; class MessageParser : public QObject { @@ -49,6 +50,7 @@ class MessageParser : public QObject Q_PROPERTY (QString html READ html NOTIFY htmlChanged) Q_PROPERTY (QAbstractItemModel* partTree READ partTree NOTIFY htmlChanged) Q_PROPERTY (QAbstractItemModel* newTree READ newTree NOTIFY htmlChanged) + Q_PROPERTY (QAbstractItemModel* attachments READ attachments NOTIFY htmlChanged) public: explicit MessageParser(QObject *parent = Q_NULLPTR); @@ -60,6 +62,7 @@ public: void setMessage(const QVariant &to); QAbstractItemModel *partTree() const; QAbstractItemModel *newTree() const; + QAbstractItemModel *attachments() const; signals: void htmlChanged(); @@ -124,3 +127,28 @@ private: std::unique_ptr d; }; +class AttachmentModel : public QAbstractItemModel { + Q_OBJECT +public: + AttachmentModel(std::shared_ptr parser); + ~AttachmentModel(); + +public: + enum Roles { + TypeRole = Qt::UserRole + 1, + NameRole, + SizeRole, + IsEncryptedRole, + IsSignedRole + }; + + QHash roleNames() const Q_DECL_OVERRIDE; + QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE; + QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const Q_DECL_OVERRIDE; + QModelIndex parent(const QModelIndex &index) const Q_DECL_OVERRIDE; + int rowCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE; + int columnCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE; + +private: + std::unique_ptr d; +}; diff --git a/framework/domain/messageparser_new.cpp b/framework/domain/messageparser_new.cpp index b930f33d..8afd5956 100644 --- a/framework/domain/messageparser_new.cpp +++ b/framework/domain/messageparser_new.cpp @@ -1,5 +1,6 @@ - /* + Copyright (c) 2016 Sandro Knauß + This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your -- cgit v1.2.3