summaryrefslogtreecommitdiffstats
path: root/framework/domain/attachmentmodel.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'framework/domain/attachmentmodel.cpp')
-rw-r--r--framework/domain/attachmentmodel.cpp141
1 files changed, 141 insertions, 0 deletions
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 @@
1/*
2 Copyright (c) 2016 Sandro Knauß <knauss@kolabsys.com>
3
4 This library is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Library General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or (at your
7 option) any later version.
8
9 This library is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12 License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to the
16 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 02110-1301, USA.
18*/
19
20#include "messageparser.h"
21#include "mimetreeparser/interface.h"
22
23#include <QDebug>
24
25QString sizeHuman(const Content::Ptr &content)
26{
27 float num = content->content().size();
28 QStringList list;
29 list << "KB" << "MB" << "GB" << "TB";
30
31 QStringListIterator i(list);
32 QString unit("Bytes");
33
34 while(num >= 1024.0 && i.hasNext())
35 {
36 unit = i.next();
37 num /= 1024.0;
38 }
39
40 if (unit == "Bytes") {
41 return QString().setNum(num) + " " + unit;
42 } else {
43 return QString().setNum(num,'f',2)+" "+unit;
44 }
45}
46
47class AttachmentModelPrivate
48{
49public:
50 AttachmentModelPrivate(AttachmentModel *q_ptr, const std::shared_ptr<Parser> &parser);
51
52 AttachmentModel *q;
53 std::shared_ptr<Parser> mParser;
54 QVector<Part::Ptr> mAttachments;
55};
56
57AttachmentModelPrivate::AttachmentModelPrivate(AttachmentModel* q_ptr, const std::shared_ptr<Parser>& parser)
58 : q(q_ptr)
59 , mParser(parser)
60{
61 mAttachments = mParser->collectAttachmentParts();
62}
63
64AttachmentModel::AttachmentModel(std::shared_ptr<Parser> parser)
65 : d(std::unique_ptr<AttachmentModelPrivate>(new AttachmentModelPrivate(this, parser)))
66{
67}
68
69AttachmentModel::~AttachmentModel()
70{
71}
72
73QHash<int, QByteArray> AttachmentModel::roleNames() const
74{
75 QHash<int, QByteArray> roles;
76 roles[TypeRole] = "type";
77 roles[NameRole] = "name";
78 roles[SizeRole] = "size";
79 roles[IsEncryptedRole] = "encrypted";
80 roles[IsSignedRole] = "signed";
81 return roles;
82}
83
84QModelIndex AttachmentModel::index(int row, int column, const QModelIndex &parent) const
85{
86 if (row < 0 || column != 0) {
87 return QModelIndex();
88 }
89
90 if (row < d->mAttachments.size()) {
91 return createIndex(row, column, d->mAttachments.at(row).get());
92 }
93 return QModelIndex();
94}
95
96QVariant AttachmentModel::data(const QModelIndex &index, int role) const
97{
98 if (!index.isValid()) {
99 switch (role) {
100 case Qt::DisplayRole:
101 return QString("root");
102 }
103 return QVariant();
104 }
105
106 if (index.internalPointer()) {
107 const auto entry = static_cast<Part *>(index.internalPointer());
108 const auto content = entry->content().at(0);
109 switch(role) {
110 case TypeRole:
111 return content->mailMime()->mimetype().name();
112 case NameRole:
113 return entry->mailMime()->filename();
114 case SizeRole:
115 return sizeHuman(content);
116 case IsEncryptedRole:
117 return content->encryptions().size() > 0;
118 case IsSignedRole:
119 return content->signatures().size() > 0;
120 }
121 }
122 return QVariant();
123}
124
125QModelIndex AttachmentModel::parent(const QModelIndex &index) const
126{
127 return QModelIndex();
128}
129
130int AttachmentModel::rowCount(const QModelIndex &parent) const
131{
132 if (!parent.isValid()) {
133 return d->mAttachments.size();
134 }
135 return 0;
136}
137
138int AttachmentModel::columnCount(const QModelIndex &parent) const
139{
140 return 1;
141}