diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2017-02-08 14:09:43 +0100 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2017-02-08 14:09:43 +0100 |
commit | a41d7ee237918584ea45405d20dee4f680fe7071 (patch) | |
tree | aa87ba170b2f5b5e9f67254dd598193cd4c15987 /examples/imapresource/imapserverproxy.h | |
parent | a21368827de7a2d70ef2a260cb35c25fcf5967e8 (diff) | |
download | sink-a41d7ee237918584ea45405d20dee4f680fe7071.tar.gz sink-a41d7ee237918584ea45405d20dee4f680fe7071.zip |
Added session cache.
So we can avoid logging in for every command.
Diffstat (limited to 'examples/imapresource/imapserverproxy.h')
-rw-r--r-- | examples/imapresource/imapserverproxy.h | 87 |
1 files changed, 80 insertions, 7 deletions
diff --git a/examples/imapresource/imapserverproxy.h b/examples/imapresource/imapserverproxy.h index 6785fc2..081750f 100644 --- a/examples/imapresource/imapserverproxy.h +++ b/examples/imapresource/imapserverproxy.h | |||
@@ -124,19 +124,91 @@ struct SelectResult { | |||
124 | quint64 highestModSequence; | 124 | quint64 highestModSequence; |
125 | }; | 125 | }; |
126 | 126 | ||
127 | class Namespaces { | ||
128 | public: | ||
129 | QList<KIMAP2::MailBoxDescriptor> personal; | ||
130 | QList<KIMAP2::MailBoxDescriptor> shared; | ||
131 | QList<KIMAP2::MailBoxDescriptor> user; | ||
132 | |||
133 | KIMAP2::MailBoxDescriptor getDefaultNamespace() | ||
134 | { | ||
135 | return personal.isEmpty() ? KIMAP2::MailBoxDescriptor{} : personal.first(); | ||
136 | } | ||
137 | |||
138 | KIMAP2::MailBoxDescriptor getNamespace(const QString &mailbox) | ||
139 | { | ||
140 | for (const auto &ns : personal) { | ||
141 | if (mailbox.startsWith(ns.name)) { | ||
142 | return ns; | ||
143 | } | ||
144 | } | ||
145 | for (const auto &ns : shared) { | ||
146 | if (mailbox.startsWith(ns.name)) { | ||
147 | return ns; | ||
148 | } | ||
149 | } | ||
150 | for (const auto &ns : user) { | ||
151 | if (mailbox.startsWith(ns.name)) { | ||
152 | return ns; | ||
153 | } | ||
154 | } | ||
155 | return KIMAP2::MailBoxDescriptor{}; | ||
156 | } | ||
157 | }; | ||
158 | |||
159 | class CachedSession { | ||
160 | public: | ||
161 | |||
162 | CachedSession() = default; | ||
163 | CachedSession(KIMAP2::Session *session, const QStringList &cap, const Namespaces &ns) : mSession(session), mCapabilities(cap), mNamespaces(ns) | ||
164 | { | ||
165 | } | ||
166 | |||
167 | bool isConnected() | ||
168 | { | ||
169 | return (mSession->state() == KIMAP2::Session::State::Authenticated || mSession->state() == KIMAP2::Session::State::Selected) ; | ||
170 | } | ||
171 | |||
172 | bool isValid() | ||
173 | { | ||
174 | return mSession; | ||
175 | } | ||
176 | |||
177 | KIMAP2::Session *mSession = nullptr; | ||
178 | QStringList mCapabilities; | ||
179 | Namespaces mNamespaces; | ||
180 | }; | ||
181 | |||
182 | class SessionCache : public QObject { | ||
183 | Q_OBJECT | ||
184 | public: | ||
185 | void recycleSession(const CachedSession &session) | ||
186 | { | ||
187 | mSessions << session; | ||
188 | } | ||
189 | |||
190 | CachedSession getSession() | ||
191 | { | ||
192 | while (!mSessions.isEmpty()) { | ||
193 | auto session = mSessions.takeLast(); | ||
194 | if (session.isConnected()) { | ||
195 | return session; | ||
196 | } | ||
197 | } | ||
198 | return CachedSession{}; | ||
199 | } | ||
200 | private: | ||
201 | QList<CachedSession> mSessions; | ||
202 | }; | ||
203 | |||
127 | class ImapServerProxy { | 204 | class ImapServerProxy { |
128 | KIMAP2::Session *mSession; | 205 | KIMAP2::Session *mSession; |
129 | QStringList mCapabilities; | 206 | QStringList mCapabilities; |
207 | Namespaces mNamespaces; | ||
130 | 208 | ||
131 | QSet<QString> mPersonalNamespaces; | ||
132 | QChar mPersonalNamespaceSeparator; | ||
133 | QSet<QString> mSharedNamespaces; | ||
134 | QChar mSharedNamespaceSeparator; | ||
135 | QSet<QString> mUserNamespaces; | ||
136 | QChar mUserNamespaceSeparator; | ||
137 | 209 | ||
138 | public: | 210 | public: |
139 | ImapServerProxy(const QString &serverUrl, int port); | 211 | ImapServerProxy(const QString &serverUrl, int port, SessionCache *sessionCache = nullptr); |
140 | 212 | ||
141 | //Standard IMAP calls | 213 | //Standard IMAP calls |
142 | KAsync::Job<void> login(const QString &username, const QString &password); | 214 | KAsync::Job<void> login(const QString &username, const QString &password); |
@@ -186,6 +258,7 @@ public: | |||
186 | private: | 258 | private: |
187 | QString getNamespace(const QString &name); | 259 | QString getNamespace(const QString &name); |
188 | QObject mGuard; | 260 | QObject mGuard; |
261 | SessionCache *mSessionCache; | ||
189 | }; | 262 | }; |
190 | 263 | ||
191 | } | 264 | } |