EngSrc/IEImageFinder.cpp
changeset 3 93fff7023be8
equal deleted inserted replaced
2:e1e28b0273b0 3:93fff7023be8
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors: Juha Kauppinen, Mika Hokkanen
       
    13 * 
       
    14 * Description: Photo Browser
       
    15 *
       
    16 */
       
    17 
       
    18 // INCLUDE FILES
       
    19 
       
    20 #include <eikenv.h>
       
    21 #include <exifread.h>
       
    22 #include <f32file.h>
       
    23 #include "IEImageFinder.h"
       
    24 #include "IEImageList.h"
       
    25 #include "IEImageProcessing.h"
       
    26 #include "IEImageData.h"
       
    27 #include "IEEngineImp.h"
       
    28 #include "IEEngineUtils.h"
       
    29 #ifdef _S60_5x_ACCELEROMETER_
       
    30 #include "IESensorMonitor.h"
       
    31 #endif
       
    32 
       
    33 #define LATETHUMBCHECK
       
    34 
       
    35 // ================= MEMBER FUNCTIONS =======================
       
    36 
       
    37 CIEImageFinder* CIEImageFinder::NewL(
       
    38         CIEFileLoader* aCallback, 
       
    39         RArray<CImageData*>& aFileNameData, 
       
    40         RArray<CImageData*>& aFaceFileNameData,
       
    41         RCriticalSection* aCritical)
       
    42     {
       
    43     CIEImageFinder* self = new(ELeave) CIEImageFinder(aCallback, aFileNameData, aFaceFileNameData, aCritical);
       
    44     CleanupStack::PushL(self);
       
    45     self->ConstructL();
       
    46     CleanupStack::Pop();
       
    47     return self;
       
    48     }
       
    49 
       
    50 CIEImageFinder* CIEImageFinder::NewLC(
       
    51         CIEFileLoader* aCallback, 
       
    52         RArray<CImageData*>& aFileNameData, 
       
    53         RArray<CImageData*>& aFaceFileNameData, 
       
    54         RCriticalSection* aCritical)
       
    55     {
       
    56     CIEImageFinder* self = new(ELeave) CIEImageFinder(aCallback, aFileNameData, aFaceFileNameData, aCritical);
       
    57     CleanupStack::PushL( self );
       
    58     self->ConstructL();
       
    59     return self;
       
    60     }
       
    61 
       
    62 CIEImageFinder::CIEImageFinder(
       
    63         CIEFileLoader* aCallback, 
       
    64         RArray<CImageData*>& aFileNameData, 
       
    65         RArray<CImageData*>& aFaceFileNameData, 
       
    66         RCriticalSection* aCritical) :
       
    67         //iList(aFileNameData, aCallback),
       
    68         iCallback(aCallback),
       
    69         iFileNameData(aFileNameData),
       
    70         iFaceFileNameData(aFaceFileNameData),
       
    71         iCritical(aCritical),
       
    72         iIEEngineUtils(iFileServer)
       
    73     {
       
    74     DP0_IMAGIC(_L("CIEImageFinder::CIEImageFinder++ --"));
       
    75     }
       
    76 
       
    77 
       
    78 void CIEImageFinder::ConstructL()
       
    79     {
       
    80     DP0_IMAGIC(_L("CIEImageFinder::ConstructL++"));
       
    81     
       
    82     User::LeaveIfError(iFileServer.Connect());
       
    83     
       
    84     iImageList = &iCallback->GetImageList();
       
    85     
       
    86     //iFileSystemMonitor = CFileSystemMonitorAO::NewL(iFileServer, this);
       
    87     
       
    88     // Creating Engine Utility class pointer */
       
    89         
       
    90     DP0_IMAGIC(_L("CIEImageFinder::ConstructL--"));
       
    91     }
       
    92 
       
    93 CIEImageFinder::~CIEImageFinder()
       
    94     {
       
    95     DP0_IMAGIC(_L("CIEImageFinder::~CIEImageFinder++"));
       
    96   	
       
    97     iFileServer.Close();
       
    98   	
       
    99   	DP0_IMAGIC(_L("CIEImageFinder::~CIEImageFinder--"));
       
   100     }
       
   101 
       
   102 TBool CIEImageFinder::IsSearching() const
       
   103     {
       
   104     return (iCallback->ImageFinderState() == CIEFileLoader::EImageFinderRunning);
       
   105     }
       
   106 
       
   107 void CIEImageFinder::StartFinderL(const TDesC& aSearchName)
       
   108     {
       
   109     DP0_IMAGIC(_L("CIEImageFinder::StartFinderL++"));
       
   110 
       
   111 #ifdef IMAGIC_DATABASE
       
   112     // Read initial list from database
       
   113     TRAP_IGNORE(iImageList->ReadDatabaseL());
       
   114 #endif    
       
   115     if (IsSearching())
       
   116         SearchFilesL(aSearchName);
       
   117     
       
   118     DP0_IMAGIC(_L("CIEImageFinder::StartFinderL--"));
       
   119     }
       
   120 
       
   121 void CIEImageFinder::SearchFilesL(const TDesC& aSearchName)
       
   122     {
       
   123     DP0_IMAGIC(_L("CIEImageFinder::SearchFilesL++"));
       
   124      
       
   125     // Use phone memory, external memory card and internal memory card for file scanning 
       
   126     TFileName rootPathPhoneMemory = PathInfo::PhoneMemoryRootPath();
       
   127     rootPathPhoneMemory.Append(ImagePath);
       
   128      
       
   129     TFileName rootPathMCard = PathInfo::MemoryCardRootPath();
       
   130     rootPathMCard.Append(ImagePath);
       
   131      
       
   132     TFileName rootPathFDrive;
       
   133     rootPathFDrive.Copy(KRootPathFDrive);
       
   134     rootPathFDrive.Append(ImagePath);
       
   135      
       
   136     //Append drives to array
       
   137     RArray<TFileName> drives;
       
   138     drives.Append(rootPathPhoneMemory);
       
   139     drives.Append(rootPathMCard);
       
   140     drives.Append(rootPathFDrive);
       
   141 
       
   142     DP0_IMAGIC(_L("CIEImageFinder::SearchFilesL start"));    
       
   143     
       
   144     for(TInt i=0; i<drives.Count() && IsSearching(); i++)
       
   145         {
       
   146         if(BaflUtils::PathExists(iFileServer, drives[i]))
       
   147             {
       
   148             DP2_IMAGIC(_L("CIEImageFinder::SearchFilesL  aRootPath = %S, aSearchName = %S ++"), &drives[i], &aSearchName);
       
   149              
       
   150             CDirScan* dirScan = CDirScan::NewL(iFileServer);
       
   151              
       
   152             TRAPD(err, dirScan->SetScanDataL(
       
   153                     drives[i], 
       
   154                     KEntryAttDir|KEntryAttMatchExclusive, 
       
   155                     EDirDescending/*|EDescending*/|ESortByDate, 
       
   156                     CDirScan::EScanUpTree));
       
   157 
       
   158             if(err == KErrNone)
       
   159                 {
       
   160                 CDir* dir;
       
   161                 while(IsSearching())
       
   162                     {
       
   163                     TRAP(err, dirScan->NextL(dir));
       
   164                     if(err != KErrNone || dir == NULL)
       
   165                         {
       
   166                         if (dir)
       
   167                             delete dir;
       
   168                         break;
       
   169                         }
       
   170                   
       
   171                     TRAP(err, ScanDirL(dir, dirScan->FullPath(), aSearchName));
       
   172                     delete dir;
       
   173                     if (err != KErrNone)
       
   174                         break;
       
   175                     }
       
   176                 }
       
   177           
       
   178             delete dirScan;
       
   179             }
       
   180         }
       
   181     
       
   182 #ifdef IMAGIC_DATABASE      
       
   183         // Remove files from deleted directories
       
   184     if (IsSearching())
       
   185         iImageList->RemoveNonExistImagesL(NULL, iFileServer);
       
   186 #endif
       
   187     
       
   188 #ifdef LATETHUMBCHECK    
       
   189     // Check all thumbnails
       
   190     for (TInt i = 0;i < iFileNameData.Count() && IsSearching();i++)
       
   191         {
       
   192         CheckCreatedThumbnails(*iFileNameData[i]);
       
   193         
       
   194         // Number of faces is unknown and thumbnail exists
       
   195         if (iFileNameData[i]->GetNumberOfFaces() < 0 &&
       
   196             iFileNameData[i]->IsImageReady(ESize512x512))
       
   197             {
       
   198             // Read number of faces from thumb's exif
       
   199             RArray<TRect> faceCoordinates;
       
   200             TFileName fileName;
       
   201             iFileNameData[i]->GetFileName(fileName, ESize512x512);
       
   202             TRAPD(error, iIEEngineUtils.ReadFaceCoordinatesL(fileName, faceCoordinates));
       
   203             if (error == KErrNone)
       
   204                 {
       
   205                 TInt count = faceCoordinates.Count();
       
   206                 DP2_IMAGIC(_L("Read face# %d for %S"), count, &fileName);
       
   207                 iFileNameData[i]->SetNumberOfFaces(count);
       
   208                 iImageList->SetChanged(fileName);
       
   209                 }
       
   210             }
       
   211         }
       
   212 #endif    
       
   213 
       
   214     // Let engine know that all file has been added to filename array
       
   215     //iCallback->AllFilesAddedToFilenameArray();
       
   216          
       
   217     drives.Close();
       
   218 
       
   219 #ifdef IMAGIC_DATABASE    
       
   220     // Write list to database
       
   221     TRAP_IGNORE(iImageList->WriteDatabaseL());
       
   222 #endif    
       
   223     
       
   224     DP0_IMAGIC(_L("CIEImageFinder::SearchFilesL--"));
       
   225     }
       
   226 
       
   227 void CIEImageFinder::ScanDirL(CDir* aDir, const TDesC& aDirPath, const TDesC& aWild)
       
   228     {
       
   229     DP1_IMAGIC(_L("CIEImageFinder::ScanDirL++ %S"), &aDirPath);
       
   230     
       
   231     TParse parse;
       
   232     parse.Set(aWild, &aDirPath, NULL);
       
   233     TPtrC spec(parse.FullName());
       
   234     
       
   235     /*if(aDirPath.Find(KCRootBgroundImages) != KErrNotFound)
       
   236         return;*/
       
   237      
       
   238     TFindFile findFile(iFileServer);
       
   239      
       
   240     if (findFile.FindWildByPath(parse.FullName(), NULL, aDir) == KErrNone)
       
   241         {
       
   242         // Sort in time order
       
   243         aDir->Sort(EDescending|ESortByDate);
       
   244 
       
   245         // Go through dir in inverted order
       
   246         for(TInt i = 0;i < aDir->Count() && IsSearching();i++)
       
   247             {
       
   248             parse.Set((*aDir)[i].iName, &spec, NULL);
       
   249                
       
   250             // Full filename with path of original image
       
   251             TFileName imageFileName;
       
   252             imageFileName = parse.FullName();
       
   253                
       
   254             // Check if file exist and not be hidden
       
   255             TBool visible = EFalse;
       
   256             TRAPD(err, visible = iImageList->IsImageViewableL(imageFileName, iFileServer));
       
   257             if (err != KErrNone || !visible)
       
   258                 {
       
   259                 DP0_IMAGIC(_L("CIEImageFinder::ScanDirL - not found or hidden file"));
       
   260                 continue;
       
   261                 }
       
   262             
       
   263             CImageData* imageData = iImageList->GetImageData(imageFileName);
       
   264             
       
   265             // Read file time
       
   266             TTime fileTime(0);
       
   267             TRAP(err, iIEEngineUtils.GetModifiedTimeL(imageFileName, fileTime));
       
   268             if (err != KErrNone)
       
   269                 {
       
   270                 DP0_IMAGIC(_L("CIEImageFinder::ScanDirL - get file time failed"));
       
   271 
       
   272                 // File is locked for writing, use old file time if possible
       
   273                 if (imageData == NULL)
       
   274                     continue;
       
   275                 else
       
   276                     fileTime = imageData->GetFileTime();
       
   277                 }
       
   278             
       
   279             // Image already exist in list
       
   280             if (imageData != NULL)
       
   281                 {
       
   282                 DP0_IMAGIC(_L("CIEImageFinder::ScanDirL - image exist"));
       
   283                 // File has been changed
       
   284                 if (imageData->GetFileTime() != fileTime)
       
   285                     {
       
   286                     DP0_IMAGIC(_L("CIEImageFinder::ScanDirL - file time is different"));
       
   287                     // Delete thumbnails and remove image from the list
       
   288                     iIEEngineUtils.DeleteThumbnails(imageFileName, iFileServer);
       
   289                     iImageList->Remove(iImageList->GetImageIndex(imageData), iFileServer);
       
   290                     imageData = NULL;
       
   291                     }
       
   292                 }
       
   293 
       
   294             // Image already exist
       
   295             if (imageData == NULL)
       
   296                 {
       
   297                 // Read aspect ratio
       
   298                 TSize size;
       
   299                 TRAP(err, iIEEngineUtils.GetImageSizeL(imageFileName, size));
       
   300                 if (err != KErrNone)
       
   301                     {
       
   302                     DP0_IMAGIC(_L("CIEImageFinder::ScanDirL - get size failed"));
       
   303                     continue;
       
   304                     }
       
   305 
       
   306                 // Read EXIF created time (use file time if EXIF fails)
       
   307                 TTime createdTime;
       
   308                 TUint16 orientation = 0;
       
   309                 TRAP(err, iIEEngineUtils.GetExifDateTimeAndOrientationL(
       
   310                         imageFileName, 
       
   311                         createdTime, 
       
   312                         orientation));
       
   313                 
       
   314                 if (err != KErrNone) 
       
   315                     {
       
   316                     DP0_IMAGIC(_L("CIEImageFinder::ScanDirL - No EXIF date"));
       
   317                     createdTime = fileTime;
       
   318                     }
       
   319                   
       
   320                 // Add image to list
       
   321                 DP0_IMAGIC(_L("CIEImageFinder::ScanDirL - Call AddImageL"));
       
   322                 TRAP(err, imageData = iImageList->CreateImageDataL(imageFileName, createdTime, orientation));
       
   323                 DP1_IMAGIC(_L("CIEImageFinder::ScanDirL - AddImageL returned: err:% d"), err);
       
   324                 
       
   325                 if (err != KErrNone)
       
   326                     {
       
   327                     imageData = NULL;
       
   328                     }
       
   329                 else if (imageData) 
       
   330 				    {
       
   331                     imageData->SetFileTime(fileTime);
       
   332                     imageData->SetSize(size);
       
   333                     iImageList->AddImage(imageData);
       
   334                     }
       
   335                 iImageList->SetChanged(imageFileName);
       
   336                 }
       
   337             
       
   338             DP0_IMAGIC(_L("CIEImageFinder::ScanDirL - 03"));
       
   339             // Mark original image as ready
       
   340             if (imageData)
       
   341                 {
       
   342                 DP0_IMAGIC(_L("CIEImageFinder::ScanDirL - 04"));
       
   343                 imageData->SetImageReady(EFullSize, ETrue);
       
   344                 }
       
   345             }
       
   346         
       
   347             delete aDir;
       
   348         }
       
   349     
       
   350 #ifdef IMAGIC_DATABASE    
       
   351     //RemoveDeletedImagesL(*array, &aDirPath);
       
   352 #endif    
       
   353     
       
   354     DP0_IMAGIC(_L("CIEImageFinder::ScanDirL--"));
       
   355     }
       
   356 
       
   357 void CIEImageFinder::CheckCreatedThumbnails(CImageData& aImageData) const
       
   358     {
       
   359     TThumbSize sizes[] = { /*EFullSize,*/ ESize512x512, ESize128x128, ESize32x32 };
       
   360     
       
   361     TInt numSizes = sizeof(sizes) / sizeof(TThumbSize);
       
   362     for (TInt i = 0;i < numSizes; i++)
       
   363         {
       
   364         TFileName fileName;
       
   365         aImageData.GetFileName(fileName, sizes[i]);
       
   366         if (BaflUtils::FileExists(iFileServer, fileName))
       
   367             aImageData.SetImageReady(sizes[i], ETrue);
       
   368         else
       
   369             // Mark all smaller sizes non-exist
       
   370             //while (i < numSizes)
       
   371                 {
       
   372                 aImageData.SetImageReady(sizes[i], EFalse);
       
   373                 //i++;
       
   374                 }
       
   375         }    
       
   376     }
       
   377 
       
   378 void CIEImageFinder::FileSystemChanged()
       
   379     {
       
   380     //Scan filesystem for new images
       
   381     //SearchForNewFilesL(KRootImagePath, KFileString);
       
   382     
       
   383 #ifdef __WINS__
       
   384     SearchFilesL(KFileString);
       
   385 #else
       
   386     TFileName rootPath = PathInfo::MemoryCardRootPath();
       
   387     rootPath.Append(ImagePath);
       
   388     SearchFilesL(KFileString);
       
   389 #endif
       
   390     
       
   391     }