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