idlefw/plugins/mcsplugin/publisher/src/mcsplugindata.cpp
changeset 85 7feec50967db
parent 4 1a2a00e78665
child 86 e492551a0d54
equal deleted inserted replaced
4:1a2a00e78665 85:7feec50967db
     1 /*
       
     2 * Copyright (c) 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:  
       
    15 *
       
    16 */
       
    17 #include <mcsmenuitem.h>
       
    18 #include <mcsmenufilter.h>
       
    19 #include <itemmap.h>
       
    20 #include <aistrcnv.h>
       
    21 
       
    22 #include "mcsplugindata.h"
       
    23 #include "mcspluginengine.h"
       
    24 
       
    25 using namespace HSPluginSettingsIf;
       
    26 
       
    27 _LIT8( KAppUid, "271012080" );
       
    28 _LIT( KMenuAttrParam, "param" );
       
    29 _LIT( KMenuAttrLocked, "locked" );
       
    30 _LIT8( KProperNameType, "type" );
       
    31 _LIT8( KProperNameParam, "param" );
       
    32 _LIT8( KProperNameUid, "uid" );
       
    33 _LIT8( KProperNameView, "view" );
       
    34 _LIT8( KProperValueFolder, "folder" );
       
    35 _LIT8( KProperValueBookmark, "bookmark" );
       
    36 _LIT8( KProperValueAppl, "application" );
       
    37 
       
    38 // ======== LOCAL FUNCTIONS ========
       
    39 
       
    40 static void ItemMapArrayCleanupFunc( TAny* aPointerArray )
       
    41     {
       
    42     RPointerArray<CItemMap>* p = static_cast<RPointerArray<CItemMap>*>( aPointerArray ); 
       
    43     p->ResetAndDestroy();
       
    44     p->Close();
       
    45     }
       
    46 
       
    47 // ======== MEMBER FUNCTIONS ========
       
    48 
       
    49 void TMCSData::SetMenuItem( TMenuItem& aMenuItem )
       
    50     {
       
    51     iMenuItem = aMenuItem;
       
    52     }
       
    53 
       
    54 TMenuItem& TMCSData::MenuItem()
       
    55     {
       
    56     return iMenuItem;
       
    57     }
       
    58 
       
    59 void TMCSData::SetDirty( TBool aDirty )
       
    60     {
       
    61     iDirty = aDirty;
       
    62     }
       
    63 
       
    64 TBool TMCSData::IsDirty() const
       
    65     {
       
    66     return iDirty;
       
    67     }
       
    68     
       
    69 // ---------------------------------------------------------------------------
       
    70 // Symbian 2nd phase constructor can leave
       
    71 // ---------------------------------------------------------------------------
       
    72 //
       
    73 CMCSPluginData* CMCSPluginData::NewL( CMCSPluginEngine& aEngine, const TDesC8& aInstanceUid )
       
    74     {
       
    75     CMCSPluginData* self = new (ELeave) CMCSPluginData( aEngine, aInstanceUid );
       
    76     CleanupStack::PushL( self );
       
    77     self->ConstructL();
       
    78     CleanupStack::Pop( self );
       
    79  
       
    80     return self;
       
    81     }
       
    82     
       
    83 // ---------------------------------------------------------------------------
       
    84 // Default constructor
       
    85 // ---------------------------------------------------------------------------
       
    86 //
       
    87 CMCSPluginData::CMCSPluginData( CMCSPluginEngine& aEngine, const TDesC8& aInstanceUid )
       
    88     : iEngine( aEngine ), iInstanceUid( aInstanceUid )
       
    89     {
       
    90     }
       
    91     
       
    92 // ---------------------------------------------------------------------------
       
    93 // Symbian 2nd phase constructor can leave
       
    94 // ---------------------------------------------------------------------------
       
    95 //
       
    96 void CMCSPluginData::ConstructL()
       
    97     {
       
    98      // AILaunch uid in decimal format
       
    99     iPluginSettings = CHomescreenSettings::NewL( KAppUid,
       
   100                                                  iInstanceUid,
       
   101                                                  this );
       
   102     UpdateDataL();
       
   103     }
       
   104 
       
   105 // ---------------------------------------------------------------------------
       
   106 // Destructor
       
   107 // ---------------------------------------------------------------------------
       
   108 //
       
   109 CMCSPluginData::~CMCSPluginData()
       
   110     {   
       
   111     iData.Close();
       
   112     delete iPluginSettings;
       
   113     }
       
   114 
       
   115 // ---------------------------------------------------------------------------
       
   116 // Gets the instance specific settings from HSPS and creates data items
       
   117 // ---------------------------------------------------------------------------
       
   118 //
       
   119 void CMCSPluginData::UpdateDataL()
       
   120     {
       
   121     RPointerArray<CItemMap> settings;
       
   122     TCleanupItem settingsCleanupItem( ItemMapArrayCleanupFunc, &settings );
       
   123     CleanupStack::PushL( settingsCleanupItem );
       
   124     iPluginSettings->GetSettingsL( iInstanceUid, settings );
       
   125     TInt count = settings.Count();
       
   126     for( TInt i = 0; i < count; i++ )
       
   127        {
       
   128         CItemMap* itemMap = settings[i];
       
   129         RPointerArray<HSPluginSettingsIf::CPropertyMap>& properties
       
   130             = itemMap->Properties();
       
   131         TMenuItem item = CreateMenuItemL( properties );
       
   132         TMCSData data;
       
   133         TInt id = -1;
       
   134         
       
   135         if ( count == iData.Count() )
       
   136             {
       
   137             id = iData[i].MenuItem().Id();
       
   138             }
       
   139                    
       
   140         if ( id > 0 )
       
   141             {
       
   142             if ( item.Id() != id )
       
   143                 {
       
   144                 data.SetMenuItem( item );
       
   145                 data.SetDirty( ETrue );
       
   146                 iData.Remove( i );
       
   147                 iData.InsertL( data, i );
       
   148                 }
       
   149         	}
       
   150         else
       
   151             {
       
   152             data.SetMenuItem( item );
       
   153             data.SetDirty( ETrue );
       
   154             iData.AppendL( data );
       
   155         	}
       
   156         }
       
   157     
       
   158         CleanupStack::PopAndDestroy(); // settingsCleanupItem
       
   159     }
       
   160 
       
   161 // ---------------------------------------------------------------------------
       
   162 // 
       
   163 // ---------------------------------------------------------------------------
       
   164 //
       
   165 TInt CMCSPluginData::SettingsChangedL( const TDesC8& /*aEvent*/,  const TDesC8& /*aPluginName*/,
       
   166                            const TDesC8& /*aPluginUid*/, const TDesC8& /*aPluginId*/ )
       
   167     {
       
   168     UpdateDataL();
       
   169     return KErrNone;
       
   170     }
       
   171 
       
   172 // ---------------------------------------------------------------------------
       
   173 // Gets the menu item from engine using the setting properties as filter
       
   174 // ---------------------------------------------------------------------------
       
   175 //
       
   176 TMenuItem CMCSPluginData::CreateMenuItemL( RPointerArray<HSPluginSettingsIf::CPropertyMap>& aProperties )
       
   177     {
       
   178     CMenuFilter* filter = CMenuFilter::NewLC();
       
   179 
       
   180     // Exclude 'view' and 'param' attributes from search criteria by default
       
   181     // Criterias will be added to filter if setting defines them
       
   182     filter->DoNotHaveAttributeL( KMenuAttrView );
       
   183     filter->DoNotHaveAttributeL( KMenuAttrParam );
       
   184     TBool isFolder = EFalse;
       
   185     
       
   186     // first, we need to check if the item is folder
       
   187     for ( TInt i = 0; i < aProperties.Count(); i++ )
       
   188         {
       
   189         if ( aProperties[i]->Name() == KProperNameType )
       
   190             {
       
   191             if ( aProperties[i]->Value() == KProperValueFolder )
       
   192                 {
       
   193                 isFolder = ETrue;
       
   194                 }
       
   195             break;
       
   196             }
       
   197         }
       
   198 
       
   199     // then add all property/value pairs to the filter
       
   200     for ( TInt i = 0; i < aProperties.Count(); i++ )
       
   201         {
       
   202 
       
   203         // skip the type property
       
   204         if( aProperties[i]->Name() == KProperNameType )
       
   205             {
       
   206             continue;
       
   207             }
       
   208 
       
   209         HBufC* name( NULL );
       
   210         HBufC* value( NULL );
       
   211         name = AiUtility::CopyToBufferL( name, aProperties[i]->Name() );
       
   212         CleanupStack::PushL( name );
       
   213         value = AiUtility::CopyToBufferL( value, aProperties[i]->Value() );
       
   214         CleanupStack::PushL( value );
       
   215 
       
   216         if ( value->Length() != 0 )
       
   217             {
       
   218             // in case of folder, we just have to extract 
       
   219             // id from param attribute and return item with this id
       
   220             if ( aProperties[i]->Name() ==  KProperNameParam && isFolder )
       
   221                 {
       
   222                 TMenuItem item;
       
   223                 // convert id to integer
       
   224                 TInt id;
       
   225                 TLex16 lextmp( value->Ptr() );
       
   226                 lextmp.Val( id );
       
   227                 item.SetType( KMenuTypeFolder );
       
   228                 item.SetId( id );
       
   229 
       
   230                 CleanupStack::PopAndDestroy( value );
       
   231                 CleanupStack::PopAndDestroy( name );
       
   232                 CleanupStack::PopAndDestroy( filter );
       
   233 
       
   234                 return item;
       
   235                 }
       
   236             else
       
   237                 {
       
   238                 // otherwise, we just add name/value into filter
       
   239                 filter->HaveAttributeL( *name, *value );
       
   240                 }
       
   241             }
       
   242         CleanupStack::PopAndDestroy( value );
       
   243         CleanupStack::PopAndDestroy( name );
       
   244         }
       
   245     
       
   246     // locked property excluded from search pattern 
       
   247     filter->DoNotHaveAttributeL( KMenuAttrLocked );
       
   248     
       
   249     TMenuItem item = iEngine.FindMenuItemL( *filter );
       
   250     CleanupStack::PopAndDestroy( filter );
       
   251     return item;
       
   252     }
       
   253 
       
   254 // ---------------------------------------------------------------------------
       
   255 // Returns menu item for given index
       
   256 // ---------------------------------------------------------------------------
       
   257 //
       
   258 TMCSData& CMCSPluginData::DataItemL( TInt aIndex )
       
   259     {
       
   260     if( aIndex < 0 || aIndex >= iData.Count())
       
   261         {
       
   262         User::Leave( KErrArgument ); 
       
   263         }
       
   264     return iData[aIndex];
       
   265     }
       
   266 
       
   267 // ---------------------------------------------------------------------------
       
   268 // Replaces menuitem in data instance
       
   269 // ---------------------------------------------------------------------------
       
   270 //
       
   271 void CMCSPluginData::ReplaceMenuItemL( const TInt& aIndex, TMenuItem& aMenuItem )
       
   272     {
       
   273     TMCSData& data = iData[aIndex];
       
   274     data.SetMenuItem( aMenuItem );
       
   275     data.SetDirty( ETrue );
       
   276     }
       
   277 
       
   278 // ---------------------------------------------------------------------------
       
   279 // Save the setting persistently to HSPS
       
   280 // TODO HSPS setting api should be changed so that items and properties can 
       
   281 // be added/removed dynamically. Now widgetconfiguration.xml must have all the
       
   282 // properties for every item even though property is not used.
       
   283 // It makes this function more compolicated.
       
   284 // ---------------------------------------------------------------------------
       
   285 //
       
   286 void CMCSPluginData::SaveSettingsL( const TInt& aIndex, CMenuItem& aMenuItem )
       
   287     {
       
   288     RPointerArray<CItemMap> settingItems;
       
   289     CleanupClosePushL( settingItems );
       
   290     iPluginSettings->GetSettingsL( iInstanceUid, settingItems );
       
   291     if ( aIndex >= 0 && aIndex < settingItems.Count() )
       
   292         {
       
   293         TBool exists( EFalse );
       
   294         CItemMap* itemMap = settingItems[aIndex];
       
   295         RPointerArray<HSPluginSettingsIf::CPropertyMap> properties;
       
   296         properties = itemMap->Properties();
       
   297         for ( TInt i= 0; i < properties.Count(); i++ )
       
   298             {
       
   299             if ( properties[i]->Name() == KProperNameType )
       
   300                 {
       
   301                 TPtrC type = aMenuItem.Type();
       
   302                 if ( type == KMenuTypeUrl )
       
   303                     {
       
   304                     properties[i]->SetValueL( KProperValueBookmark );
       
   305                     }
       
   306                 else
       
   307                     {
       
   308                     properties[i]->SetValueL( KProperValueAppl );
       
   309                     }
       
   310                 }
       
   311             else if ( properties[i]->Name() == KProperNameUid )
       
   312                 {
       
   313                 TPtrC uid = aMenuItem.GetAttributeL( KMenuAttrUid, exists );
       
   314                 if ( exists )
       
   315                     {
       
   316                     HBufC8* uid8( NULL );
       
   317                     uid8 = AiUtility::CopyToBufferL( uid8, uid );
       
   318                     CleanupStack::PushL( uid8 );
       
   319                     properties[i]->SetValueL( *uid8 );
       
   320                     CleanupStack::PopAndDestroy( uid8 );
       
   321                     }
       
   322                 else
       
   323                     {
       
   324                     properties[i]->SetValueL( KNullDesC8 );
       
   325                     }
       
   326                 }
       
   327             else if ( properties[i]->Name() == KProperNameView )
       
   328                 {
       
   329                 TPtrC view = aMenuItem.GetAttributeL( KMenuAttrView, exists );
       
   330                 if ( exists )
       
   331                     {
       
   332                     HBufC8* view8( NULL );
       
   333                     view8 = AiUtility::CopyToBufferL( view8, view );
       
   334                     CleanupStack::PushL( view8 );
       
   335                     properties[i]->SetValueL( *view8 );
       
   336                     CleanupStack::PopAndDestroy( view8 );
       
   337                     }
       
   338                 else
       
   339                     {
       
   340                     properties[i]->SetValueL( KNullDesC8 );
       
   341                     }
       
   342                 }
       
   343             else if ( properties[i]->Name() == KProperNameParam )
       
   344                 {
       
   345                 TPtrC param = aMenuItem.GetAttributeL( KMenuAttrParam, exists );
       
   346                 if ( exists )
       
   347                     {
       
   348                     HBufC8* param8( NULL );
       
   349                     param8 = AiUtility::CopyToBufferL( param8, param );
       
   350                     CleanupStack::PushL( param8 );
       
   351                     properties[i]->SetValueL( *param8 );
       
   352                     CleanupStack::PopAndDestroy( param8 );
       
   353                     }
       
   354                 else
       
   355                     {
       
   356                     properties[i]->SetValueL( KNullDesC8 );
       
   357                     }
       
   358                 }
       
   359             }
       
   360         }
       
   361 		// ETrue tells that changes are stored also to plugin reference 
       
   362     iPluginSettings->SetSettingsL( iInstanceUid, settingItems, ETrue );
       
   363     CleanupStack::PopAndDestroy( &settingItems );
       
   364     }
       
   365