psln/pslnslidesetdialog/src/pslnslidesetmodel.cpp
changeset 37 89c890c70182
parent 34 6b5204869ed5
child 45 667edd0b8678
equal deleted inserted replaced
34:6b5204869ed5 37:89c890c70182
     1 /*
       
     2 * Copyright (c) 2006-2007 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:  Model class for slide set dialog.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // Central repository.
       
    20 #include <centralrepository.h>
       
    21 #include <pslninternalcrkeys.h>
       
    22 #include "PslnVariationPrivateCRKeys.h"
       
    23 #include "PslnVariant.hrh"
       
    24 #include <AknSkinsInternalCRKeys.h>
       
    25 
       
    26 // Notes & Dialogs
       
    27 #include <AknGlobalNote.h>
       
    28 #include <aknnotewrappers.h>
       
    29 
       
    30 // Skin server.
       
    31 #include <AknsWallpaperUtils.h>
       
    32 #include <AknsSrvClient.h>
       
    33 
       
    34 // Generic.
       
    35 #include <bautils.h>
       
    36 
       
    37 // Psln Slide set specific.
       
    38 #include <pslnslidesetdialoginterface.h>
       
    39 #include "pslnslidesetmodel.h"
       
    40 #include "pslnslidesetconst.hrh"
       
    41 #include "pslnslidesetconst.h"
       
    42 #include <pslnslidesetdialogrsc.rsg>
       
    43 
       
    44 // Slide set duration default value: 5 seconds.
       
    45 const TInt KPslnSlideSetDefaultDuration = 5;
       
    46 // Slide set backlight default value: off.
       
    47 const TInt KPslnSlideSetBacklightOff = 0;
       
    48 // Slide set interval default value: 10min.
       
    49 const TInt KPslnSlideSetDefaultInterval = EPslnSlideSetInterval10Min;
       
    50 // Slide set minimum duration is 1 second.
       
    51 const TInt KPslnSlideSetMinDuration = 1;
       
    52 // Slide set maximum duration is 60 seconds.
       
    53 const TInt KPslnSlideSetMaxDuration = 60;
       
    54 // Slide set maximum backlight period is 30secs.
       
    55 const TInt KPslnSlideSetMaxBacklight = 30;
       
    56 // Slide set minimum interval period is 1 min.
       
    57 const TInt KPslnSlideSetMinInterval = EPslnSlideSetInterval1Min;
       
    58 // Slide set maximum interval period is 1 day.
       
    59 const TInt KPslnSlideSetMaxInterval = EPslnSlideSetInterval1Day;
       
    60 
       
    61 
       
    62 // ======== MEMBER FUNCTIONS ========
       
    63 // ---------------------------------------------------------------------------
       
    64 // C++ constructor can NOT contain any code, that might leave.
       
    65 // ---------------------------------------------------------------------------
       
    66 //
       
    67 CPslnSlideSetModel::CPslnSlideSetModel() : iImageListChanged ( EFalse )
       
    68     {
       
    69     }
       
    70 
       
    71 // ---------------------------------------------------------------------------
       
    72 // Symbian 2nd phase constructor can leave.
       
    73 // ---------------------------------------------------------------------------
       
    74 //
       
    75 void CPslnSlideSetModel::ConstructL()
       
    76     {
       
    77     iRepository = CRepository::NewL( KCRUidThemes );
       
    78     iCoeEnv = CCoeEnv::Static();
       
    79     }
       
    80 
       
    81 // ---------------------------------------------------------------------------
       
    82 // Two-phased constructor.
       
    83 // ---------------------------------------------------------------------------
       
    84 //
       
    85 CPslnSlideSetModel* CPslnSlideSetModel::NewL()
       
    86     {
       
    87     CPslnSlideSetModel* self = CPslnSlideSetModel::NewLC();
       
    88     CleanupStack::Pop( self );
       
    89     return self;
       
    90     }
       
    91 
       
    92 // ---------------------------------------------------------------------------
       
    93 // Two-phased constructor. Same as above, but leaves object to the 
       
    94 // cleanup stack.
       
    95 // ---------------------------------------------------------------------------
       
    96 //
       
    97 CPslnSlideSetModel* CPslnSlideSetModel::NewLC()
       
    98     {
       
    99     CPslnSlideSetModel* self = new( ELeave ) CPslnSlideSetModel;
       
   100     CleanupStack::PushL( self );
       
   101     self->ConstructL();
       
   102     return self;
       
   103     }
       
   104 
       
   105 // ---------------------------------------------------------------------------
       
   106 // Destructor.
       
   107 // ---------------------------------------------------------------------------
       
   108 //
       
   109 CPslnSlideSetModel::~CPslnSlideSetModel()
       
   110     {
       
   111     delete iRepository;
       
   112     }
       
   113 
       
   114 // ---------------------------------------------------------------------------
       
   115 // Returns current selection for slide set type: random / image set.
       
   116 // ---------------------------------------------------------------------------
       
   117 //
       
   118 TInt CPslnSlideSetModel::GetSlideSetType( const TInt aSlideSetType ) const
       
   119     {
       
   120     TInt imageSelection = KErrNotFound;
       
   121 
       
   122     TInt error = KErrNone;
       
   123     if ( aSlideSetType == EPslnScreensaverDialog )
       
   124         {
       
   125         error = iRepository->Get( 
       
   126             KThemesScreenSaverSlideSetType, 
       
   127             imageSelection );
       
   128         }
       
   129     else
       
   130         {
       
   131         error = iRepository->Get( 
       
   132             KThemesWallpaperSlideSetType, 
       
   133             imageSelection );
       
   134         }
       
   135     // In case of error, or invalid value, just return default value.
       
   136     if ( error != KErrNone ||
       
   137          ( imageSelection != KPslnSlideSetRandomIndex && 
       
   138            imageSelection != KPslnSlideSetImageSelImageSet ) )
       
   139         {
       
   140         if ( aSlideSetType == EPslnScreensaverDialog )
       
   141             {
       
   142             return KPslnSlideSetRandomIndex;
       
   143             }
       
   144         else
       
   145             {
       
   146             return KPslnSlideSetImageSelImageSet;
       
   147             }
       
   148         }
       
   149     return imageSelection;
       
   150     }
       
   151 
       
   152 // ---------------------------------------------------------------------------
       
   153 // Sets slide set type (random/image set).
       
   154 // ---------------------------------------------------------------------------
       
   155 //
       
   156 TInt CPslnSlideSetModel::SetSlideSetType( 
       
   157         const TInt aSlideSetType, const TInt aValue )
       
   158     {
       
   159     TInt error = KErrNone;
       
   160 
       
   161     TInt prevValue = KErrNone; 
       
   162     if ( aSlideSetType == EPslnScreensaverDialog )
       
   163         {
       
   164         error = iRepository->Get( KThemesScreenSaverSlideSetType, prevValue );
       
   165         }
       
   166     else
       
   167         {
       
   168         error = iRepository->Get( KThemesWallpaperSlideSetType, prevValue );            
       
   169         }
       
   170     
       
   171     // Validate that value is valid and changed.
       
   172     if ( ( aValue == KPslnSlideSetImageSelImageSet || 
       
   173            aValue == KPslnSlideSetRandomIndex ) &&
       
   174          ( prevValue != aValue ) )
       
   175         {
       
   176         // Store selection.
       
   177         if ( aSlideSetType == EPslnScreensaverDialog )
       
   178             {
       
   179             error = iRepository->Set( KThemesScreenSaverSlideSetType, aValue );
       
   180             }
       
   181         else
       
   182             {
       
   183             error = iRepository->Set( KThemesWallpaperSlideSetType, aValue );
       
   184             }
       
   185         }
       
   186     else
       
   187         {
       
   188         error = KErrArgument;
       
   189         }
       
   190     return error;
       
   191     }
       
   192 
       
   193 // ---------------------------------------------------------------------------
       
   194 // Returns current selection for screensaver slide set duration.
       
   195 // ---------------------------------------------------------------------------
       
   196 //
       
   197 TInt CPslnSlideSetModel::GetSlideSetDuration() const
       
   198     {
       
   199     TInt slideSetDuration = KPslnSlideSetDefaultDuration;
       
   200     TInt error = KErrNone;
       
   201     error = iRepository->Get( 
       
   202         KThemesScreenSaverSlideSetDuration, 
       
   203         slideSetDuration );
       
   204     if ( error != KErrNone )
       
   205         {
       
   206         // return default value in case of error.
       
   207         return KPslnSlideSetDefaultDuration;
       
   208         }
       
   209     return slideSetDuration;
       
   210     }
       
   211 
       
   212 // ---------------------------------------------------------------------------
       
   213 // Sets screensaver slide set duration.
       
   214 // ---------------------------------------------------------------------------
       
   215 //
       
   216 TInt CPslnSlideSetModel::SetSlideSetDuration( const TInt aValue )
       
   217     {
       
   218     TInt error = KErrNone;
       
   219     TInt prevValue = KErrNone; 
       
   220     error = iRepository->Get( KThemesScreenSaverSlideSetDuration, prevValue );
       
   221 
       
   222     // Validate value and check that new value is different from previous
       
   223     // value.
       
   224     if ( ( aValue >= KPslnSlideSetMinDuration && 
       
   225            aValue <= KPslnSlideSetMaxDuration ) &&
       
   226          ( prevValue != aValue ) )
       
   227         {
       
   228         error = iRepository->Set( 
       
   229             KThemesScreenSaverSlideSetDuration, 
       
   230             aValue );
       
   231         }
       
   232     else
       
   233         {
       
   234         error = KErrArgument;
       
   235         }
       
   236     return error;
       
   237     }
       
   238 
       
   239 // ---------------------------------------------------------------------------
       
   240 // Returns current selection for screensaver slide set backlight period.
       
   241 // ---------------------------------------------------------------------------
       
   242 //
       
   243 TInt CPslnSlideSetModel::GetSlideSetBacklight() const
       
   244     {
       
   245     TInt slideSetBacklight = KPslnSlideSetBacklightOff;
       
   246     TInt error = KErrNone;
       
   247     error = iRepository->Get( 
       
   248         KThemesScreenSaverSlideSetBacklight, 
       
   249         slideSetBacklight );
       
   250     if ( error != KErrNone )
       
   251         {
       
   252         // return default value in case of error.
       
   253         return KPslnSlideSetBacklightOff;
       
   254         }
       
   255     return slideSetBacklight;
       
   256     }
       
   257 
       
   258 // ---------------------------------------------------------------------------
       
   259 // Sets screensaver slide set backlight period.
       
   260 // ---------------------------------------------------------------------------
       
   261 //
       
   262 TInt CPslnSlideSetModel::SetSlideSetBacklight( const TInt aValue )
       
   263     {
       
   264     TInt error = KErrNone;
       
   265     TInt prevValue = KErrNone; 
       
   266     error = iRepository->Get( KThemesScreenSaverSlideSetBacklight, prevValue );
       
   267 
       
   268     if ( ( aValue >= KPslnSlideSetBacklightOff ||
       
   269            aValue <= KPslnSlideSetMaxBacklight ) &&
       
   270          ( prevValue != aValue ) )
       
   271         {
       
   272         error = iRepository->Set( 
       
   273             KThemesScreenSaverSlideSetBacklight, 
       
   274             aValue );
       
   275         }
       
   276     else
       
   277         {
       
   278         error = KErrArgument;
       
   279         }
       
   280     return error;
       
   281     }
       
   282 
       
   283 // ---------------------------------------------------------------------------
       
   284 // Returns current selection for wallpaper slide set image change interval.
       
   285 // ---------------------------------------------------------------------------
       
   286 //
       
   287 TInt CPslnSlideSetModel::GetSlideSetInterval() const
       
   288     {
       
   289     TInt slideSetInterval = KPslnSlideSetDefaultInterval;
       
   290     TInt error = KErrNone;
       
   291     error = iRepository->Get( 
       
   292         KThemesWallpaperSlideSetInterval, 
       
   293         slideSetInterval );
       
   294     if ( error != KErrNone )
       
   295         {
       
   296         // return default value in case of error.
       
   297         return KPslnSlideSetDefaultInterval;
       
   298         }
       
   299     return slideSetInterval;
       
   300     }
       
   301 
       
   302 // ---------------------------------------------------------------------------
       
   303 // Sets wallpaper slide set image change period.
       
   304 // ---------------------------------------------------------------------------
       
   305 //
       
   306 TInt CPslnSlideSetModel::SetSlideSetInterval( const TInt aValue )
       
   307     {
       
   308     TInt error = KErrNone;
       
   309     TInt prevValue = KErrNone; 
       
   310     error = iRepository->Get( KThemesWallpaperSlideSetInterval, prevValue );
       
   311 
       
   312     if ( ( aValue >= KPslnSlideSetMinInterval ||
       
   313            aValue <= KPslnSlideSetMaxInterval ) && 
       
   314          ( prevValue != aValue ) )
       
   315         {
       
   316         error = iRepository->Set( KThemesWallpaperSlideSetInterval, aValue );
       
   317         }
       
   318     else
       
   319         {
       
   320         error = KErrArgument;
       
   321         }
       
   322     return error;
       
   323     }
       
   324 
       
   325 // ---------------------------------------------------------------------------
       
   326 // Stores list of image filenames to be used as slide set image set.
       
   327 // ---------------------------------------------------------------------------
       
   328 //
       
   329 void CPslnSlideSetModel::SetImageListL( const TInt aSlideSetType, 
       
   330     CDesCArray& aSelectedFiles, const TInt aLaunchMode )
       
   331     {
       
   332     if ( aSlideSetType == EPslnWallpaperDialog )
       
   333         {
       
   334         if ( aLaunchMode == CPslnSlidesetDialogInterface::EPslnNormal )
       
   335             {
       
   336             TInt retVal = 
       
   337                 AknsWallpaperUtils::SetSlidesetWallpaper( aSelectedFiles, NULL );
       
   338             if ( retVal != KErrNone )
       
   339                 {
       
   340                 // Show information note about image
       
   341                 HBufC* prompt = iCoeEnv->AllocReadResourceLC( 
       
   342                         R_PSLN_SLIDE_SET_IMAGE_CORRUPTED );
       
   343                 CAknInformationNote* note = 
       
   344                     new (ELeave) CAknInformationNote( ETrue );
       
   345                 note->ExecuteLD( *prompt );
       
   346                 CleanupStack::PopAndDestroy( prompt );
       
   347                 }
       
   348             }
       
   349         // Write file names to file, if configuring only.
       
   350         else
       
   351             {
       
   352             TInt fileType = EAknsSrvInifileSSWP;
       
   353             StoreImageListToFileL( fileType, aSelectedFiles );
       
   354             }
       
   355         }
       
   356     else
       
   357         {
       
   358         TInt fileType = EAknsSrvInifileSSSS;
       
   359         StoreImageListToFileL( fileType, aSelectedFiles );
       
   360         }
       
   361     iImageListChanged = ETrue;
       
   362     }
       
   363 
       
   364 // ---------------------------------------------------------------------------
       
   365 // Check if the image file exists.
       
   366 // ---------------------------------------------------------------------------
       
   367 //
       
   368 TBool CPslnSlideSetModel::SlideSetImageFileExistsL( 
       
   369     const TInt aSlideSetType ) const
       
   370     {    
       
   371     // Get file from server.
       
   372     RFile imgFile;
       
   373     OpenImageFileL( imgFile, aSlideSetType );
       
   374     CleanupClosePushL( imgFile );
       
   375 
       
   376     // Check if the file exists.
       
   377     TInt fileSize = 0;
       
   378     TBool retValue = EFalse;
       
   379     User::LeaveIfError( imgFile.Size( fileSize ) );
       
   380 
       
   381     // If file has size, then it exists.
       
   382     if ( fileSize > 0 )
       
   383         {
       
   384         retValue = ETrue;
       
   385         }
       
   386 
       
   387     CleanupStack::PopAndDestroy(); // imgFile
       
   388     return retValue;
       
   389     }
       
   390 
       
   391 // ---------------------------------------------------------------------------
       
   392 // Indicates whether or not setting values have been updated.
       
   393 // ---------------------------------------------------------------------------
       
   394 //
       
   395 TBool CPslnSlideSetModel::HasDialogUpdatedValues() const
       
   396     {
       
   397     return iImageListChanged;
       
   398     }
       
   399 
       
   400 // ---------------------------------------------------------------------------
       
   401 // Counts how many images have been selected.
       
   402 // ---------------------------------------------------------------------------
       
   403 //
       
   404 TInt CPslnSlideSetModel::GetImageCountInSlideSetL( 
       
   405     const TInt aSlideSetType ) const
       
   406     {
       
   407     // Get file from server.
       
   408     RFile imgFile;
       
   409     OpenImageFileL( imgFile, aSlideSetType );
       
   410     CleanupClosePushL( imgFile );
       
   411 
       
   412     TInt fileSize = 0;
       
   413     TInt imageCount = 0;
       
   414     User::LeaveIfError( imgFile.Size( fileSize ) );
       
   415     
       
   416     // If file size is zero => no images.
       
   417     if ( fileSize > 0 )
       
   418         {
       
   419         TFileText textFile;
       
   420         textFile.Set( imgFile );
       
   421         textFile.Seek( ESeekStart );
       
   422         
       
   423         RFs& fs = CCoeEnv::Static()->FsSession();
       
   424         // Count images from file.
       
   425         FOREVER
       
   426             {
       
   427             TFileName filename;
       
   428             if( textFile.Read( filename ) != KErrNone )
       
   429                 {                
       
   430                 break;
       
   431                 }
       
   432             if ( BaflUtils::FileExists(fs, filename) )
       
   433             	{
       
   434             	imageCount++;
       
   435             	}
       
   436             }
       
   437         }
       
   438     CleanupStack::PopAndDestroy(); // imgFile
       
   439     return imageCount;
       
   440     }
       
   441 
       
   442 // ---------------------------------------------------------------------------
       
   443 // Sets feature support - see pslnslidesetconst.h for feature list.
       
   444 // ---------------------------------------------------------------------------
       
   445 //
       
   446 void CPslnSlideSetModel::GetFeatureSupportL( TBitFlags& aFeatureBitFlags ) const
       
   447     {
       
   448     CRepository* featureRep = CRepository::NewLC( KCRUidThemesVariation );
       
   449     TInt value = KErrNone;
       
   450     featureRep->Get( KThemesLocalVariation, value );
       
   451 
       
   452     aFeatureBitFlags.ClearAll();
       
   453 
       
   454     //map value to feature flags
       
   455     if ( value & KPslnRandomSlideSets )
       
   456         {
       
   457         aFeatureBitFlags.Set( EPslnSlideSetFeatureRandom );
       
   458         }
       
   459     if ( value & KPslnRemoveSlideSetTimeout )
       
   460         {
       
   461         aFeatureBitFlags.Set( EPslnSlideSetBacklightRemoved );
       
   462         }
       
   463 
       
   464     CleanupStack::PopAndDestroy( featureRep );
       
   465     }
       
   466 
       
   467 // ---------------------------------------------------------------------------
       
   468 // Stores slide set image file names to file.
       
   469 // ---------------------------------------------------------------------------
       
   470 //
       
   471 void CPslnSlideSetModel::StoreImageListToFileL( 
       
   472     const TInt aSlideSetType, CDesCArray& aSelectedFiles  ) const
       
   473     {
       
   474     // Get file from server.
       
   475     RFile imgFile;
       
   476     OpenImageFileL( imgFile, aSlideSetType );
       
   477     CleanupClosePushL( imgFile );
       
   478     User::LeaveIfError( imgFile.SetSize( 0 ) ); // possibly wipe existing file 
       
   479 
       
   480     // Finally, write image filenames to the file.
       
   481     TFileText textFile;
       
   482     textFile.Set( imgFile );
       
   483     textFile.Seek( ESeekStart );
       
   484 
       
   485     for ( TInt imgCount = 0; imgCount < aSelectedFiles.Count(); imgCount++ )
       
   486         {
       
   487         User::LeaveIfError( 
       
   488             textFile.Write( aSelectedFiles.MdcaPoint( imgCount ) ) );
       
   489         }
       
   490     imgFile.Flush();
       
   491 
       
   492     CleanupStack::PopAndDestroy(); // imgFile
       
   493     }
       
   494 
       
   495 // ---------------------------------------------------------------------------
       
   496 // Open image list file for operations.
       
   497 // ---------------------------------------------------------------------------
       
   498 //
       
   499 void CPslnSlideSetModel::OpenImageFileL( RFile& aImageFile, const TInt aSlideSetType ) const
       
   500     {
       
   501     // First, connect to skin server.
       
   502     RAknsSrvSession skinsrv;
       
   503     User::LeaveIfError( skinsrv.Connect() );
       
   504     CleanupClosePushL( skinsrv );
       
   505 
       
   506     // Then get file handle.
       
   507     TInt fileserverhandle = 0;
       
   508     TInt filehandle = 0;
       
   509     // Validate type and open image file.
       
   510     if ( aSlideSetType == EAknsSrvInifileSSWP )
       
   511         {
       
   512         fileserverhandle = 
       
   513             skinsrv.OpenImageInifile( EAknsSrvInifileSSWP, filehandle );
       
   514         }
       
   515     else
       
   516         {
       
   517         fileserverhandle = 
       
   518             skinsrv.OpenImageInifile( EAknsSrvInifileSSSS, filehandle );
       
   519 
       
   520         }
       
   521     if ( fileserverhandle <= 0 || filehandle == 0 )
       
   522         {
       
   523         User::Leave( fileserverhandle );
       
   524         }
       
   525 
       
   526     // Finally adopt file from server.
       
   527     User::LeaveIfError( aImageFile.AdoptFromServer( fileserverhandle, filehandle ) );
       
   528     CleanupStack::PopAndDestroy(); // skinsrv
       
   529     }
       
   530 
       
   531 // End of file