bluetoothengine/btnotif/btnotifsrv/src/bluetoothnotification.cpp
branchRCL_3
changeset 56 9386f31cc85b
parent 55 613943a21004
child 61 269724087bed
equal deleted inserted replaced
55:613943a21004 56:9386f31cc85b
     1 /*
       
     2 * Copyright (c) 2010 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: Description: Class for managing an actual user notification or query.
       
    15 *              It hides UI framework-specifics in a private class.
       
    16 *
       
    17 */
       
    18 
       
    19 #include "bluetoothnotification.h"
       
    20 #include <hb/hbcore/hbsymbianvariant.h>
       
    21 #include "btnotificationmanager.h"
       
    22 #include "btnotifserver.h"
       
    23 #include "bluetoothtrace.h"
       
    24 #include <utf.h>  // for debugging
       
    25 
       
    26 /**  Identifier of Bluetooth device dialog plug-in. */
       
    27 _LIT( KBTDevDialogId, "com.nokia.hb.btdevicedialog/1.0" );
       
    28 /**  Key name of result. */
       
    29 _LIT( KBTDevDialogResult, "result" );
       
    30 _LIT( KBTDevDialogInput, "input" );
       
    31 
       
    32 // ======== MEMBER FUNCTIONS ========
       
    33 
       
    34 // ---------------------------------------------------------------------------
       
    35 // C++ default constructor
       
    36 // ---------------------------------------------------------------------------
       
    37 //
       
    38 CBluetoothNotification::CBluetoothNotification( CBTNotificationManager* aManager )
       
    39 :   iManager( aManager )
       
    40     {
       
    41     }
       
    42 
       
    43 
       
    44 // ---------------------------------------------------------------------------
       
    45 // Symbian 2nd-phase constructor
       
    46 // ---------------------------------------------------------------------------
       
    47 //
       
    48 void CBluetoothNotification::ConstructL()
       
    49     {
       
    50     iNotificationData = CHbSymbianVariantMap::NewL();
       
    51     iDialog = CHbDeviceDialogSymbian::NewL();
       
    52     }
       
    53 
       
    54 
       
    55 // ---------------------------------------------------------------------------
       
    56 // NewL.
       
    57 // ---------------------------------------------------------------------------
       
    58 //
       
    59 CBluetoothNotification* CBluetoothNotification::NewL( 
       
    60     CBTNotificationManager* aManager )
       
    61     {
       
    62 	BOstraceFunctionEntry0( DUMMY_DEVLIST );
       
    63     CBluetoothNotification* self = new( ELeave ) CBluetoothNotification( aManager );
       
    64     CleanupStack::PushL( self );
       
    65     self->ConstructL();
       
    66     CleanupStack::Pop( self );
       
    67 	BOstraceFunctionExit0( DUMMY_DEVLIST );
       
    68     return self;
       
    69     }
       
    70 
       
    71 
       
    72 // ---------------------------------------------------------------------------
       
    73 // Destructor
       
    74 // ---------------------------------------------------------------------------
       
    75 //
       
    76 CBluetoothNotification::~CBluetoothNotification()
       
    77 {
       
    78     BOstraceFunctionEntry1( DUMMY_DEVLIST, this );
       
    79     delete iDialog;
       
    80     delete iNotificationData;
       
    81     delete iReturnData;
       
    82 	BOstraceFunctionExit1( DUMMY_DEVLIST, this )
       
    83 }
       
    84 
       
    85 // ---------------------------------------------------------------------------
       
    86 // Sets the data to be shown to the user.
       
    87 // ---------------------------------------------------------------------------
       
    88 //
       
    89 TInt CBluetoothNotification::SetData( TInt aDataType, const TDesC& aData )
       
    90     {
       
    91     TRAPD( err, SetDataL( aDataType, aData ) );
       
    92     return (int) err;
       
    93     }
       
    94 
       
    95 
       
    96 // ---------------------------------------------------------------------------
       
    97 // Sets the data to be shown to the user.
       
    98 // ---------------------------------------------------------------------------
       
    99 //
       
   100 TInt CBluetoothNotification::SetData( TInt aDataType, TInt aData )
       
   101     {
       
   102     TRAPD( err, SetDataL( aDataType, aData ) );
       
   103     return (int) err;
       
   104     }
       
   105 
       
   106 // ---------------------------------------------------------------------------
       
   107 // Updates the data to be shown to the user.
       
   108 // ---------------------------------------------------------------------------
       
   109 //
       
   110 TInt CBluetoothNotification::Update( const TDesC& aData )
       
   111     {
       
   112     BOstraceFunctionEntry1( DUMMY_DEVLIST, this );
       
   113     (void) aData;
       
   114     int ret = iDialog->Update( *iNotificationData );
       
   115     delete iNotificationData;
       
   116     iNotificationData = NULL;
       
   117     TRAP( ret, iNotificationData = CHbSymbianVariantMap::NewL() );
       
   118     BOstraceFunctionExit1( DUMMY_DEVLIST, this );
       
   119     return ret;
       
   120     }
       
   121 
       
   122 
       
   123 // ---------------------------------------------------------------------------
       
   124 // Show the notification, which means that it is added to the queue.
       
   125 // ---------------------------------------------------------------------------
       
   126 //
       
   127 void CBluetoothNotification::ShowL()
       
   128     {
       
   129 	BOstraceFunctionEntry1( DUMMY_DEVLIST, this );
       
   130     SetDataL( TBluetoothDialogParams::EDialogType, iType );
       
   131     SetDataL( TBluetoothDialogParams::EResource, iResourceId );
       
   132     delete iReturnData;
       
   133     iReturnData = NULL;
       
   134     iReturnData = CHbSymbianVariantMap::NewL();
       
   135     iDialog->Show( KBTDevDialogId(), *iNotificationData, this );
       
   136     delete iNotificationData;
       
   137     iNotificationData = NULL;
       
   138     iNotificationData = CHbSymbianVariantMap::NewL();
       
   139     BOstraceFunctionExit1( DUMMY_DEVLIST, this );
       
   140     }
       
   141 
       
   142 // ---------------------------------------------------------------------------
       
   143 // Stop showing the notification.
       
   144 // ---------------------------------------------------------------------------
       
   145 //
       
   146 TInt CBluetoothNotification::Close()
       
   147     {
       
   148     BOstraceFunctionEntry1( DUMMY_DEVLIST, this );
       
   149     iDialog->Cancel();
       
   150     iManager->ReleaseNotification( this );
       
   151 	BOstraceFunctionExit0( DUMMY_DEVLIST );
       
   152     return KErrNone;
       
   153     }
       
   154 
       
   155 
       
   156 // ---------------------------------------------------------------------------
       
   157 // Sets the data to be shown to the user.
       
   158 // ---------------------------------------------------------------------------
       
   159 //
       
   160 void CBluetoothNotification::SetDataL( TInt aType, const TDesC& aData )
       
   161     {
       
   162     BOstraceFunctionEntryExt( DUMMY_DEVLIST, this, aType );
       
   163     TBuf16<6> key;
       
   164     CHbSymbianVariant* value = NULL;
       
   165     switch( aType )
       
   166         {
       
   167         case TBluetoothDialogParams::EAddress:
       
   168         case TBluetoothDeviceDialog::EDeviceName:
       
   169         case TBluetoothDeviceDialog::EAdditionalDesc:
       
   170         case TBluetoothDialogParams::EDialogTitle:
       
   171             key.Num(aType);
       
   172             value = CHbSymbianVariant::NewL( (TAny*) &aData, CHbSymbianVariant::EDes );
       
   173 			BtTraceBlock( 
       
   174                     TBuf<32> buf;
       
   175                     switch (aType) {
       
   176                         case TBluetoothDialogParams::EAddress:
       
   177                             _LIT(KAddress,"EAddress");
       
   178                             buf.Append(KAddress); 
       
   179                             break;
       
   180                         case TBluetoothDeviceDialog::EDeviceName:
       
   181                             _LIT(KDeviceName,"EDeviceName");
       
   182                             buf.Append(KDeviceName); 
       
   183                             break;
       
   184                         case TBluetoothDeviceDialog::EAdditionalDesc:
       
   185                             _LIT(KAdditionalDesc,"EAdditionalDesc");
       
   186                             buf.Append(KAdditionalDesc); 
       
   187                             break;
       
   188                     }
       
   189                     TPtrC p(buf);
       
   190                     TPtrC16 *ptr = (TPtrC16 *)value->Data();
       
   191                     BOstraceExt2( TRACE_DEBUG, DUMMY_DEVLIST, "SetData [%S] = [%S]", &p, ptr);
       
   192                     );
       
   193             User::LeaveIfError(iNotificationData->Add( key, value ));   // Takes ownership of value
       
   194             break;
       
   195         case TBluetoothDialogParams::EResource:
       
   196         case TBluetoothDeviceDialog::EDeviceClass:
       
   197         case TBluetoothDeviceDialog::EAdditionalInt:
       
   198             PanicServer( EBTNotifPanicBadArgument );
       
   199             break;
       
   200         case TBluetoothDialogParams::ENoParams:
       
   201         case TBluetoothDeviceDialog::ENoParams:
       
   202         default:
       
   203             break;
       
   204         }
       
   205 		BOstraceFunctionExit1( DUMMY_DEVLIST, this );
       
   206     }
       
   207 
       
   208 
       
   209 // ---------------------------------------------------------------------------
       
   210 // Sets the data to be shown to the user.
       
   211 // ---------------------------------------------------------------------------
       
   212 //
       
   213 void CBluetoothNotification::SetDataL( TInt aType, TInt aData )
       
   214     {
       
   215     BOstraceFunctionEntryExt( DUMMY_DEVLIST, this, aType );
       
   216     TBuf<6> key;
       
   217     CHbSymbianVariant* value = NULL;
       
   218     switch( aType )
       
   219         {
       
   220         case TBluetoothDialogParams::EDialogType:
       
   221         case TBluetoothDialogParams::EResource:
       
   222         case TBluetoothDialogParams::EDialogTitle:
       
   223         case TBluetoothDeviceDialog::EDeviceClass:
       
   224         case TBluetoothDeviceDialog::EAdditionalInt:
       
   225             key.Num(aType);
       
   226             value = CHbSymbianVariant::NewL( (TAny*) &aData, CHbSymbianVariant::EInt );
       
   227 			BtTraceBlock( 
       
   228                     TBuf<32> buf;
       
   229                     switch (aType) {
       
   230                         case TBluetoothDialogParams::EDialogType:
       
   231                             buf = _L("EDialogType"); 
       
   232                             break;
       
   233                         case TBluetoothDialogParams::EResource:
       
   234                             buf = _L("EResource");
       
   235                             break;
       
   236                         case TBluetoothDeviceDialog::EDeviceClass:
       
   237                             buf = _L("EDeviceClass");
       
   238                             break;
       
   239                         case TBluetoothDeviceDialog::EAdditionalInt:
       
   240                             buf = _L("EAdditionalInt");
       
   241                             break;
       
   242                     }
       
   243                     TPtrC p(buf);
       
   244                     TInt *intPtr = (TInt *)value->Data();
       
   245                     BOstraceExt2( TRACE_DEBUG, DUMMY_DEVLIST, "SetData [%S] = [%d]", &p, *intPtr);
       
   246                     );
       
   247 			User::LeaveIfError(iNotificationData->Add( key, value ));   // Takes ownership of value
       
   248             break;
       
   249         case TBluetoothDialogParams::EAddress:
       
   250         case TBluetoothDeviceDialog::EDeviceName:
       
   251             PanicServer( EBTNotifPanicBadArgument );
       
   252             break;
       
   253         case TBluetoothDialogParams::ENoParams:
       
   254         case TBluetoothDeviceDialog::ENoParams:
       
   255         default:
       
   256             break;
       
   257         }
       
   258 		BOstraceFunctionExit1( DUMMY_DEVLIST, this );
       
   259     }
       
   260 
       
   261 // ---------------------------------------------------------------------------
       
   262 // From class MHbDeviceDialogObserver.
       
   263 // Callback called when data is received from a device dialog.
       
   264 // ---------------------------------------------------------------------------
       
   265 //
       
   266 void CBluetoothNotification::DataReceived( CHbSymbianVariantMap& aData )
       
   267     {
       
   268     BOstraceFunctionEntry1( DUMMY_DEVLIST, this );
       
   269     BtTraceBlock( debugHbSymbianVariantMap(aData); );
       
   270     CHbSymbianVariant* value = NULL;
       
   271     for( TInt i = 0; i < aData.Keys().MdcaCount(); i++ )
       
   272         {
       
   273         TPtrC key( aData.Keys().MdcaPoint( i ).Ptr(), aData.Keys().MdcaPoint( i ).Length() );
       
   274         const CHbSymbianVariant* valueRef = aData.Get( key );
       
   275         value = NULL;
       
   276         TRAP_IGNORE( value = 
       
   277                 CHbSymbianVariant::NewL( valueRef->Data(), valueRef->Type() ) );
       
   278         if ( value )
       
   279             {
       
   280             TInt err = iReturnData->Add( key, value );
       
   281             if ( err ) 
       
   282                 {
       
   283                 // todo: better exception handing than ignoring this failure?
       
   284                 }
       
   285             }
       
   286         }
       
   287     if( iObserver )
       
   288         {
       
   289     	iObserver->MBRDataReceived( aData );
       
   290         }
       
   291     BOstraceFunctionExit1( DUMMY_DEVLIST, this );
       
   292     }
       
   293 
       
   294 #ifdef BLUETOOTHTRACE_ENABLED
       
   295 
       
   296 void CBluetoothNotification::debugHbSymbianVariantMap( CHbSymbianVariantMap& aData)
       
   297     {
       
   298     for( TInt i = 0; i < aData.Keys().MdcaCount(); i++ )
       
   299         {
       
   300         TBuf<128> buf;
       
   301         TPtrC key( aData.Keys().MdcaPoint( i ).Ptr(), aData.Keys().MdcaPoint( i ).Length() );
       
   302         buf = key;
       
   303         buf.Append(_L(" = "));
       
   304         const CHbSymbianVariant* value = aData.Get( key );
       
   305         TBuf<16> nbr;
       
   306         TBuf<32> newBuf;
       
   307         switch (value->Type()) {
       
   308             case CHbSymbianVariant::EInt :
       
   309                 buf.Append(_L("[EInt] ")); 
       
   310                 nbr.Num(*((TInt*)value->Data()));
       
   311                 buf.Append(nbr);
       
   312                 break;
       
   313             case  CHbSymbianVariant::EBool :
       
   314                 buf.Append(_L("[EBool] ")); 
       
   315                 buf.Append(*((TBool*)value->Data()) ? _L("True") : _L("False"));
       
   316                 break;
       
   317             case CHbSymbianVariant::EUint :
       
   318                 buf.Append( _L("[EUint] ")); 
       
   319                 nbr.Num(*((TUint*)value->Data()));
       
   320                 buf.Append(nbr);
       
   321                 break;
       
   322             case CHbSymbianVariant::EReal  :
       
   323                 buf.Append(_L("[EReal] ")); 
       
   324                 nbr.Num(*((TReal*)value->Data()));
       
   325                 buf.Append(nbr);
       
   326                 break;
       
   327             case CHbSymbianVariant::EDes :  // TDesC
       
   328                 buf.Append(_L("[EDes] ")); 
       
   329                 buf.Append(*((TPtrC16 *)value->Data()));
       
   330                 break;
       
   331             case CHbSymbianVariant::EBinary :  // TDesC8
       
   332                 buf.Append(_L("[EBinary] ")); 
       
   333                 // the following function caused problems when converting this function to 
       
   334                 // a trace function in bluetoothtrace.h
       
   335                 CnvUtfConverter::ConvertToUnicodeFromUtf8(newBuf,*((TPtrC8 *)value->Data()) );
       
   336                 buf.Append(newBuf);
       
   337                 break;
       
   338             case CHbSymbianVariant::EChar  : // a TChar
       
   339                 buf.Append(_L("[EChar] ")); 
       
   340                 buf.Append(*((TChar *)value->Data()));
       
   341                 break;
       
   342             case CHbSymbianVariant::ERect  : // a TRect
       
   343             case CHbSymbianVariant::EPoint : // a TPoint
       
   344             case CHbSymbianVariant::ESize  : // a TSize
       
   345             case CHbSymbianVariant::EDesArray : //  a MDesCArray
       
   346                 break;
       
   347             default:
       
   348                 break;
       
   349             }
       
   350         TPtrC p(buf);
       
   351         BOstraceExt1( TRACE_DEBUG, DUMMY_DEVLIST, "HbSymbianVariantMap [%S]", &p);
       
   352         }
       
   353     }
       
   354 #endif // BLUETOOTHTRACE_ENABLED
       
   355 // ---------------------------------------------------------------------------
       
   356 // From class MHbDeviceDialogObserver.
       
   357 // Callback called when a device dialog is closed.
       
   358 // ---------------------------------------------------------------------------
       
   359 //
       
   360 void CBluetoothNotification::DeviceDialogClosed( TInt aCompletionCode )
       
   361     {
       
   362     BOstraceFunctionEntryExt( DUMMY_DEVLIST, this, aCompletionCode );
       
   363     TPckg<TBool> result( EFalse );
       
   364     TPtrC8 resultPtr( result );
       
   365     const CHbSymbianVariant* value = iReturnData->Get( KBTDevDialogResult );
       
   366     if( value && value->IsValid() )
       
   367         {
       
   368         result() = *value->Value<TBool>();
       
   369         }
       
   370     RBuf8 resultBuf;
       
   371     value = iReturnData->Get( KBTDevDialogInput );
       
   372     if( value && value->IsValid() )
       
   373         {
       
   374         HBufC8* data = value->Value<HBufC8>();
       
   375         if( !resultBuf.Create( data->Length() + result.Length() ) )
       
   376             {
       
   377             resultBuf = *data;
       
   378             resultBuf.Insert( 0, result );
       
   379             resultPtr.Set( resultBuf );
       
   380             }
       
   381         }
       
   382     if( iObserver )
       
   383         {
       
   384         if( aCompletionCode == CHbDeviceDialogSymbian::ECancelledError )
       
   385             {
       
   386             aCompletionCode = KErrCancel;
       
   387             }
       
   388         iObserver->MBRNotificationClosed( aCompletionCode, resultPtr );
       
   389         }
       
   390     resultBuf.Close();
       
   391     iManager->ReleaseNotification( this );
       
   392     // Note that we might get deleted after releasing ourselves.
       
   393     BOstraceFunctionExit0( DUMMY_DEVLIST );
       
   394     }
       
   395