messagingapp/msgappfw/plugins/msgplugin/src/ccsmsghandler.cpp
changeset 44 36f374c67aa8
parent 34 84197e66a4bd
child 52 12db4185673b
equal deleted inserted replaced
43:35b64624a9e7 44:36f374c67aa8
    19 
    19 
    20 
    20 
    21 // USER INCLUDES
    21 // USER INCLUDES
    22 #include "ccsmsghandler.h"
    22 #include "ccsmsghandler.h"
    23 #include "msgbiouids.h"
    23 #include "msgbiouids.h"
       
    24 #include "ccsdefs.h"
    24 
    25 
    25 // SYSTEM INCLUDES
    26 // SYSTEM INCLUDES
    26 #include <smsclnt.h>                   
    27 #include <smsclnt.h>                   
    27 #include <mmsclient.h>
    28 #include <mmsclient.h>
    28 #include <mtclreg.h>
    29 #include <mtclreg.h>
    66 // ----------------------------------------------------------------------------
    67 // ----------------------------------------------------------------------------
    67 //
    68 //
    68 CCsMsgHandler::~CCsMsgHandler()
    69 CCsMsgHandler::~CCsMsgHandler()
    69     {
    70     {
    70     PRINT ( _L("Enter CCsMsgHandler::~CCsMsgHandler") );
    71     PRINT ( _L("Enter CCsMsgHandler::~CCsMsgHandler") );
    71 
    72     if(iIdle)
       
    73         {
       
    74         iIdle->Cancel();
       
    75         delete iIdle;
       
    76         iIdle=NULL;
       
    77         }
    72     if(iMsgPluginUtility)
    78     if(iMsgPluginUtility)
    73         {
    79         {
    74         delete iMsgPluginUtility;
    80         delete iMsgPluginUtility;
    75         iMsgPluginUtility = NULL;
    81         iMsgPluginUtility = NULL;
    76         }
    82         }
   105         {
   111         {
   106         delete iSession;
   112         delete iSession;
   107         iSession = NULL;
   113         iSession = NULL;
   108         }
   114         }
   109 
   115 
   110     if ( iRootEntry )
       
   111         {
       
   112         delete iRootEntry;
       
   113         iRootEntry = NULL;
       
   114         }
       
   115 
       
   116     if ( iMessages )
       
   117         {
       
   118 	    iMessages->Reset();
       
   119         delete iMessages;
       
   120         iMessages = NULL;
       
   121         }
       
   122     
       
   123     if(iMessageArray)
   116     if(iMessageArray)
   124     {
   117     {
   125         iMessageArray->Reset();
   118         iMessageArray->Reset();
   126         iMessageArray->Close();
   119         iMessageArray->Close();
   127         delete iMessageArray;
   120         delete iMessageArray;
   144 
   137 
   145     iSession = CMsvSession::OpenSyncL(*this);
   138     iSession = CMsvSession::OpenSyncL(*this);
   146 
   139 
   147     iMsgPluginUtility = CCsMsgPluginUtility::NewL();
   140     iMsgPluginUtility = CCsMsgPluginUtility::NewL();
   148 
   141 
   149     iConverstationEntryList = new(ELeave)RPointerArray<CCsConversationEntry>();
   142     iConverstationEntryList = new(ELeave) RPointerArray<CCsConversationEntry>(1);
   150 
   143 
   151     iMtmRegistry = CClientMtmRegistry::NewL( *iSession );
   144     iMtmRegistry = CClientMtmRegistry::NewL( *iSession );
   152 
   145 
   153     iSmsMtm = static_cast<CSmsClientMtm*>( iMtmRegistry->
   146     iSmsMtm = static_cast<CSmsClientMtm*>( iMtmRegistry->
   154             NewMtmL( KSenduiMtmSmsUid ) );
   147             NewMtmL( KSenduiMtmSmsUid ) );
   157             NewMtmL( KSenduiMtmMmsUid ) );
   150             NewMtmL( KSenduiMtmMmsUid ) );
   158 
   151 
   159     iState = EReadInbox;
   152     iState = EReadInbox;
   160 
   153 
   161     iMessageArray = new (ELeave)RArray <TMsvId>(KMessageArrayGranularity);
   154     iMessageArray = new (ELeave)RArray <TMsvId>(KMessageArrayGranularity);
   162 
       
   163     iMessageCount = 0;
       
   164 
   155 
   165     PRINT ( _L("End CCsMsgHandler::ConstructL") );
   156     PRINT ( _L("End CCsMsgHandler::ConstructL") );
   166     }
   157     }
   167 
   158 
   168 // ----------------------------------------------------------------------------
   159 // ----------------------------------------------------------------------------
   199     
   190     
   200     if ( entry.Parent() == KMsvGlobalInBoxIndexEntryIdValue ||
   191     if ( entry.Parent() == KMsvGlobalInBoxIndexEntryIdValue ||
   201          entry.Parent() == KMsvSentEntryIdValue ||
   192          entry.Parent() == KMsvSentEntryIdValue ||
   202          entry.Parent() == KMsvGlobalOutBoxIndexEntryIdValue )
   193          entry.Parent() == KMsvGlobalOutBoxIndexEntryIdValue )
   203         {
   194         {
       
   195         CleanupResetAndDestroyPushL(addressList);
       
   196         
   204         iMsgPluginUtility->CreateContactL(iSession, entry, addressList);
   197         iMsgPluginUtility->CreateContactL(iSession, entry, addressList);
   205 
   198 
   206         //process entry
   199         //process entry
   207         ExtractAddressesL(entry, ERead, addressList);
   200         ExtractAddressesL(entry, ERead, addressList);
   208 
   201         
   209         addressList.ResetAndDestroy();	
   202         // Cleanup addressList
       
   203         CleanupStack::PopAndDestroy(&addressList);
   210         }
   204         }
   211 
   205 
   212     PRINT ( _L("Exit CCsMsgHandler::ProcessResultsL") );
   206     PRINT ( _L("Exit CCsMsgHandler::ProcessResultsL") );
   213     }
   207     }
   214 
   208 
   346             {
   340             {
   347             // sent/inbox/Outbox entry
   341             // sent/inbox/Outbox entry
   348             if ( aParent == KMsvSentEntryIdValue ||
   342             if ( aParent == KMsvSentEntryIdValue ||
   349                  aParent == KMsvGlobalInBoxIndexEntryIdValue )
   343                  aParent == KMsvGlobalInBoxIndexEntryIdValue )
   350                 {
   344                 {
       
   345                 CleanupResetAndDestroyPushL(addressList);
   351                 // currently server needs number, so getting it from header
   346                 // currently server needs number, so getting it from header
   352                 iMsgPluginUtility->CreateContactL(iSession, entry,
   347                 iMsgPluginUtility->CreateContactL(iSession, entry,
   353                         addressList);
   348                         addressList);
   354 
   349 
   355                 //process entry
   350                 //process entry
   356                 // entry created in sent already exists, hence an update
   351                 // entry created in sent already exists, hence an update
   357                 ExtractAddressesL(entry, EUpdate, addressList);
   352                 ExtractAddressesL(entry, EUpdate, addressList);
   358 
   353 
   359                 addressList.ResetAndDestroy();
   354                 CleanupStack::PopAndDestroy(&addressList);
   360                 }  
   355                 }  
   361                 
   356                 
   362             //for drafts and outbox, the entry changes multiple times,
   357             //for drafts and outbox, the entry changes multiple times,
   363             // so making a check if entry has changed
   358             // so making a check if entry has changed
   364             else if(aParent == KMsvGlobalOutBoxIndexEntryIdValue)
   359             else if(aParent == KMsvGlobalOutBoxIndexEntryIdValue)
   370                     
   365                     
   371                     // if the entry state is sent, we dont need to process it
   366                     // if the entry state is sent, we dont need to process it
   372                     // as it is going to appear in sent items
   367                     // as it is going to appear in sent items
   373                     if( ECsSendStateSent != iMsgPluginUtility->GetSendState(entry) )
   368                     if( ECsSendStateSent != iMsgPluginUtility->GetSendState(entry) )
   374                         {
   369                         {
       
   370                          CleanupResetAndDestroyPushL(addressList);
   375                         // currently server needs number, so getting it from header
   371                         // currently server needs number, so getting it from header
   376                         iMsgPluginUtility->CreateContactL(iSession, entry,
   372                         iMsgPluginUtility->CreateContactL(iSession, entry,
   377                                 addressList);
   373                                 addressList);
   378                         
   374                         
   379                         //process entry
   375                         //process entry
   380                         // entry created in sent already exists, hence an update
   376                         // entry created in sent already exists, hence an update
   381                         ExtractAddressesL(entry, EUpdate, addressList);
   377                         ExtractAddressesL(entry, EUpdate, addressList);
   382                         
   378                         
   383                         iPrevEntry = entry;
   379                         iPrevEntry = entry;
   384                         
   380                         
   385                         addressList.ResetAndDestroy();
   381                         CleanupStack::PopAndDestroy(&addressList);
   386                         }
   382                         }
   387                     }//end check changed entry
   383                     }//end check changed entry
   388                 }
   384                 }
   389             }
   385             }
   390         }//end for loop
   386         }//end for loop
   406     HBufC* contact = NULL;
   402     HBufC* contact = NULL;
   407     HBufC* sDescription = NULL;
   403     HBufC* sDescription = NULL;
   408 
   404 
   409     if (aContact.Length()>0)
   405     if (aContact.Length()>0)
   410         {
   406         {
   411         contact = aContact.AllocL();
   407         contact = aContact.AllocLC();
   412         CleanupStack::PushL(contact);
       
   413         }
   408         }
   414     if (aDescription.Length()>0)
   409     if (aDescription.Length()>0)
   415         {
   410         {
   416         sDescription = aDescription.AllocL();
   411         sDescription = aDescription.AllocLC();
   417         CleanupStack::PushL(sDescription);
       
   418         }
   412         }
   419 
   413 
   420     //use utility to create conversation entry
   414     //use utility to create conversation entry
   421     CCsConversationEntry *conversationEntry = iMsgPluginUtility->CreateConversationEntryL(
   415     CCsConversationEntry *conversationEntry = iMsgPluginUtility->CreateConversationEntryL(
   422             contact,
   416             contact,
   502 
   496 
   503     if ( (aEntry.iBioType && 
   497     if ( (aEntry.iBioType && 
   504 	      aEntry.iBioType != KUidMsgSubTypeMmsAudioMsg.iUid ) || 
   498 	      aEntry.iBioType != KUidMsgSubTypeMmsAudioMsg.iUid ) || 
   505 		  aEntry.iMtm == KSenduiMtmBtUid ) 
   499 		  aEntry.iMtm == KSenduiMtmBtUid ) 
   506         {
   500         {
   507         tmpBuffer = aEntry.iDescription.AllocL();
   501         tmpBuffer = aEntry.iDescription.AllocLC();
   508         description.Set( tmpBuffer->Des() );         
   502         description.Set( tmpBuffer->Des() );         
   509         }    
   503         }    
   510     else if ( aEntry.iMtm == KSenduiMtmSmsUid )  
   504     else if ( aEntry.iMtm == KSenduiMtmSmsUid )  
   511         {
   505         {
   512         iSmsMtm->SwitchCurrentEntryL( aEntry.Id() );
   506         iSmsMtm->SwitchCurrentEntryL( aEntry.Id() );
   513         iSmsMtm->LoadMessageL();
   507         iSmsMtm->LoadMessageL();
   514             
   508             
   515         CRichText& body = iSmsMtm->Body();
   509         CRichText& body = iSmsMtm->Body();
   516         TInt smsLength = body.DocumentLength();      
   510         TInt smsLength = body.DocumentLength();      
   517         tmpBuffer = HBufC::NewL(smsLength);
   511         tmpBuffer = HBufC::NewLC(smsLength);
   518         TPtr ptr(tmpBuffer->Des());
   512         TPtr ptr(tmpBuffer->Des());
   519         body.Extract(ptr, 0);
   513         body.Extract(ptr, 0);
   520         description.Set( tmpBuffer->Des() );  
   514         description.Set( tmpBuffer->Des() );  
   521         }
   515         }
   522     else if ( aEntry.iMtm == KSenduiMtmMmsUid  || 
   516     else if ( aEntry.iMtm == KSenduiMtmMmsUid  || 
   523 	          aEntry.iMtm == KSenduiMMSNotificationUid)  
   517 	          aEntry.iMtm == KSenduiMMSNotificationUid)  
   524         {
   518         {
   525         tmpBuffer = aEntry.iDescription.AllocL();
   519         tmpBuffer = aEntry.iDescription.AllocLC();
   526         description.Set( tmpBuffer->Des() );         
   520         description.Set( tmpBuffer->Des() );         
   527 
   521 
   528         iMmsMtm->SwitchCurrentEntryL( aEntry.Id() );
   522         iMmsMtm->SwitchCurrentEntryL( aEntry.Id() );
   529         iMmsMtm->LoadMessageL();
   523         iMmsMtm->LoadMessageL();
   530 
   524 
   566     else
   560     else
   567         {
   561         {
   568         // Unknown
   562         // Unknown
   569         ProcessEntryL(aEvent, KNullDesC, description, aEntry);       
   563         ProcessEntryL(aEvent, KNullDesC, description, aEntry);       
   570         }
   564         }
   571 
   565     
   572     delete tmpBuffer;
   566     //Cleanup tmpBuffer
       
   567     if ( tmpBuffer )
       
   568         {
       
   569 		 CleanupStack::PopAndDestroy();
       
   570         }
   573 
   571 
   574     PRINT ( _L("Exit CCsMsgHandler::ExtractAddressesL") );
   572     PRINT ( _L("Exit CCsMsgHandler::ExtractAddressesL") );
   575     }
   573     }
   576 
   574 
   577 // -----------------------------------------------------------------------------
   575 // -----------------------------------------------------------------------------
   587 // CCsMsgHandler::UploadMsgL()
   585 // CCsMsgHandler::UploadMsgL()
   588 // State machine to upload all messages 
   586 // State machine to upload all messages 
   589 // -----------------------------------------------------------------------------
   587 // -----------------------------------------------------------------------------
   590 //
   588 //
   591 TInt CCsMsgHandler::UploadMsgL() 
   589 TInt CCsMsgHandler::UploadMsgL() 
   592 {
   590     {
       
   591     //Release scheduler for scheduling.    
       
   592     User::After(1);
       
   593     
   593     switch ( iState ) 
   594     switch ( iState ) 
   594     {
   595         {
   595         case EReadInbox:
   596         case EReadInbox:
   596         {
   597             {
   597             iRootEntry = iSession->GetEntryL(KMsvGlobalInBoxIndexEntryId);
   598             UpdateMessageArrayL(KMsvGlobalInBoxIndexEntryId);
   598 
       
   599             // Set sort order
       
   600             TMsvSelectionOrdering order;
       
   601             order.SetSorting(EMsvSortById);
       
   602             iRootEntry->SetSortTypeL(order);
       
   603 
       
   604             iMessages = iRootEntry->ChildrenL();
       
   605             iMessageCount = iRootEntry->Count();
       
   606             if(iMessageCount)
       
   607             {
       
   608                 for(int i = 0; i < iMessageCount; i ++)
       
   609                 {
       
   610                     iMessageArray->Append(iMessages->At(i));
       
   611                 }
       
   612             }
       
   613 
       
   614             iState = EReadSent;
   599             iState = EReadSent;
   615             CleanupL();              
       
   616 
       
   617             return 1;
   600             return 1;
   618         }
   601             }
   619 
   602 
   620         case EReadSent:
   603         case EReadSent:
   621         {
   604             {
   622             iRootEntry = iSession->GetEntryL(KMsvSentEntryId);
   605             UpdateMessageArrayL(KMsvSentEntryId);
   623 
       
   624             // Set sort order
       
   625             TMsvSelectionOrdering order;
       
   626             order.SetSorting(EMsvSortById);
       
   627             iRootEntry->SetSortTypeL(order);
       
   628 
       
   629             iMessages = iRootEntry->ChildrenL();    
       
   630             iMessageCount = iRootEntry->Count();
       
   631             if(iMessageCount)
       
   632             {
       
   633                 for(int i = 0; i < iMessageCount; i++ )
       
   634                 {
       
   635                     iMessageArray->Append(iMessages->At(i));
       
   636                 }
       
   637             }
       
   638 
       
   639             iState = EReadOutbox;
   606             iState = EReadOutbox;
   640             CleanupL();
       
   641 
       
   642             return 1;
   607             return 1;
   643         }
   608             }
   644 
   609 
   645         case EReadOutbox:
   610         case EReadOutbox:
   646         {
   611             {
   647             iRootEntry = iSession->GetEntryL(KMsvGlobalOutBoxIndexEntryId);
   612             UpdateMessageArrayL(KMsvGlobalOutBoxIndexEntryId);
   648 
   613             iState = ESortEntries;            
   649             // Set sort order
       
   650             TMsvSelectionOrdering order;
       
   651             order.SetSorting(EMsvSortById);
       
   652             iRootEntry->SetSortTypeL(order);
       
   653 
       
   654             iMessages = iRootEntry->ChildrenL();  
       
   655             iMessageCount = iRootEntry->Count();
       
   656 
       
   657             if(iMessageCount)
       
   658             {
       
   659                 for(int i = 0; i < iMessageCount; i ++)
       
   660                 {
       
   661                     iMessageArray->Append(iMessages->At(i));
       
   662                 }
       
   663                 iMessageCount=0;
       
   664             }
       
   665             iState = ESortEntries;
       
   666             CleanupL();
       
   667 
       
   668             return 1;
   614             return 1;
   669         }
   615             }
   670         case ESortEntries:
   616         case ESortEntries:
   671         {
   617             {
   672              //Sort the elements in the array by descending order of TMsvId's
   618             //Sort the elements in the array by descending order of TMsvId's
   673             TLinearOrder<TMsvId> order(CompareOrder);
   619             TLinearOrder<TMsvId> order(CompareOrder);
   674             iMessageArray->Sort(order);
   620             iMessageArray->Sort(order);
   675             iState = EProcessEntries;
   621             iState = EProcessEntries;
   676             return 1;
   622             return 1;
   677         }
   623             }            
   678         
       
   679         case EProcessEntries:
   624         case EProcessEntries:
   680         { 
   625             { 
   681             //Process one entry at a time in sequence
   626             //Process one entry at a time in sequence
   682             //Process the first element in the array on each call, till the end
   627             //Process the first element in the array on each call, till the end
   683             if(iMessageArray->Count())
   628             if(iMessageArray->Count())
   684             {
   629                 {
   685                 ProcessResultsL(iSession->GetEntryL(iMessageArray->operator[](0))->Entry());
   630                 CMsvEntry* msvEntry= iSession->
       
   631                         GetEntryL(iMessageArray->operator[](0));
       
   632                 CleanupStack::PushL(msvEntry);
       
   633                 ProcessResultsL(msvEntry->Entry());
       
   634                 CleanupStack::PopAndDestroy(msvEntry);
   686                 iMessageArray->Remove(0);
   635                 iMessageArray->Remove(0);
   687             }
   636                 }
   688             else
   637             else
   689             {
   638                 {
       
   639                 iMessageArray->Reset();
   690                 iMsgObserver->HandleCachingCompleted();
   640                 iMsgObserver->HandleCachingCompleted();
   691                 return 0; //DONE 
   641                 return 0; //DONE 
   692             }
   642                 }            
   693 
       
   694             iState = EProcessEntries;
   643             iState = EProcessEntries;
   695             return 1; 
   644             return 1; 
   696         }
   645         }
   697     }
   646     }
   698     return 0;    
   647     return 0;    
   720     {
   669     {
   721     PRINT ( _L("Enter CCsMsgHandler::Start") );
   670     PRINT ( _L("Enter CCsMsgHandler::Start") );
   722 
   671 
   723     iState = EReadInbox;
   672     iState = EReadInbox;
   724     TCallBack callback = TCallBack(UploadMsg, (TAny*) this);
   673     TCallBack callback = TCallBack(UploadMsg, (TAny*) this);
   725     iIdle = CIdle::NewL(CActive::EPriorityLow);
   674     
       
   675     if(iIdle)
       
   676         {
       
   677         delete iIdle;
       
   678         iIdle = NULL;
       
   679         }
       
   680     iIdle = CIdle::NewL(CActive::EPriorityIdle);
   726     iIdle->Start(callback);
   681     iIdle->Start(callback);
   727 
   682 
   728     PRINT ( _L("End CCsMsgHandler::Start") );
   683     PRINT ( _L("End CCsMsgHandler::Start") );
   729     }
       
   730 
       
   731 // -----------------------------------------------------------------------------
       
   732 // CCsMsgHandler::CleanupL()
       
   733 // Helper function for state machine cleanup
       
   734 // -----------------------------------------------------------------------------
       
   735 //
       
   736 void CCsMsgHandler::CleanupL()
       
   737     {
       
   738     if ( iRootEntry )
       
   739         {
       
   740         delete iRootEntry;
       
   741         iRootEntry = NULL;
       
   742         }
       
   743 
       
   744     if ( iMessages )
       
   745         {
       
   746 	    iMessages->Reset();
       
   747         delete iMessages;
       
   748         iMessages = NULL;
       
   749         }
       
   750 
       
   751     iMessageCount = 0;     
       
   752     }
   684     }
   753 
   685 
   754 // -----------------------------------------------------------------------------
   686 // -----------------------------------------------------------------------------
   755 // CCsMsgHandler::IsMtmSupported()
   687 // CCsMsgHandler::IsMtmSupported()
   756 // 
   688 // 
   833         type = ECsUnknown;
   765         type = ECsUnknown;
   834         break;
   766         break;
   835     }
   767     }
   836     return (type);
   768     return (type);
   837 }
   769 }
       
   770 // -----------------------------------------------------------------------------
       
   771 // void CCsMsgHandler::UpdateMessageArrayL(const TMsvId& aFolderId)
       
   772 // Update iMessageArray with the message ID before upload message to csserver. 
       
   773 // -----------------------------------------------------------------------------
       
   774 //
       
   775 void CCsMsgHandler::UpdateMessageArrayL(const TMsvId& aFolderId)
       
   776     {
       
   777     CMsvEntry* msvEntry = iSession->GetEntryL(aFolderId);
       
   778     CleanupStack::PushL(msvEntry);
       
   779     // Set sort order
       
   780     TMsvSelectionOrdering order;
       
   781     order.SetSorting(EMsvSortById);
       
   782     msvEntry->SetSortTypeL(order);
       
   783 
       
   784     CMsvEntrySelection* messages = msvEntry->ChildrenL();  
       
   785     // Cleanup msvEntry
       
   786     CleanupStack::PopAndDestroy(msvEntry);
       
   787     CleanupStack::PushL(messages);
       
   788 
       
   789     TInt messageCount = messages->Count();
       
   790     
       
   791     // In case of large number of message caching
       
   792     // to avoid multiple reallocation alloc memory
       
   793     // one shot.
       
   794     if( messageCount > KMessageArrayGranularity)
       
   795         iMessageArray->Reserve(messageCount);
       
   796     
       
   797     for(int i = 0; i < messageCount; i ++)
       
   798         {
       
   799         TMsvId msgId=messages->At(i);
       
   800         iMessageArray->Append(msgId);
       
   801         }
       
   802     // Cleanup messages
       
   803     CleanupStack::PopAndDestroy(messages); 
       
   804     }
   838 // End of file
   805 // End of file
   839 
   806