diff options
Diffstat (limited to 'framework/src/domain/attachmentmodel.cpp')
-rw-r--r-- | framework/src/domain/attachmentmodel.cpp | 145 |
1 files changed, 145 insertions, 0 deletions
diff --git a/framework/src/domain/attachmentmodel.cpp b/framework/src/domain/attachmentmodel.cpp new file mode 100644 index 00000000..e98c6fc0 --- /dev/null +++ b/framework/src/domain/attachmentmodel.cpp | |||
@@ -0,0 +1,145 @@ | |||
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 <QIcon> | ||
24 | #include <QDebug> | ||
25 | |||
26 | QString sizeHuman(const Content::Ptr &content) | ||
27 | { | ||
28 | float num = content->content().size(); | ||
29 | QStringList list; | ||
30 | list << "KB" << "MB" << "GB" << "TB"; | ||
31 | |||
32 | QStringListIterator i(list); | ||
33 | QString unit("Bytes"); | ||
34 | |||
35 | while(num >= 1024.0 && i.hasNext()) | ||
36 | { | ||
37 | unit = i.next(); | ||
38 | num /= 1024.0; | ||
39 | } | ||
40 | |||
41 | if (unit == "Bytes") { | ||
42 | return QString().setNum(num) + " " + unit; | ||
43 | } else { | ||
44 | return QString().setNum(num,'f',2)+" "+unit; | ||
45 | } | ||
46 | } | ||
47 | |||
48 | class AttachmentModelPrivate | ||
49 | { | ||
50 | public: | ||
51 | AttachmentModelPrivate(AttachmentModel *q_ptr, const std::shared_ptr<Parser> &parser); | ||
52 | |||
53 | AttachmentModel *q; | ||
54 | std::shared_ptr<Parser> mParser; | ||
55 | QVector<Part::Ptr> mAttachments; | ||
56 | }; | ||
57 | |||
58 | AttachmentModelPrivate::AttachmentModelPrivate(AttachmentModel* q_ptr, const std::shared_ptr<Parser>& parser) | ||
59 | : q(q_ptr) | ||
60 | , mParser(parser) | ||
61 | { | ||
62 | mAttachments = mParser->collectAttachmentParts(); | ||
63 | } | ||
64 | |||
65 | AttachmentModel::AttachmentModel(std::shared_ptr<Parser> parser) | ||
66 | : d(std::unique_ptr<AttachmentModelPrivate>(new AttachmentModelPrivate(this, parser))) | ||
67 | { | ||
68 | } | ||
69 | |||
70 | AttachmentModel::~AttachmentModel() | ||
71 | { | ||
72 | } | ||
73 | |||
74 | QHash<int, QByteArray> AttachmentModel::roleNames() const | ||
75 | { | ||
76 | QHash<int, QByteArray> roles; | ||
77 | roles[TypeRole] = "type"; | ||
78 | roles[NameRole] = "name"; | ||
79 | roles[SizeRole] = "size"; | ||
80 | roles[IconRole] = "icon"; | ||
81 | roles[IsEncryptedRole] = "encrypted"; | ||
82 | roles[IsSignedRole] = "signed"; | ||
83 | return roles; | ||
84 | } | ||
85 | |||
86 | QModelIndex AttachmentModel::index(int row, int column, const QModelIndex &parent) const | ||
87 | { | ||
88 | if (row < 0 || column != 0) { | ||
89 | return QModelIndex(); | ||
90 | } | ||
91 | |||
92 | if (row < d->mAttachments.size()) { | ||
93 | return createIndex(row, column, d->mAttachments.at(row).get()); | ||
94 | } | ||
95 | return QModelIndex(); | ||
96 | } | ||
97 | |||
98 | QVariant AttachmentModel::data(const QModelIndex &index, int role) const | ||
99 | { | ||
100 | if (!index.isValid()) { | ||
101 | switch (role) { | ||
102 | case Qt::DisplayRole: | ||
103 | return QString("root"); | ||
104 | } | ||
105 | return QVariant(); | ||
106 | } | ||
107 | |||
108 | if (index.internalPointer()) { | ||
109 | const auto entry = static_cast<Part *>(index.internalPointer()); | ||
110 | const auto content = entry->content().at(0); | ||
111 | switch(role) { | ||
112 | case TypeRole: | ||
113 | return content->mailMime()->mimetype().name(); | ||
114 | case NameRole: | ||
115 | return entry->mailMime()->filename(); | ||
116 | case IconRole: | ||
117 | return QIcon::fromTheme(content->mailMime()->mimetype().iconName()); | ||
118 | case SizeRole: | ||
119 | return sizeHuman(content); | ||
120 | case IsEncryptedRole: | ||
121 | return content->encryptions().size() > 0; | ||
122 | case IsSignedRole: | ||
123 | return content->signatures().size() > 0; | ||
124 | } | ||
125 | } | ||
126 | return QVariant(); | ||
127 | } | ||
128 | |||
129 | QModelIndex AttachmentModel::parent(const QModelIndex &index) const | ||
130 | { | ||
131 | return QModelIndex(); | ||
132 | } | ||
133 | |||
134 | int AttachmentModel::rowCount(const QModelIndex &parent) const | ||
135 | { | ||
136 | if (!parent.isValid()) { | ||
137 | return d->mAttachments.size(); | ||
138 | } | ||
139 | return 0; | ||
140 | } | ||
141 | |||
142 | int AttachmentModel::columnCount(const QModelIndex &parent) const | ||
143 | { | ||
144 | return 1; | ||
145 | } | ||