| 31 |      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 (ECsBlueTooth == aConversationEntry->GetType())
 | 
|  |    294 |     {
 | 
|  |    295 |         return 1;
 | 
|  |    296 |     }
 | 
|  |    297 |     return 0;
 | 
|  |    298 | }
 | 
|  |    299 | 
 | 
|  |    300 | // ---------------------------------------------------------------------------
 | 
|  |    301 | // Move to next state
 | 
|  |    302 | // ---------------------------------------------------------------------------
 | 
|  |    303 | //
 | 
|  |    304 | void CCsConversationCacheHelper::IssueRequest()
 | 
|  |    305 | {
 | 
|  |    306 |     if (!IsActive())
 | 
|  |    307 |     {
 | 
|  |    308 |         iStatus = KRequestPending;
 | 
|  |    309 |         TRequestStatus* status = &iStatus;
 | 
|  |    310 |         SetActive();
 | 
|  |    311 |         User::RequestComplete(status, KErrNone);
 | 
|  |    312 |     }
 | 
|  |    313 | }
 | 
|  |    314 | 
 | 
|  |    315 | // ----------------------------------------------------------------------------
 | 
|  |    316 | // CCsConversationCacheHelper::ContactResolved
 | 
|  |    317 | // Handle a contact resolved in the contact database
 | 
|  |    318 | // ----------------------------------------------------------------------------
 | 
|  |    319 | void CCsConversationCacheHelper::ResolveContact(
 | 
|  |    320 |                                                 CCsConversationEvent* aConverastionEvent)
 | 
|  |    321 | {
 | 
|  |    322 |     CCsContactDetail contactDetail;
 | 
|  |    323 | 
 | 
|  |    324 |     //get contact from event
 | 
|  |    325 |     CCsConversationEntry* ConvEntry =
 | 
|  |    326 |             aConverastionEvent->ClientConversation()->GetConversationEntry();
 | 
|  |    327 | 
 | 
|  |    328 |     if (ConvEntry)
 | 
|  |    329 |     {
 | 
|  |    330 |         HBufC* Contact = ConvEntry->Contact();
 | 
|  |    331 |         if (Contact)
 | 
|  |    332 |         {
 | 
|  |    333 |             QString contactAddress =
 | 
|  |    334 |                     S60QConversions::s60DescToQString(Contact->Des());
 | 
|  |    335 | 
 | 
|  |    336 |             iConversationCache->ContactsManager()->resolver()->resolveContact(contactAddress,
 | 
|  |    337 |                                                                               contactDetail);
 | 
|  |    338 | 
 | 
|  |    339 |             int contactId = contactDetail.contactId;
 | 
|  |    340 |             TInt cIndex = iConversationCache->FindConversation(contactId);
 | 
|  |    341 | 
 | 
|  |    342 |             if (cIndex == KErrNotFound)
 | 
|  |    343 |             {
 | 
|  |    344 |                 // Add as new conversation
 | 
|  |    345 |                 HBufC* displayName = NULL;
 | 
|  |    346 |                 if(!contactDetail.displayName.isEmpty())
 | 
|  |    347 |                 {
 | 
|  |    348 |                     displayName=S60QConversions::qStringToS60Desc(contactDetail.displayName);
 | 
|  |    349 |                 }
 | 
|  |    350 |                 TRAPD(error, AddNewConversationL( aConverastionEvent->
 | 
|  |    351 |                                 ClientConversation()->GetConversationEntry(),
 | 
|  |    352 |                                 contactId,
 | 
|  |    353 |                                 displayName));
 | 
|  |    354 | 
 | 
|  |    355 |                 if (displayName) delete displayName;                
 | 
|  |    356 | 
 | 
|  |    357 |                 if (error != KErrNone)
 | 
|  |    358 |                 {
 | 
|  |    359 |                     // handle error
 | 
|  |    360 |                 }
 | 
|  |    361 |             }
 | 
|  |    362 |             else
 | 
|  |    363 |             {
 | 
|  |    364 |                 // this is when two contacts are having same contact Id,
 | 
|  |    365 |                 // in that case it should add into an existing conversation
 | 
|  |    366 |                 TRAPD(error, AddConversationEntryL( 
 | 
|  |    367 |                         aConverastionEvent->ClientConversation(
 | 
|  |    368 |                                 )->GetConversationEntry(), cIndex));
 | 
|  |    369 |                 if (error != KErrNone)
 | 
|  |    370 |                 {
 | 
|  |    371 |                     // handle error
 | 
|  |    372 |                 }
 | 
|  |    373 |             }
 | 
|  |    374 | 
 | 
|  |    375 |             // now remove the entry from event list
 | 
|  |    376 |             DeleteEvent(*aConverastionEvent);
 | 
|  |    377 | 
 | 
|  |    378 |             // call start update cache
 | 
|  |    379 |             StartCacheUpdate();
 | 
|  |    380 |         }
 | 
|  |    381 |     }
 | 
|  |    382 | }
 | 
|  |    383 | 
 | 
|  |    384 | // ----------------------------------------------------------------------------
 | 
|  |    385 | // Handles the case where conversation is already in cache
 | 
|  |    386 | // This will add new conversation entry into the corresponding conversation
 | 
|  |    387 | // pls note there is already a conversation exist for this entry
 | 
|  |    388 | // hence this entry gets appended inside conversation list
 | 
|  |    389 | // ----------------------------------------------------------------------------
 | 
|  |    390 | void CCsConversationCacheHelper::AddConversationEntryL(
 | 
|  |    391 |                                                        CCsConversationEntry *aConEntry,
 | 
|  |    392 |                                                        TInt aConversationIndex)
 | 
|  |    393 | {
 | 
|  |    394 |     RPointerArray<CCsConversation>* conversationList =
 | 
|  |    395 |             iConversationCache->ConversationList();
 | 
|  |    396 | 
 | 
|  |    397 |     CCsConversation* conversation = (*conversationList)[aConversationIndex];
 | 
|  |    398 | 
 | 
|  |    399 |     CCsConversationEntry* prevLatestEntry = conversation->GetLatestEntryL();
 | 
|  |    400 | 
 | 
|  |    401 |     if (prevLatestEntry)
 | 
|  |    402 |     {
 | 
|  |    403 |         prevLatestEntry = prevLatestEntry->CloneL();
 | 
|  |    404 |     }
 | 
|  |    405 |     else
 | 
|  |    406 |     {
 | 
|  |    407 |         prevLatestEntry = aConEntry->CloneL();
 | 
|  |    408 |     }
 | 
|  |    409 | 
 | 
|  |    410 |     CleanupStack::PushL(prevLatestEntry);
 | 
|  |    411 | 
 | 
|  |    412 |     // save the previous unread message count
 | 
|  |    413 |     TUint16 prevUnreadCount = conversation->GetUnreadMessageCount();
 | 
|  |    414 | 
 | 
|  |    415 |     TUint32 event;
 | 
|  |    416 |     conversation->UpdateEntryL(aConEntry, event);
 | 
|  |    417 | 
 | 
|  |    418 |     // Send update notify to conversation list
 | 
|  |    419 |     if (IsNotifyRequiredL(conversation,
 | 
|  |    420 |                           prevLatestEntry,
 | 
|  |    421 |                           KConversationListEventUpdate,
 | 
|  |    422 |                           prevUnreadCount))
 | 
|  |    423 |     {
 | 
|  |    424 |         CCsConversationEntry* convEntry = conversation->GetLatestEntryL();
 | 
|  |    425 |         CCsClientConversation* clientConvLatest =
 | 
|  |    426 |                 iConversationCache->CreateClientConvLC(conversation, convEntry);
 | 
|  |    427 |         
 | 
|  |    428 |         // THIS IS USED FOR SHOWING NOTIFICATIONS. NOTIFICATION IS SHOWN ONLY
 | 
|  |    429 |         // WHEN ECsAttributeNewEntryAdded IS SET. 
 | 
|  |    430 |         if(iConversationEvent->IsUpdateConversationEventSet() 
 | 
|  |    431 |                 && event == KConversationEventNew)
 | 
|  |    432 |             {
 | 
|  |    433 |             clientConvLatest->GetConversationEntry()->
 | 
|  |    434 |             ChangeAttributes(ECsAttributeNewEntryAdded,ECsAttributeNone);
 | 
|  |    435 |             }
 | 
|  |    436 |         
 | 
|  |    437 |         iConversationCache->NotifyL(clientConvLatest,
 | 
|  |    438 |                                     KConversationListEventUpdate);
 | 
|  |    439 |         CleanupStack::PopAndDestroy(clientConvLatest);
 | 
|  |    440 |     }
 | 
|  |    441 | 
 | 
|  |    442 |     // Send update notify to conversations
 | 
|  |    443 |     CCsClientConversation* clientConv =
 | 
|  |    444 |             iConversationCache->CreateClientConvLC(conversation, aConEntry);
 | 
|  |    445 |     iConversationCache->NotifyL(clientConv, event);
 | 
|  |    446 | 
 | 
|  |    447 |     // Cleanup
 | 
|  |    448 |     CleanupStack::PopAndDestroy(clientConv);
 | 
|  |    449 |     CleanupStack::PopAndDestroy(prevLatestEntry);
 | 
|  |    450 | }
 | 
|  |    451 | 
 | 
|  |    452 | // ----------------------------------------------------------------------------
 | 
|  |    453 | // CCsConversationCache::DeleteConversationL
 | 
|  |    454 | // Delete conversation entry from conversation list
 | 
|  |    455 | // there are few checks of how to notify the UI about the changes 
 | 
|  |    456 | // for the various use cases. 
 | 
|  |    457 | // Delete requires the conversation Id and type (SMS, MMS etc.)
 | 
|  |    458 | // ----------------------------------------------------------------------------
 | 
|  |    459 | void CCsConversationCacheHelper::DeleteConversationEntryL(
 | 
|  |    460 |                                                           CCsConversationEntry* aConversationEntry)
 | 
|  |    461 | {
 | 
|  |    462 |     // For MMS, search the entry inside all conversations and delete.
 | 
|  |    463 |     // For SMS, stop on first match.
 | 
|  |    464 |     TBool stopOnFirstMatch = ETrue;
 | 
|  |    465 | 
 | 
|  |    466 |     RPointerArray<CCsConversation>* conversationList =
 | 
|  |    467 |             iConversationCache->ConversationList();
 | 
|  |    468 | 
 | 
|  |    469 |     // the deletion needs search the entryId inside whole conversation
 | 
|  |    470 |     // search the entry id and delete the entry
 | 
|  |    471 |     for (TInt loop = 0; loop < iConversationCache->ConversationList()->Count(); loop++)
 | 
|  |    472 |     {
 | 
|  |    473 |         CCsConversation* conversation =
 | 
|  |    474 |                 static_cast<CCsConversation*> ( (*conversationList)[loop]);
 | 
|  |    475 | 
 | 
|  |    476 |         TInt indexDeletion = conversation->FindEntry(aConversationEntry);
 | 
|  |    477 | 
 | 
|  |    478 |         if (indexDeletion != KErrNotFound)
 | 
|  |    479 |         {
 | 
|  |    480 |             // Get the entry from cache to check the type
 | 
|  |    481 |             CCsConversationEntry* cacheEntry =
 | 
|  |    482 |                     conversation->GetEntryL(indexDeletion);
 | 
|  |    483 | 
 | 
|  |    484 |             if (cacheEntry->GetType() == ECsMMS || cacheEntry->GetType()
 | 
|  |    485 |                     == ECsAudio)
 | 
|  |    486 |             {
 | 
|  |    487 |                 stopOnFirstMatch = EFalse;
 | 
|  |    488 |             }
 | 
|  |    489 |             
 | 
|  |    490 |             TUint16 prevUnreadCount = conversation->GetUnreadMessageCount();
 | 
|  |    491 | 
 | 
|  |    492 |             // Delete the conversation entry from this conversation
 | 
|  |    493 |             conversation->DeleteEntryL(indexDeletion);
 | 
|  |    494 | 
 | 
|  |    495 |             // Notify client of conversation list change.
 | 
|  |    496 |             if (IsNotifyRequiredL(conversation,
 | 
|  |    497 |                                   aConversationEntry,
 | 
|  |    498 |                                   KConversationListEventDelete,
 | 
|  |    499 |                                   prevUnreadCount))
 | 
|  |    500 |             {
 | 
|  |    501 |                 // Delete
 | 
|  |    502 |                 CCsClientConversation
 | 
|  |    503 |                         * clientConv =
 | 
|  |    504 |                                 iConversationCache->CreateClientConvLC(conversation,
 | 
|  |    505 |                                                                        aConversationEntry);
 | 
|  |    506 |                 iConversationCache->NotifyL(clientConv,
 | 
|  |    507 |                                             KConversationListEventDelete);
 | 
|  |    508 |                 CleanupStack::PopAndDestroy(clientConv);
 | 
|  |    509 | 
 | 
|  |    510 |                 // Update with latest entry
 | 
|  |    511 |                 CCsConversationEntry* conEntry =
 | 
|  |    512 |                         conversation->GetLatestEntryL();
 | 
|  |    513 |                 if (conEntry)
 | 
|  |    514 |                 {
 | 
|  |    515 |                     CCsClientConversation
 | 
|  |    516 |                             * clientConv =
 | 
|  |    517 |                                     iConversationCache->CreateClientConvLC(conversation,
 | 
|  |    518 |                                                                            conEntry);
 | 
|  |    519 |                     iConversationCache->NotifyL(clientConv,
 | 
|  |    520 |                                                 KConversationListEventNew);
 | 
|  |    521 |                     CleanupStack::PopAndDestroy(clientConv);
 | 
|  |    522 |                 }
 | 
|  |    523 |             }
 | 
|  |    524 | 
 | 
|  |    525 |             // Notify client of conversation change
 | 
|  |    526 |             if (!conversation->IsDeleted())
 | 
|  |    527 |             {
 | 
|  |    528 |                 CCsClientConversation
 | 
|  |    529 |                         * clientConv =
 | 
|  |    530 |                                 iConversationCache->CreateClientConvLC(conversation,
 | 
|  |    531 |                                                                        aConversationEntry);
 | 
|  |    532 |                 iConversationCache->NotifyL(clientConv,
 | 
|  |    533 |                                             KConversationEventDelete);
 | 
|  |    534 |                 CleanupStack::PopAndDestroy(clientConv);
 | 
|  |    535 |             }
 | 
|  |    536 | 
 | 
|  |    537 |             // check if all entries are deleted then 
 | 
|  |    538 |             // delete the conversation from cache
 | 
|  |    539 |             if (conversation->GetEntryCount() == 0
 | 
|  |    540 |                     && !conversation->IsSpecialConversation())
 | 
|  |    541 |             {
 | 
|  |    542 |                 conversationList->Remove(loop);
 | 
|  |    543 |                 delete conversation;
 | 
|  |    544 |                 //reset the counters
 | 
|  |    545 |                 loop -= 1;
 | 
|  |    546 |             }
 | 
|  |    547 | 
 | 
|  |    548 |             // Stop searching    
 | 
|  |    549 |             if (stopOnFirstMatch) break;
 | 
|  |    550 |         }
 | 
|  |    551 |     } // for
 | 
|  |    552 | }
 | 
|  |    553 | 
 | 
|  |    554 | // ----------------------------------------------------------------------------
 | 
|  |    555 | // CCsConversationCacheHelper::AddNewConversationL
 | 
|  |    556 | // add new conversation into cache
 | 
|  |    557 | // this shall be called after resolving entry from phonebook
 | 
|  |    558 | // ----------------------------------------------------------------------------
 | 
|  |    559 | void CCsConversationCacheHelper::AddNewConversationL(
 | 
|  |    560 |                                                      CCsConversationEntry* aConversationEntry,
 | 
|  |    561 |                                                      TInt32 aContactId,
 | 
|  |    562 |                                                      const HBufC* aDisplayName)
 | 
|  |    563 | {
 | 
|  |    564 |     CCsConversation* conversation = CCsConversation::NewL();
 | 
|  |    565 |     CleanupStack::PushL(conversation);
 | 
|  |    566 | 
 | 
|  |    567 |     //set conversation entry id
 | 
|  |    568 |     // by default just set the count of list as of now, as it will be unique
 | 
|  |    569 |     conversation->SetConversationId(GetNextRowId());
 | 
|  |    570 | 
 | 
|  |    571 |     // add the conversation entry
 | 
|  |    572 |     conversation->AddEntryL(aConversationEntry);
 | 
|  |    573 | 
 | 
|  |    574 |     // fill firstname and lastname and contact Id
 | 
|  |    575 |     conversation->AddContactDetailsL(aContactId,
 | 
|  |    576 |                                      *aDisplayName);
 | 
|  |    577 | 
 | 
|  |    578 |     // fill the phone number
 | 
|  |    579 |     if (aConversationEntry->Contact())
 | 
|  |    580 |     {
 | 
|  |    581 |         conversation->AddContactDetailsL(* (aConversationEntry->Contact()));
 | 
|  |    582 |     }
 | 
|  |    583 | 
 | 
|  |    584 |     // add into the list
 | 
|  |    585 |     iConversationCache->ConversationList()->AppendL(conversation);
 | 
|  |    586 | 
 | 
|  |    587 |     // Send add notify to conversation list
 | 
|  |    588 |     CCsClientConversation* clientConv =
 | 
|  |    589 |             iConversationCache->CreateClientConvLC(conversation,
 | 
|  |    590 |                                                    aConversationEntry);
 | 
|  |    591 |     
 | 
|  |    592 |     // THIS IS USED FOR SHOWING NOTIFICATIONS. NOTIFICATION IS SHOWN ONLY
 | 
|  |    593 |     // WHEN ECsAttributeNewEntryAdded IS SET.
 | 
|  |    594 |     if(iConversationEvent->IsUpdateConversationEventSet()) 
 | 
|  |    595 |         {
 | 
|  |    596 |         clientConv->GetConversationEntry()->
 | 
|  |    597 |         ChangeAttributes(ECsAttributeNewEntryAdded,ECsAttributeNone);
 | 
|  |    598 |         }
 | 
|  |    599 | 
 | 
|  |    600 |     iConversationCache->NotifyL(clientConv, KConversationListEventNew);
 | 
|  |    601 |     CleanupStack::PopAndDestroy(clientConv);
 | 
|  |    602 | 
 | 
|  |    603 |     CleanupStack::Pop(conversation);
 | 
|  |    604 | 
 | 
|  |    605 |     PRINT ( _L("CCsConversationCacheHelper::AddNewConversationL - Conversation Added") );
 | 
|  |    606 | }
 | 
|  |    607 | 
 | 
|  |    608 | // ----------------------------------------------------------------------------
 | 
|  |    609 | // CCsConversationCache::GetPendingEventCount
 | 
|  |    610 | // Pending event count
 | 
|  |    611 | // ----------------------------------------------------------------------------
 | 
|  |    612 | TInt CCsConversationCacheHelper::GetPendingEventCount()
 | 
|  |    613 | {
 | 
|  |    614 |     return iConversationEventList->Count();
 | 
|  |    615 | }
 | 
|  |    616 | 
 | 
|  |    617 | // ---------------------------------------------------------------------------
 | 
|  |    618 | // Get next event from event list
 | 
|  |    619 | // ---------------------------------------------------------------------------
 | 
|  |    620 | //
 | 
|  |    621 | CCsConversationEvent* CCsConversationCacheHelper::GetNextEvent()
 | 
|  |    622 | {
 | 
|  |    623 |     // check in case there are no events in cache
 | 
|  |    624 |     if (iConversationEventList->Count() > 0)
 | 
|  |    625 |     {
 | 
|  |    626 |         return (*iConversationEventList)[0];
 | 
|  |    627 |     }
 | 
|  |    628 |     return NULL;
 | 
|  |    629 | }
 | 
|  |    630 | 
 | 
|  |    631 | // ---------------------------------------------------------------------------
 | 
|  |    632 | // Find CCsConversationEvent from event Q based on ConversationEntryId.
 | 
|  |    633 | // ---------------------------------------------------------------------------
 | 
|  |    634 | TInt CCsConversationCacheHelper::FindEvent(
 | 
|  |    635 |                                            const CCsConversationEvent& aConvEvent)
 | 
|  |    636 | {
 | 
|  |    637 |     // find the event inside event list and send the index
 | 
|  |    638 |     // KErrNotFound in  case None
 | 
|  |    639 |     TInt
 | 
|  |    640 |             index =
 | 
|  |    641 |                     iConversationEventList->Find(&aConvEvent,
 | 
|  |    642 |                                                  CCsConversationEvent::CompareByEntryId);
 | 
|  |    643 | 
 | 
|  |    644 |     return index;
 | 
|  |    645 | }
 | 
|  |    646 | 
 | 
|  |    647 | // ----------------------------------------------------------------------------
 | 
|  |    648 | // CCsConversationCache::GetNextRowId
 | 
|  |    649 | // Get the conversation list count where to add next conversation
 | 
|  |    650 | // ----------------------------------------------------------------------------
 | 
|  |    651 | TInt16 CCsConversationCacheHelper::GetNextRowId()
 | 
|  |    652 | {
 | 
|  |    653 |     // increment the index and return
 | 
|  |    654 |     iConversationIndex++;
 | 
|  |    655 |     return iConversationIndex;
 | 
|  |    656 | }
 | 
|  |    657 | 
 | 
|  |    658 | // ---------------------------------------------------------------------------
 | 
|  |    659 | // Find CCsConversationEvent based on ConversationEntryId and delete it.
 | 
|  |    660 | // ---------------------------------------------------------------------------
 | 
|  |    661 | //
 | 
|  |    662 | void CCsConversationCacheHelper::DeleteEvent(
 | 
|  |    663 |                                              const CCsConversationEvent& aConvEvent)
 | 
|  |    664 | {
 | 
|  |    665 |     TInt index = FindEvent(aConvEvent);
 | 
|  |    666 | 
 | 
|  |    667 |     if (KErrNotFound != index)
 | 
|  |    668 |     {
 | 
|  |    669 |         CCsConversationEvent* conversationEvent =
 | 
|  |    670 |                 (*iConversationEventList)[index];
 | 
|  |    671 |         iConversationEventList->Remove(index);
 | 
|  |    672 |         delete conversationEvent;
 | 
|  |    673 |     }
 | 
|  |    674 | }
 | 
|  |    675 | 
 | 
|  |    676 | // ----------------------------------------------------------------------------
 | 
|  |    677 | // CCsConversationCache::IsNotifyRequiredL
 | 
|  |    678 | // checks whether notification for conversation list is required
 | 
|  |    679 | // ----------------------------------------------------------------------------
 | 
|  |    680 | TBool CCsConversationCacheHelper::IsNotifyRequiredL(
 | 
|  |    681 |                                                     CCsConversation* aConversation,
 | 
|  |    682 |                                                     CCsConversationEntry* aConversationEntry,
 | 
|  |    683 |                                                     TUint32 aEvent,
 | 
|  |    684 |                                                     TUint16 aPreviousUnreadMsgsCount)
 | 
|  |    685 | {
 | 
|  |    686 |     TUint16 presentUnreadMessageCount = aConversation->GetUnreadMessageCount();
 | 
|  |    687 |     CCsConversationEntry* latestEntry = aConversation->GetLatestEntryL();
 | 
|  |    688 | 
 | 
|  |    689 |     // Add & Update
 | 
|  |    690 |     if (aEvent & KConversationListEventNew || aEvent
 | 
|  |    691 |             & KConversationListEventUpdate)
 | 
|  |    692 |     {
 | 
|  |    693 |         // Check whether deleted is ongoing. Don't Notify.
 | 
|  |    694 |         if (aConversation->IsDeleted())
 | 
|  |    695 |         {
 | 
|  |    696 |             return EFalse;
 | 
|  |    697 |         }
 | 
|  |    698 | 
 | 
|  |    699 |         // Check whether latest entry is same and unread count has not changed. Don't Notify.
 | 
|  |    700 |         if (CCsConversationEntry::Compare(*latestEntry, *aConversationEntry)
 | 
|  |    701 |                 == 0 && (presentUnreadMessageCount == aPreviousUnreadMsgsCount)
 | 
|  |    702 |                 && (latestEntry->GetSendState()
 | 
|  |    703 |                         == aConversationEntry->GetSendState()))
 | 
|  |    704 |         {
 | 
|  |    705 |             return EFalse;
 | 
|  |    706 |         }
 | 
|  |    707 |     }
 | 
|  |    708 | 
 | 
|  |    709 |     // Delete
 | 
|  |    710 |     if (aEvent & KConversationListEventDelete)
 | 
|  |    711 |     {
 | 
|  |    712 |         // Not Last entry during delete. Don't Notify.
 | 
|  |    713 |         if (aConversation->IsDeleted() && aConversation->GetEntryCount() > 0)
 | 
|  |    714 |         {
 | 
|  |    715 |             return EFalse;
 | 
|  |    716 |         }
 | 
|  |    717 |     }
 | 
|  |    718 | 
 | 
|  |    719 |     // Notify.
 | 
|  |    720 |     return ETrue;
 | 
|  |    721 | }
 | 
|  |    722 | 
 | 
|  |    723 | // ----------------------------------------------------------------------------
 | 
|  |    724 | // CCsConversationCacheHelper::ConversationEventList
 | 
|  |    725 | // Pls refer to .h file
 | 
|  |    726 | // ----------------------------------------------------------------------------
 | 
|  |    727 | RPointerArray<CCsConversationEvent>*
 | 
|  |    728 | CCsConversationCacheHelper::ConversationEventList()
 | 
|  |    729 | {
 | 
|  |    730 |     return iConversationEventList;
 | 
|  |    731 | }
 | 
|  |    732 | 
 | 
|  |    733 | //end of file
 |