| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). |
| 4 | ** Contact: http://www.qt-project.org/legal |
| 5 | ** |
| 6 | ** This file is part of the Qt Messaging Framework. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and Digia. For licensing terms and |
| 14 | ** conditions see http://qt.digia.com/licensing. For further information |
| 15 | ** use the contact form at http://qt.digia.com/contact-us. |
| 16 | ** |
| 17 | ** GNU Lesser General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 19 | ** General Public License version 2.1 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 2.1 requirements |
| 23 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
| 24 | ** |
| 25 | ** In addition, as a special exception, Digia gives you certain additional |
| 26 | ** rights. These rights are described in the Digia Qt LGPL Exception |
| 27 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
| 28 | ** |
| 29 | ** GNU General Public License Usage |
| 30 | ** Alternatively, this file may be used under the terms of the GNU |
| 31 | ** General Public License version 3.0 as published by the Free Software |
| 32 | ** Foundation and appearing in the file LICENSE.GPL included in the |
| 33 | ** packaging of this file. Please review the following information to |
| 34 | ** ensure the GNU General Public License version 3.0 requirements will be |
| 35 | ** met: http://www.gnu.org/copyleft/gpl.html. |
| 36 | ** |
| 37 | ** |
| 38 | ** $QT_END_LICENSE$ |
| 39 | ** |
| 40 | ****************************************************************************/ |
| 41 | |
| 42 | |
| 43 | #include "servicehandler.h" |
| 44 | #include <longstream_p.h> |
| 45 | #include <QDataStream> |
| 46 | #include <QIODevice> |
| 47 | #include <qmailmessageserver.h> |
| 48 | #include <qmailserviceconfiguration.h> |
| 49 | #include <qmailstore.h> |
| 50 | #include <qmaildisconnected.h> |
| 51 | #include <qmaillog.h> |
| 52 | #include <qmailmessage.h> |
| 53 | #include <qmailcontentmanager.h> |
| 54 | #include <qmailnamespace.h> |
| 55 | #include <QCoreApplication> |
| 56 | #include <QDir> |
| 57 | #include <QDateTime> |
| 58 | #include <QTimer> |
| 59 | |
| 60 | // Account preparation is handled by an external function |
| 61 | extern void prepareAccounts(); |
| 62 | |
| 63 | namespace { |
| 64 | |
| 65 | template <typename T1> |
| 66 | QByteArray serialize(const T1& v1) |
| 67 | { |
| 68 | QByteArray data; |
| 69 | QDataStream os(&data, QIODevice::WriteOnly); |
| 70 | os << v1; |
| 71 | return data; |
| 72 | } |
| 73 | |
| 74 | template <typename T1, typename T2> |
| 75 | QByteArray serialize(const T1& v1, const T2& v2) |
| 76 | { |
| 77 | QByteArray data; |
| 78 | QDataStream os(&data, QIODevice::WriteOnly); |
| 79 | os << v1 << v2; |
| 80 | return data; |
| 81 | } |
| 82 | |
| 83 | template <typename T1, typename T2, typename T3> |
| 84 | QByteArray serialize(const T1& v1, const T2& v2, const T3& v3) |
| 85 | { |
| 86 | QByteArray data; |
| 87 | QDataStream os(&data, QIODevice::WriteOnly); |
| 88 | os << v1 << v2 << v3; |
| 89 | return data; |
| 90 | } |
| 91 | |
| 92 | template <typename T1, typename T2, typename T3, typename T4> |
| 93 | QByteArray serialize(const T1& v1, const T2& v2, const T3& v3, const T4& v4) |
| 94 | { |
| 95 | QByteArray data; |
| 96 | QDataStream os(&data, QIODevice::WriteOnly); |
| 97 | os << v1 << v2 << v3 << v4; |
| 98 | return data; |
| 99 | } |
| 100 | |
| 101 | template <typename T1, typename T2, typename T3, typename T4, typename T5> |
| 102 | QByteArray serialize(const T1& v1, const T2& v2, const T3& v3, const T4& v4, const T5& v5) |
| 103 | { |
| 104 | QByteArray data; |
| 105 | QDataStream os(&data, QIODevice::WriteOnly); |
| 106 | os << v1 << v2 << v3 << v4 << v5; |
| 107 | return data; |
| 108 | } |
| 109 | |
| 110 | template <typename T1, typename T2, typename T3, typename T4, typename T5, typename T6> |
| 111 | QByteArray serialize(const T1& v1, const T2& v2, const T3& v3, const T4& v4, const T5& v5, const T6& v6) |
| 112 | { |
| 113 | QByteArray data; |
| 114 | QDataStream os(&data, QIODevice::WriteOnly); |
| 115 | os << v1 << v2 << v3 << v4 << v5 << v6; |
| 116 | return data; |
| 117 | } |
| 118 | |
| 119 | template <typename T1> |
| 120 | void deserialize(const QByteArray &data, T1& v1) |
| 121 | { |
| 122 | QDataStream is(data); |
| 123 | is >> v1; |
| 124 | } |
| 125 | |
| 126 | template <typename T1, typename T2> |
| 127 | void deserialize(const QByteArray &data, T1& v1, T2& v2) |
| 128 | { |
| 129 | QDataStream is(data); |
| 130 | is >> v1 >> v2; |
| 131 | } |
| 132 | |
| 133 | template <typename T1, typename T2, typename T3> |
| 134 | void deserialize(const QByteArray &data, T1& v1, T2& v2, T3& v3) |
| 135 | { |
| 136 | QDataStream is(data); |
| 137 | is >> v1 >> v2 >> v3; |
| 138 | } |
| 139 | |
| 140 | template <typename T1, typename T2, typename T3, typename T4> |
| 141 | void deserialize(const QByteArray &data, T1& v1, T2& v2, T3& v3, T4& v4) |
| 142 | { |
| 143 | QDataStream is(data); |
| 144 | is >> v1 >> v2 >> v3 >> v4; |
| 145 | } |
| 146 | |
| 147 | template <typename T1, typename T2, typename T3, typename T4, typename T5> |
| 148 | void deserialize(const QByteArray &data, T1& v1, T2& v2, T3& v3, T4& v4, T5& v5) |
| 149 | { |
| 150 | QDataStream is(data); |
| 151 | is >> v1 >> v2 >> v3 >> v4 >> v5; |
| 152 | } |
| 153 | |
| 154 | template <typename T1, typename T2, typename T3, typename T4, typename T5, typename T6> |
| 155 | void deserialize(const QByteArray &data, T1& v1, T2& v2, T3& v3, T4& v4, T5& v5, T6& v6) |
| 156 | { |
| 157 | QDataStream is(data); |
| 158 | is >> v1 >> v2 >> v3 >> v4 >> v5 >> v6; |
| 159 | } |
| 160 | |
| 161 | QSet<QMailAccountId> messageAccounts(const QMailMessageIdList &ids) |
| 162 | { |
| 163 | QSet<QMailAccountId> accountIds; // accounts that own these messages |
| 164 | |
| 165 | foreach (const QMailMessageMetaData &metaData, QMailStore::instance()->messagesMetaData( |
| 166 | QMailMessageKey::id(ids), |
| 167 | QMailMessageKey::ParentAccountId, |
| 168 | QMailStore::ReturnDistinct)) |
| 169 | { |
| 170 | if (metaData.parentAccountId().isValid()) |
| 171 | accountIds.insert(metaData.parentAccountId()); |
| 172 | } |
| 173 | |
| 174 | return accountIds; |
| 175 | } |
| 176 | |
| 177 | QSet<QMailAccountId> messageAccount(const QMailMessageId &id) |
| 178 | { |
| 179 | return messageAccounts(QMailMessageIdList() << id); |
| 180 | } |
| 181 | |
| 182 | QSet<QMailAccountId> folderAccounts(const QMailFolderIdList &ids) |
| 183 | { |
| 184 | // Find the accounts that own these folders |
| 185 | QSet<QMailAccountId> accountIds; |
| 186 | if (!ids.isEmpty()) { |
| 187 | foreach (const QMailFolderId &folderId, ids) { |
| 188 | QMailFolder folder(folderId); |
| 189 | if (folder.id().isValid() && folder.parentAccountId().isValid()) |
| 190 | accountIds.insert(folder.parentAccountId()); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | return accountIds; |
| 195 | } |
| 196 | |
| 197 | QSet<QMailAccountId> folderAccount(const QMailFolderId &id) |
| 198 | { |
| 199 | return folderAccounts(QMailFolderIdList() << id); |
| 200 | } |
| 201 | |
| 202 | QMap<QMailAccountId, QMailMessageIdList> accountMessages(const QMailMessageIdList &ids) |
| 203 | { |
| 204 | // Allocate each message to the relevant account |
| 205 | QMap<QMailAccountId, QMailMessageIdList> map; |
| 206 | |
| 207 | foreach (const QMailMessageMetaData &metaData, QMailStore::instance()->messagesMetaData(QMailMessageKey::id(ids), |
| 208 | QMailMessageKey::Id | QMailMessageKey::ParentAccountId, |
| 209 | QMailStore::ReturnAll)) { |
| 210 | if (metaData.id().isValid() && metaData.parentAccountId().isValid()) |
| 211 | map[metaData.parentAccountId()].append(metaData.id()); |
| 212 | } |
| 213 | |
| 214 | return map; |
| 215 | } |
| 216 | |
| 217 | quint64 newLocalActionId() { |
| 218 | static quint64 id(QCoreApplication::applicationPid() << 32); |
| 219 | return ++id; |
| 220 | } |
| 221 | |
| 222 | struct ResolverSet |
| 223 | { |
| 224 | QMap<QMailAccountId, QList<QPair<QMailMessagePart::Location, QMailMessagePart::Location> > > map; |
| 225 | |
| 226 | void append(const QMailMessageMetaData &metaData) |
| 227 | { |
| 228 | QMailMessagePart::Location location; |
| 229 | location.setContainingMessageId(metaData.id()); |
| 230 | |
| 231 | map[metaData.parentAccountId()].append(qMakePair(location, QMailMessagePart::Location())); |
| 232 | } |
| 233 | |
| 234 | bool operator()(const QMailMessagePart &part) |
| 235 | { |
| 236 | if ((part.referenceType() != QMailMessagePart::None) && part.referenceResolution().isEmpty()) { |
| 237 | // We need to resolve this part's reference |
| 238 | if (part.referenceType() == QMailMessagePart::MessageReference) { |
| 239 | // Link this message to the referenced message |
| 240 | QMailMessageMetaData referencedMessage(part.messageReference()); |
| 241 | |
| 242 | QMailMessagePart::Location location; |
| 243 | location.setContainingMessageId(referencedMessage.id()); |
| 244 | map[referencedMessage.parentAccountId()].append(qMakePair(location, part.location())); |
| 245 | } else if (part.referenceType() == QMailMessagePart::PartReference) { |
| 246 | // Link this message to the referenced part's location |
| 247 | QMailMessageMetaData referencedMessage(part.partReference().containingMessageId()); |
| 248 | |
| 249 | QMailMessagePart::Location location(part.partReference()); |
| 250 | map[referencedMessage.parentAccountId()].append(qMakePair(location, part.location())); |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | return true; |
| 255 | } |
| 256 | }; |
| 257 | |
| 258 | QMap<QMailAccountId, QList<QPair<QMailMessagePart::Location, QMailMessagePart::Location> > > messageResolvers(const QMailMessageIdList &ids) |
| 259 | { |
| 260 | // Find all the unresolved references in these messages |
| 261 | ResolverSet set; |
| 262 | |
| 263 | foreach (const QMailMessageId id, ids) { |
| 264 | QMailMessage outgoing(id); |
| 265 | |
| 266 | if (outgoing.status() & QMailMessage::HasUnresolvedReferences) { |
| 267 | // Record the locations of parts needed for this message |
| 268 | outgoing.foreachPart<ResolverSet&>(set); |
| 269 | } |
| 270 | if (outgoing.status() & QMailMessage::TransmitFromExternal) { |
| 271 | // Just record this message's location |
| 272 | set.append(outgoing); |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | return set.map; |
| 277 | } |
| 278 | |
| 279 | QSet<QMailAccountId> accountsApplicableTo(QMailMessageKey messagekey, QSet<QMailAccountId> const& total) |
| 280 | { |
| 281 | typedef QPair< QSet<QMailAccountId>, QSet<QMailAccountId> > IncludedExcludedPair; |
| 282 | |
| 283 | struct L { |
| 284 | static IncludedExcludedPair extractAccounts(QMailMessageKey const& key) |
| 285 | { |
| 286 | bool isNegated(key.isNegated()); |
| 287 | |
| 288 | IncludedExcludedPair r; |
| 289 | |
| 290 | QSet<QMailAccountId> & included = isNegated ? r.second : r.first; |
| 291 | QSet<QMailAccountId> & excluded = isNegated ? r.first : r.second; |
| 292 | |
| 293 | foreach(QMailMessageKey::ArgumentType const& arg, key.arguments()) |
| 294 | { |
| 295 | switch (arg.property) |
| 296 | { |
| 297 | case QMailMessageKey::ParentFolderId: |
| 298 | case QMailMessageKey::AncestorFolderIds: |
| 299 | if (arg.op == QMailKey::Equal || arg.op == QMailKey::Includes) { |
| 300 | Q_ASSERT(arg.valueList.count() == 1); |
| 301 | Q_ASSERT(arg.valueList[0].canConvert<QMailFolderId>()); |
| 302 | included.insert(QMailFolder(arg.valueList[0].value<QMailFolderId>()).parentAccountId()); |
| 303 | } else if (arg.op == QMailKey::NotEqual || arg.op == QMailKey::Excludes) { |
| 304 | Q_ASSERT(arg.valueList.count() == 1); |
| 305 | Q_ASSERT(arg.valueList[0].canConvert<QMailFolderId>()); |
| 306 | excluded.insert(QMailFolder(arg.valueList[0].value<QMailFolderId>()).parentAccountId()); |
| 307 | } else { |
| 308 | Q_ASSERT(false); |
| 309 | } |
| 310 | break; |
| 311 | case QMailMessageKey::ParentAccountId: |
| 312 | if (arg.op == QMailKey::Equal || arg.op == QMailKey::Includes) { |
| 313 | Q_ASSERT(arg.valueList.count() == 1); |
| 314 | Q_ASSERT(arg.valueList[0].canConvert<QMailAccountId>()); |
| 315 | included.insert(arg.valueList[0].value<QMailAccountId>()); |
| 316 | } else if (arg.op == QMailKey::NotEqual || arg.op == QMailKey::Excludes) { |
| 317 | Q_ASSERT(arg.valueList.count() == 1); |
| 318 | Q_ASSERT(arg.valueList[0].canConvert<QMailAccountId>()); |
| 319 | excluded.insert(arg.valueList[0].value<QMailAccountId>()); |
| 320 | } else { |
| 321 | Q_ASSERT(false); |
| 322 | } |
| 323 | default: |
| 324 | break; |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | if (key.combiner() == QMailKey::None) { |
| 329 | Q_ASSERT(key.subKeys().size() == 0); |
| 330 | Q_ASSERT(key.arguments().size() <= 1); |
| 331 | } else if (key.combiner() == QMailKey::Or) { |
| 332 | foreach (QMailMessageKey const& k, key.subKeys()) { |
| 333 | IncludedExcludedPair v(extractAccounts(k)); |
| 334 | included.unite(v.first); |
| 335 | excluded.unite(v.second); |
| 336 | } |
| 337 | } else if (key.combiner() == QMailKey::And) { |
| 338 | bool filled(included.size() == 0 && excluded.size() == 0 ? false : true); |
| 339 | |
| 340 | for (QList<QMailMessageKey>::const_iterator it(key.subKeys().begin()) ; it != key.subKeys().end() ; ++it) { |
| 341 | IncludedExcludedPair next(extractAccounts(*it)); |
| 342 | if (next.first.size() != 0 || next.second.size() != 0) { |
| 343 | if (filled) { |
| 344 | included.intersect(next.first); |
| 345 | excluded.intersect(next.second); |
| 346 | } else { |
| 347 | filled = true; |
| 348 | included.unite(next.first); |
| 349 | excluded.unite(next.second); |
| 350 | } |
| 351 | } |
| 352 | } |
| 353 | } else { |
| 354 | Q_ASSERT(false); |
| 355 | } |
| 356 | |
| 357 | return r; |
| 358 | } |
| 359 | }; |
| 360 | |
| 361 | IncludedExcludedPair result(L::extractAccounts(messagekey)); |
| 362 | if (result.first.isEmpty()) { |
| 363 | return total - result.second; |
| 364 | } else { |
| 365 | return (total & result.first) - result.second; |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | struct TextPartSearcher |
| 370 | { |
| 371 | QString text; |
| 372 | |
| 373 | TextPartSearcher(const QString &text) : text(text) {} |
| 374 | |
| 375 | bool operator()(const QMailMessagePart &part) |
| 376 | { |
| 377 | if (part.contentType().type().toLower() == "text") { |
| 378 | if (part.body().data().contains(text, Qt::CaseInsensitive)) { |
| 379 | return false; |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | // Keep searching |
| 384 | return true; |
| 385 | } |
| 386 | }; |
| 387 | |
| 388 | bool messageBodyContainsText(const QMailMessage &message, const QString& text) |
| 389 | { |
| 390 | // Search only messages or message parts that are of type 'text/*' |
| 391 | if (message.hasBody()) { |
| 392 | if (message.contentType().type().toLower() == "text") { |
| 393 | if (message.body().data().contains(text, Qt::CaseInsensitive)) |
| 394 | return true; |
| 395 | } |
| 396 | } else if (message.multipartType() != QMailMessage::MultipartNone) { |
| 397 | TextPartSearcher searcher(text); |
| 398 | if (message.foreachPart<TextPartSearcher&>(searcher) == false) { |
| 399 | return true; |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | return false; |
| 404 | } |
| 405 | |
| 406 | QString requestsFileName() |
| 407 | { |
| 408 | #if defined(CLICK) || defined(SNAP) |
| 409 | return QMail::tempDir() + QStringLiteral("qmf-messageserver-requests"); |
| 410 | #else |
| 411 | return QDir::tempPath() + "/qmf-messageserver-requests"; |
| 412 | #endif |
| 413 | } |
| 414 | |
| 415 | QList<QString> obsoleteContentIdentifiers(QList<QMailMessageMetaData*> list) |
| 416 | { |
| 417 | QList<QString> result; |
| 418 | foreach (QMailMessageMetaData *m, list) { |
| 419 | QString identifier = m->customField("qmf-obsolete-contentid"); |
| 420 | if (!identifier.isEmpty()) { |
| 421 | result.append(identifier); |
| 422 | } |
| 423 | } |
| 424 | return result; |
| 425 | } |
| 426 | |
| 427 | QList<QString> contentIdentifiers(QList<QMailMessageMetaData*> list) |
| 428 | { |
| 429 | QList<QString> result; |
| 430 | foreach (QMailMessageMetaData *m, list) { |
| 431 | QString identifier = m->contentIdentifier(); |
| 432 | if (!identifier.isEmpty()) { |
| 433 | result.append(identifier); |
| 434 | } |
| 435 | } |
| 436 | return result; |
| 437 | } |
| 438 | |
| 439 | const QString DontSend = "dontSend"; |
| 440 | const QString SendFailedTime = "sendFailedTime"; |
| 441 | const uint MaxSendFailedAttempts = 5; |
| 442 | |
| 443 | void markFailedMessage(QMailServerRequestType requestType, const QMailServiceAction::Status & status) |
| 444 | { |
| 445 | switch (requestType) { |
| 446 | case TransmitMessagesRequestType: |
| 447 | { |
| 448 | if (status.messageId.isValid()) { |
| 449 | QMailMessageMetaData metaData(status.messageId); |
| 450 | |
| 451 | if (metaData.customField(DontSend) != "true") { |
| 452 | uint sendFailedCount = metaData.customField(DontSend).toUInt(); |
| 453 | ++sendFailedCount; |
| 454 | if (sendFailedCount >= MaxSendFailedAttempts) { |
| 455 | metaData.setCustomField(DontSend, "true"); |
| 456 | } |
| 457 | else { |
| 458 | metaData.setCustomField(DontSend, QString::number(sendFailedCount)); |
| 459 | } |
| 460 | } |
| 461 | metaData.setCustomField(SendFailedTime, |
| 462 | QString::number(QDateTime::currentDateTimeUtc().toTime_t())); |
| 463 | QMailStore::instance()->updateMessagesMetaData( |
| 464 | QMailMessageKey::id(status.messageId), |
| 465 | QMailMessageKey::Custom, metaData); |
| 466 | } |
| 467 | } |
| 468 | break; |
| 469 | default: |
| 470 | break; |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | } |
| 475 | |
| 476 | |
| 477 | ServiceHandler::MessageSearch::MessageSearch(quint64 action, const QMailMessageIdList &ids, const QString &text) |
| 478 | : _action(action), |
| 479 | _ids(ids), |
| 480 | _text(text), |
| 481 | _active(false), |
| 482 | _total(ids.count()), |
| 483 | _progress(0) |
| 484 | { |
| 485 | } |
| 486 | |
| 487 | quint64 ServiceHandler::MessageSearch::action() const |
| 488 | { |
| 489 | return _action; |
| 490 | } |
| 491 | |
| 492 | const QString &ServiceHandler::MessageSearch::bodyText() const |
| 493 | { |
| 494 | return _text; |
| 495 | } |
| 496 | |
| 497 | bool ServiceHandler::MessageSearch::pending() const |
| 498 | { |
| 499 | return !_active; |
| 500 | } |
| 501 | |
| 502 | void ServiceHandler::MessageSearch::inProgress() |
| 503 | { |
| 504 | _active = true; |
| 505 | } |
| 506 | |
| 507 | bool ServiceHandler::MessageSearch::isEmpty() const |
| 508 | { |
| 509 | return _ids.isEmpty(); |
| 510 | } |
| 511 | |
| 512 | uint ServiceHandler::MessageSearch::total() const |
| 513 | { |
| 514 | return _total; |
| 515 | } |
| 516 | |
| 517 | uint ServiceHandler::MessageSearch::progress() const |
| 518 | { |
| 519 | return _progress; |
| 520 | } |
| 521 | |
| 522 | QMailMessageIdList ServiceHandler::MessageSearch::takeBatch() |
| 523 | { |
| 524 | // TODO: this should be smarter |
| 525 | static const int BatchSize = 10; |
| 526 | |
| 527 | QMailMessageIdList result; |
| 528 | if (_ids.count() <= BatchSize) { |
| 529 | result = _ids; |
| 530 | _ids.clear(); |
| 531 | } else { |
| 532 | result = _ids.mid(0, BatchSize); |
| 533 | _ids = _ids.mid(BatchSize, -1); |
| 534 | } |
| 535 | |
| 536 | _progress += result.count(); |
| 537 | return result; |
| 538 | } |
| 539 | |
| 540 | |
| 541 | ServiceHandler::ServiceHandler(QObject* parent) |
| 542 | : QObject(parent), |
| 543 | _requestsFile(requestsFileName()) |
| 544 | { |
| 545 | LongStream::cleanupTempFiles(); |
| 546 | |
| 547 | ::prepareAccounts(); |
| 548 | |
| 549 | if (QMailStore *store = QMailStore::instance()) { |
| 550 | connect(store, SIGNAL(accountsAdded(QMailAccountIdList)), this, SLOT(accountsAdded(QMailAccountIdList))); |
| 551 | connect(store, SIGNAL(accountsUpdated(QMailAccountIdList)), this, SLOT(accountsUpdated(QMailAccountIdList))); |
| 552 | connect(store, SIGNAL(accountsRemoved(QMailAccountIdList)), this, SLOT(accountsRemoved(QMailAccountIdList))); |
| 553 | |
| 554 | // All necessary accounts now exist - create the service instances |
| 555 | registerAccountServices(store->queryAccounts()); |
| 556 | } |
| 557 | |
| 558 | // See if there are any requests remaining from our previous run |
| 559 | if (_requestsFile.exists()) { |
| 560 | if (!_requestsFile.open(QIODevice::ReadOnly)) { |
| 561 | qWarning() << "Unable to open requests file for read!"; |
| 562 | } else { |
| 563 | // Every request still in the file failed to complete |
| 564 | for (QByteArray line = _requestsFile.readLine(); !line.isEmpty(); line = _requestsFile.readLine()) { |
| 565 | if (quint64 action = line.trimmed().toULongLong()) { |
| 566 | _failedRequests.append(action); |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | _requestsFile.close(); |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | if (!_requestsFile.open(QIODevice::WriteOnly)) { |
| 575 | qWarning() << "Unable to open requests file for write!" << _requestsFile.fileName(); |
| 576 | } else { |
| 577 | if (!_requestsFile.resize(0)) { |
| 578 | qWarning() << "Unable to truncate requests file!"; |
| 579 | } else { |
| 580 | _requestsFile.flush(); |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | if (!_failedRequests.isEmpty()) { |
| 585 | // Allow the clients some time to reconnect, then report our failures |
| 586 | QTimer::singleShot(2000, this, SLOT(reportFailures())); |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | ServiceHandler::~ServiceHandler() |
| 591 | { |
| 592 | foreach (QMailMessageService *service, serviceMap.values()) { |
| 593 | service->cancelOperation(QMailServiceAction::Status::ErrInternalStateReset, tr("Destroying Service handler")); |
| 594 | delete service; |
| 595 | } |
| 596 | |
| 597 | serviceMap.clear(); |
| 598 | } |
| 599 | |
| 600 | void ServiceHandler::registerAccountServices(const QMailAccountIdList &ids) |
| 601 | { |
| 602 | foreach (const QMailAccountId &id, ids) { |
| 603 | QMailAccount account(id); |
| 604 | if (account.status() & QMailAccount::Enabled) { |
| 605 | QMailAccountConfiguration config(id); |
| 606 | |
| 607 | // See if this account is configured to use a master account |
| 608 | QMailServiceConfiguration internalCfg(&config, "qmf"); |
| 609 | QString masterId(internalCfg.value("masterAccountId", "")); |
| 610 | if (!masterId.isEmpty()) { |
| 611 | QMailAccount master(QMailAccountId(masterId.toInt())); |
| 612 | if (master.id().isValid()) { |
| 613 | masterAccount.insert(id, master.id()); |
| 614 | } else { |
| 615 | qWarning() << "Unable to locate master account:" << masterId << "for account:" << id; |
| 616 | } |
| 617 | } else { |
| 618 | foreach (const QString& service, config.services()) { |
| 619 | QMailServiceConfiguration svcCfg(&config, service); |
| 620 | if ((svcCfg.type() != QMailServiceConfiguration::Storage) && |
| 621 | (svcCfg.type() != QMailServiceConfiguration::Unknown)) { |
| 622 | // We need to instantiate this service for this account |
| 623 | registerAccountService(id, svcCfg); |
| 624 | } |
| 625 | } |
| 626 | } |
| 627 | } |
| 628 | } |
| 629 | } |
| 630 | |
| 631 | void ServiceHandler::removeServiceFromActions(QMailMessageService *removeService) |
| 632 | { |
| 633 | if (removeService == NULL) |
| 634 | return; |
| 635 | |
| 636 | for (QMap<quint64, ActionData>::iterator it(mActiveActions.begin()) ; it != mActiveActions.end(); ++it) { |
| 637 | if (it->services.remove(removeService)) { |
| 638 | qDebug() << "Removed service from action"; |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | for (QList<Request>::iterator it(mRequests.begin()); it != mRequests.end();) { |
| 643 | if (it->services.contains(removeService) || it->preconditions.contains(removeService)) |
| 644 | { |
| 645 | reportFailure(it->action, QMailServiceAction::Status::ErrFrameworkFault, tr("Service became unavailable, couldn't dispatch")); |
| 646 | it = mRequests.erase(it); |
| 647 | continue; |
| 648 | } |
| 649 | ++it; |
| 650 | } |
| 651 | } |
| 652 | |
| 653 | void ServiceHandler::deregisterAccountServices(const QMailAccountIdList &ids, QMailServiceAction::Status::ErrorCode code, const QString &text) |
| 654 | { |
| 655 | QMap<QPair<QMailAccountId, QString>, QPointer<QMailMessageService> >::iterator it = serviceMap.begin(); |
| 656 | while (it != serviceMap.end()) { |
| 657 | if (ids.contains(it.key().first)) { |
| 658 | // Remove any services associated with this account |
| 659 | if (QMailMessageService *service = it.value()) { |
| 660 | qDebug() << "Deregistering service:" << service->service() << "for account:" << it.key().first; |
| 661 | service->cancelOperation(code, text); |
| 662 | removeServiceFromActions(service); |
| 663 | delete service; |
| 664 | } |
| 665 | |
| 666 | it = serviceMap.erase(it); |
| 667 | } else { |
| 668 | ++it; |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | foreach (const QMailAccountId &id, ids) { |
| 673 | // Remove trailing references to this account's services |
| 674 | QMap<QMailAccountId, QMailAccountId>::iterator it = masterAccount.find(id); |
| 675 | if (it != masterAccount.end()) { |
| 676 | masterAccount.erase(it); |
| 677 | } else { |
| 678 | if (QMailMessageSource *source = accountSource(id)) |
| 679 | sourceService.remove(source); |
| 680 | |
| 681 | if (QMailMessageSink *sink = accountSink(id)) |
| 682 | sinkService.remove(sink); |
| 683 | |
| 684 | sourceMap.remove(id); |
| 685 | sinkMap.remove(id); |
| 686 | } |
| 687 | } |
| 688 | } |
| 689 | |
| 690 | void ServiceHandler::reregisterAccountServices(QMailAccountIdList ids, QMailServiceAction::Status::ErrorCode code, const QString &text) |
| 691 | { |
| 692 | QMailAccountIdList totalDeregisterAccountList; // Avoid deregistering each account individually (it's more expensive) |
| 693 | |
| 694 | // Remove and re-create these accounts' services (and only if we need to) |
| 695 | foreach(QMailAccountId const& id, ids) { |
| 696 | QMailAccount account(id); |
| 697 | if (account.status() & QMailAccount::Enabled) { |
| 698 | QMailAccountConfiguration config(id); |
| 699 | |
| 700 | // See if this account is configured to use a master account |
| 701 | QMailServiceConfiguration internalCfg(&config, "qmf"); |
| 702 | QString masterId(internalCfg.value("masterAccountId", "")); |
| 703 | if (!masterId.isEmpty()) { |
| 704 | QMailAccount master(QMailAccountId(masterId.toInt())); |
| 705 | if (master.id().isValid()) { |
| 706 | // It's possible that before it was using a normal account |
| 707 | deregisterAccountServices(QMailAccountIdList() << id, code, text); // this can't wait, as it clears the masterAccount list |
| 708 | Q_ASSERT(!masterAccount.contains(id)); // no duplicates, as deregisterAccountServies should remove from this list |
| 709 | masterAccount.insert(id, master.id()); |
| 710 | } else { |
| 711 | qWarning() << "Unable to locate master account:" << masterId << "for account:" << id; |
| 712 | } |
| 713 | } else { // not using a master account |
| 714 | QSet<QString> newServices; |
| 715 | QSet<QString> oldServices; |
| 716 | |
| 717 | foreach (const QString& service, config.services()) { |
| 718 | QMailServiceConfiguration svcCfg(&config, service); |
| 719 | if (svcCfg.type() == QMailServiceConfiguration::Source || svcCfg.type() == QMailServiceConfiguration::Sink || svcCfg.type() == QMailServiceConfiguration::SourceAndSink) { |
| 720 | newServices.insert(service); |
| 721 | } |
| 722 | } |
| 723 | |
| 724 | // First we will go through all services in serviceMap that belong to this account |
| 725 | // and check if they are still valid, if they are -- see if they need reloading |
| 726 | // if they're not, remove them. If there's new ones, add them |
| 727 | for (QMap< QPair<QMailAccountId, QString>, QPointer<QMailMessageService> >::iterator it(serviceMap.begin()); it != serviceMap.end(); ++it) { |
| 728 | if (it.key().first == id) { // we're in the right account |
| 729 | QString serviceName(it.key().second); |
| 730 | QSet<QString>::iterator newServiceIt(newServices.find(serviceName)); |
| 731 | if (newServiceIt != newServices.end()) { |
| 732 | // only some inner details have changed, only re-register if need be |
| 733 | QPointer<QMailMessageService> service(it.value()); |
| 734 | Q_ASSERT(!service.isNull()); |
| 735 | if (service->requiresReregistration()) { |
| 736 | // Ok, we must remove it |
| 737 | qDebug() << "Deregistering service:" << service->service() << "for account:" << it.key().first; |
| 738 | service->cancelOperation(code, text); |
| 739 | removeServiceFromActions(service); |
| 740 | delete service; |
| 741 | } else { |
| 742 | // it could handle this change, we don't need to register a new one |
| 743 | newServices.erase(newServiceIt); |
| 744 | } |
| 745 | } else { |
| 746 | // It was using a service, that it is no longer using |
| 747 | oldServices.insert(serviceName); |
| 748 | } |
| 749 | } |
| 750 | } |
| 751 | |
| 752 | foreach(QString const& oldService, oldServices) { |
| 753 | deregisterAccountService(id, oldService, code, text); |
| 754 | } |
| 755 | foreach(QString const& newService, newServices) { |
| 756 | registerAccountService(id, QMailServiceConfiguration(&config, newService)); |
| 757 | } |
| 758 | } |
| 759 | } else { // account is not enabled, disable all services |
| 760 | totalDeregisterAccountList.push_back(id); |
| 761 | } |
| 762 | } |
| 763 | |
| 764 | deregisterAccountServices(totalDeregisterAccountList, code, text); |
| 765 | } |
| 766 | |
| 767 | void ServiceHandler::deregisterAccountService(const QMailAccountId &id, const QString &serviceName, QMailServiceAction::Status::ErrorCode code, const QString &text) |
| 768 | { |
| 769 | QMailMessageService *service = 0; |
| 770 | |
| 771 | QMap<QPair<QMailAccountId, QString>, QPointer<QMailMessageService> >::iterator it = serviceMap.begin(); |
| 772 | while (it != serviceMap.end()) { |
| 773 | if (it.key().first == id && it.key().second == serviceName) { |
| 774 | // Remove any services associated with this account |
| 775 | service = it.value(); |
| 776 | qDebug() << "Deregistering service:" << service->service() << "for account:" << it.key().first; |
| 777 | service->cancelOperation(code, text); |
| 778 | removeServiceFromActions(service); |
| 779 | |
| 780 | it = serviceMap.erase(it); |
| 781 | // Hm, probably should be breaking here ... but eh |
| 782 | } else { |
| 783 | ++it; |
| 784 | } |
| 785 | } |
| 786 | |
| 787 | if (service) { |
| 788 | if (service->hasSource()) { |
| 789 | if (QMailMessageSource *source = accountSource(id)) { |
| 790 | sourceService.remove(source); |
| 791 | sourceMap.remove(id); |
| 792 | } |
| 793 | } |
| 794 | if (service->hasSink()) { |
| 795 | if (QMailMessageSink *sink = accountSink(id)) { |
| 796 | sinkService.remove(sink); |
| 797 | sinkMap.remove(id); |
| 798 | } |
| 799 | } |
| 800 | } |
| 801 | |
| 802 | delete service; |
| 803 | } |
| 804 | |
| 805 | void ServiceHandler::accountsAdded(const QMailAccountIdList &ids) |
| 806 | { |
| 807 | registerAccountServices(ids); |
| 808 | } |
| 809 | |
| 810 | void ServiceHandler::accountsUpdated(const QMailAccountIdList &ids) |
| 811 | { |
| 812 | #ifdef USE_ACCOUNTS_QT |
| 813 | QMailAccountIdList idList; |
| 814 | foreach (const QMailAccountId &id, ids) { |
| 815 | if (!sourceMap.contains(id) && !sinkMap.contains(id)) { |
| 816 | idList << id; |
| 817 | } |
| 818 | } |
| 819 | if (!idList.isEmpty()) { |
| 820 | registerAccountServices(idList); |
| 821 | } |
| 822 | |
| 823 | #else |
| 824 | // Only respond to updates that were generated by other processes |
| 825 | if (QMailStore::instance()->asynchronousEmission()) { |
| 826 | reregisterAccountServices(ids, QMailServiceAction::Status::ErrInternalStateReset, tr("Account updated by other process")); |
| 827 | } |
| 828 | #endif |
| 829 | } |
| 830 | |
| 831 | void ServiceHandler::accountsRemoved(const QMailAccountIdList &ids) |
| 832 | { |
| 833 | deregisterAccountServices(ids, QMailServiceAction::Status::ErrInternalStateReset, tr("Account removed")); |
| 834 | foreach (const QMailAccountId &id, ids) { |
| 835 | // remove messages from this account |
| 836 | QMailMessageKey messageKey(QMailMessageKey::parentAccountId(id)); |
| 837 | QMailStore::instance()->removeMessages(messageKey); |
| 838 | // remove folders from this account |
| 839 | QMailFolderKey accountKey(QMailFolderKey::parentAccountId(id)); |
| 840 | QMailStore::instance()->removeFolders(accountKey); |
| 841 | } |
| 842 | } |
| 843 | |
| 844 | QMailAccountId ServiceHandler::transmissionAccountId(const QMailAccountId &accountId) const |
| 845 | { |
| 846 | QMap<QMailAccountId, QMailAccountId>::const_iterator it = masterAccount.find(accountId); |
| 847 | if (it != masterAccount.end()) |
| 848 | return it.value(); |
| 849 | |
| 850 | return accountId; |
| 851 | } |
| 852 | |
| 853 | void ServiceHandler::registerAccountSource(const QMailAccountId &accountId, QMailMessageSource *source, QMailMessageService *service) |
| 854 | { |
| 855 | sourceMap.insert(accountId, source); |
| 856 | sourceService.insert(source, service); |
| 857 | |
| 858 | connect(source, SIGNAL(newMessagesAvailable(quint64)), this, SIGNAL(newMessagesAvailable())); // TODO: <- I don't this this makes much sense.. |
| 859 | connect(source, SIGNAL(newMessagesAvailable()), this, SIGNAL(newMessagesAvailable())); |
| 860 | |
| 861 | // if (service->usesConcurrentActions()) { // This can be uncommented to be stricter. |
| 862 | connect(source, SIGNAL(messagesDeleted(QMailMessageIdList,quint64)), this, SLOT(messagesDeleted(QMailMessageIdList,quint64))); |
| 863 | connect(source, SIGNAL(messagesCopied(QMailMessageIdList, quint64)), this, SLOT(messagesCopied(QMailMessageIdList, quint64))); |
| 864 | connect(source, SIGNAL(messagesMoved(QMailMessageIdList, quint64)), this, SLOT(messagesMoved(QMailMessageIdList, quint64))); |
| 865 | connect(source, SIGNAL(messagesFlagged(QMailMessageIdList, quint64)), this, SLOT(messagesFlagged(QMailMessageIdList, quint64))); |
| 866 | connect(source, SIGNAL(messagesPrepared(QMailMessageIdList, quint64)), this, SLOT(messagesPrepared(QMailMessageIdList, quint64))); |
| 867 | connect(source, SIGNAL(matchingMessageIds(QMailMessageIdList, quint64)), this, SLOT(matchingMessageIds(QMailMessageIdList, quint64))); |
| 868 | connect(source, SIGNAL(remainingMessagesCount(uint, quint64)), this, SLOT(remainingMessagesCount(uint, quint64))); |
| 869 | connect(source, SIGNAL(messagesCount(uint, quint64)), this, SLOT(messagesCount(uint, quint64))); |
| 870 | connect(source, SIGNAL(protocolResponse(QString, QVariant, quint64)), this, SLOT(protocolResponse(QString, QVariant, quint64))); |
| 871 | // } else { |
| 872 | connect(source, SIGNAL(messagesDeleted(QMailMessageIdList)), this, SLOT(messagesDeleted(QMailMessageIdList))); |
| 873 | connect(source, SIGNAL(messagesCopied(QMailMessageIdList)), this, SLOT(messagesCopied(QMailMessageIdList))); |
| 874 | connect(source, SIGNAL(messagesMoved(QMailMessageIdList)), this, SLOT(messagesMoved(QMailMessageIdList))); |
| 875 | connect(source, SIGNAL(messagesFlagged(QMailMessageIdList)), this, SLOT(messagesFlagged(QMailMessageIdList))); |
| 876 | connect(source, SIGNAL(messagesPrepared(QMailMessageIdList)), this, SLOT(messagesPrepared(QMailMessageIdList))); |
| 877 | connect(source, SIGNAL(matchingMessageIds(QMailMessageIdList)), this, SLOT(matchingMessageIds(QMailMessageIdList))); |
| 878 | connect(source, SIGNAL(remainingMessagesCount(uint)), this, SLOT(remainingMessagesCount(uint))); |
| 879 | connect(source, SIGNAL(messagesCount(uint)), this, SLOT(messagesCount(uint))); |
| 880 | connect(source, SIGNAL(protocolResponse(QString, QVariant)), this, SLOT(protocolResponse(QString, QVariant))); |
| 881 | // } |
| 882 | } |
| 883 | |
| 884 | QMailMessageSource *ServiceHandler::accountSource(const QMailAccountId &accountId) const |
| 885 | { |
| 886 | QMap<QMailAccountId, QMailMessageSource*>::const_iterator it = sourceMap.find(transmissionAccountId(accountId)); |
| 887 | if (it != sourceMap.end()) |
| 888 | return *it; |
| 889 | |
| 890 | return 0; |
| 891 | } |
| 892 | |
| 893 | void ServiceHandler::registerAccountSink(const QMailAccountId &accountId, QMailMessageSink *sink, QMailMessageService *service) |
| 894 | { |
| 895 | sinkMap.insert(accountId, sink); |
| 896 | sinkService.insert(sink, service); |
| 897 | |
| 898 | // if (service->usesConcurrentActions()) { // this can be uncommented to be stricter |
| 899 | connect(sink, SIGNAL(messagesTransmitted(QMailMessageIdList, quint64)), this, SLOT(messagesTransmitted(QMailMessageIdList, quint64))); |
| 900 | connect(sink, SIGNAL(messagesFailedTransmission(QMailMessageIdList, QMailServiceAction::Status::ErrorCode, quint64)), |
| 901 | this, SLOT(messagesFailedTransmission(QMailMessageIdList, QMailServiceAction::Status::ErrorCode, quint64))); |
| 902 | |
| 903 | // } else { |
| 904 | connect(sink, SIGNAL(messagesTransmitted(QMailMessageIdList)), this, SLOT(messagesTransmitted(QMailMessageIdList))); |
| 905 | connect(sink, SIGNAL(messagesFailedTransmission(QMailMessageIdList, QMailServiceAction::Status::ErrorCode)), |
| 906 | this, SLOT(messagesFailedTransmission(QMailMessageIdList, QMailServiceAction::Status::ErrorCode))); |
| 907 | // } |
| 908 | } |
| 909 | |
| 910 | QMailMessageSink *ServiceHandler::accountSink(const QMailAccountId &accountId) const |
| 911 | { |
| 912 | QMap<QMailAccountId, QMailMessageSink*>::const_iterator it = sinkMap.find(transmissionAccountId(accountId)); |
| 913 | if (it != sinkMap.end()) |
| 914 | return *it; |
| 915 | |
| 916 | return 0; |
| 917 | } |
| 918 | |
| 919 | QMailMessageService *ServiceHandler::createService(const QString &name, const QMailAccountId &accountId) |
| 920 | { |
| 921 | QMailMessageService *service = QMailMessageServiceFactory::createService(name, accountId); |
| 922 | connect(service, SIGNAL(connectivityChanged(QMailServiceAction::Connectivity)), this, SLOT(connectivityChanged(QMailServiceAction::Connectivity))); |
| 923 | connect(service, SIGNAL(availabilityChanged(bool)), this, SLOT(availabilityChanged(bool))); |
| 924 | |
| 925 | |
| 926 | |
| 927 | if (service) { |
| 928 | // if (service->usesConcurrentActions()) { // this can be uncommented to be stricter |
| 929 | connect(service, SIGNAL(activityChanged(QMailServiceAction::Activity, quint64)), this, SLOT(activityChanged(QMailServiceAction::Activity, quint64))); |
| 930 | connect(service, SIGNAL(statusChanged(const QMailServiceAction::Status, quint64)), this, SLOT(statusChanged(const QMailServiceAction::Status, quint64))); |
| 931 | connect(service, SIGNAL(progressChanged(uint, uint, quint64)), this, SLOT(progressChanged(uint, uint, quint64))); |
| 932 | connect(service, SIGNAL(actionCompleted(bool, quint64)), this, SLOT(actionCompleted(bool, quint64))); |
| 933 | // } else { |
| 934 | connect(service, SIGNAL(activityChanged(QMailServiceAction::Activity)), this, SLOT(activityChanged(QMailServiceAction::Activity))); |
| 935 | connect(service, SIGNAL(statusChanged(const QMailServiceAction::Status)), this, SLOT(statusChanged(const QMailServiceAction::Status))); |
| 936 | connect(service, SIGNAL(progressChanged(uint, uint)), this, SLOT(progressChanged(uint, uint))); |
| 937 | connect(service, SIGNAL(actionCompleted(bool)), this, SLOT(actionCompleted(bool))); |
| 938 | // } |
| 939 | } |
| 940 | |
| 941 | return service; |
| 942 | } |
| 943 | |
| 944 | void ServiceHandler::registerAccountService(const QMailAccountId &accountId, const QMailServiceConfiguration &svcCfg) |
| 945 | { |
| 946 | QMap<QPair<QMailAccountId, QString>, QPointer<QMailMessageService> >::const_iterator it = serviceMap.find(qMakePair(accountId, svcCfg.service())); |
| 947 | if (it == serviceMap.end()) { |
| 948 | // We need to create a new service for this account |
| 949 | if (QMailMessageService *service = createService(svcCfg.service(), accountId)) { |
| 950 | serviceMap.insert(qMakePair(accountId, svcCfg.service()), service); |
| 951 | qDebug() << "Registering service:" << svcCfg.service() << "for account:" << accountId; |
| 952 | |
| 953 | if (service->hasSource()) |
| 954 | registerAccountSource(accountId, &service->source(), service); |
| 955 | |
| 956 | if (service->hasSink()) |
| 957 | registerAccountSink(accountId, &service->sink(), service); |
| 958 | |
| 959 | if (!service->available()) |
| 960 | mUnavailableServices.insert(service); |
| 961 | } else { |
| 962 | qWarning() << "Unable to instantiate service:" << svcCfg.service(); |
| 963 | } |
| 964 | } |
| 965 | } |
| 966 | |
| 967 | bool ServiceHandler::servicesAvailable(const Request &req) const |
| 968 | { |
| 969 | foreach(QPointer<QMailMessageService> service, (req.services + req.preconditions)) { |
| 970 | if (!serviceAvailable(service)) |
| 971 | return false; |
| 972 | } |
| 973 | return true; |
| 974 | } |
| 975 | |
| 976 | bool ServiceHandler::serviceAvailable(QPointer<QMailMessageService> service) const |
| 977 | { |
| 978 | if (!service || (!service->usesConcurrentActions() && mServiceAction.contains(service)) || mUnavailableServices.contains(service)) |
| 979 | return false; |
| 980 | |
| 981 | return true; |
| 982 | } |
| 983 | |
| 984 | QSet<QMailMessageService*> ServiceHandler::sourceServiceSet(const QMailAccountId &id) const |
| 985 | { |
| 986 | QSet<QMailMessageService*> services; |
| 987 | |
| 988 | // See if this account's service has a source |
| 989 | if (QMailMessageSource *source = accountSource(id)) { |
| 990 | services.insert(sourceService.value(source)); |
| 991 | } else { |
| 992 | qWarning() << "Unable to find message source for account:" << id; |
| 993 | } |
| 994 | |
| 995 | return services; |
| 996 | } |
| 997 | |
| 998 | QSet<QMailMessageService*> ServiceHandler::sourceServiceSet(const QSet<QMailAccountId> &ids) const |
| 999 | { |
| 1000 | QSet<QMailMessageService*> services; |
| 1001 | |
| 1002 | foreach (const QMailAccountId &id, ids) { |
| 1003 | // See if this account's service has a source |
| 1004 | if (QMailMessageSource *source = accountSource(id)) { |
| 1005 | services.insert(sourceService.value(source)); |
| 1006 | } else { |
| 1007 | qWarning() << "Unable to find message source for account:" << id; |
| 1008 | return QSet<QMailMessageService*>(); |
| 1009 | } |
| 1010 | } |
| 1011 | |
| 1012 | return services; |
| 1013 | } |
| 1014 | |
| 1015 | QSet<QMailMessageService*> ServiceHandler::sinkServiceSet(const QMailAccountId &id) const |
| 1016 | { |
| 1017 | QSet<QMailMessageService*> services; |
| 1018 | |
| 1019 | if (QMailMessageSink *sink = accountSink(id)) { |
| 1020 | services.insert(sinkService[sink]); |
| 1021 | } else { |
| 1022 | qWarning() << "Unable to find message sink for account:" << id; |
| 1023 | } |
| 1024 | |
| 1025 | return services; |
| 1026 | } |
| 1027 | |
| 1028 | QSet<QMailMessageService*> ServiceHandler::sinkServiceSet(const QSet<QMailAccountId> &ids) const |
| 1029 | { |
| 1030 | QSet<QMailMessageService*> services; |
| 1031 | |
| 1032 | foreach (const QMailAccountId &id, ids) { |
| 1033 | if (QMailMessageSink *sink = accountSink(id)) { |
| 1034 | services.insert(sinkService[sink]); |
| 1035 | } else { |
| 1036 | qWarning() << "Unable to find message sink for account:" << id; |
| 1037 | return QSet<QMailMessageService*>(); |
| 1038 | } |
| 1039 | } |
| 1040 | |
| 1041 | return services; |
| 1042 | } |
| 1043 | |
| 1044 | quint64 ServiceHandler::sourceAction(QMailMessageSource *source) const |
| 1045 | { |
| 1046 | if (source) |
| 1047 | if (QMailMessageService *service = sourceService[source]) |
| 1048 | return serviceAction(service); |
| 1049 | |
| 1050 | return 0; |
| 1051 | } |
| 1052 | |
| 1053 | quint64 ServiceHandler::sinkAction(QMailMessageSink *sink) const |
| 1054 | { |
| 1055 | if (sink) |
| 1056 | if (QMailMessageService *service = sinkService[sink]) |
| 1057 | return serviceAction(service); |
| 1058 | |
| 1059 | return 0; |
| 1060 | } |
| 1061 | |
| 1062 | quint64 ServiceHandler::serviceAction(QMailMessageService *service) const |
| 1063 | { |
| 1064 | QMap<QPointer<QMailMessageService>, quint64>::const_iterator it = mServiceAction.find(service); |
| 1065 | if (it != mServiceAction.end()) |
| 1066 | return it.value(); |
| 1067 | |
| 1068 | return 0; |
| 1069 | } |
| 1070 | |
| 1071 | void ServiceHandler::enqueueRequest(quint64 action, const QByteArray &data, const QSet<QMailMessageService*> &services, RequestServicer servicer, CompletionSignal completion, QMailServerRequestType description, const QSet<QMailMessageService*> &preconditions) |
| 1072 | { |
| 1073 | QSet<QPointer<QMailMessageService> > safeServices; |
| 1074 | QSet<QPointer<QMailMessageService> > safePreconditions; |
| 1075 | Request req; |
| 1076 | foreach(QMailMessageService *service, services) |
| 1077 | safeServices.insert(service); |
| 1078 | foreach(QMailMessageService *precondition, preconditions) |
| 1079 | safePreconditions.insert(precondition); |
| 1080 | req.action = action; |
| 1081 | req.data = data; |
| 1082 | req.services = safeServices; |
| 1083 | req.preconditions = safePreconditions; |
| 1084 | req.servicer = servicer; |
| 1085 | req.completion = completion; |
| 1086 | req.description = description; |
| 1087 | |
| 1088 | mRequests.append(req); |
| 1089 | |
| 1090 | // Add this request to the outstanding list |
| 1091 | if (!_outstandingRequests.contains(action)) { |
| 1092 | _outstandingRequests.insert(action); |
| 1093 | |
| 1094 | QByteArray requestNumber(QByteArray::number(action)); |
| 1095 | _requestsFile.write(requestNumber.append('\n')); |
| 1096 | _requestsFile.flush(); |
| 1097 | } |
| 1098 | |
| 1099 | QTimer::singleShot(0, this, SLOT(dispatchRequest())); |
| 1100 | } |
| 1101 | |
| 1102 | namespace { |
| 1103 | const char* requestTypeNames[] = |
| 1104 | { |
| 1105 | "AcknowledgeNewMessagesRequest", |
| 1106 | "TransmitMessagesRequest", |
| 1107 | "RetrieveFolderListRequest", |
| 1108 | "RetrieveMessageListRequest", |
| 1109 | "RetrieveNewMessagesRequest", |
| 1110 | "RetrieveMessagesRequest", |
| 1111 | "RetrieveMessagePartRequest", |
| 1112 | "RetrieveMessageRangeRequest", |
| 1113 | "RetrieveMessagePartRangeRequest", |
| 1114 | "RetrieveAllRequest", |
| 1115 | "ExportUpdatesRequest", |
| 1116 | "SynchronizeRequest", |
| 1117 | "CopyMessagesRequest", |
| 1118 | "MoveMessagesRequest", |
| 1119 | "FlagMessagesRequest", |
| 1120 | "CreateFolderRequest", |
| 1121 | "RenameFolderRequest", |
| 1122 | "DeleteFolderRequest", |
| 1123 | "CancelTransferRequest", |
| 1124 | "DeleteMessagesRequest", |
| 1125 | "SearchMessagesRequest", |
| 1126 | "CancelSearchRequest", |
| 1127 | "ListActionsRequest", |
| 1128 | "ProtocolRequestRequestType" |
| 1129 | }; |
| 1130 | } |
| 1131 | |
| 1132 | void ServiceHandler::dispatchRequest() |
| 1133 | { |
| 1134 | QList<Request>::iterator request(mRequests.begin()); |
| 1135 | while(request != mRequests.end()) |
| 1136 | { |
| 1137 | if (!servicesAvailable(*request)) { |
| 1138 | ++request; |
| 1139 | continue; |
| 1140 | } |
| 1141 | |
| 1142 | // Limit number of concurrent actions serviced on the device |
| 1143 | if (mActiveActions.count() >= QMail::maximumConcurrentServiceActions()) |
| 1144 | return; |
| 1145 | |
| 1146 | // Limit number of concurrent actions serviced per process |
| 1147 | const quint64 requestProcess = request->action >> 32; |
| 1148 | int requestProcessCount = 1; // including the request |
| 1149 | foreach(quint64 actionId, mActiveActions.keys()) { |
| 1150 | if ((actionId >> 32) == requestProcess) { |
| 1151 | if (++requestProcessCount > QMail::maximumConcurrentServiceActionsPerProcess()) { |
| 1152 | break; |
| 1153 | } |
| 1154 | } |
| 1155 | } |
| 1156 | |
| 1157 | if (requestProcessCount > QMail::maximumConcurrentServiceActionsPerProcess()) { |
| 1158 | ++request; |
| 1159 | continue; |
| 1160 | } |
| 1161 | |
| 1162 | // Associate the services with the action, so that signals are reported correctly |
| 1163 | foreach (QMailMessageService *service, request->services) |
| 1164 | mServiceAction.insert(service, request->action); |
| 1165 | |
| 1166 | // The services required for this request are available |
| 1167 | ActionData data; |
| 1168 | data.services = request->services; |
| 1169 | data.completion = request->completion; |
| 1170 | data.unixTimeExpiry = QDateTime::currentDateTime().toTime_t() + ExpirySeconds; |
| 1171 | data.reported = false; |
| 1172 | data.description = request->description; |
| 1173 | data.progressTotal = 0; |
| 1174 | data.progressCurrent = 0; |
| 1175 | data.status = QMailServiceAction::Status(QMailServiceAction::Status::ErrNoError, QString(), QMailAccountId(), QMailFolderId(), QMailMessageId()); |
| 1176 | |
| 1177 | mActiveActions.insert(request->action, data); |
| 1178 | qDebug() << "Running action" << ::requestTypeNames[data.description] << request->action; |
| 1179 | emit actionStarted(QMailActionData(request->action, request->description, 0, 0, |
| 1180 | data.status.errorCode, data.status.text, |
| 1181 | data.status.accountId, data.status.folderId, data.status.messageId)); |
| 1182 | emit activityChanged(request->action, QMailServiceAction::InProgress); |
| 1183 | |
| 1184 | if ((this->*request->servicer)(request->action, request->data)) { |
| 1185 | // This action is now underway |
| 1186 | |
| 1187 | if (mActionExpiry.isEmpty()) { |
| 1188 | // Start the expiry timer. Convert to miliseconds, and avoid shooting too early |
| 1189 | const int expiryMs = ExpirySeconds * 1000; |
| 1190 | QTimer::singleShot(expiryMs + 50, this, SLOT(expireAction())); |
| 1191 | } |
| 1192 | mActionExpiry.append(request->action); |
| 1193 | } else { |
| 1194 | mActiveActions.remove(request->action); |
| 1195 | |
| 1196 | qWarning() << "Unable to dispatch request:" << request->action << "to services:" << request->services; |
| 1197 | emit activityChanged(request->action, QMailServiceAction::Failed); |
| 1198 | |
| 1199 | foreach (QMailMessageService *service, request->services) |
| 1200 | mServiceAction.remove(service); |
| 1201 | } |
| 1202 | |
| 1203 | request = mRequests.erase(request); |
| 1204 | } |
| 1205 | } |
| 1206 | |
| 1207 | void ServiceHandler::updateAction(quint64 action) |
| 1208 | { |
| 1209 | QLinkedList<quint64>::iterator it = qFind(mActionExpiry.begin(), mActionExpiry.end(), action); |
| 1210 | if (it != mActionExpiry.end()) { |
| 1211 | // Move this action to the end of the list |
| 1212 | mActionExpiry.erase(it); |
| 1213 | mActionExpiry.append(action); |
| 1214 | |
| 1215 | // Update the expiry time for this action |
| 1216 | mActiveActions[action].unixTimeExpiry = QDateTime::currentDateTime().toTime_t() + ExpirySeconds; |
| 1217 | } |
| 1218 | } |
| 1219 | |
| 1220 | void ServiceHandler::expireAction() |
| 1221 | { |
| 1222 | uint now(QDateTime::currentDateTime().toTime_t()); |
| 1223 | |
| 1224 | if (!mActionExpiry.isEmpty()) { |
| 1225 | quint64 action = *mActionExpiry.begin(); |
| 1226 | |
| 1227 | QMap<quint64, ActionData>::iterator it = mActiveActions.find(action); |
| 1228 | if (it != mActiveActions.end()) { |
| 1229 | ActionData &data(it.value()); |
| 1230 | |
| 1231 | // Is the oldest action expired? |
| 1232 | if (data.unixTimeExpiry <= now) { |
| 1233 | qWarning() << "Expired request:" << action; |
| 1234 | reportFailure(action, QMailServiceAction::Status::ErrTimeout, tr("Request is not progressing")); |
| 1235 | emit activityChanged(action, QMailServiceAction::Failed); |
| 1236 | |
| 1237 | QSet<QMailAccountId> serviceAccounts; |
| 1238 | |
| 1239 | bool retrievalSetModified(false); |
| 1240 | bool transmissionSetModified(false); |
| 1241 | |
| 1242 | // Defend against action being removed from mActiveActions when activityChanged signal is emitted |
| 1243 | it = mActiveActions.find(action); |
| 1244 | if (it != mActiveActions.end()) { |
| 1245 | ActionData &data(it.value()); |
| 1246 | // Remove this action |
| 1247 | foreach (QMailMessageService *service, data.services) { |
| 1248 | if (!service) |
| 1249 | continue; |
| 1250 | serviceAccounts.insert(service->accountId()); |
| 1251 | mServiceAction.remove(service); |
| 1252 | |
| 1253 | QMailAccountId accountId(service->accountId()); |
| 1254 | if (accountId.isValid()) { |
| 1255 | if (data.completion == &ServiceHandler::retrievalCompleted) { |
| 1256 | if (_retrievalAccountIds.contains(accountId)) { |
| 1257 | _retrievalAccountIds.remove(accountId); |
| 1258 | retrievalSetModified = true; |
| 1259 | } |
| 1260 | } else if (data.completion == &ServiceHandler::transmissionCompleted) { |
| 1261 | if (_transmissionAccountIds.contains(accountId)) { |
| 1262 | _transmissionAccountIds.remove(accountId); |
| 1263 | transmissionSetModified = true; |
| 1264 | } |
| 1265 | } |
| 1266 | } |
| 1267 | } |
| 1268 | |
| 1269 | if (retrievalSetModified) { |
| 1270 | QMailStore::instance()->setRetrievalInProgress(_retrievalAccountIds.toList()); |
| 1271 | } |
| 1272 | if (transmissionSetModified) { |
| 1273 | QMailStore::instance()->setTransmissionInProgress(_transmissionAccountIds.toList()); |
| 1274 | } |
| 1275 | |
| 1276 | mActiveActions.erase(it); |
| 1277 | } |
| 1278 | |
| 1279 | mActionExpiry.removeFirst(); |
| 1280 | |
| 1281 | // Restart the service(s) for each of these accounts |
| 1282 | QMailAccountIdList ids(serviceAccounts.toList()); |
| 1283 | deregisterAccountServices(ids, QMailServiceAction::Status::ErrTimeout, tr("Request is not progressing")); |
| 1284 | registerAccountServices(ids); |
| 1285 | |
| 1286 | // See if there are more actions to dispatch |
| 1287 | dispatchRequest(); |
| 1288 | } |
| 1289 | } |
| 1290 | } |
| 1291 | |
| 1292 | QLinkedList<quint64>::iterator expiryIt(mActionExpiry.begin()); |
| 1293 | while (expiryIt != mActionExpiry.end()) { |
| 1294 | if (mActiveActions.contains(*expiryIt)) { |
| 1295 | uint nextExpiry(mActiveActions.value(*expiryIt).unixTimeExpiry); |
| 1296 | |
| 1297 | // miliseconds until it expires.. |
| 1298 | uint nextShot(nextExpiry <= now ? 0 : (nextExpiry - now) * 1000 + 50); |
| 1299 | QTimer::singleShot(nextShot, this, SLOT(expireAction())); |
| 1300 | return; |
| 1301 | } else { |
| 1302 | expiryIt = mActionExpiry.erase(expiryIt); // Just remove this non-existent action |
| 1303 | } |
| 1304 | } |
| 1305 | } |
| 1306 | |
| 1307 | // Cancelled by user |
| 1308 | void ServiceHandler::cancelTransfer(quint64 action) |
| 1309 | { |
| 1310 | cancelLocalSearch(action); |
| 1311 | QMap<quint64, ActionData>::iterator it = mActiveActions.find(action); |
| 1312 | if (it != mActiveActions.end()) { |
| 1313 | bool retrievalSetModified(false); |
| 1314 | bool transmissionSetModified(false); |
| 1315 | |
| 1316 | const ActionData &data(it.value()); |
| 1317 | foreach (QMailMessageService *service, data.services) { |
| 1318 | if (service) { |
| 1319 | if (service->usesConcurrentActions()) |
| 1320 | service->cancelOperation(QMailServiceAction::Status::ErrCancel, tr("Cancelled by user"), it.key()); |
| 1321 | else |
| 1322 | service->cancelOperation(QMailServiceAction::Status::ErrCancel, tr("Cancelled by user")); |
| 1323 | } |
| 1324 | mServiceAction.remove(service); |
| 1325 | if (!service) { |
| 1326 | qWarning() << "Unable to cancel null service for action:" << action; |
| 1327 | continue; |
| 1328 | } |
| 1329 | |
| 1330 | QMailAccountId accountId(service->accountId()); |
| 1331 | if (accountId.isValid()) { |
| 1332 | if (data.completion == &ServiceHandler::retrievalCompleted) { |
| 1333 | if (_retrievalAccountIds.contains(accountId)) { |
| 1334 | _retrievalAccountIds.remove(accountId); |
| 1335 | retrievalSetModified = true; |
| 1336 | } |
| 1337 | } else if (data.completion == &ServiceHandler::transmissionCompleted) { |
| 1338 | if (_transmissionAccountIds.contains(accountId)) { |
| 1339 | _transmissionAccountIds.remove(accountId); |
| 1340 | transmissionSetModified = true; |
| 1341 | } |
| 1342 | } |
| 1343 | } |
| 1344 | } |
| 1345 | |
| 1346 | if (retrievalSetModified) { |
| 1347 | QMailStore::instance()->setRetrievalInProgress(_retrievalAccountIds.toList()); |
| 1348 | } |
| 1349 | if (transmissionSetModified) { |
| 1350 | QMailStore::instance()->setTransmissionInProgress(_transmissionAccountIds.toList()); |
| 1351 | } |
| 1352 | |
| 1353 | //The ActionData might have already been deleted by actionCompleted, triggered by cancelOperation |
| 1354 | it = mActiveActions.find(action); |
| 1355 | if (it != mActiveActions.end()) mActiveActions.erase(it); |
| 1356 | |
| 1357 | // See if there are more actions |
| 1358 | QTimer::singleShot(0, this, SLOT(dispatchRequest())); |
| 1359 | } else { |
| 1360 | // See if this is a pending request that we can abort |
| 1361 | QList<Request>::iterator it = mRequests.begin(), end = mRequests.end(); |
| 1362 | for ( ; it != end; ++it) { |
| 1363 | if ((*it).action == action) { |
| 1364 | mRequests.erase(it); |
| 1365 | break; |
| 1366 | } |
| 1367 | } |
| 1368 | |
| 1369 | // Report this action as failed |
| 1370 | reportFailure(action, QMailServiceAction::Status::ErrCancel, tr("Cancelled by user")); |
| 1371 | } |
| 1372 | } |
| 1373 | |
| 1374 | void ServiceHandler::transmitMessages(quint64 action, const QMailAccountId &accountId) |
| 1375 | { |
| 1376 | // Ensure that this account has a sink configured |
| 1377 | QSet<QMailMessageService*> sinks(sinkServiceSet(accountId)); |
| 1378 | if (sinks.isEmpty()) { |
| 1379 | reportFailure(action, QMailServiceAction::Status::ErrNoConnection, tr("Unable to enqueue messages for transmission")); |
| 1380 | } else { |
| 1381 | // We need to see if any sources are required to prepare these messages |
| 1382 | QMailMessageKey accountKey(QMailMessageKey::parentAccountId(accountId)); |
| 1383 | QMailMessageKey outboxKey(QMailMessageKey::status(QMailMessage::Outbox) & ~QMailMessageKey::status(QMailMessage::Trash)); |
| 1384 | |
| 1385 | // We need to prepare messages to: |
| 1386 | // - resolve references |
| 1387 | // - move to the Sent folder prior to transmission |
| 1388 | quint64 preparationStatus(QMailMessage::HasUnresolvedReferences | QMailMessage::TransmitFromExternal); |
| 1389 | QMailMessageKey unresolvedKey(QMailMessageKey::status(preparationStatus, QMailDataComparator::Includes)); |
| 1390 | |
| 1391 | QSet<QMailMessageService*> sources; |
| 1392 | |
| 1393 | QMailMessageIdList unresolvedMessages(QMailStore::instance()->queryMessages(accountKey & outboxKey & unresolvedKey)); |
| 1394 | if (!unresolvedMessages.isEmpty()) { |
| 1395 | // Find the accounts that own these messages |
| 1396 | QMap<QMailAccountId, QList<QPair<QMailMessagePart::Location, QMailMessagePart::Location> > > unresolvedLists(messageResolvers(unresolvedMessages)); |
| 1397 | |
| 1398 | sources = sourceServiceSet(unresolvedLists.keys().toSet()); |
| 1399 | |
| 1400 | // Emit no signal after completing preparation |
| 1401 | enqueueRequest(action, serialize(unresolvedLists), sources, &ServiceHandler::dispatchPrepareMessages, 0, TransmitMessagesRequestType); |
| 1402 | } |
| 1403 | |
| 1404 | // The transmit action is dependent on the availability of the sources, since they must complete their preparation step first |
| 1405 | enqueueRequest(action, serialize(accountId), sinks, &ServiceHandler::dispatchTransmitMessages, &ServiceHandler::transmissionCompleted, TransmitMessagesRequestType, sources); |
| 1406 | } |
| 1407 | } |
| 1408 | |
| 1409 | void ServiceHandler::transmitMessage(quint64 action, const QMailMessageId &messageId) |
| 1410 | { |
| 1411 | QMailAccountId accountId(QMailMessageMetaData(messageId).parentAccountId()); |
| 1412 | // Ensure that this account has a sink configured |
| 1413 | QSet<QMailMessageService*> sinks(sinkServiceSet(accountId)); |
| 1414 | if (sinks.isEmpty()) { |
| 1415 | reportFailure(action, QMailServiceAction::Status::ErrNoConnection, tr("Unable to enqueue messages for transmission")); |
| 1416 | } else { |
| 1417 | // We need to see if any sources are required to prepare these messages |
| 1418 | // We need to prepare messages to: |
| 1419 | // - resolve references |
| 1420 | // - move to the Sent folder prior to transmission |
| 1421 | quint64 preparationStatus(QMailMessage::HasUnresolvedReferences | QMailMessage::TransmitFromExternal); |
| 1422 | |
| 1423 | QMailMessageIdList unresolvedMessages; |
| 1424 | QMailMessageMetaData metaData(messageId); |
| 1425 | if (metaData.status() & preparationStatus) { |
| 1426 | unresolvedMessages << messageId; |
| 1427 | } |
| 1428 | |
| 1429 | QSet<QMailMessageService*> sources; |
| 1430 | if (!unresolvedMessages.isEmpty()) { |
| 1431 | // Find the accounts that own these messages |
| 1432 | QMap<QMailAccountId, QList<QPair<QMailMessagePart::Location, QMailMessagePart::Location> > > unresolvedLists(messageResolvers(unresolvedMessages)); |
| 1433 | |
| 1434 | sources = sourceServiceSet(unresolvedLists.keys().toSet()); |
| 1435 | |
| 1436 | // Emit no signal after completing preparation |
| 1437 | enqueueRequest(action, serialize(unresolvedLists), sources, &ServiceHandler::dispatchPrepareMessages, 0, TransmitMessagesRequestType); |
| 1438 | } |
| 1439 | |
| 1440 | // The transmit action is dependent on the availability of the sources, since they must complete their preparation step first |
| 1441 | enqueueRequest(action, serialize(messageId), sinks, &ServiceHandler::dispatchTransmitMessage, &ServiceHandler::transmissionCompleted, TransmitMessagesRequestType, sources); |
| 1442 | } |
| 1443 | } |
| 1444 | |
| 1445 | bool ServiceHandler::dispatchPrepareMessages(quint64 action, const QByteArray &data) |
| 1446 | { |
| 1447 | QMap<QMailAccountId, QList<QPair<QMailMessagePart::Location, QMailMessagePart::Location> > > unresolvedLists; |
| 1448 | |
| 1449 | deserialize(data, unresolvedLists); |
| 1450 | |
| 1451 | // Prepare any unresolved messages for transmission |
| 1452 | QMap<QMailAccountId, QList<QPair<QMailMessagePart::Location, QMailMessagePart::Location> > >::const_iterator it = unresolvedLists.begin(), end = unresolvedLists.end(); |
| 1453 | for ( ; it != end; ++it) { |
| 1454 | if (QMailMessageSource *source = accountSource(it.key())) { |
| 1455 | if (!source->prepareMessages(it.value())) { |
| 1456 | qWarning() << "Unable to service request to prepare messages for account:" << it.key(); |
| 1457 | return false; |
| 1458 | } else { |
| 1459 | // This account is now transmitting |
| 1460 | setTransmissionInProgress(it.key(), true); |
| 1461 | } |
| 1462 | } else { |
| 1463 | reportFailure(action, QMailServiceAction::Status::ErrFrameworkFault, tr("Unable to locate source for account"), it.key()); |
| 1464 | return false; |
| 1465 | } |
| 1466 | } |
| 1467 | |
| 1468 | return true; |
| 1469 | } |
| 1470 | |
| 1471 | bool ServiceHandler::dispatchTransmitMessages(quint64 action, const QByteArray &data) |
| 1472 | { |
| 1473 | QMailAccountId accountId; |
| 1474 | |
| 1475 | deserialize(data, accountId); |
| 1476 | |
| 1477 | if (QMailMessageSink *sink = accountSink(accountId)) { |
| 1478 | // Transmit any messages in the Outbox for this account |
| 1479 | QMailMessageKey accountKey(QMailMessageKey::parentAccountId(accountId)); |
| 1480 | QMailMessageKey outboxKey(QMailMessageKey::status(QMailMessage::Outbox) & ~QMailMessageKey::status(QMailMessage::Trash)); |
| 1481 | QMailMessageKey sendKey(QMailMessageKey::customField("dontSend", "true", QMailDataComparator::NotEqual)); |
| 1482 | QMailMessageKey noSendKey(QMailMessageKey::customField("dontSend", QMailDataComparator::Absent)); |
| 1483 | |
| 1484 | QMailMessageIdList toTransmit( |
| 1485 | QMailStore::instance()->queryMessages( |
| 1486 | accountKey & outboxKey & (noSendKey | sendKey))); |
| 1487 | |
| 1488 | bool success(sinkService.value(sink)->usesConcurrentActions() |
| 1489 | ? sink->transmitMessages(toTransmit, action) |
| 1490 | : sink->transmitMessages(toTransmit)); |
| 1491 | |
| 1492 | if (success) { |
| 1493 | // This account is now transmitting |
| 1494 | setTransmissionInProgress(accountId, true); |
| 1495 | } else { |
| 1496 | qWarning() << "Unable to service request to add messages to sink for account:" << accountId; |
| 1497 | return false; |
| 1498 | } |
| 1499 | } else { |
| 1500 | reportFailure(action, QMailServiceAction::Status::ErrFrameworkFault, tr("Unable to locate sink for account"), accountId); |
| 1501 | return false; |
| 1502 | } |
| 1503 | |
| 1504 | return true; |
| 1505 | } |
| 1506 | |
| 1507 | bool ServiceHandler::dispatchTransmitMessage(quint64 action, const QByteArray &data) |
| 1508 | { |
| 1509 | QMailMessageId messageId; |
| 1510 | |
| 1511 | deserialize(data, messageId); |
| 1512 | |
| 1513 | QMailMessageMetaData metaData(messageId); |
| 1514 | QMailAccountId accountId(metaData.parentAccountId()); |
| 1515 | if (QMailMessageSink *sink = accountSink(accountId)) { |
| 1516 | // Transmit the messages if it's in the Outbox. |
| 1517 | quint64 outboxStatus(QMailMessage::Outbox & ~QMailMessage::Trash); |
| 1518 | bool success = false; |
| 1519 | QMailMessageIdList toTransmit; |
| 1520 | if (metaData.status() & outboxStatus) { |
| 1521 | toTransmit << messageId; |
| 1522 | |
| 1523 | success = sinkService.value(sink)->usesConcurrentActions() |
| 1524 | ? sink->transmitMessages(toTransmit, action) |
| 1525 | : sink->transmitMessages(toTransmit); |
| 1526 | } |
| 1527 | |
| 1528 | if (success) { |
| 1529 | // This account is now transmitting |
| 1530 | setTransmissionInProgress(accountId, true); |
| 1531 | } else { |
| 1532 | qWarning() << "Unable to service request to add message " << messageId << " to sink for account:" << accountId; |
| 1533 | return false; |
| 1534 | } |
| 1535 | } else { |
| 1536 | reportFailure(action, QMailServiceAction::Status::ErrFrameworkFault, tr("Unable to locate sink for account"), accountId); |
| 1537 | return false; |
| 1538 | } |
| 1539 | |
| 1540 | return true; |
| 1541 | } |
| 1542 | |
| 1543 | void ServiceHandler::retrieveFolderList(quint64 action, const QMailAccountId &accountId, const QMailFolderId &folderId, bool descending) |
| 1544 | { |
| 1545 | QSet<QMailMessageService*> sources(sourceServiceSet(accountId)); |
| 1546 | if (sources.isEmpty()) { |
| 1547 | reportFailure(action, QMailServiceAction::Status::ErrNoConnection, tr("Unable to retrieve folder list for unconfigured account")); |
| 1548 | } else { |
| 1549 | enqueueRequest(action, serialize(accountId, folderId, descending), sources, &ServiceHandler::dispatchRetrieveFolderListAccount, &ServiceHandler::retrievalCompleted, RetrieveFolderListRequestType); |
| 1550 | } |
| 1551 | } |
| 1552 | |
| 1553 | bool ServiceHandler::dispatchRetrieveFolderListAccount(quint64 action, const QByteArray &data) |
| 1554 | { |
| 1555 | QMailAccountId accountId; |
| 1556 | QMailFolderId folderId; |
| 1557 | bool descending; |
| 1558 | |
| 1559 | deserialize(data, accountId, folderId, descending); |
| 1560 | |
| 1561 | if (QMailMessageSource *source = accountSource(accountId)) { |
| 1562 | bool success(sourceService.value(source)->usesConcurrentActions() |
| 1563 | ? source->retrieveFolderList(accountId, folderId, descending, action) |
| 1564 | : source->retrieveFolderList(accountId, folderId, descending)); |
| 1565 | |
| 1566 | if (success) { |
| 1567 | // This account is now retrieving (arguably...) |
| 1568 | setRetrievalInProgress(accountId, true); |
| 1569 | } else { |
| 1570 | qWarning() << "Unable to service request to retrieve folder list for account:" << accountId; |
| 1571 | return false; |
| 1572 | } |
| 1573 | } else { |
| 1574 | reportFailure(action, QMailServiceAction::Status::ErrFrameworkFault, tr("Unable to locate source for account"), accountId); |
| 1575 | return false; |
| 1576 | } |
| 1577 | |
| 1578 | return true; |
| 1579 | } |
| 1580 | |
| 1581 | void ServiceHandler::retrieveMessageList(quint64 action, const QMailAccountId &accountId, const QMailFolderId &folderId, uint minimum, const QMailMessageSortKey &sort) |
| 1582 | { |
| 1583 | QSet<QMailMessageService*> sources(sourceServiceSet(accountId)); |
| 1584 | if (sources.isEmpty()) { |
| 1585 | reportFailure(action, QMailServiceAction::Status::ErrNoConnection, tr("Unable to retrieve message list for unconfigured account")); |
| 1586 | } else { |
| 1587 | enqueueRequest(action, serialize(accountId, folderId, minimum, sort), sources, &ServiceHandler::dispatchRetrieveMessageList, &ServiceHandler::retrievalCompleted, RetrieveMessageListRequestType); |
| 1588 | } |
| 1589 | } |
| 1590 | |
| 1591 | bool ServiceHandler::dispatchRetrieveMessageList(quint64 action, const QByteArray &data) |
| 1592 | { |
| 1593 | QMailAccountId accountId; |
| 1594 | QMailFolderId folderId; |
| 1595 | uint minimum; |
| 1596 | QMailMessageSortKey sort; |
| 1597 | |
| 1598 | deserialize(data, accountId, folderId, minimum, sort); |
| 1599 | |
| 1600 | if (QMailMessageSource *source = accountSource(accountId)) { |
| 1601 | bool success(sourceService.value(source)->usesConcurrentActions() |
| 1602 | ? source->retrieveMessageList(accountId, folderId, minimum, sort, action) |
| 1603 | : source->retrieveMessageList(accountId, folderId, minimum, sort)); |
| 1604 | if (success) { |
| 1605 | // This account is now retrieving |
| 1606 | setRetrievalInProgress(accountId, true); |
| 1607 | } else { |
| 1608 | qWarning() << "Unable to service request to retrieve message list for folder:" << folderId; |
| 1609 | return false; |
| 1610 | } |
| 1611 | } else { |
| 1612 | reportFailure(action, QMailServiceAction::Status::ErrFrameworkFault, tr("Unable to locate source for account"), accountId); |
| 1613 | return false; |
| 1614 | } |
| 1615 | |
| 1616 | return true; |
| 1617 | } |
| 1618 | |
| 1619 | void ServiceHandler::retrieveMessageLists(quint64 action, const QMailAccountId &accountId, const QMailFolderIdList &folderIds, uint minimum, const QMailMessageSortKey &sort) |
| 1620 | { |
| 1621 | QSet<QMailMessageService*> sources(sourceServiceSet(accountId)); |
| 1622 | if (sources.isEmpty()) { |
| 1623 | reportFailure(action, QMailServiceAction::Status::ErrNoConnection, tr("Unable to retrieve message list for unconfigured account")); |
| 1624 | } else { |
| 1625 | enqueueRequest(action, serialize(accountId, folderIds, minimum, sort), sources, &ServiceHandler::dispatchRetrieveMessageLists, &ServiceHandler::retrievalCompleted, RetrieveMessageListRequestType); |
| 1626 | } |
| 1627 | } |
| 1628 | |
| 1629 | bool ServiceHandler::dispatchRetrieveMessageLists(quint64 action, const QByteArray &data) |
| 1630 | { |
| 1631 | QMailAccountId accountId; |
| 1632 | QMailFolderIdList folderIds; |
| 1633 | uint minimum; |
| 1634 | QMailMessageSortKey sort; |
| 1635 | |
| 1636 | deserialize(data, accountId, folderIds, minimum, sort); |
| 1637 | |
| 1638 | if (QMailMessageSource *source = accountSource(accountId)) { |
| 1639 | bool success(sourceService.value(source)->usesConcurrentActions() |
| 1640 | ? source->retrieveMessageLists(accountId, folderIds, minimum, sort, action) |
| 1641 | : source->retrieveMessageLists(accountId, folderIds, minimum, sort)); |
| 1642 | if (success) { |
| 1643 | // This account is now retrieving |
| 1644 | setRetrievalInProgress(accountId, true); |
| 1645 | } else { |
| 1646 | qWarning() << "Unable to service request to retrieve message list for folders:" << folderIds; |
| 1647 | return false; |
| 1648 | } |
| 1649 | } else { |
| 1650 | reportFailure(action, QMailServiceAction::Status::ErrFrameworkFault, tr("Unable to locate source for account"), accountId); |
| 1651 | return false; |
| 1652 | } |
| 1653 | |
| 1654 | return true; |
| 1655 | } |
| 1656 | |
| 1657 | void ServiceHandler::retrieveNewMessages(quint64 action, const QMailAccountId &accountId, const QMailFolderIdList &folderIds) |
| 1658 | { |
| 1659 | QSet<QMailMessageService*> sources(sourceServiceSet(accountId)); |
| 1660 | if (sources.isEmpty()) { |
| 1661 | reportFailure(action, QMailServiceAction::Status::ErrNoConnection, tr("Unable to retrieve new messages for unconfigured account")); |
| 1662 | } else { |
| 1663 | enqueueRequest(action, serialize(accountId, folderIds), sources, &ServiceHandler::dispatchRetrieveNewMessages, &ServiceHandler::retrievalCompleted, RetrieveNewMessagesRequestType); |
| 1664 | } |
| 1665 | } |
| 1666 | |
| 1667 | bool ServiceHandler::dispatchRetrieveNewMessages(quint64 action, const QByteArray &data) |
| 1668 | { |
| 1669 | QMailAccountId accountId; |
| 1670 | QMailFolderIdList folderIds; |
| 1671 | |
| 1672 | deserialize(data, accountId, folderIds); |
| 1673 | |
| 1674 | if (QMailMessageSource *source = accountSource(accountId)) { |
| 1675 | bool success(sourceService.value(source)->usesConcurrentActions() |
| 1676 | ? source->retrieveNewMessages(accountId, folderIds) |
| 1677 | : source->retrieveNewMessages(accountId, folderIds)); |
| 1678 | if (success) { |
| 1679 | // This account is now retrieving |
| 1680 | setRetrievalInProgress(accountId, true); |
| 1681 | } else { |
| 1682 | qWarning() << "Unable to service request to retrieve new messages for folders:" << folderIds; |
| 1683 | return false; |
| 1684 | } |
| 1685 | } else { |
| 1686 | reportFailure(action, QMailServiceAction::Status::ErrFrameworkFault, tr("Unable to locate source for account"), accountId); |
| 1687 | return false; |
| 1688 | } |
| 1689 | |
| 1690 | return true; |
| 1691 | } |
| 1692 | |
| 1693 | void ServiceHandler::createStandardFolders(quint64 action, const QMailAccountId &accountId) |
| 1694 | { |
| 1695 | QSet<QMailMessageService*> sources(sourceServiceSet(accountId)); |
| 1696 | if (sources.isEmpty()) { |
| 1697 | reportFailure(action, QMailServiceAction::Status::ErrNoConnection, tr("Unable to retrieve standard folders for unconfigured account")); |
| 1698 | } |
| 1699 | else { |
| 1700 | enqueueRequest(action, serialize(accountId), sources, &ServiceHandler::dispatchCreateStandardFolders, &ServiceHandler::retrievalCompleted, RetrieveFolderListRequestType); |
| 1701 | } |
| 1702 | } |
| 1703 | |
| 1704 | bool ServiceHandler::dispatchCreateStandardFolders(quint64 action, const QByteArray &data) |
| 1705 | { |
| 1706 | QMailAccountId accountId; |
| 1707 | |
| 1708 | deserialize(data, accountId); |
| 1709 | |
| 1710 | if (QMailMessageSource *source = accountSource(accountId)) { |
| 1711 | bool success(sourceService.value(source)->usesConcurrentActions() |
| 1712 | ? source->createStandardFolders(accountId, action) |
| 1713 | : source->createStandardFolders(accountId)); |
| 1714 | if (success) { |
| 1715 | return true; |
| 1716 | } else { |
| 1717 | qWarning() << "Unable to service request to create standard folder for account:" << accountId; |
| 1718 | return false; |
| 1719 | } |
| 1720 | |
| 1721 | } else { |
| 1722 | reportFailure(action, QMailServiceAction::Status::ErrFrameworkFault, tr("Unable to locate source for account"), accountId); |
| 1723 | return false; |
| 1724 | } |
| 1725 | } |
| 1726 | |
| 1727 | void ServiceHandler::retrieveMessages(quint64 action, const QMailMessageIdList &messageIds, QMailRetrievalAction::RetrievalSpecification spec) |
| 1728 | { |
| 1729 | QMap<QMailAccountId, QMailMessageIdList> messageLists(accountMessages(messageIds)); |
| 1730 | |
| 1731 | QSet<QMailMessageService*> sources(sourceServiceSet(messageLists.keys().toSet())); |
| 1732 | if (sources.isEmpty()) { |
| 1733 | reportFailure(action, QMailServiceAction::Status::ErrNoConnection, tr("Unable to retrieve messages for unconfigured account")); |
| 1734 | } else { |
| 1735 | enqueueRequest(action, serialize(messageLists, spec), sources, &ServiceHandler::dispatchRetrieveMessages, &ServiceHandler::retrievalCompleted, RetrieveMessagesRequestType); |
| 1736 | } |
| 1737 | } |
| 1738 | |
| 1739 | bool ServiceHandler::dispatchRetrieveMessages(quint64 action, const QByteArray &data) |
| 1740 | { |
| 1741 | QMap<QMailAccountId, QMailMessageIdList> messageLists; |
| 1742 | QMailRetrievalAction::RetrievalSpecification spec; |
| 1743 | |
| 1744 | deserialize(data, messageLists, spec); |
| 1745 | |
| 1746 | QMap<QMailAccountId, QMailMessageIdList>::const_iterator it = messageLists.begin(), end = messageLists.end(); |
| 1747 | for ( ; it != end; ++it) { |
| 1748 | if (QMailMessageSource *source = accountSource(it.key())) { |
| 1749 | bool success(sourceService.value(source)->usesConcurrentActions() |
| 1750 | ? source->retrieveMessages(it.value(), spec, action) |
| 1751 | : source->retrieveMessages(it.value(), spec)); |
| 1752 | |
| 1753 | if (success) { |
| 1754 | // This account is now retrieving |
| 1755 | if (!_retrievalAccountIds.contains(it.key())) { |
| 1756 | _retrievalAccountIds.insert(it.key()); |
| 1757 | } |
| 1758 | } else { |
| 1759 | qWarning() << "Unable to service request to retrieve messages for account:" << it.key() << "with spec" << spec; |
| 1760 | return false; |
| 1761 | } |
| 1762 | } else { |
| 1763 | reportFailure(action, QMailServiceAction::Status::ErrFrameworkFault, tr("Unable to locate source for account"), it.key()); |
| 1764 | return false; |
| 1765 | } |
| 1766 | } |
| 1767 | |
| 1768 | QMailStore::instance()->setRetrievalInProgress(_retrievalAccountIds.toList()); |
| 1769 | return true; |
| 1770 | } |
| 1771 | |
| 1772 | void ServiceHandler::retrieveMessagePart(quint64 action, const QMailMessagePart::Location &partLocation) |
| 1773 | { |
| 1774 | QSet<QMailAccountId> accountIds(messageAccount(partLocation.containingMessageId())); |
| 1775 | QSet<QMailMessageService*> sources(sourceServiceSet(accountIds)); |
| 1776 | if (sources.isEmpty()) { |
| 1777 | reportFailure(action, QMailServiceAction::Status::ErrNoConnection, tr("Unable to retrieve message part for unconfigured account")); |
| 1778 | } else { |
| 1779 | enqueueRequest(action, serialize(*accountIds.begin(), partLocation), sources, &ServiceHandler::dispatchRetrieveMessagePart, &ServiceHandler::retrievalCompleted, RetrieveMessagePartRequestType); |
| 1780 | } |
| 1781 | } |
| 1782 | |
| 1783 | bool ServiceHandler::dispatchRetrieveMessagePart(quint64 action, const QByteArray &data) |
| 1784 | { |
| 1785 | QMailAccountId accountId; |
| 1786 | QMailMessagePart::Location partLocation; |
| 1787 | |
| 1788 | deserialize(data, accountId, partLocation); |
| 1789 | |
| 1790 | if (QMailMessageSource *source = accountSource(accountId)) { |
| 1791 | bool success(sourceService.value(source)->usesConcurrentActions() |
| 1792 | ? source->retrieveMessagePart(partLocation, action) |
| 1793 | : source->retrieveMessagePart(partLocation)); |
| 1794 | |
| 1795 | if (success) { |
| 1796 | // This account is now retrieving |
| 1797 | setRetrievalInProgress(accountId, true); |
| 1798 | } else { |
| 1799 | qWarning() << "Unable to service request to retrieve part for message:" << partLocation.containingMessageId(); |
| 1800 | return false; |
| 1801 | } |
| 1802 | } else { |
| 1803 | reportFailure(action, QMailServiceAction::Status::ErrFrameworkFault, tr("Unable to locate source for account"), accountId); |
| 1804 | return false; |
| 1805 | } |
| 1806 | |
| 1807 | return true; |
| 1808 | } |
| 1809 | |
| 1810 | void ServiceHandler::retrieveMessageRange(quint64 action, const QMailMessageId &messageId, uint minimum) |
| 1811 | { |
| 1812 | QSet<QMailAccountId> accountIds(messageAccount(messageId)); |
| 1813 | QSet<QMailMessageService*> sources(sourceServiceSet(accountIds)); |
| 1814 | if (sources.isEmpty()) { |
| 1815 | reportFailure(action, QMailServiceAction::Status::ErrNoConnection, tr("Unable to retrieve message range for unconfigured account")); |
| 1816 | } else { |
| 1817 | enqueueRequest(action, serialize(*accountIds.begin(), messageId, minimum), sources, &ServiceHandler::dispatchRetrieveMessageRange, &ServiceHandler::retrievalCompleted, RetrieveMessageRangeRequestType); |
| 1818 | } |
| 1819 | } |
| 1820 | |
| 1821 | bool ServiceHandler::dispatchRetrieveMessageRange(quint64 action, const QByteArray &data) |
| 1822 | { |
| 1823 | QMailAccountId accountId; |
| 1824 | QMailMessageId messageId; |
| 1825 | uint minimum; |
| 1826 | |
| 1827 | deserialize(data, accountId, messageId, minimum); |
| 1828 | |
| 1829 | if (QMailMessageSource *source = accountSource(accountId)) { |
| 1830 | bool success(sourceService.value(source)->usesConcurrentActions() |
| 1831 | ? source->retrieveMessageRange(messageId, minimum, action) |
| 1832 | : source->retrieveMessageRange(messageId, minimum)); |
| 1833 | |
| 1834 | if (success) { |
| 1835 | // This account is now retrieving |
| 1836 | setRetrievalInProgress(accountId, true); |
| 1837 | } else { |
| 1838 | qWarning() << "Unable to service request to retrieve range:" << minimum << "for message:" << messageId; |
| 1839 | return false; |
| 1840 | } |
| 1841 | } else { |
| 1842 | reportFailure(action, QMailServiceAction::Status::ErrFrameworkFault, tr("Unable to locate source for account"), accountId); |
| 1843 | return false; |
| 1844 | } |
| 1845 | |
| 1846 | return true; |
| 1847 | } |
| 1848 | |
| 1849 | void ServiceHandler::retrieveMessagePartRange(quint64 action, const QMailMessagePart::Location &partLocation, uint minimum) |
| 1850 | { |
| 1851 | QSet<QMailAccountId> accountIds(messageAccount(partLocation.containingMessageId())); |
| 1852 | QSet<QMailMessageService*> sources(sourceServiceSet(accountIds)); |
| 1853 | if (sources.isEmpty()) { |
| 1854 | reportFailure(action, QMailServiceAction::Status::ErrNoConnection, tr("Unable to retrieve message part range for unconfigured account")); |
| 1855 | } else { |
| 1856 | enqueueRequest(action, serialize(*accountIds.begin(), partLocation, minimum), sources, &ServiceHandler::dispatchRetrieveMessagePartRange, &ServiceHandler::retrievalCompleted, RetrieveMessagePartRangeRequestType); |
| 1857 | } |
| 1858 | } |
| 1859 | |
| 1860 | bool ServiceHandler::dispatchRetrieveMessagePartRange(quint64 action, const QByteArray &data) |
| 1861 | { |
| 1862 | QMailAccountId accountId; |
| 1863 | QMailMessagePart::Location partLocation; |
| 1864 | uint minimum; |
| 1865 | |
| 1866 | deserialize(data, accountId, partLocation, minimum); |
| 1867 | |
| 1868 | if (QMailMessageSource *source = accountSource(accountId)) { |
| 1869 | |
| 1870 | bool success(sourceService.value(source)->usesConcurrentActions() |
| 1871 | ? source->retrieveMessagePartRange(partLocation, minimum, action) |
| 1872 | : source->retrieveMessagePartRange(partLocation, minimum)); |
| 1873 | |
| 1874 | if (success) { |
| 1875 | // This account is now retrieving |
| 1876 | setRetrievalInProgress(accountId, true); |
| 1877 | } else { |
| 1878 | qWarning() << "Unable to service request to retrieve range:" << minimum << "for part in message:" << partLocation.containingMessageId(); |
| 1879 | return false; |
| 1880 | } |
| 1881 | } else { |
| 1882 | reportFailure(action, QMailServiceAction::Status::ErrFrameworkFault, tr("Unable to locate source for account"), accountId); |
| 1883 | return false; |
| 1884 | } |
| 1885 | |
| 1886 | return true; |
| 1887 | } |
| 1888 | |
| 1889 | void ServiceHandler::retrieveAll(quint64 action, const QMailAccountId &accountId) |
| 1890 | { |
| 1891 | QSet<QMailMessageService*> sources(sourceServiceSet(accountId)); |
| 1892 | if (sources.isEmpty()) { |
| 1893 | reportFailure(action, QMailServiceAction::Status::ErrNoConnection, tr("Unable to retrieve all messages for unconfigured account")); |
| 1894 | } else { |
| 1895 | enqueueRequest(action, serialize(accountId), sources, &ServiceHandler::dispatchRetrieveAll, &ServiceHandler::retrievalCompleted, RetrieveAllRequestType); |
| 1896 | } |
| 1897 | } |
| 1898 | |
| 1899 | bool ServiceHandler::dispatchRetrieveAll(quint64 action, const QByteArray &data) |
| 1900 | { |
| 1901 | QMailAccountId accountId; |
| 1902 | |
| 1903 | deserialize(data, accountId); |
| 1904 | |
| 1905 | if (QMailMessageSource *source = accountSource(accountId)) { |
| 1906 | bool success(sourceService.value(source)->usesConcurrentActions() |
| 1907 | ? source->retrieveAll(accountId, action) |
| 1908 | : source->retrieveAll(accountId)); |
| 1909 | |
| 1910 | if (success) { |
| 1911 | // This account is now retrieving |
| 1912 | setRetrievalInProgress(accountId, true); |
| 1913 | } else { |
| 1914 | qWarning() << "Unable to service request to retrieve all messages for account:" << accountId; |
| 1915 | return false; |
| 1916 | } |
| 1917 | } else { |
| 1918 | reportFailure(action, QMailServiceAction::Status::ErrFrameworkFault, tr("Unable to locate source for account"), accountId); |
| 1919 | return false; |
| 1920 | } |
| 1921 | |
| 1922 | return true; |
| 1923 | } |
| 1924 | |
| 1925 | void ServiceHandler::exportUpdates(quint64 action, const QMailAccountId &accountId) |
| 1926 | { |
| 1927 | QSet<QMailMessageService*> sources(sourceServiceSet(accountId)); |
| 1928 | if (sources.isEmpty()) { |
| 1929 | reportFailure(action, QMailServiceAction::Status::ErrNoConnection, tr("Unable to export updates for unconfigured account")); |
| 1930 | } else { |
| 1931 | enqueueRequest(action, serialize(accountId), sources, &ServiceHandler::dispatchExportUpdates, &ServiceHandler::retrievalCompleted, ExportUpdatesRequestType); |
| 1932 | } |
| 1933 | } |
| 1934 | |
| 1935 | bool ServiceHandler::dispatchExportUpdates(quint64 action, const QByteArray &data) |
| 1936 | { |
| 1937 | QMailAccountId accountId; |
| 1938 | |
| 1939 | deserialize(data, accountId); |
| 1940 | |
| 1941 | if (QMailMessageSource *source = accountSource(accountId)) { |
| 1942 | bool success(sourceService.value(source)->usesConcurrentActions() |
| 1943 | ? source->exportUpdates(accountId, action) |
| 1944 | : source->exportUpdates(accountId)); |
| 1945 | |
| 1946 | if (!success) { |
| 1947 | qWarning() << "Unable to service request to export updates for account:" << accountId; |
| 1948 | return false; |
| 1949 | } |
| 1950 | } else { |
| 1951 | reportFailure(action, QMailServiceAction::Status::ErrFrameworkFault, tr("Unable to locate source for account"), accountId); |
| 1952 | return false; |
| 1953 | } |
| 1954 | |
| 1955 | return true; |
| 1956 | } |
| 1957 | |
| 1958 | void ServiceHandler::synchronize(quint64 action, const QMailAccountId &accountId) |
| 1959 | { |
| 1960 | QSet<QMailMessageService*> sources(sourceServiceSet(accountId)); |
| 1961 | if (sources.isEmpty()) { |
| 1962 | reportFailure(action, QMailServiceAction::Status::ErrNoConnection, tr("Unable to synchronize unconfigured account")); |
| 1963 | } else { |
| 1964 | enqueueRequest(action, serialize(accountId), sources, &ServiceHandler::dispatchSynchronize, &ServiceHandler::retrievalCompleted, SynchronizeRequestType); |
| 1965 | } |
| 1966 | } |
| 1967 | |
| 1968 | bool ServiceHandler::dispatchSynchronize(quint64 action, const QByteArray &data) |
| 1969 | { |
| 1970 | QMailAccountId accountId; |
| 1971 | |
| 1972 | deserialize(data, accountId); |
| 1973 | |
| 1974 | if (QMailMessageSource *source = accountSource(accountId)) { |
| 1975 | |
| 1976 | bool success(sourceService.value(source)->usesConcurrentActions() |
| 1977 | ? source->exportUpdates(accountId, action) |
| 1978 | : source->exportUpdates(accountId)); |
| 1979 | |
| 1980 | if (success) { |
| 1981 | // This account is now retrieving |
| 1982 | setRetrievalInProgress(accountId, true); |
| 1983 | } else { |
| 1984 | qWarning() << "Unable to service request to synchronize account:" << accountId; |
| 1985 | return false; |
| 1986 | } |
| 1987 | } else { |
| 1988 | reportFailure(action, QMailServiceAction::Status::ErrFrameworkFault, tr("Unable to locate source for account"), accountId); |
| 1989 | return false; |
| 1990 | } |
| 1991 | |
| 1992 | return true; |
| 1993 | } |
| 1994 | |
| 1995 | void ServiceHandler::onlineDeleteMessages(quint64 action, const QMailMessageIdList& messageIds, QMailStore::MessageRemovalOption option) |
| 1996 | { |
| 1997 | QSet<QMailMessageService*> sources; |
| 1998 | |
| 1999 | if (option == QMailStore::NoRemovalRecord) { |
| 2000 | // Delete these records locally without involving the source |
| 2001 | discardMessages(action, messageIds); |
| 2002 | } else { |
| 2003 | QMap<QMailAccountId, QMailMessageIdList> messageLists(accountMessages(messageIds)); |
| 2004 | sources = sourceServiceSet(messageLists.keys().toSet()); |
| 2005 | if (sources.isEmpty()) { |
| 2006 | reportFailure(action, QMailServiceAction::Status::ErrNoConnection, tr("Unable to delete messages for unconfigured account")); |
| 2007 | } else { |
| 2008 | QString description(tr("Deleting messages")); |
| 2009 | enqueueRequest(action, serialize(messageLists), sources, &ServiceHandler::dispatchOnlineDeleteMessages, &ServiceHandler::storageActionCompleted, DeleteMessagesRequestType); |
| 2010 | } |
| 2011 | } |
| 2012 | } |
| 2013 | |
| 2014 | void ServiceHandler::discardMessages(quint64 action, QMailMessageIdList messageIds) |
| 2015 | { |
| 2016 | uint progress = 0; |
| 2017 | uint total = messageIds.count(); |
| 2018 | |
| 2019 | emit progressChanged(action, progress, total); |
| 2020 | |
| 2021 | // Just delete all these messages |
| 2022 | if (!messageIds.isEmpty() |
| 2023 | && !QMailStore::instance()->removeMessages(QMailMessageKey::id(messageIds), QMailStore::NoRemovalRecord)) { |
| 2024 | qWarning() << "Unable to service request to discard messages"; |
| 2025 | |
| 2026 | reportFailure(action, QMailServiceAction::Status::ErrEnqueueFailed, tr("Unable to discard messages")); |
| 2027 | return; |
| 2028 | } |
| 2029 | |
| 2030 | emit progressChanged(action, total, total); |
| 2031 | emit messagesDeleted(action, messageIds); |
| 2032 | emit storageActionCompleted(action); |
| 2033 | emit activityChanged(action, QMailServiceAction::Successful); |
| 2034 | QMailStore::instance()->flushIpcNotifications(); |
| 2035 | return; |
| 2036 | } |
| 2037 | |
| 2038 | bool ServiceHandler::dispatchOnlineDeleteMessages(quint64 action, const QByteArray &data) |
| 2039 | { |
| 2040 | QMap<QMailAccountId, QMailMessageIdList> messageLists; |
| 2041 | |
| 2042 | deserialize(data, messageLists); |
| 2043 | |
| 2044 | QMap<QMailAccountId, QMailMessageIdList>::const_iterator it = messageLists.begin(), end = messageLists.end(); |
| 2045 | for ( ; it != end; ++it) { |
| 2046 | if (QMailMessageSource *source = accountSource(it.key())) { |
| 2047 | bool success(sourceService.value(source)->usesConcurrentActions() |
| 2048 | ? source->deleteMessages(it.value(), action) |
| 2049 | : source->deleteMessages(it.value())); |
| 2050 | |
| 2051 | if (!success) { |
| 2052 | qWarning() << "Unable to service request to delete messages for account:" << it.key(); |
| 2053 | return false; |
| 2054 | } |
| 2055 | } else { |
| 2056 | reportFailure(action, QMailServiceAction::Status::ErrFrameworkFault, tr("Unable to locate source for account"), it.key()); |
| 2057 | return false; |
| 2058 | } |
| 2059 | } |
| 2060 | |
| 2061 | return true; |
| 2062 | } |
| 2063 | |
| 2064 | void ServiceHandler::onlineCopyMessages(quint64 action, const QMailMessageIdList& messageIds, const QMailFolderId &destination) |
| 2065 | { |
| 2066 | QSet<QMailAccountId> accountIds(folderAccount(destination)); |
| 2067 | |
| 2068 | if (accountIds.isEmpty()) { |
| 2069 | // Copy to a non-account folder |
| 2070 | QSet<QMailMessageService*> sources; |
| 2071 | enqueueRequest(action, serialize(messageIds, destination), sources, &ServiceHandler::dispatchCopyToLocal, &ServiceHandler::storageActionCompleted, CopyMessagesRequestType); |
| 2072 | } else { |
| 2073 | QSet<QMailMessageService*> sources(sourceServiceSet(accountIds)); |
| 2074 | if (sources.isEmpty()) { |
| 2075 | reportFailure(action, QMailServiceAction::Status::ErrNoConnection, tr("Unable to copy messages to unconfigured account")); |
| 2076 | } else if (sources.count() > 1) { |
| 2077 | reportFailure(action, QMailServiceAction::Status::ErrFrameworkFault, tr("Unable to copy messages to multiple destination accounts!")); |
| 2078 | } else { |
| 2079 | enqueueRequest(action, serialize(messageIds, destination), sources, &ServiceHandler::dispatchOnlineCopyMessages, &ServiceHandler::storageActionCompleted, CopyMessagesRequestType); |
| 2080 | } |
| 2081 | } |
| 2082 | } |
| 2083 | |
| 2084 | bool ServiceHandler::dispatchOnlineCopyMessages(quint64 action, const QByteArray &data) |
| 2085 | { |
| 2086 | QMailMessageIdList messageIds; |
| 2087 | QMailFolderId destination; |
| 2088 | |
| 2089 | deserialize(data, messageIds, destination); |
| 2090 | if (messageIds.isEmpty()) |
| 2091 | return false; |
| 2092 | |
| 2093 | QSet<QMailAccountId> accountIds(folderAccount(destination)); |
| 2094 | |
| 2095 | if (accountIds.count() != 1) { |
| 2096 | reportFailure(action, QMailServiceAction::Status::ErrNoConnection, tr("Unable to copy messages to unconfigured account")); |
| 2097 | return false; |
| 2098 | } else { |
| 2099 | QMailAccountId accountId(*accountIds.begin()); |
| 2100 | if (QMailMessageSource *source = accountSource(accountId)) { |
| 2101 | |
| 2102 | bool success(sourceService.value(source)->usesConcurrentActions() |
| 2103 | ? source->copyMessages(messageIds, destination, action) |
| 2104 | : source->copyMessages(messageIds, destination)); |
| 2105 | |
| 2106 | if (!success) { |
| 2107 | qWarning() << "Unable to service request to copy messages to folder:" << destination << "for account:" << accountId; |
| 2108 | return false; |
| 2109 | } |
| 2110 | } else { |
| 2111 | reportFailure(action, QMailServiceAction::Status::ErrFrameworkFault, tr("Unable to locate source for account"), accountId); |
| 2112 | return false; |
| 2113 | } |
| 2114 | } |
| 2115 | |
| 2116 | return true; |
| 2117 | } |
| 2118 | |
| 2119 | bool ServiceHandler::dispatchCopyToLocal(quint64 action, const QByteArray &data) |
| 2120 | { |
| 2121 | QMailMessageIdList messageIds; |
| 2122 | QMailFolderId destination; |
| 2123 | |
| 2124 | deserialize(data, messageIds, destination); |
| 2125 | if (messageIds.isEmpty()) |
| 2126 | return false; |
| 2127 | |
| 2128 | uint progress = 0; |
| 2129 | uint total = messageIds.count(); |
| 2130 | |
| 2131 | emit progressChanged(action, progress, total); |
| 2132 | |
| 2133 | // Create a copy of each message |
| 2134 | foreach (const QMailMessageId id, messageIds) { |
| 2135 | QMailMessage message(id); |
| 2136 | |
| 2137 | message.setId(QMailMessageId()); |
| 2138 | message.setContentIdentifier(QString()); |
| 2139 | message.setParentFolderId(destination); |
| 2140 | |
| 2141 | if (!QMailStore::instance()->addMessage(&message)) { |
| 2142 | qWarning() << "Unable to service request to copy messages to folder:" << destination << "for account:" << message.parentAccountId(); |
| 2143 | |
| 2144 | reportFailure(action, QMailServiceAction::Status::ErrFrameworkFault, tr("Unable to copy messages for account"), message.parentAccountId()); |
| 2145 | return false; |
| 2146 | } else { |
| 2147 | emit progressChanged(action, ++progress, total); |
| 2148 | } |
| 2149 | } |
| 2150 | |
| 2151 | emit activityChanged(action, QMailServiceAction::Successful); |
| 2152 | return true; |
| 2153 | } |
| 2154 | |
| 2155 | void ServiceHandler::onlineMoveMessages(quint64 action, const QMailMessageIdList& messageIds, const QMailFolderId &destination) |
| 2156 | { |
| 2157 | QSet<QMailMessageService*> sources; |
| 2158 | |
| 2159 | QMap<QMailAccountId, QMailMessageIdList> messageLists(accountMessages(messageIds)); |
| 2160 | sources = sourceServiceSet(messageLists.keys().toSet()); |
| 2161 | if (sources.isEmpty()) { |
| 2162 | reportFailure(action, QMailServiceAction::Status::ErrNoConnection, tr("Unable to move messages for unconfigured account")); |
| 2163 | } else { |
| 2164 | enqueueRequest(action, serialize(messageLists, destination), sources, &ServiceHandler::dispatchOnlineMoveMessages, &ServiceHandler::storageActionCompleted, MoveMessagesRequestType); |
| 2165 | } |
| 2166 | } |
| 2167 | |
| 2168 | bool ServiceHandler::dispatchOnlineMoveMessages(quint64 action, const QByteArray &data) |
| 2169 | { |
| 2170 | QMap<QMailAccountId, QMailMessageIdList> messageLists; |
| 2171 | QMailFolderId destination; |
| 2172 | |
| 2173 | deserialize(data, messageLists, destination); |
| 2174 | |
| 2175 | QMap<QMailAccountId, QMailMessageIdList>::const_iterator it = messageLists.begin(), end = messageLists.end(); |
| 2176 | for ( ; it != end; ++it) { |
| 2177 | if (QMailMessageSource *source = accountSource(it.key())) { |
| 2178 | |
| 2179 | bool success(sourceService.value(source)->usesConcurrentActions() |
| 2180 | ? source->moveMessages(it.value(), destination, action) |
| 2181 | : source->moveMessages(it.value(), destination)); |
| 2182 | |
| 2183 | if (!success) { |
| 2184 | qWarning() << "Unable to service request to move messages to folder:" << destination << "for account:" << it.key(); |
| 2185 | return false; |
| 2186 | } |
| 2187 | } else { |
| 2188 | reportFailure(action, QMailServiceAction::Status::ErrFrameworkFault, tr("Unable to locate source for account"), it.key()); |
| 2189 | return false; |
| 2190 | } |
| 2191 | } |
| 2192 | |
| 2193 | return true; |
| 2194 | } |
| 2195 | |
| 2196 | void ServiceHandler::onlineFlagMessagesAndMoveToStandardFolder(quint64 action, const QMailMessageIdList& messageIds, quint64 setMask, quint64 unsetMask) |
| 2197 | { |
| 2198 | QSet<QMailMessageService*> sources; |
| 2199 | |
| 2200 | QMap<QMailAccountId, QMailMessageIdList> messageLists(accountMessages(messageIds)); |
| 2201 | sources = sourceServiceSet(messageLists.keys().toSet()); |
| 2202 | if (sources.isEmpty()) { |
| 2203 | reportFailure(action, QMailServiceAction::Status::ErrNoConnection, tr("Unable to flag messages for unconfigured account")); |
| 2204 | } else { |
| 2205 | enqueueRequest(action, serialize(messageLists, setMask, unsetMask), sources, &ServiceHandler::dispatchOnlineFlagMessagesAndMoveToStandardFolder, &ServiceHandler::storageActionCompleted, FlagMessagesRequestType); |
| 2206 | } |
| 2207 | } |
| 2208 | |
| 2209 | void ServiceHandler::addOrUpdateMessages(quint64 action, const QString &filename, bool add) |
| 2210 | { |
| 2211 | QFile file(filename); |
| 2212 | QFileInfo fi(file); |
| 2213 | QMailMessageIdList ids; |
| 2214 | QString err; |
| 2215 | if (add) { |
| 2216 | err = tr("Unable to async add messages"); |
| 2217 | } else { |
| 2218 | err = tr("Unable to async update messages"); |
| 2219 | } |
| 2220 | if (fi.exists() && fi.isFile() && fi.isReadable()) { |
| 2221 | file.open(QIODevice::ReadOnly); |
| 2222 | QDataStream stream(&file); |
| 2223 | while (!stream.atEnd()) { |
| 2224 | QMailMessage message; |
| 2225 | QMailStore *store = QMailStore::instance(); |
| 2226 | stream >> message; |
| 2227 | if (add) { |
| 2228 | store->addMessage(&message); |
| 2229 | } else { |
| 2230 | store->updateMessage(&message); |
| 2231 | } |
| 2232 | if (store->lastError() != QMailStore::NoError) { |
| 2233 | reportFailure(action, QMailServiceAction::Status::ErrFrameworkFault, err); |
| 2234 | return; |
| 2235 | } |
| 2236 | ids.append(message.id()); |
| 2237 | } |
| 2238 | if (add) { |
| 2239 | emit messagesAdded(action, ids); |
| 2240 | } else { |
| 2241 | emit messagesUpdated(action, ids); |
| 2242 | } |
| 2243 | emit storageActionCompleted(action); |
| 2244 | emit activityChanged(action, QMailServiceAction::Successful); |
| 2245 | return; |
| 2246 | } |
| 2247 | file.remove(); |
| 2248 | reportFailure(action, QMailServiceAction::Status::ErrFrameworkFault, err); |
| 2249 | } |
| 2250 | |
| 2251 | void ServiceHandler::addMessages(quint64 action, const QString &filename) |
| 2252 | { |
| 2253 | addOrUpdateMessages(action, filename, true); |
| 2254 | QMailStore::instance()->flushIpcNotifications(); |
| 2255 | } |
| 2256 | |
| 2257 | void ServiceHandler::updateMessages(quint64 action, const QString &filename) |
| 2258 | { |
| 2259 | addOrUpdateMessages(action, filename, false); |
| 2260 | QMailStore::instance()->flushIpcNotifications(); |
| 2261 | } |
| 2262 | |
| 2263 | void ServiceHandler::addMessages(quint64 action, const QMailMessageMetaDataList &messages) |
| 2264 | { |
| 2265 | bool failure = false; |
| 2266 | QList<QMailMessageMetaData*> list; |
| 2267 | QString scheme; |
| 2268 | if (messages.count()) { |
| 2269 | scheme = messages.first().contentScheme(); |
| 2270 | } |
| 2271 | foreach (QMailMessageMetaData m, messages) { |
| 2272 | if (m.contentScheme() != scheme) { |
| 2273 | reportFailure(action, |
| 2274 | QMailServiceAction::Status::ErrFrameworkFault, |
| 2275 | tr("Unable to async add messages, " |
| 2276 | "inconsistent contentscheme")); |
| 2277 | } |
| 2278 | } |
| 2279 | foreach (QMailMessageMetaData m, messages) { |
| 2280 | list.append(new QMailMessageMetaData(m)); |
| 2281 | } |
| 2282 | if (scheme.isEmpty()) { |
| 2283 | scheme = QMailContentManagerFactory::defaultScheme(); |
| 2284 | } |
| 2285 | if (!list.isEmpty()) { |
| 2286 | QMailContentManager *content = QMailContentManagerFactory::create(scheme); |
| 2287 | content->ensureDurability(contentIdentifiers(list)); |
| 2288 | QMailStore *store = QMailStore::instance(); |
| 2289 | store->addMessages(list); |
| 2290 | failure |= (store->lastError() != QMailStore::NoError); |
| 2291 | store->ensureDurability(); |
| 2292 | failure |= (store->lastError() != QMailStore::NoError); |
| 2293 | } |
| 2294 | |
| 2295 | QMailMessageIdList ids; |
| 2296 | while (!list.isEmpty()) { |
| 2297 | QMailMessageMetaData *data(list.takeFirst()); |
| 2298 | ids.append(data->id()); |
| 2299 | delete data; |
| 2300 | } |
| 2301 | |
| 2302 | if (failure) { |
| 2303 | reportFailure(action, |
| 2304 | QMailServiceAction::Status::ErrFrameworkFault, |
| 2305 | tr("Unable to async update messages")); |
| 2306 | return; |
| 2307 | } |
| 2308 | |
| 2309 | emit messagesAdded(action, ids); |
| 2310 | emit storageActionCompleted(action); |
| 2311 | emit activityChanged(action, QMailServiceAction::Successful); |
| 2312 | QMailStore::instance()->flushIpcNotifications(); |
| 2313 | } |
| 2314 | |
| 2315 | void ServiceHandler::updateMessages(quint64 action, const QMailMessageMetaDataList &messages) |
| 2316 | { |
| 2317 | bool failure = false; |
| 2318 | QList<QMailMessageMetaData*> list; |
| 2319 | QString scheme; |
| 2320 | if (messages.count()) { |
| 2321 | scheme = messages.first().contentScheme(); |
| 2322 | } |
| 2323 | foreach (QMailMessageMetaData m, messages) { |
| 2324 | if (m.contentScheme() != scheme) { |
| 2325 | reportFailure(action, |
| 2326 | QMailServiceAction::Status::ErrFrameworkFault, |
| 2327 | tr("Unable to async update messages, " |
| 2328 | "inconsistent contentscheme")); |
| 2329 | } |
| 2330 | } |
| 2331 | foreach (QMailMessageMetaData m, messages) { |
| 2332 | list.append(new QMailMessageMetaData(m)); |
| 2333 | } |
| 2334 | if (scheme.isEmpty()) { |
| 2335 | scheme = QMailContentManagerFactory::defaultScheme(); |
| 2336 | } |
| 2337 | if (!list.isEmpty()) { |
| 2338 | QMailContentManager *content = QMailContentManagerFactory::create(scheme); |
| 2339 | QList<QString> obsoleteIds(obsoleteContentIdentifiers(list)); |
| 2340 | if (!obsoleteIds.isEmpty()) { |
| 2341 | content->ensureDurability(contentIdentifiers(list)); |
| 2342 | foreach (QMailMessageMetaData *m, list) { |
| 2343 | m->removeCustomField("qmf-obsolete-contentid"); |
| 2344 | } |
| 2345 | } // else only update metadata in mailstore |
| 2346 | QMailStore *store = QMailStore::instance(); |
| 2347 | store->updateMessages(list); |
| 2348 | failure |= (store->lastError() != QMailStore::NoError); |
| 2349 | store->ensureDurability(); |
| 2350 | failure |= (store->lastError() != QMailStore::NoError); |
| 2351 | |
| 2352 | content->remove(obsoleteIds); |
| 2353 | } |
| 2354 | |
| 2355 | QMailMessageIdList ids; |
| 2356 | while (!list.isEmpty()) { |
| 2357 | QMailMessageMetaData *data(list.takeFirst()); |
| 2358 | ids.append(data->id()); |
| 2359 | delete data; |
| 2360 | } |
| 2361 | |
| 2362 | if (failure) { |
| 2363 | reportFailure(action, |
| 2364 | QMailServiceAction::Status::ErrFrameworkFault, |
| 2365 | tr("Unable to async update messages")); |
| 2366 | return; |
| 2367 | } |
| 2368 | |
| 2369 | emit messagesUpdated(action, ids); |
| 2370 | emit storageActionCompleted(action); |
| 2371 | emit activityChanged(action, QMailServiceAction::Successful); |
| 2372 | QMailStore::instance()->flushIpcNotifications(); |
| 2373 | } |
| 2374 | |
| 2375 | bool ServiceHandler::dispatchOnlineFlagMessagesAndMoveToStandardFolder(quint64 action, const QByteArray &data) |
| 2376 | { |
| 2377 | QMap<QMailAccountId, QMailMessageIdList> messageLists; |
| 2378 | quint64 setMask; |
| 2379 | quint64 unsetMask; |
| 2380 | |
| 2381 | deserialize(data, messageLists, setMask, unsetMask); |
| 2382 | |
| 2383 | Q_ASSERT(!messageLists.empty()); |
| 2384 | |
| 2385 | QMap<QMailAccountId, QMailMessageIdList>::const_iterator it = messageLists.begin(), end = messageLists.end(); |
| 2386 | for ( ; it != end; ++it) { |
| 2387 | if (QMailMessageSource *source = accountSource(it.key())) { |
| 2388 | bool success(sourceService.value(source)->usesConcurrentActions() |
| 2389 | ? source->flagMessages(it.value(), setMask, unsetMask, action) |
| 2390 | : source->flagMessages(it.value(), setMask, unsetMask)); |
| 2391 | |
| 2392 | if (!success) { |
| 2393 | qWarning() << "Unable to service request to flag messages for account:" << it.key(); |
| 2394 | return false; |
| 2395 | } |
| 2396 | } else { |
| 2397 | reportFailure(action, QMailServiceAction::Status::ErrFrameworkFault, tr("Unable to locate source for account"), it.key()); |
| 2398 | return false; |
| 2399 | } |
| 2400 | } |
| 2401 | |
| 2402 | return true; |
| 2403 | } |
| 2404 | |
| 2405 | void ServiceHandler::deleteMessages(quint64 action, const QMailMessageIdList& messageIds) |
| 2406 | { |
| 2407 | uint total = messageIds.count(); |
| 2408 | |
| 2409 | // Just delete all these messages from device and mark for deletion on server when exportUpdates is called |
| 2410 | if (!messageIds.isEmpty() && !QMailStore::instance()->removeMessages(QMailMessageKey::id(messageIds), QMailStore::CreateRemovalRecord)) { |
| 2411 | qWarning() << "Unable to service request to delete messages"; |
| 2412 | reportFailure(action, QMailServiceAction::Status::ErrFrameworkFault, tr("Could not delete messages")); |
| 2413 | return; |
| 2414 | } |
| 2415 | |
| 2416 | emit progressChanged(action, total, total); |
| 2417 | emit messagesDeleted(action, messageIds); |
| 2418 | emit storageActionCompleted(action); |
| 2419 | emit activityChanged(action, QMailServiceAction::Successful); |
| 2420 | QMailStore::instance()->flushIpcNotifications(); |
| 2421 | return; |
| 2422 | } |
| 2423 | |
| 2424 | void ServiceHandler::rollBackUpdates(quint64 action, const QMailAccountId &mailAccountId) |
| 2425 | { |
| 2426 | QMailDisconnected::rollBackUpdates(mailAccountId); |
| 2427 | |
| 2428 | emit storageActionCompleted(action); |
| 2429 | emit activityChanged(action, QMailServiceAction::Successful); |
| 2430 | QMailStore::instance()->flushIpcNotifications(); |
| 2431 | return; |
| 2432 | } |
| 2433 | |
| 2434 | void ServiceHandler::moveToStandardFolder(quint64 action, const QMailMessageIdList& ids, quint64 standardFolder) |
| 2435 | { |
| 2436 | QMailDisconnected::moveToStandardFolder(ids, static_cast<QMailFolder::StandardFolder>(standardFolder)); |
| 2437 | messagesMoved(ids, action); |
| 2438 | messagesFlagged(ids, action); |
| 2439 | |
| 2440 | emit storageActionCompleted(action); |
| 2441 | emit activityChanged(action, QMailServiceAction::Successful); |
| 2442 | QMailStore::instance()->flushIpcNotifications(); |
| 2443 | return; |
| 2444 | } |
| 2445 | |
| 2446 | void ServiceHandler::moveToFolder(quint64 action, const QMailMessageIdList& ids, const QMailFolderId& folderId) |
| 2447 | { |
| 2448 | QMailDisconnected::moveToFolder(ids, folderId); |
| 2449 | messagesMoved(ids, action); |
| 2450 | |
| 2451 | emit storageActionCompleted(action); |
| 2452 | emit activityChanged(action, QMailServiceAction::Successful); |
| 2453 | QMailStore::instance()->flushIpcNotifications(); |
| 2454 | return; |
| 2455 | } |
| 2456 | |
| 2457 | void ServiceHandler::flagMessages(quint64 action, const QMailMessageIdList& ids, quint64 setMask, quint64 unsetMask) |
| 2458 | { |
| 2459 | QMailDisconnected::flagMessages(ids, setMask, unsetMask, ""); |
| 2460 | messagesFlagged(ids, action); |
| 2461 | |
| 2462 | emit storageActionCompleted(action); |
| 2463 | emit activityChanged(action, QMailServiceAction::Successful); |
| 2464 | QMailStore::instance()->flushIpcNotifications(); |
| 2465 | return; |
| 2466 | } |
| 2467 | |
| 2468 | void ServiceHandler::restoreToPreviousFolder(quint64 action, const QMailMessageKey& key) |
| 2469 | { |
| 2470 | QMailDisconnected::restoreToPreviousFolder(key); |
| 2471 | |
| 2472 | emit storageActionCompleted(action); |
| 2473 | emit activityChanged(action, QMailServiceAction::Successful); |
| 2474 | QMailStore::instance()->flushIpcNotifications(); |
| 2475 | return; |
| 2476 | } |
| 2477 | |
| 2478 | void ServiceHandler::onlineCreateFolder(quint64 action, const QString &name, const QMailAccountId &accountId, const QMailFolderId &parentId) |
| 2479 | { |
| 2480 | if(accountId.isValid()) { |
| 2481 | |
| 2482 | QSet<QMailAccountId> accounts; |
| 2483 | if (parentId.isValid()) { |
| 2484 | accounts = folderAccount(parentId); |
| 2485 | } else { |
| 2486 | accounts.insert(accountId); |
| 2487 | } |
| 2488 | |
| 2489 | QSet<QMailMessageService *> sources(sourceServiceSet(accounts)); |
| 2490 | |
| 2491 | enqueueRequest(action, serialize(name, accountId, parentId), sources, &ServiceHandler::dispatchOnlineCreateFolder, &ServiceHandler::storageActionCompleted, CreateFolderRequestType); |
| 2492 | } else { |
| 2493 | reportFailure(action, QMailServiceAction::Status::ErrInvalidData, tr("Unable to create folder for invalid account")); |
| 2494 | } |
| 2495 | } |
| 2496 | |
| 2497 | bool ServiceHandler::dispatchOnlineCreateFolder(quint64 action, const QByteArray &data) |
| 2498 | { |
| 2499 | QString name; |
| 2500 | QMailAccountId accountId; |
| 2501 | QMailFolderId parentId; |
| 2502 | |
| 2503 | deserialize(data, name, accountId, parentId); |
| 2504 | |
| 2505 | if(QMailMessageSource *source = accountSource(accountId)) { |
| 2506 | bool success(sourceService.value(source)->usesConcurrentActions() |
| 2507 | ? source->createFolder(name, accountId, parentId, action) |
| 2508 | : source->createFolder(name, accountId, parentId)); |
| 2509 | if (success) { |
| 2510 | return true; |
| 2511 | } else { |
| 2512 | qWarning() << "Unable to service request to create folder for account:" << accountId; |
| 2513 | return false; |
| 2514 | } |
| 2515 | |
| 2516 | } else { |
| 2517 | reportFailure(action, QMailServiceAction::Status::ErrFrameworkFault, tr("Unable to locate source for account"), accountId); |
| 2518 | return false; |
| 2519 | } |
| 2520 | } |
| 2521 | |
| 2522 | void ServiceHandler::onlineRenameFolder(quint64 action, const QMailFolderId &folderId, const QString &name) |
| 2523 | { |
| 2524 | if(folderId.isValid()) { |
| 2525 | QSet<QMailAccountId> accounts = folderAccount(folderId); |
| 2526 | QSet<QMailMessageService *> sources(sourceServiceSet(accounts)); |
| 2527 | |
| 2528 | enqueueRequest(action, serialize(folderId, name), sources, &ServiceHandler::dispatchOnlineRenameFolder, &ServiceHandler::storageActionCompleted, RenameFolderRequestType); |
| 2529 | } else { |
| 2530 | reportFailure(action, QMailServiceAction::Status::ErrInvalidData, tr("Unable to rename invalid folder")); |
| 2531 | } |
| 2532 | } |
| 2533 | |
| 2534 | bool ServiceHandler::dispatchOnlineRenameFolder(quint64 action, const QByteArray &data) |
| 2535 | { |
| 2536 | QMailFolderId folderId; |
| 2537 | QString newFolderName; |
| 2538 | |
| 2539 | deserialize(data, folderId, newFolderName); |
| 2540 | |
| 2541 | if(QMailMessageSource *source = accountSource(QMailFolder(folderId).parentAccountId())) { |
| 2542 | bool success(sourceService.value(source)->usesConcurrentActions() |
| 2543 | ? source->renameFolder(folderId, newFolderName, action) |
| 2544 | : source->renameFolder(folderId, newFolderName)); |
| 2545 | if (success) { |
| 2546 | return true; |
| 2547 | } else { |
| 2548 | qWarning() << "Unable to service request to rename folder id:" << folderId; |
| 2549 | return false; |
| 2550 | } |
| 2551 | |
| 2552 | } else { |
| 2553 | reportFailure(action, QMailServiceAction::Status::ErrFrameworkFault, tr("Unable to locate source for account"), QMailFolder(folderId).parentAccountId()); |
| 2554 | return false; |
| 2555 | } |
| 2556 | } |
| 2557 | |
| 2558 | void ServiceHandler::onlineDeleteFolder(quint64 action, const QMailFolderId &folderId) |
| 2559 | { |
| 2560 | if(folderId.isValid()) { |
| 2561 | QSet<QMailAccountId> accounts = folderAccount(folderId); |
| 2562 | QSet<QMailMessageService *> sources(sourceServiceSet(accounts)); |
| 2563 | |
| 2564 | enqueueRequest(action, serialize(folderId), sources, &ServiceHandler::dispatchOnlineDeleteFolder, &ServiceHandler::storageActionCompleted, DeleteFolderRequestType); |
| 2565 | } else { |
| 2566 | reportFailure(action, QMailServiceAction::Status::ErrInvalidData, tr("Unable to delete invalid folder")); |
| 2567 | } |
| 2568 | } |
| 2569 | |
| 2570 | bool ServiceHandler::dispatchOnlineDeleteFolder(quint64 action, const QByteArray &data) |
| 2571 | { |
| 2572 | QMailFolderId folderId; |
| 2573 | |
| 2574 | deserialize(data, folderId); |
| 2575 | |
| 2576 | if(QMailMessageSource *source = accountSource(QMailFolder(folderId).parentAccountId())) { |
| 2577 | if(source->deleteFolder(folderId)) { |
| 2578 | return true; |
| 2579 | } else { |
| 2580 | qWarning() << "Unable to service request to delete folder id:" << folderId; |
| 2581 | return false; |
| 2582 | } |
| 2583 | } else { |
| 2584 | reportFailure(action, QMailServiceAction::Status::ErrFrameworkFault, tr("Unable to locate source for account"), QMailFolder(folderId).parentAccountId()); |
| 2585 | return false; |
| 2586 | } |
| 2587 | } |
| 2588 | |
| 2589 | void ServiceHandler::searchMessages(quint64 action, const QMailMessageKey& filter, const QString& bodyText, QMailSearchAction::SearchSpecification spec, const QMailMessageSortKey &sort) |
| 2590 | { |
| 2591 | searchMessages(action, filter, bodyText, spec, 0, sort, NoLimit); |
| 2592 | } |
| 2593 | |
| 2594 | void ServiceHandler::searchMessages(quint64 action, const QMailMessageKey& filter, const QString& bodyText, QMailSearchAction::SearchSpecification spec, quint64 limit, const QMailMessageSortKey &sort) |
| 2595 | { |
| 2596 | searchMessages(action, filter, bodyText, spec, limit, sort, Limit); |
| 2597 | } |
| 2598 | |
| 2599 | void ServiceHandler::countMessages(quint64 action, const QMailMessageKey& filter, const QString& bodyText) |
| 2600 | { |
| 2601 | searchMessages(action, filter, bodyText, QMailSearchAction::Remote, 0, QMailMessageSortKey(), Count); |
| 2602 | } |
| 2603 | |
| 2604 | void ServiceHandler::searchMessages(quint64 action, const QMailMessageKey& filter, const QString& bodyText, QMailSearchAction::SearchSpecification spec, quint64 limit, const QMailMessageSortKey &sort, SearchType searchType) |
| 2605 | { |
| 2606 | if (spec == QMailSearchAction::Remote) { |
| 2607 | // Find the accounts that we need to search within from the criteria |
| 2608 | QSet<QMailAccountId> searchAccountIds(accountsApplicableTo(filter, sourceMap.keys().toSet())); |
| 2609 | |
| 2610 | QSet<QMailMessageService*> sources(sourceServiceSet(searchAccountIds)); |
| 2611 | if (sources.isEmpty()) { |
| 2612 | reportFailure(action, QMailServiceAction::Status::ErrNoConnection, tr("Unable to search messages for unconfigured account")); |
| 2613 | } else { |
| 2614 | enqueueRequest(action, serialize(searchAccountIds, filter, bodyText, limit, sort, static_cast<int>(searchType)), sources, &ServiceHandler::dispatchSearchMessages, &ServiceHandler::searchCompleted, SearchMessagesRequestType); |
| 2615 | } |
| 2616 | } else { |
| 2617 | // Find the messages that match the filter criteria |
| 2618 | QMailMessageIdList searchIds(QMailStore::instance()->queryMessages(filter, sort)); |
| 2619 | |
| 2620 | // Schedule this search |
| 2621 | mSearches.append(MessageSearch(action, searchIds, bodyText)); |
| 2622 | QTimer::singleShot(0, this, SLOT(continueSearch())); |
| 2623 | } |
| 2624 | } |
| 2625 | |
| 2626 | bool ServiceHandler::dispatchSearchMessages(quint64 action, const QByteArray &data) |
| 2627 | { |
| 2628 | QSet<QMailAccountId> accountIds; |
| 2629 | QMailMessageKey filter; |
| 2630 | QString bodyText; |
| 2631 | qint64 limit; |
| 2632 | QMailMessageSortKey sort; |
| 2633 | int searchType; |
| 2634 | bool sentSearch = false; |
| 2635 | |
| 2636 | deserialize(data, accountIds, filter, bodyText, limit, sort, searchType); |
| 2637 | |
| 2638 | foreach (const QMailAccountId &accountId, accountIds) { |
| 2639 | if (QMailMessageSource *source = accountSource(accountId)) { |
| 2640 | bool success(false); |
| 2641 | bool concurrent(sourceService.value(source)->usesConcurrentActions()); |
| 2642 | switch (searchType) |
| 2643 | { |
| 2644 | case NoLimit: |
| 2645 | if (concurrent) { |
| 2646 | success = source->searchMessages(filter, bodyText, sort, action); |
| 2647 | } else { |
| 2648 | success = source->searchMessages(filter, bodyText, sort); |
| 2649 | } |
| 2650 | break; |
| 2651 | case Limit: |
| 2652 | if (concurrent) { |
| 2653 | success = source->searchMessages(filter, bodyText, limit, sort, action); |
| 2654 | } else { |
| 2655 | success = source->searchMessages(filter, bodyText, limit, sort); |
| 2656 | } |
| 2657 | break; |
| 2658 | case Count: |
| 2659 | if (concurrent) { |
| 2660 | success = source->countMessages(filter, bodyText, action); |
| 2661 | } else { |
| 2662 | success = source->countMessages(filter, bodyText); |
| 2663 | } |
| 2664 | break; |
| 2665 | default: |
| 2666 | qWarning() << "Unknown SearchType"; |
| 2667 | break; |
| 2668 | } |
| 2669 | |
| 2670 | //only dispatch to appropriate account |
| 2671 | if (success) { |
| 2672 | sentSearch = true; //we've at least sent one |
| 2673 | } else { |
| 2674 | //do it locally instead |
| 2675 | qWarning() << "Unable to do remote search, doing it locally instead"; |
| 2676 | mSearches.append(MessageSearch(action, QMailStore::instance()->queryMessages(filter, sort), bodyText)); |
| 2677 | QTimer::singleShot(0, this, SLOT(continueSearch())); |
| 2678 | } |
| 2679 | } else { |
| 2680 | reportFailure(action, QMailServiceAction::Status::ErrFrameworkFault, tr("Unable to locate source for account"), accountId); |
| 2681 | } |
| 2682 | } |
| 2683 | |
| 2684 | return sentSearch; |
| 2685 | } |
| 2686 | |
| 2687 | |
| 2688 | void ServiceHandler::cancelLocalSearch(quint64 action) // cancel local search |
| 2689 | { |
| 2690 | QList<MessageSearch>::iterator it = mSearches.begin(), end = mSearches.end(); |
| 2691 | for ( ; it != end; ++it) { |
| 2692 | if ((*it).action() == action) { |
| 2693 | if (!mMatchingIds.isEmpty()) |
| 2694 | emit matchingMessageIds(action, mMatchingIds); |
| 2695 | |
| 2696 | emit searchCompleted(action); |
| 2697 | |
| 2698 | mSearches.erase(it); |
| 2699 | return; |
| 2700 | } |
| 2701 | } |
| 2702 | } |
| 2703 | |
| 2704 | void ServiceHandler::shutdown() |
| 2705 | { |
| 2706 | QTimer::singleShot(0,QCoreApplication::instance(),SLOT(quit())); |
| 2707 | } |
| 2708 | |
| 2709 | void ServiceHandler::listActions() |
| 2710 | { |
| 2711 | QMailActionDataList list; |
| 2712 | |
| 2713 | for(QMap<quint64, ActionData>::iterator i(mActiveActions.begin()) ; i != mActiveActions.end(); ++i) |
| 2714 | { |
| 2715 | QMailActionData t(i.key(), i->description, i->progressTotal, i->progressCurrent, |
| 2716 | i->status.errorCode, i->status.text, |
| 2717 | i->status.accountId, i->status.folderId, i->status.messageId); |
| 2718 | list.append(t); |
| 2719 | } |
| 2720 | |
| 2721 | emit actionsListed(list); |
| 2722 | } |
| 2723 | |
| 2724 | // concurrent actions |
| 2725 | void ServiceHandler::statusChanged(const QMailServiceAction::Status s, quint64 a) |
| 2726 | { |
| 2727 | QMap<quint64, ActionData>::iterator it = mActiveActions.find(a); |
| 2728 | if (it != mActiveActions.end()) { |
| 2729 | it->status = s; |
| 2730 | } |
| 2731 | emit statusChanged(a, s); |
| 2732 | } |
| 2733 | |
| 2734 | void ServiceHandler::availabilityChanged(bool b, quint64 a) |
| 2735 | { |
| 2736 | emit availabilityChanged(a, b); |
| 2737 | } |
| 2738 | |
| 2739 | void ServiceHandler::connectivityChanged(QMailServiceAction::Connectivity c, quint64 a) |
| 2740 | { |
| 2741 | emit connectivityChanged(a, c); |
| 2742 | } |
| 2743 | |
| 2744 | void ServiceHandler::activityChanged(QMailServiceAction::Activity act, quint64 a) |
| 2745 | { |
| 2746 | emit activityChanged(a, act); |
| 2747 | } |
| 2748 | |
| 2749 | void ServiceHandler::progressChanged(uint p, uint t, quint64 a) |
| 2750 | { |
| 2751 | QMap<quint64, ActionData>::iterator it = mActiveActions.find(a); |
| 2752 | if (it != mActiveActions.end()) { |
| 2753 | it->progressCurrent = p; |
| 2754 | it->progressTotal = t; |
| 2755 | } |
| 2756 | emit progressChanged(a, p, t); |
| 2757 | } |
| 2758 | |
| 2759 | void ServiceHandler::actionCompleted(bool success, quint64 action) |
| 2760 | { |
| 2761 | qDebug() << "Action completed" << action << "result" << (success ? "success" : "failure"); |
| 2762 | QMailMessageService *service = qobject_cast<QMailMessageService*>(sender()); |
| 2763 | Q_ASSERT(service); |
| 2764 | |
| 2765 | actionCompleted(success, service, action); |
| 2766 | } |
| 2767 | |
| 2768 | void ServiceHandler::actionCompleted(bool success, QMailMessageService *service, quint64 action) |
| 2769 | { |
| 2770 | qDebug() << "Action completed" << action << "result" << (success ? "success" : "failure"); |
| 2771 | QMap<quint64, ActionData>::iterator it = mActiveActions.find(action); |
| 2772 | if (it != mActiveActions.end()) { |
| 2773 | ActionData &data(it.value()); |
| 2774 | |
| 2775 | if (!mSentIds.isEmpty() && (data.completion == &ServiceHandler::transmissionCompleted)) { |
| 2776 | if (accountSource(service->accountId())) { |
| 2777 | // Mark these message as Sent |
| 2778 | quint64 setMask(QMailMessage::Sent); |
| 2779 | quint64 unsetMask(QMailMessage::Outbox | QMailMessage::Draft | QMailMessage::LocalOnly); |
| 2780 | |
| 2781 | QMailMessageKey idsKey(QMailMessageKey::id(mSentIds)); |
| 2782 | |
| 2783 | bool failed = !QMailStore::instance()->updateMessagesMetaData(idsKey, setMask, true); |
| 2784 | failed = !QMailStore::instance()->updateMessagesMetaData(idsKey, unsetMask, false) || failed; |
| 2785 | |
| 2786 | if (failed) { |
| 2787 | qWarning() << "Unable to flag messages:" << mSentIds; |
| 2788 | } |
| 2789 | |
| 2790 | // FWOD messages have already been uploaded to the remote server, don't try to upload twice |
| 2791 | quint64 externalStatus(QMailMessage::TransmitFromExternal | QMailMessage::HasUnresolvedReferences); |
| 2792 | QMailMessageKey externalKey(QMailMessageKey::status(externalStatus, QMailDataComparator::Includes)); |
| 2793 | QMailMessageKey sentIdsKey(QMailMessageKey::id(mSentIds)); |
| 2794 | QMailMessageIdList sentNonFwodIds = QMailStore::instance()->queryMessages(sentIdsKey & ~externalKey); |
| 2795 | |
| 2796 | // Move sent messages to sent folder on remote server |
| 2797 | QMap<QMailAccountId, QMailMessageIdList> groupedMessages(accountMessages(sentNonFwodIds)); |
| 2798 | if (!groupedMessages.empty()) { // messages are still around |
| 2799 | enqueueRequest(newLocalActionId(), |
| 2800 | serialize(groupedMessages, setMask, unsetMask), |
| 2801 | sourceServiceSet(service->accountId()), |
| 2802 | &ServiceHandler::dispatchOnlineFlagMessagesAndMoveToStandardFolder, |
| 2803 | &ServiceHandler::storageActionCompleted, |
| 2804 | FlagMessagesRequestType); |
| 2805 | } |
| 2806 | } |
| 2807 | |
| 2808 | mSentIds.clear(); |
| 2809 | } |
| 2810 | |
| 2811 | QSet<QPointer<QMailMessageService> >::iterator sit = data.services.find(service); |
| 2812 | if (sit != data.services.end()) { |
| 2813 | // Remove this service from the set for the action |
| 2814 | data.services.erase(sit); |
| 2815 | |
| 2816 | // This account is no longer retrieving/transmitting |
| 2817 | QMailAccountId accountId(service->accountId()); |
| 2818 | if (accountId.isValid()) { |
| 2819 | if (data.completion == &ServiceHandler::retrievalCompleted) { |
| 2820 | setRetrievalInProgress(accountId, false); |
| 2821 | } else if (data.completion == &ServiceHandler::transmissionCompleted) { |
| 2822 | setTransmissionInProgress(accountId, false); |
| 2823 | } |
| 2824 | } |
| 2825 | |
| 2826 | if (success) { |
| 2827 | if (data.services.isEmpty() && data.completion && (data.reported == false)) { |
| 2828 | // Report success |
| 2829 | emit (this->*data.completion)(action); |
| 2830 | data.reported = true; |
| 2831 | } |
| 2832 | } else { |
| 2833 | // This action has failed - mark it so that it can't be reported as successful |
| 2834 | data.reported = true; |
| 2835 | } |
| 2836 | } |
| 2837 | |
| 2838 | if (data.services.isEmpty()) { |
| 2839 | // This action is finished |
| 2840 | mActiveActions.erase(it); |
| 2841 | emit activityChanged(action, success ? QMailServiceAction::Successful : QMailServiceAction::Failed); |
| 2842 | } |
| 2843 | } |
| 2844 | |
| 2845 | if (_outstandingRequests.contains(action)) { |
| 2846 | _outstandingRequests.remove(action); |
| 2847 | |
| 2848 | // Ensure this request is no longer in the file |
| 2849 | _requestsFile.resize(0); |
| 2850 | foreach (quint64 req, _outstandingRequests) { |
| 2851 | QByteArray requestNumber(QByteArray::number(req)); |
| 2852 | _requestsFile.write(requestNumber.append('\n')); |
| 2853 | } |
| 2854 | _requestsFile.flush(); |
| 2855 | } |
| 2856 | |
| 2857 | mServiceAction.remove(service); |
| 2858 | |
| 2859 | |
| 2860 | // See if there are pending requests |
| 2861 | QTimer::singleShot(0, this, SLOT(dispatchRequest())); |
| 2862 | } |
| 2863 | |
| 2864 | void ServiceHandler::messagesTransmitted(const QMailMessageIdList& ml, quint64 a) |
| 2865 | { |
| 2866 | emit messagesTransmitted(a, ml); |
| 2867 | } |
| 2868 | |
| 2869 | void ServiceHandler::messagesFailedTransmission(const QMailMessageIdList& ml, QMailServiceAction::Status::ErrorCode err, quint64 a) |
| 2870 | { |
| 2871 | emit messagesFailedTransmission(a, ml, err); |
| 2872 | } |
| 2873 | |
| 2874 | void ServiceHandler::messagesDeleted(const QMailMessageIdList& ml, quint64 a) |
| 2875 | { |
| 2876 | emit messagesDeleted(a, ml); |
| 2877 | } |
| 2878 | |
| 2879 | void ServiceHandler::messagesCopied(const QMailMessageIdList& ml, quint64 a) |
| 2880 | { |
| 2881 | emit messagesCopied(a, ml); |
| 2882 | } |
| 2883 | |
| 2884 | void ServiceHandler::messagesMoved(const QMailMessageIdList& ml, quint64 a) |
| 2885 | { |
| 2886 | emit messagesMoved(a, ml); |
| 2887 | } |
| 2888 | |
| 2889 | void ServiceHandler::messagesFlagged(const QMailMessageIdList& ml, quint64 a) |
| 2890 | { |
| 2891 | emit messagesFlagged(a, ml); |
| 2892 | } |
| 2893 | |
| 2894 | void ServiceHandler::messagesPrepared(const QMailMessageIdList& ml, quint64 a) |
| 2895 | { |
| 2896 | Q_UNUSED(ml); |
| 2897 | Q_UNUSED(a); |
| 2898 | } |
| 2899 | |
| 2900 | void ServiceHandler::matchingMessageIds(const QMailMessageIdList& ml, quint64 a) |
| 2901 | { |
| 2902 | emit matchingMessageIds(a, ml); |
| 2903 | } |
| 2904 | |
| 2905 | void ServiceHandler::remainingMessagesCount(uint count, quint64 a) |
| 2906 | { |
| 2907 | emit remainingMessagesCount(a, count); |
| 2908 | } |
| 2909 | |
| 2910 | void ServiceHandler::messagesCount(uint count, quint64 a) |
| 2911 | { |
| 2912 | emit messagesCount(a, count); |
| 2913 | } |
| 2914 | |
| 2915 | void ServiceHandler::protocolResponse(const QString &response, const QVariant &data, quint64 a) |
| 2916 | { |
| 2917 | emit protocolResponse(a, response, data); |
| 2918 | } |
| 2919 | |
| 2920 | // end concurrent actions |
| 2921 | |
| 2922 | void ServiceHandler::protocolRequest(quint64 action, const QMailAccountId &accountId, const QString &request, const QVariant &data) |
| 2923 | { |
| 2924 | QSet<QMailMessageService*> sources(sourceServiceSet(accountId)); |
| 2925 | if (sources.isEmpty()) { |
| 2926 | reportFailure(action, QMailServiceAction::Status::ErrNoConnection, tr("Unable to forward protocol-specific request for unconfigured account")); |
| 2927 | } else { |
| 2928 | enqueueRequest(action, serialize(accountId, request, data), sources, &ServiceHandler::dispatchProtocolRequest, &ServiceHandler::protocolRequestCompleted, ProtocolRequestRequestType); |
| 2929 | } |
| 2930 | } |
| 2931 | |
| 2932 | bool ServiceHandler::dispatchProtocolRequest(quint64 action, const QByteArray &data) |
| 2933 | { |
| 2934 | QMailAccountId accountId; |
| 2935 | QString request; |
| 2936 | QVariant requestData; |
| 2937 | |
| 2938 | deserialize(data, accountId, request, requestData); |
| 2939 | |
| 2940 | if (QMailMessageSource *source = accountSource(accountId)) { |
| 2941 | bool success(sourceService.value(source)->usesConcurrentActions() |
| 2942 | ? source->protocolRequest(accountId, request, requestData, action) |
| 2943 | : source->protocolRequest(accountId, request, requestData)); |
| 2944 | if (!success) { |
| 2945 | qWarning() << "Unable to service request to forward protocol-specific request to account:" << accountId; |
| 2946 | return false; |
| 2947 | } |
| 2948 | } else { |
| 2949 | reportFailure(action, QMailServiceAction::Status::ErrFrameworkFault, tr("Unable to locate source for account"), accountId); |
| 2950 | return false; |
| 2951 | } |
| 2952 | |
| 2953 | return true; |
| 2954 | } |
| 2955 | |
| 2956 | void ServiceHandler::messagesDeleted(const QMailMessageIdList &messageIds) |
| 2957 | { |
| 2958 | if (quint64 action = sourceAction(qobject_cast<QMailMessageSource*>(sender()))) |
| 2959 | emit messagesDeleted(action, messageIds); |
| 2960 | } |
| 2961 | |
| 2962 | void ServiceHandler::messagesCopied(const QMailMessageIdList &messageIds) |
| 2963 | { |
| 2964 | if (quint64 action = sourceAction(qobject_cast<QMailMessageSource*>(sender()))) |
| 2965 | emit messagesCopied(action, messageIds); |
| 2966 | } |
| 2967 | |
| 2968 | void ServiceHandler::messagesMoved(const QMailMessageIdList &messageIds) |
| 2969 | { |
| 2970 | if (quint64 action = sourceAction(qobject_cast<QMailMessageSource*>(sender()))) |
| 2971 | emit messagesMoved(action, messageIds); |
| 2972 | } |
| 2973 | |
| 2974 | void ServiceHandler::messagesFlagged(const QMailMessageIdList &messageIds) |
| 2975 | { |
| 2976 | if (quint64 action = sourceAction(qobject_cast<QMailMessageSource*>(sender()))) |
| 2977 | emit messagesFlagged(action, messageIds); |
| 2978 | } |
| 2979 | |
| 2980 | void ServiceHandler::messagesPrepared(const QMailMessageIdList &messageIds) |
| 2981 | { |
| 2982 | Q_UNUSED(messageIds) |
| 2983 | } |
| 2984 | |
| 2985 | void ServiceHandler::matchingMessageIds(const QMailMessageIdList &messageIds) |
| 2986 | { |
| 2987 | if (quint64 action = sourceAction(qobject_cast<QMailMessageSource*>(sender()))) |
| 2988 | emit matchingMessageIds(action, messageIds); |
| 2989 | } |
| 2990 | |
| 2991 | void ServiceHandler::remainingMessagesCount(uint count) |
| 2992 | { |
| 2993 | if (quint64 action = sourceAction(qobject_cast<QMailMessageSource*>(sender()))) |
| 2994 | emit remainingMessagesCount(action, count); |
| 2995 | } |
| 2996 | |
| 2997 | void ServiceHandler::messagesCount(uint count) |
| 2998 | { |
| 2999 | if (quint64 action = sourceAction(qobject_cast<QMailMessageSource*>(sender()))) |
| 3000 | emit messagesCount(action, count); |
| 3001 | } |
| 3002 | |
| 3003 | void ServiceHandler::protocolResponse(const QString &response, const QVariant &data) |
| 3004 | { |
| 3005 | if (quint64 action = sourceAction(qobject_cast<QMailMessageSource*>(sender()))) |
| 3006 | emit protocolResponse(action, response, data); |
| 3007 | } |
| 3008 | |
| 3009 | void ServiceHandler::messagesTransmitted(const QMailMessageIdList &messageIds) |
| 3010 | { |
| 3011 | if (QMailMessageSink *sink = qobject_cast<QMailMessageSink*>(sender())) { |
| 3012 | if (quint64 action = sinkAction(sink)) { |
| 3013 | mSentIds << messageIds; |
| 3014 | |
| 3015 | emit messagesTransmitted(action, messageIds); |
| 3016 | } |
| 3017 | } |
| 3018 | } |
| 3019 | |
| 3020 | void ServiceHandler::messagesFailedTransmission(const QMailMessageIdList &messageIds, QMailServiceAction::Status::ErrorCode error) |
| 3021 | { |
| 3022 | if (QMailMessageSink *sink = qobject_cast<QMailMessageSink*>(sender())) { |
| 3023 | if (quint64 action = sinkAction(sink)) { |
| 3024 | emit messagesFailedTransmission(action, messageIds, error); |
| 3025 | } |
| 3026 | } |
| 3027 | } |
| 3028 | |
| 3029 | void ServiceHandler::availabilityChanged(bool available) |
| 3030 | { |
| 3031 | if (QMailMessageService *service = qobject_cast<QMailMessageService*>(sender())) { |
| 3032 | if (available) { |
| 3033 | mUnavailableServices.remove(service); |
| 3034 | |
| 3035 | // See if there are pending requests that can now be dispatched |
| 3036 | QTimer::singleShot(0, this, SLOT(dispatchRequest())); |
| 3037 | } else { |
| 3038 | mUnavailableServices.insert(service); |
| 3039 | } |
| 3040 | } |
| 3041 | } |
| 3042 | |
| 3043 | void ServiceHandler::connectivityChanged(QMailServiceAction::Connectivity connectivity) |
| 3044 | { |
| 3045 | if (QMailMessageService *service = qobject_cast<QMailMessageService*>(sender())) |
| 3046 | if (quint64 action = serviceAction(service)) { |
| 3047 | updateAction(action); |
| 3048 | emit connectivityChanged(action, connectivity); |
| 3049 | } |
| 3050 | } |
| 3051 | |
| 3052 | void ServiceHandler::activityChanged(QMailServiceAction::Activity activity) |
| 3053 | { |
| 3054 | if (QMailMessageService *service = qobject_cast<QMailMessageService*>(sender())) |
| 3055 | if (quint64 action = serviceAction(service)) { |
| 3056 | updateAction(action); |
| 3057 | emit activityChanged(action, activity); |
| 3058 | } |
| 3059 | } |
| 3060 | |
| 3061 | void ServiceHandler::statusChanged(const QMailServiceAction::Status status) |
| 3062 | { |
| 3063 | if (QMailMessageService *service = qobject_cast<QMailMessageService*>(sender())) |
| 3064 | if (quint64 action = serviceAction(service)) { |
| 3065 | updateAction(action); |
| 3066 | QMap<quint64, ActionData>::iterator it = mActiveActions.find(action); |
| 3067 | if (it != mActiveActions.end()) { |
| 3068 | it->status = status; |
| 3069 | } |
| 3070 | reportFailure(action, status); |
| 3071 | } |
| 3072 | } |
| 3073 | |
| 3074 | void ServiceHandler::progressChanged(uint progress, uint total) |
| 3075 | { |
| 3076 | if (QMailMessageService *service = qobject_cast<QMailMessageService*>(sender())) |
| 3077 | if (quint64 action = serviceAction(service)) { |
| 3078 | updateAction(action); |
| 3079 | QMap<quint64, ActionData>::iterator it = mActiveActions.find(action); |
| 3080 | if (it != mActiveActions.end()) { |
| 3081 | it->progressCurrent = progress; |
| 3082 | it->progressTotal = total; |
| 3083 | } |
| 3084 | emit progressChanged(action, progress, total); |
| 3085 | } |
| 3086 | } |
| 3087 | |
| 3088 | void ServiceHandler::actionCompleted(bool success) |
| 3089 | { |
| 3090 | if (QMailMessageService *service = qobject_cast<QMailMessageService*>(sender())) { |
| 3091 | if (quint64 action = serviceAction(service)) { |
| 3092 | actionCompleted(success, service, action); |
| 3093 | return; |
| 3094 | } |
| 3095 | } |
| 3096 | |
| 3097 | qWarning() << "Would not determine server/action completing"; |
| 3098 | // See if there are pending requests |
| 3099 | QTimer::singleShot(0, this, SLOT(dispatchRequest())); |
| 3100 | } |
| 3101 | |
| 3102 | void ServiceHandler::reportFailure(quint64 action, QMailServiceAction::Status::ErrorCode code, const QString &text, const QMailAccountId &accountId, const QMailFolderId &folderId, const QMailMessageId &messageId) |
| 3103 | { |
| 3104 | reportFailure(action, QMailServiceAction::Status(code, text, accountId, folderId, messageId)); |
| 3105 | } |
| 3106 | |
| 3107 | void ServiceHandler::reportFailure(quint64 action, const QMailServiceAction::Status status) |
| 3108 | { |
| 3109 | if (status.errorCode != QMailServiceAction::Status::ErrNoError |
| 3110 | && mActiveActions.contains(action)) { |
| 3111 | markFailedMessage(mActiveActions[action].description, status); |
| 3112 | } |
| 3113 | |
| 3114 | emit statusChanged(action, status); |
| 3115 | |
| 3116 | // Treat all errors as failures |
| 3117 | if (status.errorCode != QMailServiceAction::Status::ErrNoError) |
| 3118 | emit activityChanged(action, QMailServiceAction::Failed); |
| 3119 | } |
| 3120 | |
| 3121 | void ServiceHandler::continueSearch() |
| 3122 | { |
| 3123 | if (!mSearches.isEmpty()) { |
| 3124 | MessageSearch ¤tSearch(mSearches.first()); |
| 3125 | |
| 3126 | if (currentSearch.pending()) { |
| 3127 | mMatchingIds.clear(); |
| 3128 | currentSearch.inProgress(); |
| 3129 | |
| 3130 | if (!currentSearch.isEmpty()) { |
| 3131 | emit progressChanged(currentSearch.action(), 0, currentSearch.total()); |
| 3132 | } |
| 3133 | } |
| 3134 | |
| 3135 | if (!currentSearch.isEmpty()) { |
| 3136 | foreach (const QMailMessageId &id, currentSearch.takeBatch()) { |
| 3137 | QMailMessage message(id); |
| 3138 | QString text(currentSearch.bodyText()); |
| 3139 | if (text.isEmpty() || messageBodyContainsText(message, text)) |
| 3140 | mMatchingIds.append(id); |
| 3141 | } |
| 3142 | |
| 3143 | emit progressChanged(currentSearch.action(), currentSearch.progress(), currentSearch.total()); |
| 3144 | } |
| 3145 | |
| 3146 | if (currentSearch.isEmpty()) { |
| 3147 | // Nothing more to search - we're finished |
| 3148 | emit matchingMessageIds(currentSearch.action(), mMatchingIds); |
| 3149 | emit searchCompleted(currentSearch.action()); |
| 3150 | |
| 3151 | if (mActiveActions.contains(currentSearch.action())) { |
| 3152 | // There is remote searching in progress - wait for completion |
| 3153 | } else { |
| 3154 | // We're finished with this search |
| 3155 | mSearches.removeFirst(); |
| 3156 | |
| 3157 | if (!mSearches.isEmpty()) |
| 3158 | QTimer::singleShot(0, this, SLOT(continueSearch())); |
| 3159 | } |
| 3160 | } else { |
| 3161 | QTimer::singleShot(0, this, SLOT(continueSearch())); |
| 3162 | } |
| 3163 | } |
| 3164 | } |
| 3165 | |
| 3166 | void ServiceHandler::reportFailures() |
| 3167 | { |
| 3168 | // We have no active accounts at this point |
| 3169 | QMailStore::instance()->setRetrievalInProgress(QMailAccountIdList()); |
| 3170 | QMailStore::instance()->setTransmissionInProgress(QMailAccountIdList()); |
| 3171 | |
| 3172 | while (!_failedRequests.isEmpty()) { |
| 3173 | quint64 action(_failedRequests.takeFirst()); |
| 3174 | reportFailure(action, QMailServiceAction::Status::ErrFrameworkFault, tr("Failed to perform requested action!")); |
| 3175 | } |
| 3176 | } |
| 3177 | |
| 3178 | void ServiceHandler::setRetrievalInProgress(const QMailAccountId &accountId, bool inProgress) |
| 3179 | { |
| 3180 | bool modified(false); |
| 3181 | |
| 3182 | if (inProgress && !_retrievalAccountIds.contains(accountId)) { |
| 3183 | _retrievalAccountIds.insert(accountId); |
| 3184 | modified = true; |
| 3185 | } else if (!inProgress && _retrievalAccountIds.contains(accountId)) { |
| 3186 | _retrievalAccountIds.remove(accountId); |
| 3187 | modified = true; |
| 3188 | } |
| 3189 | |
| 3190 | if (modified) { |
| 3191 | QMailStore::instance()->setRetrievalInProgress(_retrievalAccountIds.toList()); |
| 3192 | } |
| 3193 | } |
| 3194 | |
| 3195 | void ServiceHandler::setTransmissionInProgress(const QMailAccountId &accountId, bool inProgress) |
| 3196 | { |
| 3197 | bool modified(false); |
| 3198 | |
| 3199 | if (inProgress && !_transmissionAccountIds.contains(accountId)) { |
| 3200 | _transmissionAccountIds.insert(accountId); |
| 3201 | modified = true; |
| 3202 | } else if (!inProgress && _transmissionAccountIds.contains(accountId)) { |
| 3203 | _transmissionAccountIds.remove(accountId); |
| 3204 | modified = true; |
| 3205 | } |
| 3206 | |
| 3207 | if (modified) { |
| 3208 | QMailStore::instance()->setTransmissionInProgress(_transmissionAccountIds.toList()); |
| 3209 | } |
| 3210 | } |
| 3211 | |
| 3212 | uint qHash(QPointer<QMailMessageService> service) |
| 3213 | { |
| 3214 | return qHash(service.data()); |
| 3215 | } |
| 3216 |