idlefw/plugins/mcsplugin/publisher/src/mcsplugindata.cpp
branchRCL_3
changeset 14 15e4dd19031c
parent 12 502e5d91ad42
child 16 b276298d5729
equal deleted inserted replaced
12:502e5d91ad42 14:15e4dd19031c
     1 /*
     1 /*
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
     2 * Copyright (c) 2009 - 2010 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     3 * All rights reserved.
     4 * This component and the accompanying materials are made available
     4 * This component and the accompanying materials are made available
     5 * under the terms of "Eclipse Public License v1.0"
     5 * under the terms of "Eclipse Public License v1.0"
     6 * which accompanies this distribution, and is available
     6 * which accompanies this distribution, and is available
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
    16 */
    16 */
    17 #include <mcsmenuitem.h>
    17 #include <mcsmenuitem.h>
    18 #include <mcsmenufilter.h>
    18 #include <mcsmenufilter.h>
    19 #include <itemmap.h>
    19 #include <itemmap.h>
    20 #include <aistrcnv.h>
    20 #include <aistrcnv.h>
    21 #include <favouritesdbobserver.h>
       
    22 #include <favouritesdb.h>        
       
    23 #include <msvuids.h>        // For KMsvRootIndexEntryIdValue
       
    24 
       
    25 #include <mcsmenuitem.h>
       
    26 #include <mcsmenufilter.h>
       
    27 
    21 
    28 #include "mcsplugindata.h"
    22 #include "mcsplugindata.h"
    29 #include "mcspluginengine.h"
    23 #include "mcspluginengine.h"
    30 
    24 
    31 using namespace HSPluginSettingsIf;
    25 using namespace HSPluginSettingsIf;
    34 _LIT( KMenuAttrLocked, "locked" );
    28 _LIT( KMenuAttrLocked, "locked" );
    35 _LIT8( KProperNameType, "type" );
    29 _LIT8( KProperNameType, "type" );
    36 _LIT8( KProperNameParam, "param" );
    30 _LIT8( KProperNameParam, "param" );
    37 _LIT8( KProperNameUid, "uid" );
    31 _LIT8( KProperNameUid, "uid" );
    38 _LIT8( KProperNameView, "view" );
    32 _LIT8( KProperNameView, "view" );
       
    33 _LIT8( KProperNameLocked, "locked" );
    39 _LIT8( KProperValueFolder, "folder" );
    34 _LIT8( KProperValueFolder, "folder" );
    40 _LIT8( KProperValueBookmark, "bookmark" );
    35 _LIT8( KProperValueBookmark, "bookmark" );
    41 _LIT8( KProperValueAppl, "application" );
    36 _LIT8( KProperValueAppl, "application" );
    42 
    37 _LIT8( KProperValueMailbox, "mailbox" );
    43 _LIT( KMailboxUid, "0x100058c5" );
    38 _LIT8( KMenuAttrUndefUid, "0x99999991" );
    44 _LIT8( KMailboxUid8, "0x100058c5" );
    39 
    45 _LIT( KMenuMailboxIconId, "16388" );
       
    46 _LIT( KMenuMailboxMaskId, "16389" );
       
    47 
       
    48 _LIT( KUrl, "url" );
       
    49 _LIT( KMenuIconFile, "aimcsplugin.mif" );
       
    50 _LIT( KMenuBookmarkIconId, "16386" );
       
    51 _LIT( KMenuBookmarkMaskId, "16387" );
       
    52 _LIT( KInitialRefCount, "1" );
       
    53 _LIT( KMenuAttrRefcount, "ref_count" );
       
    54 _LIT( KMyMenuData, "matrixmenudata" );
    40 _LIT( KMyMenuData, "matrixmenudata" );
    55 _LIT( KMenuTypeShortcut, "menu:shortcut" );
    41 _LIT( KMenuTypeMailbox, "menu:mailbox" );
    56 _LIT( KMenuAttrParameter, "param" );
    42 
    57 
    43 
    58 #define KMCSCmailMtmUidValue 0x2001F406
    44 #define KMCSCmailMtmUidValue 0x2001F406
    59 
    45 
    60 // ======== LOCAL FUNCTIONS ========
    46 // ======== LOCAL FUNCTIONS ========
    61 
    47 
    66     p->Close();
    52     p->Close();
    67     }
    53     }
    68 
    54 
    69 // ======== MEMBER FUNCTIONS ========
    55 // ======== MEMBER FUNCTIONS ========
    70 
    56 
    71 void TMCSData::SetMenuItem( TMenuItem& aMenuItem )
    57 // ---------------------------------------------------------------------------
       
    58 // Default constructor
       
    59 // ---------------------------------------------------------------------------
       
    60 //
       
    61 CMCSData::CMCSData()
       
    62     {
       
    63     }
       
    64 
       
    65 // ---------------------------------------------------------------------------
       
    66 // Destructor
       
    67 // ---------------------------------------------------------------------------
       
    68 //
       
    69 CMCSData::~CMCSData()
       
    70     {
       
    71     delete iName;
       
    72     delete iValue;
       
    73     }
       
    74 
       
    75 // ---------------------------------------------------------------------------
       
    76 // 
       
    77 // ---------------------------------------------------------------------------
       
    78 //
       
    79 void CMCSData::SetMenuItem( TMenuItem& aMenuItem )
    72     {
    80     {
    73     iMenuItem = aMenuItem;
    81     iMenuItem = aMenuItem;
    74     }
    82     }
    75 
    83 
    76 TMenuItem& TMCSData::MenuItem()
    84 // ---------------------------------------------------------------------------
       
    85 // 
       
    86 // ---------------------------------------------------------------------------
       
    87 //
       
    88 TMenuItem& CMCSData::MenuItem()
    77     {
    89     {
    78     return iMenuItem;
    90     return iMenuItem;
    79     }
    91     }
    80 
    92 
    81 void TMCSData::SetDirty( TBool aDirty )
    93 // ---------------------------------------------------------------------------
       
    94 // 
       
    95 // ---------------------------------------------------------------------------
       
    96 //
       
    97 TDesC& CMCSData::Name()
       
    98     {
       
    99     return *iName;
       
   100     }
       
   101     
       
   102 // ---------------------------------------------------------------------------
       
   103 // 
       
   104 // ---------------------------------------------------------------------------
       
   105 //
       
   106 void CMCSData::SetNameL( const TDesC& aName )
       
   107     {
       
   108 	delete iName;
       
   109 	iName = NULL;
       
   110     iName = aName.AllocL();
       
   111     }
       
   112 
       
   113 // ---------------------------------------------------------------------------
       
   114 // 
       
   115 // ---------------------------------------------------------------------------
       
   116 //
       
   117 TDesC& CMCSData::Value()
       
   118     {
       
   119     return *iValue;
       
   120     }
       
   121     
       
   122 // ---------------------------------------------------------------------------
       
   123 // 
       
   124 // ---------------------------------------------------------------------------
       
   125 //
       
   126 void CMCSData::SetValueL( const TDesC& aValue )
       
   127     {
       
   128 	delete iValue;
       
   129 	iValue = NULL;
       
   130     iValue = aValue.AllocL();
       
   131     }
       
   132 
       
   133 // ---------------------------------------------------------------------------
       
   134 // 
       
   135 // ---------------------------------------------------------------------------
       
   136 //
       
   137 void CMCSData::SetDirty( TBool aDirty )
    82     {
   138     {
    83     iDirty = aDirty;
   139     iDirty = aDirty;
    84     }
   140     }
    85 
   141 
    86 TBool TMCSData::IsDirty() const
   142 // ---------------------------------------------------------------------------
       
   143 // 
       
   144 // ---------------------------------------------------------------------------
       
   145 //
       
   146 TBool CMCSData::IsDirty() const
    87     {
   147     {
    88     return iDirty;
   148     return iDirty;
    89     }
   149     }
    90     
   150     
       
   151 
    91 // ---------------------------------------------------------------------------
   152 // ---------------------------------------------------------------------------
    92 // Symbian 2nd phase constructor can leave
   153 // Symbian 2nd phase constructor can leave
    93 // ---------------------------------------------------------------------------
   154 // ---------------------------------------------------------------------------
    94 //
   155 //
    95 CMCSPluginData* CMCSPluginData::NewL( CMCSPluginEngine& aEngine, const TDesC8& aInstanceUid )
   156 CMCSPluginData* CMCSPluginData::NewL( CMCSPluginEngine& aEngine, const TDesC8& aInstanceUid )
   115 // Symbian 2nd phase constructor can leave
   176 // Symbian 2nd phase constructor can leave
   116 // ---------------------------------------------------------------------------
   177 // ---------------------------------------------------------------------------
   117 //
   178 //
   118 void CMCSPluginData::ConstructL()
   179 void CMCSPluginData::ConstructL()
   119     {
   180     {
   120     iMsvSession = CMsvSession::OpenAsObserverL( *this );
       
   121     iPluginSettings = CHomescreenSettings::Instance();
   181     iPluginSettings = CHomescreenSettings::Instance();
   122     if( iPluginSettings == NULL )
   182     if( iPluginSettings == NULL )
   123         {
   183         {
   124         User::Leave( KErrUnknown );
   184         User::Leave( KErrUnknown );
   125         }
   185         }
   126     iPluginSettings->AddObserverL( this );
   186     iPluginSettings->AddObserverL( this );
   127 
   187 
   128     iSaveWatcher = CMCSPluginWatcher::NewL( CMCSPluginWatcher::EOperation );
       
   129 
       
   130     iMenu.OpenL( KMyMenuData );
   188     iMenu.OpenL( KMyMenuData );
   131 
   189 
   132     UpdateDataL();
   190     UpdateDataL();
   133     }
   191     }
   134 
   192 
   140     {
   198     {
   141     if( iPluginSettings )
   199     if( iPluginSettings )
   142         {
   200         {
   143         iPluginSettings->RemoveObserver( this );
   201         iPluginSettings->RemoveObserver( this );
   144         }
   202         }
   145 
   203     iData.ResetAndDestroy();
   146     iData.Close();
       
   147     iMenu.Close();
   204     iMenu.Close();
   148 
       
   149     delete iSaveWatcher;
       
   150     delete iMsvSession;
       
   151     }
   205     }
   152 
   206 
   153 // ---------------------------------------------------------------------------
   207 // ---------------------------------------------------------------------------
   154 // Gets the instance specific settings from HSPS and creates data items
   208 // Gets the instance specific settings from HSPS and creates data items
   155 // ---------------------------------------------------------------------------
   209 // ---------------------------------------------------------------------------
   166     for ( TInt i = 0; i < count; i++ )
   220     for ( TInt i = 0; i < count; i++ )
   167        {
   221        {
   168         CItemMap* itemMap = settings[ i ];
   222         CItemMap* itemMap = settings[ i ];
   169         RPointerArray<HSPluginSettingsIf::CPropertyMap>& properties
   223         RPointerArray<HSPluginSettingsIf::CPropertyMap>& properties
   170             = itemMap->Properties();
   224             = itemMap->Properties();
   171         TMenuItem item = CreateMenuItemL( properties );
   225         CMCSData* data = GetMenuDataL( properties );
   172         TMCSData data;
       
   173 
   226 
   174         if ( wasEmpty )
   227         if ( wasEmpty )
   175             {
   228             {
   176             // list of shortcut slot was empty
   229             // list of shortcut slot was empty
   177             // we append the shortcut data slots one-by-one to the list
   230             // we append the shortcut data slots one-by-one to the list
   178             data.SetMenuItem( item );
   231             data->SetDirty( ETrue );
   179             data.SetDirty( ETrue );
       
   180             iData.AppendL( data );
   232             iData.AppendL( data );
   181             }
   233             }
   182         else 
   234         else 
   183             {
   235             {
   184             // check for updates in existing shortcut data slot
   236             // check for updates in existing shortcut data slot
   185             // if menuitem id has changed, replace the item and 
   237             // if menuitem id has changed, replace the item and 
   186             // set as dirty
   238             // set as dirty
   187             TInt id = -1;
   239             TInt id = -1;
   188             id = iData[ i ].MenuItem().Id();
   240             id = iData[ i ]->MenuItem().Id();
   189             
   241             
   190             if ( item.Id() != id )
   242             if ( data->MenuItem().Id() != id )
   191                 {
   243                 {
   192                 data.SetMenuItem( item );
   244                 data->SetDirty( ETrue );
   193                 data.SetDirty( ETrue );
   245                 CMCSData* oldData = iData[i];
   194                 iData.Remove( i );
   246                 iData.Remove( i );
       
   247                 delete oldData;
   195                 iData.InsertL( data, i );
   248                 iData.InsertL( data, i );
   196                 }
   249                 }
   197             }
   250             }
   198         }
   251         }
   199     
   252     CleanupStack::PopAndDestroy(); // settingsCleanupItem 
   200         CleanupStack::PopAndDestroy(); // settingsCleanupItem 
   253     }
   201     }
   254 
   202 
   255 // ---------------------------------------------------------------------------
   203 
   256 // Removes obsolete data and saves 'Undefined' item to HSPS
   204 // ---------------------------------------------------------------------------
   257 // Used when active mailbox is deleted from system.
   205 // 
   258 // ---------------------------------------------------------------------------
       
   259 //
       
   260 void CMCSPluginData::RemoveDataL( TInt aId )
       
   261     {
       
   262     TInt count = iData.Count();
       
   263     for( TInt i = 0; i < count; i++ )
       
   264         {
       
   265         CMCSData* data = iData[i];
       
   266         if( data->MenuItem().Type() == KMenuTypeMailbox &&
       
   267             data->MenuItem().Id() == aId )
       
   268             {
       
   269             iData[i]->MenuItem().SetId( KErrNotFound );
       
   270             iData[i]->SetDirty( ETrue );
       
   271             SaveUndefinedItemL( i );
       
   272             }
       
   273         }
       
   274     }
       
   275 
       
   276 // ---------------------------------------------------------------------------
       
   277 // Call back from Homescreen settings
   206 // ---------------------------------------------------------------------------
   278 // ---------------------------------------------------------------------------
   207 //
   279 //
   208 void CMCSPluginData::SettingsChangedL( const TDesC8& /*aEvent*/,  const TDesC8& /*aPluginName*/,
   280 void CMCSPluginData::SettingsChangedL( const TDesC8& /*aEvent*/,  const TDesC8& /*aPluginName*/,
   209                            const TDesC8& /*aPluginUid*/, const TDesC8& aPluginId )
   281                            const TDesC8& /*aPluginUid*/, const TDesC8& aPluginId )
   210     {
   282     {
   216 
   288 
   217 // ---------------------------------------------------------------------------
   289 // ---------------------------------------------------------------------------
   218 // Gets the menu item from engine using the setting properties as filter
   290 // Gets the menu item from engine using the setting properties as filter
   219 // ---------------------------------------------------------------------------
   291 // ---------------------------------------------------------------------------
   220 //
   292 //
   221 TMenuItem CMCSPluginData::CreateMenuItemL( RPointerArray<HSPluginSettingsIf::CPropertyMap>& aProperties )
   293 CMCSData* CMCSPluginData::GetMenuDataL( RPointerArray<HSPluginSettingsIf::CPropertyMap>& aProperties )
       
   294     {
       
   295     TPtrC8 type;
       
   296     TPtrC8 uid;
       
   297     TPtrC8 view;
       
   298     TPtrC8 param;
       
   299     
       
   300     // first we need to check the item type
       
   301     for ( TInt i = 0; i < aProperties.Count(); i++ )
       
   302         {
       
   303         if ( aProperties[i]->Name() == KProperNameType )
       
   304             {
       
   305             type.Set( aProperties[i]->Value());
       
   306             }
       
   307         else if( aProperties[i]->Name() == KProperNameUid)
       
   308             {
       
   309             uid.Set( aProperties[i]->Value());
       
   310             }
       
   311         else if( aProperties[i]->Name() == KProperNameView)
       
   312             {
       
   313             view.Set( aProperties[i]->Value());
       
   314             }
       
   315         else if( aProperties[i]->Name() == KProperNameParam )
       
   316             {
       
   317             param.Set( aProperties[i]->Value());
       
   318             }
       
   319         }
       
   320     CMCSData* data = new ( ELeave ) CMCSData();
       
   321     CleanupStack::PushL( data );
       
   322     if( type == KProperValueBookmark )
       
   323         {
       
   324         GetBkmDataL( view, param, *data );
       
   325         }
       
   326     else if( type == KProperValueFolder )
       
   327         {
       
   328         GetFolderData( param, *data );
       
   329         }
       
   330     else if( type == KProperValueMailbox )
       
   331         {
       
   332         GetMailboxDataL( uid, param, *data );
       
   333         }
       
   334     else
       
   335         {
       
   336         GetMCSDataL( aProperties, *data );
       
   337         }
       
   338     CleanupStack::Pop( data );
       
   339     return data;
       
   340     }
       
   341 
       
   342 // ---------------------------------------------------------------------------
       
   343 // Creates bookmark data item.
       
   344 // ---------------------------------------------------------------------------
       
   345 //
       
   346 void CMCSPluginData::GetBkmDataL( const TDesC8& aView, const TDesC8& aParam, CMCSData& aData )
       
   347     {
       
   348     TMenuItem item;
       
   349     item.SetType( KMenuTypeUrl );
       
   350     aData.SetMenuItem( item );
       
   351 
       
   352     HBufC* view( NULL );
       
   353     view = AiUtility::CopyToBufferL( view, aView );
       
   354     CleanupStack::PushL( view );
       
   355     aData.SetValueL( *view );
       
   356     CleanupStack::PopAndDestroy( view );
       
   357     
       
   358     HBufC* param( NULL );
       
   359     param = AiUtility::CopyToBufferL( param, aParam );
       
   360     CleanupStack::PushL( param );
       
   361     aData.SetNameL( *param );
       
   362     CleanupStack::PopAndDestroy( param );
       
   363     }
       
   364     
       
   365 // ---------------------------------------------------------------------------
       
   366 // Creates folder data item.
       
   367 // ---------------------------------------------------------------------------
       
   368 //
       
   369 void CMCSPluginData::GetFolderData( const TDesC8& aParam, CMCSData& aData )
       
   370     {
       
   371     // In folder case, we have to extract id from 
       
   372     // param attribute and return item with this id
       
   373     // convert id to integer
       
   374     TInt id;
       
   375     TLex8 lextmp( aParam);
       
   376     lextmp.Val( id );
       
   377     
       
   378     TMenuItem item;
       
   379     item.SetType( KMenuTypeFolder );
       
   380     item.SetId( id );
       
   381     aData.SetMenuItem( item );
       
   382     }
       
   383 
       
   384 // ---------------------------------------------------------------------------
       
   385 // Creates mailbox data item.
       
   386 // ---------------------------------------------------------------------------
       
   387 //
       
   388 void CMCSPluginData::GetMailboxDataL( const TDesC8& aUid, const TDesC8& aParam, CMCSData& aData )
       
   389     {
       
   390     TInt id( KErrNotFound );
       
   391     TLex8 lextmp( aUid);
       
   392     lextmp.Val( id );
       
   393     
       
   394     TMenuItem item;
       
   395     item.SetType( KMenuTypeMailbox );
       
   396     item.SetId( id );
       
   397     aData.SetMenuItem( item );
       
   398     
       
   399     HBufC* param( NULL );
       
   400     param = AiUtility::CopyToBufferL( param, aParam );
       
   401     CleanupStack::PushL( param );
       
   402     
       
   403     aData.SetNameL( *param );
       
   404     
       
   405     CleanupStack::PopAndDestroy( param );
       
   406     }
       
   407     
       
   408 // ---------------------------------------------------------------------------
       
   409 // Gets data item from MCS
       
   410 // ---------------------------------------------------------------------------
       
   411 //
       
   412 void CMCSPluginData::GetMCSDataL( RPointerArray<HSPluginSettingsIf::CPropertyMap>& aProperties,
       
   413         CMCSData& aData)
   222     {
   414     {
   223     CMenuFilter* filter = CMenuFilter::NewLC();
   415     CMenuFilter* filter = CMenuFilter::NewLC();
   224 
   416 
   225     // Exclude 'view' and 'param' attributes from search criteria by default
   417     // Exclude 'view' and 'param' attributes from search criteria by default
   226     // Criterias will be added to filter if setting defines them
   418     // Criterias will be added to filter if setting defines them
   227     filter->DoNotHaveAttributeL( KMenuAttrView );
   419     filter->DoNotHaveAttributeL( KMenuAttrView );
   228     filter->DoNotHaveAttributeL( KMenuAttrParam );
   420     filter->DoNotHaveAttributeL( KMenuAttrParam );
   229     TBool isFolder = EFalse;
   421     
   230     
       
   231     // first, we need to check if the item is folder
       
   232     for ( TInt i = 0; i < aProperties.Count(); i++ )
       
   233         {
       
   234         if ( aProperties[i]->Name() == KProperNameType )
       
   235             {
       
   236             if ( aProperties[i]->Value() == KProperValueFolder )
       
   237                 {
       
   238                 isFolder = ETrue;
       
   239                 }
       
   240             break;
       
   241             }
       
   242         }
       
   243 
       
   244     // then add all property/value pairs to the filter
   422     // then add all property/value pairs to the filter
   245     for ( TInt i = 0; i < aProperties.Count(); i++ )
   423     for ( TInt i = 0; i < aProperties.Count(); i++ )
   246         {
   424         {
   247 
       
   248         // skip the type property
   425         // skip the type property
   249         if( aProperties[i]->Name() == KProperNameType )
   426         if( aProperties[i]->Name() == KProperNameType )
   250             {
   427             {
   251             continue;
   428             continue;
   252             }
   429             }
   258         value = AiUtility::CopyToBufferL( value, aProperties[i]->Value() );
   435         value = AiUtility::CopyToBufferL( value, aProperties[i]->Value() );
   259         CleanupStack::PushL( value );
   436         CleanupStack::PushL( value );
   260 
   437 
   261         if ( value->Length() != 0 )
   438         if ( value->Length() != 0 )
   262             {
   439             {
   263             // in case of folder, we just have to extract 
   440             filter->HaveAttributeL( *name, *value );  
   264             // id from param attribute and return item with this id
       
   265             if ( aProperties[i]->Name() ==  KProperNameParam && isFolder )
       
   266                 {
       
   267                 TMenuItem item;
       
   268                 // convert id to integer
       
   269                 TInt id;
       
   270                 TLex16 lextmp( value->Ptr() );
       
   271                 lextmp.Val( id );
       
   272                 item.SetType( KMenuTypeFolder );
       
   273                 item.SetId( id );
       
   274 
       
   275                 CleanupStack::PopAndDestroy( value );
       
   276                 CleanupStack::PopAndDestroy( name );
       
   277                 CleanupStack::PopAndDestroy( filter );
       
   278 
       
   279                 return item;
       
   280                 }
       
   281             else
       
   282                 {
       
   283                 // otherwise, we just add name/value into filter
       
   284                 filter->HaveAttributeL( *name, *value );
       
   285                 }
       
   286             }
   441             }
   287         CleanupStack::PopAndDestroy( value );
   442         CleanupStack::PopAndDestroy( value );
   288         CleanupStack::PopAndDestroy( name );
   443         CleanupStack::PopAndDestroy( name );
   289         }
   444         }
   290     
   445     
   291     // locked property excluded from search pattern 
   446     // locked property excluded from search pattern 
   292     filter->DoNotHaveAttributeL( KMenuAttrLocked );
   447     filter->DoNotHaveAttributeL( KMenuAttrLocked );
   293     
   448     
   294     TMenuItem item = iEngine.FindMenuItemL( *filter );
   449     TMenuItem item = iEngine.FindMenuItemL( *filter );
   295     CleanupStack::PopAndDestroy( filter );
   450     CleanupStack::PopAndDestroy( filter );
   296     return item;
   451     aData.SetMenuItem( item );
   297     }
   452     }
   298 
   453 
   299 // ---------------------------------------------------------------------------
   454 // ---------------------------------------------------------------------------
   300 // Returns menu item for given index
   455 // Returns menu item for given index
   301 // ---------------------------------------------------------------------------
   456 // ---------------------------------------------------------------------------
   302 //
   457 //
   303 TMCSData& CMCSPluginData::DataItemL( TInt aIndex )
   458 CMCSData& CMCSPluginData::DataItemL( TInt aIndex )
   304     {
   459     {
   305     if( aIndex < 0 || aIndex >= iData.Count())
   460     if( aIndex < 0 || aIndex >= iData.Count())
   306         {
   461         {
   307         User::Leave( KErrArgument ); 
   462         User::Leave( KErrArgument ); 
   308         }
   463         }
   309     return iData[aIndex];
   464     return *iData[aIndex];
   310     }
   465     }
   311 
   466 
   312 // ---------------------------------------------------------------------------
   467 // ---------------------------------------------------------------------------
   313 // Replaces menuitem in data instance
   468 // Save the undefined item.
   314 // ---------------------------------------------------------------------------
   469 // ---------------------------------------------------------------------------
   315 //
   470 //
   316 void CMCSPluginData::ReplaceMenuItemL( const TInt& aIndex, TMenuItem& aMenuItem )
   471 void CMCSPluginData::SaveUndefinedItemL( const TInt& aIndex )
   317     {
       
   318     TMCSData& data = iData[aIndex];
       
   319     data.SetMenuItem( aMenuItem );
       
   320     data.SetDirty( ETrue );
       
   321     }
       
   322 
       
   323 // ---------------------------------------------------------------------------
       
   324 // Save the setting persistently to HSPS
       
   325 // TODO HSPS setting api should be changed so that items and properties can 
       
   326 // be added/removed dynamically. Now widgetconfiguration.xml must have all the
       
   327 // properties for every item even though property is not used.
       
   328 // It makes this function more compolicated.
       
   329 // ---------------------------------------------------------------------------
       
   330 //
       
   331 void CMCSPluginData::SaveSettingsL( const TInt& aIndex, CMenuItem& aMenuItem )
       
   332     {
   472     {
   333     RPointerArray<CItemMap> settingItems;
   473     RPointerArray<CItemMap> settingItems;
   334     CleanupClosePushL( settingItems );
   474     CleanupClosePushL( settingItems );
   335     iPluginSettings->GetSettingsL( iInstanceUid, settingItems );
   475     iPluginSettings->GetSettingsL( iInstanceUid, settingItems );
   336     if ( aIndex >= 0 && aIndex < settingItems.Count() )
   476     if ( aIndex >= 0 && aIndex < settingItems.Count() )
   337         {
   477         {
   338         TBool exists( EFalse );
   478         CItemMap* itemMap = settingItems[ aIndex ];
   339         CItemMap* itemMap = settingItems[aIndex];
       
   340         RPointerArray<HSPluginSettingsIf::CPropertyMap> properties;
   479         RPointerArray<HSPluginSettingsIf::CPropertyMap> properties;
   341         properties = itemMap->Properties();
   480         properties = itemMap->Properties();
   342         for ( TInt i= 0; i < properties.Count(); i++ )
   481         for ( TInt i = 0; i < properties.Count(); i++ )
   343             {
   482             {
   344             if ( properties[i]->Name() == KProperNameType )
   483             if ( properties[ i ]->Name() == KProperNameType )
   345                 {
   484                 {
   346                 TPtrC type = aMenuItem.Type();
   485                 properties[ i ]->SetValueL( KProperValueAppl );
   347                 if ( type == KMenuTypeUrl )
       
   348                     {
       
   349                     properties[i]->SetValueL( KProperValueBookmark );
       
   350                     }
       
   351                 else
       
   352                     {
       
   353                     properties[i]->SetValueL( KProperValueAppl );
       
   354                     }
       
   355                 }
   486                 }
   356             else if ( properties[i]->Name() == KProperNameUid )
   487             else if ( properties[ i ]->Name() == KProperNameUid )
   357                 {
   488                 {
   358                 TPtrC uid = aMenuItem.GetAttributeL( KMenuAttrUid, exists );
   489                 properties[ i ]->SetValueL( KMenuAttrUndefUid );
   359                 if ( exists )
       
   360                     {
       
   361                     HBufC8* uid8( NULL );
       
   362                     uid8 = AiUtility::CopyToBufferL( uid8, uid );
       
   363                     CleanupStack::PushL( uid8 );
       
   364                     properties[i]->SetValueL( *uid8 );
       
   365                     CleanupStack::PopAndDestroy( uid8 );
       
   366                     }
       
   367                 else
       
   368                     {
       
   369                     properties[i]->SetValueL( KNullDesC8 );
       
   370                     }
       
   371                 }
   490                 }
   372             else if ( properties[i]->Name() == KProperNameView )
   491             else if ( properties[ i ]->Name() == KProperNameView )
   373                 {
   492                 {
   374                 TPtrC view = aMenuItem.GetAttributeL( KMenuAttrView, exists );
   493                 properties[ i ]->SetValueL( KNullDesC8 );
   375                 if ( exists )
       
   376                     {
       
   377                     HBufC8* view8( NULL );
       
   378                     view8 = AiUtility::CopyToBufferL( view8, view );
       
   379                     CleanupStack::PushL( view8 );
       
   380                     properties[i]->SetValueL( *view8 );
       
   381                     CleanupStack::PopAndDestroy( view8 );
       
   382                     }
       
   383                 else
       
   384                     {
       
   385                     properties[i]->SetValueL( KNullDesC8 );
       
   386                     }
       
   387                 }
   494                 }
   388             else if ( properties[i]->Name() == KProperNameParam )
   495             else if ( properties[ i ]->Name() == KProperNameParam )
   389                 {
   496                 {
   390                 TPtrC param = aMenuItem.GetAttributeL( KMenuAttrParam, exists );
   497                 properties[ i ]->SetValueL( KNullDesC8 );
   391                 if ( exists )
       
   392                     {
       
   393                     HBufC8* param8( NULL );
       
   394                     param8 = AiUtility::CopyToBufferL( param8, param );
       
   395                     CleanupStack::PushL( param8 );
       
   396                     properties[i]->SetValueL( *param8 );
       
   397                     CleanupStack::PopAndDestroy( param8 );
       
   398                     }
       
   399                 else
       
   400                     {
       
   401                     properties[i]->SetValueL( KNullDesC8 );
       
   402                     }
       
   403                 }
   498                 }
   404             }
   499             else if ( properties[ i ]->Name() == KProperNameLocked )
   405         }
   500                 {
   406     // ETrue tells that changes are stored also to plugin reference 
   501                 properties[i]->SetValueL( KNullDesC8 );
       
   502                 }
       
   503             }
       
   504         }
       
   505     // ETrue tells that modified settings are stored also to plugin reference
   407     iPluginSettings->SetSettingsL( iInstanceUid, settingItems, ETrue );
   506     iPluginSettings->SetSettingsL( iInstanceUid, settingItems, ETrue );
   408     CleanupStack::PopAndDestroy( &settingItems );
   507     CleanupStack::Pop( &settingItems );
   409     }
   508     settingItems.ResetAndDestroy();
   410 
   509 
   411 // ---------------------------------------------------------------------------
   510     }
   412 // Gets MCS Plugin folder ID. This hidden folder in matrixmenudata.xml is used 
   511 
   413 // for storing run-time generated menuitems
   512 // End of file
   414 // ---------------------------------------------------------------------------
       
   415 //
       
   416 TInt CMCSPluginData::GetMCSPluginFolderIdL()
       
   417     {
       
   418     TInt folderId;
       
   419     
       
   420     _LIT( KMCSFolder, "mcsplugin_folder" );
       
   421 
       
   422     CMenuItem* item( NULL );
       
   423     CMenuFilter* filter = CMenuFilter::NewL();
       
   424     CleanupStack::PushL( filter );
       
   425     filter->SetType( KMenuTypeFolder );
       
   426     filter->HaveAttributeL( KMenuAttrLongName, KMCSFolder );
       
   427 
       
   428     const TInt rootId = iMenu.RootFolderL();
       
   429     RArray<TMenuItem> itemArray;
       
   430     CleanupClosePushL( itemArray );
       
   431     iMenu.GetItemsL( itemArray, rootId, filter, ETrue );
       
   432     if ( itemArray.Count() > 0 )
       
   433         {
       
   434         item = CMenuItem::OpenL( iMenu, itemArray[0] );
       
   435         folderId = item->Id();
       
   436         }
       
   437     else 
       
   438         {
       
   439         folderId = iMenu.RootFolderL();
       
   440         }
       
   441     CleanupStack::PopAndDestroy( &itemArray );
       
   442     CleanupStack::PopAndDestroy( filter ); 
       
   443 
       
   444     delete item; 
       
   445 
       
   446     return folderId;
       
   447     }
       
   448 
       
   449 // ---------------------------------------------------------------------------
       
   450 // Creates runtime generated menuitems (bookmarks/mailboxes if they 
       
   451 // it does not exist yet in MCS. If they do, their ref_count is incremented.
       
   452 // Called during Plugin startup.
       
   453 // ---------------------------------------------------------------------------
       
   454 //
       
   455 void CMCSPluginData::CreateRuntimeMenuItemsL()
       
   456     {
       
   457     
       
   458     // start mailboxes observing and get the number of
       
   459     // mailboxes defined in the device
       
   460 
       
   461     TMsvId entryID = KMsvRootIndexEntryIdValue;
       
   462     CMsvEntry* rootEntry = iMsvSession->GetEntryL( entryID );
       
   463     TInt mailboxCount = rootEntry->Count();
       
   464     CleanupStack::PushL( rootEntry );
       
   465     
       
   466     RPointerArray<CItemMap> settings;
       
   467     TCleanupItem settingsCleanupItem( ItemMapArrayCleanupFunc, &settings );
       
   468     CleanupStack::PushL( settingsCleanupItem );
       
   469     iPluginSettings->GetSettingsL( iInstanceUid, settings );
       
   470 
       
   471     RFavouritesSession bookmarkSess;
       
   472     RFavouritesDb bookmarkDb;
       
   473 
       
   474     User::LeaveIfError( bookmarkSess.Connect() );
       
   475     User::LeaveIfError( bookmarkDb.Open( bookmarkSess, KBrowserBookmarks ));
       
   476 
       
   477     TInt count = settings.Count();
       
   478     for( TInt i = 0; i < count; i++ )
       
   479        {
       
   480         CItemMap* itemMap = settings[i];
       
   481         RPointerArray<HSPluginSettingsIf::CPropertyMap>& properties
       
   482             = itemMap->Properties();
       
   483 
       
   484         TPtrC8 uid8, type, param8;
       
   485 
       
   486         for( TInt j = 0; j < properties.Count(); j++ )
       
   487             {
       
   488 
       
   489             if( properties[j]->Name() == KProperNameType )
       
   490                 {
       
   491                 type.Set( properties[j]->Value() );
       
   492                 }
       
   493             else if ( properties[j]->Name() == KProperNameUid )
       
   494                 {
       
   495                 uid8.Set( properties[j]->Value() );
       
   496                 }
       
   497             else if ( properties[j]->Name() ==  KProperNameParam )
       
   498                 {
       
   499                 param8.Set( properties[j]->Value() );
       
   500                 }
       
   501             }
       
   502 
       
   503         if( type == KProperValueBookmark )
       
   504             {
       
   505             
       
   506             // The shortcut is a bookmark
       
   507             TMenuItem menuItem = CreateMenuItemL( properties );
       
   508 
       
   509             CActiveSchedulerWait* wait = 
       
   510                             new ( ELeave ) CActiveSchedulerWait;
       
   511             CleanupStack::PushL( wait );
       
   512             
       
   513             if( menuItem.Id() == 0 )
       
   514                 {
       
   515                 TLex8 uidLex( uid8.Mid( 1, uid8.Length() - 2 ) );
       
   516                 TUint32 id;
       
   517                 uidLex.Val(id, EHex);
       
   518 
       
   519                 CFavouritesItem* bkmItem = CFavouritesItem::NewLC();
       
   520                 TInt bcount = bookmarkDb.Get( TInt32( id ), *bkmItem );
       
   521 
       
   522                 HBufC *uid( NULL );
       
   523                 uid = AiUtility::CopyToBufferL( uid, uid8 );
       
   524                 CleanupStack::PushL( uid );
       
   525 
       
   526                 CMenuItem* newItem = CMenuItem::CreateL( iMenu, 
       
   527                                                          KMenuTypeUrl, 
       
   528                                                          GetMCSPluginFolderIdL(),
       
   529                                                          0 );
       
   530                 CleanupStack::PushL( newItem );
       
   531 
       
   532                 newItem->SetAttributeL( KMenuAttrUid, *uid );
       
   533                 newItem->SetAttributeL( KMenuAttrLongName, bkmItem->Name() );
       
   534                 newItem->SetAttributeL( KMenuAttrIconFile, KMenuIconFile );
       
   535                 newItem->SetAttributeL( KMenuAttrIconId, KMenuBookmarkIconId );
       
   536                 newItem->SetAttributeL( KMenuAttrMaskId, KMenuBookmarkMaskId );
       
   537                 newItem->SetAttributeL( KMenuAttrRefcount, KInitialRefCount );
       
   538                 newItem->SetAttributeL( KUrl , bkmItem->Url() );
       
   539 
       
   540                 CMenuOperation* op = newItem->SaveL( iSaveWatcher->iStatus );
       
   541 
       
   542                 iSaveWatcher->StopAndWatch( op, wait );
       
   543 
       
   544                 // Start the nested scheduler loop.
       
   545                 wait->Start();
       
   546 
       
   547                 SaveSettingsL( i, *newItem );
       
   548 
       
   549                 CleanupStack::PopAndDestroy( newItem );
       
   550                 CleanupStack::PopAndDestroy( uid );
       
   551                 CleanupStack::PopAndDestroy( bkmItem );
       
   552                 }
       
   553             else
       
   554                 {
       
   555                 CMenuItem* item = CMenuItem::OpenL( iMenu, menuItem );
       
   556                 CleanupStack::PushL( item );
       
   557                 if ( iEngine.UpdateMenuItemsRefCountL( item, 1 ) > 0 )
       
   558                     {
       
   559                     CMenuOperation* op = item->SaveL( iSaveWatcher->iStatus );
       
   560                     iSaveWatcher->StopAndWatch( op, wait );
       
   561                     // Start the nested scheduler loop.
       
   562                     wait->Start();
       
   563                     SaveSettingsL( i, *item );
       
   564                     }
       
   565                 CleanupStack::PopAndDestroy( item );
       
   566                 }
       
   567 
       
   568             CleanupStack::PopAndDestroy( wait );
       
   569             wait = NULL;
       
   570             }
       
   571 
       
   572         else if ( uid8 == KMailboxUid8 && mailboxCount > 0 )
       
   573              {
       
   574              // The shortcut is a Mailbox
       
   575              
       
   576              TMenuItem menuItem = CreateMenuItemL( properties );
       
   577 
       
   578              CActiveSchedulerWait* wait = 
       
   579                              new ( ELeave ) CActiveSchedulerWait;
       
   580              CleanupStack::PushL( wait );
       
   581 
       
   582              if ( menuItem.Id() == 0 )
       
   583                 {
       
   584                 // mailbox menuitem does not exist yet. We have to create it
       
   585                 // first, we try to find its ID among existing mailboxes:bì 
       
   586 
       
   587                 // extract Mailbox ID from HSPS
       
   588                 TInt pos = param8.Locate( TChar( ':' ) ) + 1;
       
   589                 TPtrC8 mailboxId8 = param8.Mid( pos );
       
   590                 
       
   591                 HBufC *mailboxId( NULL );
       
   592                 mailboxId = AiUtility::CopyToBufferL( mailboxId, mailboxId8 );
       
   593                 CleanupStack::PushL( mailboxId );
       
   594                 
       
   595                 // compare ID with existing mailboxes
       
   596                 rootEntry->SetSortTypeL( TMsvSelectionOrdering( 
       
   597                             KMsvGroupByType | KMsvGroupByStandardFolders, 
       
   598                             EMsvSortByDetailsReverse, ETrue ) );
       
   599                 
       
   600                 TBuf<255> boxId;
       
   601                 TBool found = EFalse;
       
   602                 TInt index = rootEntry->Count();
       
   603 
       
   604                 while ( !found && --index >= 0 )
       
   605                     {
       
   606                     const TMsvEntry& tentry = ( *rootEntry )[ index ];
       
   607                     if ( tentry.iMtm.iUid == KMCSCmailMtmUidValue )
       
   608                         {
       
   609                         boxId.Num( tentry.Id() );
       
   610                         if ( boxId == *mailboxId )
       
   611                             {
       
   612                             found = ETrue;
       
   613                             }
       
   614                         }
       
   615                     }
       
   616 
       
   617                 CleanupStack::PopAndDestroy( mailboxId );
       
   618                 
       
   619                 // mailbox still exists in Mail application
       
   620                 // we have to create a new menuitem
       
   621                 if ( found )
       
   622                     {
       
   623 
       
   624                     // get the mailbox name
       
   625                     const TMsvEntry& tentry = ( *rootEntry )[ index ];
       
   626                     TPtrC name = tentry.iDetails;
       
   627 
       
   628                     HBufC *param( NULL );
       
   629                     param = AiUtility::CopyToBufferL( param, param8 );
       
   630                     CleanupStack::PushL( param );
       
   631 
       
   632                     // create a new menuitem with ref_count 1
       
   633                     CMenuItem* newItem = CMenuItem::CreateL( iMenu, 
       
   634                                                          KMenuTypeShortcut, 
       
   635                                                          GetMCSPluginFolderIdL(), 
       
   636                                                          0 );
       
   637                     CleanupStack::PushL( newItem );
       
   638 
       
   639                     // mailbox is a shortcut item with "mailbox:mailboxID" parameter
       
   640                     newItem->SetAttributeL( KMenuAttrUid, KMailboxUid );
       
   641                     newItem->SetAttributeL( KMenuAttrLongName, name );
       
   642                     newItem->SetAttributeL( KMenuAttrParameter, *param );
       
   643                     newItem->SetAttributeL( KMenuAttrRefcount, KInitialRefCount );
       
   644 
       
   645                     // setting icon for the shortcut
       
   646                     newItem->SetAttributeL( KMenuAttrIconFile, KMenuIconFile );
       
   647                     newItem->SetAttributeL( KMenuAttrIconId, KMenuMailboxIconId );
       
   648                     newItem->SetAttributeL( KMenuAttrMaskId, KMenuMailboxMaskId );
       
   649 
       
   650                     CMenuOperation* op = newItem->SaveL( iSaveWatcher->iStatus );
       
   651                     iSaveWatcher->StopAndWatch( op, wait );
       
   652 
       
   653                     // Start the nested scheduler loop.
       
   654                     wait->Start();
       
   655 
       
   656                     SaveSettingsL( i, *newItem );
       
   657 
       
   658                     CleanupStack::PopAndDestroy( newItem );
       
   659                     CleanupStack::PopAndDestroy( param );
       
   660                     }
       
   661                 }
       
   662              else 
       
   663                 {
       
   664                 // mailbox menu item already exists -> increment ref_count by 1
       
   665                 CMenuItem* item = CMenuItem::OpenL( iMenu, menuItem );
       
   666                 CleanupStack::PushL( item );
       
   667                 if ( iEngine.UpdateMenuItemsRefCountL( item, 1 ) > 0 )
       
   668                     {
       
   669                     CMenuOperation* op = item->SaveL( iSaveWatcher->iStatus );
       
   670                     iSaveWatcher->StopAndWatch( op, wait );
       
   671 
       
   672                     // Start the nested scheduler loop.
       
   673                     wait->Start();
       
   674                     SaveSettingsL( i, *item );
       
   675                     }
       
   676                  CleanupStack::PopAndDestroy( item );
       
   677                 }
       
   678              CleanupStack::PopAndDestroy( wait );
       
   679              wait = NULL;
       
   680              
       
   681              }
       
   682         }
       
   683     CleanupStack::PopAndDestroy(); // settingsCleanupItem
       
   684     CleanupStack::PopAndDestroy( rootEntry );
       
   685     }
       
   686 
       
   687 // ---------------------------------------------------------------------------
       
   688 // From class MMsvSessionObserver.
       
   689 // Handles an event from the message server.
       
   690 // ---------------------------------------------------------------------------
       
   691 //
       
   692 void CMCSPluginData::HandleSessionEventL(
       
   693                                      TMsvSessionEvent /*aEvent*/, 
       
   694                                      TAny* /*aArg1*/, 
       
   695                                      TAny* /*aArg2*/,
       
   696                                      TAny* /*aArg3*/ )
       
   697     {
       
   698 
       
   699     }