diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-05-22 15:27:52 +0200 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-05-22 15:27:52 +0200 |
commit | f38554ad4c7ff918bf7792413e6599e69d30cf3a (patch) | |
tree | 8f971c3c5349166e6f2b488c7983bc868627e524 /examples/imapresource/imapresource.cpp | |
parent | 1613964dd195a21eb7ff28699b76db8d41fb8408 (diff) | |
download | sink-f38554ad4c7ff918bf7792413e6599e69d30cf3a.tar.gz sink-f38554ad4c7ff918bf7792413e6599e69d30cf3a.zip |
Support for folder hierarchies
Diffstat (limited to 'examples/imapresource/imapresource.cpp')
-rw-r--r-- | examples/imapresource/imapresource.cpp | 37 |
1 files changed, 23 insertions, 14 deletions
diff --git a/examples/imapresource/imapresource.cpp b/examples/imapresource/imapresource.cpp index baa88b9..49cbb20 100644 --- a/examples/imapresource/imapresource.cpp +++ b/examples/imapresource/imapresource.cpp | |||
@@ -49,6 +49,7 @@ | |||
49 | #undef DEBUG_AREA | 49 | #undef DEBUG_AREA |
50 | #define DEBUG_AREA "resource.imap" | 50 | #define DEBUG_AREA "resource.imap" |
51 | 51 | ||
52 | using namespace Imap; | ||
52 | 53 | ||
53 | ImapResource::ImapResource(const QByteArray &instanceIdentifier, const QSharedPointer<Sink::Pipeline> &pipeline) | 54 | ImapResource::ImapResource(const QByteArray &instanceIdentifier, const QSharedPointer<Sink::Pipeline> &pipeline) |
54 | : Sink::GenericResource(instanceIdentifier, pipeline), | 55 | : Sink::GenericResource(instanceIdentifier, pipeline), |
@@ -71,20 +72,22 @@ QByteArray ImapResource::createFolder(const QString &folderPath, const QByteArra | |||
71 | auto remoteId = folderPath.toUtf8(); | 72 | auto remoteId = folderPath.toUtf8(); |
72 | auto bufferType = ENTITY_TYPE_FOLDER; | 73 | auto bufferType = ENTITY_TYPE_FOLDER; |
73 | Sink::ApplicationDomain::Folder folder; | 74 | Sink::ApplicationDomain::Folder folder; |
74 | folder.setProperty("name", folderPath.split('/').last()); | 75 | auto folderPathParts = folderPath.split('/'); |
76 | const auto name = folderPathParts.takeLast(); | ||
77 | folder.setProperty("name", name); | ||
75 | folder.setProperty("icon", icon); | 78 | folder.setProperty("icon", icon); |
76 | 79 | ||
77 | // if (!md.isRoot()) { | 80 | if (!folderPathParts.isEmpty()) { |
78 | // folder.setProperty("parent", resolveRemoteId(ENTITY_TYPE_FOLDER, md.parent().path().toUtf8(), synchronizationTransaction)); | 81 | folder.setProperty("parent", resolveRemoteId(ENTITY_TYPE_FOLDER, folderPathParts.join('/').toUtf8(), synchronizationTransaction)); |
79 | // } | 82 | } |
80 | createOrModify(transaction, synchronizationTransaction, *mFolderAdaptorFactory, bufferType, remoteId, folder); | 83 | createOrModify(transaction, synchronizationTransaction, *mFolderAdaptorFactory, bufferType, remoteId, folder); |
81 | return remoteId; | 84 | return remoteId; |
82 | } | 85 | } |
83 | 86 | ||
84 | void ImapResource::synchronizeFolders(const QStringList &folderList, Sink::Storage::Transaction &transaction, Sink::Storage::Transaction &synchronizationTransaction) | 87 | void ImapResource::synchronizeFolders(const QVector<Folder> &folderList, Sink::Storage::Transaction &transaction, Sink::Storage::Transaction &synchronizationTransaction) |
85 | { | 88 | { |
86 | const QByteArray bufferType = ENTITY_TYPE_FOLDER; | 89 | const QByteArray bufferType = ENTITY_TYPE_FOLDER; |
87 | Trace() << "Found folders " << folderList; | 90 | Trace() << "Found folders " << folderList.size(); |
88 | 91 | ||
89 | scanForRemovals(transaction, synchronizationTransaction, bufferType, | 92 | scanForRemovals(transaction, synchronizationTransaction, bufferType, |
90 | [&bufferType, &transaction](const std::function<void(const QByteArray &)> &callback) { | 93 | [&bufferType, &transaction](const std::function<void(const QByteArray &)> &callback) { |
@@ -99,12 +102,18 @@ void ImapResource::synchronizeFolders(const QStringList &folderList, Sink::Stora | |||
99 | }); | 102 | }); |
100 | }, | 103 | }, |
101 | [&folderList](const QByteArray &remoteId) -> bool { | 104 | [&folderList](const QByteArray &remoteId) -> bool { |
102 | return folderList.contains(remoteId); | 105 | //folderList.contains(remoteId) |
106 | for (const auto folderPath : folderList) { | ||
107 | if (folderPath.pathParts.join('/') == remoteId) { | ||
108 | return true; | ||
109 | } | ||
110 | } | ||
111 | return false; | ||
103 | } | 112 | } |
104 | ); | 113 | ); |
105 | 114 | ||
106 | for (const auto folderPath : folderList) { | 115 | for (const auto folderPath : folderList) { |
107 | createFolder(folderPath, "folder", transaction, synchronizationTransaction); | 116 | createFolder(folderPath.pathParts.join('/'), "folder", transaction, synchronizationTransaction); |
108 | } | 117 | } |
109 | } | 118 | } |
110 | 119 | ||
@@ -176,9 +185,8 @@ KAsync::Job<void> ImapResource::synchronizeWithSource(Sink::Storage &mainStore, | |||
176 | Log() << " Synchronizing"; | 185 | Log() << " Synchronizing"; |
177 | return KAsync::start<void>([this, &mainStore, &synchronizationStore](KAsync::Future<void> future) { | 186 | return KAsync::start<void>([this, &mainStore, &synchronizationStore](KAsync::Future<void> future) { |
178 | ImapServerProxy imap(mServer, mPort); | 187 | ImapServerProxy imap(mServer, mPort); |
179 | QStringList folderList; | 188 | QVector<Folder> folderList; |
180 | // QList<KAsync::Future<void>> waitCondition; | 189 | auto folderFuture = imap.fetchFolders([this, &imap, &mainStore, &synchronizationStore, &folderList](const QVector<Folder> &folders) { |
181 | auto folderFuture = imap.fetchFolders([this, &imap, &mainStore, &synchronizationStore, &folderList](const QStringList &folders) { | ||
182 | auto transaction = mainStore.createTransaction(Sink::Storage::ReadOnly); | 190 | auto transaction = mainStore.createTransaction(Sink::Storage::ReadOnly); |
183 | auto syncTransaction = synchronizationStore.createTransaction(Sink::Storage::ReadWrite); | 191 | auto syncTransaction = synchronizationStore.createTransaction(Sink::Storage::ReadWrite); |
184 | synchronizeFolders(folders, transaction, syncTransaction); | 192 | synchronizeFolders(folders, transaction, syncTransaction); |
@@ -215,16 +223,17 @@ KAsync::Job<void> ImapResource::synchronizeWithSource(Sink::Storage &mainStore, | |||
215 | auto messagesFuture = imap.fetchMessages(folder, [this, &mainStore, &synchronizationStore, folder](const QVector<Message> &messages) { | 223 | auto messagesFuture = imap.fetchMessages(folder, [this, &mainStore, &synchronizationStore, folder](const QVector<Message> &messages) { |
216 | auto transaction = mainStore.createTransaction(Sink::Storage::ReadOnly); | 224 | auto transaction = mainStore.createTransaction(Sink::Storage::ReadOnly); |
217 | auto syncTransaction = synchronizationStore.createTransaction(Sink::Storage::ReadWrite); | 225 | auto syncTransaction = synchronizationStore.createTransaction(Sink::Storage::ReadWrite); |
218 | Trace() << "Synchronizing mails" << folder; | 226 | Trace() << "Synchronizing mails" << folder.pathParts.join('/'); |
219 | synchronizeMails(transaction, syncTransaction, folder, messages); | 227 | synchronizeMails(transaction, syncTransaction, folder.pathParts.join('/'), messages); |
220 | transaction.commit(); | 228 | transaction.commit(); |
221 | syncTransaction.commit(); | 229 | syncTransaction.commit(); |
222 | }); | 230 | }); |
223 | messagesFuture.waitForFinished(); | 231 | messagesFuture.waitForFinished(); |
224 | if (messagesFuture.errorCode()) { | 232 | if (messagesFuture.errorCode()) { |
225 | future.setError(1, "Folder sync failed: " + folder); | 233 | future.setError(1, "Folder sync failed: " + folder.pathParts.join('/')); |
226 | return; | 234 | return; |
227 | } | 235 | } |
236 | Trace() << "Folder synchronized: " << folder.pathParts.join('/'); | ||
228 | } | 237 | } |
229 | 238 | ||
230 | 239 | ||