diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-06-23 17:05:47 +0200 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-06-25 23:34:51 +0200 |
commit | b29a17465d1e52bd7dd5c57f08e7af53e915eee6 (patch) | |
tree | ab618b5f0a1c89ba1332723b5ebad03bce92fb2c /common/specialpurposepreprocessor.cpp | |
parent | 2ce8dcf40f22dd4e9cf4a6b1c8f5386993ebba6e (diff) | |
download | sink-b29a17465d1e52bd7dd5c57f08e7af53e915eee6.tar.gz sink-b29a17465d1e52bd7dd5c57f08e7af53e915eee6.zip |
Share special purpose preprocessor implementation.
Diffstat (limited to 'common/specialpurposepreprocessor.cpp')
-rw-r--r-- | common/specialpurposepreprocessor.cpp | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/common/specialpurposepreprocessor.cpp b/common/specialpurposepreprocessor.cpp new file mode 100644 index 0000000..2892105 --- /dev/null +++ b/common/specialpurposepreprocessor.cpp | |||
@@ -0,0 +1,88 @@ | |||
1 | #include "specialpurposepreprocessor.h" | ||
2 | #include "entityreader.h" | ||
3 | #include "query.h" | ||
4 | #include "applicationdomaintype.h" | ||
5 | |||
6 | using namespace Sink; | ||
7 | |||
8 | static QHash<QByteArray, QString> specialPurposeFolders() | ||
9 | { | ||
10 | QHash<QByteArray, QString> hash; | ||
11 | //FIXME localize | ||
12 | hash.insert("drafts", "Drafts"); | ||
13 | hash.insert("trash", "Trash"); | ||
14 | return hash; | ||
15 | } | ||
16 | |||
17 | static QHash<QString, QByteArray> specialPurposeNames() | ||
18 | { | ||
19 | QHash<QString, QByteArray> hash; | ||
20 | for (const auto &value : specialPurposeFolders().values()) { | ||
21 | hash.insert(value.toLower(), specialPurposeFolders().key(value)); | ||
22 | } | ||
23 | return hash; | ||
24 | } | ||
25 | |||
26 | //specialpurpose, name | ||
27 | static QHash<QByteArray, QString> sSpecialPurposeFolders = specialPurposeFolders(); | ||
28 | //Lowercase-name, specialpurpose | ||
29 | static QHash<QString, QByteArray> sSpecialPurposeNames = specialPurposeNames(); | ||
30 | |||
31 | namespace SpecialPurpose { | ||
32 | bool isSpecialPurposeFolderName(const QString &name) | ||
33 | { | ||
34 | return sSpecialPurposeNames.contains(name.toLower()); | ||
35 | } | ||
36 | |||
37 | QByteArray getSpecialPurposeType(const QString &name) | ||
38 | { | ||
39 | return sSpecialPurposeNames.value(name.toLower()); | ||
40 | } | ||
41 | } | ||
42 | |||
43 | SpecialPurposeProcessor::SpecialPurposeProcessor(const QByteArray &resourceType, const QByteArray &resourceInstanceIdentifier) : mResourceType(resourceType), mResourceInstanceIdentifier(resourceInstanceIdentifier) {} | ||
44 | |||
45 | QByteArray SpecialPurposeProcessor::ensureFolder(Sink::Storage::Transaction &transaction, const QByteArray &specialPurpose) | ||
46 | { | ||
47 | if (!mSpecialPurposeFolders.contains(specialPurpose)) { | ||
48 | //Try to find an existing drafts folder | ||
49 | Sink::EntityReader<ApplicationDomain::Folder> reader(mResourceType, mResourceInstanceIdentifier, transaction); | ||
50 | reader.query(Sink::Query().filter<ApplicationDomain::Folder::SpecialPurpose>(Query::Comparator(specialPurpose, Query::Comparator::Contains)), | ||
51 | [this, specialPurpose](const ApplicationDomain::Folder &f) -> bool{ | ||
52 | mSpecialPurposeFolders.insert(specialPurpose, f.identifier()); | ||
53 | return false; | ||
54 | }); | ||
55 | if (!mSpecialPurposeFolders.contains(specialPurpose)) { | ||
56 | Trace() << "Failed to find a drafts folder, creating a new one"; | ||
57 | auto folder = ApplicationDomain::Folder::create(mResourceInstanceIdentifier); | ||
58 | folder.setSpecialPurpose(QByteArrayList() << specialPurpose); | ||
59 | folder.setName(sSpecialPurposeFolders.value(specialPurpose)); | ||
60 | folder.setIcon("folder"); | ||
61 | //This processes the pipeline synchronously | ||
62 | createEntity(folder); | ||
63 | mSpecialPurposeFolders.insert(specialPurpose, folder.identifier()); | ||
64 | } | ||
65 | } | ||
66 | return mSpecialPurposeFolders.value(specialPurpose); | ||
67 | } | ||
68 | |||
69 | void SpecialPurposeProcessor::moveToFolder(Sink::ApplicationDomain::BufferAdaptor &newEntity, Sink::Storage::Transaction &transaction) | ||
70 | { | ||
71 | if (newEntity.getProperty("trash").toBool()) { | ||
72 | newEntity.setProperty("folder", ensureFolder(transaction, "trash")); | ||
73 | return; | ||
74 | } | ||
75 | if (newEntity.getProperty("draft").toBool()) { | ||
76 | newEntity.setProperty("folder", ensureFolder(transaction, "drafts")); | ||
77 | } | ||
78 | } | ||
79 | |||
80 | void SpecialPurposeProcessor::newEntity(const QByteArray &uid, qint64 revision, Sink::ApplicationDomain::BufferAdaptor &newEntity, Sink::Storage::Transaction &transaction) | ||
81 | { | ||
82 | moveToFolder(newEntity, transaction); | ||
83 | } | ||
84 | |||
85 | void SpecialPurposeProcessor::modifiedEntity(const QByteArray &uid, qint64 revision, const Sink::ApplicationDomain::BufferAdaptor &oldEntity, Sink::ApplicationDomain::BufferAdaptor &newEntity, Sink::Storage::Transaction &transaction) | ||
86 | { | ||
87 | moveToFolder(newEntity, transaction); | ||
88 | } | ||