idlefw/plugins/mcsplugin/settings/src/mcspluginsettingsapplist.cpp
branchRCL_3
changeset 14 15e4dd19031c
parent 8 d0529222e3f0
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".
    13 *
    13 *
    14 * Description:  Application list for settings listbox
    14 * Description:  Application list for settings listbox
    15 *
    15 *
    16 */
    16 */
    17 
    17 
    18 
       
    19 #include <e32cmn.h>
       
    20 #include <msvuids.h>        // For KMsvRootIndexEntryIdValue
       
    21 #include <SenduiMtmUids.h>
       
    22 #include <StringLoader.h>
       
    23 #include <aistrcnv.h>
    18 #include <aistrcnv.h>
    24 #include <featmgr.h>
       
    25 #include <mcsmenuitem.h>
    19 #include <mcsmenuitem.h>
    26 #include <mcsmenufilter.h>
    20 #include <mcsmenufilter.h>
    27 
    21 
    28 #include "mcspluginsettingsapplist.h"
    22 #include "mcspluginsettingsapplist.h"
    29 #include "mcspluginsettingsmodel.h"
    23 #include "mcspluginsettingsmodel.h" // For TSettingItem
    30 #include "mcspluginwatcher.h"
       
    31 
    24 
    32 _LIT( KMyMenuData, "matrixmenudata" );
    25 _LIT( KMyMenuData, "matrixmenudata" );
    33 _LIT( KMenuTypeShortcut, "menu:shortcut" );
    26 _LIT( KMenuTypeShortcut, "menu:shortcut" );
    34 _LIT( KMenuAttrRefcount, "ref_count" );
    27 _LIT( KMenuTypeMailbox, "menu:mailbox" );
    35 _LIT( KMenuParamMailbox, "mailbox:" );
       
    36 _LIT( KMenuAttrParameter, "param" );
    28 _LIT( KMenuAttrParameter, "param" );
    37 _LIT( KMenuAttrLocked, "locked" );
    29 _LIT( KMenuAttrLocked, "locked" );
    38 _LIT( KMenuIconFile, "aimcsplugin.mif" );
       
    39 _LIT( KMenuIconId, "16388" );
       
    40 _LIT( KMenuMaskId, "16389" );
       
    41 _LIT( KMailboxUid, "0x100058c5" );
       
    42 _LIT( KInitialRefCount, "1" );
       
    43 _LIT( KMCSFolder, "mcsplugin_folder" );
       
    44 _LIT8( KItemLocked, "locked");
    30 _LIT8( KItemLocked, "locked");
    45 _LIT8( KProperValueFolder, "folder" );
    31 _LIT8( KProperValueFolder, "folder" );
    46 _LIT( KMenuAttrUndefUid, "0x99999991" );
    32 _LIT( KMenuAttrUndefUid, "0x99999991" );
    47 _LIT( KMenuItemLongName, "long_name" );
    33 _LIT( KMenuItemLongName, "long_name" );
    48 
    34 
    54 // First-phase construction
    40 // First-phase construction
    55 // ---------------------------------------------------------------------------
    41 // ---------------------------------------------------------------------------
    56 //
    42 //
    57 CMCSPluginSettingsAppList::CMCSPluginSettingsAppList()
    43 CMCSPluginSettingsAppList::CMCSPluginSettingsAppList()
    58     {
    44     {
    59     iMCSPluginFolderId = 0;
       
    60     }
    45     }
    61 
    46 
    62 // ---------------------------------------------------------------------------
    47 // ---------------------------------------------------------------------------
    63 // Second-phase construction
    48 // Second-phase construction
    64 // ---------------------------------------------------------------------------
    49 // ---------------------------------------------------------------------------
    66 void CMCSPluginSettingsAppList::ConstructL()
    51 void CMCSPluginSettingsAppList::ConstructL()
    67     {
    52     {
    68     iMsvSession = CMsvSession::OpenAsObserverL(*this);
    53     iMsvSession = CMsvSession::OpenAsObserverL(*this);
    69 
    54 
    70     iMenu.OpenL( KMyMenuData );
    55     iMenu.OpenL( KMyMenuData );
    71     iSaveWatcher = CMCSPluginWatcher::NewL( CMCSPluginWatcher::EOperation );
       
    72     iUpdateWatcher = CMCSPluginWatcher::NewL( CMCSPluginWatcher::EOperation );
       
    73     iRemoveWatcher = CMCSPluginWatcher::NewL( CMCSPluginWatcher::EOperation );
       
    74     
    56     
    75     // Get "Undefined" icon and text
    57     // Get "Undefined" icon and text
    76     CMenuFilter* filter = CMenuFilter::NewL();
    58     CMenuFilter* filter = CMenuFilter::NewL();
    77     CleanupStack::PushL( filter );
    59     CleanupStack::PushL( filter );
    78 
    60 
    82     TMenuItem item;
    64     TMenuItem item;
    83     const TInt root = iMenu.RootFolderL();
    65     const TInt root = iMenu.RootFolderL();
    84     RArray<TMenuItem> items;
    66     RArray<TMenuItem> items;
    85     CleanupClosePushL( items );
    67     CleanupClosePushL( items );
    86     iMenu.GetItemsL( items, root, filter, ETrue );
    68     iMenu.GetItemsL( items, root, filter, ETrue );
    87     TMenuItem undefItem;
       
    88 
    69 
    89     if ( items.Count() > 0 )
    70     if ( items.Count() > 0 )
    90         {
    71         {
    91         undefItem = items[ 0 ];
    72         iUndefinedItem = CMenuItem::OpenL( iMenu, items[ 0 ] );
    92         CMenuItem* undefinedItem = CMenuItem::OpenL( iMenu, undefItem );
       
    93         iUndefinedText = NULL;
    73         iUndefinedText = NULL;
    94 
    74 
    95         if ( undefinedItem )
    75         if ( iUndefinedItem )
    96             {
    76             {
    97             TBool exists( KErrNotFound );
    77             TBool exists( KErrNotFound );//CleanupStack::PushL( undefinedItem );
    98             CleanupStack::PushL( undefinedItem );
    78             TPtrC undefined = iUndefinedItem->GetAttributeL( KMenuItemLongName, exists );
    99             TPtrC undefined = undefinedItem->GetAttributeL( KMenuItemLongName, exists );
       
   100 
    79 
   101             if ( exists )
    80             if ( exists )
   102                 {
    81                 {
   103                 iUndefinedText = HBufC::NewMaxL( undefined.Length() );
    82                 iUndefinedText = HBufC::NewMaxL( undefined.Length() );
   104                 iUndefinedText->Des().Copy( undefined );
    83                 iUndefinedText->Des().Copy( undefined );
   105                 }
    84                 }
   106             else
    85             else
   107                 {
    86                 {
   108                 iUndefinedText = KNullDesC().Alloc();
    87                 iUndefinedText = KNullDesC().Alloc();
   109                 }
    88                 }
   110             CleanupStack::PopAndDestroy( undefinedItem );
       
   111             }
    89             }
   112         }
    90         }
   113 
    91 
   114     CleanupStack::PopAndDestroy( &items );
    92     CleanupStack::PopAndDestroy( &items );
   115     CleanupStack::PopAndDestroy( filter );
    93     CleanupStack::PopAndDestroy( filter );
   136     {
   114     {
   137     delete iMsvSession;
   115     delete iMsvSession;
   138 
   116 
   139     iListItems.ResetAndDestroy();
   117     iListItems.ResetAndDestroy();
   140     iMenu.Close();
   118     iMenu.Close();
   141     delete iSaveWatcher;
   119     
   142     delete iUpdateWatcher;
   120     delete iUndefinedText;
   143     delete iRemoveWatcher;
   121     delete iUndefinedItem;
   144     
       
   145     if ( iUndefinedText )
       
   146         {
       
   147         delete iUndefinedText;
       
   148         iUndefinedText = NULL;
       
   149         }
       
   150     }
   122     }
   151 
   123 
   152 // ---------------------------------------------------------------------------
   124 // ---------------------------------------------------------------------------
   153 // From MDesCArray
   125 // From MDesCArray
   154 // Returns the number of descriptor elements in a descriptor array.
   126 // Returns the number of descriptor elements in a descriptor array.
   294             }
   266             }
   295         }
   267         }
   296     return settingItem;
   268     return settingItem;
   297     }
   269     }
   298 
   270 
   299 
   271 // ---------------------------------------------------------------------------
   300 // ---------------------------------------------------------------------------
   272 // Returns menuitems at given index.
   301 // Returns menuitems at given index. Since this method is called during
   273 // ---------------------------------------------------------------------------
   302 // adding the item to the Desktop widget, we also have to increment
   274 //
   303 // ref_count attribute if the item is run-time generated (i.e. Mailbox)
   275 CMenuItem* CMCSPluginSettingsAppList::ItemL( const TInt& aIndex )
   304 // ---------------------------------------------------------------------------
   276     {
   305 //
       
   306 CMenuItem& CMCSPluginSettingsAppList::ItemL( const TInt& aIndex )
       
   307     {
       
   308 
       
   309     CMenuItem* menuItem( NULL );
   277     CMenuItem* menuItem( NULL );
   310     
   278     // check if index is within the list boundaries
   311     // check if index in within the list boundaries
       
   312     if ( aIndex >= 0 && aIndex < iListItems.Count() )
   279     if ( aIndex >= 0 && aIndex < iListItems.Count() )
   313         {
   280         {
   314 
       
   315         menuItem = iListItems[ aIndex ];
   281         menuItem = iListItems[ aIndex ];
   316 
   282         }
   317         TBool hasParam = EFalse;
   283     return menuItem;
   318         CleanupStack::PushL( menuItem );
       
   319         TPtrC param = menuItem->GetAttributeL( KMenuAttrParameter, hasParam ); 
       
   320         CleanupStack::Pop( menuItem );
       
   321 
       
   322         // if item is a mailbox, add it to MCS
       
   323         // (if it is not already there)
       
   324         if ( hasParam && param.Find( KMenuParamMailbox ) != KErrNotFound )
       
   325             {
       
   326 
       
   327             // set up a filter for finding the mailbox
       
   328             // with given ID in MCS
       
   329             CMenuFilter* filter = CMenuFilter::NewL();
       
   330             CleanupStack::PushL( filter );
       
   331 
       
   332             filter->SetType( KMenuTypeShortcut );
       
   333             filter->HaveAttributeL( KMenuAttrParameter, param );
       
   334 
       
   335             // search menu from the Root folder with the filter
       
   336             const TInt rootId = iMenu.RootFolderL();
       
   337             RArray<TMenuItem> itemArray;
       
   338             CleanupClosePushL( itemArray );
       
   339             iMenu.GetItemsL( itemArray, rootId, filter, ETrue );
       
   340                 
       
   341             // save the number of findings
       
   342             TInt count( itemArray.Count() );
       
   343 
       
   344             // if MenuItem does not exist in MCS
       
   345             if ( count == 0 )
       
   346                 {
       
   347                 // save the item into Matrixmenudata.xml
       
   348                 // the "op" variable is cleaned up by iSaveWatcher when asynchronous 
       
   349                 // operation finishes
       
   350                 CleanupStack::PushL( menuItem );
       
   351                 CMenuOperation* op = menuItem->SaveL( iSaveWatcher->iStatus );
       
   352                 CleanupStack::Pop( menuItem );
       
   353                 iSaveWatcher->Watch( op ); 
       
   354                 }
       
   355             else
       
   356                 {
       
   357                 // Item already exists in MCS
       
   358                 // If it has reference counter, increment it before returning.
       
   359                 CMenuItem* itm = CMenuItem::OpenL( iMenu, itemArray[ 0 ] );
       
   360 
       
   361                 TInt newRefCount = UpdateMenuItemsRefCountL( itm, 1 );
       
   362                 if ( newRefCount > -1 )
       
   363                     {
       
   364                     CleanupStack::PushL( itm );
       
   365                     CMenuOperation* op = itm->SaveL( iSaveWatcher->iStatus );
       
   366                     CleanupStack::PopAndDestroy( itm );
       
   367                     iSaveWatcher->Watch( op );
       
   368                     }
       
   369                 }
       
   370             CleanupStack::PopAndDestroy( &itemArray );
       
   371             CleanupStack::PopAndDestroy( filter );
       
   372             }
       
   373         }
       
   374     return *menuItem;
       
   375     }
   284     }
   376 
   285 
   377 // ---------------------------------------------------------------------------
   286 // ---------------------------------------------------------------------------
   378 // Helper method for comparing names of two menuitems. Used to sort the list
   287 // Helper method for comparing names of two menuitems. Used to sort the list
   379 // of items.
   288 // of items.
   401     TLinearOrder<CMenuItem> sortMethod( CMCSPluginSettingsAppList::CompareNameL );
   310     TLinearOrder<CMenuItem> sortMethod( CMCSPluginSettingsAppList::CompareNameL );
   402     
   311     
   403     CMenuFilter* filter = CMenuFilter::NewL();
   312     CMenuFilter* filter = CMenuFilter::NewL();
   404     CleanupStack::PushL( filter );
   313     CleanupStack::PushL( filter );
   405 
   314 
   406     // skip run-time generated items
   315     // skip locked items
   407     filter->DoNotHaveAttributeL( KMenuAttrRefcount );
       
   408     filter->DoNotHaveAttributeL( KMenuAttrLocked );
   316     filter->DoNotHaveAttributeL( KMenuAttrLocked );
   409     const TInt rootId = iMenu.RootFolderL();
   317     const TInt rootId = iMenu.RootFolderL();
   410     RArray<TMenuItem> itemArray;
   318     RArray<TMenuItem> itemArray;
   411     CleanupClosePushL( itemArray );
   319     CleanupClosePushL( itemArray );
   412     iMenu.GetItemsL( itemArray, rootId, filter, ETrue );
   320     iMenu.GetItemsL( itemArray, rootId, filter, ETrue );
   458 // Adds remote mailboxes to the application list.
   366 // Adds remote mailboxes to the application list.
   459 // ---------------------------------------------------------------------------
   367 // ---------------------------------------------------------------------------
   460 //
   368 //
   461 void CMCSPluginSettingsAppList::AddMailboxesL()
   369 void CMCSPluginSettingsAppList::AddMailboxesL()
   462     {
   370     {
   463     
       
   464     iListItems.ResetAndDestroy();
       
   465     CMsvEntry* rootEntry = GetRootEntryL();
   371     CMsvEntry* rootEntry = GetRootEntryL();
   466     CleanupStack::PushL(rootEntry);
   372     CleanupStack::PushL(rootEntry);
   467     TBuf<255> mailboxId;
   373     TBuf<255> mailboxId;
   468     
   374     
   469     TInt cnt = rootEntry->Count();
   375     TInt cnt = rootEntry->Count();
   492 // ---------------------------------------------------------------------------
   398 // ---------------------------------------------------------------------------
   493 //
   399 //
   494 void CMCSPluginSettingsAppList::AddMailboxL( const TDesC& aMailbox,
   400 void CMCSPluginSettingsAppList::AddMailboxL( const TDesC& aMailbox,
   495                                              const TDesC& aMailboxId )
   401                                              const TDesC& aMailboxId )
   496     {
   402     {
   497         // prepare param value
   403     TLinearOrder<CMenuItem> sortMethod( CMCSPluginSettingsAppList::CompareNameL );
   498         HBufC* params = HBufC::NewLC( KMenuParamMailbox().Length() + aMailboxId.Length() );
   404     CMenuItem* newItem = CMenuItem::CreateL( iMenu, KMenuTypeMailbox, 0, 0 );
   499         params->Des().Copy( KMenuParamMailbox );
   405     CleanupStack::PushL( newItem );
   500         params->Des().Append( aMailboxId );
   406 
   501         TPtrC paramValue( params->Des() );
   407     // mailbox is a shortcut item with "mailbox:mailboxID" parameter
   502 
   408     newItem->SetAttributeL( KMenuAttrUid, aMailboxId );
   503         TLinearOrder<CMenuItem> sortMethod( CMCSPluginSettingsAppList::CompareNameL );
   409     newItem->SetAttributeL( KMenuAttrLongName, aMailbox );
   504         CMenuItem* newItem = CMenuItem::CreateL( iMenu, 
   410     // Mailbox name is saved to settings into param field.
   505                                                  KMenuTypeShortcut, 
   411     newItem->SetAttributeL( KMenuAttrParameter, aMailbox );
   506                                                  GetMCSPluginFolderIdL(), 
   412 
   507                                                  0 );
   413     // append the item into iListItems lists
   508         CleanupStack::PushL( newItem );
   414     User::LeaveIfError( iListItems.InsertInOrderAllowRepeats( newItem, sortMethod ) );
   509 
   415     CleanupStack::Pop( newItem );
   510         // mailbox is a shortcut item with "mailbox:mailboxID" parameter
   416     }
   511         newItem->SetAttributeL( KMenuAttrUid, KMailboxUid );
       
   512         newItem->SetAttributeL( KMenuAttrLongName, aMailbox );
       
   513         newItem->SetAttributeL( KMenuAttrParameter, paramValue );
       
   514         newItem->SetAttributeL( KMenuAttrRefcount, KInitialRefCount );
       
   515 
       
   516         // setting icon for the shortcut
       
   517         newItem->SetAttributeL( KMenuAttrIconFile, KMenuIconFile );
       
   518         newItem->SetAttributeL( KMenuAttrIconId, KMenuIconId );
       
   519         newItem->SetAttributeL( KMenuAttrMaskId, KMenuMaskId );
       
   520 
       
   521         // append the item into iListItems lists
       
   522         User::LeaveIfError( iListItems.InsertInOrderAllowRepeats( newItem, sortMethod ) );
       
   523         CleanupStack::Pop( newItem );
       
   524         CleanupStack::PopAndDestroy( params );
       
   525     }
       
   526 
       
   527 // ---------------------------------------------------------------------------
       
   528 // Removes run-time generated menuitem (i.e. Mailbox) from MCS
       
   529 // If the item at given index is not run-time generated, return
       
   530 // ---------------------------------------------------------------------------
       
   531 //
       
   532 void CMCSPluginSettingsAppList::RemoveMenuItemL( TInt aIndex )
       
   533     {
       
   534 
       
   535     if ( aIndex < 0 || aIndex > iListItems.Count() - 1 )
       
   536         {
       
   537         return;
       
   538         }
       
   539 
       
   540     CMenuItem* menuItem = iListItems[ aIndex ];
       
   541     
       
   542     TBool hasParam = ETrue;
       
   543     TPtrC param = menuItem->GetAttributeL( KMenuAttrParameter, hasParam );
       
   544 
       
   545     if ( !hasParam )
       
   546         {
       
   547         // nothing to do
       
   548         return;
       
   549         }
       
   550 
       
   551     // set up a filter for finding the mailbox
       
   552     // with given ID in MCS
       
   553     CMenuFilter* filter = CMenuFilter::NewL();
       
   554     CleanupStack::PushL( filter );
       
   555 
       
   556     filter->SetType( KMenuTypeShortcut );
       
   557     filter->HaveAttributeL( KMenuAttrParameter, param );
       
   558 
       
   559     // search menu from the Root folder with the filter
       
   560     const TInt rootId = iMenu.RootFolderL();
       
   561     RArray<TMenuItem> itemArray;
       
   562     CleanupClosePushL( itemArray );
       
   563     iMenu.GetItemsL( itemArray, rootId, filter, ETrue );
       
   564 
       
   565     // save the number of findings
       
   566     TInt count( itemArray.Count() );
       
   567     
       
   568     if ( count > 0 )
       
   569         {
       
   570         // Item already exists in MCS
       
   571         // If it has reference counter, increment it before returning.
       
   572         CMenuItem* itm = CMenuItem::OpenL( iMenu, itemArray[ 0 ] );
       
   573         
       
   574         // decrement ref_count attribute 
       
   575         TInt newRefCount = UpdateMenuItemsRefCountL( itm, -1 );
       
   576         if ( newRefCount > 0 )
       
   577             {
       
   578             CleanupStack::PushL( itm ); 
       
   579             CMenuOperation* op = itm->SaveL( iUpdateWatcher->iStatus );
       
   580             CleanupStack::Pop( itm );
       
   581             iUpdateWatcher->Watch( op );
       
   582             }
       
   583         else if ( newRefCount == 0 )
       
   584             {
       
   585             // counter reached 0 -> item is not referenced by any shortcut
       
   586             // so remove it from MCS
       
   587             if ( iRemoveWatcher->IsActive() )
       
   588                {
       
   589                return;
       
   590                }
       
   591             CMenuOperation* op = iMenu.RemoveL( itm->Id(), iRemoveWatcher->iStatus );
       
   592             iRemoveWatcher->Watch( op );
       
   593             }
       
   594             delete itm;
       
   595         }
       
   596     CleanupStack::PopAndDestroy( &itemArray );
       
   597     CleanupStack::PopAndDestroy( filter );
       
   598     }
       
   599 
       
   600 // ---------------------------------------------------------------------------
       
   601 // Gets MCS Plugin folder ID. This hidden folder in matrixmenudata.xml is used 
       
   602 // for storing run-time generated menuitems
       
   603 // ---------------------------------------------------------------------------
       
   604 //
       
   605 TInt CMCSPluginSettingsAppList::GetMCSPluginFolderIdL()
       
   606     {
       
   607     
       
   608     if ( iMCSPluginFolderId == 0 )
       
   609         {
       
   610         CMenuItem* item( NULL );
       
   611         CMenuFilter* filter = CMenuFilter::NewL();
       
   612         CleanupStack::PushL( filter );
       
   613         filter->SetType( KMenuTypeFolder );
       
   614         filter->HaveAttributeL( KMenuAttrLongName, KMCSFolder );
       
   615         const TInt rootId = iMenu.RootFolderL();
       
   616         RArray<TMenuItem> itemArray;
       
   617         CleanupClosePushL( itemArray );
       
   618         iMenu.GetItemsL( itemArray, rootId, filter, ETrue );
       
   619         if ( itemArray.Count() > 0 )
       
   620             {
       
   621             item = CMenuItem::OpenL( iMenu, itemArray[ 0 ] );
       
   622             iMCSPluginFolderId = item->Id();
       
   623             }
       
   624         else 
       
   625             {
       
   626             iMCSPluginFolderId = iMenu.RootFolderL();
       
   627             }
       
   628         CleanupStack::PopAndDestroy( &itemArray );
       
   629         CleanupStack::PopAndDestroy( filter ); 
       
   630         delete item; 
       
   631         }
       
   632     return iMCSPluginFolderId;
       
   633     
       
   634     }
       
   635 
       
   636 // ---------------------------------------------------------------------------
       
   637 // Helper method for updating ref_count attribute of run-time generated 
       
   638 // menuitems
       
   639 // ---------------------------------------------------------------------------
       
   640 //
       
   641 TInt CMCSPluginSettingsAppList::UpdateMenuItemsRefCountL( CMenuItem* aItem, 
       
   642                                                           TInt aValueToAdd )
       
   643     {
       
   644     
       
   645     TBool exists = EFalse;
       
   646     CleanupStack::PushL( aItem ); 
       
   647     TPtrC param = aItem->GetAttributeL( KMenuAttrRefcount, exists );
       
   648     CleanupStack::Pop( aItem );
       
   649     if ( exists )
       
   650         {
       
   651         TInt references;
       
   652         TLex16 lextmp( param );
       
   653         lextmp.Val( references );
       
   654         references += aValueToAdd;
       
   655         TBuf<128> buf;
       
   656         buf.NumUC( references );
       
   657         // set new ref_count
       
   658         CleanupStack::PushL( aItem ); 
       
   659         aItem->SetAttributeL( KMenuAttrRefcount, buf );
       
   660         CleanupStack::Pop( aItem );
       
   661         // return new ref_count
       
   662         return references;
       
   663         }
       
   664     return -1;
       
   665     }
       
   666 
       
   667 
   417 
   668 // End of File.
   418 // End of File.