webengine/widgetregistry/Server/src/WidgetEntry.cpp
changeset 0 dd21522fd290
child 17 c8a366e56285
equal deleted inserted replaced
-1:000000000000 0:dd21522fd290
       
     1 /*
       
     2 * Copyright (c) 2006, 2008 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 metadata, one version per widget.
       
    15 *
       
    16 *
       
    17 */
       
    18 
       
    19 #include "WidgetEntry.h"
       
    20 #include "WidgetRegistryConstants.h"
       
    21 #include <s32file.h>
       
    22 #include <f32file.h>
       
    23 #include <APGTASK.H>
       
    24 //#include <widgetappdefs.rh>
       
    25 
       
    26 // EXTERNAL DATA STRUCTURES
       
    27 
       
    28 // EXTERNAL FUNCTION PROTOTYPES
       
    29 
       
    30 // CONSTANTS
       
    31 #define SAPISECURITYPROMPTNEEDED 0x0001
       
    32 #define SAPIPROMPTLESS           0x0002
       
    33 #define SAPIACCESSDENIED         0x0003
       
    34 
       
    35 // MACROS
       
    36 
       
    37 // LOCAL CONSTANTS AND MACROS
       
    38 
       
    39 // for externalize
       
    40 _LIT( KXmlPropStart, "<prop>" );
       
    41 _LIT( KXmlPropEnd, "</prop>" );
       
    42 _LIT( KXmlValStart, "<val>" );
       
    43 _LIT( KXmlValEnd, "</val>" );
       
    44 _LIT( KXmlTypeStart, "<type>" );
       
    45 _LIT( KXmlTypeEnd, "</type>" );
       
    46 _LIT( KXmlNewline, "\x0D\x0A" ); // DOS/Symbian style works with XML parsers
       
    47 
       
    48 // for internalize
       
    49 _LIT8( KXmlPropTag, "prop" );
       
    50 _LIT8( KXmlValTag, "val" );
       
    51 _LIT8( KXmlTypeTag, "type" );
       
    52 _LIT( KXmlDataTypeBool, "bool" );
       
    53 _LIT( KXmlDataTypeInt, "int" );
       
    54 _LIT( KXmlDataTypeString, "string" );
       
    55 _LIT( KXmlDataTypeUid, "uid" );
       
    56 
       
    57 // MODULE DATA STRUCTURES
       
    58 
       
    59 // LOCAL FUNCTION PROTOTYPES
       
    60 
       
    61 // FORWARD DECLARATIONS
       
    62 
       
    63 // ============================= LOCAL FUNCTIONS ===============================
       
    64 
       
    65 // ============================ MEMBER FUNCTIONS ===============================
       
    66 
       
    67 
       
    68 // ============================================================================
       
    69 // CWidgetEntry::NewL()
       
    70 // two-phase constructor
       
    71 //
       
    72 // @since 3.1
       
    73 // ============================================================================
       
    74 //
       
    75 CWidgetEntry* CWidgetEntry::NewL()
       
    76     {
       
    77     CWidgetEntry *self = new ( ELeave ) CWidgetEntry();
       
    78     CleanupStack::PushL( self );
       
    79     self->ConstructL();
       
    80     CleanupStack::Pop();
       
    81     return self;
       
    82     }
       
    83 
       
    84 // ============================================================================
       
    85 // CWidgetEntry::NewL()
       
    86 // two-phase constructor
       
    87 //
       
    88 // @since 5.0
       
    89 // ============================================================================
       
    90 //    
       
    91 CWidgetEntry* CWidgetEntry::NewL( RPointerArray<CWidgetPropertyValue>** aProps )
       
    92 {
       
    93     CWidgetEntry* tmp = NewL();
       
    94     for ( TInt i = 0; i < (*aProps)->Count(); i++ )
       
    95     {
       
    96         CWidgetPropertyValue* value = CWidgetPropertyValue::NewL();
       
    97         tmp->iPropertyValues.AppendL( value );
       
    98         (*tmp)[i].iType = (**aProps)[i]->iType;
       
    99         (*tmp)[i].iValue = (**aProps)[i]->iValue; // shallow copy of strings
       
   100         (**aProps)[i]->iType = EWidgetPropTypeUnknown;
       
   101         delete (**aProps)[i];
       
   102     }
       
   103     (*aProps)->Close();
       
   104     delete *aProps;
       
   105     *aProps = NULL;
       
   106     return tmp;
       
   107 }
       
   108 
       
   109 // ============================================================================
       
   110 // CWidgetEntry::CWidgetEntry()
       
   111 // C++ constructor
       
   112 //
       
   113 // @since 3.1
       
   114 // ============================================================================
       
   115 //
       
   116 CWidgetEntry::CWidgetEntry()
       
   117     : iPropertyValues( EWidgetPropertyIdCount ),
       
   118       iBlanketPermGranted ( EFalse),
       
   119       iFullView ( EFalse),
       
   120       iMiniView ( EFalse)
       
   121     {
       
   122     }
       
   123 
       
   124 // ============================================================================
       
   125 // CWidgetEntry::~CWidgetEntry()
       
   126 // destructor
       
   127 //
       
   128 // @since 3.1
       
   129 // ============================================================================
       
   130 //
       
   131 CWidgetEntry::~CWidgetEntry()
       
   132     {
       
   133     iPropertyValues.ResetAndDestroy();
       
   134     }
       
   135 
       
   136 // ============================================================================
       
   137 // CWidgetEntry::ConstructL()
       
   138 // symbian second phase constructor
       
   139 //
       
   140 // @since 3.1
       
   141 // ============================================================================
       
   142 //
       
   143 void CWidgetEntry::ConstructL()
       
   144     {
       
   145     }
       
   146 
       
   147 // ============================================================================
       
   148 // CWidgetEntry::InternalizeBinaryL()
       
   149 // read from persistent storage(file)
       
   150 //
       
   151 // @since 3.1
       
   152 // ============================================================================
       
   153 //
       
   154 void CWidgetEntry::InternalizeBinaryL( RReadStream& aReadStream )
       
   155     {
       
   156     for (TInt i = iPropertyValues.Count() ; i < EWidgetPropertyIdCount; i++)
       
   157         {
       
   158         CWidgetPropertyValue* val = CWidgetPropertyValue::NewL();
       
   159         CleanupStack::PushL(val);
       
   160         iPropertyValues.AppendL( val );
       
   161         CleanupStack::Pop( val ); 
       
   162         }
       
   163     // read what should be EWidgetPropertyListVersion
       
   164     (*this)[0].DeserializeL( aReadStream );
       
   165 
       
   166     // For now, leave if version doesn't match compiled-in version,
       
   167     // FUTURE do something smarter
       
   168     if ( ( EWidgetPropTypeUnknown == (*this)[EWidgetPropertyListVersion].iType )
       
   169          || ( WIDGETPROPERTYLISTVERSION != (*this)[EWidgetPropertyListVersion] ) )
       
   170         {
       
   171         User::Leave( KErrCorrupt );
       
   172         }
       
   173 
       
   174     // fill property values array
       
   175     for ( TInt i = 1; i < EWidgetPropertyIdCount; ++i )
       
   176         {
       
   177         (*this)[i].DeserializeL( aReadStream );
       
   178         }
       
   179     }
       
   180 
       
   181 
       
   182 // ============================================================================
       
   183 // CWidgetEntry::InternalizeXmlL()
       
   184 // read from persistent storage(file)
       
   185 //
       
   186 // @since 5.0
       
   187 // ============================================================================
       
   188 //
       
   189 void CWidgetEntry::InternalizeXmlL( RFs& aFileSession,
       
   190                                     xmlDocPtr aDoc,
       
   191                                     xmlNode* n,
       
   192                                     CWidgetRegistryXml* aXmlProcessor )
       
   193     {
       
   194     // <prop>name<val>value<type>typename</type></val></prop>
       
   195     //
       
   196     // prop subtree traversal assumes strict adherence to the
       
   197     // prototype structure, otherwise code leaves with corrupt code
       
   198     for ( ;
       
   199           n;
       
   200           )
       
   201         {
       
   202         // permit some non element stuff (comment, whitespace...) before <prop>
       
   203         while ( n && ( n->type != XML_ELEMENT_NODE ) )
       
   204             {
       
   205             n = n->next;
       
   206             }
       
   207         if ( NULL == n )
       
   208             {
       
   209             for (TInt i = iPropertyValues.Count(); i < EWidgetPropertyIdCount; i++)
       
   210                 {
       
   211                 CWidgetPropertyValue* val = CWidgetPropertyValue::NewL();
       
   212                 CleanupStack::PushL(val);
       
   213                 iPropertyValues.AppendL( val );
       
   214                 CleanupStack::Pop(); // val
       
   215                 }
       
   216             return;
       
   217             }
       
   218         TPtrC8 propTag( n->name );
       
   219         if ( 0 != propTag.Compare( KXmlPropTag() ) )
       
   220             {
       
   221             // TODO unrecognized subtree?
       
   222             return;
       
   223             }
       
   224         // TODO validate n->children != NULL and type XML_TEXT_NODE
       
   225         HBufC* name;
       
   226         aXmlProcessor->GetContentL( aFileSession, aDoc, n->children, &name );
       
   227 
       
   228         // get value array index (TWidgetPropertyId) for name
       
   229         TPtr namePtr( name->Des() );
       
   230         TInt propId =
       
   231             aXmlProcessor->GetPropertyId( namePtr );
       
   232         delete name;
       
   233         name = NULL;
       
   234         if ( EWidgetPropertyIdInvalid == propId )
       
   235             {
       
   236             User::Leave( KErrNoMemory );
       
   237             }
       
   238 
       
   239         for (TInt i = iPropertyValues.Count(); i <= propId; i++)
       
   240             {
       
   241             CWidgetPropertyValue* val = CWidgetPropertyValue::NewL();
       
   242             CleanupStack::PushL(val);
       
   243             iPropertyValues.AppendL( val );
       
   244             CleanupStack::Pop(); // val
       
   245             }
       
   246 
       
   247         n = n->children->next; // down to val
       
   248         if ( NULL == n )
       
   249             {
       
   250             User::Leave( KErrCorrupt );
       
   251             }
       
   252         TPtrC8 valTag( n->name );
       
   253         if ( 0 != valTag.Compare( KXmlValTag() ) )
       
   254             {
       
   255             User::Leave( KErrCorrupt );
       
   256             }
       
   257         if (propId >= EWidgetPropertyIdCount) // unsupported property
       
   258             {
       
   259             HBufC* value = NULL;
       
   260             if (n->children)
       
   261                 {
       
   262                 aXmlProcessor->GetTextContentAsStringL( aFileSession, aDoc, n->children, &value );
       
   263                 }
       
   264             else
       
   265                 {
       
   266                 value = KNullDesC().AllocL();
       
   267                 }
       
   268             (*this)[propId].iValue.s = value;
       
   269             (*this)[propId].iType = EWidgetPropTypeBlob;
       
   270             n = (n->parent)->next; // up two and next sibling
       
   271             continue;
       
   272             }
       
   273         // TODO validate n->children != NULL and type XML_TEXT_NODE
       
   274         HBufC* value;
       
   275         aXmlProcessor->GetContentL( aFileSession, aDoc, n->children, &value );
       
   276         CleanupStack::PushL( value );
       
   277 
       
   278         n = n->children->next; // down to type
       
   279         if ( NULL == n )
       
   280             {
       
   281             User::Leave( KErrCorrupt );
       
   282             }
       
   283         TPtrC8 typeTag( n->name );
       
   284         if ( 0 != typeTag.Compare( KXmlTypeTag() ) )
       
   285             {
       
   286             User::Leave( KErrCorrupt );
       
   287             }
       
   288         // TODO validate n->children != NULL and type XML_TEXT_NODE
       
   289         HBufC* type;
       
   290         aXmlProcessor->GetContentL( aFileSession, aDoc, n->children, &type );
       
   291         CleanupStack::PushL( type );
       
   292 
       
   293         // now have: name, value, type
       
   294         // convert type string to TWidgetPropertyType
       
   295         //
       
   296         // assume void/unknown is not put in XML format so anything
       
   297         // not recognized should be handled like other unrecognized
       
   298         // subtree
       
   299         TWidgetPropertyType typeEnum = EWidgetPropTypeUnknown;
       
   300         if ( 0 == type->Des().Compare( KXmlDataTypeBool() ) )
       
   301             {
       
   302             typeEnum = EWidgetPropTypeBool;
       
   303             }
       
   304         else if ( 0 == type->Des().Compare( KXmlDataTypeInt() ) )
       
   305             {
       
   306             typeEnum = EWidgetPropTypeInt;
       
   307             }
       
   308         else if ( 0 == type->Des().Compare( KXmlDataTypeString() ) )
       
   309             {
       
   310             typeEnum = EWidgetPropTypeString;
       
   311             }
       
   312         else if ( 0 == type->Des().Compare( KXmlDataTypeUid() ) )
       
   313             {
       
   314             typeEnum = EWidgetPropTypeUid;
       
   315             }
       
   316         CleanupStack::PopAndDestroy( type );
       
   317 
       
   318         // TODO handle unknown type due to future extensions: add prop
       
   319         // subtree to list of unrecognized subtrees
       
   320 
       
   321         // set prop according to type
       
   322         switch ( typeEnum )
       
   323             {
       
   324         case EWidgetPropTypeBool:
       
   325             if ( 0 == value->Des().Compare( _L("0") ) )
       
   326                 {
       
   327                 (*this)[propId].iValue.i = 0;
       
   328                 }
       
   329             else
       
   330                 {
       
   331                 (*this)[propId].iValue.i = 1;
       
   332                 }
       
   333             break;
       
   334         case EWidgetPropTypeInt:
       
   335             {
       
   336             TLex toInt( value->Des() );
       
   337             TInt k;
       
   338             if ( KErrNone != toInt.Val( k ) )
       
   339                 {
       
   340                 User::Leave( KErrCorrupt );
       
   341                 }
       
   342             (*this)[propId].iValue.i = k;
       
   343             }
       
   344             break;
       
   345         case EWidgetPropTypeString:
       
   346             (*this)[propId].iValue.s = value;
       
   347             break;
       
   348         case EWidgetPropTypeUid:
       
   349             {
       
   350             TLex toUid( value->Des() );
       
   351             TInt u;
       
   352             if ( KErrNone != toUid.Val( u ) )
       
   353                 {
       
   354                 User::Leave( KErrCorrupt );
       
   355                 }
       
   356             (*this)[propId].iValue.uid = TUid::Uid( u );
       
   357             }
       
   358             break;
       
   359             };
       
   360 
       
   361         (*this)[propId].iType = typeEnum;
       
   362 
       
   363         CleanupStack::Pop( value );
       
   364         if ( EWidgetPropTypeString != typeEnum )
       
   365             {
       
   366             delete value;
       
   367             }
       
   368 
       
   369         n = ((n->parent)->parent)->next; // up two and next sibling
       
   370         }
       
   371     }
       
   372 
       
   373 
       
   374 // ============================================================================
       
   375 // CWidgetEntry::ExternalizeBinaryL()
       
   376 // write to persistent storage(file)
       
   377 //
       
   378 // @since 3.1
       
   379 // ============================================================================
       
   380 //
       
   381 void CWidgetEntry::ExternalizeBinaryL( RWriteStream& aWriteStream )
       
   382     {
       
   383     TInt i = 0;
       
   384     for ( ; i < EWidgetPropertyIdCount; ++i )
       
   385         {
       
   386         (*this)[i].SerializeL( aWriteStream );
       
   387         }
       
   388     }
       
   389 
       
   390 void CWidgetEntry::ExternalizeXmlL( RWriteStream& aWriteStream,
       
   391                                     CWidgetRegistryXml* aXmlProcessor,
       
   392                                     RFs& aFileSession )
       
   393     {
       
   394     xmlDocPtr doc = NULL; // not really used
       
   395     TInt i = 0;
       
   396     // For each property, write an XML entry
       
   397     for ( ; i < EWidgetPropertyIdCount; ++i )
       
   398         {
       
   399         // skip props without values
       
   400         if ( EWidgetPropTypeUnknown == (*this)[i].iType )
       
   401             {
       
   402             continue;
       
   403             }
       
   404 
       
   405         TBuf<KMaxFileName> str;
       
   406         // <prop>name
       
   407         str.Append( KXmlPropStart );
       
   408         str.Append( aXmlProcessor->XmlPropertyName( i ) );  
       
   409         aWriteStream.WriteL( str );  
       
   410         // <val>value
       
   411         str.SetLength( 0 );
       
   412         str.Append( KXmlValStart );
       
   413         aWriteStream.WriteL( str );
       
   414         str.SetLength( 0 );
       
   415         switch ( (*this)[i].iType )
       
   416             {
       
   417         case EWidgetPropTypeBool:
       
   418             {
       
   419             TInt j = (*this)[i];
       
   420             if ( 0 == j )
       
   421                 {
       
   422                 str.Append( _L("0") );
       
   423                 }
       
   424             else
       
   425                 {
       
   426                 str.Append( _L("1") );
       
   427                 }
       
   428             }
       
   429             break;
       
   430         case EWidgetPropTypeInt:
       
   431             {
       
   432             TInt k = (*this)[i];
       
   433             str.AppendFormat( _L("%d"), k );
       
   434             }
       
   435             break;
       
   436         case EWidgetPropTypeString:
       
   437             {
       
   438             str.Append( (*this)[i] );
       
   439             }
       
   440             break;
       
   441         case EWidgetPropTypeUid:
       
   442             const TUid& u = (*this)[i];
       
   443             TInt l = u.iUid;
       
   444             str.AppendFormat( _L("%d"), l );
       
   445             break;
       
   446             };
       
   447         aWriteStream.WriteL( str );
       
   448 
       
   449         // <type>type
       
   450         str.SetLength( 0 );
       
   451         str.Append( KXmlTypeStart );
       
   452         switch ( (*this)[i].iType )
       
   453             {
       
   454         case EWidgetPropTypeBool:
       
   455             str.Append( _L("bool") );
       
   456             break;
       
   457         case EWidgetPropTypeInt:
       
   458             str.Append( _L("int") );
       
   459             break;
       
   460         case EWidgetPropTypeString:
       
   461             str.Append( _L("string") );
       
   462             break;
       
   463         case EWidgetPropTypeUid:
       
   464             str.Append( _L("uid") );
       
   465             break;
       
   466             };
       
   467         aWriteStream.WriteL( str );
       
   468 
       
   469         // </type></val></prop>
       
   470         str.SetLength( 0 );
       
   471         str.Append( KXmlTypeEnd );
       
   472         str.Append( KXmlValEnd );
       
   473         str.Append( KXmlPropEnd );
       
   474         str.Append( KXmlNewline );
       
   475         aWriteStream.WriteL( str );
       
   476         }
       
   477 
       
   478     for ( ; i < iPropertyValues.Count(); ++i )
       
   479         {
       
   480         TBuf<KMaxFileName> str;
       
   481         // <prop>name
       
   482         str.Append( KXmlPropStart );
       
   483         str.Append( aXmlProcessor->XmlPropertyName( i ) );
       
   484         aWriteStream.WriteL( str );
       
   485 
       
   486         // <val>value
       
   487         str.SetLength( 0 );
       
   488         str.Append( KXmlValStart );
       
   489         aWriteStream.WriteL( str );
       
   490         str.SetLength( 0 );
       
   491         const HBufC* s = (iPropertyValues[i])->iValue.s;
       
   492         str.Append( *s );
       
   493         aWriteStream.WriteL( str );
       
   494         str.SetLength( 0 );
       
   495         str.Append( KXmlValEnd );
       
   496         str.Append( KXmlPropEnd );
       
   497         str.Append( KXmlNewline );
       
   498         aWriteStream.WriteL( str );
       
   499         }
       
   500     }
       
   501 
       
   502 
       
   503 // ============================================================================
       
   504 // CWidgetEntry::Active()
       
   505 // Is widget running? 0 if not, non zero if running.
       
   506 //
       
   507 // @since 3.1
       
   508 // ============================================================================
       
   509 //
       
   510 TInt CWidgetEntry::ActiveL()
       
   511     {
       
   512     if ( iActive )
       
   513         {
       
   514         // check that WidgetUI didn't crash, this assumes all widgets
       
   515         // in the registry are running under WidgetUI
       
   516         RWsSession wsSession;
       
   517         User::LeaveIfError( wsSession.Connect() );
       
   518         CleanupClosePushL( wsSession );
       
   519         TApaTaskList taskList( wsSession );
       
   520         TApaTask task = taskList.FindApp( KUidWidgetUi );
       
   521         if ( EFalse == task.Exists() )
       
   522             {
       
   523             // widget UI crashed, reset active
       
   524             iActive = 0;
       
   525             }
       
   526         CleanupStack::PopAndDestroy( &wsSession );
       
   527         }
       
   528     return iActive;
       
   529 }
       
   530 
       
   531 // ============================================================================
       
   532 // CWidgetEntry::SapiAccessState()
       
   533 // Does widget have sapi access? after accepting security prompt, promptless or no access.
       
   534 //
       
   535 // @since 5.0
       
   536 // ============================================================================
       
   537 //
       
   538 TInt CWidgetEntry::SapiAccessState()
       
   539     {
       
   540     if( GetFullViewState() && !GetMiniViewState())
       
   541         {
       
   542         return SAPISECURITYPROMPTNEEDED;
       
   543         }
       
   544     else if ( GetMiniViewState() && GetBlanketPermGranted() )
       
   545         {
       
   546         return SAPIPROMPTLESS;
       
   547         }
       
   548     else
       
   549         {
       
   550         return SAPIACCESSDENIED;
       
   551         }
       
   552         
       
   553     }
       
   554 
       
   555 //  End of File