IMPSengine/engsrv/src/impsorphans.cpp
changeset 0 094583676ce7
equal deleted inserted replaced
-1:000000000000 0:094583676ce7
       
     1 /*
       
     2 * Copyright (c) 2004 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: Class for imps oprhan manage.
       
    15 *
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include    <e32std.h>
       
    22 #include    <e32base.h>
       
    23 #include    "ImpsOrphans.h"
       
    24 #include    "impsfields.h"
       
    25 #include    "impsdataaccessor.h"
       
    26 #include    "impssdatautils.h"
       
    27 #include    "impskey.h"
       
    28 
       
    29 // MACROS
       
    30 #ifndef _DEBUG
       
    31 #define _NO_IMPS_LOGGING_
       
    32 #endif
       
    33 
       
    34 // constant
       
    35 _LIT( KDefaultApplicationID, "Default" );
       
    36 static const TInt KDefaultQueueCapacity = 10; //The capacity of the orphan queue
       
    37 static const TInt KDefaultOrphanValidity = 3600;
       
    38 
       
    39 //**********************************
       
    40 // CImpsOrphans
       
    41 //**********************************
       
    42 CImpsOrphans::CImpsOrphans( )
       
    43     {
       
    44     }
       
    45 
       
    46 // -----------------------------------------------------------------------------
       
    47 // CImpsOrphans::ConstructL
       
    48 // 2nd phase constructor
       
    49 // -----------------------------------------------------------------------------
       
    50 //
       
    51 void CImpsOrphans::ConstructL()
       
    52     {
       
    53     iOrphanQueue = new( ELeave ) COrphanQueue( KDefaultQueueCapacity );
       
    54     iLauncherProxy = CImpsAppLauncherProxy::NewL( this );
       
    55     }
       
    56 
       
    57 // -----------------------------------------------------------------------------
       
    58 // CImpsOrphans::NewL
       
    59 //
       
    60 // -----------------------------------------------------------------------------
       
    61 //
       
    62 CImpsOrphans* CImpsOrphans::NewL()
       
    63     {
       
    64     // Perform the construction.
       
    65     CImpsOrphans* self = new ( ELeave ) CImpsOrphans( );
       
    66 
       
    67     CleanupStack::PushL( self );
       
    68     self->ConstructL();
       
    69     CleanupStack::Pop();
       
    70 
       
    71     return self;
       
    72     }
       
    73 
       
    74 // -----------------------------------------------------------------------------
       
    75 // Destructor
       
    76 //
       
    77 // -----------------------------------------------------------------------------
       
    78 //
       
    79 CImpsOrphans::~CImpsOrphans()
       
    80     {
       
    81     delete iLauncherProxy;
       
    82     delete iOrphanQueue;
       
    83     }
       
    84 
       
    85 
       
    86 // -----------------------------------------------------------------------------
       
    87 // CImpsOrphans::NewOrphanL
       
    88 // Creates a new oprhan message and tries to launch the receiving application
       
    89 // -----------------------------------------------------------------------------
       
    90 //
       
    91 void CImpsOrphans::NewOrphanL( CImpsFields* aMsg, TImpsSessIdent aCspId )
       
    92     {
       
    93     // Handle only NewMessage & Invitation
       
    94     if ( ( aMsg->MessageType() != EImpsNewMessage ) &&
       
    95          ( aMsg->MessageType() != EImpsInviteUserReq ) )
       
    96         {
       
    97 #ifndef _NO_IMPS_LOGGING_
       
    98         CImpsClientLogger::Log( _L( "CImpsOrphans: Orphan filtered out %d" ), aMsg->MessageType() );
       
    99 #endif
       
   100         // Do not delete aMsg immediately but leave. This is because of
       
   101         // CImpsCSPSession::CreateSapResponse still has references
       
   102         // to CImpsFields entity used after this method call.
       
   103         User::Leave( KErrNotSupported );
       
   104         return;
       
   105         }
       
   106 
       
   107     TImpsEventType eventType( ServiceType( ( TImpsMessageType )aMsg->MessageType() ) );
       
   108     COrphan* orphan = AddOrphanL( aMsg, eventType, aCspId ); // the ownership is in the iOrphanList
       
   109     // Call application launcher
       
   110     TInt err( iLauncherProxy->LaunchApplicationL( orphan->ApplicationId(), *( orphan->iSap ), *( orphan->iUserId ) ) );
       
   111     if ( err != KErrNone )
       
   112         {
       
   113         // Application starting failed
       
   114         // Orphaned message is kept in the queue until it expires or it is overwritten
       
   115 #ifndef _NO_IMPS_LOGGING_
       
   116         CImpsClientLogger::Log( _L( "CImpsOrphans: Starting failed %d." ), err );
       
   117 #endif
       
   118         }
       
   119     }
       
   120 
       
   121 // -----------------------------------------------------------------------------
       
   122 // CImpsOrphans::NextOrphanLC
       
   123 // Transfers the next orphan message from the queue for handling
       
   124 // -----------------------------------------------------------------------------
       
   125 //
       
   126 CImpsFields* CImpsOrphans::NextOrphanLC( const TDesC& aCID,
       
   127                                          TImpsEventType aServiceType,
       
   128                                          TImpsSessIdent aCspId )
       
   129     {
       
   130     if ( ( aServiceType == EImpsEventMessage ) ||
       
   131          ( aServiceType == EImpsEventCommon ) )
       
   132         {
       
   133         return iOrphanQueue->OrphanMessageLC( aCID, aServiceType, aCspId );
       
   134         }
       
   135     else
       
   136         {
       
   137         return NULL;
       
   138         }
       
   139     }
       
   140 
       
   141 // -----------------------------------------------------------------------------
       
   142 // CImpsOrphans::CheckExpiryL
       
   143 // Deletes the expired orphan messages
       
   144 // -----------------------------------------------------------------------------
       
   145 //
       
   146 void CImpsOrphans::CheckExpiryL( )
       
   147     {
       
   148 
       
   149     TTime myExpiry;
       
   150     myExpiry.HomeTime();
       
   151 
       
   152     iOrphanQueue->DiscardExpired( myExpiry );
       
   153 
       
   154     }
       
   155 
       
   156 // -----------------------------------------------------------------------------
       
   157 // CImpsOrphans::Stop
       
   158 //
       
   159 // (other items were commented in a header).
       
   160 // -----------------------------------------------------------------------------
       
   161 //
       
   162 void CImpsOrphans::Stop( )
       
   163     {
       
   164     }
       
   165 
       
   166 // -----------------------------------------------------------------------------
       
   167 // CImpsOrphans::HandleAppLaunchL
       
   168 // Handles the application launch
       
   169 // -----------------------------------------------------------------------------
       
   170 //
       
   171 void CImpsOrphans::HandleAppLaunch( const TDesC& /*aApplicationId*/, TInt aStatus )
       
   172     {
       
   173 #ifndef _NO_IMPS_LOGGING_
       
   174     CImpsClientLogger::Log( _L( "CImpsOrphans: HandleAppLaunchL status %d" ), aStatus );
       
   175 #endif
       
   176 
       
   177     // removing the warning
       
   178     aStatus++;
       
   179     }
       
   180 // -----------------------------------------------------------------------------
       
   181 // CImpsOrphans::AddOrphanL
       
   182 // Creates and adds an orphan message to queue
       
   183 // (other items were commented in a header).
       
   184 // -----------------------------------------------------------------------------
       
   185 //
       
   186 COrphan* CImpsOrphans::AddOrphanL( CImpsFields *aFields,
       
   187                                    TImpsEventType aServiceType,
       
   188                                    TImpsSessIdent aCspId )
       
   189     {
       
   190     // Get the ClientID, SessionID
       
   191     TPtrC sessionID = aFields->SessionIdL();
       
   192     TPtrC applicationID;
       
   193 
       
   194     TImpsDataAccessor myAc( aFields );
       
   195     TImpsSDataUtils::GetApplicationIDL( &myAc, applicationID );
       
   196     // Get the Validity time from message: relative time
       
   197     TTimeIntervalSeconds validity;
       
   198     GetValidityFromMessageL( myAc, validity );
       
   199 
       
   200     if ( applicationID.Length() == 0 )
       
   201         {
       
   202         applicationID.Set( KDefaultApplicationID );
       
   203         }
       
   204 #ifndef _NO_IMPS_LOGGING_
       
   205     CImpsClientLogger::Log( _L( "CImpsOrphans: NewOrphan(appID: %S, servType: %u, SAP: %S, user: %S)" ), &applicationID, aServiceType, &aCspId.SAP(), &aCspId.UserId() );
       
   206 #endif
       
   207 
       
   208     // Set the validity as absolute time
       
   209     TTime myTime;
       
   210     myTime.HomeTime();
       
   211     myTime += validity;
       
   212 
       
   213 #ifndef _NO_IMPS_LOGGING_
       
   214     _LIT( KDateString5, "%-B%:0%J%:1%T%:2%S%:3%+B" );
       
   215     TBuf<30> dateString;
       
   216     myTime.FormatL( dateString, KDateString5 );
       
   217     CImpsClientLogger::Log( _L( "CImpsOrphans: NewOrphan valid till %S" ), &dateString );
       
   218 #endif
       
   219 
       
   220     COrphan* orphan = COrphan::NewL( applicationID,
       
   221                                      sessionID,
       
   222                                      aServiceType,
       
   223                                      aCspId.SAP(),
       
   224                                      aCspId.UserId(),
       
   225                                      myTime,
       
   226                                      aFields );
       
   227     iOrphanQueue->Add( *orphan );
       
   228     return orphan;
       
   229     }
       
   230 
       
   231 
       
   232 // -----------------------------------------------------------------------------
       
   233 // CImpsOrphans::GetValidityFromMessage
       
   234 // Retrieves the message validity from the instant message. The validity is in
       
   235 // seconds. If the message did not contain the validity then the default
       
   236 // validity is returned.
       
   237 // (other items were commented in a header).
       
   238 // -----------------------------------------------------------------------------
       
   239 //
       
   240 void CImpsOrphans::GetValidityFromMessageL( MImpsDataAccessor& aAc,
       
   241                                             TTimeIntervalSeconds& aValidity )
       
   242     {
       
   243     TInt validity;
       
   244     CImpsKey* myKey = CImpsKey::NewLC();
       
   245     myKey->AddL( CREATEKEY( EImpsKeySession, 0 ) );
       
   246     myKey->AddL( CREATEKEY( EImpsKeyTransaction, 0 ) );
       
   247     myKey->AddL( CREATEKEY( EImpsKeyTransactionContent, 0 ) );
       
   248     myKey->AddL( CREATEKEY( EImpsKeyNewMessage, 0 ) );
       
   249     myKey->AddL( CREATEKEY( EImpsKeyMessageInfo, 0 ) );
       
   250     myKey->AddL( CREATEKEY( EImpsKeyValidity, 0 ) );
       
   251     if ( aAc.RestoreIntegerL( myKey, validity ) )
       
   252         {
       
   253         aValidity = validity;
       
   254         }
       
   255     else
       
   256         {
       
   257         aValidity = KDefaultOrphanValidity;
       
   258         }
       
   259     CleanupStack::PopAndDestroy( );  //myKey
       
   260     }
       
   261 
       
   262 
       
   263 TImpsEventType CImpsOrphans::ServiceType( TImpsMessageType aMessageType )
       
   264     {
       
   265     TImpsEventType event( EImpsEventNone );
       
   266 
       
   267     switch ( aMessageType )
       
   268         {
       
   269             // IM
       
   270         case EImpsNewMessage:
       
   271             event = EImpsEventMessage;
       
   272             break;
       
   273             // Fundamental
       
   274         case EImpsInviteUserReq:
       
   275             event = EImpsEventCommon;
       
   276             break;
       
   277         default:
       
   278             event = EImpsEventNone;
       
   279         }
       
   280     return event;
       
   281     }
       
   282 
       
   283 
       
   284 //////////////////////////////////////////////////////////////////////////
       
   285 // -----------------------------------------------------------------------------
       
   286 // COrphanQueue::COrphanQueue
       
   287 // Constructor
       
   288 // -----------------------------------------------------------------------------
       
   289 //
       
   290 COrphanQueue::COrphanQueue( TInt aCapacity ):
       
   291         iOrphanList( _FOFF( COrphan, iLink ) ),  //lint !e413
       
   292         iCapacity( aCapacity ),
       
   293         iSize( 0 )
       
   294     {
       
   295     }
       
   296 
       
   297 // -----------------------------------------------------------------------------
       
   298 // COrphanQueue::~COrphanQueue
       
   299 // Destructor
       
   300 // -----------------------------------------------------------------------------
       
   301 //
       
   302 COrphanQueue::~COrphanQueue()
       
   303     {
       
   304     TDblQueIter<COrphan> iter ( iOrphanList );
       
   305     iter.SetToFirst();
       
   306 
       
   307     while ( iter )
       
   308         {
       
   309         COrphan* orphan = iter;
       
   310         iter++;
       
   311         orphan->Destroy();
       
   312         }
       
   313     }
       
   314 
       
   315 // -----------------------------------------------------------------------------
       
   316 // COrphanQueue::SetLength
       
   317 // Sets the new capacity of the queue
       
   318 // -----------------------------------------------------------------------------
       
   319 //
       
   320 void COrphanQueue::SetLength( TInt aNewCapacity )
       
   321     {
       
   322     iCapacity = aNewCapacity;
       
   323     }
       
   324 
       
   325 // -----------------------------------------------------------------------------
       
   326 // COrphanQueue::Add
       
   327 // Adds a new orphan message to the queue
       
   328 // -----------------------------------------------------------------------------
       
   329 //
       
   330 void COrphanQueue::Add( COrphan& aOrphan )
       
   331     {
       
   332     // Check the capacity
       
   333     __ASSERT_DEBUG( iCapacity, User::Panic( KImpsPanicCategory, EImpsCorrupted ) );
       
   334     if ( iSize < iCapacity )
       
   335         {
       
   336         // Simply add to the end
       
   337         iOrphanList.AddLast( aOrphan );
       
   338         ++iSize;
       
   339         }
       
   340     else
       
   341         {
       
   342         // Remove the 1st and add to the last
       
   343         COrphan* orphan = iOrphanList.First();
       
   344         orphan->Destroy();
       
   345         iOrphanList.AddLast( aOrphan );
       
   346         }
       
   347     }
       
   348 
       
   349 // -----------------------------------------------------------------------------
       
   350 // COrphanQueue::OrphanMessage
       
   351 // Returns the orphan message with the given client ID and removes it from the
       
   352 // queue. NULL is returned if not found.
       
   353 // -----------------------------------------------------------------------------
       
   354 //
       
   355 CImpsFields* COrphanQueue::OrphanMessageLC( const TDesC& aApplicationId,
       
   356                                             TImpsEventType aServiceType,
       
   357                                             TImpsSessIdent aCspId )
       
   358     {
       
   359     TPtrC applicationId;
       
   360     aApplicationId.Length() ? applicationId.Set( aApplicationId ) : applicationId.Set( KDefaultApplicationID );
       
   361 
       
   362 #ifndef _NO_IMPS_LOGGING_
       
   363     CImpsClientLogger::Log( _L( "CImpsOrphans: NextOrphanLC(appID: %S, servType: %u, SAP: %S, user: %S)" ), &applicationId, aServiceType, &aCspId.SAP(), &aCspId.UserId() );
       
   364 #endif
       
   365 
       
   366     TDblQueIter<COrphan> iter ( iOrphanList );
       
   367     iter.SetToFirst();
       
   368 
       
   369     while ( iter )
       
   370         {
       
   371         COrphan* orphan = iter;
       
   372         iter++;
       
   373         // Find the given ClientID, Service Type, SAP and UserId
       
   374         if ( ( !applicationId.Compare( orphan->ApplicationId() ) )  &&
       
   375              ( aServiceType == orphan->iServiceType ) &&
       
   376              ( !orphan->iSap->CompareF( aCspId.SAP() ) )  &&
       
   377              ( !orphan->iUserId->CompareF( aCspId.UserId() ) ) )
       
   378             {
       
   379             // found
       
   380             // transfer the ownership to the caller
       
   381             CImpsFields* impsFields = orphan->Message();
       
   382             orphan->RemoveOwnership();
       
   383             orphan->Destroy();
       
   384             --iSize;
       
   385             CleanupStack::PushL( impsFields );
       
   386 #ifndef _NO_IMPS_LOGGING_
       
   387             CImpsClientLogger::Log( _L( "CImpsOrphans: NextOrphanLC(): Found! " ) );
       
   388 #endif
       
   389             return impsFields;
       
   390             }
       
   391         }
       
   392 #ifndef _NO_IMPS_LOGGING_
       
   393     CImpsClientLogger::Log( _L( "CImpsOrphans: NextOrphanLC() Not found!" ) );
       
   394 #endif
       
   395 
       
   396     return NULL;
       
   397     }
       
   398 
       
   399 // -----------------------------------------------------------------------------
       
   400 // COrphanQueue::DestroyOrphans
       
   401 // Deletes all orphan messages which have the give ClientID
       
   402 // -----------------------------------------------------------------------------
       
   403 //
       
   404 void COrphanQueue::DestroyOrphans( TDesC& aApplicationId )
       
   405     {
       
   406     TDblQueIter<COrphan> iter ( iOrphanList );
       
   407     iter.SetToFirst();
       
   408 
       
   409     while ( iter )
       
   410         {
       
   411         COrphan* orphan = iter;
       
   412         iter++;
       
   413         if ( !aApplicationId.Compare( orphan->ApplicationId() ) )
       
   414             {
       
   415             orphan->Destroy();
       
   416             --iSize;
       
   417             }
       
   418         }
       
   419     }
       
   420 
       
   421 // -----------------------------------------------------------------------------
       
   422 // COrphanQueue::DiscardExpired
       
   423 // Deletes all orphan messages which are expired
       
   424 // -----------------------------------------------------------------------------
       
   425 //
       
   426 void COrphanQueue::DiscardExpired( TTime aExpiry )
       
   427     {
       
   428 
       
   429 #ifndef _NO_IMPS_LOGGING_
       
   430     CImpsClientLogger::Log( _L( "CImpsOrphans: Check expired" ) );
       
   431 #endif
       
   432 
       
   433     TDblQueIter<COrphan> iter ( iOrphanList );
       
   434     iter.SetToFirst();
       
   435 
       
   436     while ( iter )
       
   437         {
       
   438         COrphan* orphan = iter;
       
   439         iter++;
       
   440         // Check the validity
       
   441 
       
   442         if ( orphan->Expiry() < aExpiry )
       
   443             {
       
   444 #ifndef _NO_IMPS_LOGGING_
       
   445             CImpsClientLogger::Log( _L( "CImpsOrphans: Expired discarded" ) );
       
   446 #endif
       
   447             orphan->Destroy();
       
   448             --iSize;
       
   449             }
       
   450         }
       
   451 
       
   452 
       
   453     }
       
   454 //////////////////////////////////////////////////////////////////////////
       
   455 
       
   456 // -----------------------------------------------------------------------------
       
   457 // COrphan::COrphan
       
   458 // -----------------------------------------------------------------------------
       
   459 
       
   460 COrphan::COrphan(
       
   461     const TDesC& aApplicationID,
       
   462     const TDesC& aSessionId,
       
   463     TImpsEventType aServiceType,
       
   464     TTime aValidity,
       
   465     CImpsFields* aFields ) :
       
   466         iServiceType( aServiceType ),
       
   467         iFields ( aFields ),
       
   468         iExpiry( aValidity )
       
   469     {
       
   470     iCID = aApplicationID;
       
   471     iSessionId = aSessionId;
       
   472     }
       
   473 
       
   474 // -----------------------------------------------------------------------------
       
   475 // COrphan::ConstructL
       
   476 // -----------------------------------------------------------------------------
       
   477 void COrphan::ConstructL(
       
   478     const TDesC&    aSap,
       
   479     const TDesC&    aUserId )
       
   480     {
       
   481     iSap = aSap.AllocL();
       
   482     iUserId = aUserId.AllocL();
       
   483     }
       
   484 
       
   485 // -----------------------------------------------------------------------------
       
   486 // COrphan::NewL
       
   487 // -----------------------------------------------------------------------------
       
   488 COrphan* COrphan::NewL(
       
   489     const TDesC& aCID,
       
   490     const TDesC& aSessionId,
       
   491     TImpsEventType aServiceType,
       
   492     const TDesC&    aSap,
       
   493     const TDesC&    aUserId,
       
   494     TTime aValidity,
       
   495     CImpsFields* aFields )
       
   496     {
       
   497     COrphan* self = new ( ELeave ) COrphan( aCID, aSessionId, aServiceType, aValidity, aFields );
       
   498 
       
   499     CleanupStack::PushL( self );
       
   500     self->ConstructL( aSap, aUserId );
       
   501     CleanupStack::Pop();
       
   502 
       
   503     return self;
       
   504 
       
   505     }
       
   506 // -----------------------------------------------------------------------------
       
   507 // COrphan::~COrphan
       
   508 // -----------------------------------------------------------------------------
       
   509 COrphan::~COrphan()
       
   510     {
       
   511     delete iSap;
       
   512     delete iUserId;
       
   513     } //lint !e1540 iFields freed in Destroy
       
   514 
       
   515 // -----------------------------------------------------------------------------
       
   516 // COrphan::Destroy
       
   517 // -----------------------------------------------------------------------------
       
   518 void COrphan::Destroy()
       
   519     {
       
   520     iLink.Deque();
       
   521     delete iFields;
       
   522     iFields = NULL;
       
   523     delete this;
       
   524     }
       
   525 
       
   526 // -----------------------------------------------------------------------------
       
   527 // COrphan::ClientId
       
   528 // -----------------------------------------------------------------------------
       
   529 TDesC& COrphan::ApplicationId()
       
   530     {
       
   531     return iCID;
       
   532     }
       
   533 
       
   534 // -----------------------------------------------------------------------------
       
   535 // COrphan::Message
       
   536 // -----------------------------------------------------------------------------
       
   537 CImpsFields* COrphan::Message()
       
   538     {
       
   539     return iFields;
       
   540     }
       
   541 
       
   542 // -----------------------------------------------------------------------------
       
   543 // COrphan::Expiry
       
   544 // -----------------------------------------------------------------------------
       
   545 TTime COrphan::Expiry()
       
   546     {
       
   547     return iExpiry;
       
   548     }
       
   549 
       
   550 // -----------------------------------------------------------------------------
       
   551 // COrphan::RemoveOwnership
       
   552 // -----------------------------------------------------------------------------
       
   553 void COrphan::RemoveOwnership()
       
   554     {
       
   555     iFields = NULL;
       
   556     }
       
   557 
       
   558 //  End of File