diff options
author | Sandro Knauß <sknauss@kde.org> | 2016-10-18 15:30:11 +0200 |
---|---|---|
committer | Sandro Knauß <sknauss@kde.org> | 2016-10-18 15:30:11 +0200 |
commit | d708e1310cc8e65fab078eb6b5b4a325de462a24 (patch) | |
tree | 48ceae94d176776da5b1836e4ef454f0edb3c3a0 | |
parent | 0fa8aa51aac15233820b9f9c576584e6ff8ee151 (diff) | |
download | kube-d708e1310cc8e65fab078eb6b5b4a325de462a24.tar.gz kube-d708e1310cc8e65fab078eb6b5b4a325de462a24.zip |
start implementing a nested model with sig->enc->part->content
-rw-r--r-- | framework/domain/messageparser.h | 9 | ||||
-rw-r--r-- | framework/domain/messageparser_new.cpp | 313 |
2 files changed, 302 insertions, 20 deletions
diff --git a/framework/domain/messageparser.h b/framework/domain/messageparser.h index 5eb355e7..e3b81dd7 100644 --- a/framework/domain/messageparser.h +++ b/framework/domain/messageparser.h | |||
@@ -33,11 +33,15 @@ class QAbstractItemModel; | |||
33 | 33 | ||
34 | class Parser; | 34 | class Parser; |
35 | class Part; | 35 | class Part; |
36 | class Encryption; | ||
37 | class Signature; | ||
36 | typedef std::shared_ptr<Part> PartPtr; | 38 | typedef std::shared_ptr<Part> PartPtr; |
37 | class Content; | 39 | class Content; |
38 | typedef std::shared_ptr<Content> ContentPtr; | 40 | typedef std::shared_ptr<Content> ContentPtr; |
39 | class MessagePartPrivate; | 41 | class MessagePartPrivate; |
40 | 42 | ||
43 | class NewModelPrivate; | ||
44 | |||
41 | class MessageParser : public QObject | 45 | class MessageParser : public QObject |
42 | { | 46 | { |
43 | Q_OBJECT | 47 | Q_OBJECT |
@@ -123,6 +127,7 @@ class NewModel : public QAbstractItemModel { | |||
123 | Q_OBJECT | 127 | Q_OBJECT |
124 | public: | 128 | public: |
125 | NewModel(std::shared_ptr<Parser> parser); | 129 | NewModel(std::shared_ptr<Parser> parser); |
130 | ~NewModel(); | ||
126 | 131 | ||
127 | public: | 132 | public: |
128 | enum Roles { | 133 | enum Roles { |
@@ -140,8 +145,6 @@ public: | |||
140 | int columnCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE; | 145 | int columnCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE; |
141 | 146 | ||
142 | private: | 147 | private: |
143 | std::shared_ptr<Parser> mParser; | 148 | std::unique_ptr<NewModelPrivate> d; |
144 | QVector<PartPtr> mParts; | ||
145 | QMap<Part *, std::shared_ptr<NewContentModel>> mContentMap; | ||
146 | }; | 149 | }; |
147 | 150 | ||
diff --git a/framework/domain/messageparser_new.cpp b/framework/domain/messageparser_new.cpp index 4395f2e3..ffe7550f 100644 --- a/framework/domain/messageparser_new.cpp +++ b/framework/domain/messageparser_new.cpp | |||
@@ -21,8 +21,38 @@ | |||
21 | 21 | ||
22 | #include <QDebug> | 22 | #include <QDebug> |
23 | 23 | ||
24 | NewModel::NewModel(std::shared_ptr<Parser> parser) | 24 | Q_DECLARE_METATYPE(Part *) |
25 | : mParser(parser) | 25 | Q_DECLARE_METATYPE(Content *) |
26 | Q_DECLARE_METATYPE(Signature *) | ||
27 | Q_DECLARE_METATYPE(Encryption *) | ||
28 | |||
29 | class NewModelPrivate | ||
30 | { | ||
31 | public: | ||
32 | NewModelPrivate(NewModel *q_ptr, const std::shared_ptr<Parser> &parser); | ||
33 | |||
34 | QSharedPointer<QVariant> getVar(const std::shared_ptr<Signature> &sig); | ||
35 | QSharedPointer<QVariant> getVar(const std::shared_ptr<Encryption> &enc); | ||
36 | QSharedPointer<QVariant> getVar(const std::shared_ptr<Part> &part); | ||
37 | |||
38 | int getPos(Signature *sig); | ||
39 | int getPos(Encryption *enc); | ||
40 | int getPos(Part *part); | ||
41 | |||
42 | NewModel *q; | ||
43 | QVector<Part::Ptr> mParts; | ||
44 | |||
45 | std::shared_ptr<Parser> mParser; | ||
46 | QMap<Part *, std::shared_ptr<NewContentModel>> mContentMap; | ||
47 | private: | ||
48 | QMap<std::shared_ptr<Signature>, QSharedPointer<QVariant>> mSignatureMap; | ||
49 | QMap<std::shared_ptr<Encryption>, QSharedPointer<QVariant>> mEncryptionMap; | ||
50 | QMap<std::shared_ptr<Part>, QSharedPointer<QVariant>> mPartMap; | ||
51 | }; | ||
52 | |||
53 | NewModelPrivate::NewModelPrivate(NewModel *q_ptr, const std::shared_ptr<Parser> &parser) | ||
54 | : q(q_ptr) | ||
55 | , mParser(parser) | ||
26 | { | 56 | { |
27 | mParts = mParser->collectContentParts(); | 57 | mParts = mParser->collectContentParts(); |
28 | foreach(const auto &part, mParts) { | 58 | foreach(const auto &part, mParts) { |
@@ -30,6 +60,84 @@ NewModel::NewModel(std::shared_ptr<Parser> parser) | |||
30 | } | 60 | } |
31 | } | 61 | } |
32 | 62 | ||
63 | QSharedPointer<QVariant> NewModelPrivate::getVar(const std::shared_ptr<Signature> &sig) | ||
64 | { | ||
65 | if (!mSignatureMap.contains(sig)) { | ||
66 | auto var = new QVariant(); | ||
67 | var->setValue(sig.get()); | ||
68 | mSignatureMap.insert(sig, QSharedPointer<QVariant>(var)); | ||
69 | } | ||
70 | return mSignatureMap.value(sig); | ||
71 | } | ||
72 | |||
73 | QSharedPointer<QVariant> NewModelPrivate::getVar(const std::shared_ptr<Encryption> &enc) | ||
74 | { | ||
75 | if (!mEncryptionMap.contains(enc)) { | ||
76 | auto var = new QVariant(); | ||
77 | var->setValue(enc.get()); | ||
78 | mEncryptionMap.insert(enc, QSharedPointer<QVariant>(var)); | ||
79 | } | ||
80 | return mEncryptionMap.value(enc); | ||
81 | } | ||
82 | |||
83 | QSharedPointer<QVariant> NewModelPrivate::getVar(const std::shared_ptr<Part> &part) | ||
84 | { | ||
85 | if (!mPartMap.contains(part)) { | ||
86 | auto var = new QVariant(); | ||
87 | var->setValue(part.get()); | ||
88 | mPartMap.insert(part, QSharedPointer<QVariant>(var)); | ||
89 | } | ||
90 | return mPartMap.value(part); | ||
91 | } | ||
92 | |||
93 | int NewModelPrivate::getPos(Signature *signature) | ||
94 | { | ||
95 | const auto first = mParts.first(); | ||
96 | int i = 0; | ||
97 | foreach(const auto &sig, first->signatures()) { | ||
98 | if (sig.get() == signature) { | ||
99 | break; | ||
100 | } | ||
101 | i++; | ||
102 | } | ||
103 | return i; | ||
104 | } | ||
105 | |||
106 | int NewModelPrivate::getPos(Encryption *encryption) | ||
107 | { | ||
108 | const auto first = mParts.first(); | ||
109 | int i = 0; | ||
110 | foreach(const auto &enc, first->encryptions()) { | ||
111 | if (enc.get() == encryption) { | ||
112 | break; | ||
113 | } | ||
114 | i++; | ||
115 | } | ||
116 | return i; | ||
117 | } | ||
118 | |||
119 | |||
120 | int NewModelPrivate::getPos(Part *part) | ||
121 | { | ||
122 | int i = 0; | ||
123 | foreach(const auto &p, mParts) { | ||
124 | if (p.get() == part) { | ||
125 | break; | ||
126 | } | ||
127 | i++; | ||
128 | } | ||
129 | return i; | ||
130 | } | ||
131 | |||
132 | NewModel::NewModel(std::shared_ptr<Parser> parser) | ||
133 | : d(std::unique_ptr<NewModelPrivate>(new NewModelPrivate(this, parser))) | ||
134 | { | ||
135 | } | ||
136 | |||
137 | NewModel::~NewModel() | ||
138 | { | ||
139 | } | ||
140 | |||
33 | QHash<int, QByteArray> NewModel::roleNames() const | 141 | QHash<int, QByteArray> NewModel::roleNames() const |
34 | { | 142 | { |
35 | QHash<int, QByteArray> roles; | 143 | QHash<int, QByteArray> roles; |
@@ -40,12 +148,71 @@ QHash<int, QByteArray> NewModel::roleNames() const | |||
40 | return roles; | 148 | return roles; |
41 | } | 149 | } |
42 | 150 | ||
151 | |||
43 | QModelIndex NewModel::index(int row, int column, const QModelIndex &parent) const | 152 | QModelIndex NewModel::index(int row, int column, const QModelIndex &parent) const |
44 | { | 153 | { |
154 | if (row < 0 || column != 0) { | ||
155 | return QModelIndex(); | ||
156 | } | ||
45 | if (!parent.isValid()) { | 157 | if (!parent.isValid()) { |
46 | if (row < mParts.size()) { | 158 | const auto first = d->mParts.first(); |
47 | auto part = mParts.at(row); | 159 | if (first->signatures().size() > 0) { |
48 | return createIndex(row, column, part.get()); | 160 | if (row == 0) { |
161 | const auto sig = first->signatures().at(row); | ||
162 | return createIndex(row, column, d->getVar(sig).data()); | ||
163 | } | ||
164 | } else if (first->encryptions().size() > 0) { | ||
165 | if (row == 0) { | ||
166 | const auto enc = first->encryptions().at(row); | ||
167 | return createIndex(row, column, d->getVar(enc).data()); | ||
168 | } | ||
169 | } else { | ||
170 | if (row < d->mParts.size()) { | ||
171 | auto part = d->mParts.at(row); | ||
172 | return createIndex(row, column, d->getVar(part).data()); | ||
173 | } | ||
174 | } | ||
175 | } else { | ||
176 | if (!parent.internalPointer()) { | ||
177 | return QModelIndex(); | ||
178 | } | ||
179 | const auto data = static_cast<QVariant *>(parent.internalPointer()); | ||
180 | const auto first = d->mParts.first(); | ||
181 | int encpos = -1; | ||
182 | int partpos = -1; | ||
183 | if (data->userType() == qMetaTypeId<Signature *>()) { | ||
184 | const auto signature = data->value<Signature *>(); | ||
185 | int i = d->getPos(signature); | ||
186 | |||
187 | if (i+1 < first->signatures().size()) { | ||
188 | if (row != 0) { | ||
189 | return QModelIndex(); | ||
190 | } | ||
191 | const auto sig = first->signatures().at(i+1); | ||
192 | return createIndex(0,0, d->getVar(sig).data()); | ||
193 | } | ||
194 | |||
195 | if (first->encryptions().size() > 0) { | ||
196 | encpos = 0; | ||
197 | } | ||
198 | } else if (data->userType() == qMetaTypeId<Encryption *>()) { | ||
199 | const auto encryption = data->value<Encryption *>(); | ||
200 | encpos = d->getPos(encryption) + 1; | ||
201 | } | ||
202 | |||
203 | if (encpos > -1 && encpos < first->encryptions().size()) { | ||
204 | if (row != 0) { | ||
205 | return QModelIndex(); | ||
206 | } | ||
207 | const auto enc = first->encryptions().at(encpos); | ||
208 | return createIndex(0,0, d->getVar(enc).data()); | ||
209 | } | ||
210 | |||
211 | if (row < d->mParts.size()) { | ||
212 | auto part = d->mParts.at(row); | ||
213 | auto var = new QVariant(); | ||
214 | var->setValue(part.get()); | ||
215 | return createIndex(row, column, d->getVar(part).data()); | ||
49 | } | 216 | } |
50 | } | 217 | } |
51 | return QModelIndex(); | 218 | return QModelIndex(); |
@@ -53,17 +220,44 @@ QModelIndex NewModel::index(int row, int column, const QModelIndex &parent) cons | |||
53 | 220 | ||
54 | QVariant NewModel::data(const QModelIndex &index, int role) const | 221 | QVariant NewModel::data(const QModelIndex &index, int role) const |
55 | { | 222 | { |
56 | if (!index.parent().isValid()) { | 223 | if (!index.isValid()) { |
57 | auto part = static_cast<Part *>(index.internalPointer()); | 224 | if (role == Qt::DisplayRole) { |
58 | switch (role) { | 225 | return QString("root"); |
59 | case TypeRole: | 226 | } |
60 | return QString::fromLatin1(part->type()); | 227 | return QVariant(); |
61 | case IsEmbededRole: | 228 | } |
62 | return index.parent().isValid(); | 229 | if (index.internalPointer()) { |
63 | case SecurityLevelRole: | 230 | const auto data = static_cast<QVariant *>(index.internalPointer()); |
64 | return QStringLiteral("GRAY"); | 231 | if (data->userType() == qMetaTypeId<Signature *>()) { |
65 | case ContentsRole: | 232 | const auto signature = data->value<Signature *>(); |
66 | return QVariant::fromValue<QAbstractItemModel *>(mContentMap.value(part).get()); | 233 | int i = d->getPos(signature); |
234 | switch(role) { | ||
235 | case Qt::DisplayRole: | ||
236 | case TypeRole: | ||
237 | return QStringLiteral("Signature%1").arg(i); | ||
238 | } | ||
239 | } else if (data->userType() == qMetaTypeId<Encryption *>()) { | ||
240 | const auto first = d->mParts.first(); | ||
241 | const auto encryption = data->value<Encryption *>(); | ||
242 | int i = d->getPos(encryption); | ||
243 | switch(role) { | ||
244 | case Qt::DisplayRole: | ||
245 | case TypeRole: | ||
246 | return QStringLiteral("Encryption%1").arg(i); | ||
247 | } | ||
248 | } else if (data->userType() == qMetaTypeId<Part *>()) { | ||
249 | const auto part = data->value<Part *>(); | ||
250 | switch (role) { | ||
251 | case Qt::DisplayRole: | ||
252 | case TypeRole: | ||
253 | return QString::fromLatin1(part->type()); | ||
254 | case IsEmbededRole: | ||
255 | return index.parent().isValid(); | ||
256 | case SecurityLevelRole: | ||
257 | return QStringLiteral("GRAY"); | ||
258 | case ContentsRole: | ||
259 | return QVariant::fromValue<QAbstractItemModel *>(d->mContentMap.value(part).get()); | ||
260 | } | ||
67 | } | 261 | } |
68 | } | 262 | } |
69 | return QVariant(); | 263 | return QVariant(); |
@@ -71,13 +265,98 @@ QVariant NewModel::data(const QModelIndex &index, int role) const | |||
71 | 265 | ||
72 | QModelIndex NewModel::parent(const QModelIndex &index) const | 266 | QModelIndex NewModel::parent(const QModelIndex &index) const |
73 | { | 267 | { |
268 | if (!index.internalPointer()) { | ||
269 | return QModelIndex(); | ||
270 | } | ||
271 | const auto data = static_cast<QVariant *>(index.internalPointer()); | ||
272 | if (data->userType() == qMetaTypeId<Signature *>()) { | ||
273 | const auto signature = data->value<Signature *>(); | ||
274 | const auto first = d->mParts.first(); | ||
275 | int i = d->getPos(signature); | ||
276 | |||
277 | if (i > 1) { | ||
278 | const auto sig = first->signatures().at(i-1); | ||
279 | return createIndex(0, 0, d->getVar(sig).data()); | ||
280 | } | ||
281 | |||
282 | return QModelIndex(); | ||
283 | } else if (data->userType() == qMetaTypeId<Encryption *>()) { | ||
284 | const auto encryption = data->value<Encryption *>(); | ||
285 | const auto first = d->mParts.first(); | ||
286 | int i = d->getPos(encryption); | ||
287 | |||
288 | if (i > 1) { | ||
289 | const auto enc = first->encryptions().at(i-1); | ||
290 | return createIndex(0, 0, d->getVar(enc).data()); | ||
291 | } | ||
292 | |||
293 | if (first->signatures().size() > 0) { | ||
294 | const int row = first->signatures().size() - 1; | ||
295 | const auto sig = first->signatures().at(row); | ||
296 | return createIndex(0, 0, d->getVar(sig).data()); | ||
297 | } else { | ||
298 | return QModelIndex(); | ||
299 | } | ||
300 | } else if (data->userType() == qMetaTypeId<Part *>()) { | ||
301 | const auto first = d->mParts.first(); | ||
302 | if (first->encryptions().size() > 0) { | ||
303 | const int row = first->encryptions().size() - 1; | ||
304 | const auto enc = first->encryptions().at(row); | ||
305 | auto var = new QVariant(); | ||
306 | var->setValue(enc.get()); | ||
307 | return createIndex(0, 0, d->getVar(enc).data()); | ||
308 | } | ||
309 | if (first->signatures().size() > 0) { | ||
310 | const int row = first->signatures().size() - 1; | ||
311 | const auto sig = first->signatures().at(row); | ||
312 | return createIndex(0, 0, d->getVar(sig).data()); | ||
313 | } | ||
314 | return QModelIndex(); | ||
315 | } | ||
74 | return QModelIndex(); | 316 | return QModelIndex(); |
75 | } | 317 | } |
76 | 318 | ||
77 | int NewModel::rowCount(const QModelIndex &parent) const | 319 | int NewModel::rowCount(const QModelIndex &parent) const |
78 | { | 320 | { |
79 | if (!parent.isValid()) { | 321 | if (!parent.isValid()) { |
80 | return mParts.size(); | 322 | const auto first = d->mParts.first(); |
323 | if (first->signatures().size() > 0) { | ||
324 | return 1; | ||
325 | } else if (first->encryptions().size() > 0) { | ||
326 | return 1; | ||
327 | } else { | ||
328 | return d->mParts.size(); | ||
329 | } | ||
330 | } else { | ||
331 | if (!parent.internalPointer()) { | ||
332 | return 0; | ||
333 | } | ||
334 | const auto data = static_cast<QVariant *>(parent.internalPointer()); | ||
335 | if (data->userType() == qMetaTypeId<Signature *>()) { | ||
336 | const auto signature = data->value<Signature *>(); | ||
337 | const auto first = d->mParts.first(); | ||
338 | int i = d->getPos(signature); | ||
339 | |||
340 | if (i+1 < first->signatures().size()) { | ||
341 | return 1; | ||
342 | } | ||
343 | |||
344 | if (first->encryptions().size() > 0) { | ||
345 | return 1; | ||
346 | } | ||
347 | |||
348 | return d->mParts.size(); | ||
349 | } else if (data->userType() == qMetaTypeId<Encryption *>()) { | ||
350 | const auto encryption = data->value<Encryption *>(); | ||
351 | const auto first = d->mParts.first(); | ||
352 | int i = d->getPos(encryption); | ||
353 | |||
354 | if (i+1 < first->encryptions().size()) { | ||
355 | return 1; | ||
356 | } | ||
357 | |||
358 | return d->mParts.size(); | ||
359 | } | ||
81 | } | 360 | } |
82 | return 0; | 361 | return 0; |
83 | } | 362 | } |