imagehandlingutilities/thumbnailmanager/thumbnailserver/src/thumbnailstore.cpp
changeset 0 2014ca87e772
child 1 235a7fc86938
equal deleted inserted replaced
-1:000000000000 0:2014ca87e772
       
     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:  Store for thumbnails.
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 #include <s32mem.h>
       
    20 #include <e32cmn.h>
       
    21 #include <fbs.h>
       
    22 #include <imageconversion.h>
       
    23 #include <e32base.h>
       
    24 #include <exifread.h>
       
    25 
       
    26 #include <iclextjpegapi.h>
       
    27 #include "thumbnailstore.h"
       
    28 #include "thumbnailsql.h"
       
    29 #include "thumbnaillog.h"
       
    30 #include "thumbnailmanageruids.hrh"
       
    31 #include "thumbnailcenrep.h"
       
    32 #include "thumbnailpanic.h"
       
    33 #include "thumbnailmanagerconstants.h"
       
    34 #include "thumbnailserver.h"
       
    35 
       
    36 
       
    37 
       
    38 _LIT8( KThumbnailSqlConfig, "page_size=16384; cache_size=32;" );
       
    39 
       
    40 const TInt KStreamBufferSize = 1024 * 8;
       
    41 const TInt KMajor = 3;
       
    42 const TInt KMinor = 2;
       
    43 
       
    44 // Database path without drive letter
       
    45 _LIT( KThumbnailDatabaseName, ":[102830AB]thumbnail_v2.db" );
       
    46 
       
    47 // Allow access to database only for the server process
       
    48 const TSecurityPolicy KThumbnailDatabaseSecurityPolicy( TSecureId(
       
    49     THUMBNAIL_MANAGER_SERVER_UID ));
       
    50 
       
    51 
       
    52 // ---------------------------------------------------------------------------
       
    53 // RThumbnailTransaction::RThumbnailTransaction::TThumbnailPersistentSize
       
    54 // ---------------------------------------------------------------------------
       
    55 //
       
    56 RThumbnailTransaction::RThumbnailTransaction( RSqlDatabase& aDatabase ):
       
    57     iDatabase( aDatabase ), iState( EClosed )
       
    58     {
       
    59     // No implementation required
       
    60     }
       
    61 
       
    62 
       
    63 // ---------------------------------------------------------------------------
       
    64 // RThumbnailTransaction::BeginL()
       
    65 // ---------------------------------------------------------------------------
       
    66 //
       
    67 void RThumbnailTransaction::BeginL()
       
    68     {
       
    69     const TInt err = iDatabase.Exec( KThumbnailBeginTransaction );
       
    70     if ( err >= 0 )
       
    71         {
       
    72         iState = EOpen;
       
    73         }
       
    74     else
       
    75         {
       
    76         iState = EError;
       
    77         User::Leave( err );
       
    78         }
       
    79     }
       
    80 
       
    81 
       
    82 // ---------------------------------------------------------------------------
       
    83 // RThumbnailTransaction::Close()
       
    84 // ---------------------------------------------------------------------------
       
    85 //
       
    86 void RThumbnailTransaction::Close()
       
    87     {
       
    88     if ( iState != EClosed )
       
    89         {
       
    90         Rollback();
       
    91         }
       
    92     }
       
    93 
       
    94 
       
    95 // ---------------------------------------------------------------------------
       
    96 // RThumbnailTransaction::CommitL()
       
    97 // ---------------------------------------------------------------------------
       
    98 //
       
    99 void RThumbnailTransaction::CommitL()
       
   100     {
       
   101     User::LeaveIfError( iDatabase.Exec( KThumbnailCommitTransaction ));
       
   102     iState = EClosed;
       
   103     }
       
   104 
       
   105 
       
   106 // ---------------------------------------------------------------------------
       
   107 // RThumbnailTransaction::Rollback()
       
   108 // ---------------------------------------------------------------------------
       
   109 //
       
   110 TInt RThumbnailTransaction::Rollback()
       
   111     {
       
   112     const TInt err = iDatabase.Exec( KThumbnailRollbackTransaction );
       
   113     if ( err >= 0 )
       
   114         {
       
   115         iState = EClosed;
       
   116         }
       
   117     return err;
       
   118     }
       
   119 
       
   120 // ======== MEMBER FUNCTIONS ========
       
   121 
       
   122 // ---------------------------------------------------------------------------
       
   123 // CThumbnailStore::NewL()
       
   124 // Two-phased constructor.
       
   125 // ---------------------------------------------------------------------------
       
   126 //
       
   127 CThumbnailStore* CThumbnailStore::NewL( RFs& aFs, TInt aDrive, TDesC& aImei, CThumbnailServer* aServer )
       
   128     {
       
   129     CThumbnailStore* self = new( ELeave )CThumbnailStore( aFs, aDrive, aImei, aServer );
       
   130     CleanupStack::PushL( self );
       
   131     self->ConstructL();
       
   132     CleanupStack::Pop( self );
       
   133     return self;
       
   134     }
       
   135 
       
   136 
       
   137 // ---------------------------------------------------------------------------
       
   138 // CThumbnailStore::~CThumbnailStore()
       
   139 // Destructor.
       
   140 // ---------------------------------------------------------------------------
       
   141 //
       
   142 CThumbnailStore::~CThumbnailStore()
       
   143     {
       
   144     TN_DEBUG1( "CThumbnailStore::~CThumbnailStore()" );
       
   145 
       
   146     if(!iServer->IsFormatting())
       
   147         {
       
   148  	    FlushCacheTable( ETrue );
       
   149         }
       
   150     if( iAutoFlushTimer )
       
   151         {
       
   152         iAutoFlushTimer->Cancel();
       
   153         delete iAutoFlushTimer;
       
   154         iAutoFlushTimer = NULL;
       
   155         }
       
   156     
       
   157     iDatabase.Close();
       
   158     TN_DEBUG1( "CThumbnailStore::~CThumbnailStore() - database closed" );
       
   159     }
       
   160 
       
   161 
       
   162 // ---------------------------------------------------------------------------
       
   163 // CThumbnailStore::CThumbnailStore()
       
   164 // C++ default constructor can NOT contain any code, that might leave.
       
   165 // ---------------------------------------------------------------------------
       
   166 //
       
   167 CThumbnailStore::CThumbnailStore( RFs& aFs, TInt aDrive, TDesC& aImei,  CThumbnailServer* aServer ): 
       
   168     iFs( aFs ), iDrive( aDrive ), iBatchItemCount(0), iImei(aImei), iServer(aServer)
       
   169     {
       
   170     // no implementation required
       
   171     }
       
   172 
       
   173 
       
   174 // ---------------------------------------------------------------------------
       
   175 // CThumbnailStore::ConstructL()
       
   176 // Symbian 2nd phase constructor can leave.
       
   177 // ---------------------------------------------------------------------------
       
   178 //
       
   179 void CThumbnailStore::ConstructL()
       
   180     {
       
   181     TN_DEBUG1( "CThumbnailStore::ConstructL()" );
       
   182 
       
   183 #ifdef _DEBUG
       
   184     iThumbCounter = 0;
       
   185 #endif
       
   186     
       
   187     HBufC* databasePath = HBufC::NewLC( KMaxFileName );
       
   188     TPtr pathPtr = databasePath->Des();
       
   189     TChar driveChar = 0;
       
   190     User::LeaveIfError( RFs::DriveToChar( iDrive, driveChar ));
       
   191     pathPtr.Append( driveChar );
       
   192     pathPtr.Append( KThumbnailDatabaseName );
       
   193 
       
   194     TVolumeInfo volumeinfo;
       
   195     iFs.Volume(volumeinfo, iDrive);
       
   196     TUint id = volumeinfo.iUniqueID;
       
   197     TBuf<50> mediaid;
       
   198     mediaid.Num(id);
       
   199     TBool newDatabase(EFalse);
       
   200     
       
   201     TInt error = KErrNone;
       
   202     
       
   203     TInt err = iDatabase.Open( pathPtr );
       
   204     if ( err == KErrNotFound )
       
   205         {
       
   206         // db not found, create new
       
   207         TN_DEBUG1( "CThumbnailStore::ConstructL() -- 1 creating database" );
       
   208         const TDesC8& config = KThumbnailSqlConfig;
       
   209 
       
   210         RSqlSecurityPolicy securityPolicy;
       
   211         CleanupClosePushL( securityPolicy );
       
   212         securityPolicy.Create( KThumbnailDatabaseSecurityPolicy );
       
   213 
       
   214         iDatabase.CreateL( pathPtr, securityPolicy, &config );
       
   215         CleanupStack::PopAndDestroy( &securityPolicy );
       
   216 
       
   217         TN_DEBUG1( "CThumbnailStore::ConstructL() -- 1 database created ok" );
       
   218                 
       
   219         RFile64 file;
       
   220         file.Create(iFs, mediaid, EFileShareReadersOrWriters );
       
   221         file.Close();
       
   222         newDatabase = ETrue;
       
   223         }
       
   224     else if ( err == KErrNone)
       
   225         {
       
   226         // db found, check version and rowids
       
   227         error = CheckVersionL();
       
   228         if(error == KErrNone)
       
   229             {
       
   230             error = CheckRowIDsL();
       
   231             }
       
   232         
       
   233         }
       
   234     
       
   235     // if wrong version, corrupted database or other error opening db
       
   236     if ( error == KErrNotSupported || (err != KErrNone && err != KErrNotFound) )
       
   237         {
       
   238         TN_DEBUG1( "CThumbnailStore::ConstructL() -- delete databases" );
       
   239         
       
   240         // delete db and create new
       
   241         iDatabase.Close();
       
   242         iDatabase.Delete(pathPtr);
       
   243         
       
   244         TN_DEBUG1( "CThumbnailStore::ConstructL() -- 2 creating database" );
       
   245         
       
   246         const TDesC8& config = KThumbnailSqlConfig;
       
   247 
       
   248         RSqlSecurityPolicy securityPolicy;
       
   249         CleanupClosePushL( securityPolicy );
       
   250         securityPolicy.Create( KThumbnailDatabaseSecurityPolicy );
       
   251 
       
   252         iDatabase.CreateL( pathPtr, securityPolicy, &config );
       
   253         CleanupStack::PopAndDestroy( &securityPolicy );
       
   254 
       
   255         TN_DEBUG1( "CThumbnailStore::ConstructL() -- 2 database created ok" );
       
   256         
       
   257         RFile64 file;
       
   258         file.Create(iFs, mediaid, EFileShareReadersOrWriters );
       
   259         file.Close();
       
   260         }
       
   261     else if(!newDatabase)
       
   262         {
       
   263         //check ownership
       
   264         if(CheckImeiL() != KErrNone)
       
   265             {
       
   266             ResetThumbnailIDs();
       
   267             
       
   268             //take ownership
       
   269             UpdateImeiL();
       
   270             
       
   271             //Remove blacklist markings
       
   272             TRAP_IGNORE( RemoveDbFlagL( KThumbnailDbFlagBlacklisted ) );
       
   273             }
       
   274         
       
   275         //check is MMC known
       
   276         if(CheckMediaIDL() != KErrNone )
       
   277             {
       
   278             ResetThumbnailIDs();
       
   279             
       
   280             //Remove blacklist markings
       
   281             TRAP_IGNORE( RemoveDbFlagL( KThumbnailDbFlagBlacklisted ) );
       
   282             }
       
   283         }
       
   284               
       
   285     CleanupStack::PopAndDestroy( databasePath );
       
   286     
       
   287     // add tables
       
   288     TRAPD(tableError, CreateTablesL() );
       
   289     
       
   290     if(!tableError)
       
   291         {
       
   292         AddVersionAndImeiL();
       
   293         }
       
   294     
       
   295     err = iDatabase.Exec( KThumbnailCreateTempInfoTable );
       
   296     TN_DEBUG2("CThumbnailStore::CreateTablesL() KThumbnailCreateTempInfoTable %d", err);
       
   297     User::LeaveIfError( err );
       
   298     err = iDatabase.Exec( KThumbnailCreateTempInfoDataTable );
       
   299     TN_DEBUG2("CThumbnailStore::CreateTablesL() KThumbnailCreateTempInfoDataTable %d", err);
       
   300     User::LeaveIfError( err );
       
   301     }
       
   302 
       
   303 
       
   304 // ---------------------------------------------------------------------------
       
   305 // CThumbnailStore::StoreThumbnailL()
       
   306 // Stores thumbnail image.
       
   307 // ---------------------------------------------------------------------------
       
   308 //
       
   309 void CThumbnailStore::StoreThumbnailL( const TDesC& aPath, const TDes8& aData,
       
   310     const TSize& aSize, const TSize& aOriginalSize, const TThumbnailFormat& aFormat, TInt aFlags, 
       
   311 	const TThumbnailSize& aThumbnailSize, TThumbnailId aThumbnailId, const TBool aThumbFromPath )
       
   312     {
       
   313     TN_DEBUG1( "CThumbnailStore::StoreThumbnailL( const TDes8& ) in" );
       
   314 
       
   315 #ifdef _DEBUG
       
   316     TTime aStart, aStop;
       
   317     aStart.UniversalTime();
       
   318 #endif
       
   319 
       
   320     //Encapsulate insert to Transaction
       
   321     RThumbnailTransaction transaction( iDatabase );
       
   322     CleanupClosePushL( transaction );
       
   323     transaction.BeginL();
       
   324     
       
   325     RSqlStatement stmt;
       
   326     CleanupClosePushL( stmt );
       
   327     // Insert into ThumbnailInfo
       
   328     User::LeaveIfError( stmt.Prepare( iDatabase, KThumbnailInsertThumbnailInfoByPathAndId ));
       
   329 
       
   330     TInt paramIndex = stmt.ParameterIndex( KThumbnailSqlParamPath );
       
   331     User::LeaveIfError( paramIndex );
       
   332     User::LeaveIfError( stmt.BindText( paramIndex, aPath ));
       
   333 
       
   334     paramIndex = stmt.ParameterIndex( KThumbnailSqlParamWidth );
       
   335     User::LeaveIfError( paramIndex );
       
   336     User::LeaveIfError( stmt.BindInt( paramIndex, aSize.iWidth ));
       
   337 
       
   338     paramIndex = stmt.ParameterIndex( KThumbnailSqlParamHeight );
       
   339     User::LeaveIfError( paramIndex );
       
   340     User::LeaveIfError( stmt.BindInt( paramIndex, aSize.iHeight ));
       
   341 
       
   342     paramIndex = stmt.ParameterIndex( KThumbnailSqlParamOriginalWidth );
       
   343     User::LeaveIfError( paramIndex );
       
   344     User::LeaveIfError( stmt.BindInt( paramIndex, aOriginalSize.iWidth ));
       
   345 
       
   346     paramIndex = stmt.ParameterIndex( KThumbnailSqlParamOriginalHeight );
       
   347     User::LeaveIfError( paramIndex );
       
   348     User::LeaveIfError( stmt.BindInt( paramIndex, aOriginalSize.iHeight ));
       
   349 
       
   350     paramIndex = stmt.ParameterIndex( KThumbnailSqlParamFormat );
       
   351     User::LeaveIfError( paramIndex );
       
   352     User::LeaveIfError( stmt.BindInt( paramIndex, aFormat ));
       
   353 
       
   354     paramIndex = stmt.ParameterIndex( KThumbnailSqlParamFlags );
       
   355     User::LeaveIfError( paramIndex );
       
   356     User::LeaveIfError( stmt.BindInt( paramIndex, aFlags ));
       
   357 
       
   358     paramIndex = stmt.ParameterIndex( KThumbnailSqlParamSize );
       
   359     User::LeaveIfError( paramIndex );
       
   360     User::LeaveIfError( stmt.BindInt( paramIndex, aThumbnailSize ));
       
   361     
       
   362     if( aThumbnailId > 0 )
       
   363         {
       
   364         paramIndex = stmt.ParameterIndex( KThumbnailSqlParamId );
       
   365         User::LeaveIfError( paramIndex );
       
   366         User::LeaveIfError( stmt.BindInt( paramIndex, aThumbnailId ));
       
   367         }
       
   368     else
       
   369         {
       
   370         TN_DEBUG1( "CThumbnailStore::StoreThumbnailL( ) aThumbnailId == 0" );
       
   371         }
       
   372     
       
   373     // orientation temporarily to 0
       
   374     paramIndex = stmt.ParameterIndex( KThumbnailSqlParamOrientation );
       
   375     User::LeaveIfError( paramIndex );
       
   376     User::LeaveIfError( stmt.BindInt( paramIndex, 0 ));
       
   377     
       
   378     // thumb from associated path
       
   379     TInt fromPath = aThumbFromPath;
       
   380     paramIndex = stmt.ParameterIndex( KThumbnailSqlParamThumbFromPath );
       
   381     User::LeaveIfError( paramIndex );
       
   382     User::LeaveIfError( stmt.BindInt( paramIndex, fromPath ));
       
   383     
       
   384     // try getting modification time from file
       
   385     TTime timeStamp;
       
   386     
       
   387     if (aPath.Length())
       
   388         {
       
   389         iFs.Modified(aPath, timeStamp);
       
   390         }
       
   391     else
       
   392         {
       
   393         // otherwise current time
       
   394         timeStamp.UniversalTime();
       
   395         }
       
   396     
       
   397     paramIndex = stmt.ParameterIndex( KThumbnailSqlParamModified );
       
   398     User::LeaveIfError( paramIndex );
       
   399     User::LeaveIfError( stmt.BindInt64( paramIndex, timeStamp.Int64() ));
       
   400     
       
   401     User::LeaveIfError( stmt.Exec());
       
   402     CleanupStack::PopAndDestroy( &stmt );
       
   403     
       
   404     RSqlStatement stmtData;
       
   405     CleanupClosePushL( stmtData );
       
   406     // Insert into ThumbnailInfoData
       
   407     TInt err = stmtData.Prepare( iDatabase, KThumbnailInsertTempThumbnailInfoData );
       
   408        
       
   409 #ifdef _DEBUG
       
   410     TPtrC errorMsg = iDatabase.LastErrorMessage();
       
   411     TN_DEBUG2( "CThumbnailStore::FetchThumbnailL() KThumbnailInsertTempThumbnailInfoData %S" , &errorMsg);
       
   412 #endif    
       
   413     User::LeaveIfError( err );
       
   414     
       
   415     paramIndex = stmtData.ParameterIndex( KThumbnailSqlParamData );
       
   416     User::LeaveIfError( paramIndex );
       
   417     User::LeaveIfError( stmtData.BindBinary( paramIndex, aData ));
       
   418 
       
   419     User::LeaveIfError( stmtData.Exec());
       
   420     CleanupStack::PopAndDestroy( &stmtData );
       
   421 	
       
   422     // Commit transaction
       
   423     transaction.CommitL();
       
   424     CleanupStack::PopAndDestroy( &transaction );
       
   425 
       
   426     iBatchItemCount++;
       
   427     
       
   428     FlushCacheTable();
       
   429 
       
   430 #ifdef _DEBUG
       
   431     iThumbCounter++;
       
   432     TN_DEBUG2( "CThumbnailStore::THUMBSTORE-COUNTER----------, Thumbs = %d", iThumbCounter );
       
   433     
       
   434     aStop.UniversalTime();
       
   435     TN_DEBUG2( "CThumbnailStore::StoreThumbnailL() insert to table %d ms", (TInt)aStop.MicroSecondsFrom(aStart).Int64()/1000);
       
   436 #endif 
       
   437     TN_DEBUG1( "CThumbnailStore::StoreThumbnailL( const TDes8& ) out" );
       
   438     }
       
   439 
       
   440 
       
   441 // ---------------------------------------------------------------------------
       
   442 // CThumbnailStore::StoreThumbnailL()
       
   443 // Stores thumbnail image.
       
   444 // ---------------------------------------------------------------------------
       
   445 //
       
   446 void CThumbnailStore::StoreThumbnailL( const TDesC& aPath, CFbsBitmap*
       
   447     aThumbnail, const TSize& aOriginalSize, TBool /*aCropped*/, const TThumbnailSize aThumbnailSize, 
       
   448     const TThumbnailId aThumbnailId, const TBool aThumbFromPath, TBool aBlackListed )
       
   449     {
       
   450     TN_DEBUG1( "CThumbnailStore::StoreThumbnailL( CFbsBitmap* ) in" );
       
   451 
       
   452     __ASSERT_DEBUG(( aThumbnail ), ThumbnailPanic( EThumbnailNullPointer ));
       
   453 
       
   454     // check for duplicates
       
   455     TBool exists = FindDuplicateL(aPath, aThumbnailId, aThumbnailSize);
       
   456     
       
   457     TSize thumbSize = aThumbnail->SizeInPixels();
       
   458     for ( TInt i = iPersistentSizes.Count(); --i >= 0; )
       
   459         {
       
   460         TThumbnailPersistentSize & persistentSize = iPersistentSizes[i];
       
   461         
       
   462         // don't store duplicates or custom sizes
       
   463         if ( !exists && (aThumbnailSize != ECustomThumbnailSize && 
       
   464                          thumbSize.iWidth > 0 && thumbSize.iHeight > 0 ))
       
   465             {
       
   466             TInt flags = 0;
       
   467             if ( persistentSize.iCrop )
       
   468                 {
       
   469                 flags |= KThumbnailDbFlagCropped;
       
   470                 }
       
   471             
       
   472             if( aBlackListed )
       
   473                 {
       
   474                 flags |= KThumbnailDbFlagBlacklisted;
       
   475                 }
       
   476             
       
   477             if( (aThumbnailSize == EImageFullScreenThumbnailSize || aThumbnailSize == EVideoFullScreenThumbnailSize ||
       
   478                  aThumbnailSize == EAudioFullScreenThumbnailSize) && !aBlackListed )
       
   479                 {
       
   480                 HBufC8* data = NULL;
       
   481                 CImageEncoder* iEncoder = CImageEncoder::DataNewL( data,  KJpegMime(), CImageEncoder::EOptionAlwaysThread );
       
   482                 TJpegImageData* imageData = new (ELeave) TJpegImageData();
       
   483              
       
   484                 // Set some format specific data
       
   485                 imageData->iSampleScheme = TJpegImageData::EColor444;
       
   486                 imageData->iQualityFactor = 75; //?
       
   487              
       
   488                 CFrameImageData* iFrameImageData = CFrameImageData::NewL();
       
   489              
       
   490                 // frameData - ownership passed to iFrameImageData after AppendImageData
       
   491                 User::LeaveIfError(iFrameImageData->AppendImageData(imageData));
       
   492                 
       
   493 #ifdef _DEBUG
       
   494         TN_DEBUG4( "CThumbnailStore::StoreThumbnailL() size %d x %d displaymode %d ", 
       
   495                 aThumbnail->SizeInPixels().iWidth, 
       
   496                 aThumbnail->SizeInPixels().iHeight, 
       
   497                 aThumbnail->DisplayMode());
       
   498 #endif
       
   499                 
       
   500                 TRequestStatus request;
       
   501                 iEncoder->Convert( &request, *aThumbnail, iFrameImageData);
       
   502                 User::WaitForRequest( request);
       
   503                   
       
   504                 if(request== KErrNone)
       
   505                   {
       
   506                   TPtr8 ptr  = data->Des(); 
       
   507                   StoreThumbnailL( aPath, ptr, aThumbnail->SizeInPixels(), aOriginalSize,
       
   508                           EThumbnailFormatJpeg, flags, aThumbnailSize, aThumbnailId, aThumbFromPath  );
       
   509                   }
       
   510              
       
   511                 delete iFrameImageData;
       
   512                 iFrameImageData = NULL;          
       
   513                 
       
   514                 delete iEncoder;
       
   515                 iEncoder = NULL;    
       
   516                 delete data;
       
   517                 data = NULL;                
       
   518                 }
       
   519             else
       
   520                 {
       
   521                 CBufFlat* buf = CBufFlat::NewL( KStreamBufferSize );
       
   522                 CleanupStack::PushL( buf );
       
   523                 RBufWriteStream stream;
       
   524                 stream.Open( *buf );
       
   525                 aThumbnail->ExternalizeL( stream );
       
   526             
       
   527                 StoreThumbnailL( aPath, buf->Ptr( 0 ), aThumbnail->SizeInPixels(),
       
   528                     aOriginalSize, EThumbnailFormatFbsBitmap, flags, aThumbnailSize, aThumbnailId );
       
   529   
       
   530                 CleanupStack::PopAndDestroy( buf );
       
   531                 }
       
   532             
       
   533             break;
       
   534             }
       
   535         }
       
   536     
       
   537     TN_DEBUG1( "CThumbnailStore::StoreThumbnailL( CFbsBitmap* ) out" );
       
   538     }
       
   539 
       
   540 
       
   541 // ---------------------------------------------------------------------------
       
   542 // Finds possible existing duplicate thumbnail.
       
   543 // ---------------------------------------------------------------------------
       
   544 //
       
   545 TBool CThumbnailStore::FindDuplicateL( const TDesC& aPath, const TThumbnailId aThumbnailId,
       
   546                                        const TThumbnailSize& aThumbnailSize )
       
   547     {
       
   548     TN_DEBUG1( "CThumbnailStore::FindDuplicateL()" );
       
   549     
       
   550     TInt rowStatus = 0;
       
   551     TInt paramIndex = 0;
       
   552     TInt found = EFalse;
       
   553     
       
   554     RSqlStatement stmt;
       
   555     CleanupClosePushL( stmt );     
       
   556     
       
   557     User::LeaveIfError( stmt.Prepare( iDatabase, KTempFindDuplicate ));
       
   558     paramIndex = stmt.ParameterIndex( KThumbnailSqlParamPath );
       
   559     User::LeaveIfError( paramIndex );
       
   560     User::LeaveIfError( stmt.BindText( paramIndex, aPath ));
       
   561         
       
   562     paramIndex = stmt.ParameterIndex( KThumbnailSqlParamId );
       
   563     User::LeaveIfError( paramIndex );
       
   564     User::LeaveIfError( stmt.BindInt( paramIndex, aThumbnailId ));
       
   565     
       
   566     paramIndex = stmt.ParameterIndex( KThumbnailSqlParamSize );
       
   567     User::LeaveIfError( paramIndex );
       
   568     User::LeaveIfError( stmt.BindInt( paramIndex, aThumbnailSize ));
       
   569     
       
   570     rowStatus = stmt.Next();
       
   571     
       
   572     //if not found from temp table, look from real table
       
   573     if(rowStatus != KSqlAtRow)
       
   574         {
       
   575         stmt.Close();
       
   576         CleanupStack::PopAndDestroy( &stmt );
       
   577         CleanupClosePushL( stmt );
       
   578            
       
   579         User::LeaveIfError( stmt.Prepare( iDatabase, KFindDuplicate ));
       
   580         paramIndex = stmt.ParameterIndex( KThumbnailSqlParamPath );
       
   581         User::LeaveIfError( paramIndex );
       
   582         User::LeaveIfError( stmt.BindText( paramIndex, aPath ));
       
   583             
       
   584         paramIndex = stmt.ParameterIndex( KThumbnailSqlParamId );
       
   585         User::LeaveIfError( paramIndex );
       
   586         User::LeaveIfError( stmt.BindInt( paramIndex, aThumbnailId ));
       
   587         
       
   588         paramIndex = stmt.ParameterIndex( KThumbnailSqlParamSize );
       
   589         User::LeaveIfError( paramIndex );
       
   590         User::LeaveIfError( stmt.BindInt( paramIndex, aThumbnailSize ));
       
   591         
       
   592         rowStatus = stmt.Next();
       
   593         
       
   594         if(rowStatus == KSqlAtRow)
       
   595             {
       
   596             TN_DEBUG1( "CThumbnailStore::FindDuplicateL() - duplicate in main table" );
       
   597             
       
   598             found = ETrue;
       
   599             }
       
   600         else
       
   601             {
       
   602             TN_DEBUG1( "CThumbnailStore::FindDuplicateL() - no duplicate" );
       
   603             }
       
   604         }
       
   605     else
       
   606         {
       
   607         TN_DEBUG1( "CThumbnailStore::FindDuplicateL() - duplicate in temp table" );
       
   608         
       
   609         found = ETrue;
       
   610         }
       
   611     
       
   612     stmt.Close();
       
   613     CleanupStack::PopAndDestroy( &stmt );
       
   614     
       
   615     return found;
       
   616     }
       
   617 
       
   618 
       
   619 // ---------------------------------------------------------------------------
       
   620 // Create database tables
       
   621 // ---------------------------------------------------------------------------
       
   622 //
       
   623 void CThumbnailStore::CreateTablesL()
       
   624     {
       
   625     TN_DEBUG1( "CThumbnailStore::CreateTablesL()" );
       
   626     TInt err = 0;
       
   627     err = iDatabase.Exec( KThumbnailCreateInfoTable );
       
   628     TN_DEBUG2( "CThumbnailStore::CreateTablesL() KThumbnailCreateInfoTable err=%d", err );
       
   629     err = iDatabase.Exec( KThumbnailCreateInfoDataTable );
       
   630     TN_DEBUG2( "CThumbnailStore::CreateTablesL() KThumbnailCreateInfoDataTable err=%d", err );
       
   631     err = iDatabase.Exec( KThumbnailCreateInfoTableIndex1 );
       
   632     TN_DEBUG2( "CThumbnailStore::CreateTablesL() KThumbnailCreateInfoTableIndex1 err=%d", err );
       
   633     err = iDatabase.Exec( KThumbnailCreateInfoTableIndex2 );
       
   634     TN_DEBUG2( "CThumbnailStore::CreateTablesL() KThumbnailCreateInfoTableIndex2 err=%d", err );
       
   635     err = iDatabase.Exec(KThumbnailVersionTable);
       
   636     TN_DEBUG2( "CThumbnailStore::CreateTablesL() KThumbnailVersionTable err=%d", err );
       
   637     User::LeaveIfError( err );
       
   638     }
       
   639 
       
   640 
       
   641 // ---------------------------------------------------------------------------
       
   642 // Get missing sizes by Path
       
   643 // ---------------------------------------------------------------------------
       
   644 //     
       
   645 void CThumbnailStore::GetMissingSizesAndIDsL( const TDesC& aPath, TInt aSourceType, RArray <
       
   646     TThumbnailPersistentSize > & aMissingSizes, TBool& aMissingIDs )
       
   647     {
       
   648     TN_DEBUG2( "CThumbnailStore::GetMissingSizesAndIDsL() aSourceType == %d", aSourceType );
       
   649     // define sizes to be checked
       
   650     const TInt count = iPersistentSizes.Count();
       
   651     
       
   652     for ( TInt i = 0 ; i < count; i++ )
       
   653         {
       
   654         if ( iPersistentSizes[ i ].iSourceType == aSourceType && iPersistentSizes[ i ].iAutoCreate)
       
   655             {
       
   656             aMissingSizes.Append( iPersistentSizes[ i ] );
       
   657             }
       
   658         }
       
   659     
       
   660     TInt missingSizeCount = aMissingSizes.Count();
       
   661     aMissingIDs = EFalse;
       
   662         
       
   663     TN_DEBUG3( "CThumbnailStore::GetMissingSizesAndIDsL() missingSizeCount == %d, missingIDs == %d", missingSizeCount, aMissingIDs );
       
   664     
       
   665     // check temp table first
       
   666     RSqlStatement stmt;
       
   667     CleanupClosePushL( stmt );
       
   668     User::LeaveIfError( stmt.Prepare( iDatabase, KThumbnailSelectTempSizeByPath ));
       
   669     TInt paramIndex = stmt.ParameterIndex( KThumbnailSqlParamPath );
       
   670     User::LeaveIfError( paramIndex );
       
   671     User::LeaveIfError( stmt.BindText( paramIndex, aPath ));
       
   672        
       
   673     TInt rowStatus = stmt.Next();
       
   674 
       
   675     TInt round = 1;
       
   676     TInt size = 0;
       
   677     TInt id = 0;
       
   678     
       
   679     while (round <= 2)
       
   680         {
       
   681         while ( rowStatus == KSqlAtRow && missingSizeCount > 0 )
       
   682             {
       
   683             size = stmt.ColumnInt( 0 );
       
   684             id = stmt.ColumnInt( 1 );
       
   685 			
       
   686 			TN_DEBUG2( "CThumbnailStore::GetMissingSizesAndIDsL() id == %d", id );
       
   687             
       
   688 			//if TNId is not valid mark that some are missing so that UpdateDb is run later
       
   689             if ( id <= 0)
       
   690                 {
       
   691                 TN_DEBUG1( "CThumbnailStore::GetMissingSizesAndIDsL() missing ID");
       
   692                 aMissingIDs = ETrue;
       
   693                 }
       
   694             
       
   695             missingSizeCount = aMissingSizes.Count();
       
   696             for ( TInt i = 0; i < missingSizeCount; i++ )
       
   697                 {
       
   698                 if ( aMissingSizes[ i ].iType == size )
       
   699                     {
       
   700                     TN_DEBUG1( "CThumbnailStore::GetMissingSizesL() -- thumbnail found" );
       
   701                     aMissingSizes.Remove( i );
       
   702                     missingSizeCount--;
       
   703                     break;
       
   704                     }
       
   705                 }
       
   706                 
       
   707             rowStatus = stmt.Next();
       
   708             }
       
   709         stmt.Close();
       
   710         CleanupStack::PopAndDestroy( &stmt );
       
   711         
       
   712         // all found
       
   713         if (missingSizeCount == 0)
       
   714             {
       
   715             return;
       
   716             }
       
   717         else if (round == 1)
       
   718             {
       
   719             // change to real table
       
   720             CleanupClosePushL( stmt );
       
   721             User::LeaveIfError( stmt.Prepare( iDatabase, KThumbnailSelectSizeByPath ));
       
   722             paramIndex = stmt.ParameterIndex( KThumbnailSqlParamPath );
       
   723             User::LeaveIfError( paramIndex );
       
   724             User::LeaveIfError( stmt.BindText( paramIndex, aPath ));
       
   725             rowStatus = stmt.Next();    
       
   726             }
       
   727         
       
   728         round++;
       
   729         }
       
   730     }
       
   731 
       
   732 
       
   733 // ---------------------------------------------------------------------------
       
   734 // CThumbnailStore::FetchThumbnailL()
       
   735 // Fetches thumbnail image.
       
   736 // ---------------------------------------------------------------------------
       
   737 //
       
   738 TInt CThumbnailStore::FetchThumbnailL( TThumbnailId aThumbnailId, 
       
   739         CFbsBitmap*& aThumbnail, TDesC8* & aData, TThumbnailSize aThumbnailSize, TSize &aThumbnailRealSize )
       
   740     {
       
   741     TN_DEBUG3( "CThumbnailStore::FetchThumbnailL(%d) aThumbnailSize == %d", aThumbnailId, aThumbnailSize );
       
   742      delete aThumbnail;
       
   743      aThumbnail = NULL;
       
   744 
       
   745      RSqlStatement stmt;
       
   746      CleanupClosePushL( stmt );
       
   747      
       
   748      TInt paramIndex = 0;
       
   749      TInt found = KErrNotFound;
       
   750      TInt rowStatus = 0;
       
   751      TInt column = 0;
       
   752      TInt count = 0;
       
   753      TThumbnailSize thumbnailImage = EUnknownThumbnailSize;
       
   754      TThumbnailSize thumbnailVideo = EUnknownThumbnailSize;
       
   755      TThumbnailSize thumbnailAudio = EUnknownThumbnailSize;
       
   756      TBool inTempTable( ETrue );
       
   757      
       
   758      if(aThumbnailSize == EFullScreenThumbnailSize)
       
   759          {
       
   760          thumbnailImage = EImageFullScreenThumbnailSize;
       
   761          thumbnailVideo = EVideoFullScreenThumbnailSize;
       
   762          thumbnailAudio = EAudioFullScreenThumbnailSize;
       
   763          }
       
   764      else if(aThumbnailSize == EGridThumbnailSize)
       
   765          {
       
   766          thumbnailImage = EImageGridThumbnailSize;
       
   767          thumbnailVideo = EVideoGridThumbnailSize;
       
   768          thumbnailAudio = EAudioGridThumbnailSize;
       
   769          }
       
   770      else if(aThumbnailSize == EListThumbnailSize)
       
   771          {
       
   772          thumbnailImage = EImageListThumbnailSize;
       
   773          thumbnailVideo = EVideoListThumbnailSize;
       
   774          thumbnailAudio = EAudioListThumbnailSize;
       
   775          }
       
   776      
       
   777      if(aThumbnailSize == EFullScreenThumbnailSize ||
       
   778         aThumbnailSize == EGridThumbnailSize ||
       
   779         aThumbnailSize == EListThumbnailSize )
       
   780          {
       
   781          TN_DEBUG1( "CThumbnailStore::FetchThumbnailL() -- No DataType -- TEMP TABLE lookup" );
       
   782          User::LeaveIfError( stmt.Prepare( iDatabase, KThumbnailSelectTempInfoByIdv2 ));
       
   783          paramIndex = stmt.ParameterIndex( KThumbnailSqlParamId );
       
   784          User::LeaveIfError( paramIndex );
       
   785          User::LeaveIfError( stmt.BindInt( paramIndex, aThumbnailId ));
       
   786              
       
   787          paramIndex = stmt.ParameterIndex( KThumbnailSqlParamSizeImage );
       
   788          User::LeaveIfError( paramIndex );
       
   789          User::LeaveIfError( stmt.BindInt( paramIndex, thumbnailImage ));
       
   790          
       
   791          paramIndex = stmt.ParameterIndex( KThumbnailSqlParamSizeVideo );
       
   792          User::LeaveIfError( paramIndex );
       
   793          User::LeaveIfError( stmt.BindInt( paramIndex, thumbnailVideo ));
       
   794          
       
   795          paramIndex = stmt.ParameterIndex( KThumbnailSqlParamSizeAudio );
       
   796          User::LeaveIfError( paramIndex );
       
   797          User::LeaveIfError( stmt.BindInt( paramIndex, thumbnailAudio ));
       
   798              
       
   799          rowStatus = stmt.Next();
       
   800          //if not found from temp table, look from real table
       
   801          if(rowStatus != KSqlAtRow)
       
   802            {
       
   803            TN_DEBUG1( "CThumbnailStore::FetchThumbnailL() -- No DataType -- MAIN TABLE lookup" );
       
   804            inTempTable = EFalse;
       
   805            stmt.Close();
       
   806            CleanupStack::PopAndDestroy( &stmt );
       
   807            CleanupClosePushL( stmt );
       
   808                 
       
   809            User::LeaveIfError( stmt.Prepare( iDatabase, KThumbnailSelectInfoByIdv2 ));
       
   810              
       
   811            paramIndex = stmt.ParameterIndex( KThumbnailSqlParamId );
       
   812            User::LeaveIfError( paramIndex );
       
   813            User::LeaveIfError( stmt.BindInt( paramIndex, aThumbnailId ));
       
   814                  
       
   815            paramIndex = stmt.ParameterIndex( KThumbnailSqlParamSizeImage );
       
   816            User::LeaveIfError( paramIndex );
       
   817            User::LeaveIfError( stmt.BindInt( paramIndex, thumbnailImage ));
       
   818                     
       
   819            paramIndex = stmt.ParameterIndex( KThumbnailSqlParamSizeVideo );
       
   820            User::LeaveIfError( paramIndex );
       
   821            User::LeaveIfError( stmt.BindInt( paramIndex, thumbnailVideo ));
       
   822                     
       
   823            paramIndex = stmt.ParameterIndex( KThumbnailSqlParamSizeAudio );
       
   824            User::LeaveIfError( paramIndex );
       
   825            User::LeaveIfError( stmt.BindInt( paramIndex, thumbnailAudio ));
       
   826              
       
   827            rowStatus = stmt.Next();
       
   828            }               
       
   829          }
       
   830      else
       
   831          {
       
   832          TN_DEBUG1( "CThumbnailStore::FetchThumbnailL() -- TEMP TABLE lookup" );
       
   833          User::LeaveIfError( stmt.Prepare( iDatabase, KThumbnailSelectTempInfoById ));
       
   834          paramIndex = stmt.ParameterIndex( KThumbnailSqlParamId );
       
   835          User::LeaveIfError( paramIndex );
       
   836          User::LeaveIfError( stmt.BindInt( paramIndex, aThumbnailId ));
       
   837      
       
   838          paramIndex = stmt.ParameterIndex( KThumbnailSqlParamSize );
       
   839          User::LeaveIfError( paramIndex );
       
   840          User::LeaveIfError( stmt.BindInt( paramIndex, aThumbnailSize ));
       
   841      
       
   842          rowStatus = stmt.Next();
       
   843 
       
   844          //if not found from temp table, look from real table
       
   845          if(rowStatus != KSqlAtRow)
       
   846             {
       
   847             TN_DEBUG1( "CThumbnailStore::FetchThumbnailL() -- MAIN TABLE lookup" );
       
   848             inTempTable = EFalse;
       
   849             stmt.Close();
       
   850             CleanupStack::PopAndDestroy( &stmt );
       
   851             CleanupClosePushL( stmt );
       
   852         
       
   853             User::LeaveIfError( stmt.Prepare( iDatabase, KThumbnailSelectInfoById ));
       
   854      
       
   855             paramIndex = stmt.ParameterIndex( KThumbnailSqlParamId );
       
   856             User::LeaveIfError( paramIndex );
       
   857             User::LeaveIfError( stmt.BindInt( paramIndex, aThumbnailId ));
       
   858          
       
   859             paramIndex = stmt.ParameterIndex( KThumbnailSqlParamSize );
       
   860             User::LeaveIfError( paramIndex );
       
   861             User::LeaveIfError( stmt.BindInt( paramIndex, aThumbnailSize ));
       
   862      
       
   863             rowStatus = stmt.Next();
       
   864             }
       
   865          }
       
   866      if(rowStatus == KSqlAtRow)
       
   867         {
       
   868          TN_DEBUG1( "CThumbnailStore::FetchThumbnailL() -- thumbnail found" );
       
   869          // Check whether blacklisted thumbnail entry modified. 
       
   870          // If thumbnail is marked as blacklisted and timestamp has 
       
   871          // changed, delete thumbnails from tables and leave with 
       
   872          // KErrNotFound to get thumbnail regenerated.
       
   873          column = 4;
       
   874          TInt flags = stmt.ColumnInt( column );
       
   875          if( flags & KThumbnailDbFlagBlacklisted )
       
   876              {
       
   877              TBool modified = EFalse;
       
   878              CheckModifiedByIdL( aThumbnailId, inTempTable, modified );
       
   879              if( modified )
       
   880                  {
       
   881                  // Close db to get deletion of thumbnails executed.
       
   882                  stmt.Close();
       
   883                  CleanupStack::PopAndDestroy( &stmt );
       
   884                  DeleteThumbnailsL( aThumbnailId );
       
   885                  User::Leave( KErrNotFound );
       
   886                  }
       
   887              else
       
   888                  {
       
   889                  User::Leave( KErrCompletion );
       
   890                  }
       
   891              }
       
   892          
       
   893          found = KErrNone;
       
   894          count = 0;
       
   895          count++;
       
   896          column = 0;
       
   897          TInt format = stmt.ColumnInt( column++ );  
       
   898          if(format == 1 /*TThumbnailFormat::EThumbnailFormatJpeg */ )
       
   899              {
       
   900              TPtrC8 ptr = stmt.ColumnBinaryL( column++ );
       
   901              HBufC8* data = ptr.AllocL() ;
       
   902              aThumbnail = NULL;
       
   903              aData = data;     
       
   904              
       
   905           } else {
       
   906              TPtrC8 ptr = stmt.ColumnBinaryL( column );
       
   907              RDesReadStream stream( ptr );
       
   908              aThumbnail = new( ELeave )CFbsBitmap();
       
   909              aThumbnail->InternalizeL( stream );
       
   910              aData = NULL;
       
   911           }
       
   912          
       
   913         //fetch real size of TN
       
   914         column = 2;
       
   915         aThumbnailRealSize.iWidth = stmt.ColumnInt( column++ );
       
   916         aThumbnailRealSize.iHeight = stmt.ColumnInt( column );
       
   917         }
       
   918      else
       
   919          {
       
   920          TN_DEBUG1( "CThumbnailStore::FetchThumbnailL() -- thumbnail NOT found" );
       
   921          }
       
   922          
       
   923      stmt.Close();
       
   924      CleanupStack::PopAndDestroy( &stmt );
       
   925 
       
   926      User::LeaveIfError( found );
       
   927      return found;
       
   928     }
       
   929 
       
   930 
       
   931 // ---------------------------------------------------------------------------
       
   932 // CThumbnailStore::FetchThumbnailL()
       
   933 // Fetches thumbnail image.
       
   934 // ---------------------------------------------------------------------------
       
   935 //
       
   936 void CThumbnailStore::FetchThumbnailL( const TDesC& aPath, CFbsBitmap* &
       
   937     aThumbnail, TDesC8* & aData, const TThumbnailSize aThumbnailSize, TSize &aThumbnailRealSize )
       
   938     {
       
   939     TN_DEBUG3( "CThumbnailStore::FetchThumbnailL(%S) aThumbnailSize==%d", &aPath, aThumbnailSize );
       
   940     delete aThumbnail;
       
   941     aThumbnail = NULL;
       
   942 
       
   943     RSqlStatement stmt;
       
   944     CleanupClosePushL( stmt );
       
   945     
       
   946     TInt paramIndex = 0;
       
   947     TInt found = KErrNotFound;
       
   948     TInt rowStatus = 0;
       
   949     TInt column = 0;
       
   950     TBool inTempTable = ETrue;
       
   951     
       
   952     TN_DEBUG1( "CThumbnailStore::FetchThumbnailL() -- TEMP TABLE lookup" );
       
   953     TInt err = stmt.Prepare( iDatabase, KThumbnailSelectTempInfoByPath );
       
   954 
       
   955 #ifdef _DEBUG
       
   956     TPtrC errorMsg = iDatabase.LastErrorMessage();
       
   957     TN_DEBUG2( "CThumbnailStore::FetchThumbnailL() %S" , &errorMsg);
       
   958 #endif
       
   959     User::LeaveIfError( err );
       
   960     
       
   961     paramIndex = stmt.ParameterIndex( KThumbnailSqlParamPath );
       
   962     User::LeaveIfError( paramIndex );
       
   963     User::LeaveIfError( stmt.BindText( paramIndex, aPath ));
       
   964     
       
   965     paramIndex = stmt.ParameterIndex( KThumbnailSqlParamSize );
       
   966     User::LeaveIfError( paramIndex );
       
   967     User::LeaveIfError( stmt.BindInt( paramIndex, aThumbnailSize ));
       
   968     
       
   969     rowStatus = stmt.Next();
       
   970 
       
   971     //if not found from temp table, look from real table
       
   972     if(rowStatus != KSqlAtRow)
       
   973        {
       
   974        TN_DEBUG1( "CThumbnailStore::FetchThumbnailL() -- MAIN TABLE lookup" );
       
   975        inTempTable = EFalse;
       
   976        stmt.Close();
       
   977        CleanupStack::PopAndDestroy( &stmt );
       
   978        CleanupClosePushL( stmt );
       
   979        
       
   980        User::LeaveIfError( stmt.Prepare( iDatabase, KThumbnailSelectInfoByPath ));
       
   981     
       
   982         paramIndex = stmt.ParameterIndex( KThumbnailSqlParamPath );
       
   983         User::LeaveIfError( paramIndex );
       
   984         User::LeaveIfError( stmt.BindText( paramIndex, aPath ));
       
   985         
       
   986         paramIndex = stmt.ParameterIndex( KThumbnailSqlParamSize );
       
   987         User::LeaveIfError( paramIndex );
       
   988         User::LeaveIfError( stmt.BindInt( paramIndex, aThumbnailSize ));
       
   989     
       
   990         rowStatus = stmt.Next();
       
   991        }
       
   992 
       
   993     if(rowStatus == KSqlAtRow)
       
   994        {
       
   995         TN_DEBUG1( "CThumbnailStore::FetchThumbnailL() -- thumbnail found" );
       
   996         // Check whether blacklisted thumbnail entry modified. 
       
   997         // If thumbnail is marked as blacklisted and timestamp has 
       
   998         // changed, delete thumbnails from tables and leave with 
       
   999         // KErrNotFound to get thumbnail regenerated.
       
  1000         column = 4;
       
  1001         TInt flags = stmt.ColumnInt( column );
       
  1002         if( flags & KThumbnailDbFlagBlacklisted && aPath.Length() )
       
  1003             {
       
  1004             TBool modified = EFalse;
       
  1005             CheckModifiedByPathL( aPath, inTempTable, modified );
       
  1006             if( modified )
       
  1007                 {
       
  1008                 // Close db to get deletion of thumbnails executed.
       
  1009                 stmt.Close();
       
  1010                 CleanupStack::PopAndDestroy( &stmt );
       
  1011                 DeleteThumbnailsL( aPath );
       
  1012                 User::Leave( KErrNotFound );
       
  1013                 }
       
  1014             else
       
  1015                 {
       
  1016                 User::Leave( KErrCompletion );
       
  1017                 }
       
  1018             }
       
  1019         else if( !(flags & KThumbnailDbFlagBlacklisted) )
       
  1020             {
       
  1021             found = KErrNone;
       
  1022             column = 0;
       
  1023             TInt format = stmt.ColumnInt( column++ );  
       
  1024             if(format == 1 /*TThumbnailFormat::EThumbnailFormatJpeg */ )
       
  1025                {
       
  1026                TPtrC8 ptr = stmt.ColumnBinaryL( column++ );
       
  1027                HBufC8* data = ptr.AllocL() ;
       
  1028                aThumbnail = NULL;
       
  1029                aData = data;
       
  1030                
       
  1031             } else {
       
  1032     
       
  1033                TPtrC8 ptr = stmt.ColumnBinaryL( column++ );
       
  1034                RDesReadStream stream( ptr );
       
  1035                aThumbnail = new( ELeave )CFbsBitmap();
       
  1036                aThumbnail->InternalizeL( stream );
       
  1037                aData = NULL;
       
  1038                }
       
  1039             
       
  1040             //fetch real size of TN
       
  1041             column = 2;
       
  1042             aThumbnailRealSize.iWidth = stmt.ColumnInt( column++ );
       
  1043             aThumbnailRealSize.iHeight = stmt.ColumnInt( column );
       
  1044             }
       
  1045         }
       
  1046     else
       
  1047         {
       
  1048         TN_DEBUG1( "CThumbnailStore::FetchThumbnailL() -- thumbnail NOT found" );
       
  1049         }
       
  1050         
       
  1051     stmt.Close();
       
  1052     CleanupStack::PopAndDestroy( &stmt );
       
  1053 
       
  1054     User::LeaveIfError( found );
       
  1055     }
       
  1056 
       
  1057 // -----------------------------------------------------------------------------
       
  1058 // Delete thumbnails for given object file by Path
       
  1059 // -----------------------------------------------------------------------------
       
  1060 //
       
  1061 void CThumbnailStore::DeleteThumbnailsL( const TDesC& aPath )
       
  1062     {
       
  1063     RThumbnailTransaction transaction( iDatabase );
       
  1064     CleanupClosePushL( transaction );
       
  1065     transaction.BeginL();
       
  1066     RSqlStatement stmt;
       
  1067     CleanupClosePushL( stmt );
       
  1068     
       
  1069     TInt paramIndex = 0;
       
  1070     TInt paramIndex1 = 0;
       
  1071     TInt paramIndex2 = 0;
       
  1072     TInt rowStatus = 0;
       
  1073     TInt column = 0;
       
  1074     TInt rowid = 0;
       
  1075     TInt deleteCount = 0;
       
  1076     
       
  1077     TN_DEBUG1( "CThumbnailStore::DeleteThumbnailsByPathL() -- TEMP TABLE lookup" );
       
  1078     TInt err = stmt.Prepare( iDatabase, KTempThumbnailSqlSelectRowIDInfoByPath);
       
  1079     User::LeaveIfError( err );
       
  1080     
       
  1081     paramIndex = stmt.ParameterIndex( KThumbnailSqlParamPath );
       
  1082     User::LeaveIfError( paramIndex );
       
  1083     User::LeaveIfError( stmt.BindText( paramIndex, aPath ));
       
  1084     
       
  1085     rowStatus = stmt.Next();
       
  1086     RSqlStatement stmt_info;
       
  1087     CleanupClosePushL( stmt_info );
       
  1088     err = stmt_info.Prepare( iDatabase, KTempThumbnailSqlDeleteInfoByPath);
       
  1089     RSqlStatement stmt_infodata;
       
  1090     CleanupClosePushL( stmt_infodata );
       
  1091     err = stmt_infodata.Prepare( iDatabase, KTempThumbnailSqlDeleteInfoDataByPath);
       
  1092     
       
  1093     
       
  1094     while(rowStatus == KSqlAtRow)
       
  1095        {  
       
  1096        rowid = stmt.ColumnInt( column ); 
       
  1097        paramIndex1 = stmt_info.ParameterIndex( KThumbnailSqlParamRowID );
       
  1098        User::LeaveIfError( paramIndex1 );
       
  1099        User::LeaveIfError( stmt_info.BindInt( paramIndex1, rowid ));
       
  1100        
       
  1101        deleteCount = stmt_info.Exec();
       
  1102        stmt_info.Reset();
       
  1103        User::LeaveIfError( deleteCount );
       
  1104              
       
  1105        paramIndex2 = stmt_infodata.ParameterIndex( KThumbnailSqlParamRowID );
       
  1106        User::LeaveIfError( paramIndex2 );
       
  1107        User::LeaveIfError( stmt_infodata.BindInt( paramIndex2, rowid ));
       
  1108              
       
  1109        deleteCount = stmt_infodata.Exec();
       
  1110        stmt_infodata.Reset();
       
  1111        User::LeaveIfError( deleteCount );
       
  1112        
       
  1113        TN_DEBUG1( "CThumbnailStore::DeleteThumbnailsByPathL() -- TEMP TABLE lookup - thumbnail deleted" );
       
  1114        
       
  1115        // fetch another row (temp table rowIDs are updated immediately)
       
  1116        stmt.Reset();
       
  1117        
       
  1118        paramIndex = stmt.ParameterIndex( KThumbnailSqlParamPath );
       
  1119        User::LeaveIfError( paramIndex );
       
  1120        User::LeaveIfError( stmt.BindText( paramIndex, aPath ));
       
  1121        
       
  1122        rowStatus = stmt.Next();   
       
  1123        }
       
  1124     stmt_infodata.Close();
       
  1125     stmt_info.Close();
       
  1126     CleanupStack::PopAndDestroy( &stmt_infodata );
       
  1127     CleanupStack::PopAndDestroy( &stmt_info );
       
  1128     
       
  1129 
       
  1130     //look from real table 
       
  1131     TN_DEBUG1( "CThumbnailStore::DeleteThumbnailByPathL() -- MAIN TABLE lookup" );
       
  1132     stmt.Close();
       
  1133     CleanupStack::PopAndDestroy( &stmt );
       
  1134     CleanupClosePushL( stmt );   
       
  1135     User::LeaveIfError( stmt.Prepare( iDatabase, KThumbnailSqlSelectRowIDInfoByPath ));
       
  1136     
       
  1137     User::LeaveIfError( err );
       
  1138     
       
  1139     paramIndex = stmt.ParameterIndex( KThumbnailSqlParamPath );
       
  1140     User::LeaveIfError( paramIndex );
       
  1141     User::LeaveIfError( stmt.BindText( paramIndex, aPath ));
       
  1142          
       
  1143     rowStatus = stmt.Next();   
       
  1144     CleanupClosePushL( stmt_info );
       
  1145     err = stmt_info.Prepare( iDatabase, KThumbnailSqlDeleteInfoByPath);
       
  1146     CleanupClosePushL( stmt_infodata );
       
  1147     err = stmt_infodata.Prepare( iDatabase, KThumbnailSqlDeleteInfoDataByPath);
       
  1148        
       
  1149        
       
  1150     while(rowStatus == KSqlAtRow)
       
  1151        { 
       
  1152        rowid = stmt.ColumnInt( column ); 
       
  1153        paramIndex1 = stmt_info.ParameterIndex( KThumbnailSqlParamRowID );
       
  1154        User::LeaveIfError( paramIndex1 );
       
  1155        User::LeaveIfError( stmt_info.BindInt( paramIndex1, rowid ));
       
  1156           
       
  1157        deleteCount = stmt_info.Exec();
       
  1158        stmt_info.Reset();
       
  1159        User::LeaveIfError( deleteCount );
       
  1160                 
       
  1161        paramIndex2 = stmt_infodata.ParameterIndex( KThumbnailSqlParamRowID );  
       
  1162        User::LeaveIfError( paramIndex2 );
       
  1163        User::LeaveIfError( stmt_infodata.BindInt( paramIndex2, rowid ));
       
  1164                 
       
  1165        deleteCount = stmt_infodata.Exec();
       
  1166        stmt_infodata.Reset();
       
  1167        User::LeaveIfError( deleteCount );
       
  1168        
       
  1169        TN_DEBUG1( "CThumbnailStore::DeleteThumbnailByPathL() -- MAIN TABLE lookup - thumbnail deleted" );
       
  1170        
       
  1171        rowStatus = stmt.Next();
       
  1172        }
       
  1173     
       
  1174     stmt_infodata.Close();
       
  1175     stmt_info.Close();
       
  1176     stmt.Close();
       
  1177     CleanupStack::PopAndDestroy( &stmt_infodata );
       
  1178     CleanupStack::PopAndDestroy( &stmt_info );
       
  1179     CleanupStack::PopAndDestroy( &stmt );    
       
  1180     transaction.CommitL();
       
  1181     CleanupStack::PopAndDestroy( &transaction );
       
  1182     }
       
  1183 
       
  1184 // -----------------------------------------------------------------------------
       
  1185 // Delete thumbnails for given object file by Id
       
  1186 // -----------------------------------------------------------------------------
       
  1187 //
       
  1188 void CThumbnailStore::DeleteThumbnailsL( const TThumbnailId& aTNId )
       
  1189     {
       
  1190 #ifdef _DEBUG
       
  1191     TTime aStart, aStop;
       
  1192     aStart.UniversalTime();
       
  1193 #endif
       
  1194         
       
  1195     TInt paramIndex = 0;
       
  1196     TInt paramIndex1 = 0;
       
  1197     TInt paramIndex2 = 0;
       
  1198     TInt rowStatus = 0;
       
  1199     TInt column = 0;
       
  1200     TInt rowid = 0;
       
  1201     TInt deleteCount = 0;
       
  1202       
       
  1203     RThumbnailTransaction transaction( iDatabase );
       
  1204     CleanupClosePushL( transaction );
       
  1205     transaction.BeginL();
       
  1206     RSqlStatement stmt;
       
  1207     CleanupClosePushL( stmt );
       
  1208     
       
  1209     TN_DEBUG1( "CThumbnailStore::DeleteThumbnailsByIdL() -- TEMP TABLE lookup" );
       
  1210     TInt err = stmt.Prepare( iDatabase, KTempThumbnailSqlSelectRowIDInfoByID);
       
  1211     User::LeaveIfError( err );
       
  1212         
       
  1213     paramIndex = stmt.ParameterIndex( KThumbnailSqlParamId );
       
  1214     User::LeaveIfError( paramIndex );
       
  1215     User::LeaveIfError( stmt.BindInt( paramIndex, aTNId ));
       
  1216         
       
  1217     rowStatus = stmt.Next();
       
  1218     RSqlStatement stmt_info;
       
  1219     CleanupClosePushL( stmt_info );
       
  1220     err = stmt_info.Prepare( iDatabase, KTempThumbnailSqlDeleteInfoByID);
       
  1221     RSqlStatement stmt_infodata;
       
  1222     CleanupClosePushL( stmt_infodata );
       
  1223     err = stmt_infodata.Prepare( iDatabase, KTempThumbnailSqlDeleteInfoDataByID);
       
  1224         
       
  1225         
       
  1226     while(rowStatus == KSqlAtRow)
       
  1227        {  
       
  1228        rowid = stmt.ColumnInt( column ); 
       
  1229        paramIndex1 = stmt_info.ParameterIndex( KThumbnailSqlParamRowID );
       
  1230        User::LeaveIfError( paramIndex1 );
       
  1231        User::LeaveIfError( stmt_info.BindInt( paramIndex1, rowid ));
       
  1232            
       
  1233        err = stmt_info.Exec();
       
  1234        stmt_info.Reset();
       
  1235        User::LeaveIfError( err );
       
  1236                  
       
  1237        paramIndex2 = stmt_infodata.ParameterIndex( KThumbnailSqlParamRowID );
       
  1238        User::LeaveIfError( paramIndex2 );
       
  1239        User::LeaveIfError( stmt_infodata.BindInt( paramIndex2, rowid ));
       
  1240                  
       
  1241        err = stmt_infodata.Exec();
       
  1242        stmt_infodata.Reset();
       
  1243        User::LeaveIfError( err );
       
  1244        deleteCount++;
       
  1245        
       
  1246        TN_DEBUG1( "CThumbnailStore::DeleteThumbnailsByIdL() -- TEMP TABLE lookup - thumbnail deleted" );
       
  1247        
       
  1248        // fetch another row (temp table rowIDs are updated immediately)
       
  1249        stmt.Reset();
       
  1250        
       
  1251        paramIndex = stmt.ParameterIndex( KThumbnailSqlParamId );
       
  1252        User::LeaveIfError( paramIndex );
       
  1253        User::LeaveIfError( stmt.BindInt( paramIndex, aTNId ));
       
  1254        
       
  1255        rowStatus = stmt.Next();    
       
  1256        }
       
  1257     
       
  1258     stmt_infodata.Close();
       
  1259     stmt_info.Close();
       
  1260     CleanupStack::PopAndDestroy( &stmt_infodata );
       
  1261     CleanupStack::PopAndDestroy( &stmt_info );
       
  1262         
       
  1263     
       
  1264     //look from real table       
       
  1265     TN_DEBUG1( "CThumbnailStore::DeleteThumbnailByIdL() -- MAIN TABLE lookup" );
       
  1266     stmt.Close();
       
  1267     CleanupStack::PopAndDestroy( &stmt );
       
  1268     CleanupClosePushL( stmt );   
       
  1269     User::LeaveIfError( stmt.Prepare( iDatabase, KThumbnailSqlSelectRowIDInfoByID ));
       
  1270         
       
  1271     User::LeaveIfError( err );
       
  1272         
       
  1273     paramIndex = stmt.ParameterIndex( KThumbnailSqlParamId );
       
  1274     User::LeaveIfError( paramIndex );
       
  1275     User::LeaveIfError( stmt.BindInt( paramIndex, aTNId ));
       
  1276              
       
  1277     rowStatus = stmt.Next();   
       
  1278     CleanupClosePushL( stmt_info );
       
  1279     err = stmt_info.Prepare( iDatabase, KThumbnailSqlDeleteInfoByID);
       
  1280     CleanupClosePushL( stmt_infodata );
       
  1281     err = stmt_infodata.Prepare( iDatabase, KThumbnailSqlDeleteInfoDataByID);
       
  1282            
       
  1283            
       
  1284     while(rowStatus == KSqlAtRow)
       
  1285        { 
       
  1286        rowid = stmt.ColumnInt( column ); 
       
  1287        paramIndex1 = stmt_info.ParameterIndex( KThumbnailSqlParamRowID );
       
  1288        User::LeaveIfError( paramIndex1 );
       
  1289        User::LeaveIfError( stmt_info.BindInt( paramIndex1, rowid ));
       
  1290               
       
  1291        err = stmt_info.Exec();
       
  1292        stmt_info.Reset();
       
  1293        User::LeaveIfError( err );
       
  1294                     
       
  1295        paramIndex2 = stmt_infodata.ParameterIndex( KThumbnailSqlParamRowID );  
       
  1296        User::LeaveIfError( paramIndex2 );
       
  1297        User::LeaveIfError( stmt_infodata.BindInt( paramIndex2, rowid ));
       
  1298                     
       
  1299        err = stmt_infodata.Exec();
       
  1300        stmt_infodata.Reset();
       
  1301        User::LeaveIfError( err );
       
  1302        deleteCount++;
       
  1303        
       
  1304        TN_DEBUG1( "CThumbnailStore::DeleteThumbnailByIdL() -- MAIN TABLE lookup - thumbnail deleted" );
       
  1305        
       
  1306        rowStatus = stmt.Next();
       
  1307        }
       
  1308         
       
  1309     stmt_infodata.Close();
       
  1310     stmt_info.Close();
       
  1311     stmt.Close();
       
  1312     CleanupStack::PopAndDestroy( &stmt_infodata );
       
  1313     CleanupStack::PopAndDestroy( &stmt_info );
       
  1314     CleanupStack::PopAndDestroy( &stmt );    
       
  1315     transaction.CommitL();
       
  1316     CleanupStack::PopAndDestroy( &transaction );
       
  1317     
       
  1318 #ifdef _DEBUG
       
  1319     aStop.UniversalTime();
       
  1320     TN_DEBUG2( "CThumbnailStore::DeleteThumbnailsByIdL() took %d ms", (TInt)aStop.MicroSecondsFrom(aStart).Int64()/1000);
       
  1321 #endif
       
  1322     
       
  1323     if(!deleteCount)
       
  1324         {
       
  1325         User::Leave(KErrNotFound);
       
  1326         }
       
  1327     }
       
  1328 
       
  1329 
       
  1330 // ---------------------------------------------------------------------------
       
  1331 // CThumbnailStore::PersistentSizes()
       
  1332 // ---------------------------------------------------------------------------
       
  1333 //
       
  1334 void CThumbnailStore::SetPersistentSizes(const RArray < TThumbnailPersistentSize > &aSizes)
       
  1335     {
       
  1336     iPersistentSizes = aSizes;
       
  1337     }
       
  1338 
       
  1339 // ---------------------------------------------------------------------------
       
  1340 // CThumbnailStore::FlushCacheTable()
       
  1341 // ---------------------------------------------------------------------------
       
  1342 //
       
  1343 void CThumbnailStore::FlushCacheTable( TBool aForce )
       
  1344     {
       
  1345     TN_DEBUG1("CThumbnailStore::FlushCacheTable() in");
       
  1346     
       
  1347     StopAutoFlush();
       
  1348     
       
  1349     if(iBatchItemCount <= 0)
       
  1350         {
       
  1351         // cache empty
       
  1352         return;
       
  1353         }
       
  1354     
       
  1355     if(iBatchItemCount < KMaxBatchItems && !aForce)
       
  1356        {
       
  1357        //some items in cache
       
  1358        StartAutoFlush();
       
  1359        return;
       
  1360        }
       
  1361     
       
  1362     //cache full, flush now
       
  1363     iBatchItemCount = 0;
       
  1364     
       
  1365 #ifdef _DEBUG
       
  1366     TTime aStart, aStop;
       
  1367     aStart.UniversalTime();
       
  1368 #endif
       
  1369     
       
  1370     // Move data from temp table to main....
       
  1371     TInt err_begin = iDatabase.Exec( KThumbnailBeginTransaction );
       
  1372     TN_DEBUG2("CThumbnailStore::FlushCacheTable() KThumbnailBeginTransaction %d", err_begin);
       
  1373     
       
  1374     TInt err_tempinfo = iDatabase.Exec( KThumbnailMoveFromTempInfoToMainTable );
       
  1375     TN_DEBUG2("CThumbnailStore::FlushCacheTable() KThumbnailMoveFromTempInfoToMainTable %d", err_tempinfo);
       
  1376     
       
  1377     TInt err_tempdata = iDatabase.Exec( KThumbnailMoveFromTempDataToMainTable );
       
  1378     TN_DEBUG2("CThumbnailStore::FlushCacheTable() KThumbnailMoveFromTempDataToMainTable %d", err_tempdata);
       
  1379     
       
  1380     TInt err_delinfo = iDatabase.Exec( KThumbnailDeleteFromTempInfoTable );
       
  1381     TN_DEBUG2("CThumbnailStore::FlushCacheTable() KThumbnailDeleteFromTempInfoTable %d", err_delinfo);
       
  1382     
       
  1383     TInt err_deldata = iDatabase.Exec( KThumbnailDeleteFromTempDataTable );
       
  1384     TN_DEBUG2("CThumbnailStore::FlushCacheTable() KThumbnailDeleteFromTempDataTable %d", err_deldata);
       
  1385    
       
  1386     
       
  1387     if( err_tempinfo < 0 || err_tempdata < 0  || err_delinfo < 0  || err_deldata < 0 )
       
  1388         {
       
  1389         TInt err = iDatabase.Exec( KThumbnailRollbackTransaction );
       
  1390         TN_DEBUG2("CThumbnailStore::FlushCacheTable() KThumbnailRollbackTransaction %d", err);
       
  1391         }
       
  1392     else
       
  1393         {
       
  1394         TInt err_commit = iDatabase.Exec( KThumbnailCommitTransaction );
       
  1395         TN_DEBUG2("CThumbnailStore::FlushCacheTable() KThumbnailCommitTransaction %d", err_commit);
       
  1396         }
       
  1397     
       
  1398 #ifdef _DEBUG
       
  1399     aStop.UniversalTime();
       
  1400     TN_DEBUG2( "CThumbnailStore::FlushCacheTable() took %d ms", (TInt)aStop.MicroSecondsFrom(aStart).Int64()/1000);
       
  1401 #endif
       
  1402 
       
  1403     TN_DEBUG1("CThumbnailStore::FlushCacheTable() out");
       
  1404     }
       
  1405 
       
  1406 
       
  1407 // -----------------------------------------------------------------------------
       
  1408 // Find store for thumbnails by Id
       
  1409 // -----------------------------------------------------------------------------
       
  1410 //
       
  1411 void CThumbnailStore::FindStoreL(TThumbnailId aThumbnailId)
       
  1412     {
       
  1413     TN_DEBUG2( "CThumbnailStore::FindStore( %d )", aThumbnailId );
       
  1414      RSqlStatement stmt;
       
  1415      CleanupClosePushL( stmt );
       
  1416          
       
  1417      TInt paramIndex = 0;
       
  1418      TInt found = KErrNotFound;
       
  1419      TInt rowStatus = 0;
       
  1420          
       
  1421      TN_DEBUG1( "CThumbnailStore::FindStore() -- TEMP TABLE lookup" );
       
  1422      User::LeaveIfError( stmt.Prepare( iDatabase, KTempThumbnailSqlSelectRowIDInfoByID ));
       
  1423      paramIndex = stmt.ParameterIndex( KThumbnailSqlParamId );
       
  1424      User::LeaveIfError( paramIndex );
       
  1425      User::LeaveIfError( stmt.BindInt( paramIndex, aThumbnailId ));
       
  1426          
       
  1427      rowStatus = stmt.Next();
       
  1428 
       
  1429     //if not found from temp table, look from real table
       
  1430     if(rowStatus != KSqlAtRow)
       
  1431         {
       
  1432         TN_DEBUG1( "CThumbnailStore::FindStore() -- MAIN TABLE lookup" );
       
  1433         stmt.Close();
       
  1434         CleanupStack::PopAndDestroy( &stmt );
       
  1435         CleanupClosePushL( stmt );
       
  1436             
       
  1437         User::LeaveIfError( stmt.Prepare( iDatabase, KThumbnailSqlSelectRowIDInfoByID ));
       
  1438          
       
  1439         paramIndex = stmt.ParameterIndex( KThumbnailSqlParamId );
       
  1440         User::LeaveIfError( paramIndex );
       
  1441         User::LeaveIfError( stmt.BindInt( paramIndex, aThumbnailId ));
       
  1442             
       
  1443         rowStatus = stmt.Next();
       
  1444         }
       
  1445 
       
  1446     if( rowStatus == KSqlAtRow )
       
  1447         {
       
  1448         TN_DEBUG1( "CThumbnailStore::FindStore() -- thumbnail found" );
       
  1449         found = KErrNone;
       
  1450         }
       
  1451     else
       
  1452         {
       
  1453         TN_DEBUG1( "CThumbnailStore::FindStore() -- thumbnail NOT found" );
       
  1454         }
       
  1455              
       
  1456     stmt.Close();
       
  1457     CleanupStack::PopAndDestroy( &stmt );
       
  1458 
       
  1459     User::LeaveIfError( found );
       
  1460     }
       
  1461 
       
  1462 // -----------------------------------------------------------------------------
       
  1463 // Updates path in current store by Id
       
  1464 // -----------------------------------------------------------------------------
       
  1465 //
       
  1466 TBool CThumbnailStore::UpdateStoreL( TThumbnailId aItemId, const TDesC& aNewPath )
       
  1467     {
       
  1468     TN_DEBUG3( "CThumbnailStore::UpdateStore( %d, %S) by ID", aItemId, &aNewPath);
       
  1469 
       
  1470     TBool doUpdate(EFalse);
       
  1471     TPath oldPath;
       
  1472     TInt column = 0;
       
  1473     
       
  1474     RSqlStatement stmt;
       
  1475     CleanupClosePushL( stmt );
       
  1476        
       
  1477     //check if path needs updating in temp table
       
  1478     User::LeaveIfError( stmt.Prepare( iDatabase, KThumbnailSelectTempPathByID ));
       
  1479     
       
  1480     TInt paramIndex = stmt.ParameterIndex( KThumbnailSqlParamId );
       
  1481     User::LeaveIfError( paramIndex );
       
  1482     User::LeaveIfError( stmt.BindInt( paramIndex, aItemId ));
       
  1483      
       
  1484     TInt rowStatus = stmt.Next();
       
  1485 
       
  1486      //if not found from temp table, look from real table
       
  1487      if(rowStatus != KSqlAtRow)
       
  1488          {
       
  1489          stmt.Close();
       
  1490          CleanupStack::PopAndDestroy( &stmt );
       
  1491          CleanupClosePushL( stmt );
       
  1492          
       
  1493          //check if path needs updating in main table
       
  1494          User::LeaveIfError( stmt.Prepare( iDatabase, KThumbnailSelectPathByID ));
       
  1495          
       
  1496          paramIndex = stmt.ParameterIndex( KThumbnailSqlParamId );
       
  1497          User::LeaveIfError( paramIndex );
       
  1498          User::LeaveIfError( stmt.BindInt( paramIndex, aItemId ));
       
  1499           
       
  1500          rowStatus = stmt.Next();
       
  1501          }
       
  1502      
       
  1503      if(rowStatus == KSqlAtRow)
       
  1504          {
       
  1505            TN_DEBUG1( "CThumbnailStore::UpdateStore() -- matching TN ID found" );
       
  1506            oldPath = stmt.ColumnTextL(column);
       
  1507             
       
  1508             if(oldPath.CompareF(aNewPath) != 0)
       
  1509                 {
       
  1510                 TN_DEBUG1( "CThumbnailStore::UpdateStore() -- path no match" );
       
  1511                 doUpdate = ETrue;
       
  1512                 }
       
  1513             else
       
  1514                 {
       
  1515                 TN_DEBUG1( "CThumbnailStore::UpdateStore() -- path match, skip..." );
       
  1516                 }
       
  1517          }
       
  1518      
       
  1519     stmt.Close();
       
  1520     CleanupStack::PopAndDestroy( &stmt );
       
  1521         
       
  1522     if(!doUpdate)
       
  1523         {
       
  1524         TN_DEBUG2( "CThumbnailStore::UpdateStore() -- no need to update old path=%S", &oldPath );
       
  1525         return EFalse;
       
  1526         }
       
  1527     
       
  1528     //Encapsulate update to Transaction
       
  1529     RThumbnailTransaction transaction( iDatabase );
       
  1530     CleanupClosePushL( transaction );
       
  1531     transaction.BeginL();
       
  1532 	
       
  1533     CleanupClosePushL( stmt );
       
  1534      
       
  1535     TN_DEBUG1( "CThumbnailStore::UpdateStore() -- do temp path update" );
       
  1536 
       
  1537     User::LeaveIfError( stmt.Prepare( iDatabase, KTempThumbnailSqlUpdateById ));
       
  1538     
       
  1539     paramIndex = stmt.ParameterIndex( KThumbnailSqlParamPath );
       
  1540     User::LeaveIfError( paramIndex );
       
  1541     User::LeaveIfError( stmt.BindText( paramIndex, aNewPath ));
       
  1542        
       
  1543     paramIndex = stmt.ParameterIndex( KThumbnailSqlParamId );
       
  1544     User::LeaveIfError( paramIndex );
       
  1545     User::LeaveIfError( stmt.BindInt( paramIndex, aItemId ));
       
  1546     User::LeaveIfError( stmt.Exec());
       
  1547     
       
  1548     stmt.Close();
       
  1549     CleanupStack::PopAndDestroy( &stmt );
       
  1550     CleanupClosePushL( stmt );
       
  1551     
       
  1552     TN_DEBUG1( "CThumbnailStore::UpdateStore() -- do main table path update" );
       
  1553              
       
  1554     User::LeaveIfError( stmt.Prepare( iDatabase, KThumbnailSqlUpdateById ));
       
  1555                 
       
  1556     paramIndex = stmt.ParameterIndex( KThumbnailSqlParamPath );
       
  1557     User::LeaveIfError( paramIndex );
       
  1558     User::LeaveIfError( stmt.BindText( paramIndex, aNewPath ));
       
  1559 
       
  1560     paramIndex = stmt.ParameterIndex( KThumbnailSqlParamId );
       
  1561     User::LeaveIfError( paramIndex );
       
  1562     User::LeaveIfError( stmt.BindInt( paramIndex, aItemId ));  
       
  1563             
       
  1564     User::LeaveIfError( stmt.Exec());   
       
  1565     CleanupStack::PopAndDestroy( &stmt );
       
  1566     
       
  1567     // Commit transaction
       
  1568     transaction.CommitL();
       
  1569     CleanupStack::PopAndDestroy( &transaction );
       
  1570     
       
  1571     return ETrue;
       
  1572     }
       
  1573 
       
  1574 // -----------------------------------------------------------------------------
       
  1575 // Update IDs by Path
       
  1576 // -----------------------------------------------------------------------------
       
  1577 //
       
  1578 void CThumbnailStore::UpdateStoreL( const TDesC& aPath, TThumbnailId aNewId )
       
  1579     {
       
  1580     TN_DEBUG3( "CThumbnailStore::UpdateStore( %S, %d ) by Path", &aPath, aNewId);
       
  1581     
       
  1582 #ifdef _DEBUG
       
  1583     TTime aStart, aStop;
       
  1584     aStart.UniversalTime();
       
  1585 #endif
       
  1586     
       
  1587     //Encapsulate update to Transaction
       
  1588     RThumbnailTransaction transaction( iDatabase );
       
  1589     CleanupClosePushL( transaction );
       
  1590     transaction.BeginL();
       
  1591     
       
  1592     RSqlStatement stmt;
       
  1593     CleanupClosePushL( stmt );
       
  1594        
       
  1595     TN_DEBUG1( "CThumbnailStore::UpdateStoreL() -- do temp ID update" );
       
  1596     
       
  1597     TInt err = stmt.Prepare( iDatabase, KTempThumbnailUpdateIdByPath );
       
  1598     
       
  1599 #ifdef _DEBUG
       
  1600     TPtrC errorMsg = iDatabase.LastErrorMessage();
       
  1601     TN_DEBUG2( "CThumbnailStore::UpdateStoreL() KTempThumbnailUpdateIdByPath %S" , &errorMsg);
       
  1602 #endif
       
  1603     
       
  1604     User::LeaveIfError( err );
       
  1605     
       
  1606     TInt paramIndex = stmt.ParameterIndex( KThumbnailSqlParamId );
       
  1607     User::LeaveIfError( paramIndex );
       
  1608     User::LeaveIfError( stmt.BindInt( paramIndex, aNewId ));
       
  1609     
       
  1610     paramIndex = stmt.ParameterIndex( KThumbnailSqlParamPath );
       
  1611     User::LeaveIfError( paramIndex );
       
  1612     User::LeaveIfError( stmt.BindText( paramIndex, aPath ));
       
  1613     
       
  1614     err =  stmt.Exec();
       
  1615     
       
  1616     TN_DEBUG2( "CThumbnailStore::UpdateStoreL() err==%d", err );
       
  1617     
       
  1618     stmt.Close();
       
  1619     CleanupStack::PopAndDestroy( &stmt );
       
  1620     CleanupClosePushL( stmt );
       
  1621     
       
  1622     TN_DEBUG1( "CThumbnailStore::UpdateStoreL() -- do main table ID update" );
       
  1623     
       
  1624     err = stmt.Prepare( iDatabase, KThumbnailUpdateIdByPath );
       
  1625     
       
  1626 #ifdef _DEBUG
       
  1627     TPtrC errorMsg2 = iDatabase.LastErrorMessage();
       
  1628     TN_DEBUG2( "CThumbnailStore::UpdateStoreL() KThumbnailUpdateIdByPath %S" , &errorMsg2);
       
  1629 #endif    
       
  1630     
       
  1631     User::LeaveIfError( err );
       
  1632     
       
  1633     paramIndex = stmt.ParameterIndex( KThumbnailSqlParamId );
       
  1634     User::LeaveIfError( paramIndex );
       
  1635     User::LeaveIfError( stmt.BindInt( paramIndex, aNewId ));
       
  1636     
       
  1637     paramIndex = stmt.ParameterIndex( KThumbnailSqlParamPath );
       
  1638     User::LeaveIfError( paramIndex );
       
  1639     User::LeaveIfError( stmt.BindText( paramIndex, aPath ));
       
  1640     
       
  1641     err =  stmt.Exec();
       
  1642     
       
  1643     TN_DEBUG2( "CThumbnailStore::UpdateStoreL() err==%d", err );
       
  1644     
       
  1645     stmt.Close();
       
  1646     CleanupStack::PopAndDestroy( &stmt ); 
       
  1647     
       
  1648     // Commit transaction
       
  1649     transaction.CommitL();
       
  1650     CleanupStack::PopAndDestroy( &transaction );
       
  1651     
       
  1652 #ifdef _DEBUG
       
  1653     aStop.UniversalTime();
       
  1654     TN_DEBUG2( "CThumbnailStore::UpdateStoreL() took %d ms", (TInt)aStop.MicroSecondsFrom(aStart).Int64()/1000);
       
  1655 #endif
       
  1656     }
       
  1657 
       
  1658 // -----------------------------------------------------------------------------
       
  1659 // Checks if given modification timestamp is newer than in DB
       
  1660 // -----------------------------------------------------------------------------
       
  1661 //
       
  1662 TBool CThumbnailStore::CheckModifiedL( const TThumbnailId aItemId, const TInt64 aModified )
       
  1663     {
       
  1664     TN_DEBUG2( "CThumbnailStore::CheckModifiedL( %d )", aItemId);
       
  1665 
       
  1666     TInt column = 0;
       
  1667     
       
  1668     RSqlStatement stmt;
       
  1669     CleanupClosePushL( stmt );
       
  1670        
       
  1671     User::LeaveIfError( stmt.Prepare( iDatabase, KThumbnailSelectTempModifiedByID ));
       
  1672     
       
  1673     TInt paramIndex = stmt.ParameterIndex( KThumbnailSqlParamId );
       
  1674     User::LeaveIfError( paramIndex );
       
  1675     User::LeaveIfError( stmt.BindInt( paramIndex, aItemId ));
       
  1676      
       
  1677     TInt rowStatus = stmt.Next();
       
  1678 
       
  1679      //if not found from temp table, look from real table
       
  1680      if(rowStatus != KSqlAtRow)
       
  1681          {
       
  1682          stmt.Close();
       
  1683          CleanupStack::PopAndDestroy( &stmt );
       
  1684          CleanupClosePushL( stmt );
       
  1685          
       
  1686          User::LeaveIfError( stmt.Prepare( iDatabase, KThumbnailSelectModifiedByID ));
       
  1687          
       
  1688          paramIndex = stmt.ParameterIndex( KThumbnailSqlParamId );
       
  1689          User::LeaveIfError( paramIndex );
       
  1690          User::LeaveIfError( stmt.BindInt( paramIndex, aItemId ));
       
  1691           
       
  1692          rowStatus = stmt.Next();
       
  1693          }
       
  1694      
       
  1695      TBool modified = EFalse;
       
  1696      
       
  1697      if(rowStatus == KSqlAtRow)
       
  1698          {
       
  1699          TInt64 oldModified = stmt.ColumnInt64( column );
       
  1700          
       
  1701          if (oldModified < aModified)
       
  1702              {
       
  1703              TN_DEBUG1( "CThumbnailStore::CheckModifiedL() -- timestamp is newer than original" );
       
  1704              modified = ETrue;
       
  1705              }
       
  1706          else if (oldModified > aModified)
       
  1707              {
       
  1708              TN_DEBUG1( "CThumbnailStore::CheckModifiedL() -- timestamp is older than original" );
       
  1709              }
       
  1710          else if (oldModified == aModified)
       
  1711              {
       
  1712              TN_DEBUG1( "CThumbnailStore::CheckModifiedL() -- timestamp is the same as original" );
       
  1713              }
       
  1714          }
       
  1715      
       
  1716     stmt.Close();
       
  1717     CleanupStack::PopAndDestroy( &stmt ); 
       
  1718     
       
  1719     return modified;
       
  1720     }
       
  1721 
       
  1722 // -----------------------------------------------------------------------------
       
  1723 // Fetches thumbnails from store by Id
       
  1724 // -----------------------------------------------------------------------------
       
  1725 //
       
  1726 void CThumbnailStore::FetchThumbnailsL(TThumbnailId aItemId, RArray < TThumbnailDatabaseData* >& aThumbnails)
       
  1727     {
       
  1728     TN_DEBUG1( "CThumbnailStore::FetchThumbnails()" );
       
  1729     RSqlStatement stmt;
       
  1730     CleanupClosePushL( stmt );
       
  1731     
       
  1732     // first temp table
       
  1733     User::LeaveIfError( stmt.Prepare( iDatabase, KThumbnailSelectTempById ));
       
  1734 
       
  1735     TInt paramIndex = stmt.ParameterIndex( KThumbnailSqlParamId );
       
  1736     User::LeaveIfError( paramIndex );
       
  1737     User::LeaveIfError( stmt.BindInt( paramIndex, aItemId ));
       
  1738 
       
  1739     TInt rowStatus = stmt.Next();
       
  1740     
       
  1741     TPath path;
       
  1742     TPath tnPath;
       
  1743     while ( rowStatus == KSqlAtRow)
       
  1744         {
       
  1745         TN_DEBUG1( "CThumbnailStore::FetchThumbnails() -- thumbnail found from temp table" );
       
  1746         
       
  1747         TInt column = 0;
       
  1748             
       
  1749         TThumbnailDatabaseData* newRow = new(ELeave) TThumbnailDatabaseData;
       
  1750             
       
  1751         TInt err = stmt.ColumnText( column++, newRow->iPath );
       
  1752         newRow->iTnId = stmt.ColumnInt( column++ );
       
  1753         newRow->iSize = stmt.ColumnInt( column++ );
       
  1754         newRow->iFormat = stmt.ColumnInt( column++ );
       
  1755         err = stmt.ColumnText( column++, newRow->iTnPath);
       
  1756         newRow->iWidth = stmt.ColumnInt( column++ );
       
  1757         newRow->iHeight = stmt.ColumnInt( column++ );
       
  1758         newRow->iOrigWidth = stmt.ColumnInt( column++ );
       
  1759         newRow->iOrigHeight = stmt.ColumnInt( column++ );
       
  1760         newRow->iFlags = stmt.ColumnInt( column++ );
       
  1761         newRow->iVideoPosition = stmt.ColumnInt( column++ );
       
  1762         newRow->iOrientation = stmt.ColumnInt( column++ );
       
  1763         newRow->iThumbFromPath = stmt.ColumnInt( column++ );
       
  1764         newRow->iModified = stmt.ColumnInt64( column++ );
       
  1765         
       
  1766         if(newRow->iFormat == 0)
       
  1767             {
       
  1768             TPtrC8 ptr = stmt.ColumnBinaryL( column++ );
       
  1769             RDesReadStream stream( ptr );
       
  1770             newRow->iBlob = new( ELeave )CFbsBitmap();
       
  1771             newRow->iBlob->InternalizeL( stream );
       
  1772             }
       
  1773         else if(newRow->iFormat == 1)
       
  1774             {
       
  1775             TPtrC8 ptr = stmt.ColumnBinaryL( column++ );
       
  1776             HBufC8* data = ptr.AllocL() ;
       
  1777             newRow->iBlob = NULL;
       
  1778             newRow->iData = data;
       
  1779             }
       
  1780         
       
  1781         aThumbnails.Append( newRow );
       
  1782                
       
  1783         rowStatus = stmt.Next();
       
  1784         }
       
  1785     
       
  1786     // then real table
       
  1787     stmt.Close();
       
  1788     CleanupStack::PopAndDestroy(&stmt); 
       
  1789     CleanupClosePushL( stmt );
       
  1790     User::LeaveIfError( stmt.Prepare( iDatabase, KThumbnailSelectById ));
       
  1791 
       
  1792     paramIndex = stmt.ParameterIndex( KThumbnailSqlParamId );
       
  1793     User::LeaveIfError( paramIndex );
       
  1794     User::LeaveIfError( stmt.BindInt( paramIndex, aItemId ));
       
  1795 
       
  1796     rowStatus = stmt.Next();
       
  1797     while ( rowStatus == KSqlAtRow)
       
  1798         {
       
  1799         TN_DEBUG1( "CThumbnailStore::FetchThumbnails() -- thumbnail found from real table" );
       
  1800         
       
  1801         TInt column = 0;
       
  1802         
       
  1803         TThumbnailDatabaseData* newRow = new(ELeave) TThumbnailDatabaseData;
       
  1804         
       
  1805         TInt err = stmt.ColumnText( column++, newRow->iPath );
       
  1806         newRow->iTnId = stmt.ColumnInt( column++ );
       
  1807         newRow->iSize = stmt.ColumnInt( column++ );
       
  1808         newRow->iFormat = stmt.ColumnInt( column++ );
       
  1809         err = stmt.ColumnText( column++, newRow->iTnPath);
       
  1810         newRow->iWidth = stmt.ColumnInt( column++ );
       
  1811         newRow->iHeight = stmt.ColumnInt( column++ );
       
  1812         newRow->iOrigWidth = stmt.ColumnInt( column++ );
       
  1813         newRow->iOrigHeight = stmt.ColumnInt( column++ );
       
  1814         newRow->iFlags = stmt.ColumnInt( column++ );
       
  1815         newRow->iVideoPosition = stmt.ColumnInt( column++ );
       
  1816         newRow->iOrientation = stmt.ColumnInt( column++ );
       
  1817         newRow->iThumbFromPath = stmt.ColumnInt( column++ );
       
  1818         newRow->iModified = stmt.ColumnInt64( column++ );
       
  1819         
       
  1820         if(newRow->iFormat == 0)
       
  1821             {
       
  1822             TPtrC8 ptr = stmt.ColumnBinaryL( column++ );
       
  1823             RDesReadStream stream( ptr );
       
  1824             newRow->iBlob = new( ELeave )CFbsBitmap();
       
  1825             newRow->iBlob->InternalizeL( stream );
       
  1826             }
       
  1827         else if(newRow->iFormat == 1)
       
  1828             {
       
  1829             TPtrC8 ptr = stmt.ColumnBinaryL( column++ );
       
  1830             HBufC8* data = ptr.AllocL() ;
       
  1831             newRow->iBlob = NULL;
       
  1832             newRow->iData = data;
       
  1833             }
       
  1834         
       
  1835         aThumbnails.Append( newRow );
       
  1836               
       
  1837         rowStatus = stmt.Next();
       
  1838         }
       
  1839     
       
  1840     stmt.Close();
       
  1841     CleanupStack::PopAndDestroy( &stmt );
       
  1842     }
       
  1843 
       
  1844 // -----------------------------------------------------------------------------
       
  1845 // Stores thumbnails to store
       
  1846 // -----------------------------------------------------------------------------
       
  1847 //
       
  1848 
       
  1849 void CThumbnailStore::StoreThumbnailsL(const TDesC& aNewPath, RArray < TThumbnailDatabaseData* >& aThumbnails)
       
  1850     {
       
  1851     TN_DEBUG1( "CThumbnailStore::StoreThumbnails()" );
       
  1852     
       
  1853     TInt ThumbnailCount = aThumbnails.Count();
       
  1854     RSqlStatement stmt;
       
  1855     for ( TInt i = 0; i < ThumbnailCount; i++ )
       
  1856         {
       
  1857         RThumbnailTransaction transaction( iDatabase );
       
  1858         CleanupClosePushL( transaction );
       
  1859         transaction.BeginL();
       
  1860         
       
  1861         CleanupClosePushL( stmt );
       
  1862           
       
  1863         User::LeaveIfError( stmt.Prepare( iDatabase, KThumbnailInsertThumbnailInfoByPathAndId ));
       
  1864         
       
  1865         TInt paramIndex = stmt.ParameterIndex( KThumbnailSqlParamPath );
       
  1866         User::LeaveIfError( paramIndex );
       
  1867         User::LeaveIfError( stmt.BindText( paramIndex, aNewPath ));
       
  1868         
       
  1869         paramIndex = stmt.ParameterIndex( KThumbnailSqlParamWidth );
       
  1870         User::LeaveIfError( paramIndex );
       
  1871         User::LeaveIfError( stmt.BindInt( paramIndex, aThumbnails[ i ]->iWidth ));
       
  1872         
       
  1873         paramIndex = stmt.ParameterIndex( KThumbnailSqlParamHeight );
       
  1874         User::LeaveIfError( paramIndex );
       
  1875         User::LeaveIfError( stmt.BindInt( paramIndex, aThumbnails[ i ]->iHeight ));
       
  1876         
       
  1877         paramIndex = stmt.ParameterIndex( KThumbnailSqlParamOriginalWidth );
       
  1878         User::LeaveIfError( paramIndex );
       
  1879         User::LeaveIfError( stmt.BindInt( paramIndex, aThumbnails[ i ]->iOrigWidth ));
       
  1880         
       
  1881         paramIndex = stmt.ParameterIndex( KThumbnailSqlParamOriginalHeight );
       
  1882         User::LeaveIfError( paramIndex );
       
  1883         User::LeaveIfError( stmt.BindInt( paramIndex, aThumbnails[ i ]->iOrigHeight ));
       
  1884         
       
  1885         paramIndex = stmt.ParameterIndex( KThumbnailSqlParamFormat );
       
  1886         User::LeaveIfError( paramIndex );
       
  1887         User::LeaveIfError( stmt.BindInt( paramIndex, aThumbnails[ i ]->iFormat ));
       
  1888         
       
  1889         paramIndex = stmt.ParameterIndex( KThumbnailSqlParamFlags );
       
  1890         User::LeaveIfError( paramIndex );
       
  1891         User::LeaveIfError( stmt.BindInt( paramIndex, aThumbnails[ i ]->iFlags ));
       
  1892         
       
  1893         paramIndex = stmt.ParameterIndex( KThumbnailSqlParamSize );
       
  1894         User::LeaveIfError( paramIndex );
       
  1895         User::LeaveIfError( stmt.BindInt( paramIndex, aThumbnails[ i ]->iSize ));
       
  1896         
       
  1897         paramIndex = stmt.ParameterIndex( KThumbnailSqlParamId );
       
  1898         User::LeaveIfError( paramIndex );
       
  1899         User::LeaveIfError( stmt.BindInt( paramIndex, aThumbnails[ i ]->iTnId ));
       
  1900         
       
  1901         paramIndex = stmt.ParameterIndex( KThumbnailSqlParamOrientation );
       
  1902         User::LeaveIfError( paramIndex );
       
  1903         User::LeaveIfError( stmt.BindInt( paramIndex, aThumbnails[ i ]->iOrientation ));
       
  1904         
       
  1905         paramIndex = stmt.ParameterIndex( KThumbnailSqlParamThumbFromPath );
       
  1906         User::LeaveIfError( paramIndex );
       
  1907         User::LeaveIfError( stmt.BindInt( paramIndex, aThumbnails[ i ]->iThumbFromPath ));
       
  1908         
       
  1909         paramIndex = stmt.ParameterIndex( KThumbnailSqlParamModified );
       
  1910         User::LeaveIfError( paramIndex );
       
  1911         User::LeaveIfError( stmt.BindInt64( paramIndex, aThumbnails[ i ]->iModified ));
       
  1912         
       
  1913         User::LeaveIfError( stmt.Exec());    
       
  1914         CleanupStack::PopAndDestroy( &stmt );
       
  1915         
       
  1916         RSqlStatement stmtData;
       
  1917         CleanupClosePushL( stmtData );
       
  1918         TInt err = stmtData.Prepare( iDatabase, KThumbnailInsertTempThumbnailInfoData );
       
  1919         
       
  1920         if(aThumbnails[ i ]->iFormat == 0)
       
  1921             {
       
  1922             CBufFlat* buf = CBufFlat::NewL( KStreamBufferSize );
       
  1923             CleanupStack::PushL( buf );
       
  1924             RBufWriteStream stream;
       
  1925             stream.Open( *buf );
       
  1926             aThumbnails[ i ]->iBlob->ExternalizeL( stream );
       
  1927             paramIndex = stmtData.ParameterIndex( KThumbnailSqlParamData );
       
  1928             User::LeaveIfError( paramIndex );
       
  1929             User::LeaveIfError( stmtData.BindBinary( paramIndex, buf->Ptr( 0 ) ));
       
  1930             CleanupStack::PopAndDestroy( buf );
       
  1931             delete aThumbnails[i]->iBlob;
       
  1932             aThumbnails[i]->iBlob = NULL;
       
  1933             }
       
  1934         else if(aThumbnails[ i ]->iFormat == 1)
       
  1935             {
       
  1936             paramIndex = stmtData.ParameterIndex( KThumbnailSqlParamData );
       
  1937             User::LeaveIfError( paramIndex );
       
  1938             User::LeaveIfError( stmtData.BindBinary( paramIndex, *aThumbnails[ i ]->iData ));
       
  1939             delete aThumbnails[i]->iData;
       
  1940             aThumbnails[i]->iData = NULL;
       
  1941             }
       
  1942         
       
  1943         User::LeaveIfError( stmtData.Exec());
       
  1944         CleanupStack::PopAndDestroy( &stmtData );
       
  1945         
       
  1946         // Commit transaction
       
  1947         transaction.CommitL();
       
  1948         CleanupStack::PopAndDestroy( &transaction );
       
  1949         
       
  1950         delete aThumbnails[i];
       
  1951         aThumbnails[i] = NULL;
       
  1952         iBatchItemCount++;                          
       
  1953         }
       
  1954     
       
  1955     FlushCacheTable();
       
  1956     }
       
  1957 
       
  1958 // -----------------------------------------------------------------------------
       
  1959 // CheckVersionAndImeiL()
       
  1960 // -----------------------------------------------------------------------------
       
  1961 //
       
  1962 TInt CThumbnailStore::CheckImeiL()
       
  1963     {
       
  1964     TN_DEBUG1( "CThumbnailStore::CheckImeiL()" );
       
  1965     RSqlStatement stmt;
       
  1966     CleanupClosePushL( stmt );
       
  1967          
       
  1968     TInt rowStatus = 0;
       
  1969     TInt column = 0;
       
  1970     TBuf<KImeiBufferSize> imei;
       
  1971       
       
  1972     TInt ret = stmt.Prepare( iDatabase, KThumbnailSelectFromVersion );
       
  1973     if(ret < 0 )
       
  1974        {  
       
  1975        stmt.Close();
       
  1976        CleanupStack::PopAndDestroy( &stmt ); 
       
  1977        return KErrNotSupported;
       
  1978        }
       
  1979               
       
  1980     rowStatus = stmt.Next();
       
  1981     
       
  1982     if ( rowStatus == KSqlAtRow)    
       
  1983        {        
       
  1984        column=2;
       
  1985        stmt.ColumnText( column++, imei);  
       
  1986        }
       
  1987     
       
  1988     stmt.Close();
       
  1989     CleanupStack::PopAndDestroy( &stmt ); 
       
  1990     
       
  1991     if( imei == iImei )
       
  1992       {
       
  1993       return KErrNone;  
       
  1994       }
       
  1995     else
       
  1996       {
       
  1997       return KErrNotSupported;  
       
  1998       }
       
  1999     }
       
  2000 
       
  2001 // -----------------------------------------------------------------------------
       
  2002 // CheckVersionAndImeiL()
       
  2003 // -----------------------------------------------------------------------------
       
  2004 //
       
  2005 TInt CThumbnailStore::CheckVersionL()
       
  2006     {
       
  2007     TN_DEBUG1( "CThumbnailStore::CheckVersionL()" );
       
  2008     RSqlStatement stmt;
       
  2009     CleanupClosePushL( stmt );
       
  2010          
       
  2011     TInt rowStatus = 0;
       
  2012     TInt column = 0;
       
  2013     TInt minor = 0;
       
  2014     TInt major = 0;
       
  2015 
       
  2016       
       
  2017     TInt ret = stmt.Prepare( iDatabase, KThumbnailSelectFromVersion );
       
  2018     if(ret < 0 )
       
  2019        {  
       
  2020        stmt.Close();
       
  2021        CleanupStack::PopAndDestroy( &stmt ); 
       
  2022        return KErrNotSupported;
       
  2023        }
       
  2024               
       
  2025     rowStatus = stmt.Next();
       
  2026     
       
  2027     if ( rowStatus == KSqlAtRow)    
       
  2028        {        
       
  2029        major = stmt.ColumnInt( column++);
       
  2030        minor = stmt.ColumnInt( column++);
       
  2031        }
       
  2032     
       
  2033     stmt.Close();
       
  2034     CleanupStack::PopAndDestroy( &stmt ); 
       
  2035     
       
  2036     if(major == KMajor && minor == KMinor )
       
  2037       {
       
  2038       return KErrNone;  
       
  2039       }
       
  2040     else
       
  2041       {
       
  2042       return KErrNotSupported;  
       
  2043       }
       
  2044     }
       
  2045 
       
  2046 
       
  2047 // -----------------------------------------------------------------------------
       
  2048 // CheckVersionAndImeiL()
       
  2049 // -----------------------------------------------------------------------------
       
  2050 //
       
  2051 TInt CThumbnailStore::CheckMediaIDL()
       
  2052     {
       
  2053     
       
  2054     TN_DEBUG1( "CThumbnailStore::CheckMediaIDL()" );
       
  2055     TInt err = 0;
       
  2056     
       
  2057     TVolumeInfo volumeinfo;
       
  2058     err = iFs.Volume(volumeinfo, iDrive);
       
  2059     TUint id = volumeinfo.iUniqueID;
       
  2060     TBuf<50> mediaid;
       
  2061     mediaid.Num(id);
       
  2062     
       
  2063     RFile64 file;
       
  2064     err = file.Open(iFs, mediaid, EFileShareReadersOrWriters);
       
  2065     if(err)
       
  2066        {
       
  2067        file.Create(iFs, mediaid, EFileShareReadersOrWriters );
       
  2068        file.Close();
       
  2069        return KErrNotSupported;
       
  2070        } 
       
  2071     file.Close();
       
  2072     return KErrNone;  
       
  2073     }
       
  2074      
       
  2075 // -----------------------------------------------------------------------------
       
  2076 // AddVersionAndImeiL()
       
  2077 // -----------------------------------------------------------------------------
       
  2078 //
       
  2079 void CThumbnailStore::AddVersionAndImeiL()
       
  2080     {
       
  2081     
       
  2082     TN_DEBUG1( "CThumbnailStore::AddVersionAndImei()" );
       
  2083     RSqlStatement stmt;
       
  2084     CleanupClosePushL( stmt );
       
  2085             
       
  2086     TInt paramIndex = 0;
       
  2087             
       
  2088     User::LeaveIfError( stmt.Prepare( iDatabase, KThumbnailInsertToVersion ));
       
  2089     paramIndex = stmt.ParameterIndex( KThumbnailSqlParamImei );
       
  2090     User::LeaveIfError( paramIndex );
       
  2091     User::LeaveIfError( stmt.BindText( paramIndex, iImei ));  
       
  2092     
       
  2093     paramIndex = stmt.ParameterIndex( KThumbnailSqlParamMinor );
       
  2094     User::LeaveIfError( paramIndex );
       
  2095     User::LeaveIfError( stmt.BindInt( paramIndex, KMinor )); 
       
  2096     
       
  2097     paramIndex = stmt.ParameterIndex( KThumbnailSqlParamMajor );
       
  2098     User::LeaveIfError( paramIndex );
       
  2099     User::LeaveIfError( stmt.BindInt( paramIndex, KMajor )); 
       
  2100     
       
  2101     User::LeaveIfError( stmt.Exec());
       
  2102     stmt.Close();
       
  2103     CleanupStack::PopAndDestroy( &stmt );
       
  2104     
       
  2105     }
       
  2106 
       
  2107 // -----------------------------------------------------------------------------
       
  2108 // ResetThumbnailIDs()
       
  2109 // -----------------------------------------------------------------------------
       
  2110 //
       
  2111 void CThumbnailStore::ResetThumbnailIDs()
       
  2112     {
       
  2113     TN_DEBUG1( "CThumbnailStore::ResetThumbnailIDs()" );
       
  2114 
       
  2115     TInt err = iDatabase.Exec( KTempThumbnailResetIDs );
       
  2116     TN_DEBUG2( "CThumbnailStore::ResetThumbnailIDs() KThumbnailResetIDs - temp table, err=%d", err );
       
  2117     
       
  2118     err = iDatabase.Exec( KThumbnailResetIDs );
       
  2119     TN_DEBUG2( "CThumbnailStore::ResetThumbnailIDs() KThumbnailResetIDs - main table, err=%d", err );
       
  2120     }
       
  2121 
       
  2122 
       
  2123 // -----------------------------------------------------------------------------
       
  2124 // UpdateImeiL()
       
  2125 // -----------------------------------------------------------------------------
       
  2126 //
       
  2127 void CThumbnailStore::UpdateImeiL()
       
  2128     {
       
  2129     TN_DEBUG1( "CThumbnailStore::UpdateImeiL()" );
       
  2130     RSqlStatement stmt;
       
  2131     CleanupClosePushL( stmt );
       
  2132          
       
  2133       
       
  2134     TInt ret = stmt.Prepare( iDatabase, KThumbnailUpdateIMEI );
       
  2135     
       
  2136     TInt paramIndex = stmt.ParameterIndex( KThumbnailSqlParamImei );
       
  2137     User::LeaveIfError( paramIndex );
       
  2138     User::LeaveIfError( stmt.BindText( paramIndex, iImei ));
       
  2139     
       
  2140     TInt err =  stmt.Exec();
       
  2141     
       
  2142     TN_DEBUG2( "CThumbnailStore::UpdateImeiL() err==%d", err );
       
  2143     
       
  2144     User::LeaveIfError( err );
       
  2145     
       
  2146     stmt.Close();
       
  2147     CleanupStack::PopAndDestroy( &stmt ); 
       
  2148     }
       
  2149 
       
  2150 // -----------------------------------------------------------------------------
       
  2151 // StartAutoFlush()
       
  2152 // -----------------------------------------------------------------------------
       
  2153 //
       
  2154 void CThumbnailStore::StartAutoFlush()
       
  2155     {
       
  2156     TN_DEBUG1( "CThumbnailStore::StartAutoFlush()" );
       
  2157     
       
  2158     TInt err = KErrNone;
       
  2159     
       
  2160     if( iAutoFlushTimer )
       
  2161         {
       
  2162         iAutoFlushTimer->Cancel();
       
  2163         }
       
  2164     else
       
  2165         {
       
  2166         TRAP(err, iAutoFlushTimer = CPeriodic::NewL(CActive::EPriorityIdle) );
       
  2167         }
       
  2168     
       
  2169     if (err != KErrNone)
       
  2170         {
       
  2171         TN_DEBUG2( "CThumbnailStore::StartAutoFlush() - Error creating timer (%d)", err );
       
  2172         }
       
  2173     else
       
  2174         {
       
  2175         iAutoFlushTimer->Start( KAutoFlushTimeout, KAutoFlushTimeout, 
       
  2176                                 TCallBack(AutoFlushTimerCallBack, this));
       
  2177         }
       
  2178     }
       
  2179 
       
  2180 // -----------------------------------------------------------------------------
       
  2181 // StopAutoFlush()
       
  2182 // -----------------------------------------------------------------------------
       
  2183 //
       
  2184 void CThumbnailStore::StopAutoFlush()
       
  2185     {
       
  2186     TN_DEBUG1( "CThumbnailStore::StopAutoFlush()" );
       
  2187     if( iAutoFlushTimer )
       
  2188         {
       
  2189         iAutoFlushTimer->Cancel();
       
  2190         }
       
  2191     }
       
  2192 
       
  2193 // ---------------------------------------------------------------------------
       
  2194 // CThumbnailStore::AutoFlushTimerCallBack()
       
  2195 // ---------------------------------------------------------------------------
       
  2196 //
       
  2197 TInt CThumbnailStore::AutoFlushTimerCallBack(TAny* aAny)
       
  2198     {
       
  2199     TN_DEBUG1( "CThumbnailStore::AutoFlushTimerCallBack()");
       
  2200     CThumbnailStore* self = static_cast<CThumbnailStore*>( aAny );
       
  2201     
       
  2202     self->FlushCacheTable(ETrue);
       
  2203 
       
  2204     return KErrNone; // Return value ignored by CPeriodic
       
  2205     }
       
  2206 
       
  2207 TInt CThumbnailStore::CheckRowIDsL()
       
  2208     {
       
  2209     TN_DEBUG1( "CThumbnailStore::CheckRowIDs()");
       
  2210     
       
  2211     RSqlStatement stmt;
       
  2212     CleanupClosePushL( stmt );
       
  2213     TInt column = 0;   
       
  2214     TInt rowStatus = 0;
       
  2215     TInt64 inforows = 0;
       
  2216     TInt64 datarows = 0;
       
  2217     
       
  2218     TInt ret = stmt.Prepare( iDatabase, KGetInfoRowID );
       
  2219     if(ret < 0)
       
  2220         {
       
  2221         stmt.Close();
       
  2222         CleanupStack::PopAndDestroy( &stmt );
       
  2223         return KErrNotSupported;
       
  2224         }
       
  2225     rowStatus = stmt.Next();
       
  2226                 
       
  2227     if ( rowStatus == KSqlAtRow)    
       
  2228         {        
       
  2229         inforows = stmt.ColumnInt64( column );  
       
  2230         }
       
  2231             
       
  2232     stmt.Close();
       
  2233     CleanupStack::PopAndDestroy( &stmt );
       
  2234             
       
  2235     CleanupClosePushL( stmt );
       
  2236     ret = stmt.Prepare( iDatabase, KGetDataRowID );
       
  2237     if(ret < 0)
       
  2238         {
       
  2239         stmt.Close();
       
  2240         CleanupStack::PopAndDestroy( &stmt );
       
  2241         return KErrNotSupported;
       
  2242         }
       
  2243     rowStatus = stmt.Next();
       
  2244                        
       
  2245     if ( rowStatus == KSqlAtRow)    
       
  2246         {        
       
  2247         datarows = stmt.ColumnInt64( column );  
       
  2248         }
       
  2249             
       
  2250     stmt.Close();
       
  2251     CleanupStack::PopAndDestroy( &stmt );
       
  2252             
       
  2253     if( inforows != datarows)
       
  2254         {
       
  2255         return KErrNotSupported;
       
  2256         }  
       
  2257     else
       
  2258         {
       
  2259         return KErrNone;
       
  2260         }
       
  2261     }
       
  2262 
       
  2263 void CThumbnailStore::CheckModifiedByIdL( TUint32 aId, TBool aTempTable,
       
  2264     TBool& aModified )
       
  2265     {
       
  2266     TN_DEBUG1( "CThumbnailStore::CheckModifiedByIdL()");
       
  2267     
       
  2268     RSqlStatement stmt;
       
  2269     CleanupClosePushL( stmt );
       
  2270     TInt column( 0 );
       
  2271     
       
  2272     if( aTempTable )
       
  2273         {
       
  2274         User::LeaveIfError( stmt.Prepare( iDatabase, KThumbnailSelectTempPathModifiedByID ) );
       
  2275         }
       
  2276     else
       
  2277         {
       
  2278         User::LeaveIfError( stmt.Prepare( iDatabase, KThumbnailSelectPathModifiedByID ) );
       
  2279         }
       
  2280     TInt paramIndex = stmt.ParameterIndex( KThumbnailSqlParamId );
       
  2281     User::LeaveIfError( paramIndex );
       
  2282     User::LeaveIfError( stmt.BindInt( paramIndex, aId ));
       
  2283      
       
  2284     TInt rowStatus = stmt.Next();
       
  2285     
       
  2286     if(rowStatus == KSqlAtRow)
       
  2287         {
       
  2288         TPath path = stmt.ColumnTextL(column++);
       
  2289            
       
  2290         if (path.Length())
       
  2291             {
       
  2292             TInt64 modified = stmt.ColumnInt64( column );
       
  2293             TTime timeStamp;
       
  2294             iFs.Modified( path, timeStamp );
       
  2295             
       
  2296             if( modified != timeStamp.Int64() )
       
  2297                 {
       
  2298                 aModified = ETrue;
       
  2299                 }
       
  2300             }
       
  2301         }
       
  2302     
       
  2303     stmt.Close();
       
  2304     CleanupStack::PopAndDestroy( &stmt );
       
  2305     }
       
  2306 
       
  2307 void CThumbnailStore::CheckModifiedByPathL( const TDesC& aPath, TBool aTempTable,
       
  2308     TBool& aModified )
       
  2309     {
       
  2310     TN_DEBUG1( "CThumbnailStore::CheckModifiedByPathL()");
       
  2311     
       
  2312     RSqlStatement stmt;
       
  2313     CleanupClosePushL( stmt );
       
  2314     TInt column( 0 );
       
  2315     
       
  2316     if( aTempTable )
       
  2317         {
       
  2318         User::LeaveIfError( stmt.Prepare( iDatabase, KThumbnailSelectTempModifiedByPath ) );
       
  2319         }
       
  2320     else
       
  2321         {
       
  2322         User::LeaveIfError( stmt.Prepare( iDatabase, KThumbnailSelectModifiedByPath ) );
       
  2323         }
       
  2324     TInt paramIndex = stmt.ParameterIndex( KThumbnailSqlParamPath );
       
  2325     User::LeaveIfError( paramIndex );
       
  2326     User::LeaveIfError( stmt.BindText( paramIndex, aPath ));
       
  2327      
       
  2328     TInt rowStatus = stmt.Next();
       
  2329     
       
  2330     if(rowStatus == KSqlAtRow)
       
  2331         {
       
  2332         TInt64 modified = stmt.ColumnInt64( column );
       
  2333         TTime timeStamp;
       
  2334         iFs.Modified( aPath, timeStamp );
       
  2335         
       
  2336         if( modified != timeStamp.Int64() )
       
  2337             {
       
  2338             aModified = ETrue;
       
  2339             }
       
  2340         }
       
  2341     
       
  2342     stmt.Close();
       
  2343     CleanupStack::PopAndDestroy( &stmt );
       
  2344     }
       
  2345 
       
  2346 // -----------------------------------------------------------------------------
       
  2347 // RemoveDbFlagL()
       
  2348 // -----------------------------------------------------------------------------
       
  2349 //
       
  2350 void CThumbnailStore::RemoveDbFlagL(TThumbnailDbFlags aFlag)
       
  2351     {
       
  2352     TN_DEBUG1( "CThumbnailStore::RemoveBlacklistedFlag()" );
       
  2353     
       
  2354     RSqlStatement stmt;
       
  2355     CleanupClosePushL( stmt );
       
  2356 
       
  2357     User::LeaveIfError( stmt.Prepare( iDatabase, KThumbnailRemoveBlacklistedFlag ));
       
  2358     
       
  2359     TInt paramIndex = stmt.ParameterIndex( KThumbnailSqlParamFlag );
       
  2360     User::LeaveIfError( paramIndex );
       
  2361     User::LeaveIfError( stmt.BindInt( paramIndex, aFlag ));
       
  2362 
       
  2363     TInt err = stmt.Exec();
       
  2364    
       
  2365     TN_DEBUG2( "CThumbnailStore::RemoveBlacklistedFlag() - main table, err=%d", err );
       
  2366     
       
  2367     CleanupStack::PopAndDestroy( &stmt );
       
  2368     }
       
  2369 
       
  2370 // End of file