photosgallery/collectionframework/thumbnailcreator/src/glxtnfileutility.cpp
changeset 0 4e91876724a2
child 18 bcb43dc84c44
equal deleted inserted replaced
-1:000000000000 0:4e91876724a2
       
     1 /*
       
     2 * Copyright (c) 2008-2009 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:    Utility for thumbnail tasks handling files
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 /**
       
    22  * @internal reviewed 31/07/2007 by Simon Brooks
       
    23  */
       
    24 
       
    25 #include "glxtnfileutility.h"
       
    26 #include <glxtracer.h>
       
    27 #include <glxpanic.h>
       
    28 #include <glxthumbnailinfo.h>
       
    29 #include <pathinfo.h>
       
    30 #include <s32file.h>
       
    31 #include <featmgr.h>
       
    32 #include <bldvariant.hrh>   // For feature constants
       
    33 
       
    34 _LIT(KGlxBadFileList, "glxbadfilelist.dat");
       
    35 _LIT(KGlxBadFileMarker, "glxbadfilemarker.dat");
       
    36 
       
    37 // -----------------------------------------------------------------------------
       
    38 // Constructor
       
    39 // -----------------------------------------------------------------------------
       
    40 //
       
    41 CGlxtnFileUtility::CGlxtnFileUtility()
       
    42     {
       
    43     TRACER("CGlxtnFileUtility::CGlxtnFileUtility()");
       
    44     }
       
    45 
       
    46 // -----------------------------------------------------------------------------
       
    47 // ConstructL
       
    48 // Symbian 2nd phase constructor can leave.
       
    49 // -----------------------------------------------------------------------------
       
    50 //
       
    51 void CGlxtnFileUtility::ConstructL()
       
    52     {
       
    53     TRACER("CGlxtnFileUtility::ConstructL()");
       
    54 	User::LeaveIfError(iFs.Connect());
       
    55 
       
    56     iFs.PrivatePath(iBadFileDir);
       
    57     iBadFileDir.Insert(
       
    58                 0, PathInfo::PhoneMemoryRootPath().Left( KMaxDriveName ) );
       
    59 
       
    60     TRAP_IGNORE(ReadBadFileListL());
       
    61 
       
    62     LayoutSpecificDataL();
       
    63     
       
    64     }
       
    65 
       
    66 // -----------------------------------------------------------------------------
       
    67 // NewL
       
    68 // Two-phased constructor.
       
    69 // -----------------------------------------------------------------------------
       
    70 //
       
    71 CGlxtnFileUtility* CGlxtnFileUtility::NewL()
       
    72     {
       
    73     TRACER("CGlxtnFileUtility::NewL()");
       
    74     CGlxtnFileUtility* self = new (ELeave) CGlxtnFileUtility;
       
    75 
       
    76     CleanupStack::PushL(self);
       
    77     self->ConstructL();
       
    78     CleanupStack::Pop();
       
    79 
       
    80     return self;
       
    81     }
       
    82 
       
    83 // -----------------------------------------------------------------------------
       
    84 // Destructor
       
    85 // -----------------------------------------------------------------------------
       
    86 //
       
    87 CGlxtnFileUtility::~CGlxtnFileUtility()
       
    88     {
       
    89     TRACER("CGlxtnFileUtility::~CGlxtnFileUtility()");
       
    90 	iFs.Close();
       
    91 	iBadFileArray.ResetAndDestroy();
       
    92 	iPersistentSizeClasses.Close();
       
    93     }
       
    94 
       
    95 // -----------------------------------------------------------------------------
       
    96 // FsSession
       
    97 // Provide file server session for opening images from files.
       
    98 // -----------------------------------------------------------------------------
       
    99 //
       
   100 RFs& CGlxtnFileUtility::FsSession()
       
   101     {
       
   102     TRACER("RFs& CGlxtnFileUtility::FsSession()");
       
   103     return iFs;
       
   104     }
       
   105 
       
   106 // -----------------------------------------------------------------------------
       
   107 // CheckBadFileListL
       
   108 // Test whether a file is on the bad file list.  If not the bad file marker
       
   109 // is set to the filename, so that if a panic occurs it will be added to the
       
   110 // bad file list.
       
   111 // -----------------------------------------------------------------------------
       
   112 //
       
   113 void CGlxtnFileUtility::CheckBadFileListL(const TDesC& aFilename)
       
   114     {
       
   115     TRACER("0CGlxtnFileUtility::CheckBadFileListL(const TDesC& aFilename)");
       
   116     for ( TInt i = 0; i < iBadFileArray.Count(); i++ )
       
   117         {
       
   118         if ( 0 == iBadFileArray[i]->CompareF(aFilename) )
       
   119             {
       
   120             // File is bad
       
   121             User::Leave( KErrCorrupt );
       
   122             }
       
   123         }
       
   124 
       
   125     // Set bad file marker
       
   126     TPath path(iBadFileDir);
       
   127     path.Append(KGlxBadFileMarker);
       
   128 
       
   129     // Ensure directory exists
       
   130     TInt err = iFs.MkDirAll(path);
       
   131     if ( err != KErrAlreadyExists )
       
   132         {
       
   133         User::LeaveIfError(err);
       
   134         }
       
   135 
       
   136     RFileWriteStream stream;
       
   137     CleanupClosePushL(stream);
       
   138     User::LeaveIfError(stream.Replace(
       
   139                                 iFs, path, EFileWrite | EFileShareExclusive));
       
   140     stream << aFilename;
       
   141     stream.CommitL();
       
   142     CleanupStack::PopAndDestroy(&stream);
       
   143     }
       
   144 
       
   145 // -----------------------------------------------------------------------------
       
   146 // ClearBadFileMarker
       
   147 // Clear the bad file marker.  Called when processing a file is complete (no
       
   148 // panic occurred).
       
   149 // -----------------------------------------------------------------------------
       
   150 //
       
   151 void CGlxtnFileUtility::ClearBadFileMarker()
       
   152     {
       
   153     TRACER("void CGlxtnFileUtility::ClearBadFileMarker()");
       
   154     TPath path(iBadFileDir);
       
   155     path.Append(KGlxBadFileMarker);
       
   156 
       
   157     ( void )iFs.Delete( path );  // Ignore error
       
   158     }
       
   159 
       
   160 // -----------------------------------------------------------------------------
       
   161 // IsPersistentSize
       
   162 // Test whether a generated thumbnail should be stored in persistent storage.
       
   163 // -----------------------------------------------------------------------------
       
   164 //
       
   165 TBool CGlxtnFileUtility::IsPersistentSize(const TSize& aSize)
       
   166     {
       
   167     TRACER("CGlxtnFileUtility::IsPersistentSize()");
       
   168     for ( TInt i = 0; i < iPersistentSizeClasses.Count(); i++ )
       
   169         {
       
   170         if ( iPersistentSizeClasses[i] == aSize )
       
   171             {
       
   172             return ETrue;
       
   173             }
       
   174         }
       
   175 
       
   176     return EFalse;
       
   177     }
       
   178 
       
   179 // -----------------------------------------------------------------------------
       
   180 // ReadBadFileListL
       
   181 // -----------------------------------------------------------------------------
       
   182 //
       
   183 void CGlxtnFileUtility::ReadBadFileListL()
       
   184     {
       
   185     TRACER("void CGlxtnFileUtility::ReadBadFileListL()");
       
   186     TPath pathList(iBadFileDir);
       
   187     pathList.Append(KGlxBadFileList);
       
   188     TPath pathMarker(iBadFileDir);
       
   189     pathMarker.Append(KGlxBadFileMarker);
       
   190 
       
   191     RFileReadStream stream;
       
   192     CleanupClosePushL(stream);
       
   193 
       
   194     // Check bad file list
       
   195     if ( KErrNone == stream.Open(
       
   196                             iFs, pathList, EFileRead | EFileShareReadersOnly) )
       
   197         {
       
   198         TInt err;
       
   199         // Loop until end of file
       
   200         do
       
   201             {
       
   202             TRAP(err,
       
   203                 HBufC* file = HBufC::NewLC(stream, KMaxTInt);
       
   204                 iBadFileArray.AppendL(file);
       
   205                 CleanupStack::Pop(file);
       
   206                 )
       
   207             } while ( KErrNone == err );
       
   208 
       
   209         stream.Close();
       
   210         // NOTE: We always get KErrEof even if the file is corrupted
       
   211         }
       
   212 
       
   213     // Check bad file marker
       
   214     if ( KErrNone == stream.Open(
       
   215                         iFs, pathMarker, EFileRead | EFileShareReadersOnly) )
       
   216         {
       
   217         // File exists, file in marker must be bad
       
   218         HBufC* file = HBufC::NewLC(stream, KMaxTInt);
       
   219         iBadFileArray.AppendL(file);
       
   220         CleanupStack::Pop(file);
       
   221 
       
   222         // Save the list for next gallery session
       
   223         // Recreate whole file in case existing file is corrupted
       
   224         WriteBadFileListL();
       
   225 
       
   226         stream.Close();
       
   227         ClearBadFileMarker();
       
   228         }
       
   229 
       
   230     CleanupStack::Pop(&stream); // Stream already closed
       
   231     }
       
   232 
       
   233 // -----------------------------------------------------------------------------
       
   234 // WriteBadFileListL
       
   235 // -----------------------------------------------------------------------------
       
   236 //
       
   237 void CGlxtnFileUtility::WriteBadFileListL()
       
   238     {
       
   239     TRACER("void CGlxtnFileUtility::WriteBadFileListL()");
       
   240     TPath path(iBadFileDir);
       
   241     path.Append(KGlxBadFileList);
       
   242 
       
   243     RFileWriteStream stream;
       
   244     CleanupClosePushL(stream);
       
   245     User::LeaveIfError(stream.Replace(
       
   246                         iFs, path, EFileWrite | EFileShareExclusive));
       
   247 
       
   248     for ( TInt i = 0; i < iBadFileArray.Count() ; i++ )
       
   249         {
       
   250         stream << *iBadFileArray[i];
       
   251         }
       
   252     stream.CommitL();
       
   253 
       
   254     CleanupStack::PopAndDestroy(&stream);
       
   255     }
       
   256 // -----------------------------------------------------------------------------
       
   257 // LayoutSpecificDataL
       
   258 // -----------------------------------------------------------------------------
       
   259 //	
       
   260 void CGlxtnFileUtility::LayoutSpecificDataL()
       
   261 	{
       
   262 	TRACER("void CGlxtnFileUtility::LayoutSpecificDataL()");
       
   263 	// Sets up TLS, must be done before FeatureManager is used.
       
   264 	        FeatureManager::InitializeLibL();
       
   265 	        
       
   266 	      if(FeatureManager::FeatureSupported( KFeatureIdLayout640_360_Touch ) || FeatureManager::FeatureSupported( KFeatureIdLayout360_640_Touch ))
       
   267 	          {
       
   268 	          iPersistentSizeClasses.AppendL(TSize(111,83));
       
   269               iPersistentSizeClasses.AppendL(TSize(640,360));
       
   270               iPersistentSizeClasses.AppendL(TSize(360,640));
       
   271 	          }
       
   272 	      else if(FeatureManager::FeatureSupported(KFeatureIdLayout640_480_Touch) || FeatureManager::FeatureSupported(KFeatureIdLayout480_640_Touch) || 
       
   273 	              FeatureManager::FeatureSupported(KFeatureIdLayout640_480) || FeatureManager::FeatureSupported(KFeatureIdLayout480_640))
       
   274 	          {
       
   275 	          iPersistentSizeClasses.AppendL(TSize(146,110));
       
   276 	          iPersistentSizeClasses.AppendL(TSize(640,480));
       
   277 	          iPersistentSizeClasses.AppendL(TSize(480,640));
       
   278 	          }
       
   279 	      else
       
   280 	          {
       
   281 	          iPersistentSizeClasses.AppendL(TSize(146,110));
       
   282 	          iPersistentSizeClasses.AppendL(TSize(640,480));
       
   283 	          iPersistentSizeClasses.AppendL(TSize(480,640));
       
   284 	          }
       
   285 	    // Frees the TLS. Must be done after FeatureManager is used.
       
   286 	        FeatureManager::UnInitializeLib(); 
       
   287     }
       
   288 	
       
   289  
       
   290 	//  End of File