wvuing/IMPSConnectionUI/UISrc/CCnUiUsernameAndPwdQuery.cpp
changeset 0 094583676ce7
equal deleted inserted replaced
-1:000000000000 0:094583676ce7
       
     1 /*
       
     2 * Copyright (c) 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:  Username and password multiline data query
       
    15 *
       
    16 */
       
    17 
       
    18 // INCLUDE FILES
       
    19 #include  "CCnUiUsernameAndPwdQuery.h"
       
    20 #include  "CnUiIMPSIdValidator.h"
       
    21 #include  "MCnUiUiFacade.h"
       
    22 
       
    23 #include <barsread.h>
       
    24 #include <eikcapc.h>
       
    25 #include <LayoutMetaData.cdl.h>
       
    26 
       
    27 #include <IMPSConnectionUiNG.rsg>
       
    28 
       
    29 #include 	"VariantKeys.h"
       
    30 #include 	<centralrepository.h>
       
    31 #include 	"CnUiResourceFileName.h"
       
    32 #include "CIMPSSharedDataFactory.h"
       
    33 #include "MIMPSSharedData.h"
       
    34 #include "IMPSPresenceConnectionUiConstsNG.h"
       
    35 
       
    36 // ================= MEMBER FUNCTIONS =======================
       
    37 CCnUiUsernameAndPwdQuery* CCnUiUsernameAndPwdQuery::NewL( TDes&  aUserName,
       
    38                                                           TDes&  aPwd,
       
    39                                                           TCnUiLoginDataQueryMode aMode,
       
    40                                                           TBool aDomainSelectionQueryVariation,
       
    41                                                           MCnUiUiFacade& aUiFacade )
       
    42     {
       
    43     TInitialFocus focus;
       
    44     TBool cursorToBeginning = EFalse;
       
    45 
       
    46     switch ( aMode )
       
    47         {
       
    48         case ECnUiReEdit:
       
    49             {
       
    50             //in re-edit focus always the user name field
       
    51             focus = EFirstLine;
       
    52             break;
       
    53             }
       
    54         case ECnUiNormal:
       
    55             {
       
    56             // detect which line to focus
       
    57             focus = DetectLineToFocus( aUserName, aPwd );
       
    58             break;
       
    59             }
       
    60         case ECnUiEditPrefilledUser:
       
    61             {
       
    62             // detect which line to focus
       
    63             focus = EFirstLine;
       
    64             cursorToBeginning = ETrue;
       
    65             break;
       
    66             }
       
    67         default:
       
    68             {
       
    69             focus = EFirstLine;
       
    70             break;
       
    71             }
       
    72         }
       
    73 
       
    74 
       
    75     CCnUiUsernameAndPwdQuery* self =
       
    76         new ( ELeave ) CCnUiUsernameAndPwdQuery( focus,
       
    77                                                  aUiFacade,
       
    78                                                  aDomainSelectionQueryVariation,
       
    79                                                  cursorToBeginning );
       
    80     CleanupStack::PushL( self );
       
    81     self->SetDataL( aUserName, aPwd );
       
    82     self->ConstructL();
       
    83     CleanupStack::Pop( self );
       
    84     return self;
       
    85     }
       
    86 
       
    87 
       
    88 
       
    89 // C++ default constructor can NOT contain any code, that
       
    90 // might leave.
       
    91 //
       
    92 CCnUiUsernameAndPwdQuery::CCnUiUsernameAndPwdQuery( TInitialFocus aInitialFocus,
       
    93                                                     MCnUiUiFacade& aUiFacade,
       
    94                                                     TBool aDomainSelectionQueryVariation,
       
    95                                                     TBool aCursorToBeginning /* = EFalse */ )
       
    96         : CAknMultiLineDataQueryDialog( ENoTone ),
       
    97         iInitialFocus( aInitialFocus ),
       
    98         iCursorToBeginning( aCursorToBeginning ),
       
    99         iUiFacade( aUiFacade ),
       
   100         iDomainSelectionQueryVariation( aDomainSelectionQueryVariation ),
       
   101         iForcedExit( EFalse )
       
   102     {
       
   103     }
       
   104 
       
   105 
       
   106 //ConstructL
       
   107 void CCnUiUsernameAndPwdQuery::ConstructL()
       
   108     {
       
   109     iSharedData = CIMPSSharedDataFactory::CreateTemporaryKeyHandlerL( NULL, KBrandingUid );
       
   110     }
       
   111 
       
   112 // Destructor
       
   113 CCnUiUsernameAndPwdQuery::~CCnUiUsernameAndPwdQuery()
       
   114     {
       
   115     iRFs.Close();
       
   116     delete iSharedData;
       
   117     }
       
   118 
       
   119 // -----------------------------------------------------------------------------
       
   120 // CCnUiUsernameAndPwdQuery::SetInitialCurrentLine()
       
   121 //
       
   122 // From CEikDialog
       
   123 // -----------------------------------------------------------------------------
       
   124 //
       
   125 void CCnUiUsernameAndPwdQuery::SetInitialCurrentLine()
       
   126     {
       
   127 
       
   128     //if initial line can't be set,
       
   129     //the error can be only ignored
       
   130     TInt ignore;
       
   131     TRAP( ignore, DoSetInitialCurrentLineL() );
       
   132 
       
   133     iRFs.Close();
       
   134     }
       
   135 
       
   136 
       
   137 // -----------------------------------------------------------------------------
       
   138 // CCnUiUsernameAndPwdQuery::OkToExitL()
       
   139 //
       
   140 // From CEikDialog
       
   141 // -----------------------------------------------------------------------------
       
   142 //
       
   143 TBool CCnUiUsernameAndPwdQuery::OkToExitL( TInt aButtonId )
       
   144     {
       
   145     if ( iForcedExit )
       
   146         {
       
   147         // If dialog needs to be exited because user
       
   148         // emptied user id field, allow exit and don't do any checks
       
   149         return ETrue;
       
   150         }
       
   151 
       
   152     HBufC* userId = HBufC::NewLC( FirstControl()->GetTextLength() );
       
   153     TPtr IdPtr( userId->Des() );
       
   154     FirstControl()->GetText( IdPtr );
       
   155 
       
   156     // Check userID before changing to next page
       
   157     if ( CnUiIMPSIdValidator::ValidLoginIdL( IdPtr ) )
       
   158         {
       
   159         CleanupStack::PopAndDestroy( userId );
       
   160         TBool okValue = CAknMultiLineDataQueryDialog::OkToExitL( aButtonId );
       
   161         UpdateLeftSoftKeyL();
       
   162         return okValue;
       
   163         }
       
   164     else
       
   165         {
       
   166         //WV id faulty, show note
       
   167         iUiFacade.ShowNoteL( ECnUiImproperPresenceId, IdPtr );
       
   168         CleanupStack::PopAndDestroy( userId );
       
   169 
       
   170         //try to focus whole editor line
       
   171         CEikEdwin* presIdEdwin = ( CEikEdwin* )
       
   172                                  FirstControl()->ControlByLayoutOrNull( EMultiDataFirstEdwin );
       
   173         if ( presIdEdwin )
       
   174             {
       
   175             TryChangeFocusToL( EMultilineFirstLine );
       
   176             presIdEdwin->SelectAllL();
       
   177             }
       
   178         }
       
   179 
       
   180     return EFalse;
       
   181     }
       
   182 
       
   183 // -----------------------------------------------------------------------------
       
   184 // CCnUiUsernameAndPwdQuery::TryChangeFocusToL()
       
   185 // From CEikDialog
       
   186 // -----------------------------------------------------------------------------
       
   187 //
       
   188 void CCnUiUsernameAndPwdQuery::TryChangeFocusToL( TInt aControlId )
       
   189     {
       
   190     // This special focusing is only used to revert the focus
       
   191     // back from second line to first line ( landscape mode ).
       
   192     if ( Layout_Meta_Data::IsLandscapeOrientation() &&
       
   193          aControlId == EMultilineFirstLine )
       
   194         {
       
   195         // CAknMultiLineDataQueryDialog does not support changing the query
       
   196         // backwards, so we have to do it "manually".
       
   197         // This is copied from CAknMultiLineDataQueryDialog
       
   198         TBool firstLineEnabled = ETrue;
       
   199         TBool secondLineEnabled = EFalse;
       
   200         CAknMultilineQueryControl* firstControl = FirstControl();
       
   201         CAknMultilineQueryControl* secondControl = SecondControl();
       
   202         TInt line1 = FindLineIndex( *firstControl );
       
   203         TInt line2 = FindLineIndex( *secondControl );
       
   204         CEikCaptionedControl *ctrl1 = GetLineByLineAndPageIndex( line1, 0 );
       
   205         CEikCaptionedControl *ctrl2 = GetLineByLineAndPageIndex( line2, 0 );
       
   206         ctrl1->SetLatent( !firstLineEnabled );
       
   207         ctrl2->SetLatent( !secondLineEnabled );
       
   208         ctrl1->SetLatentGroupLineFollows( !firstLineEnabled );
       
   209         ctrl2->SetLatentGroupLineFollows( !secondLineEnabled );
       
   210         ctrl1->SetFocusing( firstLineEnabled );
       
   211         ctrl2->SetFocusing( secondLineEnabled );
       
   212         }
       
   213 
       
   214     CAknMultiLineDataQueryDialog::TryChangeFocusToL( aControlId );
       
   215     }
       
   216 
       
   217 // -----------------------------------------------------------------------------
       
   218 // CCnUiUsernameAndPwdQuery::HandleResourceChange()
       
   219 // From CEikDialog
       
   220 // -----------------------------------------------------------------------------
       
   221 //
       
   222 void CCnUiUsernameAndPwdQuery::HandleResourceChange( TInt aType )
       
   223     {
       
   224     CAknMultiLineDataQueryDialog::HandleResourceChange( aType );
       
   225     if ( aType == KEikDynamicLayoutVariantSwitch )
       
   226         {
       
   227         TRAP_IGNORE( UpdateLeftSoftKeyL( ) );
       
   228         }
       
   229     }
       
   230 
       
   231 // -----------------------------------------------------------------------------
       
   232 // CCnUiUsernameAndPwdQuery::NeedToDismissQueryL
       
   233 // From CAknMultiLineDataQueryDialog
       
   234 // -----------------------------------------------------------------------------
       
   235 //
       
   236 TBool CCnUiUsernameAndPwdQuery::NeedToDismissQueryL( const TKeyEvent& aKeyEvent )
       
   237     {
       
   238     // Check if user has emptied user id field
       
   239     // and exit if domain selection query is variated on
       
   240     if ( iDomainSelectionQueryVariation &&
       
   241          ( aKeyEvent.iCode == EKeyBackspace || aKeyEvent.iCode == EKeyDelete ) )
       
   242         {
       
   243         CAknMultilineQueryControl* ctrl = FirstControl(); // user id field
       
   244         CCoeControl* coeCtrl = ctrl->ControlByLayoutOrNull( EMultiDataFirstEdwin );
       
   245         if ( coeCtrl )
       
   246             {
       
   247             CEikEdwin* firstEdwin = static_cast<CEikEdwin*>( coeCtrl );
       
   248             TInt textLen = firstEdwin->TextLength();
       
   249             TInt cursorPos = firstEdwin->CursorPos();
       
   250             if ( ( firstEdwin->Selection().Length() == textLen )
       
   251                  || ( textLen == 1 && cursorPos == 1 && aKeyEvent.iCode == EKeyBackspace )
       
   252                  || ( textLen == 1 && cursorPos == 0 && aKeyEvent.iCode == EKeyDelete ) )
       
   253                 {
       
   254                 // NeedToDismissQueryL is called first thing in OfferKeyEventL
       
   255                 // so if editor length is 1 or whole text is selected at this point
       
   256                 // it would be 0 after OfferKeyEventL, clear editor and dismiss dialog
       
   257                 firstEdwin->SelectAllL();
       
   258                 firstEdwin->ClearSelectionL();
       
   259                 iForcedExit = ETrue;
       
   260                 TryExitL( EAknSoftkeyExit );
       
   261                 return ETrue;
       
   262                 }
       
   263             else
       
   264                 {
       
   265                 // Text in editor, don't dismiss
       
   266                 return EFalse;
       
   267                 }
       
   268             }
       
   269         }
       
   270 
       
   271     // Else just return EFalse and don't dismiss dialog
       
   272     return EFalse;
       
   273     }
       
   274 
       
   275 // -----------------------------------------------------------------------------
       
   276 // CCnUiUsernameAndPwdQuery::DetectLineToFocus()
       
   277 // Private helper.
       
   278 // -----------------------------------------------------------------------------
       
   279 //
       
   280 CCnUiUsernameAndPwdQuery::TInitialFocus
       
   281 CCnUiUsernameAndPwdQuery::DetectLineToFocus(
       
   282     const TDesC& aFirstLine,
       
   283     const TDesC& aSecondLine )
       
   284     {
       
   285     //focus the second line just if the first one has something in it
       
   286     //and the second one is empty
       
   287     if ( ( aFirstLine.Length() > 0 ) && ( aSecondLine.Length() == 0 ) )
       
   288         {
       
   289         return ESecondLine;
       
   290         }
       
   291 
       
   292     //else
       
   293     return EFirstLine;
       
   294     }
       
   295 
       
   296 
       
   297 
       
   298 // -----------------------------------------------------------------------------
       
   299 // CCnUiUsernameAndPwdQuery::DoSetInitialCurrentLineL()
       
   300 // Private helper.
       
   301 // -----------------------------------------------------------------------------
       
   302 //
       
   303 void CCnUiUsernameAndPwdQuery::DoSetInitialCurrentLineL()
       
   304     {
       
   305     ActivateFirstPageL();
       
   306 
       
   307     TBool textualInput = IntResourceValueL( RSC_CHAT_VARIATION_IMPSCU_USERID_TEXTUAL_INPUTMODE );
       
   308 
       
   309     if ( ! textualInput )
       
   310         {
       
   311         // default is textual mode, but now we must switch to numeric mode
       
   312         CAknMultilineQueryControl* ctrl = FirstControl(); // user id field
       
   313         CCoeControl* coeCtrl = ctrl->ControlByLayoutOrNull( EMultiDataFirstEdwin );
       
   314         if ( coeCtrl )
       
   315             {
       
   316             CEikEdwin* edwin = static_cast<CEikEdwin*>( coeCtrl );
       
   317             edwin->SetAknEditorInputMode( EAknEditorNumericInputMode );
       
   318             }
       
   319         }
       
   320 
       
   321     if ( iCursorToBeginning )
       
   322         {
       
   323         CAknMultilineQueryControl* ctrl = FirstControl(); // user id field
       
   324         CCoeControl* coeCtrl = ctrl->ControlByLayoutOrNull( EMultiDataFirstEdwin );
       
   325         if ( coeCtrl )
       
   326             {
       
   327             CEikEdwin* firstEdwin = static_cast<CEikEdwin*>( coeCtrl );
       
   328             firstEdwin->AddFlagToUserFlags( CEikEdwin::ENoAutoSelection );
       
   329             }
       
   330         }
       
   331 
       
   332     if ( Layout_Meta_Data::IsLandscapeOrientation() )
       
   333         {
       
   334         // Show the OK-softkey also when password field is empty.
       
   335         UpdateLeftSoftKeyL();
       
   336         }
       
   337     else if ( iInitialFocus == ESecondLine )
       
   338         {
       
   339         TryChangeFocusToL( EMultilineSecondLine );
       
   340         }
       
   341     }
       
   342 
       
   343 
       
   344 
       
   345 // -----------------------------------------------------------------------------
       
   346 // CCnUiUsernameAndPwdQuery::OpenResourceFileLC()
       
   347 // !!!Notice!!!. Two variables in cleanupstack after call of this method.
       
   348 // -----------------------------------------------------------------------------
       
   349 //
       
   350 void CCnUiUsernameAndPwdQuery::OpenResourceFileLC( RResourceFile& aResourceFile )
       
   351     {
       
   352     TFileName resourceFileName;
       
   353     iRFs.Close();
       
   354     User::LeaveIfError( iRFs.Connect() );
       
   355 
       
   356     TInt err = iSharedData->GetStringKey( ( TIMPSSharedKeys )KBrandingResourceKey, resourceFileName );
       
   357     if ( err || !resourceFileName.Length() )
       
   358         {
       
   359         CnUiResourceFileName::NearestVariationForCurrentLanguage( iRFs, resourceFileName  );
       
   360         }
       
   361     aResourceFile.OpenL( iRFs, resourceFileName );
       
   362     CleanupClosePushL( aResourceFile );
       
   363     aResourceFile.ConfirmSignatureL();
       
   364     }
       
   365 
       
   366 
       
   367 
       
   368 // -----------------------------------------------------------------------------
       
   369 // CCnUiUsernameAndPwdQuery::IntResourceValueL()
       
   370 // -----------------------------------------------------------------------------
       
   371 //
       
   372 TInt CCnUiUsernameAndPwdQuery::IntResourceValueL( TInt aResourceId )
       
   373     {
       
   374 
       
   375     TInt val( 0 );
       
   376     TInt err ( KErrNone );
       
   377 
       
   378     CRepository* rep = 0;
       
   379 
       
   380     TRAP( err, rep = CRepository::NewL( KCRUidIMNG ) );
       
   381 
       
   382     if ( err == KErrNone )
       
   383         {
       
   384         TInt key = aResourceId + KIMCUStartVariationID;
       
   385 
       
   386         err = rep->Get( key, val );
       
   387 
       
   388         delete rep;
       
   389         }
       
   390 
       
   391     if ( err != KErrNone )
       
   392         {
       
   393 
       
   394         RResourceFile resFile;
       
   395         OpenResourceFileLC( resFile ); // Two items in cleanup stack.
       
   396 
       
   397         aResourceId = aResourceId + RSC_CRRSS_CHAT_VARIATION_IMPSCU_START_ID;
       
   398 
       
   399         // read the data to a buffer
       
   400         TInt plainResourceId = 0x00000fff & aResourceId; // Remove offset from id
       
   401         HBufC8* rawDataBuf = resFile.AllocReadLC( plainResourceId );
       
   402 
       
   403         // it's now as ascii code: \x00 for 0, \x01 for 1, etc.
       
   404         TUint value = ( *rawDataBuf )[ 0 ];
       
   405 
       
   406         CleanupStack::PopAndDestroy( 2 ); // rawDataBuf, resFile
       
   407 
       
   408         val = value;
       
   409         }
       
   410 
       
   411     return val;
       
   412 
       
   413     }
       
   414 
       
   415 
       
   416 //  End of File