diff options
Diffstat (limited to 'examples/maildirresource/maildirresource.cpp')
-rw-r--r-- | examples/maildirresource/maildirresource.cpp | 76 |
1 files changed, 68 insertions, 8 deletions
diff --git a/examples/maildirresource/maildirresource.cpp b/examples/maildirresource/maildirresource.cpp index f4833a8..caebd47 100644 --- a/examples/maildirresource/maildirresource.cpp +++ b/examples/maildirresource/maildirresource.cpp | |||
@@ -54,11 +54,44 @@ class FolderUpdater : public Sink::Preprocessor | |||
54 | public: | 54 | public: |
55 | FolderUpdater(const QByteArray &drafts) : mDraftsFolder(drafts) {} | 55 | FolderUpdater(const QByteArray &drafts) : mDraftsFolder(drafts) {} |
56 | 56 | ||
57 | QString getPath(const QByteArray &folderIdentifier, Sink::Storage::Transaction &transaction) | ||
58 | { | ||
59 | if (folderIdentifier.isEmpty()) { | ||
60 | return mMaildirPath; | ||
61 | } | ||
62 | QString folderPath; | ||
63 | auto db = Sink::Storage::mainDatabase(transaction, ENTITY_TYPE_FOLDER); | ||
64 | db.findLatest(folderIdentifier, [&](const QByteArray &, const QByteArray &value) { | ||
65 | Sink::EntityBuffer buffer(value); | ||
66 | const Sink::Entity &entity = buffer.entity(); | ||
67 | const auto adaptor = mFolderAdaptorFactory->createAdaptor(entity); | ||
68 | auto folderName = adaptor->getProperty("name").toString(); | ||
69 | //TODO handle non toplevel folders | ||
70 | folderPath = mMaildirPath + "/" + folderName; | ||
71 | }); | ||
72 | return folderPath; | ||
73 | } | ||
74 | |||
57 | void newEntity(const QByteArray &uid, qint64 revision, Sink::ApplicationDomain::BufferAdaptor &newEntity, Sink::Storage::Transaction &transaction) Q_DECL_OVERRIDE | 75 | void newEntity(const QByteArray &uid, qint64 revision, Sink::ApplicationDomain::BufferAdaptor &newEntity, Sink::Storage::Transaction &transaction) Q_DECL_OVERRIDE |
58 | { | 76 | { |
59 | if (newEntity.getProperty("draft").toBool()) { | 77 | if (newEntity.getProperty("draft").toBool()) { |
60 | newEntity.setProperty("folder", mDraftsFolder); | 78 | newEntity.setProperty("folder", mDraftsFolder); |
61 | } | 79 | } |
80 | const auto mimeMessage = newEntity.getProperty("mimeMessage"); | ||
81 | if (mimeMessage.isValid()) { | ||
82 | const auto oldPath = newEntity.getProperty("mimeMessage").toString(); | ||
83 | if (oldPath.startsWith(Sink::temporaryFileLocation())) { | ||
84 | auto folder = newEntity.getProperty("folder").toByteArray(); | ||
85 | const auto path = getPath(folder, transaction); | ||
86 | KPIM::Maildir maildir(path, false); | ||
87 | if (!maildir.isValid(true)) { | ||
88 | qWarning() << "Maildir is not existing: " << path; | ||
89 | } | ||
90 | auto identifier = maildir.addEntryFromPath(oldPath); | ||
91 | auto newPath = path + "/cur/" + identifier; | ||
92 | newEntity.setProperty("mimeMessage", QVariant::fromValue(newPath)); | ||
93 | } | ||
94 | } | ||
62 | } | 95 | } |
63 | 96 | ||
64 | void modifiedEntity(const QByteArray &uid, qint64 revision, const Sink::ApplicationDomain::BufferAdaptor &oldEntity, Sink::ApplicationDomain::BufferAdaptor &newEntity, | 97 | void modifiedEntity(const QByteArray &uid, qint64 revision, const Sink::ApplicationDomain::BufferAdaptor &oldEntity, Sink::ApplicationDomain::BufferAdaptor &newEntity, |
@@ -70,6 +103,33 @@ public: | |||
70 | { | 103 | { |
71 | } | 104 | } |
72 | QByteArray mDraftsFolder; | 105 | QByteArray mDraftsFolder; |
106 | QByteArray mResourceInstanceIdentifier; | ||
107 | QString mMaildirPath; | ||
108 | QSharedPointer<MaildirFolderAdaptorFactory> mFolderAdaptorFactory; | ||
109 | }; | ||
110 | |||
111 | class FolderPreprocessor : public Sink::Preprocessor | ||
112 | { | ||
113 | public: | ||
114 | FolderPreprocessor() {} | ||
115 | |||
116 | void newEntity(const QByteArray &uid, qint64 revision, Sink::ApplicationDomain::BufferAdaptor &newEntity, Sink::Storage::Transaction &transaction) Q_DECL_OVERRIDE | ||
117 | { | ||
118 | auto folderName = newEntity.getProperty("name").toString(); | ||
119 | const auto path = mMaildirPath + "/" + folderName; | ||
120 | KPIM::Maildir maildir(path, false); | ||
121 | maildir.create(); | ||
122 | } | ||
123 | |||
124 | void modifiedEntity(const QByteArray &uid, qint64 revision, const Sink::ApplicationDomain::BufferAdaptor &oldEntity, Sink::ApplicationDomain::BufferAdaptor &newEntity, | ||
125 | Sink::Storage::Transaction &transaction) Q_DECL_OVERRIDE | ||
126 | { | ||
127 | } | ||
128 | |||
129 | void deletedEntity(const QByteArray &uid, qint64 revision, const Sink::ApplicationDomain::BufferAdaptor &oldEntity, Sink::Storage::Transaction &transaction) Q_DECL_OVERRIDE | ||
130 | { | ||
131 | } | ||
132 | QString mMaildirPath; | ||
73 | }; | 133 | }; |
74 | 134 | ||
75 | MaildirResource::MaildirResource(const QByteArray &instanceIdentifier, const QSharedPointer<Sink::Pipeline> &pipeline) | 135 | MaildirResource::MaildirResource(const QByteArray &instanceIdentifier, const QSharedPointer<Sink::Pipeline> &pipeline) |
@@ -87,8 +147,9 @@ MaildirResource::MaildirResource(const QByteArray &instanceIdentifier, const QSh | |||
87 | auto folderUpdater = new FolderUpdater(QByteArray()); | 147 | auto folderUpdater = new FolderUpdater(QByteArray()); |
88 | addType(ENTITY_TYPE_MAIL, mMailAdaptorFactory, | 148 | addType(ENTITY_TYPE_MAIL, mMailAdaptorFactory, |
89 | QVector<Sink::Preprocessor*>() << new DefaultIndexUpdater<Sink::ApplicationDomain::Mail> << folderUpdater); | 149 | QVector<Sink::Preprocessor*>() << new DefaultIndexUpdater<Sink::ApplicationDomain::Mail> << folderUpdater); |
150 | auto folderPreprocessor = new FolderPreprocessor; | ||
90 | addType(ENTITY_TYPE_FOLDER, mFolderAdaptorFactory, | 151 | addType(ENTITY_TYPE_FOLDER, mFolderAdaptorFactory, |
91 | QVector<Sink::Preprocessor*>() << new DefaultIndexUpdater<Sink::ApplicationDomain::Folder>); | 152 | QVector<Sink::Preprocessor*>() << new DefaultIndexUpdater<Sink::ApplicationDomain::Folder> << folderPreprocessor); |
92 | 153 | ||
93 | KPIM::Maildir dir(mMaildirPath, true); | 154 | KPIM::Maildir dir(mMaildirPath, true); |
94 | mDraftsFolder = dir.addSubFolder("drafts"); | 155 | mDraftsFolder = dir.addSubFolder("drafts"); |
@@ -103,6 +164,10 @@ MaildirResource::MaildirResource(const QByteArray &instanceIdentifier, const QSh | |||
103 | synchronizationTransaction.commit(); | 164 | synchronizationTransaction.commit(); |
104 | 165 | ||
105 | folderUpdater->mDraftsFolder = draftsFolderLocalId; | 166 | folderUpdater->mDraftsFolder = draftsFolderLocalId; |
167 | folderUpdater->mResourceInstanceIdentifier = mResourceInstanceIdentifier; | ||
168 | folderUpdater->mFolderAdaptorFactory = mFolderAdaptorFactory; | ||
169 | folderUpdater->mMaildirPath = mMaildirPath; | ||
170 | folderPreprocessor->mMaildirPath = mMaildirPath; | ||
106 | } | 171 | } |
107 | 172 | ||
108 | static QStringList listRecursive( const QString &root, const KPIM::Maildir &dir ) | 173 | static QStringList listRecursive( const QString &root, const KPIM::Maildir &dir ) |
@@ -321,13 +386,8 @@ KAsync::Job<void> MaildirResource::replay(Sink::Storage &synchronizationStore, c | |||
321 | parentFolderRemoteId = mMaildirPath.toUtf8(); | 386 | parentFolderRemoteId = mMaildirPath.toUtf8(); |
322 | } | 387 | } |
323 | const auto parentFolderPath = parentFolderRemoteId; | 388 | const auto parentFolderPath = parentFolderRemoteId; |
324 | KPIM::Maildir maildir(parentFolderPath, false); | 389 | const auto remoteId = mail.getProperty("mimeMessage").toString().split('/').last(); |
325 | if (!maildir.isValid(true)) { | 390 | Trace() << "Creating a new mail." << remoteId; |
326 | return KAsync::error<void>(1, "Invalid folder " + parentFolderPath); | ||
327 | } | ||
328 | //FIXME move the mime message from the mimeMessage property to the proper place. | ||
329 | Trace() << "Creating a new mail."; | ||
330 | const auto remoteId = maildir.addEntryFromPath(mail.getProperty("mimeMessage").toString()); | ||
331 | if (remoteId.isEmpty()) { | 391 | if (remoteId.isEmpty()) { |
332 | Warning() << "Failed to create mail: " << remoteId; | 392 | Warning() << "Failed to create mail: " << remoteId; |
333 | return KAsync::error<void>(1, "Failed to create mail."); | 393 | return KAsync::error<void>(1, "Failed to create mail."); |