creator/engine/src/creator_fileelement.cpp
branchRCL_3
changeset 22 fad26422216a
parent 21 b3cee849fa46
child 23 f8280f3bfeb7
equal deleted inserted replaced
21:b3cee849fa46 22:fad26422216a
     1 /*
       
     2 * Copyright (c) 2010 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: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #include "creator_fileelement.h"
       
    21 #include <DrmPermission.h>
       
    22 #include <DrmConstraint.h>
       
    23 #include "creator_traces.h"
       
    24 #include "creator_file.h"
       
    25 
       
    26 using namespace creatorfile;
       
    27 
       
    28 // 15 minutes per time zone, 60 seconds per minute
       
    29 const TInt KSecondsPerTimeZone = 900;
       
    30 const TInt KMinuteInMicroseconds = 60000000;
       
    31 const TInt KTimeZoneIncrement = 15;
       
    32 
       
    33 /*
       
    34  * 
       
    35  */
       
    36 CCreatorFileElement* CCreatorFileElement::NewL(CCreatorEngine* aEngine, const TDesC& aName, const TDesC& aContext )
       
    37     {
       
    38     CCreatorFileElement* self = new (ELeave) CCreatorFileElement(aEngine);
       
    39     CleanupStack::PushL(self);
       
    40     self->ConstructL(aName, aContext);
       
    41     CleanupStack::Pop(self);
       
    42     return self;
       
    43     }
       
    44 /*
       
    45  * 
       
    46  */
       
    47 CCreatorFileElement::CCreatorFileElement(CCreatorEngine* aEngine) 
       
    48 : 
       
    49 CCreatorScriptElement(aEngine)
       
    50     {
       
    51     iIsCommandElement = ETrue;
       
    52 
       
    53     TTime currUniversal;
       
    54     TTime currLocal;
       
    55     TInt64 result( 0 );
       
    56     currUniversal.UniversalTime();
       
    57     currLocal.HomeTime();
       
    58     result = currLocal.Int64() - currUniversal.Int64();
       
    59     result /= KMinuteInMicroseconds;
       
    60     result /= KTimeZoneIncrement;
       
    61     iTimeZone = I64INT( result );
       
    62     }
       
    63 /*
       
    64  * 
       
    65  */
       
    66 void CCreatorFileElement::ExecuteCommandL()
       
    67     {
       
    68     const CCreatorScriptAttribute* amountAttr = FindAttributeByName(KAmount);
       
    69     TInt fileAmount = 1;    
       
    70     if( amountAttr )
       
    71         {
       
    72         fileAmount = ConvertStrToIntL(amountAttr->Value());
       
    73         }
       
    74     // Get 'fields' element 
       
    75     CCreatorScriptElement* fieldsElement = FindSubElement(KFields);
       
    76     if( fieldsElement && fieldsElement->SubElements().Count() > 0)
       
    77         {
       
    78         // Get sub-elements
       
    79         const RPointerArray<CCreatorScriptElement>& fields = fieldsElement->SubElements();
       
    80         // Create files, the amount of files is defined by fileAmount:
       
    81         for( TInt cI = 0; cI < fileAmount; ++cI )
       
    82             {
       
    83             CFilesParameters* param = new (ELeave) CFilesParameters;
       
    84             CleanupStack::PushL( param );
       
    85             
       
    86             for( TInt i = 0; i < fields.Count(); ++i )
       
    87                 {
       
    88                 CCreatorScriptElement* field = fields[i];
       
    89                 TPtrC elemName = field->Name();
       
    90                 TPtrC elemContent = field->Content();
       
    91 
       
    92                 const CCreatorScriptAttribute* randomAttr = field->FindAttributeByName( KRandomLength );
       
    93                 
       
    94                 if( elemName == KType )
       
    95                     {
       
    96                     param->iFileCommand = GetFileCommandL(elemContent, randomAttr || elemContent.Length() == 0);
       
    97                     }
       
    98                 else if( elemName == KDirectory )
       
    99                     {
       
   100                     delete param->iFullFilePath;
       
   101                     param->iFullFilePath = 0;
       
   102                     if( randomAttr == 0 && elemContent.Length() > 0 )
       
   103                     	{
       
   104                     	param->iFullFilePath = elemContent.AllocL();
       
   105                     	}
       
   106                     else
       
   107                         {
       
   108                         TFileName defaultPath;
       
   109                         iEngine->SetDefaultPathForFileCommandL( param->iFileCommand, defaultPath );
       
   110                         param->iFullFilePath = defaultPath.AllocL();
       
   111                         }
       
   112                     }
       
   113                 else if ( elemName == KEncryption )
       
   114                     {
       
   115                     ParseDRMElementsL( field, param );
       
   116                     }
       
   117                 }
       
   118 
       
   119             if ( !param->iFileCommand )
       
   120                 {
       
   121                 param->iFileCommand = ECmdCreateFileEntryEmptyFolder;
       
   122                 }
       
   123             iEngine->AppendToCommandArrayL( param->iFileCommand, param);
       
   124             CleanupStack::Pop( param );
       
   125             }
       
   126         }
       
   127     else
       
   128     	{
       
   129     	_LIT(KDummy, "");
       
   130     	for( TInt cI = 0; cI < fileAmount; ++cI )
       
   131     		{
       
   132     		CFilesParameters* param = new (ELeave) CFilesParameters;
       
   133     		CleanupStack::PushL(param);
       
   134     		delete param->iFullFilePath;
       
   135     		param->iFullFilePath = 0;
       
   136     		TFileName defaultPath;
       
   137     		param->iFileCommand = GetFileCommandL(KDummy, ETrue);
       
   138     		iEngine->SetDefaultPathForFileCommandL(param->iFileCommand, defaultPath);                
       
   139     		param->iFullFilePath = defaultPath.AllocL();
       
   140     		
       
   141     		iEngine->AppendToCommandArrayL(param->iFileCommand, param);
       
   142     		CleanupStack::Pop(param);
       
   143     		}
       
   144     	}
       
   145     }
       
   146 
       
   147 TInt CCreatorFileElement::GetFileCommandL( const TDesC& aFileIdStr, TBool aRandom ) const
       
   148     {
       
   149     TInt mapCount = sizeof(FileMap) / sizeof(FileMapping);
       
   150     
       
   151     if( aRandom )
       
   152         {
       
   153         return FileMap[iEngine->RandomNumber(0, mapCount-1)].CommandId();
       
   154         }
       
   155     
       
   156     for( TInt i = 0; i < mapCount; ++i )
       
   157         {
       
   158         if( CompareIgnoreCase(FileMap[i].FileName(), aFileIdStr) == 0 )
       
   159             return FileMap[i].CommandId();
       
   160         }
       
   161     LOGSTRING2("CCreatorFileElement::GetFileCommandL: Unknown file id: %S", &aFileIdStr);
       
   162     User::Leave(KErrGeneral);
       
   163     return 0; // Not reached, but disables compiler warning...
       
   164     }
       
   165 
       
   166 void CCreatorFileElement::ParseDRMElementsL( CCreatorScriptElement* aField, CFilesParameters* aParam )
       
   167     {
       
   168     const CCreatorScriptAttribute* encryptionTypeAttr = aField->FindAttributeByName( KType );
       
   169     if ( encryptionTypeAttr && encryptionTypeAttr->Value().Length() > 0 )
       
   170         {
       
   171         aParam->iEncrypt = ETrue;
       
   172         if ( encryptionTypeAttr->Value() == KDRMCD ) 
       
   173             {
       
   174             delete aParam->iPermission;
       
   175             aParam->iPermission = NULL;
       
   176             // ownership transferred:
       
   177             aParam->iPermission = CDRMPermission::NewL();
       
   178             }
       
   179         else if ( encryptionTypeAttr->Value() != KDRMFL )
       
   180             {
       
   181             // must be either KDRMCD or KDRMFL
       
   182             User::Leave( KErrArgument );
       
   183             }
       
   184         }
       
   185     else
       
   186         {
       
   187         User::Leave( KErrArgument );
       
   188         }
       
   189     
       
   190     const RPointerArray<CCreatorScriptElement>& rights = aField->SubElements();
       
   191     
       
   192     if ( encryptionTypeAttr->Value() == KDRMFL && rights.Count() )
       
   193         {
       
   194         // Right definitions not allowed with DRM Forward Lock
       
   195         // User::Leave( KErrArgument ); // Do not leave here
       
   196         return; // Just omit rights definitions
       
   197         }
       
   198     
       
   199     // parse right elements
       
   200     CDRMPermission* drmPermission = aParam->iPermission;
       
   201     for ( TInt r = 0; r < rights.Count(); r++ )
       
   202         {
       
   203         const CCreatorScriptAttribute* rightAttr = rights[r]->FindAttributeByName( KType );
       
   204         if ( rightAttr->Value() == KDRMPlayRight &&
       
   205              !( drmPermission->iAvailableRights & ERightsPlay ) )
       
   206             {
       
   207             drmPermission->iAvailableRights |= ERightsPlay;
       
   208             ParseDRMConstraintsL( rights[r], drmPermission->iPlay );
       
   209             }
       
   210         else if ( rightAttr->Value() == KDRMDisplayRight  &&
       
   211                 !( drmPermission->iAvailableRights & ERightsDisplay ))
       
   212             {
       
   213             drmPermission->iAvailableRights |= ERightsDisplay;
       
   214             ParseDRMConstraintsL( rights[r], drmPermission->iDisplay );
       
   215             }
       
   216         else if ( rightAttr->Value() == KDRMPrintRight  &&
       
   217                 !( drmPermission->iAvailableRights & ERightsPrint ) )
       
   218             {
       
   219             drmPermission->iAvailableRights |= ERightsPrint;
       
   220             ParseDRMConstraintsL( rights[r], drmPermission->iPrint );
       
   221             }
       
   222         else if ( rightAttr->Value() == KDRMExecuteRight  &&
       
   223                 !( drmPermission->iAvailableRights & ERightsExecute ) )
       
   224             {
       
   225             drmPermission->iAvailableRights |= ERightsExecute;
       
   226             ParseDRMConstraintsL( rights[r], drmPermission->iExecute );
       
   227             }
       
   228         }
       
   229     }
       
   230 
       
   231 void CCreatorFileElement::ParseDRMConstraintsL( CCreatorScriptElement* aRight, CDRMConstraint* aConstraint )
       
   232     {
       
   233     const RPointerArray<CCreatorScriptElement>& constraints = aRight->SubElements();
       
   234     for ( TInt c = 0; c < constraints.Count(); c++ )
       
   235         {
       
   236         TPtrC elemName = constraints[c]->Name();
       
   237         TPtrC elemContent = constraints[c]->Content();
       
   238         if ( elemName == KDRMCount )
       
   239             {
       
   240             TInt count = ConvertStrToIntL( elemContent );
       
   241             // apply count constraint
       
   242             aConstraint->iActiveConstraints |= EConstraintCounter;
       
   243             aConstraint->iCounter = count;
       
   244             aConstraint->iOriginalCounter = count;
       
   245             }
       
   246         else if ( elemName == KDRMInterval )
       
   247             {
       
   248             // apply interval constraint           
       
   249             aConstraint->iActiveConstraints |= EConstraintInterval;
       
   250             aConstraint->iInterval = ParseTimeInterval( elemContent );
       
   251             aConstraint->iIntervalStart = Time::NullTTime();
       
   252             }
       
   253         else if ( elemName == KDRMStartTime )
       
   254             {
       
   255             // apply start time constraint           
       
   256             aConstraint->iActiveConstraints |= EConstraintStartTime;
       
   257             TTimeIntervalSeconds offset( iTimeZone * KSecondsPerTimeZone );
       
   258             aConstraint->iStartTime = ConvertToDateTimeL( elemContent ) - offset;
       
   259             }
       
   260         else if ( elemName == KDRMEndTime )
       
   261             {
       
   262             // apply end time constraint
       
   263             aConstraint->iActiveConstraints |= EConstraintEndTime;
       
   264             TTimeIntervalSeconds offset( iTimeZone * KSecondsPerTimeZone );
       
   265             aConstraint->iEndTime = ConvertToDateTimeL( elemContent ) - offset;
       
   266             }
       
   267         else if ( elemName == KDRMAccumulated )
       
   268             {
       
   269             // apply accumulated time constraint
       
   270             aConstraint->iActiveConstraints |= EConstraintAccumulated;
       
   271             if ( aConstraint->iEndTime == Time::NullTTime() )
       
   272                 {
       
   273                 aConstraint->iEndTime = Time::MaxTTime();
       
   274                 }
       
   275             if ( aConstraint->iStartTime == Time::NullTTime() )
       
   276                 {
       
   277                 aConstraint->iStartTime = Time::MinTTime();
       
   278                 }
       
   279             aConstraint->iAccumulatedTime = ParseTimeInterval( elemContent );
       
   280             }
       
   281         }
       
   282     }
       
   283 
       
   284 TTimeIntervalSeconds CCreatorFileElement::ParseTimeInterval( TDesC& aTimeString )
       
   285     {
       
   286     // Iso8601 format (P1Y2M3DT4H5M6S) to TimeIntervalSeconds
       
   287     TLex lex;
       
   288     TInt year = 0;
       
   289     TInt month = 0;
       
   290     TInt day = 0;
       
   291     TInt hour = 0;
       
   292     TInt minute = 0;
       
   293     TInt second = 0;
       
   294 
       
   295     if (aTimeString.Length() > 0)
       
   296         {    
       
   297         lex = aTimeString;
       
   298         lex.Inc(); // skip 'P'
       
   299         lex.Val(year);
       
   300         lex.Inc();
       
   301         lex.Val(month);
       
   302         lex.Inc();
       
   303         lex.Val(day);
       
   304         lex.Inc(2);
       
   305         lex.Val(hour);
       
   306         lex.Inc();
       
   307         lex.Val(minute);
       
   308         lex.Inc();
       
   309         lex.Val(second);
       
   310         }
       
   311     TTimeIntervalSeconds result( ( ( ( year * 365 + month * 30 + day ) * 24 + hour )
       
   312                 * 60 + minute ) * 60 + second );
       
   313     return result;
       
   314     }
       
   315 
       
   316 // End of file