scrsaver/scrsaverplugins/BmpAnimScrPlugin/src/CBmpAnimSettings.cpp
changeset 14 8a173132b0aa
parent 2 058b1fc1663a
equal deleted inserted replaced
2:058b1fc1663a 14:8a173132b0aa
     1 /*
       
     2 * Copyright (c) 2003 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:     Loads and stores the bitmap animator settings
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 
       
    22 #include <e32std.h>
       
    23 
       
    24 #include "CBmpAnimSettings.h"
       
    25 
       
    26 #include "BmpAnimUtils.h"
       
    27 #include <f32file.h>
       
    28 
       
    29 // Defaults
       
    30 const TInt KRunningTime = 1000000 * 60;  // 60 secs
       
    31 const TInt KTiming = 200000;  // 5 fps 
       
    32 const TInt KSuspendTime = -1; // Max = KMaxTInt, -1 = indefinitely
       
    33 const TInt KUndefined = -1;
       
    34 
       
    35 // Configuration file name
       
    36 _LIT(KIniFile, "BmpAnimScrPlugin.ini");
       
    37 
       
    38 // Default bitmap file name
       
    39 _LIT(KMbmFileName, "BmpAnimScrPlugin.mbm");
       
    40 
       
    41 // Default bitmap file name - lansdscape
       
    42 _LIT(KMbmFileNameL, "BmpAnimScrPluginL.mbm");
       
    43 
       
    44 // Basic info section name
       
    45 _LIT8(KIniSectionInfo, "info");
       
    46 
       
    47 // Info section keys
       
    48 _LIT8(KIniKeyName, "name");  // plugin name
       
    49 _LIT8(KIniKeyNumImages, "numimages");  // number of images
       
    50 _LIT8(KIniKeyMbmFile, "mbmfile");  // name of imagefile
       
    51 _LIT8(KIniKeyMbmFileL, "mbmfilelandscape");  // name of landscape imagefile
       
    52 _LIT8(KIniKeyRunningTime, "runningtime");  // time to run animation (in millis)
       
    53 _LIT8(KIniKeyLoopCount, "loopcount");  // loop count (if time is not defined)
       
    54 _LIT8(KIniKeySuspendTime, "suspendtime");  // time to suspend (in millis)
       
    55 _LIT8(KIniKeyTiming, "timing");  // time to display each image (RE-USED BELOW)
       
    56 _LIT8(KIniKeyLights, "lights");  // lights on for 5 secs. Over 5 = number of secs
       
    57 _LIT8(KIniKeyUseLandscape, "uselandscape");  // 1 = landscape image file used
       
    58 _LIT8(KIniKeyScaleBmps, "scalebmps");  // 1 = bitmaps scaled to screen size
       
    59 
       
    60 // Image section base name
       
    61 _LIT8(KIniSectionImageBase, "image");  // image information section name
       
    62 
       
    63 // Image section keys
       
    64 _LIT8(KIniKeyType, "type");  // 0 = background, 1 = bitmap, 2 = icon
       
    65 _LIT8(KIniKeyIndex, "index");  // index of image in mbm
       
    66 _LIT8(KIniKeyMaskIndex, "maskindex");  // index of icon mask in mbm
       
    67 _LIT8(KIniKeyFile, "file");  // name of image file (not yet supported)
       
    68 _LIT8(KIniKeyPosX, "posx");  // display position X
       
    69 _LIT8(KIniKeyPosY, "posy");  // display position Y
       
    70 // _LIT8(KIniKeyTiming, "timing");  // time to display this image (NOTE RE-USE!)
       
    71 
       
    72 
       
    73 // User constructor
       
    74 CBmpAnimSettings* CBmpAnimSettings::NewL()
       
    75     {
       
    76     CBmpAnimSettings* self = new(ELeave) CBmpAnimSettings();
       
    77     
       
    78     CleanupStack::PushL(self);
       
    79     self->ConstructL();
       
    80     CleanupStack::Pop();
       
    81     
       
    82     return self;
       
    83     }
       
    84 
       
    85     
       
    86 // C'tor, sets defaults
       
    87 CBmpAnimSettings::CBmpAnimSettings()
       
    88     : iRunningTime(KRunningTime),
       
    89       iLoopCount(KUndefined),
       
    90       iSuspendTime(KSuspendTime),
       
    91       iTiming(KTiming),
       
    92       iPluginName(KPluginName),
       
    93       iMbmFileName(KMbmFileName),
       
    94       iMbmFileNameL(KMbmFileNameL),
       
    95       iLights(KUndefined),
       
    96       iUseLandscape(0),
       
    97       iScaleBmps(0)
       
    98     {
       
    99     }
       
   100 
       
   101 
       
   102 // Two-phase constructor
       
   103 void CBmpAnimSettings::ConstructL()
       
   104     {
       
   105     // Try to open the settings file
       
   106     if (OpenSettings() == KErrNone)
       
   107         {
       
   108         LoadSettingsL();
       
   109         }
       
   110     }
       
   111 
       
   112     
       
   113 // D'tor
       
   114 CBmpAnimSettings::~CBmpAnimSettings()
       
   115     {
       
   116     delete iIniData;
       
   117     }
       
   118 
       
   119 
       
   120 // --- Access functions ---
       
   121     
       
   122     
       
   123 // Loads (common) settings from the settings file
       
   124 TBool CBmpAnimSettings::LoadSettingsL()
       
   125     {
       
   126     // Sanity check
       
   127     if (!iIniData)
       
   128         {
       
   129         return EFalse;
       
   130         }
       
   131 
       
   132     // Running time (never mind the section)
       
   133     if (!(iIniData->FindVar(KIniKeyRunningTime, iRunningTime)))
       
   134         {
       
   135         // Running time not found - try loop count
       
   136         if (!(iIniData->FindVar(KIniKeyLoopCount, iLoopCount)))
       
   137             {
       
   138             // That's not there either - use default running time
       
   139             iRunningTime = KRunningTime;
       
   140             }
       
   141         else
       
   142             {
       
   143             // Loop count found - running time is undefined
       
   144             iRunningTime = KUndefined;
       
   145             }
       
   146         }
       
   147 
       
   148     // Suspend time
       
   149     if (!(iIniData->FindVar(KIniKeySuspendTime, iSuspendTime)))
       
   150         {
       
   151         iSuspendTime = KSuspendTime;  // TODO: KUndefined
       
   152         }
       
   153 
       
   154     // Default timing (need to use section as well)
       
   155     if (!(iIniData->FindVar(
       
   156               KIniSectionInfo,
       
   157               KIniKeyTiming,
       
   158               iTiming)))
       
   159         {
       
   160         iTiming = KTiming;
       
   161         }
       
   162 
       
   163     // Lights time
       
   164     if (!(iIniData->FindVar(KIniKeyLights, iLights)))
       
   165         {
       
   166         iLights = KUndefined;
       
   167         }
       
   168 
       
   169     // Use landscape
       
   170     if (!(iIniData->FindVar(KIniKeyUseLandscape, iUseLandscape)))
       
   171         {
       
   172         iUseLandscape = 0;
       
   173         }
       
   174 
       
   175     // Scale bmps
       
   176     if (!(iIniData->FindVar(KIniKeyScaleBmps, iScaleBmps)))
       
   177         {
       
   178         iScaleBmps = 0;
       
   179         }
       
   180 
       
   181     // Plugin name
       
   182     iPluginName = KNullDesC;
       
   183     if ((!(iIniData->FindVar(KIniKeyName, iPluginName))) ||
       
   184         (iPluginName.Length() <= 0))
       
   185         {
       
   186         // Not found, or empty setting - use default
       
   187         iPluginName = KPluginName;
       
   188         }
       
   189 
       
   190     // Load BMP file names
       
   191     LoadBitmapFileName();
       
   192     LoadBitmapFileName(ETrue);
       
   193     
       
   194     BMALOGGER_WRITEF(_L("BMA: Detected settings:"));
       
   195     BMALOGGER_WRITEF(_L(" Name: %S"), &iPluginName);
       
   196     BMALOGGER_WRITEF(_L(" Run for: %d"), RunningTime());
       
   197     BMALOGGER_WRITEF(_L(" Loops: %d"), LoopCount());
       
   198     BMALOGGER_WRITEF(_L(" Suspend: %d"), SuspendTime());
       
   199     BMALOGGER_WRITEF(_L(" Timing: %d"), Timing());
       
   200     BMALOGGER_WRITEF(_L(" Lights: %d"), Lights());
       
   201     BMALOGGER_WRITEF(_L(" Bitmap file: %S"), &iMbmFileName);
       
   202     BMALOGGER_WRITEF(_L(" Lanscape bitmap file: %S"), &iMbmFileNameL);
       
   203     BMALOGGER_WRITEF(_L(" Use landscape: %d"), UseLandscape());
       
   204     BMALOGGER_WRITEF(_L(" Scale bmps: %d"), ScaleBmps());
       
   205     BMALOGGER_WRITEF(_L("BMA: End settings"));
       
   206 
       
   207     return ETrue;
       
   208     }
       
   209 
       
   210 
       
   211 // Saves (common) settings
       
   212 TBool CBmpAnimSettings::SaveSettingsL()
       
   213     {
       
   214     // Sanity check
       
   215     if (!iIniData)
       
   216         {
       
   217         return EFalse;
       
   218         }
       
   219 
       
   220     // Write lights
       
   221     if (iIniData->WriteVarL(
       
   222             KIniSectionInfo, KIniKeyLights, iLights) == KErrNone)
       
   223         {
       
   224         iIniData->CommitL();
       
   225         }
       
   226 
       
   227     return ETrue;
       
   228     }
       
   229     
       
   230 // --- private ---
       
   231 
       
   232     
       
   233 // Opens the settings file (initialises the settings reader)
       
   234 TInt CBmpAnimSettings::OpenSettings()
       
   235     {
       
   236     // Locate the dll path
       
   237     TFileName dllName;
       
   238 //    Dll::FileName(dllName);
       
   239     dllName.Append(_L("\\resource\\plugins\\"));
       
   240 
       
   241     // Combine the dll path with the settings file name
       
   242     TParse p;
       
   243     p.SetNoWild(KIniFile, &dllName, NULL);
       
   244 
       
   245     BMALOGGER_WRITEF(_L("Searching INI: %S"), &(p.FullName()));
       
   246 
       
   247     // Try to open the INI file from plugin directory
       
   248     TRAPD(err, iIniData = CBmpAnimIniData::NewL(p.FullName()));
       
   249 
       
   250     if (err != KErrNone)
       
   251         {
       
   252         BMALOGGER_WRITE("INI not in plugin dir, trying \\system\\data");
       
   253         // Try \system\data instead
       
   254         TRAP(err, iIniData = CBmpAnimIniData::NewL(KIniFile));
       
   255         }
       
   256     
       
   257     if (err != KErrNone)
       
   258         {
       
   259         // Make sure the settings are not attempted to be used
       
   260         delete iIniData;
       
   261         iIniData = NULL;
       
   262         BMALOGGER_WRITE("INI not found");
       
   263         }
       
   264     else
       
   265         {
       
   266         BMALOGGER_WRITE("INI found");
       
   267         }
       
   268 
       
   269     return err;
       
   270     }
       
   271 
       
   272 
       
   273 // Gets the bitmap file name from settings, or uses default
       
   274 void CBmpAnimSettings::LoadBitmapFileName(TBool aLandscape)
       
   275     {
       
   276     // Get the DLL path name. The path is used, if only a file name
       
   277     // is specified.
       
   278     TFileName dllName;
       
   279     Dll::FileName(dllName);
       
   280     
       
   281     // Get whatever's in the settings
       
   282 	TFileName fileName;
       
   283     TBool res = ETrue;
       
   284     if (aLandscape)
       
   285         {
       
   286         res = iIniData->FindVar(KIniKeyMbmFileL, fileName);
       
   287         }
       
   288     else
       
   289         {
       
   290         res = iIniData->FindVar(KIniKeyMbmFile, fileName);
       
   291         }
       
   292     
       
   293     if (!res)
       
   294         {
       
   295         // Not found - use the default name with the plugin directory
       
   296         // there's really no need to do anything here (I think)
       
   297         fileName = (_L("\\resource\\plugins\\"));
       
   298         }
       
   299 
       
   300     // The combination order should be:
       
   301     // 1. use whatever was defined in the settings (part or full path)
       
   302     // 2. use the default name part
       
   303     // 3. use the DLL path, if no path parts have yet been defined
       
   304     TParse p;
       
   305     if (p.SetNoWild(fileName,
       
   306                     aLandscape ? &KMbmFileNameL : &KMbmFileName,
       
   307                     &dllName) == KErrNone)
       
   308         {
       
   309         if (aLandscape)
       
   310             {
       
   311             iMbmFileNameL = p.FullName();
       
   312             }
       
   313         else
       
   314             {
       
   315             iMbmFileName = p.FullName();
       
   316             }
       
   317         }
       
   318     }
       
   319 
       
   320 //  End of File