messagingfw/senduiservices/datautils/src/CSendUiAttachment.cpp
changeset 0 8e480a14352b
equal deleted inserted replaced
-1:000000000000 0:8e480a14352b
       
     1 /*
       
     2 * Copyright (c) 2002-2007 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:   Encapsulates file path and handle attachments into single class.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #include <f32file.h> 
       
    21 #include <e32base.h> 
       
    22 #include <apmstd.h>
       
    23 #include <badesca.h>
       
    24 #include <eikenv.h>
       
    25 #include <msgtextutils.h>
       
    26 #include <txtrich.h>
       
    27 #include <MuiuMsvUiServiceUtilities.h> 
       
    28 #include <mmf/common/mmfcontrollerpluginresolver.h>
       
    29 #include <sysutil.h>
       
    30 #include <MsgMimeTypes.h>
       
    31 
       
    32 #ifdef RD_MULTIPLE_DRIVE
       
    33 #include <driveinfo.h>
       
    34 #endif
       
    35 #include "CSendUiAttachment.h"
       
    36 
       
    37 // Temporary is file saved to RAM drive
       
    38 _LIT( KSenduiTempFilePath, ":\\system\\temp\\sendui\\");
       
    39 _LIT( KSenduiTempFilePathFormat, "%x_%x" );
       
    40 _LIT16( KExtTextPlain_16, ".txt" );
       
    41 const TInt KSendUiMaxTemporaryFileNameLength = 15;
       
    42 
       
    43 // -----------------------------------------------------------------------------
       
    44 //
       
    45 // -----------------------------------------------------------------------------
       
    46 //
       
    47 CSendUiAttachment::CSendUiAttachment() :CBase()
       
    48 	{
       
    49 	}
       
    50 
       
    51 // -----------------------------------------------------------------------------
       
    52 // Destructor
       
    53 // -----------------------------------------------------------------------------
       
    54 //
       
    55 CSendUiAttachment::~CSendUiAttachment()
       
    56 	{
       
    57 	if ( Type() == EAttachmentPath )
       
    58 		{
       
    59 		RFile* file = MUTABLE_CAST( RFile*, iHandle ); // must close temporary file before deleting it
       
    60 		file->Close();
       
    61 		delete file;
       
    62 		if ( iFlags & EAttachmentTemporary && iFileManager )
       
    63 		    {
       
    64             // Remove temp file and directory
       
    65             iFileManager->Delete( *iPath );
       
    66             iFileManager->RmDir( *iPath );  // In case of rubbish, empty the folder
       
    67  		    }
       
    68         if ( iFileManager ) 
       
    69             {
       
    70             delete iFileManager;
       
    71             }
       
    72 		delete iPath;
       
    73 		}
       
    74 	}	
       
    75 // -----------------------------------------------------------------------------
       
    76 // NewLC, Path
       
    77 // -----------------------------------------------------------------------------
       
    78 //
       
    79 EXPORT_C CSendUiAttachment*	CSendUiAttachment::NewLC( const TDesC16* aPath, RFs& aRFs )
       
    80     {
       
    81     CSendUiAttachment* self = new(ELeave)CSendUiAttachment( );
       
    82     CleanupStack::PushL( self );
       
    83     self->ConstructL( aPath, aRFs );
       
    84     return self;
       
    85     }
       
    86 
       
    87 // -----------------------------------------------------------------------------
       
    88 // ConstructL
       
    89 // -----------------------------------------------------------------------------
       
    90 //
       
    91 void CSendUiAttachment::ConstructL( const TDesC16* aPath, RFs& aRFs )
       
    92     {
       
    93     RFile* myfile = new(ELeave)RFile;
       
    94     CleanupStack::PushL( myfile );
       
    95     TInt err = myfile->Open( aRFs, *aPath, EFileShareReadersOnly );
       
    96     
       
    97     if ( err )
       
    98         {
       
    99         User::LeaveIfError( myfile->Open( aRFs, *aPath, EFileShareAny ) );
       
   100         }
       
   101     
       
   102     iPath = aPath->AllocL();
       
   103     myfile->Size( iSize );
       
   104     iHandle = myfile;
       
   105     CleanupStack::Pop( myfile );    
       
   106     }
       
   107 // -----------------------------------------------------------------------------
       
   108 // NewLC, file handle
       
   109 // -----------------------------------------------------------------------------
       
   110 //
       
   111 EXPORT_C CSendUiAttachment*	CSendUiAttachment::NewLC( const RFile* aHandle )
       
   112     {
       
   113     CSendUiAttachment* self = new(ELeave)CSendUiAttachment( );
       
   114     CleanupStack::PushL( self );
       
   115     self->iHandle = aHandle;
       
   116     aHandle->Size( self->iSize );
       
   117     return self;
       
   118     }
       
   119 
       
   120 // -----------------------------------------------------------------------------
       
   121 // NewLC, BodyText
       
   122 // -----------------------------------------------------------------------------
       
   123 //
       
   124 EXPORT_C CSendUiAttachment*	CSendUiAttachment::NewLC( const CRichText& aBodyText, RFs& aRFs )
       
   125     {
       
   126     CSendUiAttachment* self = new(ELeave)CSendUiAttachment( );
       
   127     CleanupStack::PushL( self );
       
   128     self->ConstructL( aBodyText, aRFs );
       
   129     return self;
       
   130     }
       
   131 
       
   132 // -----------------------------------------------------------------------------
       
   133 // ConstructL, BodyText
       
   134 // -----------------------------------------------------------------------------
       
   135 //
       
   136 void CSendUiAttachment::ConstructL( const CRichText& aBodyText, RFs& aRFs )
       
   137     {
       
   138     // character count * 2 = message size
       
   139     TInt err = KErrNone;
       
   140     TInt drive;
       
   141 #ifdef RD_MULTIPLE_DRIVE
       
   142     User::LeaveIfError( DriveInfo::GetDefaultDrive(
       
   143         DriveInfo::EDefaultRam, drive ) );
       
   144 #else
       
   145     drive = EDriveD;
       
   146 #endif    
       
   147     if ( SysUtil::DiskSpaceBelowCriticalLevelL( &aRFs, aBodyText.DocumentLength() * 2, drive ) )
       
   148         {
       
   149         User::Leave( KErrDiskFull );
       
   150         }
       
   151     
       
   152     HBufC* buf = HBufC::NewLC( KMaxFileName );
       
   153     TPtr fileNameBuf = buf->Des();
       
   154     TChar driveLetter = TChar('A');
       
   155     driveLetter += drive;
       
   156     fileNameBuf.Append( driveLetter );  
       
   157     iFileManager = CFileMan::NewL( aRFs );
       
   158     
       
   159     TFileName fileName;
       
   160     TPtrC textSnippet;
       
   161     TCharFormat charFormat;
       
   162     aBodyText.GetChars( textSnippet, charFormat, 0 );
       
   163     
       
   164     CMsgTextUtils::GetFileNameFromBuffer(
       
   165         fileName,
       
   166         textSnippet,
       
   167         KSendUiMaxTemporaryFileNameLength,
       
   168         &KExtTextPlain_16 );
       
   169 
       
   170     CreateTempPathL( fileNameBuf, fileName, aRFs );
       
   171     
       
   172     aBodyText.ExportAsTextL( fileNameBuf, CPlainText::EOrganiseByParagraph, 0 );
       
   173     
       
   174     iPath = fileNameBuf.AllocL();
       
   175     CleanupStack::PopAndDestroy( buf );
       
   176     SetFlags( EAttachmentTemporary );    
       
   177     
       
   178     // Open handle
       
   179     RFile* myfile = new(ELeave)RFile;
       
   180     CleanupStack::PushL( myfile );
       
   181     err = myfile->Open( aRFs, *iPath, EFileShareReadersOnly );
       
   182     
       
   183     if ( err )
       
   184         {
       
   185         User::LeaveIfError( myfile->Open( aRFs, *iPath, EFileShareAny ) );
       
   186         }
       
   187 
       
   188     myfile->Size( iSize );
       
   189     iHandle = myfile;
       
   190     CleanupStack::Pop( myfile ); 
       
   191     SetMimeType( TDataType(  KMsgMimeTextPlain ));
       
   192     }
       
   193 
       
   194 // -----------------------------------------------------------------------------
       
   195 // Fills the attachment array
       
   196 // -----------------------------------------------------------------------------
       
   197 //
       
   198 EXPORT_C CArrayPtrFlat<CSendUiAttachment>* CSendUiAttachment::InitAttachmentArrayLCC(
       
   199     const CDesC16Array& aPaths,
       
   200     const RArray<RFile>& aHandles,
       
   201     RFs& aFSs )
       
   202 	{
       
   203     TInt i = 0;
       
   204     if ( &aPaths ) 
       
   205         {
       
   206         i += aPaths.Count();
       
   207         }
       
   208 	if ( &aHandles )
       
   209 	    {
       
   210 	    i += aHandles.Count();
       
   211 	    }
       
   212     
       
   213     CArrayPtrFlat<CSendUiAttachment>* attachments = new(ELeave)CArrayPtrFlat<CSendUiAttachment>( i ? i : 1);
       
   214 	CleanupStack::PushL( attachments );
       
   215 	CleanupResetAndDestroyPushL( *attachments );
       
   216 	CSendUiAttachment* attachment;
       
   217 	
       
   218     if ( &aPaths )
       
   219     	{
       
   220 	    for ( i = 0; i < aPaths.Count(); i++ )
       
   221 	        {
       
   222 	        TPtrC16 tmp(aPaths.MdcaPoint( i ) );
       
   223 	        attachment = CSendUiAttachment::NewLC( &tmp, aFSs );
       
   224 	        attachments->AppendL( attachment );
       
   225             CleanupStack::Pop( attachment );
       
   226 	        }
       
   227     	}
       
   228     if ( &aHandles != NULL )
       
   229     	{
       
   230 	    for ( i = 0; i < aHandles.Count(); i++ )
       
   231 	        {
       
   232 	        attachment = CSendUiAttachment::NewLC( &(aHandles[i] ));
       
   233 	        attachments->AppendL( attachment );
       
   234             CleanupStack::Pop( attachment );
       
   235 	        }
       
   236     	}
       
   237     
       
   238     return attachments;
       
   239 	}
       
   240 	
       
   241 // -----------------------------------------------------------------------------
       
   242 // CreateTempPathL
       
   243 //
       
   244 // Creates temp path and unique file name under d:\system\temp\sendui
       
   245 // (other items were commented in a header).
       
   246 // -----------------------------------------------------------------------------
       
   247 //
       
   248 EXPORT_C void CSendUiAttachment::CreateTempPathL( 
       
   249     TDes16& aCompletePath, 
       
   250     TFileName& aTempFileName,
       
   251     RFs& aRFs )
       
   252     {
       
   253     aCompletePath.Append( KSenduiTempFilePath );
       
   254     
       
   255     if ( aTempFileName.Length() == 0 )
       
   256         {
       
   257         aCompletePath.Append( KSenduiTempFilePathFormat );
       
   258 
       
   259         TTime time;
       
   260         time.UniversalTime();
       
   261 
       
   262         TFileName* tempDir = new(ELeave) TFileName();
       
   263         CleanupStack::PushL( tempDir );
       
   264         tempDir->Format(
       
   265             aCompletePath ,
       
   266             I64HIGH( time.Int64() ),
       
   267             I64LOW( time.Int64() ) );
       
   268         aCompletePath = *tempDir;
       
   269         CleanupStack::PopAndDestroy( tempDir );
       
   270         }
       
   271     else
       
   272         {
       
   273         aCompletePath.Append( aTempFileName );
       
   274         }
       
   275 
       
   276     TInt err = aRFs.MkDirAll( aCompletePath );
       
   277 	// path may exists
       
   278 	if ( err && err != KErrAlreadyExists )
       
   279 		{
       
   280 		User::LeaveIfError( err );
       
   281 		}
       
   282     }  
       
   283 
       
   284 // -----------------------------------------------------------------------------
       
   285 // Type
       
   286 // -----------------------------------------------------------------------------
       
   287 //
       
   288 EXPORT_C TInt CSendUiAttachment::Type()
       
   289 	{
       
   290 	if ( iPath )
       
   291 		{
       
   292 		return EAttachmentPath;
       
   293 		}
       
   294 	else 
       
   295 		{
       
   296 		return  EAttachmentHandle;
       
   297 		}
       
   298 	}
       
   299 
       
   300 // end of file