src/hbcore/devicedialogbase/hbsymbianvariant.cpp
changeset 34 ed14f46c0e55
parent 2 06ff229162e9
equal deleted inserted replaced
31:7516d6d86cf5 34:ed14f46c0e55
    23 **
    23 **
    24 ****************************************************************************/
    24 ****************************************************************************/
    25 
    25 
    26 #include "hbsymbianvariant.h"
    26 #include "hbsymbianvariant.h"
    27 
    27 
    28 class CHbSymbianVariantPrivate : public CBase
    28 _LIT(KPanicSymbianVariantNotSupportedType, "CHbSymbianVariant: Not supported type");
       
    29 
       
    30 NONSHARABLE_CLASS(CHbSymbianVariantPrivate) : public CBase
    29 {
    31 {
    30 public:
    32 public:
    31     CHbSymbianVariantPrivate();
    33     CHbSymbianVariantPrivate();
    32     ~CHbSymbianVariantPrivate();
    34     ~CHbSymbianVariantPrivate();
    33 
    35 
   118             break;
   120             break;
   119         }
   121         }
   120         case CHbSymbianVariant::EDesArray: {
   122         case CHbSymbianVariant::EDesArray: {
   121             const MDesCArray* arrayPtr = reinterpret_cast<const MDesCArray*>(aData);
   123             const MDesCArray* arrayPtr = reinterpret_cast<const MDesCArray*>(aData);
   122             TInt count = arrayPtr->MdcaCount();
   124             TInt count = arrayPtr->MdcaCount();
   123             CDesC16ArrayFlat* array = new CDesC16ArrayFlat(16);
   125             CDesC16ArrayFlat* array = new (ELeave) CDesC16ArrayFlat(16);
   124             for(TInt i = 0; i < count; i++) {
   126             CleanupStack::PushL(array);
       
   127             for(TInt i = 0; i < count; ++i) {
   125                 array->AppendL(arrayPtr->MdcaPoint(i));
   128                 array->AppendL(arrayPtr->MdcaPoint(i));
   126             }
   129             }
       
   130             CleanupStack::Pop(array);
   127             iData.iBasicData.ptr = array;
   131             iData.iBasicData.ptr = array;
   128             break;
   132             break;
   129         }
   133         }
   130         case CHbSymbianVariant::EVariantMap: {
   134         case CHbSymbianVariant::EVariantMap: {
   131             const CHbSymbianVariantMap* mapPtr = 
   135             const CHbSymbianVariantMap* mapPtr = 
   132                 reinterpret_cast<const CHbSymbianVariantMap*>(aData);
   136                 reinterpret_cast<const CHbSymbianVariantMap*>(aData);
   133             CHbSymbianVariantMap* copy = CHbSymbianVariantMap::NewL();
   137             CHbSymbianVariantMap* copy = CHbSymbianVariantMap::NewL();
   134             CleanupStack::PushL(copy);
   138             CleanupStack::PushL(copy);
   135             MDesCArray& keys = mapPtr->Keys();
   139             MDesCArray& keys = mapPtr->Keys();
   136             TInt count = keys.MdcaCount();
   140             TInt count = keys.MdcaCount();
   137             for(TInt i = 0; i < count ; i++) {
   141             for(TInt i = 0; i < count ; ++i) {
   138                 TPtrC ptr = keys.MdcaPoint(i);
   142                 TPtrC ptr = keys.MdcaPoint(i);
   139                 const CHbSymbianVariant* var = mapPtr->Get(ptr);
   143                 const CHbSymbianVariant* var = mapPtr->Get(ptr);
   140                 copy->Add(ptr, CHbSymbianVariant::NewL(var->Data(), var->Type()));
   144                 copy->Add(ptr, CHbSymbianVariant::NewL(var->Data(), var->Type()));
   141             }
   145             }
   142             iData.iBasicData.ptr = copy;
   146             iData.iBasicData.ptr = copy;
   143             CleanupStack::Pop(copy);
   147             CleanupStack::Pop(copy);
   144             break;
   148             break;
   145         }
   149         }
   146         default: {
   150         default: {
   147             User::Panic(_L("CHbSymbianVariant: Not supported type"),aDataType);
   151             User::Panic(KPanicSymbianVariantNotSupportedType, aDataType);
   148             break;
   152             break;
   149         }
   153         }
   150     }
   154     }
   151     iType = aDataType;
   155     iType = aDataType;
   152     iNull = EFalse;
   156     iNull = EFalse;
   241     }
   245     }
   242     return return_value;
   246     return return_value;
   243 };
   247 };
   244 
   248 
   245 /*!
   249 /*!
   246  \class CHbSymbianVariant
   250     \class CHbSymbianVariant
   247  \brief CHbSymbianVariant implements QVariant for Symbian.
   251     \brief CHbSymbianVariant is a QVariant like union for Symbian data types.
   248 
   252 
   249  It supports part of the data types supported in QVariant:
   253     <b>This class is Symbian only. Not available on other platforms.</b>
   250  QString (TDesC)
   254 
   251  QChar (TChar)
   255     CHbSymbianVariant holds a single value of a single type at a time. CHbSymbianVariant::TType
   252  QPoint(TPoint)
   256     specifies supported data types.
   253  QRect(TRect)
   257 
   254  QSize(TSize)
   258     The class is used to pass parameters to device dialog and indicator plugins. Device dialog
   255  QByteArray(TDesC8)
   259     and indicator frameworks perform conversion between Qt and Symbian data types. Below list
   256  QStringList (MDesCArray)
   260     contains a partial list of Symbian data types and corresponding Qt types supported by
   257 
   261     the class.
   258  The data passed to the constructor is copied to the CHbSymbianVariant.
   262 
   259 
   263      <table>
   260  \sa CHbSymbianVariantMap, CHbDeviceDialog
   264          <caption> Symbian/Qt data type analogy</caption>
   261  \stable
   265          <tr><th>Symbian type</th><th>Qt type</th></tr>
   262 
   266          <tr><td>TDesC</td><td>QString</td></tr>
   263  \hbcore
   267          <tr><td>TChar</td><td>QChar</td></tr>
   264 
   268          <tr><td>TPoint</td><td>QPoint</td></tr>
       
   269          <tr><td>TRect</td><td>QRect</td></tr>
       
   270          <tr><td>TSize</td><td>QSize</td></tr>
       
   271          <tr><td>TDesC8</td><td>QByteArray</td></tr>
       
   272          <tr><td>MDesCArray</td><td>QStringList</td></tr>
       
   273          <tr><td>CHbSymbianVariantMap</td><td>QVariantMap</td></tr>
       
   274      </table>
       
   275 
       
   276     \sa CHbSymbianVariantMap, CHbDeviceDialogSymbian, CHbIndicatorSymbian
       
   277  
       
   278     \stable
       
   279     \hbcore
   265  */
   280  */
   266 
   281 
   267 /*!
   282 /*!
   268    \enum CHbSymbianVariant::TType
   283    \enum CHbSymbianVariant::TType
   269    Data types the CHbSymbianVariant can contain
   284    Data types CHbSymbianVariant supports.
   270 */
   285 */
   271 /*!
   286 /*!
   272    \var CHbSymbianVariant::EInt
   287    \var CHbSymbianVariant::TType CHbSymbianVariant::EInt
   273    a TInt
   288    Data type TInt.
   274 */
   289 */
   275 /*!
   290 /*!
   276     \var CHbSymbianVariant::EBool
   291     \var CHbSymbianVariant::TType CHbSymbianVariant::EBool
   277     a TBool
   292     Data type TBool.
   278 */
   293 */
   279 /*!
   294 /*!
   280     \var CHbSymbianVariant::EUint
   295     \var CHbSymbianVariant::TType CHbSymbianVariant::EUint
   281     a TUint
   296     Data type TUint.
   282 */
   297 */
   283 /*!
   298 /*!
   284     \var CHbSymbianVariant::EReal
   299     \var CHbSymbianVariant::TType CHbSymbianVariant::EReal
   285     a TReal
   300     Data type TReal.
   286 */
   301 */
   287 /*!
   302 /*!
   288     \var CHbSymbianVariant::ERect
   303     \var CHbSymbianVariant::TType CHbSymbianVariant::ERect
   289     a TRect
   304     Data type TRect.
   290 */
   305 */
   291 /*!
   306 /*!
   292     \var CHbSymbianVariant::EPoint
   307     \var CHbSymbianVariant::TType CHbSymbianVariant::EPoint
   293     a TPoint
   308     Data type TPoint.
   294 */
   309 */
   295 /*!
   310 /*!
   296     \var CHbSymbianVariant::ESize
   311     \var CHbSymbianVariant::TType CHbSymbianVariant::ESize
   297     a TSize
   312     Data type TSize.
   298 */
   313 */
   299 /*!
   314 /*!
   300     \var CHbSymbianVariant::EChar
   315     \var CHbSymbianVariant::TType CHbSymbianVariant::EChar
   301     a TChar
   316     Data type TChar.
   302 */
   317 */
   303 /*!
   318 /*!
   304     \var CHbSymbianVariant::EDes
   319     \var CHbSymbianVariant::TType CHbSymbianVariant::EDes
   305     a TDesC
   320     Data type TDesC.
   306 */
   321 */
   307 /*!
   322 /*!
   308     \var CHbSymbianVariant::EBinary
   323     \var CHbSymbianVariant::TType CHbSymbianVariant::EBinary
   309     a TDesC8
   324     Data type TDesC8.
   310 */
   325 */
   311 /*!
   326 /*!
   312     \var CHbSymbianVariant::EDesArray
   327     \var CHbSymbianVariant::TType CHbSymbianVariant::EDesArray
   313     a MDesCArray
   328     Data type MDesCArray.
   314 */
   329 */
   315 /*!
   330 /*!
   316     \var CHbSymbianVariant::EVariantMap
   331     \var CHbSymbianVariant::TType CHbSymbianVariant::EVariantMap
   317     a symbian variant map
   332     Data type CHbSymbianVariantMap.
   318 */
   333 */
   319 
   334 
   320  /*!
   335  /*!
   321     \fn T * CHbSymbianVariant::Value()
   336     \fn T* CHbSymbianVariant::Value() const
   322 
   337 
   323     Template class to get variants value casted to the correct pointer type. See the
   338     Template fuction to get variant's value casted to a correct pointer type. See
   324     CHbSymbianVariant::TType for the supported classes.
   339     TType for the supported types.
   325 
   340 
   326     \sa CHbSymbianVariant::TType
   341     \sa TType
   327 */
   342 */
   328 
   343 
   329 /*!
   344 /*!
   330     Creates a new CHbSymbianVariant. Note that if CHbSymbianVariant::EDesArray
   345     Creates a new CHbSymbianVariant and returns a pointer to it. Data passed to
   331     is used the \aData must be casted to MDesCArray pointer when passed to the method.
   346     the constructor is copied to the CHbSymbianVariant.
   332 */
   347 
   333 
   348     \param aData Data to copy into the variant. Note: If EDesArray is used, data pointer must be casted to MDesCArray.
       
   349     \param aDataType Type of data. 
       
   350 */
   334 EXPORT_C CHbSymbianVariant* CHbSymbianVariant::NewL(const TAny* aData, TType aDataType)
   351 EXPORT_C CHbSymbianVariant* CHbSymbianVariant::NewL(const TAny* aData, TType aDataType)
   335 {
   352 {
   336     CHbSymbianVariant* variant = new (ELeave) CHbSymbianVariant(aData, aDataType);
   353     CHbSymbianVariant* variant = new (ELeave) CHbSymbianVariant(aData, aDataType);
   337     CleanupStack::PushL(variant);
   354     CleanupStack::PushL(variant);
   338     variant->ConstructL(aData, aDataType);
   355     variant->ConstructL(aData, aDataType);
   339     CleanupStack::Pop(variant); //variant
   356     CleanupStack::Pop(variant); //variant
   340     return variant;
   357     return variant;
   341 }
   358 }
   342 
   359 
   343 
   360 /*!
   344 /*!
   361     Returns type of data stored in CHbSymbianVariant.
   345     Returns the data type stored in CHbSymbianVariant
   362 
   346 
   363     \sa TType
   347     \sa CHbSymbianVariant::TType
       
   348 */
   364 */
   349 EXPORT_C CHbSymbianVariant::TType CHbSymbianVariant::Type() const
   365 EXPORT_C CHbSymbianVariant::TType CHbSymbianVariant::Type() const
   350 {
   366 {
   351     return d->iType;
   367     return d->iType;
   352 }
   368 }
   353 
   369 
   354 /*!
   370 /*!
   355     Get TAny pointer to the CHbSymbianVariant data.
   371     Returns pointer to data stored by CHbSymbianVariant. Ownership does not changed.
   356     The CHbSymbianVariant::Value() should be used instead of this method.
   372     Value() should be used instead of this method.
   357 
       
   358     Ownership is not changed.
       
   359 */
   373 */
   360 EXPORT_C TAny* CHbSymbianVariant::Data() const
   374 EXPORT_C TAny* CHbSymbianVariant::Data() const
   361 {
   375 {
   362     return d->Data();
   376     return d->Data();
   363 }
   377 }
   364 
   378 
   365 /*!
   379 /*!
   366     Set new data to CHbSymbianVariant. Old data is deleted.
   380     Copies new data to CHbSymbianVariant. Old data is deleted.
   367 
   381     Ownership does not changed.
   368     Ownership is not changed.
   382 
       
   383     \param aData Data to copy into the variant. Note: If EDesArray is used, data pointer must be casted to MDesCArray.
       
   384     \param aDataType Type of data. 
   369 */
   385 */
   370 EXPORT_C void CHbSymbianVariant::SetData(TAny* aData, TType aDataType)
   386 EXPORT_C void CHbSymbianVariant::SetData(TAny* aData, TType aDataType)
   371 {
   387 {
   372     if(d) {
   388     if(d) {
   373         d->ResetData();
   389         d->ResetData();
   374         d->InitializeDataL(aData, aDataType);
   390         d->InitializeDataL(aData, aDataType);
   375     }
   391     }
   376 }
   392 }
   377 /*!
   393 /*!
   378     Is the the data in CHbSymbianVariant valid. data is valid if the variant
   394     Returns true if CHbSymbianVariant holds valid data.
   379     is created with valid data and data type
       
   380 */
   395 */
   381 EXPORT_C TBool CHbSymbianVariant::IsValid() const
   396 EXPORT_C TBool CHbSymbianVariant::IsValid() const
   382 {
   397 {
   383     if(d && !(d->iNull))
   398     if(d && !(d->iNull))
   384         return ETrue;
   399         return ETrue;
   385     else
   400     else
   386         return EFalse;
   401         return EFalse;
   387 }
   402 }
   388 
   403 
       
   404 /*!
       
   405     Destructs CHbSymbianVariant.
       
   406 */
   389 EXPORT_C CHbSymbianVariant::~CHbSymbianVariant()
   407 EXPORT_C CHbSymbianVariant::~CHbSymbianVariant()
   390 {
   408 {
   391     delete d;
   409     delete d;
   392     d = 0;
   410     d = 0;
   393 }
   411 }
   400 {
   418 {
   401     d = new (ELeave) CHbSymbianVariantPrivate;
   419     d = new (ELeave) CHbSymbianVariantPrivate;
   402     d->InitializeDataL(aData, aDataType);
   420     d->InitializeDataL(aData, aDataType);
   403 }
   421 }
   404 
   422 
   405 class CHbSymbianVariantMapPrivate : public CBase
   423 NONSHARABLE_CLASS(CHbSymbianVariantMapPrivate) : public CBase
   406 {
   424 {
   407 public:
   425 public:
   408     CHbSymbianVariantMapPrivate():iKeyArray(NULL),iVariantArray() {
   426     CHbSymbianVariantMapPrivate():iKeyArray(NULL),iVariantArray() {
   409         iKeyArray = new CDesC16ArrayFlat(16);
   427         iKeyArray = new CDesC16ArrayFlat(16);
   410     };
   428     };
   419     CDesC16Array* iKeyArray;
   437     CDesC16Array* iKeyArray;
   420     RPointerArray<CHbSymbianVariant> iVariantArray;
   438     RPointerArray<CHbSymbianVariant> iVariantArray;
   421 };
   439 };
   422 
   440 
   423 /*!
   441 /*!
   424   \class CHbSymbianVariantMap
   442     \class CHbSymbianVariantMap
   425   \brief CHbSymbianVariantMap is a container for CHbSymbianVariant objects.
   443     \brief CHbSymbianVariantMap is QVariantMap style container for CHbSymbianVariant objects.
   426 
   444 
   427   It replicates QVariantMap functionality to Symbian native programming side.
   445     <b>This class is Symbian only. Not available on other platforms.</b>
   428 
   446 
   429   \sa CHbSymbianVariant, CHbDeviceDialog
   447     This class is used to pass parameters to/from device dialogs.
   430   \stable
   448 
   431 */
   449     \sa CHbSymbianVariant, CHbDeviceDialogSymbian
   432 
   450     \stable
   433 /*!
   451 */
   434   Create a new CHbSymbianVariantMap. Map is empty after creation and
   452 
   435   ready for use.
   453 /*!
       
   454     Constructs a new CHbSymbianVariantMap and returns a pointer it. The container is empty after
       
   455     construction.
   436  */
   456  */
   437 EXPORT_C CHbSymbianVariantMap* CHbSymbianVariantMap::NewL()
   457 EXPORT_C CHbSymbianVariantMap* CHbSymbianVariantMap::NewL()
   438 {
   458 {
   439     CHbSymbianVariantMap* map = new (ELeave) CHbSymbianVariantMap();
   459     CHbSymbianVariantMap* map = new (ELeave) CHbSymbianVariantMap();
   440     CleanupStack::PushL(map);
   460     CleanupStack::PushL(map);
   441     map->ConstructL();
   461     map->ConstructL();
   442     CleanupStack::Pop(map); // map
   462     CleanupStack::Pop(map); // map
   443     return map;
   463     return map;
   444 }
   464 }
   445 
   465 
       
   466 /*!
       
   467     Destructs CHbSymbianVariantMap.
       
   468 */
   446 EXPORT_C CHbSymbianVariantMap::~CHbSymbianVariantMap()
   469 EXPORT_C CHbSymbianVariantMap::~CHbSymbianVariantMap()
   447 {
   470 {
   448     delete d;
   471     delete d;
   449     d = 0;
   472     d = 0;
   450 }
   473 }
   451 
   474 
   452 /*!
   475 /*!
   453  Adds a key-value pairing to the map. Takes ownership of the variant,
   476     Adds a key-value pair into the container. Takes ownership of the variant,
   454  key is copied. If key is already in the map, the value connected to the key
   477     key is copied. If key is already in the map, the value connected to the key
   455  is replaced with the new value. the old value is deleted.
   478     is replaced with the new value and the old value is deleted.
       
   479 
       
   480     \param aKey Key for the value.
       
   481     \param aVariant Value to add. 
       
   482 
       
   483     \return Returns Symbian error code. On error ownership of \p aKey is returned to caller. 
   456  */
   484  */
   457 EXPORT_C TInt CHbSymbianVariantMap::Add(const TDesC& aKey, CHbSymbianVariant* aVariant)
   485 EXPORT_C TInt CHbSymbianVariantMap::Add(const TDesC& aKey, CHbSymbianVariant* aVariant)
   458 {
   486 {
   459     TInt pos = KErrNotFound;
   487     TInt pos = KErrNotFound;
   460     TInt result = d->iKeyArray->Find(aKey, pos,ECmpNormal);
   488     TInt result = d->iKeyArray->Find(aKey, pos, ECmpNormal);
   461 
   489 
   462     // there is already a key-value pairing in the map
   490     // There is already a key-value pairing in the map
   463     if((result == KErrNone) && (pos >= 0)) {
   491     if ((result == KErrNone) && (pos >= 0)) {
   464 
   492 
   465         // for some reason directly deleting the pointer and
   493         // for some reason directly deleting the pointer and
   466         // assigning a new one to position does not work
   494         // assigning a new one to position does not work
   467         CHbSymbianVariant* remove = d->iVariantArray[pos];
   495         CHbSymbianVariant* remove = d->iVariantArray[pos];
   468         d->iVariantArray.Remove(pos);
   496         d->iVariantArray.Remove(pos);
   474     }
   502     }
   475     // new key-value pair
   503     // new key-value pair
   476     else {
   504     else {
   477         result = KErrNone;
   505         result = KErrNone;
   478         TRAP(result, d->iKeyArray->AppendL(aKey));
   506         TRAP(result, d->iKeyArray->AppendL(aKey));
   479         if(result == KErrNone){
   507         if (result == KErrNone){
   480             result = d->iVariantArray.Append(aVariant);
   508             result = d->iVariantArray.Append(aVariant);
   481             if(result) {
   509             if (result) {
   482                 d->iKeyArray->Delete(d->iKeyArray->MdcaCount()-1);
   510                 d->iKeyArray->Delete(d->iKeyArray->MdcaCount()-1);
   483             }
   511             }
   484         }
   512         }
   485     }
   513     }
   486     return result;
   514     return result;
   487 }
   515 }
   488 /*!
   516 
   489  Deletes value mapped to the key given as parameter \a aKey
   517 /*!
   490 
   518     Deletes value from the container.
   491  On error returns possible Symbian error code.
   519 
       
   520     \param aKey Key for the value.
       
   521 
       
   522     \return Returns Symbian error code.
   492  */
   523  */
   493 EXPORT_C TInt CHbSymbianVariantMap::Delete(const TDesC& aKey)
   524 EXPORT_C TInt CHbSymbianVariantMap::Delete(const TDesC& aKey)
   494 {
   525 {
   495     TInt pos = 0;
   526     TInt pos = 0;
   496     TInt result = d->iKeyArray->Find(aKey, pos,ECmpNormal);
   527     TInt result = d->iKeyArray->Find(aKey, pos,ECmpNormal);
   503     }
   534     }
   504     return result;
   535     return result;
   505 }
   536 }
   506 
   537 
   507 /*!
   538 /*!
   508   Gets value mapped to the key. CHbSymbianVariantMap keeps the ownership of the object.
   539     Gets a value from the container.
   509   Returns NULL if not found.
   540 
       
   541     \param aKey Key for the value.
       
   542 
       
   543     \return Returns pointer to the value or 0 if not found. Value ownership does not transfer.
   510  */
   544  */
   511 EXPORT_C const CHbSymbianVariant* CHbSymbianVariantMap::Get(const TDesC& aKey) const
   545 EXPORT_C const CHbSymbianVariant* CHbSymbianVariantMap::Get(const TDesC& aKey) const
   512 {
   546 {
   513     TInt pos = 0;
   547     TInt pos = 0;
   514     CHbSymbianVariant* variant = 0;
   548     CHbSymbianVariant* variant = 0;
   518     }
   552     }
   519     return variant;
   553     return variant;
   520 }
   554 }
   521 
   555 
   522 /*!
   556 /*!
   523  Get all keys in the map. CHbSymbianVariantMap keeps the ownership of the object.
   557     Returns all keys in the map. CHbSymbianVariantMap keeps ownership of the returned value.
   524  */
   558  */
   525 EXPORT_C MDesCArray& CHbSymbianVariantMap::Keys() const
   559 EXPORT_C MDesCArray& CHbSymbianVariantMap::Keys() const
   526 {
   560 {
   527     return *(d->iKeyArray);
   561     return *(d->iKeyArray);
   528 }
   562 }
   532 
   566 
   533 }
   567 }
   534 
   568 
   535 void CHbSymbianVariantMap::ConstructL()
   569 void CHbSymbianVariantMap::ConstructL()
   536 {
   570 {
   537     d = new CHbSymbianVariantMapPrivate();
   571     d = new (ELeave) CHbSymbianVariantMapPrivate();
   538 }
   572 }
   539