| 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 | #include "messageserver.h" |
| 43 | #include "servicehandler.h" |
| 44 | #include "mailmessageclient.h" |
| 45 | #include <qmailfolder.h> |
| 46 | #include <qmailmessage.h> |
| 47 | #include <qmailstore.h> |
| 48 | #include <QDataStream> |
| 49 | #include <QTimer> |
| 50 | #include <QProcess> |
| 51 | #include <qmaillog.h> |
| 52 | #include <qmailipc.h> |
| 53 | #include <newcountnotifier.h> |
| 54 | #include <qcopserver.h> |
| 55 | #include <qmailmessageserverplugin.h> |
| 56 | |
| 57 | extern "C" { |
| 58 | #ifndef Q_OS_WIN |
| 59 | #include <sys/socket.h> |
| 60 | #endif |
| 61 | #include <signal.h> |
| 62 | } |
| 63 | |
| 64 | #if defined(Q_OS_UNIX) |
| 65 | #include <unistd.h> |
| 66 | int MessageServer::sighupFd[2]; |
| 67 | #endif |
| 68 | |
| 69 | #ifdef CLICK |
| 70 | #define NO_NOTIFY_SEND |
| 71 | #endif |
| 72 | |
| 73 | MessageServer::MessageServer(QObject *parent) |
| 74 | : QObject(parent), |
| 75 | handler(0), |
| 76 | client(new MailMessageClient(this)), |
| 77 | messageCountUpdate("QPE/Messages/MessageCountUpdated"), |
| 78 | newMessageTotal(0), |
| 79 | completionAttempted(false), |
| 80 | m_started(QDateTime::currentDateTime()) |
| 81 | { |
| 82 | qDebug() << "MessageServer ctor begin"; |
| 83 | new QCopServer(this); |
| 84 | |
| 85 | #if defined(Q_OS_UNIX) |
| 86 | // SIGHUP handler. We use the trick described here: http://doc.trolltech.com/4.7-snapshot/unix-signals.html |
| 87 | // Looks shocking but the trick has certain reasons stated in Steven's book: http://cr.yp.to/docs/selfpipe.html |
| 88 | // Use a socket and notifier because signal handlers can't call Qt code |
| 89 | |
| 90 | if (::socketpair(AF_UNIX, SOCK_STREAM, 0, sighupFd)) |
| 91 | qFatal("Couldn't create HUP socketpair"); |
| 92 | snHup = new QSocketNotifier(sighupFd[1], QSocketNotifier::Read, this); |
| 93 | connect(snHup, SIGNAL(activated(int)), this, SLOT(handleSigHup())); |
| 94 | |
| 95 | struct sigaction hup; |
| 96 | hup.sa_handler = MessageServer::hupSignalHandler; |
| 97 | sigemptyset(&hup.sa_mask); |
| 98 | hup.sa_flags = 0; |
| 99 | hup.sa_flags |= SA_RESTART; |
| 100 | if (sigaction(SIGHUP, &hup, 0) > 0) |
| 101 | qFatal("Couldn't register HUP handler"); |
| 102 | #endif // defined(Q_OS_UNIX) |
| 103 | |
| 104 | QMailMessageCountMap::iterator it = messageCounts.begin(), end = messageCounts.end(); |
| 105 | for ( ; it != end; ++it) |
| 106 | it.value() = 0; |
| 107 | |
| 108 | QMailStore *store = QMailStore::instance(); |
| 109 | if (store->initializationState() != QMailStore::Initialized) { |
| 110 | qFatal("Messaging DB Invalid: Messaging cannot operate due to database incompatibilty!"); |
| 111 | // Do not close, however, or QPE will start another instance. |
| 112 | } else { |
| 113 | handler = new ServiceHandler(this); |
| 114 | |
| 115 | connect(store, SIGNAL(messagesAdded(QMailMessageIdList)), |
| 116 | this, SLOT(messagesAdded(QMailMessageIdList))); |
| 117 | connect(store, SIGNAL(messagesAdded(QMailMessageIdList)), |
| 118 | this, SLOT(notifyNewMessages(QMailMessageIdList))); |
| 119 | connect(store, SIGNAL(messagesUpdated(QMailMessageIdList)), |
| 120 | this, SLOT(messagesUpdated(QMailMessageIdList))); |
| 121 | connect(store, SIGNAL(messagesRemoved(QMailMessageIdList)), |
| 122 | this, SLOT(messagesRemoved(QMailMessageIdList))); |
| 123 | |
| 124 | // Propagate email handler signals to the client |
| 125 | connect(handler, SIGNAL(actionStarted(QMailActionData)), |
| 126 | client, SIGNAL(actionStarted(QMailActionData))); |
| 127 | connect(handler, SIGNAL(activityChanged(quint64, QMailServiceAction::Activity)), |
| 128 | client, SIGNAL(activityChanged(quint64, QMailServiceAction::Activity))); |
| 129 | connect(handler, SIGNAL(connectivityChanged(quint64, QMailServiceAction::Connectivity)), |
| 130 | client, SIGNAL(connectivityChanged(quint64, QMailServiceAction::Connectivity))); |
| 131 | connect(handler, SIGNAL(statusChanged(quint64, const QMailServiceAction::Status)), |
| 132 | client, SIGNAL(statusChanged(quint64, const QMailServiceAction::Status))); |
| 133 | connect(handler, SIGNAL(progressChanged(quint64, uint, uint)), |
| 134 | client, SIGNAL(progressChanged(quint64, uint, uint))); |
| 135 | connect(handler, SIGNAL(messagesDeleted(quint64, QMailMessageIdList)), |
| 136 | client, SIGNAL(messagesDeleted(quint64, QMailMessageIdList))); |
| 137 | connect(handler, SIGNAL(messagesCopied(quint64, QMailMessageIdList)), |
| 138 | client, SIGNAL(messagesCopied(quint64, QMailMessageIdList))); |
| 139 | connect(handler, SIGNAL(messagesMoved(quint64, QMailMessageIdList)), |
| 140 | client, SIGNAL(messagesMoved(quint64, QMailMessageIdList))); |
| 141 | connect(handler, SIGNAL(messagesFlagged(quint64, QMailMessageIdList)), |
| 142 | client, SIGNAL(messagesFlagged(quint64, QMailMessageIdList))); |
| 143 | connect(handler, SIGNAL(folderCreated(quint64, QMailFolderId)), |
| 144 | client, SIGNAL(folderCreated(quint64, QMailFolderId))); |
| 145 | connect(handler, SIGNAL(folderRenamed(quint64, QMailFolderId)), |
| 146 | client, SIGNAL(folderRenamed(quint64, QMailFolderId))); |
| 147 | connect(handler, SIGNAL(folderDeleted(quint64, QMailFolderId)), |
| 148 | client, SIGNAL(folderDeleted(quint64, QMailFolderId))); |
| 149 | connect(handler, SIGNAL(storageActionCompleted(quint64)), |
| 150 | client, SIGNAL(storageActionCompleted(quint64))); |
| 151 | connect(handler, SIGNAL(matchingMessageIds(quint64, QMailMessageIdList)), |
| 152 | client, SIGNAL(matchingMessageIds(quint64, QMailMessageIdList))); |
| 153 | connect(handler, SIGNAL(remainingMessagesCount(quint64, uint)), |
| 154 | client, SIGNAL(remainingMessagesCount(quint64, uint))); |
| 155 | connect(handler, SIGNAL(messagesCount(quint64, uint)), |
| 156 | client, SIGNAL(messagesCount(quint64, uint))); |
| 157 | connect(handler, SIGNAL(searchCompleted(quint64)), |
| 158 | client, SIGNAL(searchCompleted(quint64))); |
| 159 | connect(handler, SIGNAL(actionsListed(QMailActionDataList)), |
| 160 | client, SIGNAL(actionsListed(QMailActionDataList))); |
| 161 | connect(handler, SIGNAL(protocolResponse(quint64, QString, QVariant)), |
| 162 | client, SIGNAL(protocolResponse(quint64, QString, QVariant))); |
| 163 | connect(handler, SIGNAL(protocolRequestCompleted(quint64)), |
| 164 | client, SIGNAL(protocolRequestCompleted(quint64))); |
| 165 | connect(handler, SIGNAL(messagesTransmitted(quint64, QMailMessageIdList)), |
| 166 | client, SIGNAL(messagesTransmitted(quint64, QMailMessageIdList))); |
| 167 | connect(handler, SIGNAL(messagesFailedTransmission(quint64, QMailMessageIdList, QMailServiceAction::Status::ErrorCode)), |
| 168 | client, SIGNAL(messagesFailedTransmission(quint64, QMailMessageIdList, QMailServiceAction::Status::ErrorCode))); |
| 169 | |
| 170 | connect(handler, SIGNAL(transmissionCompleted(quint64)), |
| 171 | this, SLOT(transmissionCompleted(quint64))); |
| 172 | connect(handler, SIGNAL(retrievalCompleted(quint64)), |
| 173 | this, SLOT(retrievalCompleted(quint64))); |
| 174 | |
| 175 | // The email handler should handle the email client signals |
| 176 | connect(client, SIGNAL(transmitMessages(quint64, QMailAccountId)), |
| 177 | handler, SLOT(transmitMessages(quint64, QMailAccountId))); |
| 178 | connect(client, SIGNAL(transmitMessage(quint64, QMailMessageId)), |
| 179 | handler, SLOT(transmitMessage(quint64, QMailMessageId))); |
| 180 | connect(client, SIGNAL(retrieveFolderList(quint64, QMailAccountId, QMailFolderId, bool)), |
| 181 | handler, SLOT(retrieveFolderList(quint64, QMailAccountId, QMailFolderId, bool))); |
| 182 | connect(client, SIGNAL(retrieveMessageList(quint64, QMailAccountId, QMailFolderId, uint, QMailMessageSortKey)), |
| 183 | handler, SLOT(retrieveMessageList(quint64, QMailAccountId, QMailFolderId, uint, QMailMessageSortKey))); |
| 184 | connect(client, SIGNAL(retrieveMessageLists(quint64, QMailAccountId, QMailFolderIdList, uint, QMailMessageSortKey)), |
| 185 | handler, SLOT(retrieveMessageLists(quint64, QMailAccountId, QMailFolderIdList, uint, QMailMessageSortKey))); |
| 186 | connect(client, SIGNAL(retrieveNewMessages(quint64, QMailAccountId, QMailFolderIdList)), |
| 187 | handler, SLOT(retrieveNewMessages(quint64, QMailAccountId, QMailFolderIdList))); |
| 188 | connect(client, SIGNAL(createStandardFolders(quint64, QMailAccountId)), |
| 189 | handler, SLOT(createStandardFolders(quint64, QMailAccountId))); |
| 190 | connect(client, SIGNAL(retrieveMessages(quint64, QMailMessageIdList, QMailRetrievalAction::RetrievalSpecification)), |
| 191 | handler, SLOT(retrieveMessages(quint64, QMailMessageIdList, QMailRetrievalAction::RetrievalSpecification))); |
| 192 | connect(client, SIGNAL(retrieveMessagePart(quint64, QMailMessagePart::Location)), |
| 193 | handler, SLOT(retrieveMessagePart(quint64, QMailMessagePart::Location))); |
| 194 | connect(client, SIGNAL(retrieveMessageRange(quint64, QMailMessageId, uint)), |
| 195 | handler, SLOT(retrieveMessageRange(quint64, QMailMessageId, uint))); |
| 196 | connect(client, SIGNAL(retrieveMessagePartRange(quint64, QMailMessagePart::Location, uint)), |
| 197 | handler, SLOT(retrieveMessagePartRange(quint64, QMailMessagePart::Location, uint))); |
| 198 | connect(client, SIGNAL(retrieveAll(quint64, QMailAccountId)), |
| 199 | handler, SLOT(retrieveAll(quint64, QMailAccountId))); |
| 200 | connect(client, SIGNAL(exportUpdates(quint64, QMailAccountId)), |
| 201 | handler, SLOT(exportUpdates(quint64, QMailAccountId))); |
| 202 | connect(client, SIGNAL(synchronize(quint64, QMailAccountId)), |
| 203 | handler, SLOT(synchronize(quint64, QMailAccountId))); |
| 204 | connect(client, SIGNAL(onlineDeleteMessages(quint64, QMailMessageIdList, QMailStore::MessageRemovalOption)), |
| 205 | handler, SLOT(onlineDeleteMessages(quint64, QMailMessageIdList, QMailStore::MessageRemovalOption))); |
| 206 | connect(client, SIGNAL(onlineCopyMessages(quint64, QMailMessageIdList, QMailFolderId)), |
| 207 | handler, SLOT(onlineCopyMessages(quint64, QMailMessageIdList, QMailFolderId))); |
| 208 | connect(client, SIGNAL(onlineMoveMessages(quint64, QMailMessageIdList, QMailFolderId)), |
| 209 | handler, SLOT(onlineMoveMessages(quint64, QMailMessageIdList, QMailFolderId))); |
| 210 | connect(client, SIGNAL(onlineFlagMessagesAndMoveToStandardFolder(quint64, QMailMessageIdList, quint64, quint64)), |
| 211 | handler, SLOT(onlineFlagMessagesAndMoveToStandardFolder(quint64, QMailMessageIdList, quint64, quint64))); |
| 212 | connect(client, SIGNAL(addMessages(quint64, QString)), |
| 213 | handler, SLOT(addMessages(quint64, QString))); |
| 214 | connect(client, SIGNAL(updateMessages(quint64, QString)), |
| 215 | handler, SLOT(updateMessages(quint64, QString))); |
| 216 | connect(client, SIGNAL(addMessages(quint64, QMailMessageMetaDataList)), |
| 217 | handler, SLOT(addMessages(quint64, QMailMessageMetaDataList))); |
| 218 | connect(client, SIGNAL(updateMessages(quint64, QMailMessageMetaDataList)), |
| 219 | handler, SLOT(updateMessages(quint64, QMailMessageMetaDataList))); |
| 220 | connect(client, SIGNAL(deleteMessages(quint64, QMailMessageIdList)), |
| 221 | handler, SLOT(deleteMessages(quint64, QMailMessageIdList))); |
| 222 | connect(client, SIGNAL(rollBackUpdates(quint64, QMailAccountId)), |
| 223 | handler, SLOT(rollBackUpdates(quint64, QMailAccountId))); |
| 224 | connect(client, SIGNAL(moveToStandardFolder(quint64, QMailMessageIdList, quint64)), |
| 225 | handler, SLOT(moveToStandardFolder(quint64, QMailMessageIdList, quint64))); |
| 226 | connect(client, SIGNAL(moveToFolder(quint64, QMailMessageIdList, QMailFolderId)), |
| 227 | handler, SLOT(moveToFolder(quint64, QMailMessageIdList, QMailFolderId))); |
| 228 | connect(client, SIGNAL(flagMessages(quint64, QMailMessageIdList, quint64, quint64)), |
| 229 | handler, SLOT(flagMessages(quint64, QMailMessageIdList, quint64, quint64))); |
| 230 | connect(client, SIGNAL(restoreToPreviousFolder(quint64, QMailMessageKey)), |
| 231 | handler, SLOT(restoreToPreviousFolder(quint64, QMailMessageKey))); |
| 232 | connect(client, SIGNAL(onlineCreateFolder(quint64, QString, QMailAccountId, QMailFolderId)), |
| 233 | handler, SLOT(onlineCreateFolder(quint64, QString, QMailAccountId, QMailFolderId))); |
| 234 | connect(client, SIGNAL(onlineRenameFolder(quint64, QMailFolderId, QString)), |
| 235 | handler, SLOT(onlineRenameFolder(quint64, QMailFolderId, QString))); |
| 236 | connect(client, SIGNAL(onlineDeleteFolder(quint64, QMailFolderId)), |
| 237 | handler, SLOT(onlineDeleteFolder(quint64, QMailFolderId))); |
| 238 | connect(client, SIGNAL(cancelTransfer(quint64)), |
| 239 | handler, SLOT(cancelTransfer(quint64))); |
| 240 | connect(client, SIGNAL(protocolRequest(quint64, QMailAccountId, QString, QVariant)), |
| 241 | handler, SLOT(protocolRequest(quint64, QMailAccountId, QString, QVariant))); |
| 242 | connect(client, SIGNAL(searchMessages(quint64, QMailMessageKey, QString, QMailSearchAction::SearchSpecification, QMailMessageSortKey)), |
| 243 | handler, SLOT(searchMessages(quint64, QMailMessageKey, QString, QMailSearchAction::SearchSpecification, QMailMessageSortKey))); |
| 244 | connect(client, SIGNAL(searchMessages(quint64, QMailMessageKey, QString, QMailSearchAction::SearchSpecification, quint64, QMailMessageSortKey)), |
| 245 | handler, SLOT(searchMessages(quint64, QMailMessageKey, QString, QMailSearchAction::SearchSpecification, quint64, QMailMessageSortKey))); |
| 246 | connect(client, SIGNAL(countMessages(quint64, QMailMessageKey, QString)), |
| 247 | handler, SLOT(countMessages(quint64, QMailMessageKey, QString))); |
| 248 | connect(client, SIGNAL(shutdown()), |
| 249 | handler, SLOT(shutdown())); |
| 250 | connect(client, SIGNAL(listActions()), |
| 251 | handler, SLOT(listActions())); |
| 252 | connect(handler, SIGNAL(newMessagesAvailable()), |
| 253 | this, SLOT(reportNewCounts())); |
| 254 | connect(client, SIGNAL(acknowledgeNewMessages(QMailMessageTypeList)), |
| 255 | this, SLOT(acknowledgeNewMessages(QMailMessageTypeList))); |
| 256 | |
| 257 | QCopAdaptor::connect(this, SIGNAL(messageCountUpdated()), |
| 258 | &messageCountUpdate, MESSAGE(changeValue())); |
| 259 | |
| 260 | //clean up any temporary messages that were not cleaned up by clients |
| 261 | QTimer::singleShot(0, this, SLOT(cleanupTemporaryMessages())); |
| 262 | |
| 263 | emit client->actionsListed(QMailActionDataList()); |
| 264 | } |
| 265 | |
| 266 | #ifdef MESSAGESERVER_PLUGINS |
| 267 | qDebug() << "Initiating messageserver plugins."; |
| 268 | QStringList availablePlugins = QMailMessageServerPluginFactory::keys(); |
| 269 | |
| 270 | for (int i = 0; i < availablePlugins.size(); i++) { |
| 271 | QMailMessageServerPlugin *plugin = QMailMessageServerPluginFactory::createService(availablePlugins.at(i)); |
| 272 | plugin->exec(); |
| 273 | } |
| 274 | #endif |
| 275 | } |
| 276 | |
| 277 | MessageServer::~MessageServer() |
| 278 | { |
| 279 | } |
| 280 | |
| 281 | void MessageServer::retrievalCompleted(quint64 action) |
| 282 | { |
| 283 | // Ensure the client receives any resulting events before a notification |
| 284 | QMailStore::instance()->flushIpcNotifications(); |
| 285 | |
| 286 | if (!completionList.isEmpty()) { |
| 287 | if (!completionAttempted) { |
| 288 | // Complete the messages that we selected for immediate completion |
| 289 | completionAttempted = true; |
| 290 | handler->retrieveMessages(action, completionList.toList(), QMailRetrievalAction::Content); |
| 291 | return; |
| 292 | } else { |
| 293 | completionList.clear(); |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | completionAttempted = false; |
| 298 | emit client->retrievalCompleted(action); |
| 299 | } |
| 300 | |
| 301 | QMap<QMailMessage::MessageType, QString> typeSignatureInit() |
| 302 | { |
| 303 | QMap<QMailMessage::MessageType, QString> map; |
| 304 | |
| 305 | map.insert(QMailMessage::Sms, "newSmsCount(int)"); |
| 306 | map.insert(QMailMessage::Mms, "newMmsCount(int)"); |
| 307 | map.insert(QMailMessage::Email, "newEmailCount(int)"); |
| 308 | map.insert(QMailMessage::Instant, "newInstantCount(int)"); |
| 309 | map.insert(QMailMessage::System, "newSystemCount(int)"); |
| 310 | |
| 311 | return map; |
| 312 | } |
| 313 | |
| 314 | void MessageServer::acknowledgeNewMessages(const QMailMessageTypeList& types) |
| 315 | { |
| 316 | foreach (QMailMessage::MessageType type, types) { |
| 317 | // No messages of this type are new any longer |
| 318 | QMailMessageKey newMessages(QMailMessageKey::messageType(type)); |
| 319 | newMessages &= QMailMessageKey(QMailMessageKey::status(QMailMessage::New, QMailDataComparator::Includes)); |
| 320 | QMailStore::instance()->updateMessagesMetaData(newMessages, QMailMessage::New, false); |
| 321 | |
| 322 | if (messageCounts[type] != 0) { |
| 323 | newMessageTotal -= messageCounts[type]; |
| 324 | |
| 325 | messageCounts[type] = 0; |
| 326 | NewCountNotifier::notify(type, 0); |
| 327 | } |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | static QMap<QMailMessage::MessageType, QString> typeServiceInit() |
| 332 | { |
| 333 | QMap<QMailMessage::MessageType, QString> map; |
| 334 | |
| 335 | map.insert(QMailMessage::Sms, "NewSmsArrival"); |
| 336 | map.insert(QMailMessage::Mms, "NewMmsArrival"); |
| 337 | map.insert(QMailMessage::Email, "NewEmailArrival"); |
| 338 | map.insert(QMailMessage::Instant, "NewInstantMessageArrival"); |
| 339 | map.insert(QMailMessage::System, "NewSystemMessageArrival"); |
| 340 | |
| 341 | return map; |
| 342 | } |
| 343 | |
| 344 | QString serviceForType(QMailMessage::MessageType type) |
| 345 | { |
| 346 | static QMap<QMailMessage::MessageType, QString> typeService(typeServiceInit()); |
| 347 | return typeService[type]; |
| 348 | } |
| 349 | |
| 350 | int MessageServer::newMessageCount(QMailMessage::MessageType type) const |
| 351 | { |
| 352 | QMailMessageKey newMessageKey(QMailMessageKey::status(QMailMessage::New, QMailDataComparator::Includes)); |
| 353 | if (type != QMailMessage::AnyType) { |
| 354 | newMessageKey &= QMailMessageKey::messageType(type); |
| 355 | } |
| 356 | |
| 357 | return QMailStore::instance()->countMessages(newMessageKey); |
| 358 | } |
| 359 | |
| 360 | void MessageServer::reportNewCounts() |
| 361 | { |
| 362 | static QMap<QMailMessage::MessageType, QString> typeSignature(typeSignatureInit()); |
| 363 | |
| 364 | QMailMessageCountMap newCounts; |
| 365 | foreach (const QMailMessage::MessageType &type, typeSignature.keys()) { |
| 366 | newCounts[type] = newMessageCount(type); |
| 367 | } |
| 368 | |
| 369 | newMessageTotal = newMessageCount(QMailMessage::AnyType); |
| 370 | |
| 371 | if (newMessageTotal) { |
| 372 | // Inform QPE of changes to the new message counts |
| 373 | foreach (const QMailMessage::MessageType &type, typeSignature.keys()) { |
| 374 | if ((newCounts[type] > 0) && (newCounts[type] != messageCounts[type])) |
| 375 | NewCountNotifier::notify(type, newCounts[type]); |
| 376 | } |
| 377 | |
| 378 | // Request handling of the new message events |
| 379 | QMailMessageCountMap::const_iterator it = newCounts.begin(), end = newCounts.end(); |
| 380 | for ( ; it != end; ++it) { |
| 381 | QMailMessage::MessageType type(it.key()); |
| 382 | if (it.value() != messageCounts[type]) { |
| 383 | // This type's count has changed since last reported |
| 384 | |
| 385 | if( NewCountNotifier* action = new NewCountNotifier(type,it.value())) { |
| 386 | actionType[action] = type; |
| 387 | |
| 388 | connect(action, SIGNAL(response(bool)), this, SLOT(response(bool))); |
| 389 | connect(action, SIGNAL(error(QString)), this, SLOT(error(QString))); |
| 390 | |
| 391 | // Ensure the client receives any generated events before the arrival notification |
| 392 | QMailStore::instance()->flushIpcNotifications(); |
| 393 | if (!action->notify()) |
| 394 | qWarning() << "Unable to invoke service:" << serviceForType(type); |
| 395 | } |
| 396 | } |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | messageCounts = newCounts; |
| 401 | } |
| 402 | |
| 403 | void MessageServer::response(bool handled) |
| 404 | { |
| 405 | if (NewCountNotifier* action = static_cast<NewCountNotifier*>(sender())) { |
| 406 | if (handled) { |
| 407 | acknowledgeNewMessages(QMailMessageTypeList() << actionType[action]); |
| 408 | } |
| 409 | actionType.remove(action); |
| 410 | action->deleteLater(); |
| 411 | if (actionType.isEmpty()) { |
| 412 | // All outstanding handler events have been processed |
| 413 | emit messageCountUpdated(); |
| 414 | } |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | void MessageServer::error(const QString &message) |
| 419 | { |
| 420 | if (NewCountNotifier* action = static_cast<NewCountNotifier*>(sender())) { |
| 421 | qWarning() << "Unable to complete service:" << serviceForType(actionType[action]) << "-" << message; |
| 422 | actionType.remove(action); |
| 423 | action->deleteLater(); |
| 424 | } |
| 425 | |
| 426 | if (actionType.isEmpty()) { |
| 427 | // No outstanding handler events remain |
| 428 | emit messageCountUpdated(); |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | void MessageServer::transmissionCompleted(quint64 action) |
| 433 | { |
| 434 | // Ensure the client receives any resulting events before the completion notification |
| 435 | QMailStore::instance()->flushIpcNotifications(); |
| 436 | |
| 437 | emit client->transmissionCompleted(action); |
| 438 | } |
| 439 | |
| 440 | void MessageServer::messagesAdded(const QMailMessageIdList &ids) |
| 441 | { |
| 442 | if (!QMailStore::instance()->asynchronousEmission()) { |
| 443 | // Added in our process - from retrieval |
| 444 | foreach (const QMailMessageId &id, ids) { |
| 445 | QMailMessageMetaData message(id); |
| 446 | |
| 447 | bool complete(false); |
| 448 | if (!(message.status() & QMailMessage::ContentAvailable)) { |
| 449 | // Automatically download voicemail messages |
| 450 | if (message.content() == QMailMessage::VoicemailContent || |
| 451 | message.content() == QMailMessage::VideomailContent) { |
| 452 | complete = true; |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | if (complete) |
| 457 | completionList.insert(message.id()); |
| 458 | } |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | void MessageServer::messagesUpdated(const QMailMessageIdList &ids) |
| 463 | { |
| 464 | if (QMailStore::instance()->asynchronousEmission()) { |
| 465 | // Only need to check message counts if the update occurred in another process |
| 466 | updateNewMessageCounts(); |
| 467 | } else { |
| 468 | // If we're updating, check whether the messages have been marked as Removed |
| 469 | foreach (const QMailMessageId &id, ids) { |
| 470 | if (completionList.contains(id)) { |
| 471 | QMailMessageMetaData message(id); |
| 472 | if ((message.status() & QMailMessage::ContentAvailable) || (message.status() & QMailMessage::Removed)) { |
| 473 | // This message has been completed (or removed) |
| 474 | completionList.remove(id); |
| 475 | } |
| 476 | } |
| 477 | } |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | void MessageServer::messagesRemoved(const QMailMessageIdList &ids) |
| 482 | { |
| 483 | foreach (const QMailMessageId &id, ids) { |
| 484 | // No need to complete deleted messages |
| 485 | completionList.remove(id); |
| 486 | } |
| 487 | |
| 488 | updateNewMessageCounts(); |
| 489 | } |
| 490 | |
| 491 | void MessageServer::updateNewMessageCounts() |
| 492 | { |
| 493 | int newTotal = newMessageCount(QMailMessage::AnyType); |
| 494 | if (newTotal != newMessageTotal) { |
| 495 | // The number of messages marked as new has changed, but not via a message arrival event |
| 496 | static QMap<QMailMessage::MessageType, QString> typeSignature(typeSignatureInit()); |
| 497 | |
| 498 | // Update the individual counts |
| 499 | foreach (const QMailMessage::MessageType &type, typeSignature.keys()) { |
| 500 | int count(newMessageCount(type)); |
| 501 | if (count != messageCounts[type]) { |
| 502 | messageCounts[type] = count; |
| 503 | NewCountNotifier::notify(type, count); |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | emit messageCountUpdated(); |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | void MessageServer::cleanupTemporaryMessages() |
| 512 | { |
| 513 | QMailStore::instance()->removeMessages(QMailMessageKey::status(QMailMessage::Temporary), QMailStore::NoRemovalRecord); |
| 514 | } |
| 515 | |
| 516 | void MessageServer::notifyNewMessages(const QMailMessageIdList &ids) |
| 517 | { |
| 518 | #ifdef NO_NOTIFY_SEND |
| 519 | Q_UNUSED(ids); |
| 520 | #else |
| 521 | Q_FOREACH(const QMailMessageId &id, ids) { |
| 522 | QMailMessageMetaData d(id); |
| 523 | if ((d.status() & QMailMessage::NoNotification)) { |
| 524 | continue; |
| 525 | } |
| 526 | if (d.date().toLocalTime() < m_started) { |
| 527 | continue; |
| 528 | } |
| 529 | if ((d.status() & QMailMessage::Incoming) && |
| 530 | (d.status() & QMailMessage::New)) { |
| 531 | QMailMessage msg(id); |
| 532 | QProcess proc; |
| 533 | QStringList options; |
| 534 | options.append("-i"); |
| 535 | options.append("/usr/share/icons/dekko.png"); |
| 536 | options.append(msg.from().name()); |
| 537 | options.append(msg.subject() + "\n" + msg.preview()); |
| 538 | proc.start("notify-send", options); |
| 539 | proc.waitForFinished(1000); |
| 540 | } |
| 541 | } |
| 542 | #endif |
| 543 | } |
| 544 | |
| 545 | #if defined(Q_OS_UNIX) |
| 546 | void MessageServer::hupSignalHandler(int) |
| 547 | { |
| 548 | // Can't call Qt code. Write to the socket and the notifier will fire from the Qt event loop |
| 549 | char a = 1; |
| 550 | ::write(sighupFd[0], &a, sizeof(a)); |
| 551 | } |
| 552 | |
| 553 | void MessageServer::handleSigHup() |
| 554 | { |
| 555 | snHup->setEnabled(false); |
| 556 | char tmp; |
| 557 | ::read(sighupFd[1], &tmp, sizeof(tmp)); |
| 558 | #if defined(CLICK) |
| 559 | qMailLoggersRecreate("dekko2.dekkoproject", "dekkod", "Msgsrv"); |
| 560 | #else |
| 561 | qMailLoggersRecreate("dekko", "dekkod", "Msgsrv"); |
| 562 | #endif |
| 563 | |
| 564 | snHup->setEnabled(true); |
| 565 | } |
| 566 | |
| 567 | #endif // defined(Q_OS_UNIX) |
| 568 | |
| 569 | #ifdef SERVER_AS_QTHREAD |
| 570 | MessageServerThread::MessageServerThread() |
| 571 | { |
| 572 | } |
| 573 | |
| 574 | MessageServerThread::~MessageServerThread() |
| 575 | { |
| 576 | // Tell the thread's event loop to exit |
| 577 | // => thread returns from the call to exec() |
| 578 | exit(); |
| 579 | |
| 580 | // Wait until this thread has finished execution |
| 581 | // <=> waits until thread returns from run() |
| 582 | wait(); |
| 583 | } |
| 584 | |
| 585 | void MessageServerThread::run() |
| 586 | { |
| 587 | // This is ~/.config/dekko.dekkoproject/dekkod.conf |
| 588 | // qMailLoggersRecreate("dekko.dekkoproject", "dekkod", "Msgsrv"); |
| 589 | // Start messageserver |
| 590 | MessageServer server; |
| 591 | |
| 592 | emit messageServerStarted(); |
| 593 | |
| 594 | // Enter the thread event loop |
| 595 | (void) exec(); |
| 596 | } |
| 597 | #endif |