omadrm/drmhelper/drmhelperserver/src/ConfigObserver.cpp
changeset 0 95b198f216e5
equal deleted inserted replaced
-1:000000000000 0:95b198f216e5
       
     1 /*
       
     2 * Copyright (c) 2002 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:  observe the configuration change of DHS through Cenrep
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include    <e32base.h>
       
    22 #include    <e32std.h>
       
    23 #include    <f32file.h>
       
    24 #include    <centralrepository.h>
       
    25 #include    "ConfigObserver.h"
       
    26 #include    "DRMHelperCommon.h"
       
    27 #include    "DRMHelperServer.h"
       
    28 #include    "DRMHelperServerConfigInternalCRKeys.h"
       
    29 // EXTERNAL DATA STRUCTURES
       
    30 // EXTERNAL FUNCTION PROTOTYPES  
       
    31 // CONSTANTS
       
    32 // MACROS
       
    33 // LOCAL CONSTANTS AND MACROS
       
    34 // MODULE DATA STRUCTURES
       
    35 // LOCAL FUNCTION PROTOTYPES
       
    36 // FORWARD DECLARATIONS
       
    37 
       
    38 // ============================= LOCAL FUNCTIONS ===============================
       
    39 #ifdef _DRM_TESTING
       
    40 LOCAL_C void WriteFileL( const TDesC8& text , RFs &aFs , const TDesC& aName )
       
    41     {
       
    42     RFile file;
       
    43     TInt size;
       
    44     User::LeaveIfError( file.Open( aFs, aName , EFileWrite ) );
       
    45     CleanupClosePushL( file );
       
    46     User::LeaveIfError( file.Size( size ) );
       
    47     User::LeaveIfError( file.Write( size, text ) );
       
    48     CleanupStack::PopAndDestroy(&file); //file
       
    49     }
       
    50 
       
    51 LOCAL_C void WriteLogL( const TDesC8& text , RFs &aFs )
       
    52     {
       
    53     _LIT( KLogFile , "c:\\COLog.txt" );
       
    54     WriteFileL( text , aFs , KLogFile );
       
    55     }
       
    56 
       
    57 LOCAL_C void CreateLogL()
       
    58     {
       
    59     RFs fs;
       
    60     User::LeaveIfError(fs.Connect());
       
    61     CleanupClosePushL(fs);
       
    62     RFile file;
       
    63     User::LeaveIfError( file.Replace( fs , _L("c:\\COLog.txt") , EFileWrite ) );
       
    64     file.Close();
       
    65     CleanupStack::PopAndDestroy(&fs); //fs
       
    66     }
       
    67 
       
    68 LOCAL_C void WriteL( const TDesC& aText )
       
    69     {
       
    70     RFs fs;
       
    71     User::LeaveIfError( fs.Connect() );
       
    72     CleanupClosePushL(fs);
       
    73     HBufC8* text = HBufC8::NewLC(1000);
       
    74     TPtr8 textptr(text->Des() );
       
    75     textptr.Append( aText );
       
    76     textptr.Append(_L( "\r\n" ));
       
    77     WriteLogL(textptr , fs);
       
    78     CleanupStack::PopAndDestroy(text);
       
    79     CleanupStack::PopAndDestroy(&fs); //fs
       
    80     }
       
    81 
       
    82 LOCAL_C void WriteCurrentTimeL()
       
    83     {
       
    84     RFs fs;
       
    85     User::LeaveIfError( fs.Connect() );
       
    86     CleanupClosePushL(fs);
       
    87     HBufC8* text = HBufC8::NewLC(100);
       
    88     TPtr8 textptr(text->Des() );
       
    89 // Date and Time display
       
    90     TTime time;
       
    91     time.HomeTime();
       
    92     TBuf<256> dateString;
       
    93     _LIT(KDate,"%*E%*D%X%*N%*Y %1 %2 '%3");
       
    94     time.FormatL(dateString,KDate);
       
    95     textptr.Append(_L( "\r\n\t\tData:\t" ) );
       
    96     textptr.Append( dateString );
       
    97     _LIT(KTime,"%-B%:0%J%:1%T%:2%S%:3%+B");
       
    98     time.FormatL(dateString,KTime);
       
    99     textptr.Append(_L( "\r\n\t\tTime:\t" ) );
       
   100     textptr.Append( dateString );
       
   101     textptr.Append(_L( "\r\n" ) );
       
   102     textptr.Append(_L( "\r\n" ) );
       
   103     WriteLogL(textptr , fs);
       
   104     CleanupStack::PopAndDestroy(text);
       
   105     CleanupStack::PopAndDestroy(&fs); //fs
       
   106     }
       
   107 #endif
       
   108 
       
   109 // ============================ MEMBER FUNCTIONS ===============================
       
   110 
       
   111 // -----------------------------------------------------------------------------
       
   112 // CConfigObserver::CConfigObserver
       
   113 // C++ default constructor can NOT contain any code, that
       
   114 // might leave.
       
   115 // -----------------------------------------------------------------------------
       
   116 //
       
   117 CConfigObserver::CConfigObserver( CDRMHelperServer& aServer ):
       
   118                     CActive(CActive::EPriorityStandard),
       
   119                         iServer( aServer )
       
   120     {
       
   121     CActiveScheduler::Add( this );
       
   122     }
       
   123 
       
   124 // -----------------------------------------------------------------------------
       
   125 // CConfigObserver::ConstructL
       
   126 // Symbian 2nd phase constructor can leave.
       
   127 // -----------------------------------------------------------------------------
       
   128 //
       
   129 void CConfigObserver::ConstructL()
       
   130     {
       
   131 #ifdef _DRM_TESTING
       
   132     CreateLogL(); //test
       
   133 #endif
       
   134 
       
   135     iRepository = CRepository::NewL(KCRUidDRMHelperServerConfig);
       
   136     }
       
   137                     
       
   138 // -----------------------------------------------------------------------------
       
   139 // CConfigObserver::NewL
       
   140 // Two-phased constructor.
       
   141 // -----------------------------------------------------------------------------
       
   142 //
       
   143 CConfigObserver* CConfigObserver::NewL(CDRMHelperServer& aServer)
       
   144     {
       
   145     CConfigObserver* self = new( ELeave ) CConfigObserver(aServer);
       
   146     
       
   147     CleanupStack::PushL( self );
       
   148     self->ConstructL();
       
   149     CleanupStack::Pop(self);
       
   150 
       
   151     return self;
       
   152     }
       
   153 
       
   154     
       
   155 // Destructor
       
   156 CConfigObserver::~CConfigObserver()
       
   157     {
       
   158 #ifdef _DRM_TESTING
       
   159     TRAPD( err , WriteL(_L("CConfigObserver-Destruct")) ); 
       
   160     TRAP( err , WriteCurrentTimeL() );
       
   161 #endif
       
   162     Cancel();
       
   163     delete iRepository;
       
   164     }
       
   165 
       
   166 
       
   167 // -----------------------------------------------------------------------------
       
   168 // CConfigObserver::StartL
       
   169 // Start the system agent to listen to the event
       
   170 // (other items were commented in a header).
       
   171 // -----------------------------------------------------------------------------
       
   172 //
       
   173 void CConfigObserver::StartL()
       
   174     {
       
   175 #ifdef _DRM_TESTING
       
   176     WriteL(_L("Start"));
       
   177     WriteCurrentTimeL();
       
   178 #endif
       
   179     Cancel();
       
   180     User::LeaveIfError(iRepository->NotifyRequest( KDRMHSInformingInterval , iStatus ));
       
   181     SetActive();
       
   182     }
       
   183 
       
   184 
       
   185 // -----------------------------------------------------------------------------
       
   186 // CConfigObserver::RunL
       
   187 // from CActive
       
   188 // (other items were commented in a header).
       
   189 // -----------------------------------------------------------------------------
       
   190 //
       
   191 void CConfigObserver::RunL()
       
   192     {
       
   193 #ifdef _DRM_TESTING 
       
   194     //test code start
       
   195     WriteL(_L("RunL"));
       
   196     WriteCurrentTimeL();
       
   197     //test code end
       
   198 #endif
       
   199     
       
   200     
       
   201     TInt err( iStatus.Int() );
       
   202     TInt val = KAboutToExpireInterval;
       
   203     if (err == KErrNone)
       
   204         {
       
   205         StartL();
       
   206         User::LeaveIfError(iRepository->Get( KDRMHSInformingInterval , val ));
       
   207         iServer.SetInformingInterval(val);
       
   208         }
       
   209     else if (err != KErrCancel || 
       
   210     NCentralRepositoryConstants::KInvalidNotificationId
       
   211     )
       
   212         {
       
   213         StartL();
       
   214         }
       
   215      
       
   216     }
       
   217 
       
   218 // -----------------------------------------------------------------------------
       
   219 // CConfigObserver::DoCancel
       
   220 // From CActive
       
   221 // (other items were commented in a header).
       
   222 // -----------------------------------------------------------------------------
       
   223 //
       
   224 void CConfigObserver::DoCancel()
       
   225     {
       
   226 #ifdef _DRM_TESTING
       
   227     TRAPD( err , WriteL(_L("DoCancel")) );
       
   228     TRAP( err , WriteCurrentTimeL() );
       
   229 #endif
       
   230     iRepository->NotifyCancel(KDRMHSInformingInterval);
       
   231     }
       
   232 
       
   233 // ========================== OTHER EXPORTED FUNCTIONS =========================
       
   234 //  End of File