gssettingsuis/Gs/GSBackgroundImage/Src/BackgroundImage.cpp
branchGCC_SURGE
changeset 45 f48d04161a92
parent 27 572294aa6075
parent 43 3341fe7c643a
equal deleted inserted replaced
27:572294aa6075 45:f48d04161a92
     1 /*
       
     2 * Copyright (c) 2005 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: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include    "backgroundimage.h"
       
    22 #include    "GsLogger.h"
       
    23 #include    "settingsinternalcrkeys.h"
       
    24 #include    "SettingsPrivateCRKeys.h"
       
    25 
       
    26 #include    <aknappui.h>
       
    27 #include    <aknlayoutscalable_apps.cdl.h>
       
    28 #include    <AknUtils.h>
       
    29 #include    <apgcli.h>
       
    30 #include    <applayout.cdl.h>
       
    31 #include    <DRMCommon.h>
       
    32 #include    <Oma1DcfCreator.h>  //DRM v2 migration
       
    33 #include    <s32file.h>
       
    34 #include    <startupdomaincrkeys.h>
       
    35 #include    <sysutil.h>
       
    36 #include    <graphics/bitmapuid.h>
       
    37 
       
    38 
       
    39 
       
    40 // CONSTANTS
       
    41 // Setting values for Background and Welcome note image
       
    42 const   TInt    KGSBackgroundModeImage = 1;
       
    43 // estimated image max size in bytes
       
    44 const   TInt    KGSMaxBackgroundImgSize = 520000;
       
    45 // estimated image max size in bytes
       
    46 const   TInt    KGSMaxWelcomeNoteImgSize = 100000;
       
    47 const   TInt    KGSVTStillImageWidth = 176;
       
    48 const   TInt    KGSVTStillImageHeight = 144;
       
    49 
       
    50 //Personalisation application is availble on ROM (1) or not (0)
       
    51 const   TInt    KGSPlsnOnRom = 1;
       
    52 const   TInt    KGSPlsnNotOnRom = 0;
       
    53 
       
    54 // other constants
       
    55 _LIT( KGSBackgroundImgPath, "c:\\resource\\apps\\backgroundimage.mbm");
       
    56 _LIT( KGSVTStillImgPath, "c:\\resource\\apps\\vtstillimage.mbm");
       
    57 
       
    58 
       
    59 
       
    60 
       
    61 // FORWARD DECLARATIONS
       
    62 
       
    63 
       
    64 // ============================ MEMBER FUNCTIONS ===============================
       
    65 
       
    66 // -----------------------------------------------------------------------------
       
    67 // CGSBackgroundImage::CGSBackgroundImage
       
    68 // C++ default constructor can NOT contain any code, that
       
    69 // might leave.
       
    70 // -----------------------------------------------------------------------------
       
    71 //
       
    72 CGSBackgroundImage::CGSBackgroundImage()
       
    73     {
       
    74     }
       
    75 
       
    76 // -----------------------------------------------------------------------------
       
    77 // CGSBackgroundImage::ConstructL
       
    78 // Symbian 2nd phase constructor can leave.
       
    79 // -----------------------------------------------------------------------------
       
    80 //
       
    81 void CGSBackgroundImage::ConstructL()
       
    82     {
       
    83     iPersonalizationRepository =
       
    84         CRepository::NewL( KCRUidPersonalizationSettings );
       
    85     iStartupConfRepository = CRepository::NewL( KCRUidStartupConf );
       
    86     iTelephonyRepository = CRepository::NewL( KCRUidTelephonySettings );
       
    87 
       
    88     iDisplayMode = CEikonEnv::Static()->ScreenDevice()->DisplayMode();
       
    89     }
       
    90 
       
    91 
       
    92 // -----------------------------------------------------------------------------
       
    93 // CGSBackgroundImage::NewL
       
    94 // Two-phased constructor.
       
    95 // -----------------------------------------------------------------------------
       
    96 //
       
    97 EXPORT_C CGSBackgroundImage* CGSBackgroundImage::NewL()
       
    98     {
       
    99     CGSBackgroundImage* self = new( ELeave ) CGSBackgroundImage;
       
   100 
       
   101     CleanupStack::PushL( self );
       
   102     self->ConstructL();
       
   103     CleanupStack::Pop();
       
   104 
       
   105     return self;
       
   106     }
       
   107 
       
   108 
       
   109 // ----------------------------------------------------------------------------
       
   110 // Destructor
       
   111 //
       
   112 //-----------------------------------------------------------------------------
       
   113 //
       
   114 CGSBackgroundImage::~CGSBackgroundImage()
       
   115     {
       
   116     delete iPersonalizationRepository;
       
   117     delete iStartupConfRepository;
       
   118     delete iTelephonyRepository;
       
   119     }
       
   120 
       
   121 // ----------------------------------------------------------------------------
       
   122 // CGSBackgroundImage::BackgroundImageL
       
   123 //
       
   124 // Returns background image mode.
       
   125 // DEPRECATED
       
   126 // ----------------------------------------------------------------------------
       
   127 //
       
   128 EXPORT_C TInt CGSBackgroundImage::BackgroundImageL()
       
   129     {
       
   130     TInt image;
       
   131     User::LeaveIfError( iPersonalizationRepository->
       
   132         Get( KSettingsBackgroundImage, image ) );
       
   133     return image;
       
   134     }
       
   135 
       
   136 // ----------------------------------------------------------------------------
       
   137 // CGSBackgroundImage::SetBackgroundImageL
       
   138 //
       
   139 // Sets background image mode.
       
   140 // DEPRECATED
       
   141 // ----------------------------------------------------------------------------
       
   142 //
       
   143 EXPORT_C void CGSBackgroundImage::SetBackgroundImageL(const TInt aImage)
       
   144     {
       
   145     User::LeaveIfError( iPersonalizationRepository->
       
   146         Set( KSettingsBackgroundImage, aImage ) );
       
   147     if (aImage != KGSBackgroundModeImage)
       
   148         {
       
   149         // It doesn't matter much if the image deletion fails,
       
   150         // so no need to handle the error.
       
   151         TRAP_IGNORE( DeleteImageL( KGSBackgroundImgPath ) );
       
   152         }
       
   153 
       
   154     }
       
   155 
       
   156 // ----------------------------------------------------------------------------
       
   157 // CGSBackgroundImage::SetBackgroundImagePathL
       
   158 //
       
   159 // Sets background image.
       
   160 // DEPRECATED
       
   161 // ----------------------------------------------------------------------------
       
   162 //
       
   163 EXPORT_C void CGSBackgroundImage::SetBackgroundImagePathL(
       
   164                                                 const TDesC& aImagePath )
       
   165     {
       
   166     // Check if there is enough space in disk to store background image
       
   167     if (SysUtil::FFSSpaceBelowCriticalLevelL(NULL, KGSMaxBackgroundImgSize))
       
   168         {
       
   169         // don't show any own notes here
       
   170         User::Leave(KErrDiskFull);
       
   171         }
       
   172 
       
   173     // make sure there is no memory leaks because of iBitmap and iDimmedBmp
       
   174     if (iBitmap)
       
   175         {
       
   176         delete iBitmap;
       
   177         iBitmap = NULL;
       
   178         }
       
   179 
       
   180     iBitmap = new(ELeave) CFbsBitmap;
       
   181 
       
   182     TRect appRect = iAvkonAppUi->ApplicationRect();
       
   183     TAknLayoutRect layout;
       
   184 
       
   185     layout.LayoutRect(appRect, AknLayout::wallpaper_pane());
       
   186     LoadImageL(aImagePath, layout.Rect().Width(), layout.Rect().Height(),
       
   187             ETrue, EGSWallpaperIdle);
       
   188     TBuf<1> KGSNullParam;
       
   189     SaveBitmapsL( KGSNullParam, KGSNullParam );
       
   190 
       
   191     delete iBitmap;
       
   192     iBitmap = NULL;
       
   193     User::LeaveIfError( iPersonalizationRepository->
       
   194         Set( KSettingsBackgroundImagePath, KGSBackgroundImgPath ) );
       
   195     }
       
   196 
       
   197 // ----------------------------------------------------------------------------
       
   198 // CGSBackgroundImage::SetBackgroundImagePathL
       
   199 // Sets background image. Does not store image to SD
       
   200 // Overloaded function, takes 2 parameters
       
   201 // DEPRECATED
       
   202 // ----------------------------------------------------------------------------
       
   203 //
       
   204 EXPORT_C void CGSBackgroundImage::SetBackgroundImagePathL(
       
   205     const TDesC& aImagePath,
       
   206     TDes& aBmpFileName )
       
   207     {
       
   208     SetBackgroundImagePathL( aImagePath, aBmpFileName, EGSWallpaperIdle );
       
   209     }
       
   210 
       
   211 // ----------------------------------------------------------------------------
       
   212 // CGSBackgroundImage::SetBackgroundImagePathL
       
   213 // Sets background image. Does not store image to SD
       
   214 // Overloaded function, takes 3 parameters
       
   215 // DEPRECATED
       
   216 // ----------------------------------------------------------------------------
       
   217 //
       
   218 EXPORT_C void CGSBackgroundImage::SetBackgroundImagePathL(
       
   219     const TDesC& aImagePath,
       
   220     TDes& aBmpFileName,
       
   221     TGSWallpaperType aWallpaperType )
       
   222     {
       
   223     // Check if there is enough space in disk to store background image
       
   224     if (SysUtil::FFSSpaceBelowCriticalLevelL(NULL, KGSMaxBackgroundImgSize))
       
   225         {
       
   226         // don't show any own notes here
       
   227         User::Leave(KErrDiskFull);
       
   228         }
       
   229 
       
   230     // Make sure there is no memory leaks because of iBitmap and iDimmedBmp
       
   231     if (iBitmap)
       
   232         {
       
   233         delete iBitmap;
       
   234         iBitmap = NULL;
       
   235         }
       
   236 
       
   237     iBitmap = new(ELeave) CFbsBitmap;
       
   238 
       
   239     if( aWallpaperType == EGSWallpaperIdle )
       
   240         {
       
   241         //Reading wallpaper size
       
   242         __GSLOGSTRING(
       
   243             "[CGSBackgroundImage] SetBackgroundImagePathL |->");
       
   244         TRect screenRect( CEikonEnv::Static()->ScreenDevice()->SizeInPixels() );
       
   245         TAknLayoutRect appRect;
       
   246         appRect.LayoutRect(
       
   247             screenRect,
       
   248             AknLayout::application_window( screenRect ) );
       
   249         TAknLayoutRect mainRectSmall;
       
   250         mainRectSmall.LayoutRect(
       
   251             appRect.Rect(),
       
   252             AknLayout::main_pane( appRect.Rect(), 0, 1, 1 ) );
       
   253         TAknLayoutRect layout;
       
   254         layout.LayoutRect( mainRectSmall.Rect(), AknLayout::wallpaper_pane() );
       
   255 
       
   256         __GSLOGSTRING("[CGSBackgroundImage] SetBackgroundImagePathL");
       
   257         LoadImageL(aImagePath, layout.Rect().Width(), layout.Rect().Height(),
       
   258             ETrue, EGSWallpaperIdle);
       
   259         }
       
   260     else if( aWallpaperType == EGSWallpaperPinboard )
       
   261         {
       
   262         //Reading wallpaper size
       
   263         __GSLOGSTRING("[CGSBackgroundImage] SetBackgroundImagePathL 1");
       
   264 
       
   265         TRect screenRect( CEikonEnv::Static()->ScreenDevice()->SizeInPixels() );
       
   266         TAknLayoutRect appRect;
       
   267         appRect.LayoutRect(
       
   268             screenRect,
       
   269             AknLayout::application_window( screenRect ) );
       
   270         TAknLayoutRect mainRectSmall;
       
   271         mainRectSmall.LayoutRect(
       
   272             appRect.Rect(),
       
   273             AknLayout::main_pane( appRect.Rect(), 0, 0, 1 ) );
       
   274         TAknLayoutRect layout;
       
   275 
       
   276         layout.LayoutRect(
       
   277             mainRectSmall.Rect(), AknLayoutScalable_Apps::bg_pinb_pane(0) );
       
   278 
       
   279         __GSLOGSTRING("[CGSBackgroundImage] SetBackgroundImagePathL 2");
       
   280         LoadImageL(aImagePath, layout.Rect().Width(), layout.Rect().Height(),
       
   281             ETrue, EGSWallpaperPinboard);
       
   282         __GSLOGSTRING("[CGSBackgroundImage] SetBackgroundImagePathL -|");
       
   283         }
       
   284 
       
   285     SaveBitmapsL( aBmpFileName, aImagePath );
       
   286 
       
   287     delete iBitmap;
       
   288     iBitmap = NULL;
       
   289     }
       
   290 
       
   291 // ----------------------------------------------------------------------------
       
   292 // CGSBackgroundImage::SetWelcomeNoteImageL
       
   293 //
       
   294 // Sets welcome note image's path
       
   295 // ----------------------------------------------------------------------------
       
   296 //
       
   297 EXPORT_C void CGSBackgroundImage::SetWelcomeNoteImageL(const TDesC& aImagePath,
       
   298                           CGSAsyncImageHandling& aImgHandler )
       
   299     {
       
   300     // Check if there is enough space in disk to store welcome note image
       
   301     if ( SysUtil::FFSSpaceBelowCriticalLevelL( NULL, KGSMaxWelcomeNoteImgSize ) )
       
   302         {
       
   303         // don't show any own notes here
       
   304         User::Leave( KErrDiskFull );
       
   305         }
       
   306 
       
   307 
       
   308 
       
   309     TRect screen = iAvkonAppUi->ApplicationRect();
       
   310     TInt screenWidth = screen.Width() - 2;
       
   311     TInt screenHeight = screen.Height() - 2;
       
   312 
       
   313     //rescale
       
   314     aImgHandler.ProcessImageL( aImagePath, screenWidth, screenHeight,
       
   315                                                         EGSWelcomeNoteImage );
       
   316     }
       
   317 
       
   318 // ----------------------------------------------------------------------------
       
   319 // CGSBackgroundImage::LoadImageL
       
   320 // Loads an image to bitmap and scales it if it's too large.
       
   321 // Deprecated
       
   322 // ----------------------------------------------------------------------------
       
   323 //
       
   324 void CGSBackgroundImage::LoadImageL(const TDesC& /*aImagePath*/,
       
   325                         TInt /*aWidth*/, TInt /*aHeight*/,
       
   326                         TBool /*aBgImage*/, TGSWallpaperType /*aWallpaperType*/)
       
   327     {
       
   328     }
       
   329 
       
   330 
       
   331 // ----------------------------------------------------------------------------
       
   332 // CGSBackgroundImage::SaveBitmapsL
       
   333 // Stores background image and dimmed image to the same mbm file.
       
   334 // ----------------------------------------------------------------------------
       
   335 //
       
   336 void CGSBackgroundImage::SaveBitmapsL( TDes& aBmpFileName,
       
   337                                             const TDesC& aOriginalFullname )
       
   338     {
       
   339     RFs fs;
       
   340     User::LeaveIfError( fs.Connect() );
       
   341     CleanupClosePushL( fs );
       
   342 
       
   343     CDirectFileStore* fileStore;
       
   344     if( aBmpFileName != KNullDesC )
       
   345         {
       
   346         fileStore = CDirectFileStore::ReplaceLC(
       
   347             fs, aBmpFileName, EFileWrite );
       
   348         }
       
   349     else
       
   350         {
       
   351         fileStore = CDirectFileStore::ReplaceLC(
       
   352             fs, KGSBackgroundImgPath, EFileWrite );
       
   353         }
       
   354 
       
   355     TUidType uidType( KDirectFileStoreLayoutUid, KMultiBitmapFileImageUid );
       
   356     fileStore->SetTypeL( uidType );
       
   357 
       
   358     RStoreWriteStream bmpStream;
       
   359     TStreamId imgId = bmpStream.CreateLC( *fileStore );
       
   360     iBitmap->ExternalizeL( bmpStream );
       
   361 
       
   362     CleanupStack::PopAndDestroy(); //bmpStream
       
   363 
       
   364     RStoreWriteStream rootStream;
       
   365     // create root stream
       
   366     TStreamId rootStreamId = rootStream.CreateLC( *fileStore );
       
   367     rootStream.WriteInt32L( 1 ); // number of bitmaps
       
   368 
       
   369     rootStream << imgId; // stream id of bitmap
       
   370 
       
   371     CleanupStack::PopAndDestroy(); //rootStream
       
   372 
       
   373     fileStore->SetRootL( rootStreamId );
       
   374     CleanupStack::PopAndDestroy(); // filestore;
       
   375 
       
   376     TBool isprotected( EFalse );
       
   377 
       
   378     DRMCommon* drmclient = DRMCommon::NewL();
       
   379 
       
   380     CleanupStack::PushL( drmclient );
       
   381     TInt error = drmclient->Connect();
       
   382     if ( error != DRMCommon::EOk )
       
   383         {
       
   384         User::Leave( KErrCorrupt );
       
   385         }
       
   386 
       
   387     if ( aOriginalFullname != KNullDesC )
       
   388         {
       
   389         error = drmclient->IsProtectedFile( aOriginalFullname, isprotected );
       
   390         if ( error != DRMCommon::EOk )
       
   391             {
       
   392             User::Leave( KErrCorrupt );
       
   393             }
       
   394         }
       
   395 
       
   396 
       
   397     if ( isprotected )
       
   398         {
       
   399         HBufC8* contenturi = NULL;
       
   400         HBufC8* mimetype = NULL;
       
   401         DRMCommon::TContentProtection contentProtection;
       
   402         TUint datalen( 0 );
       
   403         CDRMRights* rights = NULL;
       
   404 
       
   405 
       
   406         error = drmclient->GetFileInfo(
       
   407             aOriginalFullname, contentProtection,
       
   408             mimetype, contenturi, datalen );
       
   409         CleanupStack::PushL( mimetype );
       
   410         CleanupStack::PushL( contenturi );
       
   411         if ( error != DRMCommon::EOk )
       
   412             {
       
   413             User::Leave( KErrCorrupt );
       
   414             }
       
   415 
       
   416         drmclient->GetActiveRights(
       
   417             *contenturi, DRMCommon::EDisplay, rights );
       
   418         CleanupStack::PushL( rights );
       
   419 
       
   420         RFile file;
       
   421         User::LeaveIfError( file.Open( fs, aBmpFileName,
       
   422                                         EFileWrite|EFileShareAny ) );
       
   423         CleanupClosePushL( file );
       
   424 
       
   425         TInt filesize( 0 );
       
   426         User::LeaveIfError( file.Size( filesize ) );
       
   427         HBufC8* content = HBufC8::NewL( filesize ); // use actual filesize
       
   428 
       
   429         CleanupStack::PushL( content );
       
   430 
       
   431         TPtr8 readPtr( content->Des() );
       
   432         User::LeaveIfError( file.Read( readPtr ) );
       
   433 
       
   434         CleanupStack::Pop(); // content
       
   435         COma1DcfCreator* dcfCreator = COma1DcfCreator::NewL();
       
   436         dcfCreator->EncryptContentL( content, *mimetype, rights );
       
   437         CleanupStack::PushL( content );
       
   438 
       
   439         TInt offset = 0;
       
   440         User::LeaveIfError( file.Seek( ESeekStart,offset ) );
       
   441         TPtr8 writePtr( content->Des() );
       
   442         User::LeaveIfError( file.Write( writePtr ) );
       
   443         User::LeaveIfError( file.Flush() );
       
   444         // mimetype, contenturi, rights, file, content
       
   445         CleanupStack::PopAndDestroy(5);
       
   446         delete dcfCreator;
       
   447         }
       
   448 
       
   449     drmclient->Disconnect();
       
   450     CleanupStack::PopAndDestroy(2); // fs & drmclient
       
   451     }
       
   452 
       
   453 
       
   454 // ----------------------------------------------------------------------------
       
   455 // CGSBackgroundImage::DeleteImageL
       
   456 // Deletes file specified in the parameter.
       
   457 // ----------------------------------------------------------------------------
       
   458 //
       
   459 EXPORT_C void CGSBackgroundImage::DeleteImageL(const TDesC& aImagePath)
       
   460     {
       
   461     RFs fs;
       
   462     User::LeaveIfError(fs.Connect());
       
   463     CleanupClosePushL(fs);
       
   464 
       
   465     User::LeaveIfError(fs.Delete(aImagePath));
       
   466 
       
   467     CleanupStack::PopAndDestroy(); // fs
       
   468     }
       
   469 
       
   470 // ----------------------------------------------------------------------------
       
   471 // CGSBackgroundImage::SetVTStillImagePathL
       
   472 //
       
   473 // Sets VT still image.
       
   474 // ----------------------------------------------------------------------------
       
   475 //
       
   476 EXPORT_C void CGSBackgroundImage::SetVTStillImagePathL(
       
   477                                            const TDesC& aImagePath,
       
   478                                            CGSAsyncImageHandling& aImgHandler )
       
   479     {
       
   480     // Check if there is enough space in disk to store still image
       
   481     if (SysUtil::FFSSpaceBelowCriticalLevelL(NULL, KGSMaxBackgroundImgSize))
       
   482         {
       
   483         // don't show any own notes here
       
   484         User::Leave(KErrDiskFull);
       
   485         }
       
   486     aImgHandler.ProcessImageL( aImagePath, KGSVTStillImageWidth,
       
   487                     KGSVTStillImageHeight, EGSVtStillImage );
       
   488     User::LeaveIfError( iTelephonyRepository->
       
   489             Set( KSettingsVTStillImagePath, KGSVTStillImgPath ) );
       
   490     }
       
   491 
       
   492 // ----------------------------------------------------------------------------
       
   493 // CGSBackgroundImage::GetPlnsStatus
       
   494 //
       
   495 // Check if Personalisation application is on the device
       
   496 // ----------------------------------------------------------------------------
       
   497 //
       
   498 EXPORT_C TInt CGSBackgroundImage::GetPlnsStatus()
       
   499     {
       
   500     TRAP_IGNORE( IsPersonalisationApplicationL() );
       
   501     return iPlnsInDevice;
       
   502     }
       
   503 
       
   504 // ----------------------------------------------------------------------------
       
   505 // CGSBackgroundImage::IsPersonalisationApplication
       
   506 //
       
   507 //
       
   508 // ----------------------------------------------------------------------------
       
   509 //
       
   510 EXPORT_C void CGSBackgroundImage::IsPersonalisationApplicationL()
       
   511     {
       
   512     RApaLsSession session;
       
   513     User::LeaveIfError( session.Connect() );
       
   514 
       
   515     const TUid KUidPsln = { 0x10005A32 };
       
   516     TApaAppInfo appInfo;
       
   517     TInt search = session.GetAppInfo( appInfo, KUidPsln );
       
   518     session.Close();
       
   519 
       
   520     if ( search == KErrNone )
       
   521         {
       
   522         TParse fileInfo;
       
   523         fileInfo.Set( appInfo.iFullName, NULL, NULL );
       
   524         TPtrC drive = fileInfo.Drive();
       
   525         _LIT( KZDrive, "Z:" );
       
   526         if ( drive == KZDrive )
       
   527             {
       
   528             iPlnsInDevice = KGSPlsnOnRom;
       
   529             }
       
   530         else
       
   531             {
       
   532             iPlnsInDevice = KGSPlsnNotOnRom;
       
   533             }
       
   534         }
       
   535     else
       
   536         {
       
   537         iPlnsInDevice = KErrNotFound;
       
   538         }
       
   539     }
       
   540 
       
   541 
       
   542 //  End of File