mobilemessaging/unieditor/model/src/UniDataUtils.cpp
changeset 0 72b543305e3a
equal deleted inserted replaced
-1:000000000000 0:72b543305e3a
       
     1 /*
       
     2 * Copyright (c) 2005 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 *           Data utility class for UniEditor & MMS related editors and viewers
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 
       
    21 // ========== INCLUDE FILES ================================
       
    22 
       
    23 #include <e32std.h>
       
    24 #include <e32base.h>
       
    25 #include <f32file.h>
       
    26 #include <data_caging_path_literals.hrh> 
       
    27 
       
    28 #include <bautils.h>
       
    29 
       
    30 #include <mtclbase.h>
       
    31 #include <msvstore.h>
       
    32 #include <mmsvattachmentmanager.h>
       
    33 #include <cmsvattachment.h>
       
    34 #include <cmsvmimeheaders.h>
       
    35 
       
    36 #include <StringLoader.h>           // for StringLoader (load and foramt strings from resources)
       
    37 #include <CommonContentPolicy.h>
       
    38 #include <MsgAttachmentUtils.h>     // for CMsgAttachmentUtils
       
    39 
       
    40 #include <msgtextutils.h>
       
    41 #include <MsgMimeTypes.h>
       
    42 
       
    43 #include <UniDataModel.rsg>           // Viewer/Editor Mimetype strings
       
    44 
       
    45 #include "UniModelConst.h"
       
    46 #include "UniDataUtils.h"
       
    47 
       
    48 // ========== LOCAL CONSTANTS AND MACROS ===================
       
    49 
       
    50 const TInt KTextBufferMaxSize( 256 );   // Max size for string read from resources. 
       
    51                                         // NOTE: This is 256 unicode chars, too.
       
    52 
       
    53 // ========== MEMBER FUNCTIONS =============================
       
    54 
       
    55 
       
    56 // ---------------------------------------------------------
       
    57 // CUniDataUtils::NewL
       
    58 // ---------------------------------------------------------
       
    59 //
       
    60 EXPORT_C CUniDataUtils* CUniDataUtils::NewL( RFs& aFs )
       
    61     {
       
    62     CUniDataUtils* data = new ( ELeave ) CUniDataUtils( aFs );
       
    63     CleanupStack::PushL( data );
       
    64     data->ConstructL();
       
    65     CleanupStack::Pop( data );
       
    66     return data;
       
    67     }
       
    68 
       
    69 
       
    70 // ---------------------------------------------------------
       
    71 // CUniDataUtils::CUniDataUtils
       
    72 //
       
    73 // Constructor
       
    74 // ---------------------------------------------------------
       
    75 //
       
    76 CUniDataUtils::CUniDataUtils( RFs& aFs )
       
    77     : iFs( aFs )
       
    78     {
       
    79     }
       
    80 
       
    81 
       
    82 // ---------------------------------------------------------
       
    83 // CUniDataUtils::ConstructL
       
    84 // ---------------------------------------------------------
       
    85 //
       
    86 void CUniDataUtils::ConstructL()
       
    87     {
       
    88     RegisterDataL();
       
    89     iTextUtils = CMsgTextUtils::NewL( iFs );
       
    90     }
       
    91 
       
    92 // ---------------------------------------------------------
       
    93 // CUniDataUtils::~CUniDataUtils
       
    94 //
       
    95 // Destructor.
       
    96 // ---------------------------------------------------------
       
    97 //
       
    98 CUniDataUtils::~CUniDataUtils()
       
    99     {
       
   100     delete iDefaultFileName;
       
   101     delete iEmptyPageString;
       
   102     delete iTextUtils;
       
   103     }
       
   104 
       
   105 // ---------------------------------------------------------
       
   106 // CUniDataUtils::CharconvIdToMibIdL
       
   107 // ---------------------------------------------------------
       
   108 //
       
   109 EXPORT_C TUint CUniDataUtils::CharconvIdToMibIdL( TUint aCharconvCharsetId )
       
   110     {
       
   111     return iTextUtils->CharconvIdToMibIdL( aCharconvCharsetId );
       
   112     }
       
   113 
       
   114 // ---------------------------------------------------------
       
   115 // CUniDataUtils::MibIdToCharconvIdL
       
   116 // ---------------------------------------------------------
       
   117 //
       
   118 EXPORT_C TUint CUniDataUtils::MibIdToCharconvIdL( TUint aMibId )
       
   119     {
       
   120     return iTextUtils->MibIdToCharconvIdL( aMibId );
       
   121     }
       
   122 
       
   123 // ---------------------------------------------------------
       
   124 // CUniDataUtils::UTF8Size
       
   125 // ---------------------------------------------------------
       
   126 EXPORT_C TInt CUniDataUtils::UTF8Size( TPtrC aText )
       
   127     {
       
   128     return CMsgTextUtils::UTF8Size( aText );
       
   129     }
       
   130 
       
   131 // ---------------------------------------------------------
       
   132 // CUniDataUtils::RegisterDataL
       
   133 //
       
   134 // Registers data 
       
   135 // ---------------------------------------------------------
       
   136 //
       
   137 void CUniDataUtils::RegisterDataL()
       
   138     {
       
   139     TFileName fileName;
       
   140     TParse parse;
       
   141     parse.Set( KUniDataModelResourceFilename, &KDC_RESOURCE_FILES_DIR, NULL );
       
   142     fileName = parse.FullName();
       
   143     
       
   144     BaflUtils::NearestLanguageFile( iFs, fileName );
       
   145 
       
   146     RResourceFile resourceFile;
       
   147     resourceFile.OpenL( const_cast<RFs&>( iFs ), fileName );
       
   148     resourceFile.ConfirmSignatureL( KDefResFileSignature );
       
   149     CleanupClosePushL( resourceFile );
       
   150 
       
   151     iDefaultFileName = ReadResStringL( resourceFile, R_MMDU_DEFAULT_FILE_NAME ); 
       
   152     iEmptyPageString = ReadResStringL( resourceFile, R_MMDU_EMPTY_SLIDE ); 
       
   153 
       
   154     CleanupStack::PopAndDestroy( &resourceFile );
       
   155     }
       
   156 
       
   157 
       
   158 // ---------------------------------------------------------
       
   159 // CUniDataUtils::ReadResStringL
       
   160 //
       
   161 // Read single string. 
       
   162 // ---------------------------------------------------------
       
   163 //
       
   164 HBufC* CUniDataUtils::ReadResStringL( const RResourceFile& aResourceFile, TInt aSrcResId )
       
   165     {
       
   166     HBufC* readBuf = HBufC::NewLC( KTextBufferMaxSize );
       
   167     TPtr8 readBuffer( (TText8*) readBuf->Ptr(), KTextBufferMaxSize * 2 );
       
   168     aResourceFile.ReadL(readBuffer, (0x00000fff & aSrcResId) ); // Remove offset from id
       
   169     
       
   170     if ( readBuffer.Length() != 0 )
       
   171         {
       
   172         readBuf->Des().SetLength((readBuffer.Length()+1)>>1);
       
   173         }
       
   174         
       
   175     CleanupStack::Pop( readBuf );
       
   176     return readBuf;
       
   177     }    
       
   178 
       
   179 // ---------------------------------------------------------
       
   180 // FileSizeL
       
   181 // ---------------------------------------------------------
       
   182 EXPORT_C TInt CUniDataUtils::FileSizeL( const TFileName& aFileName )
       
   183     {
       
   184     TInt size(0);
       
   185     RFile file;
       
   186     
       
   187     TInt err = file.Open( iFs, aFileName, EFileShareAny );
       
   188     if ( err == KErrAccessDenied || err == KErrInUse )
       
   189         {
       
   190         err = file.Open( iFs, aFileName, EFileShareReadersOnly );
       
   191         }
       
   192     if ( err == KErrNone )
       
   193         {
       
   194         file.Size( size );
       
   195         }
       
   196     else
       
   197         {
       
   198         file.Close();
       
   199         User::Leave( err );
       
   200         }
       
   201 
       
   202     file.Close();
       
   203     return size;
       
   204     }
       
   205 
       
   206 // ---------------------------------------------------------
       
   207 // CUniObject::GetAttachmentFileL
       
   208 // ---------------------------------------------------------
       
   209 //
       
   210 EXPORT_C RFile CUniDataUtils::GetAttachmentFileL( CBaseMtm& aMtm, TMsvAttachmentId aId )
       
   211     {
       
   212     RFile file;
       
   213     CMsvStore* store = aMtm.Entry().ReadStoreL();
       
   214     CleanupStack::PushL( store );
       
   215     MMsvAttachmentManager& manager = store->AttachmentManagerL();
       
   216     file = manager.GetAttachmentFileL( aId );        
       
   217     CleanupStack::PopAndDestroy( store );
       
   218     
       
   219     return file;
       
   220     }
       
   221 
       
   222 // ---------------------------------------------------------
       
   223 // CUniObject::IndexPositionOfAttachmentL
       
   224 // ---------------------------------------------------------
       
   225 //
       
   226 EXPORT_C TInt CUniDataUtils::IndexPositionOfAttachmentL(
       
   227         MMsvAttachmentManager& aManager,
       
   228         TMsvAttachmentId aId )
       
   229 	{    
       
   230     TInt count = aManager.AttachmentCount();
       
   231 	TInt foundPosition = KErrNotFound;
       
   232 
       
   233     for ( TInt i = 0; i < count; i++ )
       
   234         {
       
   235         CMsvAttachment* attachment = aManager.GetAttachmentInfoL( i );
       
   236         TMsvAttachmentId id = attachment->Id();
       
   237         delete attachment;
       
   238         if ( id == aId )
       
   239             {
       
   240             foundPosition = i;
       
   241             break;
       
   242             }
       
   243         }
       
   244         
       
   245 	// Leave with KErrNotFound if the attachment id is not found
       
   246 	User::LeaveIfError( foundPosition );
       
   247 	
       
   248 	// return the index position of attachment
       
   249 	return foundPosition;
       
   250 	}
       
   251 
       
   252 //  End of File