harvesterplugins/messaging/email/qtemailfetcher/qtemailfetcher.cpp
changeset 3 6832643895f7
parent 2 208a4ba3894c
child 7 51d10d255e92
equal deleted inserted replaced
2:208a4ba3894c 3:6832643895f7
    35     :iEmailObserver( aObserver ), 
    35     :iEmailObserver( aObserver ), 
    36      iEmailEventNotifier( NULL ),
    36      iEmailEventNotifier( NULL ),
    37      iEmailService( NULL ),
    37      iEmailService( NULL ),
    38      iMailBoxListings( NULL ), 
    38      iMailBoxListings( NULL ), 
    39      iMailFolderList( NULL ),
    39      iMailFolderList( NULL ),
    40      iEnvelopeListing( NULL )
    40      iEnvelopeListing( NULL ),
       
    41      iCurrentMailboxIndex( 0 ), 
       
    42      iCurrentFolderIndex( 0 )
    41     {
    43     {
    42     }
    44     }
    43 
    45 
    44 //------------------------------------------------------------------------------
    46 //------------------------------------------------------------------------------
    45 QEmailFetcher::~QEmailFetcher()
    47 QEmailFetcher::~QEmailFetcher()
    55     QEmailFetcher* emailFetcher = NULL;
    57     QEmailFetcher* emailFetcher = NULL;
    56     
    58     
    57     //Leak free init.
    59     //Leak free init.
    58     try{
    60     try{
    59         QEmailFetcher* emailFetcher = new QEmailFetcher( aObserver );
    61         QEmailFetcher* emailFetcher = new QEmailFetcher( aObserver );
    60         //Uncomment 'this' once the actual APIs are ready.
       
    61         emailFetcher->iEmailService = new NmEmailService( emailFetcher );
    62         emailFetcher->iEmailService = new NmEmailService( emailFetcher );
    62         emailFetcher->iEmailEventNotifier =  new NmEventNotifier( emailFetcher );
    63         emailFetcher->iEmailEventNotifier =  new NmEventNotifier( emailFetcher );
    63         emailFetcher->iMailBoxListings = new NmMailboxListing( emailFetcher );
    64         emailFetcher->iMailBoxListings = new NmMailboxListing( emailFetcher );
    64     }catch(...){ //cleanup.
    65     }catch(...){ //cleanup.
    65         delete emailFetcher; 
    66         delete emailFetcher; 
   162     return doc;
   163     return doc;
   163 }
   164 }
   164 } //anonymous namespace
   165 } //anonymous namespace
   165 
   166 
   166 //------------------------------------------------------------------------------
   167 //------------------------------------------------------------------------------
       
   168 //Just to avoid duplication of the following two lines.
       
   169 void QEmailFetcher::NotifyHarvestingComplete(){
       
   170     iCurrentMailboxIndex = iCurrentFolderIndex = 0;
       
   171     QT_TRAP_THROWING( iEmailObserver.HarvestingCompleted() );
       
   172     return;
       
   173 }
       
   174 
       
   175 //------------------------------------------------------------------------------
       
   176 void QEmailFetcher::handleMailboxesListed(int aCount){
       
   177     iCurrentMailboxIndex = 0;
       
   178     if( aCount == NmMailboxListing::MailboxListingFailed ) {
       
   179         NotifyHarvestingComplete();
       
   180         return;
       
   181     }
       
   182     if( aCount>0 && iMailBoxListings->getMailboxes( iMailBoxes ) ){
       
   183         //Already set to NULL in constructor, so safe to call delete first time.
       
   184         processNextMailbox();
       
   185     }else{
       
   186         NotifyHarvestingComplete();
       
   187         return;
       
   188     }
       
   189 }
       
   190 
       
   191 //------------------------------------------------------------------------------
   167 //Options to make async (like other plugins' Asynchronizer):
   192 //Options to make async (like other plugins' Asynchronizer):
   168 //1. Use http://doc.trolltech.com/4.6/qtimer.html and connect timeout() signal to something?
   193 //1. Use http://doc.trolltech.com/4.6/qtimer.html and connect timeout() signal to something?
   169 //Downside: 
   194 //Downside: 
   170 //Have to save the state of the function and resume. Achievable via static members. 
   195 //Have to save the state of the function and resume. Achievable via static members. 
   171 //Remeber to reset counters.
   196 //Remeber to reset counters.
   176 //4. As recommended by the email API documentation, use SingleShotTimer:
   201 //4. As recommended by the email API documentation, use SingleShotTimer:
   177 //QTimer::singleShot(nsecs,nmFolderListing,SLOT(start());
   202 //QTimer::singleShot(nsecs,nmFolderListing,SLOT(start());
   178 //
   203 //
   179 //Recommendation: Use option 4.
   204 //Recommendation: Use option 4.
   180 
   205 
   181 void QEmailFetcher::handleMailboxesListed(int aCount){
   206 void QEmailFetcher::processNextMailbox(){
   182     QList<NmMailbox> mailBoxes;
   207     //No more mailboxes, notify completion.
   183     if( aCount>0 && iMailBoxListings->getMailboxes( mailBoxes ) ){
   208     if( iCurrentMailboxIndex >= iMailBoxes.count() ) {
   184         for( int i=0; i<aCount; i++ ){
   209         NotifyHarvestingComplete();
   185             //Already set to NULL in constructor, so safe to call delete first time.
   210         return;
   186             delete iMailFolderList; iMailFolderList = NULL;
   211     }
   187             iMailFolderList = new NmFolderListing( this, mailBoxes.at( i ).id() );
   212     
   188             connect( iMailFolderList, SIGNAL(foldersListed()), this, SLOT(mailFoldersListed()) );
   213     //More mailboxes available.
   189             const int waitForSeconds = 30; //TODO Move this constant out of here if needed elsewhere
   214     delete iMailFolderList; iMailFolderList = NULL;
   190             QTimer::singleShot( waitForSeconds, iMailFolderList, SLOT( start()) );
   215     iMailFolderList = new NmFolderListing( this, iMailBoxes.at( iCurrentMailboxIndex++ ).id() );
   191         }
   216     connect( iMailFolderList, SIGNAL(foldersListed()), this, SLOT(handleMailFoldersListed()) );
   192     }
   217     const int waitForSeconds = 30; //TODO Move this constant out of here if needed elsewhere
   193 }
   218     QTimer::singleShot( waitForSeconds, iMailFolderList, SLOT( start()) );
   194 
   219 }
   195 //------------------------------------------------------------------------------
   220 
   196 void QEmailFetcher::mailFoldersListed(int aCount){
   221 //------------------------------------------------------------------------------
   197     if( aCount == NmFolderListing::FolderListingFailed ) return; //silently.
   222 void QEmailFetcher::handleMailFoldersListed(int aCount){
   198     QList<NmFolder> folders;
   223     iCurrentFolderIndex = 0;
   199     if ( aCount && iMailFolderList->getFolders( folders ) ) {
   224     if( aCount == NmFolderListing::FolderListingFailed ){
   200         for( int i=0; i<aCount; i++ ){
   225         processNextMailbox();
   201             //Already set to NULL in constructor, so safe to call delete first time.
   226         return;//Don't proceed futher.
   202             delete iEnvelopeListing; iEnvelopeListing = NULL; 
   227     }
   203             iEnvelopeListing = new NmEnvelopeListing( this, folders.at( i ).id(), 0 );
   228     if( aCount && iMailFolderList->getFolders( iFolders ) ){ 
   204             connect(iEnvelopeListing, SIGNAL(envelopesListed(int)),this,SLOT(processMessages(int)));
   229         processNextFolder();
   205             iEnvelopeListing->start();
   230     }else{
   206         }
   231         processNextMailbox();
   207     }
   232         return;
       
   233     }
       
   234 }
       
   235 
       
   236 //------------------------------------------------------------------------------
       
   237 void QEmailFetcher::processNextFolder(){
       
   238     //No more folders in current mailbox.
       
   239     if( iCurrentFolderIndex >= iFolders.count() ) {
       
   240         processNextMailbox();
       
   241         return;//Don't proceed futher.
       
   242     }
       
   243     
       
   244     //More folders to process.
       
   245     //Already set to NULL in constructor, so safe to call delete first time.
       
   246     delete iEnvelopeListing; iEnvelopeListing = NULL; 
       
   247     iEnvelopeListing= new NmEnvelopeListing( 
       
   248             this, 
       
   249             iFolders.at( iCurrentFolderIndex++ ).id(),
       
   250             iMailBoxes.at( iCurrentMailboxIndex-1 ).id() ); //we have already incremented iMailboxIndex.
       
   251 
       
   252     connect(iEnvelopeListing, SIGNAL(envelopesListed(int)),this,SLOT(processMessages(int)));
       
   253     iEnvelopeListing->start();
   208 }
   254 }
   209 
   255 
   210 //------------------------------------------------------------------------------
   256 //------------------------------------------------------------------------------
   211 void QEmailFetcher::processMessages(int aCount){
   257 void QEmailFetcher::processMessages(int aCount){
   212     if( aCount == NmMailboxListing::MailboxListingFailed ) return; //silently.
   258     if( aCount == NmEnvelopeListing::EnvelopeListingFailed ) {
       
   259         processNextFolder();
       
   260         return;//Don't proceed futher.
       
   261     }
   213     QList<NmMessageEnvelope> envelopes;
   262     QList<NmMessageEnvelope> envelopes;
   214     if ( aCount > 0 && iEnvelopeListing->getEnvelopes(envelopes) ) {
   263     if ( aCount > 0 && iEnvelopeListing->getEnvelopes(envelopes) ) {
   215         for( int i=0; i<envelopes.count(); i++ ) {
   264         for( int i=0; i<envelopes.count(); i++ ) {
   216         const NmMessageEnvelope &envelope = envelopes.at( i );
   265             const NmMessageEnvelope &envelope = envelopes.at( i );
   217         //Create document and call back observer.
   266             //Create document and call back observer.
   218         QT_TRAP_THROWING( iEmailObserver.HandleDocumentL( getSearchDocument( envelope ), ECPixAddAction ) );
   267             QT_TRAP_THROWING( iEmailObserver.HandleDocumentL( getSearchDocument( envelope ), ECPixAddAction ) );
   219         }
   268         }
   220     }
   269     }
   221 }
   270 }
   222 
   271 
   223 //------------------------------------------------------------------------------
   272 //------------------------------------------------------------------------------