messagingfw/msgcommonutils/src/msgtextutils.cpp
changeset 0 8e480a14352b
child 10 30d6238592e8
equal deleted inserted replaced
-1:000000000000 0:8e480a14352b
       
     1 /*
       
     2 * Copyright (c) 2006-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:  
       
    15 *           Miscellaneous text related utility methods.
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 
       
    21 // ========== INCLUDE FILES ================================
       
    22 
       
    23 #include <e32std.h>
       
    24 #include <e32base.h>
       
    25 #include <f32file.h>
       
    26 #include <s32file.h>
       
    27 
       
    28 #include <charconv.h>
       
    29 #include <txtrich.h>
       
    30 
       
    31 #include <cmsvattachment.h>
       
    32 #include <cmsvmimeheaders.h>
       
    33 #include <mmsvattachmentmanager.h>
       
    34 
       
    35 #include "msgtextutils.h"
       
    36 
       
    37 // ========== LOCAL CONSTANTS AND MACROS ===================
       
    38 
       
    39 /* Reddy - Moved ot header
       
    40 // Default charsets
       
    41 const TUint KDefaultCharConvCharset = KCharacterSetIdentifierAscii;
       
    42 const TUint KDefaultMIBCharset = KCharacterSetMIBEnumUsAscii;
       
    43 */
       
    44 
       
    45 const TInt KMaxSampleLengthForAutoDetection = 512;
       
    46 const TInt KCharsetPluginArrayGranularity = 10;
       
    47 
       
    48 const TUint KCharParagraphSeparator = 0x2029;
       
    49 const TUint KCharLineFeed = 0x0a;
       
    50 const TUint KCharCarriageReturn = 0x0d;
       
    51 const TUint KCharAsciiDot = 0x2e;
       
    52 const TUint KCharAsciiMax = 0x7f;
       
    53 
       
    54 const TInt KMaxFileNameLength = 8;      // must be at least 4!
       
    55 const TInt KMaxFileExtensionLenght = 5; // includes dot
       
    56 const TInt KMaxFileTotalLength = 12;
       
    57 
       
    58 _LIT8( KReplaceChar8, "_" );
       
    59 _LIT16( KReplaceChar16, "_" );
       
    60 
       
    61 // ========== MEMBER FUNCTIONS =============================
       
    62 
       
    63 
       
    64 // ---------------------------------------------------------
       
    65 // CMsgTextUtils::NewL
       
    66 // ---------------------------------------------------------
       
    67 //
       
    68 EXPORT_C CMsgTextUtils* CMsgTextUtils::NewL( RFs& aFs )
       
    69     {
       
    70     CMsgTextUtils* data = new ( ELeave ) CMsgTextUtils( aFs );
       
    71     CleanupStack::PushL( data );
       
    72     data->ConstructL();
       
    73     CleanupStack::Pop();
       
    74     return data;
       
    75     }
       
    76 
       
    77 
       
    78 // ---------------------------------------------------------
       
    79 // CMsgTextUtils::CMsgTextUtils
       
    80 //
       
    81 // Constructor
       
    82 // ---------------------------------------------------------
       
    83 //
       
    84 CMsgTextUtils::CMsgTextUtils( RFs& aFs )
       
    85     : iFs( aFs )
       
    86     {
       
    87     }
       
    88 
       
    89 
       
    90 // ---------------------------------------------------------
       
    91 // CMsgTextUtils::ConstructL
       
    92 // ---------------------------------------------------------
       
    93 //
       
    94 void CMsgTextUtils::ConstructL()
       
    95     {
       
    96     // Nothing to do. Reserved for future use.
       
    97     }
       
    98 
       
    99 // ---------------------------------------------------------
       
   100 // CMsgTextUtils::~CMsgTextUtils
       
   101 //
       
   102 // Destructor.
       
   103 // ---------------------------------------------------------
       
   104 //
       
   105 CMsgTextUtils::~CMsgTextUtils()
       
   106     {
       
   107     delete iCharConv;
       
   108     }
       
   109 
       
   110 // ---------------------------------------------------------
       
   111 // CMsgTextUtils::ConvertToBuffer8LC
       
   112 // 
       
   113 // ---------------------------------------------------------
       
   114 //
       
   115 EXPORT_C HBufC8* CMsgTextUtils::ConvertToBuffer8LC(
       
   116         const TDesC& aText,
       
   117         TUint aCharacterSetIdentifier )
       
   118     {
       
   119     TInt origLength = aText.Length();
       
   120     TInt maxLength = origLength;
       
   121     HBufC8* resultBuffer = HBufC8::NewLC( maxLength ); 
       
   122     if ( !iCharConv )
       
   123         {
       
   124         iCharConv = CCnvCharacterSetConverter::NewL();
       
   125         }
       
   126 
       
   127     if ( iCharConv->PrepareToConvertToOrFromL( aCharacterSetIdentifier, iFs ) !=
       
   128         CCnvCharacterSetConverter::EAvailable )
       
   129         {
       
   130         User::Leave( KErrNotSupported );
       
   131         }
       
   132 
       
   133     iCharConv->SetReplacementForUnconvertibleUnicodeCharactersL( KReplaceChar8 );
       
   134 
       
   135     TBuf8<40> outputBuffer;
       
   136     TPtrC16 remainderOfUnicode( aText );
       
   137 
       
   138     FOREVER
       
   139         {
       
   140         const TInt doneAll = iCharConv->ConvertFromUnicode( outputBuffer, remainderOfUnicode );
       
   141 
       
   142         if ( doneAll == CCnvCharacterSetConverter::EErrorIllFormedInput )
       
   143             {
       
   144             User::Leave( KErrCorrupt );
       
   145             }
       
   146         else if ( doneAll < 0 )
       
   147             {
       
   148             // For future expansion of errors
       
   149             User::Leave( KErrGeneral );
       
   150             }
       
   151         else
       
   152             { // lint
       
   153             }
       
   154 
       
   155         // Make sure outputBuffer fits into resultBuffer
       
   156         while ( resultBuffer->Length() + outputBuffer.Length() > maxLength )
       
   157             {
       
   158             // Increase resultBuffer length by origLength
       
   159             maxLength += origLength;
       
   160             resultBuffer = resultBuffer->ReAllocL( maxLength );
       
   161             CleanupStack::Pop(); // resultBuffer
       
   162             CleanupStack::PushL(resultBuffer);
       
   163             }
       
   164         // Append to result
       
   165         resultBuffer->Des().Append( outputBuffer );
       
   166 
       
   167         if ( doneAll == 0 )
       
   168             {
       
   169             return resultBuffer; // All converted
       
   170             }
       
   171 
       
   172         remainderOfUnicode.Set( remainderOfUnicode.Right( doneAll ));
       
   173         }
       
   174     }       
       
   175 
       
   176 // ---------------------------------------------------------
       
   177 // CMsgTextUtils::ConvertToBuffer16LC
       
   178 // 
       
   179 // ---------------------------------------------------------
       
   180 //
       
   181 EXPORT_C HBufC* CMsgTextUtils::ConvertToBuffer16LC(
       
   182         const TDesC& aText,
       
   183         TUint aCharacterSetIdentifier )
       
   184     {
       
   185     HBufC8* resultBuffer = ConvertToBuffer8LC( aText, aCharacterSetIdentifier );
       
   186 
       
   187     HBufC* finalresult = HBufC::NewL( resultBuffer->Length() );
       
   188     finalresult->Des().Copy( *resultBuffer );
       
   189     CleanupStack::PopAndDestroy(); // resultbuffer
       
   190     CleanupStack::PushL( finalresult );
       
   191         
       
   192     return finalresult;
       
   193     }
       
   194     
       
   195 // ---------------------------------------------------------
       
   196 // CMsgTextUtils::ConvertToFileL
       
   197 // 
       
   198 // ---------------------------------------------------------
       
   199 //
       
   200 EXPORT_C void CMsgTextUtils::ConvertToFileL(
       
   201         const TDesC& aText,
       
   202         RFile& aFile,
       
   203         TUint aCharacterSetIdentifier )
       
   204     {
       
   205     // Takes ownership of "aFile".
       
   206     RFileWriteStream writer( aFile );
       
   207     writer.PushL();
       
   208 
       
   209     if ( !iCharConv )
       
   210         {
       
   211         iCharConv = CCnvCharacterSetConverter::NewL();
       
   212         }
       
   213 
       
   214     if ( iCharConv->PrepareToConvertToOrFromL( aCharacterSetIdentifier, iFs )
       
   215         != CCnvCharacterSetConverter::EAvailable )
       
   216         {
       
   217         User::Leave( KErrNotSupported );
       
   218         }
       
   219 
       
   220     iCharConv->SetReplacementForUnconvertibleUnicodeCharactersL( KReplaceChar8 );
       
   221 
       
   222     TBuf8<128> outputBuffer;
       
   223     TPtrC16 remainderOfUnicodeText( aText );
       
   224     FOREVER
       
   225         {
       
   226         TInt doneAll = iCharConv->ConvertFromUnicode(
       
   227             outputBuffer, remainderOfUnicodeText );
       
   228         if ( doneAll == CCnvCharacterSetConverter::EErrorIllFormedInput )
       
   229             {
       
   230             User::Leave( KErrCorrupt );
       
   231             }
       
   232         else if ( doneAll < 0 ) // future-proof against "TError" expanding
       
   233             {
       
   234             User::Leave( KErrGeneral );
       
   235             }
       
   236         else
       
   237             { // lint
       
   238             }
       
   239 
       
   240         writer.WriteL( outputBuffer );
       
   241             
       
   242         if ( doneAll == 0 )
       
   243             {
       
   244             // All of aText has been converted and handled
       
   245             writer.CommitL();
       
   246             writer.Pop();
       
   247             writer.Close();
       
   248             return;
       
   249             }
       
   250         remainderOfUnicodeText.Set( remainderOfUnicodeText.Right( doneAll ) );
       
   251         }
       
   252     }
       
   253 
       
   254 // ---------------------------------------------------------
       
   255 // CMsgTextUtils::CharconvIdToMibIdL
       
   256 // 
       
   257 // ---------------------------------------------------------
       
   258 //
       
   259 EXPORT_C TUint CMsgTextUtils::CharconvIdToMibIdL( TUint aCharconvCharsetId )
       
   260     {
       
   261     // Switch-case is here for performance optimization
       
   262     TUint charset = 0;
       
   263     switch ( aCharconvCharsetId )
       
   264         {
       
   265         case 0:
       
   266             // Symbian OS native charset
       
   267             charset = KCharacterSetMIBEnumIso10646Ucs2;
       
   268             break;
       
   269         case KCharacterSetIdentifierAscii:
       
   270             charset = KCharacterSetMIBEnumUsAscii;
       
   271             break;
       
   272         case KCharacterSetIdentifierUtf8:
       
   273             charset = KCharacterSetMIBEnumUtf8;
       
   274             break;
       
   275         case KCharacterSetIdentifierIso88591:
       
   276             charset = KCharacterSetMIBEnumISO_8859_1;
       
   277             break;
       
   278         case KCharacterSetIdentifierIso88592:
       
   279             charset = KCharacterSetMIBEnumISO_8859_2;
       
   280             break;
       
   281         case KCharacterSetIdentifierIso88593:
       
   282             charset = KCharacterSetMIBEnumISO_8859_3;
       
   283             break;
       
   284         case KCharacterSetIdentifierIso88594:
       
   285             charset = KCharacterSetMIBEnumISO_8859_4;
       
   286             break;
       
   287         case KCharacterSetIdentifierIso88595:
       
   288             charset = KCharacterSetMIBEnumISO_8859_5;
       
   289             break;
       
   290         case KCharacterSetIdentifierIso88596:
       
   291             charset = KCharacterSetMIBEnumISO_8859_6;
       
   292             break;
       
   293         case KCharacterSetIdentifierIso88597:
       
   294             charset = KCharacterSetMIBEnumISO_8859_7;
       
   295             break;
       
   296         case KCharacterSetIdentifierIso88598:
       
   297             charset = KCharacterSetMIBEnumISO_8859_8;
       
   298             break;
       
   299         case KCharacterSetIdentifierIso88599:
       
   300             charset = KCharacterSetMIBEnumISO_8859_9;
       
   301             break;
       
   302         case KCharacterSetIdentifierIso885910:
       
   303             charset = KCharacterSetMIBEnumISO_8859_10;
       
   304             break;
       
   305         case KCharacterSetIdentifierIso885913:
       
   306             charset = KCharacterSetMIBEnumISO_8859_13;
       
   307             break;
       
   308         case KCharacterSetIdentifierIso885914:
       
   309             charset = KCharacterSetMIBEnumISO_8859_14;
       
   310             break;
       
   311         case KCharacterSetIdentifierIso885915:
       
   312             charset = KCharacterSetMIBEnumISO_8859_15;
       
   313             break;
       
   314         case KCharacterSetIdentifierUtf7:
       
   315             charset = KCharacterSetMIBEnumUtf7;
       
   316             break;
       
   317         case KCharacterSetIdentifierCodePage1252:
       
   318             charset = KCharacterSetMIBEnumCodePage1252;
       
   319             break;
       
   320         // Chinese charsets
       
   321         case KCharacterSetIdentifierGb2312:
       
   322             charset = KCharacterSetMIBEnumGb2312;
       
   323             break;
       
   324         case KCharacterSetIdentifierHz:
       
   325             charset = KCharacterSetMIBEnumHz;
       
   326             break;
       
   327         case KCharacterSetIdentifierGbk:
       
   328             charset = KCharacterSetMIBEnumGbk;
       
   329             break;
       
   330         case KCharacterSetIdentifierBig5:
       
   331             charset = KCharacterSetMIBEnumBig5;
       
   332             break;
       
   333         // Japanese charsets
       
   334         case KCharacterSetIdentifierShiftJis:
       
   335             charset = KCharacterSetMIBEnumShiftJis;
       
   336             break;
       
   337         case KCharacterSetIdentifierIso2022Jp:
       
   338             charset = KCharacterSetMIBEnumIso2022Jp;
       
   339             break;
       
   340         case KCharacterSetIdentifierJis:
       
   341             charset = KCharacterSetMIBEnumJis;
       
   342             break;
       
   343         case KCharacterSetIdentifierEucJpPacked:
       
   344             charset = KCharacterSetMIBEnumEucJpPacked;
       
   345             break;
       
   346         default:
       
   347             {
       
   348             if ( !iCharConv )
       
   349                 {
       
   350                 iCharConv = CCnvCharacterSetConverter::NewL();
       
   351                 }
       
   352             charset = iCharConv->ConvertCharacterSetIdentifierToMibEnumL(
       
   353                 aCharconvCharsetId, iFs );
       
   354             if ( charset == 0 )
       
   355                 {
       
   356                 charset = KDefaultMIBCharset;
       
   357                 }
       
   358             break;
       
   359             }
       
   360         }
       
   361     return charset;
       
   362     }
       
   363 
       
   364 // ---------------------------------------------------------
       
   365 // CMsgTextUtils::MibIdToCharconvIdL
       
   366 // 
       
   367 // ---------------------------------------------------------
       
   368 //
       
   369 EXPORT_C TUint CMsgTextUtils::MibIdToCharconvIdL( TUint aMibId )
       
   370     {
       
   371     // Switch-case is here for performance optimization
       
   372     TUint charset = 0;
       
   373     switch ( aMibId )
       
   374         {
       
   375         case KCharacterSetMIBEnumIso10646Ucs2:
       
   376         case KCharacterSetMIBEnumUTF16:
       
   377             //no conversion for Unicode
       
   378             charset = 0;
       
   379             break;
       
   380         case KCharacterSetMIBEnumUTF16BE:
       
   381             charset = KCharacterSetIdentifierUnicodeBig; 
       
   382             break;
       
   383         case KCharacterSetMIBEnumUTF16LE:
       
   384             charset = KCharacterSetIdentifierUnicodeLittle;  
       
   385             break;
       
   386         case KCharacterSetMIBEnumUsAscii:
       
   387             charset = KCharacterSetIdentifierAscii;
       
   388             break;
       
   389         case KCharacterSetMIBEnumUtf8:
       
   390             charset = KCharacterSetIdentifierUtf8;
       
   391             break;
       
   392         case KCharacterSetMIBEnumISO_8859_1:
       
   393             charset = KCharacterSetIdentifierIso88591;
       
   394             break;
       
   395         case KCharacterSetMIBEnumISO_8859_2:
       
   396             charset = KCharacterSetIdentifierIso88592;
       
   397             break;
       
   398         case KCharacterSetMIBEnumISO_8859_3:
       
   399             charset = KCharacterSetIdentifierIso88593;
       
   400             break;
       
   401         case KCharacterSetMIBEnumISO_8859_4:
       
   402             charset = KCharacterSetIdentifierIso88594;
       
   403             break;
       
   404         case KCharacterSetMIBEnumISO_8859_5:
       
   405             charset = KCharacterSetIdentifierIso88595;
       
   406             break;
       
   407         case KCharacterSetMIBEnumISO_8859_6:
       
   408             charset = KCharacterSetIdentifierIso88596;
       
   409             break;
       
   410         case KCharacterSetMIBEnumISO_8859_7:
       
   411             charset = KCharacterSetIdentifierIso88597;
       
   412             break;
       
   413         case KCharacterSetMIBEnumISO_8859_8:
       
   414             charset = KCharacterSetIdentifierIso88598;
       
   415             break;
       
   416         case KCharacterSetMIBEnumISO_8859_9:
       
   417             charset = KCharacterSetIdentifierIso88599;
       
   418             break;
       
   419         case KCharacterSetMIBEnumISO_8859_10:
       
   420             charset = KCharacterSetIdentifierIso885910;
       
   421             break;
       
   422         case KCharacterSetMIBEnumISO_8859_13:
       
   423             charset = KCharacterSetIdentifierIso885913;
       
   424             break;
       
   425         case KCharacterSetMIBEnumISO_8859_14:
       
   426             charset = KCharacterSetIdentifierIso885914;
       
   427             break;
       
   428         case KCharacterSetMIBEnumISO_8859_15:
       
   429             charset = KCharacterSetIdentifierIso885915;
       
   430             break;
       
   431         case KCharacterSetMIBEnumUtf7:
       
   432             charset = KCharacterSetIdentifierUtf7;
       
   433             break;
       
   434         case KCharacterSetMIBEnumCodePage1252:
       
   435             charset = KCharacterSetIdentifierCodePage1252;
       
   436             break;
       
   437         // Chinese charsets
       
   438         case KCharacterSetMIBEnumGb2312:
       
   439             charset = KCharacterSetIdentifierGb2312;
       
   440             break;
       
   441         case KCharacterSetMIBEnumHz:
       
   442             charset = KCharacterSetIdentifierHz;
       
   443             break;
       
   444         case KCharacterSetMIBEnumGbk:
       
   445             charset = KCharacterSetIdentifierGbk;
       
   446             break;
       
   447         case KCharacterSetMIBEnumBig5:
       
   448             charset = KCharacterSetIdentifierBig5;
       
   449             break;
       
   450         // Japanese charsets
       
   451         case KCharacterSetMIBEnumShiftJis:
       
   452             charset = KCharacterSetIdentifierShiftJis;
       
   453             break;
       
   454         case KCharacterSetMIBEnumIso2022Jp:
       
   455             charset = KCharacterSetIdentifierIso2022Jp;
       
   456             break;
       
   457         case KCharacterSetMIBEnumJis:
       
   458             charset = KCharacterSetIdentifierJis;
       
   459             break;
       
   460         case KCharacterSetMIBEnumEucJpPacked:
       
   461             charset = KCharacterSetIdentifierEucJpPacked;
       
   462             break;
       
   463         default:
       
   464             {
       
   465             if ( !iCharConv )
       
   466                 {
       
   467                 iCharConv = CCnvCharacterSetConverter::NewL();
       
   468                 }
       
   469             charset = iCharConv->ConvertMibEnumOfCharacterSetToIdentifierL( aMibId, iFs );
       
   470             if ( charset == 0 )
       
   471                 {
       
   472                 charset = KDefaultCharConvCharset;
       
   473                 }
       
   474             break;
       
   475             }
       
   476         }
       
   477     return charset;
       
   478     }
       
   479 
       
   480 // ---------------------------------------------------------
       
   481 // ConvertParagraphSeparatorsLC
       
   482 // ---------------------------------------------------------
       
   483 //
       
   484 EXPORT_C HBufC* CMsgTextUtils::ConvertParagraphSeparatorsLC( const TDesC& aText )
       
   485     {
       
   486     TInt position;
       
   487     TPtrC ptr;
       
   488 
       
   489     TInt numberOfSeparators = 0;
       
   490 
       
   491     position = 0;
       
   492     ptr.Set( aText.Mid( 0 ) ); 
       
   493     while ( position != KErrNotFound && position < aText.Length() )
       
   494         {
       
   495         ptr.Set( ptr.Mid( position ) ); 
       
   496         position = ptr.Locate( TChar( KCharParagraphSeparator ) );
       
   497         if ( position != KErrNotFound )
       
   498             {
       
   499             numberOfSeparators++;
       
   500             position++; // point past separator just found
       
   501             }
       
   502         }
       
   503 
       
   504     HBufC* convertedText = HBufC::NewLC( aText.Length() + numberOfSeparators );
       
   505     ptr.Set( aText.Mid( 0 ) );
       
   506     TInt start = 0;
       
   507     position = aText.Locate( TChar( KCharParagraphSeparator ) );
       
   508     while ( position != KErrNotFound && start < aText.Length() )
       
   509         {
       
   510         ptr.Set( aText.Mid( start ) ); 
       
   511         position = ptr.Locate( TChar( KCharParagraphSeparator ) );
       
   512         if ( position != KErrNotFound )
       
   513             {
       
   514             convertedText->Des().Append( ptr.Left( position ) );
       
   515             convertedText->Des().Append( TChar( KCharCarriageReturn ) );
       
   516             convertedText->Des().Append( TChar( KCharLineFeed ) );
       
   517             start = start + position + 1; // point past separator
       
   518             }
       
   519         }
       
   520     // append what is left after last separator has been found
       
   521     if ( start < aText.Length() )
       
   522         {
       
   523         convertedText->Des().Append( aText.Mid( start ) );
       
   524         }
       
   525 
       
   526     return convertedText;
       
   527     }
       
   528 
       
   529 // ---------------------------------------------------------
       
   530 // CMsgTextUtils::TrimAndRemoveNonAlphaDigit
       
   531 // 
       
   532 // NOTE: This is intended for small strings. With long 
       
   533 // strings if could be more efficient to to seek replacable
       
   534 // chars than to loop thru every char.
       
   535 // ---------------------------------------------------------
       
   536 //
       
   537 EXPORT_C void CMsgTextUtils::TrimAndRemoveNonAlphaDigit( TDes& aString )
       
   538     {
       
   539     aString.Trim();
       
   540     for (TInt i = aString.Length(); --i >= 0 ;)
       
   541         {
       
   542         TChar c = (TChar) aString[i];
       
   543         // Allow dots i.e. "." because they are allowed in URI and
       
   544         // because filename are created from content-location
       
   545         // so this will result to "filename.txt" instead of "filename_txt"
       
   546         if ( aString[i] > KCharAsciiMax ||
       
   547             ( aString[i] != KCharAsciiDot && !c.IsAlphaDigit() ) )
       
   548             {
       
   549             aString.Replace( i, 1, KReplaceChar16 );
       
   550             }
       
   551         }
       
   552     }
       
   553 
       
   554 // ---------------------------------------------------------
       
   555 // UTF8Size
       
   556 // ---------------------------------------------------------
       
   557 //
       
   558 EXPORT_C TInt CMsgTextUtils::UTF8Size( TPtrC aText )
       
   559     {
       
   560     TInt count = 0;
       
   561     TInt sizeInBytes = 0;
       
   562     TUint16 charValue;
       
   563     while ( count < aText.Length() )
       
   564         {
       
   565         charValue = aText[count];
       
   566         if ( charValue < 0x80 )
       
   567             {
       
   568             sizeInBytes += 1;
       
   569             }
       
   570         else if ( charValue < 0x800 )
       
   571             {
       
   572             sizeInBytes += 2;
       
   573             }
       
   574         else //if ( charValue < 0x10000 )
       
   575             {
       
   576             sizeInBytes += 3;
       
   577             }
       
   578         count++;
       
   579         }
       
   580     return sizeInBytes;
       
   581     }
       
   582 
       
   583 // ---------------------------------------------------------
       
   584 // ConvertLineBreaksL
       
   585 // ---------------------------------------------------------
       
   586 //
       
   587 EXPORT_C void CMsgTextUtils::ConvertLineBreaksL( CRichText& aText, TInt aMode )
       
   588     {
       
   589     if ( aMode & ECRLFtoLF )
       
   590         {
       
   591         DoConvertCRLFL( aText );
       
   592         }
       
   593     }
       
   594 
       
   595 // ---------------------------------------------------------
       
   596 // DoConvertCRLFL
       
   597 // ---------------------------------------------------------
       
   598 //
       
   599 void CMsgTextUtils::DoConvertCRLFL( CRichText& aText )
       
   600     {
       
   601     TInt i( 0 );
       
   602     TInt documentLength( aText.DocumentLength() );
       
   603     
       
   604     while ( i < documentLength )
       
   605         {
       
   606         if ( aText.Read( i, 1 ).Locate( KCharCarriageReturn ) != KErrNotFound )
       
   607             {
       
   608             if ( i < documentLength - 1 &&
       
   609                  aText.Read( i + 1, 1 ).Locate( KCharLineFeed ) != KErrNotFound )
       
   610                 {
       
   611                 // "CR+LF" -> "LF"
       
   612                 aText.DeleteL( i, 1 );
       
   613                 documentLength--;
       
   614                 }
       
   615             }
       
   616         i++;
       
   617         }
       
   618     }
       
   619 
       
   620 // -----------------------------------------------------------------------------
       
   621 // RecognizeCharSetL
       
   622 // -----------------------------------------------------------------------------
       
   623 //
       
   624 EXPORT_C TUint CMsgTextUtils::RecognizeCharSetL( RFs& aFs, RFile& aFile )
       
   625     {
       
   626     TUint charSet( 0 );
       
   627     
       
   628     HBufC8* sample = HBufC8::NewLC( KMaxSampleLengthForAutoDetection );
       
   629 	TPtr8 sampleDes = sample->Des();
       
   630     
       
   631     TInt fileSize( 0 );
       
   632     User::LeaveIfError( aFile.Size( fileSize ) );
       
   633     User::LeaveIfError( aFile.Read( 0, sampleDes, fileSize > sampleDes.MaxLength()
       
   634         ? sampleDes.MaxLength() 
       
   635         : fileSize ) );
       
   636 
       
   637 
       
   638     // Check for Byte Order Mark (BOM)
       
   639     // U+FEFF ZERO WIDTH NON-BREAKING SPACE
       
   640     //
       
   641     // (from www.unicode.org FAQ)
       
   642     // - FE FF -> UTF-16, big-endian 
       
   643     // - FF FE -> UTF-16, little-endian 
       
   644     //
       
   645     if ( !sampleDes.Length() || 
       
   646         ( sampleDes.Size() >= 2 && 
       
   647         ( ( sampleDes[0] == 0xfe && sampleDes[1] == 0xff ) ||
       
   648         ( sampleDes[0] == 0xff && sampleDes[1] == 0xfe ) ) ) )
       
   649         {
       
   650         // Zero means no conversion
       
   651         charSet = 0;
       
   652         }
       
   653     else
       
   654         {
       
   655         const CArrayFix<CCnvCharacterSetConverter::SCharacterSet>* availableSets = 
       
   656             CCnvCharacterSetConverter::CreateArrayOfCharacterSetsAvailableLC( aFs );
       
   657 
       
   658         CArrayFix<CCnvCharacterSetConverter::SCharacterSet>* toDetectSets =
       
   659             new ( ELeave ) CArrayFixFlat<CCnvCharacterSetConverter::SCharacterSet>( KCharsetPluginArrayGranularity );
       
   660         CleanupStack::PushL( toDetectSets );
       
   661 
       
   662         const TInt count( availableSets->Count() );
       
   663         for ( TInt i( 0 ); i < count; i++ )
       
   664             {
       
   665             switch ( availableSets->At( i ).Identifier() )
       
   666                 {
       
   667                 case KCharacterSetIdentifierSms7Bit:
       
   668                     // remove Sms7Bit from charset list
       
   669                     break;
       
   670                 default:
       
   671                     toDetectSets->AppendL( availableSets->At( i ) );
       
   672                     break;
       
   673                 }
       
   674             }
       
   675 
       
   676         TUint resultCharset( 0 );
       
   677         TInt confidence( 0 );
       
   678         CCnvCharacterSetConverter::AutoDetectCharacterSetL(
       
   679             confidence, resultCharset, *toDetectSets, sampleDes );
       
   680         charSet = resultCharset;
       
   681         
       
   682         CleanupStack::PopAndDestroy( 2 ); // availableSets, toDetectSets
       
   683         }
       
   684     CleanupStack::PopAndDestroy( sample );
       
   685     TInt seekPos(0);
       
   686     aFile.Seek( ESeekStart, seekPos );
       
   687     return charSet;
       
   688     }
       
   689 
       
   690 // ---------------------------------------------------------
       
   691 // GetSafeAttachmentNameLC
       
   692 // ---------------------------------------------------------
       
   693 //
       
   694 EXPORT_C HBufC* CMsgTextUtils::GetSafeAttachmentNameLC(
       
   695         MMsvAttachmentManager& aManager,
       
   696         const TDesC& aFileName,
       
   697         TMsvAttachmentId aAttachmentId,
       
   698         TBool aContentLocation )
       
   699     {
       
   700     TBuf<KMaxFileNameLength> name;
       
   701     TBuf<KMaxFileExtensionLenght> ext;
       
   702     TBuf<KMaxFileTotalLength> candidate;
       
   703     TParsePtrC parse( aFileName );
       
   704 
       
   705     ext.Copy( parse.Ext().Left( KMaxFileExtensionLenght ) );
       
   706     CMsgTextUtils::TrimAndRemoveNonAlphaDigit( ext );
       
   707 
       
   708     TInt nameLen = Min( KMaxFileNameLength,
       
   709         ( KMaxFileTotalLength - ext.Length() ) );
       
   710     name.Copy( parse.Name().Left( nameLen ) );    
       
   711     CMsgTextUtils::TrimAndRemoveNonAlphaDigit( name );
       
   712 
       
   713     candidate.Copy( name );
       
   714     candidate.Append( ext );
       
   715 
       
   716     TBool safeFound = EFalse;
       
   717     TInt count = aManager.AttachmentCount();
       
   718     TInt i = 0;
       
   719     while ( !safeFound && i < count )
       
   720         {
       
   721         safeFound = ETrue;
       
   722         for ( TInt ii = 0; ii < count && safeFound; ii++ )
       
   723             {
       
   724             CMsvAttachment* attachment = aManager.GetAttachmentInfoL( ii );
       
   725             CleanupStack::PushL( attachment );
       
   726             if ( aContentLocation )
       
   727                 {
       
   728                 CMsvMimeHeaders* msvMime = CMsvMimeHeaders::NewLC();
       
   729                 msvMime->RestoreL( *attachment );            
       
   730                 if ( attachment->Id() != aAttachmentId &&
       
   731                     !candidate.CompareF( msvMime->ContentLocation() ) )
       
   732                     {
       
   733                     safeFound = EFalse;
       
   734                     }
       
   735                 CleanupStack::PopAndDestroy( msvMime );
       
   736                 }
       
   737             else
       
   738                 {
       
   739                 if ( attachment->Id() != aAttachmentId &&
       
   740                     !candidate.CompareF( attachment->AttachmentName() ) )
       
   741                     {
       
   742                     safeFound = EFalse;
       
   743                     }
       
   744                 }
       
   745             CleanupStack::PopAndDestroy( attachment );
       
   746             }
       
   747         i++;
       
   748         if ( !safeFound )
       
   749             {
       
   750             name.Zero();
       
   751             name.Copy( parse.Name().Left( nameLen - 3 ) );
       
   752             CMsgTextUtils::TrimAndRemoveNonAlphaDigit( name );
       
   753             TBuf<3> num;
       
   754             num.NumFixedWidth( i, EDecimal, 3 );
       
   755             name.Append( num );
       
   756             candidate.Zero();
       
   757             candidate.Copy( name );
       
   758             candidate.Append( ext );
       
   759             }
       
   760         }
       
   761 
       
   762     // It is guaranteed that we always find a safe candidate:
       
   763     // - either safeFound == ETrue, or
       
   764     // - we've got "count + 1st" candidate while there are
       
   765     //   "count" existing attachments.
       
   766     HBufC* safeFileName = candidate.AllocLC();
       
   767     return safeFileName;
       
   768     }
       
   769 
       
   770 // ---------------------------------------------------------
       
   771 // from: TBool CImRecvConvert::IsIllegalChar(const TUint aChar)
       
   772 // ---------------------------------------------------------
       
   773 //
       
   774 LOCAL_C TBool IsIllegalChar(const TUint aChar)
       
   775     {
       
   776     return (
       
   777         aChar == '*'  ||
       
   778         aChar == '\\' ||
       
   779         aChar == '<'  ||
       
   780         aChar == '>'  ||
       
   781         aChar == ':'  ||
       
   782         aChar == '.'  ||
       
   783         aChar == '"'  ||
       
   784         aChar == '/'  ||
       
   785         aChar == '|'  ||
       
   786         aChar == '?'  ||
       
   787         aChar == CEditableText::EParagraphDelimiter  ||
       
   788         aChar == CEditableText::ELineBreak  ||
       
   789         aChar <  ' ' );
       
   790     }
       
   791 
       
   792 // ---------------------------------------------------------
       
   793 // GetFileNameFromBuffer
       
   794 // ---------------------------------------------------------
       
   795 //
       
   796 EXPORT_C void CMsgTextUtils::GetFileNameFromBuffer(
       
   797     TFileName& aFileName,
       
   798     const TDesC& aBuffer,
       
   799     TInt aMaxLength,
       
   800     const TDesC* aExt /*= NULL*/ )
       
   801     {
       
   802     if ( aExt != NULL )
       
   803         {
       
   804         aMaxLength -= aExt->Length();
       
   805         }
       
   806 
       
   807     TInt len = aBuffer.Length();
       
   808     TInt max = Min( len, aMaxLength );
       
   809 
       
   810     //__ASSERT_DEBUG( max > 0, Panic( EMsgZeroLength ) );
       
   811 
       
   812     aFileName.Zero();
       
   813 
       
   814     TInt cc = 0;
       
   815     TUint ch;
       
   816     TUint ch1 = 0;
       
   817     TBool spaces = EFalse;
       
   818     for ( TInt i = 0; i < len && cc < max; i++ )
       
   819         {
       
   820         ch = aBuffer[i];
       
   821 
       
   822         // ignore spaces from beginning of the buffer until first
       
   823         // non-space is encountered.
       
   824         if ( !spaces && ch != ' ' )
       
   825             {
       
   826             spaces = ETrue;
       
   827             }
       
   828 
       
   829         if ( i > 0 )
       
   830             {
       
   831             ch1 = aBuffer[i - 1];
       
   832             }
       
   833 
       
   834         // strip illegal chars away.
       
   835         // checks also if previous and current chars are '.'
       
   836         if ( spaces && ! IsIllegalChar( ch ) )
       
   837             {
       
   838             if ( !( i > 0 && ch == '.' && ch1 == '.' ) )
       
   839                 {
       
   840                 aFileName.Append( ch );
       
   841                 cc++;
       
   842                 }
       
   843             }
       
   844         }
       
   845 
       
   846     aFileName.Trim();
       
   847 
       
   848     // If filename is empty at this point, do not append extension either.
       
   849     // Instead, empty filename is returned so that caller can use whatever
       
   850     // default s/he has for it.
       
   851     if ( aFileName.Length() > 0 && aExt != NULL )
       
   852         {
       
   853         aFileName.Append( *aExt );
       
   854         }
       
   855     }
       
   856 
       
   857 // ---------------------------------------------------------
       
   858 // CMsgTextUtils::ConvertToUnicodeL
       
   859 // Converts input 8-bit data buffer (in given foreign charset type) to unicode buffer.
       
   860 // ---------------------------------------------------------
       
   861 EXPORT_C HBufC16* CMsgTextUtils::ConvertToUnicodeL( const TDesC8& aText, TUint aCharacterSetIdentifier )
       
   862     {
       
   863     TInt origLength = aText.Length();
       
   864     TInt maxLength = origLength;
       
   865 
       
   866     if ( !iCharConv )
       
   867         {
       
   868         iCharConv = CCnvCharacterSetConverter::NewL();
       
   869         }
       
   870     
       
   871     //buffer to hold target data and return to caller
       
   872     HBufC* resultBuffer = HBufC::NewL( maxLength );
       
   873     _LIT8( KReplaceChar8, "_" );
       
   874     
       
   875     if ( iCharConv->PrepareToConvertToOrFromL( aCharacterSetIdentifier, iFs ) !=
       
   876         CCnvCharacterSetConverter::EAvailable )
       
   877         {
       
   878         User::Leave( KErrNotSupported );
       
   879         }
       
   880 
       
   881     //TODO:: need to set endianess if required.
       
   882     //TODO:: below line is not necessary i guess???
       
   883     //The missing character is simply replaced by the Unicode character which represents unknown characters (0xFFFD)
       
   884     //iCharConv->SetReplacementForUnconvertibleUnicodeCharactersL( KReplaceChar8 );
       
   885 
       
   886     TBuf16<40> outputBuffer;
       
   887     TPtrC8 remainderOfinputBuf ( aText );
       
   888     //TODO: KStateDefault header include
       
   889     TInt aState = 0; // KStateDefault;
       
   890 
       
   891     FOREVER
       
   892         {
       
   893         const TInt doneAll = iCharConv->ConvertToUnicode( outputBuffer, remainderOfinputBuf, aState );
       
   894 
       
   895         if ( doneAll == CCnvCharacterSetConverter::EErrorIllFormedInput )
       
   896             {
       
   897             User::Leave( KErrCorrupt );
       
   898             }
       
   899         else if ( doneAll < 0 )
       
   900             {
       
   901             // For future expansion of errors
       
   902             User::Leave( KErrGeneral );
       
   903             }
       
   904         else
       
   905             { // lint
       
   906             }
       
   907 
       
   908         // Make sure outputBuffer fits into resultBuffer
       
   909         while ( resultBuffer->Length() + outputBuffer.Length() > maxLength )
       
   910             {
       
   911             // Increase resultBuffer length by origLength
       
   912             maxLength += origLength;
       
   913             resultBuffer = resultBuffer->ReAllocL( maxLength );
       
   914             }
       
   915         // Append to result
       
   916         resultBuffer->Des().Append( outputBuffer );
       
   917 
       
   918         if ( doneAll == 0 )
       
   919             {                       
       
   920             /* Testing */
       
   921             /* start --- 
       
   922             resultBuffer = resultBuffer->ReAllocL( maxLength + 20);
       
   923             resultBuffer->Des().Append(_L("Korean Enc:415-5434"));
       
   924              ---- End */            
       
   925             return resultBuffer; // All converted
       
   926             }
       
   927         //TODO:: set the buffer correctly w.r.t length, Remember this is HBUFC16
       
   928         remainderOfinputBuf.Set( remainderOfinputBuf.Right( doneAll ));
       
   929         }
       
   930     }
       
   931 
       
   932 // ---------------------------------------------------------
       
   933 // CMsgTextUtils::ConvertPtrToDesC16
       
   934 // Converts input 8-bit data buffer to 16-bit data buffer.
       
   935 // ---------------------------------------------------------
       
   936 EXPORT_C void CMsgTextUtils::ConvertPtrToDesC16( const TDes8& aFromBuff8, TDes16& aToBuff16 )
       
   937     {
       
   938     TInt i = 0;
       
   939     TInt j = 0;
       
   940 
       
   941     aToBuff16.FillZ( aToBuff16.MaxSize()/2 ); //MaxSize returns in bytes, hence devide by 2
       
   942     while( j < (aFromBuff8.Length() - 1) ) 
       
   943         {
       
   944         aToBuff16[i] = aFromBuff8[j++] & 0xff; // low byte
       
   945         aToBuff16[i++] |= (aFromBuff8[j++] << 8); // high byte
       
   946         }
       
   947     aToBuff16.SetLength(i);
       
   948     }
       
   949 
       
   950 //  End of File