idlefw/plugins/mcsplugin/publisher/src/mcsplugin.cpp
branchRCL_3
changeset 8 d0529222e3f0
parent 2 b7904b40483f
child 11 bd874ee5e5e2
--- a/idlefw/plugins/mcsplugin/publisher/src/mcsplugin.cpp	Tue Feb 02 00:23:10 2010 +0200
+++ b/idlefw/plugins/mcsplugin/publisher/src/mcsplugin.cpp	Fri Feb 19 23:07:29 2010 +0200
@@ -15,50 +15,48 @@
 *
 */
 
-
+// System includes
 #include <ecom/ecom.h>
 #include <ecom/implementationproxy.h>
 #include <gulicon.h>
+#include <mcsmenuitem.h>
+
+// User includes
 #include <aicontentobserver.h>
 #include <aiutility.h>
 #include <aistrcnv.h>
-#include <mcsmenuitem.h>
-
-#include <aknskinsinternalcrkeys.h> // For working with settings API
-#include <centralrepository.h> // Headers Used for CRepository
-
 #include "mcspluginuids.hrh"
 #include "mcsplugin.h"
 #include "mcsplugindata.h"
 #include "mcspluginengine.h"
 #include "aipluginsettings.h"
 
-
+// Constants
 const TUint KPluginNameSeprator =  '/';
 
 const TInt KImplUidMCSPlugin = AI_UID_ECOM_IMPLEMENTATION_CONTENTPUBLISHER_MCSPLUGIN;
-// CONST CLASS VARIABLES
-const TImplementationProxy KImplementationTable[] =
-    {
-    IMPLEMENTATION_PROXY_ENTRY( KImplUidMCSPlugin, CMCSPlugin::NewL ) 
-    };
 
 _LIT( KEventNameLaunchByIndex,  "LaunchByIndex" );
 _LIT( KEventNameShowSettings,   "ShowSettings" );
 _LIT( KContentItemTypeText, "text" );
 _LIT( KContentItemTypeImage, "image" );
 
+const TImplementationProxy KImplementationTable[] =
+    {
+    IMPLEMENTATION_PROXY_ENTRY( KImplUidMCSPlugin, CMCSPlugin::NewL ) 
+    };
+
 // ======== LOCAL FUNCTIONS ========
 
 // ======== MEMBER FUNCTIONS ========
-
-// ---------------------------------------------------------------------------
-// Symbian 2nd phase constructor can leave
-// ---------------------------------------------------------------------------
+// ----------------------------------------------------------------------------
+// CMCSPlugin::NewL
+//
+// ----------------------------------------------------------------------------
 //
 CMCSPlugin* CMCSPlugin::NewL()
     {
-    CMCSPlugin* self = new (ELeave) CMCSPlugin;
+    CMCSPlugin* self = new ( ELeave ) CMCSPlugin;
     CleanupStack::PushL( self );
     self->ConstructL();
     CleanupStack::Pop( self );
@@ -66,60 +64,28 @@
     return self;
     }
     
-// ---------------------------------------------------------------------------
-// Default constructor
-// ---------------------------------------------------------------------------
+// ----------------------------------------------------------------------------
+// CMCSPlugin::CMCSPlugin
+//
+// ----------------------------------------------------------------------------
 //
 CMCSPlugin::CMCSPlugin()
     {
     }
     
-// ---------------------------------------------------------------------------
-// Symbian 2nd phase constructor can leave
-// ---------------------------------------------------------------------------
+// ----------------------------------------------------------------------------
+// CMCSPlugin::ConstructL
+// 
+// ----------------------------------------------------------------------------
 //
 void CMCSPlugin::ConstructL()
-    { 
-    iInfo.iUid.iUid = AI_UID_ECOM_IMPLEMENTATION_CONTENTPUBLISHER_MCSPLUGIN; 
-    // We need to Query Central Repository
-    iRepository  = CRepository::NewL( KCRUidPersonalisation );
-
-    // Setting up watcher which calls HandleNotifyL method 
-    // everytime the SkinUID changes in central repository
-    iRepositoryWatcher = CMCSPluginWatcher::NewL( CMCSPluginWatcher::ENotify );
-    iRepository->NotifyRequest( KPslnActiveSkinUid, iRepositoryWatcher->iStatus );
-    iRepositoryWatcher->WatchNotify( this );
-    }
-
-// ---------------------------------------------------------------------------
-// Handle Skin UID change
-// ---------------------------------------------------------------------------
-//
-void CMCSPlugin::HandleNotifyL()
-    {
-
-    // Skin ID has changed. Set all MenuItems on Widget dirty
-    // and re-publish to update icons
-    if ( iEngine )
-        {
-        TInt dataCount = iEngine->MenuItemCount();
-        for ( TInt i = 0; i < dataCount; i++ )
-            {
-            iEngine->MenuDataL( i ).SetDirty( ETrue );
-            }
-        PublishL();
-        }
-
-    // Skin ID Notification must be activated again
-    iRepositoryWatcher->Cancel();
-    iRepository->NotifyRequest( KPslnActiveSkinUid, iRepositoryWatcher->iStatus );
-    iRepositoryWatcher->WatchNotify( this );
+    {         
     }
     
-// ---------------------------------------------------------------------------
+// ----------------------------------------------------------------------------
 // Destructor
 // Deletes all data created to heap
-// ---------------------------------------------------------------------------
+// ----------------------------------------------------------------------------
 //
 CMCSPlugin::~CMCSPlugin()
     {
@@ -134,55 +100,41 @@
     iObservers.Close();
     
     DeleteContentModel();
-
-    if ( iRepository )
-        {
-        delete iRepository;
-        iRepository = NULL;
-        }
-
-    if ( iRepositoryWatcher )
-        {
-        iRepositoryWatcher->Cancel();
-        delete iRepositoryWatcher;
-        iRepositoryWatcher = NULL;
-        }
     }
 
-// ---------------------------------------------------------------------------
+// ----------------------------------------------------------------------------
+// CMCSPlugin::PublishL
 // Publishes the all the items
-// ---------------------------------------------------------------------------
+// ----------------------------------------------------------------------------
 //
 void CMCSPlugin::PublishL()
-    {
-    
-    if ( !iEngine )
-        {
-        iEngine = CMCSPluginEngine::NewL( *this, iInfo.iNamespace );
-        }
-
+    {    
     TInt err( KErrNone );
     TInt observers( iObservers.Count() );
-    TInt transactionId = reinterpret_cast<TInt>( this );
+    TInt transactionId( reinterpret_cast<TInt>( this ) );
     TInt menuItems ( iEngine->MenuItemCount() );
 
     for ( TInt i = 0; i < observers; i++ )
         {
-        MAiContentObserver* observer = iObservers[ i ];
+        MAiContentObserver* observer( iObservers[ i ] );
         err = observer->StartTransaction( transactionId );
+        
         if ( err == KErrNotSupported )
             {
             return;
             }
+        
         // Publish content to all items
         for ( TInt j = 0; j < menuItems; j++ )
             {
             // Index has to start from 1 ( j + 1 )
             PublishLItemL( *observer, iEngine->MenuDataL( j ), ( j + 1 ) );
             }// shortcut count
+        
         if ( err == KErrNone )
             {
             err = observer->Commit( transactionId );
+            
             if ( err == KErrNotSupported )
                 {
                 return;
@@ -197,20 +149,22 @@
         }
     }
 
-// ---------------------------------------------------------------------------
+// ----------------------------------------------------------------------------
+// CMCSPlugin::PublishLItemL
 // Publishes one item to given index
-// ---------------------------------------------------------------------------
+// ----------------------------------------------------------------------------
 //
-void CMCSPlugin::PublishLItemL( MAiContentObserver& aObserver, TMCSData& aData, TInt aIndex )
+void CMCSPlugin::PublishLItemL( MAiContentObserver& aObserver, 
+    TMCSData& aData, TInt aIndex )
     {
-
     if( !aData.IsDirty() )
         {
         return;
         }
 
-    CMenuItem* item = iEngine->FetchMenuItemL( aData.MenuItem() );
+    CMenuItem* item( iEngine->FetchMenuItemL( aData.MenuItem() ) );
     CleanupStack::PushL( item );
+    
     // One widget item has iDataCount number of elements
     for ( TInt i = 0; i < iDataCount; i++ )
         {
@@ -219,137 +173,140 @@
             //Publish image
             if ( aObserver.CanPublish( *this, i, aIndex ) )
                 {
-                CGulIcon* icon = iEngine->ItemIconL( *item, TPtrC16( ( const TText16* ) iContentModel[ i ].cid ) );
+                CGulIcon* icon( iEngine->ItemIconL( *item, 
+                    TPtrC16( ( const TText16* ) iContentModel[ i ].cid ) ) );
+                
                 aObserver.PublishPtr( *this, i, icon , aIndex );
                 }
             }
-        else if ( iContentModel[ i ].type == KAiContentTypeText )
+        else if ( iContentModel[i].type == KAiContentTypeText )
             {
             //Publish  text
             if ( aObserver.CanPublish( *this, i, aIndex ) )
                 {
-                TPtrC name = iEngine->ItemTextL( *item, TPtrC16( ( const TText16* ) iContentModel[ i ].cid ) );
+                TPtrC name( iEngine->ItemTextL( *item, 
+                    TPtrC16( ( const TText16* ) iContentModel[ i ].cid ) ) );
+                
                 aObserver.Publish( *this, i, name, aIndex );
                 }
             }
         }//content items
 
     CleanupStack::PopAndDestroy( item );
-
     }
 
-// ---------------------------------------------------------------------------
-// From class CAiContentPublisher
-// Plug-in is requested to unload its engines due backup operation
-// ---------------------------------------------------------------------------
+// ----------------------------------------------------------------------------
+// CMCSPlugin::Start
+// 
+// ----------------------------------------------------------------------------
 //
-void CMCSPlugin::Stop( TAiTransitionReason aReason )
-    {
-    
-    if ( aReason == EAiBackupRestoreStarted )
-        {
-        Suspend( aReason );
-        }
-
+void CMCSPlugin::Start( TStartReason /*aReason*/ )
+    {    
     }
 
-// ---------------------------------------------------------------------------
-// From class CAiContentPublisher
-// Plug-in is instructed that it is allowed to consume CPU resources
-// ---------------------------------------------------------------------------
+// ----------------------------------------------------------------------------
+// CMCSPlugin::Stop
+// 
+// ----------------------------------------------------------------------------
 //
-void CMCSPlugin::Resume( TAiTransitionReason aReason )
+void CMCSPlugin::Stop( TStopReason /*aReason*/ )
     {
-    if( aReason == EAiIdleBackground )
-        {
-        return;
-        }
-
-    if ( aReason == EAiBackupRestoreEnded )
-        {
-        if ( iEngine )
-            {
-            TRAP_IGNORE( iEngine->ResumeL() );
-            }
-        }
-
-    TRAP_IGNORE( DoResumeL( aReason ) );
-    return;
     }
 
-// ---------------------------------------------------------------------------
-// From class CAiContentPublisher
-// Plug-in is instructed that it is not allowed to consume CPU resources
-// ---------------------------------------------------------------------------
+// ----------------------------------------------------------------------------
+// CMCSPlugin::Resume
+// 
+// ----------------------------------------------------------------------------
 //
-void CMCSPlugin::Suspend( TAiTransitionReason aReason )
-    {
-    if ( aReason == EAiBackupRestoreStarted && iEngine )
+void CMCSPlugin::Resume( TResumeReason aReason )
+    {       
+    if ( aReason == EForeground )
         {
-        iEngine->Suspend();
+        iEngine->SetBackupRestore( EFalse );
+        
+        if ( PublishRequired() )
+            {
+            TRAP_IGNORE( PublishL() );
+            }        
         }
     }
 
-// ---------------------------------------------------------------------------
-// From class CAiContentPublisher
-// The plug-in MUST maintain a registry of subscribers and send 
-// notification to all of them whenever the state changes or new content
-// is available
-// ---------------------------------------------------------------------------
+// ----------------------------------------------------------------------------
+// CMCSPlugin::Suspend
+// 
+// ----------------------------------------------------------------------------
+//
+void CMCSPlugin::Suspend( TSuspendReason aReason )
+    {    
+    if ( aReason == EGeneralThemeChange )
+        {                   
+        TInt dataCount( iEngine->MenuItemCount() );
+        
+        for ( TInt i = 0; i < dataCount; i++ )
+            {
+            TRAP_IGNORE( iEngine->MenuDataL( i ).SetDirty( ETrue ) );
+            }                
+        }
+    else if ( aReason == EBackupRestore )
+        {
+        // Prevent item launching during backup / restore 
+        iEngine->SetBackupRestore( ETrue );
+        }
+    }
+
+// ----------------------------------------------------------------------------
+// CMCSPlugin::SubscribeL
+//
+// ----------------------------------------------------------------------------
 //
 void CMCSPlugin::SubscribeL( MAiContentObserver& aObserver )
     { 
     iObservers.AppendL( &aObserver );
     }
    
-// ---------------------------------------------------------------------------
-// Compare method to exclude the similar content items from array.
-// ---------------------------------------------------------------------------
-//
-TInt CMCSPlugin::CompareItems( const MAiPluginSettings& aFirst,
-        const MAiPluginSettings& aSecond )
-    {
-    MAiPluginSettings& first = const_cast<MAiPluginSettings&>(aFirst);
-    MAiPluginSettings& second = const_cast<MAiPluginSettings&>(aSecond);
-    return first.AiPluginContentItem().Name().CompareC(second.AiPluginContentItem().Name());    
-    }
-
-// ---------------------------------------------------------------------------
-// From class CAiContentPublisher
-// Plug-ins take ownership of the settings array, so it must either
-// store it in a member or free it.
-// Creates dynamic content model.
-// ---------------------------------------------------------------------------
+// ----------------------------------------------------------------------------
+// CMCSPlugin::ConfigureL
+// 
+// ----------------------------------------------------------------------------
 //
 void CMCSPlugin::ConfigureL( RAiSettingsItemArray& aSettings )
-    {
+    {    
+    iEngine = CMCSPluginEngine::NewL( *this, PublisherInfo().Namespace() );
     
     TLinearOrder<MAiPluginSettings> sortMethod( CMCSPlugin::CompareItems );
     RAiSettingsItemArray contentItemsArr;
 
-    TInt count = aSettings.Count();
+    TInt count( aSettings.Count() );
+    
     for ( TInt i = 0; i < count; i++ )
        {
-       MAiPluginSettings* pluginSetting = aSettings[ i ];
-       if( pluginSetting->AiPluginItemType() == EAiPluginContentItem )
+       MAiPluginSettings* setting( aSettings[ i ] );
+       
+       if( setting->AiPluginItemType() == EAiPluginContentItem )
            {
-           MAiPluginContentItem& contItem = pluginSetting->AiPluginContentItem();
-           TPtrC name = contItem.Name();
-           TPtrC type = contItem.Type();
-           contentItemsArr.InsertInOrder( pluginSetting, sortMethod );
+           MAiPluginContentItem& contItem( setting->AiPluginContentItem() );
+           TPtrC name( contItem.Name() );
+           TPtrC type( contItem.Type() );
+           
+           contentItemsArr.InsertInOrder( setting, sortMethod );
            }
-
        }
+    
     iDataCount = contentItemsArr.Count();
+    
     if ( iDataCount > 0 )
         {
         // Create the dynamic content Model
         DeleteContentModel();
+        
         iContentModel = new ( ELeave ) TAiContentItem[ iDataCount ];
+        
         for ( TInt i = 0; i < iDataCount; i++ )
             {
             iContentModel[i].id = i;
-            MAiPluginContentItem& contentItem = ( contentItemsArr[ i ] )->AiPluginContentItem();
+        
+            MAiPluginContentItem& contentItem( 
+                contentItemsArr[ i ]->AiPluginContentItem() );
 
             if( contentItem.Type() == KContentItemTypeText )
                 {
@@ -361,153 +318,87 @@
                 // image
                 iContentModel[i].type = KAiContentTypeBitmap;
                 }
-            TInt pos = contentItem.Name().Locate( KPluginNameSeprator );
+            
+            TInt pos( contentItem.Name().Locate( KPluginNameSeprator ) );
             
             HBufC* contentId = HBufC::NewL( contentItem.Name().Length() );
             CleanupStack::PushL( contentId );
-            TPtr ptr = contentId->Des();
+            
+            TPtr ptr( contentId->Des() ); 
             ptr = contentItem.Name().Mid( pos + 1 );
-            TInt sizeOfContentId = ptr.Size() +sizeof( wchar_t );
-            iContentModel[i].cid = static_cast<const wchar_t*>( User::AllocL( sizeOfContentId ) );
-            Mem::Copy((TAny*)iContentModel[i].cid, ptr.PtrZ(), sizeOfContentId);
+            
+            TInt sizeOfContentId( ptr.Size() + sizeof( wchar_t ) );
+            
+            iContentModel[i].cid = 
+                static_cast<const wchar_t*>( User::AllocL( sizeOfContentId ) );
+                
+            Mem::Copy( ( TAny* )iContentModel[i].cid, 
+                ptr.PtrZ(), sizeOfContentId );
+            
             CleanupStack::PopAndDestroy( contentId );
             }
-        iContent = AiUtility::CreateContentItemArrayIteratorL( iContentModel, iDataCount );
+        
+        iContent = AiUtility::CreateContentItemArrayIteratorL( 
+                iContentModel, iDataCount );
         }
+    
     contentItemsArr.Reset();
     // We own the array so destroy it
     aSettings.ResetAndDestroy();
     }
 
-// ---------------------------------------------------------------------------
-// From class CAiContentPublisher
-// Returns the extension interface. Actual type depends on the passed 
-// aUid argument.
-// ---------------------------------------------------------------------------
+// ----------------------------------------------------------------------------
+// CMCSPlugin::GetProperty
+// 
+// ----------------------------------------------------------------------------
 //
-TAny* CMCSPlugin::Extension( TUid aUid )
-    {    
-    if (aUid == KExtensionUidProperty)
-           {
-        return static_cast<MAiPropertyExtension*>(this);
+TAny* CMCSPlugin::GetProperty( TProperty aProperty )
+    {
+    if( aProperty == EPublisherContent )
+        {
+        return static_cast< MAiContentItemIterator* >( iContent );        
         }
-    else if (aUid == KExtensionUidEventHandler)
-        {
-        return static_cast<MAiEventHandlerExtension*>(this);
-        }
-    else
-        {    
-        return NULL;
-        }
+        
+    return NULL;
     }
-
-// ---------------------------------------------------------------------------
-// From class MAiPropertyExtension
-// Read property of publisher plug-in.
-// ---------------------------------------------------------------------------
+ 
+// ----------------------------------------------------------------------------
+// CMCSPlugin::HandleEvent
+// 
+// ----------------------------------------------------------------------------
 //
-TAny* CMCSPlugin::GetPropertyL( TInt aProperty )
-    {
-    TAny* property = NULL;
-    
-    switch ( aProperty )
+void CMCSPlugin::HandleEvent( const TDesC& aEventName, const TDesC& aParam )
+    {       
+    if( aEventName == KEventNameLaunchByIndex )
+        {        
+        TInt32 index;
+        AiUtility::ParseInt( index, aParam );
+        
+        TRAP_IGNORE( iEngine->LaunchItemL( index - 1 ) );
+        }
+    else if( aEventName == KEventNameShowSettings )
         {
-        case EAiPublisherInfo:
-            {
-             property = static_cast<TAiPublisherInfo*>( &iInfo );
-            break;  
-            }       
-    
-        case EAiPublisherContent:
-            {
-            property = static_cast<MAiContentItemIterator*>( iContent );
-            break;    
-            }
-        }
-    return property;
+        TRAP_IGNORE( iEngine->ShowSettingsL() );
+        }    
     }
 
-// ---------------------------------------------------------------------------
-// From class MAiPropertyExtension
-// Write property value to optimize the content model.
-// ---------------------------------------------------------------------------
+// ----------------------------------------------------------------------------
+// CMCSPlugin::CompareItems
+//
+// ----------------------------------------------------------------------------
 //
-void CMCSPlugin::SetPropertyL( TInt aProperty, TAny* aValue )
-    {  
-    switch ( aProperty )
-        {
-		case EAiPublisherInfo:
-			{
-			if( aValue )
-			    {
-    		    const TAiPublisherInfo* info = static_cast<const TAiPublisherInfo*>( aValue );
-	    	    iInfo.iName.Copy(info->iName);
-	    	    iInfo.iNamespace.Copy(info->iNamespace);
-			    }
-		    break;
-		    }
-		default:
-		    break;         
-        }
-    }
- 
-// ---------------------------------------------------------------------------
-// From class MAiEventHandlerExtension.
-// Handles an event sent by the AI framework.
-// ---------------------------------------------------------------------------
-//
-void CMCSPlugin::HandleEvent( TInt /*aEvent*/, const TDesC& /*aParam*/ )
+TInt CMCSPlugin::CompareItems( const MAiPluginSettings& aFirst,
+    const MAiPluginSettings& aSecond )
     {
-    // We have no way of reporting errors to framework so just ignore them.
-    //TRAP_IGNORE( iEngine->HandleEventL(aEvent, aParam ));
-    } 
-
-// ---------------------------------------------------------------------------
-// From class MAiEventHandlerExtension.
-// Handles an event sent by the AI framework.
-// ---------------------------------------------------------------------------
-//
-void CMCSPlugin::HandleEvent( const TDesC& aEventName, const TDesC& aParam )
-    {
-    if ( iEngine )
-        {
-        if( aEventName == KEventNameLaunchByIndex )
-            {
-            // We have no way of reporting errors to framework so just ignore them.
-            TInt32 index;
-            AiUtility::ParseInt( index, aParam );
-            TRAP_IGNORE( iEngine->LaunchItemL( index - 1 ));
-            }
-        else if( aEventName == KEventNameShowSettings )
-            {
-            TRAP_IGNORE( iEngine->ShowSettingsL() );
-            }
-        }
+    MAiPluginSettings& first = const_cast<MAiPluginSettings&>(aFirst);
+    MAiPluginSettings& second = const_cast<MAiPluginSettings&>(aSecond);
+    return first.AiPluginContentItem().Name().CompareC(second.AiPluginContentItem().Name());    
     }
 
-// ---------------------------------------------------------------------------
-// From class CAiContentPublisher
-// framework instructs plug-in that it is allowed to consume CPU resources
-// ---------------------------------------------------------------------------
+// ----------------------------------------------------------------------------
+// CMCSPlugin::DeleteContentModel
 //
-void CMCSPlugin::DoResumeL( TAiTransitionReason /*aReason*/ )
-    {
-    PublishL();
-    }
-    
-// ---------------------------------------------------------------------------
-// Frees engine resources
-// ---------------------------------------------------------------------------
-//    
-void CMCSPlugin::FreeEngine()
-    {
-    delete iEngine;
-    iEngine = NULL;
-    }
-
-// ---------------------------------------------------------------------------
-// Delete content model
-// ---------------------------------------------------------------------------
+// ----------------------------------------------------------------------------
 //    
 void CMCSPlugin::DeleteContentModel()
     {
@@ -528,11 +419,31 @@
         iContentModel = NULL;        
         }    
     }
-  
+
+// ----------------------------------------------------------------------------
+// CMCSPlugin::PublishRequired
+//
+// ----------------------------------------------------------------------------
+//    
+TBool CMCSPlugin::PublishRequired() const
+    {
+    TInt count( iEngine->MenuItemCount() );
+    
+    TBool retval( EFalse );
+    
+    for ( TInt i = 0; !retval && i < count; i++ )
+        {               
+        TRAP_IGNORE( retval = iEngine->MenuDataL( i ).IsDirty() );       
+        }
+        
+    
+    return retval;
+    }
+
 // ======== GLOBAL FUNCTIONS ========
-// ---------------------------------------------------------------------------
+// ----------------------------------------------------------------------------
 // Constructs and returns an application object.
-// ---------------------------------------------------------------------------
+// ----------------------------------------------------------------------------
 //
 EXPORT_C const TImplementationProxy* ImplementationGroupProxy( 
     TInt& aTableCount )
@@ -541,3 +452,5 @@
         sizeof( TImplementationProxy );
     return KImplementationTable;
     }
+
+// End of file