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