1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/genericservices/mimerecognitionfw/apmime/DATASTOR.CPP Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,437 @@
1.4 +// Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
1.5 +// All rights reserved.
1.6 +// This component and the accompanying materials are made available
1.7 +// under the terms of "Eclipse Public License v1.0"
1.8 +// which accompanies this distribution, and is available
1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.10 +//
1.11 +// Initial Contributors:
1.12 +// Nokia Corporation - initial contribution.
1.13 +//
1.14 +// Contributors:
1.15 +//
1.16 +// Description:
1.17 +//
1.18 +
1.19 +#include "DATASTOR.H"
1.20 +#include "APMPAN.H"
1.21 +
1.22 +const TUid KOpenServiceUid = { 0x10208DCA };
1.23 +
1.24 +const TInt KAppMappingGranularity=1;
1.25 +//
1.26 +// class TMappingDataTypeToApp
1.27 +//
1.28 +
1.29 +EXPORT_C TMappingDataTypeToApp::TMappingDataTypeToApp()
1.30 + :iDataType(TDataType(_L8(""))), iServiceUid(KOpenServiceUid)
1.31 + {
1.32 + iAppUid.iUid=0;
1.33 + }
1.34 +
1.35 +EXPORT_C TMappingDataTypeToApp::TMappingDataTypeToApp(const TDataType& aDataType,
1.36 + TDataTypePriority aPriority, TUid aAppUid)
1.37 + :iDataType(aDataType),
1.38 + iPriority(aPriority),
1.39 + iAppUid(aAppUid),
1.40 + iServiceUid(KOpenServiceUid)
1.41 + {
1.42 + }
1.43 +
1.44 +EXPORT_C TMappingDataTypeToApp::TMappingDataTypeToApp(const TDataType& aDataType,
1.45 + TDataTypePriority aPriority, TUid aAppUid, TUid aServiceUid)
1.46 + :iDataType(aDataType),
1.47 + iPriority(aPriority),
1.48 + iAppUid(aAppUid),
1.49 + iServiceUid(aServiceUid)
1.50 + {
1.51 + }
1.52 +
1.53 +EXPORT_C void TMappingDataTypeToApp::InternalizeL(RReadStream& aStream)
1.54 + {
1.55 + aStream >> iDataType;
1.56 + iPriority=aStream.ReadInt32L();
1.57 + iAppUid.iUid=aStream.ReadInt32L();
1.58 + iServiceUid.iUid=aStream.ReadInt32L();
1.59 + }
1.60 +
1.61 +EXPORT_C void TMappingDataTypeToApp::ExternalizeL(RWriteStream& aStream) const
1.62 + {
1.63 + aStream << iDataType;
1.64 + aStream.WriteInt32L(iPriority);
1.65 + aStream.WriteInt32L(iAppUid.iUid);
1.66 + aStream.WriteInt32L(iServiceUid.iUid);
1.67 + }
1.68 +
1.69 +
1.70 +//
1.71 +// class CTypeStoreManager
1.72 +//
1.73 +
1.74 +EXPORT_C void CTypeStoreManager::InternalizeL(RReadStream& aStream)
1.75 +/** Internalises the array of data type mappings from a read stream.
1.76 +
1.77 +@param aStream Stream from which the data type mappings are internalised. */
1.78 + {
1.79 + aStream >> iAppMappings;
1.80 + }
1.81 +
1.82 +EXPORT_C void CTypeStoreManager::ExternalizeL(RWriteStream& aStream) const
1.83 +/** Externalises the data type mappings to a write stream.
1.84 +
1.85 +@param aStream Stream to which the data type mappings should be externalised. */
1.86 + {
1.87 + aStream << iAppMappings;
1.88 + }
1.89 +
1.90 +EXPORT_C CTypeStoreManager* CTypeStoreManager::NewL(RFs& aFs)
1.91 +/** Constructs a CTypeStoreManager object.
1.92 +
1.93 +@param aFs A session with the file server.
1.94 +@return The newly created CTypeStoreManager object. */
1.95 + {
1.96 + CTypeStoreManager* self = new(ELeave) CTypeStoreManager(aFs);
1.97 + CleanupStack::PushL(self);
1.98 + self->ConstructL();
1.99 + CleanupStack::Pop(self);
1.100 + return (self);
1.101 + }
1.102 +
1.103 +void CTypeStoreManager::ConstructL()
1.104 + {
1.105 + TChar sysDrive = RFs::GetSystemDriveChar();
1.106 + TInt maxSizeOfFileName = KIniFileName().Length() + 1;
1.107 + iIniFileName.CreateL(maxSizeOfFileName);
1.108 + iIniFileName.Append(sysDrive);
1.109 + iIniFileName.Append(KIniFileName());
1.110 + }
1.111 +CTypeStoreManager::CTypeStoreManager(RFs& aFs)
1.112 + : iAppMappings(KAppMappingGranularity),
1.113 + iFs(aFs)
1.114 + {
1.115 + __ASSERT_ALWAYS(RProcess().SecureId()==0x10003a3f, Panic(EPanicNotBeingUsedFromWithinApparcServerProcess));
1.116 + }
1.117 +
1.118 +EXPORT_C CTypeStoreManager::~CTypeStoreManager()
1.119 +/** Destructor. */
1.120 + {
1.121 + iIniFileName.Close();
1.122 + }
1.123 +
1.124 +EXPORT_C void CTypeStoreManager::StoreL()
1.125 +/** Stores the data type mappings to the data store ini file
1.126 +(c:\\System\\Data\\Dtstor.ini), creating a new ini file if it does
1.127 +not already exist. */
1.128 + {
1.129 + CDictionaryStore* iniFile=OpenIniFileLC();
1.130 + iniFile->RemoveL(KUidDatastorSettings);
1.131 + RDictionaryWriteStream outStream;
1.132 + outStream.AssignLC(*iniFile,KUidDatastorSettings);
1.133 + outStream << *this;
1.134 + outStream.CommitL();
1.135 + CleanupStack::PopAndDestroy();
1.136 + iniFile->CommitL();
1.137 + CleanupStack::PopAndDestroy(); // inifile
1.138 + }
1.139 +
1.140 +EXPORT_C void CTypeStoreManager::RestoreL()
1.141 +/** Restores the data type mappings from the data store ini file. */
1.142 + {
1.143 + CDictionaryStore* iniFile=OpenIniFileLC();
1.144 + RDictionaryReadStream inStream;
1.145 + inStream.OpenLC(*iniFile,KUidDatastorSettings);
1.146 + inStream >> *this;
1.147 + CleanupStack::PopAndDestroy(2); // inStream * iniFile
1.148 + }
1.149 +
1.150 +/** Changes an existing data type mapping, or adds a new one.
1.151 +
1.152 +If the data type does not exist in the store, the new mapping is appended.
1.153 +If the data type does exist, its mapping is replaced.
1.154 +
1.155 +The service is considered to be the KOpenServiceUid service.
1.156 +
1.157 +@param aDataType A new or existing data type.
1.158 +@param aPriority The priority with which the application handles the data type.
1.159 +@param aUid The UID of the application to associate with the data type.
1.160 +*/
1.161 +EXPORT_C void CTypeStoreManager::InsertDataMappingL(const TDataType& aDataType, TDataTypePriority aPriority, TUid aUid)
1.162 + {
1.163 + InsertDataMappingL(aDataType, aPriority, aUid, KOpenServiceUid);
1.164 + }
1.165 +
1.166 +/** Changes an existing data type mapping, or adds a new one.
1.167 +
1.168 +If the data type does not exist in the store, the new mapping is appended.
1.169 +If the data type does exist, its mapping is replaced.
1.170 +
1.171 +@param aDataType A new or existing data type.
1.172 +@param aPriority The priority with which the application handles the data type.
1.173 +@param aUid The UID of the application to associate with the data type.
1.174 +@param aServiceUid The UID of the service.
1.175 +*/
1.176 +EXPORT_C void CTypeStoreManager::InsertDataMappingL(const TDataType& aDataType,
1.177 + TDataTypePriority aPriority, TUid aUid, TUid aServiceUid)
1.178 + {
1.179 + TInt i = FindDataMapping(aDataType, aServiceUid);
1.180 + if ( i == KErrNotFound )
1.181 + iAppMappings.AppendL( TMappingDataTypeToApp( aDataType, aPriority, aUid, aServiceUid ) );
1.182 + else
1.183 + {
1.184 + TMappingDataTypeToApp& mapping = iAppMappings[i];
1.185 + mapping.iDataType=aDataType;
1.186 + mapping.iPriority=aPriority;
1.187 + mapping.iAppUid=aUid;
1.188 + mapping.iServiceUid=aServiceUid;
1.189 + }
1.190 + }
1.191 +
1.192 +/** Changes an existing data type mapping, or adds a new one.
1.193 +If the data type does not exist in the store, or if it does and its existing priority
1.194 +is less than aPriority, the new mapping is added to the store, or replaces the existing one.
1.195 +Otherwise, no change is made.
1.196 +
1.197 +The service is considered to be the KOpenServiceUid service.
1.198 +
1.199 +@param aDataType A new or existing data type.
1.200 +@param aPriority The priority with which the application handles the data type.
1.201 +@param aUid The UID of the application to associate with the data type.
1.202 +@return ETrue if the new mapping was added or an existing mapping replaced, EFalse if no
1.203 +change was made.
1.204 +*/
1.205 +EXPORT_C TBool CTypeStoreManager::InsertIfHigherL(const TDataType& aDataType, TDataTypePriority aPriority, TUid aUid)
1.206 + {
1.207 + return InsertIfHigherL(aDataType, aPriority, aUid, KOpenServiceUid);
1.208 + }
1.209 +
1.210 +/** Changes an existing data type mapping, or adds a new one.
1.211 +If the data type does not exist in the store, or if it does and its existing priority
1.212 +is less than aPriority, the new mapping is added to the store, or replaces the existing one.
1.213 +Otherwise, no change is made.
1.214 +
1.215 +@param aDataType A new or existing data type.
1.216 +@param aPriority The priority with which the application handles the data type.
1.217 +@param aUid The UID of the application to associate with the data type.
1.218 +@param aServiceUid The UID of the service.
1.219 +@return ETrue if the new mapping was added or an existing mapping replaced, EFalse if no
1.220 +change was made.
1.221 +*/
1.222 +EXPORT_C TBool CTypeStoreManager::InsertIfHigherL(const TDataType& aDataType,
1.223 + TDataTypePriority aPriority, TUid aUid, TUid aServiceUid)
1.224 + {
1.225 + TInt i = FindDataMapping( aDataType, aServiceUid );
1.226 + if ( i == KErrNotFound || iAppMappings[i].iPriority < aPriority )
1.227 + {
1.228 + InsertDataMappingL( aDataType, aPriority, aUid, aServiceUid );
1.229 + return ETrue;
1.230 + }
1.231 + else
1.232 + return EFalse;
1.233 + }
1.234 +
1.235 +/** Removes an existing data type mapping from the store.
1.236 +
1.237 +The service is considered to be the KOpenServiceUid service.
1.238 +
1.239 +@param aDataType Data type whose mapping should be removed.
1.240 +@panic USER 0 The specified data type cannot be found. Debug builds only.
1.241 +*/
1.242 +EXPORT_C void CTypeStoreManager::DeleteDataMapping(const TDataType& aDataType)
1.243 + {
1.244 + DeleteDataMapping(aDataType, KOpenServiceUid);
1.245 + }
1.246 +
1.247 +/** Removes an existing data type mapping from the store.
1.248 +
1.249 +@param aDataType Data type whose mapping should be removed.
1.250 +@param aServiceUid The UID of the service.
1.251 +@panic USER 0 The specified data type cannot be found. Debug builds only.
1.252 +*/
1.253 +EXPORT_C void CTypeStoreManager::DeleteDataMapping(const TDataType& aDataType,
1.254 + TUid aServiceUid)
1.255 + {
1.256 + TInt i=FindDataMapping(aDataType, aServiceUid);
1.257 + __ASSERT_DEBUG(i!=KErrNotFound,User::Invariant());
1.258 + iAppMappings.Delete(i);
1.259 + }
1.260 +
1.261 +/** Gets the UID of the application mapped to the specified data type.
1.262 +
1.263 +The service is considered to be the KOpenServiceUid service.
1.264 +
1.265 +@param aDataType The data type.
1.266 +@param aUid On return, the UID of the application associated with the
1.267 +data type, or KNullUid if the data type is not found.
1.268 +*/
1.269 +EXPORT_C void CTypeStoreManager::GetAppByDataType(const TDataType& aDataType, TUid& aUid) const
1.270 + {
1.271 + GetAppByDataType(aDataType, KOpenServiceUid, aUid);
1.272 + }
1.273 +
1.274 +/** Gets the UID of the application mapped to the specified data type.
1.275 +
1.276 +The service is considered to be the KOpenServiceUid service.
1.277 +
1.278 +@param aDataType The data type.
1.279 +@param aServiceUid The UID of the service.
1.280 +@param aUid On return, the UID of the application associated with the
1.281 +data type, or KNullUid if the data type is not found.
1.282 +*/
1.283 +EXPORT_C void CTypeStoreManager::GetAppByDataType(const TDataType& aDataType,
1.284 + TUid aServiceUid, TUid& aUid) const
1.285 + {
1.286 + TInt i=FindDataMapping(aDataType, aServiceUid);
1.287 + aUid=(i==KErrNotFound? KNullUid : iAppMappings[i].iAppUid);
1.288 + }
1.289 +
1.290 +EXPORT_C void CTypeStoreManager::GetDataTypesByAppL(TUid aUid, CArrayFix<TDataType>* aTypeArray) const
1.291 +/** Populates an array with all the data types supported by the specified application.
1.292 +
1.293 +If the specified UID is zero, the array is populated with all the data types found in
1.294 +the store.
1.295 +
1.296 +@param aUid An application UID.
1.297 +@param aTypeArray An empty array. On return, contains all data types supported by the
1.298 +application.
1.299 +@panic APMIME 5 The array is NULL.
1.300 +@panic APMIME 6 The array passed to the function is not empty. Debug builds only. */
1.301 + {
1.302 + __ASSERT_ALWAYS(aTypeArray,Panic(EInvalidArgument));
1.303 + __ASSERT_DEBUG(!aTypeArray->Count(),Panic(EArrayNotEmpty));
1.304 + TInt count=iAppMappings.Count();
1.305 + for (TInt i=0; i<count; i++)
1.306 + {
1.307 + if ((iAppMappings[i].iAppUid==aUid || aUid.iUid==0) &&
1.308 + (iAppMappings[i].iServiceUid == KOpenServiceUid))
1.309 + {
1.310 + aTypeArray->AppendL(iAppMappings[i].iDataType);
1.311 + }
1.312 + }
1.313 + }
1.314 +
1.315 +EXPORT_C const CArrayFixFlat<TMappingDataTypeToApp>& CTypeStoreManager::MappingArray() const
1.316 +/** Returns the array of data type mappings.
1.317 +
1.318 +@return The array of data type mappings. */
1.319 + {
1.320 + return iAppMappings;
1.321 + }
1.322 +
1.323 +TInt CTypeStoreManager::FindDataMapping(const TDataType& aDataType,
1.324 + const TUid& aServiceUid) const
1.325 + {
1.326 + TInt count=iAppMappings.Count();
1.327 + for (TInt i=0; i<count; i++)
1.328 + {
1.329 + // Match the pattern at the start of the mime type
1.330 + if ((iAppMappings[i].iDataType.Des8().Match(aDataType.Des8())==0) &&
1.331 + (iAppMappings[i].iServiceUid == aServiceUid))
1.332 + {
1.333 + return i;
1.334 + }
1.335 + }
1.336 + return KErrNotFound;
1.337 + }
1.338 +
1.339 +CDictionaryStore* CTypeStoreManager::OpenIniFileLC() const
1.340 +// Open dtstor's ini file - will create a new one if it doesn't exist or is corrupted
1.341 + {
1.342 + const TPtrC iniFileName(IniFileName());
1.343 + TInt err=iFs.MkDirAll(iniFileName);
1.344 + if(err!=KErrAlreadyExists)
1.345 + {
1.346 + User::LeaveIfError(err);
1.347 + }
1.348 + CDictionaryStore* iniFile=NULL;
1.349 + TRAP(err,iniFile=CDictionaryFileStore::OpenL(iFs,iniFileName,KUidDatastor));
1.350 + if (err==KErrNone)
1.351 + {
1.352 + CleanupStack::PushL(iniFile);
1.353 + }
1.354 + else if (err==KErrEof || err==KErrCorrupt)
1.355 + {
1.356 + User::LeaveIfError(iFs.Delete(iniFileName));
1.357 + iniFile=CDictionaryFileStore::OpenLC(iFs,iniFileName,KUidDatastor);
1.358 + err=KErrNone;
1.359 + }
1.360 + User::LeaveIfError(err);
1.361 + return iniFile;
1.362 + }
1.363 +
1.364 +EXPORT_C void CTypeStoreManager::InsertAndStoreDataMappingL(const TDataType& aDataType,
1.365 + TDataTypePriority aPriority, TUid aUid)
1.366 + {
1.367 + InsertAndStoreDataMappingL(aDataType, aPriority, aUid, KOpenServiceUid);
1.368 + }
1.369 +
1.370 +EXPORT_C void CTypeStoreManager::InsertAndStoreDataMappingL(const TDataType& aDataType,
1.371 + TDataTypePriority aPriority, TUid aUid, TUid aServiceUid)
1.372 + {
1.373 + InsertDataMappingL(aDataType, aPriority, aUid, aServiceUid);
1.374 + TRAPD(ret,StoreL());
1.375 + if(ret!=KErrNone)
1.376 + {
1.377 + DeleteDataMapping(aDataType, aServiceUid);
1.378 + }
1.379 + }
1.380 +
1.381 +EXPORT_C TBool CTypeStoreManager::InsertAndStoreIfHigherL(const TDataType& aDataType, TDataTypePriority aPriority, TUid aUid)
1.382 + {
1.383 + return InsertAndStoreIfHigherL(aDataType, aPriority, aUid, KOpenServiceUid);
1.384 + }
1.385 +
1.386 +EXPORT_C TBool CTypeStoreManager::InsertAndStoreIfHigherL(const TDataType& aDataType,
1.387 + TDataTypePriority aPriority, TUid aUid, TUid aServiceUid)
1.388 + {
1.389 + TInt i = FindDataMapping( aDataType, aServiceUid );
1.390 + if ( i == KErrNotFound || iAppMappings[i].iPriority < aPriority )
1.391 + {
1.392 + InsertAndStoreDataMappingL( aDataType, aPriority, aUid, aServiceUid );
1.393 + return ETrue;
1.394 + }
1.395 + else
1.396 + return EFalse;
1.397 + }
1.398 +
1.399 +EXPORT_C void CTypeStoreManager::DeleteAndStoreDataMappingL(const TDataType& aDataType)
1.400 + {
1.401 + DeleteAndStoreDataMappingL(aDataType, KOpenServiceUid);
1.402 + }
1.403 +
1.404 +EXPORT_C void CTypeStoreManager::DeleteAndStoreDataMappingL(const TDataType& aDataType,
1.405 + TUid aServiceUid)
1.406 + {
1.407 + TInt i=FindDataMapping(aDataType, aServiceUid);
1.408 + __ASSERT_DEBUG(i!=KErrNotFound,User::Invariant());
1.409 + TMappingDataTypeToApp mapping (iAppMappings[i].iDataType,iAppMappings[i].iPriority,iAppMappings[i].iAppUid,iAppMappings[i].iServiceUid);
1.410 + iAppMappings.Delete(i);
1.411 + TRAPD(ret,StoreL());
1.412 + if(ret!=KErrNone)
1.413 + {
1.414 + iAppMappings.InsertL(i,mapping);
1.415 + }
1.416 + }
1.417 +
1.418 +//Removes data mappings related to aAppUid
1.419 +//Returns modification status of service registry
1.420 +EXPORT_C TBool CTypeStoreManager::DeleteApplicationDataMappings(const TUid aAppUid)
1.421 + {
1.422 + TInt count=iAppMappings.Count();
1.423 + TInt index=0;
1.424 + TBool modified=EFalse;
1.425 +
1.426 + //goes through service registry to find data mappings related to aAppUid
1.427 + while(index<count)
1.428 + {
1.429 + if (iAppMappings[index].iAppUid==aAppUid)
1.430 + {
1.431 + iAppMappings.Delete(index);
1.432 + //As data mapping is removed from service registry, reduce the count
1.433 + count--;
1.434 + modified=ETrue;
1.435 + }
1.436 + else
1.437 + index++;
1.438 + }
1.439 + return(modified);
1.440 + }