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