messagingapp/msgappfw/server/src/ccssession.cpp
changeset 31 ebfee66fde93
child 34 84197e66a4bd
equal deleted inserted replaced
30:6a20128ce557 31:ebfee66fde93
       
     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 Session class
       
    15  *
       
    16  */
       
    17 
       
    18 // INCLUDE FILES
       
    19 #include <ccsconversationentry.h>
       
    20 #include <ccsclientconversation.h>
       
    21 
       
    22 #include "ccsconversationevent.h"
       
    23 #include "ccsdebug.h"
       
    24 #include "ccsserver.h"
       
    25 #include "ccssession.h"
       
    26 #include "ccsplugininterface.h"
       
    27 #include "ccsconversationcache.h"
       
    28 #include "ccscontactsresolver.h"
       
    29 #include "ccsconversationdeletehandler.h"
       
    30 #include "ccsconversationmarkreadhandler.h"
       
    31 
       
    32 // CONSTANTS
       
    33 const TInt KTinyBuffer = 256; // 256 bytes
       
    34 const TInt KBigBuffer = 2048; // 2K
       
    35 
       
    36 // ============================== MEMBER FUNCTIONS ============================
       
    37 
       
    38 // ----------------------------------------------------------------------------
       
    39 // CCsSession::NewL
       
    40 // Two Phase Construction
       
    41 // ----------------------------------------------------------------------------
       
    42 CCsSession* CCsSession::NewL(CCsServer* aServer)
       
    43 {
       
    44     PRINT ( _L("Enter CCsSession::NewL") );
       
    45 
       
    46     CCsSession* self = new (ELeave) CCsSession(aServer);
       
    47     CleanupStack::PushL(self);
       
    48     self->ConstructL();
       
    49     CleanupStack::Pop(self); // self
       
    50 
       
    51     PRINT ( _L("End CCsSession::NewL") );
       
    52 
       
    53     return self;
       
    54 }
       
    55 
       
    56 // ----------------------------------------------------------------------------
       
    57 // CCsSession::CCsSession
       
    58 // Construtor
       
    59 // ----------------------------------------------------------------------------
       
    60 CCsSession::CCsSession(CCsServer* aServer) :
       
    61     iServer(aServer)
       
    62 {
       
    63 }
       
    64 
       
    65 // ----------------------------------------------------------------------------
       
    66 // CCsSession::ConstructL
       
    67 // Second phase constructor
       
    68 // ----------------------------------------------------------------------------
       
    69 void CCsSession::ConstructL()
       
    70 {
       
    71     PRINT ( _L("Enter CCsSession::ConstructL") );
       
    72 
       
    73     iBufferOverflow = EFalse;
       
    74     iGetConversationBufferOverflow = EFalse;
       
    75     des = NULL;
       
    76 
       
    77     iNotifyHandling = EFalse;
       
    78     iConversationListChangeObserver = EFalse;
       
    79     iConversationChangeObserver = EFalse;
       
    80     iCachingChangeObserver = EFalse;
       
    81     iMonitoredConversation = NULL;
       
    82 
       
    83     // initialize the event List
       
    84     iEventList = new (ELeave) RPointerArray<CCsConversationEvent> ();
       
    85 
       
    86     iReqCnt = 1; //Let's start the event ID from 1
       
    87 
       
    88     PRINT ( _L("End CCsSession::ConstructL") );
       
    89 }
       
    90 
       
    91 // ----------------------------------------------------------------------------
       
    92 // CCsSession::CCsSession
       
    93 // Destructor
       
    94 // ----------------------------------------------------------------------------
       
    95 CCsSession::~CCsSession()
       
    96 {
       
    97     PRINT ( _L("Enter CCsSession::~CCsSession") );
       
    98 
       
    99     if (des)
       
   100     {
       
   101         delete des;
       
   102         des = NULL;
       
   103     }
       
   104 
       
   105     if (iEventList)
       
   106     {
       
   107         iEventList->ResetAndDestroy();
       
   108         iEventList->Close();
       
   109         delete iEventList;
       
   110         iEventList = NULL;
       
   111     }
       
   112 
       
   113     if (iMonitoredConversation)
       
   114     {
       
   115         delete iMonitoredConversation;
       
   116         iMonitoredConversation = NULL;
       
   117     }
       
   118 
       
   119     PRINT ( _L("End CCsSession::~CCsSession") );
       
   120 }
       
   121 
       
   122 // ----------------------------------------------------------------------------
       
   123 // CCsSession::ServiceL
       
   124 //
       
   125 // ----------------------------------------------------------------------------
       
   126 void CCsSession::ServiceL(const RMessage2& aMessage)
       
   127 {
       
   128     TInt errStatus = KErrNone;
       
   129 
       
   130     // Do the service
       
   131     TRAP ( errStatus, DoServiceL(aMessage) );
       
   132 
       
   133     // Check the error status returned
       
   134     if (errStatus != KErrNone)
       
   135     {
       
   136         aMessage.Complete(errStatus);
       
   137     }
       
   138 }
       
   139 
       
   140 // ----------------------------------------------------------------------------
       
   141 // CCsSession::DoServiceL
       
   142 //
       
   143 // ----------------------------------------------------------------------------
       
   144 void CCsSession::DoServiceL(const RMessage2& aMessage)
       
   145 {
       
   146     switch (aMessage.Function())
       
   147     {
       
   148         case EGetConversationList:
       
   149             PRINT ( _L("Received function EGetConversationList") )
       
   150             GetConversationListL(aMessage);
       
   151             break;
       
   152             
       
   153         case EGetConversationUnreadList:
       
   154             PRINT ( _L("Received function EGetConversationUnreadList") )
       
   155             GetConversationUnreadListL(aMessage);
       
   156             break;
       
   157 
       
   158         case EGetConversations:
       
   159             PRINT ( _L("Received function EGetConversations") )
       
   160             GetConversationsL(aMessage);
       
   161             break;
       
   162             
       
   163         case EGetTotalUnreadCount:
       
   164             PRINT ( _L("Received function EGetTotalUnreadCount") )
       
   165             GetTotalUnreadCountL(aMessage);
       
   166             break;
       
   167 
       
   168         case ERequestChangeEvent:
       
   169             PRINT ( _L("Received function ERequestChangeEvent") )
       
   170             RequestChangeEventL(aMessage);
       
   171             break;
       
   172 
       
   173         case ERemoveChangeEvent:
       
   174             PRINT ( _L("Received function ERemoveChangeEvent") )
       
   175             RemoveChangeEventL(aMessage);
       
   176             break;
       
   177 
       
   178         case ESetConversationListChangeObserver:
       
   179             PRINT ( _L("Received function ESetConversationListChangeObserver") )
       
   180             SetConversationListChangeObserverL(aMessage);
       
   181             break;
       
   182 
       
   183         case EResetConversationListChangeObserver:
       
   184             PRINT ( _L("Received function EResetConversationListChangeObserver") )
       
   185             ResetConversationListChangeObserverL(aMessage);
       
   186             break;
       
   187 
       
   188         case ESetConversationChangeObserver:
       
   189             PRINT ( _L("Received function ESetConversationChangeObserver") )
       
   190             SetConversationChangeObserverL(aMessage);
       
   191             break;
       
   192 
       
   193         case EResetConversationChangeObserver:
       
   194             PRINT ( _L("Received function EResetConversationChangeObserver") )
       
   195             ResetConversationChangeObserverL(aMessage);
       
   196             break;
       
   197 
       
   198         case ESetCachingStatusObserver:
       
   199             PRINT ( _L("Received function ESetCachingStatusObserver") )
       
   200             SetCachingStatusObserverL(aMessage);
       
   201             break;
       
   202 
       
   203         case EResetCachingStatusObserver:
       
   204             PRINT ( _L("Received function EResetCachingStatusObserver") )
       
   205             ResetCachingStatusObserverL(aMessage);
       
   206             break;
       
   207 
       
   208         case EGetCachingStatus:
       
   209             GetCachingStatusL(aMessage);
       
   210             break;
       
   211 
       
   212         case EShutdown:
       
   213             PRINT ( _L("Received function EShutdown") )
       
   214             ShutdownServerL(aMessage);
       
   215             break;
       
   216 
       
   217         case EUserDeleteConversation:
       
   218             PRINT ( _L("Received function EDeleteConversation") )
       
   219             DeleteConversationL(aMessage);
       
   220             break;
       
   221 
       
   222         case EGetConversationId:
       
   223             PRINT ( _L("Received function EGetConversationId") )
       
   224             GetConversationIdL(aMessage);
       
   225             break;
       
   226             
       
   227         case EGetConversationIdFromAddress:
       
   228             PRINT ( _L("Received function EGetConversationIdFromAddress") )
       
   229             GetConversationIdfromAddressL(aMessage);
       
   230             break;
       
   231 			
       
   232 		case EGetConversationFromMessageId:
       
   233             PRINT ( _L("Received function EGetConversationFromMessageId") )
       
   234             GetConversationFromMessageIdL(aMessage);
       
   235             break;
       
   236 
       
   237         case EUserMarkReadConversation:
       
   238             PRINT ( _L("Received function EUserMarkReadConversation") )
       
   239             MarkConversationReadL(aMessage);
       
   240             break;
       
   241     }
       
   242 
       
   243     return;
       
   244 }
       
   245 
       
   246 // ----------------------------------------------------------------------------
       
   247 // CCsSession::ServiceError
       
   248 //
       
   249 // ----------------------------------------------------------------------------
       
   250 void CCsSession::ServiceError(const RMessage2& aMessage, TInt aError)
       
   251 {
       
   252     aMessage.Complete(aError);
       
   253 }
       
   254 
       
   255 // ----------------------------------------------------------------------------
       
   256 // CCsSession::GetConversationEntryListL
       
   257 // the function to handle the request of Listing
       
   258 // of recent(latest) conversation entry and
       
   259 // list of dispalyname for all stored conversation entry ID
       
   260 // ----------------------------------------------------------------------------
       
   261 void CCsSession::GetConversationListL(const RMessage2& aMessage)
       
   262 {
       
   263     PRINT ( _L("Enter CCsSession::GetConversationListL") );
       
   264     PRINT_TIMESTAMP ("Enter CCsSession::GetConversationListL");
       
   265 
       
   266     if (iBufferOverflow == EFalse)
       
   267     {
       
   268         RPointerArray<CCsClientConversation>* ClientConversationList =
       
   269                 new (ELeave) RPointerArray<CCsClientConversation> ();
       
   270 
       
   271         // get cache pointer
       
   272         CCsConversationCache* cache = iServer->ConversationCacheInterface();
       
   273 
       
   274         // Call cache function to get recent conversation entry list
       
   275         // with dispaly name for all stored conversation entry ID
       
   276         cache->GetConversationListL(ClientConversationList);
       
   277         CleanupStack::PushL(ClientConversationList);
       
   278 
       
   279         //write all list data into stream
       
   280         // create a new buffer for writing into stream
       
   281         CBufFlat* buf = CBufFlat::NewL(KBigBuffer);
       
   282         CleanupStack::PushL(buf);
       
   283 
       
   284         RBufWriteStream writeStream(*buf);
       
   285         writeStream.PushL();
       
   286 
       
   287         TInt listCount = ClientConversationList->Count();
       
   288 
       
   289         if (listCount == 0)
       
   290         {
       
   291             iConversationListChangeObserver = ETrue;
       
   292         }
       
   293 
       
   294         // write the count first
       
   295         writeStream.WriteUint16L(listCount);
       
   296 
       
   297         // now go through the list and do externalize
       
   298         for (int iloop = 0; iloop < listCount; iloop++)
       
   299         {
       
   300             CCsClientConversation
       
   301                     * ClientConversation =
       
   302                             static_cast<CCsClientConversation*> ( (*ClientConversationList)[iloop]);
       
   303             //write list of ClientConversation
       
   304             ClientConversation->ExternalizeL(writeStream);
       
   305         }
       
   306 
       
   307         // Results are already packed in the stream
       
   308         writeStream.CommitL();
       
   309 
       
   310         // --------------------------------------------------------------
       
   311         // Create a heap descriptor from the buffer
       
   312         des = HBufC8::NewLC(buf->Size());
       
   313         CleanupStack::Pop(des);
       
   314         TPtr8 ptr(des->Des());
       
   315         buf->Read(0, ptr, buf->Size());
       
   316 
       
   317         // cleanup
       
   318         CleanupStack::PopAndDestroy(2, buf); // writestream, buf
       
   319         CleanupStack::Pop(ClientConversationList);
       
   320 
       
   321         // destroy objects inside list
       
   322         ClientConversationList->ResetAndDestroy();
       
   323         ClientConversationList->Close();
       
   324         delete ClientConversationList;
       
   325         ClientConversationList = NULL;
       
   326     }
       
   327 
       
   328     TInt rcevdBufferSize = aMessage.GetDesMaxLength(1);
       
   329     TInt reqdBufferSize = des->Size();
       
   330 
       
   331     PRINT1 ( _L("Received buffer size = %d"), rcevdBufferSize );
       
   332     PRINT1 ( _L("Required buffer size = %d"), reqdBufferSize );
       
   333 
       
   334     // If the received buffer size from Client API is less than
       
   335     // the required buffer size write the required buffer size
       
   336     // and return.
       
   337     if (rcevdBufferSize < reqdBufferSize)
       
   338     {
       
   339         PRINT ( _L("In-adequate buffer received") );
       
   340         PRINT ( _L("Packing the required buffer size in response") );
       
   341 
       
   342         TPckgC<TInt> bufferSizePackage(reqdBufferSize);
       
   343         aMessage.WriteL(1, bufferSizePackage);
       
   344         aMessage.Complete(EGetConversationListBufferOverflow);
       
   345         iBufferOverflow = ETrue;
       
   346     }
       
   347     else
       
   348     {
       
   349         PRINT ( _L("Adequate buffer received") );
       
   350         PRINT ( _L("Packing the results in response") )
       
   351 
       
   352         aMessage.Write(1, *des);
       
   353         aMessage.Complete(EGetConversationListOperationComplete);
       
   354         iBufferOverflow = EFalse;
       
   355         delete des;
       
   356         des = NULL;
       
   357     }
       
   358 
       
   359     PRINT_TIMESTAMP ("End CCsSession::GetConversationListL");
       
   360     PRINT ( _L("End CCsSession::GetConversationListL") );
       
   361 }
       
   362 
       
   363 // ----------------------------------------------------------------------------
       
   364 // CCsSession::GetConversationUnreadListL
       
   365 // the function to handle the request of Listing
       
   366 // of recent(latest) unread conversation entry and
       
   367 // list of dispalyname for all stored conversation entry ID
       
   368 // ----------------------------------------------------------------------------
       
   369 void CCsSession::GetConversationUnreadListL(const RMessage2& aMessage)
       
   370     {
       
   371     PRINT ( _L("Enter CCsSession::GetConversationUnreadListL") );
       
   372     PRINT_TIMESTAMP ("Enter CCsSession::GetConversationUnreadListL");
       
   373 
       
   374     if (iBufferOverflow == EFalse)
       
   375         {
       
   376         RPointerArray<CCsClientConversation>* ClientConversationList =
       
   377                 new (ELeave) RPointerArray<CCsClientConversation> ();
       
   378 
       
   379         // get cache pointer
       
   380         CCsConversationCache* cache = iServer->ConversationCacheInterface();
       
   381 
       
   382         // Call cache function to get recent conversation entry list
       
   383         // with dispaly name for all stored conversation entry ID
       
   384         cache->GetConversationUnreadListL(ClientConversationList);
       
   385 
       
   386         CleanupStack::PushL(ClientConversationList);
       
   387 
       
   388         //write all list data into stream
       
   389         // create a new buffer for writing into stream
       
   390         CBufFlat* buf = CBufFlat::NewL(KBigBuffer);
       
   391         CleanupStack::PushL(buf);
       
   392 
       
   393         RBufWriteStream writeStream(*buf);
       
   394         writeStream.PushL();
       
   395 
       
   396         TInt listCount  = ClientConversationList->Count();
       
   397 
       
   398         if (listCount == 0)
       
   399             {
       
   400             iConversationListChangeObserver = ETrue;
       
   401             }
       
   402 
       
   403         // write the count first
       
   404         writeStream.WriteUint16L(listCount);
       
   405 
       
   406         // now go through the list and do externalize
       
   407         for (int iloop=0 ; iloop < listCount ; iloop++)
       
   408             {
       
   409             CCsClientConversation* ClientConversation =
       
   410             static_cast<CCsClientConversation*>((*ClientConversationList)[iloop]);
       
   411             //write list of ClientConversation
       
   412             ClientConversation->ExternalizeL(writeStream);
       
   413             }
       
   414 
       
   415         // Results are already packed in the stream
       
   416         writeStream.CommitL();
       
   417 
       
   418         // --------------------------------------------------------------
       
   419         // Create a heap descriptor from the buffer
       
   420         des = HBufC8::NewLC(buf->Size());
       
   421         CleanupStack::Pop(des);
       
   422         TPtr8 ptr(des->Des());
       
   423         buf->Read(0, ptr, buf->Size());
       
   424 
       
   425         // cleanup
       
   426         CleanupStack::PopAndDestroy(2, buf); // writestream, buf
       
   427         CleanupStack::Pop(ClientConversationList);
       
   428 
       
   429         // destroy objects inside list
       
   430         ClientConversationList->ResetAndDestroy();
       
   431         ClientConversationList->Close();
       
   432         delete ClientConversationList;
       
   433         ClientConversationList = NULL;
       
   434         }
       
   435 
       
   436     TInt rcevdBufferSize = aMessage.GetDesMaxLength(1);
       
   437     TInt reqdBufferSize  = des->Size();
       
   438 
       
   439     PRINT1 ( _L("Received buffer size = %d"), rcevdBufferSize );
       
   440     PRINT1 ( _L("Required buffer size = %d"), reqdBufferSize );
       
   441 
       
   442     if ( rcevdBufferSize < reqdBufferSize )
       
   443         {
       
   444         PRINT ( _L("In-adequate buffer received") );
       
   445         PRINT ( _L("Packing the required buffer size in response") );
       
   446 
       
   447         TPckgC<TInt> overflowPackage(ETrue);
       
   448         aMessage.WriteL(0, overflowPackage);
       
   449         TPckgC<TInt> bufferSizePackage(reqdBufferSize);
       
   450         aMessage.WriteL(1, bufferSizePackage);
       
   451         aMessage.Complete(KErrNone);
       
   452         iBufferOverflow = ETrue;
       
   453         }
       
   454     else
       
   455         {
       
   456         PRINT ( _L("Adequate buffer received") );
       
   457         PRINT ( _L("Packing the results in response") )
       
   458 
       
   459         TPckgC<TInt> overflowPackage(EFalse);
       
   460         aMessage.WriteL(0, overflowPackage);
       
   461         aMessage.Write(1, *des);
       
   462         aMessage.Complete(KErrNone);
       
   463         iBufferOverflow = EFalse;
       
   464         delete des;
       
   465         des = NULL;
       
   466         }
       
   467 
       
   468     PRINT_TIMESTAMP ("End CCsSession::GetConversationUnreadListL");
       
   469     PRINT ( _L("End CCsSession::GetConversationUnreadListL") );
       
   470     }
       
   471 
       
   472 // ----------------------------------------------------------------------------
       
   473 // CCsSession::GetConversationsL
       
   474 // the function to handle the request
       
   475 // of conversation entry list for one conversation entry ID
       
   476 // ----------------------------------------------------------------------------
       
   477 void CCsSession::GetConversationsL(const RMessage2& aMessage)
       
   478 {
       
   479     PRINT ( _L("Enter CCsSession::GetConversationsL") );
       
   480 
       
   481     if (iGetConversationBufferOverflow == EFalse)
       
   482     {
       
   483         // Read Contact from the message
       
   484         HBufC8* buffer = HBufC8::NewLC(KBigBuffer);
       
   485 
       
   486         TPtr8 bufferPtr(buffer->Des());
       
   487         aMessage.ReadL(0, bufferPtr);
       
   488 
       
   489         // Stream over the buffer
       
   490         RDesReadStream stream(bufferPtr);
       
   491         stream.PushL();
       
   492 
       
   493         // get cache pointer
       
   494         CCsConversationCache* cache = iServer->ConversationCacheInterface();
       
   495 
       
   496         // read the Client Conversation consist of Entry Id
       
   497         CCsClientConversation* ClientConversation =
       
   498                 CCsClientConversation::NewL();
       
   499         CleanupStack::PushL(ClientConversation);
       
   500         ClientConversation->InternalizeL(stream);
       
   501         CleanupStack::Pop(ClientConversation);
       
   502         CleanupStack::PopAndDestroy(2, buffer);//stream, buffer
       
   503 
       
   504         CleanupStack::PushL(ClientConversation);
       
   505         RPointerArray<CCsConversationEntry>* conversationEntryList =
       
   506                 new (ELeave) RPointerArray<CCsConversationEntry> ();
       
   507         CleanupStack::PushL(conversationEntryList);
       
   508 
       
   509         // get conversationlist for given ClientConversation 
       
   510         cache->GetConversationsL(ClientConversation, conversationEntryList);
       
   511 
       
   512         // create a new buffer for writing into stream
       
   513         // write all list data into stream
       
   514         CBufFlat* buf = CBufFlat::NewL(KBigBuffer);
       
   515         CleanupStack::PushL(buf);
       
   516 
       
   517         RBufWriteStream writeStream(*buf);
       
   518         writeStream.PushL();
       
   519 
       
   520         TInt ItemCount = conversationEntryList->Count();
       
   521         //write  recent conversation entry list
       
   522         writeStream.WriteInt32L(ItemCount);
       
   523 
       
   524         // Write the conversation entry
       
   525         for (TInt iloop = 0; iloop < ItemCount; iloop++)
       
   526         {
       
   527             CCsConversationEntry
       
   528                     * entry =
       
   529                             static_cast<CCsConversationEntry*> ( (*conversationEntryList)[iloop]);
       
   530             entry->ExternalizeL(writeStream);
       
   531         }
       
   532 
       
   533         // Results are already packed in the stream
       
   534         writeStream.CommitL();
       
   535 
       
   536         // --------------------------------------------------------------
       
   537         // Create a heap descriptor from the buffer
       
   538         des = HBufC8::NewLC(buf->Size());
       
   539         CleanupStack::Pop(des);
       
   540         TPtr8 ptr(des->Des());
       
   541         buf->Read(0, ptr, buf->Size());
       
   542 
       
   543         CleanupStack::PopAndDestroy(2, buf); // writestream, buf
       
   544         CleanupStack::Pop(conversationEntryList);
       
   545 
       
   546         // Cleanup
       
   547         conversationEntryList->ResetAndDestroy();
       
   548         conversationEntryList->Close();
       
   549         delete conversationEntryList;
       
   550         conversationEntryList = NULL;
       
   551         CleanupStack::PopAndDestroy(ClientConversation);
       
   552     }
       
   553 
       
   554     TInt rcevdBufferSize = aMessage.GetDesMaxLength(1);
       
   555     TInt reqdBufferSize = des->Size();
       
   556 
       
   557     PRINT1 ( _L("Received buffer size = %d"), rcevdBufferSize );
       
   558     PRINT1 ( _L("Required buffer size = %d"), reqdBufferSize );
       
   559 
       
   560     // If the received buffer size from Client API is less than
       
   561     // the required buffer size write the required buffer size
       
   562     if (rcevdBufferSize < reqdBufferSize)
       
   563     {
       
   564         PRINT ( _L("In-adequate buffer received") );
       
   565         PRINT ( _L("Packing the required buffer size in response") );
       
   566 
       
   567         TPckgC<TInt> bufferSizePackage(reqdBufferSize);
       
   568         aMessage.WriteL(1, bufferSizePackage);
       
   569         aMessage.Complete(EGetConversationBufferOverflow);
       
   570         iGetConversationBufferOverflow = ETrue;
       
   571     }
       
   572     else
       
   573     {
       
   574         PRINT ( _L("Adequate buffer received") );
       
   575         PRINT ( _L("Packing the results in response") )
       
   576 
       
   577         aMessage.Write(1, *des);
       
   578         aMessage.Complete(EGetConversationOperationComplete);
       
   579         iGetConversationBufferOverflow = EFalse;
       
   580         delete des;
       
   581         des = NULL;
       
   582     }
       
   583 
       
   584     PRINT ( _L("End CCsSession::GetConversationsL") );
       
   585 }
       
   586 
       
   587 // ----------------------------------------------------------------------------
       
   588 // CCsSession::ShutdownServerL
       
   589 // Stops the scheduler
       
   590 // ----------------------------------------------------------------------------
       
   591 void CCsSession::ShutdownServerL(const RMessage2& aMessage)
       
   592 {
       
   593     aMessage.Complete(KErrNone);
       
   594     CActiveScheduler::Stop();
       
   595 
       
   596     PRINT ( _L("CCsSession::ShutdownServerL - Server ShutDown") );
       
   597 }
       
   598 
       
   599 // ----------------------------------------------------------------------------
       
   600 // CCsSession::HandleNewConversationListEventL
       
   601 // Notify client about new conversation event
       
   602 // ----------------------------------------------------------------------------
       
   603 void CCsSession::HandleNewConversationListEventL(
       
   604                                                  CCsClientConversation* aClientConversation)
       
   605 {
       
   606     PRINT ( _L("Enter CCsSession::HandleNewConversationListEventL") );
       
   607 
       
   608     if (!iConversationListChangeObserver)
       
   609         return;
       
   610 
       
   611     if (! (iNotifyHandling))
       
   612     {
       
   613         //append in notify list
       
   614         CCsConversationEvent* conversationEvent = CCsConversationEvent::NewL();
       
   615         CleanupStack::PushL(conversationEvent);
       
   616         conversationEvent->SetClientConversationL(*aClientConversation);
       
   617         conversationEvent->SetEvent(KConversationListEventNew);
       
   618         iEventList->AppendL(conversationEvent);
       
   619         CleanupStack::Pop(conversationEvent);
       
   620     }
       
   621     else
       
   622     {
       
   623         // create a new buffer for writing into stream
       
   624         CBufFlat* buf = CBufFlat::NewL(KBigBuffer);
       
   625         CleanupStack::PushL(buf);
       
   626 
       
   627         RBufWriteStream writeStream(*buf);
       
   628         writeStream.PushL();
       
   629 
       
   630         //externalize ClientConversation
       
   631         aClientConversation->ExternalizeL(writeStream);
       
   632 
       
   633         // Results are already packed in the stream
       
   634         writeStream.CommitL();
       
   635 
       
   636         // --------------------------------------------------------------
       
   637         // Create a heap descriptor from the buffer
       
   638         HBufC8* notifyDes = HBufC8::NewLC(buf->Size());
       
   639         TPtr8 ptr(notifyDes->Des());
       
   640         buf->Read(0, ptr, buf->Size());
       
   641 
       
   642         iAsyncReqRMessage.Write(1, *notifyDes);
       
   643         iAsyncReqRMessage.Complete(EAddConversationListEvent);
       
   644         CleanupStack::PopAndDestroy(3, buf); // notifyDes, writestream, buf 
       
   645         iNotifyHandling = EFalse;
       
   646     }
       
   647 
       
   648     PRINT ( _L("End CCsSession::HandleNewConversationListEventL") );
       
   649 }
       
   650 
       
   651 // ----------------------------------------------------------------------------
       
   652 // CCsSession::HandleDeleteConversationListEventL
       
   653 // Notify client about delete conversation event
       
   654 // ----------------------------------------------------------------------------
       
   655 void CCsSession::HandleDeleteConversationListEventL(
       
   656                                                     CCsClientConversation* aClientConversation)
       
   657 {
       
   658     PRINT ( _L("Enter CCsSession::HandleDeleteConversationListEventL") );
       
   659 
       
   660     if (!iConversationListChangeObserver)
       
   661         return;
       
   662 
       
   663     if (! (iNotifyHandling))
       
   664     {
       
   665         //append in notify list
       
   666         CCsConversationEvent* conversationEvent = CCsConversationEvent::NewL();
       
   667         conversationEvent->SetClientConversationL(*aClientConversation);
       
   668         CleanupStack::PushL(conversationEvent);
       
   669         conversationEvent->SetEvent(KConversationListEventDelete);
       
   670         iEventList->AppendL(conversationEvent);
       
   671         CleanupStack::Pop(conversationEvent);
       
   672     }
       
   673     else
       
   674     {
       
   675         // create a new buffer for writing into stream
       
   676         CBufFlat* buf = CBufFlat::NewL(KBigBuffer);
       
   677         CleanupStack::PushL(buf);
       
   678 
       
   679         RBufWriteStream writeStream(*buf);
       
   680         writeStream.PushL();
       
   681 
       
   682         //externalize ClientConversation
       
   683         aClientConversation->ExternalizeL(writeStream);
       
   684 
       
   685         // Results are already packed in the stream
       
   686         writeStream.CommitL();
       
   687 
       
   688         // --------------------------------------------------------------
       
   689         // Create a heap descriptor from the buffer
       
   690         HBufC8* notifyDes = HBufC8::NewLC(buf->Size());
       
   691         TPtr8 ptr(notifyDes->Des());
       
   692         buf->Read(0, ptr, buf->Size());
       
   693 
       
   694         iAsyncReqRMessage.Write(1, *notifyDes);
       
   695         iAsyncReqRMessage.Complete(EDeleteConversationListEvent);
       
   696         CleanupStack::PopAndDestroy(3, buf); // notifyDes, writestream, buf
       
   697         iNotifyHandling = EFalse;
       
   698     }
       
   699 
       
   700     PRINT ( _L("End CCsSession::HandleDeleteConversationListEventL") );
       
   701 }
       
   702 
       
   703 // ----------------------------------------------------------------------------
       
   704 // CCsSession::HandleModifyConversationListEventL
       
   705 // Notify client about update conversation event
       
   706 // ----------------------------------------------------------------------------
       
   707 void CCsSession::HandleModifyConversationListEventL(
       
   708                                                     CCsClientConversation* aClientConversation)
       
   709 {
       
   710     PRINT ( _L("Enter CCsSession::HandleModifyConversationListEventL") );
       
   711 
       
   712     if (!iConversationListChangeObserver)
       
   713         return;
       
   714 
       
   715     if (! (iNotifyHandling))
       
   716     {
       
   717         //append in notify list
       
   718         CCsConversationEvent* conversationEvent = CCsConversationEvent::NewL();
       
   719         conversationEvent->SetClientConversationL(*aClientConversation);
       
   720         CleanupStack::PushL(conversationEvent);
       
   721         conversationEvent->SetEvent(KConversationListEventUpdate);
       
   722         iEventList->AppendL(conversationEvent);
       
   723         CleanupStack::Pop(conversationEvent);
       
   724     }
       
   725     else
       
   726     {
       
   727         // create a new buffer for writing into stream
       
   728         CBufFlat* buf = CBufFlat::NewL(KBigBuffer);
       
   729         CleanupStack::PushL(buf);
       
   730 
       
   731         RBufWriteStream writeStream(*buf);
       
   732         writeStream.PushL();
       
   733 
       
   734         //externalize ClientConversation
       
   735         aClientConversation->ExternalizeL(writeStream);
       
   736 
       
   737         // Results are already packed in the stream
       
   738         writeStream.CommitL();
       
   739         // --------------------------------------------------------------
       
   740 
       
   741         // Create a heap descriptor from the buffer
       
   742         HBufC8* notifyDes = HBufC8::NewLC(buf->Size());
       
   743         TPtr8 ptr(notifyDes->Des());
       
   744         buf->Read(0, ptr, buf->Size());
       
   745 
       
   746         iAsyncReqRMessage.Write(1, *notifyDes);
       
   747         iAsyncReqRMessage.Complete(EModifyConversationListEvent);
       
   748         CleanupStack::PopAndDestroy(3, buf); // notifyDes, writestream, buf
       
   749         iNotifyHandling = EFalse;
       
   750     }
       
   751 
       
   752     PRINT ( _L("End CCsSession::HandleModifyConversationListEventL") );
       
   753 }
       
   754 
       
   755 // ----------------------------------------------------------------------------
       
   756 // CCsSession::RequestChangeEventL
       
   757 // the function to register for cache change event notification
       
   758 // ----------------------------------------------------------------------------
       
   759 void CCsSession::RequestChangeEventL(const RMessage2& aMessage)
       
   760 {
       
   761     iAsyncReqRMessage = aMessage;
       
   762     iNotifyHandling = ETrue;
       
   763 
       
   764     if (iEventList->Count() > 0)
       
   765     {
       
   766         CCsConversationEvent* conversationEvent = (*iEventList)[0];
       
   767 
       
   768         // check if the reqCnt matches with the latest arrived count
       
   769         // then its not a duplicate, delete the top event
       
   770         if (iReqCnt == aMessage.Int2())
       
   771         {
       
   772             iEventList->Remove(0);
       
   773             delete conversationEvent;
       
   774 
       
   775             if (iEventList->Count() > 0)
       
   776             {
       
   777                 // increment the count
       
   778                 iReqCnt++;
       
   779                 conversationEvent = (*iEventList)[0];
       
   780             }
       
   781             else
       
   782             {
       
   783                 // no more pending events, simply return
       
   784                 return;
       
   785             }
       
   786         }
       
   787 
       
   788         HBufC8* buffer = HBufC8::NewLC(4);
       
   789         TPtr8 cntPtr(buffer->Des());
       
   790         _LIT8(KFormat,"%d");
       
   791         cntPtr.Format(KFormat, iReqCnt);
       
   792         iAsyncReqRMessage.Write(0, *buffer);
       
   793 
       
   794         // create a new buffer for writing into stream
       
   795         CBufFlat* buf = CBufFlat::NewL(KBigBuffer);
       
   796         CleanupStack::PushL(buf);
       
   797 
       
   798         RBufWriteStream writeStream(*buf);
       
   799         writeStream.PushL();
       
   800 
       
   801         // Externalize ClientConversation
       
   802         if (conversationEvent->ClientConversation())
       
   803             conversationEvent->ClientConversation()->ExternalizeL(writeStream);
       
   804 
       
   805         // Results are already packed in the stream
       
   806         writeStream.CommitL();
       
   807 
       
   808         // --------------------------------------------------------------
       
   809         // Create a heap descriptor from the buffer
       
   810         HBufC8* notifyDes = HBufC8::NewLC(buf->Size());
       
   811         TPtr8 ptr(notifyDes->Des());
       
   812         buf->Read(0, ptr, buf->Size());
       
   813 
       
   814         iAsyncReqRMessage.Write(1, *notifyDes);
       
   815 
       
   816         NotifyClient(conversationEvent);
       
   817 
       
   818         CleanupStack::PopAndDestroy(4, buffer);
       
   819         // notifyDes, writestream, buf, buffer
       
   820         iNotifyHandling = EFalse;
       
   821     }
       
   822 }
       
   823 
       
   824 // ----------------------------------------------------------------------------
       
   825 // CCsSession::RemoveChangeEventL
       
   826 // Deregister the cache change event notification
       
   827 // ----------------------------------------------------------------------------
       
   828 void CCsSession::RemoveChangeEventL(const RMessage2& aMessage)
       
   829 {
       
   830     if (! (iNotifyHandling))
       
   831     {
       
   832         // complete message with aMessage
       
   833         aMessage.Complete(KErrNone);
       
   834     }
       
   835     else
       
   836     {
       
   837         iAsyncReqRMessage.Complete(KErrCancel);
       
   838         iNotifyHandling = EFalse;
       
   839         // complete message with aMessage
       
   840         aMessage.Complete(KErrNone);
       
   841     }
       
   842 }
       
   843 
       
   844 // ----------------------------------------------------------------------------
       
   845 // CCsSession::GetCachingStatusL
       
   846 // the function to request conversation server
       
   847 // to get caching status.
       
   848 // ----------------------------------------------------------------------------
       
   849 void CCsSession::GetCachingStatusL(const RMessage2& aMessage)
       
   850 {
       
   851     PRINT ( _L("Enter CCsSession::GetCachingStatusL") );
       
   852 
       
   853     // create a new buffer for writing into stream
       
   854     CBufFlat* buf = CBufFlat::NewL(KTinyBuffer);
       
   855 
       
   856     CleanupStack::PushL(buf);
       
   857     RBufWriteStream writeStream(*buf);
       
   858     writeStream.PushL();
       
   859 
       
   860     // Externalize caching status
       
   861     writeStream.WriteUint8L(iServer->GetCachingStatus());
       
   862 
       
   863     // Results are already packed in the stream
       
   864     writeStream.CommitL();
       
   865     // --------------------------------------------------------------
       
   866 
       
   867     // Create a heap descriptor from the buffer
       
   868     HBufC8* des = HBufC8::NewLC(buf->Size());
       
   869     CleanupStack::Pop(des);
       
   870     TPtr8 ptr(des->Des());
       
   871     buf->Read(0, ptr, buf->Size());
       
   872 
       
   873     CleanupStack::PopAndDestroy(2, buf); // writestream, buf
       
   874 
       
   875     aMessage.Write(1, *des);
       
   876     aMessage.Complete(KErrNone);
       
   877     delete des;
       
   878 
       
   879     PRINT ( _L("End CCsSession::GetCachingStatusL") );
       
   880 }
       
   881 
       
   882 // ----------------------------------------------------------------------------
       
   883 // CCsSession::GetTotalUnreadCountL
       
   884 // Gets total unread conversation entries.
       
   885 // ----------------------------------------------------------------------------
       
   886 void CCsSession::GetTotalUnreadCountL(const RMessage2& aMessage)
       
   887     {
       
   888     PRINT ( _L("Enter CCsSession::GetTotalUnreadCountL") );
       
   889 
       
   890     // create a new buffer for writing into stream
       
   891     CBufFlat* buf = CBufFlat::NewL(KTinyBuffer);
       
   892 
       
   893     CleanupStack::PushL(buf);
       
   894     RBufWriteStream writeStream(*buf);
       
   895     writeStream.PushL();
       
   896 
       
   897     CCsConversationCache* cache = iServer->ConversationCacheInterface();
       
   898 
       
   899     // Externalize caching status
       
   900     writeStream.WriteUint32L(cache->GetTotalUnreadCount());
       
   901 
       
   902     // Results are already packed in the stream
       
   903     writeStream.CommitL();
       
   904     // --------------------------------------------------------------
       
   905 
       
   906     // Create a heap descriptor from the buffer
       
   907     HBufC8* des = HBufC8::NewLC(buf->Size());
       
   908     CleanupStack::Pop(des);
       
   909     TPtr8 ptr(des->Des());
       
   910     buf->Read(0, ptr, buf->Size());
       
   911 
       
   912     CleanupStack::PopAndDestroy(2, buf); // writestream, buf
       
   913 
       
   914     aMessage.Write(1, *des);
       
   915     aMessage.Complete(KErrNone);
       
   916     delete des;
       
   917 
       
   918     PRINT ( _L("End CCsSession::GetTotalUnreadCountL") );
       
   919     }
       
   920 
       
   921 // ----------------------------------------------------------------------------
       
   922 // CCsSession::SetConversationListChangeObserverL
       
   923 // the function to request conversation server
       
   924 // to set ConversationListChangeObserver.
       
   925 // ----------------------------------------------------------------------------
       
   926 void CCsSession::SetConversationListChangeObserverL(const RMessage2& aMessage)
       
   927 {
       
   928     iConversationListChangeObserver = ETrue;
       
   929     aMessage.Complete(KErrNone);
       
   930 }
       
   931 
       
   932 // ----------------------------------------------------------------------------
       
   933 // CCsSession::ResetConversationListChangeObserverL
       
   934 // the function to request conversation server
       
   935 // to reset ConversationListChangeObserver.
       
   936 // ----------------------------------------------------------------------------
       
   937 void CCsSession::ResetConversationListChangeObserverL(const RMessage2& aMessage)
       
   938 {
       
   939     iConversationListChangeObserver = EFalse;
       
   940     aMessage.Complete(KErrNone);
       
   941 }
       
   942 
       
   943 // ----------------------------------------------------------------------------
       
   944 // CCsSession::SetConversationChangeObserverL
       
   945 // the function to request conversation server
       
   946 // to set ConversationChangeObserver.
       
   947 // ----------------------------------------------------------------------------
       
   948 void CCsSession::SetConversationChangeObserverL(const RMessage2& aMessage)
       
   949 {
       
   950     // Read Contact from the message
       
   951     HBufC8* buffer = HBufC8::NewLC(KBigBuffer);
       
   952 
       
   953     TPtr8 bufferPtr(buffer->Des());
       
   954     aMessage.ReadL(0, bufferPtr);
       
   955 
       
   956     // Stream over the buffer
       
   957     RDesReadStream stream(bufferPtr);
       
   958     stream.PushL();
       
   959 
       
   960     // Get the client conversation
       
   961     CCsClientConversation* clientConversation = CCsClientConversation::NewL();
       
   962     CleanupStack::PushL(clientConversation);
       
   963     clientConversation->InternalizeL(stream);
       
   964     CleanupStack::Pop(clientConversation);
       
   965 
       
   966     CleanupStack::PopAndDestroy(2, buffer);//stream, buffer
       
   967 
       
   968     iMonitoredConversation = clientConversation;
       
   969     iConversationChangeObserver = ETrue;
       
   970 
       
   971     aMessage.Complete(KErrNone);
       
   972 }
       
   973 
       
   974 // ----------------------------------------------------------------------------
       
   975 // CCsSession::ResetConversationChangeObserverL
       
   976 // the function to request conversation server
       
   977 // to reset ConversationChangeObserver.
       
   978 // ----------------------------------------------------------------------------
       
   979 void CCsSession::ResetConversationChangeObserverL(const RMessage2& aMessage)
       
   980 {
       
   981     // Read Contact from the message
       
   982     HBufC8* buffer = HBufC8::NewLC(KBigBuffer);
       
   983 
       
   984     TPtr8 bufferPtr(buffer->Des());
       
   985     aMessage.ReadL(0, bufferPtr);
       
   986 
       
   987     // Stream over the buffer
       
   988     RDesReadStream stream(bufferPtr);
       
   989     stream.PushL();
       
   990 
       
   991     // Read the client conversation
       
   992     CCsClientConversation* clientConversation = CCsClientConversation::NewL();
       
   993     CleanupStack::PushL(clientConversation);
       
   994     clientConversation->InternalizeL(stream);
       
   995     CleanupStack::Pop(clientConversation);
       
   996 
       
   997     CleanupStack::PopAndDestroy(2, buffer); //stream, buffer
       
   998 
       
   999     if (iMonitoredConversation)
       
  1000     {
       
  1001         delete iMonitoredConversation;
       
  1002         iMonitoredConversation = NULL;
       
  1003         iConversationChangeObserver = EFalse;
       
  1004     }
       
  1005 
       
  1006     delete clientConversation;
       
  1007 
       
  1008     aMessage.Complete(KErrNone);
       
  1009 }
       
  1010 
       
  1011 // ----------------------------------------------------------------------------
       
  1012 // CCsSession::SetCachingStatusObserverL
       
  1013 // the function to request conversation server
       
  1014 // to set caching status observer flag.
       
  1015 // ----------------------------------------------------------------------------
       
  1016 void CCsSession::SetCachingStatusObserverL(const RMessage2& aMessage)
       
  1017 {
       
  1018     iCachingChangeObserver = ETrue;
       
  1019     aMessage.Complete(KErrNone);
       
  1020 }
       
  1021 
       
  1022 // ----------------------------------------------------------------------------
       
  1023 // CCsSession::ResetCachingStatusObserverL
       
  1024 // the function to request conversation server
       
  1025 // to reset caching status observer flag.
       
  1026 // ----------------------------------------------------------------------------
       
  1027 void CCsSession::ResetCachingStatusObserverL(const RMessage2& aMessage)
       
  1028 {
       
  1029     iCachingChangeObserver = EFalse;
       
  1030     aMessage.Complete(KErrNone);
       
  1031 }
       
  1032 
       
  1033 // ----------------------------------------------------------------------------
       
  1034 // CCsSession::HandleNewConversationEventL
       
  1035 // the function to handles the new conversation event received from cache
       
  1036 // asynchronously
       
  1037 // ----------------------------------------------------------------------------
       
  1038 void CCsSession::HandleNewConversationEventL(
       
  1039                                              CCsClientConversation* aClientConversation)
       
  1040 {
       
  1041     PRINT ( _L("Enter CCsSession::HandleNewConversationEventL") );
       
  1042 
       
  1043     if (!iConversationChangeObserver)
       
  1044         return;
       
  1045 
       
  1046     if (aClientConversation->GetConversationEntryId()
       
  1047             != iMonitoredConversation->GetConversationEntryId())
       
  1048         return;
       
  1049 
       
  1050     if (! (iNotifyHandling))
       
  1051     {
       
  1052         //append in notify list
       
  1053         CCsConversationEvent* conversationEvent = CCsConversationEvent::NewL();
       
  1054         CleanupStack::PushL(conversationEvent);
       
  1055         conversationEvent->SetClientConversationL(*aClientConversation);
       
  1056         conversationEvent->SetEvent(KConversationEventNew);
       
  1057         iEventList->AppendL(conversationEvent);
       
  1058         CleanupStack::Pop(conversationEvent);
       
  1059     }
       
  1060     else
       
  1061     {
       
  1062         // create a new buffer for writing into stream
       
  1063         CBufFlat* buf = CBufFlat::NewL(KBigBuffer);
       
  1064         CleanupStack::PushL(buf);
       
  1065 
       
  1066         RBufWriteStream writeStream(*buf);
       
  1067         writeStream.PushL();
       
  1068 
       
  1069         //externalize ClientConversation
       
  1070         aClientConversation->ExternalizeL(writeStream);
       
  1071 
       
  1072         // Results are already packed in the stream
       
  1073         writeStream.CommitL();
       
  1074         // --------------------------------------------------------------
       
  1075 
       
  1076         // Create a heap descriptor from the buffer
       
  1077         HBufC8* des = HBufC8::NewLC(buf->Size());
       
  1078         CleanupStack::Pop(des);
       
  1079         TPtr8 ptr(des->Des());
       
  1080         buf->Read(0, ptr, buf->Size());
       
  1081 
       
  1082         CleanupStack::PopAndDestroy(2, buf); // writestream, buf
       
  1083 
       
  1084         iAsyncReqRMessage.Write(1, *des);
       
  1085         iAsyncReqRMessage.Complete(EAddConversationEvent);
       
  1086         delete des;
       
  1087         iNotifyHandling = EFalse;
       
  1088     }
       
  1089 
       
  1090     PRINT ( _L("End CCsSession::HandleNewConversationEventL") );
       
  1091 }
       
  1092 
       
  1093 // ----------------------------------------------------------------------------
       
  1094 // CCsSession::HandleDeleteConversationEventL
       
  1095 // the function to handles the delete conversation event received from cache
       
  1096 // asynchronously
       
  1097 // ----------------------------------------------------------------------------
       
  1098 void CCsSession::HandleDeleteConversationEventL(
       
  1099                                                 CCsClientConversation* aClientConversation)
       
  1100 {
       
  1101     PRINT ( _L("Enter CCsSession::HandleDeleteConversationEventL") );
       
  1102 
       
  1103     if (!iConversationChangeObserver)
       
  1104         return;
       
  1105 
       
  1106     if (aClientConversation->GetConversationEntryId()
       
  1107             != iMonitoredConversation->GetConversationEntryId())
       
  1108         return;
       
  1109 
       
  1110     if (! (iNotifyHandling))
       
  1111     {
       
  1112         //append in notify list
       
  1113         CCsConversationEvent* conversationEvent = CCsConversationEvent::NewL();
       
  1114         CleanupStack::PushL(conversationEvent);
       
  1115         conversationEvent->SetClientConversationL(*aClientConversation);
       
  1116         conversationEvent->SetEvent(KConversationEventDelete);
       
  1117         iEventList->AppendL(conversationEvent);
       
  1118         CleanupStack::Pop(conversationEvent);
       
  1119     }
       
  1120     else
       
  1121     {
       
  1122         // create a new buffer for writing into stream
       
  1123         CBufFlat* buf = CBufFlat::NewL(KBigBuffer);
       
  1124         CleanupStack::PushL(buf);
       
  1125 
       
  1126         RBufWriteStream writeStream(*buf);
       
  1127         writeStream.PushL();
       
  1128 
       
  1129         //externalize ClientConversation
       
  1130         aClientConversation->ExternalizeL(writeStream);
       
  1131 
       
  1132         // Results are already packed in the stream
       
  1133         writeStream.CommitL();
       
  1134         // --------------------------------------------------------------
       
  1135 
       
  1136         // Create a heap descriptor from the buffer
       
  1137         HBufC8* des = HBufC8::NewLC(buf->Size());
       
  1138         CleanupStack::Pop(des);
       
  1139         TPtr8 ptr(des->Des());
       
  1140         buf->Read(0, ptr, buf->Size());
       
  1141 
       
  1142         CleanupStack::PopAndDestroy(2, buf); // writestream, buf
       
  1143 
       
  1144         iAsyncReqRMessage.Write(1, *des);
       
  1145         iAsyncReqRMessage.Complete(EDeleteConversationEvent);
       
  1146         delete des;
       
  1147         iNotifyHandling = EFalse;
       
  1148     }
       
  1149 
       
  1150     PRINT ( _L("End CCsSession::HandleDeleteConversationEventL") );
       
  1151 }
       
  1152 
       
  1153 // ----------------------------------------------------------------------------
       
  1154 // CCsSession::HandleModifyConversationEventL
       
  1155 // the function to handles the modify conversation event received from cache
       
  1156 // asynchronously
       
  1157 // ----------------------------------------------------------------------------
       
  1158 void CCsSession::HandleModifyConversationEventL(
       
  1159                                                 CCsClientConversation* aClientConversation)
       
  1160 {
       
  1161     PRINT ( _L("Enter CCsSession::HandleModifyConversationEventL") );
       
  1162 
       
  1163     if (!iConversationChangeObserver)
       
  1164         return;
       
  1165 
       
  1166     if (aClientConversation->GetConversationEntryId()
       
  1167             != iMonitoredConversation->GetConversationEntryId())
       
  1168         return;
       
  1169 
       
  1170     if (! (iNotifyHandling))
       
  1171     {
       
  1172         //append in notify list
       
  1173         CCsConversationEvent* conversationEvent = CCsConversationEvent::NewL();
       
  1174         CleanupStack::PushL(conversationEvent);
       
  1175         conversationEvent->SetClientConversationL(*aClientConversation);
       
  1176         conversationEvent->SetEvent(KConversationEventUpdate);
       
  1177         iEventList->AppendL(conversationEvent);
       
  1178         CleanupStack::Pop(conversationEvent);
       
  1179     }
       
  1180     else
       
  1181     {
       
  1182         // create a new buffer for writing into stream
       
  1183         CBufFlat* buf = CBufFlat::NewL(KBigBuffer);
       
  1184         CleanupStack::PushL(buf);
       
  1185 
       
  1186         RBufWriteStream writeStream(*buf);
       
  1187         writeStream.PushL();
       
  1188 
       
  1189         //externalize ClientConversation
       
  1190         aClientConversation->ExternalizeL(writeStream);
       
  1191 
       
  1192         // Results are already packed in the stream
       
  1193         writeStream.CommitL();
       
  1194         // --------------------------------------------------------------
       
  1195 
       
  1196         // Create a heap descriptor from the buffer
       
  1197         HBufC8* des = HBufC8::NewLC(buf->Size());
       
  1198         CleanupStack::Pop(des);
       
  1199         TPtr8 ptr(des->Des());
       
  1200         buf->Read(0, ptr, buf->Size());
       
  1201 
       
  1202         CleanupStack::PopAndDestroy(2, buf); // writestream, buf
       
  1203 
       
  1204         iAsyncReqRMessage.Write(1, *des);
       
  1205         iAsyncReqRMessage.Complete(EModifyConversationEvent);
       
  1206         delete des;
       
  1207         iNotifyHandling = EFalse;
       
  1208     }
       
  1209 
       
  1210     PRINT ( _L("End CCsSession::HandleModifyConversationEventL") );
       
  1211 }
       
  1212 
       
  1213 // ----------------------------------------------------------------------------
       
  1214 // CCsSession::NotifyClient
       
  1215 // The function to notify client for cache change event
       
  1216 // ----------------------------------------------------------------------------
       
  1217 void CCsSession::NotifyClient(CCsConversationEvent* aConversationEvent)
       
  1218 {
       
  1219     if (aConversationEvent->IsNewConversationListEventSet())
       
  1220     {
       
  1221         iAsyncReqRMessage.Complete(EAddConversationListEvent);
       
  1222     }
       
  1223     else if (aConversationEvent->IsDeleteConversationListEventSet())
       
  1224     {
       
  1225         iAsyncReqRMessage.Complete(EDeleteConversationListEvent);
       
  1226     }
       
  1227     else if (aConversationEvent->IsUpdateConversationListEventSet())
       
  1228     {
       
  1229         iAsyncReqRMessage.Complete(EModifyConversationListEvent);
       
  1230     }
       
  1231     else if (aConversationEvent->IsNewConversationEventSet())
       
  1232     {
       
  1233         iAsyncReqRMessage.Complete(EAddConversationEvent);
       
  1234     }
       
  1235     else if (aConversationEvent->IsDeleteConversationEventSet())
       
  1236     {
       
  1237         iAsyncReqRMessage.Complete(EDeleteConversationEvent);
       
  1238     }
       
  1239     else if (aConversationEvent->IsUpdateConversationEventSet())
       
  1240     {
       
  1241         iAsyncReqRMessage.Complete(EModifyConversationEvent);
       
  1242     }
       
  1243     else if (aConversationEvent->IsRefreshConversationListEventSet())
       
  1244     {
       
  1245         iAsyncReqRMessage.Complete(KConversationEventListRefresh);
       
  1246     }
       
  1247     else if (aConversationEvent->IsRefreshConversationEventSet())
       
  1248     {
       
  1249         iAsyncReqRMessage.Complete(KConversationEventRefresh);
       
  1250     }
       
  1251 }
       
  1252 
       
  1253 // ----------------------------------------------------------------------------
       
  1254 // CCsSession::HandleChangeEventL
       
  1255 // The function handles cache change events
       
  1256 // ----------------------------------------------------------------------------
       
  1257 void CCsSession::HandleChangeEventL(CCsClientConversation* aConversation,
       
  1258                                     TUint32 aEvent)
       
  1259 {
       
  1260     if (aEvent & KConversationListEventNew)
       
  1261     {
       
  1262         HandleNewConversationListEventL(aConversation);
       
  1263     }
       
  1264     else if (aEvent & KConversationListEventUpdate)
       
  1265     {
       
  1266         HandleModifyConversationListEventL(aConversation);
       
  1267     }
       
  1268     else if (aEvent & KConversationListEventDelete)
       
  1269     {
       
  1270         HandleDeleteConversationListEventL(aConversation);
       
  1271     }
       
  1272     else if (aEvent & KConversationEventNew)
       
  1273     {
       
  1274         HandleNewConversationEventL(aConversation);
       
  1275     }
       
  1276     else if (aEvent & KConversationEventUpdate)
       
  1277     {
       
  1278         HandleModifyConversationEventL(aConversation);
       
  1279     }
       
  1280     else if (aEvent & KConversationEventDelete)
       
  1281     {
       
  1282         HandleDeleteConversationEventL(aConversation);
       
  1283     }
       
  1284     else if (aEvent & KConversationEventListRefresh)
       
  1285     {
       
  1286         HandleRefreshConversationListL();
       
  1287     }
       
  1288     else if (aEvent & KConversationEventRefresh)
       
  1289     {
       
  1290         HandleRefreshConversationL();
       
  1291     }
       
  1292 }
       
  1293 
       
  1294 // ----------------------------------------------------------------------------
       
  1295 // CCsSession::DeleteConversationL
       
  1296 // ----------------------------------------------------------------------------
       
  1297 void CCsSession::DeleteConversationL(const RMessage2& aMessage)
       
  1298 {
       
  1299     PRINT ( _L("Enter CCsSession::DeleteConversationL") );
       
  1300 
       
  1301     TInt conversationId = aMessage.Int0();
       
  1302 
       
  1303     // Delete handler
       
  1304     CCsConversationCache* cache = iServer->ConversationCacheInterface();
       
  1305     CCsConversationDeleteHandler* deleteHandler =
       
  1306             CCsConversationDeleteHandler::NewL(cache);
       
  1307 
       
  1308     deleteHandler->DeleteL(conversationId);
       
  1309     aMessage.Complete(EUserDeleteConversationComplete);
       
  1310 
       
  1311     PRINT ( _L("End CCsSession::DeleteConversationL") );
       
  1312 }
       
  1313 
       
  1314 // ----------------------------------------------------------------------------
       
  1315 // CCsSession::HandleRefreshConversationListL
       
  1316 // ----------------------------------------------------------------------------
       
  1317 void CCsSession::HandleRefreshConversationListL()
       
  1318 {
       
  1319     if (!iConversationListChangeObserver)
       
  1320         return;
       
  1321 
       
  1322     if (! (iNotifyHandling))
       
  1323     {
       
  1324         // Append in notify list
       
  1325         CCsConversationEvent* conversationEvent = CCsConversationEvent::NewL();
       
  1326         CleanupStack::PushL(conversationEvent);
       
  1327         conversationEvent->SetEvent(KConversationEventListRefresh);
       
  1328         iEventList->AppendL(conversationEvent);
       
  1329         CleanupStack::Pop(conversationEvent);
       
  1330     }
       
  1331     else
       
  1332     {
       
  1333         iAsyncReqRMessage.Complete(ERefreshConversationListEvent);
       
  1334         iNotifyHandling = EFalse;
       
  1335     }
       
  1336 }
       
  1337 
       
  1338 // ----------------------------------------------------------------------------
       
  1339 // CCsSession::HandleRefreshConversationL
       
  1340 // ----------------------------------------------------------------------------
       
  1341 void CCsSession::HandleRefreshConversationL()
       
  1342 {
       
  1343     if (!iConversationChangeObserver)
       
  1344         return;
       
  1345 
       
  1346     if (! (iNotifyHandling))
       
  1347     {
       
  1348         // Append in notify list
       
  1349         CCsConversationEvent* conversationEvent = CCsConversationEvent::NewL();
       
  1350         CleanupStack::PushL(conversationEvent);
       
  1351         conversationEvent->SetEvent(KConversationEventRefresh);
       
  1352         iEventList->AppendL(conversationEvent);
       
  1353         CleanupStack::Pop(conversationEvent);
       
  1354     }
       
  1355     else
       
  1356     {
       
  1357         iAsyncReqRMessage.Complete(ERefreshConversationEvent);
       
  1358         iNotifyHandling = EFalse;
       
  1359     }
       
  1360 }
       
  1361 
       
  1362 // ----------------------------------------------------------------------------
       
  1363 // Get conversation id
       
  1364 // ----------------------------------------------------------------------------
       
  1365 void CCsSession::GetConversationIdL(const RMessage2& aMessage)
       
  1366 {
       
  1367     // create a new buffer for writing into stream
       
  1368     CBufFlat* buf = CBufFlat::NewL(KTinyBuffer);
       
  1369 
       
  1370     CleanupStack::PushL(buf);
       
  1371     RBufWriteStream writeStream(*buf);
       
  1372     writeStream.PushL();
       
  1373 
       
  1374     // Get the contact id
       
  1375     TInt contactId = aMessage.Int0();
       
  1376     CCsConversationCache* cache = iServer->ConversationCacheInterface();
       
  1377     TInt conversationId = cache->GetConversationIdL(contactId);
       
  1378 
       
  1379     // Externalize link
       
  1380     writeStream.WriteInt32L(conversationId);
       
  1381 
       
  1382     // Results are already packed in the stream
       
  1383     writeStream.CommitL();
       
  1384 
       
  1385     // Create a heap descriptor from the buffer
       
  1386     HBufC8* des = HBufC8::NewLC(buf->Size());
       
  1387     CleanupStack::Pop(des);
       
  1388     TPtr8 ptr(des->Des());
       
  1389     buf->Read(0, ptr, buf->Size());
       
  1390 
       
  1391     CleanupStack::PopAndDestroy(2, buf); // writestream, buf
       
  1392 
       
  1393     aMessage.Write(1, *des);
       
  1394     aMessage.Complete(EGetConversationIdComplete);
       
  1395     delete des;
       
  1396 }
       
  1397 
       
  1398 // ----------------------------------------------------------------------------
       
  1399 // GetConversationFromMessageIdL
       
  1400 // ----------------------------------------------------------------------------
       
  1401 void CCsSession::GetConversationFromMessageIdL(const RMessage2& aMessage)
       
  1402     {
       
  1403     // create a new buffer for writing into stream
       
  1404     CBufFlat* buf = CBufFlat::NewL(KBigBuffer);
       
  1405     CleanupStack::PushL(buf);
       
  1406 
       
  1407     RBufWriteStream writeStream(*buf);
       
  1408     writeStream.PushL();
       
  1409 
       
  1410     // Get the message id
       
  1411     TInt messageId = aMessage.Int0();
       
  1412     CCsConversationCache* cache = iServer->ConversationCacheInterface();
       
  1413     CCsClientConversation* conversation = cache->GetConversationFromMessageIdL(messageId);
       
  1414     
       
  1415     // if no conversation exists for given message-id, 
       
  1416     // create a dummy conversation and complete response
       
  1417     if(conversation == NULL)
       
  1418     {
       
  1419         //create dummy conversation
       
  1420         conversation = CCsClientConversation::NewL();
       
  1421         CleanupStack::PushL(conversation);
       
  1422         conversation->SetConversationEntryId(-1);
       
  1423         CCsConversationEntry* entry = CCsConversationEntry::NewL();
       
  1424 		CleanupStack::PushL(entry);
       
  1425         entry->SetEntryId(-1);
       
  1426         conversation->SetConversationEntryL(entry); // clone
       
  1427 		CleanupStack::PopAndDestroy(entry);
       
  1428     }
       
  1429     else
       
  1430     {
       
  1431         CleanupStack::PushL(conversation);
       
  1432     }
       
  1433 
       
  1434     // Externalize 
       
  1435     conversation->ExternalizeL(writeStream);
       
  1436     
       
  1437     // Results are already packed in the stream
       
  1438     writeStream.CommitL();
       
  1439     
       
  1440     // Create a heap descriptor from the buffer
       
  1441     HBufC8* des = HBufC8::NewLC(buf->Size());
       
  1442     CleanupStack::Pop(des);
       
  1443     TPtr8 ptr(des->Des());
       
  1444     buf->Read(0, ptr, buf->Size());
       
  1445     
       
  1446     CleanupStack::PopAndDestroy(3, buf); // conversation, writestream, buf
       
  1447     
       
  1448     aMessage.Write(1, *des);
       
  1449     aMessage.Complete(EGetConversationFromMessageIdComplete);
       
  1450     delete des;
       
  1451     }
       
  1452 
       
  1453 // ----------------------------------------------------------------------------
       
  1454 // CCsSession::GetConversationIdfromAddressL
       
  1455 // ----------------------------------------------------------------------------
       
  1456 void CCsSession::GetConversationIdfromAddressL(const RMessage2& aMessage)
       
  1457 {
       
  1458     TInt deslen = aMessage.GetDesLength(0);
       
  1459     
       
  1460     // Copy the data into a buffer
       
  1461     RBuf buffer;
       
  1462     buffer.CreateL(deslen);
       
  1463     buffer.CleanupClosePushL();
       
  1464     aMessage.ReadL(0,buffer,0);
       
  1465 
       
  1466 
       
  1467     // Get the contact id
       
  1468     CCsConversationCache* cache = iServer->ConversationCacheInterface();
       
  1469     TInt conversationId = cache->GetConversationIdFromAddressL(buffer);
       
  1470 
       
  1471     // create a new buffer for writing into stream
       
  1472     CBufFlat* buf = CBufFlat::NewL(KTinyBuffer);
       
  1473     CleanupStack::PushL(buf);
       
  1474 
       
  1475     RBufWriteStream writeStream(*buf);
       
  1476     writeStream.PushL();
       
  1477     // Externalize link
       
  1478     writeStream.WriteInt32L(conversationId);
       
  1479 
       
  1480     // Results are already packed in the stream
       
  1481     writeStream.CommitL();
       
  1482 
       
  1483     // Create a heap descriptor from the buffer
       
  1484     HBufC8* des = HBufC8::NewLC(buf->Size());
       
  1485     CleanupStack::Pop(des);
       
  1486     TPtr8 ptr(des->Des());
       
  1487     buf->Read(0, ptr, buf->Size());
       
  1488  
       
  1489     CleanupStack::PopAndDestroy(2, buf); // writestream, buf
       
  1490     CleanupStack::PopAndDestroy(&buffer);
       
  1491 
       
  1492     aMessage.Write(1, *des);
       
  1493     aMessage.Complete(EGetConversationIdFromAddressComplete);
       
  1494     delete des;
       
  1495 }
       
  1496 
       
  1497 // ----------------------------------------------------------------------------
       
  1498 // CCsSession::MarkConversationReadL
       
  1499 // ----------------------------------------------------------------------------
       
  1500 void CCsSession::MarkConversationReadL(const RMessage2& aMessage)
       
  1501 {
       
  1502     PRINT ( _L("Enter CCsSession::MarkConversationReadL") );
       
  1503 
       
  1504     TInt conversationId = aMessage.Int0();
       
  1505 
       
  1506     // Mark read handler
       
  1507     CCsConversationCache* cache = iServer->ConversationCacheInterface();
       
  1508     CCsConversationMarkReadHandler* markHandler = CCsConversationMarkReadHandler::NewL(cache);
       
  1509 
       
  1510     markHandler->MarkReadL(conversationId);
       
  1511     
       
  1512     aMessage.Complete(EUserMarkReadConversationComplete);
       
  1513 
       
  1514     PRINT ( _L("End CCsSession::MarkConversationReadL") );
       
  1515 }
       
  1516 //EOF