summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2016-06-13 14:39:57 +0200
committerChristian Mollekopf <chrigi_1@fastmail.fm>2016-06-13 14:39:57 +0200
commit82ca7ebb9c24fef6f1860eeca9577d998af03c7f (patch)
treeb3e43325ccf1d315f351424e178cac1fbd7e9933
parent29cca3919ed373c486c3c9acec32481918baeb58 (diff)
downloadsink-82ca7ebb9c24fef6f1860eeca9577d998af03c7f.tar.gz
sink-82ca7ebb9c24fef6f1860eeca9577d998af03c7f.zip
Implemented maildir mail moves and got it to pass tests again
-rw-r--r--examples/maildirresource/facade.cpp1
-rw-r--r--examples/maildirresource/libmaildir/maildir.cpp8
-rw-r--r--examples/maildirresource/maildirresource.cpp61
-rw-r--r--examples/maildirresource/maildirresource.h2
4 files changed, 53 insertions, 19 deletions
diff --git a/examples/maildirresource/facade.cpp b/examples/maildirresource/facade.cpp
index a7a0348..d8fc02d 100644
--- a/examples/maildirresource/facade.cpp
+++ b/examples/maildirresource/facade.cpp
@@ -48,6 +48,7 @@ MaildirResourceMailFacade::MaildirResourceMailFacade(const QByteArray &instanceI
48 value.setProperty("mimeMessage", list.at(0).filePath()); 48 value.setProperty("mimeMessage", list.at(0).filePath());
49 } 49 }
50 } 50 }
51 value.setChangedProperties(QSet<QByteArray>());
51 }; 52 };
52} 53}
53 54
diff --git a/examples/maildirresource/libmaildir/maildir.cpp b/examples/maildirresource/libmaildir/maildir.cpp
index c55e00e..de704f2 100644
--- a/examples/maildirresource/libmaildir/maildir.cpp
+++ b/examples/maildirresource/libmaildir/maildir.cpp
@@ -141,6 +141,10 @@ public:
141 141
142 QString findRealKey(const QString& key) const 142 QString findRealKey(const QString& key) const
143 { 143 {
144 if (key.isEmpty()) {
145 qWarning() << "Empty key: " << key;
146 return key;
147 }
144 // KeyCache* keyCache = KeyCache::self(); 148 // KeyCache* keyCache = KeyCache::self();
145 // if (keyCache->isNewKey(path, key)) { 149 // if (keyCache->isNewKey(path, key)) {
146 if (QFile::exists(path + QString::fromLatin1("/new/") + key)) { 150 if (QFile::exists(path + QString::fromLatin1("/new/") + key)) {
@@ -670,7 +674,7 @@ QString Maildir::addEntry(const QByteArray& data)
670 do { 674 do {
671 uniqueKey = createUniqueFileName() + d->hostName; 675 uniqueKey = createUniqueFileName() + d->hostName;
672 key = d->path + QLatin1String("/tmp/") + uniqueKey; 676 key = d->path + QLatin1String("/tmp/") + uniqueKey;
673 finalKey = d->path + QLatin1String("/new/") + uniqueKey; 677 finalKey = d->path + QLatin1String("/cur/") + uniqueKey;
674 curKey = d->path + QLatin1String("/cur/") + uniqueKey; 678 curKey = d->path + QLatin1String("/cur/") + uniqueKey;
675 } while (QFile::exists(key) || QFile::exists(finalKey) || QFile::exists(curKey)); 679 } while (QFile::exists(key) || QFile::exists(finalKey) || QFile::exists(curKey));
676 680
@@ -911,7 +915,7 @@ QString Maildir::moveEntryTo(const QString &key, const Maildir &destination)
911 } 915 }
912 QFile f(realKey); 916 QFile f(realKey);
913 // ### is this safe regarding the maildir locking scheme? 917 // ### is this safe regarding the maildir locking scheme?
914 const QString targetKey = destination.path() + QDir::separator() + QLatin1String("new") + QDir::separator() + key; 918 const QString targetKey = destination.path() + QDir::separator() + QLatin1String("cur") + QDir::separator() + key;
915 if (!f.rename(targetKey)) { 919 if (!f.rename(targetKey)) {
916 qDebug() << "Failed to rename" << realKey << "to" << targetKey << "! Error: " << f.errorString();; 920 qDebug() << "Failed to rename" << realKey << "to" << targetKey << "! Error: " << f.errorString();;
917 d->lastError = f.errorString(); 921 d->lastError = f.errorString();
diff --git a/examples/maildirresource/maildirresource.cpp b/examples/maildirresource/maildirresource.cpp
index 9800f7f..05441b0 100644
--- a/examples/maildirresource/maildirresource.cpp
+++ b/examples/maildirresource/maildirresource.cpp
@@ -139,10 +139,30 @@ public:
139 const auto path = getPath(folder, transaction); 139 const auto path = getPath(folder, transaction);
140 KPIM::Maildir maildir(path, false); 140 KPIM::Maildir maildir(path, false);
141 if (!maildir.isValid(true)) { 141 if (!maildir.isValid(true)) {
142 qWarning() << "Maildir is not existing: " << path; 142 Warning() << "Maildir is not existing: " << path;
143 } 143 }
144 auto identifier = maildir.addEntryFromPath(oldPath); 144 auto identifier = maildir.addEntryFromPath(oldPath);
145 return path + "/" + identifier; 145 return path + "/" + identifier;
146 } else {
147 //Handle moves
148 const auto path = getPath(folder, transaction);
149 KPIM::Maildir maildir(path, false);
150 if (!maildir.isValid(true)) {
151 Warning() << "Maildir is not existing: " << path;
152 }
153 auto oldIdentifier = KPIM::Maildir::getKeyFromFile(oldPath);
154 auto pathParts = oldPath.split('/');
155 pathParts.takeLast();
156 auto oldDirectory = pathParts.join('/');
157 if (oldDirectory == path) {
158 return oldPath;
159 }
160 KPIM::Maildir oldMaildir(oldDirectory, false);
161 if (!oldMaildir.isValid(false)) {
162 Warning() << "Maildir is not existing: " << path;
163 }
164 auto identifier = oldMaildir.moveEntryTo(oldIdentifier, maildir);
165 return path + "/" + identifier;
146 } 166 }
147 return oldPath; 167 return oldPath;
148 } 168 }
@@ -161,21 +181,29 @@ public:
161 void modifiedEntity(const QByteArray &uid, qint64 revision, const Sink::ApplicationDomain::BufferAdaptor &oldEntity, Sink::ApplicationDomain::BufferAdaptor &newEntity, 181 void modifiedEntity(const QByteArray &uid, qint64 revision, const Sink::ApplicationDomain::BufferAdaptor &oldEntity, Sink::ApplicationDomain::BufferAdaptor &newEntity,
162 Sink::Storage::Transaction &transaction) Q_DECL_OVERRIDE 182 Sink::Storage::Transaction &transaction) Q_DECL_OVERRIDE
163 { 183 {
164 //TODO deal with moves 184 if (newEntity.getProperty("draft").toBool()) {
185 newEntity.setProperty("folder", mDraftsFolder);
186 }
165 const auto mimeMessage = newEntity.getProperty("mimeMessage"); 187 const auto mimeMessage = newEntity.getProperty("mimeMessage");
166 if (mimeMessage.isValid() && mimeMessage.toString() != oldEntity.getProperty("mimeMessage").toString()) { 188 const auto newFolder = newEntity.getProperty("folder");
167 //Remove the olde mime message if there is a new one 189 const bool mimeMessageChanged = mimeMessage.isValid() && mimeMessage.toString() != oldEntity.getProperty("mimeMessage").toString();
168 const auto filePath = getFilePathFromMimeMessagePath(oldEntity.getProperty("mimeMessage").toString()); 190 const bool folderChanged = newFolder.isValid() && newFolder.toString() != oldEntity.getProperty("mimeMessage").toString();
169 QFile::remove(filePath); 191 if (mimeMessageChanged || folderChanged) {
170 192 Trace() << "Moving mime message: " << mimeMessageChanged << folderChanged;
171 newEntity.setProperty("mimeMessage", moveMessage(mimeMessage.toString(), newEntity.getProperty("folder").toByteArray(), transaction)); 193 auto newPath = moveMessage(mimeMessage.toString(), newEntity.getProperty("folder").toByteArray(), transaction);
172 Trace() << "Modified message: " << filePath << oldEntity.getProperty("mimeMessage").toString(); 194 if (newPath != oldEntity.getProperty("mimeMessage").toString()) {
195 const auto oldPath = getFilePathFromMimeMessagePath(oldEntity.getProperty("mimeMessage").toString());
196 newEntity.setProperty("mimeMessage", newPath);
197 //Remove the olde mime message if there is a new one
198 QFile::remove(oldPath);
199 }
173 } 200 }
174 201
175 auto mimeMessagePath = newEntity.getProperty("mimeMessage").toString(); 202 auto mimeMessagePath = newEntity.getProperty("mimeMessage").toString();
176 const auto maildirPath = getPath(newEntity.getProperty("folder").toByteArray(), transaction); 203 const auto maildirPath = getPath(newEntity.getProperty("folder").toByteArray(), transaction);
177 KPIM::Maildir maildir(maildirPath, false); 204 KPIM::Maildir maildir(maildirPath, false);
178 QString identifier = KPIM::Maildir::getKeyFromFile(mimeMessagePath); 205 const auto file = getFilePathFromMimeMessagePath(mimeMessagePath);
206 QString identifier = KPIM::Maildir::getKeyFromFile(file);
179 207
180 //get flags from 208 //get flags from
181 KPIM::Maildir::Flags flags; 209 KPIM::Maildir::Flags flags;
@@ -247,14 +275,17 @@ public:
247 return list; 275 return list;
248 } 276 }
249 277
250 QByteArray createFolder(const QString &folderPath, const QByteArray &icon) 278 QByteArray createFolder(const QString &folderPath, const QByteArray &icon, const QByteArrayList &specialpurpose = QByteArrayList())
251 { 279 {
252 auto remoteId = folderPath.toUtf8(); 280 auto remoteId = folderPath.toUtf8();
253 auto bufferType = ENTITY_TYPE_FOLDER; 281 auto bufferType = ENTITY_TYPE_FOLDER;
254 KPIM::Maildir md(folderPath, folderPath == mMaildirPath); 282 KPIM::Maildir md(folderPath, folderPath == mMaildirPath);
255 Sink::ApplicationDomain::Folder folder; 283 Sink::ApplicationDomain::Folder folder;
256 folder.setProperty("name", md.name()); 284 folder.setName(md.name());
257 folder.setProperty("icon", icon); 285 folder.setIcon(icon);
286 if (!specialpurpose.isEmpty()) {
287 folder.setSpecialPurpose(specialpurpose);
288 }
258 289
259 if (!md.isRoot()) { 290 if (!md.isRoot()) {
260 folder.setProperty("parent", syncStore().resolveRemoteId(ENTITY_TYPE_FOLDER, md.parent().path().toUtf8())); 291 folder.setProperty("parent", syncStore().resolveRemoteId(ENTITY_TYPE_FOLDER, md.parent().path().toUtf8()));
@@ -478,10 +509,10 @@ MaildirResource::MaildirResource(const QByteArray &instanceIdentifier, const QSh
478 setupPreprocessors(ENTITY_TYPE_FOLDER, QVector<Sink::Preprocessor*>() << folderPreprocessor << new DefaultIndexUpdater<Sink::ApplicationDomain::Folder>); 509 setupPreprocessors(ENTITY_TYPE_FOLDER, QVector<Sink::Preprocessor*>() << folderPreprocessor << new DefaultIndexUpdater<Sink::ApplicationDomain::Folder>);
479 510
480 KPIM::Maildir dir(mMaildirPath, true); 511 KPIM::Maildir dir(mMaildirPath, true);
481 mDraftsFolder = dir.addSubFolder("drafts");
482 Trace() << "Started maildir resource for maildir: " << mMaildirPath; 512 Trace() << "Started maildir resource for maildir: " << mMaildirPath;
513 mDraftsFolder = dir.addSubFolder("Drafts");
483 514
484 auto remoteId = synchronizer->createFolder(mDraftsFolder, "folder"); 515 auto remoteId = synchronizer->createFolder(mDraftsFolder, "folder", QByteArrayList() << "drafts");
485 auto draftsFolderLocalId = synchronizer->syncStore().resolveRemoteId(ENTITY_TYPE_FOLDER, remoteId); 516 auto draftsFolderLocalId = synchronizer->syncStore().resolveRemoteId(ENTITY_TYPE_FOLDER, remoteId);
486 synchronizer->commit(); 517 synchronizer->commit();
487 synchronizer->commitSync(); 518 synchronizer->commitSync();
diff --git a/examples/maildirresource/maildirresource.h b/examples/maildirresource/maildirresource.h
index b3bce5e..d481b22 100644
--- a/examples/maildirresource/maildirresource.h
+++ b/examples/maildirresource/maildirresource.h
@@ -49,8 +49,6 @@ public:
49 KAsync::Job<void> inspect(int inspectionType, const QByteArray &inspectionId, const QByteArray &domainType, const QByteArray &entityId, const QByteArray &property, const QVariant &expectedValue) Q_DECL_OVERRIDE; 49 KAsync::Job<void> inspect(int inspectionType, const QByteArray &inspectionId, const QByteArray &domainType, const QByteArray &entityId, const QByteArray &property, const QVariant &expectedValue) Q_DECL_OVERRIDE;
50 static void removeFromDisk(const QByteArray &instanceIdentifier); 50 static void removeFromDisk(const QByteArray &instanceIdentifier);
51private: 51private:
52 // KAsync::Job<QByteArray> replay(const Sink::ApplicationDomain::Mail &, Sink::Operation, const QByteArray &oldRemoteId) Q_DECL_OVERRIDE;
53 // KAsync::Job<QByteArray> replay(const Sink::ApplicationDomain::Folder &, Sink::Operation, const QByteArray &oldRemoteId) Q_DECL_OVERRIDE;
54 52
55 QStringList listAvailableFolders(); 53 QStringList listAvailableFolders();
56 QString mMaildirPath; 54 QString mMaildirPath;