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