src/messaging/qmessagestore_qmf.cpp
changeset 0 876b1a06bc25
equal deleted inserted replaced
-1:000000000000 0:876b1a06bc25
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the Qt Mobility Components.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 #include "qmessagestore.h"
       
    42 #include "qmessagemanager.h"
       
    43 #include "qmfhelpers_p.h"
       
    44 
       
    45 #include <qmailstore.h>
       
    46 
       
    47 #include <QCoreApplication>
       
    48 #include <QEvent>
       
    49 
       
    50 #include "qmessagestore_qmf_p.h"
       
    51 
       
    52 using namespace QTM_PREPEND_NAMESPACE(QmfHelpers);
       
    53 
       
    54 
       
    55 QTM_BEGIN_NAMESPACE
       
    56 
       
    57 Q_SCOPED_STATIC_DEFINE(QMessageStore,QMessageStorePrivate,storeInstance);
       
    58 
       
    59 QMailStore *QMessageStorePrivate::convert(QMessageStore *store)
       
    60 {
       
    61     return store->d_ptr->_store;
       
    62 }
       
    63 
       
    64 QMailStore *QMessageStorePrivate::convert(QMessageManager &manager)
       
    65 {
       
    66     return manager.store->d_ptr->_store;
       
    67 }
       
    68 
       
    69 void QMessageStorePrivate::registerMessageStatus(QMailStore *store, const QString &field)
       
    70 {
       
    71     store->registerMessageStatusFlag(field);
       
    72 }
       
    73 
       
    74 void QMessageStorePrivate::createNonexistentFolder(QMailStore *store, const QString &path, quint64 status)
       
    75 {
       
    76     QMailFolderKey pathKey(QMailFolderKey::path(path));
       
    77     QMailFolderKey accountKey(QMailFolderKey::parentAccountId(QMailAccountId()));
       
    78 
       
    79     if (store->countFolders(pathKey & accountKey) == 0) {
       
    80         QMailFolder folder;
       
    81         folder.setPath(path);
       
    82         folder.setStatus(status);
       
    83 
       
    84         if (!store->addFolder(&folder)) {
       
    85             qWarning() << "Unable to add folder for:" << path;
       
    86         }
       
    87     }
       
    88 }
       
    89 
       
    90 void QMessageStorePrivate::messagesAdded(const QMailMessageIdList &ids)
       
    91 {
       
    92     processFilters(ids, &QMessageStorePrivate::messageAdded);
       
    93 }
       
    94 
       
    95 void QMessageStorePrivate::messagesRemoved(const QMailMessageIdList &ids)
       
    96 {
       
    97     processFilters(ids, &QMessageStorePrivate::messageRemoved);
       
    98 }
       
    99 
       
   100 void QMessageStorePrivate::messagesUpdated(const QMailMessageIdList &ids)
       
   101 {
       
   102     processFilters(ids, &QMessageStorePrivate::messageUpdated);
       
   103 }
       
   104 
       
   105 namespace QmfHelpers {
       
   106 
       
   107 QMailStore *convert(QMessageStore *store)
       
   108 {
       
   109     return QMessageStorePrivate::convert(store);
       
   110 }
       
   111 
       
   112 QMailStore *convert(QMessageManager &manager)
       
   113 {
       
   114     return QMessageStorePrivate::convert(manager);
       
   115 }
       
   116 
       
   117 }
       
   118 
       
   119 QMessageStore::QMessageStore(QObject *parent)
       
   120     : QObject(parent),
       
   121       d_ptr(new QMessageStorePrivate)
       
   122 {
       
   123     connect(d_ptr, SIGNAL(messageAdded(QMessageId, QMessageManager::NotificationFilterIdSet)), this, SIGNAL(messageAdded(QMessageId, QMessageManager::NotificationFilterIdSet)));
       
   124     connect(d_ptr, SIGNAL(messageRemoved(QMessageId, QMessageManager::NotificationFilterIdSet)), this, SIGNAL(messageRemoved(QMessageId, QMessageManager::NotificationFilterIdSet)));
       
   125     connect(d_ptr, SIGNAL(messageUpdated(QMessageId, QMessageManager::NotificationFilterIdSet)), this, SIGNAL(messageUpdated(QMessageId, QMessageManager::NotificationFilterIdSet)));
       
   126 }
       
   127 
       
   128 QMessageStore::~QMessageStore()
       
   129 {
       
   130     delete d_ptr;
       
   131 }
       
   132 
       
   133 QMessageManager::Error QMessageStore::error() const
       
   134 {
       
   135     if (d_ptr->_error != QMessageManager::NoError) {
       
   136         return d_ptr->_error;
       
   137     }
       
   138 
       
   139     return convert(d_ptr->_store->lastError());
       
   140 }
       
   141 
       
   142 QMessageIdList QMessageStore::queryMessages(const QMessageFilter &filter, const QMessageSortOrder &sortOrder, uint limit, uint offset) const
       
   143 {
       
   144     if (filter.matchFlags() != 0) {
       
   145         d_ptr->_error = QMessageManager::NotYetImplemented;
       
   146         return QMessageIdList();
       
   147     }
       
   148     
       
   149     d_ptr->_error = QMessageManager::NoError;
       
   150     return convert(d_ptr->_store->queryMessages(convert(filter), convert(sortOrder), limit, offset));
       
   151 }
       
   152 
       
   153 QMessageIdList QMessageStore::queryMessages(const QMessageFilter &filter, const QString &body, QMessageDataComparator::MatchFlags matchFlags, const QMessageSortOrder &sortOrder, uint limit, uint offset) const
       
   154 {
       
   155     if (filter.matchFlags() != 0) {
       
   156         d_ptr->_error = QMessageManager::NotYetImplemented;
       
   157         return QMessageIdList();
       
   158     }
       
   159     
       
   160     d_ptr->_error = QMessageManager::NoError;
       
   161     QMessageIdList ids(convert(d_ptr->_store->queryMessages(convert(filter), convert(sortOrder))));
       
   162     
       
   163     Qt::CaseSensitivity searchOptions(Qt::CaseInsensitive);
       
   164     if (matchFlags & QMessageDataComparator::MatchCaseSensitive) {
       
   165         searchOptions = Qt::CaseSensitive;
       
   166     }
       
   167     
       
   168     QMessageIdList result;
       
   169     foreach (QMessageId id, ids) {
       
   170         QMessage message(id);
       
   171         QMessageContentContainer bodyContainer(message.find(message.bodyId()));
       
   172         if (bodyContainer.textContent().contains(body, searchOptions)) {
       
   173             result.append(id);
       
   174             if (limit && ((uint)result.count() >= offset + limit)) {
       
   175                 break;
       
   176             }
       
   177         }
       
   178     }
       
   179     
       
   180     if (offset) {
       
   181         return result.mid(offset, (limit ? limit : -1));
       
   182     } else {
       
   183         return result;
       
   184     }
       
   185 }
       
   186 
       
   187 QMessageFolderIdList QMessageStore::queryFolders(const QMessageFolderFilter &filter, const QMessageFolderSortOrder &sortOrder, uint limit, uint offset) const
       
   188 {
       
   189     if (filter.matchFlags() != 0) {
       
   190         d_ptr->_error = QMessageManager::NotYetImplemented;
       
   191         return QMessageFolderIdList();
       
   192     }
       
   193     
       
   194     d_ptr->_error = QMessageManager::NoError;
       
   195     return convert(d_ptr->_store->queryFolders(convert(filter), convert(sortOrder), limit, offset));
       
   196 }
       
   197 
       
   198 QMessageAccountIdList QMessageStore::queryAccounts(const QMessageAccountFilter &filter, const QMessageAccountSortOrder &sortOrder, uint limit, uint offset) const
       
   199 {
       
   200     if (filter.matchFlags() != 0) {
       
   201         d_ptr->_error = QMessageManager::NotYetImplemented;
       
   202         return QMessageAccountIdList();
       
   203     }
       
   204     
       
   205     d_ptr->_error = QMessageManager::NoError;
       
   206     return convert(d_ptr->_store->queryAccounts(convert(filter), convert(sortOrder), limit, offset));
       
   207 }
       
   208 
       
   209 int QMessageStore::countMessages(const QMessageFilter& filter) const
       
   210 {
       
   211     if (filter.matchFlags() != 0) {
       
   212         d_ptr->_error = QMessageManager::NotYetImplemented;
       
   213         return 0;
       
   214     }
       
   215     
       
   216     d_ptr->_error = QMessageManager::NoError;
       
   217     return d_ptr->_store->countMessages(convert(filter));
       
   218 }
       
   219 
       
   220 int QMessageStore::countFolders(const QMessageFolderFilter& filter) const
       
   221 {
       
   222     if (filter.matchFlags() != 0) {
       
   223         d_ptr->_error = QMessageManager::NotYetImplemented;
       
   224         return 0;
       
   225     }
       
   226     
       
   227     d_ptr->_error = QMessageManager::NoError;
       
   228     return d_ptr->_store->countFolders(convert(filter));
       
   229 }
       
   230 
       
   231 int QMessageStore::countAccounts(const QMessageAccountFilter& filter) const
       
   232 {
       
   233     if (filter.matchFlags() != 0) {
       
   234         d_ptr->_error = QMessageManager::NotYetImplemented;
       
   235         return 0;
       
   236     }
       
   237     
       
   238     d_ptr->_error = QMessageManager::NoError;
       
   239     return d_ptr->_store->countAccounts(convert(filter));
       
   240 }
       
   241 
       
   242 bool QMessageStore::removeMessage(const QMessageId& id, QMessageManager::RemovalOption option)
       
   243 {
       
   244     d_ptr->_error = QMessageManager::NoError;
       
   245     return d_ptr->_store->removeMessage(convert(id), convert(option));
       
   246 }
       
   247 
       
   248 bool QMessageStore::removeMessages(const QMessageFilter& filter, QMessageManager::RemovalOption option)
       
   249 {
       
   250     d_ptr->_error = QMessageManager::NoError;
       
   251     return d_ptr->_store->removeMessages(convert(filter), convert(option));
       
   252 }
       
   253 
       
   254 bool QMessageStore::addMessage(QMessage *m)
       
   255 {
       
   256     // Ensure that the size estimate is updated if necessary
       
   257     (void)m->size();
       
   258 
       
   259     QMailMessage *msg(convert(m));
       
   260 
       
   261     d_ptr->_error = QMessageManager::NoError;
       
   262     return d_ptr->_store->addMessage(msg);
       
   263 }
       
   264 
       
   265 bool QMessageStore::updateMessage(QMessage *m)
       
   266 {
       
   267     // Ensure that the size estimate is updated if necessary
       
   268     (void)m->size();
       
   269 
       
   270     QMailMessage msg(convert(*m));
       
   271 
       
   272     d_ptr->_error = QMessageManager::NoError;
       
   273     return d_ptr->_store->updateMessage(&msg);
       
   274 }
       
   275 
       
   276 QMessage QMessageStore::message(const QMessageId& id) const
       
   277 {
       
   278     d_ptr->_error = QMessageManager::NoError;
       
   279     return convert(d_ptr->_store->message(convert(id)));
       
   280 }
       
   281 
       
   282 QMessageFolder QMessageStore::folder(const QMessageFolderId& id) const
       
   283 {
       
   284     d_ptr->_error = QMessageManager::NoError;
       
   285     return convert(d_ptr->_store->folder(convert(id)));
       
   286 }
       
   287 
       
   288 QMessageAccount QMessageStore::account(const QMessageAccountId& id) const
       
   289 {
       
   290     d_ptr->_error = QMessageManager::NoError;
       
   291     return convert(d_ptr->_store->account(convert(id)));
       
   292 }
       
   293 
       
   294 QMessageManager::NotificationFilterId QMessageStore::registerNotificationFilter(const QMessageFilter &filter)
       
   295 {
       
   296     QMessageManager::NotificationFilterId filterId = ++d_ptr->_filterId;
       
   297     d_ptr->_filters.insert(filterId, QmfHelpers::convert(filter));
       
   298     return filterId;
       
   299 }
       
   300 
       
   301 void QMessageStore::unregisterNotificationFilter(QMessageManager::NotificationFilterId notificationFilterId)
       
   302 {
       
   303     d_ptr->_filters.remove(notificationFilterId);
       
   304 }
       
   305 
       
   306 QMessageStore* QMessageStore::instance()
       
   307 {
       
   308     static bool initialised(false);
       
   309 
       
   310     QMessageStore* store(QMessageStorePrivate::storeInstance());
       
   311 
       
   312     if (!initialised) {
       
   313         initialised = true;
       
   314 
       
   315         QMailStore *mailStore(convert(store));
       
   316 
       
   317         // Connect the store's signals
       
   318         QMessageStorePrivate *storeImpl(store->d_ptr);
       
   319         connect(mailStore, SIGNAL(messagesAdded(QMailMessageIdList)), storeImpl, SLOT(messagesAdded(QMailMessageIdList)));
       
   320         connect(mailStore, SIGNAL(messagesRemoved(QMailMessageIdList)), storeImpl, SLOT(messagesRemoved(QMailMessageIdList)));
       
   321         connect(mailStore, SIGNAL(messagesUpdated(QMailMessageIdList)), storeImpl, SLOT(messagesUpdated(QMailMessageIdList)));
       
   322 
       
   323         // Perform any initialisation tasks
       
   324         QMessageStorePrivate::registerMessageStatus(mailStore, "QMessage::HighPriority");
       
   325         QMessageStorePrivate::registerMessageStatus(mailStore, "QMessage::LowPriority");
       
   326 
       
   327         // Create the standard folders if they do not exist
       
   328         typedef QPair<const char*, quint64> FolderAttributes;
       
   329         foreach (const FolderAttributes &folder, QList<FolderAttributes>() << FolderAttributes("Inbox", QMailFolder::Incoming)
       
   330                                                                            << FolderAttributes("Outbox", QMailFolder::Outgoing)
       
   331                                                                            << FolderAttributes("Drafts", QMailFolder::Outgoing | QMailFolder::Trash)
       
   332                                                                            << FolderAttributes("Sent", QMailFolder::Outgoing | QMailFolder::Sent)
       
   333                                                                            << FolderAttributes("Trash", QMailFolder::Trash)) {
       
   334             QMessageStorePrivate::createNonexistentFolder(mailStore, folder.first, folder.second);
       
   335         }
       
   336     }
       
   337 
       
   338     return store;
       
   339 }
       
   340 
       
   341 #include "moc_qmessagestore_qmf_p.cpp"
       
   342     
       
   343 QTM_END_NAMESPACE