backupandrestore/backupengine/src/sbeconfig.cpp
changeset 0 d0791faffa3f
child 15 f85613f12947
equal deleted inserted replaced
-1:000000000000 0:d0791faffa3f
       
     1 // Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // Implementation of sbeconfig
       
    15 // 
       
    16 //
       
    17 
       
    18 /**
       
    19  @file
       
    20 */
       
    21 #include <e32std.h>
       
    22 #include <connect/panic.h>
       
    23 #include "sbeconfig.h"
       
    24 #include "sblog.h"
       
    25 #include <xml/parser.h>
       
    26 
       
    27 namespace conn
       
    28 	{
       
    29 	const TInt KSIDLength = 8;
       
    30 	
       
    31 	// XML type
       
    32 	_LIT8(KMimeType, "text/xml");
       
    33 	
       
    34 	// elements
       
    35 	_LIT8(KConfig, "sbe_config");
       
    36 	_LIT8(KHeap, "heap");
       
    37 	_LIT8(KCentRep, "central_repository");
       
    38 	_LIT8(KDrives, "exclude_drives");
       
    39 	_LIT8(KAppCloseDelay, "app_close_delay");
       
    40 	
       
    41 	_LIT8(KSize, "size");
       
    42 	_LIT8(KUid, "uid");
       
    43 	_LIT8(KList, "list");
       
    44 	
       
    45 	_LIT8(KRedFact, "reduction_factor");
       
    46 	_LIT8(KMaxRetries, "max_retries");
       
    47 	
       
    48 	_LIT8(KDelay, "delay");
       
    49 	
       
    50 	// default setting if no file found
       
    51 	const TInt KSBEGSHDefaultSize = 2097152;
       
    52 	const TInt KSBEGSHReductionFactor = 2;
       
    53 	const TInt KSBEGSHMaxRetries = 5;
       
    54 	
       
    55 	const TInt KMinHeapSize = 131072;
       
    56 	
       
    57 	const TInt KDefaultDelay = 0;
       
    58 	
       
    59 	_LIT_SECURE_ID(KCentRepSID,0x10202BE9);
       
    60 	_LIT(KConfigFile, "sbeconfig.xml");
       
    61 	
       
    62 	/**
       
    63 	Symbian Constructor
       
    64 	@param RFs& reference to RFs
       
    65 	@return CSBEConfig* pointer to CSBEConfig
       
    66 	*/
       
    67 	CSBEConfig* CSBEConfig::NewL(RFs& aRFs)
       
    68 		{
       
    69 		CSBEConfig* self = new (ELeave) CSBEConfig(aRFs);
       
    70 		return self;
       
    71 		}
       
    72 	
       
    73 	/**
       
    74 	C++ Constructor
       
    75 	*/
       
    76 	CSBEConfig::CSBEConfig(RFs& aRFs) : iRFs(aRFs), iFileName(KConfigFile), iConfigTagVisited(EFalse)
       
    77 		{
       
    78 		SetDefault();
       
    79 		}
       
    80 	/** 
       
    81 	Destructor
       
    82 	*/
       
    83 	CSBEConfig::~CSBEConfig()
       
    84 		{
       
    85 		delete iConverter;
       
    86 		}
       
    87 	
       
    88 	/**
       
    89 	Heap Values
       
    90 	@param TInt& aMaxSize of the heap to try to allocate
       
    91 	@param TInt& aReductionFactor in case allocation fail
       
    92 	@param TInt& number of retries to try to reduce the heap by the ReductionFactor
       
    93 	*/
       
    94 	void CSBEConfig::HeapValues(TInt& aMaxSize, TInt& aReductionFactor, TInt& aMaxRetries) const
       
    95 		{
       
    96 		aMaxSize = iSBEGSHMaxSize;
       
    97 		aReductionFactor = iReductionFactor;
       
    98 		aMaxRetries = iMaxRetries;
       
    99 		}
       
   100 		
       
   101 	/**
       
   102 	Secure Id for central repository, needed deprecated use of centrep tag in xml
       
   103 	@return TSecureId& aSecureId
       
   104 	*/	
       
   105 	TSecureId CSBEConfig::CentRepId() const
       
   106 		{
       
   107 		return iCentRepId;
       
   108 		}
       
   109 	
       
   110 	/**
       
   111 	Exclude list of drives from backup/restore
       
   112 	@return TDriveList& aDriveList
       
   113 	*/	
       
   114 	const TDriveList& CSBEConfig::ExcludeDriveList() const
       
   115 		{
       
   116 		return iDrives;
       
   117 		}
       
   118 	
       
   119 	/**
       
   120 	Extra time delay to close all non-system apps 
       
   121 	@return TInt& iAppCloseDelay
       
   122 	*/
       
   123 	TUint CSBEConfig::AppCloseDelay() const
       
   124 		{
       
   125 		return iAppCloseDelay;
       
   126 		}
       
   127 
       
   128 	/**
       
   129 	Set the values to Defaults
       
   130 	*/	
       
   131 	void CSBEConfig::SetDefault()
       
   132 		{
       
   133 		iSBEGSHMaxSize = KSBEGSHDefaultSize;
       
   134 		iCentRepId = KCentRepSID;
       
   135 		iDrives.SetLength(KMaxDrives);
       
   136 		iDrives.FillZ();
       
   137 		iDrives[EDriveZ] = ETrue;
       
   138 		iReductionFactor = KSBEGSHReductionFactor;
       
   139 		iMaxRetries = KSBEGSHMaxRetries;
       
   140 		iAppCloseDelay = KDefaultDelay;
       
   141 		}
       
   142 		
       
   143 	/**
       
   144 	Method to convert string of drives (eg. cdez) to member variable TDriveList
       
   145 	@param const TDesC8& reference to string
       
   146 	*/	
       
   147 	TInt CSBEConfig::StringToDrives(const TDesC8& aDes)
       
   148 		{
       
   149 		iDrives.SetLength(KMaxDrives);
       
   150 		iDrives.FillZ();
       
   151 		
       
   152 		TInt err = KErrNone;
       
   153 		TInt length = aDes.Length();
       
   154 		for (TInt i = 0; i < length; ++i)
       
   155 			{
       
   156 			TInt pos;
       
   157 			err = iRFs.CharToDrive(aDes.Ptr()[i], pos);
       
   158 			if (err != KErrNone)
       
   159 				{
       
   160 				break;
       
   161 				}
       
   162 			iDrives[pos] = ETrue;
       
   163 			}
       
   164 		return err;
       
   165 		}
       
   166 	
       
   167 	/**
       
   168 	Parses the config file if found
       
   169 	@leave with System wide Error Codes
       
   170 	*/	
       
   171 	void CSBEConfig::ParseL()
       
   172 		{
       
   173 		iRFs.PrivatePath(iFileName);
       
   174 		TFindFile findFile(iRFs);
       
   175 		User::LeaveIfError(findFile.FindByPath(KConfigFile, &iFileName));
       
   176 		
       
   177 		iFileName = findFile.File();
       
   178 		// Connect to the parser
       
   179 		CParser* parser = CParser::NewLC(KMimeType, *this);
       
   180 		
       
   181 		// Parse the file
       
   182 		Xml::ParseL(*parser, iRFs, iFileName);
       
   183 		
       
   184 		CleanupStack::PopAndDestroy(parser);
       
   185 		}
       
   186 		
       
   187 	/**
       
   188 	A method to handle attributes
       
   189 	@param RAttributeArray& aAttributes 
       
   190 	@return TInt System Wide Error
       
   191 	*/	
       
   192 	TInt CSBEConfig::HandleAttributesElement(const RAttributeArray& aAttributes)
       
   193 		{
       
   194 		TInt err = KErrNone;
       
   195 		// Loop through reading out attribute values
       
   196 		const TUint count = aAttributes.Count();
       
   197 		for (TInt x = 0; x < count && err == KErrNone; x++)
       
   198 			{
       
   199 			TPtrC8 attrib = aAttributes[x].Attribute().LocalName().DesC();
       
   200 			TPtrC8 value = aAttributes[x].Value().DesC();
       
   201 			if (!attrib.CompareF(KDelay))
       
   202 				{
       
   203 				TLex8 lex(value);
       
   204 				TInt appCloseDelay = 0;
       
   205 				err = lex.Val(appCloseDelay);
       
   206 				if (appCloseDelay < 0)
       
   207 					{
       
   208 					__LOG("CSBEConfig::HandleAttributesElement() - Configuration Error: the time delay is negative");
       
   209 					err = KErrCorrupt;
       
   210 					}
       
   211 				else
       
   212 					{
       
   213 					iAppCloseDelay = appCloseDelay;
       
   214 					}
       
   215 				}
       
   216 			if (!attrib.CompareF(KRedFact))
       
   217 				{
       
   218 				TLex8 lex(value);
       
   219 				err = lex.Val(iReductionFactor);
       
   220 				if (iReductionFactor < 0)
       
   221 					{
       
   222 					__LOG("CSBEConfig::HandleAttributesElement() - Configuration Error: the reductionFactor is negative");
       
   223 					err = KErrCorrupt;
       
   224 					}
       
   225 				}
       
   226 			else if (!attrib.CompareF(KMaxRetries))
       
   227 				{
       
   228 				TLex8 lex(value);
       
   229 				err = lex.Val(iMaxRetries);
       
   230 				if (iMaxRetries < 0)
       
   231 					{
       
   232 					__LOG("CSBEConfig::HandleAttributesElement() - Configuration Error: the maxRetries is negative");
       
   233 					err = KErrCorrupt;
       
   234 					}
       
   235 				}
       
   236 			if (!attrib.CompareF(KSize))
       
   237 				{
       
   238 				TLex8 lex(value);
       
   239 				err = lex.Val(iSBEGSHMaxSize);
       
   240 				if (iSBEGSHMaxSize < KMinHeapSize)
       
   241 					{
       
   242 					__LOG1("CSBEConfig::HandleAttributesElement() - Configuration Error: heap size is less then minimum %d", KMinHeapSize);
       
   243 					err = KErrCorrupt;
       
   244 					}
       
   245 				} // if
       
   246 			else if (!attrib.CompareF(KUid))
       
   247 				{
       
   248 				TLex8 lex;
       
   249 				if (value.Length() >= KSIDLength)
       
   250 					{
       
   251 					lex = value.Right(KSIDLength);
       
   252 					err = lex.Val(iCentRepId.iId, EHex);
       
   253 					if (iCentRepId.iId == 0)
       
   254 						{
       
   255 						err = KErrCorrupt;
       
   256 						}
       
   257 					}
       
   258 				if (err != KErrNone)
       
   259 					{
       
   260 					__LOG("CSBEConfig::HandleAttributesElement() - Configuration Error: central_repostiory is NOT a HEX number");
       
   261 					err = KErrCorrupt;
       
   262 					}
       
   263 				} // else if
       
   264 			else if (!attrib.CompareF(KList))
       
   265 				{
       
   266 				err = StringToDrives(value);
       
   267 				if (err != KErrNone)
       
   268 					{
       
   269 					__LOG("CSBEConfig::HandleAttributesElement() - Configuration Error: list doesn't have valid characters from a-z");
       
   270 					}
       
   271 				} // else if
       
   272 				
       
   273 			} // for x
       
   274 		return err;
       
   275 		}
       
   276 		
       
   277 // From MContentHandler
       
   278 	void CSBEConfig::OnStartDocumentL(const RDocumentParameters& /*aDocParam*/, TInt /*aErrorCode*/)
       
   279 	/**
       
   280 	Start of the document, creates Character Converter to convert from/to unicode
       
   281 	
       
   282 	@see MContentHandler::OnStartDocumentL()
       
   283 	@leave if fails to set encoding
       
   284 	*/
       
   285 		{
       
   286 		// Create a converter for converting strings to Unicode
       
   287 		iConverter = CCnvCharacterSetConverter::NewL();
       
   288 
       
   289 		// We only convert from UTF-8 to UTF-16
       
   290 		if (iConverter->PrepareToConvertToOrFromL(KCharacterSetIdentifierUtf8, iRFs) == CCnvCharacterSetConverter::ENotAvailable)
       
   291 			{
       
   292 			User::Leave(KErrNotFound);
       
   293 			}
       
   294 		}
       
   295 		
       
   296 	void CSBEConfig::OnEndDocumentL(TInt /*aErrorCode*/)
       
   297 	/**
       
   298 	End of document. destroys converter object
       
   299 	
       
   300 	@see MContentHandler::OnEndDocumentL()
       
   301 	*/
       
   302 		{
       
   303 		// We've finished parsing the document, hence destroy the converter object
       
   304 		delete iConverter;
       
   305 		iConverter = NULL;
       
   306 		}
       
   307 		
       
   308 	void CSBEConfig::OnStartElementL(const RTagInfo& aElement, const RAttributeArray& aAttributes, TInt /*aErrCode*/)
       
   309 	/**
       
   310 	Element to parse on the start
       
   311 	
       
   312 	@see MContentHandler::OnStartElementL()
       
   313 	
       
   314 	@param aElement RTagInfo&
       
   315 	@param aAttributes RAttributeArray&
       
   316 	*/
       
   317 		{
       
   318 		TInt err = KErrNone;
       
   319 		TPtrC8 localName(aElement.LocalName().DesC());
       
   320 		if (!localName.CompareF(KConfig))
       
   321 			{
       
   322 			iConfigTagVisited = ETrue;
       
   323 			} // if
       
   324 		else if (iConfigTagVisited)
       
   325 			{
       
   326 			if (!localName.CompareF(KHeap) || !localName.CompareF(KCentRep) || !localName.CompareF(KDrives) || !localName.CompareF(KAppCloseDelay))
       
   327 				{
       
   328 				err = HandleAttributesElement(aAttributes);
       
   329 				} // if
       
   330 			else
       
   331 				{
       
   332 				err = KErrCorrupt;
       
   333 				} // else if
       
   334 			} // else if
       
   335 		else
       
   336 			{
       
   337 			err = KErrCorrupt;
       
   338 			}
       
   339 		User::LeaveIfError(err);
       
   340 		}
       
   341 		
       
   342 	void CSBEConfig::OnEndElementL(const RTagInfo& /*aElement*/, TInt /*aErrorCode*/)
       
   343 	/**
       
   344 	Element to parse at the end
       
   345 	
       
   346 	@see MContentHandler::OnEndElementL()
       
   347 	@param const aElement RTagInfo&
       
   348 	*/
       
   349 		{
       
   350 		}
       
   351 		
       
   352 	void CSBEConfig::OnContentL(const TDesC8& /*aBytes*/, TInt /*aErrorCode*/)
       
   353 	/** 
       
   354 	@see MContentHandler::OnContentL()
       
   355 	*/
       
   356 		{
       
   357 		}
       
   358 		
       
   359 	void CSBEConfig::OnStartPrefixMappingL(const RString& /*aPrefix*/, const RString& /*aUri*/, TInt /*aErrorCode*/)
       
   360 	/** 
       
   361 	@see MContentHandler::OnStartPrefixMappingL()
       
   362 	*/
       
   363 		{
       
   364 		}
       
   365 		
       
   366 	void CSBEConfig::OnEndPrefixMappingL(const RString& /*aPrefix*/, TInt /*aErrorCode*/)
       
   367 	/** 
       
   368 	@see MContentHandler::OnEndPrefixMappingL()
       
   369 	*/
       
   370 		{
       
   371 		}
       
   372 		
       
   373 	void CSBEConfig::OnIgnorableWhiteSpaceL(const TDesC8& /*aBytes*/, TInt /*aErrorCode*/)
       
   374 	/** 
       
   375 	@see MContentHandler::OnIgnorableWhiteSpaceL()
       
   376 	*/
       
   377 		{
       
   378 		}
       
   379 		
       
   380 	void CSBEConfig::OnSkippedEntityL(const RString& /*aName*/, TInt /*aErrorCode*/)
       
   381 	/** 
       
   382 	@see MContentHandler::OnSkippedEntityL()
       
   383 	*/
       
   384 		{
       
   385 		}
       
   386 		
       
   387 	void CSBEConfig::OnProcessingInstructionL(const TDesC8& /*aTarget*/, const TDesC8& /*aData*/, TInt /*aErrorCode*/)
       
   388 	/** 
       
   389 	@see MContentHandler::OnProcessingInstructionL()
       
   390 	*/
       
   391 		{
       
   392 		}
       
   393 		
       
   394 	void CSBEConfig::OnError(TInt /*aErrorCode*/)
       
   395 	/** 
       
   396 	@see MContentHandler::OnError()
       
   397 	*/
       
   398 		{
       
   399 		}
       
   400 		
       
   401 	TAny* CSBEConfig::GetExtendedInterface(const TInt32 /*aUid*/)
       
   402 	/** 
       
   403 	@see MContentHandler::GetExtendedInterface()
       
   404 	*/
       
   405 		{
       
   406 		return NULL;
       
   407 		}
       
   408 		
       
   409 	
       
   410 	}