web_plat/widget_registry_api/inc/WidgetPropertyValue.h
changeset 0 dd21522fd290
child 37 cb62a4f66ebe
equal deleted inserted replaced
-1:000000000000 0:dd21522fd290
       
     1 /*
       
     2 * Copyright (c) 2007 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 the License "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:  Holds property values.
       
    15 *
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 #ifndef WIDGETPROPERTYVALUE_H
       
    21 #define WIDGETPROPERTYVALUE_H
       
    22 
       
    23 #include <e32base.h>
       
    24 #include <s32strm.h>
       
    25 #include "WidgetRegistryConstants.h"
       
    26 
       
    27 // serialize length = header + value
       
    28 const TInt32 KWidgetPropertyValSerializeMaxLength = 32 + KWidgetRegistryVal;
       
    29 
       
    30 enum TWidgetPropertyType
       
    31     {
       
    32     EWidgetPropTypeUnknown = 0,
       
    33     EWidgetPropTypeBool,
       
    34     EWidgetPropTypeInt,
       
    35     EWidgetPropTypeString,
       
    36     EWidgetPropTypeUid,
       
    37     EWidgetPropTypeBlob
       
    38     };
       
    39 
       
    40 /**
       
    41 *  CWidgetData
       
    42 *  @since 3.1
       
    43 */
       
    44 class CWidgetPropertyValue : public CBase
       
    45     {
       
    46 public:
       
    47     /**
       
    48      * Two phase constructor
       
    49      * @return none
       
    50      * @since 3.1
       
    51      **/
       
    52     static CWidgetPropertyValue* NewL()
       
    53         {
       
    54         CWidgetPropertyValue *self = new ( ELeave ) CWidgetPropertyValue();
       
    55         CleanupStack::PushL( self );
       
    56         self->ConstructL();
       
    57         CleanupStack::Pop();
       
    58         return self;
       
    59         }
       
    60 
       
    61     /**
       
    62      * Destructor
       
    63      * @return none
       
    64      * @since 3.1
       
    65      **/
       
    66     ~CWidgetPropertyValue()
       
    67         {
       
    68         if ( EWidgetPropTypeString == iType )
       
    69             {
       
    70             delete iValue.s;
       
    71             }
       
    72         }
       
    73 
       
    74     operator TInt() const
       
    75         {
       
    76         if ( EWidgetPropTypeInt == iType )
       
    77             {
       
    78             return iValue.i;
       
    79             }
       
    80         return 0;
       
    81         }
       
    82 
       
    83     operator const TDesC&()
       
    84         {
       
    85         if ( EWidgetPropTypeString == iType )
       
    86             {
       
    87             return *iValue.s;
       
    88             }
       
    89         return KNullDesC;
       
    90         }
       
    91 
       
    92     operator const TUid&()
       
    93         {
       
    94         if ( EWidgetPropTypeUid == iType )
       
    95             {
       
    96             return iValue.uid;
       
    97             }
       
    98         return KNullUid;
       
    99         }
       
   100 
       
   101     CWidgetPropertyValue& operator= (TInt i)
       
   102         {
       
   103         iValue.i = i;
       
   104         iType = EWidgetPropTypeInt;
       
   105         return *this;
       
   106         }
       
   107 
       
   108     CWidgetPropertyValue& operator= (const TDesC& s)
       
   109         {
       
   110         // self-assignment check
       
   111         if ( iValue.s != &s )
       
   112             {
       
   113             delete iValue.s;
       
   114             iValue.s = s.AllocL();
       
   115             iType = EWidgetPropTypeString;
       
   116             }
       
   117             return *this;
       
   118         }
       
   119 
       
   120     CWidgetPropertyValue& operator= (TUid uid)
       
   121         {
       
   122         iValue.uid = uid;
       
   123         iType = EWidgetPropTypeUid;
       
   124         return *this;
       
   125         }
       
   126 
       
   127     void Reset()
       
   128         {
       
   129         if ( EWidgetPropTypeString == iType )
       
   130             {
       
   131             delete iValue.s;
       
   132             iValue.s = NULL;
       
   133             }
       
   134         iType = EWidgetPropTypeUnknown;
       
   135         }
       
   136 
       
   137     void SerializeL( RWriteStream& aStream ) const
       
   138         {
       
   139         switch ( iType )
       
   140             {
       
   141         case EWidgetPropTypeUnknown:
       
   142             aStream.WriteInt32L( 7 );
       
   143             aStream.WriteL( _L("unknown"), 7 );
       
   144             break;
       
   145         case EWidgetPropTypeBool:
       
   146             aStream.WriteInt32L( 4 );
       
   147             aStream.WriteL( _L("bool"), 4 );
       
   148             aStream.WriteInt32L( iValue.i );
       
   149             break;
       
   150         case EWidgetPropTypeInt:
       
   151             aStream.WriteInt32L( 3 );
       
   152             aStream.WriteL( _L("int"), 3 );
       
   153             aStream.WriteInt32L( iValue.i );
       
   154             break;
       
   155         case EWidgetPropTypeString:
       
   156             aStream.WriteInt32L( 3 );
       
   157             aStream.WriteL( _L("str"), 3 );
       
   158             aStream.WriteInt32L( iValue.s->Length() );
       
   159             aStream.WriteL( *(iValue.s), iValue.s->Length() );
       
   160             break;
       
   161         case EWidgetPropTypeUid:
       
   162             aStream.WriteInt32L( 3 );
       
   163             aStream.WriteL( _L("uid"), 3 );
       
   164             aStream.WriteInt32L( iValue.uid.iUid );
       
   165             break;
       
   166         default:
       
   167             User::Leave( KErrCorrupt );
       
   168             }
       
   169         }
       
   170 
       
   171     void DeserializeL( RReadStream& aStream )
       
   172         {
       
   173         Reset();
       
   174         TInt x = 0;
       
   175         TInt len = aStream.ReadInt32L();
       
   176         if ( len > KMaxFileName )
       
   177             {
       
   178             User::Leave( KErrCorrupt );
       
   179             }
       
   180         TFileName buf;
       
   181         aStream.ReadL( buf, len );
       
   182         if ( 0 == buf.Compare( _L("unknown") ) )
       
   183             {
       
   184             iType = EWidgetPropTypeUnknown;
       
   185             iValue.i = 0;
       
   186             }
       
   187         else if ( 0 == buf.Compare( _L("bool") ) )
       
   188             {
       
   189             iType = EWidgetPropTypeBool;
       
   190             iValue.i = aStream.ReadInt32L();
       
   191             }
       
   192         else if ( 0 == buf.Compare( _L("int") ) )
       
   193             {
       
   194             iType = EWidgetPropTypeInt;
       
   195             iValue.i = aStream.ReadInt32L();
       
   196             }
       
   197         else if ( 0 == buf.Compare( _L("str") ) )
       
   198             {
       
   199             iType = EWidgetPropTypeString;
       
   200             len = aStream.ReadInt32L();
       
   201             if ( len <= KMaxFileName )
       
   202                 {
       
   203                 HBufC* vBuf = HBufC::NewLC( len );
       
   204                 TPtr16 p = vBuf->Des();
       
   205                 aStream.ReadL( p, len );
       
   206                 iValue.s = vBuf;
       
   207                 CleanupStack::Pop(); // vBuf
       
   208                 }
       
   209             else
       
   210                 {
       
   211                 User::Leave( KErrCorrupt );
       
   212                 }
       
   213             }
       
   214         else if ( 0 == buf.Compare( _L("uid") ) )
       
   215             {
       
   216             iType = EWidgetPropTypeUid;
       
   217             TUid uid = TUid::Uid( aStream.ReadInt32L() );
       
   218             iValue.uid = uid;
       
   219             }
       
   220         else
       
   221             {
       
   222             User::Leave( KErrCorrupt );
       
   223             }
       
   224         }
       
   225 
       
   226 protected:
       
   227     /**
       
   228      * Constructor
       
   229      */
       
   230     CWidgetPropertyValue() {}
       
   231 
       
   232     /**
       
   233      * 2-phase constructor
       
   234      */
       
   235     void ConstructL() {}
       
   236 
       
   237 private:
       
   238     // no copy constructor
       
   239     CWidgetPropertyValue( const CWidgetPropertyValue& );
       
   240     // no assignment
       
   241     CWidgetPropertyValue& operator=( const CWidgetPropertyValue& );
       
   242 
       
   243 public:
       
   244     TWidgetPropertyType iType;
       
   245     union
       
   246         {
       
   247         TInt i;
       
   248         HBufC* s;
       
   249         TUid uid;
       
   250         } iValue;
       
   251     };
       
   252 
       
   253 // RPointerArray<CWidgetPropertyValue>  RWidgetPropertyValues(EWidgetPropertyIdCount)
       
   254 // and use is ended by ResetAndDestroy()
       
   255 
       
   256 
       
   257 #endif // WIDGETPROPERTYVALUE_H