diff -r 2f67eb14d003 -r 4a2987baf8f7 harvesterplugins/messaging/email/qtemailfetcher/qtemailfetcher.cpp --- a/harvesterplugins/messaging/email/qtemailfetcher/qtemailfetcher.cpp Wed Jun 23 17:22:18 2010 +0100 +++ b/harvesterplugins/messaging/email/qtemailfetcher/qtemailfetcher.cpp Mon Jun 28 11:03:15 2010 +0530 @@ -17,29 +17,30 @@ #include "qtemailfetcher.h" #include -#include #include #include #include #include #include -//#include //If we happen to use QThread::yieldCurrentThread() +#include //Symbian specific details; picked up from cemailplugin.cpp. //Refactor it to cpixmaindefs.h _LIT(KMsgBaseAppClassGeneric, "root msg email"); _LIT(KMsgSubject, "Subject"); +_LIT(KMsgSender, "Sender"); _LIT(KMsgRecipients, "Recipients"); _LIT(KMsgBody, "Body"); _LIT(KMailBoxId, "MailBoxId"); _LIT(KFolderId, "FolderId"); +_LIT(KHasAttachment, "HasAttachment"); _LIT(KMailBoxName, "MailBoxName"); -_LIT(KFolderName, "FolderName"); +//_LIT(KFolderName, "FolderName"); _LIT(KMimeTypeField, CPIX_MIMETYPE_FIELD); _LIT(KMimeTypeMsg, "Messages"); - - +_LIT(KAttachment, "Attachment"); +_LIT(KSentTime, "SentTime"); //------------------------------------------------------------------------------ QEmailFetcher::QEmailFetcher( MEmailItemObserver& aObserver ) @@ -58,11 +59,15 @@ //------------------------------------------------------------------------------ QEmailFetcher::~QEmailFetcher() { - iEmailEventNotifier->cancel(); + if ( iEmailEventNotifier ) + iEmailEventNotifier->cancel(); + iMailBoxes.clear(); + iFolders.clear(); delete iEmailEventNotifier; delete iEmailService; - delete iMailBoxListings; + delete iEnvelopeListing; delete iMailFolderList; + delete iMailBoxListings; } //------------------------------------------------------------------------------ @@ -75,7 +80,7 @@ emailFetcher->iEmailService = new NmApiEmailService( emailFetcher ); emailFetcher->iEmailEventNotifier = new NmApiEventNotifier( emailFetcher ); emailFetcher->iMailBoxListings = new NmApiMailboxListing( emailFetcher ); - initialize( emailFetcher ); //Do the rest of the init. + emailFetcher->initialize( ); //Do the rest of the init. }catch(...){ //cleanup. qDebug() << "QEmailFetcher::newInstance ( Catch Block)"; delete emailFetcher; @@ -92,24 +97,19 @@ } //------------------------------------------------------------------------------ -void QEmailFetcher::initialize( QEmailFetcher* aThis ){ - //The use of 'aThis' is because the current function is static. +void QEmailFetcher::initialize( ){ qDebug() << "QEmailFetcher::initialize :START"; - connect( aThis->iEmailService, SIGNAL(initialized(bool)), - aThis, SLOT(emailServiceIntialized(bool)) ); - aThis->iEmailService->initialise(); + connect( iEmailService, SIGNAL(initialized(bool)), + this, SLOT(emailServiceIntialized(bool)) ); + iEmailService->initialise(); //Monitor for Message changes - aThis->connect( aThis->iEmailEventNotifier, - SIGNAL(messageEvent(MessageEvent, quint64, quint64, QList)), - aThis, - SLOT(handleMessageEvent(MessageEvent, quint64, quint64, QList)) ); - //Monitor for Mailbox changes - aThis->connect( aThis->iEmailEventNotifier, - SIGNAL(mailboxEvent(MailboxEvent, QList)), - aThis, - SLOT(handlemailboxEvent(MailboxEvent, QList))); + connect( iEmailEventNotifier, + SIGNAL(messageEvent(EmailClientApi::NmApiMessageEvent, quint64, quint64, QList)), + this, + SLOT(handleMessageEvent(EmailClientApi::NmApiMessageEvent, quint64, quint64, QList)) ); //Start the monitoring - aThis->iEmailEventNotifier->start(); + iEmailEventNotifier->start(); + qDebug() << "QEmailFetcher::Started monitoring for Email message event"; qDebug() << "QEmailFetcher::initialize :END"; } @@ -143,28 +143,12 @@ } //------------------------------------------------------------------------------ -// TODO Remove this code if qt_QString2TPtrC works. -// TODO If this function is used, remember to release memory. -// Ownership with caller. -//HBufC* qt_QString2HBufC(const QString& aString) -//{ -// HBufC *buffer; -//#ifdef QT_NO_UNICODE -// TPtrC8 ptr(reinterpret_cast(aString.toLocal8Bit().constData())); -//#else -// TPtrC16 ptr(qt_QString2TPtrC(aString)); -//#endif -// buffer = q_check_ptr(HBufC::New(ptr.Length())); -// buffer->Des().Copy(ptr); -// return buffer; -//} - -CSearchDocument* getPartialSearchDocument( const NmApiMessageEnvelope& aEnvelope ) { +CSearchDocument* getPartialSearchDocument( quint64 aEnvelopeId ) { qDebug() << "getPartialSearchDocument :START"; CSearchDocument* doc = 0; QT_TRAP_THROWING( //Use qt_Qstring2TPtrC since we are working with const EmailMessageEnvelope. - doc = CSearchDocument::NewL( qt_QString2TPtrC( QString().setNum( aEnvelope.id() ) ), + doc = CSearchDocument::NewL( qt_QString2TPtrC( QString().setNum( aEnvelopeId ) ), KMsgBaseAppClassGeneric ); ); qDebug() << "getPartialSearchDocument :END"; @@ -173,56 +157,95 @@ } //anonymous namespace //------------------------------------------------------------------------------ -CSearchDocument* QEmailFetcher::getSearchDocument( const NmApiMessageEnvelope& aEnvelope ,quint64 aMailboxId, quint64 aFolderId ){ +CSearchDocument* QEmailFetcher::getSearchDocumentL( const NmApiMessageEnvelope& aEnvelope ,quint64 aMailboxId, quint64 aFolderId ){ QList toList; - qDebug() << "QEmailFetcher::getSearchDocument :START"; + qDebug() << "QEmailFetcher::getSearchDocumentL :START"; + //We need ALL the recipients in a SINGLE field. + //Need to cast away const-ness since the get method is unfortunately not const. const_cast(aEnvelope).getToRecipients( toList ); - - //We need ALL the recipients in a SINGLE field. QString recipients = ""; for( int i=0; i(aEnvelope).getCcRecipients( toList ); + for( int i=0; i(aEnvelope).getPlainTextBody( body ); QString msgBody = body.content(); - + qDebug() << "QEmailFetcher::Body of mail using paintextAPI:"<< aEnvelope.plainText() ; CSearchDocument* doc = 0; QT_TRAP_THROWING( //Use qt_Qstring2TPtrC since we are working with const EmailMessageEnvelope. doc = CSearchDocument::NewL( qt_QString2TPtrC( QString().setNum( aEnvelope.id() ) ), KMsgBaseAppClassGeneric ); - doc->AddFieldL( KMimeTypeField, KMimeTypeMsg, CDocumentField::EStoreYes | CDocumentField::EIndexUnTokenized); - doc->AddFieldL( KMsgSubject, qt_QString2TPtrC( aEnvelope.subject() ), CDocumentField::EStoreYes | CDocumentField::EIndexTokenized ); - doc->AddFieldL( KMsgRecipients, qt_QString2TPtrC( recipients ), CDocumentField::EStoreYes | CDocumentField::EIndexTokenized ); - doc->AddFieldL( KMsgBody, qt_QString2TPtrC( msgBody ), CDocumentField::EStoreYes | CDocumentField::EIndexTokenized ); - doc->AddFieldL( KMailBoxId, qt_QString2TPtrC( QString().setNum( aMailboxId ) ), CDocumentField::EStoreYes | CDocumentField::EIndexUnTokenized ); - doc->AddFieldL( KFolderId, qt_QString2TPtrC( QString().setNum( aFolderId ) ), CDocumentField::EStoreYes | CDocumentField::EIndexUnTokenized ); + //Add the sender details + doc->AddFieldL( KMsgSender, qt_QString2TPtrC( const_cast(aEnvelope).sender() ), + CDocumentField::EStoreYes | CDocumentField::EIndexTokenized | CDocumentField::EIndexFreeText); + //Add the Mimetype + doc->AddFieldL( KMimeTypeField, KMimeTypeMsg, CDocumentField::EStoreYes | CDocumentField::EIndexUnTokenized ); + //Add the Subject field + doc->AddFieldL( KMsgSubject, qt_QString2TPtrC( aEnvelope.subject() ), + CDocumentField::EStoreYes | CDocumentField::EIndexTokenized ); + //Add the recipients list (Includes To and CC fields) + doc->AddFieldL( KMsgRecipients, qt_QString2TPtrC( recipients ), + CDocumentField::EStoreYes | CDocumentField::EIndexTokenized | CDocumentField::EIndexFreeText ); + //Add the email body + doc->AddFieldL( KMsgBody, qt_QString2TPtrC( msgBody ), + CDocumentField::EStoreYes | CDocumentField::EIndexTokenized ); + //Add the mailboxid + doc->AddFieldL( KMailBoxId, qt_QString2TPtrC( QString().setNum( aMailboxId ) ), + CDocumentField::EStoreYes | CDocumentField::EIndexUnTokenized | CDocumentField::EAggregateNo ); + //Add the folder Id + doc->AddFieldL( KFolderId, qt_QString2TPtrC( QString().setNum( aFolderId ) ), + CDocumentField::EStoreYes | CDocumentField::EIndexUnTokenized | CDocumentField::EAggregateNo ); + //Add the attachment field. This field will be added only if there is any attachment.Details of the attachment + //are not added due to lack of email application support. + if ( aEnvelope.hasAttachments() ) + doc->AddFieldL( KHasAttachment, KAttachment, CDocumentField::EStoreYes | CDocumentField::EIndexTokenized); + //Add mailbox name + NmApiMailbox aMailBox; + iEmailService->getMailbox( aMailboxId, aMailBox ); + doc->AddFieldL( KMailBoxName, qt_QString2TPtrC(aMailBox.name()) , + CDocumentField::EStoreYes | CDocumentField::EIndexTokenized | CDocumentField::EIndexFreeText ); -// NmApiMailbox aMailBox; -// iEmailService->getMailbox( aMailboxId, aMailBox ); -// doc->AddFieldL( KMailBoxName, qt_QString2TPtrC(aMailBox.name()) , CDocumentField::EStoreYes | CDocumentField::EIndexUnTokenized ); - //TODO : Add folder name field - //_LIT(KFolderName, "FolderName"); + // Sent date time KSentTime + QDateTime time = aEnvelope.sentTime(); + doc->AddFieldL( KSentTime, qt_QString2TPtrC(time.toString ()) , + CDocumentField::EStoreYes | CDocumentField::EIndexTokenized ); + //TODO : Foldername is not harvested as there is no API exposed from email application to get the foldername based on folderID. + //This feature will be implemented as soon as we get supporting API's from Email team + //Adding subject and body to the excerpt. QString excerpt ; excerpt = aEnvelope.subject(); excerpt += msgBody ; doc->AddExcerptL( qt_QString2TPtrC(excerpt) ); ); - qDebug() << "QEmailFetcher::getSearchDocument :END"; + qDebug() << "QEmailFetcher::getSearchDocumentL :END"; return doc; } //------------------------------------------------------------------------------ -//Just to avoid duplication of the following two lines. void QEmailFetcher::NotifyHarvestingComplete(){ qDebug() << "QEmailFetcher::NotifyHarvestingComplete :START"; iCurrentMailboxIndex = iCurrentFolderIndex = 0; + //Free the iMailBoxes and iFolders + iMailBoxes.clear(); + iFolders.clear(); QT_TRAP_THROWING( iEmailObserver.HarvestingCompleted() ); qDebug() << "QEmailFetcher::NotifyHarvestingComplete :END"; return; @@ -271,13 +294,14 @@ qDebug() << "QEmailFetcher::processNextMailbox :END (harvesting completed)"; return; } - //More mailboxes available. delete iMailFolderList; iMailFolderList = NULL; iMailFolderList = new NmApiFolderListing( this, iMailBoxes.at( iCurrentMailboxIndex++ ).id() ); connect( iMailFolderList, SIGNAL(foldersListed( qint32 )), this, SLOT(handleMailFoldersListed( qint32)) ); - const int waitForSeconds = 30; //TODO Move this constant out of here if needed elsewhere - QTimer::singleShot( waitForSeconds, iMailFolderList, SLOT( start()) ); + iMailFolderList->start(); + +// const int waitForSeconds = 30; //TODO Move this constant out of here if needed elsewhere +// QTimer::singleShot( waitForSeconds, iMailFolderList, SLOT( start()) ); qDebug() << "QEmailFetcher::processNextMailbox :END (goto next mailbox)"; } @@ -309,7 +333,6 @@ processNextMailbox(); return;//Don't proceed futher. } - //More folders to process. //Already set to NULL in constructor, so safe to call delete first time. delete iEnvelopeListing; iEnvelopeListing = NULL; @@ -336,15 +359,15 @@ for( int i=0; i aMessageList){ +void QEmailFetcher::handleMessageEvent( EmailClientApi::NmApiMessageEvent aEvent, quint64 aMailboxId, quint64 aFolderId, QList aMessageList){ NmApiMessageEnvelope envelope; qDebug() << "QEmailFetcher::handleMessageEvent :START"; const int messageCount = aMessageList.count(); @@ -355,7 +378,7 @@ if( iEmailService->getEnvelope( aMailboxId, aFolderId, aMessageList.at( i ), envelope ) ){ qDebug() << "QEmailFetcher::handleMessageEvent :HandleDocumentL"; QT_TRAP_THROWING( - iEmailObserver.HandleDocumentL( getSearchDocument( envelope, aMailboxId, aFolderId ), + iEmailObserver.HandleDocumentL( getSearchDocumentL( envelope, aMailboxId, aFolderId ), //Doing this simply avoids *duplicate* code for update action. aEvent == MessageCreated ? ECPixAddAction : ECPixUpdateAction ) ); } @@ -363,56 +386,11 @@ } else if( aEvent == MessageDeleted ) { qDebug() << "QEmailFetcher::handleMessageEvent :MessageDeleted"; - //TODO We can do better. For delete, we dont have to create full document. Just the ID should be enough. - //We can have another function called getPartialSearchDocument so deletes will be faster. for( int i=0; igetEnvelope( aMailboxId, aFolderId, aMessageList.at( i ), envelope ) ){ qDebug() << "QEmailFetcher::handleMessageEvent :MessageDeleted : HandleDocumentL"; QT_TRAP_THROWING( - iEmailObserver.HandleDocumentL( getPartialSearchDocument( envelope ), ECPixRemoveAction ) ); + iEmailObserver.HandleDocumentL( getPartialSearchDocument( aMessageList.at( i ) ), ECPixRemoveAction ) ); } } } - } } - -//-------------------------------------------------------------------------------------- -void QEmailFetcher::handlemailboxEvent( EmailClientApi::NmApiMailboxEvent event, QList idlist ){ - const int mailboxCount = idlist.count(); - if( event == MailboxCreated ) - { - //New mailbox is created.Harvest the Mailbox name and all the folder names - } - else - { - //MailBox is deleted so delete the document related to all the mailbox and folders in the mailbox - } -} - -CSearchDocument* QEmailFetcher::getMailboxorfolderSearchDocument( quint64 aMailboxId, quint64 aFolderId, TEmailDocType aDocType, QString aFoldername ){ - - CSearchDocument* doc = 0; - QT_TRAP_THROWING( - //Use qt_Qstring2TPtrC since we are working with const EmailMessageEnvelope. - if ( aDocType == EEmailTypeFolder) - { - doc = CSearchDocument::NewL( qt_QString2TPtrC( QString().setNum( aFolderId ) ), - KMsgBaseAppClassGeneric ); - doc->AddFieldL( KFolderName, qt_QString2TPtrC(aFoldername) , CDocumentField::EStoreYes | CDocumentField::EIndexUnTokenized ); - doc->AddFieldL( KFolderId, qt_QString2TPtrC( QString().setNum( aFolderId ) ), CDocumentField::EStoreYes | CDocumentField::EIndexUnTokenized ); - } - else if ( aDocType == EEmailTypeMailBox ) - { - doc = CSearchDocument::NewL( qt_QString2TPtrC( QString().setNum( aMailboxId ) ), - KMsgBaseAppClassGeneric ); - NmApiMailbox aMailBox; - iEmailService->getMailbox( aMailboxId, aMailBox ); - doc->AddFieldL( KMailBoxName, qt_QString2TPtrC(aMailBox.name()) , CDocumentField::EStoreYes | CDocumentField::EIndexUnTokenized ); - } - doc->AddFieldL( KMimeTypeField, KMimeTypeMsg, CDocumentField::EStoreYes | CDocumentField::EIndexUnTokenized); - doc->AddFieldL( KMailBoxId, qt_QString2TPtrC( QString().setNum( aMailboxId ) ), CDocumentField::EStoreYes | CDocumentField::EIndexUnTokenized ); - - ); - qDebug() << "QEmailFetcher::getSearchDocument :END"; - return doc; -}