uiservicetab/vimpstcmdprocess/src/cvimpstprocessArray.cpp
branchRCL_3
changeset 22 3104fc151679
parent 21 2b7283837edb
child 23 9a48e301e94b
equal deleted inserted replaced
21:2b7283837edb 22:3104fc151679
     1 /*
       
     2 * Copyright (c) 2008 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:  implementation for command process array
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <coemain.h>
       
    21 
       
    22 #include "uiservicetabtracer.h"
       
    23 
       
    24 #include "cvimpstprocessarray.h"
       
    25 
       
    26 #include "mvimpstprocessArrayobserver.h"
       
    27 #include "cvimpststoragemanagerfactory.h"
       
    28 #include "mvimpststorageserviceview.h"
       
    29 #include "tvimpstenums.h"
       
    30 #include "cvimpstprocessarrayitem.h"
       
    31 #include "mvimpststorageitemmodel.h"
       
    32 #include "tvimpstconsts.h"
       
    33 #include "mvimpststoragecontactlist.h"
       
    34 #include "mvimpststoragevpbkstorehandler.h"
       
    35 #include "mvimpstengine.h"
       
    36 #include "cvimpstprocessfriendrequestitem.h"
       
    37 #include "mvimpstenginesubservice.h"
       
    38 #include <MVPbkFieldType.h>
       
    39 #include <MVPbkContactFieldUriData.h>
       
    40 #include <MVPbkContactFieldTextData.h>
       
    41 //rsg file for resource id
       
    42 #include <vimpstuires.rsg>
       
    43 #include "vimpstutils.h"
       
    44 #include <MVPbkContactLink.h>
       
    45 #include <VPbkEng.rsg>
       
    46 #include "mvimpstengineimsubservice.h"
       
    47 #include "mvimpstenginepresencesubservice.h"
       
    48 
       
    49 #include <apgtask.h> 
       
    50 #include "imcvuiparams.h"
       
    51 
       
    52 // --------------------------------------------------------------------------
       
    53 // CVIMPSTProcessArray::CIMArrayProcess
       
    54 // --------------------------------------------------------------------------
       
    55 //
       
    56 CVIMPSTProcessArray::CVIMPSTProcessArray( MVIMPSTEngine& aEngine)
       
    57 :iOwnDataIndex(KOwnDataIndex),
       
    58 iContactListIndex(KOwnDataIndex),
       
    59 iEngine(aEngine)
       
    60         {
       
    61         iServiceId = iEngine.ServiceId();
       
    62         }
       
    63 
       
    64 // --------------------------------------------------------------------------
       
    65 // CVIMPSTProcessArray::~CVIMPSTProcessArray
       
    66 // --------------------------------------------------------------------------
       
    67 //
       
    68 CVIMPSTProcessArray::~CVIMPSTProcessArray()
       
    69     {
       
    70    
       
    71     TRACER_AUTO;
       
    72     if(iContactInterface)
       
    73         {
       
    74         iContactInterface->RemoveObserver( this );	
       
    75         }
       
    76     iItemArray.ResetAndDestroy();
       
    77     //iItemArray.Close();
       
    78     
       
    79     
       
    80     iUnKnownContactArray.Close();
       
    81     iAddRequestArray.Close();
       
    82     
       
    83     if(iData)
       
    84         {
       
    85         delete iData;
       
    86         iData = NULL;
       
    87         }
       
    88     //Get IM SubService     
       
    89     MVIMPSTEngineSubService* subService1 =    	   
       
    90     (iEngine.SubService(TVIMPSTEnums::EIM));
       
    91 
       
    92     if(subService1)
       
    93         {
       
    94         MVIMPSTEngineIMSubService& imSubService = 
       
    95         MVIMPSTEngineIMSubService::Cast (*subService1);
       
    96         imSubService.UnRegisterChatObserver(this);
       
    97         }
       
    98 
       
    99     //subscribe for ownpresencechangeevent    			
       
   100     MVIMPSTEngineSubService* subService =    	   
       
   101     (iEngine.SubService(TVIMPSTEnums::EPresence));
       
   102 
       
   103     if(subService)
       
   104         {
       
   105         MVIMPSTEnginePresenceSubService& presence = 
       
   106         MVIMPSTEnginePresenceSubService::Cast (*subService);
       
   107         presence.UnRegisterPresenceEventObserver(this);
       
   108         } 
       
   109 
       
   110     
       
   111     }
       
   112 
       
   113 // --------------------------------------------------------------------------
       
   114 // CVIMPSTProcessArray::NewL
       
   115 // --------------------------------------------------------------------------
       
   116 //
       
   117 CVIMPSTProcessArray* CVIMPSTProcessArray::NewL(
       
   118         MVIMPSTEngine& aEngine)
       
   119     {
       
   120     TRACER_AUTO;
       
   121     CVIMPSTProcessArray* self = new(ELeave) CVIMPSTProcessArray(aEngine);
       
   122     CleanupStack::PushL(self);
       
   123     self->ConstructL();
       
   124     CleanupStack::Pop(self);   
       
   125     return self;
       
   126     
       
   127     }
       
   128 
       
   129 // --------------------------------------------------------------------------
       
   130 // CVIMPSTProcessArray::ConstructL
       
   131 // --------------------------------------------------------------------------
       
   132 //
       
   133 void CVIMPSTProcessArray::ConstructL()
       
   134     {
       
   135    
       
   136     TRACER_AUTO;
       
   137     iContactInterface = CVIMPSTStorageManagerFactory::ContactListInterfaceL(iServiceId);
       
   138     if(iContactInterface)
       
   139         {        
       
   140         iContactInterface->AddObserverL( this );// listen these events..        
       
   141         HBufC* unnamed = VIMPSTUtils::LoadResourceL( R_SERVTAB_UNNAMED );
       
   142         if( unnamed )
       
   143             {
       
   144             CleanupStack::PushL( unnamed );
       
   145             iContactInterface->SetUnnamedTextL(unnamed); // takes ownership
       
   146             CleanupStack::Pop( unnamed );	
       
   147             }
       
   148         }
       
   149     iContactListModel = CVIMPSTStorageManagerFactory::ItemModelInterfaceL(iServiceId);
       
   150     iLoginState = iEngine.ServiceState();
       
   151     
       
   152     iIMSupported = EFalse;
       
   153     iPresenceSupported = EFalse;
       
   154     //Get IM SubService     
       
   155     MVIMPSTEngineSubService* subService1 =(iEngine.SubService(TVIMPSTEnums::EIM));
       
   156     if(subService1)
       
   157         {
       
   158         MVIMPSTEngineIMSubService& imSubService = 
       
   159         MVIMPSTEngineIMSubService::Cast (*subService1);
       
   160         imSubService.RegisterChatObserver(this);
       
   161         iIMSupported = ETrue;
       
   162         }
       
   163     iData = HBufC::NewL(512);
       
   164     iAddRequestArray.Reset();
       
   165     iUnKnownContactArray.Reset();
       
   166     //subscribe for ownpresencechangeevent
       
   167     MVIMPSTEngineSubService* subService =    	   
       
   168     (iEngine.SubService(TVIMPSTEnums::EPresence));
       
   169     if(subService)
       
   170         {        
       
   171         MVIMPSTEnginePresenceSubService& presence = 
       
   172         MVIMPSTEnginePresenceSubService::Cast (*subService);
       
   173         presence.RegisterPresenceEventObserverL(this);     
       
   174         iPresenceSupported = ETrue;
       
   175         }
       
   176 	ResetArray();
       
   177 	
       
   178     }
       
   179 // --------------------------------------------------------------------------
       
   180 // CVIMPSTProcessArray::HandleStorageChangeL
       
   181 // --------------------------------------------------------------------------
       
   182 //
       
   183 void CVIMPSTProcessArray::HandleStorageChangeL( TVIMPSTEnums::TVIMPSTStorgaeEventType aEventType,
       
   184 			         							MVIMPSTStorageContactList* /*aList*/, 
       
   185 				        						MVIMPSTStorageContact* aContact,
       
   186 				        						TInt aContactIndex )
       
   187     {
       
   188    
       
   189     TRACER_AUTO;
       
   190     // 0th index OwnItem + unknow contacts + add request
       
   191     aContactIndex = aContactIndex + 1 + iUnKnownContactArray.Count() + iAddRequestArray.Count();
       
   192     
       
   193     switch( aEventType )
       
   194         {
       
   195         case TVIMPSTEnums::EStorageContactReadComplete:
       
   196             {           
       
   197             TRACE("EStorageContactReadComplete");
       
   198             if(iProcessObservers)
       
   199                 {
       
   200                 /* passing 0 so that the focus is on owndata item */
       
   201                 iProcessObservers->HandleAdditionL(TVIMPSTEnums::EOwnStatusItem, 0 ); // focus own item
       
   202                 }           
       
   203             break;
       
   204             }
       
   205         case TVIMPSTEnums::EStorageContactFetchComplete:
       
   206             {            
       
   207             TRACE("EStorageContactFetchComplete");
       
   208             TRACE( " iItemArray Count: %d", iItemArray.Count() );
       
   209 
       
   210             if(iProcessObservers)
       
   211                 {
       
   212                 /* passing 0 so that the focus is on owndata item */
       
   213                 iProcessObservers->HandleAdditionL(TVIMPSTEnums::EOwnStatusItem, 0 ); // focus own item
       
   214                 }
       
   215 
       
   216             ResetArray();
       
   217             iFetchCompleted = ETrue;
       
   218             GetAndCreateOpenChatListL();
       
   219             TRACE( "EStorageContactFetchComplete End" );
       
   220             break; 	
       
   221             }
       
   222         case TVIMPSTEnums::EStorageContactReading:
       
   223         case TVIMPSTEnums::EStorageContactFetching:
       
   224         case TVIMPSTEnums::EStorageContactSynchronizing:
       
   225             {
       
   226             TRACE( "EStorageContactFetching/EStorageContactFetching" );
       
   227             /*
       
   228              *	This event occurs whenever we have contacts added to contactlist. This happens in the following scenarios
       
   229              *	1. At the time of login, when we fetch contacts, contact-by-contact is added to the storage. so we get this event.
       
   230              * 	2. When we add a contact manually (either from phonebook, or manu adding etc.. ) we get this callback.
       
   231              */
       
   232             TRACE( "contactindex = %d",aContactIndex );
       
   233             CVIMPSTProcessContactItem* contactItem = CVIMPSTProcessContactItem::NewL (*this, const_cast<TDesC&>(aContact->Name() ),
       
   234                     const_cast<TDesC&>(aContact->UserId() ),
       
   235                     aContact->ContactLink(),
       
   236                     const_cast<TDesC&>(aContact->StatusText()),
       
   237                     aContact->OnlineStatus());
       
   238             
       
   239             contactItem->SetAvatarIndex(aContact->AvatarIndex());
       
   240 
       
   241             /* All the time the index should be less than the item array's count, but during the fetch of contacts from cdb file to the view, 
       
   242              * if we havent got a contactviewready, and during that if I get contactaddition, before getting contactFetchComplete, we should keep 
       
   243              * appending the items to the array for display, once we get the entire list, its sorted anyway while insertion, and when presence occurs
       
   244              * the contacts are re-sorted. 
       
   245              */
       
   246             TRACE( "itemarraycount = %d",iItemArray.Count() );
       
   247             if (aContactIndex >= iItemArray.Count() )
       
   248                 {                
       
   249                 TRACE( "append contact item %d", contactItem);
       
   250                 iItemArray.Append(contactItem);
       
   251                 }
       
   252             else
       
   253                 {              
       
   254                 TRACE( "Insert at index = %d", aContactIndex);
       
   255                 iItemArray.Insert(contactItem, aContactIndex );
       
   256                 }               
       
   257             if(iProcessObservers)
       
   258                 {
       
   259                 /* passing 0 so that the focus is on owndata item */
       
   260                 iProcessObservers->HandleAdditionL(TVIMPSTEnums::EOwnStatusItem, 0);
       
   261                 }
       
   262             if( aContact->AvatarContent().Length() && iProcessObservers )
       
   263                 {
       
   264                 iProcessObservers->HandleAvatarChangeL( aContact->UserId() );
       
   265                 }
       
   266            
       
   267             break;
       
   268             }
       
   269         case TVIMPSTEnums::EStorageEventContactAddition:
       
   270             {            
       
   271             TRACE("EStorageEventContactAddition");
       
   272 
       
   273             TBool removed = RemoveFromUnknonOrInvitationListL( aContact->UserId(), EFalse );
       
   274             if( removed )
       
   275                 {
       
   276                 aContactIndex = aContactIndex - 1; // one contact is removed from iUnknownContactArray 
       
   277                 }
       
   278             /*
       
   279              *	This event occurs whenever we have contacts added to contactlist. This happens in the following scenarios
       
   280              *	1. At the time of login, when we fetch contacts, contact-by-contact is added to the storage. so we get this event.
       
   281              * 	2. When we add a contact manually (either from phonebook, or manu adding etc.. ) we get this callback.
       
   282                  */
       
   283             TPtrC userId = aContact->UserId();
       
   284                 TRACE( "contactindex = %d",aContactIndex );
       
   285                 CVIMPSTProcessContactItem* contactItem = CVIMPSTProcessContactItem::NewL ( *this, aContact->Name() ,
       
   286                                                                                             userId,
       
   287                                                                                             aContact->ContactLink(),
       
   288                                                                                             const_cast<TDesC&>(aContact->StatusText()),
       
   289                                                                                             aContact->OnlineStatus() );
       
   290 
       
   291                 contactItem->SetAvatarIndex(aContact->AvatarIndex());
       
   292             /* All the time the index should be less than the item array's count, but during the fetch of contacts from cdb file to the view, 
       
   293              * if we havent got a contactviewready, and during that if I get contactaddition, before getting contactFetchComplete, we should keep 
       
   294              * appending the items to the array for display, once we get the entire list, its sorted anyway while insertion, and when presence occurs
       
   295              * the contacts are re-sorted. 
       
   296              */
       
   297             TRACE( "itemarraycount = %d",iItemArray.Count() );
       
   298             if (aContactIndex >= iItemArray.Count() )
       
   299                 {                
       
   300                 TRACE( "append contact item = %d", contactItem);
       
   301                 iItemArray.Append(contactItem);
       
   302                 }                
       
   303             else
       
   304                 {                
       
   305                 TRACE( "Insert at index = %d", aContactIndex);
       
   306                 iItemArray.Insert(contactItem, aContactIndex );
       
   307                 }                
       
   308              // check if  pending message exist
       
   309             if( TVIMPSTEnums::ESVCERegistered == iLoginState && userId.Length() )
       
   310                 {
       
   311                 if( IsConversationExistL( userId ) )
       
   312                     {
       
   313                     contactItem->SetConversationOpen(ETrue);
       
   314                     }
       
   315                 if( IsUnreadMessageExistsL( userId ) )
       
   316                     {
       
   317                     contactItem->SetMsgPending(ETrue);
       
   318                     }
       
   319                 }
       
   320 
       
   321             if(iProcessObservers)
       
   322                 {
       
   323                 if (aContact->AvatarContent().Length())
       
   324                     {
       
   325                     iProcessObservers->HandleAvatarChangeL(aContact->UserId());
       
   326                     }
       
   327                 /* passing aContactIndex so that the focus is on owndata item */
       
   328                 iProcessObservers->HandleAdditionL(TVIMPSTEnums::EContactItem, aContactIndex );
       
   329                 }   
       
   330            
       
   331             break;
       
   332             }
       
   333         case TVIMPSTEnums::EStorageEventContactDelete:
       
   334             {
       
   335             TRACE( " EStorageEventContactDelete" );
       
   336             TRACE( "contactindex = %d",aContactIndex );
       
   337             TRACE( "itemarraycount = %d",iItemArray.Count() );
       
   338             if( aContactIndex < iItemArray.Count() )
       
   339                 {
       
   340                 MVIMPSTProcessArrayItem* deletedItem = iItemArray[ aContactIndex ];               
       
   341                 TRACE( "contact removed in item array of index = %d",aContactIndex );
       
   342                 iItemArray.Remove (aContactIndex);
       
   343                 delete deletedItem;
       
   344                 iItemArray.Compress();
       
   345                 TRACE( "contactindex = %d",aContactIndex );
       
   346                 }
       
   347             if(iProcessObservers)
       
   348                 {
       
   349                 iProcessObservers->HandleDeletionL(TVIMPSTEnums::EContactItem, aContactIndex);
       
   350                 }            
       
   351             break;
       
   352             }
       
   353         case TVIMPSTEnums::EStorageAvatarChange:
       
   354             {           
       
   355             TRACE("EStorageAvatarChange");
       
   356             if(iProcessObservers  && aContact )
       
   357                 {               
       
   358                 TRACE( "EStorageAvatarChange inside if" );
       
   359                 TPtrC aUserId = aContact->UserId();                
       
   360                 TRACE( "EStorageAvatarChange aUserId = %S", &aUserId );
       
   361                 iProcessObservers->HandleAvatarChangeL( aContact->UserId() );
       
   362                 }           
       
   363             break;            
       
   364             }
       
   365         case TVIMPSTEnums::EStorageOwnPresenceChange:
       
   366             {
       
   367             TRACE( "EStorageOwnPresenceChange" );
       
   368             if(iProcessObservers)
       
   369                 {
       
   370                 /* passing index as 0, so thta focus remains at the owndata item */
       
   371                 iProcessObservers->HandleAdditionL(TVIMPSTEnums::EOwnStatusItem, KErrNotFound );
       
   372                 }           
       
   373             break;
       
   374             }
       
   375         case TVIMPSTEnums::EStorageMultiplePresenceChange:
       
   376 	        {	        
       
   377 	        TRACE("EStorageMultiplePresenceChange " );
       
   378 	        // mostly this will get called after just login and fetch time
       
   379 	        ResetArray();
       
   380             if(iProcessObservers )
       
   381                 {
       
   382                 iProcessObservers->HandleAdditionL(TVIMPSTEnums::EContactItem, KErrNotFound );
       
   383                 }            
       
   384 	        break;	
       
   385 	        }
       
   386         case TVIMPSTEnums::EStoragePresenceChange:
       
   387             {           
       
   388             TRACE( " EStoragePresenceChange" );
       
   389             /* 
       
   390              *	After Sort we get the new index... So first we need to remove the contact from the old position, 
       
   391              *	and then re-insert it at the correct obtained position (newIndex in this case)
       
   392              */
       
   393             TRACE("contactindex = %d",aContactIndex );
       
   394             TRACE( "itemarraycount = %d",iItemArray.Count() );
       
   395             if( aContactIndex < iItemArray.Count() )
       
   396                 {                
       
   397                 TInt newIndex = iContactListModel->IndexOfContact( aContact );               
       
   398                 TRACE( "new index = %d",newIndex );
       
   399                 CVIMPSTProcessContactItem* newItem = CVIMPSTProcessContactItem::NewL(*this, const_cast<TDesC&>(aContact->Name() ),
       
   400                         const_cast<TDesC&>(aContact->UserId() ),
       
   401                         aContact->ContactLink(),
       
   402                         const_cast<TDesC&>(aContact->StatusText()),
       
   403                         aContact->OnlineStatus() );
       
   404                 
       
   405                 
       
   406                 MVIMPSTProcessArrayItem* oldItem = iItemArray[ aContactIndex ]; 
       
   407                 newItem->SetAvatarIndex(aContact->AvatarIndex()); // copy the avatar index too.
       
   408                 
       
   409                 TBool isOldConversationExists(EFalse);
       
   410                 TBool isOldMsgPending(EFalse);
       
   411                 // checking if previously chat/msgs were existing
       
   412                 if( TVIMPSTEnums::ESVCERegistered == iLoginState && aContact->UserId().Length() )
       
   413                     {
       
   414 
       
   415                     if( IsConversationExistL( aContact->UserId() ) )
       
   416                         {
       
   417                         isOldConversationExists = ETrue;
       
   418                         }
       
   419                     if( IsUnreadMessageExistsL( aContact->UserId() ) )
       
   420                         {
       
   421                         isOldMsgPending = ETrue;
       
   422                         }
       
   423                    }
       
   424                 TRACE( "contact removed in item array of index = %d",aContactIndex );
       
   425                 iItemArray.Remove(aContactIndex );
       
   426                 delete oldItem;
       
   427                 iItemArray.Compress();
       
   428                 
       
   429                 // restoring if previously chat/msgs were existing
       
   430                 if( TVIMPSTEnums::ESVCERegistered == iLoginState && aContact->UserId().Length() )
       
   431                     {
       
   432                     if( isOldConversationExists )
       
   433                         {
       
   434                         newItem->SetConversationOpen(ETrue);
       
   435                         }
       
   436                     if( isOldMsgPending )
       
   437                         {
       
   438                         newItem->SetMsgPending(ETrue);
       
   439                         }
       
   440                     }
       
   441                 // Add it in the new index
       
   442                 newIndex = newIndex + 1 + iUnKnownContactArray.Count()+ iAddRequestArray.Count();
       
   443                 if (newIndex >= iItemArray.Count())
       
   444                     {                   
       
   445                     TRACE( "append contact item = %d", newIndex);
       
   446                     iItemArray.Append (newItem);
       
   447                     }
       
   448                 else
       
   449                     {                    
       
   450                     TRACE( "Insert at index = %d", newItem);
       
   451                     iItemArray.Insert (newItem, newIndex);
       
   452                     }   
       
   453                 }
       
   454             if(iProcessObservers )
       
   455                 {
       
   456                 iProcessObservers->HandleAdditionL(TVIMPSTEnums::EContactItem, KErrNotFound );
       
   457                 }
       
   458             
       
   459             break;
       
   460             }
       
   461         case TVIMPSTEnums::EStorageEventOwnUserChanged:
       
   462             {
       
   463             TRACE( "EStorageEventOwnUserChanged " );
       
   464             TRACE( " start Event = %d",aEventType );
       
   465             ResetArray();
       
   466             if(iProcessObservers )
       
   467                 {
       
   468                 iProcessObservers->HandleDeletionL(TVIMPSTEnums::EOwnStatusItem, 0 ); // focus own item
       
   469 
       
   470                 if( aContact )
       
   471                     {
       
   472                     iProcessObservers->HandleAvatarChangeL( aContact->UserId() );
       
   473                     }
       
   474                 }
       
   475             
       
   476 
       
   477             break;
       
   478             }
       
   479         case TVIMPSTEnums::EStorageAllContactRemoved:
       
   480             {           
       
   481             TRACE("EStorageAllContactRemoved");
       
   482             TRACE( "EStorageAllContactRemoved count = %d", iItemArray.Count() );
       
   483             ResetArray();
       
   484             if(iProcessObservers )
       
   485                 {
       
   486                 iProcessObservers->HandleDeletionL(TVIMPSTEnums::EOwnStatusItem, 0 ); // focus own item
       
   487                 }
       
   488           
       
   489             break;
       
   490             }
       
   491         case TVIMPSTEnums::EStorageEventContactChange: 
       
   492 	        {
       
   493 	        TRACE("EStorageEventContactChange");	        
       
   494 	        // display name is changed ,might be contact is re-arranged
       
   495             TRACE( "contactindex = %d",aContactIndex );
       
   496             TRACE( "itemarraycount = %d",iItemArray.Count() );
       
   497             if( aContactIndex < iItemArray.Count() )
       
   498                 {
       
   499                 if ( aContact )
       
   500                      {
       
   501                     CVIMPSTProcessContactItem* newItem = CVIMPSTProcessContactItem::NewL(*this, const_cast<TDesC&>(aContact->Name() ),
       
   502                             const_cast<TDesC&>(aContact->UserId() ),
       
   503                             aContact->ContactLink(),
       
   504                             const_cast<TDesC&>(aContact->StatusText()),
       
   505                             aContact->OnlineStatus() );
       
   506                     
       
   507                     
       
   508                     MVIMPSTProcessArrayItem* oldItem = iItemArray[ aContactIndex ];
       
   509                     newItem->SetAvatarIndex(aContact->AvatarIndex()); // copy the avatar index too.
       
   510                 TRACE("contact removed in item array of index = %d",aContactIndex );
       
   511                     // set the conversation open flag from old contact, as only the display name would have changed.
       
   512                     newItem->SetConversationOpen(oldItem->IsConversationOpen());
       
   513                     newItem->SetMsgPending( oldItem->IsMsgPending() );
       
   514                     iItemArray.Remove(aContactIndex );
       
   515                     delete oldItem;
       
   516                     iItemArray.Compress();
       
   517                 TRACE( "Insert at index = %d", aContactIndex);
       
   518                     TInt newIndex = iContactListModel->IndexOfContact( aContact );
       
   519                      // Add it in the new index
       
   520                     newIndex = newIndex + 1 + iUnKnownContactArray.Count() + iAddRequestArray.Count();
       
   521                     if (newIndex >= iItemArray.Count())
       
   522                         {
       
   523                     TRACE("append contact item = %d", newIndex);
       
   524                         iItemArray.Append (newItem);
       
   525                         }
       
   526                     else
       
   527                         {
       
   528                     TRACE( "Insert at index = %d", newItem);
       
   529                         iItemArray.Insert(newItem, newIndex);
       
   530                         } 
       
   531                     // inform the cv about the display name changes
       
   532                     if(aContact && aContact->UserId().Length() && newItem->IsConversationOpen())
       
   533                         {
       
   534                     TRACE("EStorageEventContactChange ");
       
   535                         TApaTaskList taskList( CCoeEnv::Static()->WsSession() );
       
   536                         TApaTask task( taskList.FindApp( KConversationViewAppUid ) );
       
   537                     
       
   538                         if ( task.Exists() )
       
   539                             {
       
   540                         TRACE("EStorageEventContactChange - task exists");                       
       
   541                             // packing of data ,passed to conversation view
       
   542                             TPckgBuf< TIMCVUiParams > params;
       
   543                             params().iBuddyId = aContact->UserId();
       
   544                             params().iBuddyName = aContact->Name();
       
   545                             params().iServiceId = iServiceId;
       
   546                             params().iUpdate = ETrue;
       
   547                             task.SendMessage( 
       
   548                                     TUid::Uid( KUidApaMessageSwitchOpenFileValue ), params );
       
   549                             }
       
   550                         }
       
   551                     } 
       
   552                 
       
   553               }
       
   554             if(iProcessObservers )
       
   555                 {
       
   556                 iProcessObservers->HandleAdditionL(TVIMPSTEnums::EContactItem, aContactIndex );
       
   557                 }           
       
   558 	        break;	
       
   559 	        }          
       
   560         case TVIMPSTEnums::EStorageEventUserIdPostChange:
       
   561             {
       
   562             TRACE( "EStorageEventUserIdPostChange" );
       
   563             TRACE( "contactindex = %d",aContactIndex );
       
   564             TRACE( "itemarraycount = %d",iItemArray.Count() );
       
   565             if( aContactIndex < iItemArray.Count() )
       
   566                 {
       
   567                 CVIMPSTProcessContactItem* newItem = CVIMPSTProcessContactItem::NewL(*this, const_cast<TDesC&>(aContact->Name() ),
       
   568                         const_cast<TDesC&>(aContact->UserId() ),
       
   569                         aContact->ContactLink(),
       
   570                         const_cast<TDesC&>(aContact->StatusText()),
       
   571                         aContact->OnlineStatus() );
       
   572                 
       
   573                 
       
   574                 MVIMPSTProcessArrayItem* oldItem = iItemArray[ aContactIndex ];
       
   575                 newItem->SetAvatarIndex(aContact->AvatarIndex()); // copy the avatar index too.
       
   576                 TRACE( "contact removed in item array of index = %d",aContactIndex );
       
   577                 iItemArray.Remove(aContactIndex );
       
   578                 delete oldItem;
       
   579                 iItemArray.Compress();
       
   580                 TRACE( "Insert at index = %d", aContactIndex);
       
   581                 iItemArray.InsertL (newItem, aContactIndex);
       
   582                 }
       
   583             if(iProcessObservers )
       
   584                 {
       
   585                 iProcessObservers->HandleAdditionL(TVIMPSTEnums::EContactItem, aContactIndex );
       
   586                 }
       
   587             TRACE( "EStorageEventUserIdPostChange" );
       
   588             break;
       
   589             }
       
   590         default:
       
   591             {
       
   592             TRACE("default");          
       
   593             break;
       
   594             }
       
   595         }
       
   596     
       
   597     }
       
   598 
       
   599 // --------------------------------------------------------------------------
       
   600 // CVIMPSTProcessArray::AddObserver
       
   601 // --------------------------------------------------------------------------
       
   602 //
       
   603 void CVIMPSTProcessArray::AddObserver( MVIMPSTProcessArrayObserver* aObserver ) 
       
   604     {
       
   605     iProcessObservers = aObserver;
       
   606     }
       
   607 // --------------------------------------------------------------------------
       
   608 // CVIMPSTProcessArray::RemoveObserver
       
   609 // --------------------------------------------------------------------------
       
   610 //
       
   611 void CVIMPSTProcessArray::RemoveObserver( ) 
       
   612     {
       
   613     iProcessObservers = NULL;
       
   614     }
       
   615 
       
   616 // -----------------------------------------------------------------------------
       
   617 // CVIMPSTProcessArray::GetItemNameTextL
       
   618 // -----------------------------------------------------------------------------
       
   619 
       
   620 TPtrC CVIMPSTProcessArray::GetItemNameText(TInt aIndex)
       
   621     {
       
   622     TVIMPSTEnums::TItem itemtype = GetType( aIndex );
       
   623 	MVIMPSTProcessArrayItem* arrayItem = NULL;
       
   624     /* Codescanner warning is ignored, since Bound check is done 
       
   625      * inside the GetType()
       
   626      * method*/
       
   627     switch(itemtype)
       
   628         {
       
   629         case TVIMPSTEnums::EOwnStatusItem:
       
   630             {
       
   631             MVIMPSTProcessArrayItem *arrayItem = (MVIMPSTProcessArrayItem *)iItemArray[aIndex];
       
   632             return arrayItem->GetItemUserId(); // no name for own user	
       
   633             }
       
   634         case TVIMPSTEnums::EContactListItem:
       
   635         case TVIMPSTEnums::EContactItem:
       
   636             { 
       
   637             arrayItem = (MVIMPSTProcessArrayItem *)iItemArray[aIndex];
       
   638            	break;	
       
   639             }
       
   640         case TVIMPSTEnums::EFriendRequestItem:
       
   641             {
       
   642             arrayItem = (MVIMPSTProcessArrayItem *)iItemArray[aIndex];
       
   643             break;
       
   644             }
       
   645        case TVIMPSTEnums::EUnknonContactItem:
       
   646            {
       
   647            arrayItem = (MVIMPSTProcessArrayItem *)iItemArray[aIndex];
       
   648            break;
       
   649            }
       
   650         case TVIMPSTEnums::EInvalid:
       
   651         default:
       
   652             {
       
   653             break;
       
   654             }
       
   655         }
       
   656 	if( arrayItem )
       
   657 		{
       
   658 		return arrayItem->GetItemNameText();	
       
   659 		}
       
   660 	return KNullDesC();
       
   661     }
       
   662 
       
   663 // -----------------------------------------------------------------------------
       
   664 // CVIMPSTProcessArray::GetItemUserId
       
   665 // -----------------------------------------------------------------------------
       
   666 
       
   667 TPtrC CVIMPSTProcessArray::GetItemUserId(TInt aIndex)
       
   668     {
       
   669     TVIMPSTEnums::TItem itemtype = GetType( aIndex );
       
   670 
       
   671     /* Codescanner warning is ignored, since Bound check is done 
       
   672      * inside the GetType()
       
   673      * method*/
       
   674     switch(itemtype)    
       
   675         {
       
   676         case TVIMPSTEnums::EContactItem:
       
   677         case TVIMPSTEnums::EFriendRequestItem:
       
   678         case TVIMPSTEnums::EUnknonContactItem:
       
   679         case TVIMPSTEnums::EOwnStatusItem:
       
   680             {
       
   681             MVIMPSTProcessArrayItem *arrayItem = (MVIMPSTProcessArrayItem *)iItemArray[aIndex];
       
   682             return arrayItem->GetItemUserId(); // no name for own user	
       
   683             }
       
   684         default:
       
   685             {
       
   686             return KNullDesC(); 
       
   687             }
       
   688         }
       
   689     }
       
   690 
       
   691 // -----------------------------------------------------------------------------
       
   692 // CVIMPSTProcessArray::ContactLink
       
   693 // -----------------------------------------------------------------------------
       
   694 
       
   695 MVPbkContactLink* CVIMPSTProcessArray::ContactLink(TInt aIndex)
       
   696     {
       
   697     TVIMPSTEnums::TItem itemtype = GetType( aIndex );
       
   698 
       
   699     /* Codescanner warning is ignored, since Bound check is done 
       
   700      * inside the GetType()
       
   701      * method*/
       
   702     switch(itemtype)
       
   703         {
       
   704         case TVIMPSTEnums::EOwnStatusItem:
       
   705             {
       
   706             return NULL;	
       
   707             }
       
   708         case TVIMPSTEnums::EContactListItem:
       
   709         case TVIMPSTEnums::EContactItem:
       
   710             { 	
       
   711             MVIMPSTProcessArrayItem *arrayItem = (MVIMPSTProcessArrayItem *)iItemArray[aIndex];
       
   712             return arrayItem->ContactLink();	
       
   713             }
       
   714         case TVIMPSTEnums::EInvalid:
       
   715         default:
       
   716             {
       
   717             }
       
   718         }
       
   719     return NULL;
       
   720     }
       
   721 
       
   722 // -----------------------------------------------------------------------------
       
   723 // CVIMPSTProcessArray::Count
       
   724 // -----------------------------------------------------------------------------
       
   725 
       
   726 TInt CVIMPSTProcessArray::Count() const
       
   727 	{
       
   728 	// return the total count here.
       
   729 	return iItemArray.Count() ;
       
   730 	}
       
   731 // -----------------------------------------------------------------------------
       
   732 // CVIMPSTProcessArray::IsSubServiceSupportedL
       
   733 // -----------------------------------------------------------------------------
       
   734 
       
   735 TBool CVIMPSTProcessArray::IsSubServiceSupportedL(TVIMPSTEnums::SubServiceType aType ) const
       
   736 	{
       
   737 	return iEngine.IsSubServiceSupportedL(aType );
       
   738 	}
       
   739 
       
   740 // -----------------------------------------------------------------------------
       
   741 // CVIMPSTProcessArray::IsIMSupported
       
   742 // -----------------------------------------------------------------------------
       
   743 TBool CVIMPSTProcessArray::IsIMSupported()
       
   744     {
       
   745     return iIMSupported;
       
   746     }
       
   747 
       
   748 // -----------------------------------------------------------------------------
       
   749 // CVIMPSTProcessArray::IsPresenceSupported
       
   750 // -----------------------------------------------------------------------------
       
   751 TBool CVIMPSTProcessArray::IsPresenceSupported()
       
   752     {
       
   753     return iPresenceSupported;
       
   754     }
       
   755 
       
   756 // -----------------------------------------------------------------------------
       
   757 // CVIMPSTProcessArray::GetType
       
   758 // Get the type from engine and return the type
       
   759 // -----------------------------------------------------------------------------
       
   760 
       
   761 TVIMPSTEnums::TItem CVIMPSTProcessArray::GetType(TInt aIndex) const
       
   762 	{
       
   763 	TInt itemArrayCount = iItemArray.Count();
       
   764 	if(itemArrayCount <=0 || (aIndex < 0 || aIndex >= itemArrayCount) )
       
   765 	    {
       
   766 	    return TVIMPSTEnums::EInvalid;
       
   767 	    }
       
   768 
       
   769 	else if(itemArrayCount>0)
       
   770 	    {
       
   771 	    TInt index = aIndex <= 0 ? 0 : aIndex;
       
   772 	    MVIMPSTProcessArrayItem* item = iItemArray[ index ];	
       
   773 	    return item->Type();
       
   774 	    }
       
   775 	else
       
   776 	    {
       
   777 	    return TVIMPSTEnums::EInvalid;
       
   778 	    }	
       
   779 	}
       
   780 // -----------------------------------------------------------------------------
       
   781 // CVIMPSTProcessArray::FillItemL
       
   782 // Fill up the complete array one item at a time
       
   783 // -----------------------------------------------------------------------------
       
   784 
       
   785 void CVIMPSTProcessArray::FillItemL()
       
   786     {
       
   787     TRACER_AUTO;
       
   788     TInt count = 0;
       
   789     if(iContactListModel)
       
   790         {
       
   791         count = iContactListModel->Count();
       
   792         }
       
   793 
       
   794     // the index is starting from 1, because we already added owndata item to the list.......
       
   795     for ( TInt index = 0; index < count ; index++)
       
   796         {
       
   797         // fetch the item and process correct type
       
   798         MVIMPSTStorageItemModel::SItem item = iContactListModel->Item( index );
       
   799         switch( item.iType )
       
   800             {
       
   801             case MVIMPSTStorageItemModel::EContactList:
       
   802                 break;
       
   803             case MVIMPSTStorageItemModel::EContactItem:
       
   804                 {
       
   805                 // contact item
       
   806                 // add this items to contact item -- CONTACT 
       
   807                 // create an object of this type and append this to the rpointerarray
       
   808                 MVIMPSTStorageContact* contact = item.iContact;							   
       
   809                 TPtrC userId = contact->UserId();
       
   810                 CVIMPSTProcessContactItem* contactItem = 
       
   811                 CVIMPSTProcessContactItem::NewL(*this, contact->Name(),
       
   812                 								userId ,
       
   813                         						contact->ContactLink(),
       
   814                         			            const_cast<TDesC&>(contact->StatusText()),
       
   815                         			            contact->OnlineStatus() );
       
   816                 
       
   817                 contactItem->SetAvatarIndex(contact->AvatarIndex());
       
   818                 
       
   819                 TInt otherCount = 1 + iUnKnownContactArray.Count() + iAddRequestArray.Count();
       
   820                 contactItem->SetItemIndex(index + otherCount );
       
   821                 //append this to the array
       
   822                 iItemArray.AppendL(contactItem);
       
   823                 if( TVIMPSTEnums::ESVCERegistered == iLoginState && userId.Length() )
       
   824                     {
       
   825                     if( IsUnreadMessageExistsL( userId ) )
       
   826                         {
       
   827                         contactItem->SetMsgPending(ETrue);
       
   828                         }
       
   829                     if( IsConversationExistL( userId ) )
       
   830                         {
       
   831                         contactItem->SetConversationOpen(ETrue);
       
   832                         }
       
   833                     }
       
   834                 break;
       
   835                 }
       
   836             default:
       
   837                 {
       
   838                 // the call shouldn't be here
       
   839                 break;
       
   840                 }
       
   841             }
       
   842         }
       
   843 
       
   844     }
       
   845 
       
   846 // -----------------------------------------------------------------------------
       
   847 // CVIMPSTProcessArray::FillOwnDataL
       
   848 // FillOwnDataL implementation
       
   849 // -----------------------------------------------------------------------------
       
   850 // fill the owndata at the begining of array.....
       
   851 TBool CVIMPSTProcessArray::FillOwnDataL()
       
   852     {
       
   853     TRACER_AUTO;
       
   854     TBool ownDataAdded = EFalse;
       
   855     TPtr dataPtr = iData->Des();    
       
   856     // Check whether the user has logged in before, if has
       
   857     // then dont update anything just return   
       
   858     //check if its logged in or not.
       
   859     // 1. if not logged in append 
       
   860     switch(iLoginState)
       
   861         {
       
   862         case TVIMPSTEnums::ESVCERegistered:
       
   863             {
       
   864             TPtrC userId = iContactInterface->OwnContactL().UserId();
       
   865             dataPtr.Copy(userId);
       
   866             break;
       
   867             }
       
   868         case TVIMPSTEnums::ESVCENetworkConnecting:
       
   869             {
       
   870             HBufC* tempStr = VIMPSTUtils::LoadResourceL( R_QTN_SERVTAB_LOGGING_LIST_ITEM );
       
   871             if(tempStr)
       
   872                 {
       
   873                 dataPtr.Copy(*tempStr);     
       
   874                 delete tempStr; 
       
   875                 }
       
   876 
       
   877             break;
       
   878             }
       
   879         case TVIMPSTEnums::ESVCEUpdatingContacts:
       
   880             {
       
   881             HBufC* tempStr = VIMPSTUtils::LoadResourceL( R_QTN_SERVTAB_UPDATING_LIST_ITEM );
       
   882             if(tempStr)
       
   883                 {
       
   884                 dataPtr.Copy(*tempStr);     
       
   885                 delete tempStr; 
       
   886                 }
       
   887 
       
   888             break;
       
   889             }
       
   890         case TVIMPSTEnums::ESVCEWaitingForNetwork:
       
   891             {
       
   892             HBufC* tempStr = VIMPSTUtils::LoadResourceL( R_QTN_SERVTAB_WAITING_LIST_ITEM );
       
   893             if(tempStr)
       
   894                 {
       
   895                 dataPtr.Copy(*tempStr);     
       
   896                 delete tempStr; 
       
   897                 }
       
   898 			break;	
       
   899             }
       
   900         case TVIMPSTEnums::ESVCENetworkDisConnecting:
       
   901             {
       
   902             HBufC* tempStr = NULL;
       
   903             if ( iEngine.IsSubServiceSupportedL(TVIMPSTEnums::EPresence) ) 
       
   904                 {
       
   905                 // if presence enabled use double line listbox string
       
   906                 tempStr= VIMPSTUtils::LoadResourceL( R_QTN_SERVTAB_LOGGINGOUT_LIST_ITEM );
       
   907                 }
       
   908             else
       
   909                 { 
       
   910                 // if  presence is not enabled use  single line listbox string
       
   911                 tempStr= VIMPSTUtils::LoadResourceL( R_QTN_SERVTAB_SINGLE_LINE_LOGGINGOUT_LIST_ITEM );
       
   912                 }
       
   913 
       
   914             if(tempStr)
       
   915                 {
       
   916                 dataPtr.Copy(*tempStr);     
       
   917                 delete tempStr;
       
   918                 }			
       
   919 
       
   920             break;
       
   921             }
       
   922         case TVIMPSTEnums::ESVCENotRegistered:
       
   923         default:
       
   924             {
       
   925             HBufC* str = NULL;
       
   926             if ( iEngine.SubService( TVIMPSTEnums::EPresence) ) 
       
   927                 {
       
   928                 // if presence enabled use double line listbox string
       
   929                 str = VIMPSTUtils::LoadResourceL( R_QTN_SERVTAB_LOGIN_LIST_ITEM );
       
   930                 }
       
   931             else
       
   932                 { 
       
   933                 // if  presence is not enabled use  single line listbox string
       
   934                 // get  the username from settings
       
   935                 TPtrC ownUserId = iContactInterface->OwnContactL().UserId();
       
   936                 if ( ownUserId.Length() )
       
   937                     { 
       
   938                     // if username is available then show with login item
       
   939                     // remove domain part and give it to resource laoder
       
   940                     str= VIMPSTUtils::LoadResourceL( R_QTN_SERVTAB_SINGLE_LINE_LOGIN_LIST_ITEM_WITH_USERNAME , VIMPSTUtils::DisplayId( ownUserId ) );
       
   941                     }
       
   942                 else
       
   943                     {
       
   944                     // this condition occures when there is allocated memory for username with NO data (length =0 )
       
   945                     // just make sure "no memory leak" without depending on the Function  LoginUserNameFromSettingsL()
       
   946                     // since that would return the allocated memory without Data (length=0 ) 
       
   947                     // just show login item since no username is available in the settings
       
   948                     str= VIMPSTUtils::LoadResourceL( R_QTN_SERVTAB_SINGLE_LINE_LOGIN_LIST_ITEM );
       
   949                     }
       
   950                 }
       
   951             if( str )
       
   952                 {
       
   953                 dataPtr.Copy(*str);		
       
   954                 delete str;		
       
   955                 }
       
   956             break;
       
   957             }
       
   958         }
       
   959     if( iItemArray.Count() )
       
   960 	    {
       
   961 	    // own item is already exist delete it before adding a fresh one
       
   962 	    MVIMPSTProcessArrayItem* arrayItem = iItemArray[ 0 ]; // should be own data
       
   963 	    if( arrayItem->Type() == TVIMPSTEnums::EOwnStatusItem )
       
   964 		    {
       
   965 	    	iItemArray.Remove(0);   //iItemArray takes ownership of owndataItem, do not delete here
       
   966 	    	delete arrayItem;
       
   967 	    	iItemArray.Compress();
       
   968 	    	}
       
   969 	    }
       
   970     //common code for all the above cases to update the owndata item to list at index 0
       
   971     CVIMPSTProcessOwnDataItem* owndataItem = CVIMPSTProcessOwnDataItem::NewL(*this, *iData);
       
   972     owndataItem->SetItemIndex(0);
       
   973     //append this to the array
       
   974     iItemArray.InsertL( owndataItem,0 );   //iItemArray takes ownership of owndataItem, do not delete here
       
   975     iContactListIndex = 0;
       
   976     ownDataAdded = ETrue;     
       
   977     return ownDataAdded;
       
   978     }
       
   979 
       
   980 // -----------------------------------------------------------------------------
       
   981 // CVIMPSTProcessArray::FillArrayL
       
   982 // -----------------------------------------------------------------------------
       
   983 
       
   984 void CVIMPSTProcessArray::FillArrayL()
       
   985     {
       
   986     if(FillOwnDataL() )
       
   987         {
       
   988         // then fill the conversations..or open chats....	
       
   989         }
       
   990     else
       
   991         {
       
   992         iContactListIndex = 0; // we have not added any thing to main array..so index is zero...
       
   993         }
       
   994     FillUnknownContactsL(); //will fill all unknown contact from iUnknownContactArray to iItemArray.
       
   995     FillAddRequestDataL();//will fill all requests from iAddRequestArray to iItemArray.
       
   996     FillItemL();   
       
   997     }
       
   998 
       
   999 
       
  1000 // -----------------------------------------------------------------------------
       
  1001 // CVIMPSTProcessArray::ResetArray
       
  1002 // -----------------------------------------------------------------------------
       
  1003 
       
  1004 void CVIMPSTProcessArray::ResetArray()
       
  1005     {
       
  1006     RemoveUnKnownContacts(); //will remove all the unknown contacts from iItemArray.
       
  1007     RemoveAddRequestData();//will remove all the requests from iItemArray.
       
  1008     iItemArray.ResetAndDestroy();
       
  1009     TRAPD( err, FillArrayL());
       
  1010     if ( err != KErrNone )
       
  1011         {
       
  1012         CActiveScheduler::Current()->Error( err );
       
  1013         }
       
  1014     }
       
  1015 
       
  1016 // -----------------------------------------------------------------------------
       
  1017 // CVIMPSTProcessArray::SetLoginState
       
  1018 // -----------------------------------------------------------------------------
       
  1019 
       
  1020 void CVIMPSTProcessArray::SetLoginStateL(TVIMPSTEnums::TVIMPSTRegistrationState aLoginState)
       
  1021     {
       
  1022     TRACER_AUTO;
       
  1023     iLoginState = aLoginState;
       
  1024     if(aLoginState != TVIMPSTEnums::ESVCERegistered )
       
  1025         {
       
  1026         iFetchCompleted = EFalse;
       
  1027         RemoveUnKnownContacts();
       
  1028         RemoveAddRequestData();
       
  1029         iUnKnownContactArray.ResetAndDestroy();// delete all items
       
  1030         iUnKnownContactArray.Reset();
       
  1031         iAddRequestArray.ResetAndDestroy();// delete all items
       
  1032         iAddRequestArray.Reset();
       
  1033         
       
  1034         // Reset contact list array (append the contacts to contact list array 
       
  1035         // by getting from contactlist model) before contacts update.  
       
  1036         if(aLoginState == TVIMPSTEnums::ESVCENotRegistered )
       
  1037         	{
       
  1038 			ResetArray();
       
  1039         	}
       
  1040         
       
  1041         // reset the status of all the contacts.
       
  1042         // no need to check the item type here. by the time its here all the other 
       
  1043         // type except contact item are removed.
       
  1044         TInt count = iItemArray.Count();
       
  1045         for(TInt i = 0; i < count ; i++)
       
  1046             {
       
  1047              iItemArray[i]->DoFormatStringL();
       
  1048             }
       
  1049         }
       
  1050     FillOwnDataL(); // this will change the own item based on connection status
       
  1051     }
       
  1052 // -----------------------------------------------------------------------------
       
  1053 // CVIMPSTProcessArray::GetLoginState
       
  1054 // -----------------------------------------------------------------------------
       
  1055 
       
  1056 TVIMPSTEnums::TVIMPSTRegistrationState CVIMPSTProcessArray::GetLoginState()
       
  1057     {
       
  1058     return iLoginState;
       
  1059     }
       
  1060 
       
  1061 // -----------------------------------------------------------------------------
       
  1062 // CVIMPSTProcessArray::GetOnlineStatus
       
  1063 // -----------------------------------------------------------------------------
       
  1064 
       
  1065 TVIMPSTEnums::TOnlineStatus CVIMPSTProcessArray::GetOnlineStatusL(TInt aIndex)
       
  1066     {
       
  1067     TRACER_AUTO;
       
  1068     TVIMPSTEnums::TItem itemtype = GetType( aIndex );
       
  1069     TVIMPSTEnums::TOnlineStatus status = TVIMPSTEnums::EUnknown;
       
  1070     switch(itemtype)
       
  1071         {
       
  1072         case TVIMPSTEnums::EOwnStatusItem:
       
  1073             {
       
  1074             if( iContactInterface )
       
  1075 	            {
       
  1076                  status = iContactInterface->OwnContactL().OnlineStatus();
       
  1077 	            }
       
  1078             break;
       
  1079             }
       
  1080         case TVIMPSTEnums::EContactItem:
       
  1081             { 
       
  1082 
       
  1083             status = iItemArray[aIndex]->OnlineStatus();
       
  1084             break;
       
  1085             }
       
  1086         case TVIMPSTEnums::EInvalid:
       
  1087         default:
       
  1088             {
       
  1089             break;
       
  1090             }
       
  1091         }
       
  1092     return status;
       
  1093     }
       
  1094 //-----------------------------------------------------------
       
  1095 //CVIMPSTProcessArray::StatusText
       
  1096 //-----------------------------------------------------------
       
  1097 // 
       
  1098 const TDesC&  CVIMPSTProcessArray::StatusTextL(TInt aIndex ) 
       
  1099     {
       
  1100     TRACER_AUTO;
       
  1101     TVIMPSTEnums::TItem itemtype = GetType( aIndex );
       
  1102     switch(itemtype)
       
  1103         {
       
  1104         case TVIMPSTEnums::EOwnStatusItem:
       
  1105             {
       
  1106             if( iContactInterface )
       
  1107 	            {
       
  1108 	            return iContactInterface->OwnContactL().StatusText();	
       
  1109 	            }
       
  1110             break;
       
  1111             }
       
  1112         case TVIMPSTEnums::EContactItem:
       
  1113             { 
       
  1114             return iItemArray[aIndex]->StatusText();
       
  1115             }
       
  1116         case TVIMPSTEnums::EInvalid:
       
  1117         default:
       
  1118             {
       
  1119             break;
       
  1120             }
       
  1121         }
       
  1122     return KNullDesC;
       
  1123     }
       
  1124 //-----------------------------------------------------------
       
  1125 //CVIMPSTProcessArray::RemoveFromUnknonOrInvitationListL
       
  1126 //-----------------------------------------------------------
       
  1127 //
       
  1128 TBool CVIMPSTProcessArray::RemoveFromUnknonOrInvitationListL( const TDesC& aAddedUserId ,TBool aBlocked )
       
  1129     {
       
  1130     TRACER_AUTO;
       
  1131     TBool found = RemoveFromUnknonListL( aAddedUserId );
       
  1132     if( !found )
       
  1133         {
       
  1134         found = RemoveFromInvitationListL( aAddedUserId );
       
  1135         }
       
  1136     //Get IM SubService  
       
  1137     if( aBlocked && found && aAddedUserId.Length() )
       
  1138         {
       
  1139         MVIMPSTEngineSubService* subService =         
       
  1140         (iEngine.SubService(TVIMPSTEnums::EIM));
       
  1141 
       
  1142         if(subService )
       
  1143             {
       
  1144             MVIMPSTEngineIMSubService& imSubService = 
       
  1145             MVIMPSTEngineIMSubService::Cast (*subService);
       
  1146             imSubService.CloseConversationL( aAddedUserId );
       
  1147             }
       
  1148         }
       
  1149     return found;
       
  1150     }
       
  1151 //-----------------------------------------------------------
       
  1152 //CVIMPSTProcessArray::RemoveFromUnknonListL
       
  1153 //-----------------------------------------------------------
       
  1154 //
       
  1155 TBool CVIMPSTProcessArray::RemoveFromUnknonListL( const TDesC& aAddedUserId )
       
  1156     {
       
  1157    
       
  1158     TRACER_AUTO;
       
  1159     TPtrC addedUserId = VIMPSTUtils::DisplayId( aAddedUserId );
       
  1160     TBool found = EFalse;
       
  1161     TInt count = iUnKnownContactArray.Count();
       
  1162     CVIMPSTProcessUnknownContactItem* unknownItem = NULL;
       
  1163     TInt index = KErrNotFound;
       
  1164     TInt itemIndex = KErrNotFound;
       
  1165     // check in unknon array
       
  1166     for(index = 0 ; index < count ; index++)
       
  1167         {
       
  1168         unknownItem = iUnKnownContactArray[index];
       
  1169         TPtrC contactId = VIMPSTUtils::DisplayId( unknownItem->GetItemUserId() );
       
  1170         if( addedUserId.Compare(  contactId ) == 0 )
       
  1171             {
       
  1172             itemIndex = iItemArray.Find(unknownItem);
       
  1173             if( itemIndex > -1 )
       
  1174                 {
       
  1175                 found = ETrue;
       
  1176                 }
       
  1177             break;
       
  1178             }
       
  1179        
       
  1180         }
       
  1181     if( found )
       
  1182         {
       
  1183         iItemArray.Remove(itemIndex);
       
  1184         iUnKnownContactArray.Remove( index );
       
  1185         delete unknownItem;
       
  1186         unknownItem = NULL;
       
  1187         iItemArray.Compress();
       
  1188         iUnKnownContactArray.Compress();
       
  1189         if( iProcessObservers )
       
  1190             {
       
  1191             // this is to refresh the list box
       
  1192             // KErrNotFound , focus remain at same position
       
  1193             iProcessObservers->HandleDeletionL(TVIMPSTEnums::EUnknonContactItem, KErrNotFound );
       
  1194             }
       
  1195         }
       
  1196   
       
  1197     return found;
       
  1198     }
       
  1199 //-----------------------------------------------------------
       
  1200 //CVIMPSTProcessArray::RemoveFromInvitationListL
       
  1201 //-----------------------------------------------------------
       
  1202 //
       
  1203 TBool CVIMPSTProcessArray::RemoveFromInvitationListL( const TDesC& aAddedUserId )
       
  1204     {
       
  1205     TRACER_AUTO;
       
  1206     TPtrC addedUserId = VIMPSTUtils::DisplayId( aAddedUserId );
       
  1207     TBool found = EFalse;
       
  1208     TInt index = KErrNotFound;
       
  1209     TInt itemIndex = KErrNotFound;
       
  1210     // item not foun in unknown list
       
  1211     // check in invitation item array
       
  1212     TInt count = iAddRequestArray.Count();
       
  1213     CVIMPSTProcessFriendRequestItem* requestItem = NULL;
       
  1214     for(index = 0 ; index < count ; index++)
       
  1215         {
       
  1216         requestItem = iAddRequestArray[index];
       
  1217         TPtrC contactId = VIMPSTUtils::DisplayId( requestItem->GetItemUserId() );
       
  1218         if( addedUserId.Compare( contactId ) == 0 )
       
  1219             {
       
  1220             itemIndex = iItemArray.Find(requestItem);
       
  1221             if( itemIndex > -1 )
       
  1222                 {
       
  1223                 found = ETrue;
       
  1224                 }
       
  1225             break;
       
  1226             }
       
  1227         }
       
  1228     if( found )
       
  1229         {
       
  1230         iItemArray.Remove(itemIndex);
       
  1231         iAddRequestArray.Remove( index );
       
  1232         delete requestItem;
       
  1233         requestItem = NULL;
       
  1234         iItemArray.Compress();
       
  1235         iAddRequestArray.Compress();
       
  1236         if( iProcessObservers )
       
  1237             {
       
  1238             // this is to refresh the list box
       
  1239             // KErrNotFound , focus remain at same position
       
  1240             iProcessObservers->HandleDeletionL(TVIMPSTEnums::EFriendRequestItem, KErrNotFound );
       
  1241             }
       
  1242         }
       
  1243     return found;
       
  1244     }
       
  1245 //-----------------------------------------------------------
       
  1246 //CVIMPSTProcessArray::MapContactListPositions
       
  1247 //-----------------------------------------------------------
       
  1248 //
       
  1249 void CVIMPSTProcessArray::MapContactListPositions(RArray<TInt>& aPos )
       
  1250     {
       
  1251     // add conversation also this list ..and return..
       
  1252     RArray<TInt> listPos; 
       
  1253 
       
  1254     //	iContactListModel->MapContactListPositions(listPos);
       
  1255     TInt positionIndex = 0;
       
  1256     // if count is more than 0..fill the maparray...
       
  1257     if(iItemArray.Count()  + iContactListIndex > 0) 
       
  1258         {
       
  1259 
       
  1260         TInt count = listPos.Count();
       
  1261 
       
  1262         for(TInt posIndex = 0; posIndex < count; posIndex++, positionIndex++)
       
  1263             {
       
  1264             aPos.Append( listPos[posIndex] + iContactListIndex );
       
  1265             }
       
  1266         }
       
  1267     listPos.Close();	
       
  1268     }
       
  1269 // -----------------------------------------------------------------------------
       
  1270 // CVIMPSTProcessArray::GetAndCreateOpenChatListL
       
  1271 // return contact index including friend request.
       
  1272 // -----------------------------------------------------------------------------
       
  1273 void CVIMPSTProcessArray::GetAndCreateOpenChatListL() 
       
  1274     {
       
  1275     TRACER_AUTO;
       
  1276     //Get IM SubService     
       
  1277     MVIMPSTEngineSubService* subService =         
       
  1278                 (iEngine.SubService(TVIMPSTEnums::EIM));
       
  1279     RArray<SIMCacheChatItem> openChats;
       
  1280     openChats.Reset();
       
  1281     if(subService)
       
  1282         {
       
  1283         MVIMPSTEngineIMSubService& imSubService = 
       
  1284             MVIMPSTEngineIMSubService::Cast (*subService);
       
  1285         openChats = imSubService.GetOpenChatListL();
       
  1286         }
       
  1287     while( openChats.Count() )
       
  1288         {
       
  1289         SIMCacheChatItem chat = openChats[ 0 ]; // first item 
       
  1290         TPtrC contactId = *chat.iBuddyId;
       
  1291         MVIMPSTProcessArrayItem* arrayItem = FindArrayItem( contactId );
       
  1292         TBool msgPending = EFalse;
       
  1293         if( contactId.Length() )
       
  1294             {
       
  1295             if( IsUnreadMessageExistsL( contactId ) )
       
  1296                 {
       
  1297                 msgPending = ETrue;
       
  1298                 }
       
  1299             }
       
  1300         if( arrayItem )
       
  1301             {
       
  1302             arrayItem->SetConversationOpen( ETrue );
       
  1303             arrayItem->SetMsgPending( msgPending );
       
  1304             }
       
  1305         else
       
  1306             {
       
  1307             // not in buddy List
       
  1308             DoHandleUnKnownContactMessageL( contactId ,msgPending );
       
  1309             }
       
  1310         openChats.Remove( 0 );
       
  1311         delete chat.iBuddyId;
       
  1312         chat.iBuddyId = NULL;
       
  1313         openChats.Compress();
       
  1314         }
       
  1315     }
       
  1316 //-----------------------------------------------------------
       
  1317 //CVIMPSTProcessArray::FindArrayItem
       
  1318 //-----------------------------------------------------------
       
  1319 // 
       
  1320 MVIMPSTProcessArrayItem* CVIMPSTProcessArray::FindArrayItem( const TDesC& aSenderId )
       
  1321     {
       
  1322     TPtrC senderId = VIMPSTUtils::DisplayId( aSenderId );
       
  1323     MVIMPSTProcessArrayItem* arrayItem = NULL;
       
  1324     TInt count = iItemArray.Count();
       
  1325     for( TInt i = 1; i < count; i++ )
       
  1326         {
       
  1327         MVIMPSTProcessArrayItem* contactItem = (MVIMPSTProcessArrayItem *)iItemArray[i];
       
  1328         TPtrC contactId = VIMPSTUtils::DisplayId( contactItem->GetItemUserId() );
       
  1329         if( senderId.Compare(  contactId ) == 0 )
       
  1330             {
       
  1331             arrayItem = contactItem;
       
  1332             break;
       
  1333             }
       
  1334         }
       
  1335     return arrayItem;
       
  1336     }
       
  1337 //-----------------------------------------------------------
       
  1338 //CVIMPSTProcessArray::HandleChatMessageEventL
       
  1339 //-----------------------------------------------------------
       
  1340 // 
       
  1341 void CVIMPSTProcessArray::HandleChatMessageEventL( TVIMPSTEnums::TIMEventType aEventType ,
       
  1342                                                     const TDesC& aSender )
       
  1343     {
       
  1344     TRACER_AUTO;
       
  1345     if( !iFetchCompleted )
       
  1346         {
       
  1347         // contact matching will not be correct until fetching completed
       
  1348         // hence return 
       
  1349         return;
       
  1350         }
       
  1351     switch( aEventType )
       
  1352         {
       
  1353         case TVIMPSTEnums::EIMUnreadMessage:
       
  1354             {
       
  1355             if( FindAndMarkContactAsOpenChat( aSender , ETrue ) )
       
  1356                 {
       
  1357                 if(iProcessObservers)
       
  1358                     {
       
  1359                     iProcessObservers->HandleAdditionL(TVIMPSTEnums::EContactItem, KErrNotFound );
       
  1360                     }
       
  1361                 }
       
  1362             else 
       
  1363                 {
       
  1364                 DoHandleUnKnownContactMessageL( aSender , ETrue );
       
  1365                 }
       
  1366             break;
       
  1367             }
       
  1368         case TVIMPSTEnums::EIMUnreadChange:
       
  1369             {
       
  1370             FindAndMarkContactAsOpenChat( aSender , EFalse ) ;
       
  1371             if(iProcessObservers)
       
  1372                 {
       
  1373                 iProcessObservers->HandleAdditionL(TVIMPSTEnums::EContactItem,KErrNotFound );
       
  1374                 }
       
  1375             break;
       
  1376             }
       
  1377         case TVIMPSTEnums::EIMChatStarted:
       
  1378             {
       
  1379             if( FindAndMarkContactAsOpenChat( aSender , EFalse ) )
       
  1380                 {
       
  1381                 if(iProcessObservers)
       
  1382                     {
       
  1383                     iProcessObservers->HandleAdditionL(TVIMPSTEnums::EContactItem, KErrNotFound );
       
  1384                     }
       
  1385                 }
       
  1386             else 
       
  1387                 {
       
  1388                 DoHandleUnKnownContactMessageL( aSender, EFalse );
       
  1389                 }
       
  1390             break;
       
  1391             }
       
  1392         case TVIMPSTEnums::EIMChatClosed:
       
  1393             {
       
  1394             if( !RemoveFromUnknonListL( aSender ) )
       
  1395                 {
       
  1396                 MVIMPSTProcessArrayItem* arrayItem = FindArrayItem( aSender );
       
  1397                 if( arrayItem )
       
  1398                     {
       
  1399                     arrayItem->SetConversationOpen( EFalse );
       
  1400                     arrayItem->SetMsgPending( EFalse );
       
  1401                     }
       
  1402                 }
       
  1403             if(iProcessObservers)
       
  1404                 {
       
  1405                 iProcessObservers->HandleAdditionL(TVIMPSTEnums::EContactItem, KErrNotFound );
       
  1406                 }
       
  1407             break;
       
  1408             }
       
  1409         case TVIMPSTEnums::EIMAllChatClosed:
       
  1410             {
       
  1411             RemoveUnKnownContacts();
       
  1412             iUnKnownContactArray.ResetAndDestroy();// delete all items
       
  1413             ResetOpenConversationPendingMsg();
       
  1414             if(iProcessObservers)
       
  1415                 {
       
  1416                 iProcessObservers->HandleAdditionL(TVIMPSTEnums::EContactItem, KErrNotFound );
       
  1417                 }
       
  1418             break;
       
  1419             }
       
  1420         case TVIMPSTEnums::EIMRequestCompleted:
       
  1421             {
       
  1422             if(iProcessObservers)
       
  1423                 {
       
  1424                 iProcessObservers->HandleAdditionL(TVIMPSTEnums::EContactItem, KErrNotFound );
       
  1425                 }
       
  1426             break;
       
  1427             }
       
  1428         default:
       
  1429             {
       
  1430             // do nothing
       
  1431             }
       
  1432         }
       
  1433     }
       
  1434 //-----------------------------------------------------------
       
  1435 //CVIMPSTProcessArray::HandleAddRequestEventL
       
  1436 //-----------------------------------------------------------
       
  1437 // 
       
  1438 void  CVIMPSTProcessArray::HandleAddRequestEventL(TVIMPSTEnums::TOperationType aType, const TDesC& aRequesterId,
       
  1439                                                     const TDesC& aRequestorDisplayName )
       
  1440     {
       
  1441     TRACER_AUTO;
       
  1442   	//add new add request item at top of list(i.e at index 1, as 0 is own id).
       
  1443     //when request is entertained remove item from list.
       
  1444     switch( aType )
       
  1445 	    {
       
  1446 		case TVIMPSTEnums::EAddItem :
       
  1447 			{
       
  1448 			if( (!FindAnyAddRequest(aRequesterId ) )&& ( !FindAnyContactServiceField( aRequesterId ) ) && !(IsContactBlocked( aRequesterId )) )
       
  1449 				{
       
  1450 				CVIMPSTProcessFriendRequestItem* addRequestItem = CVIMPSTProcessFriendRequestItem::NewL(*this, aRequesterId,aRequestorDisplayName);
       
  1451 				/* Add it as the first Item of IAddRequestARray as its like stack */
       
  1452 				iAddRequestArray.Insert(addRequestItem,0); 
       
  1453 				/*
       
  1454 				*	always the new friend request will be shown in the beginning, so 0th item is own item, 1st item is new friendrequest
       
  1455 				*	and remaining remains same.. 
       
  1456 				*/
       
  1457 				// ownership is in iItemArray
       
  1458 				TInt invitationIndex = iUnKnownContactArray.Count()+1;
       
  1459 				iItemArray.Insert(addRequestItem, invitationIndex); 
       
  1460 				if(iProcessObservers)
       
  1461 					{
       
  1462 					iProcessObservers->HandleAdditionL( TVIMPSTEnums::EFriendRequestItem ,KErrNotFound );
       
  1463 					}
       
  1464 				}
       
  1465 			break;
       
  1466 			}
       
  1467 		case TVIMPSTEnums::ERemoveItem :
       
  1468 			{
       
  1469 			TInt count = iAddRequestArray.Count();
       
  1470 			CVIMPSTProcessFriendRequestItem* requestItem = NULL;
       
  1471 			for(TInt i = 0 ; i < count ; i++)
       
  1472 				{
       
  1473 				requestItem = iAddRequestArray[i];
       
  1474 				if( aRequesterId.Compare(requestItem->GetItemUserId()) == 0 )
       
  1475 					{
       
  1476 					TInt index = iItemArray.Find(requestItem);
       
  1477 					if(index != KErrNotFound)
       
  1478 						{
       
  1479 						iItemArray.Remove(index);
       
  1480 						iItemArray.Compress();
       
  1481 						}
       
  1482 					iAddRequestArray.Remove(i);        
       
  1483 					delete requestItem;
       
  1484 					iAddRequestArray.Compress();
       
  1485 					break;
       
  1486 					}        
       
  1487 				}    
       
  1488 			if(iProcessObservers)
       
  1489 				{
       
  1490 				iProcessObservers->HandleDeletionL(TVIMPSTEnums::EFriendRequestItem, KErrNotFound );
       
  1491 				}
       
  1492 			break;
       
  1493 			} 
       
  1494 		default :
       
  1495 			break;
       
  1496 		}
       
  1497     }
       
  1498 //-----------------------------------------------------------
       
  1499 //CVIMPSTProcessArray::DoHandleUnKnownContactMessageL
       
  1500 //-----------------------------------------------------------
       
  1501 // 
       
  1502 void CVIMPSTProcessArray::DoHandleUnKnownContactMessageL( const TDesC& aSenderId ,TBool aIsMsgPending )
       
  1503     {
       
  1504     TRACER_AUTO;
       
  1505     TBool contactExist = EFalse ;
       
  1506     TInt count = iUnKnownContactArray.Count();
       
  1507     CVIMPSTProcessUnknownContactItem* unknownItem = NULL;
       
  1508     for( TInt i=0; i<count; i++ )
       
  1509         {
       
  1510         unknownItem = iUnKnownContactArray[i];
       
  1511         if( aSenderId.Compare( unknownItem->GetItemUserId() ) == 0 )
       
  1512             {
       
  1513             contactExist = ETrue;
       
  1514             unknownItem->SetConversationOpen(ETrue);
       
  1515             unknownItem->SetMsgPending( aIsMsgPending );
       
  1516             break;
       
  1517             }
       
  1518         }
       
  1519     if( !contactExist )
       
  1520         {
       
  1521         CVIMPSTProcessUnknownContactItem* addItem = CVIMPSTProcessUnknownContactItem::NewL(*this, aSenderId);
       
  1522         /* Add it as the first Item of IAddRequestARray as its like stack */
       
  1523         iUnKnownContactArray.Insert(addItem,0); 
       
  1524         // ownership is in iItemArray
       
  1525         iItemArray.Insert(addItem, 1);  // just after own Item
       
  1526         addItem->SetMsgPending( aIsMsgPending );
       
  1527         addItem->SetConversationOpen(ETrue);
       
  1528         }
       
  1529     if(iProcessObservers)
       
  1530         {
       
  1531         iProcessObservers->HandleAdditionL( TVIMPSTEnums::EUnknonContactItem ,KErrNotFound );
       
  1532         }
       
  1533     }
       
  1534 //-----------------------------------------------------------
       
  1535 //CVIMPSTProcessArray::IsMsgPending
       
  1536 //-----------------------------------------------------------
       
  1537 // 
       
  1538 TBool CVIMPSTProcessArray::IsMsgPending(TInt aIndex)
       
  1539     {
       
  1540     if( aIndex < iItemArray.Count() && aIndex >= 0 )
       
  1541         {
       
  1542         MVIMPSTProcessArrayItem* item = (MVIMPSTProcessArrayItem *)iItemArray[aIndex];
       
  1543         if(item)
       
  1544             {   
       
  1545             return item->IsMsgPending();
       
  1546             }   
       
  1547         }
       
  1548     return EFalse;
       
  1549     }
       
  1550 
       
  1551 //-----------------------------------------------------------
       
  1552 //CVIMPSTProcessArray::IsConversationExist
       
  1553 //-----------------------------------------------------------
       
  1554 // 
       
  1555 TBool CVIMPSTProcessArray::IsConversationExist(TInt aIndex)
       
  1556     {
       
  1557     if( aIndex < iItemArray.Count() && aIndex >= 0 )
       
  1558         {
       
  1559         MVIMPSTProcessArrayItem* item = (MVIMPSTProcessArrayItem *)iItemArray[aIndex];
       
  1560         if(item)
       
  1561             {   
       
  1562             return item->IsConversationOpen();
       
  1563             }   
       
  1564         }
       
  1565     return EFalse;
       
  1566     }
       
  1567 
       
  1568 //-----------------------------------------------------------
       
  1569 //CVIMPSTProcessArray::ResetPendingMsg
       
  1570 //-----------------------------------------------------------
       
  1571 // 
       
  1572 void CVIMPSTProcessArray::ResetPendingMsg(TInt aIndex)
       
  1573     {
       
  1574     if( aIndex < iItemArray.Count() && aIndex >= 0 )
       
  1575         {
       
  1576         MVIMPSTProcessArrayItem* item = (MVIMPSTProcessArrayItem *)iItemArray[aIndex];
       
  1577         if(item)
       
  1578             {   
       
  1579             item->SetMsgPending( EFalse );
       
  1580             }   
       
  1581         }
       
  1582     }
       
  1583 //-----------------------------------------------------------
       
  1584 //CVIMPSTProcessArray::ResetOpenConversationPendingMsg
       
  1585 //-----------------------------------------------------------
       
  1586 // 
       
  1587 void CVIMPSTProcessArray::ResetOpenConversationPendingMsg()
       
  1588     {
       
  1589     for( TInt index  = 0; index < iItemArray.Count() ;index++ )
       
  1590         {
       
  1591         MVIMPSTProcessArrayItem* item = (MVIMPSTProcessArrayItem *)iItemArray[index];
       
  1592         if(item)
       
  1593             {   
       
  1594             item->SetConversationOpen( EFalse );
       
  1595             item->SetMsgPending( EFalse );
       
  1596             }   
       
  1597         }
       
  1598     }
       
  1599 // -----------------------------------------------------------------------------
       
  1600 // CVIMPSTProcessArray::FindAnyContact
       
  1601 // Try to load with given ID, return NULL if not found.
       
  1602 // -----------------------------------------------------------------------------
       
  1603 TBool CVIMPSTProcessArray::FindAnyContact( const TDesC& aContactId )
       
  1604     {
       
  1605     if(aContactId.Length() != 0)
       
  1606         {
       
  1607         MVIMPSTStorageContact* contact = iContactInterface->FindContactByUserId(aContactId);
       
  1608         if( contact )
       
  1609             {
       
  1610             return ETrue;
       
  1611             }
       
  1612         }
       
  1613     return EFalse;
       
  1614     }
       
  1615 
       
  1616 // -----------------------------------------------------------------------------
       
  1617 // CVIMPSTProcessArray::FindAnyContactServiceField
       
  1618 // Try to load with given ID, return NULL if not found.
       
  1619 // -----------------------------------------------------------------------------
       
  1620 TBool CVIMPSTProcessArray::FindAnyContactServiceField( const TDesC& aContactId )
       
  1621     {
       
  1622     if(aContactId.Length() != 0)
       
  1623         {
       
  1624         MVIMPSTStorageContact* contact = iContactInterface->FindContactByUserId(aContactId);
       
  1625         if( contact )
       
  1626             {
       
  1627             return ETrue;
       
  1628             }
       
  1629         }
       
  1630     return EFalse;
       
  1631     }
       
  1632 // -----------------------------------------------------------------------------
       
  1633 // CVIMPSTProcessArray::FindAndMarkContactAsOpenChat
       
  1634 // Try to load with given ID, return NULL if not found.
       
  1635 // -----------------------------------------------------------------------------
       
  1636 TBool CVIMPSTProcessArray::FindAndMarkContactAsOpenChat( const TDesC& aContactId, TBool aMesssagePending )
       
  1637     {
       
  1638     TPtrC senderId = VIMPSTUtils::DisplayId( aContactId );
       
  1639     TBool ret = EFalse;
       
  1640     TInt count = iItemArray.Count();
       
  1641     for( TInt i = 1; i < count; i++ )
       
  1642         {
       
  1643         MVIMPSTProcessArrayItem* contactItem = (MVIMPSTProcessArrayItem *)iItemArray[i];
       
  1644         TPtrC contactId = VIMPSTUtils::DisplayId( contactItem->GetItemUserId() );
       
  1645         if( senderId.Compare(  contactId ) == 0 )
       
  1646             {
       
  1647             contactItem->SetConversationOpen( ETrue );
       
  1648             contactItem->SetMsgPending( aMesssagePending );
       
  1649             ret = ETrue;
       
  1650             break;
       
  1651             }
       
  1652         }
       
  1653     return ret;
       
  1654     }
       
  1655 //-----------------------------------------------------------------------------
       
  1656 // CVIMPSTProcessArray::IsUnreadMessageExists
       
  1657 // ( Other items commented in header )
       
  1658 //-----------------------------------------------------------------------------    
       
  1659 TBool CVIMPSTProcessArray::IsUnreadMessageExistsL(const TDesC& aRecipientId) 
       
  1660     {
       
  1661     TBool ret = EFalse;
       
  1662     TInt unreaMsgCount = KErrNone;
       
  1663     //Get IM SubService     
       
  1664     MVIMPSTEngineSubService* subService =         
       
  1665             (iEngine.SubService(TVIMPSTEnums::EIM));
       
  1666     if(subService)
       
  1667         {
       
  1668         MVIMPSTEngineIMSubService& imSubService = 
       
  1669         MVIMPSTEngineIMSubService::Cast (*subService);
       
  1670         unreaMsgCount = imSubService.GetUnreadCountL( aRecipientId );
       
  1671         }
       
  1672     if( unreaMsgCount )
       
  1673         {
       
  1674         ret = ETrue;
       
  1675         }
       
  1676     return ret; 
       
  1677     }
       
  1678 //-----------------------------------------------------------------------------
       
  1679 // CVIMPSTProcessArray::IsConversationExistL
       
  1680 // ( Other items commented in header )
       
  1681 //-----------------------------------------------------------------------------    
       
  1682 TBool CVIMPSTProcessArray::IsConversationExistL(const TDesC& aRecipientId) 
       
  1683     {
       
  1684     //Get IM SubService     
       
  1685     MVIMPSTEngineSubService* subService1 =    	   
       
  1686                 (iEngine.SubService(TVIMPSTEnums::EIM));
       
  1687     if(subService1)
       
  1688         {
       
  1689         MVIMPSTEngineIMSubService& imSubService = 
       
  1690                     MVIMPSTEngineIMSubService::Cast (*subService1);
       
  1691         return imSubService.IsConversationExistL( aRecipientId );
       
  1692         }
       
  1693     return EFalse; 
       
  1694     }
       
  1695 //-----------------------------------------------------------------------------
       
  1696 // CVIMPSTProcessArray::FindContactIndexL
       
  1697 // ( Other items commented in header )
       
  1698 //-----------------------------------------------------------------------------    
       
  1699 TInt CVIMPSTProcessArray::FindContactIndexL( const TDesC& aContactId )
       
  1700     {
       
  1701     MVIMPSTStorageContact* newContact = iContactInterface->FindContactByUserId( aContactId );
       
  1702     if(newContact)
       
  1703         {
       
  1704         // return the index of contact rather bool value..
       
  1705         // 1 is for own data item.
       
  1706         return (iContactListIndex + 1 + iContactListModel->IndexOfContact( newContact ));
       
  1707         }
       
  1708     return KErrNotFound;
       
  1709     }
       
  1710 
       
  1711 
       
  1712 //-----------------------------------------------------------------------------
       
  1713 // CVIMPSTProcessArray::FillUnknownContactsL
       
  1714 // ( Other items commented in header )
       
  1715 //------------------------------------------------------------------------------
       
  1716 void CVIMPSTProcessArray::FillUnknownContactsL()
       
  1717     {
       
  1718     TInt count = iUnKnownContactArray.Count();
       
  1719     /* 
       
  1720      * Index = 1 cos 0th index will be OwnData, and 1 - n where n is the number of friend requests, will be friend requests 
       
  1721      */
       
  1722     TInt index = 1; 
       
  1723     for(TInt i = 0 ; i < count ; i++)
       
  1724         {
       
  1725         // all friend request item ownership is transfered to iItemArray
       
  1726         CVIMPSTProcessUnknownContactItem* unknownItem = iUnKnownContactArray[i];
       
  1727         unknownItem->SetItemIndex(index);        
       
  1728         iItemArray.Insert(unknownItem, index);        
       
  1729         index ++;
       
  1730         }
       
  1731     }
       
  1732 
       
  1733 //-----------------------------------------------------------------------------
       
  1734 // CVIMPSTProcessArray::FillAddRequestDataL
       
  1735 // ( Other items commented in header )
       
  1736 //------------------------------------------------------------------------------
       
  1737 void CVIMPSTProcessArray::FillAddRequestDataL()
       
  1738     {
       
  1739     TInt count = iAddRequestArray.Count();
       
  1740 	/* 
       
  1741 	 * Index = unknowncontacts are tops so after that 1 cos 0th index will be OwnData, and 1 - n where n is the number of friend requests, will be friend requests 
       
  1742 	 */
       
  1743 	TInt index = iUnKnownContactArray.Count() +1 ; 
       
  1744     for(TInt i = 0 ; i < count ; i++)
       
  1745         {
       
  1746         // all friend request item ownership is transfered to iItemArray
       
  1747         CVIMPSTProcessFriendRequestItem* requestItem = iAddRequestArray[i];
       
  1748         requestItem->SetItemIndex(index );        
       
  1749         iItemArray.Insert(requestItem, index);        
       
  1750 		index ++;
       
  1751         }
       
  1752     }
       
  1753 
       
  1754 //-----------------------------------------------------------------------------
       
  1755 // CVIMPSTProcessArray::RemoveUnKnownContacts
       
  1756 // ( Other items commented in header )
       
  1757 //------------------------------------------------------------------------------
       
  1758 void CVIMPSTProcessArray::RemoveUnKnownContacts()
       
  1759     {
       
  1760     TInt count = iUnKnownContactArray.Count();
       
  1761     for(TInt i = 0 ; i < count ; i++)
       
  1762         {
       
  1763         CVIMPSTProcessUnknownContactItem* unknownItem = iUnKnownContactArray[i];
       
  1764         TInt index = iItemArray.Find(unknownItem);
       
  1765         if(index > -1)
       
  1766             {            
       
  1767             iItemArray.Remove(index);
       
  1768             iItemArray.Compress();
       
  1769             }       
       
  1770         }
       
  1771     }
       
  1772 //-----------------------------------------------------------------------------
       
  1773 // CVIMPSTProcessArray::RemoveAddRequestData
       
  1774 // ( Other items commented in header )
       
  1775 //------------------------------------------------------------------------------
       
  1776 void CVIMPSTProcessArray::RemoveAddRequestData()
       
  1777     {
       
  1778     TInt count = iAddRequestArray.Count();
       
  1779     for(TInt i = 0 ; i < count ; i++)
       
  1780         {
       
  1781         CVIMPSTProcessFriendRequestItem* requestItem = iAddRequestArray[i];
       
  1782         TInt index = iItemArray.Find(requestItem);
       
  1783         if(index > -1)
       
  1784             {            
       
  1785             iItemArray.Remove(index);
       
  1786             iItemArray.Compress();
       
  1787             }       
       
  1788         }
       
  1789     
       
  1790     }
       
  1791 //-----------------------------------------------------------------------------
       
  1792 // CVIMPSTProcessArray::LoginUserIdFromStoreL
       
  1793 // ( Other items commented in header )
       
  1794 //------------------------------------------------------------------------------
       
  1795 
       
  1796 const TDesC& CVIMPSTProcessArray::LoginUserIdFromStoreL() const
       
  1797     {
       
  1798     if( iContactInterface )
       
  1799 		{
       
  1800 		// most of the time this get called 
       
  1801 		return iContactInterface->OwnContactL().UserId();
       
  1802 		}
       
  1803     return KNullDesC;
       
  1804     }
       
  1805 
       
  1806 //-----------------------------------------------------------------------------
       
  1807 // CVIMPSTProcessArray::AvatarIndex
       
  1808 // ( Other items commented in header )
       
  1809 //------------------------------------------------------------------------------
       
  1810 
       
  1811 TInt CVIMPSTProcessArray::AvatarIndex(TInt aContactIndex)
       
  1812     {
       
  1813     TVIMPSTEnums::TItem itemtype = GetType( aContactIndex );
       
  1814     /* Codescanner warning is ignored, since Bound check is done 
       
  1815      * inside the GetType()   method*/
       
  1816 
       
  1817     if( TVIMPSTEnums::EContactItem == itemtype )
       
  1818         {
       
  1819        // if the type is a contact item then return the index
       
  1820         MVIMPSTStorageContact* contact = iContactInterface->FindContactByUserId( GetItemUserId(aContactIndex) );
       
  1821         if(contact)
       
  1822             {
       
  1823             // if any contact is found then return the index
       
  1824             return contact->AvatarIndex();
       
  1825             }
       
  1826         }
       
  1827     return KErrNone;
       
  1828     }
       
  1829 //-----------------------------------------------------------------------------
       
  1830 // CVIMPSTProcessArray::SetAvatarIndex
       
  1831 // ( Other items commented in header )
       
  1832 //------------------------------------------------------------------------------
       
  1833 
       
  1834 void CVIMPSTProcessArray::SetAvatarIndex( TInt aContactIndex ,TInt aAvatarIndex )
       
  1835     {
       
  1836     TVIMPSTEnums::TItem itemtype = GetType( aContactIndex );
       
  1837     if( TVIMPSTEnums::EContactItem == itemtype )
       
  1838         {
       
  1839         // if the type is a contact item then set the index 
       
  1840         MVIMPSTStorageContact* contact = iContactInterface->FindContactByUserId(GetItemUserId(aContactIndex));
       
  1841         if(contact)
       
  1842             {
       
  1843             // if any contact is found then set the index
       
  1844             contact->SetAvatarIndex( aAvatarIndex );
       
  1845             }           
       
  1846             
       
  1847         iItemArray[aContactIndex]->SetAvatarIndex(aAvatarIndex);
       
  1848         }
       
  1849 
       
  1850     }
       
  1851 
       
  1852 //-----------------------------------------------------------------------------
       
  1853 // CVIMPSTProcessArray::AvatarContent
       
  1854 // ( Other items commented in header )
       
  1855 //------------------------------------------------------------------------------
       
  1856 
       
  1857 const TDesC8& CVIMPSTProcessArray::AvatarContent(TInt aContactIndex)
       
  1858     {   
       
  1859     TVIMPSTEnums::TItem itemtype = GetType( aContactIndex );
       
  1860 
       
  1861     if(  itemtype == TVIMPSTEnums::EContactItem )
       
  1862         {
       
  1863         // if the type is a contact item then get the avatar content from strorage
       
  1864         MVIMPSTStorageContact* contact = iContactInterface->FindContactByUserId(GetItemUserId(aContactIndex));
       
  1865         if(contact)
       
  1866             {
       
  1867             // if any contact is found then get the content
       
  1868             return contact->AvatarContent();
       
  1869             }
       
  1870         }
       
  1871     return KNullDesC8();
       
  1872     }
       
  1873 //-----------------------------------------------------------------------------
       
  1874 // CVIMPSTProcessArray::OwnAvatarContentL
       
  1875 // ( Other items commented in header )
       
  1876 //------------------------------------------------------------------------------
       
  1877 
       
  1878 const TDesC8& CVIMPSTProcessArray::OwnAvatarContentL( ) const
       
  1879 	{   
       
  1880     MVIMPSTStorageContact& contact = iContactInterface->OwnContactL();             
       
  1881     // if any contact is found then get the content
       
  1882     return contact.AvatarContent();
       
  1883 	}
       
  1884 //-----------------------------------------------------------------------------
       
  1885 // CVIMPSTProcessArray::OwnAvatarIndexL
       
  1886 // ( Other items commented in header )
       
  1887 //------------------------------------------------------------------------------
       
  1888 
       
  1889 TInt CVIMPSTProcessArray::OwnAvatarIndexL( )
       
  1890     {
       
  1891     MVIMPSTStorageContact& contact = iContactInterface->OwnContactL();  
       
  1892     return contact.AvatarIndex();           
       
  1893     }
       
  1894 //-----------------------------------------------------------------------------
       
  1895 // CVIMPSTProcessArray::SetOwnAvatarIndexL
       
  1896 // ( Other items commented in header )
       
  1897 //------------------------------------------------------------------------------
       
  1898 
       
  1899 void CVIMPSTProcessArray::SetOwnAvatarIndexL( TInt aAvatarIndex )
       
  1900     {
       
  1901     MVIMPSTStorageContact& contact = iContactInterface->OwnContactL();             
       
  1902     contact.SetAvatarIndex( aAvatarIndex );
       
  1903     }
       
  1904 
       
  1905 //-----------------------------------------------------------------------------
       
  1906 // CVIMPSTProcessArray::FindAnyAddRequest
       
  1907 // ( Other items commented in header )
       
  1908 //------------------------------------------------------------------------------
       
  1909 
       
  1910 TInt CVIMPSTProcessArray::FindAnyAddRequest( const TDesC& aRequesterId )
       
  1911     {
       
  1912     TInt count = iAddRequestArray.Count();
       
  1913     for(TInt i=0; i<count; i++)
       
  1914         {   
       
  1915         if((aRequesterId.Compare(iAddRequestArray[i]->GetItemUserId()))==0 )                
       
  1916             {
       
  1917             return ETrue;
       
  1918             }
       
  1919         }
       
  1920     return EFalse;
       
  1921     }
       
  1922 
       
  1923 // -----------------------------------------------------------------------------
       
  1924 // CVIMPSTProcessArray::GetSelectedItemIndex
       
  1925 // return contact index including friend request.
       
  1926 // -----------------------------------------------------------------------------
       
  1927 TInt CVIMPSTProcessArray::GetSelectedItemIndex(const TDesC& aContactId) 
       
  1928 	{
       
  1929 	TInt itemArrayCount = iItemArray.Count();
       
  1930 	for(TInt index=0; index<itemArrayCount; index++)
       
  1931 		{
       
  1932 		MVIMPSTProcessArrayItem* item = iItemArray[ index ];
       
  1933 		if(0 == aContactId.Compare(item->GetItemUserId()))
       
  1934 		    {
       
  1935 		    return index; 
       
  1936 		    }
       
  1937 		}
       
  1938 	return KErrNotFound;
       
  1939 	}
       
  1940 
       
  1941 // --------------------------------------------------------------------------
       
  1942 // CVIMPSTProcessArray::IsContactBlocked
       
  1943 // --------------------------------------------------------------------------
       
  1944 TBool CVIMPSTProcessArray::IsContactBlocked(const TDesC& aUserId)
       
  1945     {
       
  1946     MVIMPSTEngineSubService* subService =          
       
  1947                         (iEngine.SubService(TVIMPSTEnums::EPresence));
       
  1948     if(subService)
       
  1949         {
       
  1950         MVIMPSTEnginePresenceSubService& presence = 
       
  1951                 MVIMPSTEnginePresenceSubService::Cast (*subService);
       
  1952                 
       
  1953         RPointerArray<HBufC> *blockedlist = presence .GetBlockedList();
       
  1954         
       
  1955         if(blockedlist)
       
  1956             {
       
  1957             for(TInt i=0; i<blockedlist->Count(); ++i)
       
  1958                 {
       
  1959                 if(0 == aUserId.CompareC((*blockedlist)[i]->Des()))
       
  1960                     {
       
  1961                     return ETrue;
       
  1962                     }
       
  1963                 }
       
  1964             }
       
  1965         }
       
  1966     return EFalse;  
       
  1967     }       
       
  1968 
       
  1969 
       
  1970 // --------------------------------------------------------------------------
       
  1971 // CVIMPSTProcessArray::GetFormattedString
       
  1972 // --------------------------------------------------------------------------
       
  1973 TPtrC16 CVIMPSTProcessArray::GetFormattedString(TInt aIndex)
       
  1974     {
       
  1975     if((aIndex >= 0) && (aIndex< iItemArray.Count()))
       
  1976         return iItemArray[aIndex]->FormattedItemString();
       
  1977     else
       
  1978         return KNullDesC();
       
  1979     }
       
  1980 
       
  1981 //END OF FILE