diff options
Diffstat (limited to 'common')
-rw-r--r-- | common/CMakeLists.txt | 1 | ||||
-rw-r--r-- | common/specialpurposepreprocessor.cpp | 88 | ||||
-rw-r--r-- | common/specialpurposepreprocessor.h | 43 |
3 files changed, 132 insertions, 0 deletions
diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index 3083a2e..5eb15ba 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt | |||
@@ -74,6 +74,7 @@ set(command_SRCS | |||
74 | sourcewriteback.cpp | 74 | sourcewriteback.cpp |
75 | entityreader.cpp | 75 | entityreader.cpp |
76 | mailpreprocessor.cpp | 76 | mailpreprocessor.cpp |
77 | specialpurposepreprocessor.cpp | ||
77 | ${storage_SRCS}) | 78 | ${storage_SRCS}) |
78 | 79 | ||
79 | add_library(${PROJECT_NAME} SHARED ${command_SRCS}) | 80 | add_library(${PROJECT_NAME} SHARED ${command_SRCS}) |
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 | } | ||
diff --git a/common/specialpurposepreprocessor.h b/common/specialpurposepreprocessor.h new file mode 100644 index 0000000..a33701b --- /dev/null +++ b/common/specialpurposepreprocessor.h | |||
@@ -0,0 +1,43 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2015 Christian Mollekopf <chrigi_1@fastmail.fm> | ||
3 | * | ||
4 | * This program is free software; you can redistribute it and/or modify | ||
5 | * it under the terms of the GNU General Public License as published by | ||
6 | * the Free Software Foundation; either version 2 of the License, or | ||
7 | * (at your option) any later version. | ||
8 | * | ||
9 | * This program is distributed in the hope that it will be useful, | ||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | * GNU General Public License for more details. | ||
13 | * | ||
14 | * You should have received a copy of the GNU General Public License | ||
15 | * along with this program; if not, write to the | ||
16 | * Free Software Foundation, Inc., | ||
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
18 | */ | ||
19 | #include "sink_export.h" | ||
20 | |||
21 | #include "pipeline.h" | ||
22 | |||
23 | namespace SpecialPurpose { | ||
24 | bool SINK_EXPORT isSpecialPurposeFolderName(const QString &name); | ||
25 | QByteArray SINK_EXPORT getSpecialPurposeType(const QString &name); | ||
26 | } | ||
27 | |||
28 | class SINK_EXPORT SpecialPurposeProcessor : public Sink::Preprocessor | ||
29 | { | ||
30 | public: | ||
31 | SpecialPurposeProcessor(const QByteArray &resourceType, const QByteArray &resourceInstanceIdentifier); | ||
32 | |||
33 | QByteArray ensureFolder(Sink::Storage::Transaction &transaction, const QByteArray &specialPurpose); | ||
34 | |||
35 | void moveToFolder(Sink::ApplicationDomain::BufferAdaptor &newEntity, Sink::Storage::Transaction &transaction); | ||
36 | |||
37 | void newEntity(const QByteArray &uid, qint64 revision, Sink::ApplicationDomain::BufferAdaptor &newEntity, Sink::Storage::Transaction &transaction) Q_DECL_OVERRIDE; | ||
38 | void modifiedEntity(const QByteArray &uid, qint64 revision, const Sink::ApplicationDomain::BufferAdaptor &oldEntity, Sink::ApplicationDomain::BufferAdaptor &newEntity, Sink::Storage::Transaction &transaction) Q_DECL_OVERRIDE; | ||
39 | |||
40 | QHash<QByteArray, QByteArray> mSpecialPurposeFolders; | ||
41 | QByteArray mResourceType; | ||
42 | QByteArray mResourceInstanceIdentifier; | ||
43 | }; | ||