Fixed issue 52517
[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::Ui;
37 using namespace Tizen::Ui::Controls;
38 using namespace Tizen::Ui::Scenes;
39
40 FileMoveTimer::FileMoveTimer(String& destDirectory, IList* list, FileListPresentationModel* presentationModel,
41                 IFileOpInvalidateListener* pInvalidate, bool isCopyOperation)
42         : GlTimerBase(pInvalidate, FILE_ACTION_MOVE)
43         , __pMoveIndexList(list)
44         , __moveToDir(destDirectory)
45         , __isCopyOperation(false)
46         , __pPresentationModel(presentationModel)
47         , __scanDirMap(SingleObjectDeleter)
48         , __pContentManager(null)
49 {
50         __scanDirMap.Construct();
51 }
52
53 FileMoveTimer::~FileMoveTimer(void)
54 {
55         AppLogDebug("ENTER");
56         ScanDirectories();
57         delete __pMoveIndexList;
58         delete __pContentManager;
59         AppLogDebug("EXIT");
60 }
61
62 IList *
63 FileMoveTimer::TimerStart(void)
64 {
65         AppLogDebug("ENTER");
66         if (&__moveToDir == null || __moveToDir.IsEmpty())
67         {
68                 AppLogDebug("EXIT 1(%s)", GetErrorMessage(GetLastResult()));
69                 return null;
70         }
71
72         if (__pMoveIndexList == null || __pMoveIndexList->GetCount() <= 0)
73         {
74                 AppLogDebug("EXIT 2(%s)", GetErrorMessage(GetLastResult()));
75                 return null;
76         }
77
78         if (__pMoveIndexList->GetCount() > 0)
79         {
80                 __pContentManager = new (std::nothrow) ContentManager();
81                 __pContentManager->Construct();
82                 if (__isCopyOperation == true)
83                 {
84                         SetActionMode(FILE_ACTION_COPY);
85                 }
86                 if (File::IsFileExist(__moveToDir) == false)
87                 {
88                         Directory::Create(__moveToDir, true);
89                 }
90                 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
91                 IList* pCntIdList = __pPresentationModel->GetContentIdListAtIndexN(*__pMoveIndexList);
92                 return pCntIdList;
93         }
94         else
95         {
96                 return null;
97         }
98 }
99
100 result
101 FileMoveTimer::TimerExpired(const Object* contentId)
102 {
103         AppLogDebug("ENTER");
104         const ContentId* cntId = static_cast<const ContentId*>(contentId);
105         if(cntId == null)
106         {
107                 return E_FAILURE;
108         }
109         ContentInfo* cntInfo = __pContentManager->GetContentInfoN(*cntId);
110         if (cntInfo)
111         {
112                 String filePath = cntInfo->GetContentPath();
113                 delete cntInfo;
114                 result r = __pPresentationModel->MoveToContentFile(filePath, __moveToDir, __isCopyOperation);
115                 if (!IsFailed(r))
116                 {
117                         if (__isCopyOperation == false)
118                         {
119                                 //For Move operation, add the directory being moved from for scanning
120                                 String dirPath;
121                                 int index =0;
122                                 result r = filePath.LastIndexOf(DIRECTORY_SEPARATOR, filePath.GetLength() - 1, index);
123                                 TryReturn(r == E_SUCCESS, false, "[%s] Unable to get Dirpath", GetErrorMessage(r));
124                                 filePath.SubString(0, index, dirPath);
125                                 if(!__scanDirMap.ContainsKey(dirPath))
126                                 {
127                                         __scanDirMap.Add(new String(dirPath), new Integer(0));
128                                 }
129                         }
130                         if (!__scanDirMap.ContainsKey(__moveToDir))
131                         {
132                                 __scanDirMap.Add(new String(__moveToDir), new Integer(0));
133                         }
134                 }
135                 return r;
136         }
137         return E_FAILURE;
138 }
139
140 result
141 FileMoveTimer::ScanDirectories(void)
142 {
143         AppLogDebug("ENTER");
144         RequestId reqId;
145         result r = E_FAILURE;
146         if (__scanDirMap.GetCount() > 0)
147         {
148                 AppLogDebug("Print AlbumScanMoveToDirPath 2(%d)", __scanDirMap.GetCount());
149                 IMapEnumerator* pMapEnum = __scanDirMap.GetMapEnumeratorN();
150                 String* pKey = null;
151                 while (pMapEnum->MoveNext() == E_SUCCESS)
152                 {
153                         pKey = static_cast< String* > (pMapEnum->GetKey());
154                         r = ContentManager::ScanDirectory(*(pKey), true, null, reqId);
155                 }
156                 __scanDirMap.RemoveAll();
157                 delete pMapEnum;
158         }
159         AppLogDebug("EXIT(%s)", GetErrorMessage(r));
160         return r;
161 }
162
163 void
164 FileMoveTimer::TimerCancel(int, enum FileActionCancelRes res)
165 {
166         AppLogDebug("ENTER");
167         if (res == CANCEL_USER || (res == CANCEL_SYS_ERROR && GetMovedCount() > 0) )
168         {
169                 String albumName = __pPresentationModel->ConvertToAlbumName(__moveToDir);
170
171                 if (albumName != EMPTY_SPACE)
172                 {
173                         IList* pDirectoryList = new (std::nothrow) ArrayList(SingleObjectDeleter);
174                         pDirectoryList->Add(new (std::nothrow) String(__moveToDir));
175                         __pPresentationModel->SetCurrentAlbumInfo(albumName, *pDirectoryList);
176                         __pPresentationModel->SetCurrentAlbumContentType(CONTENT_TYPE_ALL);
177                         delete pDirectoryList;
178
179                         SceneManager* pSceneManager = SceneManager::GetInstance();
180                         pSceneManager->GoForward(ForwardSceneTransition(IDSCN_ALL_LIST));
181                 }
182         }
183         else if (res == CANCEL_SYS_ERROR && GetMovedCount() == 0)
184         {
185                 if (__isCopyOperation == true)
186                 {
187                         MessageBox messageBox;
188                         messageBox.Construct(L"", L"Unable to copy files.",
189                                         MSGBOX_STYLE_NONE, 3000);
190                         int modalResult;
191                         messageBox.ShowAndWait(modalResult);
192                 }
193         }
194         AppLogDebug("EXIt");
195 }
196
197 int
198 FileMoveTimer::GetDirScanCount() const
199 {
200         AppLogDebug("ENTER");
201         return __scanDirMap.GetCount();
202 }
203
204 void
205 FileMoveTimer::TimerComplete(int, enum FileActionCompleteRes res)
206 {
207         AppLogDebug("ENTER");
208         String albumName = __pPresentationModel->ConvertToAlbumName(__moveToDir);
209
210         if (albumName != EMPTY_SPACE)
211         {
212                 IList* pDirectoryList = new (std::nothrow) ArrayList(SingleObjectDeleter);
213                 pDirectoryList->Add(new (std::nothrow) String(__moveToDir));
214                 __pPresentationModel->SetCurrentAlbumInfo(albumName, *pDirectoryList);
215                 __pPresentationModel->SetCurrentAlbumContentType(CONTENT_TYPE_ALL);
216                 delete pDirectoryList;
217
218                 SceneManager* pSceneManager = SceneManager::GetInstance();
219                 pSceneManager->GoForward(ForwardSceneTransition(IDSCN_ALL_LIST));
220         }
221         AppLogDebug("EXIt");
222 }
223
224 void
225 FileMoveTimer::SetCopy(void)
226 {
227         __isCopyOperation = true;
228 }
229
230 void
231 FileMoveTimer::ClearCopy(void)
232 {
233         __isCopyOperation = false;
234 }