diff options
Diffstat (limited to 'framework/domain/messageparser_new.cpp')
-rw-r--r-- | framework/domain/messageparser_new.cpp | 292 |
1 files changed, 132 insertions, 160 deletions
diff --git a/framework/domain/messageparser_new.cpp b/framework/domain/messageparser_new.cpp index 44cd6b6b..2a3b68e9 100644 --- a/framework/domain/messageparser_new.cpp +++ b/framework/domain/messageparser_new.cpp | |||
@@ -26,11 +26,54 @@ Q_DECLARE_METATYPE(Content *) | |||
26 | Q_DECLARE_METATYPE(Signature *) | 26 | Q_DECLARE_METATYPE(Signature *) |
27 | Q_DECLARE_METATYPE(Encryption *) | 27 | Q_DECLARE_METATYPE(Encryption *) |
28 | 28 | ||
29 | class Entry | ||
30 | { | ||
31 | public: | ||
32 | ~Entry() | ||
33 | { | ||
34 | foreach(auto child, mChildren) { | ||
35 | delete child; | ||
36 | } | ||
37 | mChildren.clear(); | ||
38 | } | ||
39 | |||
40 | void addChild(Entry *entry) | ||
41 | { | ||
42 | mChildren.append(entry); | ||
43 | entry->mParent = this; | ||
44 | } | ||
45 | |||
46 | int pos() | ||
47 | { | ||
48 | if(!mParent) { | ||
49 | return -1; | ||
50 | } | ||
51 | int i=0; | ||
52 | foreach(const auto &child, mParent->mChildren) { | ||
53 | if (child == this) { | ||
54 | return i; | ||
55 | } | ||
56 | i++; | ||
57 | } | ||
58 | return -1; | ||
59 | } | ||
60 | |||
61 | QSharedPointer<QVariant> mData; | ||
62 | |||
63 | Entry *mParent; | ||
64 | QVector<Entry *> mChildren; | ||
65 | }; | ||
66 | |||
29 | class NewModelPrivate | 67 | class NewModelPrivate |
30 | { | 68 | { |
31 | public: | 69 | public: |
32 | NewModelPrivate(NewModel *q_ptr, const std::shared_ptr<Parser> &parser); | 70 | NewModelPrivate(NewModel *q_ptr, const std::shared_ptr<Parser> &parser); |
33 | 71 | ||
72 | void createTree(); | ||
73 | Entry *addSignatures(Entry *parent, QVector<Signature::Ptr> signatures); | ||
74 | Entry *addEncryptions(Entry *parent, QVector<Encryption::Ptr> encryptions); | ||
75 | Entry *addPart(Entry *parent, Part *part); | ||
76 | |||
34 | QSharedPointer<QVariant> getVar(const std::shared_ptr<Signature> &sig); | 77 | QSharedPointer<QVariant> getVar(const std::shared_ptr<Signature> &sig); |
35 | QSharedPointer<QVariant> getVar(const std::shared_ptr<Encryption> &enc); | 78 | QSharedPointer<QVariant> getVar(const std::shared_ptr<Encryption> &enc); |
36 | QSharedPointer<QVariant> getVar(const std::shared_ptr<Part> &part); | 79 | QSharedPointer<QVariant> getVar(const std::shared_ptr<Part> &part); |
@@ -45,6 +88,7 @@ public: | |||
45 | 88 | ||
46 | NewModel *q; | 89 | NewModel *q; |
47 | QVector<Part::Ptr> mParts; | 90 | QVector<Part::Ptr> mParts; |
91 | Entry *mRoot; | ||
48 | 92 | ||
49 | std::shared_ptr<Parser> mParser; | 93 | std::shared_ptr<Parser> mParser; |
50 | QMap<Part *, std::shared_ptr<NewContentModel>> mContentMap; | 94 | QMap<Part *, std::shared_ptr<NewContentModel>> mContentMap; |
@@ -63,6 +107,79 @@ NewModelPrivate::NewModelPrivate(NewModel *q_ptr, const std::shared_ptr<Parser> | |||
63 | foreach(const auto &part, mParts) { | 107 | foreach(const auto &part, mParts) { |
64 | mContentMap.insert(part.get(), std::shared_ptr<NewContentModel>(new NewContentModel(part, mParser))); | 108 | mContentMap.insert(part.get(), std::shared_ptr<NewContentModel>(new NewContentModel(part, mParser))); |
65 | } | 109 | } |
110 | createTree(); | ||
111 | } | ||
112 | |||
113 | Entry * NewModelPrivate::addSignatures(Entry *parent, QVector<Signature::Ptr> signatures) | ||
114 | { | ||
115 | auto ret = parent; | ||
116 | foreach(const auto &sig, signatures) { | ||
117 | auto entry = new Entry(); | ||
118 | entry->mData = getVar(sig); | ||
119 | ret = entry; | ||
120 | parent->addChild(entry); | ||
121 | } | ||
122 | return ret; | ||
123 | } | ||
124 | |||
125 | Entry * NewModelPrivate::addEncryptions(Entry *parent, QVector<Encryption::Ptr> encryptions) | ||
126 | { | ||
127 | auto ret = parent; | ||
128 | foreach(const auto &enc, encryptions) { | ||
129 | auto entry = new Entry(); | ||
130 | entry->mData = getVar(enc); | ||
131 | parent->addChild(entry); | ||
132 | ret = entry; | ||
133 | } | ||
134 | return ret; | ||
135 | } | ||
136 | |||
137 | Entry * NewModelPrivate::addPart(Entry *parent, Part *part) | ||
138 | { | ||
139 | auto entry = new Entry(); | ||
140 | entry->mData = getVar(part); | ||
141 | parent->addChild(entry); | ||
142 | |||
143 | foreach(const auto &content, part->content()) { | ||
144 | auto _entry = entry; | ||
145 | _entry = addSignatures(_entry, content->signatures().mid(part->signatures().size())); | ||
146 | _entry = addEncryptions(_entry, content->encryptions().mid(part->encryptions().size())); | ||
147 | auto c = new Entry(); | ||
148 | c->mData = getVar(content); | ||
149 | _entry->addChild(c); | ||
150 | } | ||
151 | |||
152 | foreach(const auto &sp, part->subParts()) { | ||
153 | auto _entry = entry; | ||
154 | _entry = addSignatures(_entry, sp->signatures().mid(part->signatures().size())); | ||
155 | _entry = addEncryptions(_entry, sp->encryptions().mid(part->encryptions().size())); | ||
156 | addPart(_entry, sp.get()); | ||
157 | } | ||
158 | return entry; | ||
159 | } | ||
160 | |||
161 | void NewModelPrivate::createTree() | ||
162 | { | ||
163 | mRoot = new Entry(); | ||
164 | auto parent = mRoot; | ||
165 | Part *pPart = nullptr; | ||
166 | QVector<Signature::Ptr> signatures; | ||
167 | QVector<Encryption::Ptr> encryptions; | ||
168 | foreach(const auto part, mParts) { | ||
169 | auto _parent = parent; | ||
170 | if (pPart != part->parent()) { | ||
171 | auto _parent = mRoot; | ||
172 | _parent = addSignatures(_parent, part->parent()->signatures()); | ||
173 | _parent = addEncryptions(_parent, part->parent()->encryptions()); | ||
174 | signatures = part->parent()->signatures(); | ||
175 | encryptions = part->parent()->encryptions(); | ||
176 | parent = _parent; | ||
177 | pPart = part->parent(); | ||
178 | } | ||
179 | _parent = addSignatures(_parent, part->signatures().mid(signatures.size())); | ||
180 | _parent = addEncryptions(_parent, part->encryptions().mid(encryptions.size())); | ||
181 | addPart(_parent, part.get()); | ||
182 | } | ||
66 | } | 183 | } |
67 | 184 | ||
68 | QSharedPointer<QVariant> NewModelPrivate::getVar(const std::shared_ptr<Signature> &sig) | 185 | QSharedPointer<QVariant> NewModelPrivate::getVar(const std::shared_ptr<Signature> &sig) |
@@ -192,73 +309,13 @@ QModelIndex NewModel::index(int row, int column, const QModelIndex &parent) cons | |||
192 | if (row < 0 || column != 0) { | 309 | if (row < 0 || column != 0) { |
193 | return QModelIndex(); | 310 | return QModelIndex(); |
194 | } | 311 | } |
195 | if (!parent.isValid()) { | 312 | Entry *entry = d->mRoot; |
196 | const auto first = d->mParts.first(); | 313 | if (parent.isValid()) { |
197 | if (first->signatures().size() > 0) { | 314 | entry = static_cast<Entry *>(parent.internalPointer()); |
198 | if (row == 0) { | 315 | } |
199 | const auto sig = first->signatures().at(row); | ||
200 | return createIndex(row, column, d->getVar(sig).data()); | ||
201 | } | ||
202 | } else if (first->encryptions().size() > 0) { | ||
203 | if (row == 0) { | ||
204 | const auto enc = first->encryptions().at(row); | ||
205 | return createIndex(row, column, d->getVar(enc).data()); | ||
206 | } | ||
207 | } else { | ||
208 | if (row < d->mParts.size()) { | ||
209 | auto part = d->mParts.at(row); | ||
210 | return createIndex(row, column, d->getVar(part).data()); | ||
211 | } | ||
212 | } | ||
213 | } else { | ||
214 | if (!parent.internalPointer()) { | ||
215 | return QModelIndex(); | ||
216 | } | ||
217 | const auto data = static_cast<QVariant *>(parent.internalPointer()); | ||
218 | const auto first = d->mParts.first(); | ||
219 | int encpos = -1; | ||
220 | int partpos = -1; | ||
221 | if (data->userType() == qMetaTypeId<Signature *>()) { | ||
222 | const auto signature = data->value<Signature *>(); | ||
223 | int i = d->getPos(signature); | ||
224 | |||
225 | if (i+1 < first->signatures().size()) { | ||
226 | if (row != 0) { | ||
227 | return QModelIndex(); | ||
228 | } | ||
229 | const auto sig = first->signatures().at(i+1); | ||
230 | return createIndex(0,0, d->getVar(sig).data()); | ||
231 | } | ||
232 | |||
233 | if (first->encryptions().size() > 0) { | ||
234 | encpos = 0; | ||
235 | } | ||
236 | } else if (data->userType() == qMetaTypeId<Encryption *>()) { | ||
237 | const auto encryption = data->value<Encryption *>(); | ||
238 | encpos = d->getPos(encryption) + 1; | ||
239 | } else if (data->userType() == qMetaTypeId<Part *>()) { | ||
240 | const auto part = data->value<Part *>(); | ||
241 | if (row < part->content().size()) { | ||
242 | auto c = part->content().at(row); | ||
243 | return createIndex(row, column, d->getVar(c).data()); | ||
244 | } | ||
245 | return QModelIndex(); | ||
246 | } | ||
247 | |||
248 | if (encpos > -1 && encpos < first->encryptions().size()) { | ||
249 | if (row != 0) { | ||
250 | return QModelIndex(); | ||
251 | } | ||
252 | const auto enc = first->encryptions().at(encpos); | ||
253 | return createIndex(0,0, d->getVar(enc).data()); | ||
254 | } | ||
255 | 316 | ||
256 | if (row < d->mParts.size()) { | 317 | if (row < entry->mChildren.size()) { |
257 | auto part = d->mParts.at(row); | 318 | return createIndex(row, column, entry->mChildren.at(row)); |
258 | auto var = new QVariant(); | ||
259 | var->setValue(part.get()); | ||
260 | return createIndex(row, column, d->getVar(part).data()); | ||
261 | } | ||
262 | } | 319 | } |
263 | return QModelIndex(); | 320 | return QModelIndex(); |
264 | } | 321 | } |
@@ -272,7 +329,8 @@ QVariant NewModel::data(const QModelIndex &index, int role) const | |||
272 | return QVariant(); | 329 | return QVariant(); |
273 | } | 330 | } |
274 | if (index.internalPointer()) { | 331 | if (index.internalPointer()) { |
275 | const auto data = static_cast<QVariant *>(index.internalPointer()); | 332 | const auto entry = static_cast<Entry *>(index.internalPointer()); |
333 | const auto data = entry->mData; | ||
276 | if (data->userType() == qMetaTypeId<Signature *>()) { | 334 | if (data->userType() == qMetaTypeId<Signature *>()) { |
277 | const auto signature = data->value<Signature *>(); | 335 | const auto signature = data->value<Signature *>(); |
278 | int i = d->getPos(signature); | 336 | int i = d->getPos(signature); |
@@ -321,61 +379,9 @@ QModelIndex NewModel::parent(const QModelIndex &index) const | |||
321 | if (!index.internalPointer()) { | 379 | if (!index.internalPointer()) { |
322 | return QModelIndex(); | 380 | return QModelIndex(); |
323 | } | 381 | } |
324 | const auto data = static_cast<QVariant *>(index.internalPointer()); | 382 | const auto entry = static_cast<Entry *>(index.internalPointer()); |
325 | if (data->userType() == qMetaTypeId<Signature *>()) { | 383 | if (entry->mParent) { |
326 | const auto signature = data->value<Signature *>(); | 384 | return createIndex(entry->pos(), 0, entry->mParent); |
327 | const auto first = d->mParts.first(); | ||
328 | int i = d->getPos(signature); | ||
329 | |||
330 | if (i > 1) { | ||
331 | const auto sig = first->signatures().at(i-1); | ||
332 | return createIndex(0, 0, d->getVar(sig).data()); | ||
333 | } | ||
334 | |||
335 | return QModelIndex(); | ||
336 | } else if (data->userType() == qMetaTypeId<Encryption *>()) { | ||
337 | const auto encryption = data->value<Encryption *>(); | ||
338 | const auto first = d->mParts.first(); | ||
339 | int i = d->getPos(encryption); | ||
340 | |||
341 | if (i > 1) { | ||
342 | const auto enc = first->encryptions().at(i-1); | ||
343 | return createIndex(0, 0, d->getVar(enc).data()); | ||
344 | } | ||
345 | |||
346 | if (first->signatures().size() > 0) { | ||
347 | const int row = first->signatures().size() - 1; | ||
348 | const auto sig = first->signatures().at(row); | ||
349 | return createIndex(0, 0, d->getVar(sig).data()); | ||
350 | } else { | ||
351 | return QModelIndex(); | ||
352 | } | ||
353 | } else if (data->userType() == qMetaTypeId<Part *>()) { | ||
354 | const auto first = d->mParts.first(); | ||
355 | if (first->encryptions().size() > 0) { | ||
356 | const int row = first->encryptions().size() - 1; | ||
357 | const auto enc = first->encryptions().at(row); | ||
358 | auto var = new QVariant(); | ||
359 | var->setValue(enc.get()); | ||
360 | return createIndex(0, 0, d->getVar(enc).data()); | ||
361 | } | ||
362 | if (first->signatures().size() > 0) { | ||
363 | const int row = first->signatures().size() - 1; | ||
364 | const auto sig = first->signatures().at(row); | ||
365 | return createIndex(0, 0, d->getVar(sig).data()); | ||
366 | } | ||
367 | return QModelIndex(); | ||
368 | } else if (data->userType() == qMetaTypeId<Content *>()) { | ||
369 | const auto content = data->value<Content *>(); | ||
370 | const auto parent = content->parent(); | ||
371 | if (!parent) { | ||
372 | return QModelIndex(); | ||
373 | } | ||
374 | int pos = d->getPos(parent); | ||
375 | if (pos < 0 || pos >= d->mParts.size()) { | ||
376 | return QModelIndex(); | ||
377 | } | ||
378 | return createIndex(pos, 0, d->getVar(parent).data()); | ||
379 | } | 385 | } |
380 | return QModelIndex(); | 386 | return QModelIndex(); |
381 | } | 387 | } |
@@ -383,47 +389,13 @@ QModelIndex NewModel::parent(const QModelIndex &index) const | |||
383 | int NewModel::rowCount(const QModelIndex &parent) const | 389 | int NewModel::rowCount(const QModelIndex &parent) const |
384 | { | 390 | { |
385 | if (!parent.isValid()) { | 391 | if (!parent.isValid()) { |
386 | const auto first = d->mParts.first(); | 392 | return d->mRoot->mChildren.size(); |
387 | if (first->signatures().size() > 0) { | ||
388 | return 1; | ||
389 | } else if (first->encryptions().size() > 0) { | ||
390 | return 1; | ||
391 | } else { | ||
392 | return d->mParts.size(); | ||
393 | } | ||
394 | } else { | 393 | } else { |
395 | if (!parent.internalPointer()) { | 394 | if (!parent.internalPointer()) { |
396 | return 0; | 395 | return 0; |
397 | } | 396 | } |
398 | const auto data = static_cast<QVariant *>(parent.internalPointer()); | 397 | const auto entry = static_cast<Entry *>(parent.internalPointer()); |
399 | if (data->userType() == qMetaTypeId<Signature *>()) { | 398 | return entry->mChildren.size(); |
400 | const auto signature = data->value<Signature *>(); | ||
401 | const auto first = d->mParts.first(); | ||
402 | int i = d->getPos(signature); | ||
403 | |||
404 | if (i+1 < first->signatures().size()) { | ||
405 | return 1; | ||
406 | } | ||
407 | |||
408 | if (first->encryptions().size() > 0) { | ||
409 | return 1; | ||
410 | } | ||
411 | |||
412 | return d->mParts.size(); | ||
413 | } else if (data->userType() == qMetaTypeId<Encryption *>()) { | ||
414 | const auto encryption = data->value<Encryption *>(); | ||
415 | const auto first = d->mParts.first(); | ||
416 | int i = d->getPos(encryption); | ||
417 | |||
418 | if (i+1 < first->encryptions().size()) { | ||
419 | return 1; | ||
420 | } | ||
421 | |||
422 | return d->mParts.size(); | ||
423 | } else if (data->userType() == qMetaTypeId<Part *>()) { | ||
424 | const auto part = data->value<Part *>(); | ||
425 | return part->content().size(); | ||
426 | } | ||
427 | } | 399 | } |
428 | return 0; | 400 | return 0; |
429 | } | 401 | } |