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