messagingapp/msgappfw/server/src/ccsconversationcachehelper.cpp
changeset 23 238255e8b033
child 25 84d9eb65b26f
equal deleted inserted replaced
5:4697dfb2d7ad 23:238255e8b033
       
     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:  This class helps CS Cache in processing the data.
       
    15  *
       
    16  */
       
    17 
       
    18 // INCLUDE FILES
       
    19 // SYSTEM INCLUDE FILES
       
    20 #include <ccsdefs.h>
       
    21 #include <ccsconversationentry.h>
       
    22 #include <ccsclientconversation.h>
       
    23 
       
    24 // USER INCLUDE FILES
       
    25 #include "ccsconversationcachehelper.h"
       
    26 #include "ccsconversationcache.h"
       
    27 #include "ccsconversation.h"
       
    28 #include "ccsconversationentry.h"
       
    29 #include "ccsconversationevent.h"
       
    30 #include "ccscontactsmanager.h"
       
    31 #include "ccscontactsresolver.h"
       
    32 #include "s60qconversions.h"
       
    33 #include "ccsdebug.h"
       
    34 
       
    35 // ============================== MEMBER FUNCTIONS ============================
       
    36 
       
    37 // ----------------------------------------------------------------------------
       
    38 // CCsConversationCacheHelper::NewL
       
    39 // Two Phase Construction
       
    40 // ----------------------------------------------------------------------------
       
    41 CCsConversationCacheHelper*
       
    42 CCsConversationCacheHelper::NewL(CCsConversationCache* aConversationCache)
       
    43 {
       
    44     PRINT ( _L("Enter CCsConversationCacheHelper::NewL") );
       
    45 
       
    46     CCsConversationCacheHelper* self =
       
    47             new (ELeave) CCsConversationCacheHelper(aConversationCache);
       
    48 
       
    49     CActiveScheduler::Add(self);
       
    50     CleanupStack::PushL(self);
       
    51     self->ConstructL();
       
    52     CleanupStack::Pop(self);
       
    53 
       
    54     PRINT ( _L("End CCsConversationCacheHelper::NewL") );
       
    55 
       
    56     return self;
       
    57 }
       
    58 
       
    59 // ----------------------------------------------------------------------------
       
    60 // CCsConversationCacheHelper::CCsConversationCacheHelper
       
    61 // Construtor
       
    62 // ----------------------------------------------------------------------------
       
    63 CCsConversationCacheHelper::CCsConversationCacheHelper(
       
    64                                                        CCsConversationCache* aConversationCache) :
       
    65     CActive(EPriorityLow), iConversationCache(aConversationCache)
       
    66 {
       
    67 }
       
    68 
       
    69 // ----------------------------------------------------------------------------
       
    70 // CCsConversationCacheHelper::ConstructL
       
    71 // Second phase constructor
       
    72 // ----------------------------------------------------------------------------
       
    73 void CCsConversationCacheHelper::ConstructL()
       
    74 {
       
    75     iConversationEvent = NULL;
       
    76     iConversationIndex = KErrNotFound;
       
    77     iCurrentConversationIndex = KErrNotFound;
       
    78 
       
    79     //initialize the event list
       
    80     iConversationEventList
       
    81             = new (ELeave) RPointerArray<CCsConversationEvent> ();
       
    82 
       
    83     // now create all special type of conversations
       
    84     // like BT, IRDA, Unknown Drafts etc.
       
    85     TInt loop = 0;
       
    86     for (loop = 0; loop < KMaxSpecialConversations; loop++)
       
    87     {
       
    88         CreateSpecialConversationL();
       
    89     }
       
    90 }
       
    91 
       
    92 // ----------------------------------------------------------------------------
       
    93 // CCsConversationCache::CreateSpecialConversationL
       
    94 // Second part of constructL
       
    95 // This shall create all special type of messages like BT, Unknown Draft, IRDA etc.
       
    96 // Pls note this shall be placed by order at starting of cache
       
    97 // ----------------------------------------------------------------------------
       
    98 void CCsConversationCacheHelper::CreateSpecialConversationL()
       
    99 {
       
   100     // Create a conversation for Drafts Unknown case and set it in
       
   101     // 0th position and the conversation Id for drafts unknown case is
       
   102     // KDratsUnknownConversationId
       
   103     CCsConversation* conversation = CCsConversation::NewL();
       
   104 
       
   105     conversation->SetConversationId(GetNextRowId());
       
   106 
       
   107     // add into the list
       
   108     iConversationCache->ConversationList()->AppendL(conversation);
       
   109 }
       
   110 
       
   111 // ----------------------------------------------------------------------------
       
   112 // CCsConversationCacheHelper::CCsConversationCacheHelper
       
   113 // Destructor
       
   114 // ----------------------------------------------------------------------------
       
   115 CCsConversationCacheHelper::~CCsConversationCacheHelper()
       
   116 {
       
   117     Cancel();
       
   118 
       
   119     // delete the event list
       
   120     if (iConversationEventList)
       
   121     {
       
   122         iConversationEventList->ResetAndDestroy();
       
   123         iConversationEventList->Close();
       
   124         delete iConversationEventList;
       
   125         iConversationEventList = NULL;
       
   126     }
       
   127 }
       
   128 // -----------------------------------------------------------------------------
       
   129 // CCsConversationCacheHelper::DoCancel()
       
   130 // DoCancel implementation from CActive Object
       
   131 // -----------------------------------------------------------------------------
       
   132 //
       
   133 void CCsConversationCacheHelper::DoCancel()
       
   134 {
       
   135     // do nothing    
       
   136 }
       
   137 // ----------------------------------------------------------------------------
       
   138 // CCsConversationCacheHelper::Run
       
   139 // Called from the scheduler
       
   140 // ----------------------------------------------------------------------------
       
   141 void CCsConversationCacheHelper::RunL()
       
   142 {
       
   143     switch (iState)
       
   144     {
       
   145         case ECsProcessConversation:
       
   146         {
       
   147             HandleProcessConversationL();
       
   148         }
       
   149             break;
       
   150 
       
   151         case ECsSpecialConversation:
       
   152         {
       
   153             HandleSpecialConversationL();
       
   154         }
       
   155             break;
       
   156 
       
   157         case ECsConversationFoundInCache:
       
   158         {
       
   159             HandleConversationInCacheL();
       
   160         }
       
   161             break;
       
   162 
       
   163         case ECsResolveConversation:
       
   164         {
       
   165             ResolveContact(iConversationEvent);
       
   166         }
       
   167             break;
       
   168     }
       
   169 }
       
   170 // ----------------------------------------------------------------------------
       
   171 // CCsConversationCacheHelper::StartCacheUpdate
       
   172 // this shall start looking at cache data and update if entry already exist
       
   173 // shall put request to phonebook in case new data
       
   174 // ----------------------------------------------------------------------------
       
   175 void CCsConversationCacheHelper::StartCacheUpdate()
       
   176 {
       
   177     iState = ECsProcessConversation;
       
   178     IssueRequest();
       
   179 }
       
   180 // ---------------------------------------------------------------------------
       
   181 // CCsConversationCacheHelper::HandleProcessConversationL()
       
   182 // Process Conversation and update cache
       
   183 // ---------------------------------------------------------------------------
       
   184 //
       
   185 void CCsConversationCacheHelper::HandleProcessConversationL()
       
   186 {
       
   187     iConversationEvent = GetNextEvent();
       
   188     // check to see if all the events are consumed
       
   189     // then mark cache as caching done..
       
   190     if (!iConversationEvent)
       
   191     {
       
   192         iConversationCache->CachingCompletedL();
       
   193         return;
       
   194     }
       
   195 
       
   196     CCsConversationEntry* conEntry =
       
   197             iConversationEvent->ClientConversation()->GetConversationEntry();
       
   198 
       
   199     //handle add, modify events
       
   200     if (iConversationEvent->Event() != KConversationEventDelete)
       
   201     {
       
   202         TUint8 SpecialId = NeedsSpecialProcessing(conEntry);
       
   203 
       
   204         if (SpecialId)
       
   205         {
       
   206             // this is for draft(unlnown)/BT/IR processing            
       
   207             iState = ECsSpecialConversation;
       
   208         }
       
   209         // check only if contact is a valid address
       
   210         else if (conEntry->Contact())
       
   211         {
       
   212             iCurrentConversationIndex
       
   213                     = iConversationCache->FindConversation(* (conEntry->Contact()));
       
   214             if (iCurrentConversationIndex != KErrNotFound)
       
   215             {
       
   216                 iState = ECsConversationFoundInCache;
       
   217             }
       
   218             else
       
   219             {
       
   220                 iState = ECsResolveConversation;
       
   221             }
       
   222         }
       
   223         else
       
   224         {
       
   225             // this is when contact number is NULL
       
   226             // delete the entry from temp list
       
   227             DeleteEvent(*iConversationEvent);
       
   228             iState = ECsProcessConversation;
       
   229         }
       
   230     }
       
   231     else
       
   232     {
       
   233         //handle delete event
       
   234         DeleteConversationEntryL(conEntry);
       
   235         DeleteEvent(*iConversationEvent);
       
   236         iState = ECsProcessConversation;
       
   237     }
       
   238     //move to other state
       
   239     IssueRequest();
       
   240 }
       
   241 // ---------------------------------------------------------------------------
       
   242 // CCsConversationCacheHelper::HandleSpecialConversationL()
       
   243 // Hnadle the case when the conversation has no contact,
       
   244 // BlueTooth/IrDa messages
       
   245 // ---------------------------------------------------------------------------
       
   246 //
       
   247 void CCsConversationCacheHelper::HandleSpecialConversationL()
       
   248 {
       
   249     CCsConversationEntry* conEntry =
       
   250             iConversationEvent->ClientConversation()->GetConversationEntry();
       
   251 
       
   252     if (ECsBlueTooth == conEntry->GetType())
       
   253     {
       
   254         AddConversationEntryL(conEntry, KBluetoothMsgsConversationId);
       
   255     }
       
   256     else
       
   257     {
       
   258         AddConversationEntryL(conEntry, KUnknownConversationId);
       
   259     }
       
   260     DeleteEvent(*iConversationEvent);
       
   261     iState = ECsProcessConversation;
       
   262     IssueRequest();
       
   263 
       
   264     PRINT ( _L("CCsConversationCacheHelper::HandleSpecialConversationL") );
       
   265 }
       
   266 // ---------------------------------------------------------------------------
       
   267 // CCsConversationCacheHelper::HandleConversationInCacheL()
       
   268 // Handle the case when the conversation is found in the cache
       
   269 // so there is no need for resolving the contact
       
   270 // ---------------------------------------------------------------------------
       
   271 //
       
   272 void CCsConversationCacheHelper::HandleConversationInCacheL()
       
   273 {
       
   274     CCsConversationEntry* conEntry =
       
   275             iConversationEvent->ClientConversation()->GetConversationEntry();
       
   276 
       
   277     AddConversationEntryL(conEntry, iCurrentConversationIndex);
       
   278 
       
   279     DeleteEvent(*iConversationEvent);
       
   280 
       
   281     // reset back the conversation index
       
   282     iCurrentConversationIndex = KErrNotFound;
       
   283     iState = ECsProcessConversation;
       
   284     IssueRequest();
       
   285 }
       
   286 // ----------------------------------------------------------------------------
       
   287 // This function identifies if the conentry needs special processing
       
   288 // Drafts message with NULL contact/Bluetooth/IRDA etc
       
   289 // ----------------------------------------------------------------------------
       
   290 TUint8 CCsConversationCacheHelper::NeedsSpecialProcessing(
       
   291                                                           CCsConversationEntry* aConversationEntry)
       
   292 {
       
   293     if (aConversationEntry->Contact() == NULL
       
   294             && aConversationEntry->IsAttributeSet(ECsAttributeDraft))
       
   295     {
       
   296         return 1;
       
   297     }
       
   298     return 0;
       
   299 }
       
   300 
       
   301 // ---------------------------------------------------------------------------
       
   302 // Move to next state
       
   303 // ---------------------------------------------------------------------------
       
   304 //
       
   305 void CCsConversationCacheHelper::IssueRequest()
       
   306 {
       
   307     if (!IsActive())
       
   308     {
       
   309         iStatus = KRequestPending;
       
   310         TRequestStatus* status = &iStatus;
       
   311         SetActive();
       
   312         User::RequestComplete(status, KErrNone);
       
   313     }
       
   314 }
       
   315 
       
   316 // ----------------------------------------------------------------------------
       
   317 // CCsConversationCacheHelper::ContactResolved
       
   318 // Handle a contact resolved in the contact database
       
   319 // ----------------------------------------------------------------------------
       
   320 void CCsConversationCacheHelper::ResolveContact(
       
   321                                                 CCsConversationEvent* aConverastionEvent)
       
   322 {
       
   323     CCsContactDetail contactDetail;
       
   324 
       
   325     //get contact from event
       
   326     CCsConversationEntry* ConvEntry =
       
   327             aConverastionEvent->ClientConversation()->GetConversationEntry();
       
   328 
       
   329     if (ConvEntry)
       
   330     {
       
   331         HBufC* Contact = ConvEntry->Contact();
       
   332         if (Contact)
       
   333         {
       
   334             QString contactAddress =
       
   335                     S60QConversions::s60DescToQString(Contact->Des());
       
   336 
       
   337             iConversationCache->ContactsManager()->resolver()->resolveContact(contactAddress,
       
   338                                                                               contactDetail);
       
   339 
       
   340             int contactId = contactDetail.contactId;
       
   341             TInt cIndex = iConversationCache->FindConversation(contactId);
       
   342 
       
   343             if (cIndex == KErrNotFound)
       
   344             {
       
   345                 // Add as new conversation
       
   346                 HBufC* firstName = NULL;
       
   347                 if(!contactDetail.firstName.isEmpty())
       
   348                 {
       
   349                     firstName=S60QConversions::qStringToS60Desc(contactDetail.firstName);
       
   350                 }
       
   351                 HBufC* lastName=NULL;
       
   352                 if(!contactDetail.lastName.isEmpty())
       
   353                 {
       
   354                     lastName=S60QConversions::qStringToS60Desc(contactDetail.lastName);
       
   355                 }
       
   356                 
       
   357                 HBufC* nickName=NULL;
       
   358                 if(!contactDetail.nickName.isEmpty())
       
   359                 {
       
   360                     nickName=S60QConversions::qStringToS60Desc(contactDetail.nickName);
       
   361                 }
       
   362                 
       
   363                 TRAPD(error, AddNewConversationL( aConverastionEvent->
       
   364                                 ClientConversation()->GetConversationEntry(),
       
   365                                 contactId,
       
   366                                 firstName,
       
   367                                 lastName,
       
   368                                 nickName));
       
   369 
       
   370                 if (firstName) delete firstName;
       
   371                 if (lastName) delete lastName;
       
   372                 if (nickName) delete nickName;
       
   373 
       
   374                 if (error != KErrNone)
       
   375                 {
       
   376                     // handle error
       
   377                 }
       
   378             }
       
   379             else
       
   380             {
       
   381                 // this is when two contacts are having same contact Id,
       
   382                 // in that case it should add into an existing conversation
       
   383                 TRAPD(error, AddConversationEntryL( aConverastionEvent->
       
   384                                 ClientConversation()->GetConversationEntry(), cIndex));
       
   385                 if (error != KErrNone)
       
   386                 {
       
   387                     // handle error
       
   388                 }
       
   389             }
       
   390 
       
   391             // now remove the entry from event list
       
   392             DeleteEvent(*aConverastionEvent);
       
   393 
       
   394             // call start update cache
       
   395             StartCacheUpdate();
       
   396         }
       
   397     }
       
   398 }
       
   399 
       
   400 // ----------------------------------------------------------------------------
       
   401 // Handles the case where conversation is already in cache
       
   402 // This will add new conversation entry into the corresponding conversation
       
   403 // pls note there is already a conversation exist for this entry
       
   404 // hence this entry gets appended inside conversation list
       
   405 // ----------------------------------------------------------------------------
       
   406 void CCsConversationCacheHelper::AddConversationEntryL(
       
   407                                                        CCsConversationEntry *aConEntry,
       
   408                                                        TInt aConversationIndex)
       
   409 {
       
   410     RPointerArray<CCsConversation>* conversationList =
       
   411             iConversationCache->ConversationList();
       
   412 
       
   413     CCsConversation* conversation = (*conversationList)[aConversationIndex];
       
   414 
       
   415     CCsConversationEntry* prevLatestEntry =
       
   416             conversation->GetLatestEntryL()->CloneL();
       
   417 
       
   418     CleanupStack::PushL(prevLatestEntry);
       
   419 
       
   420     // save the previous unread message count
       
   421     TUint16 prevUnreadCount = conversation->GetUnreadMessageCount();
       
   422 
       
   423     TUint32 event;
       
   424     conversation->UpdateEntryL(aConEntry, event);
       
   425 
       
   426     // Send update notify to conversation list
       
   427     if (IsNotifyRequiredL(conversation,
       
   428                           prevLatestEntry,
       
   429                           KConversationListEventUpdate,
       
   430                           prevUnreadCount))
       
   431     {
       
   432         CCsConversationEntry* convEntry = conversation->GetLatestEntryL();
       
   433         CCsClientConversation* clientConvLatest =
       
   434                 iConversationCache->CreateClientConvLC(conversation, convEntry);
       
   435         
       
   436         // THIS IS USED FOR SHOWING NOTIFICATIONS. NOTIFICATION IS SHOWN ONLY
       
   437         // WHEN ECsAttributeNewEntryAdded IS SET. 
       
   438         if(iConversationEvent->IsUpdateConversationEventSet() 
       
   439                 && event == KConversationEventNew)
       
   440             {
       
   441             clientConvLatest->GetConversationEntry()->
       
   442             ChangeAttributes(ECsAttributeNewEntryAdded,ECsAttributeNone);
       
   443             }
       
   444         
       
   445         iConversationCache->NotifyL(clientConvLatest,
       
   446                                     KConversationListEventUpdate);
       
   447         CleanupStack::PopAndDestroy(clientConvLatest);
       
   448     }
       
   449 
       
   450     // Send update notify to conversations
       
   451     CCsClientConversation* clientConv =
       
   452             iConversationCache->CreateClientConvLC(conversation, aConEntry);
       
   453     iConversationCache->NotifyL(clientConv, event);
       
   454 
       
   455     // Cleanup
       
   456     CleanupStack::PopAndDestroy(clientConv);
       
   457     CleanupStack::PopAndDestroy(prevLatestEntry);
       
   458 }
       
   459 
       
   460 // ----------------------------------------------------------------------------
       
   461 // CCsConversationCache::DeleteConversationL
       
   462 // Delete conversation entry from conversation list
       
   463 // there are few checks of how to notify the UI about the changes 
       
   464 // for the various use cases. 
       
   465 // Delete requires the conversation Id and type (SMS, MMS etc.)
       
   466 // ----------------------------------------------------------------------------
       
   467 void CCsConversationCacheHelper::DeleteConversationEntryL(
       
   468                                                           CCsConversationEntry* aConversationEntry)
       
   469 {
       
   470     // For MMS, search the entry inside all conversations and delete.
       
   471     // For SMS, stop on first match.
       
   472     TBool stopOnFirstMatch = ETrue;
       
   473 
       
   474     RPointerArray<CCsConversation>* conversationList =
       
   475             iConversationCache->ConversationList();
       
   476 
       
   477     // the deletion needs search the entryId inside whole conversation
       
   478     // search the entry id and delete the entry
       
   479     for (TInt loop = 0; loop < iConversationCache->ConversationList()->Count(); loop++)
       
   480     {
       
   481         CCsConversation* conversation =
       
   482                 static_cast<CCsConversation*> ( (*conversationList)[loop]);
       
   483 
       
   484         TInt indexDeletion = conversation->FindEntry(aConversationEntry);
       
   485 
       
   486         if (indexDeletion != KErrNotFound)
       
   487         {
       
   488             // Get the entry from cache to check the type
       
   489             CCsConversationEntry* cacheEntry =
       
   490                     conversation->GetEntryL(indexDeletion);
       
   491 
       
   492             if (cacheEntry->GetType() == ECsMMS || cacheEntry->GetType()
       
   493                     == ECsAudio)
       
   494             {
       
   495                 stopOnFirstMatch = EFalse;
       
   496             }
       
   497             
       
   498             TUint16 prevUnreadCount = conversation->GetUnreadMessageCount();
       
   499 
       
   500             // Delete the conversation entry from this conversation
       
   501             conversation->DeleteEntryL(indexDeletion);
       
   502 
       
   503             // Notify client of conversation list change.
       
   504             if (IsNotifyRequiredL(conversation,
       
   505                                   aConversationEntry,
       
   506                                   KConversationListEventDelete,
       
   507                                   prevUnreadCount))
       
   508             {
       
   509                 // Delete
       
   510                 CCsClientConversation
       
   511                         * clientConv =
       
   512                                 iConversationCache->CreateClientConvLC(conversation,
       
   513                                                                        aConversationEntry);
       
   514                 iConversationCache->NotifyL(clientConv,
       
   515                                             KConversationListEventDelete);
       
   516                 CleanupStack::PopAndDestroy(clientConv);
       
   517 
       
   518                 // Update with latest entry
       
   519                 CCsConversationEntry* conEntry =
       
   520                         conversation->GetLatestEntryL();
       
   521                 if (conEntry)
       
   522                 {
       
   523                     CCsClientConversation
       
   524                             * clientConv =
       
   525                                     iConversationCache->CreateClientConvLC(conversation,
       
   526                                                                            conEntry);
       
   527                     iConversationCache->NotifyL(clientConv,
       
   528                                                 KConversationListEventNew);
       
   529                     CleanupStack::PopAndDestroy(clientConv);
       
   530                 }
       
   531             }
       
   532 
       
   533             // Notify client of conversation change
       
   534             if (!conversation->IsDeleted())
       
   535             {
       
   536                 CCsClientConversation
       
   537                         * clientConv =
       
   538                                 iConversationCache->CreateClientConvLC(conversation,
       
   539                                                                        aConversationEntry);
       
   540                 iConversationCache->NotifyL(clientConv,
       
   541                                             KConversationEventDelete);
       
   542                 CleanupStack::PopAndDestroy(clientConv);
       
   543             }
       
   544 
       
   545             // check if all entries are deleted then 
       
   546             // delete the conversation from cache
       
   547             if (conversation->GetEntryCount() == 0
       
   548                     && !conversation->IsSpecialConversation())
       
   549             {
       
   550                 conversationList->Remove(loop);
       
   551                 delete conversation;
       
   552                 //reset the counters
       
   553                 loop -= 1;
       
   554             }
       
   555 
       
   556             // Stop searching    
       
   557             if (stopOnFirstMatch) break;
       
   558         }
       
   559     } // for
       
   560 }
       
   561 
       
   562 // ----------------------------------------------------------------------------
       
   563 // CCsConversationCacheHelper::AddNewConversationL
       
   564 // add new conversation into cache
       
   565 // this shall be called after resolving entry from phonebook
       
   566 // ----------------------------------------------------------------------------
       
   567 void CCsConversationCacheHelper::AddNewConversationL(
       
   568                                                      CCsConversationEntry* aConversationEntry,
       
   569                                                      TInt32 aContactId,
       
   570                                                      const HBufC* aFirstName,
       
   571                                                      const HBufC* aLastName,
       
   572                                                      const HBufC* aNickName)
       
   573 {
       
   574     CCsConversation* conversation = CCsConversation::NewL();
       
   575     CleanupStack::PushL(conversation);
       
   576 
       
   577     //set conversation entry id
       
   578     // by default just set the count of list as of now, as it will be unique
       
   579     conversation->SetConversationId(GetNextRowId());
       
   580 
       
   581     // add the conversation entry
       
   582     conversation->AddEntryL(aConversationEntry);
       
   583 
       
   584     // fill firstname and lastname and contact Id
       
   585     conversation->AddContactDetailsL(aContactId,
       
   586                                      *aFirstName,
       
   587                                      *aLastName,
       
   588                                      *aNickName);
       
   589 
       
   590     // fill the phone number
       
   591     if (aConversationEntry->Contact())
       
   592     {
       
   593         conversation->AddContactDetailsL(* (aConversationEntry->Contact()));
       
   594     }
       
   595 
       
   596     // add into the list
       
   597     iConversationCache->ConversationList()->AppendL(conversation);
       
   598 
       
   599     // Send add notify to conversation list
       
   600     CCsClientConversation* clientConv =
       
   601             iConversationCache->CreateClientConvLC(conversation,
       
   602                                                    aConversationEntry);
       
   603     
       
   604     // THIS IS USED FOR SHOWING NOTIFICATIONS. NOTIFICATION IS SHOWN ONLY
       
   605     // WHEN ECsAttributeNewEntryAdded IS SET.
       
   606     if(iConversationEvent->IsUpdateConversationEventSet()) 
       
   607         {
       
   608         clientConv->GetConversationEntry()->
       
   609         ChangeAttributes(ECsAttributeNewEntryAdded,ECsAttributeNone);
       
   610         }
       
   611 
       
   612     iConversationCache->NotifyL(clientConv, KConversationListEventNew);
       
   613     CleanupStack::PopAndDestroy(clientConv);
       
   614 
       
   615     CleanupStack::Pop(conversation);
       
   616 
       
   617     PRINT ( _L("CCsConversationCacheHelper::AddNewConversationL - Conversation Added") );
       
   618 }
       
   619 
       
   620 // ----------------------------------------------------------------------------
       
   621 // CCsConversationCache::GetPendingEventCount
       
   622 // Pending event count
       
   623 // ----------------------------------------------------------------------------
       
   624 TInt CCsConversationCacheHelper::GetPendingEventCount()
       
   625 {
       
   626     return iConversationEventList->Count();
       
   627 }
       
   628 
       
   629 // ---------------------------------------------------------------------------
       
   630 // Get next event from event list
       
   631 // ---------------------------------------------------------------------------
       
   632 //
       
   633 CCsConversationEvent* CCsConversationCacheHelper::GetNextEvent()
       
   634 {
       
   635     // check in case there are no events in cache
       
   636     if (iConversationEventList->Count() > 0)
       
   637     {
       
   638         return (*iConversationEventList)[0];
       
   639     }
       
   640     return NULL;
       
   641 }
       
   642 
       
   643 // ---------------------------------------------------------------------------
       
   644 // Find CCsConversationEvent from event Q based on ConversationEntryId.
       
   645 // ---------------------------------------------------------------------------
       
   646 TInt CCsConversationCacheHelper::FindEvent(
       
   647                                            const CCsConversationEvent& aConvEvent)
       
   648 {
       
   649     // find the event inside event list and send the index
       
   650     // KErrNotFound in  case None
       
   651     TInt
       
   652             index =
       
   653                     iConversationEventList->Find(&aConvEvent,
       
   654                                                  CCsConversationEvent::CompareByEntryId);
       
   655 
       
   656     return index;
       
   657 }
       
   658 
       
   659 // ----------------------------------------------------------------------------
       
   660 // CCsConversationCache::GetNextRowId
       
   661 // Get the conversation list count where to add next conversation
       
   662 // ----------------------------------------------------------------------------
       
   663 TInt16 CCsConversationCacheHelper::GetNextRowId()
       
   664 {
       
   665     // increment the index and return
       
   666     iConversationIndex++;
       
   667     return iConversationIndex;
       
   668 }
       
   669 
       
   670 // ---------------------------------------------------------------------------
       
   671 // Find CCsConversationEvent based on ConversationEntryId and delete it.
       
   672 // ---------------------------------------------------------------------------
       
   673 //
       
   674 void CCsConversationCacheHelper::DeleteEvent(
       
   675                                              const CCsConversationEvent& aConvEvent)
       
   676 {
       
   677     TInt index = FindEvent(aConvEvent);
       
   678 
       
   679     if (KErrNotFound != index)
       
   680     {
       
   681         CCsConversationEvent* conversationEvent =
       
   682                 (*iConversationEventList)[index];
       
   683         iConversationEventList->Remove(index);
       
   684         delete conversationEvent;
       
   685     }
       
   686 }
       
   687 
       
   688 // ----------------------------------------------------------------------------
       
   689 // CCsConversationCache::IsNotifyRequiredL
       
   690 // checks whether notification for conversation list is required
       
   691 // ----------------------------------------------------------------------------
       
   692 TBool CCsConversationCacheHelper::IsNotifyRequiredL(
       
   693                                                     CCsConversation* aConversation,
       
   694                                                     CCsConversationEntry* aConversationEntry,
       
   695                                                     TUint32 aEvent,
       
   696                                                     TUint16 aPreviousUnreadMsgsCount)
       
   697 {
       
   698     TUint16 presentUnreadMessageCount = aConversation->GetUnreadMessageCount();
       
   699     CCsConversationEntry* latestEntry = aConversation->GetLatestEntryL();
       
   700 
       
   701     // Add & Update
       
   702     if (aEvent & KConversationListEventNew || aEvent
       
   703             & KConversationListEventUpdate)
       
   704     {
       
   705         // Check whether deleted is ongoing. Don't Notify.
       
   706         if (aConversation->IsDeleted())
       
   707         {
       
   708             return EFalse;
       
   709         }
       
   710 
       
   711         // Check whether latest entry is same and unread count has not changed. Don't Notify.
       
   712         if (CCsConversationEntry::Compare(*latestEntry, *aConversationEntry)
       
   713                 == 0 && (presentUnreadMessageCount == aPreviousUnreadMsgsCount)
       
   714                 && (latestEntry->GetSendState()
       
   715                         == aConversationEntry->GetSendState()))
       
   716         {
       
   717             return EFalse;
       
   718         }
       
   719     }
       
   720 
       
   721     // Delete
       
   722     if (aEvent & KConversationListEventDelete)
       
   723     {
       
   724         // Not Last entry during delete. Don't Notify.
       
   725         if (aConversation->IsDeleted() && aConversation->GetEntryCount() > 0)
       
   726         {
       
   727             return EFalse;
       
   728         }
       
   729     }
       
   730 
       
   731     // Notify.
       
   732     return ETrue;
       
   733 }
       
   734 
       
   735 // ----------------------------------------------------------------------------
       
   736 // CCsConversationCacheHelper::ConversationEventList
       
   737 // Pls refer to .h file
       
   738 // ----------------------------------------------------------------------------
       
   739 RPointerArray<CCsConversationEvent>*
       
   740 CCsConversationCacheHelper::ConversationEventList()
       
   741 {
       
   742     return iConversationEventList;
       
   743 }
       
   744 
       
   745 //end of file