phonebookui/Phonebook2/USIMExtension/src/CPsu2CharConv.cpp
changeset 0 e686773b3f54
equal deleted inserted replaced
-1:000000000000 0:e686773b3f54
       
     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:  A class for converts characters into SMS 7 bit format and
       
    15 *                decides if the data is too long.
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 #include "CPsu2CharConv.h"
       
    21 
       
    22 // System includes
       
    23 
       
    24 // Debug headers
       
    25 #include <Pbk2Debug.h>
       
    26 
       
    27 /// Unnamed namespace for local definitions
       
    28 namespace {
       
    29 
       
    30 const TInt KMaxSizeOfConvertedChar = 5;
       
    31 const TInt KReductionSize = 3;
       
    32 const TInt KOneCharacter = 1;
       
    33 const TInt KTwoBytes = 2;
       
    34 _LIT( KAtSign, "@" );
       
    35 const TInt KLineSeparator( 0x2028 );
       
    36 const TInt KParagraphSeparator( 0x2029 );
       
    37 
       
    38 } // namespace
       
    39 
       
    40 // -----------------------------------------------------------------------------
       
    41 // CPsu2CharConv::CPsu2CharConv
       
    42 // C++ default constructor can NOT contain any code, that
       
    43 // might leave.
       
    44 // -----------------------------------------------------------------------------
       
    45 //
       
    46 CPsu2CharConv::CPsu2CharConv( RFs& aFs, const TUint aCharacterSet ) :
       
    47     iFs( aFs ),
       
    48     iCharacterSetIdentifier( aCharacterSet )
       
    49     {
       
    50     }
       
    51 
       
    52 // -----------------------------------------------------------------------------
       
    53 // CPsu2CharConv::ConstructL
       
    54 // Symbian 2nd phase constructor can leave.
       
    55 // -----------------------------------------------------------------------------
       
    56 //
       
    57 void CPsu2CharConv::ConstructL()
       
    58     {
       
    59     iConverter = CCnvCharacterSetConverter::NewL();
       
    60     iConverter->PrepareToConvertToOrFromL(
       
    61         iCharacterSetIdentifier,
       
    62         iFs );
       
    63     }
       
    64 
       
    65 void CPsu2CharConv::SetDownGradeLf( CCnvCharacterSetConverter::
       
    66         TDowngradeForExoticLineTerminatingCharacters aDowngrade )
       
    67     {
       
    68     iConverter->SetDowngradeForExoticLineTerminatingCharacters( aDowngrade );
       
    69     }
       
    70 
       
    71 // -----------------------------------------------------------------------------
       
    72 // CPsu2CharConv::NewL
       
    73 // Two-phased constructor.
       
    74 // -----------------------------------------------------------------------------
       
    75 //
       
    76 CPsu2CharConv* CPsu2CharConv::NewL( RFs& aFs, const TUint aChacterSet )
       
    77     {
       
    78     CPsu2CharConv* self = NewLC( aFs, aChacterSet );
       
    79     CleanupStack::Pop( self );
       
    80     return self;
       
    81     }
       
    82 
       
    83 // -----------------------------------------------------------------------------
       
    84 // CPsu2CharConv::NewLC
       
    85 // Two-phased constructor.
       
    86 // -----------------------------------------------------------------------------
       
    87 //
       
    88 CPsu2CharConv* CPsu2CharConv::NewLC( RFs& aFs, const TUint aChacterSet )
       
    89     {
       
    90     CPsu2CharConv* self = new( ELeave ) CPsu2CharConv( aFs, aChacterSet );
       
    91     CleanupStack::PushL( self );
       
    92     self->ConstructL();
       
    93     return self;
       
    94     }
       
    95 
       
    96 // -----------------------------------------------------------------------------
       
    97 // ~CPsu2CharConv
       
    98 // -----------------------------------------------------------------------------
       
    99 //
       
   100 CPsu2CharConv::~CPsu2CharConv()
       
   101     {
       
   102     delete iConverter;
       
   103     }
       
   104 
       
   105 // -----------------------------------------------------------------------------
       
   106 // CPsu2CharConv::CheckFieldValidityL
       
   107 // -----------------------------------------------------------------------------
       
   108 //
       
   109 TPtrC CPsu2CharConv::CheckFieldValidityL(
       
   110         const TDesC& aData, 
       
   111         TInt aMaxLength,
       
   112         TPsu2FieldValidity& aValidity )
       
   113     {
       
   114     TPtrC result( aData );
       
   115     aValidity = EOk;
       
   116 
       
   117     TBuf8<KMaxSizeOfConvertedChar> convBuf;
       
   118     TInt unConv = 0;
       
   119     TInt simLength = 0;
       
   120     TInt maxUiLength = 0;
       
   121     TInt uiLength = aData.Length();
       
   122     TBool hasConvertedChar = EFalse;
       
   123 
       
   124     // Make the conversion one character at a time
       
   125     for ( TInt i = 0; i < uiLength; ++i )
       
   126         {
       
   127         convBuf.Zero();
       
   128         TPtrC ch( aData.Mid( i, KOneCharacter ) );
       
   129         User::LeaveIfError( 
       
   130             iConverter->ConvertFromUnicode( convBuf, ch, unConv ) );
       
   131 
       
   132         if ( unConv > 0 )
       
   133             {
       
   134             // If the character can't be converted it's unicode,
       
   135             // in that case it's assumed that all characters take two bytes as
       
   136             // in UCS2 coding scheme 1
       
   137             simLength = uiLength * KTwoBytes;
       
   138             maxUiLength = aMaxLength / KTwoBytes;
       
   139             }
       
   140         else
       
   141             {
       
   142             // Character was converted. If the converted character
       
   143             // differs from original, it requires more space from SIM.
       
   144             // Make the reduction only once, since it appears not matter
       
   145             // how many funny characters there are.
       
   146             if ( !hasConvertedChar )
       
   147                 {
       
   148                 TBuf16<KMaxSizeOfConvertedChar> convBuf16;
       
   149                 convBuf16.Copy( convBuf );
       
   150                 if ( ch.Compare( convBuf16 ) != 0 && ch.Compare( KAtSign ) != 0 )
       
   151                     {
       
   152                     // SIM max.length is decreased once by 3 chars, if special
       
   153                     // char is other than @. This kind of behaviour seems to
       
   154                     // be in all SIMs with different name field sizes.
       
   155                     aMaxLength -= KReductionSize;
       
   156                     hasConvertedChar = ETrue;
       
   157                     }
       
   158                 }
       
   159             simLength += convBuf.Length();
       
   160             maxUiLength = i;
       
   161             }
       
   162 
       
   163         if ( simLength > aMaxLength )
       
   164             {
       
   165             // If length is too long no need to continue checking
       
   166             aValidity = ETooLong;
       
   167             result.Set( aData.Left( maxUiLength ) );
       
   168             break;
       
   169             }
       
   170         }    
       
   171 
       
   172     return result;
       
   173     }
       
   174 
       
   175 // -----------------------------------------------------------------------------
       
   176 // CPsu2CharConv::CheckFieldLengthL
       
   177 // -----------------------------------------------------------------------------
       
   178 //
       
   179 TPtrC CPsu2CharConv::CheckFieldLengthL(
       
   180         const TDesC& aData, 
       
   181         TInt aMaxLength,
       
   182         TInt& aUnconvertedCount )
       
   183     {
       
   184     TPtrC result( aData );
       
   185     HBufC8* convertedData = HBufC8::NewLC( aData.Length() * KMaxSizeOfConvertedChar );
       
   186     TPtr8 convertedDataPtr = convertedData->Des();
       
   187 
       
   188     // Start with full length data and remove one character on each iteration
       
   189     for( TInt i( aData.Length() ); i > 0; i-- )
       
   190         {
       
   191         TPtrC convertableData( aData.Left( i ) );
       
   192         convertedDataPtr.Zero();
       
   193 
       
   194         User::LeaveIfError(
       
   195                 iConverter->ConvertFromUnicode(
       
   196                         convertedDataPtr, convertableData, aUnconvertedCount ) );
       
   197         TInt convertedLength = convertedDataPtr.Length();
       
   198 
       
   199         // Check if any extra space needs to be reserved (there is some
       
   200         // differences in special character conversions between Symbian
       
   201         // character converter and SAT)
       
   202         ReviseLength( convertableData, convertedLength );
       
   203 
       
   204         // Check if the conversion will fit to the space available
       
   205         if( convertedLength <= aMaxLength )
       
   206             {
       
   207             PBK2_DEBUG_PRINT( PBK2_DEBUG_STRING
       
   208                 ("CPsu2CharConv::CheckFieldLengthL passed, max len: %i"), i );
       
   209             // Maximum allowed data length found, no need to continue
       
   210             result.Set( convertableData );
       
   211             break;
       
   212             }
       
   213         }
       
   214 
       
   215     CleanupStack::PopAndDestroy( convertedData );
       
   216     return result;
       
   217     }
       
   218 
       
   219 // -----------------------------------------------------------------------------
       
   220 // CPsu2CharConv::ReviseMaxLength
       
   221 // Checks some special cases to need if extra space needs to be reserved for
       
   222 // the data when saving to (U)SIM.
       
   223 // -----------------------------------------------------------------------------
       
   224 //
       
   225 void CPsu2CharConv::ReviseLength(
       
   226         const TDesC& aData, TInt& aConvertedLength )
       
   227     {
       
   228     // Check if the data contains unicode line/paragraph separator(s).
       
   229     // If it does it will require some extra space when saved to (U)SIM.
       
   230     for( TInt i(0); i < aData.Length(); i++ )
       
   231         {
       
   232         if( aData[i] == KLineSeparator || aData[i] == KParagraphSeparator )
       
   233             {
       
   234             PBK2_DEBUG_PRINT( PBK2_DEBUG_STRING
       
   235                 ("CPsu2CharConv::CheckFieldLengthL separator found" ) );
       
   236             const TInt KParagraphSeparatorExtraSpace(3);
       
   237             aConvertedLength += KParagraphSeparatorExtraSpace;
       
   238             // Search no more, reserve more space only once
       
   239             break;
       
   240             }
       
   241         }
       
   242     }
       
   243 
       
   244 // -----------------------------------------------------------------------------
       
   245 // CPsu2CharConv::IsUnicodeL
       
   246 // 
       
   247 // -----------------------------------------------------------------------------
       
   248 //
       
   249 TBool CPsu2CharConv::IsUnicodeL( const TDesC& aText )
       
   250     {
       
   251     TBool isUnicode( EFalse );
       
   252     TInt unconvertedCount(0);
       
   253     HBufC8* convertedText = HBufC8::NewLC( aText.Length() );
       
   254     TPtr8 convertedTextPtr = convertedText->Des(); 
       
   255 
       
   256     iConverter->ConvertFromUnicode( convertedTextPtr, aText, unconvertedCount );
       
   257 
       
   258     // If any characters were not converted or if the converted text is longer
       
   259     // than the original the text must be unicode
       
   260     if( unconvertedCount > 0 ||
       
   261         aText.Length() < convertedText->Length() )
       
   262         {
       
   263         isUnicode = ETrue;
       
   264         }
       
   265 
       
   266     CleanupStack::PopAndDestroy( convertedText );
       
   267     return isUnicode;
       
   268     }
       
   269 
       
   270 // End of File