ximpfw/presence/srcpresencecache/presencecacheserver/presencecachesession.cpp
changeset 0 e6b17d312c8b
child 21 5872ac9a5671
equal deleted inserted replaced
-1:000000000000 0:e6b17d312c8b
       
     1 /*
       
     2 * Copyright (c) 2007, 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:  Impementation for Presence Cache Server session
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <presenceobjectfactory.h>
       
    21 #include <personpresenceinfo.h>
       
    22 #include <presencebuddyinfo.h>
       
    23 #include <presencebuddyinfolist.h>
       
    24 #include <presenceinfofieldcollection.h>
       
    25 
       
    26 #include "presencecachesession.h"
       
    27 #include "presencecacheserver.h"
       
    28 #include "ximptrace.h"
       
    29 #include "ximpobjecthelpers.h"
       
    30 #include "presenceinfoimp.h"
       
    31 #include "presencebuddyinfolistimp.h"
       
    32 #include "presencebuddyinfoimp.h"
       
    33 #include "ximpidentityimp.h"
       
    34 
       
    35 #include "presencecacheservicestore.h"
       
    36 #include "presencecachebuddystore.h"
       
    37 
       
    38 //Include Cache server namespace
       
    39 using namespace NCacheSrv;
       
    40 
       
    41 
       
    42 // ---------------------------------------------------------------------------
       
    43 // CPresenceCacheSession::NewL()
       
    44 // ---------------------------------------------------------------------------
       
    45 //
       
    46 CPresenceCacheSession* CPresenceCacheSession::NewL( CPresenceCacheServer& aServer )
       
    47     {
       
    48     CPresenceCacheSession* self = CPresenceCacheSession::NewLC( aServer );
       
    49     CleanupStack::Pop(self);
       
    50     return self;    
       
    51     }
       
    52     
       
    53 // ---------------------------------------------------------------------------
       
    54 // CPresenceCacheSession::NewLC()
       
    55 // ---------------------------------------------------------------------------
       
    56 //
       
    57 CPresenceCacheSession* CPresenceCacheSession::NewLC( CPresenceCacheServer& aServer )
       
    58     {
       
    59     CPresenceCacheSession* self = new ( ELeave ) CPresenceCacheSession( aServer );
       
    60     CleanupStack::PushL(self);
       
    61     self->ConstructL();
       
    62     return self;    
       
    63     }
       
    64 
       
    65 // ---------------------------------------------------------------------------
       
    66 // CPresenceCacheSession::~CPresenceCacheSession()
       
    67 // ---------------------------------------------------------------------------
       
    68 //
       
    69 CPresenceCacheSession::~CPresenceCacheSession()
       
    70     {
       
    71     if(iDataPack)
       
    72         delete iDataPack;
       
    73     if(iDataPackAsync)
       
    74         delete iDataPackAsync;
       
    75     if(iDataPackNotifier)
       
    76         delete iDataPackNotifier;
       
    77     if(iBuddypresInfoList)
       
    78         delete iBuddypresInfoList;
       
    79     if(iActiveHelper)
       
    80         delete iActiveHelper;
       
    81     iServer.DecrementSessions();
       
    82     RemoveMySubscriptions(); // remove all subscriptions subscribed by this session
       
    83     iSubscribedStores.Close();
       
    84     TRACE_1( _L("CPresenceCacheSession[%d]::~CPresenceCacheSession()"), this );
       
    85     }
       
    86 
       
    87 
       
    88 // ---------------------------------------------------------------------------
       
    89 // CPresenceCacheSession::ServiceL()
       
    90 // ---------------------------------------------------------------------------
       
    91 //
       
    92 void CPresenceCacheSession::ServiceL( const RMessage2& aMessage )
       
    93     {
       
    94     switch ( aMessage.Function() )
       
    95         {
       
    96         case NRequest::EPrepReadPresenceInfoSync:
       
    97             HandlePrepReadPresenceInfoSyncL(aMessage);
       
    98             break;
       
    99             
       
   100         case NRequest::EGetLastPreparedPacket:
       
   101             HandleGetLastPreparedPacketL(aMessage);
       
   102             break;
       
   103             
       
   104         case NRequest::EGetLastAsyncPreparedPacket:
       
   105             HandleGetLastPreparedAsyncPacketL(aMessage);
       
   106             break;
       
   107             
       
   108         case NRequest::EBuddyCountInAllServices:
       
   109             HandleBuddyCountInAllServices(aMessage);
       
   110             break;
       
   111             
       
   112         case NRequest::EBuddyCountInService:
       
   113             HandleBuddyCountInService(aMessage);
       
   114             break;
       
   115             
       
   116         case NRequest::EGetServiceCount:
       
   117             HandleGetServiceCount(aMessage);
       
   118             break;
       
   119             
       
   120         case NRequest::EDeleteService:
       
   121             HandleDeleteService(aMessage);
       
   122             break;
       
   123             
       
   124         case NRequest::EDeletePresence:
       
   125             HandleDeletePresence(aMessage);
       
   126             break;
       
   127             
       
   128         case NRequest::ECancelAsyncOperation:
       
   129             HandleCancel(aMessage);
       
   130             break;
       
   131 
       
   132         case NRequest::EWritePresenceInfoSync:
       
   133             HandleWritePresenceInfoSyncL(aMessage);
       
   134             break;
       
   135 
       
   136         case NRequest::EWritePresenceInfoAsync:
       
   137             HandleWritePresenceInfoAsyncL(aMessage);
       
   138             break;
       
   139             
       
   140         case NRequest::EPrepReadAllBuddiesPresenceInService:
       
   141             HandlePrepReadAllBuddiesPresenceInService(aMessage);
       
   142             break;
       
   143             
       
   144         case NRequest::ESubscribeBuddyPresenceChange:// 0:aService.iUid, 1:myIdPack
       
   145             HandleSubscribeBuddyPresenceChange(aMessage);
       
   146             break;
       
   147         
       
   148         case NRequest::EUnSubscribeBuddyPresenceChange: // 0:aService.iUid, 1:myIdPack
       
   149             HandleUnSubscribeBuddyPresenceChange(aMessage);
       
   150             break;
       
   151         
       
   152         case NRequest::EGetLastNotifiedtPacket: // 0:&ptrBuf
       
   153             HandleGetLastNotifiedtPacketL(aMessage);
       
   154             break;
       
   155         
       
   156         case NRequest::EWaitingForNotification: // 0:&sizePckg
       
   157             HandleWaitingForNotification(aMessage);
       
   158             break;
       
   159         
       
   160         case NRequest::ECancelWaitingForNotification:
       
   161             HandleCancelWaitingForNotification(aMessage);
       
   162             break;
       
   163 
       
   164         default:
       
   165         	TRACE( _L("CPresenceCacheSession::ServiceL - default"));
       
   166             aMessage.Complete(KErrArgument);
       
   167             break;
       
   168         }
       
   169     }
       
   170 
       
   171 // ---------------------------------------------------------------------------
       
   172 // CPresenceCacheSession::HandlePrepReadPresenceInfoSyncL()
       
   173 // ---------------------------------------------------------------------------
       
   174 //
       
   175 void CPresenceCacheSession::HandlePrepReadPresenceInfoSyncL( const RMessage2& aMessage )
       
   176     {
       
   177     TRACE( _L("CPresenceCacheSession::HandlePrepReadPresenceInfoSyncL - begin"));
       
   178 
       
   179     TInt sizeOfReturnPacket(NULL);
       
   180  
       
   181     // unpacking identity
       
   182     const TInt pckSize = aMessage.GetDesLengthL(1);
       
   183     HBufC8* idPack = HBufC8::NewLC( pckSize );
       
   184     TPtr8 paramPckPtr = idPack->Des();
       
   185     aMessage.ReadL( 1, paramPckPtr );
       
   186     CXIMPIdentityImp* identity = CXIMPIdentityImp::NewLC();
       
   187     TXIMPObjectPacker< CXIMPIdentityImp >::UnPackL(*identity, *idPack);
       
   188     
       
   189     TPtrC serviceName = GetServiceName(identity->Identity());
       
   190     
       
   191     // now processing
       
   192     TInt serviceCount = iServer.iPresenceCache.Count();
       
   193     MPresenceBuddyInfo* buddyPresenceInfo(NULL);
       
   194     CPresenceCacheBuddyStore* buddyStore(NULL);
       
   195     
       
   196     TRACE_1( _L("_______serviceCount[%d]"), serviceCount);
       
   197     
       
   198     for(TInt i=0; i<serviceCount; i++)
       
   199         {
       
   200         if (serviceName == (iServer.iPresenceCache[i])->ServiceName())
       
   201             {
       
   202             buddyStore = (iServer.iPresenceCache[i])->FindAndGet(*identity);
       
   203             if(buddyStore)
       
   204                 buddyPresenceInfo = buddyStore->PresenceBuddyInfo();
       
   205             break;
       
   206             }
       
   207         }
       
   208     
       
   209     CleanupStack::PopAndDestroy(identity);
       
   210     CleanupStack::PopAndDestroy(idPack);
       
   211     
       
   212     TInt err = KErrNotFound;
       
   213     
       
   214     TRACE_1( _L("_______buddyPresenceInfo[%d]"), buddyPresenceInfo);
       
   215     if(buddyPresenceInfo) // if found
       
   216         {
       
   217         const CPresenceInfoImp* presInfo = 
       
   218             TXIMPGetImpClassOrPanic< const CPresenceInfoImp >::From(*(buddyPresenceInfo->PresenceInfo()));
       
   219         if(iDataPack)
       
   220             delete iDataPack; iDataPack=NULL;
       
   221         iDataPack = PackPresenceInfoLC(*presInfo);
       
   222         CleanupStack::Pop(iDataPack);
       
   223         sizeOfReturnPacket = (iDataPack->Des()).Size();
       
   224         err = KErrNone;
       
   225         }
       
   226     
       
   227     TPckgBuf<TInt> p(sizeOfReturnPacket);
       
   228     aMessage.WriteL(2,p);            
       
   229     aMessage.Complete(err);
       
   230     TRACE( _L("CPresenceCacheSession::HandlePrepReadPresenceInfoSyncL - end"));
       
   231     }
       
   232 
       
   233 // ---------------------------------------------------------------------------
       
   234 // CPresenceCacheSession::HandleGetLastPreparedPacketL()
       
   235 // ---------------------------------------------------------------------------
       
   236 //
       
   237 void CPresenceCacheSession::HandleGetLastPreparedPacketL( const RMessage2& aMessage )
       
   238     {
       
   239     TRACE( _L("CPresenceCacheSession::HandleGetLastPreparedPacketL() - begin"));
       
   240     if(iDataPack)
       
   241         aMessage.WriteL(0, *(iDataPack));
       
   242     aMessage.Complete(KErrNone);   
       
   243     }
       
   244 
       
   245 // ---------------------------------------------------------------------------
       
   246 // CPresenceCacheSession::HandleGetLastPreparedAsyncPacketL()
       
   247 // ---------------------------------------------------------------------------
       
   248 //
       
   249 void CPresenceCacheSession::HandleGetLastPreparedAsyncPacketL( const RMessage2& aMessage )
       
   250     {
       
   251     if(iDataPackAsync)
       
   252         aMessage.WriteL(0, *(iDataPackAsync));
       
   253     aMessage.Complete(KErrNone);   
       
   254     }
       
   255 
       
   256 // ---------------------------------------------------------------------------
       
   257 // CPresenceCacheSession::HandleWritePresenceInfoSyncL()
       
   258 // ---------------------------------------------------------------------------
       
   259 //
       
   260 void CPresenceCacheSession::HandleWritePresenceInfoSyncL(const RMessage2& aMessage )
       
   261     {
       
   262     TRACE( _L("CPresenceCacheSession::HandleWritePresenceInfoSyncL - begin"));
       
   263     TInt err(KErrNone);
       
   264     const TInt pckSize = aMessage.GetDesLengthL(0);
       
   265     if(iDataPack)
       
   266         delete iDataPack; iDataPack=NULL;
       
   267     iDataPack = HBufC8::NewL( pckSize );
       
   268     TPtr8 paramPckPtr = iDataPack->Des();
       
   269     aMessage.ReadL( 0, paramPckPtr );
       
   270     
       
   271     CPresenceBuddyInfoImp* buddypresenceInfo = CPresenceBuddyInfoImp::NewLC();
       
   272    
       
   273 
       
   274     TXIMPObjectPacker< CPresenceBuddyInfoImp >::UnPackL(*buddypresenceInfo, *iDataPack);
       
   275     
       
   276     err = TryWriteBuddyToCacheL(buddypresenceInfo);
       
   277     
       
   278     if(err==KErrNone) //ownership transferred    
       
   279         CleanupStack::Pop(buddypresenceInfo);
       
   280     else
       
   281         CleanupStack::PopAndDestroy(buddypresenceInfo);    
       
   282     aMessage.Complete(err);
       
   283     TRACE( _L("CPresenceCacheSession::HandleWritePresenceInfoSyncL - end"));
       
   284     }
       
   285 
       
   286 // ---------------------------------------------------------------------------
       
   287 // CPresenceCacheSession::HandlePrepReadAllBuddiesPresenceInService()
       
   288 // ---------------------------------------------------------------------------
       
   289 //
       
   290 void CPresenceCacheSession::HandlePrepReadAllBuddiesPresenceInService
       
   291                                                         (const RMessage2& aMessage )
       
   292     {
       
   293     if(iActiveHelper->IsActive())
       
   294         aMessage.Complete(KErrServerBusy);
       
   295     else
       
   296         {
       
   297         iMessage = aMessage;
       
   298         
       
   299         iAsyncReq = NRequest::EPrepReadAllBuddiesPresenceInService;
       
   300         iActiveHelper->Start();
       
   301         }
       
   302     }
       
   303 
       
   304 // ---------------------------------------------------------------------------
       
   305 // CPresenceCacheSession::ReadAllBuddiesPresenceInServiceL()
       
   306 // ---------------------------------------------------------------------------
       
   307 //
       
   308 void CPresenceCacheSession::ReadAllBuddiesPresenceInServiceL()
       
   309     {
       
   310     const TInt pckSize = iMessage.GetDesLengthL(0);
       
   311     HBufC* serviceName = HBufC16::NewLC( pckSize );
       
   312     TPtr16 paramPckPtr = serviceName->Des();
       
   313     iMessage.ReadL( 0, paramPckPtr );
       
   314     
       
   315     TInt serviceCount = iServer.iPresenceCache.Count();
       
   316     CPresenceCacheServiceStore* serviceStore(NULL);
       
   317     CPresenceBuddyInfoListImp* buddyPresenceInfoList(NULL);
       
   318     CPresenceCacheBuddyStore* buddyStore(NULL);
       
   319     MPresenceBuddyInfo* presBuddyInfo(NULL);
       
   320     TInt presCount(0);
       
   321     TInt count(0);
       
   322     
       
   323     for(TInt i=0; i<serviceCount; i++)
       
   324         {
       
   325         serviceStore = iServer.iPresenceCache[i];
       
   326         if (serviceName->Des() == serviceStore->ServiceName())
       
   327             {
       
   328             presCount = serviceStore->PresenceCount();
       
   329             // if it has presences
       
   330             if(presCount)
       
   331                 {
       
   332                 buddyPresenceInfoList = CPresenceBuddyInfoListImp::NewLC(*serviceName);
       
   333                 buddyPresenceInfoList->SetOwnObjects(EFalse);
       
   334                 count = serviceStore->Count();
       
   335                 for(TInt j=0; j<count; j++)
       
   336                     {
       
   337                     buddyStore = serviceStore->GetObjectCollection()[j];
       
   338                     presBuddyInfo = buddyStore->PresenceBuddyInfo();
       
   339                     if(presBuddyInfo)
       
   340                         buddyPresenceInfoList->BlindAddL(presBuddyInfo);    
       
   341                     }
       
   342                 }
       
   343             break;
       
   344             }
       
   345         }
       
   346     
       
   347     TInt size(KErrNotFound); // this may also indicate error to client if negative
       
   348     if(buddyPresenceInfoList) // if found
       
   349         {
       
   350         if(iDataPackAsync)
       
   351             delete iDataPackAsync; iDataPackAsync = NULL;
       
   352         iDataPackAsync = TXIMPObjectPacker< CPresenceBuddyInfoListImp>::PackL( *buddyPresenceInfoList);
       
   353         size = (iDataPackAsync->Des()).Size();
       
   354         CleanupStack::PopAndDestroy(buddyPresenceInfoList);
       
   355         }
       
   356     
       
   357     CleanupStack::PopAndDestroy(serviceName);
       
   358     iAsyncReq = NRequest::ENoRequestMade;
       
   359     iMessage.Complete(size);
       
   360     }
       
   361     
       
   362 
       
   363 // ---------------------------------------------------------------------------
       
   364 // CPresenceCacheSession::TryWriteBuddyToCache()
       
   365 // ---------------------------------------------------------------------------
       
   366 //
       
   367 TInt CPresenceCacheSession::TryWriteBuddyToCacheL(MPresenceBuddyInfo* aBuddyPresInfo)
       
   368     {
       
   369     if(!aBuddyPresInfo)
       
   370         return KErrArgument;
       
   371     
       
   372     TPtrC serviceName = GetServiceName(aBuddyPresInfo->BuddyId()->Identity());
       
   373     TInt serviceCount = iServer.iPresenceCache.Count();
       
   374     CPresenceCacheBuddyStore* buddyStore(NULL);
       
   375     
       
   376     for(TInt i=0; i<serviceCount; i++)
       
   377         {
       
   378         if (serviceName == (iServer.iPresenceCache[i])->ServiceName())
       
   379             {
       
   380             buddyStore = (iServer.iPresenceCache[i])->AddOrReplacePresenceL(aBuddyPresInfo);
       
   381             break;
       
   382             }
       
   383         }
       
   384     if(!buddyStore) // if not written
       
   385         {
       
   386         CPresenceCacheServiceStore* newServiceStore = CPresenceCacheServiceStore::NewLC();
       
   387         newServiceStore->SetServiceNameL(serviceName);
       
   388         buddyStore = newServiceStore->AddOrReplacePresenceL(aBuddyPresInfo);
       
   389         (iServer.iPresenceCache).Append(newServiceStore); //ownership transferred
       
   390         CleanupStack::Pop(newServiceStore);
       
   391         }
       
   392         
       
   393     if(buddyStore) //  inform to subscribers
       
   394         NotifyAllSubscribersL(buddyStore);
       
   395         
       
   396     return buddyStore ? KErrNone : KErrGeneral;
       
   397     }
       
   398     
       
   399 
       
   400 // ---------------------------------------------------------------------------
       
   401 // CPresenceCacheSession::HandleCancel()
       
   402 // ---------------------------------------------------------------------------
       
   403 //
       
   404 void CPresenceCacheSession::HandleCancel(const RMessage2& aMessage )
       
   405     {
       
   406     if (iActiveHelper->IsActive())
       
   407         iActiveHelper->Cancel(); //see also iActiveHelper->DoCancel()
       
   408     aMessage.Complete(KErrNotFound);
       
   409     }
       
   410     
       
   411 // ---------------------------------------------------------------------------
       
   412 // CPresenceCacheSession::HandleBuddyCountInAllServices()
       
   413 // ---------------------------------------------------------------------------
       
   414 //
       
   415 void CPresenceCacheSession::HandleBuddyCountInAllServices(const RMessage2& aMessage )
       
   416     {
       
   417     TRACE( _L("CPresenceCacheSession::HandleBuddyCountInAllServices - begin"));
       
   418     TInt count(NULL);
       
   419     TInt serviceCount = iServer.iPresenceCache.Count();
       
   420     
       
   421     for(TInt i=0; i<serviceCount; i++)
       
   422         {
       
   423         count = (count) + ((iServer.iPresenceCache[i])->PresenceCount());
       
   424         }
       
   425     
       
   426     TPckgBuf<TInt> p(count);
       
   427     aMessage.WriteL(0,p);               
       
   428     aMessage.Complete(KErrNone);
       
   429     TRACE( _L("CPresenceCacheSession::HandleBuddyCountInAllServices - end"));
       
   430     }
       
   431 
       
   432 // ---------------------------------------------------------------------------
       
   433 // CPresenceCacheSession::HandleBuddyCountInService()
       
   434 // ---------------------------------------------------------------------------
       
   435 //
       
   436 void CPresenceCacheSession::HandleBuddyCountInService(const RMessage2& aMessage )
       
   437     {
       
   438     TInt count(NULL);
       
   439     
       
   440     const TInt pckSize = aMessage.GetDesLengthL(1);
       
   441     HBufC* serviceName = HBufC16::NewLC( pckSize );
       
   442     TPtr16 paramPckPtr = serviceName->Des();
       
   443     aMessage.ReadL( 1, paramPckPtr );
       
   444     
       
   445     TInt serviceCount = iServer.iPresenceCache.Count();
       
   446     TInt err(KErrNotFound);
       
   447     
       
   448     for(TInt i=0; i<serviceCount; i++)
       
   449         {
       
   450         if (serviceName->Des() == (iServer.iPresenceCache[i])->ServiceName())
       
   451             {
       
   452             count = ((iServer.iPresenceCache[i])->PresenceCount());
       
   453             err = KErrNone;
       
   454             break;
       
   455             }
       
   456         }
       
   457     
       
   458     CleanupStack::PopAndDestroy(serviceName);
       
   459     TPckgBuf<TInt> p(count);
       
   460     aMessage.WriteL(0,p);
       
   461     aMessage.Complete(err);    
       
   462     }
       
   463 
       
   464 // ---------------------------------------------------------------------------
       
   465 // CPresenceCacheSession::HandleDeleteService()
       
   466 // ---------------------------------------------------------------------------
       
   467 //
       
   468 void CPresenceCacheSession::HandleDeleteService(const RMessage2& aMessage )
       
   469     {
       
   470     TRACE( _L("CPresenceCacheSession::HandleDeleteService - begin"));
       
   471     TInt err(KErrNotFound);
       
   472     
       
   473     const TInt pckSize = aMessage.GetDesLengthL(0);
       
   474     HBufC* serviceName = HBufC16::NewLC( pckSize );
       
   475     TPtr16 paramPckPtr = serviceName->Des();
       
   476     aMessage.ReadL( 0, paramPckPtr );
       
   477     
       
   478     TInt serviceCount = iServer.iPresenceCache.Count();
       
   479     TInt buddyCount(NULL);
       
   480     
       
   481     TRACE_1( _L("_______serviceCount[%d]"), serviceCount);
       
   482     for(TInt i=0; i<serviceCount; i++)
       
   483         {
       
   484         if (serviceName->Des() == (iServer.iPresenceCache[i])->ServiceName())
       
   485             {
       
   486             (iServer.iPresenceCache[i])->RemoveAllPresences();
       
   487             buddyCount = (iServer.iPresenceCache[i])->GetObjectCollection().Count();
       
   488             TRACE_1( _L("_______buddyCountAfter[%d]"), buddyCount);
       
   489             
       
   490             //if there are no buddies left in this service delete this whole service
       
   491             if(buddyCount==0)
       
   492                 {
       
   493                 delete iServer.iPresenceCache[i];
       
   494                 iServer.iPresenceCache.Remove(i);
       
   495                 }
       
   496                 
       
   497             //inform all subscribers
       
   498             for(TInt j=0;j<buddyCount;j++)
       
   499                 {
       
   500                 NotifyAllSubscribersL((iServer.iPresenceCache[i])->GetObjectCollection()[j]);
       
   501                 }
       
   502             err = KErrNone;
       
   503             break;
       
   504             }
       
   505         }
       
   506     CleanupStack::PopAndDestroy(serviceName);
       
   507     aMessage.Complete(err);
       
   508     TRACE( _L("CPresenceCacheSession::HandleDeleteService - end"));
       
   509     }
       
   510     
       
   511 // ---------------------------------------------------------------------------
       
   512 // CPresenceCacheSession::HandleDeletePresence()
       
   513 // ---------------------------------------------------------------------------
       
   514 //
       
   515 void CPresenceCacheSession::HandleDeletePresence(const RMessage2& aMessage )
       
   516     {
       
   517     const TInt pckSize = aMessage.GetDesLengthL(0);
       
   518     HBufC8* idPack = HBufC8::NewLC( pckSize );
       
   519     TPtr8 paramPckPtr = idPack->Des();
       
   520     aMessage.ReadL( 0, paramPckPtr );
       
   521     TInt err(KErrNotFound);
       
   522     TInt buddyCount(NULL);
       
   523     
       
   524     CXIMPIdentityImp* identity = CXIMPIdentityImp::NewLC();
       
   525     
       
   526     TXIMPObjectPacker< CXIMPIdentityImp >::UnPackL(*identity, *idPack);
       
   527     
       
   528     TPtrC serviceName = GetServiceName(identity->Identity());
       
   529     
       
   530     TInt serviceCount = iServer.iPresenceCache.Count();
       
   531     
       
   532     CPresenceCacheBuddyStore* buddyStore(NULL);
       
   533     for(TInt i=0; i<serviceCount; i++)
       
   534         {
       
   535         if(((iServer.iPresenceCache[i])->ServiceName()) == serviceName)
       
   536             {
       
   537             buddyStore = (iServer.iPresenceCache[i])->FindAndRemove(*identity,err);
       
   538             
       
   539             buddyCount = (iServer.iPresenceCache[i])->GetObjectCollection().Count();
       
   540             TRACE_1( _L("_______buddyCountAfter[%d]"), buddyCount);
       
   541             
       
   542             //if there are no buddies left in this service delete this whole service
       
   543             if(buddyCount==0)
       
   544                 {
       
   545                 delete iServer.iPresenceCache[i];
       
   546                 iServer.iPresenceCache.Remove(i);
       
   547                 }
       
   548             break;
       
   549             }
       
   550         }
       
   551     
       
   552     CleanupStack::PopAndDestroy(identity);
       
   553     CleanupStack::PopAndDestroy(idPack);
       
   554     aMessage.Complete(err);
       
   555     
       
   556     if(buddyStore) // if subscribers
       
   557         NotifyAllSubscribersL(buddyStore);
       
   558     }
       
   559     
       
   560 
       
   561 // ---------------------------------------------------------------------------
       
   562 // CPresenceCacheSession::HandleGetServiceCount()
       
   563 // ---------------------------------------------------------------------------
       
   564 //
       
   565 void CPresenceCacheSession::HandleGetServiceCount(const RMessage2& aMessage )
       
   566     {
       
   567     // iLastServiceCount is also used for mem aloc by client before calling
       
   568     // GetAllServices
       
   569     TInt serviceCount = iServer.iPresenceCache.Count();
       
   570     iLastServiceCount = 0;
       
   571     TRACE_1( _L("CPresenceCacheSession::HandleGetServiceCount realcount[%d]- end"),serviceCount);
       
   572     for(TInt i=0;i<serviceCount;i++)
       
   573         {
       
   574         if((iServer.iPresenceCache[i])->PresenceCount())
       
   575             iLastServiceCount++;
       
   576         }
       
   577 
       
   578     TPckgBuf<TInt> p(iLastServiceCount);
       
   579     aMessage.WriteL(0,p);
       
   580     aMessage.Complete(KErrNone);
       
   581     }
       
   582 
       
   583 // ---------------------------------------------------------------------------
       
   584 // CPresenceCacheSession::HandleGetAllServicesL()
       
   585 // ---------------------------------------------------------------------------
       
   586 //
       
   587 /*
       
   588 void CPresenceCacheSession::HandleGetAllServicesL(const RMessage2& aMessage )   
       
   589     {
       
   590     TInt serviceCount = iServer.iPresenceCache.Count();
       
   591     TInt size(NULL);
       
   592     
       
   593     if(serviceCount)
       
   594     	{
       
   595         CArrayFixFlat<TUid>* services = new (ELeave) CArrayFixFlat<TUid>(1);
       
   596         CleanupStack::PushL(services);
       
   597     	
       
   598     	for(TInt i=0; i<serviceCount; i++)
       
   599 	        {
       
   600 	        if((iServer.iPresenceCache[i])->PresenceCount())
       
   601 	            services->AppendL((iServer.iPresenceCache[i])->Service());
       
   602 	        }
       
   603 	    
       
   604 	    if (iDataPack)
       
   605 	        {
       
   606 	        delete iDataPack; iDataPack = NULL;
       
   607 	        }
       
   608 	    iDataPack = PackServicesL(services);
       
   609 	    CleanupStack::PopAndDestroy(services);
       
   610 	    size = (iDataPack->Des()).Size();
       
   611     	}
       
   612     aMessage.Complete(size);
       
   613     }*/
       
   614     
       
   615 // ---------------------------------------------------------------------------
       
   616 // CPresenceCacheSession::HandleWritePresenceInfoAsyncL()
       
   617 // ---------------------------------------------------------------------------
       
   618 //
       
   619 void CPresenceCacheSession::HandleWritePresenceInfoAsyncL(const RMessage2& aMessage )
       
   620     {
       
   621     if(iActiveHelper->IsActive())
       
   622         {
       
   623         aMessage.Complete(KErrServerBusy);
       
   624         return;        
       
   625         }
       
   626     iMessage = aMessage;
       
   627     
       
   628     const TInt pckSize = aMessage.GetDesLengthL(0);
       
   629     if(iDataPackAsync)
       
   630         delete iDataPackAsync; iDataPackAsync = NULL;
       
   631     iDataPackAsync = HBufC8::NewL( pckSize );
       
   632     TPtr8 paramPckPtr = iDataPackAsync->Des();
       
   633     aMessage.ReadL( 0, paramPckPtr );
       
   634     
       
   635     if(iBuddypresInfoList)
       
   636         delete iBuddypresInfoList; iBuddypresInfoList = NULL;
       
   637     iBuddypresInfoList = CPresenceBuddyInfoListImp::NewLC(KNullDesC);
       
   638     CleanupStack::Pop(iBuddypresInfoList);
       
   639     
       
   640     TXIMPObjectPacker< CPresenceBuddyInfoListImp >::UnPackL(*iBuddypresInfoList, *iDataPackAsync);
       
   641     
       
   642     iAsyncReq = NRequest::EWritePresenceInfoAsync;
       
   643     iActiveHelper->Start();
       
   644     return;
       
   645     }
       
   646 
       
   647 // ---------------------------------------------------------------------------
       
   648 // CPresenceCacheSession::WritePresenceInfoAsyncL()
       
   649 // ---------------------------------------------------------------------------
       
   650 //
       
   651 void CPresenceCacheSession::WritePresenceInfoAsyncL()
       
   652     {
       
   653     TInt err(KErrNone);
       
   654     TInt thisTimeWrites(0);
       
   655     if(!iBuddypresInfoList) // return if this function is wrongly called
       
   656         return;
       
   657     
       
   658     // This routine reads data from iBuddypresInfoList and tries to write 
       
   659     // data to cache on each runl cycle. It writes either KMaxWriteEachCycle 
       
   660     // times, or upto data is finnished. After data is written KMaxWriteEachCycle
       
   661     // times and still more data is left, it makes this class active so that this
       
   662     // function is again called on next RunL.
       
   663     while(iBuddypresInfoList->Count())
       
   664         {
       
   665         err = TryWriteBuddyToCacheL((iBuddypresInfoList->GetObjectCollection())[0]);
       
   666         if(err==KErrNone) // if no erroe then pop the object from list
       
   667             (iBuddypresInfoList->GetObjectCollection()).Remove(0);
       
   668         else
       
   669             break; // break in case of error
       
   670         thisTimeWrites++;    
       
   671         if(thisTimeWrites == NConstants::KMaxWriteEachCycle) // this times writes are over
       
   672             {
       
   673             if(iActiveHelper->IsActive())// unusual condition
       
   674                 {
       
   675                 err = KErrGeneral;
       
   676                 break;
       
   677                 }
       
   678             iActiveHelper->Start();
       
   679             return; // set for next time writing.
       
   680             }
       
   681        }
       
   682     // if we able to come out of while loop either writing is complete or
       
   683     // there was some error while writing, in both cases finnish writing
       
   684     iAsyncReq = NRequest::ENoRequestMade;
       
   685     iMessage.Complete(err);
       
   686     return;
       
   687     }
       
   688 
       
   689 // ---------------------------------------------------------------------------
       
   690 // CPresenceCacheSession::HandleSubscribeBuddyPresenceChange()
       
   691 // ---------------------------------------------------------------------------
       
   692 //
       
   693 void CPresenceCacheSession::HandleSubscribeBuddyPresenceChange
       
   694                                                 (const RMessage2& aMessage )
       
   695     {
       
   696     TRACE( _L("CPresenceCacheSession::HandleSubscribeBuddyPresenceChange - begin"));
       
   697 
       
   698     // Extract info from message
       
   699     const TInt pckSize = aMessage.GetDesLengthL(1);
       
   700     HBufC8* idPack = HBufC8::NewLC( pckSize );
       
   701     TPtr8 paramPckPtr = idPack->Des();
       
   702     aMessage.ReadL( 1, paramPckPtr );
       
   703     
       
   704     CXIMPIdentityImp* identity = CXIMPIdentityImp::NewLC();
       
   705     
       
   706     TXIMPObjectPacker< CXIMPIdentityImp >::UnPackL(*identity, *idPack);
       
   707     
       
   708     TPtrC serviceName = GetServiceName(identity->Identity());
       
   709     
       
   710     CPresenceCacheBuddyStore* buddyStore(NULL);
       
   711     CPresenceCacheServiceStore* serviceStore(NULL);
       
   712     
       
   713     TInt err(KErrGeneral); // there must not be error in this handler
       
   714                            // but just for debug purpose
       
   715     
       
   716     
       
   717     // now processing
       
   718     TInt serviceCount = iServer.iPresenceCache.Count();
       
   719 
       
   720     
       
   721     TRACE_1( _L("_______serviceCount[%d]"), serviceCount);
       
   722     
       
   723     for(TInt i=0; i<serviceCount; i++)
       
   724         {
       
   725         if (serviceName == (iServer.iPresenceCache[i])->ServiceName())
       
   726             {
       
   727             serviceStore = iServer.iPresenceCache[i];
       
   728             buddyStore = (iServer.iPresenceCache[i])->FindAndGet(*identity);
       
   729             break;
       
   730             }
       
   731         }
       
   732         
       
   733     if(!buddyStore) // if buddy was not found, create and add it
       
   734         {
       
   735         if(!serviceStore) //if service was also not found, create it
       
   736             {
       
   737             serviceStore = CPresenceCacheServiceStore::NewLC();
       
   738             serviceStore->SetServiceNameL(serviceName);
       
   739             (iServer.iPresenceCache).Append(serviceStore); //ownership transferred
       
   740             CleanupStack::Pop(serviceStore);  
       
   741             }
       
   742         buddyStore = CPresenceCacheBuddyStore::NewLC(serviceStore,identity);
       
   743         err = serviceStore->AddBlind(buddyStore); //ownership transferred
       
   744         CleanupStack::Pop(buddyStore);
       
   745         CleanupStack::Pop(identity);
       
   746         } 
       
   747     else
       
   748         CleanupStack::PopAndDestroy(identity); // since in this case ownership wasnt tranferred
       
   749     
       
   750     CleanupStack::PopAndDestroy(idPack);
       
   751     
       
   752     TRACE_1( _L("buddyStore[%d]"), buddyStore);
       
   753     if(buddyStore) // must be there, just for double check
       
   754         {
       
   755         buddyStore->AddSubscribedSession(this);
       
   756         if(iSubscribedStores.Find(buddyStore) < 0) //if session not already there
       
   757             iSubscribedStores.Append(buddyStore);
       
   758         err = KErrNone;
       
   759         }
       
   760     
       
   761     aMessage.Complete(err); // there shouldnt be any errors
       
   762     TRACE( _L("CPresenceCacheSession::HandleSubscribeBuddyPresenceChange - end"));
       
   763     }
       
   764 
       
   765 // ---------------------------------------------------------------------------
       
   766 // CPresenceCacheSession::HandleUnSubscribeBuddyPresenceChange()
       
   767 // ---------------------------------------------------------------------------
       
   768 //
       
   769 void CPresenceCacheSession::HandleUnSubscribeBuddyPresenceChange
       
   770                                                 (const RMessage2& aMessage )
       
   771     {
       
   772     TRACE( _L("CPresenceCacheSession::HandleUnSubscribeBuddyPresenceChange - begin"));
       
   773 
       
   774     // Extract info from message
       
   775     const TInt pckSize = aMessage.GetDesLengthL(1);
       
   776     HBufC8* idPack = HBufC8::NewLC( pckSize );
       
   777     TPtr8 paramPckPtr = idPack->Des();
       
   778     aMessage.ReadL( 1, paramPckPtr );
       
   779     TInt err(KErrNotFound);
       
   780     
       
   781     CXIMPIdentityImp* identity = CXIMPIdentityImp::NewLC();
       
   782     
       
   783     TXIMPObjectPacker< CXIMPIdentityImp >::UnPackL(*identity, *idPack);
       
   784     
       
   785     TPtrC serviceName = GetServiceName(identity->Identity());
       
   786     
       
   787     
       
   788     // now processing
       
   789     TInt serviceCount = iServer.iPresenceCache.Count();
       
   790     CPresenceCacheBuddyStore* buddyStore(NULL);
       
   791     
       
   792     TRACE_1( _L("_______serviceCount[%d]"), serviceCount);
       
   793     
       
   794     for(TInt i=0; i<serviceCount; i++)
       
   795         {
       
   796         if (serviceName == (iServer.iPresenceCache[i])->ServiceName())
       
   797             {
       
   798             buddyStore = (iServer.iPresenceCache[i])->FindAndGet(*identity);
       
   799             break;
       
   800             }
       
   801         }
       
   802     
       
   803     CleanupStack::PopAndDestroy(identity);
       
   804     CleanupStack::PopAndDestroy(idPack);
       
   805     
       
   806     TRACE_1( _L("buddyStore[%d]"), buddyStore);
       
   807     if(buddyStore) // if found
       
   808         {
       
   809         buddyStore->RemoveSubscribedSession(this);
       
   810         iSubscribedStores.Remove(iSubscribedStores.Find(buddyStore));
       
   811         err = KErrNone;
       
   812         }
       
   813     
       
   814     aMessage.Complete(err);
       
   815     TRACE_1( _L("CPresenceCacheSession::HandleUnSubscribeBuddyPresenceChange (%d) - end"),err);
       
   816     }
       
   817 
       
   818 // ---------------------------------------------------------------------------
       
   819 // CPresenceCacheSession::HandleGetLastNotifiedtPacketL()
       
   820 // ---------------------------------------------------------------------------
       
   821 //
       
   822 void CPresenceCacheSession::HandleGetLastNotifiedtPacketL(const RMessage2& aMessage )
       
   823     {
       
   824     TRACE( _L("CPresenceCacheSession::HandleGetLastNotifiedtPacketL() - begin"));
       
   825     if(iDataPackNotifier)
       
   826         aMessage.WriteL(0, *(iDataPackNotifier));
       
   827     aMessage.Complete(KErrNone);
       
   828     }
       
   829 
       
   830 // ---------------------------------------------------------------------------
       
   831 // CPresenceCacheSession::HandleWaitingForNotification()
       
   832 // ---------------------------------------------------------------------------
       
   833 //
       
   834 void CPresenceCacheSession::HandleWaitingForNotification(const RMessage2& aMessage )
       
   835     {
       
   836     iMessageForNoti = aMessage;
       
   837     }
       
   838 
       
   839 // ---------------------------------------------------------------------------
       
   840 // CPresenceCacheSession::HandleCancelWaitingForNotification()
       
   841 // ---------------------------------------------------------------------------
       
   842 //
       
   843 void CPresenceCacheSession::HandleCancelWaitingForNotification
       
   844                                                     (const RMessage2& aMessage )
       
   845     {
       
   846     aMessage.Complete(KErrNone);
       
   847     if(!iMessageForNoti.IsNull())
       
   848         iMessageForNoti.Complete(KErrCancel);
       
   849     }
       
   850 
       
   851 // ---------------------------------------------------------------------------
       
   852 // CPresenceCacheSession::NotifyPresenceChangeL()
       
   853 // ---------------------------------------------------------------------------
       
   854 //
       
   855 void CPresenceCacheSession::NotifyPresenceChangeL(
       
   856                                const CPresenceBuddyInfoImp* aPresenceBuddyInfo)
       
   857     {
       
   858     TRACE( _L("CPresenceCacheSession::NotifyPresenceChangeL() - begin"));
       
   859     TInt sizeOfReturnPacket(NULL);
       
   860     if(aPresenceBuddyInfo && (!iMessageForNoti.IsNull())) // if pointers are valid
       
   861         {
       
   862         if(iDataPackNotifier)
       
   863             delete iDataPackNotifier; iDataPackNotifier=NULL;
       
   864         iDataPackNotifier = PackBuddyPresenceInfoLC(aPresenceBuddyInfo);
       
   865         CleanupStack::Pop(iDataPackNotifier);
       
   866         sizeOfReturnPacket = (iDataPackNotifier->Des()).Size();
       
   867         TPckgBuf<TInt> p(sizeOfReturnPacket);
       
   868         iMessageForNoti.WriteL(0,p);            
       
   869         iMessageForNoti.Complete(KErrNone);
       
   870         TRACE( _L("_______message was valid"));
       
   871         }
       
   872     TRACE( _L("CPresenceCacheSession::NotifyPresenceChangeL - end"));
       
   873     }
       
   874     
       
   875 // ---------------------------------------------------------------------------
       
   876 // CPresenceCacheSession::NotifyAllSubscribersL()
       
   877 // ---------------------------------------------------------------------------
       
   878 //
       
   879 void CPresenceCacheSession::NotifyAllSubscribersL
       
   880                                         (CPresenceCacheBuddyStore* aBuddyStore)
       
   881     {
       
   882     TRACE( _L("CPresenceCacheSession::NotifyAllSubscribers() - begin"));
       
   883     if(!aBuddyStore)
       
   884         return;
       
   885     TInt subsCount = aBuddyStore->GetSubscribedSessions().Count();
       
   886     TRACE_1( _L("_______total subscribed sessions[%d]"), subsCount);
       
   887     TBool ownBuddyInfo(EFalse);
       
   888     const CPresenceBuddyInfoImp* buddyInfo;
       
   889     
       
   890     if(subsCount) // start processing if there are subscribers
       
   891         {
       
   892         //if presence has been deleted we need to identity and service id in the packed
       
   893         //which will be sent to clients
       
   894         if(!(aBuddyStore->PresenceBuddyInfo()))
       
   895             {
       
   896             CPresenceBuddyInfoImp* buddyInfoTemp = CPresenceBuddyInfoImp::NewLC();
       
   897             MXIMPIdentity* id = CXIMPIdentityImp::NewLC(aBuddyStore->BuddyId()->Identity());
       
   898             buddyInfoTemp->SetBuddyId(id);
       
   899             CleanupStack::Pop(1); //id
       
   900             ownBuddyInfo = ETrue;
       
   901             buddyInfo = buddyInfoTemp;
       
   902             }
       
   903         else
       
   904             {
       
   905             buddyInfo = 
       
   906                 TXIMPGetImpClassOrPanic< const CPresenceBuddyInfoImp >::From( *(aBuddyStore->PresenceBuddyInfo()) );    
       
   907             }
       
   908             
       
   909         CPresenceCacheSession* session(NULL);
       
   910         for(TInt i=0;i<subsCount;i++)
       
   911             {
       
   912             session = aBuddyStore->GetSubscribedSessions()[i];
       
   913             if(iServer.SessionExists(session))
       
   914                 {
       
   915                 TRACE_1( _L("_______valid session notified[%d]"), session);
       
   916                 session->NotifyPresenceChangeL(buddyInfo);
       
   917                 }
       
   918             else
       
   919                 {
       
   920                 aBuddyStore->RemoveSubscribedSession(session);
       
   921                 subsCount--;//substract this removed session from total count
       
   922                 i--;
       
   923                 TRACE_1( _L("_______invalid session removed[%d]"), session);
       
   924                 }    
       
   925             }
       
   926         }
       
   927         
       
   928     if(ownBuddyInfo)
       
   929         CleanupStack::PopAndDestroy(1); //buddyInfoTemp
       
   930     TRACE( _L("CPresenceCacheSession::NotifyAllSubscribers() - end"));
       
   931     }
       
   932 
       
   933 // ---------------------------------------------------------------------------
       
   934 // CPresenceCacheSession::GetServiceName()
       
   935 // ---------------------------------------------------------------------------
       
   936 //
       
   937 TPtrC CPresenceCacheSession::GetServiceName(const TDesC& aXspId)
       
   938     {
       
   939     _LIT(KColon, ":");
       
   940     TInt pos = aXspId.Find(KColon);
       
   941     TPtrC serviceName;
       
   942     if(pos>0) // if colon found and there is something before colon, i.e. xsp id
       
   943         {
       
   944         serviceName.Set(aXspId.Left(pos));
       
   945         }
       
   946     else
       
   947         serviceName.Set(TPtrC());
       
   948     return serviceName;
       
   949     }
       
   950         
       
   951 // ---------------------------------------------------------------------------
       
   952 // CPresenceCacheSession::Cancel()
       
   953 // ---------------------------------------------------------------------------
       
   954 //
       
   955 void CPresenceCacheSession::Cancel()
       
   956     {
       
   957     if (iAsyncReq != NRequest::ENoRequestMade)
       
   958         {
       
   959         iMessage.Complete(KErrCancel);
       
   960         iAsyncReq = NRequest::ENoRequestMade;
       
   961         }
       
   962     return;   
       
   963     }
       
   964     
       
   965 // ---------------------------------------------------------------------------
       
   966 // CPresenceCacheSession::PackPresenceDocLC()
       
   967 // ---------------------------------------------------------------------------
       
   968 //
       
   969 HBufC8* CPresenceCacheSession::PackPresenceInfoLC(const MPresenceInfo& aPresInfo)
       
   970     {
       
   971     const CPresenceInfoImp* tmp =
       
   972         TXIMPGetImpClassOrPanic< const CPresenceInfoImp >::From(aPresInfo);
       
   973     HBufC8* pack = TXIMPObjectPacker< CPresenceInfoImp>::PackL( *tmp);
       
   974     CleanupStack::PushL( pack );
       
   975 
       
   976     return pack;
       
   977     }
       
   978     
       
   979 // ---------------------------------------------------------------------------
       
   980 // CPresenceCacheSession::PackBuddyPresenceInfoLC()
       
   981 // ---------------------------------------------------------------------------
       
   982 //
       
   983 HBufC8* CPresenceCacheSession::PackBuddyPresenceInfoLC(
       
   984                                     const CPresenceBuddyInfoImp* aBuddyPresInfo)
       
   985     {
       
   986     HBufC8* pack(NULL);
       
   987     if(aBuddyPresInfo)
       
   988         {
       
   989         //const CPresenceBuddyInfoImp* pifImp = 
       
   990         //    TXIMPGetImpClassOrPanic< const CPresenceBuddyInfoImp >::From( *aBuddyPresInfo );    
       
   991         
       
   992         pack = TXIMPObjectPacker< const CPresenceBuddyInfoImp>::PackL( *aBuddyPresInfo );
       
   993         CleanupStack::PushL( pack );
       
   994         }
       
   995 
       
   996     return pack;
       
   997     }
       
   998     
       
   999 
       
  1000 // ---------------------------------------------------------------------------
       
  1001 // CPresenceCacheSession::PackServicesL()
       
  1002 // ---------------------------------------------------------------------------
       
  1003 //
       
  1004 /*
       
  1005 HBufC8* CPresenceCacheSession::PackServicesL(CArrayFixFlat<TUid>* aArray )
       
  1006 	{
       
  1007     CBufFlat* packBuf = CBufFlat::NewL( NConstants::KGranularity );
       
  1008     CleanupStack::PushL( packBuf );
       
  1009 
       
  1010     RBufWriteStream ws;
       
  1011     ws.Open( *packBuf ); // CSI: 65 #
       
  1012     CleanupClosePushL( ws );
       
  1013 
       
  1014     // Get count of objects
       
  1015     TInt objCount = aArray->Count();
       
  1016     // write the count
       
  1017     ws.WriteInt32L( objCount );
       
  1018     // objects
       
  1019     for ( TInt count(0); count < objCount; count++ )
       
  1020         {
       
  1021         ws<<(aArray->At(count));
       
  1022         }
       
  1023 
       
  1024     ws.CommitL();
       
  1025     CleanupStack::PopAndDestroy(); //ws
       
  1026 
       
  1027     HBufC8* packBufDesc = packBuf->Ptr(0).AllocL();
       
  1028     CleanupStack::PopAndDestroy( packBuf );
       
  1029 
       
  1030     return packBufDesc;
       
  1031 	}*/
       
  1032 
       
  1033 // ---------------------------------------------------------------------------
       
  1034 // CPresenceCacheSession::CPresenceCacheSession()
       
  1035 // ---------------------------------------------------------------------------
       
  1036 //
       
  1037 CPresenceCacheSession::CPresenceCacheSession( CPresenceCacheServer& aServer )
       
  1038                                                         : iServer( aServer ),
       
  1039                                                         iLastServiceCount(0)
       
  1040     {
       
  1041     TRACE( _L("CPresenceCacheSession::CPresenceCacheSession()"));
       
  1042     }
       
  1043 
       
  1044 // ---------------------------------------------------------------------------
       
  1045 // CPresenceCacheSession::ConstructL()
       
  1046 // ---------------------------------------------------------------------------
       
  1047 //
       
  1048 void CPresenceCacheSession::ConstructL()
       
  1049     {
       
  1050     iActiveHelper = CCacheSessionActiveHelper::NewL(this);    
       
  1051     iServer.IncrementSessions();
       
  1052     return;
       
  1053     }
       
  1054     
       
  1055 // ---------------------------------------------------------------------------
       
  1056 // CPresenceCacheSession::RemoveMySubscriptions()
       
  1057 // ---------------------------------------------------------------------------
       
  1058 //
       
  1059 void CPresenceCacheSession::RemoveMySubscriptions()
       
  1060     {
       
  1061     TInt count = iSubscribedStores.Count();
       
  1062     for(TInt i=0;i<count;i++)
       
  1063         {
       
  1064         // it can be assumed that store is alive, since it won't be deleted
       
  1065         // until there is any session subscribed to it
       
  1066         (iSubscribedStores[i])->RemoveSubscribedSession(this);
       
  1067         }
       
  1068     }
       
  1069     
       
  1070     
       
  1071 // ---------------------------------------------------------------------------
       
  1072 // CCacheSessionActiveHelper::NewL()
       
  1073 // ---------------------------------------------------------------------------
       
  1074 //
       
  1075 CCacheSessionActiveHelper* CCacheSessionActiveHelper::NewL
       
  1076                                             (CPresenceCacheSession* aSession )
       
  1077     {
       
  1078     CCacheSessionActiveHelper* self = new(ELeave) CCacheSessionActiveHelper(aSession);
       
  1079     CleanupStack::PushL( self );
       
  1080     self->ConstructL( );
       
  1081     CleanupStack::Pop( self );
       
  1082     return self;
       
  1083     }
       
  1084 
       
  1085 // ---------------------------------------------------------------------------
       
  1086 // CCacheSessionActiveHelper::~CCacheSessionActiveHelper()
       
  1087 // ---------------------------------------------------------------------------
       
  1088 //
       
  1089 CCacheSessionActiveHelper::~CCacheSessionActiveHelper( )
       
  1090     {
       
  1091     Cancel();   
       
  1092     }
       
  1093 
       
  1094 // ---------------------------------------------------------------------------
       
  1095 // CCacheSessionActiveHelper::Start()
       
  1096 // ---------------------------------------------------------------------------
       
  1097 //
       
  1098 void CCacheSessionActiveHelper::Start()
       
  1099     {
       
  1100     TRequestStatus* status = &iStatus;
       
  1101     User::RequestComplete(status,KErrNone);
       
  1102     SetActive();
       
  1103     }
       
  1104 
       
  1105 // ---------------------------------------------------------------------------
       
  1106 // CCacheSessionActiveHelper::CCacheSessionActiveHelper()
       
  1107 // ---------------------------------------------------------------------------
       
  1108 //
       
  1109 CCacheSessionActiveHelper::CCacheSessionActiveHelper(CPresenceCacheSession* aSession ) 
       
  1110                                                     : CActive(EPriorityStandard)
       
  1111     {
       
  1112     iCacheSession = aSession;
       
  1113     }
       
  1114 
       
  1115 // ---------------------------------------------------------------------------
       
  1116 // CCacheSessionActiveHelper::RunL()
       
  1117 // ---------------------------------------------------------------------------
       
  1118 //
       
  1119 void CCacheSessionActiveHelper::RunL()
       
  1120     {
       
  1121     switch (iCacheSession->iAsyncReq)
       
  1122         {
       
  1123         case NRequest::EWritePresenceInfoAsync:
       
  1124             iCacheSession->WritePresenceInfoAsyncL();
       
  1125             break;
       
  1126 
       
  1127         case NRequest::EPrepReadAllBuddiesPresenceInService:
       
  1128             iCacheSession->ReadAllBuddiesPresenceInServiceL();
       
  1129             break;
       
  1130 
       
  1131         case NRequest::ENoRequestMade:
       
  1132         default:
       
  1133             break;
       
  1134         }    
       
  1135     }
       
  1136 
       
  1137 // ---------------------------------------------------------------------------
       
  1138 // CCacheSessionActiveHelper::DoCancel()
       
  1139 // ---------------------------------------------------------------------------
       
  1140 //
       
  1141 void CCacheSessionActiveHelper::DoCancel()
       
  1142     {
       
  1143     iCacheSession->Cancel();
       
  1144     }
       
  1145 
       
  1146 // ---------------------------------------------------------------------------
       
  1147 // CCacheSessionActiveHelper::ConstructL()
       
  1148 // ---------------------------------------------------------------------------
       
  1149 //
       
  1150 void CCacheSessionActiveHelper::ConstructL()
       
  1151     {
       
  1152 	CActiveScheduler::Add(this);
       
  1153     }
       
  1154 
       
  1155 
       
  1156 // End of File