presencefwsimpleadpt/src/simplepluginentitywatcher.cpp
changeset 0 c8caa15ef882
equal deleted inserted replaced
-1:000000000000 0:c8caa15ef882
       
     1 /*
       
     2 * Copyright (c) 2006 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:    SIMPLE Protocol implementation for Presence Framework
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 #include <e32std.h>
       
    22 #include <utf.h>
       
    23 
       
    24 #include <ximpdatasubscriptionstate.h>
       
    25 #include <personpresenceinfo.h>
       
    26 #include <protocolpresencewatchingdatahost.h>
       
    27 #include <presenceinfo.h>
       
    28 #include <presenceinfofieldcollection.h>
       
    29 #include <presenceinfofield.h>
       
    30 #include <presenceinfofieldvaluetext.h>
       
    31 #include <presenceinfofieldvalueenum.h>
       
    32 #include <presenceobjectfactory.h>
       
    33 #include <presentitygroupmemberinfo.h>
       
    34 #include <protocolpresencedatahost.h>
       
    35 #include <protocolpresentitygroupsdatahost.h>
       
    36 #include <ximpobjectcollection.h>
       
    37 #include <ximpobjectfactory.h>
       
    38 #include <ximpprotocolconnectionhost.h>
       
    39 #include <ximpstatus.h>
       
    40 #include <ximpidentity.h>
       
    41 
       
    42 #include <simplefactory.h>
       
    43 
       
    44 #include <msimplewinfo.h>
       
    45 #include <msimplewatcher.h>
       
    46 #include <msimpledocument.h>
       
    47 #include <msimpleelement.h>
       
    48 
       
    49 #include "simpleplugincommon.h"
       
    50 #include "simplepluginentitywatcher.h"
       
    51 #include "simpleplugindebugutils.h"
       
    52 #include "simpleplugindata.h"
       
    53 #include "simpleutils.h"
       
    54 #include "simplepluginwatcher.h"
       
    55 
       
    56 
       
    57 // ======== MEMBER FUNCTIONS ========
       
    58 
       
    59 // ---------------------------------------------------------------------------
       
    60 // CSimplePluginEntityWatcher::CSimplePluginEntityWatcher
       
    61 // ---------------------------------------------------------------------------
       
    62 //
       
    63 CSimplePluginEntityWatcher::CSimplePluginEntityWatcher(
       
    64     MSimplePluginConnectionObs& aObs,
       
    65     MSimpleConnection& aConn,
       
    66     CSimplePluginWatcher& aWatcher
       
    67      )
       
    68   : iPluginWatcher(aWatcher), iConnObs(aObs), iConnection(aConn),
       
    69   iOperation(EPluginIdle)
       
    70     {
       
    71     }
       
    72 
       
    73 // ---------------------------------------------------------------------------
       
    74 // CSimplePluginEntityWatcher::ConstructL
       
    75 // ---------------------------------------------------------------------------
       
    76 //
       
    77 void CSimplePluginEntityWatcher::ConstructL( )
       
    78     {
       
    79     iWatcher = TSimpleFactory::NewWatcherL( iConnection, *this );
       
    80     }
       
    81 
       
    82 // ---------------------------------------------------------------------------
       
    83 // CSimplePluginEntityWatcher::NewL
       
    84 // ---------------------------------------------------------------------------
       
    85 //
       
    86 CSimplePluginEntityWatcher* CSimplePluginEntityWatcher::NewL(
       
    87     MSimplePluginConnectionObs& aObs,
       
    88     MSimpleConnection& aConn,
       
    89     CSimplePluginWatcher& aWatcher )
       
    90     {
       
    91 #ifdef _DEBUG
       
    92     PluginLogger::Log(_L("PluginEntityWatcher: NewL"));
       
    93 #endif
       
    94     CSimplePluginEntityWatcher* self =
       
    95         new( ELeave ) CSimplePluginEntityWatcher( aObs, aConn, aWatcher );
       
    96     CleanupStack::PushL( self );
       
    97     self->ConstructL(  );
       
    98     CleanupStack::Pop( self );
       
    99     return self;
       
   100     }
       
   101 
       
   102 // ---------------------------------------------------------------------------
       
   103 // CSimplePluginEntityWatcher::~CSimplePluginEntityWatcher
       
   104 // ---------------------------------------------------------------------------
       
   105 //
       
   106 CSimplePluginEntityWatcher::~CSimplePluginEntityWatcher()
       
   107     {
       
   108 #ifdef _DEBUG
       
   109     PluginLogger::Log(_L("PluginEntityWatcher: DESTRUCTOR"));
       
   110 #endif
       
   111     if ( iWatcher )
       
   112         {
       
   113         iWatcher->Close();
       
   114         }
       
   115     delete iEntityId;
       
   116     delete iSimpleEntityId;
       
   117     }
       
   118 
       
   119 // ---------------------------------------------------------------------------
       
   120 // CSimplePluginEntityWatcher::StartSubscribeL
       
   121 // ---------------------------------------------------------------------------
       
   122 //
       
   123 void CSimplePluginEntityWatcher::StartSubscribeL(
       
   124     const TDesC8& aPresentityId )
       
   125     {
       
   126     delete iEntityId;
       
   127     iEntityId = NULL;
       
   128 
       
   129     iListSubsActive = EFalse;
       
   130 
       
   131 #ifdef _DEBUG
       
   132     PluginLogger::Log(_L("PluginEntityWatcher: -> SubscribeL"));
       
   133 #endif
       
   134     iSimpleId = iWatcher->SubscribeL( aPresentityId, NULL, ETrue, EFalse );
       
   135 
       
   136     // Save entity id after successful call
       
   137     iEntityId = aPresentityId.AllocL();
       
   138 
       
   139     iOperation = EPluginStart;
       
   140     }
       
   141 
       
   142 // ---------------------------------------------------------------------------
       
   143 // CSimplePluginEntityWatcher::StartSubscribeListL
       
   144 // ---------------------------------------------------------------------------
       
   145 //
       
   146 void CSimplePluginEntityWatcher::StartSubscribeListL(
       
   147     const TDesC8& aPresentityId )
       
   148     {
       
   149     delete iEntityId;
       
   150     iEntityId = NULL;
       
   151 
       
   152     iListSubsActive = ETrue;
       
   153 
       
   154 #ifdef _DEBUG
       
   155     PluginLogger::Log(_L("PluginEntityWatcher: -> SubscribeListL"));
       
   156 #endif
       
   157     iSimpleId = iWatcher->SubscribeListL( aPresentityId, NULL, ETrue, EFalse );
       
   158 
       
   159     // Save entity id after successful call
       
   160     iEntityId = aPresentityId.AllocL();
       
   161 
       
   162     iOperation = EPluginStart;
       
   163     }
       
   164 
       
   165 // ---------------------------------------------------------------------------
       
   166 // CSimplePluginEntityWatcher::SetSimpleNameL
       
   167 // ---------------------------------------------------------------------------
       
   168 //
       
   169 void CSimplePluginEntityWatcher::SetSimpleNameL(
       
   170     const TDesC8& aPresentityId )
       
   171     {
       
   172     delete iSimpleEntityId;
       
   173     iSimpleEntityId = NULL;
       
   174 
       
   175 
       
   176     // Save entity id after successful call
       
   177     iSimpleEntityId = aPresentityId.AllocL();
       
   178 
       
   179     }
       
   180 
       
   181 // ---------------------------------------------------------------------------
       
   182 // CSimplePluginEntityWatcher::StopSubscribeL
       
   183 // ---------------------------------------------------------------------------
       
   184 //
       
   185 void CSimplePluginEntityWatcher::StopSubscribeL(
       
   186      )
       
   187     {
       
   188 #ifdef _DEBUG
       
   189     PluginLogger::Log(_L("PluginEntityWatcher: -> UnsubscribeL"));
       
   190 #endif
       
   191     iSimpleId = iWatcher->UnsubscribeL( );
       
   192     iOperation = EPluginStop;
       
   193     }
       
   194 
       
   195 // ---------------------------------------------------------------------------
       
   196 // CSimplePluginEntityWatcher::WatcherReqCompleteL
       
   197 // ---------------------------------------------------------------------------
       
   198 //
       
   199 void CSimplePluginEntityWatcher::WatcherReqCompleteL(
       
   200     TInt /*aOpId*/, TInt aStatus )
       
   201     {
       
   202 #ifdef _DEBUG
       
   203     PluginLogger::Log(_L("PluginEntityWatcher: WatcherReqCompleteL"));
       
   204 #endif
       
   205     TPluginEntityWatcherOperation orig = iOperation;
       
   206     iOperation = EPluginIdle;
       
   207 
       
   208     CompleteClientReq( aStatus );
       
   209 
       
   210     if ( aStatus && ( orig == EPluginStop || orig == EPluginStart) )
       
   211         {
       
   212 #ifdef _DEBUG
       
   213         PluginLogger::Log(_L("PluginEntityWatcher: calls DeleteWatcher **"));
       
   214 #endif
       
   215         // Delete this entity as useless
       
   216         iPluginWatcher.DeleteWatcher( iEntityId->Des() );
       
   217         // Do not call anything, since the method call above deletes this instance.
       
   218         }
       
   219     }
       
   220 
       
   221 // ---------------------------------------------------------------------------
       
   222 // CSimplePluginEntityWatcher::WatcherNotificationL
       
   223 // ---------------------------------------------------------------------------
       
   224 //
       
   225 void CSimplePluginEntityWatcher::WatcherNotificationL(
       
   226     MSimpleDocument& aDocument )
       
   227     {
       
   228 #ifdef _DEBUG
       
   229     PluginLogger::Log(_L("PluginEntityWatcher: WatcherNotificationL" ));
       
   230 #endif
       
   231 
       
   232     // No need to check the expiration here since WatcherTerminatedL
       
   233     // is called then too.
       
   234     MProtocolPresenceWatchingDataHost& watcherHost =
       
   235         iConnObs.Host()->ProtocolPresenceDataHost().WatchingDataHost();
       
   236     MPresenceInfo* prInfo =  iConnObs.PresenceObjectFactory().NewPresenceInfoLC();
       
   237 
       
   238     CSimplePluginData::NotifyToPrInfoL( iConnObs.PresenceObjectFactory(), aDocument, *prInfo );
       
   239 
       
   240 #ifdef _DEBUG
       
   241     // ---------------------------------------------------------
       
   242     const MPersonPresenceInfo* pers_debug = prInfo->PersonPresence();
       
   243     const MPresenceInfoFieldCollection& coll_debug = pers_debug->Fields();
       
   244     TInt count_debug = coll_debug.FieldCount();
       
   245     PluginLogger::Log(_L("PluginEntityWatcher: nbr of fields received =%d"), count_debug );
       
   246     // ---------------------------------------------------------
       
   247 #endif
       
   248 
       
   249     MXIMPIdentity* identity2 = iConnObs.ObjectFactory().NewIdentityLC();
       
   250     HBufC* uniBuffer =
       
   251         CnvUtfConverter::ConvertToUnicodeFromUtf8L( *aDocument.EntityURI() );
       
   252     CleanupStack::PushL( uniBuffer );               // << uniBuffer
       
   253     identity2->SetIdentityL( uniBuffer->Des() );
       
   254     CleanupStack::PopAndDestroy( uniBuffer );  // >> uniBuffer
       
   255 
       
   256     if ( iListSubsActive )
       
   257         {
       
   258         // Set Group Id
       
   259         HBufC* uniBuffer2 =
       
   260             CnvUtfConverter::ConvertToUnicodeFromUtf8L( iEntityId->Des() );
       
   261         CleanupStack::PushL( uniBuffer2 );
       
   262         MXIMPIdentity* gId = iConnObs.ObjectFactory().NewIdentityLC(); // << gId
       
   263         gId->SetIdentityL( uniBuffer2->Des() );
       
   264 
       
   265         MXIMPObjectCollection* coll =
       
   266             iConnObs.ObjectFactory().NewObjectCollectionLC();    // << coll
       
   267 
       
   268        iPluginWatcher.GetEntitiesInListL( uniBuffer2->Des(), *coll );
       
   269 
       
   270         MProtocolPresentityGroupsDataHost& groupHost =
       
   271             iConnObs.Host()->ProtocolPresenceDataHost().GroupsDataHost();
       
   272 #ifdef _DEBUG
       
   273         PluginLogger::Log(
       
   274         _L("PluginEntityWatcher: callback HandlePresentityGroupContentL"));
       
   275 #endif
       
   276         groupHost.HandlePresentityGroupContentL( gId, coll );
       
   277 
       
   278         CleanupStack::Pop( 2 ); // >> gId, coll
       
   279         CleanupStack::PopAndDestroy( uniBuffer2 );
       
   280 
       
   281         // This is done only for first notification for group subscription
       
   282         iListSubsActive = EFalse;
       
   283         }
       
   284 
       
   285     // PrFw Host API callback
       
   286 #ifdef _DEBUG
       
   287     PluginLogger::Log(
       
   288         _L("PluginEntityWatcher: callback HandleSubscribedPresentityPresenceL"));
       
   289 #endif
       
   290     watcherHost.HandleSubscribedPresentityPresenceL( identity2, prInfo );
       
   291     CleanupStack::Pop();        // >> identity2
       
   292     CleanupStack::Pop();        // >> prInfo
       
   293     }
       
   294 
       
   295 // ---------------------------------------------------------------------------
       
   296 // CSimplePluginEntityWatcher::WatcherListNotificationL
       
   297 // ---------------------------------------------------------------------------
       
   298 //
       
   299 void CSimplePluginEntityWatcher::WatcherListNotificationL(
       
   300     MSimplePresenceList& aList )
       
   301     {
       
   302 #ifdef _DEBUG
       
   303     PluginLogger::Log(_L("PluginEntityWatcher: WatcherListNotificationL" ));
       
   304 #endif
       
   305 
       
   306     // call all the necessary callbacks, for new data + teminated ones.
       
   307 
       
   308     // check out whether fullstate list or not
       
   309     TBool fullState( EFalse );
       
   310 
       
   311     MProtocolPresenceWatchingDataHost& watcherHost =
       
   312         iConnObs.Host()->ProtocolPresenceDataHost().WatchingDataHost();
       
   313 
       
   314     RPointerArray<MPresenceInfo> entities;
       
   315     RPointerArray<MXIMPIdentity> terminated;
       
   316     RPointerArray<MXIMPIdentity> allEntities;
       
   317 
       
   318     TRAPD( err, CSimplePluginData::NotifyListToPrInfoL(
       
   319         iConnObs.ObjectFactory(), iConnObs.PresenceObjectFactory(),
       
   320         aList, entities, allEntities, terminated, fullState ));
       
   321     if ( err )
       
   322         {
       
   323 #ifdef _DEBUG
       
   324         PluginLogger::Log(_L("PluginEntityWatcher: WatcherListNotificationL error *" ));
       
   325 #endif
       
   326         entities.ResetAndDestroy();
       
   327         terminated.ResetAndDestroy();
       
   328         allEntities.ResetAndDestroy();
       
   329         entities.Close();
       
   330         terminated.Close();
       
   331         allEntities.Close();
       
   332         return;
       
   333         }
       
   334 
       
   335     CompleteClientReq( KErrNone );
       
   336 
       
   337     // Call HandleSubscribedPresentityPresenceL for all users
       
   338     TInt counter = entities.Count();
       
   339     for ( TInt i = counter-1 ; i>=0; i-- )
       
   340         {
       
   341         // ---------------------------------------------------------
       
   342 #ifdef _DEBUG
       
   343         const MPersonPresenceInfo* info = entities[i]->PersonPresence();
       
   344         const MPresenceInfoFieldCollection& coll_debug = info->Fields();
       
   345         TInt count_debug = coll_debug.FieldCount();
       
   346 
       
   347         PluginLogger::Log(
       
   348             _L("PluginWatcher: callback HandleSubscribedPresentityPresenceL nbrFields=%d"), count_debug );
       
   349 #endif
       
   350     // ---------------------------------------------------------
       
   351 
       
   352         watcherHost.HandleSubscribedPresentityPresenceL(
       
   353             allEntities[i], entities[i] );
       
   354         // Owenership is transferred
       
   355         allEntities.Remove(i);
       
   356         entities.Remove(i);
       
   357         }
       
   358 
       
   359     // call SetPresentityPresenceDataSubscriptionStateL for terminated users
       
   360     counter = terminated.Count();
       
   361     for ( TInt i = counter-1 ; i>=0; i-- )
       
   362         {
       
   363         MXIMPDataSubscriptionState *state = iConnObs.ObjectFactory().NewDataSubscriptionStateLC();
       
   364         state->SetSubscriptionStateL(
       
   365             MXIMPDataSubscriptionState::ESubscriptionInactive );
       
   366 #ifdef _DEBUG
       
   367         PluginLogger::Log(
       
   368                 _L("PluginWatcher: TEST COVERS 1"));
       
   369         PluginLogger::Log(
       
   370         _L("PluginWatcher: callback SetPresentityPresenceDataSubscriptionStateL"));
       
   371 #endif
       
   372         watcherHost.SetPresentityPresenceDataSubscriptionStateL(
       
   373             terminated[i], state, NULL );
       
   374         CleanupStack::Pop( 1 ); // state
       
   375         // Owenership is transferred
       
   376         terminated.Remove(i);
       
   377         }
       
   378 
       
   379     entities.ResetAndDestroy();
       
   380     terminated.ResetAndDestroy();
       
   381     allEntities.ResetAndDestroy();
       
   382 
       
   383     entities.Close();
       
   384     terminated.Close();
       
   385     allEntities.Close();
       
   386     }
       
   387 
       
   388 // ---------------------------------------------------------------------------
       
   389 // CSimplePluginEntityWatcher::WatcherTerminatedL
       
   390 // ---------------------------------------------------------------------------
       
   391 //
       
   392 void CSimplePluginEntityWatcher::WatcherTerminatedL(
       
   393     TInt /*aOpId*/, TInt /*aReason*/ )
       
   394     {
       
   395     // Call PrFw Host and  tell to CSimplePluginWatcher that
       
   396     // this entity can be deleted.
       
   397 
       
   398 #ifdef _DEBUG
       
   399     PluginLogger::Log(_L("PluginEntityWatcher: WatcherTerminatedL" ));
       
   400 #endif
       
   401 
       
   402     MProtocolPresenceWatchingDataHost& watcherHost =
       
   403         iConnObs.Host()->ProtocolPresenceDataHost().WatchingDataHost();
       
   404     MXIMPDataSubscriptionState *state = iConnObs.ObjectFactory().NewDataSubscriptionStateLC();
       
   405     MXIMPStatus* status = iConnObs.ObjectFactory().NewStatusLC();
       
   406     state->SetSubscriptionStateL(
       
   407         MXIMPDataSubscriptionState::ESubscriptionInactive );
       
   408     status->SetResultCode( KErrCompletion );
       
   409     MXIMPIdentity* identity = iConnObs.ObjectFactory().NewIdentityLC();
       
   410 
       
   411     HBufC16* buf16 = HBufC16::NewLC( PresentityId().Length());
       
   412     buf16->Des().Copy( PresentityId() );
       
   413     identity->SetIdentityL( buf16->Des() );
       
   414     CleanupStack::PopAndDestroy( buf16 );
       
   415 
       
   416 #ifdef _DEBUG
       
   417     PluginLogger::Log(
       
   418     _L("PluginWatcher: callback SetPresentityPresenceDataSubscriptionStateL"));
       
   419 #endif
       
   420     watcherHost.SetPresentityPresenceDataSubscriptionStateL(
       
   421         identity, state, status );
       
   422     CleanupStack::Pop( 3 ); // identity, status, state
       
   423 
       
   424     // Delete this entity as useless
       
   425     iPluginWatcher.DeleteWatcher( iEntityId->Des() );
       
   426     // Do not call anything, since the method call above deletes this instance.
       
   427 
       
   428     }
       
   429 
       
   430 // ---------------------------------------------------------------------------
       
   431 // CSimplePluginEntityWatcher::CompleteClientReq
       
   432 // ---------------------------------------------------------------------------
       
   433 //
       
   434 void CSimplePluginEntityWatcher::CompleteClientReq( TInt aStatus )
       
   435     {
       
   436     // complete the open PrFw request immediately here!
       
   437     iPluginWatcher.CompleteWatcher( aStatus );
       
   438     }
       
   439 
       
   440 // ---------------------------------------------------------------------------
       
   441 // CSimplePluginEntityWatcher::PresentityId
       
   442 // ---------------------------------------------------------------------------
       
   443 //
       
   444 TPtrC8 CSimplePluginEntityWatcher::PresentityId( )
       
   445     {
       
   446     return iEntityId ? iEntityId->Des() : TPtrC8();
       
   447     }
       
   448 
       
   449 // ---------------------------------------------------------------------------
       
   450 // CSimplePluginEntityWatcher::PrFwRequestId
       
   451 // ---------------------------------------------------------------------------
       
   452 //
       
   453 TXIMPRequestId CSimplePluginEntityWatcher::PrFwRequestId( )
       
   454     {
       
   455     return iPrFwId;
       
   456     }
       
   457 
       
   458 
       
   459 // End of file