messagingapp/msgappfw/plugins/msgplugin/src/ccsmsghandler.cpp
changeset 37 518b245aa84c
child 41 25fe1fe642e3
equal deleted inserted replaced
25:84d9eb65b26f 37:518b245aa84c
       
     1 /*
       
     2 * Copyright (c) 2007 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:  CS Message Handler, This class caches the conversation server
       
    15  *                with the message data and also sends run-time updates to the
       
    16  *                server with the message data
       
    17  *
       
    18 */
       
    19 
       
    20 
       
    21 // USER INCLUDES
       
    22 #include "ccsmsghandler.h"
       
    23 #include "msgbiouids.h"
       
    24 #include "ccsdefs.h"
       
    25 
       
    26 // SYSTEM INCLUDES
       
    27 #include <smsclnt.h>                   
       
    28 #include <mmsclient.h>
       
    29 #include <mtclreg.h>
       
    30 #include <csmsemailfields.h>
       
    31 #include <txtrich.h>
       
    32 #include <msvids.h>
       
    33 #include <e32const.h>
       
    34 #include <SendUiConsts.h>
       
    35 #include <pushentry.h> // For services messages
       
    36 #include <e32cmn.h>
       
    37 
       
    38 
       
    39 //CONSTANTS
       
    40 const TInt KMessageArrayGranularity = 50;
       
    41 
       
    42 // NOTE:- DRAFT ENTRIES ARE NOT HANDLED IN THE PLUGIN
       
    43 
       
    44 // ============================== MEMBER FUNCTIONS ============================
       
    45 // ----------------------------------------------------------------------------
       
    46 // CCsMsgHandler::NewL
       
    47 // Two Phase Construction
       
    48 // ----------------------------------------------------------------------------
       
    49 //
       
    50 CCsMsgHandler* CCsMsgHandler::NewL(MCsMsgObserver *aMsgObserver)
       
    51     {
       
    52     PRINT ( _L("Enter CCsMsgHandler::NewL") );
       
    53 
       
    54     CCsMsgHandler* self = new ( ELeave ) CCsMsgHandler();
       
    55     CleanupStack::PushL( self );
       
    56     self->ConstructL(aMsgObserver);
       
    57     CleanupStack::Pop( self );
       
    58 
       
    59     PRINT ( _L("End CCsMsgHandler::NewL") );
       
    60 
       
    61     return self;
       
    62     }
       
    63 
       
    64 // ----------------------------------------------------------------------------
       
    65 // CCsMsgHandler::~CCsMsgHandler
       
    66 // Destructor
       
    67 // ----------------------------------------------------------------------------
       
    68 //
       
    69 CCsMsgHandler::~CCsMsgHandler()
       
    70     {
       
    71     PRINT ( _L("Enter CCsMsgHandler::~CCsMsgHandler") );
       
    72     if(iIdle)
       
    73         {
       
    74         iIdle->Cancel();
       
    75         delete iIdle;
       
    76         iIdle=NULL;
       
    77         }
       
    78     if(iMsgPluginUtility)
       
    79         {
       
    80         delete iMsgPluginUtility;
       
    81         iMsgPluginUtility = NULL;
       
    82         }
       
    83 
       
    84     if(iConverstationEntryList)
       
    85         {
       
    86         iConverstationEntryList->ResetAndDestroy();
       
    87         iConverstationEntryList->Close();
       
    88         delete iConverstationEntryList;
       
    89         iConverstationEntryList = NULL;
       
    90         }
       
    91 
       
    92     if(iSmsMtm)
       
    93         {
       
    94         delete iSmsMtm;
       
    95         iSmsMtm = NULL;
       
    96         }
       
    97 
       
    98     if(iMmsMtm)
       
    99         {
       
   100         delete iMmsMtm;
       
   101         iMmsMtm = NULL;
       
   102         }
       
   103 
       
   104     if(iMtmRegistry)
       
   105         {
       
   106         delete iMtmRegistry;
       
   107         iMtmRegistry = NULL;
       
   108         }
       
   109 
       
   110     if(iSession)
       
   111         {
       
   112         delete iSession;
       
   113         iSession = NULL;
       
   114         }
       
   115 
       
   116     if(iMessageArray)
       
   117     {
       
   118         iMessageArray->Reset();
       
   119         iMessageArray->Close();
       
   120         delete iMessageArray;
       
   121         iMessageArray = NULL;
       
   122     }
       
   123 
       
   124     PRINT ( _L("End CCsMsgHandler::~CCsMsgHandler") );
       
   125     }
       
   126 
       
   127 // ----------------------------------------------------------------------------
       
   128 // CCsMsgHandler::ConstructL
       
   129 // Two Phase Construction
       
   130 // ----------------------------------------------------------------------------
       
   131 //
       
   132 void CCsMsgHandler::ConstructL(MCsMsgObserver *aMsgObserver)
       
   133     {
       
   134     PRINT ( _L("Enter CCsMsgHandler::ConstructL") );
       
   135 
       
   136     iMsgObserver = aMsgObserver;
       
   137 
       
   138     iSession = CMsvSession::OpenSyncL(*this);
       
   139 
       
   140     iMsgPluginUtility = CCsMsgPluginUtility::NewL();
       
   141 
       
   142     iConverstationEntryList = new(ELeave) RPointerArray<CCsConversationEntry>(1);
       
   143 
       
   144     iMtmRegistry = CClientMtmRegistry::NewL( *iSession );
       
   145 
       
   146     iSmsMtm = static_cast<CSmsClientMtm*>( iMtmRegistry->
       
   147             NewMtmL( KSenduiMtmSmsUid ) );
       
   148 
       
   149     iMmsMtm = static_cast<CMmsClientMtm*>( iMtmRegistry->
       
   150             NewMtmL( KSenduiMtmMmsUid ) );
       
   151 
       
   152     iState = EReadInbox;
       
   153 
       
   154     iMessageArray = new (ELeave)RArray <TMsvId>(KMessageArrayGranularity);
       
   155 
       
   156     PRINT ( _L("End CCsMsgHandler::ConstructL") );
       
   157     }
       
   158 
       
   159 // ----------------------------------------------------------------------------
       
   160 // CCsMsgHandler::CCsMsgHandler
       
   161 // Two Phase Construction
       
   162 // ----------------------------------------------------------------------------
       
   163 //
       
   164 CCsMsgHandler::CCsMsgHandler()
       
   165     {
       
   166     }
       
   167 
       
   168 // -----------------------------------------------------------------------------
       
   169 // CCsMsgHandler::ProcessResultsL()
       
   170 // ProcessResultsL fetches results for the searchsort query and
       
   171 // updates the CCsServer with the fetched data
       
   172 // -----------------------------------------------------------------------------
       
   173 //
       
   174 void CCsMsgHandler::ProcessResultsL(TMsvEntry entry)
       
   175     {
       
   176     PRINT ( _L("Enter CCsMsgHandler::ProcessResultsL"));
       
   177 
       
   178     // Cleanup the conversationEntry List initially.
       
   179     // So that the previous entries not cleaned up in
       
   180     // the event of Leave have been clean up.
       
   181     iConverstationEntryList->ResetAndDestroy();
       
   182 
       
   183     RPointerArray<HBufC> addressList;
       
   184 
       
   185     // Ignore hidden items during upload
       
   186     if ( entry.Visible() == EFalse )
       
   187         {
       
   188         return;
       
   189         }
       
   190     
       
   191     if ( entry.Parent() == KMsvGlobalInBoxIndexEntryIdValue ||
       
   192          entry.Parent() == KMsvSentEntryIdValue ||
       
   193          entry.Parent() == KMsvGlobalOutBoxIndexEntryIdValue )
       
   194         {
       
   195         CleanupResetAndDestroyPushL(addressList);
       
   196         
       
   197         iMsgPluginUtility->CreateContactL(iSession, entry, addressList);
       
   198 
       
   199         //process entry
       
   200         ExtractAddressesL(entry, ERead, addressList);
       
   201         
       
   202         // Cleanup addressList
       
   203         CleanupStack::PopAndDestroy(&addressList);
       
   204         }
       
   205 
       
   206     PRINT ( _L("Exit CCsMsgHandler::ProcessResultsL") );
       
   207     }
       
   208 
       
   209 // ----------------------------------------------------------------------------
       
   210 // CCsMsgHandler::HandleSessionEventL
       
   211 // Implemented for MMsvSessionObserver
       
   212 // ----------------------------------------------------------------------------
       
   213 //
       
   214 void CCsMsgHandler::HandleSessionEventL( TMsvSessionEvent aEvent,
       
   215         TAny* aArg1,
       
   216         TAny* aArg2,
       
   217         TAny* /*aArg3*/)
       
   218     {
       
   219     PRINT1 ( _L("Enter CCsMsgHandler::HandleSessionEventL aEvent=%d"),aEvent );
       
   220 
       
   221     CMsvEntrySelection* selection=NULL;
       
   222     TMsvId parent;
       
   223 
       
   224     //args
       
   225     if(aArg1 == NULL || aArg2 == NULL)
       
   226         {
       
   227         PRINT ( _L("Enter CCsMsgHandler::HandleSessionEventL arguments invalid"));
       
   228         return;
       
   229         }
       
   230 
       
   231     //start, processing the event
       
   232     selection= (CMsvEntrySelection*)aArg1;
       
   233     parent = *(TMsvId*)aArg2;
       
   234 
       
   235     // Cleanup the conversationEntry List initially.
       
   236     iConverstationEntryList->ResetAndDestroy();
       
   237 
       
   238     switch( aEvent )
       
   239         {
       
   240         case EMsvEntriesChanged:
       
   241         case EMsvEntriesMoved:
       
   242             {
       
   243             HandleEventL(selection,parent);
       
   244             }
       
   245             break;
       
   246             
       
   247         case EMsvEntriesDeleted:
       
   248             {
       
   249             if ( parent == KMsvGlobalInBoxIndexEntryIdValue || 
       
   250                  parent == KMsvSentEntryIdValue ||
       
   251                  parent == KMsvGlobalOutBoxIndexEntryIdValue)
       
   252                 {
       
   253                 for( TInt i=0 ; i < selection->Count() ; i++ )
       
   254                     {
       
   255                     TMsvId id = selection->At( i );
       
   256 
       
   257                     //use utility to create conversation entry
       
   258                     CCsConversationEntry *conversationEntry = 
       
   259                     iMsgPluginUtility->CreateConversationEntryL(
       
   260                             NULL,
       
   261                             id,
       
   262                             0,
       
   263                             parent,
       
   264                             NULL,
       
   265                             ECsSendStateUnknown,
       
   266                             ECsAttributeNone,
       
   267                             0);
       
   268                     CleanupStack::PushL(conversationEntry);
       
   269                     iConverstationEntryList->AppendL( conversationEntry );
       
   270                     CleanupStack::Pop(conversationEntry);
       
   271 
       
   272                     // call observer interface for each entry
       
   273                     iMsgObserver->HandleDeleteCompleteL(iConverstationEntryList);
       
   274 
       
   275                     //cleanup before next iteration
       
   276                     iConverstationEntryList->ResetAndDestroy();
       
   277                     }
       
   278                 }
       
   279             }
       
   280             break;
       
   281             
       
   282         case EMsvMediaChanged:
       
   283             iMsgObserver->HandleRefreshCompleteL();
       
   284             break;
       
   285         }
       
   286 
       
   287     // Cleanup the conversationEntry List before function exits.
       
   288     iConverstationEntryList->ResetAndDestroy();
       
   289     PRINT ( _L("Exit CCsMsgHandler::HandleSessionEventL") );
       
   290     }
       
   291 
       
   292 // ---------------------------------------------------------------------
       
   293 // CCsMsgHandler::HandleEvent
       
   294 // Handle events
       
   295 // ---------------------------------------------------------------------
       
   296 //
       
   297 void CCsMsgHandler::HandleEventL(CMsvEntrySelection* aSelection, TMsvId aParent)
       
   298     {
       
   299     PRINT ( _L("Enter CCsMsgHandler::HandleEvent") );
       
   300     TMsvEntry entry;
       
   301     TMsvId service;
       
   302     TInt error= KErrNone;
       
   303     RPointerArray<HBufC> addressList;
       
   304 
       
   305     for( TInt i=0 ; i < aSelection->Count() ; i++ )
       
   306         {
       
   307         error = iSession->GetEntry(aSelection->At(i),service,entry);
       
   308         
       
   309         if ( entry.Visible() == EFalse )
       
   310             {
       
   311             // Do a delete if entry becomes invisible.
       
   312             // e.g) My Nokia registration messages.
       
   313             RPointerArray<CCsConversationEntry>* hiddenEntries =  
       
   314                     new (ELeave) RPointerArray<CCsConversationEntry>();
       
   315             
       
   316             CCsConversationEntry *conversationEntry = 
       
   317                     iMsgPluginUtility->CreateConversationEntryL ( 
       
   318                             NULL,
       
   319                             entry.Id(),
       
   320                             0,
       
   321                             aParent,
       
   322                             NULL,
       
   323                             ECsSendStateUnknown,
       
   324                             ECsAttributeNone,
       
   325                             0 );  
       
   326             CleanupStack::PushL(conversationEntry);
       
   327             hiddenEntries->AppendL( conversationEntry );
       
   328             CleanupStack::Pop(conversationEntry);
       
   329             
       
   330             // Delete at server
       
   331             iMsgObserver->HandleDeleteCompleteL(hiddenEntries);
       
   332             hiddenEntries->ResetAndDestroy();
       
   333             hiddenEntries->Close();
       
   334             delete hiddenEntries;
       
   335             
       
   336             break;
       
   337             }
       
   338         
       
   339         if ( (KErrNone == error) && (IsMtmSupported(entry.iMtm.iUid) ) )
       
   340             {
       
   341             // sent/inbox/Outbox entry
       
   342             if ( aParent == KMsvSentEntryIdValue ||
       
   343                  aParent == KMsvGlobalInBoxIndexEntryIdValue )
       
   344                 {
       
   345                 CleanupResetAndDestroyPushL(addressList);
       
   346                 // currently server needs number, so getting it from header
       
   347                 iMsgPluginUtility->CreateContactL(iSession, entry,
       
   348                         addressList);
       
   349 
       
   350                 //process entry
       
   351                 // entry created in sent already exists, hence an update
       
   352                 ExtractAddressesL(entry, EUpdate, addressList);
       
   353 
       
   354                 CleanupStack::PopAndDestroy(&addressList);
       
   355                 }  
       
   356                 
       
   357             //for drafts and outbox, the entry changes multiple times,
       
   358             // so making a check if entry has changed
       
   359             else if(aParent == KMsvGlobalOutBoxIndexEntryIdValue)
       
   360                 {
       
   361                 //outbox
       
   362                 if(!(iMsgPluginUtility->CompareEntry(iPrevEntry,entry,aParent)))
       
   363                     {
       
   364                     TUint sendState = entry.SendingState();
       
   365                     
       
   366                     // if the entry state is sent, we dont need to process it
       
   367                     // as it is going to appear in sent items
       
   368                     if( ECsSendStateSent != iMsgPluginUtility->GetSendState(entry) )
       
   369                         {
       
   370                          CleanupResetAndDestroyPushL(addressList);
       
   371                         // currently server needs number, so getting it from header
       
   372                         iMsgPluginUtility->CreateContactL(iSession, entry,
       
   373                                 addressList);
       
   374                         
       
   375                         //process entry
       
   376                         // entry created in sent already exists, hence an update
       
   377                         ExtractAddressesL(entry, EUpdate, addressList);
       
   378                         
       
   379                         iPrevEntry = entry;
       
   380                         
       
   381                         CleanupStack::PopAndDestroy(&addressList);
       
   382                         }
       
   383                     }//end check changed entry
       
   384                 }
       
   385             }
       
   386         }//end for loop
       
   387 
       
   388     PRINT ( _L("Exit CCsMsgHandler::HandleEvent") );
       
   389     }
       
   390 
       
   391 // ----------------------------------------------------------------------------
       
   392 // CCsMsgHandler::CreateAndAddEntryL
       
   393 // Creates CCsConversationEntry and adds to the list
       
   394 // ----------------------------------------------------------------------------
       
   395 //
       
   396 void CCsMsgHandler::CreateAndAddEntryL(const TDesC& aContact, 
       
   397         const TDesC& aDescription, 
       
   398         const TMsvEntry& aEntry)
       
   399     {
       
   400     PRINT ( _L("Enter CCsMsgHandler::CreateAndAddEntryL") );
       
   401 
       
   402     HBufC* contact = NULL;
       
   403     HBufC* sDescription = NULL;
       
   404 
       
   405     if (aContact.Length()>0)
       
   406         {
       
   407         contact = aContact.AllocLC();
       
   408         }
       
   409     if (aDescription.Length()>0)
       
   410         {
       
   411         sDescription = aDescription.AllocLC();
       
   412         }
       
   413 
       
   414     //use utility to create conversation entry
       
   415     CCsConversationEntry *conversationEntry = iMsgPluginUtility->CreateConversationEntryL(
       
   416             contact,
       
   417             aEntry.Id(),
       
   418             aEntry.iDate.Int64(),
       
   419             aEntry.Parent(),
       
   420             sDescription,
       
   421             iMsgPluginUtility->GetSendState(aEntry),
       
   422             iMsgPluginUtility->GetMsgAttributes(aEntry),
       
   423             ExtractCsType(aEntry));
       
   424     CleanupStack::PushL(conversationEntry);
       
   425 
       
   426     //add to the list
       
   427     iConverstationEntryList->AppendL( conversationEntry );
       
   428     CleanupStack::Pop(conversationEntry);
       
   429 
       
   430     // cleanup
       
   431     if (sDescription)
       
   432         {
       
   433         CleanupStack::PopAndDestroy(sDescription);
       
   434         }
       
   435     if (contact)
       
   436         {
       
   437         CleanupStack::PopAndDestroy(contact);
       
   438         }
       
   439 
       
   440     PRINT ( _L("Exit CCsMsgHandler::CreateAndAddEntryL") );
       
   441     }
       
   442 
       
   443 // ----------------------------------------------------------------------------
       
   444 // CCsMsgHandler::ProcessEntryL
       
   445 // Creates CCsConversationEntry and adds to the list
       
   446 // ----------------------------------------------------------------------------
       
   447 //
       
   448 void CCsMsgHandler::ProcessEntryL( TEventType aEvent, const TDesC& aContact, 
       
   449         const TDesC& aDescription, const TMsvEntry& aEntry)
       
   450     {
       
   451     PRINT ( _L("Enter CCsMsgHandler::ProcessEntryL") );
       
   452 
       
   453     // create and add entry to the list
       
   454     CreateAndAddEntryL(aContact,aDescription,aEntry);
       
   455 
       
   456     // call cs observer interface for each entry
       
   457     switch(aEvent)
       
   458         {
       
   459         case ERead:
       
   460             {
       
   461             iMsgObserver->HandleReadCompleteL(iConverstationEntryList);
       
   462             }
       
   463             break;
       
   464         case EUpdate:
       
   465             {
       
   466             iMsgObserver->HandleUpdateCompleteL(iConverstationEntryList);
       
   467             }
       
   468             break;
       
   469         }
       
   470 
       
   471     // Cleanup the conversationEntry List, as we are just sending one entry at a time
       
   472     iConverstationEntryList->ResetAndDestroy();
       
   473 
       
   474     PRINT ( _L("Exit CCsMsgHandler::ProcessEntryL") );
       
   475     }
       
   476 
       
   477 // ----------------------------------------------------------------------------
       
   478 // CCsMsgHandler::ExtractAddressL
       
   479 // Extracts the addresses in the to field and updates them to server
       
   480 // ----------------------------------------------------------------------------
       
   481 //
       
   482 void CCsMsgHandler::ExtractAddressesL( 
       
   483         TMsvEntry aEntry, 
       
   484         TEventType aEvent, 
       
   485         RPointerArray<HBufC>& addressList )
       
   486     {
       
   487     PRINT ( _L("Enter CCsMsgHandler::ExtractAddressesL") );
       
   488 
       
   489     // For SMS read the whole body.
       
   490     // For bio type SMS read the iDescription
       
   491     // For other message types read the iDescription
       
   492     // Note: After the LoadMessageL() the iDescription is getting 
       
   493     // deleted so need to make a copy before that.
       
   494     TPtrC description;
       
   495     HBufC* tmpBuffer = NULL;
       
   496 
       
   497     if ( (aEntry.iBioType && 
       
   498 	      aEntry.iBioType != KUidMsgSubTypeMmsAudioMsg.iUid ) || 
       
   499 		  aEntry.iMtm == KSenduiMtmBtUid ) 
       
   500         {
       
   501         tmpBuffer = aEntry.iDescription.AllocLC();
       
   502         description.Set( tmpBuffer->Des() );         
       
   503         }    
       
   504     else if ( aEntry.iMtm == KSenduiMtmSmsUid )  
       
   505         {
       
   506         iSmsMtm->SwitchCurrentEntryL( aEntry.Id() );
       
   507         iSmsMtm->LoadMessageL();
       
   508             
       
   509         CRichText& body = iSmsMtm->Body();
       
   510         TInt smsLength = body.DocumentLength();      
       
   511         tmpBuffer = HBufC::NewLC(smsLength);
       
   512         TPtr ptr(tmpBuffer->Des());
       
   513         body.Extract(ptr, 0);
       
   514         description.Set( tmpBuffer->Des() );  
       
   515         }
       
   516     else if ( aEntry.iMtm == KSenduiMtmMmsUid  || 
       
   517 	          aEntry.iMtm == KSenduiMMSNotificationUid)  
       
   518         {
       
   519         tmpBuffer = aEntry.iDescription.AllocLC();
       
   520         description.Set( tmpBuffer->Des() );         
       
   521 
       
   522         iMmsMtm->SwitchCurrentEntryL( aEntry.Id() );
       
   523         iMmsMtm->LoadMessageL();
       
   524 
       
   525         // Handle addresses
       
   526         if ( aEntry.Parent() == KMsvGlobalInBoxIndexEntryIdValue )
       
   527             {
       
   528             HBufC* address = iMmsMtm->Sender().AllocL();
       
   529             addressList.Append(address);
       
   530             }
       
   531         else if ( aEntry.Parent() == KMsvSentEntryIdValue ||
       
   532                   aEntry.Parent() == KMsvGlobalOutBoxIndexEntryIdValue )
       
   533             {
       
   534             const CMsvRecipientList& addresses = iMmsMtm->AddresseeList();
       
   535             TInt count = addresses.Count();
       
   536 
       
   537             for( TInt i = 0; i < count; i++)
       
   538                 {
       
   539                 HBufC* address = addresses[i].AllocL();
       
   540                 TPtrC pureAddress = iMsgPluginUtility->PureAddress(*address);
       
   541                 HBufC* address1 = pureAddress.AllocL();
       
   542                 addressList.Append(address1);
       
   543                 delete address;
       
   544                 }
       
   545             }
       
   546         }
       
   547     else
       
   548         {
       
   549         return;
       
   550         }
       
   551 
       
   552     if ( addressList.Count() > 0 )
       
   553         {
       
   554         for ( int i = 0; i < addressList.Count(); i++ )
       
   555             {
       
   556             HBufC* address = addressList[i];
       
   557             ProcessEntryL(aEvent, *address, description, aEntry);
       
   558             }
       
   559         }
       
   560     else
       
   561         {
       
   562         // Unknown
       
   563         ProcessEntryL(aEvent, KNullDesC, description, aEntry);       
       
   564         }
       
   565     
       
   566     //Cleanup tmpBuffer
       
   567     if ( tmpBuffer )
       
   568         {
       
   569 		 CleanupStack::PopAndDestroy();
       
   570         }
       
   571 
       
   572     PRINT ( _L("Exit CCsMsgHandler::ExtractAddressesL") );
       
   573     }
       
   574 
       
   575 // -----------------------------------------------------------------------------
       
   576 // CCsMsgHandler::CompareOrder()
       
   577 // Method for determining the sorting behaviour of RArray of TMsvId's  
       
   578 // -----------------------------------------------------------------------------
       
   579 //
       
   580 static TInt CompareOrder(const TMsvId& aFirst, const TMsvId& aSecond)
       
   581     {
       
   582     return aSecond - aFirst;
       
   583     }
       
   584 // -----------------------------------------------------------------------------
       
   585 // CCsMsgHandler::UploadMsgL()
       
   586 // State machine to upload all messages 
       
   587 // -----------------------------------------------------------------------------
       
   588 //
       
   589 TInt CCsMsgHandler::UploadMsgL() 
       
   590     {
       
   591     //Release scheduler for scheduling.    
       
   592     User::After(1);
       
   593     
       
   594     switch ( iState ) 
       
   595         {
       
   596         case EReadInbox:
       
   597             {
       
   598             UpdateMessageArrayL(KMsvGlobalInBoxIndexEntryId);
       
   599             iState = EReadSent;
       
   600             return 1;
       
   601             }
       
   602 
       
   603         case EReadSent:
       
   604             {
       
   605             UpdateMessageArrayL(KMsvSentEntryId);
       
   606             iState = EReadOutbox;
       
   607             return 1;
       
   608             }
       
   609 
       
   610         case EReadOutbox:
       
   611             {
       
   612             UpdateMessageArrayL(KMsvGlobalOutBoxIndexEntryId);
       
   613             iState = ESortEntries;            
       
   614             return 1;
       
   615             }
       
   616         case ESortEntries:
       
   617             {
       
   618             //Sort the elements in the array by descending order of TMsvId's
       
   619             TLinearOrder<TMsvId> order(CompareOrder);
       
   620             iMessageArray->Sort(order);
       
   621             iState = EProcessEntries;
       
   622             return 1;
       
   623             }            
       
   624         case EProcessEntries:
       
   625             { 
       
   626             //Process one entry at a time in sequence
       
   627             //Process the first element in the array on each call, till the end
       
   628             if(iMessageArray->Count())
       
   629                 {
       
   630                 CMsvEntry* msvEntry= iSession->
       
   631                         GetEntryL(iMessageArray->operator[](0));
       
   632                 CleanupStack::PushL(msvEntry);
       
   633                 ProcessResultsL(msvEntry->Entry());
       
   634                 CleanupStack::PopAndDestroy(msvEntry);
       
   635                 iMessageArray->Remove(0);
       
   636                 }
       
   637             else
       
   638                 {
       
   639                 iMessageArray->Reset();
       
   640                 iMsgObserver->HandleCachingCompleted();
       
   641                 return 0; //DONE 
       
   642                 }            
       
   643             iState = EProcessEntries;
       
   644             return 1; 
       
   645         }
       
   646     }
       
   647     return 0;    
       
   648 }
       
   649 
       
   650 // -----------------------------------------------------------------------------
       
   651 // CCsMsgHandler::UploadMsg()
       
   652 // CIdle callback 
       
   653 // -----------------------------------------------------------------------------
       
   654 //
       
   655 TInt CCsMsgHandler::UploadMsg(TAny* aArg)
       
   656     {
       
   657     CCsMsgHandler* handler = (CCsMsgHandler*) aArg; 
       
   658     TInt ok = 0;
       
   659     TRAPD(err, ok = handler->UploadMsgL());
       
   660     return ((err == KErrNone) && ok);
       
   661     }
       
   662 
       
   663 // -----------------------------------------------------------------------------
       
   664 // CCsMsgHandler::StartL()
       
   665 // This function starts the state machine to fetch sms data from msvserver
       
   666 // -----------------------------------------------------------------------------
       
   667 //
       
   668 void CCsMsgHandler::StartL()
       
   669     {
       
   670     PRINT ( _L("Enter CCsMsgHandler::Start") );
       
   671 
       
   672     iState = EReadInbox;
       
   673     TCallBack callback = TCallBack(UploadMsg, (TAny*) this);
       
   674     
       
   675     if(iIdle)
       
   676         {
       
   677         delete iIdle;
       
   678         iIdle = NULL;
       
   679         }
       
   680     iIdle = CIdle::NewL(CActive::EPriorityIdle);
       
   681     iIdle->Start(callback);
       
   682 
       
   683     PRINT ( _L("End CCsMsgHandler::Start") );
       
   684     }
       
   685 
       
   686 // -----------------------------------------------------------------------------
       
   687 // CCsMsgHandler::IsMtmSupported()
       
   688 // 
       
   689 // -----------------------------------------------------------------------------
       
   690 //
       
   691 TBool CCsMsgHandler::IsMtmSupported(long uid)	
       
   692     {
       
   693     if ( KSenduiMtmSmsUidValue == uid || \
       
   694          KSenduiMtmBtUidValue == uid || \
       
   695          KSenduiMtmMmsUidValue == uid || \
       
   696          KSenduiMtmBioUidValue == uid  || \
       
   697          KSenduiMMSNotificationUidValue == uid || \
       
   698          KUidMtmWapPush ==  TUid::Uid(uid) )
       
   699         {
       
   700         return ETrue;
       
   701         }	
       
   702     return EFalse;
       
   703     }
       
   704 
       
   705 // -----------------------------------------------------------------------------
       
   706 // CCsMsgHandler::ExtractCsType()
       
   707 // Extracts the Message type based on the MTM Uid 
       
   708 // -----------------------------------------------------------------------------
       
   709 //
       
   710 TCsType CCsMsgHandler::ExtractCsType( const TMsvEntry& aEntry)
       
   711     {
       
   712     TCsType type = ECsUnknown;
       
   713     switch(aEntry.iMtm.iUid)
       
   714         {
       
   715         case KSenduiMtmSmsUidValue:            
       
   716             type = ECsSMS;
       
   717             if (aEntry.iBioType == KMsgBioUidVCard.iUid)
       
   718                 {
       
   719                 type = ECsBioMsg_VCard;
       
   720                 }
       
   721             break;
       
   722         case KSenduiMtmBtUidValue:
       
   723             type = ECsBlueTooth;
       
   724             break;
       
   725         case KSenduiMtmMmsUidValue:        
       
   726            if(aEntry.iBioType == KUidMsgSubTypeMmsAudioMsg.iUid)
       
   727                 {
       
   728                 type = ECsAudio;
       
   729                 }
       
   730             else
       
   731                 {
       
   732                 type = ECsMMS;
       
   733                 }
       
   734             break;
       
   735         case KSenduiMMSNotificationUidValue:            
       
   736             type = ECsMmsNotification;
       
   737             break;
       
   738         case KSenduiMtmBioUidValue:
       
   739             { 
       
   740             type = ECsBioMsg; 
       
   741             
       
   742             // based on the biotype uid set message type
       
   743             if(aEntry.iBioType == KMsgBioUidRingingTone.iUid)
       
   744                 {
       
   745                 type = ECsRingingTone;
       
   746                 }
       
   747             else if(aEntry.iBioType == KMsgBioProvisioningMessage.iUid)
       
   748                 {
       
   749                 type = ECsProvisioning;
       
   750                 }
       
   751 		    else if (aEntry.iBioType == KMsgBioUidVCard.iUid)
       
   752                 {
       
   753                 type = ECsBioMsg_VCard;
       
   754                 }
       
   755             else if (aEntry.iBioType == KMsgBioUidVCalendar.iUid)
       
   756                 {
       
   757                 type = ECsBioMsg_VCal;
       
   758        		 }
       
   759        		 else if (aEntry.iBioType == KMsgBioNokiaServiceMessage.iUid) {
       
   760             	type = ECsBioMgs_NokiaService;
       
   761         }
       
   762     }
       
   763         break;
       
   764     default:
       
   765         type = ECsUnknown;
       
   766         break;
       
   767     }
       
   768     return (type);
       
   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     }
       
   805 // End of file
       
   806