emailservices/nmclientapi/src/nmapiengine.cpp
changeset 18 578830873419
child 23 2dc6caa42ec3
equal deleted inserted replaced
4:e7aa27f58ae1 18:578830873419
       
     1 /*
       
     2  * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3  * All rights reserved.
       
     4  * This component and the accompanying materials are made available
       
     5  * under the terms of "Eclipse Public License v1.0"
       
     6  * which accompanies this distribution, and is available
       
     7  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8  *
       
     9  * Initial Contributors:
       
    10  * Nokia Corporation - initial contribution.
       
    11  *
       
    12  * Contributors:
       
    13  *
       
    14  * Description:
       
    15  *
       
    16  */
       
    17 
       
    18 
       
    19 #include "nmapitypesconverter.h"
       
    20 #include "nmapidataplugininterface.h"
       
    21 #include "nmapidatapluginfactory.h"
       
    22 #include "nmapiengine.h"
       
    23 
       
    24 
       
    25 NmEngine *NmEngine::mInstance = NULL;
       
    26 quint32 NmEngine::mReferenceCount = 0;
       
    27 
       
    28 /*!
       
    29  \class NmEngine
       
    30  \brief The NmEngine class provides access to data for
       
    31  mailboxes, folders and messages. If the dataplugin can't be loaded, fatal assert is called.
       
    32 
       
    33  */
       
    34 
       
    35 /*!
       
    36  Private constructor
       
    37  */
       
    38 NmEngine::NmEngine() :
       
    39     mFactory(NULL)
       
    40 {
       
    41     mFactory = NmDataPluginFactory::instance();
       
    42     Q_ASSERT(mFactory->pluginInstances()->count() > 0);
       
    43 }
       
    44 
       
    45 /*!
       
    46  Destructor
       
    47  */
       
    48 NmEngine::~NmEngine()
       
    49 {
       
    50     NmDataPluginFactory::releaseInstance(mFactory);
       
    51 }
       
    52 
       
    53 /*!
       
    54  * Creates instance of NmEngine and prvide pointer to it.
       
    55  * 
       
    56  * If engine exist, it increase references count and provide pointer to it.
       
    57  * 
       
    58  * \return Instance of engine
       
    59  */
       
    60 NmEngine* NmEngine::instance()
       
    61 {
       
    62     if (!mInstance) {
       
    63         mInstance = new NmEngine();
       
    64     }
       
    65     mReferenceCount++;
       
    66     return mInstance;
       
    67 }
       
    68 
       
    69 /*!
       
    70  * Creates connections for events from email store.
       
    71  */
       
    72 void NmEngine::startCollectingEvents()
       
    73 {
       
    74     QList<QObject*>* pluginList = mFactory->pluginInstances();
       
    75     for (int i = 0; i < pluginList->count(); i++) {
       
    76         QObject* plugin = pluginList->at(i);
       
    77 
       
    78         QObject::connect(plugin, SIGNAL(messageEvent(NmMessageEvent, NmId, QList<NmId> , NmId)),
       
    79             this, SLOT(messageChangedArrived(NmMessageEvent, NmId, QList<NmId> , NmId)),
       
    80             Qt::UniqueConnection);
       
    81 
       
    82         connect(plugin, SIGNAL(mailboxEvent(NmMailboxEvent, QList<NmId> )), this, SLOT(
       
    83             mailboxChangedArrived(NmMailboxEvent, QList<NmId> )), Qt::UniqueConnection);
       
    84 
       
    85     }
       
    86 
       
    87 }
       
    88 
       
    89 /*!
       
    90  * It process mailbox ids from email store for given event.
       
    91  * On end it emit \sa emailStoreEvent
       
    92  * 
       
    93  * \arg Event of mailbox change
       
    94  * \arg List of mailbox ids for that event 
       
    95  */
       
    96 void NmEngine::mailboxChangedArrived(NmMailboxEvent mailboxEvent, const QList<NmId> &mailboxIds)
       
    97 {
       
    98     NmApiMessage message;
       
    99     message.objectType = EMailbox;
       
   100     switch (mailboxEvent) {
       
   101         case NmMailboxCreated: {
       
   102             message.action = ENew;
       
   103         }
       
   104             break;
       
   105         case NmMailboxDeleted: {
       
   106             message.action = EDeleted;
       
   107         }
       
   108             break;
       
   109         case NmMailboxChanged: {
       
   110             message.action = EChange;
       
   111         }
       
   112             break;
       
   113         default:
       
   114             break;
       
   115     }
       
   116 
       
   117     message.folderId = 0;
       
   118     message.mailboxId = 0;
       
   119 
       
   120     for (int i = 0; i < mailboxIds.count(); i++) {
       
   121         message.objectIds.append(mailboxIds.at(i).id());
       
   122     }
       
   123 
       
   124     emit emailStoreEvent(message);
       
   125 }
       
   126 
       
   127 /*!
       
   128  * It process message ids from email store for given event.
       
   129  * On end it emit \sa emailStoreEvent
       
   130  * 
       
   131  * \arg Event of message change
       
   132  * \arg List of message ids for that event 
       
   133  */
       
   134 void NmEngine::messageChangedArrived(
       
   135     NmMessageEvent messageEvent,
       
   136     const NmId &folderId,
       
   137     const QList<NmId> &messageIds,
       
   138     const NmId &mailboxId)
       
   139 {
       
   140     NmApiMessage message;
       
   141     message.objectType = EMessage;
       
   142     switch (messageEvent) {
       
   143         case NmMessageCreated: {
       
   144             message.action = ENew;
       
   145         }
       
   146             break;
       
   147         case NmMessageDeleted: {
       
   148             message.action = EDeleted;
       
   149         }
       
   150             break;
       
   151         case NmMessageChanged: {
       
   152             message.action = EChange;
       
   153         }
       
   154             break;
       
   155         default:
       
   156             break;
       
   157     }
       
   158 
       
   159     message.folderId = folderId.id();
       
   160     message.mailboxId = mailboxId.id();
       
   161 
       
   162     for (int i = 0; i < messageIds.count(); i++) {
       
   163         message.objectIds.append(messageIds.at(i).id());
       
   164     }
       
   165 
       
   166     emit emailStoreEvent(message);
       
   167 }
       
   168 
       
   169 /*!
       
   170  * Release instance of engine if there are not referenced objects. 
       
   171  * If there are refenced objects it only decrease refenced count.
       
   172  * 
       
   173  * \arg Instance of engine
       
   174  */
       
   175 void NmEngine::releaseInstance(NmEngine *&instance)
       
   176 {
       
   177     //can't have passed out instances if we don't have any
       
   178     if (mInstance) {
       
   179         if (instance == mInstance) {
       
   180             instance = NULL;
       
   181             mReferenceCount--;
       
   182         }
       
   183         if (0 >= mReferenceCount) {
       
   184             delete mInstance;
       
   185             mInstance = NULL;
       
   186         }
       
   187     }
       
   188 }
       
   189 
       
   190 /*!
       
   191  *    It get all mailboxes from email store
       
   192  *    
       
   193  *    \sa EmailClientApi::NmMailbox
       
   194  *    \arg List of mailboxes to be filled.
       
   195  */
       
   196 void NmEngine::listMailboxes(QList<EmailClientApi::NmMailbox> &mailboxList)
       
   197 {
       
   198     QList<NmMailbox*> mailboxFromPlugin;
       
   199     int count = mFactory->pluginInstances()->count();
       
   200     for (int i = 0; i < count; i++) {
       
   201         NmDataPluginInterface *instance = mFactory->interfaceInstance(
       
   202             mFactory->pluginInstances()->at(i));
       
   203         if (instance) {
       
   204             instance->listMailboxes(mailboxFromPlugin);
       
   205         }
       
   206     }
       
   207     while (mailboxFromPlugin.isEmpty() == false) {
       
   208         NmMailbox* tempNmMailbox = mailboxFromPlugin.takeFirst();
       
   209         mailboxList << NmToApiConverter::NmMailbox2ApiNmMailbox(*tempNmMailbox);
       
   210         delete tempNmMailbox;
       
   211     }
       
   212 }
       
   213 
       
   214 /*!
       
   215  *    It get all folders from email store for given mailbox
       
   216  *    
       
   217  *    \sa EmailClientApi::NmFolder
       
   218  *    \arg Mailbox id from where folders should be returned
       
   219  *    \arg List of folders to be filled.
       
   220  */
       
   221 void NmEngine::listFolders(const quint64 mailboxId, QList<EmailClientApi::NmFolder> &folderList)
       
   222 {
       
   223     QList<NmFolder*> folderFromPlugin;
       
   224     NmDataPluginInterface *instance = mFactory->interfaceInstance(mailboxId);
       
   225     if (instance) {
       
   226         instance->listFolders(mailboxId, folderFromPlugin);
       
   227     }
       
   228 
       
   229     while (folderFromPlugin.isEmpty() == false) {
       
   230         NmFolder* tempNmFolder = folderFromPlugin.takeFirst();
       
   231         folderList << NmToApiConverter::NmFolder2ApiNmFolder(*tempNmFolder);
       
   232         delete tempNmFolder;
       
   233     }
       
   234 }
       
   235 
       
   236 /*!
       
   237  *    It get all envelopes from email store for given mailbox and folder
       
   238  *    
       
   239  *    \sa EmailClientApi::NmMessageEnvelope
       
   240  *    \arg Mailbox id from where envelope should be returned
       
   241  *    \arg Folder id from where envelope should be returned
       
   242  *    \arg List of envelopes to be filled.
       
   243  */
       
   244 void NmEngine::listEnvelopes(const quint64 mailboxId, const quint64 folderId, QList<
       
   245     EmailClientApi::NmMessageEnvelope> &messageEnvelopeList)
       
   246 {
       
   247     QList<NmMessageEnvelope*> envelopes;
       
   248     NmDataPluginInterface *instance = mFactory->interfaceInstance(folderId);
       
   249     if (instance) {
       
   250         instance->listMessages(mailboxId, folderId, envelopes);
       
   251     }
       
   252 
       
   253     while (!envelopes.isEmpty()) {
       
   254         NmMessageEnvelope* env = envelopes.takeFirst();
       
   255 
       
   256         EmailClientApi::NmMessageEnvelope nmEnvelope =
       
   257             NmToApiConverter::NmMessageEnvelope2ApiEnvelope(*env);
       
   258         NmMessage* message = new NmMessage();
       
   259         instance->getMessageById(mailboxId, folderId, nmEnvelope.id(), message);
       
   260 
       
   261         QString plainText = QString();
       
   262         if (message->plainTextBodyPart()) {
       
   263             plainText = message->plainTextBodyPart()->textContent();
       
   264         }
       
   265 
       
   266         nmEnvelope.setPlainText(plainText);
       
   267         nmEnvelope.setFetchedSize(message->fetchedSize());
       
   268         nmEnvelope.setTotalSize(message->size());
       
   269 
       
   270         messageEnvelopeList << nmEnvelope;
       
   271         delete env;
       
   272     }
       
   273 }
       
   274 
       
   275 /*!
       
   276  * Return envelope given by mailbox, folder and envelope id.
       
   277  * 
       
   278  * \arg Mailbox id from where envlope should come
       
   279  * \arg Folder id from where envlope should come
       
   280  * \arg Id of envelope which should be returned
       
   281  * \arg Envelope to fill.
       
   282  * 
       
   283  * \return Return true if it will find any envelope
       
   284  */
       
   285 bool NmEngine::envelopeById(
       
   286     const quint64 mailboxId,
       
   287     const quint64 folderId,
       
   288     const quint64 envelopeId,
       
   289     EmailClientApi::NmMessageEnvelope& envelope)
       
   290 {
       
   291     QList<EmailClientApi::NmMessageEnvelope> envelopes;
       
   292     listEnvelopes(mailboxId, folderId, envelopes);
       
   293 
       
   294     //flag indicating that envelope with given id was found
       
   295     bool found = false;
       
   296 
       
   297     for (int i = 0; i < envelopes.count(); i++) {
       
   298         if (envelopes.at(i).id() == envelopeId) {
       
   299             envelope = envelopes.at(i);
       
   300             found = true;
       
   301             break;
       
   302         }
       
   303     }
       
   304 
       
   305     return found ? true : false;
       
   306 }
       
   307 
       
   308 /*!
       
   309  * Return mailbox given by mailbox id.
       
   310  * 
       
   311  * \arg Id of Mailbox which should be returned
       
   312  * \arg Mailbox to fill.
       
   313  * 
       
   314  * \return Return true if it will find any envelope
       
   315  */
       
   316 bool NmEngine::mailboxById(const quint64 mailboxId, EmailClientApi::NmMailbox &mailbox)
       
   317 {
       
   318     NmDataPluginInterface *instance = mFactory->interfaceInstance(mailboxId);
       
   319 
       
   320     if (!instance) {
       
   321         return false;
       
   322     }
       
   323 
       
   324     NmMailbox *nmmailbox;
       
   325     instance->getMailboxById(NmId(mailboxId), nmmailbox);
       
   326 
       
   327     if (!nmmailbox) {
       
   328         return false;
       
   329     }
       
   330 
       
   331     mailbox = NmToApiConverter::NmMailbox2ApiNmMailbox(*nmmailbox);
       
   332 
       
   333     return true;
       
   334 
       
   335 }
       
   336