Fixed prevent issue
[apps/osp/Gallery.git] / src / GlFileUpdateTimer.cpp
index c3465cd..1ef83cc 100644 (file)
@@ -1,7 +1,7 @@
 //
 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
 //
-// Licensed under the Flora License, Version 1.0 (the License);
+// Licensed under the Flora License, Version 1.1 (the License);
 // you may not use this file except in compliance with the License.
 // You may obtain a copy of the License at
 //
@@ -19,6 +19,8 @@
  * @brief              This is the source file for FileUpdateTimer class.
  */
 
+#include <FIo.h>
+
 #include "GlFileUpdateTimer.h"
 #include "GlFileListPresentationModel.h"
 
@@ -27,6 +29,7 @@ using namespace Tizen::Base::Collection;
 using namespace Tizen::Base::Utility;
 using namespace Tizen::Content;
 using namespace Tizen::Io;
+using namespace Tizen::System;
 using namespace Tizen::Ui;
 using namespace Tizen::Ui::Controls;
 using namespace Tizen::Ui::Scenes;
@@ -40,44 +43,169 @@ FileUpdateTimer::FileUpdateTimer(Tizen::Base::Collection::IList* pDelList
        , __pUpdateIndexList(pDelList)
        , __pFileUpdateManager(pFDM)
        , __rotateMode(rotateMode)
+       , __scanDirMap(SingleObjectDeleter)
+       , __pContentManager(null)
 {
+       __scanDirMap.Construct();
+       DeviceManager::AddDeviceEventListener(DEVICE_TYPE_STORAGE_CARD, *this);
 }
 
 FileUpdateTimer::~FileUpdateTimer(void)
 {
+       AppLogDebug("ENTER");
+       DeviceManager::RemoveDeviceEventListener(DEVICE_TYPE_STORAGE_CARD, *this);
+       ScanDirectories();
+       delete __pUpdateIndexList;
+       delete __pContentManager;
+       AppLogDebug("EXIT");
+}
+
+IList *
+FileUpdateTimer::TimerStart(void)
+{
+       AppLogDebug("ENTER");
+       __pContentManager = new (std::nothrow) ContentManager();
+       __pContentManager->Construct();
+       IList* pCntIdList = __pFileUpdateManager->GetContentIdListAtIndexN(*__pUpdateIndexList);
+       AppLogDebug("EXIT");
+       return pCntIdList;
+}
+
+result
+FileUpdateTimer::TimerExpired(const Object* contentId)
+{
+       AppLogDebug("ENTER");
        if (_actionId == FILE_ACTION_DELETE)
        {
-               delete __pUpdateIndexList;
+               const ContentId* cntId = static_cast<const ContentId*>(contentId);
+               if(cntId == null)
+               {
+                       return E_FAILURE;
+               }
+               ContentInfo* cntInfo = __pContentManager->GetContentInfoN(*cntId);
+               if (cntInfo)
+               {
+                       String filePath = cntInfo->GetContentPath();
+                       delete cntInfo;
+                       result r = __pFileUpdateManager->DeleteContentFile(filePath);
+                       if (!IsFailed(r))
+                       {
+                               String dirPath;
+                               int index =0;
+                               result r = filePath.LastIndexOf(DIRECTORY_SEPARATOR, filePath.GetLength() - 1, index);
+                               TryReturn(r == E_SUCCESS, false, "[%s] Unable to get Dirpath", GetErrorMessage(r));
+                               filePath.SubString(0, index, dirPath);
+                               if(!__scanDirMap.ContainsKey(dirPath))
+                               {
+                                       __scanDirMap.Add(new String(dirPath), new Integer(0));
+                               }
+                       }
+                       return r;
+               }
+               else
+               {
+                       return E_FAILURE;
+               }
+       }
+       else
+       {
+               result r = E_SUCCESS;
+               const ContentId* cntId = static_cast<const ContentId*>(contentId);
+               if (contentId != null)
+               {
+                   r = __pFileUpdateManager->RotateImage(*cntId, __rotateMode);
+                   if ( r == E_SUCCESS )
+                   {
+                       __pFileUpdateManager->RequestThumbnail(*cntId); // do not request thumbnail for video files
+                   }
+                   else
+                   {
+                       return r;
+                   }
+               }
+               return r;
        }
+       AppLogDebug("EXIT");
 }
 
-IList * FileUpdateTimer::TimerStart(void)
+result
+FileUpdateTimer::ScanDirectories(void)
 {
+       AppLogDebug("ENTER");
+       RequestId reqId;
+       result r = E_SUCCESS;
        if (_actionId == FILE_ACTION_DELETE)
        {
-               return __pFileUpdateManager->GetContentIdListAtIndexN(*__pUpdateIndexList);
+               int scanCount = __scanDirMap.GetCount();
+               if( scanCount == 0)
+               {
+                       return r;
+               }
+               IMapEnumerator* pMapEnum = __scanDirMap.GetMapEnumeratorN();
+               String* pKey = null;
+               while (pMapEnum->MoveNext() == E_SUCCESS)
+               {
+                       pKey = static_cast< String* > (pMapEnum->GetKey());
+                       r = ContentManager::ScanDirectory(*(pKey), true, null, reqId);
+               }
+               __scanDirMap.RemoveAll();
+               AppLogDebug("EXIT(%s)", GetErrorMessage(r));
+               return r;
        }
        else
        {
-               return __pUpdateIndexList;
+               AppLogDebug("EXIT, nothing to be done");
+               return E_SUCCESS;
        }
 }
 
-result FileUpdateTimer::TimerExpired(const Object* contentId)
+int
+FileUpdateTimer::GetDirScanCount()
 {
-       if (_actionId == FILE_ACTION_DELETE)
+       IMapEnumerator* pMapEnum = __scanDirMap.GetMapEnumeratorN();
+       String* pKey = null;
+       while (pMapEnum->MoveNext() == E_SUCCESS)
        {
-               return __pFileUpdateManager->DeleteContentFile(*(static_cast<const ContentId*>(contentId)));
+               pKey = static_cast< String* > (pMapEnum->GetKey());
+               if (File::IsFileExist(*pKey) == false)
+               {
+                       __scanDirMap.Remove(*pKey);
+               }
        }
-       else
+       delete pMapEnum;
+       return __scanDirMap.GetCount();
+}
+
+void
+FileUpdateTimer::OnDeviceStateChanged(DeviceType deviceType, const Tizen::Base::String& state)
+{
+       AppLogDebug("ENTER");
+       if (deviceType == DEVICE_TYPE_STORAGE_CARD && state == DEVICE_STORAGE_CARD_UNMOUNTED &&
+                       _actionId == FILE_ACTION_DELETE)
        {
-               result r = E_SUCCESS;
-               const Integer* pIndex = static_cast<const Integer*>(contentId);
-               if (pIndex != null)
+               String sdcPath = Environment::GetExternalStoragePath();
+               IMapEnumerator* pMapEnum = __scanDirMap.GetMapEnumeratorN();
+               String* pKey = null;
+               while (pMapEnum->MoveNext() == E_SUCCESS)
                {
-                       r = __pFileUpdateManager->RotateImage(pIndex->ToInt(), __rotateMode);
-                       __pFileUpdateManager->RequestThumbnail(pIndex->ToInt());
+                       pKey = static_cast< String* > (pMapEnum->GetKey());
+                       if (pKey->StartsWith(sdcPath, 0))
+                       {
+                               __scanDirMap.Remove(*pKey);
+                       }
                }
-               return r;
+               delete pMapEnum;
        }
+       AppLogDebug("EXIT");
+}
+
+void
+FileUpdateTimer::TimerCancel(int, enum FileActionCancelRes res)
+{
+       DeviceManager::RemoveDeviceEventListener(DEVICE_TYPE_STORAGE_CARD, *this);
+}
+void
+FileUpdateTimer::TimerComplete(int, enum FileActionCompleteRes res)
+{
+       DeviceManager::RemoveDeviceEventListener(DEVICE_TYPE_STORAGE_CARD, *this);
 }