creator/engine/src/creator_file.cpp
branchRCL_3
changeset 21 b3cee849fa46
equal deleted inserted replaced
20:48060abbbeaf 21:b3cee849fa46
       
     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 #include <DRMRights.h>
       
    20 
       
    21 #include "engine.h"
       
    22 #include "enginewrapper.h"
       
    23 #include "creator_file.h" 
       
    24 #include "creator_traces.h"
       
    25 
       
    26 using namespace ContentAccess;
       
    27 
       
    28 static const TInt KFilesFieldLength = 256;
       
    29 
       
    30 //_LIT(KCreatorFilesPrefixName, "CR_");
       
    31 //_LIT(KCreatorFilesPrefixFolderName, "CR_FLDR_");
       
    32 
       
    33 //----------------------------------------------------------------------------
       
    34 
       
    35 CFilesParameters::CFilesParameters()
       
    36     {
       
    37     LOGSTRING("Creator: CFilesParameters::CFilesParameters");
       
    38     iFullFilePath = HBufC::New(KFilesFieldLength);
       
    39     }
       
    40 
       
    41 CFilesParameters::CFilesParameters( CFilesParameters& aCopy )
       
    42     {
       
    43     LOGSTRING("Creator: CFilesParameters::CFilesParameters");
       
    44     iFullFilePath = HBufC::New(KFilesFieldLength);
       
    45     iFullFilePath->Des().Copy( *aCopy.iFullFilePath );
       
    46     iFileCommand = aCopy.iFileCommand;
       
    47     iEncrypt = aCopy.iEncrypt;
       
    48     if ( aCopy.iPermission )
       
    49         {
       
    50         TRAP_IGNORE(    
       
    51             iPermission = CDRMPermission::NewL();
       
    52             iPermission->DuplicateL( *aCopy.iPermission );
       
    53             );
       
    54         }
       
    55     }
       
    56 
       
    57 CFilesParameters::~CFilesParameters()
       
    58     {
       
    59     LOGSTRING("Creator: CFilesParameters::~CFilesParameters");
       
    60     delete iFullFilePath;
       
    61     delete iPermission;
       
    62     }
       
    63 
       
    64 //----------------------------------------------------------------------------
       
    65 
       
    66 CCreatorFiles* CCreatorFiles::NewL(CCreatorEngine* aEngine)
       
    67     {
       
    68     CCreatorFiles* self = CCreatorFiles::NewLC(aEngine);
       
    69     CleanupStack::Pop(self);
       
    70     return self;
       
    71     }
       
    72 
       
    73 CCreatorFiles* CCreatorFiles::NewLC(CCreatorEngine* aEngine)
       
    74     {
       
    75     CCreatorFiles* self = new (ELeave) CCreatorFiles;
       
    76     CleanupStack::PushL(self);
       
    77     self->ConstructL(aEngine);
       
    78     return self;
       
    79     }
       
    80 
       
    81 CCreatorFiles::CCreatorFiles() :
       
    82     iFs ( CEikonEnv::Static()->FsSession() )
       
    83     {
       
    84     }
       
    85 
       
    86 void CCreatorFiles::ConstructL(CCreatorEngine* aEngine)
       
    87     {
       
    88     LOGSTRING("Creator: CCreatorFiles::ConstructL");
       
    89 
       
    90     iEngine = aEngine;
       
    91 
       
    92     User::LeaveIfError( iApaLs.Connect() );
       
    93     
       
    94     iFilePaths = new (ELeave) CDesCArrayFlat( 4 );
       
    95     
       
    96     // Restore file id 
       
    97     CDictionaryFileStore* store = iEngine->FileStoreLC();
       
    98     User::LeaveIfNull( store );
       
    99     if ( store->IsPresentL( KUidDictionaryUidFiles ) )
       
   100         {
       
   101         RDictionaryReadStream in;
       
   102         in.OpenLC( *store, KUidDictionaryUidFiles );
       
   103         TRAPD( err, iFileId = in.ReadInt32L() );
       
   104         if ( err )
       
   105             {
       
   106             iFileId = 1;
       
   107             }
       
   108         CleanupStack::PopAndDestroy(); // in
       
   109         }
       
   110     else
       
   111         {
       
   112         iFileId = 1;
       
   113         }
       
   114     CleanupStack::PopAndDestroy( store );
       
   115     }
       
   116 
       
   117 CCreatorFiles::~CCreatorFiles()
       
   118     {
       
   119     LOGSTRING("Creator: CCreatorFiles::~CCreatorFiles");
       
   120     
       
   121     // this is done only once per file operation:
       
   122     if ( iFilePaths && iFilePaths->Count() )
       
   123         {
       
   124         TRAP_IGNORE( StorePathsForDeleteL( *iFilePaths ) );
       
   125         }
       
   126     delete iFilePaths;
       
   127     delete iParameters;
       
   128     delete iUserParameters;
       
   129     iApaLs.Close();
       
   130     }
       
   131 
       
   132 //----------------------------------------------------------------------------
       
   133 
       
   134 void CCreatorFiles::QueryDialogClosedL(TBool aPositiveAction, TInt aUserData)
       
   135     {
       
   136     LOGSTRING("Creator: CCreatorFiles::QueryDialogClosedL");
       
   137     
       
   138     if( aPositiveAction == EFalse )
       
   139         {
       
   140         iEngine->ShutDownEnginesL();
       
   141         return;
       
   142         }
       
   143     
       
   144     const TDesC* showText = &KSavingText;
       
   145     TBool finished(EFalse);
       
   146     TBool retval(ETrue);
       
   147     switch(aUserData)
       
   148         {
       
   149         case ECreatorFilesDelete:
       
   150             showText = &KDeletingText;
       
   151             iEntriesToBeCreated = 1;
       
   152             finished = ETrue;
       
   153             break;
       
   154         case ECreatorFilesStart:
       
   155             {
       
   156             // set a default directory  (eg. c:\Nokia\Images\)
       
   157             iEngine->SetDefaultPathForFileCommandL(iCommand, iDirectoryQueriedFromUser);
       
   158             TBuf<50> promptText;
       
   159             if (iCommand == ECmdCreateFileEntryEmptyFolder)
       
   160                 promptText.Copy( _L("Specify the folder path and name") );
       
   161             else
       
   162                 promptText.Copy( _L("Specify the directory") );
       
   163             
       
   164             // show directory query dialog
       
   165             retval = iEngine->GetEngineWrapper()->DirectoryQueryDialog(promptText, iDirectoryQueriedFromUser, this, ECreatorFilesGetDirectory );
       
   166             }
       
   167             break;
       
   168         case ECreatorFilesGetDirectory:
       
   169             // check that the root folder is correct
       
   170             if ( iDirectoryQueriedFromUser.Length() < 3  ||  BaflUtils::CheckFolder( iFs, iDirectoryQueriedFromUser.Left(3) ) != KErrNone )
       
   171                 {
       
   172                 iEngine->GetEngineWrapper()->ShowErrorMessage(_L("Invalid path"));
       
   173                 retval = EFalse;
       
   174                 }        
       
   175             else
       
   176                 {
       
   177                 // check the directory contains a trailing backlash
       
   178                 if ( iDirectoryQueriedFromUser.Right(1) != _L("\\") )
       
   179                     {
       
   180                     iDirectoryQueriedFromUser.Append(_L("\\"));
       
   181                     }
       
   182                 // copy the directory name to a class member
       
   183                 if ( iCommand == ECmdCreateFileEntryEmptyFolder )
       
   184                     {
       
   185                     finished = ETrue;
       
   186                     }
       
   187                 else
       
   188                     {
       
   189                     retval = AskDRMDataFromUserL();
       
   190                     }
       
   191                 }
       
   192             break;
       
   193         case ECreatorFilesAskDRMData:
       
   194             if ( iDummy > 0 )
       
   195                 {
       
   196                 iUserParameters->iEncrypt = ETrue;
       
   197                 }
       
   198             if ( iDummy == 2 )
       
   199                 {
       
   200                 iUserParameters->iPermission = CDRMPermission::NewL();
       
   201                 CDRMPermission* perm = iUserParameters->iPermission; 
       
   202                 perm->iTopLevel->iActiveConstraints = EConstraintNone;
       
   203                 perm->iPlay->iActiveConstraints = EConstraintNone;
       
   204                 perm->iDisplay->iActiveConstraints = EConstraintNone;
       
   205                 perm->iPrint->iActiveConstraints = EConstraintNone;
       
   206                 perm->iExecute->iActiveConstraints = EConstraintNone;
       
   207                 perm->iUniqueID = 0;
       
   208                 // DRM Combined Delivery
       
   209                 iDummy = 0;
       
   210                 retval = iEngine->GetEngineWrapper()->EntriesQueryDialog( &iDummy, _L("How many counts(0=unlimited)?"), ETrue, this, ECreatorFilesAskDRM_CD_Counts );
       
   211                 }
       
   212             else
       
   213                 {
       
   214                 finished = ETrue;
       
   215                 }
       
   216             break;
       
   217         case ECreatorFilesAskDRM_CD_Counts:
       
   218             if ( iDummy > 0 )
       
   219                 {
       
   220                 TInt count = iDummy;
       
   221                 CDRMPermission* perm = iUserParameters->iPermission;
       
   222                 // apply constraints to all permission types
       
   223                 // applied type will be selected by setting iAvailableRights
       
   224                 // when determining the file type
       
   225                 perm->iDisplay->iActiveConstraints |= EConstraintCounter;
       
   226                 perm->iDisplay->iCounter = count;
       
   227                 perm->iDisplay->iOriginalCounter = count;
       
   228     
       
   229                 perm->iPlay->iActiveConstraints |= EConstraintCounter;
       
   230                 perm->iPlay->iCounter = count;
       
   231                 perm->iPlay->iOriginalCounter = count;
       
   232     
       
   233                 perm->iPrint->iActiveConstraints |= EConstraintCounter;
       
   234                 perm->iPrint->iCounter = count;
       
   235                 perm->iPrint->iOriginalCounter = count;
       
   236                 
       
   237                 perm->iExecute->iActiveConstraints |= EConstraintCounter;
       
   238                 perm->iExecute->iCounter = count;
       
   239                 perm->iExecute->iOriginalCounter = count;
       
   240                 }
       
   241             iDummy = 0;
       
   242             retval = iEngine->GetEngineWrapper()->EntriesQueryDialog( &iDummy, _L("How many minutes until expire(0=unlimited)?"), ETrue, 
       
   243                 this, ECreatorFilesAskDRM_CD_Minutes
       
   244                 );
       
   245             break;
       
   246         case ECreatorFilesAskDRM_CD_Minutes:
       
   247             if ( iDummy > 0 )
       
   248                 {
       
   249                 TInt minutes = iDummy;
       
   250                 CDRMPermission* perm = iUserParameters->iPermission;            
       
   251                 // apply constraints to all permission types
       
   252                 // applied type will be selected by setting iAvailableRights
       
   253                 // when determining the file type            
       
   254                 perm->iDisplay->iActiveConstraints |= EConstraintInterval;
       
   255                 perm->iDisplay->iInterval = TTimeIntervalSeconds( 60 * minutes );
       
   256                 perm->iDisplay->iIntervalStart = Time::NullTTime();
       
   257                 
       
   258                 perm->iPlay->iActiveConstraints |= EConstraintInterval;
       
   259                 perm->iPlay->iInterval = TTimeIntervalSeconds( 60 * minutes );
       
   260                 perm->iPlay->iIntervalStart = Time::NullTTime();
       
   261     
       
   262                 perm->iPrint->iActiveConstraints |= EConstraintInterval;
       
   263                 perm->iPrint->iInterval = TTimeIntervalSeconds( 60 * minutes );
       
   264                 perm->iPrint->iIntervalStart = Time::NullTTime();
       
   265     
       
   266                 perm->iExecute->iActiveConstraints |= EConstraintInterval;
       
   267                 perm->iExecute->iInterval = TTimeIntervalSeconds( 60 * minutes );
       
   268                 perm->iExecute->iIntervalStart = Time::NullTTime();
       
   269                 }
       
   270             finished = ETrue;
       
   271             break;
       
   272         default:
       
   273             //some error
       
   274             retval = EFalse;
       
   275             break;
       
   276         }
       
   277     if( retval == EFalse )
       
   278         {
       
   279         iEngine->ShutDownEnginesL();
       
   280         }
       
   281     else if( finished )
       
   282         {
       
   283         // add this command to command array
       
   284         iEngine->AppendToCommandArrayL(iCommand, NULL, iEntriesToBeCreated);
       
   285         // started exucuting commands
       
   286         iEngine->ExecuteFirstCommandL( *showText );
       
   287         }
       
   288     }
       
   289 
       
   290 //----------------------------------------------------------------------------
       
   291 
       
   292 TBool CCreatorFiles::AskDataFromUserL(TInt aCommand)
       
   293     {
       
   294     LOGSTRING("Creator: CCreatorFiles::AskDataFromUserL");
       
   295     
       
   296     CCreatorModuleBase::AskDataFromUserL( aCommand );
       
   297     
       
   298     if ( aCommand == ECmdDeleteCreatorFiles )
       
   299         {
       
   300         return iEngine->GetEngineWrapper()->YesNoQueryDialog( _L("Delete all files created with Creator?"), this, ECreatorFilesDelete );
       
   301         }
       
   302     
       
   303     delete iUserParameters;
       
   304     iUserParameters = NULL;
       
   305     iUserParameters = new(ELeave) CFilesParameters();
       
   306     
       
   307     iDirectoryQueriedFromUser.Copy( KNullDesC );
       
   308 
       
   309     return iEngine->GetEngineWrapper()->EntriesQueryDialog(&iEntriesToBeCreated, _L("How many entries to create?"), EFalse, this, ECreatorFilesStart );
       
   310     }
       
   311 
       
   312 
       
   313 //----------------------------------------------------------------------------
       
   314 
       
   315 TInt CCreatorFiles::CreateFileEntryL(CFilesParameters *aParameters, TInt aCommand)
       
   316     {
       
   317     LOGSTRING("Creator: CCreatorFiles::CreateFileEntryL");
       
   318 
       
   319     // clear any existing parameter definations
       
   320     delete iParameters;
       
   321     iParameters = NULL;
       
   322     TFileName directoryToBeCreated;
       
   323             
       
   324     CFilesParameters* parameters = aParameters;
       
   325     
       
   326     if (!parameters)
       
   327         {
       
   328         if ( iUserParameters )
       
   329             {
       
   330             iParameters = new (ELeave) CFilesParameters( *iUserParameters );
       
   331             // iUserParameters = NULL;
       
   332             }
       
   333         else
       
   334             {
       
   335             // random data needed if no predefined data available
       
   336             iParameters = new (ELeave) CFilesParameters;            
       
   337             }
       
   338         parameters = iParameters;
       
   339         }
       
   340 
       
   341     TInt err = KErrNone;
       
   342 
       
   343     // if we just create directories
       
   344     if ( aCommand == ECmdCreateFileEntryEmptyFolder)
       
   345         {
       
   346         // strip the last backslash from the path
       
   347         if( iDirectoryQueriedFromUser.Length() > 0)
       
   348         	directoryToBeCreated = iDirectoryQueriedFromUser;
       
   349         else if( parameters->iFullFilePath && parameters->iFullFilePath->Des().Length() > 0 )
       
   350         	directoryToBeCreated = parameters->iFullFilePath->Des();
       
   351         else 
       
   352         	return err;
       
   353         
       
   354         _LIT(KSlash, "\\");
       
   355         if( directoryToBeCreated.Right(1) == KSlash )
       
   356         	directoryToBeCreated.SetLength ( directoryToBeCreated.Length() - 1 ); 
       
   357             
       
   358         // generate a unique file name
       
   359         err = CApaApplication::GenerateFileName( iFs, directoryToBeCreated);
       
   360         if (err != KErrNone)
       
   361             return err;
       
   362 
       
   363         // now append the backslah back
       
   364         directoryToBeCreated.Append( _L("\\") );
       
   365         
       
   366         // now create the new directory
       
   367         err = iFs.MkDirAll( directoryToBeCreated );
       
   368         
       
   369         // Add directoryToBeCreated to store
       
   370         iFilePaths->AppendL( directoryToBeCreated );
       
   371 
       
   372         LOGSTRING3("Creator: CCreatorFiles::CreateFileEntryL creating empty directory %S returns err", &directoryToBeCreated, err);
       
   373         }
       
   374 
       
   375     else  // files
       
   376         {
       
   377         LOGSTRING2("Creator: CCreatorFiles::CreateFileEntryL file id is %d", aCommand);
       
   378 
       
   379         // get source
       
   380         TFileName fullSourcePath;
       
   381         switch (aCommand)
       
   382             {
       
   383 	        case ECmdCreateFileEntryJPEG_25kB:      { fullSourcePath = iEngine->TestDataPathL( CCreatorEngine::EJPEG_25kB );  break; }
       
   384 	        case ECmdCreateFileEntryJPEG_200kB:     { fullSourcePath = iEngine->TestDataPathL( CCreatorEngine::EJPEG_200kB );  break; }
       
   385 	        case ECmdCreateFileEntryJPEG_500kB:     { fullSourcePath = iEngine->TestDataPathL( CCreatorEngine::EJPEG_500kB );  break; }
       
   386 	        case ECmdCreateFileEntryPNG_15kB:       { fullSourcePath = iEngine->TestDataPathL( CCreatorEngine::EPNG_15kB );  break; }
       
   387 	        case ECmdCreateFileEntryGIF_2kB:        { fullSourcePath = iEngine->TestDataPathL( CCreatorEngine::EGIF_2kB );  break; }
       
   388 	        case ECmdCreateFileEntryRNG_1kB:        { fullSourcePath = iEngine->TestDataPathL( CCreatorEngine::ERNG_1kB );  break; }
       
   389 	        case ECmdCreateFileEntryMIDI_10kB:      { fullSourcePath = iEngine->TestDataPathL( CCreatorEngine::EMIDI_10kB );  break; }
       
   390 	        case ECmdCreateFileEntryWAV_20kB:       { fullSourcePath = iEngine->TestDataPathL( CCreatorEngine::EWAVE_20kB );  break; }
       
   391 	        case ECmdCreateFileEntryAMR_20kB:       { fullSourcePath = iEngine->TestDataPathL( CCreatorEngine::EAMR_20kB );  break; }
       
   392 	        case ECmdCreateFileEntryXLS_15kB:       { fullSourcePath = iEngine->TestDataPathL( CCreatorEngine::EExcel_15kB );  break; }
       
   393 	        case ECmdCreateFileEntryDOC_20kB:       { fullSourcePath = iEngine->TestDataPathL( CCreatorEngine::EWord_20kB );  break; }
       
   394 	        case ECmdCreateFileEntryPPT_40kB:       { fullSourcePath = iEngine->TestDataPathL( CCreatorEngine::EPowerPoint_40kB );  break; }
       
   395 	        case ECmdCreateFileEntryTXT_10kB:       { fullSourcePath = iEngine->TestDataPathL( CCreatorEngine::EText_10kB );  break; }
       
   396 	        case ECmdCreateFileEntryTXT_70kB:       { fullSourcePath = iEngine->TestDataPathL( CCreatorEngine::EText_70kB );  break; }
       
   397 	        case ECmdCreateFileEntry3GPP_70kB:      { fullSourcePath = iEngine->TestDataPathL( CCreatorEngine::E3GPP_70kB );  break; }
       
   398 	        case ECmdCreateFileEntryMP3_250kB:      { fullSourcePath = iEngine->TestDataPathL( CCreatorEngine::EMP3_250kB );  break; }
       
   399 	        case ECmdCreateFileEntryAAC_100kB:      { fullSourcePath = iEngine->TestDataPathL( CCreatorEngine::EAAC_100kB );  break; }
       
   400 	        case ECmdCreateFileEntryRM_95kB:        { fullSourcePath = iEngine->TestDataPathL( CCreatorEngine::ERM_95kB );  break; }
       
   401 	        case ECmdCreateFileEntryBMP_25kB:       { fullSourcePath = iEngine->TestDataPathL( CCreatorEngine::EBMP_25kB );  break; }
       
   402 	        case ECmdCreateFileEntryDeck_1kB:       { fullSourcePath = iEngine->TestDataPathL( CCreatorEngine::ESavedDeck_1kB );  break; }
       
   403 	        case ECmdCreateFileEntryHTML_20kB:      { fullSourcePath = iEngine->TestDataPathL( CCreatorEngine::EHTML_20kB );  break; }
       
   404 	        case ECmdCreateFileEntryJAD_1kB:        { fullSourcePath = iEngine->TestDataPathL( CCreatorEngine::EJAD_1kB );  break; }
       
   405 	        case ECmdCreateFileEntryJAR_10kB:       { fullSourcePath = iEngine->TestDataPathL( CCreatorEngine::EJAR_10kB );  break; }
       
   406 	        case ECmdCreateFileEntryJP2_65kB:       { fullSourcePath = iEngine->TestDataPathL( CCreatorEngine::EJP2_65kB );  break; }
       
   407 	        case ECmdCreateFileEntryMP4_200kB:      { fullSourcePath = iEngine->TestDataPathL( CCreatorEngine::EMP4_200kB );  break; }
       
   408 	        case ECmdCreateFileEntryMXMF_40kB:      { fullSourcePath = iEngine->TestDataPathL( CCreatorEngine::EMXMF_40kB );  break; }
       
   409 	        case ECmdCreateFileEntryRAM_1kB:        { fullSourcePath = iEngine->TestDataPathL( CCreatorEngine::ERAM_1kB );  break; }
       
   410 	        case ECmdCreateFileEntrySVG_15kB:       { fullSourcePath = iEngine->TestDataPathL( CCreatorEngine::ESVG_15kB );  break; }
       
   411 	        case ECmdCreateFileEntrySWF_15kB:       { fullSourcePath = iEngine->TestDataPathL( CCreatorEngine::ESWF_15kB );  break; }
       
   412 	        case ECmdCreateFileEntryTIF_25kB:       { fullSourcePath = iEngine->TestDataPathL( CCreatorEngine::ETIF_25kB );  break; }
       
   413 	        case ECmdCreateFileEntryVCF_1kB:        { fullSourcePath = iEngine->TestDataPathL( CCreatorEngine::EVCF_1kB );  break; }
       
   414 	        case ECmdCreateFileEntryVCS_1kB:        { fullSourcePath = iEngine->TestDataPathL( CCreatorEngine::EVCS_1kB );  break; }
       
   415 	        case ECmdCreateFileEntrySISX_10kB:      { fullSourcePath = iEngine->TestDataPathL( CCreatorEngine::ESISX_10kB );  break; }
       
   416 	        case ECmdCreateFileEntryWMA_50kB:       { fullSourcePath = iEngine->TestDataPathL( CCreatorEngine::EWMA_50kB );  break; }
       
   417 	        case ECmdCreateFileEntryWMV_200kB:      { fullSourcePath = iEngine->TestDataPathL( CCreatorEngine::EWMV_200kB );  break; }
       
   418 	        case ECmdCreateFileEntryEmptyFolder:    { User::Panic(_L("EmptyFolder"), 801); break; }
       
   419 	        default:                                { return KErrPathNotFound; }
       
   420             }
       
   421 
       
   422 
       
   423         // define the full target path
       
   424         TFileName fullTargetPath;
       
   425 
       
   426         if ( parameters->iFullFilePath && parameters->iFullFilePath->Des().Length() > 3 )
       
   427             {
       
   428             _LIT(KSlash, "\\");
       
   429             // check the path is ok
       
   430             fullTargetPath = parameters->iFullFilePath->Des();
       
   431             
       
   432             if(fullTargetPath.Right(1) == KSlash)
       
   433                 {
       
   434                 // Remove '\' from the end, because the filename check does not work with it:
       
   435                 fullTargetPath.SetLength ( fullTargetPath.Length() - 1 );
       
   436                 }
       
   437             
       
   438             if (!iFs.IsValidName( fullTargetPath ))
       
   439 		        User::Leave(KErrBadName);
       
   440 
       
   441             // target path = directory + the file name from source path
       
   442             TParse parser;
       
   443             parser.Set(fullSourcePath, NULL, NULL);
       
   444             
       
   445             // Add '\' to the end:
       
   446             fullTargetPath.Append(KSlash);            
       
   447             // Add filename:
       
   448             fullTargetPath.Append( parser.NameAndExt() );
       
   449 
       
   450             LOGSTRING2("Creator: CCreatorFiles::CreateFileEntryL iFullFilePath used, fullTargetPath: %S", &fullTargetPath);
       
   451             }
       
   452     
       
   453         else if ( iDirectoryQueriedFromUser.Length() > 0 )
       
   454             {
       
   455             // target path = directory + the file name from source path
       
   456             TParse parser;
       
   457             parser.Set(fullSourcePath, NULL, NULL);
       
   458 
       
   459             fullTargetPath = iDirectoryQueriedFromUser;
       
   460             fullTargetPath.Append( parser.NameAndExt() );
       
   461 
       
   462             LOGSTRING2("Creator: CCreatorFiles::CreateFileEntryL iDirectoryQueriedFromUser used, fullTargetPath: %S", &fullTargetPath);
       
   463             }
       
   464 
       
   465         else
       
   466             {   
       
   467             LOGSTRING("Creator: CCreatorFiles::CreateFileEntryL leaving with KErrPathNotFound");
       
   468             User::Leave(KErrPathNotFound);
       
   469             }
       
   470     
       
   471         TBool encrypt = parameters->iEncrypt; 
       
   472         if ( encrypt ) fullTargetPath.Append( KOma2DcfExtension );
       
   473         // check the target path has a unique filename ie we won't overwrite existing files
       
   474         // also generates any missing directories
       
   475         if ( BaflUtils::FileExists( iFs, fullTargetPath ) )
       
   476             {
       
   477             GenerateFileNameL( fullTargetPath );
       
   478             }
       
   479         User::LeaveIfError(CApaApplication::GenerateFileName( iFs, fullTargetPath ) );
       
   480         
       
   481         if ( encrypt )
       
   482             {
       
   483             EncryptFileL( fullSourcePath, fullTargetPath, parameters );
       
   484             }
       
   485         else
       
   486             {
       
   487             // copy the file (synchronous function)
       
   488             err = BaflUtils::CopyFile(iFs, fullSourcePath, fullTargetPath);
       
   489             LOGSTRING4("Creator: CCreatorFiles::CreateFileEntryL copy %S to %S, err=%d", &fullSourcePath, &fullTargetPath, err);            
       
   490             }
       
   491         
       
   492         if (err != KErrNone)
       
   493             User::Leave(err);  // leave because copying failed
       
   494 
       
   495         // Add fullTargetPath to store
       
   496         iFilePaths->AppendL( fullTargetPath );
       
   497         
       
   498 		// make sure that the file won't have a read only attribute
       
   499 		TEntry fileEntry;
       
   500 		iFs.Entry(fullTargetPath, fileEntry);
       
   501 		iFs.SetEntry(fullTargetPath, fileEntry.iModified, NULL, KEntryAttReadOnly);
       
   502 
       
   503         // clear variables
       
   504 		parameters->iFullFilePath->Des().Copy ( KNullDesC );
       
   505         }
       
   506 
       
   507     return err;
       
   508     }
       
   509 
       
   510 //----------------------------------------------------------------------------
       
   511 
       
   512 void CCreatorFiles::EncryptFileL( const TDesC& aInFileName, const TDesC& aOutFileName, CFilesParameters *aParameters )
       
   513     {
       
   514     LOGSTRING("Creator: CCreatorFiles::EncryptFileL");
       
   515     TBuf8<64> mime;
       
   516     SetMimeTypeL( aInFileName, mime, aParameters );
       
   517     CSupplier* supplier = CSupplier::NewLC();
       
   518 
       
   519     CMetaDataArray* metaData = CMetaDataArray::NewLC();
       
   520     
       
   521     // Tell the agent which MIME type to use for the encrypted data
       
   522     metaData->AddL( KOmaImportMimeTypeField, mime );
       
   523 
       
   524     if ( aParameters && aParameters->iPermission )
       
   525         {
       
   526         // Combined Delivery file will be created
       
   527         SetPermissionsL( metaData, aOutFileName, aParameters );
       
   528         }
       
   529     
       
   530     supplier->SetOutputDirectoryL( iDirectoryQueriedFromUser );
       
   531     
       
   532     // The KOmaImportContentType is a OMA DRM agent specific MIME type which
       
   533     // indicates that plain content is to be encrypted
       
   534     CImportFile* importFile = supplier->ImportFileL( KOmaImportContentType,
       
   535                                                      *metaData,
       
   536                                                      aOutFileName );
       
   537     CleanupStack::PushL( importFile );
       
   538     
       
   539     // Peek the source file size:
       
   540     TInt fileLen( 0 );
       
   541     RFile file;
       
   542     User::LeaveIfError( file.Open( iFs, aInFileName, EFileRead ) );
       
   543     CleanupClosePushL( file );
       
   544     User::LeaveIfError( file.Size( fileLen ) );
       
   545     CleanupStack::PopAndDestroy( &file );
       
   546     
       
   547     // Read the source file to inmemory buffer
       
   548     RFileReadStream rs;    
       
   549     User::LeaveIfError( rs.Open( iFs,
       
   550                                  aInFileName,
       
   551                                  EFileStream | EFileRead ) ); 
       
   552     CleanupClosePushL( rs );
       
   553     HBufC8* fileBuf = HBufC8::NewLC( fileLen );
       
   554     TPtr8 p = fileBuf->Des();
       
   555     rs.ReadL( p, fileLen );
       
   556     
       
   557     // Start encryption
       
   558     TInt err = importFile->WriteData( p );
       
   559     if ( err == KErrCANewFileHandleRequired )
       
   560         {
       
   561         RFile file;
       
   562         User::LeaveIfError( file.Create( iFs, aOutFileName, EFileWrite ) );
       
   563         CleanupClosePushL( file );
       
   564         importFile->ContinueWithNewOutputFile( file, aOutFileName );
       
   565         CleanupStack::PopAndDestroy( &file );
       
   566         }
       
   567     else
       
   568         {
       
   569         User::LeaveIfError( err );
       
   570         }
       
   571     User::LeaveIfError( importFile->WriteDataComplete() );
       
   572     CleanupStack::PopAndDestroy( fileBuf );
       
   573     CleanupStack::PopAndDestroy( &rs );    
       
   574     CleanupStack::PopAndDestroy( importFile );
       
   575     CleanupStack::PopAndDestroy( metaData );
       
   576     CleanupStack::PopAndDestroy( supplier );
       
   577     }
       
   578 
       
   579 //----------------------------------------------------------------------------
       
   580 
       
   581 void CCreatorFiles::SetPermissionsL( CMetaDataArray* aMetaData, const TDesC& aOutFileName, CFilesParameters *aParameters )
       
   582     {
       
   583     LOGSTRING("Creator: CCreatorFiles::SetPermissionsL");
       
   584     CDRMRights* rights = CDRMRights::NewL();
       
   585     CleanupStack::PushL( rights );
       
   586     
       
   587     HBufC8* cnturi = HBufC8::NewL( KMaxFileName );
       
   588     cnturi->Des().Copy( aOutFileName );
       
   589     
       
   590     CDRMAsset* asset = CDRMAsset::NewLC();
       
   591     asset->iUid = cnturi;
       
   592     // Set the asset to the rights class, it will duplicate the asset
       
   593     rights->SetAssetL( *asset );
       
   594     CleanupStack::PopAndDestroy( asset );
       
   595     
       
   596     rights->SetPermissionL( *aParameters->iPermission );
       
   597     
       
   598     // Construct externalized presentation of the rights object
       
   599     TInt rightsSize = 1024 *100;
       
   600     HBufC8* rightBuf = HBufC8::NewLC( rightsSize ); 
       
   601     TPtr8 bptr = rightBuf->Des();
       
   602     bptr.SetLength( rightsSize );
       
   603     RMemWriteStream iWriteStream;
       
   604     iWriteStream.Open( (TAny*)(rightBuf->Ptr() ), rightsSize );
       
   605     CleanupClosePushL( iWriteStream );
       
   606     iWriteStream << *rights;
       
   607     iWriteStream.CommitL();
       
   608     TPtr8 rp = rightBuf->Des();
       
   609     
       
   610     // Add rights to metadata
       
   611     aMetaData->AddL( KOmaImportRightsField, rp );
       
   612     CleanupStack::PopAndDestroy( &iWriteStream );
       
   613     CleanupStack::PopAndDestroy( rightBuf );
       
   614     CleanupStack::PopAndDestroy( rights );
       
   615     }
       
   616 
       
   617 //----------------------------------------------------------------------------
       
   618 
       
   619 void CCreatorFiles::SetMimeTypeL( const TDesC& aFileName, TDes8& aMime, CFilesParameters *aParameters )
       
   620     {
       
   621     LOGSTRING("Creator: CCreatorFiles::SetMimeTypeL");
       
   622     TUid appUid;
       
   623     TDataType dataType;
       
   624     User::LeaveIfError( iApaLs.AppForDocument( aFileName, appUid, dataType ) );
       
   625     if ( dataType.Des().Length() )
       
   626         {
       
   627         aMime.Copy( dataType.Des() );    
       
   628         }
       
   629     else
       
   630         {
       
   631         // set default mime, because it was not recognized by iApaLs
       
   632         aMime.Copy( _L("text/plain") );
       
   633         }
       
   634 
       
   635     // set DRM permissions according the type of the file
       
   636     if ( aParameters->iPermission )
       
   637         {
       
   638         if ( dataType.Des().FindF( _L("image") ) > KErrNotFound )
       
   639             {
       
   640             aParameters->iPermission->iAvailableRights = ERightsDisplay | ERightsPrint;
       
   641             }
       
   642         else if ( dataType.Des().FindF( _L("audio") ) > KErrNotFound ||
       
   643                   dataType.Des().FindF( _L("video") ) > KErrNotFound ||
       
   644                   dataType.Des().FindF( _L("tone") ) > KErrNotFound || // e.g. application/vnd.nokia.ringing-tone
       
   645                   dataType.Des().FindF( _L("realmedia") ) > KErrNotFound )
       
   646             {
       
   647             // media files
       
   648             aParameters->iPermission->iAvailableRights = ERightsPlay;
       
   649             }
       
   650         else if ( dataType.Des().FindF( _L("archive") ) > KErrNotFound ||
       
   651                   dataType.Des().FindF( _L("x-sis") ) > KErrNotFound )
       
   652             {
       
   653             // application/java-archive
       
   654             // x-epoc/x-sisx-app
       
   655             aParameters->iPermission->iAvailableRights = ERightsExecute;
       
   656             }
       
   657         else if ( dataType.Des().FindF( _L("application") ) > KErrNotFound ||
       
   658                   dataType.Des().FindF( _L("text") ) > KErrNotFound )
       
   659             {
       
   660             // application/msexcel
       
   661             // application/msword
       
   662             // text/plain
       
   663             // etc.
       
   664             aParameters->iPermission->iAvailableRights = ERightsDisplay | ERightsPrint;
       
   665             }
       
   666         else
       
   667             {
       
   668             // other filetype
       
   669             aParameters->iPermission->iAvailableRights = ERightsDisplay;
       
   670             }
       
   671         }
       
   672     }
       
   673 
       
   674 //----------------------------------------------------------------------------
       
   675 
       
   676 TBool CCreatorFiles::AskDRMDataFromUserL()
       
   677     {
       
   678     LOGSTRING("Creator: CCreatorFiles::AskDRMDataFromUserL");
       
   679     // Encryption -dialog
       
   680     CDesCArrayFlat* items = new(ELeave) CDesCArrayFlat(5);
       
   681     CleanupStack::PushL(items);
       
   682 
       
   683     // Add entires to list
       
   684     items->AppendL( _L("None") );
       
   685     items->AppendL( _L("DRM Forward Lock") );
       
   686     items->AppendL( _L("DRM Combined Delivery") );
       
   687 
       
   688     
       
   689 	// create a popup list
       
   690     iDummy = 0;
       
   691     TBool retval = iEngine->GetEngineWrapper()->PopupListDialog(_L("Encryption"), items, &iDummy, this, ECreatorFilesAskDRMData );
       
   692     CleanupStack::PopAndDestroy( items );
       
   693     return retval;
       
   694     }
       
   695 
       
   696 //----------------------------------------------------------------------------
       
   697 void CCreatorFiles::DeleteAllL()
       
   698     {
       
   699     LOGSTRING("Creator: CCreatorFiles::DeleteAllL");
       
   700     User::Leave( KErrNotSupported ); // will not be supported
       
   701     }
       
   702 
       
   703 //----------------------------------------------------------------------------
       
   704 void CCreatorFiles::DeleteAllCreatedByCreatorL()
       
   705     {
       
   706     LOGSTRING("Creator: CCreatorFiles::DeleteAllCreatedByCreatorL");
       
   707     iFileId = 1;
       
   708     CDictionaryFileStore* store = iEngine->FileStoreLC();
       
   709     User::LeaveIfNull( store );
       
   710         
       
   711     if ( store->IsPresentL( KUidDictionaryUidFiles ) )
       
   712         {
       
   713         RDictionaryReadStream in;
       
   714         in.OpenLC( *store, KUidDictionaryUidFiles );
       
   715         CFileMan* fileMan = CFileMan::NewL( iFs );
       
   716         CleanupStack::PushL( fileMan );
       
   717         TFileName fullPath;
       
   718         // ignore return value, don't update iFileId here:
       
   719         TRAPD( err, in.ReadInt32L() );
       
   720         while ( !err )
       
   721             {
       
   722             TInt len( KErrNotFound );
       
   723             TRAP( err, len = in.ReadInt8L() );  // will leave with KErrEof
       
   724             if ( !err )
       
   725                 {
       
   726                 TRAP( err, in.ReadL( fullPath, len ) );                    
       
   727                 }
       
   728             if ( !err )
       
   729                 {
       
   730                 TEntry fileEntry;
       
   731                 iFs.Entry( fullPath, fileEntry );
       
   732                 if ( fileEntry.IsDir() )
       
   733                     {
       
   734                     fileMan->RmDir( fullPath ); // ignore return value
       
   735                     }
       
   736                 else
       
   737                     {
       
   738                     iFs.Delete( fullPath ); // ignore return value    
       
   739                     }                    
       
   740                 }
       
   741             }
       
   742         CleanupStack::PopAndDestroy( fileMan );
       
   743         CleanupStack::PopAndDestroy( &in );
       
   744         
       
   745         // files deleted, remove the Creator internal file registry
       
   746         store->Remove( KUidDictionaryUidFiles );
       
   747         store->CommitL();
       
   748         }
       
   749     CleanupStack::PopAndDestroy( store );
       
   750     }
       
   751 
       
   752 //----------------------------------------------------------------------------
       
   753 void CCreatorFiles::StorePathsForDeleteL( CDesCArray& aPaths )
       
   754     {
       
   755     LOGSTRING("Creator: CCreatorFiles::StorePathsForDeleteL");
       
   756     CDictionaryFileStore* store = iEngine->FileStoreLC();
       
   757     User::LeaveIfNull( store );
       
   758     
       
   759     // backup previous filepaths from store
       
   760     // otherwise they would be overwritten when calling out.WriteL
       
   761     CDesCArray* previousPaths = new (ELeave) CDesCArrayFlat( 4 );
       
   762     CleanupStack::PushL( previousPaths );
       
   763     
       
   764     TFileName fullPath;
       
   765     
       
   766     if ( store->IsPresentL( KUidDictionaryUidFiles ) )
       
   767         {
       
   768         RDictionaryReadStream in;
       
   769         in.OpenLC( *store, KUidDictionaryUidFiles );
       
   770         // ignore return value, don't update iFileId here:
       
   771         TRAPD( err, in.ReadInt32L() );
       
   772         while ( !err ) 
       
   773             {
       
   774             TRAP( err,
       
   775                 TInt len = in.ReadInt8L(); // will leave with KErrEof
       
   776                 in.ReadL( fullPath, len );
       
   777                 previousPaths->AppendL( fullPath );
       
   778                 );
       
   779             }
       
   780         CleanupStack::PopAndDestroy(); // in
       
   781         }
       
   782     
       
   783     RDictionaryWriteStream out;       
       
   784     out.AssignLC( *store, KUidDictionaryUidFiles );
       
   785     
       
   786     // write latest file id to store
       
   787     out.WriteInt32L( iFileId );
       
   788     
       
   789     // restore previous paths to store
       
   790     for ( TInt i = 0; i < previousPaths->Count(); i++ )
       
   791         {
       
   792         out.WriteInt8L( (*previousPaths)[i].Length() );
       
   793         out.WriteL( (*previousPaths)[i] );            
       
   794         }
       
   795 
       
   796     // write new paths to store
       
   797     for ( TInt i = 0; i < aPaths.Count(); i++ )
       
   798         {
       
   799         out.WriteInt8L( aPaths[i].Length() );
       
   800         out.WriteL( aPaths[i] );            
       
   801         }
       
   802     
       
   803     out.CommitL();
       
   804     CleanupStack::PopAndDestroy(); // out
       
   805     
       
   806     store->CommitL();
       
   807     CleanupStack::PopAndDestroy( previousPaths );
       
   808     CleanupStack::PopAndDestroy( store );
       
   809     }
       
   810 
       
   811 //----------------------------------------------------------------------------
       
   812 void CCreatorFiles::GenerateFileNameL( TFileName& aRootName )
       
   813     {
       
   814     LOGSTRING("Creator: CCreatorFiles::GenerateFileNameL");
       
   815     if ( iFileId )
       
   816         {
       
   817         TBuf<16> extension;
       
   818         if ( iFileId < 10 )
       
   819             {
       
   820             extension.Format( _L("(0%d)"), iFileId );     
       
   821             }
       
   822         else
       
   823             {
       
   824             extension.Format( _L("(%d)"), iFileId );
       
   825             }
       
   826         aRootName.Insert( aRootName.Locate( '.' ), extension );
       
   827         }
       
   828     iFileId++;
       
   829     }
       
   830 
       
   831 // End of file