harvesterplugins/messaging/email/qtemailfetcher/qtemailfetcher.cpp
changeset 12 993ab30e92fc
parent 11 773be20e0a25
child 20 68cdadcf169e
equal deleted inserted replaced
11:773be20e0a25 12:993ab30e92fc
    15 *
    15 *
    16 */
    16 */
    17 
    17 
    18 #include "qtemailfetcher.h"
    18 #include "qtemailfetcher.h"
    19 #include <csearchdocument.h>
    19 #include <csearchdocument.h>
    20 #include <cpixmaindefs.h>
       
    21 #include <nmapiemailaddress.h>
    20 #include <nmapiemailaddress.h>
    22 #include <nmapimessagebody.h>
    21 #include <nmapimessagebody.h>
    23 #include <nmapimailbox.h>
    22 #include <nmapimailbox.h>
    24 #include <QTimer>
    23 #include <QTimer>
    25 #include <QDebug>
    24 #include <QDebug>
    26 //#include <QThread> //If we happen to use QThread::yieldCurrentThread()
    25 #include <qdatetime.h>
    27 
    26 
    28 //Symbian specific details; picked up from cemailplugin.cpp. 
    27 //Symbian specific details; picked up from cemailplugin.cpp. 
    29 //Refactor it to cpixmaindefs.h
    28 //Refactor it to cpixmaindefs.h
    30 _LIT(KMsgBaseAppClassGeneric, "root msg email");
    29 _LIT(KMsgBaseAppClassGeneric, "root msg email");
    31 
    30 
    32 _LIT(KMsgSubject, "Subject");
    31 _LIT(KMsgSubject, "Subject");
       
    32 _LIT(KMsgSender, "Sender");
    33 _LIT(KMsgRecipients, "Recipients");
    33 _LIT(KMsgRecipients, "Recipients");
    34 _LIT(KMsgBody, "Body");
    34 _LIT(KMsgBody, "Body");
    35 _LIT(KMailBoxId, "MailBoxId");
    35 _LIT(KMailBoxId, "MailBoxId");
    36 _LIT(KFolderId, "FolderId");
    36 _LIT(KFolderId, "FolderId");
    37 _LIT(KHasAttachment, "HasAttachment");
    37 _LIT(KHasAttachment, "HasAttachment");
    38 _LIT(KMailBoxName, "MailBoxName");
    38 _LIT(KMailBoxName, "MailBoxName");
    39 _LIT(KFolderName, "FolderName");
    39 //_LIT(KFolderName, "FolderName");
    40 _LIT(KMimeTypeField, CPIX_MIMETYPE_FIELD);
    40 _LIT(KMimeTypeField, CPIX_MIMETYPE_FIELD);
    41 _LIT(KMimeTypeMsg, "Messages");
    41 _LIT(KMimeTypeMsg, "Messages");
    42 
    42 _LIT(KAttachment, "Attachment");
    43 
    43 _LIT(KSentTime, "SentTime");
    44 
    44 
    45 //------------------------------------------------------------------------------
    45 //------------------------------------------------------------------------------
    46 QEmailFetcher::QEmailFetcher( MEmailItemObserver& aObserver )
    46 QEmailFetcher::QEmailFetcher( MEmailItemObserver& aObserver )
    47     :iEmailObserver( aObserver ), 
    47     :iEmailObserver( aObserver ), 
    48      iEmailEventNotifier( NULL ),
    48      iEmailEventNotifier( NULL ),
    57     }
    57     }
    58 
    58 
    59 //------------------------------------------------------------------------------
    59 //------------------------------------------------------------------------------
    60 QEmailFetcher::~QEmailFetcher()
    60 QEmailFetcher::~QEmailFetcher()
    61     {
    61     {
    62     iEmailEventNotifier->cancel();
    62     if ( iEmailEventNotifier )
       
    63        iEmailEventNotifier->cancel();
       
    64     iMailBoxes.clear();
       
    65     iFolders.clear();
    63     delete iEmailEventNotifier;
    66     delete iEmailEventNotifier;
    64     delete iEmailService;
    67     delete iEmailService;
    65     delete iMailBoxListings;
    68     delete iEnvelopeListing;
    66     delete iMailFolderList;
    69     delete iMailFolderList;
       
    70     delete iMailBoxListings;    
    67     }
    71     }
    68 
    72 
    69 //------------------------------------------------------------------------------
    73 //------------------------------------------------------------------------------
    70 QEmailFetcher* QEmailFetcher::newInstance( MEmailItemObserver& aObserver ){
    74 QEmailFetcher* QEmailFetcher::newInstance( MEmailItemObserver& aObserver ){
    71     QEmailFetcher* emailFetcher = NULL;
    75     QEmailFetcher* emailFetcher = NULL;
    74     try{
    78     try{
    75         emailFetcher = new QEmailFetcher( aObserver );
    79         emailFetcher = new QEmailFetcher( aObserver );
    76         emailFetcher->iEmailService = new NmApiEmailService( emailFetcher );
    80         emailFetcher->iEmailService = new NmApiEmailService( emailFetcher );
    77         emailFetcher->iEmailEventNotifier =  new NmApiEventNotifier( emailFetcher );
    81         emailFetcher->iEmailEventNotifier =  new NmApiEventNotifier( emailFetcher );
    78         emailFetcher->iMailBoxListings = new NmApiMailboxListing( emailFetcher );
    82         emailFetcher->iMailBoxListings = new NmApiMailboxListing( emailFetcher );
    79         initialize( emailFetcher ); //Do the rest of the init.
    83         emailFetcher->initialize( ); //Do the rest of the init.
    80     }catch(...){ //cleanup.
    84     }catch(...){ //cleanup.
    81     qDebug() << "QEmailFetcher::newInstance ( Catch Block)";
    85     qDebug() << "QEmailFetcher::newInstance ( Catch Block)";
    82         delete emailFetcher; 
    86         delete emailFetcher; 
    83         delete emailFetcher->iEmailService;
    87         delete emailFetcher->iEmailService;
    84         delete emailFetcher->iEmailEventNotifier;
    88         delete emailFetcher->iEmailEventNotifier;
    91     qDebug() << "QEmailFetcher::newInstance :END";
    95     qDebug() << "QEmailFetcher::newInstance :END";
    92     return emailFetcher; //returns only if not null.
    96     return emailFetcher; //returns only if not null.
    93 }
    97 }
    94 
    98 
    95 //------------------------------------------------------------------------------
    99 //------------------------------------------------------------------------------
    96 void QEmailFetcher::initialize( QEmailFetcher* aThis ){
   100 void QEmailFetcher::initialize( ){    
    97     //The use of 'aThis' is because the current function is static.
       
    98     qDebug() << "QEmailFetcher::initialize :START";
   101     qDebug() << "QEmailFetcher::initialize :START";
    99     connect( aThis->iEmailService, SIGNAL(initialized(bool)), 
   102     connect( iEmailService, SIGNAL(initialized(bool)), 
   100              aThis, SLOT(emailServiceIntialized(bool)) );
   103              this, SLOT(emailServiceIntialized(bool)) );
   101     aThis->iEmailService->initialise();
   104     iEmailService->initialise();
   102     //Monitor for Message changes
   105     //Monitor for Message changes
   103     aThis->connect( aThis->iEmailEventNotifier, 
   106     connect( iEmailEventNotifier, 
   104                     SIGNAL(messageEvent(MessageEvent, quint64, quint64, QList<quint64>)),
   107              SIGNAL(messageEvent(EmailClientApi::NmApiMessageEvent, quint64, quint64, QList<quint64>)),
   105                     aThis, 
   108              this, 
   106                     SLOT(handleMessageEvent(MessageEvent, quint64, quint64, QList<quint64>)) );
   109              SLOT(handleMessageEvent(EmailClientApi::NmApiMessageEvent, quint64, quint64, QList<quint64>)) );
   107     //Monitor for Mailbox changes
       
   108     aThis->connect( aThis->iEmailEventNotifier, 
       
   109                     SIGNAL(mailboxEvent(MailboxEvent, QList<quint64>)),
       
   110                     aThis, 
       
   111                     SLOT(handlemailboxEvent(MailboxEvent, QList<quint64>)));
       
   112     //Start the monitoring
   110     //Start the monitoring
   113     aThis->iEmailEventNotifier->start();
   111     iEmailEventNotifier->start();
       
   112     qDebug() << "QEmailFetcher::Started monitoring for Email message event";
   114     qDebug() << "QEmailFetcher::initialize :END";
   113     qDebug() << "QEmailFetcher::initialize :END";
   115 }
   114 }
   116 
   115 
   117 //------------------------------------------------------------------------------
   116 //------------------------------------------------------------------------------
   118 void QEmailFetcher::emailServiceIntialized(bool aAllOk){
   117 void QEmailFetcher::emailServiceIntialized(bool aAllOk){
   142 {
   141 {
   143     return TPtrC16(static_cast<const TUint16*>(string.utf16()), string.length());
   142     return TPtrC16(static_cast<const TUint16*>(string.utf16()), string.length());
   144 }
   143 }
   145 
   144 
   146 //------------------------------------------------------------------------------
   145 //------------------------------------------------------------------------------
   147 // TODO Remove this code if qt_QString2TPtrC works.
   146 CSearchDocument* getPartialSearchDocument( quint64 aEnvelopeId ) {
   148 // TODO If this function is used, remember to release memory.
       
   149 // Ownership with caller.
       
   150 //HBufC* qt_QString2HBufC(const QString& aString)
       
   151 //{
       
   152 //    HBufC *buffer;
       
   153 //#ifdef QT_NO_UNICODE
       
   154 //    TPtrC8 ptr(reinterpret_cast<const TUint8*>(aString.toLocal8Bit().constData()));
       
   155 //#else
       
   156 //    TPtrC16 ptr(qt_QString2TPtrC(aString));
       
   157 //#endif
       
   158 //    buffer = q_check_ptr(HBufC::New(ptr.Length()));
       
   159 //    buffer->Des().Copy(ptr);
       
   160 //    return buffer;
       
   161 //}
       
   162 
       
   163 CSearchDocument* getPartialSearchDocument( const NmApiMessageEnvelope& aEnvelope ) {
       
   164     qDebug() << "getPartialSearchDocument :START";
   147     qDebug() << "getPartialSearchDocument :START";
   165     CSearchDocument* doc = 0;
   148     CSearchDocument* doc = 0;
   166     QT_TRAP_THROWING(
   149     QT_TRAP_THROWING(
   167         //Use qt_Qstring2TPtrC since we are working with <b>const</b> EmailMessageEnvelope.
   150         //Use qt_Qstring2TPtrC since we are working with <b>const</b> EmailMessageEnvelope.
   168         doc = CSearchDocument::NewL( qt_QString2TPtrC( QString().setNum( aEnvelope.id() ) ), 
   151         doc = CSearchDocument::NewL( qt_QString2TPtrC( QString().setNum( aEnvelopeId ) ), 
   169                                      KMsgBaseAppClassGeneric );        
   152                                      KMsgBaseAppClassGeneric );        
   170         );
   153         );
   171     qDebug() << "getPartialSearchDocument :END";
   154     qDebug() << "getPartialSearchDocument :END";
   172     return doc;
   155     return doc;
   173     }
   156     }
   174 } //anonymous namespace
   157 } //anonymous namespace
   175 //------------------------------------------------------------------------------
   158 //------------------------------------------------------------------------------
   176 
   159 
   177 CSearchDocument* QEmailFetcher::getSearchDocument( const NmApiMessageEnvelope& aEnvelope ,quint64 aMailboxId, quint64 aFolderId ){
   160 CSearchDocument* QEmailFetcher::getSearchDocumentL( const NmApiMessageEnvelope& aEnvelope ,quint64 aMailboxId, quint64 aFolderId ){
   178     QList<NmApiEmailAddress> toList;
   161     QList<NmApiEmailAddress> toList;
   179     qDebug() << "QEmailFetcher::getSearchDocument :START";
   162     qDebug() << "QEmailFetcher::getSearchDocumentL :START";
       
   163     //We need ALL the recipients in a SINGLE field.
       
   164     
   180     //Need to cast away const-ness since the get method is unfortunately not const.
   165     //Need to cast away const-ness since the get method is unfortunately not const.
   181     const_cast<NmApiMessageEnvelope&>(aEnvelope).getToRecipients( toList );
   166     const_cast<NmApiMessageEnvelope&>(aEnvelope).getToRecipients( toList );
   182 
       
   183     //We need ALL the recipients in a SINGLE field.
       
   184     QString recipients = "";
   167     QString recipients = "";
   185     for( int i=0; i<toList.length(); i++ )
   168     for( int i=0; i<toList.length(); i++ )
   186         recipients += toList.at( i ).displayName() + " "; //or should we get address?
   169         {
   187 
   170         qDebug() << "QEmailFetcher::To receipient displayname :"<< toList.at( i ).displayName() << "Address : "<<toList.at( i ).address();
       
   171         recipients += toList.at( i ).displayName() + " ";
       
   172         recipients += toList.at( i ).address() + " ";
       
   173         }
       
   174     // Get CC receipents
       
   175     const_cast<NmApiMessageEnvelope&>(aEnvelope).getCcRecipients( toList );
       
   176     for( int i=0; i<toList.length(); i++ )
       
   177         {
       
   178         qDebug() << "QEmailFetcher::CC receipient displayname :"<< toList.at( i ).displayName() << "Address : "<<toList.at( i ).address();
       
   179         recipients += toList.at( i ).displayName() + " ";
       
   180         recipients += toList.at( i ).address() + " ";
       
   181         }
       
   182     
   188     NmApiMessageBody body;
   183     NmApiMessageBody body;
   189     //Cast away const-ness since the get method is unfortunately not const.
   184     //Cast away const-ness since the get method is unfortunately not const.
   190     //Returns void. Cannot check for success/failure.
   185     //Returns void. Cannot check for success/failure.
   191     const_cast<NmApiMessageEnvelope&>(aEnvelope).getPlainTextBody( body ); 
   186     const_cast<NmApiMessageEnvelope&>(aEnvelope).getPlainTextBody( body ); 
   192     QString msgBody = body.content();
   187     QString msgBody = body.content();
   193 
   188     qDebug() << "QEmailFetcher::Body of mail using paintextAPI:"<< aEnvelope.plainText() ;
   194     CSearchDocument* doc = 0;
   189     CSearchDocument* doc = 0;
   195     QT_TRAP_THROWING(
   190     QT_TRAP_THROWING(
   196     //Use qt_Qstring2TPtrC since we are working with <b>const</b> EmailMessageEnvelope.
   191     //Use qt_Qstring2TPtrC since we are working with <b>const</b> EmailMessageEnvelope.
   197     doc = CSearchDocument::NewL( qt_QString2TPtrC( QString().setNum( aEnvelope.id() ) ), 
   192     doc = CSearchDocument::NewL( qt_QString2TPtrC( QString().setNum( aEnvelope.id() ) ), 
   198                                  KMsgBaseAppClassGeneric );
   193                                  KMsgBaseAppClassGeneric );
   199     doc->AddFieldL( KMimeTypeField, KMimeTypeMsg, CDocumentField::EStoreYes | CDocumentField::EIndexUnTokenized | CDocumentField::EAggregateNo);
   194     //Add the sender details
   200     doc->AddFieldL( KMsgSubject, qt_QString2TPtrC( aEnvelope.subject() ), CDocumentField::EStoreYes | CDocumentField::EIndexTokenized );
   195     doc->AddFieldL( KMsgSender, qt_QString2TPtrC( const_cast<NmApiMessageEnvelope&>(aEnvelope).sender() ), 
   201     doc->AddFieldL( KMsgRecipients, qt_QString2TPtrC( recipients ), CDocumentField::EStoreYes | CDocumentField::EIndexTokenized );
   196                     CDocumentField::EStoreYes | CDocumentField::EIndexTokenized | CDocumentField::EIndexFreeText);
   202     doc->AddFieldL( KMsgBody, qt_QString2TPtrC( msgBody ), CDocumentField::EStoreYes | CDocumentField::EIndexTokenized );
   197     //Add the Mimetype
   203     doc->AddFieldL( KMailBoxId, qt_QString2TPtrC( QString().setNum( aMailboxId ) ), CDocumentField::EStoreYes | CDocumentField::EIndexUnTokenized | CDocumentField::EAggregateNo );
   198     doc->AddFieldL( KMimeTypeField, KMimeTypeMsg, CDocumentField::EStoreYes | CDocumentField::EIndexUnTokenized );
   204     doc->AddFieldL( KFolderId, qt_QString2TPtrC( QString().setNum( aFolderId ) ), CDocumentField::EStoreYes | CDocumentField::EIndexUnTokenized | CDocumentField::EAggregateNo );
   199     //Add the Subject field
   205     doc->AddFieldL( KHasAttachment, qt_QString2TPtrC( QString().setNum( aEnvelope.hasAttachments() ) ), CDocumentField::EStoreYes | CDocumentField::EIndexUnTokenized | CDocumentField::EAggregateNo);
   200     doc->AddFieldL( KMsgSubject, qt_QString2TPtrC( aEnvelope.subject() ), 
   206 //    NmApiMailbox aMailBox;
   201                     CDocumentField::EStoreYes | CDocumentField::EIndexTokenized );
   207 //    iEmailService->getMailbox( aMailboxId, aMailBox );
   202     //Add the recipients list (Includes To and CC fields)
   208 //    doc->AddFieldL( KMailBoxName, qt_QString2TPtrC(aMailBox.name()) , CDocumentField::EStoreYes | CDocumentField::EIndexUnTokenized );
   203     doc->AddFieldL( KMsgRecipients, qt_QString2TPtrC( recipients ), 
   209     //TODO : Add folder name field
   204                     CDocumentField::EStoreYes | CDocumentField::EIndexTokenized | CDocumentField::EIndexFreeText );
   210     //_LIT(KFolderName, "FolderName");
   205     //Add the email body
       
   206     doc->AddFieldL( KMsgBody, qt_QString2TPtrC( msgBody ), 
       
   207                     CDocumentField::EStoreYes | CDocumentField::EIndexTokenized );
       
   208     //Add the mailboxid
       
   209     doc->AddFieldL( KMailBoxId, qt_QString2TPtrC( QString().setNum( aMailboxId ) ), 
       
   210                     CDocumentField::EStoreYes | CDocumentField::EIndexUnTokenized | CDocumentField::EAggregateNo );
       
   211     //Add the folder Id
       
   212     doc->AddFieldL( KFolderId, qt_QString2TPtrC( QString().setNum( aFolderId ) ), 
       
   213                     CDocumentField::EStoreYes | CDocumentField::EIndexUnTokenized | CDocumentField::EAggregateNo );
       
   214     //Add the attachment field. This field will be added only if there is any attachment.Details of the attachment
       
   215     //are not added due to lack of email application support.
       
   216     if ( aEnvelope.hasAttachments() )
       
   217        doc->AddFieldL( KHasAttachment, KAttachment, CDocumentField::EStoreYes | CDocumentField::EIndexTokenized);
       
   218     //Add mailbox name
       
   219     NmApiMailbox aMailBox;
       
   220     iEmailService->getMailbox( aMailboxId, aMailBox );
       
   221     doc->AddFieldL( KMailBoxName, qt_QString2TPtrC(aMailBox.name()) , 
       
   222                     CDocumentField::EStoreYes | CDocumentField::EIndexTokenized | CDocumentField::EIndexFreeText );
       
   223     
       
   224     // Sent date time  KSentTime
       
   225     QDateTime time = aEnvelope.sentTime();    
       
   226     doc->AddFieldL( KSentTime, qt_QString2TPtrC(time.toString ()) , 
       
   227                         CDocumentField::EStoreYes | CDocumentField::EIndexTokenized );
       
   228     //TODO : Foldername is not harvested as there is no API exposed from email application to get the foldername based on folderID.
       
   229     //This feature will be implemented as soon as we get supporting API's from Email team
       
   230     
   211     //Adding subject and body to the excerpt. 
   231     //Adding subject and body to the excerpt. 
   212     QString excerpt ;
   232     QString excerpt ;
   213     excerpt = aEnvelope.subject();
   233     excerpt = aEnvelope.subject();
   214     excerpt += msgBody ;
   234     excerpt += msgBody ;
   215     doc->AddExcerptL( qt_QString2TPtrC(excerpt) );
   235     doc->AddExcerptL( qt_QString2TPtrC(excerpt) );
   216     );
   236     );
   217     qDebug() << "QEmailFetcher::getSearchDocument :END";
   237     qDebug() << "QEmailFetcher::getSearchDocumentL :END";
   218     return doc;
   238     return doc;
   219 }
   239 }
   220 
   240 
   221 
   241 
   222 //------------------------------------------------------------------------------
   242 //------------------------------------------------------------------------------
   223 //Just to avoid duplication of the following two lines.
       
   224 void QEmailFetcher::NotifyHarvestingComplete(){
   243 void QEmailFetcher::NotifyHarvestingComplete(){
   225     qDebug() << "QEmailFetcher::NotifyHarvestingComplete :START";
   244     qDebug() << "QEmailFetcher::NotifyHarvestingComplete :START";
   226     iCurrentMailboxIndex = iCurrentFolderIndex = 0;
   245     iCurrentMailboxIndex = iCurrentFolderIndex = 0;
       
   246     //Free the iMailBoxes and iFolders
       
   247     iMailBoxes.clear();
       
   248     iFolders.clear();
   227     QT_TRAP_THROWING( iEmailObserver.HarvestingCompleted() );
   249     QT_TRAP_THROWING( iEmailObserver.HarvestingCompleted() );
   228     qDebug() << "QEmailFetcher::NotifyHarvestingComplete :END";
   250     qDebug() << "QEmailFetcher::NotifyHarvestingComplete :END";
   229     return;
   251     return;
   230 }
   252 }
   231 
   253 
   270     if( iCurrentMailboxIndex >= iMailBoxes.count() ) {
   292     if( iCurrentMailboxIndex >= iMailBoxes.count() ) {
   271         NotifyHarvestingComplete();
   293         NotifyHarvestingComplete();
   272         qDebug() << "QEmailFetcher::processNextMailbox :END (harvesting completed)";
   294         qDebug() << "QEmailFetcher::processNextMailbox :END (harvesting completed)";
   273         return;
   295         return;
   274     }
   296     }
   275     
       
   276     //More mailboxes available.
   297     //More mailboxes available.
   277     delete iMailFolderList; iMailFolderList = NULL;
   298     delete iMailFolderList; iMailFolderList = NULL;
   278     iMailFolderList = new NmApiFolderListing( this, iMailBoxes.at( iCurrentMailboxIndex++ ).id() );
   299     iMailFolderList = new NmApiFolderListing( this, iMailBoxes.at( iCurrentMailboxIndex++ ).id() );
   279     connect( iMailFolderList, SIGNAL(foldersListed( qint32 )), this, SLOT(handleMailFoldersListed( qint32)) );
   300     connect( iMailFolderList, SIGNAL(foldersListed( qint32 )), this, SLOT(handleMailFoldersListed( qint32)) );
   280     const int waitForSeconds = 30; //TODO Move this constant out of here if needed elsewhere
   301     iMailFolderList->start();
   281     QTimer::singleShot( waitForSeconds, iMailFolderList, SLOT( start()) );
   302    
       
   303 //    const int waitForSeconds = 30; //TODO Move this constant out of here if needed elsewhere
       
   304 //    QTimer::singleShot( waitForSeconds, iMailFolderList, SLOT( start()) );
   282     qDebug() << "QEmailFetcher::processNextMailbox :END (goto next mailbox)";
   305     qDebug() << "QEmailFetcher::processNextMailbox :END (goto next mailbox)";
   283 }
   306 }
   284 
   307 
   285 //------------------------------------------------------------------------------
   308 //------------------------------------------------------------------------------
   286 void QEmailFetcher::handleMailFoldersListed(int aCount){
   309 void QEmailFetcher::handleMailFoldersListed(int aCount){
   308     if( iCurrentFolderIndex >= iFolders.count() ) {
   331     if( iCurrentFolderIndex >= iFolders.count() ) {
   309         qDebug() << "QEmailFetcher::processNextFolder :processNextMailbox";
   332         qDebug() << "QEmailFetcher::processNextFolder :processNextMailbox";
   310         processNextMailbox();
   333         processNextMailbox();
   311         return;//Don't proceed futher.
   334         return;//Don't proceed futher.
   312     }
   335     }
   313     
       
   314     //More folders to process.
   336     //More folders to process.
   315     //Already set to NULL in constructor, so safe to call delete first time.
   337     //Already set to NULL in constructor, so safe to call delete first time.
   316     delete iEnvelopeListing; iEnvelopeListing = NULL; 
   338     delete iEnvelopeListing; iEnvelopeListing = NULL; 
   317     iEnvelopeListing= new NmApiEnvelopeListing( 
   339     iEnvelopeListing= new NmApiEnvelopeListing( 
   318             this, 
   340             this, 
   335     QList<NmApiMessageEnvelope> envelopes;
   357     QList<NmApiMessageEnvelope> envelopes;
   336     if ( aCount > 0 && iEnvelopeListing->getEnvelopes(envelopes) ) {
   358     if ( aCount > 0 && iEnvelopeListing->getEnvelopes(envelopes) ) {
   337         for( int i=0; i<envelopes.count(); i++ ) {
   359         for( int i=0; i<envelopes.count(); i++ ) {
   338             const NmApiMessageEnvelope &envelope = envelopes.at( i );
   360             const NmApiMessageEnvelope &envelope = envelopes.at( i );
   339             //Create document and call back observer.
   361             //Create document and call back observer.
   340             QT_TRAP_THROWING( iEmailObserver.HandleDocumentL( getSearchDocument( envelope, iFolders.at( iCurrentFolderIndex -1 ).id(), iMailBoxes.at( iCurrentMailboxIndex-1 ).id() ), 
   362             QT_TRAP_THROWING( iEmailObserver.HandleDocumentL( getSearchDocumentL( envelope, iFolders.at( iCurrentFolderIndex -1 ).id(), 
   341                                                               ECPixAddAction ) );
   363                                                                                  iMailBoxes.at( iCurrentMailboxIndex-1 ).id() ), ECPixAddAction ) );
   342         }
   364         }
   343     }
   365     }
   344     qDebug() << "QEmailFetcher::processMessages :END";
   366     qDebug() << "QEmailFetcher::processMessages :END";
   345 }
   367 }
   346 
   368 
   347 //------------------------------------------------------------------------------
   369 //------------------------------------------------------------------------------
   348 void QEmailFetcher::handleMessageEvent( NmApiMessageEvent aEvent, quint64 aMailboxId, quint64 aFolderId, QList<quint64> aMessageList){
   370 void QEmailFetcher::handleMessageEvent( EmailClientApi::NmApiMessageEvent aEvent, quint64 aMailboxId, quint64 aFolderId, QList<quint64> aMessageList){
   349     NmApiMessageEnvelope envelope;
   371     NmApiMessageEnvelope envelope;
   350     qDebug() << "QEmailFetcher::handleMessageEvent :START";
   372     qDebug() << "QEmailFetcher::handleMessageEvent :START";
   351     const int messageCount = aMessageList.count();
   373     const int messageCount = aMessageList.count();
   352     if( messageCount>0 ){
   374     if( messageCount>0 ){
   353     if( aEvent == MessageCreated || aEvent == MessageChanged ){
   375     if( aEvent == MessageCreated || aEvent == MessageChanged ){
   354         qDebug() << "QEmailFetcher::handleMessageEvent :MessageCreated || MessageChanged";
   376         qDebug() << "QEmailFetcher::handleMessageEvent :MessageCreated || MessageChanged";
   355         for( int i=0; i<messageCount; i++ ){
   377         for( int i=0; i<messageCount; i++ ){
   356             if( iEmailService->getEnvelope( aMailboxId, aFolderId, aMessageList.at( i ), envelope ) ){
   378             if( iEmailService->getEnvelope( aMailboxId, aFolderId, aMessageList.at( i ), envelope ) ){
   357                qDebug() << "QEmailFetcher::handleMessageEvent :HandleDocumentL";
   379                qDebug() << "QEmailFetcher::handleMessageEvent :HandleDocumentL";
   358                 QT_TRAP_THROWING( 
   380                 QT_TRAP_THROWING( 
   359                    iEmailObserver.HandleDocumentL( getSearchDocument( envelope, aMailboxId, aFolderId ), 
   381                    iEmailObserver.HandleDocumentL( getSearchDocumentL( envelope, aMailboxId, aFolderId ), 
   360                            //Doing this simply avoids *duplicate* code for update action.
   382                            //Doing this simply avoids *duplicate* code for update action.
   361                            aEvent == MessageCreated ? ECPixAddAction : ECPixUpdateAction ) );
   383                            aEvent == MessageCreated ? ECPixAddAction : ECPixUpdateAction ) );
   362             }
   384             }
   363         }
   385         }
   364     }
   386     }
   365     else if( aEvent == MessageDeleted ) {
   387     else if( aEvent == MessageDeleted ) {
   366         qDebug() << "QEmailFetcher::handleMessageEvent :MessageDeleted";
   388         qDebug() << "QEmailFetcher::handleMessageEvent :MessageDeleted";
   367         //TODO We can do better. For delete, we dont have to create full document. Just the ID should be enough.
       
   368         //We can have another function called getPartialSearchDocument so deletes will be faster.
       
   369         for( int i=0; i<messageCount; i++ ){
   389         for( int i=0; i<messageCount; i++ ){
   370             if( iEmailService->getEnvelope( aMailboxId, aFolderId, aMessageList.at( i ), envelope ) ){
       
   371                 qDebug() << "QEmailFetcher::handleMessageEvent :MessageDeleted : HandleDocumentL";
   390                 qDebug() << "QEmailFetcher::handleMessageEvent :MessageDeleted : HandleDocumentL";
   372                 QT_TRAP_THROWING( 
   391                 QT_TRAP_THROWING( 
   373                 iEmailObserver.HandleDocumentL( getPartialSearchDocument( envelope ), ECPixRemoveAction ) );
   392                 iEmailObserver.HandleDocumentL( getPartialSearchDocument( aMessageList.at( i ) ), ECPixRemoveAction ) );
   374             }
   393             }
   375         }
   394         }
   376     }
   395     }
   377     }
   396 }
   378 }
       
   379 
       
   380 //--------------------------------------------------------------------------------------
       
   381 void QEmailFetcher::handlemailboxEvent( EmailClientApi::NmApiMailboxEvent event, QList<quint64> idlist ){
       
   382     const int mailboxCount = idlist.count();    
       
   383     if( event == MailboxCreated )
       
   384         {
       
   385         //New mailbox is created.Harvest the Mailbox name and all the folder names
       
   386         }
       
   387     else
       
   388         {
       
   389         //MailBox is deleted so delete the document related to all the mailbox and folders in the mailbox
       
   390         }
       
   391 }
       
   392 
       
   393 CSearchDocument* QEmailFetcher::getMailboxorfolderSearchDocument( quint64 aMailboxId, quint64 aFolderId, TEmailDocType aDocType, QString aFoldername ){
       
   394     
       
   395     CSearchDocument* doc = 0;
       
   396     QT_TRAP_THROWING(
       
   397     //Use qt_Qstring2TPtrC since we are working with <b>const</b> EmailMessageEnvelope.
       
   398             if ( aDocType == EEmailTypeFolder)
       
   399                 {
       
   400                 doc = CSearchDocument::NewL( qt_QString2TPtrC( QString().setNum( aFolderId ) ), 
       
   401                                  KMsgBaseAppClassGeneric );
       
   402                 doc->AddFieldL( KFolderName, qt_QString2TPtrC(aFoldername) , CDocumentField::EStoreYes | CDocumentField::EIndexUnTokenized );
       
   403                 doc->AddFieldL( KFolderId, qt_QString2TPtrC( QString().setNum( aFolderId ) ), CDocumentField::EStoreYes | CDocumentField::EIndexUnTokenized );
       
   404                 }
       
   405             else if ( aDocType == EEmailTypeMailBox )
       
   406                 {
       
   407                 doc = CSearchDocument::NewL( qt_QString2TPtrC( QString().setNum( aMailboxId ) ), 
       
   408                                              KMsgBaseAppClassGeneric );
       
   409                 NmApiMailbox aMailBox;
       
   410                 iEmailService->getMailbox( aMailboxId, aMailBox );
       
   411                 doc->AddFieldL( KMailBoxName, qt_QString2TPtrC(aMailBox.name()) , CDocumentField::EStoreYes | CDocumentField::EIndexUnTokenized );
       
   412                 }
       
   413             doc->AddFieldL( KMimeTypeField, KMimeTypeMsg, CDocumentField::EStoreYes | CDocumentField::EIndexUnTokenized);
       
   414             doc->AddFieldL( KMailBoxId, qt_QString2TPtrC( QString().setNum( aMailboxId ) ), CDocumentField::EStoreYes | CDocumentField::EIndexUnTokenized );
       
   415 
       
   416     );
       
   417     qDebug() << "QEmailFetcher::getSearchDocument :END";
       
   418     return doc;
       
   419 }