applayerprotocols/httpexamples/uaprof/examplecpimanager/examplecpimanager.cpp
changeset 0 b16258d2340f
equal deleted inserted replaced
-1:000000000000 0:b16258d2340f
       
     1 // Copyright (c) 2003-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 //
       
    15 
       
    16 // System Includes
       
    17 #include <http/thttphdrval.h>
       
    18 
       
    19 // User Includes
       
    20 #include "examplecpimanager.h"
       
    21 
       
    22 
       
    23 // The storage location for CPI files on the device.
       
    24 _LIT(KTxtCpiFilesLocation, "c:\\system\\data\\uaprof\\");
       
    25 
       
    26 // Each CPI file contains two UIDs before the actual CPI data.
       
    27 // The first UID is to indicate that the file is a UAProf CPI file.
       
    28 // The second UID is used to differentiate between Profile and Profile-Diff information.
       
    29 static const TUid KUidUaProfCpi = {0x101F55EE};
       
    30 static const TUid KUidProfile = {0x101F55F0};
       
    31 static const TUid KUidProfileDiff = {0x101F55F1};
       
    32 
       
    33 // Length in bytes of the two UIDs (Cpi File UID and either either a Profile or Profile-Diff 
       
    34 // file UID) included at the start of all CPI files.
       
    35 const TInt KFileUIDLength = sizeof(TUint) * 2;
       
    36 
       
    37 // The index of the second UID in a CPI file 
       
    38 const TInt KUID2Index = 1;
       
    39 
       
    40 
       
    41 EXPORT_C CExampleCpiManager* CExampleCpiManager::NewL(RStringPool aStringPool)
       
    42 /**	Standard C class construction method
       
    43 	@since		8.0
       
    44 	@param		aStringPool	The string pool to use for the creation of all strings in this manager
       
    45 	@return		A pointer to a fully constructed CExampleCpiManager
       
    46 */
       
    47 	{
       
    48 	CExampleCpiManager* cpiManager = new (ELeave) CExampleCpiManager(aStringPool);
       
    49 	CleanupStack::PushL(cpiManager);
       
    50 	cpiManager->ConstructL();
       
    51 	CleanupStack::Pop(cpiManager);
       
    52 	return cpiManager;
       
    53 	}
       
    54 	
       
    55 CExampleCpiManager::CExampleCpiManager(RStringPool aStringPool) 
       
    56 	:	iStringPool(aStringPool)
       
    57 /**	Standard constructor
       
    58 	@since		8.0
       
    59 	@param		aStringPool	The string pool to use for the creation of all strings in this manager
       
    60 */
       
    61 	{
       
    62 	}
       
    63 	
       
    64 void CExampleCpiManager::ConstructL()
       
    65 /**	Standard 2nd phase constructor
       
    66 	@since		8.0
       
    67 */
       
    68 	{
       
    69 	User::LeaveIfError(iFs.Connect());
       
    70 	FindCpiFilesL();
       
    71 	DiscoverProfilesL();
       
    72 	}	
       
    73 
       
    74 EXPORT_C CExampleCpiManager::~CExampleCpiManager()
       
    75 /**	Standard destructor
       
    76 	@since		8.0
       
    77 */	
       
    78 	{
       
    79 	iCpiFileList.ResetAndDestroy();
       
    80 	iProfileData.ResetAndDestroy();
       
    81 	iProfileDiffData.ResetAndDestroy();
       
    82 	iFs.Close();
       
    83 	}
       
    84 	
       
    85 EXPORT_C RPointerArray<HBufC8>& CExampleCpiManager::GetProfiles()
       
    86 /**	Gets the Profile data stored on the device
       
    87 	@since		8.0
       
    88 	@return		An array of Profiles
       
    89 */
       
    90 	{
       
    91 	return iProfileData;
       
    92 	}
       
    93 	
       
    94 EXPORT_C RPointerArray<HBufC8>& CExampleCpiManager::GetProfileDiffs()
       
    95 /**	Gets the Profile-Diff data stored on the device
       
    96 	@since		8.0
       
    97 	@return		An array of Profile-Diffs
       
    98 */
       
    99 	{
       
   100 	return iProfileDiffData;
       
   101 	}
       
   102 	
       
   103 void CExampleCpiManager::FindCpiFilesL()
       
   104 /**	Looks in the UAProf directory for any CPI files and adds them to the array of CPI files
       
   105 	@since		8.0
       
   106 */
       
   107 	{
       
   108 	TUidType uidType(KUidUaProfCpi);
       
   109 	CDir* dir;
       
   110 	// Get a list of files that match the CPI UID
       
   111 	TInt err = iFs.GetDir(KTxtCpiFilesLocation, uidType, ESortByUid, dir);
       
   112 	CleanupStack::PushL(dir);	
       
   113 	if (err == KErrPathNotFound)
       
   114 		User::LeaveIfError(iFs.MkDirAll(KTxtCpiFilesLocation()));
       
   115 	else
       
   116 		User::LeaveIfError(err);
       
   117 	
       
   118 	const TInt fileCount = dir->Count();
       
   119 	for (TInt i = 0; i < fileCount; ++i)
       
   120 		{
       
   121 		TEntry* entry = NULL;
       
   122 		entry = new (ELeave) TEntry((*dir)[i]);
       
   123 		entry->iName = KTxtCpiFilesLocation();
       
   124 		(entry->iName.Des()).Append((*dir)[i].iName);
       
   125 		User::LeaveIfError(iCpiFileList.Append(entry));
       
   126 		}
       
   127 	CleanupStack::PopAndDestroy(dir);
       
   128 	}
       
   129 	
       
   130 void CExampleCpiManager::DiscoverProfilesL()
       
   131 /**	Loop through the array of CPI files determining, through looking at the UID in 
       
   132 	the second array element, whether the CPI files are Profile or Profile-Diff files.
       
   133 	@since		8.0
       
   134 */
       
   135 	{
       
   136 	const TInt fileCount = iCpiFileList.Count();
       
   137 	for (TInt ii = 0; ii < fileCount; ++ii)
       
   138 		{
       
   139 		// Look to see if the UID of the file matches that of the UID assigned
       
   140 		// to Profile files.
       
   141 		if ((*iCpiFileList[ii])[KUID2Index] == KUidProfile)
       
   142 			BuildProfilesL(*iCpiFileList[ii], EProfile);
       
   143 			
       
   144 		// Look to see if the UID of the file matches that of the UID assigned
       
   145 		// to Profile-Diff files.
       
   146 		else if ((*iCpiFileList[ii])[KUID2Index] == KUidProfileDiff)
       
   147 			BuildProfilesL(*iCpiFileList[ii], EProfileDiff);
       
   148 		else
       
   149 			User::Leave(KErrCorrupt);
       
   150 		}
       
   151 	}
       
   152 	
       
   153 void CExampleCpiManager::BuildProfilesL(TEntry& aEntry, CExampleCpiManager::TCpiType aCpiType)
       
   154 /**	Appends the Profile and Profile-Diff data discovered to an array of Profile and
       
   155 	Profile-Diff data respectively
       
   156 	@since		8.0
       
   157 	@param		aEntry The CPI file to extract the Profile or Profile-Diff data from
       
   158 	@param		aCpiType The type of CPI the file contains i.e. Profile or Profile-Diff
       
   159 */
       
   160 	{
       
   161 	RFile cpiFile;
       
   162 	User::LeaveIfError(cpiFile.Open(iFs, aEntry.iName, EFileShareExclusive));
       
   163 	CleanupClosePushL(cpiFile);
       
   164 	
       
   165 	TInt fileSize = 0;
       
   166 	User::LeaveIfError(cpiFile.Size(fileSize));
       
   167 
       
   168 	HBufC8* fileReader = HBufC8::NewLC(fileSize);
       
   169 	TPtr8 dataBuffer = fileReader->Des();
       
   170 	// Start reading from the file following the 2 UIDs found at the beginning of the file
       
   171 	cpiFile.Read(KFileUIDLength, dataBuffer);
       
   172 	HBufC8* cpiData = dataBuffer.AllocLC();
       
   173 
       
   174 	switch(aCpiType)
       
   175 		{
       
   176 		case EProfile:
       
   177 			{
       
   178 			User::LeaveIfError(iProfileData.Append(cpiData));
       
   179 			break;
       
   180 			}
       
   181 		case EProfileDiff:
       
   182 			{
       
   183 			User::LeaveIfError(iProfileDiffData.Append(cpiData));
       
   184 			break;
       
   185 			}
       
   186 		default:
       
   187 			break;
       
   188 		}
       
   189 	CleanupStack::Pop(cpiData);
       
   190 	CleanupStack::PopAndDestroy(2, &cpiFile); // fileReader, cpiFile
       
   191 	}
       
   192