Fixed prevent issue
[apps/osp/Gallery.git] / src / GlFileMoveTimer.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                FileMoveTimer.cpp
19  * @brief               This is the source file for FileMoveTimer class.
20  */
21
22 #include <FBase.h>
23 #include <FContent.h>
24 #include <FMedia.h>
25 #include <FSystem.h>
26 #include <FUi.h>
27
28 #include "GlFileListPresentationModel.h"
29 #include "GlFileMoveTimer.h"
30
31 using namespace Tizen::Base;
32 using namespace Tizen::Base::Collection;
33 using namespace Tizen::Base::Utility;
34 using namespace Tizen::Content;
35 using namespace Tizen::Io;
36 using namespace Tizen::System;
37 using namespace Tizen::Ui;
38 using namespace Tizen::Ui::Controls;
39 using namespace Tizen::Ui::Scenes;
40
41 FileMoveTimer::FileMoveTimer(String& destDirectory, IList* list, FileListPresentationModel* presentationModel,
42                 IFileOpInvalidateListener* pInvalidate, bool isCopyOperation)
43         : GlTimerBase(pInvalidate, FILE_ACTION_MOVE)
44         , __pMoveIndexList(list)
45         , __moveToDir(destDirectory)
46         , __isCopyOperation(false)
47         , __pPresentationModel(presentationModel)
48         , __scanDirMap(SingleObjectDeleter)
49         , __pContentManager(null)
50 {
51         AppLogDebug("ENTER");
52         __scanDirMap.Construct();
53         DeviceManager::AddDeviceEventListener(DEVICE_TYPE_STORAGE_CARD, *this);
54         AppLogDebug("EXIT");
55 }
56
57 FileMoveTimer::~FileMoveTimer(void)
58 {
59         AppLogDebug("ENTER");
60         DeviceManager::RemoveDeviceEventListener(DEVICE_TYPE_STORAGE_CARD, *this);
61         ScanDirectories();
62         delete __pMoveIndexList;
63         delete __pContentManager;
64         AppLogDebug("EXIT");
65 }
66
67 IList *
68 FileMoveTimer::TimerStart(void)
69 {
70         AppLogDebug("ENTER");
71         if (&__moveToDir == null || __moveToDir.IsEmpty())
72         {
73                 AppLogDebug("EXIT 1(%s)", GetErrorMessage(GetLastResult()));
74                 return null;
75         }
76
77         if (__pMoveIndexList == null || __pMoveIndexList->GetCount() <= 0)
78         {
79                 AppLogDebug("EXIT 2(%s)", GetErrorMessage(GetLastResult()));
80                 return null;
81         }
82
83         if (__pMoveIndexList->GetCount() > 0)
84         {
85                 __pContentManager = new (std::nothrow) ContentManager();
86                 __pContentManager->Construct();
87                 if (__isCopyOperation == true)
88                 {
89                         SetActionMode(FILE_ACTION_COPY);
90                 }
91                 if (File::IsFileExist(__moveToDir) == false)
92                 {
93                         result r = Directory::Create(__moveToDir, true);
94                         AppLog("The directory create result is 2(%s)", GetErrorMessage(r));
95
96                         if ( r == E_STORAGE_FULL)
97                         {
98                                 return null;
99                         }
100
101                 }
102                 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
103                 IList* pCntIdList = __pPresentationModel->GetContentIdListAtIndexN(*__pMoveIndexList);
104                 return pCntIdList;
105         }
106         else
107         {
108                 return null;
109         }
110 }
111
112 result
113 FileMoveTimer::TimerExpired(const Object* contentId)
114 {
115         AppLogDebug("ENTER");
116         const ContentId* cntId = static_cast<const ContentId*>(contentId);
117         if(cntId == null)
118         {
119                 return E_FAILURE;
120         }
121         ContentInfo* cntInfo = __pContentManager->GetContentInfoN(*cntId);
122         if (cntInfo)
123         {
124                 String filePath = cntInfo->GetContentPath();
125                 delete cntInfo;
126                 result r = __pPresentationModel->MoveToContentFile(filePath, __moveToDir, __isCopyOperation);
127                 if (!IsFailed(r))
128                 {
129                         if (__isCopyOperation == false)
130                         {
131                                 //For Move operation, add the directory being moved from for scanning
132                                 String dirPath;
133                                 int index =0;
134                                 result r = filePath.LastIndexOf(DIRECTORY_SEPARATOR, filePath.GetLength() - 1, index);
135                                 TryReturn(r == E_SUCCESS, false, "[%s] Unable to get Dirpath", GetErrorMessage(r));
136                                 filePath.SubString(0, index, dirPath);
137                                 if(!__scanDirMap.ContainsKey(dirPath))
138                                 {
139                                         __scanDirMap.Add(new String(dirPath), new Integer(0));
140                                 }
141                         }
142                         if (!__scanDirMap.ContainsKey(__moveToDir))
143                         {
144                                 __scanDirMap.Add(new String(__moveToDir), new Integer(0));
145                         }
146                 }
147                 return r;
148         }
149         return E_FAILURE;
150 }
151
152 result
153 FileMoveTimer::ScanDirectories(void)
154 {
155         AppLogDebug("ENTER");
156         RequestId reqId;
157         result r = E_FAILURE;
158         if (__scanDirMap.GetCount() > 0)
159         {
160                 AppLogDebug("Print AlbumScanMoveToDirPath 2(%d)", __scanDirMap.GetCount());
161                 IMapEnumerator* pMapEnum = __scanDirMap.GetMapEnumeratorN();
162                 String* pKey = null;
163                 while (pMapEnum->MoveNext() == E_SUCCESS)
164                 {
165                         pKey = static_cast< String* > (pMapEnum->GetKey());
166                         r = ContentManager::ScanDirectory(*(pKey), true, null, reqId);
167                 }
168                 __scanDirMap.RemoveAll();
169                 delete pMapEnum;
170         }
171         AppLogDebug("EXIT(%s)", GetErrorMessage(r));
172         return r;
173 }
174
175 void
176 FileMoveTimer::TimerCancel(int, enum FileActionCancelRes res)
177 {
178         AppLogDebug("ENTER");
179         DeviceManager::RemoveDeviceEventListener(DEVICE_TYPE_STORAGE_CARD, *this);
180         if (res == CANCEL_USER || (res == CANCEL_SYS_ERROR && GetMovedCount() > 0) )
181         {
182                 String albumName = __pPresentationModel->ConvertToAlbumName(__moveToDir);
183
184                 if (albumName != EMPTY_SPACE)
185                 {
186                         IList* pDirectoryList = new (std::nothrow) ArrayList(SingleObjectDeleter);
187                         pDirectoryList->Add(new (std::nothrow) String(__moveToDir));
188                         __pPresentationModel->SetCurrentAlbumInfo(albumName, *pDirectoryList);
189                         __pPresentationModel->SetCurrentAlbumContentType(CONTENT_TYPE_ALL);
190                         delete pDirectoryList;
191
192                         SceneManager* pSceneManager = SceneManager::GetInstance();
193                         pSceneManager->GoForward(ForwardSceneTransition(IDSCN_ALL_LIST));
194                 }
195         }
196         else if (res == CANCEL_SYS_ERROR && GetMovedCount() == 0)
197         {
198                 if (__isCopyOperation == true)
199                 {
200                         MessageBox messageBox;
201                         messageBox.Construct(L"", L"Unable to copy files.",
202                                         MSGBOX_STYLE_NONE, 3000);
203                         int modalResult;
204                         messageBox.ShowAndWait(modalResult);
205                 }
206         }
207         AppLogDebug("EXIt");
208 }
209
210 int
211 FileMoveTimer::GetDirScanCount()
212 {
213         AppLogDebug("ENTER");
214         IMapEnumerator* pMapEnum = __scanDirMap.GetMapEnumeratorN();
215         String* pKey = null;
216         while (pMapEnum->MoveNext() == E_SUCCESS)
217         {
218                 pKey = static_cast< String* > (pMapEnum->GetKey());
219                 if (File::IsFileExist(*pKey) == false)
220                 {
221                         __scanDirMap.Remove(*pKey);
222                 }
223         }
224         delete pMapEnum;
225         return __scanDirMap.GetCount();
226 }
227
228 void
229 FileMoveTimer::TimerComplete(int, enum FileActionCompleteRes res)
230 {
231         AppLogDebug("ENTER");
232         DeviceManager::RemoveDeviceEventListener(DEVICE_TYPE_STORAGE_CARD, *this);
233         String albumName = __pPresentationModel->ConvertToAlbumName(__moveToDir);
234
235         if (albumName != EMPTY_SPACE)
236         {
237                 IList* pDirectoryList = new (std::nothrow) ArrayList(SingleObjectDeleter);
238                 pDirectoryList->Add(new (std::nothrow) String(__moveToDir));
239                 __pPresentationModel->SetCurrentAlbumInfo(albumName, *pDirectoryList);
240                 __pPresentationModel->SetCurrentAlbumContentType(CONTENT_TYPE_ALL);
241                 delete pDirectoryList;
242
243                 SceneManager* pSceneManager = SceneManager::GetInstance();
244                 pSceneManager->GoForward(ForwardSceneTransition(IDSCN_ALL_LIST));
245         }
246         AppLogDebug("EXIt");
247 }
248
249 void
250 FileMoveTimer::SetCopy(void)
251 {
252         __isCopyOperation = true;
253 }
254
255 void
256 FileMoveTimer::ClearCopy(void)
257 {
258         __isCopyOperation = false;
259 }
260
261
262 void
263 FileMoveTimer::OnDeviceStateChanged(DeviceType deviceType, const Tizen::Base::String& state)
264 {
265         AppLogDebug("ENTER");
266         if (deviceType == DEVICE_TYPE_STORAGE_CARD && state == DEVICE_STORAGE_CARD_UNMOUNTED)
267         {
268                 String sdcPath = Environment::GetExternalStoragePath();
269                 IMapEnumerator* pMapEnum = __scanDirMap.GetMapEnumeratorN();
270                 String* pKey = null;
271                 while (pMapEnum->MoveNext() == E_SUCCESS)
272                 {
273                         pKey = static_cast< String* > (pMapEnum->GetKey());
274                         if (pKey->StartsWith(sdcPath, 0))
275                         {
276                                 __scanDirMap.Remove(*pKey);
277                         }
278                 }
279                 delete pMapEnum;
280         }
281         AppLogDebug("EXIT");
282 }