omadrm/drmhelper/drmhelperserver/src/EndTimeFactory.cpp
changeset 0 95b198f216e5
equal deleted inserted replaced
-1:000000000000 0:95b198f216e5
       
     1 /*
       
     2 * Copyright (c) 2002 - 2007 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Handling end time based automated content
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include    <e32base.h>
       
    22 #include    <e32std.h>
       
    23 #include    <s32file.h>
       
    24 #include    "DRMHelperServer.h"
       
    25 #include    "EndTimeFactory.h"
       
    26 #include    "EndTimeBased.h"
       
    27 
       
    28 
       
    29 // EXTERNAL DATA STRUCTURES
       
    30 // EXTERNAL FUNCTION PROTOTYPES
       
    31 // CONSTANTS
       
    32 LOCAL_C const TInt KInformingDelay( 1 );
       
    33 
       
    34 // Maximum amount of times the expired item is handled before 
       
    35 // unregistering without client's call.
       
    36 // (In order to avoid stale registrations)
       
    37 LOCAL_C const TInt KMaxHandlingCount( 50 );
       
    38 // MACROS
       
    39 // LOCAL CONSTANTS AND MACROS
       
    40 #ifdef _DRM_TESTING
       
    41 LOCAL_C void WriteFileL( const TDesC8& text , RFs &aFs , const TDesC& aName )
       
    42     {
       
    43     RFile file;
       
    44     TInt size;
       
    45     User::LeaveIfError( file.Open( aFs, aName , EFileWrite ) );
       
    46     CleanupClosePushL( file );
       
    47     User::LeaveIfError( file.Size( size ) );
       
    48     User::LeaveIfError( file.Write( size, text ) );
       
    49     CleanupStack::PopAndDestroy(&file); //file
       
    50     }
       
    51 
       
    52 LOCAL_C void WriteLogL( const TDesC8& text , RFs &aFs )
       
    53     {
       
    54     _LIT( KLogFile , "c:\\ETFLog.txt" );
       
    55     WriteFileL( text , aFs , KLogFile );
       
    56     }
       
    57 
       
    58 LOCAL_C void CreateLogL()
       
    59     {
       
    60     RFs fs;
       
    61     User::LeaveIfError(fs.Connect());
       
    62     CleanupClosePushL(fs);
       
    63     RFile file;
       
    64     User::LeaveIfError( file.Replace( fs , _L("c:\\ETFLog.txt") , EFileWrite ) );
       
    65     file.Close();
       
    66     CleanupStack::PopAndDestroy(&fs); //fs
       
    67     }
       
    68 
       
    69 LOCAL_C void WriteL( const TDesC& aText )
       
    70     {
       
    71     RFs fs;
       
    72     User::LeaveIfError( fs.Connect() );
       
    73     CleanupClosePushL(fs);
       
    74     HBufC8* text = HBufC8::NewLC(1000);
       
    75     TPtr8 textptr(text->Des() );
       
    76     textptr.Append( aText );
       
    77     textptr.Append(_L( "\r\n" ));
       
    78     WriteLogL(textptr , fs);
       
    79     CleanupStack::PopAndDestroy(text);
       
    80     CleanupStack::PopAndDestroy(&fs); //fs
       
    81     }
       
    82 
       
    83 LOCAL_C void WriteL( const TDesC8& aText )
       
    84     {
       
    85     RFs fs;
       
    86     User::LeaveIfError( fs.Connect() );
       
    87     CleanupClosePushL(fs);
       
    88     HBufC8* text = HBufC8::NewLC(1000);
       
    89     TPtr8 textptr(text->Des() );
       
    90     textptr.Append( aText );
       
    91     textptr.Append(_L8( "\r\n" ));
       
    92     WriteLogL(textptr , fs);
       
    93     CleanupStack::PopAndDestroy(text);
       
    94     CleanupStack::PopAndDestroy(&fs); //fs
       
    95     }
       
    96 
       
    97 LOCAL_C void WriteTimeL( TTime aTime )
       
    98     {
       
    99     RFs fs;
       
   100     User::LeaveIfError( fs.Connect() );
       
   101     CleanupClosePushL(fs);
       
   102     HBufC8* text = HBufC8::NewLC(100);
       
   103     TPtr8 textptr(text->Des() );
       
   104     // Date and Time display
       
   105     TBuf<256> dateString;
       
   106     _LIT(KDate,"%*E%*D%X%*N%*Y %1 %2 '%3");
       
   107     aTime.FormatL(dateString,KDate);
       
   108     textptr.Append(_L( "\r\n\t\t\t\tData:\t" ) );
       
   109     textptr.Append( dateString );
       
   110     _LIT(KTime,"%-B%:0%J%:1%T%:2%S%:3%+B");
       
   111     aTime.FormatL(dateString,KTime);
       
   112     textptr.Append(_L( "\r\n\t\t\t\tTime:\t" ) );
       
   113     textptr.Append( dateString );
       
   114     textptr.Append(_L( "\r\n" ) );
       
   115     textptr.Append(_L( "\r\n" ) );
       
   116     WriteLogL(textptr , fs);
       
   117     CleanupStack::PopAndDestroy(text);
       
   118     CleanupStack::PopAndDestroy(&fs); //fs
       
   119     }
       
   120 
       
   121 LOCAL_C void WriteCurrentTimeL()
       
   122     {
       
   123     RFs fs;
       
   124     User::LeaveIfError( fs.Connect() );
       
   125     CleanupClosePushL(fs);
       
   126     HBufC8* text = HBufC8::NewLC(100);
       
   127     TPtr8 textptr(text->Des() );
       
   128     // Date and Time display
       
   129     TTime time;
       
   130     time.HomeTime();
       
   131     TBuf<256> dateString;
       
   132     _LIT(KDate,"%*E%*D%X%*N%*Y %1 %2 '%3");
       
   133     time.FormatL(dateString,KDate);
       
   134     textptr.Append(_L( "\r\n\t\tData:\t" ) );
       
   135     textptr.Append( dateString );
       
   136     _LIT(KTime,"%-B%:0%J%:1%T%:2%S%:3%+B");
       
   137     time.FormatL(dateString,KTime);
       
   138     textptr.Append(_L( "\r\n\t\tTime:\t" ) );
       
   139     textptr.Append( dateString );
       
   140     textptr.Append(_L( "\r\n" ) );
       
   141     textptr.Append(_L( "\r\n" ) );
       
   142     WriteLogL(textptr , fs);
       
   143     CleanupStack::PopAndDestroy(text);
       
   144     CleanupStack::PopAndDestroy(&fs); //fs
       
   145     }
       
   146 #endif
       
   147 // MODULE DATA STRUCTURES
       
   148 // LOCAL FUNCTION PROTOTYPES
       
   149 // FORWARD DECLARATIONS
       
   150 
       
   151 // ============================ MEMBER FUNCTIONS ===============================
       
   152 
       
   153 // -----------------------------------------------------------------------------
       
   154 // CEndTimeFactory::CEndTimeFactory
       
   155 // C++ default constructor can NOT contain any code, that
       
   156 // might leave.
       
   157 // -----------------------------------------------------------------------------
       
   158 //
       
   159 CEndTimeFactory::CEndTimeFactory( CDRMHelperServer& aServer ):
       
   160     CTimer(CActive::EPriorityStandard),
       
   161     iServer( aServer ),
       
   162     iOverflowedAlarm( Time::MaxTTime() )
       
   163     {
       
   164     iAlarm.HomeTime();
       
   165     }
       
   166 
       
   167 // -----------------------------------------------------------------------------
       
   168 // CEndTimeFactory::ConstructL
       
   169 // Symbian 2nd phase constructor can leave.
       
   170 // -----------------------------------------------------------------------------
       
   171 //
       
   172 void CEndTimeFactory::ConstructL()
       
   173     {
       
   174 #ifdef _DRM_TESTING
       
   175     CreateLogL(); //test
       
   176 #endif
       
   177     CTimer::ConstructL();
       
   178     CActiveScheduler::Add( this );
       
   179     }
       
   180 
       
   181 // -----------------------------------------------------------------------------
       
   182 // CEndTimeFactory::NewL
       
   183 // Two-phased constructor.
       
   184 // -----------------------------------------------------------------------------
       
   185 //
       
   186 CEndTimeFactory* CEndTimeFactory::NewL(CDRMHelperServer& aServer)
       
   187     {
       
   188     CEndTimeFactory* self = new( ELeave ) CEndTimeFactory(aServer);
       
   189 
       
   190     CleanupStack::PushL( self );
       
   191     self->ConstructL();
       
   192     CleanupStack::Pop(self);
       
   193 
       
   194     return self;
       
   195     }
       
   196 
       
   197 
       
   198 // Destructor
       
   199 CEndTimeFactory::~CEndTimeFactory()
       
   200     {
       
   201 #ifdef _DRM_TESTING
       
   202     TRAPD( err , WriteL(_L("CEndTimeFactory-Destruct")) );
       
   203     TRAP( err , WriteCurrentTimeL() );
       
   204 #endif
       
   205 
       
   206     iList.ResetAndDestroy();
       
   207     iList.Close();
       
   208     Cancel();
       
   209     }
       
   210 
       
   211 
       
   212 // -----------------------------------------------------------------------------
       
   213 // CEndTimeFactory::AddL
       
   214 // add a specific content uri into factory
       
   215 // (other items were commented in a header).
       
   216 // -----------------------------------------------------------------------------
       
   217 //
       
   218 void CEndTimeFactory::AddL(
       
   219         const TDesC8& aUri ,
       
   220         TTime aEndTime ,
       
   221         const TUint8& aPermType ,
       
   222         const TUint8& aRegType ,
       
   223         const TUint8& aAutoType )
       
   224     {
       
   225 #ifdef _DRM_TESTING
       
   226     WriteL(_L("Add"));
       
   227     WriteL(aUri);
       
   228     WriteCurrentTimeL();
       
   229 #endif
       
   230 
       
   231     CEndTimeBased* newItem = NULL;
       
   232     TTime home;
       
   233     TInt index = 0;
       
   234 
       
   235 
       
   236     // check if uri is already in the list
       
   237     index = Find( aUri , aPermType , aRegType , aAutoType );
       
   238     if ( index >= 0 )
       
   239         {
       
   240         // if it is in the list then just update the end time.
       
   241         iList[index]->IncreaseRegTimes();
       
   242         UpdateEndTimeL( iList[index] , aEndTime );
       
   243         return;
       
   244         }
       
   245 
       
   246     // if not in the list then create one
       
   247     newItem = CEndTimeBased::NewL( aUri , aPermType , aRegType , aAutoType );
       
   248     CleanupStack::PushL(newItem);
       
   249 
       
   250     // set end time
       
   251     newItem->SetEndTime(aEndTime);
       
   252 
       
   253     // set expiration
       
   254     home.HomeTime();
       
   255     if ( home >= aEndTime )
       
   256         {
       
   257         newItem->SetIsExpired(ETrue);
       
   258         }
       
   259 
       
   260     // add the item into list
       
   261     User::LeaveIfError( iList.Append( newItem ) );
       
   262     CleanupStack::Pop(newItem);
       
   263     // change alarm time if necessary
       
   264     if ( home >= iAlarm && !newItem->IsExpired() )
       
   265         {
       
   266 #ifdef _DRM_TESTING
       
   267         //test code start
       
   268         WriteL(_L("Add->Change iAlarm-><home"));
       
   269         WriteL(aUri);
       
   270         WriteCurrentTimeL();
       
   271         WriteL(_L("Change Time"));
       
   272         WriteTimeL(aEndTime);
       
   273         //test code end
       
   274 #endif
       
   275 
       
   276         iAlarm = aEndTime;
       
   277         }
       
   278 
       
   279     if ( !newItem->IsExpired() && iAlarm >= aEndTime && iAlarm != Time::MaxTTime() )
       
   280         {
       
   281         iAlarm = aEndTime;
       
   282         Cancel();
       
   283 
       
   284 #ifdef _DRM_TESTING
       
   285         //test code start
       
   286         WriteL(_L("Add->Change iAlarm->>Change Time"));
       
   287         WriteL(aUri);
       
   288         WriteCurrentTimeL();
       
   289         WriteL(_L("Change Time"));
       
   290         WriteTimeL(iAlarm);
       
   291         WriteL(_L("Add->Trigger iAlarm"));
       
   292         WriteL(aUri);
       
   293         WriteL(_L("Trigger Time"));
       
   294         WriteTimeL(iAlarm);
       
   295         //test code end
       
   296 #endif
       
   297 
       
   298         CTimer::At(iAlarm+TTimeIntervalSeconds(KInformingDelay));
       
   299 
       
   300         }
       
   301 
       
   302     }
       
   303 
       
   304 // -----------------------------------------------------------------------------
       
   305 // CEndTimeFactory::UpdateL
       
   306 // update a specific content uri in the list
       
   307 // (other items were commented in a header).
       
   308 // -----------------------------------------------------------------------------
       
   309 //
       
   310 void CEndTimeFactory::UpdateL()
       
   311     {
       
   312 #ifdef _DRM_TESTING
       
   313     WriteL(_L("UpdateL(all)"));
       
   314     WriteCurrentTimeL();
       
   315 #endif
       
   316 
       
   317     for( TInt i( 0 ); i < iList.Count(); i++ )
       
   318         {
       
   319         const CEndTimeBased* e( iList[i] );
       
   320         iServer.UpdateL(
       
   321                 *e->Uri(), e->PermType(), e->RegType(), e->AutoType() );
       
   322         if ( e->Count() > KMaxHandlingCount )
       
   323             {
       
   324             // Client must have forgotten to unregister item properly.
       
   325             // So let's unregister now.
       
   326             Remove( *e->Uri(), e->PermType(), e->RegType(), e->AutoType() );
       
   327             }
       
   328         }
       
   329     }
       
   330 
       
   331 // -----------------------------------------------------------------------------
       
   332 // CEndTimeFactory::UpdateEndTimeL
       
   333 // update the end time of a specific content uri in the list
       
   334 // (other items were commented in a header).
       
   335 // -----------------------------------------------------------------------------
       
   336 //
       
   337 void CEndTimeFactory::UpdateEndTimeL(
       
   338         const TDesC8& aUri ,
       
   339         const TTime& aEndTime ,
       
   340         const TUint8& aPermType ,
       
   341         const TUint8& aRegType
       
   342         )
       
   343     {
       
   344 #ifdef _DRM_TESTING
       
   345     WriteL(_L("UpdateEndTimeL(uri,endtime,perm,reg)"));
       
   346     WriteL(aUri);
       
   347     WriteCurrentTimeL();
       
   348 #endif
       
   349 
       
   350     TInt index = 0;
       
   351     index = FindNext(aUri,aPermType,aRegType,index);
       
   352     while ( index >= 0 )
       
   353         {
       
   354         UpdateEndTimeL( iList[index] , aEndTime );
       
   355         index++;
       
   356         index = FindNext(aUri,aPermType,aRegType,index);
       
   357         }
       
   358     }
       
   359 
       
   360 
       
   361 // -----------------------------------------------------------------------------
       
   362 // CEndTimeFactory::UpdateEndTimeL
       
   363 // update the end time of a specific content uri in the list
       
   364 // (other items were commented in a header).
       
   365 // -----------------------------------------------------------------------------
       
   366 //
       
   367 void CEndTimeFactory::UpdateEndTimeL(
       
   368         const TDesC8& aUri ,
       
   369         const TTime& aEndTime ,
       
   370         const TUint8& aPermType ,
       
   371         const TUint8& aRegType ,
       
   372         const TUint8& aAutoType
       
   373         )
       
   374     {
       
   375 #ifdef _DRM_TESTING
       
   376     WriteL(_L("UpdateEndTimeL(uri,endtime,perm,reg,autotype)"));
       
   377     WriteL(aUri);
       
   378     WriteCurrentTimeL();
       
   379 #endif
       
   380 
       
   381     TInt index = 0;
       
   382     index = Find(aUri,aPermType,aRegType,aAutoType);
       
   383     if ( index >= 0 )
       
   384         {
       
   385         UpdateEndTimeL( iList[index] , aEndTime );
       
   386         }
       
   387     }
       
   388 
       
   389 // -----------------------------------------------------------------------------
       
   390 // CEndTimeFactory::UpdateEndTimeL
       
   391 // update the end time of a specific content uri in the list
       
   392 // (other items were commented in a header).
       
   393 // -----------------------------------------------------------------------------
       
   394 //
       
   395 void CEndTimeFactory::UpdateEndTimeL( CEndTimeBased*& aItem , const TTime& aEndTime )
       
   396     {
       
   397 #ifdef _DRM_TESTING
       
   398     WriteL(_L("UpdateEndTimeL(item,endtime)"));
       
   399     WriteL(aItem->Uri()->Des());
       
   400     WriteCurrentTimeL();
       
   401 #endif
       
   402 
       
   403     aItem->SetEndTime(aEndTime);
       
   404     ResetAlarm();
       
   405     }
       
   406 
       
   407 
       
   408 // -----------------------------------------------------------------------------
       
   409 // CEndTimeFactory::IsRegisteredL
       
   410 // check if content uri is registered into list or not
       
   411 // (other items were commented in a header).
       
   412 // -----------------------------------------------------------------------------
       
   413 //
       
   414 TBool CEndTimeFactory::IsRegistered(
       
   415         const TDesC8& aUri ,
       
   416         const TUint8& aPermType ,
       
   417         const TUint8& aRegType ,
       
   418         const TUint8& aAutoType )
       
   419     {
       
   420 #ifdef _DRM_TESTING
       
   421     TRAPD( err , WriteL(_L("IsRegisteredL(aUri,aPermType,aRegType,aAutoType)")) );
       
   422     TRAP( err , WriteL(aUri) );
       
   423     TRAP( err , WriteCurrentTimeL() );
       
   424 #endif
       
   425 
       
   426     // check if it is in the list
       
   427     if ( Find( aUri , aPermType , aRegType , aAutoType ) >= 0 )
       
   428         {
       
   429         return ETrue;
       
   430         }
       
   431     return EFalse;
       
   432     }
       
   433 
       
   434 // -----------------------------------------------------------------------------
       
   435 // CEndTimeFactory::IsRegisteredL
       
   436 // check if content uri is registered into list or not
       
   437 // (other items were commented in a header).
       
   438 // -----------------------------------------------------------------------------
       
   439 //
       
   440 TBool CEndTimeFactory::IsRegistered(
       
   441         const TDesC8& aUri ,
       
   442         const TUint8& aPermType ,
       
   443         const TUint8& aRegType )
       
   444     {
       
   445 #ifdef _DRM_TESTING
       
   446     TRAPD( err , WriteL(_L("IsRegisteredL(aUri,aPermType,aRegType)")) );
       
   447     TRAP( err , WriteL(aUri) );
       
   448     TRAP( err , WriteCurrentTimeL() );
       
   449 #endif
       
   450 
       
   451     // check if it is in the list
       
   452     TInt index = 0;
       
   453     if ( FindNext( aUri , aPermType , aRegType , index ) >= 0 )
       
   454         {
       
   455         return ETrue;
       
   456         }
       
   457     return EFalse;
       
   458     }
       
   459 
       
   460 // -----------------------------------------------------------------------------
       
   461 // CEndTimeFactory::IsRegisteredL
       
   462 // check if content uri is registered into list or not
       
   463 // (other items were commented in a header).
       
   464 // -----------------------------------------------------------------------------
       
   465 //
       
   466 TBool CEndTimeFactory::IsRegistered2(
       
   467         const TDesC8& aUri ,
       
   468         const TUint8& aRegType,
       
   469         const TUint8& aAutoType )
       
   470     {
       
   471 #ifdef _DRM_TESTING
       
   472     TRAPD( err , WriteL(_L("IsRegisteredL(aUri,aRegType)")) );
       
   473     TRAP( err , WriteL(aUri) );
       
   474     TRAP( err , WriteCurrentTimeL() );
       
   475 #endif
       
   476 
       
   477     // check if it is in the list
       
   478     if ( Exists( aUri, aRegType, aAutoType) >= 0 )
       
   479         {
       
   480         return ETrue;
       
   481         }
       
   482     return EFalse;
       
   483     }
       
   484 
       
   485 
       
   486 
       
   487 // -----------------------------------------------------------------------------
       
   488 // CEndTimeFactory::IsRegisteredL
       
   489 // check if content uri is registered into list or not
       
   490 // (other items were commented in a header).
       
   491 // -----------------------------------------------------------------------------
       
   492 //
       
   493 TBool CEndTimeFactory::IsRegistered( const TDesC8& aUri )
       
   494     {
       
   495 #ifdef _DRM_TESTING
       
   496     TRAPD( err , WriteL(_L("IsRegisteredL(aUri)")) );
       
   497     TRAP( err , WriteL(aUri) );
       
   498     TRAP( err , WriteCurrentTimeL() );
       
   499 #endif
       
   500 
       
   501     // check if it is in the list
       
   502     TInt index = 0;
       
   503     if ( FindNext( aUri , index ) >= 0 )
       
   504         {
       
   505         return ETrue;
       
   506         }
       
   507     return EFalse;
       
   508     }
       
   509 
       
   510 
       
   511 
       
   512 // -----------------------------------------------------------------------------
       
   513 // CEndTimeFactory::Remove
       
   514 // Remove a specific content uri from factory
       
   515 // (other items were commented in a header).
       
   516 // -----------------------------------------------------------------------------
       
   517 //
       
   518 void CEndTimeFactory::Remove(
       
   519         const TDesC8& aUri ,
       
   520         const TUint8& aPermType ,
       
   521         const TUint8& aRegType ,
       
   522         const TUint8& aAutoType )
       
   523     {
       
   524 #ifdef _DRM_TESTING
       
   525     TRAPD( err , WriteL(_L("Remove")) );
       
   526     TRAP( err , WriteL(aUri) );
       
   527     TRAP( err , WriteCurrentTimeL() );
       
   528 #endif
       
   529 
       
   530     TInt index = Find( aUri , aPermType , aRegType , aAutoType );
       
   531     if ( index >= 0 )
       
   532         {
       
   533         iList[index]->DecreaseRegTimes();
       
   534 
       
   535 #ifdef _DRM_TESTING
       
   536         TBuf<20> buf;
       
   537         _LIT( KRegTimes , "RegTimes: %d");
       
   538         buf.Format( KRegTimes , iList[index]->RegTimes() );
       
   539         TRAP( err , WriteL(buf) );
       
   540 #endif
       
   541 
       
   542         if ( iList[index]->RegTimes()<1 )
       
   543             {
       
   544 #ifdef _DRM_TESTING
       
   545             TRAPD( err , WriteL(_L("Removing succeeded!")) );
       
   546             TRAP( err , WriteCurrentTimeL() );
       
   547 #endif
       
   548             delete iList[index];
       
   549             iList[index] = NULL;
       
   550             iList.Remove(index);
       
   551             iList.Compress();
       
   552             }
       
   553         }
       
   554     }
       
   555 
       
   556 // -----------------------------------------------------------------------------
       
   557 // CEndTimeFactory::RemoveAllL
       
   558 // Remove a specific content uri from factory
       
   559 // (other items were commented in a header).
       
   560 // -----------------------------------------------------------------------------
       
   561 //
       
   562 void CEndTimeFactory::RemoveAllL()
       
   563     {
       
   564 #ifdef _DRM_TESTING
       
   565     WriteL(_L("RemoveAllL"));
       
   566     WriteCurrentTimeL();
       
   567 #endif
       
   568     TInt i = 0;
       
   569     for(;i<iList.Count();i++)
       
   570         {
       
   571         iServer.RemoveNotificationL( *iList[i]->Uri() );
       
   572         }
       
   573     iList.ResetAndDestroy();
       
   574     }
       
   575 
       
   576 // -----------------------------------------------------------------------------
       
   577 // CEndTimeFactory::RunL
       
   578 // from CActive
       
   579 // (other items were commented in a header).
       
   580 // -----------------------------------------------------------------------------
       
   581 //
       
   582 void CEndTimeFactory::RunL()
       
   583     {
       
   584 #ifdef _DRM_TESTING
       
   585     //test code start
       
   586     WriteL(_L("RunL"));
       
   587     WriteL(_L("Current Time"));
       
   588     WriteCurrentTimeL();
       
   589     WriteL(_L("iAlarm"));
       
   590     WriteTimeL(iAlarm);
       
   591     //test code end
       
   592 #endif
       
   593 
       
   594     TInt error( iStatus.Int() ); // to be used in ResetAlarm
       
   595     if ( error == KErrOverflow )
       
   596         {
       
   597         // Overflow occured, next trial after desired interval / 2
       
   598         // Let's update known overflowed alarm time 1st
       
   599         iOverflowedAlarm = iAlarm;
       
   600         TInt64 alarm( iAlarm.Int64() );
       
   601         iAlarm.HomeTime();
       
   602         TInt64 h( iAlarm.Int64() );
       
   603         TTime newAlarm( alarm / 2 + h / 2 );
       
   604         iAlarm = newAlarm;
       
   605         }
       
   606 
       
   607     HandleExpiredL();
       
   608     ResetAlarm();
       
   609     }
       
   610 
       
   611 // -----------------------------------------------------------------------------
       
   612 // CEndTimeFactory::DoCancel
       
   613 // From CActive
       
   614 // (other items were commented in a header).
       
   615 // -----------------------------------------------------------------------------
       
   616 //
       
   617 void CEndTimeFactory::DoCancel()
       
   618     {
       
   619 #ifdef _DRM_TESTING
       
   620     TRAPD( err , WriteL(_L("DoCancel")) );
       
   621     TRAP( err , WriteCurrentTimeL() );
       
   622 #endif
       
   623     CTimer::DoCancel();
       
   624     }
       
   625 
       
   626 // -----------------------------------------------------------------------------
       
   627 // CEndTimeFactory::Find
       
   628 // Find index for content uri
       
   629 // (other items were commented in a header).
       
   630 // -----------------------------------------------------------------------------
       
   631 //
       
   632 TInt CEndTimeFactory::Find(
       
   633         const TDesC8& aUri ,
       
   634         const TUint8& aPermType ,
       
   635         const TUint8& aRegType ,
       
   636         const TUint8& aAutoType )
       
   637     {
       
   638 #ifdef _DRM_TESTING
       
   639     TRAPD( err , WriteL(_L("Find(aUri,aPermType,aRegType,aAutoType)")) );
       
   640     TRAP( err , WriteL(aUri) );
       
   641     TRAP( err , WriteCurrentTimeL() );
       
   642 #endif
       
   643 
       
   644     TInt i = 0;
       
   645     for ( ; i < iList.Count() ; i++ )
       
   646         {
       
   647         if ( iList[i]->Uri()->Compare( aUri ) == 0 )
       
   648             {
       
   649             if ( (iList[i]->PermType()==aPermType) )
       
   650                 {
       
   651                 if ( (iList[i]->RegType()==aRegType) )
       
   652                     {
       
   653                     if ( (iList[i]->AutoType()==aAutoType) )
       
   654                         {
       
   655                         return i;
       
   656                         }
       
   657                     }
       
   658                 }
       
   659             }
       
   660         }
       
   661     return KErrNotFound;
       
   662     }
       
   663 
       
   664 // -----------------------------------------------------------------------------
       
   665 // CEndTimeFactory::FindNext
       
   666 // Find index for content uri
       
   667 // (other items were commented in a header).
       
   668 // -----------------------------------------------------------------------------
       
   669 //
       
   670 TInt CEndTimeFactory::FindNext(const TDesC8& aUri,const TUint8& aIndex)
       
   671     {
       
   672 #ifdef _DRM_TESTING
       
   673     TRAPD( err , WriteL(_L("FindNext(aUri,aIndex)")) );
       
   674     TRAP( err , WriteL(aUri) );
       
   675     TRAP( err , WriteCurrentTimeL() );
       
   676 #endif
       
   677 
       
   678     TInt i = aIndex;
       
   679     for ( ; i < iList.Count() ; i++ )
       
   680         {
       
   681         if ( iList[i]->Uri()->Compare( aUri ) == 0 )
       
   682             {
       
   683             return i;
       
   684             }
       
   685         }
       
   686     return KErrNotFound;
       
   687     }
       
   688 
       
   689 // -----------------------------------------------------------------------------
       
   690 // CEndTimeFactory::FindNext
       
   691 // Find index for content uri
       
   692 // (other items were commented in a header).
       
   693 // -----------------------------------------------------------------------------
       
   694 //
       
   695 TInt CEndTimeFactory::FindNext(
       
   696         const TDesC8& aUri ,
       
   697         const TUint8& aPermType ,
       
   698         const TUint8& aRegType ,
       
   699         const TUint8& aIndex )
       
   700     {
       
   701 #ifdef _DRM_TESTING
       
   702     TRAPD( err , WriteL(_L("FindNext(aUri,aPermType,aRegType,aIndex)")) );
       
   703     TRAP( err , WriteL(aUri) );
       
   704     TRAP( err , WriteCurrentTimeL() );
       
   705 #endif
       
   706 
       
   707     TInt i = aIndex;
       
   708     for ( ; i < iList.Count() ; i++ )
       
   709         {
       
   710         if ( iList[i]->Uri()->Compare( aUri ) == 0 )
       
   711             {
       
   712             if ( (iList[i]->PermType()==aPermType) )
       
   713                 {
       
   714                 if ( (iList[i]->RegType()==aRegType) )
       
   715                     {
       
   716                     return i;
       
   717                     }
       
   718                 }
       
   719             }
       
   720         }
       
   721     return KErrNotFound;
       
   722     }
       
   723 
       
   724 // -----------------------------------------------------------------------------
       
   725 // CEndTimeFactory::Exists
       
   726 // Find index for content uri
       
   727 // (other items were commented in a header).
       
   728 // -----------------------------------------------------------------------------
       
   729 //
       
   730 TInt CEndTimeFactory::Exists(
       
   731         const TDesC8& aUri ,
       
   732         const TUint8& aRegType,
       
   733         const TUint8& aAutoType)
       
   734     {
       
   735 #ifdef _DRM_TESTING
       
   736     TRAPD( err , WriteL(_L("Exists(aUri,aRegType)")) );
       
   737     TRAP( err , WriteL(aUri) );
       
   738     TRAP( err , WriteCurrentTimeL() );
       
   739 #endif
       
   740 
       
   741     TInt i = 0;
       
   742     for ( ; i < iList.Count() ; i++ )
       
   743         {
       
   744         if ( iList[i]->Uri()->Compare( aUri ) == 0 )
       
   745             {
       
   746             if ( (iList[i]->RegType()==aRegType) )
       
   747                 {
       
   748                 if ( (iList[i]->AutoType()==aAutoType) )
       
   749                     {
       
   750                     return i;
       
   751                     }
       
   752                 }
       
   753             }
       
   754         }
       
   755     return KErrNotFound;
       
   756     }
       
   757 
       
   758 
       
   759 // -----------------------------------------------------------------------------
       
   760 // CEndTimeFactory::ResetAlarm
       
   761 // Reset the alarm according to the end time based content list
       
   762 // (other items were commented in a header).
       
   763 // -----------------------------------------------------------------------------
       
   764 //
       
   765 void CEndTimeFactory::ResetAlarm()
       
   766     {
       
   767 #ifdef _DRM_TESTING
       
   768     TRAPD( err , WriteL(_L("ResetAlarm")) );
       
   769     TRAP( err , WriteCurrentTimeL() );
       
   770 #endif
       
   771     TBool out = EFalse;
       
   772     TInt i = 0;
       
   773     TTime home;
       
   774 
       
   775     TTime oldAlarm( iAlarm ); // for overflow checks
       
   776 
       
   777     iAlarm.HomeTime();
       
   778     ResetExpired();
       
   779 
       
   780     for ( ; i < iList.Count() && !out ; i++ )
       
   781         {
       
   782         if ( !iList[i]->IsExpired() )
       
   783             {
       
   784             iAlarm = iList[i]->EndTime();
       
   785             out = ETrue;
       
   786             }
       
   787         }
       
   788     for ( ; i < iList.Count() ; i++ )
       
   789         {
       
   790         if ( !iList[i]->IsExpired() )
       
   791             {
       
   792             if ( iList[i]->EndTime() < iAlarm )
       
   793                 {
       
   794                 iAlarm = iList[i]->EndTime();
       
   795 #ifdef _DRM_TESTING
       
   796                 //test code start
       
   797                 TRAP( err , WriteL(_L("ResetAlarm->Change iAlarm")) );
       
   798                 TRAP( err , WriteL(iList[i]->Uri()->Des()) );
       
   799                 TRAP( err , WriteCurrentTimeL() );
       
   800                 TRAP( err , WriteL(_L("Change Time")) );
       
   801                 TRAP( err , WriteTimeL(iAlarm) );
       
   802                 //test code end
       
   803 #endif
       
   804                 }
       
   805             }
       
   806 
       
   807         }
       
   808     Cancel();
       
   809     home.HomeTime();
       
   810 
       
   811     //
       
   812     if ( iAlarm >= iOverflowedAlarm )
       
   813         {
       
   814         // let's keep old alarm, if desired alarm is later than last known overflow
       
   815         iAlarm = oldAlarm;
       
   816         }
       
   817     //
       
   818 
       
   819     if ( iAlarm > home && iAlarm != Time::MaxTTime() )
       
   820         {
       
   821 #ifdef _DRM_TESTING
       
   822         //test code start
       
   823         TRAP( err , WriteL(_L("ResetAlarm->Trigger iAlarm")) );
       
   824         TRAP( err , WriteCurrentTimeL() );
       
   825         TRAP( err , WriteL(_L("Trigger Time")) );
       
   826         TRAP( err , WriteTimeL(iAlarm) );
       
   827         //test code end
       
   828 #endif
       
   829         CTimer::At(iAlarm+TTimeIntervalSeconds(KInformingDelay));
       
   830         }
       
   831     }
       
   832 
       
   833 // -----------------------------------------------------------------------------
       
   834 // CEndTimeFactory::ResetExpired
       
   835 // Reset the expiration status for all end time items
       
   836 // (other items were commented in a header).
       
   837 // -----------------------------------------------------------------------------
       
   838 //
       
   839 void CEndTimeFactory::ResetExpired()
       
   840     {
       
   841 #ifdef _DRM_TESTING
       
   842     TRAPD( err , WriteL(_L("ResetExpired")) );
       
   843     TRAP( err , WriteCurrentTimeL() );
       
   844     TRAP( err , WriteL(_L("Time in the List")) );
       
   845 #endif
       
   846     TInt i = 0;
       
   847     TTime home;
       
   848     home.HomeTime();
       
   849     for ( ; i<iList.Count() ; i++ )
       
   850         {
       
   851 #ifdef _DRM_TESTING
       
   852         //test code start
       
   853         TRAP( err , WriteTimeL(iList[i]->EndTime()) );
       
   854         //test code end
       
   855 #endif
       
   856         if ( !iList[i]->IsExpired() )
       
   857             {
       
   858             if ( iList[i]->EndTime() <= home )
       
   859                 {
       
   860                 iList[i]->SetCount(-1);
       
   861                 iList[i]->SetIsExpired(ETrue);
       
   862                 }
       
   863             }
       
   864         else
       
   865             {
       
   866             if ( iList[i]->EndTime() > home )
       
   867                 {
       
   868                 iList[i]->SetCount(-1);
       
   869                 iList[i]->SetIsExpired(EFalse);
       
   870                 }
       
   871             }
       
   872 
       
   873         }
       
   874     }
       
   875 
       
   876 // -----------------------------------------------------------------------------
       
   877 // CEndTimeFactory::HandleExpiredL
       
   878 // Reset the expiration status for all end time items
       
   879 // (other items were commented in a header).
       
   880 // -----------------------------------------------------------------------------
       
   881 //
       
   882 void CEndTimeFactory::HandleExpiredL()
       
   883     {
       
   884 #ifdef _DRM_TESTING
       
   885     WriteL(_L("HandleExpiredL"));
       
   886     WriteCurrentTimeL();
       
   887 #endif
       
   888     TInt i = 0;
       
   889     static TBool firstBoot = ETrue;
       
   890     ResetExpired();
       
   891     for ( ; i<iList.Count() ; i++ )
       
   892         {
       
   893         if ( iList[i]->IsExpired() && iList[i]->Count() < 0 )
       
   894             {
       
   895             iList[i]->IncreaseCount();
       
   896             iServer.HandleExpiredL( iList[i] );
       
   897             }
       
   898         if ( firstBoot && iList[i]->IsExpired() ) // Count 0 means do not give
       
   899             {                                      // note in Profiles
       
   900             iList[i]->IncreaseCount();             // If previous count is 0
       
   901             iServer.HandleExpiredL( iList[i] );    // Give a note with value 1
       
   902             }
       
   903         }
       
   904     firstBoot = EFalse;
       
   905     }
       
   906 
       
   907 // -----------------------------------------------------------------------------
       
   908 // CEndTimeFactory::HandleIdleL
       
   909 // Handle about to expire- and expired- cases after idle
       
   910 // (other items were commented in a header).
       
   911 // -----------------------------------------------------------------------------
       
   912 //
       
   913 void CEndTimeFactory::HandleIdleL()
       
   914     {
       
   915 #ifdef _DRM_TESTING
       
   916     WriteL(_L("HandleIdleL"));
       
   917     WriteCurrentTimeL();
       
   918 #endif
       
   919     TInt i = 0;
       
   920     iServer.UpdateL();
       
   921     ResetExpired();
       
   922     for ( ; i<iList.Count() ; i++ )
       
   923         {
       
   924         if ( iList[i]->IsExpired() )
       
   925             {
       
   926             iList[i]->IncreaseCount();
       
   927             iServer.HandleExpiredL( iList[i] );
       
   928             if ( iList[i]->Count() == 0 )           // Count 0 means do not give
       
   929                 {                                   // note in Profiles
       
   930                 iList[i]->IncreaseCount();          // If previous count is 0
       
   931                 iServer.HandleExpiredL( iList[i] ); // Give a note with value 1
       
   932                 }
       
   933             }
       
   934         }
       
   935     }
       
   936 
       
   937 
       
   938 
       
   939 // -----------------------------------------------------------------------------
       
   940 // CEndTimeFactory::ResetRegTimes
       
   941 // reset the registered times for one specific content in the list
       
   942 // -----------------------------------------------------------------------------
       
   943 //
       
   944 void CEndTimeFactory::ResetRegTimes(
       
   945         const TDesC8& aUri ,
       
   946         const TUint8& aPermType ,
       
   947         const TUint8& aRegType ,
       
   948         const TUint8& aAutoType ,
       
   949         const TUint8& aRegTimes )
       
   950 
       
   951     {
       
   952 #ifdef _DRM_TESTING
       
   953     TRAPD( err , WriteL(_L("ResetRegTimes")) );
       
   954     TRAP( err , WriteL(aUri) );
       
   955     TRAP( err , WriteCurrentTimeL() );
       
   956 #endif
       
   957 
       
   958     TInt index = Find( aUri , aPermType , aRegType , aAutoType );
       
   959     if ( index >= 0 )
       
   960         {
       
   961         iList[index]->SetRegTimes(aRegTimes);
       
   962         }
       
   963     }
       
   964 
       
   965 // -----------------------------------------------------------------------------
       
   966 // CEndTimeFactory::ExternalizeL
       
   967 // return ETrue if item is about to expire
       
   968 // (other items were commented in a header).
       
   969 // -----------------------------------------------------------------------------
       
   970 //
       
   971 void CEndTimeFactory::ExternalizeL( RWriteStream& aStream ) const
       
   972     {
       
   973 #ifdef _DRM_TESTING
       
   974     WriteL(_L("ExternalizeL"));
       
   975     WriteCurrentTimeL();
       
   976 #endif
       
   977     TInt i = 0;
       
   978     HBufC8* buf = NULL;
       
   979     aStream.WriteUint8L(iList.Count());
       
   980     for ( ; i<iList.Count() ; i++ )
       
   981         {
       
   982         buf = iList[i]->Uri();
       
   983         aStream.WriteUint8L(buf->Length());
       
   984         aStream.WriteL(buf->Des());
       
   985         aStream.WriteUint8L(iList[i]->PermType());
       
   986         aStream.WriteUint8L(iList[i]->RegType());
       
   987         aStream.WriteUint8L(iList[i]->AutoType());
       
   988         aStream.WriteInt32L(iList[i]->RegTimes());
       
   989 #ifdef _DRM_TESTING
       
   990         //test code start
       
   991         WriteL(buf->Des());
       
   992         //test code end
       
   993 #endif
       
   994         aStream.CommitL();
       
   995         buf = NULL;
       
   996         }
       
   997     }
       
   998 
       
   999 // -----------------------------------------------------------------------------
       
  1000 // CEndTimeFactory::InternalizeL
       
  1001 // return ETrue if item is about to expire
       
  1002 // (other items were commented in a header).
       
  1003 // -----------------------------------------------------------------------------
       
  1004 //
       
  1005 void CEndTimeFactory::InternalizeL( RReadStream& aStream )
       
  1006     {
       
  1007 #ifdef _DRM_TESTING
       
  1008     WriteL(_L("InternalizeL"));
       
  1009     WriteCurrentTimeL();
       
  1010 #endif
       
  1011 
       
  1012     TPtr8 ptr( NULL , 0 , 0 );
       
  1013     TUint amount = 0;
       
  1014     TUint len = 0;
       
  1015     TUint8 permType = 0;
       
  1016     TUint8 regType = 0;
       
  1017     TUint8 autoType = 0;
       
  1018     TInt regTimes = 0;
       
  1019     HBufC8* buf = NULL;
       
  1020 
       
  1021     amount = aStream.ReadUint8L();
       
  1022 
       
  1023 
       
  1024     for ( ; amount>0 ; amount-- )
       
  1025         {
       
  1026         len = aStream.ReadUint8L();
       
  1027 #ifdef _DRM_TESTING
       
  1028         //test code start
       
  1029         TBuf<20>amountBuf;
       
  1030         _LIT(KLength , "Length:%d");
       
  1031         amountBuf.Format(KLength , len);
       
  1032         WriteL(amountBuf);
       
  1033         //test code end
       
  1034 #endif
       
  1035         buf = HBufC8::NewLC(len);
       
  1036         ptr.Set(buf->Des());
       
  1037         ptr.SetLength(len);
       
  1038         aStream.ReadL(ptr , len);
       
  1039 
       
  1040 #ifdef _DRM_TESTING
       
  1041         //test code start
       
  1042         WriteL(ptr);
       
  1043         //test code end
       
  1044 #endif
       
  1045         permType = aStream.ReadUint8L();
       
  1046         regType = aStream.ReadUint8L();
       
  1047         autoType = aStream.ReadUint8L();
       
  1048 #ifdef _DRM_TESTING
       
  1049         //test code start
       
  1050         _LIT(KPermType , "PermType:%d");
       
  1051         amountBuf.Format(KPermType , permType);
       
  1052         WriteL(amountBuf);
       
  1053         _LIT(KRegType , "RegType:%d");
       
  1054         amountBuf.Format(KRegType , regType);
       
  1055         WriteL(amountBuf);
       
  1056         _LIT(KAutoType , "AutoType:%d");
       
  1057         amountBuf.Format(KAutoType , autoType);
       
  1058         WriteL(amountBuf);
       
  1059 
       
  1060         //test code end
       
  1061 #endif
       
  1062         iServer.RegisterL( ptr , permType , regType , autoType );
       
  1063 
       
  1064         regTimes = aStream.ReadInt32L();
       
  1065 #ifdef _DRM_TESTING
       
  1066         //test code start
       
  1067         _LIT(KRegTimes , "Times:%d");
       
  1068         amountBuf.Format(KRegTimes , regTimes);
       
  1069         WriteL(amountBuf);
       
  1070         //test code end
       
  1071 #endif
       
  1072         ResetRegTimes( ptr , permType , regType , autoType , regTimes );
       
  1073         CleanupStack::PopAndDestroy(buf);
       
  1074         }
       
  1075     }
       
  1076 
       
  1077 // ========================== OTHER EXPORTED FUNCTIONS =========================
       
  1078 //  End of File