calendarui/commonutils/src/calenattachmentutils.cpp
branchRCL_3
changeset 30 bd7edf625bdd
equal deleted inserted replaced
29:12af337248b1 30:bd7edf625bdd
       
     1 /*
       
     2 * Copyright (c) 2009 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 : Utils used for calendar's attachment support
       
    15 *
       
    16 */
       
    17 
       
    18 #include <eikenv.h>
       
    19 #include <eikappui.h>
       
    20 #include <txtetext.h>
       
    21 #include <NpdApi.h>
       
    22 #include <StringLoader.h>
       
    23 #include <MGFetch.h>
       
    24 #include <apparc.h>
       
    25 #include <bautils.h>
       
    26 #include <apgcli.h>
       
    27 #include <AknCommonDialogsDynMem.h>
       
    28 #include <aknlistquerydialog.h>
       
    29 #include <AiwGenericParam.hrh>
       
    30 #include <AiwGenericParam.h>
       
    31 #include <AiwVariant.h>
       
    32 #include <StringLoader.h>           // StringLoader
       
    33 #include <DRMHelper.h>
       
    34 #include <caf/data.h>
       
    35 #include <caf/caf.h>  
       
    36 #include <driveinfo.h>
       
    37 #include <apmrec.h>
       
    38 #include <calencommonui.rsg>
       
    39 #include <avkon.rsg>
       
    40 #include <coemain.h>
       
    41 #include <calattachment.h>
       
    42 #include <CalenInterimUtils2.h>
       
    43 #include <aknnotewrappers.h>
       
    44 #include <DocumentHandler.h>
       
    45 #include <bautils.h>
       
    46 #include "calendar.hrh"
       
    47 #include "calenattachmentutils.h"
       
    48 #include "calendarui_debug.h"
       
    49 
       
    50 const TInt KCalenTextFileNameLength = 20;
       
    51 
       
    52 _LIT( KTextFilenameExtension, ".txt" );
       
    53 _LIT( KCalenUnifiedEditorTempDir, "c:\\private\\10005901\\caleneditor\\" );
       
    54 _LIT8( KNotePadTextDataType, "text/plain" );
       
    55 
       
    56 // -----------------------------------------------------------------------------
       
    57 // CCalenAttachmentUtils::FetchFile
       
    58 // Fetches any media file like images/audio files/video files
       
    59 // -----------------------------------------------------------------------------
       
    60 //
       
    61 EXPORT_C TBool CCalenAttachmentUtils::FetchFileL ( TCalenAttachmentFileType aType,
       
    62                     CDesCArray& aSelectedFiles, CAiwGenericParamList* /*aParams*/,
       
    63                     TBool /*aCheckDiskSpace*/, TBool aMultiSelect,
       
    64                     MMGFetchVerifier* aVerifier )
       
    65     {
       
    66     TRACE_ENTRY_POINT;
       
    67     
       
    68     TBool result = EFalse;
       
    69     switch(aType)
       
    70         {
       
    71         case EImage:
       
    72             {
       
    73             result = FetchExistingL( EImageFile, aSelectedFiles, aMultiSelect, aVerifier );
       
    74             break;
       
    75             }
       
    76         case ENote:
       
    77             {
       
    78             result = FetchNoteL( aSelectedFiles );
       
    79             break;
       
    80             }            
       
    81         case EAudio:
       
    82             {
       
    83             result = FetchExistingL( EAudioFile, aSelectedFiles, aMultiSelect, aVerifier );
       
    84             break;
       
    85             }
       
    86         case EVideo:
       
    87             {
       
    88             result = FetchExistingL( EVideoFile, aSelectedFiles, aMultiSelect, aVerifier );
       
    89             break;
       
    90             }
       
    91         default:
       
    92             {
       
    93             break;
       
    94             }
       
    95         }
       
    96     
       
    97     TRACE_EXIT_POINT;
       
    98     return result;
       
    99     }
       
   100 
       
   101 // -----------------------------------------------------------------------------
       
   102 // CCalenAttachmentUtils::FetchExistingL
       
   103 // Fetches existing file 
       
   104 // -----------------------------------------------------------------------------
       
   105 //
       
   106 TBool CCalenAttachmentUtils::FetchExistingL( TMediaFileType aFileType,
       
   107                                              CDesCArray& aSelectedFiles,
       
   108                                              TBool aMultiSelect,
       
   109                                              MMGFetchVerifier* aVerifier )
       
   110     {
       
   111     TRACE_ENTRY_POINT;
       
   112     TRACE_EXIT_POINT;
       
   113     
       
   114     return MGFetch::RunL( aSelectedFiles, aFileType, aMultiSelect, aVerifier );
       
   115     }
       
   116 
       
   117 // -----------------------------------------------------------------------------
       
   118 // CCalenAttachmentUtils::FetchNoteL
       
   119 // Fetches note using notepad api
       
   120 // -----------------------------------------------------------------------------
       
   121 //
       
   122 TBool CCalenAttachmentUtils::FetchNoteL(CDesCArray& aSelectedFiles)
       
   123     {
       
   124     TRACE_ENTRY_POINT;
       
   125     
       
   126     TBool result = EFalse;
       
   127     CEikonEnv* eikonEnv = CEikonEnv::Static();
       
   128 
       
   129     // get memo content from notepad to buffer.
       
   130     HBufC* buf = CNotepadApi::FetchMemoL();
       
   131 
       
   132     if(buf)
       
   133         {
       
   134         CleanupStack::PushL(buf);
       
   135 
       
   136         RFs& fs = eikonEnv->FsSession();
       
   137         TFileName filename;
       
   138         TInt max = KCalenTextFileNameLength;
       
   139 
       
   140         // first try to create filename from memo text.
       
   141         GetFileNameFromBuffer( filename, *buf, max, &KTextFilenameExtension );
       
   142 
       
   143         if(filename.Length()== 0)
       
   144             {
       
   145             eikonEnv->ReadResourceL( filename, R_QTN_CALEN_NO_NAME_FOR_FILE );
       
   146             filename.Append( KTextFilenameExtension );
       
   147             }
       
   148 
       
   149         TFileName pathname;
       
   150         GetCalenEditorTempPath( pathname, &filename );
       
   151 
       
   152         // check the file name for validity and possible name duplicates.
       
   153         TInt err = KErrNone;
       
   154         if(!BaflUtils::FolderExists(fs,pathname))
       
   155             {
       
   156             err = CApaApplication::GenerateFileName( fs, pathname );
       
   157             }
       
   158         
       
   159         if(err==KErrNone)
       
   160             {
       
   161             // write buffer to text file
       
   162             CPlainText* text = CPlainText::NewL();
       
   163             CleanupStack::PushL( text );
       
   164             text->InsertL( 0, *buf );
       
   165             text->ExportAsTextL( pathname, CPlainText::EOrganiseByParagraph, 0 );
       
   166             CleanupStack::PopAndDestroy( text );
       
   167             aSelectedFiles.AppendL( pathname );
       
   168             result = ETrue;
       
   169             }
       
   170 
       
   171         CleanupStack::PopAndDestroy( buf );
       
   172         }
       
   173     
       
   174     TRACE_EXIT_POINT;
       
   175     return result;
       
   176     }
       
   177 
       
   178 //----------------------------------------------------------------------------
       
   179 // from: TBool CImRecvConvert::IsIllegalChar(const TUint aChar)
       
   180 //----------------------------------------------------------------------------
       
   181 LOCAL_C TBool IsIllegalChar(const TUint aChar)
       
   182     {
       
   183     TRACE_ENTRY_POINT;
       
   184     TRACE_EXIT_POINT;
       
   185     
       
   186     return (
       
   187         aChar == '*'  ||
       
   188         aChar == '//' ||
       
   189         aChar == '<'  ||
       
   190         aChar == '>'  ||
       
   191         aChar == ':'  ||
       
   192         aChar == '.'  ||
       
   193         aChar == '"'  ||
       
   194         aChar == '/'  ||
       
   195         aChar == '|'  ||
       
   196         aChar == '?'  ||
       
   197         aChar == CEditableText::EParagraphDelimiter  ||
       
   198         aChar == CEditableText::ELineBreak  ||
       
   199         aChar <  ' ' );
       
   200     }
       
   201 
       
   202 // -----------------------------------------------------------------------------
       
   203 // CCalenAttachmentUtils::GetFileNameFromBuffer
       
   204 // Get the file name by making use of the content
       
   205 // -----------------------------------------------------------------------------
       
   206 //
       
   207 void CCalenAttachmentUtils::GetFileNameFromBuffer( TFileName& aFileName,
       
   208             const TDesC& aBuffer, TInt aMaxLength, const TDesC* aExt /*= NULL*/ )
       
   209     {
       
   210     TRACE_ENTRY_POINT;
       
   211     
       
   212     if(aExt!=NULL)
       
   213         {
       
   214         aMaxLength -= aExt->Length();
       
   215         }
       
   216 
       
   217     TInt len = aBuffer.Length();
       
   218     TInt max = Min( len, aMaxLength );
       
   219     aFileName.Zero();
       
   220 
       
   221     TInt cc = 0;
       
   222     TUint ch;
       
   223     TUint ch1 = 0;
       
   224     TBool spaces = EFalse;
       
   225     for( TInt i = 0; i < len && cc < max; i++ )
       
   226         {
       
   227         ch = aBuffer[i];
       
   228 
       
   229         // ignore spaces from beginning of the buffer until first
       
   230         // non-space is encountered.
       
   231         if( !spaces && ch != ' ' )
       
   232             {
       
   233             spaces = ETrue;
       
   234             }
       
   235 
       
   236         if(i>0)
       
   237             {
       
   238             ch1 = aBuffer[i-1];
       
   239             }
       
   240 
       
   241         // strip illegal chars away.
       
   242         // checks also if previous and current chars are '.'
       
   243         if( spaces && !IsIllegalChar(ch) )
       
   244             {
       
   245             if( !( i > 0 && ch == '.' && ch1 == '.' ) )
       
   246                 {
       
   247                 aFileName.Append( ch );
       
   248                 cc++;
       
   249                 }
       
   250             }
       
   251         }
       
   252 
       
   253     aFileName.Trim();
       
   254 
       
   255     // If filename is empty at this point, do not append extension either.
       
   256     // Instead, empty filename is returned so that caller can use whatever
       
   257     // default name
       
   258     if( aFileName.Length() > 0 && aExt != NULL )
       
   259         {
       
   260         aFileName.Append(*aExt);
       
   261         }
       
   262     
       
   263     TRACE_EXIT_POINT;
       
   264     }
       
   265 
       
   266 // -----------------------------------------------------------------------------
       
   267 // CCalenAttachmentUtils::GetCalenEditorTempPath
       
   268 // Returns temp path used by unified editor and optionally
       
   269 // appends file name to it.
       
   270 // -----------------------------------------------------------------------------
       
   271 //
       
   272 EXPORT_C void CCalenAttachmentUtils::GetCalenEditorTempPath( TFileName& aTempPath,
       
   273                                                     const TDesC* aFileName )
       
   274     {
       
   275     TRACE_ENTRY_POINT;
       
   276     
       
   277     TInt driveNumber( EDriveD );
       
   278     
       
   279     #ifdef RD_MULTIPLE_DRIVE
       
   280         if(DriveInfo::GetDefaultDrive( DriveInfo::EDefaultRam, driveNumber ) != KErrNone )
       
   281             {
       
   282             DriveInfo::GetDefaultDrive( DriveInfo::EDefaultPhoneMemory, driveNumber );
       
   283             }
       
   284     #endif
       
   285 
       
   286     TChar driveCharacter;
       
   287     CEikonEnv::Static()->FsSession().DriveToChar( driveNumber, driveCharacter );
       
   288 
       
   289     //aTempPath.Append( driveCharacter );
       
   290     aTempPath.Append( KCalenUnifiedEditorTempDir );
       
   291     
       
   292     if(aFileName)
       
   293         {
       
   294         aTempPath.Append(*aFileName);
       
   295         }
       
   296     
       
   297     TRACE_EXIT_POINT; 
       
   298     }
       
   299 
       
   300 // -----------------------------------------------------------------------------
       
   301 // CCalenAttachmentUtils::FetchAnyFileL
       
   302 // Fetches any file using common dialogs using file storage
       
   303 // -----------------------------------------------------------------------------
       
   304 //
       
   305 EXPORT_C TBool CCalenAttachmentUtils::FetchAnyFileL( TFileName& aFileName,
       
   306                                            MAknFileSelectionObserver* aVerifier,
       
   307                                            MAknFileFilter* aFilter )
       
   308     {
       
   309     TRACE_ENTRY_POINT;
       
   310     
       
   311     TBool result(EFalse);
       
   312     TInt supportedTypes = AknCommonDialogsDynMem::EMemoryTypePhone | 
       
   313                           AknCommonDialogsDynMem::EMemoryTypeInternalMassStorage | 
       
   314                           AknCommonDialogsDynMem::EMemoryTypeMMCExternal;
       
   315                            
       
   316     result = AknCommonDialogsDynMem::RunSelectDlgLD( supportedTypes,
       
   317                                                aFileName,
       
   318                                                R_CALEN_MEMORY_SELECTION_DIALOG,
       
   319                                                aFilter,
       
   320                                                aVerifier );
       
   321     
       
   322     TRACE_EXIT_POINT;
       
   323     return result;
       
   324     }
       
   325 
       
   326 // -----------------------------------------------------------------------------
       
   327 // CCalenAttachmentUtils::GetMimeType
       
   328 // Reads file's Mime type
       
   329 // -----------------------------------------------------------------------------
       
   330 //
       
   331 EXPORT_C TDataType CCalenAttachmentUtils::GetMimeType(RFile aFile)
       
   332     {
       
   333     TRACE_ENTRY_POINT;
       
   334 
       
   335     TDataType dataType;
       
   336     RApaLsSession aApaSession;
       
   337     if(aFile.SubSessionHandle())
       
   338         {
       
   339         TUid appUid(KNullUid);
       
   340         TInt error(0);
       
   341         
       
   342         if(!aApaSession.Handle())
       
   343             {
       
   344             error = aApaSession.Connect();
       
   345             }
       
   346         if(error == KErrNone)
       
   347             {
       
   348             TDataRecognitionResult recognitionResult;
       
   349             error = aApaSession.RecognizeData(aFile, recognitionResult);
       
   350             if( recognitionResult.iConfidence != CApaDataRecognizerType::ENotRecognized &&
       
   351                 error == KErrNone)
       
   352                 {
       
   353                 dataType = recognitionResult.iDataType;
       
   354                 }
       
   355             else
       
   356                 {
       
   357                 // not recognised
       
   358                 }
       
   359             }
       
   360         }
       
   361     
       
   362     aApaSession.Close();
       
   363     
       
   364     TRACE_EXIT_POINT;
       
   365     return dataType;    
       
   366     }
       
   367 
       
   368 // -----------------------------------------------------------------------------
       
   369 // CCalenAttachmentUtils::GetMimeType
       
   370 // Reads file's Mime type
       
   371 // -----------------------------------------------------------------------------
       
   372 //
       
   373 EXPORT_C TDataType CCalenAttachmentUtils::GetMimeTypeL(const TDesC& aFileName)
       
   374     {
       
   375     TRACE_ENTRY_POINT;
       
   376     
       
   377     TDataType dataType;
       
   378     RFs& fs = CCoeEnv::Static()->FsSession();
       
   379     RFile attachmentFile;
       
   380     User::LeaveIfError( attachmentFile.Open( fs, aFileName,
       
   381                                              EFileShareReadersOnly) );
       
   382     CleanupClosePushL(attachmentFile);
       
   383     dataType = GetMimeType(attachmentFile);    
       
   384     CleanupStack::PopAndDestroy();
       
   385     
       
   386     TRACE_EXIT_POINT;
       
   387     return dataType;    
       
   388     }
       
   389 
       
   390 // -----------------------------------------------------------------------------
       
   391 // CCalenAttachmentUtils::GetMimeType
       
   392 // Gets file size in bytes based on the file name
       
   393 // -----------------------------------------------------------------------------
       
   394 //
       
   395 EXPORT_C TInt CCalenAttachmentUtils::GetFileSizeL(const TDesC& aFileName)
       
   396     {
       
   397     TRACE_ENTRY_POINT;
       
   398 
       
   399     RFs& fs = CCoeEnv::Static()->FsSession();
       
   400     RFile attachmentFile;
       
   401     TInt fileSize(0);
       
   402     User::LeaveIfError( attachmentFile.Open( fs, aFileName,
       
   403                                              EFileShareReadersOnly) );
       
   404     CleanupClosePushL(attachmentFile);
       
   405     attachmentFile.Size(fileSize);
       
   406     CleanupStack::PopAndDestroy();
       
   407     
       
   408     TRACE_EXIT_POINT
       
   409     return fileSize;
       
   410     }
       
   411 
       
   412 // -----------------------------------------------------------------------------
       
   413 // CCalenAttachmentUtils::OpenAttachmentL
       
   414 // Opens a particular attachment
       
   415 // -----------------------------------------------------------------------------
       
   416 //
       
   417 EXPORT_C void CCalenAttachmentUtils::OpenAttachmentL(RFile& file, MAknServerAppExitObserver& aExitObserver)
       
   418     {
       
   419     TRACE_ENTRY_POINT;
       
   420     CDocumentHandler* docHandler = CDocumentHandler::NewL( CEikonEnv::Static()->Process() );
       
   421 
       
   422     docHandler->SetExitObserver( &aExitObserver );
       
   423     
       
   424     TBuf<250> fileName;
       
   425     file.FullName(fileName);
       
   426     TDataType datatype( GetMimeTypeL(fileName) );
       
   427     
       
   428     TInt ret = KErrNone;
       
   429     
       
   430     file.Close();
       
   431     RFs& fs = CEikonEnv::Static()->FsSession();
       
   432                     
       
   433     //open the file, before giving it to Notepad.
       
   434     TInt err1 = file.Open( fs, fileName, EFileRead | EFileShareReadersOnly );
       
   435     CleanupClosePushL( file );
       
   436     
       
   437     const TDesC& notepadTitle = _L("NotePad");
       
   438     
       
   439     if(datatype == KNotePadTextDataType())
       
   440         {
       
   441         // file handle ownership transferred.
       
   442         ret = CNotepadApi::ExecFileViewerL( file, 
       
   443                                             &notepadTitle,
       
   444                                             ETrue,
       
   445                                             EFalse,
       
   446                                             KCharacterSetIdentifierIso88591 );
       
   447 
       
   448         }
       
   449     else
       
   450         {
       
   451         //doc handler will open the other files (other than text file).
       
   452         TRAP( ret, docHandler->OpenFileEmbeddedL( file, datatype ) );
       
   453         }
       
   454     CleanupStack::PopAndDestroy(&file);
       
   455     delete docHandler;
       
   456     switch(ret)
       
   457         {
       
   458         case KErrNone:
       
   459             break;
       
   460         default:
       
   461             {
       
   462             CAknInformationNote* note = new ( ELeave ) CAknInformationNote;
       
   463             HBufC* cannotOpen = StringLoader::LoadL( 
       
   464                     R_QTN_CALEN_INFO_CANNOT_OPEN, CEikonEnv::Static() );
       
   465             note->ExecuteLD( *cannotOpen );
       
   466             CleanupStack::PopAndDestroy();
       
   467             }
       
   468             break;
       
   469         }
       
   470     TRACE_EXIT_POINT
       
   471     }  
       
   472 
       
   473 // -----------------------------------------------------------------------------
       
   474 // CCalenAttachmentUtils::SaveAttachmentL
       
   475 // Saves a particular attachment
       
   476 // -----------------------------------------------------------------------------
       
   477 //
       
   478 EXPORT_C TInt CCalenAttachmentUtils::SaveAttachmentL(RFile& aFile, MAknServerAppExitObserver& aExitObserver)
       
   479     {
       
   480     TRACE_ENTRY_POINT;
       
   481     TFileName fileName;
       
   482     aFile.FullName(fileName);
       
   483     TDataType nullType;
       
   484     CDocumentHandler* handler = CDocumentHandler::NewL( CEikonEnv::Static()->Process() );
       
   485     handler->SetExitObserver( &aExitObserver );
       
   486     TInt error = handler->CopyL( aFile, fileName, nullType, NULL );
       
   487     delete handler;
       
   488     TRACE_EXIT_POINT
       
   489     return error;
       
   490     }
       
   491 	
       
   492 // end of file
       
   493