messagingappbase/smartmessaging/msgeditorutils/src/MsgBioUtils.cpp
changeset 0 72b543305e3a
equal deleted inserted replaced
-1:000000000000 0:72b543305e3a
       
     1 /*
       
     2 * Copyright (c) 2002-2006 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:   Class offers helper methods for BIO controls
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 
       
    22 #include <AknQueryDialog.h>         // CAknTextQueryDialog
       
    23 #include <aknnotewrappers.h>
       
    24 #include <StringLoader.h>       // StringLoader
       
    25 
       
    26 #include "MsgBioUtils.h"            // Own header
       
    27 #include <uri16.h>                // TUriParser8
       
    28 #include <MsgEditorUtils.rsg>   // resouce identifiers
       
    29 #include "MsgEditorUtils.pan"   // for MsgEditorUtils panics
       
    30 
       
    31 // IP min length comes from 0.0.0.0 --> 7
       
    32 const TInt KIPMinLength = 7;
       
    33 // IP max lenght comes from 255.255.255.255 --> 15
       
    34 const TInt KIPMaxLength = 15;
       
    35 const TInt KMinDelimitedValue = 0;
       
    36 const TInt KMaxDelimitedValue = 255;
       
    37 const TInt KDigitMaxLength = 3;
       
    38 
       
    39 #ifdef _DEBUG
       
    40 _LIT(KMsgBioUtils,"MsgBioUtils");
       
    41 #endif
       
    42 
       
    43 // ================= MEMBER FUNCTIONS =======================
       
    44 
       
    45 // leaves if user cancels query
       
    46 EXPORT_C void MsgBioUtils::TextQueryL(TInt aQueryResource, TDes& aText)
       
    47     {
       
    48     CAknTextQueryDialog* dlg = new (ELeave) CAknTextQueryDialog(aText,CAknQueryDialog::ENoTone);
       
    49     if (dlg->ExecuteLD(aQueryResource)) //R_DATA_QUERY
       
    50         {
       
    51         // query approved
       
    52         return;
       
    53         }
       
    54     // query was cancelled
       
    55     User::Leave(KLeaveWithoutAlert);
       
    56     }
       
    57 
       
    58 EXPORT_C void MsgBioUtils::ErrorNoteL(const TDesC& aText)
       
    59     {
       
    60     CAknErrorNote* note = new(ELeave)CAknErrorNote( ETrue );
       
    61     note->ExecuteLD(aText);
       
    62     }
       
    63 
       
    64 EXPORT_C void MsgBioUtils::ErrorNoteL(TInt aStringResource)
       
    65     {
       
    66     HBufC* buf = StringLoader::LoadLC(aStringResource);
       
    67     ErrorNoteL(*buf);
       
    68     CleanupStack::PopAndDestroy(buf);
       
    69     }
       
    70 
       
    71 EXPORT_C void MsgBioUtils::ConfirmationNoteL(const TDesC& aText)
       
    72     {
       
    73     CAknConfirmationNote* note = new (ELeave) CAknConfirmationNote;
       
    74     note->ExecuteLD(aText);
       
    75     }
       
    76 
       
    77 EXPORT_C void MsgBioUtils::ConfirmationNoteL(TInt aStringResource)
       
    78     {
       
    79     HBufC* buf = StringLoader::LoadLC(aStringResource);
       
    80     ConfirmationNoteL(*buf);
       
    81     CleanupStack::PopAndDestroy(buf);
       
    82     }
       
    83 
       
    84 EXPORT_C void MsgBioUtils::InformationNoteL(const TDesC& aText)
       
    85     {
       
    86     CAknInformationNote* note = new (ELeave) CAknInformationNote;
       
    87     note->ExecuteLD(aText);
       
    88     }
       
    89 
       
    90 EXPORT_C void MsgBioUtils::InformationNoteL(TInt aStringResource)
       
    91     {
       
    92     HBufC* buf = StringLoader::LoadLC(aStringResource);
       
    93     InformationNoteL(*buf);
       
    94     CleanupStack::PopAndDestroy(buf);
       
    95     }
       
    96 
       
    97   
       
    98 EXPORT_C void MsgBioUtils::IncrementNameL(TDes& aName, TInt aMaxLength)
       
    99     {
       
   100     TInt length = aName.Length();
       
   101 
       
   102     // If this fails it means that at least default name should have been assigned
       
   103     // to item.
       
   104     __ASSERT_ALWAYS(length > 0, Panic(EMEUNameEmpty));
       
   105     TInt newOrdinal = 0;
       
   106     TInt index = length-1;
       
   107     TInt charValue = aName[index];
       
   108 
       
   109     // Check if name ends with ')'
       
   110     if (charValue == ')')
       
   111         {
       
   112         TBool cutBetweenIndexes = EFalse;
       
   113         index--;
       
   114         TInt multi = 1;
       
   115         while ( index >= 0)
       
   116             {
       
   117             charValue = aName[index];
       
   118             TChar ch(charValue);
       
   119 
       
   120             if (!ch.IsDigit())
       
   121                 {
       
   122                 // There was non-digits inside the brackets.
       
   123                 if ( charValue == '(')
       
   124                     {
       
   125                     // We found the start
       
   126                     cutBetweenIndexes = ETrue;
       
   127                     index--;
       
   128                     break;
       
   129                     }
       
   130                 else
       
   131                     {
       
   132                     // There was a non-digit inside the '(' and ')'
       
   133                     // We just append after the original name.
       
   134                     break;
       
   135                     }
       
   136                 } // if
       
   137             else
       
   138                 {
       
   139                 TInt newNum = ch.GetNumericValue();
       
   140                 if (multi <= 100000000)
       
   141                     {
       
   142                     // If there's a pattern (007) then this makes it to be (007)(01)
       
   143                     if (newNum == 0 && newOrdinal != 0 && multi > 10)
       
   144                         {
       
   145                         break;
       
   146                         }
       
   147                     newOrdinal += newNum * multi;
       
   148                     multi*=10;
       
   149                     }//0010
       
   150                 else
       
   151                     {
       
   152                     newOrdinal = 0;
       
   153                     break;
       
   154                     }
       
   155                 }
       
   156             index--;
       
   157             } // while
       
   158 
       
   159         // There was correct pattern of (0000) so now we delete it.
       
   160         if (cutBetweenIndexes)
       
   161             {
       
   162             aName.Delete(index+1, length-index);
       
   163             }
       
   164         else
       
   165             {
       
   166             // This case is for example 12345) so we get 12345)(01)
       
   167             newOrdinal = 0;
       
   168             }
       
   169         } // if
       
   170 
       
   171     // Add one to the ordinal read from the old name
       
   172     newOrdinal++;
       
   173 
       
   174     // Check that there's enough space to add the ordinal
       
   175     TInt maxLength = aMaxLength;
       
   176     TInt ordinalSpace(4); // Ordinal space comes from (00) so it's 4
       
   177     length = aName.Length();
       
   178     if (length + ordinalSpace > maxLength)
       
   179         {
       
   180         aName.Delete(maxLength-ordinalSpace, ordinalSpace);
       
   181         }
       
   182 
       
   183     // Append the ordinal at the end of the name
       
   184     HBufC* old = aName.AllocLC();
       
   185 
       
   186     _LIT(KFormatSpecLessTen, "%S(0%d)");
       
   187     _LIT(KFormatSpecOverTen, "%S(%d)");
       
   188     if (newOrdinal < 10)
       
   189         {
       
   190         aName.Format(KFormatSpecLessTen, old, newOrdinal);
       
   191         }
       
   192     else
       
   193         {
       
   194         aName.Format(KFormatSpecOverTen, old, newOrdinal);
       
   195         }
       
   196     CleanupStack::PopAndDestroy(old); // old
       
   197     }
       
   198 
       
   199 EXPORT_C TBool MsgBioUtils::CheckIP( const TDesC& aIP )
       
   200     {
       
   201     TInt length( aIP.Length() );
       
   202 
       
   203     // Check that ip length is correct.
       
   204     if ( !(length >= KIPMinLength && length <= KIPMaxLength) )
       
   205         {
       
   206         return EFalse;
       
   207         }
       
   208 
       
   209 	TChar token('.');
       
   210     const TInt KNumberOfTokenizedValues = 4;
       
   211 
       
   212     TInt startIndex( -1 ); // startIndex is advanced in loop to point to next char.
       
   213     for ( TInt i = 0; i < KNumberOfTokenizedValues; i++ )
       
   214         {
       
   215         TInt value(0);
       
   216 
       
   217         // Advance the startIndex to point to the next character.
       
   218         startIndex++;
       
   219         if ( startIndex < length )
       
   220             {
       
   221             TBool hasNumValue = GetTokenizedValue( aIP, token, startIndex, value );
       
   222             if ( !hasNumValue )
       
   223                 {
       
   224                 return EFalse;
       
   225                 }
       
   226             // Set the value to the correct variable
       
   227             switch ( i )
       
   228                 {
       
   229                 case 0:
       
   230                     {
       
   231                     
       
   232                     break;
       
   233                     }
       
   234                 case 1:
       
   235                     {
       
   236                     
       
   237                     break;
       
   238                     }
       
   239                 case 2:
       
   240                     {
       
   241                     
       
   242                     break;
       
   243                     }
       
   244                 case 3:
       
   245                     {
       
   246                     
       
   247                     break;
       
   248                     }
       
   249                 default:
       
   250                     {
       
   251                     // Check that your KNumberOfTokenizedValues is correct
       
   252                     // and add cases to this switch clause.
       
   253                     User::Invariant(); 
       
   254                     break;
       
   255                     }
       
   256                 }
       
   257             }
       
   258         else
       
   259             {
       
   260             // too short IP string
       
   261             return EFalse;
       
   262             }
       
   263         }
       
   264 
       
   265     // At this point we've extracted four Tokenized values from the
       
   266     // ip string. 
       
   267     if ( startIndex < length )
       
   268         {
       
   269         // too long IP string
       
   270         return EFalse;
       
   271         }
       
   272 
       
   273     /// do any consistency checks here.
       
   274     return ETrue;
       
   275     }
       
   276 
       
   277 EXPORT_C TBool MsgBioUtils::CheckURL( const TDesC& aUrl )
       
   278     {
       
   279     TUriParser16 uriParser;
       
   280     TInt err = uriParser.Parse( aUrl );
       
   281     if ( err == KErrNone )
       
   282         {
       
   283         return ETrue;
       
   284         }
       
   285     else
       
   286         {
       
   287         return EFalse;        
       
   288         }
       
   289     }
       
   290 
       
   291 EXPORT_C TBool MsgBioUtils::ConfirmationQueryOkCancelL(const TDesC& aText)
       
   292     {
       
   293     //This temporary descriptor is needed because the query wants a non const
       
   294     HBufC* text = aText.AllocL();
       
   295     CleanupStack::PushL(text);
       
   296     CAknQueryDialog* qDlg = new (ELeave) CAknQueryDialog(*text);
       
   297     TInt response =
       
   298         qDlg->ExecuteLD(R_MSGEDITORUTILS_GENERAL_CONFIRMATION_QUERY_OK_CANCEL);
       
   299     CleanupStack::PopAndDestroy( text );
       
   300     if (response)
       
   301         {
       
   302         return ETrue;
       
   303         }
       
   304     return EFalse;
       
   305     }
       
   306 
       
   307 EXPORT_C TBool MsgBioUtils::ConfirmationQueryOkCancelL(TInt aStringResource)
       
   308     {
       
   309     HBufC* buf = StringLoader::LoadLC(aStringResource);
       
   310     TBool result = ConfirmationQueryOkCancelL(*buf);
       
   311     CleanupStack::PopAndDestroy(buf);
       
   312     return result;
       
   313     }
       
   314 
       
   315 TBool MsgBioUtils::GetTokenizedValue( const TDesC& aIP, 
       
   316     const TChar atoken,
       
   317     TInt& aStartIndex,
       
   318     TInt& aValue )
       
   319     {
       
   320     TInt length(aIP.Length());
       
   321     __ASSERT_DEBUG( aStartIndex >= 0, User::Panic(KMsgBioUtils, KErrUnderflow ) );
       
   322     __ASSERT_DEBUG( aStartIndex < length, User::Panic(KMsgBioUtils, KErrOverflow ) );
       
   323 
       
   324     aValue = 0;
       
   325     TBool hasValue(EFalse);
       
   326     TInt rounds(0);
       
   327     while ( aStartIndex < length )
       
   328         {
       
   329         rounds++;
       
   330         if ( rounds > KDigitMaxLength + 1  ) // +1 comes from the dot (255. -> count is 4 )
       
   331             {
       
   332             return EFalse;
       
   333             }
       
   334         TChar character = aIP[aStartIndex];
       
   335         if ( character.IsDigit() )
       
   336             {
       
   337             hasValue = ETrue;
       
   338             aValue*=10;
       
   339             TInt lastDigit = character.GetNumericValue();
       
   340             aValue+=lastDigit;
       
   341 
       
   342             // Do the limit check for value.
       
   343             if ( aValue < KMinDelimitedValue || aValue > KMaxDelimitedValue )
       
   344                 {
       
   345                 return EFalse;
       
   346                 }
       
   347             }
       
   348         else
       
   349             {
       
   350             // Check if it's dot
       
   351             if ( character == atoken )
       
   352                 {
       
   353                 return hasValue;
       
   354                 }
       
   355             else // not valid
       
   356                 {
       
   357                 return EFalse;
       
   358                 }
       
   359             }
       
   360         aStartIndex++;
       
   361         }
       
   362     return hasValue; // Check that we had something.
       
   363     }
       
   364 
       
   365 // End of File