emailservices/nmclientapi/src/nmapiengine.cpp
branchRCL_3
changeset 63 d189ee25cf9d
equal deleted inserted replaced
61:dcf0eedfc1a3 63:d189ee25cf9d
       
     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 #include "nmapiheaders.h"
       
    19 
       
    20 
       
    21 NmApiEngine *NmApiEngine::mInstance = NULL;
       
    22 quint32 NmApiEngine::mReferenceCount = 0;
       
    23 
       
    24 /*!
       
    25    \class NmApiEngine
       
    26    \brief The NmEngine class provides access to data for
       
    27    mailboxes, folders and messages.
       
    28  */
       
    29 
       
    30 /*!
       
    31    Private constructor
       
    32  */
       
    33 NmApiEngine::NmApiEngine() :
       
    34     mFactory(NULL)
       
    35 {
       
    36     NM_FUNCTION;
       
    37     
       
    38     mFactory = NmApiDataPluginFactory::instance();
       
    39 }
       
    40 
       
    41 /*!
       
    42   Destructor
       
    43  */
       
    44 NmApiEngine::~NmApiEngine()
       
    45 {
       
    46     NM_FUNCTION;
       
    47     
       
    48     NmApiDataPluginFactory::releaseInstance(mFactory);
       
    49 }
       
    50 
       
    51 /*!
       
    52    Creates instance of NmApiEngine and provide pointer to it.
       
    53    
       
    54    If engine exist, it increase references count and provide pointer to it.
       
    55    
       
    56    \return Instance of engine
       
    57  */
       
    58 NmApiEngine *NmApiEngine::instance()
       
    59 {
       
    60     NM_FUNCTION;
       
    61     
       
    62     if (!mInstance) {
       
    63         mInstance = new NmApiEngine();
       
    64     }
       
    65     mReferenceCount++;
       
    66     return mInstance;
       
    67 }
       
    68 
       
    69 /*!
       
    70    Creates connections for events from email store.
       
    71  */
       
    72 void NmApiEngine::startCollectingEvents()
       
    73 {
       
    74     NM_FUNCTION;
       
    75     
       
    76     QObject *plugin = mFactory->plugin();
       
    77     if(plugin){
       
    78         connect(plugin, SIGNAL(messageEvent(NmMessageEvent, NmId, QList<NmId> , NmId)), this,
       
    79             SLOT(messageChangedArrived(NmMessageEvent, NmId, QList<NmId> , NmId)), Qt::UniqueConnection);
       
    80 
       
    81         connect(plugin, SIGNAL(mailboxEvent(NmMailboxEvent, QList<NmId> )), this, SLOT(
       
    82             mailboxChangedArrived(NmMailboxEvent, QList<NmId> )), Qt::UniqueConnection);
       
    83     }
       
    84 }
       
    85 
       
    86 /*!
       
    87    It process mailbox ids from email store for given event.
       
    88    On end it emit \sa emailStoreEvent
       
    89    
       
    90    \arg Event of mailbox change
       
    91    \arg List of mailbox ids for that event 
       
    92  */
       
    93 void NmApiEngine::mailboxChangedArrived(NmMailboxEvent mailboxEvent, const QList<NmId> &mailboxIds)
       
    94 {
       
    95     NM_FUNCTION;
       
    96     
       
    97     NmApiMessage message;
       
    98     message.objectType = EMailbox;
       
    99     switch (mailboxEvent) {
       
   100         case NmMailboxCreated: {
       
   101             message.action = ENew;
       
   102             // subscribe all events also for these new mailboxes
       
   103             for(int i=0; i<mailboxIds.count(); i++) {
       
   104                 mFactory->interfaceInstance()->subscribeMailboxEvents(mailboxIds[i]);
       
   105             }
       
   106         }
       
   107             break;
       
   108         case NmMailboxDeleted: {
       
   109             message.action = EDeleted;
       
   110             // unsubscribe all events from deleted mailboxes
       
   111             for(int i=0; i<mailboxIds.count(); i++) {
       
   112                 mFactory->interfaceInstance()->unsubscribeMailboxEvents(mailboxIds[i]);
       
   113             }
       
   114         }
       
   115             break;
       
   116         case NmMailboxChanged: {
       
   117             message.action = EChange;
       
   118         }
       
   119             break;
       
   120         default:
       
   121             break;
       
   122     }
       
   123 
       
   124     message.folderId = 0;
       
   125     message.mailboxId = 0;
       
   126 
       
   127     for (int i = 0; i < mailboxIds.count(); i++) {
       
   128         message.objectIds.append(mailboxIds.at(i).id());
       
   129     }
       
   130 
       
   131     emit emailStoreEvent(message);
       
   132 }
       
   133 
       
   134 /*!
       
   135    It process message ids from email store for given event.
       
   136    On end it emit \sa emailStoreEvent
       
   137    
       
   138    \param messageEvent Event of message change
       
   139    \param folderId Id of folder from where are messages
       
   140    \param messageIds List of message ids for that event 
       
   141    \param mailboxId Id of mailbox from where are messages
       
   142  */
       
   143 void NmApiEngine::messageChangedArrived(
       
   144     NmMessageEvent messageEvent,
       
   145     const NmId &folderId,
       
   146     const QList<NmId> &messageIds,
       
   147     const NmId &mailboxId)
       
   148 {
       
   149     NM_FUNCTION;
       
   150     
       
   151     NmApiMessage message;
       
   152     message.objectType = EMessage;
       
   153     switch (messageEvent) {
       
   154         case NmMessageCreated: {
       
   155             message.action = ENew;
       
   156         }
       
   157             break;
       
   158         case NmMessageDeleted: {
       
   159             message.action = EDeleted;
       
   160         }
       
   161             break;
       
   162         case NmMessageChanged: {
       
   163             message.action = EChange;
       
   164         }
       
   165             break;
       
   166         default:
       
   167             break;
       
   168     }
       
   169 
       
   170     message.folderId = folderId.id();
       
   171     message.mailboxId = mailboxId.id();
       
   172 
       
   173     for (int i = 0; i < messageIds.count(); i++) {
       
   174         message.objectIds.append(messageIds.at(i).id());
       
   175     }
       
   176 
       
   177     emit emailStoreEvent(message);
       
   178 }
       
   179 
       
   180 /*!
       
   181    Release instance of engine if there are not referenced objects. 
       
   182    If there are refenced objects it only decrease refenced count.
       
   183    
       
   184    \param instance Instance of engine
       
   185  */
       
   186 void NmApiEngine::releaseInstance(NmApiEngine *&instance)
       
   187 {
       
   188     NM_FUNCTION;
       
   189     
       
   190     //can't have passed out instances if we don't have any
       
   191     if (mInstance) {
       
   192         if (instance == mInstance) {
       
   193             instance = NULL;
       
   194             mReferenceCount--;
       
   195         }
       
   196         if (0 >= mReferenceCount) {
       
   197             delete mInstance;
       
   198             mInstance = NULL;
       
   199         }
       
   200     }
       
   201 }
       
   202 
       
   203 /*!
       
   204       It get all mailboxes from email store
       
   205       
       
   206       \sa EmailClientApi::NmMailbox
       
   207       \param mailboxList List of mailboxes to be filled.
       
   208  */
       
   209 void NmApiEngine::listMailboxes(QList<EmailClientApi::NmApiMailbox> &mailboxList)
       
   210 {
       
   211     NM_FUNCTION;
       
   212     
       
   213     QList<NmMailbox*> mailboxFromPlugin;
       
   214 
       
   215     NmDataPluginInterface *instance =  mFactory->interfaceInstance();
       
   216     if (instance) {
       
   217         instance->listMailboxes(mailboxFromPlugin);
       
   218     }
       
   219 
       
   220     while (mailboxFromPlugin.isEmpty() == false) {
       
   221         NmMailbox* tempNmMailbox = mailboxFromPlugin.takeLast();
       
   222 
       
   223         // subscribe all events also for these new mailboxes
       
   224         instance->subscribeMailboxEvents(tempNmMailbox->id());
       
   225 
       
   226         // construct mailboxlist to platform api
       
   227         mailboxList << NmToApiConverter::NmMailbox2NmApiMailbox(*tempNmMailbox);
       
   228 
       
   229         delete tempNmMailbox;
       
   230     }
       
   231 }
       
   232 
       
   233 /*!
       
   234       It get all folders from email store for given mailbox
       
   235       
       
   236       \sa EmailClientApi::NmApiFolder
       
   237       \param mailboxId Mailbox id from where folders should be returned
       
   238       \param folderList  of folders to be filled.
       
   239  */
       
   240 void NmApiEngine::listFolders(const quint64 mailboxId, QList<EmailClientApi::NmApiFolder> &folderList)
       
   241 {
       
   242     NM_FUNCTION;
       
   243     
       
   244     QList<NmFolder*> folderFromPlugin;
       
   245     NmDataPluginInterface *instance = mFactory->interfaceInstance();
       
   246     if (instance) {
       
   247         instance->listFolders(mailboxId, folderFromPlugin);
       
   248     }
       
   249 
       
   250     while (folderFromPlugin.isEmpty() == false) {
       
   251         NmFolder* tempNmFolder = folderFromPlugin.takeLast();
       
   252         folderList << NmToApiConverter::NmFolder2NmApiFolder(*tempNmFolder);
       
   253         delete tempNmFolder;
       
   254     }
       
   255 }
       
   256 
       
   257 /*!
       
   258       It get all envelopes from email store for given mailbox and folder
       
   259       
       
   260       \sa EmailClientApi::NmApiMessageEnvelope
       
   261       \param mailboxId Mailbox id from where envelope should be returned
       
   262       \param folderId Folder id from where envelope should be returned
       
   263       \param messageEnvelopeList List of envelopes to be filled.
       
   264  */
       
   265 void NmApiEngine::listEnvelopes(const quint64 mailboxId, const quint64 folderId, 
       
   266                     QList<EmailClientApi::NmApiMessageEnvelope> &messageEnvelopeList)
       
   267 {
       
   268     NM_FUNCTION;
       
   269     
       
   270     QList<NmMessage*> messages;
       
   271     NmDataPluginInterface *instance = mFactory->interfaceInstance();
       
   272     if (instance) {
       
   273         instance->listMessages(mailboxId, folderId, messages);
       
   274     }
       
   275 
       
   276     while (!messages.isEmpty()) {
       
   277         NmMessage* message = messages.takeFirst();
       
   278             
       
   279         EmailClientApi::NmApiMessageEnvelope nmEnvelope =
       
   280             NmToApiConverter::NmMessageEnvelope2NmApiMessageEnvelope(message->envelope());
       
   281         
       
   282         NmMessagePart *plainTextPart = message->plainTextBodyPart();
       
   283         
       
   284         QString plainText = QString();
       
   285         if (plainTextPart) {
       
   286             plainText = plainTextPart->textContent();
       
   287         }
       
   288 
       
   289         nmEnvelope.setPlainText(plainText);
       
   290         nmEnvelope.setFetchedSize(message->fetchedSize());
       
   291         nmEnvelope.setTotalSize(message->size());
       
   292 
       
   293         messageEnvelopeList << nmEnvelope;
       
   294         delete message;
       
   295     }
       
   296 }
       
   297 
       
   298 /*!
       
   299    Return envelope given by mailbox, folder and envelope id.
       
   300    
       
   301    \param mailboxId Mailbox id from where envlope should come
       
   302    \param folderId Folder id from where envlope should come
       
   303    \param folderId Id of envelope which should be returned
       
   304    \param envelope Envelope to fill.
       
   305    
       
   306    \return Return true if it will find any envelope
       
   307  */
       
   308 bool NmApiEngine::getEnvelopeById(
       
   309     const quint64 mailboxId,
       
   310     const quint64 folderId,
       
   311     const quint64 envelopeId,
       
   312     EmailClientApi::NmApiMessageEnvelope &envelope)
       
   313 {
       
   314     NM_FUNCTION;
       
   315     
       
   316     //flag indicating that envelope with given id was found
       
   317     bool found = false;
       
   318     NmDataPluginInterface *instance = mFactory->interfaceInstance();
       
   319     if (instance) {
       
   320         NmMessage *message(NULL);
       
   321         instance->getMessageById(mailboxId, folderId, envelopeId, message);
       
   322         if(message){
       
   323             NmMessageEnvelope env = message->envelope();
       
   324 
       
   325             envelope = NmToApiConverter::NmMessageEnvelope2NmApiMessageEnvelope(env);
       
   326             QString plainText = QString();
       
   327             
       
   328             NmMessagePart *plainTextPart = message->plainTextBodyPart();
       
   329             if (plainTextPart) {
       
   330                 instance->contentToMessagePart(mailboxId, folderId, envelopeId, *plainTextPart);
       
   331                 plainText = plainTextPart->textContent();
       
   332             }
       
   333 
       
   334             envelope.setPlainText(plainText);
       
   335             envelope.setFetchedSize(message->fetchedSize());
       
   336             envelope.setTotalSize(message->size());
       
   337 
       
   338             found = true;
       
   339         }
       
   340         delete message;
       
   341     }
       
   342     return found;
       
   343 }
       
   344 
       
   345 /*!
       
   346    Return mailbox given by mailbox id.
       
   347    
       
   348    \param mailboxId Id of Mailbox which should be returned
       
   349    \param mailbox Mailbox to fill.
       
   350    
       
   351    \return Return true if it will find any envelope
       
   352  */
       
   353 bool NmApiEngine::getMailboxById(const quint64 mailboxId, EmailClientApi::NmApiMailbox &mailbox)
       
   354 {
       
   355     NM_FUNCTION;
       
   356     
       
   357     //flag indicating that mailbox with given id was found
       
   358     bool found = false;
       
   359     NmDataPluginInterface *instance = mFactory->interfaceInstance();
       
   360 
       
   361     if (instance) {    
       
   362         NmMailbox *nmmailbox = NULL;
       
   363         instance->getMailboxById(NmId(mailboxId), nmmailbox);
       
   364     
       
   365         if (nmmailbox) {
       
   366             mailbox = NmToApiConverter::NmMailbox2NmApiMailbox(*nmmailbox);
       
   367             found = true;
       
   368             delete nmmailbox;
       
   369         }
       
   370     }
       
   371     
       
   372     return found;
       
   373 }