phoneapp/phoneuiview/src/cphonekeycapturecontroller.cpp
branchRCL_3
changeset 25 5266b1f337bd
parent 0 5f000ab63145
equal deleted inserted replaced
24:41a7f70b3818 25:5266b1f337bd
       
     1 /*
       
     2 * Copyright (c) 2005 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: Implementation of CPhoneKeyCaptureController class.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <eikenv.h>
       
    21 #include <w32std.h>
       
    22 #include "cphonekeycapturecontroller.h"
       
    23 #include "tphonecmdparamKeycapture.h"
       
    24 #include "phoneconstants.h"
       
    25 #include "phonelogger.h"
       
    26 
       
    27 // ================= MEMBER FUNCTIONS =======================
       
    28 // C++ default constructor can NOT contain any code, that
       
    29 // might leave.
       
    30 //
       
    31 CPhoneKeyCaptureController::CPhoneKeyCaptureController( CEikonEnv& aEikEnv ): iEikEnv( aEikEnv )
       
    32     {
       
    33     }
       
    34 
       
    35 // ---------------------------------------------------------
       
    36 // CPhoneKeyCaptureController::NewL()
       
    37 // Two-phased constructor
       
    38 // (other items were commented in a header).
       
    39 // ---------------------------------------------------------
       
    40 CPhoneKeyCaptureController* CPhoneKeyCaptureController::NewL( CEikonEnv& aEikEnv )
       
    41     {
       
    42     CPhoneKeyCaptureController* self = 
       
    43         new( ELeave ) CPhoneKeyCaptureController( aEikEnv );
       
    44     
       
    45     return self;
       
    46     }
       
    47 
       
    48 // ---------------------------------------------------------
       
    49 // Destructor
       
    50 // ---------------------------------------------------------
       
    51 // 
       
    52 CPhoneKeyCaptureController::~CPhoneKeyCaptureController()
       
    53     {
       
    54     // should we have any captured keys, stop capturing now:
       
    55     TInt capturedKeyCount = iCapturedKeys.Count();
       
    56     for( TInt i = 0; i < capturedKeyCount ; i++ )
       
    57         {
       
    58         StopKeyCapture( iCapturedKeys[i] );
       
    59         }
       
    60 
       
    61     iCapturedKeys.Close();
       
    62     }
       
    63 
       
    64 // ---------------------------------------------------------
       
    65 // CPhoneKeyCaptureController::StartCapturingKey
       
    66 // Starts capturing a key
       
    67 // (other items were commented in a header).
       
    68 // ---------------------------------------------------------
       
    69 //      
       
    70 void CPhoneKeyCaptureController::StartCapturingKey( 
       
    71     TPhoneCommandParam* aCommandParam ) 
       
    72     {
       
    73     __LOGMETHODSTARTEND(EPhoneUIView, "CPhoneKeyCaptureController::StartCapturingKey");
       
    74     TPhoneCmdParamKeyCapture* keyCaptureParam = 
       
    75         static_cast<TPhoneCmdParamKeyCapture*>( aCommandParam );
       
    76 
       
    77     TInt32 handle = KErrNotFound;
       
    78     TPhoneKeyCapture keyCapture;
       
    79     
       
    80     RWindowGroup& windowGroup = iEikEnv.RootWin();
       
    81 
       
    82     if ( !IsKeyCaptured ( keyCaptureParam->Key() ) )
       
    83         {
       
    84         __PHONELOG1( 
       
    85             EBasic, 
       
    86             EPhoneUIView,
       
    87             "Capture key %d here ", 
       
    88             static_cast<TInt>( keyCaptureParam->Key() ) );
       
    89 
       
    90         keyCapture.iKey = keyCaptureParam->Key();
       
    91         keyCapture.iKeyCode = keyCaptureParam->KeyCode();
       
    92         keyCapture.iHandle = 0; // set as initial value
       
    93         keyCapture.iHandleForUpAndDown = 0; // set as initial value
       
    94 
       
    95         switch( keyCaptureParam->CaptureType() )
       
    96             {
       
    97             case EPhoneKeyEvent:
       
    98                 keyCapture.iType = EPhoneKeyCaptureEvent;
       
    99                 keyCapture.iHandle = windowGroup.CaptureKey( 
       
   100                     keyCapture.iKeyCode, 0, 0 );
       
   101                 handle = keyCapture.iHandle;
       
   102                 break;
       
   103             case EPhoneKeyUpAndDownEvents:
       
   104                 keyCapture.iType = EPhoneKeyCaptureUpAndDownEvents;
       
   105                 keyCapture.iHandleForUpAndDown = 
       
   106                     windowGroup.CaptureKeyUpAndDowns( 
       
   107                     keyCapture.iKey, 0, 0 );
       
   108                 handle = keyCapture.iHandleForUpAndDown;
       
   109                 break;
       
   110             default: // EPhoneKeyAllEvents
       
   111                 {
       
   112                 keyCapture.iType = EPhoneKeyCaptureAllEvents;
       
   113                 keyCapture.iHandle = windowGroup.CaptureKey( 
       
   114                     keyCapture.iKeyCode, 0, 0 );
       
   115                 if ( keyCapture.iHandle >= 0 )
       
   116                     {
       
   117                     keyCapture.iHandleForUpAndDown = 
       
   118                         windowGroup.CaptureKeyUpAndDowns( 
       
   119                         keyCapture.iKey, 0, 0 );
       
   120 
       
   121                     if ( keyCapture.iHandleForUpAndDown < 0 )
       
   122                         {
       
   123                         windowGroup.CancelCaptureKey( keyCapture.iHandle );
       
   124                         }
       
   125 
       
   126                     handle = keyCapture.iHandleForUpAndDown;
       
   127                     }   
       
   128                 break;
       
   129                 }
       
   130             } // switch
       
   131 
       
   132         if ( handle >= 0 )
       
   133             {
       
   134             if ( iCapturedKeys.Append( keyCapture ) != KErrNone )
       
   135                 {
       
   136                 StopKeyCapture( keyCapture );
       
   137                 }
       
   138             }
       
   139         }   // if isKeyCaptured
       
   140     }
       
   141 
       
   142 // ---------------------------------------------------------
       
   143 // CPhoneKeyCaptureController::StopCapturingKey
       
   144 // Stops capturing a key
       
   145 // (other items were commented in a header).
       
   146 // ---------------------------------------------------------
       
   147 //
       
   148 void CPhoneKeyCaptureController::StopCapturingKey( 
       
   149     TPhoneCommandParam* aCommandParam ) 
       
   150     {
       
   151     __LOGMETHODSTARTEND(EPhoneUIView, "CPhoneKeyCaptureController::StopCapturingKey");
       
   152     TPhoneCmdParamKeyCapture* keyCaptureParam = 
       
   153         static_cast<TPhoneCmdParamKeyCapture*>( aCommandParam );
       
   154 
       
   155     const TInt capturedKeyCount = iCapturedKeys.Count();
       
   156 
       
   157     __PHONELOG2( 
       
   158         EBasic, 
       
   159         EPhoneUIView,
       
   160         "UnCapture key %d here (%d keys left)",
       
   161         static_cast<TInt>( keyCaptureParam->Key() ), 
       
   162         static_cast<TInt>( capturedKeyCount ) );
       
   163 
       
   164     TBool foundKey = EFalse;
       
   165 
       
   166     for( TInt i = 0; ( i < capturedKeyCount ) && !foundKey ; i++ )
       
   167         {
       
   168         if( iCapturedKeys[i].iKey == keyCaptureParam->Key() )
       
   169             {
       
   170             foundKey = ETrue;
       
   171             StopKeyCapture( iCapturedKeys[i] );
       
   172             iCapturedKeys.Remove(i);
       
   173             }
       
   174         }
       
   175     }
       
   176 
       
   177 // ---------------------------------------------------------
       
   178 // CPhoneKeyCaptureController::IsKeyCaptured
       
   179 // May be used to ask whether a key has been set to be captured
       
   180 // (other items were commented in a header).
       
   181 // ---------------------------------------------------------
       
   182 //      
       
   183 TBool CPhoneKeyCaptureController::IsKeyCaptured( TStdScanCode aKey ) const
       
   184     {
       
   185     const TInt capturedKeyCount = iCapturedKeys.Count();
       
   186     TBool isCaptured = EFalse;
       
   187 
       
   188     for( TInt i = 0 ; ( i < capturedKeyCount ) && !isCaptured; i++ )
       
   189         {
       
   190         isCaptured = iCapturedKeys[i].iKey == aKey; 
       
   191         }
       
   192 
       
   193     return isCaptured;
       
   194     }
       
   195 
       
   196 // ---------------------------------------------------------
       
   197 // CPhoneKeyCaptureController::StopKeyCapture
       
   198 // Stops capturing a key
       
   199 // (other items were commented in a header).
       
   200 // ---------------------------------------------------------
       
   201 //      
       
   202 void CPhoneKeyCaptureController::StopKeyCapture( TPhoneKeyCapture aKeyCapture )
       
   203     {
       
   204     RWindowGroup& windowGroup = iEikEnv.RootWin();
       
   205 
       
   206     switch ( aKeyCapture.iType )
       
   207         {
       
   208         case EPhoneKeyCaptureEvent:
       
   209             windowGroup.CancelCaptureKey( aKeyCapture.iHandle );
       
   210             break;
       
   211         case EPhoneKeyCaptureUpAndDownEvents:
       
   212             windowGroup.CancelCaptureKeyUpAndDowns( 
       
   213                 aKeyCapture.iHandleForUpAndDown );
       
   214             break;
       
   215         default: // EPhoneKeyCaptureAllEvents
       
   216             {
       
   217             windowGroup.CancelCaptureKey( aKeyCapture.iHandle );
       
   218             windowGroup.CancelCaptureKeyUpAndDowns( 
       
   219                 aKeyCapture.iHandleForUpAndDown );
       
   220             break;
       
   221             }
       
   222         }
       
   223     }
       
   224 
       
   225 // end of file