diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-05-23 13:55:36 +0200 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-05-23 13:55:36 +0200 |
commit | a853cb1a32ae9207501fc4378357107013ebce21 (patch) | |
tree | 982b30db053dc03921a5c7e55ee3f097e2d8c439 /examples/imapresource/imapserverproxy.cpp | |
parent | f38554ad4c7ff918bf7792413e6599e69d30cf3a (diff) | |
download | sink-a853cb1a32ae9207501fc4378357107013ebce21.tar.gz sink-a853cb1a32ae9207501fc4378357107013ebce21.zip |
Message removals
Diffstat (limited to 'examples/imapresource/imapserverproxy.cpp')
-rw-r--r-- | examples/imapresource/imapserverproxy.cpp | 76 |
1 files changed, 53 insertions, 23 deletions
diff --git a/examples/imapresource/imapserverproxy.cpp b/examples/imapresource/imapserverproxy.cpp index 836a9bc..dfb134c 100644 --- a/examples/imapresource/imapserverproxy.cpp +++ b/examples/imapresource/imapserverproxy.cpp | |||
@@ -24,6 +24,9 @@ | |||
24 | #include <KIMAP/KIMAP/SelectJob> | 24 | #include <KIMAP/KIMAP/SelectJob> |
25 | #include <KIMAP/KIMAP/AppendJob> | 25 | #include <KIMAP/KIMAP/AppendJob> |
26 | #include <KIMAP/KIMAP/CreateJob> | 26 | #include <KIMAP/KIMAP/CreateJob> |
27 | #include <KIMAP/KIMAP/DeleteJob> | ||
28 | #include <KIMAP/KIMAP/StoreJob> | ||
29 | #include <KIMAP/KIMAP/ExpungeJob> | ||
27 | 30 | ||
28 | #include <KIMAP/KIMAP/SessionUiProxy> | 31 | #include <KIMAP/KIMAP/SessionUiProxy> |
29 | #include <KCoreAddons/KJob> | 32 | #include <KCoreAddons/KJob> |
@@ -32,10 +35,15 @@ | |||
32 | 35 | ||
33 | using namespace Imap; | 36 | using namespace Imap; |
34 | 37 | ||
38 | const char* Imap::Flags::Seen = "\\Seen"; | ||
39 | const char* Imap::Flags::Deleted = "\\Deleted"; | ||
40 | const char* Imap::Flags::Answered = "\\Answered"; | ||
41 | const char* Imap::Flags::Flagged = "\\Flagged"; | ||
42 | |||
35 | static KAsync::Job<void> runJob(KJob *job) | 43 | static KAsync::Job<void> runJob(KJob *job) |
36 | { | 44 | { |
37 | return KAsync::start<void>([job](KAsync::Future<void> &future) { | 45 | return KAsync::start<void>([job](KAsync::Future<void> &future) { |
38 | QObject::connect(job, &KJob::result, job, [&future](KJob *job) { | 46 | QObject::connect(job, &KJob::result, [&future](KJob *job) { |
39 | if (job->error()) { | 47 | if (job->error()) { |
40 | Warning() << "Job failed: " << job->errorString(); | 48 | Warning() << "Job failed: " << job->errorString(); |
41 | future.setError(job->error(), job->errorString()); | 49 | future.setError(job->error(), job->errorString()); |
@@ -62,9 +70,6 @@ ImapServerProxy::ImapServerProxy(const QString &serverUrl, int port) : mSession( | |||
62 | 70 | ||
63 | KAsync::Job<void> ImapServerProxy::login(const QString &username, const QString &password) | 71 | KAsync::Job<void> ImapServerProxy::login(const QString &username, const QString &password) |
64 | { | 72 | { |
65 | if (mSession->state() == KIMAP::Session::State::Authenticated || mSession->state() == KIMAP::Session::State::Selected) { | ||
66 | return KAsync::null<void>(); | ||
67 | } | ||
68 | auto loginJob = new KIMAP::LoginJob(mSession); | 73 | auto loginJob = new KIMAP::LoginJob(mSession); |
69 | loginJob->setUserName(username); | 74 | loginJob->setUserName(username); |
70 | loginJob->setPassword(password); | 75 | loginJob->setPassword(password); |
@@ -75,9 +80,6 @@ KAsync::Job<void> ImapServerProxy::login(const QString &username, const QString | |||
75 | 80 | ||
76 | KAsync::Job<void> ImapServerProxy::select(const QString &mailbox) | 81 | KAsync::Job<void> ImapServerProxy::select(const QString &mailbox) |
77 | { | 82 | { |
78 | if (mSession->state() == KIMAP::Session::State::Disconnected) { | ||
79 | return KAsync::error<void>(1, "Not connected"); | ||
80 | } | ||
81 | auto select = new KIMAP::SelectJob(mSession); | 83 | auto select = new KIMAP::SelectJob(mSession); |
82 | select->setMailBox(mailbox); | 84 | select->setMailBox(mailbox); |
83 | // select->setCondstoreEnabled(serverSupportsCondstore()); | 85 | // select->setCondstoreEnabled(serverSupportsCondstore()); |
@@ -86,9 +88,6 @@ KAsync::Job<void> ImapServerProxy::select(const QString &mailbox) | |||
86 | 88 | ||
87 | KAsync::Job<void> ImapServerProxy::append(const QString &mailbox, const QByteArray &content, const QList<QByteArray> &flags, const QDateTime &internalDate) | 89 | KAsync::Job<void> ImapServerProxy::append(const QString &mailbox, const QByteArray &content, const QList<QByteArray> &flags, const QDateTime &internalDate) |
88 | { | 90 | { |
89 | if (mSession->state() == KIMAP::Session::State::Disconnected) { | ||
90 | return KAsync::error<void>(1, "Not connected"); | ||
91 | } | ||
92 | auto append = new KIMAP::AppendJob(mSession); | 91 | auto append = new KIMAP::AppendJob(mSession); |
93 | append->setMailBox(mailbox); | 92 | append->setMailBox(mailbox); |
94 | append->setContent(content); | 93 | append->setContent(content); |
@@ -97,21 +96,38 @@ KAsync::Job<void> ImapServerProxy::append(const QString &mailbox, const QByteArr | |||
97 | return runJob(append); | 96 | return runJob(append); |
98 | } | 97 | } |
99 | 98 | ||
99 | KAsync::Job<void> ImapServerProxy::store(const KIMAP::ImapSet &set, const QList<QByteArray> &flags) | ||
100 | { | ||
101 | auto store = new KIMAP::StoreJob(mSession); | ||
102 | store->setUidBased(true); | ||
103 | store->setSequenceSet(set); | ||
104 | store->setFlags(flags); | ||
105 | store->setMode(KIMAP::StoreJob::AppendFlags); | ||
106 | return runJob(store); | ||
107 | } | ||
108 | |||
100 | KAsync::Job<void> ImapServerProxy::create(const QString &mailbox) | 109 | KAsync::Job<void> ImapServerProxy::create(const QString &mailbox) |
101 | { | 110 | { |
102 | if (mSession->state() == KIMAP::Session::State::Disconnected) { | ||
103 | return KAsync::error<void>(1, "Not connected"); | ||
104 | } | ||
105 | auto create = new KIMAP::CreateJob(mSession); | 111 | auto create = new KIMAP::CreateJob(mSession); |
106 | create->setMailBox(mailbox); | 112 | create->setMailBox(mailbox); |
107 | return runJob(create); | 113 | return runJob(create); |
108 | } | 114 | } |
109 | 115 | ||
116 | KAsync::Job<void> ImapServerProxy::remove(const QString &mailbox) | ||
117 | { | ||
118 | auto job = new KIMAP::DeleteJob(mSession); | ||
119 | job->setMailBox(mailbox); | ||
120 | return runJob(job); | ||
121 | } | ||
122 | |||
123 | KAsync::Job<void> ImapServerProxy::expunge() | ||
124 | { | ||
125 | auto job = new KIMAP::ExpungeJob(mSession); | ||
126 | return runJob(job); | ||
127 | } | ||
128 | |||
110 | KAsync::Job<void> ImapServerProxy::fetch(const KIMAP::ImapSet &set, KIMAP::FetchJob::FetchScope scope, FetchCallback callback) | 129 | KAsync::Job<void> ImapServerProxy::fetch(const KIMAP::ImapSet &set, KIMAP::FetchJob::FetchScope scope, FetchCallback callback) |
111 | { | 130 | { |
112 | if (mSession->state() == KIMAP::Session::State::Disconnected) { | ||
113 | return KAsync::error<void>(1, "Not connected"); | ||
114 | } | ||
115 | auto fetch = new KIMAP::FetchJob(mSession); | 131 | auto fetch = new KIMAP::FetchJob(mSession); |
116 | fetch->setSequenceSet(set); | 132 | fetch->setSequenceSet(set); |
117 | fetch->setUidBased(true); | 133 | fetch->setUidBased(true); |
@@ -164,34 +180,48 @@ KAsync::Job<void> ImapServerProxy::list(KIMAP::ListJob::Option option, const std | |||
164 | // listJob->setQueriedNamespaces(serverNamespaces()); | 180 | // listJob->setQueriedNamespaces(serverNamespaces()); |
165 | QObject::connect(listJob, &KIMAP::ListJob::mailBoxesReceived, | 181 | QObject::connect(listJob, &KIMAP::ListJob::mailBoxesReceived, |
166 | listJob, callback); | 182 | listJob, callback); |
183 | //Figure out the separator character on the first list issued. | ||
184 | if (mSeparatorCharacter.isNull()) { | ||
185 | QObject::connect(listJob, &KIMAP::ListJob::mailBoxesReceived, | ||
186 | listJob, [this](const QList<KIMAP::MailBoxDescriptor> &mailboxes,const QList<QList<QByteArray> > &flags) { | ||
187 | if (!mailboxes.isEmpty() && mSeparatorCharacter.isNull()) { | ||
188 | mSeparatorCharacter = mailboxes.first().separator; | ||
189 | } | ||
190 | } | ||
191 | ); | ||
192 | } | ||
167 | return runJob(listJob); | 193 | return runJob(listJob); |
168 | } | 194 | } |
169 | 195 | ||
196 | KAsync::Job<void> ImapServerProxy::remove(const QString &mailbox, const QByteArray &imapSet) | ||
197 | { | ||
198 | const auto set = KIMAP::ImapSet::fromImapSequenceSet(imapSet); | ||
199 | return select(mailbox).then<void>(store(set, QByteArrayList() << Flags::Deleted)).then<void>(expunge()); | ||
200 | } | ||
201 | |||
170 | KAsync::Future<void> ImapServerProxy::fetchFolders(std::function<void(const QVector<Folder> &)> callback) | 202 | KAsync::Future<void> ImapServerProxy::fetchFolders(std::function<void(const QVector<Folder> &)> callback) |
171 | { | 203 | { |
172 | Trace() << "Fetching folders"; | 204 | Trace() << "Fetching folders"; |
173 | auto job = login("doe", "doe").then<void>(list(KIMAP::ListJob::IncludeUnsubscribed, [callback](const QList<KIMAP::MailBoxDescriptor> &mailboxes, const QList<QList<QByteArray> > &flags){ | 205 | auto job = list(KIMAP::ListJob::IncludeUnsubscribed, [callback](const QList<KIMAP::MailBoxDescriptor> &mailboxes, const QList<QList<QByteArray> > &flags){ |
174 | QVector<Folder> list; | 206 | QVector<Folder> list; |
175 | for (const auto &mailbox : mailboxes) { | 207 | for (const auto &mailbox : mailboxes) { |
176 | Trace() << "Found mailbox: " << mailbox.name; | 208 | Trace() << "Found mailbox: " << mailbox.name; |
177 | list << Folder{mailbox.name.split(mailbox.separator)}; | 209 | list << Folder{mailbox.name.split(mailbox.separator)}; |
178 | } | 210 | } |
179 | callback(list); | 211 | callback(list); |
180 | }), | ||
181 | [](int errorCode, const QString &errorString) { | ||
182 | Warning() << "Failed to list folders: " << errorCode << errorString; | ||
183 | }); | 212 | }); |
184 | return job.exec(); | 213 | return job.exec(); |
185 | } | 214 | } |
186 | 215 | ||
187 | KAsync::Future<void> ImapServerProxy::fetchMessages(const Folder &folder, std::function<void(const QVector<Message> &)> callback) | 216 | KAsync::Future<void> ImapServerProxy::fetchMessages(const Folder &folder, std::function<void(const QVector<Message> &)> callback) |
188 | { | 217 | { |
189 | //TODO use the right separator | 218 | Q_ASSERT(!mSeparatorCharacter.isNull()); |
190 | auto job = login("doe", "doe").then<void>(select(folder.pathParts.join('.'))).then<void, KAsync::Job<void>>([this, callback, folder]() -> KAsync::Job<void> { | 219 | auto job = select(folder.pathParts.join(mSeparatorCharacter)).then<void, KAsync::Job<void>>([this, callback, folder]() -> KAsync::Job<void> { |
191 | return fetchHeaders(folder.pathParts.join('.')).then<void, KAsync::Job<void>, QList<qint64>>([this, callback](const QList<qint64> &uidsToFetch){ | 220 | return fetchHeaders(folder.pathParts.join(mSeparatorCharacter)).then<void, KAsync::Job<void>, QList<qint64>>([this, callback](const QList<qint64> &uidsToFetch){ |
192 | Trace() << "Uids to fetch: " << uidsToFetch; | 221 | Trace() << "Uids to fetch: " << uidsToFetch; |
193 | if (uidsToFetch.isEmpty()) { | 222 | if (uidsToFetch.isEmpty()) { |
194 | Trace() << "Nothing to fetch"; | 223 | Trace() << "Nothing to fetch"; |
224 | callback(QVector<Message>()); | ||
195 | return KAsync::null<void>(); | 225 | return KAsync::null<void>(); |
196 | } | 226 | } |
197 | KIMAP::FetchJob::FetchScope scope; | 227 | KIMAP::FetchJob::FetchScope scope; |