usbuis/usbuinotif/src/usbuincableconnectednotifier.cpp
changeset 34 7858bc6ead78
parent 31 dfdd8240f7c8
child 35 9d8b04ca6939
equal deleted inserted replaced
31:dfdd8240f7c8 34:7858bc6ead78
     1 /*
       
     2  * Copyright (c) 2005-2009 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:  USB Cable Connected notifier class.
       
    15  *
       
    16  */
       
    17 // INCLUDE FILES
       
    18 #include <eikenv.h>          // Eikon environment
       
    19 #include <bautils.h>         // BAFL utils (for language file)
       
    20 #include <aknlists.h>
       
    21 #include <aknPopup.h> 
       
    22 #include <StringLoader.h>    // Localisation stringloader
       
    23 #include <utf.h>             // Unicode character conversion utilities
       
    24 #include <usbman.h>
       
    25 #include <usbuinotif.rsg>                   // Own resources
       
    26 #include <centralrepository.h> 
       
    27 #include <e32property.h>
       
    28 #include <e32uid.h> // KExecutableImageUid
       
    29 #include "usbuincableconnectednotifier.h"   // Own class definition
       
    30 #include "usbuinotifdebug.h"                // Debugging macros
       
    31 #include "UsbWatcherInternalCRKeys.h"
       
    32 #include "usbuinotif.hrh"
       
    33 
       
    34 // Literals
       
    35 
       
    36 _LIT(KUSBExe, "usbapplication.exe");
       
    37 const TInt KUSBUIUid = 0x2002BCA3;
       
    38 _LIT(KFileDrive,"z:");
       
    39 _LIT(KUSBUIconFileName, "usbui.mif");
       
    40 
       
    41 // ================= MEMBER FUNCTIONS =========================================
       
    42 
       
    43 // ----------------------------------------------------------------------------
       
    44 // CUSBUICableConnectedNotifier::NewL
       
    45 // Two-phased constructor.
       
    46 // ----------------------------------------------------------------------------
       
    47 //
       
    48 CUSBUICableConnectedNotifier* CUSBUICableConnectedNotifier::NewL()
       
    49     {
       
    50     FLOG(_L("[USBUINOTIF]\t CUSBUICableConnectedNotifier::NewL"));
       
    51     CUSBUICableConnectedNotifier* self =
       
    52             new (ELeave) CUSBUICableConnectedNotifier();
       
    53     CleanupStack::PushL(self);
       
    54     self->ConstructL();
       
    55     CleanupStack::Pop(self);
       
    56     FLOG(_L("[USBUINOTIF]\t CUSBUICableConnectedNotifier::NewL completed"));
       
    57     return self;
       
    58     }
       
    59 
       
    60 // ----------------------------------------------------------------------------
       
    61 // CUSBUICableConnectedNotifier::CUSBUICableConnectedNotifier
       
    62 // C++ default constructor can NOT contain any code, that
       
    63 // might leave.
       
    64 // ----------------------------------------------------------------------------
       
    65 //
       
    66 CUSBUICableConnectedNotifier::CUSBUICableConnectedNotifier()
       
    67     {
       
    68     FLOG(_L("[USBUINOTIF]\t CUSBUICableConnectedNotifier::default constructor"));
       
    69     }
       
    70 
       
    71 // ----------------------------------------------------------------------------
       
    72 // Destructor
       
    73 // ----------------------------------------------------------------------------
       
    74 //
       
    75 CUSBUICableConnectedNotifier::~CUSBUICableConnectedNotifier()
       
    76     {
       
    77     FLOG(_L("[USBUINOTIF]\t CUSBUICableConnectedNotifier::destructor"));
       
    78     //Make sure that the request is completed. Note that inside the destructor,
       
    79     //this virtual function call is to the local CUSBUICableConnectedNotifier::
       
    80     //Cancel, not to any possibly derived class implementation. 
       
    81     Cancel();
       
    82     delete iDialog;
       
    83          
       
    84     FLOG(_L("[USBUINOTIF]\t CUSBUICableConnectedNotifier::destructor completed"));
       
    85     }
       
    86 
       
    87 // ----------------------------------------------------------------------------
       
    88 // CUSBUICableConnectedNotifier::RegisterL
       
    89 // Register notifier.
       
    90 // ----------------------------------------------------------------------------
       
    91 //
       
    92 CUSBUICableConnectedNotifier::TNotifierInfo CUSBUICableConnectedNotifier::RegisterL()
       
    93     {
       
    94     FLOG(_L("[USBUINOTIF]\t CUSBUICableConnectedNotifier::RegisterL"));
       
    95     iInfo.iUid = KCableConnectedNotifierUid;
       
    96     iInfo.iChannel = KQueriesNotifier; // work in the same channel with the other 
       
    97     // queries so that we can use priorities
       
    98     iInfo.iPriority = ENotifierPriorityLow; // must be smaller than queries notifier
       
    99     FLOG(_L("[USBUINOTIF]\t CUSBUICableConnectedNotifier::RegisterL completed"));
       
   100     return iInfo;
       
   101     }
       
   102 
       
   103 // ----------------------------------------------------------------------------
       
   104 // CUSBUICableConnectedNotifier::GetParamsL
       
   105 //  Jump to RunL as soon as possible.
       
   106 // ----------------------------------------------------------------------------
       
   107 //
       
   108 void CUSBUICableConnectedNotifier::GetParamsL(const TDesC8& /*aBuffer*/,
       
   109         TInt aReplySlot, const RMessagePtr2& aMessage)
       
   110     {
       
   111     FLOG(_L("[USBUINOTIF]\t CUSBUICableConnectedNotifier::GetParamsL"));
       
   112     if ( iReplySlot != 0 || iNeedToCompleteMessage)
       
   113         {
       
   114         User::Leave(KErrInUse);
       
   115         }
       
   116 
       
   117     iMessage = aMessage;
       
   118     iNeedToCompleteMessage = ETrue;
       
   119     iReplySlot = aReplySlot;
       
   120 
       
   121     // Call SetActive() so RunL() will be called by the active scheduler
       
   122     //
       
   123     iStatus = KRequestPending;
       
   124     TRequestStatus* stat = &iStatus;
       
   125     SetActive();
       
   126     User::RequestComplete(stat, KErrNone);
       
   127     FLOG(_L("[USBUINOTIF]\t CUSBUICableConnectedNotifier::GetParamsL() completed"));
       
   128     }
       
   129 
       
   130 // ----------------------------------------------------------------------------
       
   131 // CUSBUICableConnectedNotifier::RunL
       
   132 // Ask user response and return it to caller.
       
   133 // ----------------------------------------------------------------------------
       
   134 //
       
   135 void CUSBUICableConnectedNotifier::RunL()
       
   136     {
       
   137     FLOG(_L("[USBUINOTIF]\t CUSBUICableConnectedNotifier::RunL"));
       
   138 
       
   139     DisableKeylock();
       
   140     SuppressAppSwitching(ETrue);
       
   141     RunQueryL();
       
   142     SuppressAppSwitching(EFalse);
       
   143     RestoreKeylock();
       
   144 
       
   145     FLOG(_L("[USBUINOTIF]\t CUSBUICableConnectedNotifier::RunL() completed"));
       
   146     }
       
   147 
       
   148 // ----------------------------------------------------------------------------
       
   149 // CUSBUICableConnectedNotifier::Cancel()
       
   150 // Release all own resources (member variables)
       
   151 // ----------------------------------------------------------------------------
       
   152 //
       
   153 void CUSBUICableConnectedNotifier::Cancel()
       
   154     {
       
   155     FLOG(_L("[USBUINOTIF]\t CUSBUICableConnectedNotifier::Cancel() "));
       
   156 
       
   157     iDialog->Cancel(); // cancel the dialog, if it is active
       
   158     CompleteMessage(KErrCancel);
       
   159     CUSBUINotifierBase::Cancel();
       
   160 
       
   161     FLOG(_L("[USBUINOTIF]\t CUSBUICableConnectedNotifier::Cancel() completed"));
       
   162     }
       
   163 
       
   164 // -----------------------------------------------------------------------------------------------------------
       
   165 // CUSBUICableConnectedNotifier::GetPersonalityStringL
       
   166 // Get the strings for ask on connection message query 
       
   167 // -----------------------------------------------------------------------------------------------------------
       
   168 //
       
   169 void CUSBUICableConnectedNotifier::GetPersonalityStringLC(
       
   170        HBufC*& aHeader,HBufC*& aDescription )
       
   171     {
       
   172     FLOG(_L("[USBUINOTIF]\t CUSBUICableConnectedNotifier::GetPersonalityStringL"));
       
   173     TInt CurrentPersonalityID = 0; // The ID of current USB mode
       
   174     GetCurrentIdL(CurrentPersonalityID);
       
   175     RUsb usbman;
       
   176     User::LeaveIfError(usbman.Connect());
       
   177     CleanupClosePushL(usbman);
       
   178     User::LeaveIfError(usbman.GetDescription(CurrentPersonalityID,
       
   179             aDescription));
       
   180     CleanupStack::PopAndDestroy(&usbman);
       
   181     CleanupStack::PushL(aDescription);
       
   182     aHeader = StringLoader::LoadL(R_USB_MODE_MSG_HEADER);
       
   183     CleanupStack::PushL(aHeader);
       
   184     FLOG(_L("[USBUINOTIF]\t CUSBUICableConnectedNotifier::GetPersonalityStringL completed"));
       
   185     }
       
   186 
       
   187 // -----------------------------------------------------------------------------------------------------------
       
   188 // CUSBUICableConnectedNotifier::RunQueryL
       
   189 // Run the ask on connection message query
       
   190 // -----------------------------------------------------------------------------------------------------------
       
   191 //
       
   192 void CUSBUICableConnectedNotifier::RunQueryL()
       
   193     {
       
   194     FLOG( _L("[USBUINOTIF]\t CUSBUICableConnectedNotifier::RunQueryL()"));
       
   195  
       
   196     _LIT(KNotificationType,"com.nokia.hb.devicenotificationdialog/1.0");
       
   197     _LIT(KTouchActivation, "touchActivation");
       
   198     _LIT(KIconName, "iconName");
       
   199     _LIT(KTitle, "title");
       
   200     _LIT(KText, "text");
       
   201 
       
   202     HBufC* header = NULL;
       
   203     HBufC* description =NULL;   
       
   204     GetPersonalityStringLC(header, description);
       
   205    
       
   206     TFileName usbUiIconFilename( KFileDrive );
       
   207     usbUiIconFilename += KDC_APP_BITMAP_DIR;
       
   208     usbUiIconFilename += KUSBUIconFileName;
       
   209     if( iDialog )
       
   210         {
       
   211         delete iDialog;
       
   212         iDialog = NULL;
       
   213         }
       
   214     iDialog = CHbDeviceDialogSymbian::NewL();
       
   215     CHbSymbianVariantMap *parameters =CHbSymbianVariantMap::NewL();
       
   216     CleanupStack::PushL(parameters);
       
   217 
       
   218     AddParameterL(parameters, KTitle, header, CHbSymbianVariant::EDes);
       
   219     AddParameterL(parameters, KText, description, CHbSymbianVariant::EDes);
       
   220     AddParameterL(parameters, KIconName, &usbUiIconFilename, CHbSymbianVariant::EDes);
       
   221     TBool activation=ETrue;
       
   222     AddParameterL(parameters, KTouchActivation, &activation, CHbSymbianVariant::EBool);
       
   223 
       
   224     FLOG( _L("[USBUINOTIF]\t CUSBUICableConnectedNotifier::RunQueryL() show") );     
       
   225     TBool error = iDialog->Show(KNotificationType ,*parameters, this);
       
   226     FTRACE( FPrint(
       
   227         _L( "[USBUINOTIF]\t CUSBUICableConnectedNotifier::RunQueryL() error = %d" ),
       
   228         error ) );
       
   229 
       
   230     CleanupStack::PopAndDestroy(parameters);    
       
   231     CleanupStack::PopAndDestroy(header);
       
   232     CleanupStack::PopAndDestroy(description);
       
   233     
       
   234     FLOG(_L("[USBUINOTIF]\t CUSBUICableConnectedNotifier::RunQueryL completed"));
       
   235     }
       
   236 // ----------------------------------------------------------------------------
       
   237 // CUSBUICableConnectedNotifier::GetCurrentIdL
       
   238 // get the current personality id
       
   239 // ----------------------------------------------------------------------------
       
   240 //
       
   241 void CUSBUICableConnectedNotifier::GetCurrentIdL(TInt& aCurrentPersonality)
       
   242     {
       
   243     FLOG(_L("[USBUINOTIF]\t CUSBUICableConnectedNotifier::GetCurrentIdL"));
       
   244     // Connecting and initialization:
       
   245     CRepository* repository = CRepository::NewL(KCRUidUsbWatcher);
       
   246     CleanupStack::PushL(repository);
       
   247     // Get the current USB mode
       
   248     TInt ret = repository->Get(KUsbWatcherPersonality, aCurrentPersonality);
       
   249     FTRACE( FPrint(
       
   250         _L( "[USBWATCHER]\t CUSBUICableConnectedNotifier::GetCurrentIdL() ret = %d" ),
       
   251         ret ) );    
       
   252     CleanupStack::PopAndDestroy(repository);
       
   253     FLOG(_L("[USBUINOTIF]\t CUSBUICableConnectedNotifier::GetCurrentIdL completed"));
       
   254     }
       
   255 
       
   256 // ----------------------------------------------------------------------------
       
   257 // CUSBUICableConnectedNotifier::DataReceived
       
   258 // launches the QT usb ui setting
       
   259 // ----------------------------------------------------------------------------
       
   260 //
       
   261 void CUSBUICableConnectedNotifier::DataReceived(CHbSymbianVariantMap& aData)
       
   262     {
       
   263     FLOG(_L("[USBUINOTIF]\t CUSBUICableConnectedNotifier::DataReceived"));
       
   264     _LIT(KResult, "result");
       
   265     _LIT(KActivated, "activated");
       
   266     TInt result = 0;
       
   267     MDesCArray& keys = aData.Keys();
       
   268     HBufC* key = NULL;
       
   269     for(TInt i = 0; i < keys.MdcaCount(); i++)
       
   270         {
       
   271         result = keys.MdcaPoint(i).Compare(KResult); 
       
   272         if (result == 0)
       
   273             {        
       
   274             key = HBufC::New(KResult().Length());
       
   275             if (key)
       
   276                 {
       
   277                 *key = KResult;            
       
   278                 const CHbSymbianVariant* variant = aData.Get(*key);
       
   279                 delete key;
       
   280                 HBufC* value = static_cast<HBufC*>(variant->Data());
       
   281                 result = value->Des().Compare(KActivated);
       
   282                 if (result == 0)
       
   283                     {
       
   284                     TUidType uidtype(KExecutableImageUid, TUid::Uid(0x00),
       
   285                             TUid::Uid(KUSBUIUid));
       
   286                     LaunchApplication(KUSBExe(), uidtype);
       
   287                     }
       
   288                 break;
       
   289                 }
       
   290             }            
       
   291         }
       
   292     }
       
   293 
       
   294 // ----------------------------------------------------------------------------
       
   295 // CUSBUICableConnectedNotifier::DeviceDialogClosed
       
   296 // ----------------------------------------------------------------------------
       
   297 //      
       
   298  void CUSBUICableConnectedNotifier::DeviceDialogClosed(TInt aCompletionCode)
       
   299      {
       
   300      FLOG(_L("[USBUINOTIF]\t CUSBUICableConnectedNotifier::DeviceDialogClosed()"));
       
   301      CompleteMessage(aCompletionCode);  
       
   302      }
       
   303       
       
   304 // ---------------------------------------------------------------------------
       
   305 // CUSBUICableConnectedNotifier::LaunchApplication()
       
   306 // launches the application 
       
   307 // ---------------------------------------------------------------------------
       
   308 //  
       
   309 void CUSBUICableConnectedNotifier::LaunchApplication(const TDesC & aProcessName, 
       
   310         const TUidType & aUidType) const
       
   311     {                 
       
   312     FLOG(_L("[USBUINOTIF]\t CUSBUICableConnectedNotifier::LaunchApplication()"));
       
   313     RProcess usbUiProcess;                
       
   314     TInt result = usbUiProcess.Create(aProcessName, KNullDesC, aUidType);
       
   315     FTRACE( FPrint(
       
   316         _L( "[USBUINOTIF]\t CUSBUICableConnectedNotifier::LaunchApplication() result = %d" ),
       
   317         result ) );    
       
   318     usbUiProcess.Resume();
       
   319     usbUiProcess.Close();                     
       
   320     FLOG(_L("[USBUINOTIF]\t CUSBUICableConnectedNotifier::LaunchApplication() completed"));             
       
   321     }
       
   322     
       
   323 // ---------------------------------------------------------------------------
       
   324 // CUSBUICableConnectedNotifier::AddParameter()
       
   325 // Adds a string parameter to the dialog parameters.
       
   326 // ---------------------------------------------------------------------------
       
   327 //  
       
   328 void CUSBUICableConnectedNotifier::AddParameterL(
       
   329         CHbSymbianVariantMap* aParameters,
       
   330         const TDesC& aKey,
       
   331         const TAny* aData,
       
   332         CHbSymbianVariant::TType aDataType)
       
   333     {
       
   334     FLOG( _L("[USBUINOTIF]\t CUSBUICableConnectedNotifier::AddStringParameter()") );
       
   335     FTRACE( FPrint(
       
   336         _L( "[USBWATCHER]\t CUSBUICableConnectedNotifier::RunQueryL() aKey = %S" ),
       
   337         &aKey ) );
       
   338     CHbSymbianVariant* variant = CHbSymbianVariant::NewL(aData, aDataType);
       
   339     //aParameters takes variant ownership
       
   340     User::LeaveIfError(aParameters->Add(aKey, variant));
       
   341     FLOG( _L("[USBUINOTIF]\t CUSBUICableConnectedNotifier::AddStringParameter() completed") );
       
   342     }
       
   343 
       
   344 // End of File