appfw/apparchitecture/apfile/apfmimecontentpolicy.cpp
changeset 29 6a787171e1de
parent 0 2e3d3ce01487
child 81 676b6116ca93
equal deleted inserted replaced
28:b0b858956ed5 29:6a787171e1de
     1 // Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies).
     1 // Copyright (c) 2002-2010 Nokia Corporation and/or its subsidiary(-ies).
     2 // All rights reserved.
     2 // All rights reserved.
     3 // This component and the accompanying materials are made available
     3 // This component and the accompanying materials are made available
     4 // under the terms of "Eclipse Public License v1.0"
     4 // under the terms of "Eclipse Public License v1.0"
     5 // which accompanies this distribution, and is available
     5 // which accompanies this distribution, and is available
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
    16 
    16 
    17 
    17 
    18 // INCLUDE FILES
    18 // INCLUDE FILES
    19 #include <apfmimecontentpolicy.h>
    19 #include <apfmimecontentpolicy.h>
    20 #include <f32file.h> // RFs
    20 #include <f32file.h> // RFs
    21 #include <barsread.h>
       
    22 #include <barsc.h> 
       
    23 #include <apfmimecontentpolicy.rsg>
       
    24 #include <caf/content.h>
    21 #include <caf/content.h>
    25 #include <e32base.h>
    22 #include <e32base.h>
    26 #include <apgcli.h>    // For RApaLsSession 
    23 #include <apgcli.h>    // For RApaLsSession 
    27 
    24 #include <centralrepository.h>
    28 // Resource file name.
    25 #include <apmstd.h>
    29 _LIT(KCEResourceFile, "z:\\resource\\apps\\apfmimecontentpolicy.rsc"); 
    26 
    30 
    27 
    31 // This is needed for resource reading.
    28 /* Closed content and extension information is stored in central repository with UID 0x10003A3F.
    32 const TInt KCCMask(0x00000fff);
    29  * Keys of the Closed Content and Extension information repository is divided into two parts.
       
    30  * Most significant byte is used for identifying the type,i.e. whether it is Mimetype or extension, 
       
    31  * and the least significant 3 bytes are used for uniquely identifying the entry within that type. 
       
    32  * 
       
    33  * |-------------------- Key (32-bits) ---------------------|
       
    34  *  --------------------------------------------------------
       
    35  * | type (8-bits)  |            sequence number(24-bits)   |
       
    36  *  --------------------------------------------------------
       
    37  * 
       
    38  *  The type part is used for differentiating Content type and extension keys.
       
    39  *  The value can be 
       
    40  *     0x0 - For content type key
       
    41  *     0x1 - For extension key
       
    42  *     
       
    43  * Sequence number part is used to uniquely identifying the entry within that type. 
       
    44  *  
       
    45  *  Examples: 
       
    46  *  
       
    47  *  0x00000000 - Content type key with sequence number 0x0
       
    48  *  0x00000001 - Content type key with sequence number 0x1
       
    49  *  0x01000000 - Extension type key with sequence number 0x0
       
    50  *  0x01000001 - Extension type key with sequence number 0x1
       
    51  *  0x01000002 - Extension type key with sequence number 0x2
       
    52  */
       
    53 
       
    54 
       
    55 
       
    56 //Partial key for finding MIME type keys in the repository
       
    57 const TUint32 KClosedContentTypePartialKey=0x0;
       
    58 
       
    59 //Partial key for finding extension type keys in the repository
       
    60 const TUint32 KClosedExtensionTypePartialKey=0x01000000;
       
    61 
       
    62 //Mask for finding the type (MIME or extension)of a key
       
    63 const TUint32 KClosedTypeKeyMask=0xFF000000;
       
    64 
       
    65 
       
    66 //Closed content and extension information repository UID
       
    67 const TUid KClosedContentAndExtensionInfoRepositoryUID={0x10003A3F};
    33 
    68 
    34 
    69 
    35 NONSHARABLE_CLASS(CApfMimeContentPolicyImpl) : public CBase
    70 NONSHARABLE_CLASS(CApfMimeContentPolicyImpl) : public CBase
    36 	{
    71 	{
    37 public:	 // Constructors and destructor
    72 public:	 // Constructors and destructor
    50 private:	
    85 private:	
    51 	CApfMimeContentPolicyImpl();
    86 	CApfMimeContentPolicyImpl();
    52 	void ConstructL();
    87 	void ConstructL();
    53     void ConstructL(RFs& aFs);
    88     void ConstructL(RFs& aFs);
    54 	TBool IsClosedFileL(RFile& aFileHandle, const TDesC& aFileName) const;
    89 	TBool IsClosedFileL(RFile& aFileHandle, const TDesC& aFileName) const;
    55 	void ReadResourcesL(RFs& aFs);
    90 	void ReadResourcesL();
    56 
    91 	TBool IsValidExtension(TDesC& extension);	
       
    92     TBool IsValidMimeType(TDesC& extension);
    57 private:
    93 private:
    58 	CDesCArrayFlat* iCcl;	// Closed content list.
    94 	CDesCArrayFlat* iCcl;	// Closed content list.
    59 	CDesCArrayFlat* iExtList;	// Closed extensions list.
    95 	CDesCArrayFlat* iExtList;	// Closed extensions list.
    60 	RApaLsSession   iLs;	// A session to the Application Architecture server.
    96 	RApaLsSession   iLs;	// A session to the Application Architecture server.
    61     mutable RFs iFs;	//	File session
    97     mutable RFs iFs;	//	File session
   247 	// Resource reading is done without coe & eikon env.
   283 	// Resource reading is done without coe & eikon env.
   248 	User::LeaveIfError(iFs.Connect());
   284 	User::LeaveIfError(iFs.Connect());
   249 	iFsConnected = ETrue;
   285 	iFsConnected = ETrue;
   250 	
   286 	
   251 	User::LeaveIfError(iFs.ShareProtected());
   287 	User::LeaveIfError(iFs.ShareProtected());
   252 	ReadResourcesL(iFs);
   288 	ReadResourcesL();
   253 	}
   289 	}
   254 	
   290 	
   255 /**
   291 /**
   256 By default Symbian 2nd phase constructor is private.
   292 By default Symbian 2nd phase constructor is private.
   257 @param aFs A handle to a shared file server session. 
   293 @param aFs A handle to a shared file server session. 
   258 */
   294 */
   259 void CApfMimeContentPolicyImpl::ConstructL(RFs& aFs)
   295 void CApfMimeContentPolicyImpl::ConstructL(RFs& aFs)
   260 	{
   296 	{
   261 	iFsConnected = EFalse;
   297 	iFsConnected = EFalse;
   262 	iFs = aFs;	
   298 	iFs = aFs;	
   263 	ReadResourcesL(iFs);
   299 	ReadResourcesL();
   264 	}
   300 	}
   265 
   301 
   266 /**
   302 /**
   267 Two-phased constructor.
   303 Two-phased constructor.
   268 */
   304 */
   488 /**
   524 /**
   489 Reads closed content list and closed extensions list. Connects to RApaLsSession.
   525 Reads closed content list and closed extensions list. Connects to RApaLsSession.
   490 Called by constructor.
   526 Called by constructor.
   491 @param aFs A handle to a shared file server session. 
   527 @param aFs A handle to a shared file server session. 
   492 */
   528 */
   493 void CApfMimeContentPolicyImpl::ReadResourcesL(RFs& aFs)
   529 void CApfMimeContentPolicyImpl::ReadResourcesL()
   494 	{
   530 	{
   495 	TResourceReader reader;	
   531     ASSERT(!iCcl);
   496 
   532     ASSERT(!iExtList);
   497 	// Resource reading is done without coe & eikon env.
   533     
   498 	RResourceFile rsFile;
   534 	CRepository *cenrep=CRepository::NewL(KClosedContentAndExtensionInfoRepositoryUID);	
   499 	rsFile.OpenL(aFs, KCEResourceFile);
   535 	CleanupStack::PushL(cenrep);
   500 	CleanupClosePushL(rsFile);
   536 	
   501 
   537 	RArray<TUint32> keyArray;
   502 	// Read closed content list.
   538 	CleanupClosePushL(keyArray);
   503 	// Remove offset from id
   539 	
   504     HBufC8* rBuffer = rsFile.AllocReadLC(R_COMMONENG_CLOSED_CONTENT_LIST & KCCMask);
   540     TBuf<KMaxDataTypeLength> keyData;
   505 	reader.SetBuffer(rBuffer);
   541     //Find the extenstion type keys in the repository 
   506 	ASSERT(!iCcl);
   542 	cenrep->FindL(KClosedExtensionTypePartialKey, KClosedTypeKeyMask, keyArray);
   507 	iCcl = reader.ReadDesCArrayL();
   543 	int keyCount=keyArray.Count();
   508 	CleanupStack::PopAndDestroy(rBuffer); // rBuffer
   544 
   509 
   545 	iExtList=new (ELeave) CDesCArrayFlat(keyCount);
   510 	// Read closed extensions list.
   546 
   511 	// Remove offset from id
   547 	TInt valid;
   512     rBuffer = rsFile.AllocReadLC(R_COMMONENG_CLOSED_EXTENSIONS_LIST & KCCMask); 
   548 	TInt index;
   513 	reader.SetBuffer(rBuffer);
   549 	//Get each extension type key value and store in iExtList array
   514 	ASSERT(!iExtList);
   550 	for(index=0; index<keyCount; index++)
   515 	iExtList = reader.ReadDesCArrayL();
   551 	    {
   516 	CleanupStack::PopAndDestroy(2); // rBuffer, rsFile 
   552 	    cenrep->Get(keyArray[index], keyData);
   517 	    
   553         //Check validity of the extension. If its invalid it will not be added to list.	    
       
   554 	    valid=IsValidExtension(keyData);
       
   555 	    if(valid)
       
   556 	        iExtList->AppendL(keyData);
       
   557 	    }
       
   558 	
       
   559 	keyArray.Reset();
       
   560 	
       
   561     //Find the content type keys in the repository 	
       
   562     cenrep->FindL(KClosedContentTypePartialKey, KClosedTypeKeyMask, keyArray);
       
   563     keyCount=keyArray.Count();
       
   564     
       
   565     iCcl=new (ELeave) CDesCArrayFlat(keyCount);
       
   566     
       
   567     //Get each content type key value and store in iCcl array
       
   568     for(index=0; index<keyCount; index++)
       
   569         {
       
   570         cenrep->Get(keyArray[index], keyData);  
       
   571         //Check validity of the mime type. If its invalid it will not be added to list.
       
   572         valid=IsValidMimeType(keyData);
       
   573         if(valid)        
       
   574             iCcl->AppendL(keyData);
       
   575         }
       
   576     
       
   577     CleanupStack::PopAndDestroy(2, cenrep);
       
   578 	
   518     // Sort lists to enable binary find
   579     // Sort lists to enable binary find
   519     iCcl->Sort();
   580     iCcl->Sort();
   520     iExtList->Sort();
   581     iExtList->Sort();
   521 
   582 
   522 	// Connect RApaLsSession and leave it open for the whole
   583 	// Connect RApaLsSession and leave it open for the whole
   523 	// lifetime of the object.
   584 	// lifetime of the object.
   524 	User::LeaveIfError(iLs.Connect());
   585 	User::LeaveIfError(iLs.Connect());
   525 	}
   586 	}
   526 
   587 
       
   588 
       
   589 //Checks the given extension is valid or invalid. The extension should start with a ".".
       
   590 TBool CApfMimeContentPolicyImpl::IsValidExtension(TDesC& extension)
       
   591     {
       
   592      TChar dot='.';
       
   593      //Check whether extension should start with "."
       
   594      return(extension.Locate(dot)==0);
       
   595     }
       
   596 
       
   597 //Checks the given mime type is valid or not.
       
   598 //The mime type will be in the following format type/subtype. Ex: "application/vnd.oma.drm.message"
       
   599 //Mime type should posses the following properties. Otherewise those are considered as invalid.
       
   600 //1. Only one front slash should exist. That should be followed by the type field.
       
   601 //2. There should not be any backslashes.
       
   602 
       
   603 TBool CApfMimeContentPolicyImpl::IsValidMimeType(TDesC& mimeType)
       
   604     {
       
   605     TChar backslash='\\';            
       
   606     TChar forwardslash='/';
       
   607     
       
   608     //Check any backslash is used
       
   609     TBool found=mimeType.Locate(backslash);
       
   610     if(found!=KErrNotFound)
       
   611         return(EFalse);
       
   612 
       
   613     //Locate forward slash position
       
   614     found=mimeType.Locate(forwardslash);
       
   615     
       
   616     //There should be at least one forward slash
       
   617     if(found==KErrNotFound)
       
   618         {
       
   619         return EFalse;
       
   620         }
       
   621     else
       
   622         {
       
   623         //Forward slash position should not at first or last position of the mime type
       
   624         if(found==0||(found==mimeType.Length()-1))
       
   625             return EFalse;
       
   626         
       
   627         //There should not be more than one forward slash
       
   628         found=mimeType.Mid(found+1).Locate(forwardslash);
       
   629         if(found!=KErrNotFound)
       
   630             {
       
   631             return(EFalse);       
       
   632             }
       
   633         else
       
   634             {
       
   635             //MIME format is valid
       
   636             return(ETrue);
       
   637             }
       
   638         }
       
   639     }