scrsaver/scrsaverplugins/SlideshowPlugin/src/SlideshowPlugin.cpp
branchRCL_3
changeset 26 e8d784ac1a4b
child 27 fc25e03508fd
equal deleted inserted replaced
25:aaeeca1f15af 26:e8d784ac1a4b
       
     1 /*
       
     2 * Copyright (c) 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:   This file implements the Slideshow Screensaver plugin.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #include <eikenv.h>
       
    21 #include <bautils.h>
       
    22 #include <data_caging_path_literals.hrh>
       
    23 #include <centralrepository.h>
       
    24 #include <DRMHelper.h>
       
    25 
       
    26 // #include <aknnotewrappers.h> AppendSlideL
       
    27 // #include <aknnavi.h>
       
    28 // #include <aknnavide.h>
       
    29 // #include <mmfcontrollerpluginresolver.h>
       
    30 #include <DRMHelperServerInternalCRKeys.h>
       
    31 #include <mmf/common/mmfcontrollerpluginresolver.h> // For CleanupResetAndDestroyPushL
       
    32 #include <pslninternalcrkeys.h>
       
    33 #include <pslnslidesetdialoginterface.h>
       
    34 
       
    35 // #include <NPNExtensions.h>
       
    36 #include <coecntrl.h>
       
    37 #include <ecom/ecom.h>
       
    38 
       
    39 #include <ContentListingFactory.h>
       
    40 #include <mdeobjectdef.h>
       
    41 #include <mdelogiccondition.h>
       
    42 #include <mdenamespacedef.h>
       
    43 #include <mdeconstants.h>
       
    44 #include <mdeobjectquery.h>
       
    45 
       
    46 #include <slideshowplugin.rsg>
       
    47 
       
    48 #include "SlideshowPlugin.h"
       
    49 #include "SlideshowSlide.h"
       
    50 #include "SlideshowPluginUtils.h"
       
    51 
       
    52 // Constants
       
    53 const TInt KSecsToMicros = 1000000;
       
    54 const TUint KDefaultRandomLoadingNumber = 100;
       
    55 
       
    56 // Slideshow duration times (secs)
       
    57 const TInt KMinSlideshowTime = 1;
       
    58 const TInt KMaxSlideshowTime = 60;
       
    59 const TInt KDefaultSlideshowTime = 5;
       
    60 
       
    61 // Backlight times (secs)
       
    62 const TInt KMinLightsTime = 0;
       
    63 const TInt KMaxLightsTime = 30;
       
    64 const TInt KDefaultLightsTime = 0;
       
    65 
       
    66 // Slide times 
       
    67 // const TInt KMinSlideTime = 1;
       
    68 // const TInt KMaxSlideTime = 30;
       
    69 const TInt KDefaultSlideTime = 5;
       
    70 
       
    71 // Refresh interval (Draw() call frequency) in secs.
       
    72 // For better resolution kept smaller than slide time
       
    73 const TInt KRefreshInterval = 1;
       
    74 
       
    75 // Slideshow type
       
    76 const TInt KSlideshowTypeContinuous = 0;
       
    77 const TInt KSlideshowTypeRandom = 1;
       
    78 
       
    79 // const TInt KScreensaverAnimPluginInterfaceUid = 0x102750CB;
       
    80 
       
    81 _LIT(KResourceFileName, "Z:SlideshowPlugin.rsc");
       
    82 
       
    83 
       
    84 CSlideshowSettings::CSlideshowSettings()
       
    85     {
       
    86     iSlideshowTime = KDefaultSlideshowTime;
       
    87     iLightsTime = KDefaultLightsTime;
       
    88     iSlideTime = KDefaultSlideTime;
       
    89     iSlideshowType = KSlideshowTypeContinuous;
       
    90     }
       
    91 
       
    92 // ============================ MEMBER FUNCTIONS =============================
       
    93 
       
    94 
       
    95 // ---------------------------------------------------------------------------
       
    96 // CSlideshowPlugin::NewL
       
    97 // ---------------------------------------------------------------------------
       
    98 //
       
    99 CSlideshowPlugin* CSlideshowPlugin::NewL()
       
   100     {
       
   101     CSlideshowPlugin* self = new (ELeave) CSlideshowPlugin;
       
   102     CleanupStack::PushL(self);
       
   103     self->ConstructL();
       
   104     CleanupStack::Pop(self);
       
   105     return self;
       
   106     }
       
   107 
       
   108 
       
   109 // ----------------------------------------------------------------------------
       
   110 // CSlideshowPlugin::~CSlideshowPlugin
       
   111 // ----------------------------------------------------------------------------
       
   112 //   
       
   113 CSlideshowPlugin::~CSlideshowPlugin()
       
   114     {    
       
   115     if (iResourceOffset != 0)
       
   116         {
       
   117         iEikEnv->DeleteResourceFile(iResourceOffset);
       
   118         }
       
   119 
       
   120     // Close and delete CR handlers
       
   121     NotifyCenrepChangeCancel();
       
   122     UnInitializeCenRep();
       
   123 
       
   124     // Close and delete P&S handlers
       
   125     if (iSettingsChangedSubscriber)
       
   126         {
       
   127         iSettingsChangedSubscriber->StopSubscribe();
       
   128         }
       
   129     iSettingsChangedProperty.Close();
       
   130     delete iSettingsChangedSubscriber;
       
   131     
       
   132     delete iScreensaverName;
       
   133     delete iSettings;
       
   134     delete iDrmHelper;
       
   135     delete iModel;
       
   136 
       
   137     // First model, then engine, otherwise bad things happen
       
   138     delete iMdESession;
       
   139     
       
   140     // Logging done
       
   141     SSPLOGGER_DELETE;
       
   142     }
       
   143 
       
   144 // -----------------------------------------------------------------------------
       
   145 // CSlideshowPlugin::ConstructL
       
   146 // -----------------------------------------------------------------------------
       
   147 //    
       
   148 void CSlideshowPlugin::ConstructL()
       
   149     {
       
   150     // Start logging
       
   151     SSPLOGGER_CREATE;
       
   152     
       
   153     iModel = CSlideshowModel::NewL();
       
   154     iSettings = new (ELeave) CSlideshowSettings();
       
   155 
       
   156     // Get nearest language resource file and add it to the
       
   157     // eikon environment for the duration of the plugin life
       
   158     TFileName fileName;
       
   159     TParse lParse;
       
   160     
       
   161     // Add the resource dir
       
   162     lParse.Set(KResourceFileName, &KDC_RESOURCE_FILES_DIR, NULL);
       
   163     
       
   164     // Get the filename with full path
       
   165     fileName = lParse.FullName();
       
   166 
       
   167     iEikEnv = CEikonEnv::Static();
       
   168 
       
   169     BaflUtils::NearestLanguageFile(iEikEnv->FsSession(), fileName);
       
   170         
       
   171     iResourceOffset = iEikEnv->AddResourceFileL(fileName);
       
   172 
       
   173     // Load localised name of screensaver to be returned 
       
   174     // to Themes app in Name() query
       
   175     iScreensaverName = iEikEnv->AllocReadResourceL(R_SLIDESHOW_SCREENSAVER_NAME);
       
   176 
       
   177     InitializeCenRepL();
       
   178 
       
   179     // Read settings
       
   180     ReadSettings();
       
   181  
       
   182     iTimerUpdated = EFalse;
       
   183     }
       
   184 
       
   185 
       
   186 // -----------------------------------------------------------------------------
       
   187 // CSlideshowPlugin::CSlideshowPlugin
       
   188 // -----------------------------------------------------------------------------
       
   189 CSlideshowPlugin::CSlideshowPlugin()
       
   190     {
       
   191     }
       
   192 
       
   193 
       
   194 // --- MScreensaverPlugin ---
       
   195 
       
   196 
       
   197 // -----------------------------------------------------------------------------
       
   198 // CSlideshowPlugin::InitializeL
       
   199 // -----------------------------------------------------------------------------
       
   200 TInt CSlideshowPlugin::InitializeL(MScreensaverPluginHost *aHost)
       
   201     {
       
   202     SSPLOGGER_ENTERFN("InitializeL()");
       
   203 
       
   204     iWaitActive = EFalse;
       
   205     iDrawCount = 0;
       
   206 
       
   207     if (!aHost)
       
   208         {
       
   209         return KErrArgument;
       
   210         }
       
   211 
       
   212     iHost = aHost;   
       
   213     // iHost->UseRefreshTimer( EFalse );
       
   214 
       
   215     NotifyCenrepChangeL();
       
   216 
       
   217     // No indicators while plugin running
       
   218     iHost->OverrideStandardIndicators();    
       
   219     
       
   220     // Get display info
       
   221     UpdateDisplayInfo();
       
   222 
       
   223     // Attach and start watching settings changed key
       
   224     User::LeaveIfError(iSettingsChangedProperty.Attach(
       
   225         KPSUidScreenSaver, KScreenSaverPluginSettingsChanged));
       
   226 
       
   227     iSettingsChangedSubscriber = new (ELeave) CPSSubscriber(
       
   228         TCallBack(HandleSettingsChanged, this), iSettingsChangedProperty);
       
   229     iSettingsChangedSubscriber->SubscribeL();
       
   230     
       
   231     iDrmHelper = CDRMHelper::NewL(*iEikEnv);
       
   232     
       
   233     // Load slides on start rather than here
       
   234     // LoadSlidesL();
       
   235 
       
   236     SSPLOGGER_LEAVEFN("InitializeL()");
       
   237     
       
   238     return KErrNone;
       
   239     }
       
   240 
       
   241 
       
   242 // -----------------------------------------------------------------------------
       
   243 // CSlideshowPlugin::Draw
       
   244 // -----------------------------------------------------------------------------
       
   245 TInt CSlideshowPlugin::Draw(CWindowGc& aGc)
       
   246     {
       
   247     SSPLOGGER_ENTERFN("Draw()");
       
   248     
       
   249     // Clear the screen on the very first draw call after starting
       
   250     if (iDrawCount < 0)
       
   251         {
       
   252         iDrawCount = 0;
       
   253         aGc.SetBrushColor(KRgbBlack);
       
   254         aGc.Clear();
       
   255         }
       
   256     
       
   257     // If still waiting for CLF, bail out
       
   258     if (iWaitActive)
       
   259         {
       
   260         SSPLOGGER_WRITE("Draw(): Waiting for CLF, bail out");
       
   261         SSPLOGGER_LEAVEFN("Draw()");
       
   262         return KErrNone;
       
   263         }
       
   264     // Make sure we have something to display
       
   265     if (iModel->NumberOfSlides() == 0 && iIsLoadFinished ) 
       
   266     	{
       
   267     	iHost->RevertToDefaultSaver();
       
   268         SSPLOGGER_WRITE("Draw(): Nothing to display, suspending");
       
   269         SSPLOGGER_LEAVEFN("Draw()");
       
   270 //        iHost->Suspend( -1 ); // This place will be crashed when transform to default type
       
   271         return KErrNone;
       
   272         }
       
   273     
       
   274     CSlideshowSlide* pSlide = iModel->NextSlide(EFalse);
       
   275    // get the status of the slide
       
   276     TInt status = iModel->SlideStatus(pSlide); // TInt   decoded(1), decoding(2), not decoding(3),no slide (0)
       
   277     											// 4 could not decode the slide
       
   278     if(KStatusDecodingInProgress == status) 
       
   279     	{
       
   280     	// decoding in progress... return
       
   281     	// when ever decoding is going on.. call draw method every 1 second to check the status of the decoding
       
   282     	// and draw the image as soon as it is available ( otherwise Draw is called every 5 secods, very bad user responce)
       
   283     	if(iTimerUpdated)
       
   284     		{
       
   285     		iHost->SetRefreshTimerValue(KSecsToMicros);
       
   286     		iTimerUpdated = EFalse;
       
   287     		}
       
   288     	return KErrNone;
       
   289       	}
       
   290     else if (KStatusSlideDecoded == status)
       
   291     	{
       
   292     	// File was decoded properly and available
       
   293     	SSPLOGGER_WRITEF(_L("SSP: Draw(): Got slide (%x), drawing"), pSlide);
       
   294 
       
   295     	// Consume slide's DRM rights
       
   296     	SlideshowUtil::DRMConsume(iDrmHelper, pSlide);
       
   297           
       
   298     	// Make sure the window is empty in case the bitmap doesn't
       
   299     	// fill the whole screen. Draw background black
       
   300     	aGc.SetBrushColor(KRgbBlack);
       
   301     	// aGc.SetBrushStyle(CGraphicsContext::ESolidBrush);
       
   302     	aGc.Clear();
       
   303     	// Draw image centered
       
   304     	DrawCentered(aGc, pSlide);
       
   305     	if(iHost && !iTimerUpdated)
       
   306         	{
       
   307         	iHost->SetRefreshTimerValue(iSettings->iSlideTime * KSecsToMicros);
       
   308         	iTimerUpdated = ETrue;
       
   309         	}
       
   310     	// Flush draw buffer so that the new image is displayed immediately
       
   311     	FlushDrawBuffer();
       
   312     	SSPLOGGER_WRITE("Draw(): Prepare next slide");
       
   313 
       
   314     	SSPLOGGER_LEAVEFN("Draw()");
       
   315     	}
       
   316     
       
   317     // Prefetch image for the next slide
       
   318     iModel->PrepareNextSlide(
       
   319             (iSettings->iSlideshowType == KSlideshowTypeRandom), iDi.iRect.Size());
       
   320     
       
   321     SSPLOGGER_LEAVEFN("Draw()");
       
   322 
       
   323     return KErrNone;
       
   324     	
       
   325   }
       
   326 
       
   327 
       
   328 // -----------------------------------------------------------------------------
       
   329 // CSlideshowPlugin::Name
       
   330 // -----------------------------------------------------------------------------
       
   331 const TDesC16& CSlideshowPlugin::Name() const
       
   332     {
       
   333     if (iScreensaverName)
       
   334         {
       
   335         return *iScreensaverName;
       
   336         }
       
   337     else
       
   338         {
       
   339         return KNullDesC;
       
   340         }
       
   341     }
       
   342 
       
   343 
       
   344 // -----------------------------------------------------------------------------
       
   345 // CSlideshowPlugin::Capabilities
       
   346 // -----------------------------------------------------------------------------
       
   347 TInt CSlideshowPlugin::Capabilities()
       
   348     {
       
   349     return (EScpCapsConfigure | EScpCapsSelectionNotification);
       
   350     }
       
   351 
       
   352 
       
   353 // -----------------------------------------------------------------------------
       
   354 // CSlideshowPlugin::PluginFunction
       
   355 // -----------------------------------------------------------------------------
       
   356 TInt CSlideshowPlugin::PluginFunction(
       
   357     TScPluginCaps aFunction,
       
   358     TAny* aParam)
       
   359     {
       
   360     TInt ret = KErrNone;
       
   361     
       
   362     switch (aFunction)
       
   363         {
       
   364         case EScpCapsSelectionNotification:
       
   365         case EScpCapsPreviewNotification:
       
   366         // Peek into the slideset
       
   367         TRAP(ret, LoadSlidesL());
       
   368 	    if (ret!=KErrNone || iModel->NumberOfSlides() == 0)
       
   369             {
       
   370             SSPLOGGER_WRITE("No slides selected - launching settings");
       
   371             TRAP(ret, ConfigureL(aParam));
       
   372             // Check if at least one file is selected    
       
   373             ReadSettings();// update the key value of KThemesScreenSaverSlideSetType
       
   374             TRAP(ret, LoadSlidesL());
       
   375             if ( ret == KErrNone && iModel->NumberOfSlides() == 0)
       
   376                 {
       
   377                 // Don't Pop an error note, just return error
       
   378                 return KErrCancel;          
       
   379                 }   
       
   380             }
       
   381             break;      
       
   382         case EScpCapsConfigure:
       
   383             // Configure command
       
   384             TRAP(ret, ConfigureL(aParam));
       
   385             break;
       
   386         default:
       
   387             break;
       
   388         }
       
   389     
       
   390     return ret;
       
   391     }  
       
   392 
       
   393 
       
   394 // -----------------------------------------------------------------------------
       
   395 // CSlideshowPlugin::HandleScreensaverEventL
       
   396 // -----------------------------------------------------------------------------
       
   397 TInt CSlideshowPlugin::HandleScreensaverEventL(
       
   398     TScreensaverEvent aEvent,
       
   399     TAny* /*aData*/ )
       
   400     {
       
   401     switch (aEvent)
       
   402         {
       
   403         case EScreensaverEventStopping:
       
   404             StopPlugin();
       
   405             break;
       
   406             
       
   407         case EScreensaverEventStarting:
       
   408             StartPlugin();
       
   409             break;
       
   410             
       
   411         case EScreensaverEventDisplayChanged:
       
   412             UpdateDisplayInfo();
       
   413             break;
       
   414 
       
   415         case EScreensaverEventTimeout:
       
   416             StopPlugin();
       
   417             iHost->Suspend(-1);
       
   418             break;
       
   419         
       
   420         default:
       
   421             break;
       
   422         }
       
   423     return KErrNone;
       
   424     }  
       
   425 
       
   426 // -----------------------------------------------------------------------------
       
   427 // CSlideshowPlugin::ReadSettings
       
   428 // -----------------------------------------------------------------------------
       
   429 TInt CSlideshowPlugin::ReadSettings()
       
   430     {
       
   431     
       
   432     TInt err = KErrNone;
       
   433     
       
   434     err = iSlideshowRepository->Get(
       
   435         KThemesScreenSaverSlideSetDuration,
       
   436         iSettings->iSlideshowTime);
       
   437                           
       
   438     if ((iSettings->iSlideshowTime < KMinSlideshowTime) ||
       
   439         (iSettings->iSlideshowTime > KMaxSlideshowTime))
       
   440         {
       
   441         // Out of range, set to default
       
   442         iSettings->iSlideshowTime = KDefaultSlideshowTime;
       
   443         }
       
   444     
       
   445     err = iSlideshowRepository->Get(
       
   446         KThemesScreenSaverSlideSetBacklight,
       
   447         iSettings->iLightsTime);
       
   448 
       
   449     if ((iSettings->iLightsTime < KMinLightsTime) ||
       
   450         (iSettings->iLightsTime > KMaxLightsTime))
       
   451         {
       
   452         // Out of range, set to default
       
   453         iSettings->iLightsTime = KDefaultLightsTime;
       
   454         }
       
   455 
       
   456     err = iSlideshowRepository->Get(
       
   457         KThemesScreenSaverSlideSetType,
       
   458         iSettings->iSlideshowType);
       
   459 
       
   460     // No setting for slide timing
       
   461     iSettings->iSlideTime = KDefaultSlideTime;
       
   462 
       
   463     return err;             
       
   464     }
       
   465 
       
   466 #if 0
       
   467 
       
   468 // -----------------------------------------------------------------------------
       
   469 // CSlideshowPlugin::UpdateFileName
       
   470 // -----------------------------------------------------------------------------
       
   471 //      
       
   472 TBool CSlideshowPlugin::UpdateFileName()
       
   473     {
       
   474     if ( !BaflUtils::FileExists( iEikEnv->FsSession(), 
       
   475                                 iSettings->iFileName  ) )
       
   476         {
       
   477         // File is deleted now, Show empty string
       
   478         iSettings->iFileName.Copy( KEmptyString );        
       
   479         return ETrue;
       
   480         }
       
   481     
       
   482     TBool canbeautomated(EFalse);   
       
   483 
       
   484     TInt res = 0;
       
   485     iDrmHelper->SetAutomatedType( 
       
   486                    CDRMHelper::EAutomatedTypeScreenSaver );
       
   487     res = iDrmHelper->CanSetAutomated( iSettings->iFileName, 
       
   488                                        canbeautomated );
       
   489 
       
   490     if ( res || !canbeautomated)
       
   491         {
       
   492         iSettings->iFileName.Copy( KEmptyString );
       
   493         return ETrue;
       
   494         }
       
   495     return EFalse;
       
   496     }
       
   497 
       
   498 #endif      
       
   499     
       
   500 // -----------------------------------------------------------------------------
       
   501 // CSlideshowPlugin::HandleRepositoryCallBack
       
   502 // -----------------------------------------------------------------------------
       
   503 //                
       
   504 TInt CSlideshowPlugin::HandleRepositoryCallBack(TAny* aPtr)    
       
   505     {
       
   506     STATIC_CAST(CSlideshowPlugin*, aPtr)->HandleCRSettingsChange(aPtr);
       
   507     return KErrNone;
       
   508     }
       
   509     
       
   510     
       
   511 // -----------------------------------------------------------------------------
       
   512 // CSlideshowPlugin::HandleCRSettingsChange
       
   513 // -----------------------------------------------------------------------------
       
   514 //                
       
   515 void CSlideshowPlugin::HandleCRSettingsChange(TAny* /*aPtr*/)    
       
   516     {    
       
   517     if (iSlideshowCRWatcher)
       
   518         {
       
   519         TInt changedKey = iSlideshowCRWatcher->ChangedKey();
       
   520         
       
   521         switch (changedKey)
       
   522             {
       
   523             case KThemesScreenSaverSlideSetType:
       
   524             case KThemesScreenSaverSlideSetBacklight:
       
   525             case KThemesScreenSaverSlideSetDuration:
       
   526                 // Update settings
       
   527                 ReadSettings();
       
   528                 break;
       
   529                 
       
   530             default:
       
   531                 // Not interested in other keys
       
   532                 break;
       
   533             }
       
   534         }
       
   535     }
       
   536 
       
   537 
       
   538 // -----------------------------------------------------------------------------
       
   539 // CSlideshowPlugin::HandleSettingsChanged
       
   540 // -----------------------------------------------------------------------------
       
   541 //                
       
   542 TInt CSlideshowPlugin::HandleSettingsChanged(TAny* aPtr)    
       
   543     {    
       
   544     return (STATIC_CAST(CSlideshowPlugin*, aPtr)->SettingsChanged());
       
   545     }
       
   546     
       
   547     
       
   548 // --- private functions ---
       
   549 
       
   550     
       
   551 // -----------------------------------------------------------------------------
       
   552 // CSlideshowPlugin::InitializeCenRepL
       
   553 // Connect Central Repository sessions
       
   554 // -----------------------------------------------------------------------------
       
   555 void CSlideshowPlugin::InitializeCenRepL()
       
   556     {
       
   557     
       
   558     TRAPD(err, iSlideshowRepository = CRepository::NewL(KCRUidThemes));
       
   559     User::LeaveIfError(err);
       
   560     }
       
   561 
       
   562     
       
   563 // -----------------------------------------------------------------------------
       
   564 // CSlideshowPlugin::NotifyCenrepChangeL
       
   565 // -----------------------------------------------------------------------------
       
   566 //  
       
   567 void CSlideshowPlugin::NotifyCenrepChangeL()
       
   568     { 
       
   569     iSlideshowCRWatcher = CRepositoryWatcher::NewL(
       
   570             KCRUidThemes,
       
   571             TCallBack(HandleRepositoryCallBack, this),
       
   572             iSlideshowRepository);
       
   573     }
       
   574         
       
   575 
       
   576 // -----------------------------------------------------------------------------
       
   577 // CSlideshowPlugin::NotifyCenrepChangeCancel
       
   578 // -----------------------------------------------------------------------------
       
   579 void CSlideshowPlugin::NotifyCenrepChangeCancel()
       
   580     {
       
   581     delete iSlideshowCRWatcher;
       
   582     iSlideshowCRWatcher = NULL;
       
   583     }
       
   584 
       
   585 
       
   586 // -----------------------------------------------------------------------------
       
   587 // CSlideshowPlugin::UnInitializeCenRep
       
   588 // -----------------------------------------------------------------------------
       
   589 void CSlideshowPlugin::UnInitializeCenRep()
       
   590     {       
       
   591     if (iSlideshowRepository)
       
   592         {
       
   593         delete iSlideshowRepository;
       
   594         iSlideshowRepository = NULL;  
       
   595         }
       
   596     }
       
   597 
       
   598 
       
   599 // Starts the saver when screensaver starts
       
   600 void CSlideshowPlugin::StartPlugin()
       
   601     {
       
   602     // Make sure we start from a clean slate
       
   603     iDrawCount = -1;
       
   604     
       
   605     // (Re-)load slideset. When done on every start this avoids
       
   606     // having to keep track of gallery changes and images on MMC.
       
   607     TRAP_IGNORE(LoadSlidesL());
       
   608 
       
   609     if (iModel->NumberOfSlides() == 0)
       
   610         {
       
   611         iHost->RevertToDefaultSaver();
       
   612         return;
       
   613         }
       
   614 
       
   615     iModel->PrepareNextSlide(
       
   616         (iSettings->iSlideshowType == KSlideshowTypeRandom),
       
   617         iDi.iRect.Size());
       
   618 
       
   619     if (iHost)
       
   620         {
       
   621         // Start animating as per settings
       
   622         iHost->RequestTimeout(iSettings->iSlideshowTime);
       
   623         Lights(iSettings->iLightsTime);
       
   624         // let the draw method be called every 1 second till first image is available
       
   625         iHost->SetRefreshTimerValue(KRefreshInterval * KSecsToMicros);
       
   626         SetDisplayMode();
       
   627         }
       
   628     }
       
   629 
       
   630 
       
   631 // Stops the plugin
       
   632 void CSlideshowPlugin::StopPlugin()
       
   633     {
       
   634     // Let go of pre-fetched images
       
   635     iModel->ReleaseNextSlide();
       
   636     iTimerUpdated = EFalse;
       
   637     }
       
   638     
       
   639 
       
   640 // Draws centered slides
       
   641 void CSlideshowPlugin::DrawCentered(CWindowGc& aGc, CSlideshowSlide* aSlide)
       
   642     {
       
   643     CFbsBitmap* bitmap = aSlide->Image();
       
   644     
       
   645     if (!bitmap)
       
   646         return;
       
   647     
       
   648     // Center the bitmap horizontally and vertically (crop off excess)    
       
   649     TPoint pos;
       
   650     TRect rectToDraw;
       
   651     TSize sizeBmp = bitmap->SizeInPixels();
       
   652     TInt screenWidth = iDi.iRect.Width();
       
   653     TInt screenHeight = iDi.iRect.Height();
       
   654     
       
   655     // Horizontally
       
   656     if (sizeBmp.iWidth <= screenWidth)
       
   657         {
       
   658         // Width fits on screen - center xpos
       
   659         pos.iX = (screenWidth - sizeBmp.iWidth) / 2;
       
   660         
       
   661         // Whole width of bmp can be drawn
       
   662         rectToDraw.SetWidth(sizeBmp.iWidth);
       
   663         }
       
   664     else
       
   665         {
       
   666         // Bmp wider than screen - xpos top left
       
   667         pos.iX = 0;
       
   668         
       
   669         // Adjust draw rect position and width
       
   670         rectToDraw.iTl.iX = (sizeBmp.iWidth - screenWidth) / 2;
       
   671         rectToDraw.SetWidth(screenWidth);
       
   672         }
       
   673     
       
   674     // Vertically
       
   675     if (sizeBmp.iHeight <= screenHeight)
       
   676         {
       
   677         // Height fits on screen - center ypos
       
   678         pos.iY = (screenHeight - sizeBmp.iHeight) / 2;
       
   679         
       
   680         // Whole height of bmp can be drawn
       
   681         rectToDraw.SetHeight(sizeBmp.iHeight);
       
   682         }
       
   683     else
       
   684         {
       
   685         // Bmp higher than screen - ypos top left
       
   686         pos.iY = 0;
       
   687         
       
   688         // Adjust draw rect position and height
       
   689         rectToDraw.iTl.iY = (sizeBmp.iHeight - screenHeight) / 2;
       
   690         rectToDraw.SetHeight(screenHeight);
       
   691         }
       
   692     
       
   693     // Do the drawing
       
   694     aGc.BitBlt(pos, bitmap, rectToDraw);
       
   695     }
       
   696 
       
   697 
       
   698 // Loads the slides into the model
       
   699 void CSlideshowPlugin::LoadSlidesL()
       
   700     {
       
   701     SSPLOGGER_ENTERFN("LoadSlidesL()");
       
   702     iIsLoadFinished = EFalse;
       
   703     // Based on settings, load predefined set or random slides
       
   704     if (iSettings->iSlideshowType == KSlideshowTypeRandom)
       
   705         {
       
   706         LoadRandomSlidesL();
       
   707         }
       
   708     else
       
   709         {
       
   710         LoadSlideSetL();
       
   711         }
       
   712     iIsLoadFinished = ETrue;
       
   713     SSPLOGGER_LEAVEFN("LoadSlidesL()");
       
   714     }
       
   715     
       
   716 
       
   717 // Load filenames from settings file stored in skin server directory
       
   718 void CSlideshowPlugin::LoadSlideSetL()
       
   719     {
       
   720     SSPLOGGER_ENTERFN("LoadSlideSetL()");
       
   721     
       
   722     // Start by getting rid of possibly loaded slides
       
   723     iModel->DeleteAll();
       
   724 
       
   725     // Connect to skin server.
       
   726     RAknsSrvSession skinSrv;
       
   727     TInt error = skinSrv.Connect();
       
   728     User::LeaveIfError(error);
       
   729     CleanupClosePushL(skinSrv);
       
   730 
       
   731     // Open images file
       
   732     TInt fileSrvHandle;
       
   733     TInt fileHandle;
       
   734     fileSrvHandle = skinSrv.OpenImageInifile(EAknsSrvInifileSSSS, fileHandle);
       
   735 
       
   736     // Adopt the handle to our process
       
   737     RFile imgFile;
       
   738     error = imgFile.AdoptFromServer(fileSrvHandle, fileHandle);
       
   739     User::LeaveIfError(error);
       
   740     CleanupClosePushL(imgFile);
       
   741     
       
   742     // Read image filenames from file (as text)
       
   743     TFileText textFile;
       
   744     textFile.Set(imgFile);
       
   745     textFile.Seek(ESeekStart);
       
   746 
       
   747     // Read until EOF
       
   748     TInt count = 0;
       
   749     TFileName fileName;
       
   750     while (textFile.Read(fileName) == KErrNone)
       
   751         {
       
   752         // Check that the file exists. If not, it is still OK, if it is
       
   753         // on the memory card - it may show up later. Omit files from other
       
   754         // drives that do not exist at the time of loading
       
   755         TBool exists = BaflUtils::FileExists(iEikEnv->FsSession(), fileName);
       
   756         TBool isOnMC = SlideshowUtil::IsOnMC(fileName);
       
   757         
       
   758         if (!exists) //  && (!isOnMC))
       
   759             {
       
   760             // Do not add nonexisting files // from other than memory card
       
   761             continue;
       
   762             }
       
   763 #if 0
       
   764         // Check that the file's DRM rights allow it to be displayed (if not
       
   765         // missing because not on MMC
       
   766         if (!SlideshowUtil::DRMCheck(fileName))
       
   767             {
       
   768             // No point in adding files that cannot be displayed anyway
       
   769             continue;
       
   770             }
       
   771 #endif        
       
   772         // Create a slide with the filename and store it in the model
       
   773         CSlideshowSlide* pSlide = CSlideshowSlide::NewL(fileName, isOnMC);
       
   774         CleanupStack::PushL(pSlide);
       
   775         iModel->AppendSlideL(pSlide);
       
   776         CleanupStack::Pop(pSlide);
       
   777 
       
   778         SSPLOGGER_WRITEF(_L("SSP: Slide %d added, file: %S"), count, &fileName);
       
   779         count++;
       
   780         }
       
   781 
       
   782     CleanupStack::PopAndDestroy(2); // imgFile, skinSrv
       
   783     
       
   784     SSPLOGGER_LEAVEFN("LoadSlideSetL()");    
       
   785     }
       
   786 
       
   787 
       
   788 // Load filenames from CLF
       
   789 void CSlideshowPlugin::LoadRandomSlidesL()
       
   790     {
       
   791     SSPLOGGER_ENTERFN("LoadRandomSlidesL()");
       
   792     // Start by getting rid of possibly loaded slides
       
   793     iModel->DeleteAll();
       
   794     // connect to MDS, load data or not
       
   795     ConnectToMDSSessionL();
       
   796 
       
   797     SSPLOGGER_LEAVEFN("LoadRandomSlidesL()");
       
   798     }
       
   799 
       
   800 
       
   801 // Requests display mode from host
       
   802 void CSlideshowPlugin::SetDisplayMode()
       
   803     {
       
   804     if (!iHost)
       
   805         {
       
   806         return;
       
   807         }
       
   808     
       
   809     // Exit partial mode
       
   810     iHost->ExitPartialMode();
       
   811     }
       
   812 
       
   813 
       
   814 // Requests lights
       
   815 void CSlideshowPlugin::Lights(TInt aSecs)
       
   816     {
       
   817     // Request nothing, if no time set (don't force lights off)
       
   818     if ((iHost) && (aSecs > 0))
       
   819         {
       
   820         SSPLOGGER_WRITEF(_L("SSP: Request lights for %d secs"), aSecs);
       
   821         iHost->RequestLights(aSecs);
       
   822         }
       
   823     }
       
   824 
       
   825 
       
   826 // Configure the plugin
       
   827 void CSlideshowPlugin::ConfigureL(TAny* /* aParam */)
       
   828     {
       
   829     // Look for screensaver slideset setting interface
       
   830     RImplInfoPtrArray array;            
       
   831     const TUid slidesetInterface = { 0x102823AD };
       
   832     const TUid slidesetImplUidScreensaver = { 0x102823AF };
       
   833     const TEComResolverParams emptyParams;
       
   834     
       
   835     REComSession::ListImplementationsL(
       
   836         slidesetInterface,
       
   837         emptyParams,
       
   838         KRomOnlyResolverUid,
       
   839         array);
       
   840     CleanupResetAndDestroyPushL(array);
       
   841 
       
   842     // Loop through implementations and look for screensaver
       
   843     // slideset implementation (this is not really completely
       
   844     // necessary, because we already know the implementation UID,
       
   845     // but we want to be sure it's there, and on ROM only
       
   846     TInt nCount = array.Count();
       
   847     TUid implUid;
       
   848 
       
   849     for (TInt i = 0; i < nCount; i++)
       
   850         {
       
   851         CImplementationInformation* info = array[i];
       
   852         
       
   853         if (info->ImplementationUid() == slidesetImplUidScreensaver)
       
   854             {
       
   855             // Found
       
   856             implUid = info->ImplementationUid();
       
   857             break;
       
   858             }
       
   859         }
       
   860 
       
   861     // Did we get it?
       
   862     if (implUid != slidesetImplUidScreensaver)
       
   863         {
       
   864         User::Leave(KErrNotFound);
       
   865         }
       
   866     
       
   867     // Instantiate the interface
       
   868     CPslnSlidesetDialogInterface* plugin = 
       
   869         CPslnSlidesetDialogInterface::NewL(implUid);
       
   870 
       
   871     TInt dlgRet = KErrNone;
       
   872     if (plugin)
       
   873         {
       
   874         dlgRet = plugin->ExecuteDialogLD();
       
   875         }
       
   876 
       
   877     // Cleanup
       
   878     array.ResetAndDestroy();
       
   879     CleanupStack::PopAndDestroy(); // array
       
   880 
       
   881     User::LeaveIfError(dlgRet);
       
   882     }
       
   883 
       
   884     
       
   885 // Updates the saved information about display
       
   886 void CSlideshowPlugin::UpdateDisplayInfo()
       
   887     {
       
   888     iDi.iSize = sizeof(TScreensaverDisplayInfo);
       
   889     iHost->DisplayInfo(&iDi);
       
   890     }
       
   891 
       
   892     
       
   893 // Flushes pending draws
       
   894 void CSlideshowPlugin::FlushDrawBuffer()
       
   895     {
       
   896     iEikEnv->WsSession().Flush();
       
   897     }
       
   898 
       
   899 
       
   900 // -----------------------------------------------------------------------------
       
   901 // CSlideshowPlugin::SettingsChanged
       
   902 // Called when Themes reports a settings change through P & S
       
   903 // -----------------------------------------------------------------------------
       
   904 TInt CSlideshowPlugin::SettingsChanged()
       
   905     {
       
   906     // If current key value is nonzero, it means the settings
       
   907     // have just changed, otherwise, no action necessary
       
   908     TInt value = 0;
       
   909     iSettingsChangedProperty.Get(value);
       
   910 
       
   911     if (value == EScreenSaverPluginSettingsChanged)
       
   912         {
       
   913 #if 0
       
   914         // Slide file has changed - re-load images
       
   915         TRAP_IGNORE(LoadSlidesL());
       
   916         // Pre-fetch the fist image to be drawn
       
   917         iModel->PrepareNextSlide(
       
   918             (iSettings->iSlideshowType == KSlideshowTypeRandom),
       
   919             iDi.iRect.Size());
       
   920 #endif
       
   921         iSettingsChangedProperty.Set(EScreenSaverPluginSettingsNoChange);
       
   922         }
       
   923 
       
   924     return KErrNone;
       
   925     }
       
   926 
       
   927 
       
   928 // -----------------------------------------------------------------------------
       
   929 // CSlideshowPlugin::ConnectToMDSSessionL
       
   930 // Connects to MDS Listing Framework. Can be called many times,
       
   931 // connects only once
       
   932 // -----------------------------------------------------------------------------
       
   933 void CSlideshowPlugin::ConnectToMDSSessionL()
       
   934     {
       
   935     if (!iMdESession)
       
   936         {
       
   937         iMdESession = CMdESession::NewL( *this );
       
   938         // Wait for query of MDS to complete before continuing
       
   939         WaitForMDS();
       
   940         }
       
   941     }
       
   942 
       
   943 // -----------------------------------------------------------------------------
       
   944 // CSlideshowPlugin::WaitForMDS
       
   945 // Begins wait for MDS session connected 
       
   946 // -----------------------------------------------------------------------------
       
   947 void CSlideshowPlugin::WaitForMDS()
       
   948     {
       
   949     if (iWaitActive)
       
   950         {
       
   951         return;
       
   952         }
       
   953     else
       
   954         {
       
   955         iWaitActive = ETrue;
       
   956         iWaiter.Start();
       
   957         }
       
   958     }
       
   959 
       
   960 // -----------------------------------------------------------------------------
       
   961 // CSlideshowPlugin::WaitForMDS
       
   962 // Ends wait for MDS
       
   963 // -----------------------------------------------------------------------------
       
   964 void CSlideshowPlugin::EndWaitForMDS()
       
   965     {
       
   966     if (!iWaitActive)
       
   967         {
       
   968         return;
       
   969         }
       
   970     else
       
   971         {
       
   972         iWaiter.AsyncStop();
       
   973         iWaitActive = EFalse;
       
   974         }
       
   975     }
       
   976 
       
   977 // -----------------------------------------------------------------------------
       
   978 // CSlideshowPlugin::HandleSessionOpened
       
   979 // Session is open successfully, then start a query for images
       
   980 // -----------------------------------------------------------------------------
       
   981 void CSlideshowPlugin::HandleSessionOpened(CMdESession& /*aSession*/, TInt aError)
       
   982     {
       
   983     if ( KErrNone != aError )
       
   984         {
       
   985         // Error occurred when opening session. iMdeSession must be deleted and new
       
   986         // session opened if we wish to use MdE.
       
   987         delete iMdESession;
       
   988         iMdESession = NULL;
       
   989         return;
       
   990         }
       
   991     // The session was opened successfully.
       
   992     TRAP(aError, OpenQueryL() );
       
   993     }
       
   994 
       
   995 // -----------------------------------------------------------------------------
       
   996 // CSlideshowPlugin::OpenQueryL
       
   997 // -----------------------------------------------------------------------------
       
   998 void CSlideshowPlugin::OpenQueryL()
       
   999     {
       
  1000     CMdENamespaceDef& defaultNamespaceDef = iMdESession->GetDefaultNamespaceDefL();
       
  1001     CMdEObjectDef& imageObjDef = defaultNamespaceDef.GetObjectDefL( MdeConstants::Image::KImageObject );
       
  1002 
       
  1003     // query objects with object definition "Image"
       
  1004     CMdEObjectQuery* query = iMdESession->NewObjectQueryL( defaultNamespaceDef, imageObjDef, this );
       
  1005 
       
  1006     query->FindL( KDefaultRandomLoadingNumber );
       
  1007     }
       
  1008 
       
  1009 // -----------------------------------------------------------------------------
       
  1010 // CSlideshowPlugin::HandleSessionError
       
  1011 // error happened when open the session, close session and end the waiting
       
  1012 // -----------------------------------------------------------------------------
       
  1013 void CSlideshowPlugin::HandleSessionError(CMdESession& /*aSession*/, TInt /*aError*/)
       
  1014     {
       
  1015     if ( iMdESession )
       
  1016         {
       
  1017         delete iMdESession;
       
  1018         iMdESession = NULL;
       
  1019         }
       
  1020     // error happened when open the session, so end the waiting for MDS session.
       
  1021     EndWaitForMDS();
       
  1022     }
       
  1023 
       
  1024 // -----------------------------------------------------------------------------
       
  1025 // CSlideshowPlugin::HandleQueryCompleted
       
  1026 // query completed, load the images
       
  1027 // -----------------------------------------------------------------------------
       
  1028 void CSlideshowPlugin::HandleQueryCompleted(CMdEQuery& aQuery, TInt aError)
       
  1029     {
       
  1030     if ( aError == KErrNone )
       
  1031         {
       
  1032         LoadImagesToModel( aQuery );
       
  1033         }
       
  1034     EndWaitForMDS();
       
  1035     }
       
  1036 
       
  1037 // -----------------------------------------------------------------------------
       
  1038 // CSlideshowPlugin::HandleQueryCompleted
       
  1039 // part of query completed, load the completed images
       
  1040 // -----------------------------------------------------------------------------
       
  1041 void CSlideshowPlugin::HandleQueryNewResults( CMdEQuery& aQuery,
       
  1042                                               TInt aFirstNewItemIndex,
       
  1043                                               TInt aNewItemCount)
       
  1044     {
       
  1045     LoadImagesToModel( aQuery, aFirstNewItemIndex, aNewItemCount );
       
  1046     EndWaitForMDS();
       
  1047     }
       
  1048 
       
  1049 // -----------------------------------------------------------------------------
       
  1050 // CSlideshowPlugin::LoadImagesToModel
       
  1051 // load the images when query is successfully
       
  1052 // -----------------------------------------------------------------------------
       
  1053 void CSlideshowPlugin::LoadImagesToModel(const CMdEQuery& aQuery,
       
  1054                                          TInt aFirstNewItemIndex,
       
  1055                                          TInt aNewItemCount)
       
  1056     {
       
  1057     CMdEObjectQuery& query= ( CMdEObjectQuery& ) aQuery;
       
  1058     // query is completed
       
  1059     if( aQuery.Count() > 0 )
       
  1060         {
       
  1061         // some items were found!
       
  1062         // Loop through, add filenames
       
  1063         TInt startIndex = aFirstNewItemIndex;
       
  1064         TInt nItem = aNewItemCount;
       
  1065         if ( aFirstNewItemIndex == 0 && aFirstNewItemIndex == aNewItemCount )
       
  1066             {
       
  1067             startIndex = 0;
       
  1068             nItem = aQuery.Count();
       
  1069             }
       
  1070         TInt count = 0;
       
  1071         for ( ; startIndex < nItem; startIndex++)
       
  1072             {
       
  1073             // TFileName fileName;
       
  1074             TBufC<256> fileName;
       
  1075 
       
  1076             const CMdEObject& obj = query.Result(startIndex);
       
  1077             fileName = obj.Uri();
       
  1078 
       
  1079             // Check that the file exists. If not, it is still OK, if it is
       
  1080             // on the memory card - it may show up later. Omit files from other
       
  1081             // drives that do not exist at the time of loading
       
  1082             // TODO: Can be removed, as slides are loaded on every start
       
  1083             TBool exists = BaflUtils::FileExists(iEikEnv->FsSession(), fileName);
       
  1084             TBool isOnMC = SlideshowUtil::IsOnMC(fileName);
       
  1085 
       
  1086             if (!exists)  // && (!isOnMC))
       
  1087                 {
       
  1088                 // Do not add nonexisting files from other than memory card
       
  1089                 continue;
       
  1090                 }
       
  1091 #if 0
       
  1092             // Check that the file's DRM rights allow it to be displayed (if not
       
  1093             // missing because not on MMC
       
  1094             if (!SlideshowUtil::DRMCheck(fileName))
       
  1095                 {
       
  1096                 // No point in adding files that cannot be displayed anyway
       
  1097                 continue;
       
  1098                 }
       
  1099 #endif        
       
  1100             // Create a slide with the filename and store it in the model
       
  1101             TRAPD(err, AppendSlideToModelL( fileName, isOnMC ) );
       
  1102             if ( KErrNone != err )
       
  1103                 {
       
  1104                 // appending error, go on to append next slide
       
  1105                 continue;
       
  1106                 }
       
  1107             SSPLOGGER_WRITEF(_L("SSP: Slide %d added, file: %S"), count, &fileName);
       
  1108             count++;
       
  1109             }
       
  1110         }
       
  1111     }
       
  1112 
       
  1113 // -----------------------------------------------------------------------------
       
  1114 // CSlideshowPlugin::AppendSlideToModelL
       
  1115 // Add slide to model
       
  1116 // -----------------------------------------------------------------------------
       
  1117 void CSlideshowPlugin::AppendSlideToModelL(TDesC& aFileName, TBool aIsOnMC)
       
  1118     {
       
  1119     CSlideshowSlide* pSlide = CSlideshowSlide::NewL(aFileName, aIsOnMC);
       
  1120     CleanupStack::PushL(pSlide);
       
  1121     iModel->AppendSlideL(pSlide);
       
  1122     CleanupStack::Pop(pSlide);
       
  1123     }
       
  1124 // End Of file.