harvester/server/src/restorewatcher.cpp
changeset 0 c53acadfccc6
child 1 acef663c1218
equal deleted inserted replaced
-1:000000000000 0:c53acadfccc6
       
     1 /*
       
     2 * Copyright (c) 2008-2009 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:  This implements CRestoreWatcher class.
       
    15 *
       
    16 */
       
    17 
       
    18 // SYSTEM INCLUDE
       
    19 #include <centralrepository.h>
       
    20 #include <sbdefs.h>
       
    21 #include <driveinfo.h>
       
    22 #include <PathInfo.h>
       
    23 
       
    24 // USER INCLUDE
       
    25 #include "restorewatcher.h"
       
    26 #include "harvesterlog.h"
       
    27 #include "propertywatcher.h"
       
    28 #include "clientkeywatcherkeys.h"
       
    29 
       
    30 // STATIC MEMBERS
       
    31 TInt CRestoreWatcher::iRegisteredClients = 0;
       
    32 TBool CRestoreWatcher::iRestoreDone = EFalse;
       
    33 
       
    34 
       
    35 // ============================ MEMBER FUNCTIONS ===============================
       
    36 
       
    37 // -----------------------------------------------------------------------------
       
    38 // CRestoreWatcher::NewL
       
    39 // Two-phased constructor.
       
    40 // -----------------------------------------------------------------------------
       
    41 //
       
    42 CRestoreWatcher* CRestoreWatcher::NewL()
       
    43 	{
       
    44 	WRITELOG("CRestoreWatcher* CRestoreWatcher::NewL()");
       
    45 	CRestoreWatcher* self = new (ELeave) CRestoreWatcher();
       
    46 	CleanupStack::PushL( self );
       
    47 	self->ConstructL();
       
    48 	CleanupStack::Pop( self );
       
    49 	return self;
       
    50 	}
       
    51 
       
    52 // -----------------------------------------------------------------------------
       
    53 // CRestoreWatcher::CRestoreWatcher
       
    54 // Standard C++ constructor.
       
    55 // -----------------------------------------------------------------------------
       
    56 //
       
    57 CRestoreWatcher::CRestoreWatcher() : iPropertyWatcher( NULL ), iClients( 0 )
       
    58 	{
       
    59 	// No implementation required.
       
    60 	}
       
    61 
       
    62 // -----------------------------------------------------------------------------
       
    63 // CRestoreWatcher::~CRestoreWatcher
       
    64 // Standard C++ destructor.
       
    65 // -----------------------------------------------------------------------------
       
    66 //
       
    67 CRestoreWatcher::~CRestoreWatcher()
       
    68 	{
       
    69 	
       
    70 	if( iPropertyWatcher )
       
    71 		{
       
    72 		iPropertyWatcher->StopListeningKeyChanges( 
       
    73 				KUidSystemCategory,
       
    74 				conn::KUidBackupRestoreKey, this );
       
    75 		
       
    76 	
       
    77 		iPropertyWatcher->StopListeningKeyChanges( 
       
    78 				KPSRestoreWatcherCategory,
       
    79 				KPSRestoreWatcherClientsKey, this );
       
    80 		
       
    81 		iPropertyWatcher->Delete(); // Release connection to TLS object.
       
    82 		}
       
    83 	}
       
    84 
       
    85 // -----------------------------------------------------------------------------
       
    86 // CRestoreWatcher::ConstructL
       
    87 // Symbian 2nd phase constructor.
       
    88 // -----------------------------------------------------------------------------
       
    89 //
       
    90 void CRestoreWatcher::ConstructL()
       
    91 	{
       
    92 	WRITELOG("CRestoreWatcher::ConstructL()");
       
    93 
       
    94 	CreateFileNameL();
       
    95 	CheckRestoreL();
       
    96     WRITELOG1("CRestoreWatcher::ConstructL() - iRestoreDone: %d", iRestoreDone );
       
    97 	
       
    98 	WRITELOG("CRestoreWatcher::ConstructL() - creating restore watcher PS key");
       
    99 	RProperty clientsProperty;
       
   100     const TInt error = clientsProperty.Define(
       
   101     		KPSRestoreWatcherCategory, 
       
   102 			KPSRestoreWatcherClientsKey,
       
   103 			RProperty::EInt );
       
   104 	
       
   105     clientsProperty.Close();
       
   106 	
       
   107 	if( error != KErrAlreadyExists ) // KErrAlreadyExists not error.
       
   108 		{
       
   109 		User::LeaveIfError( error );
       
   110 		}
       
   111 	
       
   112 	iPropertyWatcher = CPropertyWatcher::GetInstanceL();
       
   113 	
       
   114 	// Want to listen when backup/restore starts.
       
   115 	// Calls NotifyKeyL when key's state has changed. 
       
   116 	iPropertyWatcher->ListenKeyChangesL( 
       
   117 			KUidSystemCategory,
       
   118 			conn::KUidBackupRestoreKey, this );
       
   119 	
       
   120 	// Listen message clients.
       
   121 	iPropertyWatcher->ListenKeyChangesL( 
       
   122 			KPSRestoreWatcherCategory,
       
   123 			KPSRestoreWatcherClientsKey, this );
       
   124 	
       
   125 	
       
   126 	WRITELOG("CRestoreWatcher::ConstructL() ends");
       
   127 	}
       
   128 	
       
   129 // -----------------------------------------------------------------------------
       
   130 // CRestoreWatcher::CreateFileNameL
       
   131 // Filename flag can be changed to use cen rep!
       
   132 // -----------------------------------------------------------------------------
       
   133 //
       
   134 void CRestoreWatcher::CreateFileNameL() 
       
   135 	{
       
   136 	TInt drive ( 0 );
       
   137 	User::LeaveIfError( 
       
   138 			DriveInfo::GetDefaultDrive( DriveInfo::EDefaultSystem, drive ) );
       
   139 
       
   140     RFs fsSession;
       
   141     User::LeaveIfError( fsSession.Connect() );
       
   142   
       
   143     TChar driveLetter;
       
   144     fsSession.DriveToChar( drive, driveLetter );
       
   145 	
       
   146     iFileName.Append( driveLetter );
       
   147     iFileName.Append( KRestoreFile );  // result-> C:\\private\\200009F5\\restoredone
       
   148  
       
   149     fsSession.Close();
       
   150 	}
       
   151 
       
   152 
       
   153 // -----------------------------------------------------------------------------
       
   154 // CRestoreWatcher::NotifyKeyL
       
   155 // CPropertyWatcher's callback.
       
   156 // -----------------------------------------------------------------------------
       
   157 //
       
   158 void CRestoreWatcher::NotifyKeyL( 
       
   159 		const TInt aKeyValue,
       
   160 		const TUid aPropertyCategory,
       
   161 		const TUint aKey )
       
   162 	{
       
   163 	WRITELOG("CRestoreWatcher::NotifyKeyL() - begin");
       
   164 	
       
   165 	if( aPropertyCategory == KUidSystemCategory &&
       
   166 			aKey == conn::KUidBackupRestoreKey )
       
   167 		{
       
   168 		HandleBackupRestoreKeyActionL( aKeyValue );
       
   169 		}
       
   170 	
       
   171 	if( aPropertyCategory == KPSRestoreWatcherCategory &&
       
   172 			aKey == KPSRestoreWatcherClientsKey )
       
   173 		{
       
   174 		HandleClientsKeyActionL( aKeyValue );
       
   175 		}
       
   176 	
       
   177 	WRITELOG("CRestoreWatcher::NotifyKeyL() - end");
       
   178 	}
       
   179 
       
   180 // -----------------------------------------------------------------------------
       
   181 // CRestoreWatcher::HandleBackupKeyActionL
       
   182 // When user starts restore sets flag on.
       
   183 // -----------------------------------------------------------------------------
       
   184 //
       
   185 void CRestoreWatcher::HandleBackupRestoreKeyActionL( const TUint aKeyValue )
       
   186 	{	
       
   187 	WRITELOG("CRestoreWatcher::HandleBackupKeyActionL() - begin");
       
   188 	
       
   189 	TInt backupStateValue = aKeyValue;
       
   190 	backupStateValue &= conn::KBURPartTypeMask;
       
   191 
       
   192 	if ( backupStateValue == conn::EBURRestoreFull ||
       
   193 	     backupStateValue == conn::EBURRestorePartial )
       
   194 		{
       
   195 		WRITELOG("CRestoreWatcher::RunL() - setting restore to TRUE");
       
   196 		SetRestoreFlagL( ETrue );
       
   197 		}
       
   198 	
       
   199 	WRITELOG("CRestoreWatcher::HandleBackupKeyActionL() - end");
       
   200 	}
       
   201 
       
   202 // -----------------------------------------------------------------------------
       
   203 // CRestoreWatcher::HandleClientsKeyActionL
       
   204 // Handle message clients registering to watcher.
       
   205 // -----------------------------------------------------------------------------
       
   206 //
       
   207 void CRestoreWatcher::HandleClientsKeyActionL( const TUint aKeyValue )
       
   208 	{	
       
   209 	WRITELOG("CRestoreWatcher::HandleBackupKeyActionL() - begin");
       
   210 
       
   211 	// aKeyValue contains clients count.
       
   212 	if ( aKeyValue < iClients )
       
   213 		{
       
   214 		WRITELOG("CRestoreWatcher::HandleClientsKeyActionL - unregistering");
       
   215 		UnregisterL();
       
   216 		}
       
   217 	else if ( aKeyValue > iClients )
       
   218 		{
       
   219 		WRITELOG("CRestoreWatcher::HandleClientsKeyActionL - registering");
       
   220 		Register();
       
   221 		}
       
   222 	
       
   223 	iClients = aKeyValue;
       
   224 
       
   225 	WRITELOG("CRestoreWatcher::HandleBackupKeyActionL() - end");	
       
   226 	}
       
   227 
       
   228 // -----------------------------------------------------------------------------
       
   229 // CRestoreWatcher::Register
       
   230 // Registers client to watcher.
       
   231 // -----------------------------------------------------------------------------
       
   232 //
       
   233 TBool CRestoreWatcher::Register()
       
   234 	{	
       
   235 	WRITELOG("CRestoreWatcher::Register()");
       
   236 	if ( iRestoreDone )
       
   237 		{
       
   238 		WRITELOG("CRestoreWatcher::Register() - partial restore was done.");
       
   239 		iRegisteredClients++;
       
   240 		WRITELOG1("CRestoreWatcher::Register() - registered clients: %d", iRegisteredClients);
       
   241 		}
       
   242 	
       
   243 	return iRestoreDone;
       
   244 	}
       
   245 
       
   246 // -----------------------------------------------------------------------------
       
   247 // CRestoreWatcher::UnregisterL
       
   248 // Unregister client to watcher.
       
   249 // -----------------------------------------------------------------------------
       
   250 //
       
   251 void CRestoreWatcher::UnregisterL()
       
   252 	{
       
   253 	WRITELOG("CRestoreWatcher::UnregisterL()");
       
   254 	if ( iRegisteredClients <= 0 )
       
   255 		{
       
   256 		WRITELOG("CRestoreWatcher::UnregisterL() - 0 clients");
       
   257 		return;
       
   258 		}
       
   259 	
       
   260 	iRegisteredClients--;
       
   261 	WRITELOG1("CRestoreWatcher::UnregisterL() - registered clients: %d", iRegisteredClients);
       
   262 	
       
   263 	if ( iRegisteredClients == 0 )
       
   264 		{
       
   265 		WRITELOG("CRestoreWatcher::UnregisterL() - setting CentRep value to 0");
       
   266 		SetRestoreFlagL( EFalse );
       
   267 		}
       
   268 	}
       
   269 
       
   270 // -----------------------------------------------------------------------------
       
   271 // CRestoreWatcher::SetRestoreFlagL
       
   272 // Sets restore flag on.
       
   273 // -----------------------------------------------------------------------------
       
   274 //
       
   275 void CRestoreWatcher::SetRestoreFlagL( TBool aRestoreDone )
       
   276 	{	
       
   277 	RFs fs;
       
   278 	User::LeaveIfError( fs.Connect() );
       
   279 	
       
   280 	iRestoreDone = aRestoreDone;
       
   281 	if( aRestoreDone )
       
   282 		{	
       
   283 		RFile64 file;
       
   284 		file.Replace( fs, iFileName, EFileWrite );
       
   285 		file.Close();
       
   286 		}
       
   287 	else
       
   288 		{
       
   289 		fs.Delete( iFileName );
       
   290 		}
       
   291 	
       
   292 	fs.Close();
       
   293 	}
       
   294 
       
   295 // -----------------------------------------------------------------------------
       
   296 // CRestoreWatcher::CheckRestoreL
       
   297 // Check if restore has done when booting up harvester server.
       
   298 // -----------------------------------------------------------------------------
       
   299 //
       
   300 void CRestoreWatcher::CheckRestoreL()
       
   301 	{
       
   302 	RFs fs;
       
   303 	RFile64 file;
       
   304 	User::LeaveIfError( fs.Connect() );
       
   305 	TInt fileError( KErrNotFound );
       
   306 	fileError = file.Open( fs, iFileName, EFileRead );
       
   307 	file.Close();
       
   308 	fs.Close();
       
   309     
       
   310 	WRITELOG1("CRestoreWatcher::StartMonitoring - fileError: %d", fileError);
       
   311 	
       
   312     if ( fileError == KErrNone )
       
   313     	{
       
   314     	iRestoreDone = ETrue;
       
   315     	}
       
   316     else
       
   317     	{
       
   318     	iRestoreDone = EFalse;
       
   319     	}
       
   320 	}
       
   321 
       
   322 // End of file.