remotelock/RemoteLockEngine/Src/RemoteLock.cpp
changeset 0 164170e6151a
child 7 6e226572c5f0
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 Engine
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES 
       
    20 #include <smscmds.h>
       
    21 #include <smutset.h>
       
    22 #include <smuthdr.h>
       
    23 #include <e32property.h>
       
    24 #include <PSVariables.h>
       
    25 #include <mtclreg.h> //CClientMtmRegistry
       
    26 #include <smsclnt.h> //CSmsClientMtm
       
    27 #include <txtrich.h>
       
    28 #include <bautils.h> //file system utilities
       
    29 #include <remotelock.rsg>
       
    30 #ifdef RD_MULTIPLE_DRIVE 
       
    31 #include <driveinfo.h>
       
    32 #else
       
    33 #include <pathinfo.h> 
       
    34 #endif //RD_MULTIPLE_DRIVE 
       
    35 #include <coreapplicationuisdomainpskeys.h>
       
    36 #include <charconv.h>
       
    37 #include <ProfileEngineSDKCRKeys.h>
       
    38 #include <Profile.hrh>
       
    39 #include <stringresourcereader.h> //CStringResourceReader
       
    40 #include "RemoteLockTrace.h"
       
    41 #include "RemoteLock.h"
       
    42 
       
    43 // EXTERNAL DATA STRUCTURES
       
    44 
       
    45 // EXTERNAL FUNCTION PROTOTYPES  
       
    46 
       
    47 // CONSTANTS
       
    48 
       
    49 _LIT( KRLockResourceFileName ,"z:\\Resource\\apps\\Remotelock.RSC" );
       
    50 
       
    51 //Interval of reconnecting to the message server 
       
    52 const TInt KRLockTimerInterval( 10000000 ); 
       
    53 const TInt KRLockMaxMemoryCardPasswdLength( 8 );
       
    54 const TInt KBufLengthInUnicodeCheck = 200;
       
    55 
       
    56 // MACROS
       
    57 
       
    58 // LOCAL CONSTANTS AND MACROS
       
    59 
       
    60 // MODULE DATA STRUCTURES
       
    61 
       
    62 // LOCAL FUNCTION PROTOTYPES
       
    63 
       
    64 // FORWARD DECLARATIONS
       
    65 
       
    66 // ---------------------------------------------------------------------------
       
    67 // E32Main()
       
    68 // ---------------------------------------------------------------------------
       
    69 //
       
    70 GLDEF_C TInt E32Main() 
       
    71     {    
       
    72     __UHEAP_MARK;
       
    73     if ( User::TrapHandler() != NULL )
       
    74         {
       
    75         User::SetTrapHandler( NULL );
       
    76         }
       
    77         
       
    78     CTrapCleanup* cleanup = CTrapCleanup::New();
       
    79     if ( !cleanup )
       
    80         {
       
    81         return KErrNone;
       
    82         }
       
    83 
       
    84     TRAPD( err, ThreadStartL() );
       
    85     ( void ) err;
       
    86     
       
    87     delete cleanup;
       
    88     __UHEAP_MARKEND;
       
    89 
       
    90     return err;    
       
    91     }
       
    92 
       
    93 
       
    94 // ---------------------------------------------------------------------------
       
    95 // ThreadStartL()
       
    96 // ---------------------------------------------------------------------------
       
    97 //
       
    98 LOCAL_C TInt ThreadStartL()
       
    99     {
       
   100     RL_TRACE_PRINT(" [ rl.exe ] Inside ThreadStartL() "); 
       
   101     CActiveScheduler *threadScheduler = new CActiveScheduler;
       
   102     
       
   103     CleanupStack::PushL( threadScheduler );
       
   104     
       
   105     CActiveScheduler::Install( threadScheduler );
       
   106     
       
   107     RL_TRACE_PRINT(" [ rl.exe ] Create remotelock object "); 
       
   108 
       
   109     CRemoteLock* remoteLock = CRemoteLock::NewL();
       
   110     
       
   111     RL_TRACE_PRINT(" [ rl.exe ] Creation of Remotelock is done" );
       
   112     
       
   113     CleanupStack::PushL( remoteLock );
       
   114         
       
   115     RL_TRACE_PRINT(" Doing Rendezvous..."); 
       
   116     // Initialisation complete, now signal the client
       
   117     RProcess::Rendezvous(KErrNone);            
       
   118 
       
   119     CActiveScheduler::Start();
       
   120     
       
   121     CleanupStack::PopAndDestroy( 2 );
       
   122     
       
   123     return KErrNone;
       
   124     }
       
   125 
       
   126 
       
   127 // ---------------------------------------------------------------------------
       
   128 // CRemoteLock::NewL
       
   129 // Two-Phased constructor.
       
   130 // ---------------------------------------------------------------------------
       
   131 //
       
   132 CRemoteLock* CRemoteLock::NewL()
       
   133     {
       
   134     RL_TRACE_PRINT(" [ rl.exe ] NewL()");
       
   135     
       
   136     CRemoteLock* self = new ( ELeave ) CRemoteLock;
       
   137     
       
   138     CleanupStack::PushL( self );
       
   139     
       
   140     self->ConstructL();  
       
   141     
       
   142     CleanupStack::Pop();
       
   143     
       
   144     RL_TRACE_PRINT(" [ rl.exe ] exit NewL() ");
       
   145     
       
   146     return self;
       
   147     }
       
   148 
       
   149 
       
   150 // ---------------------------------------------------------------------------
       
   151 // Destructor
       
   152 // ---------------------------------------------------------------------------
       
   153 CRemoteLock::~CRemoteLock()
       
   154     {
       
   155     Cancel();
       
   156     delete iRemoteLockSetting;
       
   157     delete iRemoteLockCode;
       
   158     delete iMemoryCardPasswd;
       
   159     delete iStoredCode;
       
   160     delete iStoredTrimmedCode;
       
   161     delete iClientMtmForSending;
       
   162     delete iMtmReg;
       
   163     delete iMsvSession;
       
   164     delete iObserver;
       
   165     
       
   166     if ( iProfileNotifyHandler ) 
       
   167         {
       
   168       iProfileNotifyHandler->StopListening();
       
   169       delete iProfileNotifyHandler;
       
   170         }
       
   171     delete iProfileSession; 
       
   172     }
       
   173 
       
   174 
       
   175 // ---------------------------------------------------------------------------
       
   176 // CRemoteLock::HandleSessionEventL
       
   177 //
       
   178 // Called by message server when new event occures.
       
   179 // ---------------------------------------------------------------------------
       
   180 //
       
   181 void CRemoteLock::HandleSessionEventL( 
       
   182     TMsvSessionEvent aEvent, 
       
   183     TAny* aArg1, 
       
   184     TAny* aArg2, 
       
   185     TAny* /*aArg3*/  )
       
   186     {
       
   187     RL_TRACE_PRINT(" [ rl.exe ] HandleSessionEventL() ");  
       
   188     switch ( aEvent )
       
   189         {            
       
   190         // A new entry has been created in the message server
       
   191         case EMsvEntriesCreated: 
       
   192             {
       
   193             RL_TRACE_PRINT(" [ rl.exe ] HandleSessionEventL() 1");  
       
   194             if ( !iIsEnabled )
       
   195                 {
       
   196                 // Do nothing if remote lock is disabled
       
   197                 return;
       
   198                 }
       
   199             
       
   200             if ( *( static_cast<TMsvId*>( aArg2 ) ) != 
       
   201                 KMsvGlobalInBoxIndexEntryId )
       
   202                 {
       
   203                 // Doing nothing if not to inbox
       
   204                 return;
       
   205                 }
       
   206 
       
   207             CMsvEntrySelection* entries =
       
   208                 static_cast<CMsvEntrySelection*>( aArg1 );
       
   209 
       
   210             for ( TInt i = 0; i < entries->Count(); i++ )
       
   211                 {
       
   212                 TMsvEntry entryInfo;
       
   213                 TMsvId serviceId;
       
   214                 iMsvSession->GetEntry( entries->At( i ), serviceId, entryInfo );
       
   215 
       
   216                 if ( entryInfo.iMtm != KUidMsgTypeSMS || 
       
   217                     entryInfo.iDescription.Length() > KRLockMaxLockCodeLength || 
       
   218                     entryInfo.iDescription.Length() == 0)
       
   219                     {
       
   220                     // Doing nothing if not a SMS, or length not appropriate.
       
   221                     return;
       
   222                     }
       
   223 
       
   224                 // For performance reasons, lets first compare the content of 
       
   225                 // the SMS (taken from the TMsvEntry, containing no extra 
       
   226                 // spaces) against the saved digest of a trimmed remote lock 
       
   227                 // code. This way unnecessary access to CMsvEntry is avoided 
       
   228                 // if these two do not match.
       
   229                 
       
   230                 
       
   231                 if ( iRemoteLockSetting->CompareLockCodesL( 
       
   232                     entryInfo.iDescription, *iStoredTrimmedCode ) )
       
   233                     {
       
   234                     CMsvEntry* entry = 
       
   235                         iMsvSession->GetEntryL( entries->At( i ) );
       
   236                 
       
   237                     // The CMsvEntry object must be deleted by the client
       
   238                     // when it is no longer required. 
       
   239                     CleanupStack::PushL( entry );
       
   240 
       
   241                     TRAPD( error, HandleMessageL( entry ) );
       
   242 
       
   243                     CleanupStack::PopAndDestroy( 1 ); // entry
       
   244                     
       
   245                     delete iRemoteLockCode;
       
   246                     iRemoteLockCode = NULL;
       
   247                     
       
   248                     User::LeaveIfError( error );
       
   249                     } // if
       
   250                 } // for
       
   251             break;
       
   252             } // case
       
   253       
       
   254         // When message server is closed and server terminated
       
   255         case EMsvCloseSession:
       
   256         case EMsvServerFailedToStart:
       
   257         case EMsvServerTerminated:
       
   258             {
       
   259             RL_TRACE_PRINT(" [ rl.exe ] HandleSessionEventL() 2");  
       
   260             // When server is terminated remote lock will
       
   261             // try to reconnect it in every 10 seconds.
       
   262             After( KRLockTimerInterval );
       
   263             break;
       
   264             }
       
   265 
       
   266         default:
       
   267             break;
       
   268         }
       
   269     RL_TRACE_PRINT(" [ rl.exe ] exit HandleSessionEventL() ");
       
   270     }
       
   271 
       
   272 
       
   273 // ---------------------------------------------------------------------------
       
   274 // CRemoteLock::HandleRemoteLockNotifyL
       
   275 // Function is called when remote lock setting is changed by client
       
   276 // ---------------------------------------------------------------------------
       
   277 //
       
   278 void CRemoteLock::HandleRemoteLockNotifyL()
       
   279     {
       
   280     RL_TRACE_PRINT(" [ rl.exe ] HandleNotify() ");
       
   281     CheckSettingsL();
       
   282     RL_TRACE_PRINT(" [ rl.exe ] exit HandleNotify() ")
       
   283     }
       
   284     
       
   285     
       
   286 // ---------------------------------------------------------------------------
       
   287 // CRemoteLock::CRemoteLock
       
   288 // default constructor
       
   289 // ---------------------------------------------------------------------------
       
   290 CRemoteLock::CRemoteLock() : CTimer( CActive::EPriorityStandard )
       
   291     {
       
   292     iRemoteLockCode = NULL;
       
   293     iMemoryCardPasswd = NULL;
       
   294     iStoredCode = NULL;
       
   295     iStoredTrimmedCode = NULL;
       
   296     iClientMtmForSending = NULL;
       
   297     iMtmReg = NULL;
       
   298     iRemoteLockSetting = NULL;
       
   299     iMsvSession = NULL;
       
   300     iIsEnabled = EFalse;
       
   301     iLockedByRL = EFalse; 
       
   302     iMemoryCardLockedByRL = EFalse;
       
   303     iSubscribeProfile = EFalse;
       
   304     iProfileSession = NULL;
       
   305     iObserver = NULL;
       
   306     iProfileNotifyHandler = NULL;   
       
   307     }
       
   308 
       
   309 
       
   310 // ---------------------------------------------------------------------------
       
   311 // CRemoteLock::ConstructL
       
   312 // Symbian 2nd phase constructor can leave.
       
   313 // ---------------------------------------------------------------------------
       
   314 //
       
   315 void CRemoteLock::ConstructL()
       
   316     {    
       
   317     RL_TRACE_PRINT(" [ Rl.exe ] ConstructL()"); 
       
   318     
       
   319     CTimer::ConstructL();
       
   320     
       
   321     iRemoteLockSetting = CRemoteLockSettings::NewL( this ); 
       
   322      
       
   323     iRemoteLockSetting->RemoteLockNotifyL( ETrue );
       
   324     
       
   325     CheckSettingsL(); 
       
   326     // Add active object into active scheduler
       
   327     CActiveScheduler::Add( this );
       
   328         
       
   329     
       
   330     
       
   331     RL_TRACE_PRINT(" [ rl.exe ] exit ConstructL() ");
       
   332     }
       
   333     
       
   334     
       
   335 // ---------------------------------------------------------------------------
       
   336 // CRemoteLock::HandleMessageL()
       
   337 //
       
   338 // Handles the message: checks if it really is remote lock message, and acts
       
   339 // accordingly.
       
   340 // ---------------------------------------------------------------------------
       
   341 //
       
   342 void CRemoteLock::HandleMessageL( CMsvEntry* aEntry )
       
   343     {
       
   344     RL_TRACE_PRINT(" [ rl.exe ] HandleMessageL() ");
       
   345     delete iRemoteLockCode;
       
   346     iRemoteLockCode = NULL;
       
   347     TMsvEntry entryInfo = aEntry->Entry();
       
   348     if ( !iMtmReg )
       
   349         {
       
   350         iMtmReg = CClientMtmRegistry::NewL( *iMsvSession );
       
   351         }
       
   352 
       
   353     // Loops 100 times to ensure the clientMtm is correctly created
       
   354     CBaseMtm* clientMtm = NULL;
       
   355     TInt error = 0;
       
   356     TInt i;
       
   357     for ( i = 0; i < 100; i++ )
       
   358         {
       
   359         TRAP( error, clientMtm = iMtmReg->NewMtmL( entryInfo.iMtm ));
       
   360         if ( error == KErrNone )
       
   361             {
       
   362             break;
       
   363             }
       
   364         } 
       
   365    
       
   366     RL_TRACE_PRINT_NUM("[rl.exe] HandleMessageL() error after NewMTmL = %d", error );
       
   367 
       
   368     User::LeaveIfError( error );
       
   369     CleanupStack::PushL( clientMtm );        
       
   370 
       
   371     clientMtm->SwitchCurrentEntryL( entryInfo.Id() );
       
   372 
       
   373     RL_TRACE_PRINT(" [ rl.exe ] HandleMessageL() after switch Current EntryL");
       
   374    
       
   375     CSmsClientMtm* smsClientMTM = STATIC_CAST( CSmsClientMtm*, clientMtm );
       
   376     
       
   377     // Change from 100 to 20. When the message storage is memory card. 
       
   378     // LoadMessageL() function return -21 when loading the message.
       
   379     // It might be caused by slow operation, so we add 0.05 seconds pending 
       
   380     // in each loop.
       
   381     // @ check why loading operation is slow!
       
   382     for ( i = 0; i < 20; i++ )
       
   383         {   
       
   384         RL_TRACE_PRINT_NUM("[rl.exe] HandleMessageL() error load message1 = %d", error );
       
   385         
       
   386         // Loops 100 times to ensure the LoadMessageL() succeedes
       
   387         TRAP( error, smsClientMTM->LoadMessageL() );
       
   388         
       
   389         RL_TRACE_PRINT_NUM("[rl.exe] HandleMessageL() error load message2 = %d", error );
       
   390         
       
   391         if ( error == KErrNone )
       
   392             {
       
   393             break;
       
   394             }
       
   395         
       
   396         const TInt KDelay = 50000; // 0.05s
       
   397         User::After( KDelay );
       
   398         }
       
   399     
       
   400     RL_TRACE_PRINT_NUM("[rl.exe] HandleMessageL() error after LoadMessageL = %d", error );
       
   401     
       
   402     User::LeaveIfError( error );        
       
   403 
       
   404     CRichText& mtmBody = smsClientMTM->Body();                            
       
   405     
       
   406     // Let's then use the real content of the SMS (taken from 
       
   407     // the message body) and compare that against the 
       
   408     // saved digest of a complete (not trimmed) remote lock 
       
   409     // code. This ensures that remote locking happens only
       
   410     // if the SMS is exactly the same as what the user has 
       
   411     // specified.
       
   412     iRemoteLockCode = HBufC::NewL( mtmBody.DocumentLength() );
       
   413 
       
   414     TPtr smsTextPtr = iRemoteLockCode->Des();
       
   415     smsTextPtr.Copy( mtmBody.Read( 0, mtmBody.DocumentLength() ) );
       
   416     
       
   417     
       
   418     // The following code is to get the current state
       
   419     // of manuallock. If manuallock state is off, that means
       
   420     // the owner has unlocked the phone or the phone has
       
   421     // never been locked by both remote lock and auto lock.
       
   422     RL_TRACE_PRINT(" [ rl.exe ] HandleMessageL() Pub&Sub Get autoLState");    
       
   423     TInt autoLState;
       
   424     RProperty property;
       
   425     
       
   426     #ifndef RD_STARTUP_CHANGE    
       
   427     User::LeaveIfError( property.Attach( KUidSystemCategory, KPSUidAutolockStatusValue ));
       
   428     #else
       
   429     User::LeaveIfError( property.Attach( KPSUidCoreApplicationUIs, KCoreAppUIsAutolockStatus ));
       
   430     #endif //RD_STARTUP_CHANGE
       
   431     property.Get( autoLState );
       
   432     
       
   433     #ifndef RD_STARTUP_CHANGE
       
   434     if ( autoLState == EPSAutolockOff ) 
       
   435     #else
       
   436     if ( autoLState == EAutolockOff ) 
       
   437     #endif //RD_STARTUP_CHANGE      
       
   438       {  
       
   439       //Assign iLockedByRL to EFalse to indicate that
       
   440       //the phone is not locked by Remote lock, the 
       
   441       //reply SMS should be sent if correct remote lock
       
   442       //code is received.
       
   443       iLockedByRL = EFalse;
       
   444       }
       
   445     
       
   446     property.Close();
       
   447     
       
   448     // If the phone receives correct lock code, the phone
       
   449     // will be locked eventhough it may have been locked by
       
   450     // auto lock.
       
   451   
       
   452     if ( VerifyAndLockL() ) 
       
   453         {
       
   454         RL_TRACE_PRINT(" [ rl.exe ] HandleMessageL() After VerifyAndLockL() ");
       
   455         // Sets remote lock sms as read
       
   456         entryInfo.SetVisible( ETrue );
       
   457         entryInfo.SetUnread( EFalse );
       
   458         entryInfo.SetNew( EFalse );
       
   459         entryInfo.SetInPreparation( EFalse );
       
   460         entryInfo.SetReadOnly( ETrue );
       
   461         aEntry->ChangeL( entryInfo );
       
   462         
       
   463         
       
   464         // Locks MemoryCard drive.
       
   465         iStateMemoryCard = SetMemoryCardPasswdL( EFalse );
       
   466 
       
   467         if ( iClientMtmForSending )
       
   468             {
       
   469             // Previous reply sending is still in progress, let's not start 
       
   470             // sending new one.
       
   471             User::Leave( KErrNone );
       
   472             } 
       
   473         
       
   474         // The value of iLockedByRL is checked before sending SMS
       
   475         // to make sure if the phone has been locked by remote 
       
   476         // lock. By doing this, it is able to avoid the threat where 
       
   477         // malware tries to use remote lock to keep on sending SMS.
       
   478         
       
   479         
       
   480         if ( !iLockedByRL )
       
   481             { 
       
   482             RL_TRACE_PRINT(" [ rl.exe ] HandleMessageL() Send re-SMS ");
       
   483             // Replies sms
       
   484             iLockedByRL = ETrue;
       
   485             CSmsHeader& header = smsClientMTM->SmsHeader();
       
   486             HBufC* recipientAddress = header.FromAddress().AllocLC();
       
   487 
       
   488             TBool initialSendMessage = EFalse;
       
   489             TRAP( error, initialSendMessage = InitialSendMessageL() );
       
   490             if ( error != KErrNone )
       
   491                 {
       
   492                 delete iClientMtmForSending;
       
   493                 iClientMtmForSending = NULL;
       
   494                 User::Leave( error );
       
   495                 }
       
   496                 
       
   497             RL_TRACE_PRINT(" [ rl.exe ] HandleMessageL() Check recipientaddresslenght ");
       
   498             if ( recipientAddress->Length() > 0 && initialSendMessage )
       
   499                 {
       
   500                 RL_TRACE_PRINT(" [ rl.exe ] HandleMessageL() before call sendMEssage ");
       
   501                 TRAP( error, SendMessageL( *recipientAddress ) );
       
   502                 if ( error != KErrNone )
       
   503                     {
       
   504                     RL_TRACE_PRINT(" [ rl.exe ] HandleMessageL() error occurs ");
       
   505                     delete iClientMtmForSending;
       
   506                     iClientMtmForSending = NULL;
       
   507                     User::Leave( error );
       
   508                     }
       
   509                 }
       
   510             delete iClientMtmForSending;
       
   511             iClientMtmForSending = NULL;
       
   512 
       
   513             CleanupStack::PopAndDestroy( 1 ); // recipientAddress
       
   514             }
       
   515         CleanupStack::PopAndDestroy( 1 ); // clientMtm    
       
   516         }
       
   517     else
       
   518         {
       
   519         CleanupStack::PopAndDestroy( 1 ); // clientMtm
       
   520         }
       
   521     RL_TRACE_PRINT(" [ rl.exe ] exit HandleMessageL() ");
       
   522     }
       
   523 
       
   524 
       
   525 // ---------------------------------------------------------------------------
       
   526 // CRemoteLock::VerifyAndLockL()
       
   527 //
       
   528 // Compares if the remote lock received in SMS is correct.
       
   529 // If correct, locks the terminal.
       
   530 // ---------------------------------------------------------------------------
       
   531 //
       
   532 TBool CRemoteLock::VerifyAndLockL()
       
   533     {
       
   534     RL_TRACE_PRINT(" [ rl.exe ] VerifyAndLockL() ");
       
   535     
       
   536     User::LeaveIfNull( iRemoteLockCode );
       
   537     User::LeaveIfNull( iStoredCode );
       
   538 
       
   539     if ( iRemoteLockSetting->CompareLockCodesL( 
       
   540         *iRemoteLockCode, *iStoredCode ) )
       
   541         {        
       
   542         return ActivateDeviceLock();
       
   543         }
       
   544     RL_TRACE_PRINT(" [ rl.exe ] exit VerifyAndLockL() ");
       
   545     return EFalse;
       
   546     }
       
   547 
       
   548 
       
   549 // ---------------------------------------------------------------------------
       
   550 // CRemoteLock::ActivateDeviceLock()
       
   551 // Activate the device lock to lock the phone
       
   552 // ---------------------------------------------------------------------------
       
   553 // 
       
   554 TBool CRemoteLock::ActivateDeviceLock()
       
   555     {
       
   556     RL_TRACE_PRINT(" [ rl.exe ] ActivateDeviceLock() ");
       
   557        
       
   558     RProperty property;
       
   559         
       
   560     TInt err = 0;
       
   561        
       
   562     #ifndef RD_STARTUP_CHANGE
       
   563     err = property.Attach( KUidSystemCategory, 
       
   564                                KPSUidAutolockStatusValue );
       
   565     #else
       
   566     err = property.Attach( KPSUidCoreApplicationUIs, 
       
   567                                KCoreAppUIsAutolockStatus );
       
   568     #endif 
       
   569     if ( err != KErrNone )
       
   570         {
       
   571         return EFalse;
       
   572         }
       
   573 
       
   574     #ifndef RD_STARTUP_CHANGE
       
   575     property.Set( EPSRemoteLocked );
       
   576     #else
       
   577     property.Set( ERemoteLocked );
       
   578     #endif //RD_STARTUP_CHANGE 
       
   579  
       
   580     property.Close();
       
   581     RL_TRACE_PRINT(" [ rl.exe ] exit ActivateDeviceLock() ");
       
   582     return ETrue;
       
   583     }
       
   584 
       
   585 
       
   586 
       
   587 // ---------------------------------------------------------------------------
       
   588 // CRemoteLock::CheckSettingsL
       
   589 // Checks remote lock setting.
       
   590 // ---------------------------------------------------------------------------
       
   591 //
       
   592 void CRemoteLock::CheckSettingsL()
       
   593     {
       
   594     RL_TRACE_PRINT(" [ rl.exe ] CheckSettingsL () "); 
       
   595     iRemoteLockSetting->GetEnabled( iIsEnabled );
       
   596     
       
   597     delete iStoredCode;
       
   598     iStoredCode = NULL;
       
   599     iStoredCode = HBufC::NewL( KRLockStoredLockCodeLength );
       
   600     TPtr storedCodePtr = iStoredCode->Des();
       
   601     
       
   602     delete iStoredTrimmedCode;
       
   603     iStoredTrimmedCode = NULL;
       
   604     iStoredTrimmedCode = HBufC::NewL( KRLockStoredLockCodeLength );
       
   605     TPtr storedTrimmedCodePtr = iStoredTrimmedCode->Des();
       
   606     
       
   607     iRemoteLockSetting->GetLockCode( storedCodePtr, storedTrimmedCodePtr );
       
   608         
       
   609     if ( iIsEnabled )
       
   610         {
       
   611         RL_TRACE_PRINT(" [ rl.exe ] CheckSettingsL() enable "); 
       
   612        
       
   613         if ( !iMsvSession )
       
   614             {
       
   615             TRAPD( error, iMsvSession = CMsvSession::OpenAsyncL( *this ) );
       
   616         
       
   617             RL_TRACE_PRINT_NUM(" [ rl.exe ] CheckSettingsL() openasyncL %d ", error );
       
   618         
       
   619             if ( error != KErrNone )
       
   620                 {
       
   621                 After( KRLockTimerInterval );
       
   622                 }  
       
   623             }
       
   624 
       
   625        if ( !iProfileSession )
       
   626            {
       
   627            RL_TRACE_PRINT(" [ rl.exe ] CheckSettingsL() create session "); 
       
   628            iProfileSession = CRepository::NewL( KCRUidProfileEngine ); 
       
   629            }
       
   630        if ( !iObserver )
       
   631            {
       
   632            iObserver =  CRLLockObserver::NewL( this );      
       
   633            } 
       
   634        if( !iSubscribeProfile )
       
   635            {
       
   636            ProfileNotifyL( ETrue );
       
   637            iSubscribeProfile = ETrue; 
       
   638            }   
       
   639         }
       
   640     else
       
   641         {
       
   642         RL_TRACE_PRINT(" [ rl.exe ] CheckSettingsL() disable "); 
       
   643         
       
   644         delete iClientMtmForSending;
       
   645         iClientMtmForSending = NULL;
       
   646         delete iMtmReg;
       
   647         iMtmReg = NULL;
       
   648         delete iMsvSession;
       
   649         iMsvSession = NULL;
       
   650         
       
   651         RL_TRACE_PRINT(" [ rl.exe ] CheckSettingsL() delete iMsvSession ");
       
   652         
       
   653         if ( iSubscribeProfile )
       
   654             {
       
   655             if ( !iProfileSession )
       
   656                 {
       
   657                 RL_TRACE_PRINT(" [ rl.exe ] CheckSettingsL() create session "); 
       
   658                 iProfileSession = CRepository::NewL( KCRUidProfileEngine ); 
       
   659                 }
       
   660             ProfileNotifyL( EFalse );
       
   661             delete iProfileSession;
       
   662             iProfileSession = NULL;
       
   663             delete iObserver;
       
   664             iObserver = NULL; 
       
   665             iSubscribeProfile = EFalse;
       
   666             }  
       
   667         }   
       
   668     
       
   669     RL_TRACE_PRINT(" [ rl.exe ] exit CheckSettingsL() ");
       
   670     }
       
   671     
       
   672 
       
   673 // ---------------------------------------------------------------------------
       
   674 // CRemoteLock::IsMemoryCardLocked
       
   675 // Check MemoryCard whether it is locked or not.
       
   676 // ---------------------------------------------------------------------------
       
   677 //
       
   678 TBool CRemoteLock::IsMemoryCardLocked( const TDriveInfo& aDriveInfo ) const
       
   679     {
       
   680     return aDriveInfo.iMediaAtt&( KMediaAttLocked );
       
   681     }
       
   682 
       
   683 
       
   684 // ---------------------------------------------------------------------------
       
   685 // CRemoteLock::HasMemoryCardPassword
       
   686 // Check MemoryCard whether it has password.
       
   687 // ---------------------------------------------------------------------------
       
   688 //
       
   689 TBool CRemoteLock::HasMemoryCardPassword( const TDriveInfo& aDriveInfo ) const
       
   690     {
       
   691     return aDriveInfo.iMediaAtt&( KMediaAttHasPassword );
       
   692     }
       
   693 
       
   694 
       
   695 // ---------------------------------------------------------------------------
       
   696 // CRemoteLock::IsMemoryCardPresent
       
   697 // Check MemoryCard whether it is mounted into the phone and it supports locking.
       
   698 // ---------------------------------------------------------------------------
       
   699 //
       
   700 TBool CRemoteLock::IsMemoryCardPresent( const TDriveInfo& aDriveInfo ) const
       
   701     {
       
   702     return aDriveInfo.iDriveAtt&( KDriveAttRemovable ) && 
       
   703         aDriveInfo.iMediaAtt&( KMediaAttLockable );
       
   704     }
       
   705 
       
   706 
       
   707 // ---------------------------------------------------------------------------
       
   708 // CRemoteLock::SetMemoryCardPasswdL
       
   709 // Sets/remove the password to MemoryCard.
       
   710 // ---------------------------------------------------------------------------
       
   711 //
       
   712 TBool CRemoteLock::SetMemoryCardPasswdL( const TBool aClear )
       
   713     {
       
   714     RL_TRACE_PRINT(" [ rl.exe ] SetMemoryCardPasswdL() ");
       
   715     if ( aClear )
       
   716         {
       
   717         if ( iMemoryCardLockedByRL && iLockedByRL )
       
   718             {
       
   719             //remove the password
       
   720             if ( DoSetMemoryCardPasswdL( ETrue ) )
       
   721                 {
       
   722                 iMemoryCardLockedByRL = EFalse;
       
   723                 RL_TRACE_PRINT(" [ rl.exe ] exit SetMemoryCardPasswdL() ");    
       
   724                 return ETrue;   
       
   725                 }
       
   726             else
       
   727                 {
       
   728                 //Only try to remove password once no matter success or not 
       
   729                 iMemoryCardLockedByRL = EFalse; 
       
   730               }    
       
   731              
       
   732             }       
       
   733         }
       
   734     else
       
   735         {
       
   736         if ( iMemoryCardLockedByRL )
       
   737             {
       
   738             //the memory card is locked already by rlock, 
       
   739             //just return ETrue;
       
   740             return ETrue;
       
   741             }
       
   742         
       
   743         if ( !iRemoteLockCode || iRemoteLockCode->Length() == 0 )
       
   744             {
       
   745             return EFalse;
       
   746             }
       
   747         //Change the password
       
   748         if ( DoSetMemoryCardPasswdL( EFalse ) )
       
   749             {
       
   750             iMemoryCardLockedByRL = ETrue;
       
   751             RL_TRACE_PRINT(" [ rl.exe ] exit SetMemoryCardPasswdL() ");    
       
   752             return ETrue; 
       
   753             }
       
   754            
       
   755         }
       
   756     RL_TRACE_PRINT(" [ rl.exe ] exit SetMemoryCardPasswdL() ");    
       
   757     return EFalse;
       
   758     }   
       
   759 
       
   760 // ---------------------------------------------------------------------------
       
   761 // CRemoteLock::DoSetMemoryCardPasswdL
       
   762 // Do Sets/remove the password to MemoryCard.
       
   763 // ---------------------------------------------------------------------------
       
   764 //
       
   765 TBool CRemoteLock::DoSetMemoryCardPasswdL( TBool aClear )
       
   766     {
       
   767     RL_TRACE_PRINT(" [ rl.exe ] DoSetMemoryCardPasswdL() ");
       
   768     RFs fsMemoryCard;
       
   769     TInt err = fsMemoryCard.Connect();
       
   770     if ( err != KErrNone )
       
   771         {
       
   772         RL_TRACE_PRINT(" [ rl.exe ] DoSetMemoryCardPasswdL() error");
       
   773         return EFalse;
       
   774         }
       
   775          
       
   776 #ifdef RD_MULTIPLE_DRIVE 
       
   777 
       
   778    // Get the removeable user visible drives
       
   779    TDriveList driveList;
       
   780    TInt driveCount;
       
   781    
       
   782    //Get all removeable drive, both physically and logically
       
   783    User::LeaveIfError( DriveInfo::GetUserVisibleDrives(
       
   784        fsMemoryCard, driveList, driveCount, KDriveAttRemovable ) );   
       
   785    RL_TRACE_PRINT_NUM(" [ rl.exe ] DoSetMemoryCardPasswdL() driveCount = %d ", driveCount );
       
   786  
       
   787    //boolen to indicate at least one operation(clear/add password) 
       
   788    // in the loop is ok.
       
   789    TBool OperationSucceed = EFalse;
       
   790   
       
   791    RArray<TInt> arrayMemoryCardIndex;
       
   792   
       
   793    TInt max( driveList.Length() );
       
   794    
       
   795    for ( TInt i = 0; i < max; i++ )
       
   796        {
       
   797        if ( driveList[ i ] )
       
   798           {
       
   799           TUint status;	
       
   800           DriveInfo::GetDriveStatus( fsMemoryCard, i, status );	
       
   801           //To make sure the drive is physically removeable not logically removeable	
       
   802           if( status & DriveInfo::EDriveRemovable )	
       
   803               {
       
   804               //append all physical removable memory card index into this array	
       
   805               arrayMemoryCardIndex.Append(i); 
       
   806               RL_TRACE_PRINT_NUM(" [ rl.exe ] DoSetMemoryCardPasswdL() physically removable drive %d", i );
       
   807 		          }
       
   808 		      else
       
   809 		          {
       
   810 		          RL_TRACE_PRINT_NUM(" [ rl.exe ] DoSetMemoryCardPasswdL() logically removable drive %d", i );
       
   811 		          }    
       
   812 		      }    
       
   813        }
       
   814       
       
   815    //Lock/Unblock all physical removeable memory card
       
   816    for ( TInt i = 0; i < arrayMemoryCardIndex.Count(); i++ )
       
   817        {
       
   818        TDriveInfo driveInfoT;
       
   819        TInt driveIndex = arrayMemoryCardIndex[i];
       
   820        RL_TRACE_PRINT_NUM(" [ rl.exe ] DoSetMemoryCardPasswdL() driveIndex %d", driveIndex );	
       
   821        fsMemoryCard.Drive( driveInfoT, driveIndex );
       
   822     
       
   823        if ( IsMemoryCardPresent( driveInfoT ) ) 
       
   824           {
       
   825           	RL_TRACE_PRINT(" [ rl.exe ] Memory card is present");
       
   826           if ( aClear )
       
   827               {
       
   828               RL_TRACE_PRINT(" [ rl.exe ] DoSetMemoryCardPasswdL() Remove password");	
       
   829               if ( HasMemoryCardPassword( driveInfoT ) || IsMemoryCardLocked( driveInfoT ) ) 
       
   830                   { 
       
   831                   TMediaPassword memoryCardPassword; 
       
   832               
       
   833                   // Converts MemoryCardpassword
       
   834                   ConvertMemoryCardPassword( memoryCardPassword, ETrue  );
       
   835               
       
   836                   err = fsMemoryCard.ClearPassword( driveIndex, memoryCardPassword );
       
   837               
       
   838                   if ( err == KErrNone )
       
   839                       {
       
   840                   	  OperationSucceed = ETrue;
       
   841                       }
       
   842               
       
   843                   RL_TRACE_PRINT_NUM(" [ rl.exe ] exit DoSetMemoryCardPasswdL() password for index %d is cleaned", driveIndex);
       
   844                   //Go for next
       
   845                   }
       
   846               else
       
   847                   {
       
   848                   //Go for next
       
   849                   }      
       
   850                }
       
   851             else
       
   852                {
       
   853                RL_TRACE_PRINT(" [ rl.exe ] DoSetMemoryCardPasswdL() Set password");
       
   854                if ( !HasMemoryCardPassword( driveInfoT ) && !IsMemoryCardLocked( driveInfoT ) )
       
   855                    { 
       
   856                    TMediaPassword memoryCardPassword;
       
   857                 
       
   858                    TMediaPassword nullMemoryCardPassword;
       
   859                 
       
   860                    // Converts MemoryCardpassword
       
   861                    ConvertMemoryCardPassword( memoryCardPassword, EFalse );
       
   862                 
       
   863                    // Locks MemoryCard drive
       
   864                    err = fsMemoryCard.LockDrive( driveIndex, nullMemoryCardPassword, memoryCardPassword, ETrue );        
       
   865                 
       
   866                    if ( err == KErrNone )
       
   867                       {
       
   868                   	  OperationSucceed = ETrue;
       
   869                       }
       
   870                 
       
   871                    RL_TRACE_PRINT_NUM(" [ rl.exe ] DoSetMemoryCardPasswdL() password for index %d is set", driveIndex);
       
   872                    
       
   873                    //Go for next
       
   874                    }
       
   875                 else
       
   876                    {
       
   877                    //go for next
       
   878                    }   
       
   879                }
       
   880             } //if ( IsMemoryCardPresent( driveInfoT ) ) 
       
   881          } //for
       
   882     
       
   883      
       
   884      if ( aClear )
       
   885          {
       
   886      	   delete iMemoryCardPasswd; 
       
   887          iMemoryCardPasswd = NULL; 
       
   888          fsMemoryCard.Close(); 
       
   889          
       
   890          return OperationSucceed ? ETrue : EFalse;
       
   891          }
       
   892      else 
       
   893         {
       
   894 
       
   895         delete iMemoryCardPasswd;
       
   896         iMemoryCardPasswd = NULL; 
       
   897         iMemoryCardPasswd = iRemoteLockCode->AllocL();
       
   898         fsMemoryCard.Close();	
       
   899         
       
   900         return OperationSucceed ? ETrue : EFalse;
       
   901         }       
       
   902 #else
       
   903     
       
   904     TInt i = 0;
       
   905     TParsePtrC folder( PathInfo::MemoryCardRootPath() );
       
   906     fsMemoryCard.CharToDrive( folder.Drive()[ 0 ], i );
       
   907       
       
   908     TDriveInfo driveInfoT;
       
   909     fsMemoryCard.Drive( driveInfoT, i );
       
   910     
       
   911     if ( IsMemoryCardPresent( driveInfoT ) ) 
       
   912       {
       
   913       if ( aClear )
       
   914           {
       
   915           if ( HasMemoryCardPassword( driveInfoT ) || IsMemoryCardLocked( driveInfoT ) ) 
       
   916               { 
       
   917               TMediaPassword memoryCardPassword; 
       
   918               // Converts MemoryCardpassword
       
   919               ConvertMemoryCardPassword( memoryCardPassword, ETrue  );
       
   920               err = fsMemoryCard.ClearPassword( i, memoryCardPassword );
       
   921               
       
   922               if( err == KErrNone )
       
   923                   {
       
   924                   delete iMemoryCardPasswd; 
       
   925                   iMemoryCardPasswd = NULL; 
       
   926                   }
       
   927               fsMemoryCard.Close(); 
       
   928               RL_TRACE_PRINT(" [ rl.exe ] exit DoSetMemoryCardPasswdL() clear");
       
   929               return ( err == KErrNone );
       
   930               }
       
   931           else
       
   932               {
       
   933               fsMemoryCard.Close();  
       
   934               return ETrue;
       
   935               }      
       
   936            }
       
   937         else
       
   938            {
       
   939            if ( !HasMemoryCardPassword( driveInfoT ) && !IsMemoryCardLocked( driveInfoT ) )
       
   940                 { 
       
   941                 TMediaPassword memoryCardPassword;
       
   942                 TMediaPassword nullMemoryCardPassword;
       
   943                 // Converts MemoryCardpassword
       
   944                 ConvertMemoryCardPassword( memoryCardPassword, EFalse );
       
   945                 // Locks MemoryCard drive
       
   946                 err = fsMemoryCard.LockDrive( i, nullMemoryCardPassword, memoryCardPassword, ETrue );        
       
   947                 if ( err == KErrNone )
       
   948                     {
       
   949                     if ( iMemoryCardPasswd )
       
   950                           {
       
   951                           delete iMemoryCardPasswd;
       
   952                           iMemoryCardPasswd = NULL; 
       
   953                           }
       
   954                      iMemoryCardPasswd = iRemoteLockCode->AllocL();
       
   955                      }
       
   956           
       
   957                 fsMemoryCard.Close();
       
   958                 RL_TRACE_PRINT(" [ rl.exe ] exit DoSetMemoryCardPasswdL() change");
       
   959                 return ( err == KErrNone );  
       
   960                 }
       
   961             else
       
   962                 {
       
   963                 fsMemoryCard.Close();  
       
   964                 return EFalse;
       
   965                 }   
       
   966             }
       
   967         }    
       
   968     fsMemoryCard.Close();
       
   969     return EFalse;  
       
   970     
       
   971 #endif //RD_MULTIPLE_DRIVE       
       
   972     }
       
   973 
       
   974 // ---------------------------------------------------------------------------
       
   975 // CRemoteLock::ConvertMemoryCardPasswordL
       
   976 // Converts MemoryCardPassword to acceptable format.
       
   977 // ---------------------------------------------------------------------------
       
   978 //
       
   979 void CRemoteLock::ConvertMemoryCardPassword( TMediaPassword& aPassword, const TBool aClear ) 
       
   980     {
       
   981     RL_TRACE_PRINT(" [ rl.exe ] ConvertMemoryCardPassword() ");
       
   982     
       
   983     TBuf16 < KMaxMediaPassword / 2 > memoryCardPassword; 
       
   984     // Takes left most 8 digits of lockcode as
       
   985     // the password of MemoryCard.
       
   986     RL_TRACE_PRINT(" [ rl.exe ] ConvertMemoryCardPassword() 1");
       
   987     
       
   988     if ( aClear )
       
   989         {
       
   990         if ( iMemoryCardPasswd )
       
   991            {
       
   992            memoryCardPassword.Copy( iMemoryCardPasswd->Left( KRLockMaxMemoryCardPasswdLength ) );
       
   993            }
       
   994         }
       
   995      else
       
   996         {
       
   997         if ( iRemoteLockCode )
       
   998             {
       
   999             memoryCardPassword.Copy( iRemoteLockCode->Left( KRLockMaxMemoryCardPasswdLength ) );
       
  1000             }
       
  1001         } 
       
  1002   
       
  1003     // Fills the descriptor's data area with binary zeroes, i.e. 0x00, 
       
  1004     // replacing any existing data.
       
  1005     RL_TRACE_PRINT(" [ rl.exe ] ConvertMemoryCardPassword() 2");
       
  1006     aPassword.FillZ( KMaxMediaPassword ); 
       
  1007 
       
  1008     aPassword.Zero();
       
  1009     // Sets the length of the data to zero.
       
  1010     // Copies data into this descriptor replacing any existing data. 
       
  1011     // The length of this descriptor is set to reflect the new data
       
  1012     RL_TRACE_PRINT(" [ rl.exe ] ConvertMemoryCardPassword() 3");
       
  1013     aPassword.Copy( reinterpret_cast<TUint8 *>
       
  1014         ( &memoryCardPassword[ 0 ] ), memoryCardPassword.Length() * 2 );
       
  1015         
       
  1016     RL_TRACE_PRINT(" [ rl.exe ] ConvertMemoryCardPassword() over ");    
       
  1017     }
       
  1018   
       
  1019   
       
  1020 // ---------------------------------------------------------------------------
       
  1021 // CRemoteLock::InitialSendMessageL
       
  1022 // Reply SMS when the terminal is successfully locked.
       
  1023 // ---------------------------------------------------------------------------
       
  1024 //
       
  1025 TBool CRemoteLock::InitialSendMessageL()
       
  1026     {
       
  1027     RL_TRACE_PRINT(" [ rl.exe ] InitialSendMessageL() ");
       
  1028     
       
  1029     RProperty property;
       
  1030     
       
  1031     TInt err = 0;
       
  1032      
       
  1033     #ifndef RD_STARTUP_CHANGE        
       
  1034     err = property.Attach( KUidSystemCategory, KPSUidAutolockStatusValue );              
       
  1035     #else
       
  1036     err = property.Attach( KPSUidCoreApplicationUIs, KCoreAppUIsAutolockStatus );
       
  1037     #endif
       
  1038     
       
  1039     if ( err != KErrNone )
       
  1040         {
       
  1041       return EFalse;
       
  1042         }
       
  1043     
       
  1044     TInt state = 0;
       
  1045     
       
  1046     CleanupClosePushL( property );
       
  1047     
       
  1048     err = property.Get( state );
       
  1049     if ( err != KErrNone )
       
  1050         {
       
  1051       CleanupStack::PopAndDestroy( 1 );
       
  1052         return EFalse;
       
  1053         }
       
  1054          
       
  1055     #ifndef RD_STARTUP_CHANGE
       
  1056     if ( state  == EPSRemoteLocked )
       
  1057     #else
       
  1058     if ( state == ERemoteLocked )
       
  1059     #endif // RD_STARTUP_CHANGE 
       
  1060       {
       
  1061       // message server entry id
       
  1062       TMsvId msvId = NULL;
       
  1063       // Set up a new message
       
  1064       msvId = CreateNewMessageL();
       
  1065       //Set the new message to be the current entry
       
  1066       SetEntryL( msvId );
       
  1067       CleanupStack::PopAndDestroy( 1 ); // property
       
  1068       RL_TRACE_PRINT(" [ rl.exe ] exit InitialSendMessageL() ETrue "); 
       
  1069       return ETrue; 
       
  1070       }
       
  1071     CleanupStack::PopAndDestroy( 1 ); // property   
       
  1072     RL_TRACE_PRINT(" [ rl.exe ] exit InitialSendMessageL() "); 
       
  1073     return EFalse;
       
  1074     }
       
  1075 
       
  1076 
       
  1077 // ---------------------------------------------------------------------------
       
  1078 // CRemoteLock::CreateNewMessageL()
       
  1079 // Creates a new message server entry and set up default values.
       
  1080 // Return values:      TMsvId (the id of created entry)
       
  1081 // ---------------------------------------------------------------------------
       
  1082 //
       
  1083 TMsvId CRemoteLock::CreateNewMessageL()
       
  1084     {
       
  1085     RL_TRACE_PRINT(" [ rl.exe ] CreateNewMessageL() ");
       
  1086     TMsvEntry newEntry;
       
  1087     // The type of message is SMS
       
  1088     newEntry.iMtm = KUidMsgTypeSMS;
       
  1089     // The type of the entry: message
       
  1090     newEntry.iType = KUidMsvMessageEntry;                    
       
  1091     newEntry.iServiceId = KMsvLocalServiceIndexEntryId;
       
  1092     // Set the date of the entry to home time
       
  1093     newEntry.iDate.UniversalTime();                           
       
  1094     newEntry.SetInPreparation( ETrue );                      
       
  1095 
       
  1096     CMsvEntry* entry = CMsvEntry::NewL( *iMsvSession, 
       
  1097         KMsvDraftEntryIdValue, TMsvSelectionOrdering() );
       
  1098     CleanupStack::PushL( entry );
       
  1099     CMsvOperationWait* wait = CMsvOperationWait::NewLC();
       
  1100     wait->Start();    
       
  1101 
       
  1102     // Asynchronously create a new entry.    
       
  1103     CMsvOperation* oper = entry->CreateL( newEntry, wait->iStatus );
       
  1104     CleanupStack::PushL( oper );
       
  1105 
       
  1106     CActiveScheduler::Start();
       
  1107 
       
  1108     // Keep track of the progress of the create operation.
       
  1109     TMsvLocalOperationProgress progress = 
       
  1110         McliUtils::GetLocalProgressL( *oper );
       
  1111     User::LeaveIfError( progress.iError );
       
  1112 
       
  1113     // Set entry context to the created one
       
  1114     // operation progress contains the ID of the created entry
       
  1115     entry->SetEntryL( progress.iId ); 
       
  1116 
       
  1117     CleanupStack::PopAndDestroy( 3 );
       
  1118     RL_TRACE_PRINT(" [ rl.exe ] CreateNewMessageL() end ");
       
  1119     return progress.iId;
       
  1120     }
       
  1121 
       
  1122 
       
  1123 // ---------------------------------------------------------------------------
       
  1124 // CRemoteLock::SetEntryL(TMsvId aEntryId)
       
  1125 // Set up current message entry.
       
  1126 // Note: It can be useful to remember the original entry id for 
       
  1127 //       error handling.
       
  1128 // ---------------------------------------------------------------------------
       
  1129 //
       
  1130 void CRemoteLock::SetEntryL( TMsvId aEntryId )
       
  1131     {
       
  1132     RL_TRACE_PRINT(" [ rl.exe ] SetEntryL() ");
       
  1133     // Get the server entry from our session
       
  1134     CMsvEntry* entry = iMsvSession->GetEntryL( aEntryId );
       
  1135     CleanupStack::PushL( entry );
       
  1136     delete iClientMtmForSending;
       
  1137     iClientMtmForSending = NULL;
       
  1138     
       
  1139     TInt error;
       
  1140     TInt i;
       
  1141     for ( i = 0; i < 100; i++ )
       
  1142         {
       
  1143         TRAP( error, 
       
  1144             iClientMtmForSending = iMtmReg->NewMtmL( entry->Entry().iMtm ) );
       
  1145         if ( error == KErrNone )
       
  1146             {
       
  1147              RL_TRACE_PRINT(" [ rl.exe ] SetEntryL() iClientMtmForSending ");
       
  1148             break;
       
  1149             }
       
  1150         } 
       
  1151 
       
  1152     User::LeaveIfError( error );
       
  1153     iClientMtmForSending->SetCurrentEntryL( entry );
       
  1154 
       
  1155     CleanupStack::Pop( 1 ); // entry
       
  1156     RL_TRACE_PRINT(" [ rl.exe ] exit SetEntryL() ");
       
  1157     entry = NULL;
       
  1158     }
       
  1159 
       
  1160 
       
  1161 // ---------------------------------------------------------------------------
       
  1162 // CRemoteLock::MoveMessageEntryL(TMsvId aTarget) const
       
  1163 // Moves an entry to another parent.
       
  1164 // Return values:      TMsvId of the moved message
       
  1165 // ---------------------------------------------------------------------------
       
  1166 //
       
  1167 TMsvId CRemoteLock::MoveMessageEntryL( TMsvId aTarget )
       
  1168     {
       
  1169     User::LeaveIfNull( iClientMtmForSending );
       
  1170     TMsvEntry msvEntry( ( iClientMtmForSending->Entry() ).Entry() );
       
  1171     TMsvId id = msvEntry.Id();
       
  1172 
       
  1173     if ( msvEntry.Parent() != aTarget )
       
  1174         {
       
  1175         TMsvSelectionOrdering sort;
       
  1176         sort.SetShowInvisibleEntries( ETrue );    
       
  1177         // Take a handle to the parent entry
       
  1178         CMsvEntry* parentEntry = 
       
  1179             CMsvEntry::NewL( 
       
  1180             iClientMtmForSending->Session(), msvEntry.Parent(), sort );
       
  1181 
       
  1182         CleanupStack::PushL( parentEntry );
       
  1183     
       
  1184         // Move original from the parent to the new location
       
  1185         CMsvOperationWait* wait = CMsvOperationWait::NewLC();
       
  1186         wait->Start();
       
  1187     
       
  1188         CMsvOperation* op = 
       
  1189                 parentEntry->MoveL( msvEntry.Id(), aTarget, wait->iStatus );
       
  1190 
       
  1191         CleanupStack::PushL( op );
       
  1192         CActiveScheduler::Start();     
       
  1193         TMsvLocalOperationProgress prog=McliUtils::GetLocalProgressL( *op );
       
  1194         User::LeaveIfError(prog.iError);
       
  1195     
       
  1196         id = prog.iId; 
       
  1197         CleanupStack::PopAndDestroy( 3 ); // op, wait, parentEntry
       
  1198         }
       
  1199     return id;
       
  1200     }
       
  1201 
       
  1202 
       
  1203 // ---------------------------------------------------------------------------
       
  1204 // CRemoteLock::SendMessageL
       
  1205 // Reply SMS when the terminal is successfully locked.
       
  1206 // Return values:      ETrue or EFalse
       
  1207 // ---------------------------------------------------------------------------
       
  1208 //
       
  1209 TBool CRemoteLock::SendMessageL( const TDesC& aRecipientAddress )
       
  1210     {
       
  1211     RL_TRACE_PRINT(" [ rl.exe ] SendMessageL() ");
       
  1212     
       
  1213     User::LeaveIfNull( iClientMtmForSending );
       
  1214     
       
  1215     RL_TRACE_PRINT(" [ rl.exe ] SendMessageL() 1 ");
       
  1216     
       
  1217     TMsvEntry msvEntry = ( iClientMtmForSending->Entry() ).Entry();
       
  1218 
       
  1219     // We get the message body from Mtm and insert a bodytext
       
  1220     CRichText& mtmBody = iClientMtmForSending->Body();
       
  1221     mtmBody.Reset();
       
  1222     
       
  1223     TFileName fileName; 
       
  1224     fileName = KRLockResourceFileName;
       
  1225 
       
  1226     // Use CStringResourceReader instead of StringLoader
       
  1227     // StringLoader is meant for applications in app framework   
       
  1228     CStringResourceReader* resourceReader = CStringResourceReader::NewLC( fileName );
       
  1229 
       
  1230     RL_TRACE_PRINT(" [ rl.exe ] SendMessageL() 3");
       
  1231     
       
  1232     HBufC* retBuf = NULL;
       
  1233 
       
  1234     if ( iStateMemoryCard )
       
  1235         {
       
  1236         RL_TRACE_PRINT(" [ rl.exe ] SendMessageL() memory card locked");
       
  1237         // When memory card is locked.
       
  1238         TPtrC buf;
       
  1239         buf.Set( resourceReader->ReadResourceString(R_RLOC_TI_EVERYTHING_LOCKED) );
       
  1240         retBuf = buf.AllocLC();
       
  1241         }
       
  1242     else
       
  1243         {
       
  1244         RL_TRACE_PRINT(" [ rl.exe ] SendMessageL() only phone locked ");
       
  1245         TPtrC buf;
       
  1246         buf.Set( resourceReader->ReadResourceString(R_RLOC_TI_PHONE_LOCKED) );
       
  1247         retBuf = buf.AllocLC();
       
  1248         }
       
  1249     mtmBody.InsertL( 0, *retBuf );
       
  1250     msvEntry.iDescription.Set( *retBuf );
       
  1251      
       
  1252     // Set aRecipientAddress into the Details of the entry
       
  1253     msvEntry.iDetails.Set( aRecipientAddress );  
       
  1254     msvEntry.SetInPreparation( EFalse );         
       
  1255     
       
  1256     // Set the sending state (immediately)
       
  1257     msvEntry.SetSendingState( KMsvSendStateWaiting );                         
       
  1258     msvEntry.iDate.UniversalTime();   
       
  1259     // To handle the sms specifics we start using SmsMtm
       
  1260     CSmsClientMtm* smsMtm = STATIC_CAST( CSmsClientMtm*, 
       
  1261                                          iClientMtmForSending );
       
  1262     smsMtm->RestoreServiceAndSettingsL();
       
  1263 
       
  1264     // SMS MTM encapsulation of an SMS message.
       
  1265     CSmsHeader& header = smsMtm->SmsHeader();
       
  1266     CSmsSettings* sendOptions = CSmsSettings::NewL();
       
  1267     CleanupStack::PushL( sendOptions );
       
  1268     // Reset existing settings
       
  1269     sendOptions->CopyL( smsMtm->ServiceSettings() ); 
       
  1270     // Set unicode if needed
       
  1271     if ( NeedsToBeSentAsUnicodeL( retBuf->Des() ))
       
  1272         {
       
  1273         sendOptions->SetCharacterSet( TSmsDataCodingScheme::ESmsAlphabetUCS2 );
       
  1274         }
       
  1275     // Set send options to be delivered immediately
       
  1276     sendOptions->SetDelivery( ESmsDeliveryImmediately );      
       
  1277     header.SetSmsSettingsL( *sendOptions );
       
  1278     
       
  1279     // let's check if there's sc address
       
  1280     if (header.Message().ServiceCenterAddress().Length() == 0)
       
  1281         {
       
  1282         // no, there isn't. Use the default SC number. 
       
  1283         CSmsSettings* serviceSettings = NULL;
       
  1284         serviceSettings = &( smsMtm->ServiceSettings() );
       
  1285         
       
  1286         RL_TRACE_PRINT(" [ rl.exe ] SendMessageL() Get SC number ");    
       
  1287         if ( serviceSettings->ServiceCenterCount() )
       
  1288             {
       
  1289             //Set sc address to default.
       
  1290             CSmsServiceCenter& sc = serviceSettings->GetServiceCenter(
       
  1291                 serviceSettings->DefaultServiceCenter() );
       
  1292             header.Message().SetServiceCenterAddressL( sc.Address() );
       
  1293             }
       
  1294         }
       
  1295     
       
  1296     // Add recipient to the list.
       
  1297     smsMtm->AddAddresseeL( aRecipientAddress, msvEntry.iDetails );
       
  1298 
       
  1299     CMsvEntry& entry = iClientMtmForSending->Entry();
       
  1300     entry.ChangeL( msvEntry );                
       
  1301     smsMtm->SaveMessageL();                 
       
  1302 
       
  1303     // Move message to outbox
       
  1304     TMsvId movedId;
       
  1305     TInt err;
       
  1306     for ( TInt i = 0; i < 100; i++ )
       
  1307         {
       
  1308         TRAP( err, movedId = MoveMessageEntryL( 
       
  1309                                  KMsvGlobalOutBoxIndexEntryId ));  
       
  1310         if ( err == KErrNone )
       
  1311             break;
       
  1312         }
       
  1313     RL_TRACE_PRINT(" [ rl.exe ] SendMessageL() put created message in outbox ");
       
  1314     User::LeaveIfError( err );
       
  1315     // We must create an entry selection for message copies 
       
  1316     CMsvEntrySelection* selection = new ( ELeave ) CMsvEntrySelection;
       
  1317     CleanupStack::PushL( selection );
       
  1318     selection->AppendL( movedId );        
       
  1319     // schedule the sending with the active scheduler
       
  1320     SetScheduledSendingStateL( selection );   
       
  1321     CleanupStack::PopAndDestroy( 4 ); // resourceReader,retbuf,sendOptions,selection 
       
  1322     RL_TRACE_PRINT(" [ rl.exe ] Exit SendMessageL() ");
       
  1323     return ETrue; 
       
  1324     }
       
  1325 
       
  1326 
       
  1327 // ---------------------------------------------------------------------------
       
  1328 // CRemoteLock::SetScheduledSendingStateL
       
  1329 // Schedules the message to be sent through the etel server.
       
  1330 // Return values:      none
       
  1331 // ---------------------------------------------------------------------------
       
  1332 //
       
  1333 void CRemoteLock::SetScheduledSendingStateL( CMsvEntrySelection* aSelection )
       
  1334     {
       
  1335     User::LeaveIfNull( iClientMtmForSending );
       
  1336     // Add entry to task scheduler
       
  1337     TBuf8<1> dummyParams;
       
  1338     CMsvOperationWait* waiter = CMsvOperationWait::NewLC();
       
  1339     waiter->Start();
       
  1340 
       
  1341     // invoking async schedule copy command on our mtm
       
  1342     CMsvOperation* op= iClientMtmForSending->InvokeAsyncFunctionL(
       
  1343             ESmsMtmCommandScheduleCopy,
       
  1344             *aSelection,
       
  1345             dummyParams,
       
  1346             waiter->iStatus );
       
  1347 
       
  1348     CleanupStack::PushL( op );
       
  1349     CActiveScheduler::Start();
       
  1350 
       
  1351     CleanupStack::PopAndDestroy( 2 ); // op, wait 
       
  1352     }
       
  1353 
       
  1354 
       
  1355 // ---------------------------------------------------------------------------
       
  1356 // CRemoteLock::RunL
       
  1357 // Reconnect msg server after it is shut down
       
  1358 // Return values:      none
       
  1359 // ---------------------------------------------------------------------------
       
  1360 //
       
  1361 void CRemoteLock::RunL()
       
  1362     {
       
  1363     RL_TRACE_PRINT(" [ rl.exe ] RunL() ");
       
  1364     delete iClientMtmForSending;
       
  1365     iClientMtmForSending = NULL;
       
  1366     delete iMtmReg;
       
  1367     iMtmReg = NULL;
       
  1368     delete iMsvSession;
       
  1369     iMsvSession = NULL;
       
  1370 
       
  1371     TRAPD( error, iMsvSession = CMsvSession::OpenAsyncL( *this ) );
       
  1372 
       
  1373     if ( error != KErrNone )
       
  1374         {
       
  1375         After( KRLockTimerInterval );
       
  1376         }
       
  1377     RL_TRACE_PRINT(" [ rl.exe ] Exit RunL() ");    
       
  1378     }
       
  1379 
       
  1380 
       
  1381 // ---------------------------------------------------------------------------
       
  1382 // CRemoteLock::NeedsToBeSentAsUnicodeL
       
  1383 // Check if needs to be sent as unicode
       
  1384 // Return value ETrue or EFalse
       
  1385 // ---------------------------------------------------------------------------
       
  1386 //
       
  1387 TBool CRemoteLock::NeedsToBeSentAsUnicodeL( const TDesC& aInputString ) const
       
  1388     {
       
  1389     TBool needsToBeSentAsUnicode = EFalse;
       
  1390     CCnvCharacterSetConverter* const
       
  1391         characterSetConverter = CCnvCharacterSetConverter::NewLC();
       
  1392     
       
  1393     RFs fsSession;
       
  1394     TInt err = fsSession.Connect();
       
  1395     User::LeaveIfError( err );
       
  1396     CleanupClosePushL( fsSession ); 
       
  1397     const TUint KSmsEdSmsStrictPluginID = 0x101F85CD;
       
  1398     characterSetConverter->PrepareToConvertToOrFromL(
       
  1399         KSmsEdSmsStrictPluginID,
       
  1400         fsSession);
       
  1401 
       
  1402     characterSetConverter->SetDowngradeForExoticLineTerminatingCharacters(
       
  1403         CCnvCharacterSetConverter::EDowngradeExoticLineTerminatingCharactersToJustLineFeed );
       
  1404    
       
  1405     for (TPtrC remainderOfInputString( aInputString ); remainderOfInputString.Length()>0 ; )
       
  1406         {
       
  1407         TBuf8<KBufLengthInUnicodeCheck> notUsed;
       
  1408         TInt numberOfUnconvertibleCharacters = 0;
       
  1409         const TInt returnValue = 
       
  1410             characterSetConverter->ConvertFromUnicode( 
       
  1411                 notUsed,
       
  1412                 remainderOfInputString, 
       
  1413                 numberOfUnconvertibleCharacters );
       
  1414         if (( returnValue < 0 ) || ( numberOfUnconvertibleCharacters > 0 )) 
       
  1415             // if there was an error in trying to do the conversion, or if there was an
       
  1416             // unconvertible character (e.g. a Chinese character)
       
  1417             {
       
  1418             needsToBeSentAsUnicode = ETrue;
       
  1419             break;
       
  1420             }
       
  1421         
       
  1422         remainderOfInputString.Set( remainderOfInputString.Right( returnValue ));
       
  1423         }
       
  1424     CleanupStack::PopAndDestroy( 2 );
       
  1425     return needsToBeSentAsUnicode;
       
  1426     }
       
  1427 
       
  1428 
       
  1429 // ---------------------------------------------------------------------------
       
  1430 // CRemoteLock::HandleUnlockEvent()
       
  1431 // Callback function for unlocking event observer
       
  1432 // ---------------------------------------------------------------------------
       
  1433 void CRemoteLock::HandleUnlockEvent()
       
  1434     { 
       
  1435     TRAPD( err, SetMemoryCardPasswdL( ETrue ) );
       
  1436     err = err;
       
  1437     iLockedByRL = EFalse;
       
  1438     }   
       
  1439 
       
  1440 // -----------------------------------------------------------------------------
       
  1441 // CRemoteLock::ProfileNotifyL
       
  1442 // Setup Profile notifier
       
  1443 // Returns:   ETrue if everything is OK
       
  1444 //            EFalse otherwise
       
  1445 // -----------------------------------------------------------------------------
       
  1446 //
       
  1447 TBool CRemoteLock::ProfileNotifyL( 
       
  1448     const TBool aNotifyEnable ) 
       
  1449     {
       
  1450     RL_TRACE_PRINT(" [ rl.exe ] ProfileNotifyL() ");
       
  1451     
       
  1452     if ( !iProfileNotifyHandler )
       
  1453         {
       
  1454         RL_TRACE_PRINT(" [ rl.exe ] ProfileNotifyL() create notify handler");
       
  1455         iProfileNotifyHandler = CCenRepNotifyHandler::NewL( *this, *iProfileSession ); 
       
  1456         }
       
  1457     
       
  1458     if ( aNotifyEnable )
       
  1459         {
       
  1460         
       
  1461         TInt err = iProfileSession->Get( KProEngActiveProfile, iCurrentProfile );
       
  1462         User::LeaveIfError( err );
       
  1463         RL_TRACE_PRINT(" [ rl.exe ] ProfileNotifyL() startlisten");
       
  1464         iProfileNotifyHandler->StartListeningL();  
       
  1465         }
       
  1466     else
       
  1467         {
       
  1468         RL_TRACE_PRINT(" [ rl.exe ] ProfileNotifyL() stop listen ");
       
  1469         iProfileNotifyHandler->StopListening();
       
  1470         
       
  1471         delete iProfileNotifyHandler;
       
  1472         iProfileNotifyHandler = NULL;
       
  1473         }
       
  1474     RL_TRACE_PRINT(" [ rl.exe ] exit ProfileNotifyL() ");
       
  1475     return ETrue;
       
  1476     }
       
  1477     
       
  1478 // -----------------------------------------------------------------------------
       
  1479 // CRemoteLock::GetProfile
       
  1480 // Get the current Profile
       
  1481 // Returns:   ETrue if everything is OK
       
  1482 //            EFalse otherwise
       
  1483 // -----------------------------------------------------------------------------
       
  1484 //
       
  1485 TBool CRemoteLock::GetProfile( TInt& aProfile )
       
  1486     {
       
  1487     RL_TRACE_PRINT(" [ rl.exe ] GetProfile() ");
       
  1488     TInt err;
       
  1489     err = iProfileSession->Get( KProEngActiveProfile, aProfile );
       
  1490     RL_TRACE_PRINT(" [ rl.exe ] exit GetProfile() ");
       
  1491     return ( err == KErrNone );
       
  1492     }    
       
  1493 
       
  1494 
       
  1495 // -----------------------------------------------------------------------------
       
  1496 // CRemoteLock::HandleNotifyGeneric
       
  1497 // Remote lock Notify handler 
       
  1498 //
       
  1499 // -----------------------------------------------------------------------------
       
  1500 //
       
  1501 void CRemoteLock::HandleNotifyGeneric(
       
  1502     TUint32 /*aId*/ ) 
       
  1503     {
       
  1504     RL_TRACE_PRINT(" [ rl.exe ] HandleNotifyGeneric() "); 
       
  1505     
       
  1506     TInt profile = 0; 
       
  1507     GetProfile( profile ); 
       
  1508     
       
  1509     if ( ( profile == EProfileOffLineId ) && ( iIsEnabled  ) && ( iCurrentProfile != EProfileOffLineId )) 
       
  1510         {
       
  1511         ActivateDeviceLock();  
       
  1512         }
       
  1513         
       
  1514     iCurrentProfile = profile;
       
  1515     RL_TRACE_PRINT(" [ rl.exe ] exit HandleNotifyGeneric() ");
       
  1516     }
       
  1517     
       
  1518 //EOF  
       
  1519