Prevent defect fix and Minor issue fix
[apps/osp/Gallery.git] / src / GlFileUpdateTimer.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.1 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://floralicense.org/license/
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an AS IS BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 /**
18  * @file                GlFileUpdateTimer.cpp
19  * @brief               This is the source file for FileUpdateTimer class.
20  */
21
22 #include "GlFileUpdateTimer.h"
23 #include "GlFileListPresentationModel.h"
24
25 using namespace Tizen::Base;
26 using namespace Tizen::Base::Collection;
27 using namespace Tizen::Base::Utility;
28 using namespace Tizen::Content;
29 using namespace Tizen::Io;
30 using namespace Tizen::System;
31 using namespace Tizen::Ui;
32 using namespace Tizen::Ui::Controls;
33 using namespace Tizen::Ui::Scenes;
34
35 FileUpdateTimer::FileUpdateTimer(Tizen::Base::Collection::IList* pDelList
36         , IContentFileUpdateManager* pFDM
37         , IFileOpInvalidateListener* invalidate
38         , FileActionMode actionId
39         , RotateMode rotateMode)
40         : GlTimerBase(invalidate, actionId)
41         , __pUpdateIndexList(pDelList)
42         , __pFileUpdateManager(pFDM)
43         , __rotateMode(rotateMode)
44         , __scanDirMap(SingleObjectDeleter)
45         , __pContentManager(null)
46 {
47         __scanDirMap.Construct();
48         DeviceManager::AddDeviceEventListener(DEVICE_TYPE_STORAGE_CARD, *this);
49 }
50
51 FileUpdateTimer::~FileUpdateTimer(void)
52 {
53         AppLogDebug("ENTER");
54         DeviceManager::RemoveDeviceEventListener(DEVICE_TYPE_STORAGE_CARD, *this);
55         ScanDirectories();
56         delete __pUpdateIndexList;
57         delete __pContentManager;
58         AppLogDebug("EXIT");
59 }
60
61 IList *
62 FileUpdateTimer::TimerStart(void)
63 {
64         AppLogDebug("ENTER");
65         __pContentManager = new (std::nothrow) ContentManager();
66         __pContentManager->Construct();
67         IList* pCntIdList = __pFileUpdateManager->GetContentIdListAtIndexN(*__pUpdateIndexList);
68         AppLogDebug("EXIT");
69         return pCntIdList;
70 }
71
72 result
73 FileUpdateTimer::TimerExpired(const Object* contentId)
74 {
75         AppLogDebug("ENTER");
76         if (_actionId == FILE_ACTION_DELETE)
77         {
78                 const ContentId* cntId = static_cast<const ContentId*>(contentId);
79                 if(cntId == null)
80                 {
81                         return E_FAILURE;
82                 }
83                 ContentInfo* cntInfo = __pContentManager->GetContentInfoN(*cntId);
84                 if (cntInfo)
85                 {
86                         String filePath = cntInfo->GetContentPath();
87                         delete cntInfo;
88                         result r = __pFileUpdateManager->DeleteContentFile(filePath);
89                         if (!IsFailed(r))
90                         {
91                                 String dirPath;
92                                 int index =0;
93                                 result r = filePath.LastIndexOf(DIRECTORY_SEPARATOR, filePath.GetLength() - 1, index);
94                                 TryReturn(r == E_SUCCESS, false, "[%s] Unable to get Dirpath", GetErrorMessage(r));
95                                 filePath.SubString(0, index, dirPath);
96                                 if(!__scanDirMap.ContainsKey(dirPath))
97                                 {
98                                         __scanDirMap.Add(new String(dirPath), new Integer(0));
99                                 }
100                         }
101                         return r;
102                 }
103                 else
104                 {
105                         return E_FAILURE;
106                 }
107         }
108         else
109         {
110                 result r = E_SUCCESS;
111                 const ContentId* cntId = static_cast<const ContentId*>(contentId);
112                 if (contentId != null)
113                 {
114                     r = __pFileUpdateManager->RotateImage(*cntId, __rotateMode);
115                     if ( r == E_SUCCESS )
116                     {
117                         __pFileUpdateManager->RequestThumbnail(*cntId); // do not request thumbnail for video files
118                     }
119                     else
120                     {
121                         return r;
122                     }
123                 }
124                 return r;
125         }
126         AppLogDebug("EXIT");
127 }
128
129 result
130 FileUpdateTimer::ScanDirectories(void)
131 {
132         AppLogDebug("ENTER");
133         RequestId reqId;
134         result r = E_SUCCESS;
135         if (_actionId == FILE_ACTION_DELETE)
136         {
137                 int scanCount = __scanDirMap.GetCount();
138                 if( scanCount == 0)
139                 {
140                         return r;
141                 }
142                 IMapEnumerator* pMapEnum = __scanDirMap.GetMapEnumeratorN();
143                 String* pKey = null;
144                 while (pMapEnum->MoveNext() == E_SUCCESS)
145                 {
146                         pKey = static_cast< String* > (pMapEnum->GetKey());
147                         r = ContentManager::ScanDirectory(*(pKey), true, null, reqId);
148                 }
149                 __scanDirMap.RemoveAll();
150                 AppLogDebug("EXIT(%s)", GetErrorMessage(r));
151                 return r;
152         }
153         else
154         {
155                 AppLogDebug("EXIT, nothing to be done");
156                 return E_SUCCESS;
157         }
158 }
159
160 int
161 FileUpdateTimer::GetDirScanCount() const
162 {
163         return __scanDirMap.GetCount();
164 }
165
166 void
167 FileUpdateTimer::OnDeviceStateChanged(DeviceType deviceType, const Tizen::Base::String& state)
168 {
169         AppLogDebug("ENTER");
170         if (deviceType == DEVICE_TYPE_STORAGE_CARD && state == DEVICE_STORAGE_CARD_UNMOUNTED &&
171                         _actionId == FILE_ACTION_DELETE)
172         {
173                 String sdcPath = Environment::GetExternalStoragePath();
174                 IMapEnumerator* pMapEnum = __scanDirMap.GetMapEnumeratorN();
175                 String* pKey = null;
176                 while (pMapEnum->MoveNext() == E_SUCCESS)
177                 {
178                         pKey = static_cast< String* > (pMapEnum->GetKey());
179                         if (pKey->StartsWith(sdcPath, 0))
180                         {
181                                 __scanDirMap.Remove(*pKey);
182                         }
183                 }
184                 delete pMapEnum;
185         }
186         AppLogDebug("EXIT");
187 }
188
189 void
190 FileUpdateTimer::TimerCancel(int, enum FileActionCancelRes res)
191 {
192         DeviceManager::RemoveDeviceEventListener(DEVICE_TYPE_STORAGE_CARD, *this);
193 }
194 void
195 FileUpdateTimer::TimerComplete(int, enum FileActionCompleteRes res)
196 {
197         DeviceManager::RemoveDeviceEventListener(DEVICE_TYPE_STORAGE_CARD, *this);
198 }