remotelock/RemotelockSettings/Src/RemoteLockSettings.cpp
changeset 0 164170e6151a
equal deleted inserted replaced
-1:000000000000 0:164170e6151a
       
     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:  Implementation of RemoteLock Settings API
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES 
       
    21 #include <RemoteLockSettings.h>
       
    22 #include <hash.h>
       
    23 #include <centralrepository.h>
       
    24 #include "RemoteLockPrivateCRKeys.h"
       
    25 #include "RemoteLockTrace.h"
       
    26 
       
    27 // EXTERNAL DATA STRUCTURES
       
    28 
       
    29 // EXTERNAL FUNCTION PROTOTYPES  
       
    30 
       
    31 // CONSTANTS
       
    32 _LIT( KRLockLeadingHexZero, "0" );
       
    33 
       
    34 // MACROS
       
    35 
       
    36 // LOCAL CONSTANTS AND MACROS
       
    37 
       
    38 // MODULE DATA STRUCTURES
       
    39 
       
    40 // LOCAL FUNCTION PROTOTYPES
       
    41 
       
    42 // FORWARD DECLARATIONS
       
    43 
       
    44 
       
    45 // ============================ MEMBER FUNCTIONS ===============================
       
    46 // -----------------------------------------------------------------------------
       
    47 // CRemoteLockSettings::NewL
       
    48 // Two-Phased constructor.
       
    49 // -----------------------------------------------------------------------------
       
    50 //
       
    51 EXPORT_C CRemoteLockSettings* CRemoteLockSettings::NewL( 
       
    52     MRemoteLockSettingsHandler* aHandler )
       
    53     {
       
    54     RL_TRACE_PRINT(" [ rlsetting.dll ] NewL() ");
       
    55     
       
    56     CRemoteLockSettings* self = CRemoteLockSettings::NewLC( aHandler );
       
    57          
       
    58     CleanupStack::Pop( self );
       
    59     RL_TRACE_PRINT(" [ rlsetting.dll ] exit NewL() ");
       
    60     return self;
       
    61     }
       
    62 
       
    63 
       
    64 // -----------------------------------------------------------------------------
       
    65 // CRemoteLockSettings::NewLC
       
    66 // Two-Phased constructor.
       
    67 // -----------------------------------------------------------------------------
       
    68 //
       
    69 EXPORT_C CRemoteLockSettings* CRemoteLockSettings::NewLC( 
       
    70     MRemoteLockSettingsHandler* aHandler )
       
    71     {
       
    72     
       
    73     CRemoteLockSettings* self = new ( ELeave ) CRemoteLockSettings( aHandler );
       
    74     
       
    75     CleanupStack::PushL( self );
       
    76     
       
    77     self->ConstructL();
       
    78     return self;
       
    79     }
       
    80 
       
    81 
       
    82 // ----------------------------------------------------------------------------
       
    83 // Destructor
       
    84 // ----------------------------------------------------------------------------
       
    85 EXPORT_C CRemoteLockSettings::~CRemoteLockSettings()
       
    86     {
       
    87     if ( iNotifyHandler )
       
    88         {
       
    89         iNotifyHandler->StopListening();
       
    90         delete iNotifyHandler;
       
    91         }
       
    92 
       
    93     delete iSession;
       
    94     }
       
    95 
       
    96 
       
    97 // ----------------------------------------------------------------------
       
    98 // CRemoteLockSettings::GetLockCode
       
    99 // Gets remote lock code
       
   100 // Returns: ETrue
       
   101 //          EFalse  
       
   102 // ----------------------------------------------------------------------
       
   103 //
       
   104 EXPORT_C TBool CRemoteLockSettings::GetLockCode( TDes& aLockCode ) const                                               
       
   105     {
       
   106     RL_TRACE_PRINT(" [ rlsetting.dll ] Getlockcode() ");
       
   107     TInt err = iSession->Get( KRLLockCode, aLockCode ); 
       
   108     RL_TRACE_PRINT(" [ rlsetting.dll ] exit Getlockcode() ");   
       
   109     return ( err == KErrNone );
       
   110     }
       
   111 
       
   112 
       
   113 // ----------------------------------------------------------------------
       
   114 // CRemoteLockSettings::GetLockCode
       
   115 // Gets remote lock code and trimmed remote lock code
       
   116 // Returns: ETrue
       
   117 //          EFalse  
       
   118 // ----------------------------------------------------------------------
       
   119 //
       
   120 EXPORT_C TBool CRemoteLockSettings::GetLockCode( 
       
   121     TDes& aLockCode, 
       
   122     TDes& aLockCodeTrimmed ) const 
       
   123     {
       
   124     RL_TRACE_PRINT(" [ rlsetting.dll ] Getlockcode() ");
       
   125     TInt err = iSession->Get( KRLLockCode, aLockCode );    
       
   126     
       
   127     if ( err != KErrNone )
       
   128         {
       
   129         return EFalse;
       
   130         }
       
   131     
       
   132     err = iSession->Get( KRLLockCode2, aLockCodeTrimmed );
       
   133     
       
   134     if ( err == KErrNone )
       
   135         {
       
   136         RL_TRACE_PRINT(" [ rlsetting.dll ] exit Getlockcode() noError");
       
   137         return ETrue;
       
   138         }
       
   139     else
       
   140         {
       
   141         RL_TRACE_PRINT(" [ rlsetting.dll ] exit Getlockcode() Error");
       
   142         return EFalse;
       
   143         }
       
   144     }
       
   145 
       
   146 
       
   147 // -----------------------------------------------------------------------------
       
   148 // CRemoteLockSettings::SetEnabled
       
   149 // Enables and sets remote lock code
       
   150 // Returns   ETrue
       
   151 //           EFalse  
       
   152 // -----------------------------------------------------------------------------
       
   153 //
       
   154 EXPORT_C TBool CRemoteLockSettings::SetEnabledL( const TDesC& aLockCode )
       
   155     {
       
   156     RL_TRACE_PRINT(" [ rlsetting.dll ] SetEnabledL() ");
       
   157     if ( aLockCode.Length() > KRLockMaxLockCodeLength )
       
   158        {
       
   159        return EFalse;
       
   160        }  
       
   161        
       
   162     // Creates MD5 object.
       
   163     CMD5* hash = CMD5::NewL();
       
   164     CleanupStack::PushL( hash );
       
   165 
       
   166     TBuf<KRLockMaxLockCodeLength> tempBuf;
       
   167     tempBuf = aLockCode;
       
   168        
       
   169     HBufC8* hashBuf = HBufC8::NewLC( KRLockMaxLockCodeLength * 2 );        
       
   170     TPtr8 tempPtr = hashBuf->Des();
       
   171     tempPtr.Copy( tempBuf );
       
   172        
       
   173     // Calculate the digest.
       
   174     TPtrC8 hashPtr = hash->Hash( *hashBuf );
       
   175     HBufC* storeBuf = HBufC::NewLC( hash->HashSize());     
       
   176     storeBuf->Des().Copy( hashPtr );
       
   177 
       
   178        // Convert digest into hexadecimal representation
       
   179     HBufC* hexStoreBuf = HBufC::NewLC( storeBuf->Length() * 2);
       
   180     TBuf<2> singleHex;
       
   181     TInt i = 0;
       
   182     for ( i = 0; i < storeBuf->Length(); i++ )
       
   183         {
       
   184         singleHex.Num( storeBuf->Des()[i], EHex );
       
   185         if ( singleHex.Length() == 1 )
       
   186             hexStoreBuf->Des().Append( KRLockLeadingHexZero );
       
   187         hexStoreBuf->Des().Append( singleHex );
       
   188         }
       
   189        
       
   190     // Saves the digest into repository file.
       
   191     TInt err = 0;
       
   192     err = iSession->Set( KRLLockCode, *hexStoreBuf );
       
   193        
       
   194     if ( err != KErrNone )
       
   195         {
       
   196         CleanupStack::PopAndDestroy( 4 ); 
       
   197         return EFalse;
       
   198         }
       
   199         
       
   200      // Then process the trimmed password the same way.
       
   201     tempBuf.TrimAll();
       
   202     tempPtr.Copy( tempBuf );
       
   203     // Calculate the digest.
       
   204     hash->Reset();
       
   205     TPtrC8 hashPtr1 = hash->Hash( *hashBuf );
       
   206     storeBuf->Des().Copy( hashPtr1 );
       
   207     // Convert digest into hexadecimal representation
       
   208     hexStoreBuf->Des().Zero();
       
   209     for ( i = 0; i < storeBuf->Length(); i++ )
       
   210         {
       
   211         singleHex.Num( storeBuf->Des()[i], EHex );
       
   212         if ( singleHex.Length() == 1 )
       
   213             hexStoreBuf->Des().Append( KRLockLeadingHexZero );
       
   214         hexStoreBuf->Des().Append( singleHex );
       
   215         }
       
   216         
       
   217     // Saves the digest into ini file.
       
   218     err = iSession->Set( KRLLockCode2, *hexStoreBuf );
       
   219         
       
   220     if ( err != KErrNone )
       
   221         {
       
   222         RL_TRACE_PRINT(" [ rlsetting.dll ] SetenabledL() exit Error ");
       
   223         CleanupStack::PopAndDestroy( 4 );
       
   224         return EFalse;
       
   225         }
       
   226     
       
   227     err = iSession->Set( KRLEnable, 1 );
       
   228         
       
   229     if ( err != KErrNone )
       
   230         {
       
   231         RL_TRACE_PRINT(" [ rlsetting.dll ] SetenabledL() exit Error ");
       
   232         CleanupStack::PopAndDestroy( 4 );
       
   233         return EFalse;
       
   234         }
       
   235     RL_TRACE_PRINT(" [ rlsetting.dll ] SetenabledL() exit no Error ");
       
   236     CleanupStack::PopAndDestroy( 4 );//MD5, 3 Heap buffer
       
   237     return ETrue;
       
   238     }
       
   239 
       
   240 
       
   241 // -----------------------------------------------------------------------------
       
   242 // CRemoteLockSettings::SetDisabled
       
   243 // Disables remote lock
       
   244 // Returns   ETrue
       
   245 //           EFalse  
       
   246 // -----------------------------------------------------------------------------
       
   247 //
       
   248 EXPORT_C TBool CRemoteLockSettings::SetDisabled()
       
   249     {
       
   250     RL_TRACE_PRINT(" [ rlsetting.dll ] SetDisabled() ");
       
   251     TInt err;
       
   252     err = iSession->Set( KRLEnable, 0 );
       
   253     RL_TRACE_PRINT(" [ rlsetting.dll ] exit SetDisabled() ");
       
   254     return ( err == KErrNone );
       
   255     }
       
   256 
       
   257 // -----------------------------------------------------------------------------
       
   258 // CRemoteLockSettings::GetEnabled
       
   259 // Returns whether remote lock is enabled or disabled
       
   260 // Returns    ETrue
       
   261 //            EFalse  
       
   262 // -----------------------------------------------------------------------------
       
   263 //
       
   264 EXPORT_C TBool CRemoteLockSettings::GetEnabled( TBool& aEnable ) const
       
   265     {
       
   266     RL_TRACE_PRINT(" [ rlsetting.dll ] GetEnabled() ");  
       
   267     TInt err;
       
   268     err = iSession->Get( KRLEnable, aEnable );
       
   269     RL_TRACE_PRINT(" [ rlsetting.dll ] Exit GetEnabled() ");
       
   270     return ( err == KErrNone );
       
   271     }
       
   272     
       
   273    
       
   274 // -----------------------------------------------------------------------------
       
   275 // CRemoteLockSettings::CompareLockCodes
       
   276 // Compares the given remote lock code to the stored one.
       
   277 // Returns   ETrue
       
   278 //           EFalse  
       
   279 // -----------------------------------------------------------------------------
       
   280 //
       
   281 EXPORT_C TBool CRemoteLockSettings::CompareLockCodesL( 
       
   282     const TDesC& aLockCode, 
       
   283     const TDesC& aCorrectLockCode ) const
       
   284     {
       
   285     RL_TRACE_PRINT(" [ rlsetting.dll ] CompareLockCodesL() ");
       
   286     if ( aLockCode.Length() > KRLockMaxLockCodeLength || 
       
   287         aCorrectLockCode.Length() > KRLockStoredLockCodeLength )
       
   288         {
       
   289         RL_TRACE_PRINT(" [ rlsetting.dll ] exit CompareLockCodeL() lockcode length is illegal ");
       
   290         return EFalse;
       
   291         }
       
   292 
       
   293     // Creates MD5 object.
       
   294     CMD5* hash = CMD5::NewL();
       
   295     CleanupStack::PushL( hash );
       
   296 
       
   297     HBufC8* hashBuf = HBufC8::NewLC( KRLockMaxLockCodeLength * 2 );
       
   298     TPtr8 tempPtr = hashBuf->Des();
       
   299     tempPtr.Copy( aLockCode );
       
   300 
       
   301     // Hashes data.
       
   302     TPtrC8 hashPtr = hash->Hash( *hashBuf );
       
   303 
       
   304     HBufC* lockCode = HBufC::NewLC( hash->HashSize() );
       
   305 
       
   306     // Copies hashPtr to lockCode.
       
   307     lockCode->Des().Copy( hashPtr );
       
   308 
       
   309     // Convert digest into hexadecimal representation
       
   310     HBufC* hexLockCode = HBufC::NewLC( lockCode->Length() * 2);
       
   311     TBuf<2> singleHex;
       
   312     for ( TInt i = 0; i < lockCode->Length(); i++ )
       
   313         {
       
   314         singleHex.Num( lockCode->Des()[i], EHex );
       
   315         if ( singleHex.Length() == 1 )
       
   316             hexLockCode->Des().Append( KRLockLeadingHexZero );
       
   317         hexLockCode->Des().Append( singleHex );
       
   318         }
       
   319 
       
   320     RL_TRACE_PRINT(" [ rlsetting.dll ] CompareLockCodeL() compare ");
       
   321     if ( hexLockCode->Compare( aCorrectLockCode ) == 0 )
       
   322         {
       
   323         // When two hash results matches each other.
       
   324         RL_TRACE_PRINT(" [ rlsetting.dll ] ComparelockcodeL() match ");
       
   325         CleanupStack::PopAndDestroy( 4 );
       
   326         return ETrue;
       
   327         }
       
   328     else
       
   329         {
       
   330         RL_TRACE_PRINT(" [ rlsetting.dll ] CompareLockCodeL() dismatch ");
       
   331         CleanupStack::PopAndDestroy( 4 );
       
   332         return EFalse;
       
   333         }
       
   334     }
       
   335 
       
   336 
       
   337 // -----------------------------------------------------------------------------
       
   338 // CRemoteLockSettings::RemoteLockNotifyL
       
   339 // Setup RemoteLock notifier
       
   340 // Returns:   ETrue
       
   341 //            EFalse
       
   342 // -----------------------------------------------------------------------------
       
   343 //
       
   344 EXPORT_C TBool CRemoteLockSettings::RemoteLockNotifyL( 
       
   345     const TBool aNotifyEnable ) 
       
   346     {
       
   347     RL_TRACE_PRINT(" [ rlsetting.dll ] RemotelockNotifyL() ");
       
   348     if ( !iNotifyHandler )
       
   349         {
       
   350         iNotifyHandler = CCenRepNotifyHandler::NewL( *this, *iSession ); 
       
   351         }
       
   352 
       
   353     if ( aNotifyEnable )
       
   354         {
       
   355         iNotifyHandler->StartListeningL();  
       
   356         }
       
   357     else
       
   358         {
       
   359         iNotifyHandler->StopListening();
       
   360         }
       
   361     RL_TRACE_PRINT(" [ rlsetting.dll ] exit RemotelockNotifyL() ");
       
   362     return ETrue;
       
   363     }
       
   364 
       
   365 // ----------------------------------------------------------------------------
       
   366 // CRemoteLockSettings::CRemoteLockSettings
       
   367 // default constructor
       
   368 // ----------------------------------------------------------------------------
       
   369 CRemoteLockSettings::CRemoteLockSettings( MRemoteLockSettingsHandler* aHandler ) 
       
   370     {    
       
   371     iHandler = aHandler;
       
   372     } 
       
   373     
       
   374       
       
   375 // -----------------------------------------------------------------------------
       
   376 // CRemoteLockSettings::ConstructL
       
   377 // Symbian 2nd phase constructor can leave
       
   378 //------------------------------------------------------------------------------
       
   379 //
       
   380 void CRemoteLockSettings::ConstructL()
       
   381     {
       
   382     RL_TRACE_PRINT(" [ rlsetting.dll ] ConstructL() ");  
       
   383     iSession = CRepository::NewL( KCRUidRemoteLock ); 
       
   384     RL_TRACE_PRINT(" [ rlsetting.dll ] exit ConstructL() ");
       
   385     }
       
   386     
       
   387 
       
   388 // -----------------------------------------------------------------------------
       
   389 // CRemoteLockSettings::HandleNotifyGeneric
       
   390 // Notify handler
       
   391 //
       
   392 // -----------------------------------------------------------------------------
       
   393 //
       
   394 void CRemoteLockSettings::HandleNotifyGeneric( 
       
   395     TUint32 /*aId*/ ) 
       
   396     {
       
   397     RL_TRACE_PRINT(" [ rlsetting.dll ] HandleNotifyGeneric() ");
       
   398     TRAPD( error, iHandler->HandleRemoteLockNotifyL() );
       
   399     error = error;
       
   400     RL_TRACE_PRINT(" [ rlsetting.dll ] exit HandleNotifyGeneric() ");
       
   401     }
       
   402 
       
   403