fep/aknfep/UiPlugins/AknFepUiInterface/AvkonImpl/src/AknFepUiAvkonCtrlJapaneseKutenQueryDialog.cpp
changeset 0 eb1f2e154e89
child 7 6defe5d1bd39
equal deleted inserted replaced
-1:000000000000 0:eb1f2e154e89
       
     1 /*
       
     2 * Copyright (c) 2002-2004 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:            Kuten code query dialog
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 
       
    22 
       
    23 
       
    24 
       
    25 
       
    26 
       
    27 
       
    28 
       
    29 // INCLUDE FILES
       
    30 #include <barsread.h>
       
    31 
       
    32 #include <numberconversion.h>
       
    33 #include <AknQueryDialog.h>
       
    34 #include <aknQueryControl.h>
       
    35 #include <aknnotewrappers.h>                        // CAknInformationNote
       
    36 #include <aknsoundsystem.h>
       
    37 #include <charconv.h>
       
    38 #include <PtiDefs.h>                //keys
       
    39 #include <AknFep.rsg>
       
    40 #include <avkon.hrh>
       
    41 
       
    42 #include "AknFepUIAvkonCtrlJapaneseKutenQueryDialog.h"
       
    43 
       
    44 // MACROS
       
    45 
       
    46 // CONSTANTS
       
    47 const TInt KKutenCodeLength = 4;
       
    48 const TInt KDummyUnicode = 0xfffd;
       
    49 const TInt KTextKutenCodeErrorMaxSize = 32;
       
    50 const TText KHalfWidthNotSign = 0x00AC;
       
    51 const TText KFullWidthNotSign = 0xFFE2;
       
    52 
       
    53 // ================= MEMBER FUNCTIONS =========================================
       
    54 
       
    55 // -----------------------------------------------------------------------------
       
    56 // CAknFepUICtrlJapaneseKutenQueryDialog::NewL
       
    57 // Two-phased constructor.
       
    58 // -----------------------------------------------------------------------------
       
    59 //
       
    60 CAknFepUICtrlJapaneseKutenQueryDialog*
       
    61 CAknFepUICtrlJapaneseKutenQueryDialog::NewL(TInt& aCharCode, const TTone& aTone)
       
    62     {
       
    63     CAknFepUICtrlJapaneseKutenQueryDialog* self =
       
    64         new (ELeave) CAknFepUICtrlJapaneseKutenQueryDialog(aCharCode, aTone);
       
    65 
       
    66     return self;
       
    67     }
       
    68 
       
    69 // ----------------------------------------------------------------------------
       
    70 // CAknFepUICtrlJapaneseKutenQueryDialog::~CAknFepUICtrlJapaneseKutenQueryDialog()
       
    71 // Destructor.
       
    72 // ----------------------------------------------------------------------------
       
    73 //
       
    74 CAknFepUICtrlJapaneseKutenQueryDialog::~CAknFepUICtrlJapaneseKutenQueryDialog()
       
    75     {
       
    76     }
       
    77 
       
    78 // ----------------------------------------------------------------------------
       
    79 // CAknFepUICtrlJapaneseKutenQueryDialog::OfferKeyEventL
       
    80 //
       
    81 // ----------------------------------------------------------------------------
       
    82 //
       
    83 TKeyResponse CAknFepUICtrlJapaneseKutenQueryDialog::OfferKeyEventL(const TKeyEvent& aKeyEvent,
       
    84                                                                    TEventCode aType)
       
    85 {
       
    86     if (aType == EEventKey)
       
    87     {
       
    88         TInt keyCode = aKeyEvent.iCode;
       
    89         switch(keyCode)
       
    90         {
       
    91             case EPtiKey0:
       
    92             case EPtiKey1:
       
    93             case EPtiKey2:
       
    94             case EPtiKey3:
       
    95             case EPtiKey4:
       
    96             case EPtiKey5:
       
    97             case EPtiKey6:
       
    98             case EPtiKey7:
       
    99             case EPtiKey8:
       
   100             case EPtiKey9:
       
   101                 {
       
   102                     if (iDataText.Length() == KKutenCodeLength)
       
   103                     {
       
   104                         if (SoundSystem())
       
   105                         {
       
   106                         SoundSystem()->PlaySound(EErrorTone);
       
   107                         }
       
   108                     }
       
   109                 }
       
   110                 break;
       
   111             default:
       
   112                 break;
       
   113         }
       
   114     }
       
   115     return CAknTextQueryDialog::OfferKeyEventL(aKeyEvent, aType);
       
   116 }
       
   117 
       
   118 // -----------------------------------------------------------------------------
       
   119 // CAknFepUICtrlJapaneseKutenQueryDialog::OkToExitL
       
   120 // This function is called in case pressing OK or Cancel
       
   121 // -----------------------------------------------------------------------------
       
   122 //
       
   123 TBool CAknFepUICtrlJapaneseKutenQueryDialog::OkToExitL(TInt aButtonId)
       
   124     {
       
   125     TBool checkResult = EFalse;
       
   126     iCharCode = ConvertKutenCode2Unicode();
       
   127     if (iCharCode > 0)
       
   128         {
       
   129         checkResult = CAknTextQueryDialog::OkToExitL(aButtonId);
       
   130         }
       
   131     else
       
   132         {
       
   133         TResourceReader reader;
       
   134         TBufC<KTextKutenCodeErrorMaxSize> string;
       
   135         CCoeEnv::Static()->CreateResourceReaderLC(reader, R_AKNFEP_TEXT_KUTEN_INVALID_ERROR);
       
   136         string = reader.ReadTPtrC();
       
   137         CleanupStack::PopAndDestroy();      // reader
       
   138 
       
   139         CAknInformationNote* dialog = new(ELeave) CAknInformationNote();
       
   140         dialog->ExecuteLD(string);
       
   141 
       
   142         CAknQueryControl* control = QueryControl();
       
   143         if (control)
       
   144             {
       
   145             control->SetTextL(KNullDesC);
       
   146             control->DrawNow();
       
   147             }
       
   148 
       
   149         UpdateLeftSoftKeyL();
       
   150         }
       
   151 
       
   152     return checkResult;
       
   153     }
       
   154 
       
   155 // -----------------------------------------------------------------------------
       
   156 // CAknFepUICtrlJapaneseKutenQueryDialog::UpdateLeftSoftKeyL
       
   157 // Updating left softkey
       
   158 //
       
   159 // If control text is ok then display left soft key
       
   160 // otherwise don't
       
   161 // -----------------------------------------------------------------------------
       
   162 //
       
   163 void CAknFepUICtrlJapaneseKutenQueryDialog::UpdateLeftSoftKeyL()
       
   164     {
       
   165     CAknQueryControl* control = QueryControl();
       
   166     if ( control )
       
   167         {
       
   168         MakeLeftSoftkeyVisibleL();
       
   169         }
       
   170     }
       
   171 
       
   172 // ----------------------------------------------------------------------------
       
   173 // CAknFepUICtrlJapaneseKutenQueryDialog::CAknFepUICtrlJapaneseKutenQueryDialog()
       
   174 // Overload constructor.
       
   175 //-----------------------------------------------------------------------------
       
   176 //
       
   177 CAknFepUICtrlJapaneseKutenQueryDialog::CAknFepUICtrlJapaneseKutenQueryDialog(
       
   178         TInt& aCharCode, const TTone& aTone)
       
   179         : CAknTextQueryDialog(iBuffer, aTone),
       
   180           iCharCode(aCharCode),
       
   181           iCbaResId(R_AVKON_SOFTKEYS_CANCEL)
       
   182     {
       
   183     iCharCode = 0;
       
   184     }
       
   185 
       
   186 TInt CAknFepUICtrlJapaneseKutenQueryDialog::ExecuteLD(TInt aResourceId)
       
   187     {
       
   188     PrepareLC(aResourceId);
       
   189     CEikEdwin* edwin = STATIC_CAST(CEikEdwin*, (QueryControl()->ControlByLayoutOrNull(EDataLayout)));
       
   190     edwin->SetAknEditorAllowedInputModes(EAknEditorNumericInputMode);
       
   191     edwin->SetAknEditorNumericKeymap(EAknEditorPlainNumberModeKeymap);
       
   192     return(RunLD());
       
   193     }
       
   194 
       
   195 // -----------------------------------------------------------------------------
       
   196 // CAknFepUICtrlJapaneseKutenQueryDialog::MakeLeftSoftkeyVisibleL
       
   197 // Set visibility of the left softkey.
       
   198 // -----------------------------------------------------------------------------
       
   199 //
       
   200 void CAknFepUICtrlJapaneseKutenQueryDialog::MakeLeftSoftkeyVisibleL()
       
   201     {
       
   202     TBool visible = EFalse;
       
   203     CAknQueryControl* control = QueryControl();
       
   204     if (control)
       
   205         {
       
   206         control->GetText(iDataText);
       
   207         if (iDataText.Length() == KKutenCodeLength)
       
   208             {
       
   209             visible = ETrue;
       
   210             }
       
   211         }
       
   212     else
       
   213         {
       
   214         visible = EFalse;
       
   215         }
       
   216 
       
   217     TUint resid = R_AKNFEP_SOFTKEYS_EMPTY_CANCEL_EMPTY;
       
   218     if (visible)
       
   219         {
       
   220         resid = R_AVKON_SOFTKEYS_OK_CANCEL__OK;
       
   221         }
       
   222     if (iCbaResId != resid)
       
   223         {
       
   224         ButtonGroupContainer().SetCommandSetL(resid);
       
   225         ButtonGroupContainer().DrawDeferred();
       
   226         iCbaResId = resid;
       
   227         }
       
   228 
       
   229     }
       
   230 
       
   231 // -----------------------------------------------------------------------------
       
   232 // CAknFepUICtrlJapaneseKutenQueryDialog::ConvertKutenCode2Unicode
       
   233 // Convert from Kuten code to unicode, Kuten code is 4 characters which inputted in query dialog.
       
   234 // -----------------------------------------------------------------------------
       
   235 TText CAknFepUICtrlJapaneseKutenQueryDialog::ConvertKutenCode2Unicode()
       
   236     {
       
   237     TText code = 0;
       
   238     if (iBuffer.Length() == KKutenCodeLength)
       
   239         {
       
   240         TBuf<4 + 1> buf;
       
   241         TBool valid = ETrue;
       
   242         buf.FillZ(4 + 1);
       
   243 
       
   244         for (TInt i = 0; i < KKutenCodeLength; ++i)
       
   245             {
       
   246             if ((iBuffer[i] >= '0') && (iBuffer[i] <= '9'))
       
   247                 {
       
   248                 buf[i] = (TText)(iBuffer[i] - '0');
       
   249                 }
       
   250             else
       
   251                 {
       
   252                 valid = EFalse;
       
   253                 break;
       
   254                 }
       
   255             }
       
   256         if (valid)
       
   257             {
       
   258             TInt hi_code = buf[0] * 10 + buf[1] + 0x20;
       
   259             TInt low_code = buf[2] * 10 + buf[3] + 0x20;
       
   260             code = (TText)((hi_code << 8) + low_code);
       
   261             AdjustJisCode(code);
       
   262             code = JisToSjis((TText)code);
       
   263             if (AdjustSjisCode(code))
       
   264                 {
       
   265                 code = ConvSjisToUnicode(code);
       
   266                 }
       
   267             else
       
   268                 {
       
   269                 code = 0;
       
   270                 }
       
   271             }
       
   272         }
       
   273 
       
   274     // '0244" Kuten codes is converted to '0x00AC' unicode of half-width
       
   275     // character. In Japanese variant, Kuten code input must be always
       
   276     // Full-width character, so it converts from '0x00AC' to '0xFFE2'
       
   277     if (code == KHalfWidthNotSign)
       
   278         {
       
   279         code = KFullWidthNotSign;
       
   280         }
       
   281 
       
   282     return code;
       
   283     }
       
   284 
       
   285 void CAknFepUICtrlJapaneseKutenQueryDialog::AdjustJisCode(TText& aJisCode)
       
   286     {
       
   287     TUint JisHi = (aJisCode >> 8) & 0x000000ff;
       
   288     TUint JisLow = aJisCode & 0x000000ff;
       
   289 
       
   290     if ((JisHi < EJisFirstMin) || (JisHi > EJisFirstMax))
       
   291         {
       
   292         JisHi = 0;
       
   293         JisLow = 0;
       
   294         }
       
   295     else
       
   296         {
       
   297         if ((JisLow < EJisSecondMin) || (JisLow > EJisSecondMax))
       
   298             {
       
   299             JisHi = 0;
       
   300             JisLow = 0;
       
   301             }
       
   302         else
       
   303             {
       
   304             if (JisHi == EJisFirstMax)
       
   305                 {
       
   306                 if (JisLow > EJisLastSecondMax)
       
   307                     {
       
   308                     JisHi = 0;
       
   309                     JisLow = 0;
       
   310                     }
       
   311                 }
       
   312             }
       
   313         }
       
   314 
       
   315     aJisCode = (TText)((JisHi << 8) + JisLow);
       
   316     return;
       
   317     }
       
   318 
       
   319 TBool CAknFepUICtrlJapaneseKutenQueryDialog::AdjustSjisCode(TText& aSjisCode)
       
   320 {
       
   321     TUint SjisHi = (aSjisCode >> 8) & 0x000000ff;
       
   322     TUint SjisLow = aSjisCode & 0x000000ff;
       
   323 
       
   324     if ((SjisHi < ESjisFirstMin1) || (SjisHi > ESjisFirstMax2)
       
   325         || ((SjisHi > ESjisFirstMax1) && (SjisHi < ESjisFirstMin2)))
       
   326         {
       
   327         SjisHi = 0;
       
   328         SjisLow = 0;
       
   329         }
       
   330     else
       
   331         {
       
   332         if ((SjisLow < ESjisSecondMin1) || (SjisLow > ESjisSecondMax2)
       
   333             || ((SjisLow > ESjisSecondMax1) && (SjisLow < ESjisSecondMin2)))
       
   334             {
       
   335             SjisHi = 0;
       
   336             SjisLow = 0;
       
   337             }
       
   338         else
       
   339             {
       
   340             if (SjisHi == ESjisFirstMax2)
       
   341                 {
       
   342                 if (SjisLow > ESjisSecondMax2)
       
   343                     {
       
   344                     SjisHi = 0;
       
   345                     SjisLow = 0;
       
   346                     }
       
   347                 }
       
   348             }
       
   349         }
       
   350 
       
   351     if (CheckSjisCode((TText)((SjisHi << 8) + SjisLow)))
       
   352         {
       
   353         aSjisCode = (TText)((SjisHi << 8) + SjisLow);
       
   354         return ETrue;
       
   355         }
       
   356     else
       
   357         {
       
   358         return EFalse;
       
   359         }
       
   360     }
       
   361 
       
   362 TText CAknFepUICtrlJapaneseKutenQueryDialog::JisToSjis(TText aJisCode)
       
   363     {
       
   364     if (aJisCode == 0)
       
   365         {
       
   366         return 0;
       
   367         }
       
   368 
       
   369     TUint jis_hi = (aJisCode >> 8) & 0x000000ff;    /* hi byte */
       
   370     TUint jis_lo = aJisCode & 0x000000ff;           /* low byte */
       
   371 
       
   372     jis_lo += 0x1F;
       
   373     if  ((jis_hi & 1) == 0)
       
   374         {
       
   375         jis_hi >>= 1;
       
   376         jis_hi--;
       
   377         jis_lo += 0x5E;
       
   378         }
       
   379     else
       
   380         {
       
   381         jis_hi >>= 1;
       
   382         }
       
   383     if  (jis_lo >= 0x7F)
       
   384         {
       
   385         jis_lo++;
       
   386         }
       
   387     jis_hi += (jis_hi > 0x2E) ? 0xB1 : 0x71;
       
   388 
       
   389     return (TText)((jis_hi << 8) + jis_lo);
       
   390     }
       
   391 
       
   392 TBool CAknFepUICtrlJapaneseKutenQueryDialog::CheckSjisCode(TText aSjisCode)
       
   393     {
       
   394     const TText8 first = (TText8)((aSjisCode >> 8) & 0x000000ff);
       
   395     const TText8 second = (TText8)(aSjisCode & 0x000000ff);
       
   396     if ((((first >= ESjisFirstMin1) && (first <= ESjisFirstMax1))
       
   397          || ((first >= ESjisFirstMin2) && (first <= ESjisFirstMax2)))
       
   398         && (((second >= ESjisSecondMin1) && (second <= ESjisSecondMax1))
       
   399             || ((second >= ESjisSecondMin2) && (second <= ESjisSecondMax2))))
       
   400         {
       
   401         return ETrue;
       
   402         }
       
   403     else
       
   404         {
       
   405         return EFalse;
       
   406         }
       
   407     }
       
   408 
       
   409 TText CAknFepUICtrlJapaneseKutenQueryDialog::ConvSjisToUnicode(TText aSjisCode)
       
   410     {
       
   411     TText8  sjis[2];
       
   412     TText   unicode = 0;
       
   413     if (aSjisCode & 0xff00)
       
   414         {
       
   415         sjis[0] = (TText8)((aSjisCode & 0xff00) >> 8);
       
   416         sjis[1] = (TText8)(aSjisCode & 0x00ff);
       
   417         }
       
   418     else
       
   419         {
       
   420         sjis[0] = (TText8)aSjisCode;
       
   421         sjis[1] = '\0';
       
   422         }
       
   423 
       
   424     TPtrC8 foreign((TUint8*)&sjis[0], sizeof sjis);
       
   425     TPtr16 unicode_des((TUint16*)&unicode, 1);
       
   426     RFs fileServerSession;
       
   427 
       
   428     if (fileServerSession.Connect() == 0)
       
   429         {
       
   430         CCnvCharacterSetConverter*  charconv = NULL;
       
   431         TRAPD(error, charconv = CCnvCharacterSetConverter::NewL());
       
   432         if (error == KErrNone)
       
   433             {
       
   434             TRAP(error, charconv->PrepareToConvertToOrFromL(KCharacterSetIdentifierShiftJis, fileServerSession));
       
   435             if (error == KErrNone)
       
   436                 {
       
   437                 TInt state = CCnvCharacterSetConverter::KStateDefault;
       
   438                 charconv->ConvertToUnicode(unicode_des, foreign, state);
       
   439                 }
       
   440             delete charconv;
       
   441             }
       
   442         fileServerSession.Close();
       
   443         }
       
   444     if (unicode == (TText)KDummyUnicode)
       
   445         {
       
   446         unicode = 0;
       
   447         }
       
   448     return unicode;
       
   449     }
       
   450 
       
   451 // End of File