idlefw/plugins/shortcutplugin/src/caiscutplugin.cpp
branchRCL_3
changeset 9 d0529222e3f0
parent 4 1a2a00e78665
child 10 5ef93ea513cb
child 18 bd874ee5e5e2
equal deleted inserted replaced
4:1a2a00e78665 9:d0529222e3f0
     1 /*
       
     2 * Copyright (c) 2005-2006 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Plug-in main class
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <ecom/implementationproxy.h>
       
    20 #include <centralrepository.h>
       
    21 
       
    22 #include <aicontentobserver.h>
       
    23 #include <aipluginsettings.h>
       
    24 
       
    25 #include <platform/mw/aiscutuids.hrh>
       
    26 #include "aiscutpluginprivatecrkeys.h"
       
    27 #include "aiscutdefs.h"
       
    28 #include "aiscutcontentmodel.h"
       
    29 #include "caiscutplugin.h"
       
    30 #include "caiscutengine.h"
       
    31 #include "aiscutfactory.h"
       
    32 #include "caiscutshortcut.h"
       
    33 
       
    34 #include "debug.h"
       
    35 
       
    36 const TImplementationProxy KImplementationTable[] =
       
    37 {
       
    38     IMPLEMENTATION_PROXY_ENTRY(KImplUidScutPlugin, CAiScutPlugin::NewL)
       
    39 };
       
    40 
       
    41 // ======== MEMBER FUNCTIONS ========
       
    42 
       
    43 // ---------------------------------------------------------------------------
       
    44 //
       
    45 // ---------------------------------------------------------------------------
       
    46 //
       
    47 CAiScutPlugin::CAiScutPlugin()
       
    48 {
       
    49 }
       
    50 
       
    51 // ---------------------------------------------------------------------------
       
    52 //
       
    53 // ---------------------------------------------------------------------------
       
    54 //
       
    55 void CAiScutPlugin::ConstructL()
       
    56 {
       
    57     // Initialize the info which the fw uses to identify the plug-in.
       
    58     // Fill in only the uid at this point, the fw will send this back with the
       
    59     // name filled in later through SetPropertyL().
       
    60     iInfo.iUid.iUid = AI_UID_ECOM_IMPLEMENTATION_CONTENTPUBLISHER_SCUTPLUGIN;
       
    61 
       
    62     iContent = AiUtility::CreateContentItemArrayIteratorL(KAiScutContent);
       
    63     iResources = AiUtility::CreateContentItemArrayIteratorL(KAiScutResources);
       
    64     iEvents = AiUtility::CreateContentItemArrayIteratorL(KAiScutEvents);
       
    65 
       
    66     CRepository* repository = NULL;
       
    67     TRAP_IGNORE(repository = CRepository::NewL(KCRUidShortcutItems));
       
    68     // No leaving code here since 'repository' is not in cleanup stack.
       
    69     if (repository)
       
    70     {
       
    71         // A theme might not contain any publishable shortcuts at all, only
       
    72         // locked ones. To take this into account we must always delete old
       
    73         // theme-default settings to make sure the engine won't create any
       
    74         // unwanted shortcut objects. Any errors that might happen during
       
    75         // deletion are ignored to make sure the plug-in is kept alive.
       
    76         TUint32 errorKey;
       
    77 
       
    78         repository->Delete(KScutCenRepKeyThemeDefault, KScutCenRepKeyMask, errorKey);
       
    79     }
       
    80     delete repository;
       
    81 }
       
    82 
       
    83 // ---------------------------------------------------------------------------
       
    84 //
       
    85 // ---------------------------------------------------------------------------
       
    86 //
       
    87 CAiScutPlugin* CAiScutPlugin::NewL()
       
    88 {
       
    89     CAiScutPlugin* self = new (ELeave) CAiScutPlugin;
       
    90     CleanupStack::PushL(self);
       
    91     self->ConstructL();
       
    92     CleanupStack::Pop(self);
       
    93     return self;
       
    94 }
       
    95 
       
    96 // ---------------------------------------------------------------------------
       
    97 //
       
    98 // ---------------------------------------------------------------------------
       
    99 //
       
   100 CAiScutPlugin::~CAiScutPlugin()
       
   101 {
       
   102     TRAP_IGNORE( DeleteDefaultShortcutsL() );
       
   103     Release(iContent);
       
   104     Release(iResources);
       
   105     Release(iEvents);
       
   106     delete iEngine;
       
   107     iObservers.Close();
       
   108 }
       
   109 
       
   110 // ---------------------------------------------------------------------------
       
   111 // Publishes the given shortcut.
       
   112 // ---------------------------------------------------------------------------
       
   113 //
       
   114 void CAiScutPlugin::PublishShortcutsL(RAiShortcutArray& aShortcuts)
       
   115 {
       
   116     TInt err = KErrNone;
       
   117     TInt observers = iObservers.Count();
       
   118     TInt shortcuts = aShortcuts.Count();
       
   119     
       
   120     for (TInt i = 0; i < observers; ++i)
       
   121     {
       
   122         MAiContentObserver* observer = iObservers[i];
       
   123         TBool transactionStarted = EFalse;
       
   124         TInt transactionId = reinterpret_cast<TInt>(this);
       
   125         err = observer->StartTransaction(transactionId);
       
   126 
       
   127         if (err != KErrNotSupported)
       
   128         {
       
   129             // The observer does not support transactions, check for real errors.
       
   130             User::LeaveIfError(err);
       
   131             transactionStarted = ETrue;
       
   132         }
       
   133         err = KErrAlreadyExists;
       
   134         for (TInt j = 0; j < shortcuts; j++)
       
   135         {
       
   136             CAiScutShortcut* shortcut = aShortcuts[j];
       
   137 
       
   138             if (shortcut->NeedsToBePublished())
       
   139             {
       
   140                 shortcut->Publish(*this, *observer);
       
   141                 err = KErrNone;
       
   142             }
       
   143         }
       
   144 
       
   145         if ( transactionStarted && err == KErrNone )
       
   146             {
       
   147             User::LeaveIfError(observer->Commit(transactionId));
       
   148             }
       
   149         else if ( transactionStarted )
       
   150             {
       
   151             User::LeaveIfError(observer->CancelTransaction(transactionId));
       
   152             }
       
   153     }
       
   154 }
       
   155 
       
   156 // ---------------------------------------------------------------------------
       
   157 // Is plug-in suspended or not.
       
   158 // ---------------------------------------------------------------------------
       
   159 //
       
   160 TBool CAiScutPlugin::IsAlive() const
       
   161 {
       
   162     return iAlive;
       
   163 }
       
   164 
       
   165 // ---------------------------------------------------------------------------
       
   166 // Resume reason
       
   167 // ---------------------------------------------------------------------------
       
   168 //
       
   169 TAiTransitionReason CAiScutPlugin::ResumeReason() const
       
   170     {
       
   171     return iResumeReason;
       
   172     }
       
   173 
       
   174 // ---------------------------------------------------------------------------
       
   175 // From class CAiContentPublisher.
       
   176 // Resumes the plug-in.
       
   177 // ---------------------------------------------------------------------------
       
   178 //
       
   179 void CAiScutPlugin::Resume(TAiTransitionReason aReason)
       
   180 {
       
   181     TRAP_IGNORE(DoResumeL(aReason));    
       
   182 }
       
   183 
       
   184 // ---------------------------------------------------------------------------
       
   185 // Resumes the plug-in.
       
   186 // ---------------------------------------------------------------------------
       
   187 //
       
   188 void CAiScutPlugin::DoResumeL(TAiTransitionReason aReason)
       
   189 {
       
   190 	__PRINT( __DBG_FORMAT( "XAI: CAiScutPlugin::Resume reason %d alive = %d"), aReason, iAlive);
       
   191     iResumeReason = aReason;
       
   192 	// Reload the engine in case general theme changed or
       
   193 	// the engine has been suspended. 
       
   194     if (aReason == EAiGeneralThemeChanged || !iAlive)
       
   195     {
       
   196         // if general theme changed, free engine so that is will be
       
   197         // loaded again because shortcut icons must be re-created.
       
   198         FreeEngine();
       
   199     }
       
   200 
       
   201     if (!iEngine)
       
   202     {
       
   203 		iEngine = AiScutFactory::CreateAiScutEngineL(*this);
       
   204     }
       
   205 
       
   206     iEngine->ResumeL( (iAlive == EFalse) ||
       
   207         (
       
   208         aReason != EAiBacklightOn    &&
       
   209         aReason != EAiBacklightOff   &&
       
   210         aReason != EAiIdleBackground &&
       
   211         aReason != EAiIdleForeground
       
   212         ),
       
   213         aReason
       
   214     );
       
   215     
       
   216     iAlive = ETrue;
       
   217 }
       
   218 
       
   219 // ---------------------------------------------------------------------------
       
   220 // From class CAiContentPublisher.
       
   221 // Suspends the plug-in.
       
   222 // ---------------------------------------------------------------------------
       
   223 //
       
   224 void CAiScutPlugin::Suspend(TAiTransitionReason aReason)
       
   225 {
       
   226 	__PRINT( __DBG_FORMAT( "XAI: CAiScutPlugin::Suspend reason %d"), aReason);
       
   227 
       
   228     if (iEngine)
       
   229     {
       
   230         iEngine->Suspend();
       
   231     }
       
   232 
       
   233     iAlive = EFalse;
       
   234 }
       
   235 
       
   236 // ---------------------------------------------------------------------------
       
   237 // From class CAiContentPublisher.
       
   238 // Frees the plug-in engine.
       
   239 // ---------------------------------------------------------------------------
       
   240 //
       
   241 void CAiScutPlugin::Stop(TAiTransitionReason aReason)
       
   242 {
       
   243 	__PRINT( __DBG_FORMAT( "XAI: CAiScutPlugin::Stop reason %d"), aReason);
       
   244 
       
   245 	if ( aReason == EAiBackupRestoreStarted )
       
   246 	    {
       
   247 	    Suspend( aReason );
       
   248 	    }
       
   249 	else
       
   250 	    {
       
   251 	    FreeEngine();
       
   252 	    }
       
   253 }
       
   254 // ---------------------------------------------------------------------------
       
   255 // From class CAiContentPublisher.
       
   256 // Adds the content observer / subscriber to plug-in.
       
   257 // ---------------------------------------------------------------------------
       
   258 //
       
   259 void CAiScutPlugin::SubscribeL(MAiContentObserver& aObserver)
       
   260 {
       
   261     iObservers.AppendL(&aObserver);
       
   262 }
       
   263 
       
   264 void CAiScutPlugin::DeleteDefaultShortcutsL()
       
   265     {
       
   266     // Ignore any errors that might occur when deleting
       
   267     // the default keys
       
   268     TUint32 errorKey;
       
   269     CRepository* cr = CRepository::NewL(KCRUidShortcutItems);
       
   270     cr->Delete(KScutCenRepKeyThemeDefault, KScutCenRepKeyMask, errorKey);
       
   271     delete cr;
       
   272     }
       
   273 
       
   274 // ---------------------------------------------------------------------------
       
   275 // From class CAiContentPublisher.
       
   276 // Configures the plug-in.
       
   277 // ---------------------------------------------------------------------------
       
   278 //
       
   279 void CAiScutPlugin::ConfigureL(RAiSettingsItemArray& aSettings)
       
   280 {
       
   281     CRepository* repository = NULL;
       
   282 
       
   283     TRAPD(err, repository = CRepository::NewL(KCRUidShortcutItems));
       
   284 
       
   285     if (err == KErrNotFound)
       
   286     {
       
   287         // CenRep file is missing from the image, this is a serious error.
       
   288         User::Leave(err);
       
   289     }
       
   290 
       
   291     // Write settings if repository was successfully opened. All other errors
       
   292     // are ignored to ensure that the plug-in is up and running, even if
       
   293     // crippled.
       
   294 
       
   295     // No leaving code here since 'repository' is not in cleanup stack.
       
   296     if (repository)
       
   297     {
       
   298         repository->StartTransaction(CRepository::EConcurrentReadWriteTransaction);
       
   299 
       
   300         // A theme might not contain any publishable shortcuts at all, only
       
   301         // locked ones. To take this into account we must always delete old
       
   302         // theme-default settings to make sure the engine won't create any
       
   303         // unwanted shortcut objects. Any errors that might happen during
       
   304         // deletion are ignored to make sure the plug-in is kept alive.
       
   305         TUint32 errorKey;
       
   306 
       
   307         repository->Delete(KScutCenRepKeyThemeDefault, KScutCenRepKeyMask, errorKey);
       
   308         
       
   309 		__PRINTS( "XAI: CAiScutPlugin::ConfigureL");
       
   310 
       
   311         TInt count = aSettings.Count();
       
   312         if (count > 0)
       
   313         {
       
   314             // Write new shortcut definitions.
       
   315             for (TInt i = 0; i < count; ++i)
       
   316             {
       
   317                 MAiPluginSettings* settings = aSettings[ i ];
       
   318                 
       
   319                 if( settings->AiPluginItemType() == EAiPluginSettingsItem )
       
   320                     {
       
   321                     MAiPluginSettingsItem& item = settings->AiPluginSettingsItem();
       
   322                     TUint32 key = item.Key(); // implicit cast from TInt32 to TUint32.
       
   323     
       
   324     				__PRINT( __DBG_FORMAT( "XAI:   %d. key = 0x%x"), i+1, key);
       
   325     
       
   326                     // Add the bit that indicates this is a default shortcut setting.
       
   327                     key |= KScutFlagBitThemeDefault;
       
   328     
       
   329                     // Ignore possible error and keep going.
       
   330                     repository->Create(key, item.Value());
       
   331                     }
       
   332             }
       
   333         }
       
   334 
       
   335         TUint32 info = 0;
       
   336         repository->CommitTransaction(info);
       
   337 
       
   338         delete repository;
       
   339         repository = NULL;
       
   340         
       
   341         if (iEngine)
       
   342         {
       
   343             iEngine->CreateShortcutsL();
       
   344             iEngine->ResumeL( ETrue, EAiGeneralThemeChanged );
       
   345         }
       
   346     }
       
   347 
       
   348     // We don't need to store the settings so clear the array.
       
   349     aSettings.ResetAndDestroy();
       
   350 }
       
   351 
       
   352 // ---------------------------------------------------------------------------
       
   353 // From class CAiContentPublisher.
       
   354 // Returns the extension interface.
       
   355 // ---------------------------------------------------------------------------
       
   356 //
       
   357 TAny* CAiScutPlugin::Extension(TUid aUid)
       
   358 {
       
   359     if (aUid == KExtensionUidProperty)
       
   360     {
       
   361         return static_cast<MAiPropertyExtension*>(this);
       
   362     }
       
   363     else if (aUid == KExtensionUidEventHandler)
       
   364     {
       
   365         return static_cast<MAiEventHandlerExtension*>(this);
       
   366     }
       
   367     else
       
   368     {
       
   369         return NULL;
       
   370     }
       
   371 }
       
   372 
       
   373 // ---------------------------------------------------------------------------
       
   374 // From class MAiEventHandlerExtension.
       
   375 // Handles an event sent by the AI framework.
       
   376 // ---------------------------------------------------------------------------
       
   377 //
       
   378 void CAiScutPlugin::HandleEvent(TInt aEvent, const TDesC& aParam)
       
   379 {
       
   380     if (iEngine)
       
   381     {
       
   382         // We have no way of reporting errors to framework so just ignore them.
       
   383         TRAP_IGNORE(iEngine->HandleAiEventL(aEvent, aParam));
       
   384     }
       
   385 }
       
   386 
       
   387 // ---------------------------------------------------------------------------
       
   388 // CAiScutPlugin::HasMenuItem
       
   389 //
       
   390 // ---------------------------------------------------------------------------
       
   391 //
       
   392 TBool CAiScutPlugin::HasMenuItem(const TDesC16& /*aMenuItem*/)
       
   393     {
       
   394     return EFalse;
       
   395     }
       
   396 
       
   397 // ---------------------------------------------------------------------------
       
   398 // From class MAiPropertyExtension
       
   399 // Returns a plug-in property.
       
   400 // ---------------------------------------------------------------------------
       
   401 //
       
   402 TAny* CAiScutPlugin::GetPropertyL(TInt aProperty)
       
   403 {
       
   404     switch (aProperty)
       
   405     {
       
   406     case EAiPublisherInfo:
       
   407         return static_cast<TAiPublisherInfo*>(&iInfo);
       
   408 
       
   409     case EAiPublisherContent:
       
   410         return static_cast<MAiContentItemIterator*>(iContent);
       
   411 
       
   412     case EAiPublisherResources:
       
   413         return static_cast<MAiContentItemIterator*>(iResources);
       
   414 
       
   415     case EAiPublisherEvents:
       
   416         return static_cast<MAiContentItemIterator*>(iEvents);
       
   417 
       
   418     default:
       
   419         break;
       
   420     }
       
   421 
       
   422     return NULL;
       
   423 }
       
   424 
       
   425 // ---------------------------------------------------------------------------
       
   426 // From class MAiPropertyExtension
       
   427 // Sets a plug-in property to optimize the content model.
       
   428 // ---------------------------------------------------------------------------
       
   429 //
       
   430 void CAiScutPlugin::SetPropertyL(TInt aProperty, TAny* aValue)
       
   431     {
       
   432     if( aProperty == EAiPublisherInfo )
       
   433         {
       
   434         ASSERT( aValue );
       
   435         
       
   436         const TAiPublisherInfo* info( 
       
   437                 static_cast<const TAiPublisherInfo*>( aValue ) );
       
   438         
       
   439         iInfo = *info;               
       
   440         }    
       
   441     }
       
   442 
       
   443 // ---------------------------------------------------------------------------
       
   444 //
       
   445 //
       
   446 // ---------------------------------------------------------------------------
       
   447 //
       
   448 void CAiScutPlugin::FreeEngine()
       
   449 {
       
   450     delete iEngine;
       
   451     iEngine = NULL;
       
   452     iAlive = EFalse;    
       
   453 }
       
   454 
       
   455 // ======== GLOBAL FUNCTIONS ========
       
   456 
       
   457 // ---------------------------------------------------------------------------
       
   458 //
       
   459 // ---------------------------------------------------------------------------
       
   460 //
       
   461 EXPORT_C const TImplementationProxy* ImplementationGroupProxy(TInt& aTableCount)
       
   462 {
       
   463     aTableCount = sizeof(KImplementationTable) / sizeof(TImplementationProxy);
       
   464     return KImplementationTable;
       
   465 }
       
   466 
       
   467 // End of File.