diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-06-03 09:40:31 +0200 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-06-03 09:40:31 +0200 |
commit | b5789da647bfdba9e3cc9b0595271b4d8c42bb8c (patch) | |
tree | 421bed2f38c9eac1d2d6579bf6a45b95c33cfbbf /examples/imapresource/imapserverproxy.cpp | |
parent | f5b254c87a993988c9cb1fd06b5635f1a6b20f9f (diff) | |
download | sink-b5789da647bfdba9e3cc9b0595271b4d8c42bb8c.tar.gz sink-b5789da647bfdba9e3cc9b0595271b4d8c42bb8c.zip |
The imap resource can write-back changes
Diffstat (limited to 'examples/imapresource/imapserverproxy.cpp')
-rw-r--r-- | examples/imapresource/imapserverproxy.cpp | 53 |
1 files changed, 49 insertions, 4 deletions
diff --git a/examples/imapresource/imapserverproxy.cpp b/examples/imapresource/imapserverproxy.cpp index 18b8dcf..1414dbe 100644 --- a/examples/imapresource/imapserverproxy.cpp +++ b/examples/imapresource/imapserverproxy.cpp | |||
@@ -28,6 +28,7 @@ | |||
28 | #include <KIMAP/KIMAP/DeleteJob> | 28 | #include <KIMAP/KIMAP/DeleteJob> |
29 | #include <KIMAP/KIMAP/StoreJob> | 29 | #include <KIMAP/KIMAP/StoreJob> |
30 | #include <KIMAP/KIMAP/ExpungeJob> | 30 | #include <KIMAP/KIMAP/ExpungeJob> |
31 | #include <KIMAP/KIMAP/CapabilitiesJob> | ||
31 | 32 | ||
32 | #include <KIMAP/KIMAP/SessionUiProxy> | 33 | #include <KIMAP/KIMAP/SessionUiProxy> |
33 | #include <KCoreAddons/KJob> | 34 | #include <KCoreAddons/KJob> |
@@ -41,6 +42,22 @@ const char* Imap::Flags::Deleted = "\\Deleted"; | |||
41 | const char* Imap::Flags::Answered = "\\Answered"; | 42 | const char* Imap::Flags::Answered = "\\Answered"; |
42 | const char* Imap::Flags::Flagged = "\\Flagged"; | 43 | const char* Imap::Flags::Flagged = "\\Flagged"; |
43 | 44 | ||
45 | template <typename T> | ||
46 | static KAsync::Job<T> runJob(KJob *job, const std::function<T(KJob*)> &f) | ||
47 | { | ||
48 | return KAsync::start<T>([job, f](KAsync::Future<T> &future) { | ||
49 | QObject::connect(job, &KJob::result, [&future, f](KJob *job) { | ||
50 | if (job->error()) { | ||
51 | future.setError(job->error(), job->errorString()); | ||
52 | } else { | ||
53 | future.setValue(f(job)); | ||
54 | future.setFinished(); | ||
55 | } | ||
56 | }); | ||
57 | job->start(); | ||
58 | }); | ||
59 | } | ||
60 | |||
44 | static KAsync::Job<void> runJob(KJob *job) | 61 | static KAsync::Job<void> runJob(KJob *job) |
45 | { | 62 | { |
46 | return KAsync::start<void>([job](KAsync::Future<void> &future) { | 63 | return KAsync::start<void>([job](KAsync::Future<void> &future) { |
@@ -76,7 +93,21 @@ KAsync::Job<void> ImapServerProxy::login(const QString &username, const QString | |||
76 | loginJob->setPassword(password); | 93 | loginJob->setPassword(password); |
77 | loginJob->setAuthenticationMode(KIMAP::LoginJob::Plain); | 94 | loginJob->setAuthenticationMode(KIMAP::LoginJob::Plain); |
78 | loginJob->setEncryptionMode(KIMAP::LoginJob::EncryptionMode::AnySslVersion); | 95 | loginJob->setEncryptionMode(KIMAP::LoginJob::EncryptionMode::AnySslVersion); |
79 | return runJob(loginJob); | 96 | |
97 | auto capabilitiesJob = new KIMAP::CapabilitiesJob(mSession); | ||
98 | QObject::connect(capabilitiesJob, &KIMAP::CapabilitiesJob::capabilitiesReceived, [this](const QStringList &capabilities) { | ||
99 | mCapabilities = capabilities; | ||
100 | }); | ||
101 | return runJob(loginJob).then(runJob(capabilitiesJob)).then<void>([this](){ | ||
102 | Trace() << "Supported capabilities: " << mCapabilities; | ||
103 | QStringList requiredExtensions = QStringList() << "UIDPLUS"; | ||
104 | for (const auto &requiredExtension : requiredExtensions) { | ||
105 | if (!mCapabilities.contains(requiredExtension)) { | ||
106 | Warning() << "Server doesn't support required capability: " << requiredExtension; | ||
107 | //TODO fail the job | ||
108 | } | ||
109 | } | ||
110 | }); | ||
80 | } | 111 | } |
81 | 112 | ||
82 | KAsync::Job<void> ImapServerProxy::select(const QString &mailbox) | 113 | KAsync::Job<void> ImapServerProxy::select(const QString &mailbox) |
@@ -87,14 +118,16 @@ KAsync::Job<void> ImapServerProxy::select(const QString &mailbox) | |||
87 | return runJob(select); | 118 | return runJob(select); |
88 | } | 119 | } |
89 | 120 | ||
90 | KAsync::Job<void> ImapServerProxy::append(const QString &mailbox, const QByteArray &content, const QList<QByteArray> &flags, const QDateTime &internalDate) | 121 | KAsync::Job<qint64> ImapServerProxy::append(const QString &mailbox, const QByteArray &content, const QList<QByteArray> &flags, const QDateTime &internalDate) |
91 | { | 122 | { |
92 | auto append = new KIMAP::AppendJob(mSession); | 123 | auto append = new KIMAP::AppendJob(mSession); |
93 | append->setMailBox(mailbox); | 124 | append->setMailBox(mailbox); |
94 | append->setContent(content); | 125 | append->setContent(content); |
95 | append->setFlags(flags); | 126 | append->setFlags(flags); |
96 | append->setInternalDate(internalDate); | 127 | append->setInternalDate(internalDate); |
97 | return runJob(append); | 128 | return runJob<qint64>(append, [](KJob *job) -> qint64{ |
129 | return static_cast<KIMAP::AppendJob*>(job)->uid(); | ||
130 | }); | ||
98 | } | 131 | } |
99 | 132 | ||
100 | KAsync::Job<void> ImapServerProxy::store(const KIMAP::ImapSet &set, const QList<QByteArray> &flags) | 133 | KAsync::Job<void> ImapServerProxy::store(const KIMAP::ImapSet &set, const QList<QByteArray> &flags) |
@@ -135,6 +168,13 @@ KAsync::Job<void> ImapServerProxy::expunge() | |||
135 | return runJob(job); | 168 | return runJob(job); |
136 | } | 169 | } |
137 | 170 | ||
171 | KAsync::Job<void> ImapServerProxy::expunge(const KIMAP::ImapSet &set) | ||
172 | { | ||
173 | //FIXME implement UID EXPUNGE | ||
174 | auto job = new KIMAP::ExpungeJob(mSession); | ||
175 | return runJob(job); | ||
176 | } | ||
177 | |||
138 | KAsync::Job<void> ImapServerProxy::fetch(const KIMAP::ImapSet &set, KIMAP::FetchJob::FetchScope scope, FetchCallback callback) | 178 | KAsync::Job<void> ImapServerProxy::fetch(const KIMAP::ImapSet &set, KIMAP::FetchJob::FetchScope scope, FetchCallback callback) |
139 | { | 179 | { |
140 | auto fetch = new KIMAP::FetchJob(mSession); | 180 | auto fetch = new KIMAP::FetchJob(mSession); |
@@ -219,10 +259,15 @@ KAsync::Job<void> ImapServerProxy::list(KIMAP::ListJob::Option option, const std | |||
219 | return runJob(listJob); | 259 | return runJob(listJob); |
220 | } | 260 | } |
221 | 261 | ||
262 | KAsync::Job<void> ImapServerProxy::remove(const QString &mailbox, const KIMAP::ImapSet &set) | ||
263 | { | ||
264 | return select(mailbox).then<void>(store(set, QByteArrayList() << Flags::Deleted)).then<void>(expunge(set)); | ||
265 | } | ||
266 | |||
222 | KAsync::Job<void> ImapServerProxy::remove(const QString &mailbox, const QByteArray &imapSet) | 267 | KAsync::Job<void> ImapServerProxy::remove(const QString &mailbox, const QByteArray &imapSet) |
223 | { | 268 | { |
224 | const auto set = KIMAP::ImapSet::fromImapSequenceSet(imapSet); | 269 | const auto set = KIMAP::ImapSet::fromImapSequenceSet(imapSet); |
225 | return select(mailbox).then<void>(store(set, QByteArrayList() << Flags::Deleted)).then<void>(expunge()); | 270 | return remove(mailbox, set); |
226 | } | 271 | } |
227 | 272 | ||
228 | KAsync::Job<void> ImapServerProxy::fetchFolders(std::function<void(const QVector<Folder> &)> callback) | 273 | KAsync::Job<void> ImapServerProxy::fetchFolders(std::function<void(const QVector<Folder> &)> callback) |