Merge " Fixed issue 53911" into tizen_2.2
[apps/osp/Gallery.git] / src / GlTimerBase.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                GlTimerBase.cpp
19  * @brief               This is the source file for GlTimerBase class.
20  */
21
22 #include <FBase.h>
23 #include <FUi.h>
24
25 #include "GlAlbumListPresentationModel.h"
26 #include "GlContentUpdateEventListener.h"
27 #include "GlFileListPresentationModel.h"
28 #include "GlProgressBar.h"
29 #include "GlTimerBase.h"
30 #include "GlTypes.h"
31
32 using namespace Tizen::Ui;
33 using namespace Tizen::Base::Runtime;
34 using namespace Tizen::Ui::Controls;
35
36 GlTimerBase::GlTimerBase(IFileOpInvalidateListener* invalidateListener, enum FileActionMode actionId)
37         : _actionId(actionId)
38         , __pContentIdList(null)
39         , __moveToCount(0)
40         , __pMoveProBar(null)
41         , __pInvalidate(invalidateListener)
42         , __isStarted(false)
43         , __timerRes(CANCEL_BY_USER)
44 {
45         AppLogDebug("ENTER");
46         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
47 }
48
49 GlTimerBase::~GlTimerBase(void)
50 {
51         AppLogDebug("ENTER");
52         __pInvalidate = null;
53         AlbumListPresentationModel::GetInstance()->RemoveTimerScanListener();
54         FileListPresentationModel::GetInstance()->StopAnimation();
55         AlbumListPresentationModel::GetInstance()->StopAnimation();
56         ContentUpdateEventListener* pContentListener = ContentUpdateEventListener::GetInstance();
57         pContentListener->ReSetTimerActive();
58
59         if (__pContentIdList != null && __pContentIdList->GetCount() > 0 && __isStarted)
60         {
61                 __moveTimer.Cancel();
62                 __isStarted = false;
63         }
64         delete __pContentIdList;
65         if (__pMoveProBar)
66         {
67                 __pMoveProBar->HideFileProgressingPopup();
68                 delete __pMoveProBar;
69         }
70         AppLogDebug("ENTER");
71 }
72
73 bool
74 GlTimerBase::StartTimer(void)
75 {
76         AppLogDebug("ENTER");
77         __pContentIdList = TimerStart();
78         if (__pContentIdList != null && __pContentIdList->GetCount() > 0)
79         {
80                 AppLogDebug("Starting timer");
81                 __pMoveProBar = new (std::nothrow) GlProgressBar(static_cast<IActionEventListener*>(this),
82                                 static_cast<IPropagatedKeyEventListener*>(this));
83                 __pMoveProBar->ShowFileProgressingPopup(__pContentIdList->GetCount(), _actionId);
84                 __moveToCount = 0;
85                 __moveTimer.Construct(*this);
86                 __moveTimer.Start(60);
87                 __isStarted = true;
88                 ContentUpdateEventListener* pContentListener = ContentUpdateEventListener::GetInstance();
89                 pContentListener->SetTimerActive();
90                 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
91                 return E_SUCCESS;
92         }
93         AppLogDebug("EXIT1(%s)", GetErrorMessage(GetLastResult()));
94         return E_FAILURE;
95 }
96
97 void
98 GlTimerBase::CancelTimer(void)
99 {
100         AppLogDebug("ENTER");
101         __moveTimer.Cancel();
102         __pMoveProBar->HideFileProgressingPopup();
103         int scanCount = GetDirScanCount();
104         if ( (scanCount > 0 && _actionId != FILE_ACTION_MOVE ) ||
105                         (scanCount > 1 && _actionId == FILE_ACTION_MOVE ))
106         {
107                 AlbumListPresentationModel::GetInstance()->AddTimerScanListener(this, scanCount);
108                 ScanDirectories();
109                 __pInvalidate->OnScanDirStart();
110                 FileListPresentationModel::GetInstance()->ShowAnimation();
111                 AlbumListPresentationModel::GetInstance()->ShowAnimation();
112         }
113         else
114         {
115                 OnOpCancelled(CANCEL_USER);
116         }
117         __isStarted = false;
118         __moveToCount = 0;
119         __timerRes = CANCEL_BY_USER;
120         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
121 }
122
123 void
124 GlTimerBase::OnTimerScanDirComplete()
125 {
126         AppLogDebug("OnTimerScanDirComplete ENTER");
127
128         AlbumListPresentationModel::GetInstance()->RemoveTimerScanListener();
129         FileListPresentationModel::GetInstance()->StopAnimation();
130         AlbumListPresentationModel::GetInstance()->StopAnimation();
131         __pInvalidate->OnScanDirComplete();
132         switch(__timerRes)
133         {
134         case CANCEL_SYS:
135                 AppLogDebug("CANCEL_SYS");
136                 OnOpCancelled(CANCEL_SYS_ERROR);
137                 break;
138         case CANCEL_BY_USER:
139                 AppLogDebug("CANCEL_BY_USER");
140                 OnOpCancelled(CANCEL_USER);
141                 break;
142         case TIMER_COMPLETE:
143                 AppLogDebug("TIMER_COMPLETE");
144                 OnOpComplete(COMPLETE_SUCCESS);
145                 break;
146         default:
147                 break;
148         }
149         AppLogDebug("ENTER");
150 }
151
152 void
153 GlTimerBase::OnTimerExpired(Timer& timer)
154 {
155         AppLogDebug("ENTER");
156         if (&timer == &__moveTimer)
157         {
158                 AppLogDebug("__pContentIdList count is %d", __pContentIdList->GetCount());
159                 if (__moveToCount < __pContentIdList->GetCount())
160                 {
161                         AppLogDebug("__pContentIdList count is %d", __pContentIdList->GetCount());
162                         if (__pContentIdList->GetAt(__moveToCount) != null)
163                         {
164                                 result r = TimerExpired(__pContentIdList->GetAt(__moveToCount));
165                                 if (IsFailed(r))
166                                 {
167                                         if (r == E_STORAGE_FULL || r == E_IO)
168                                         {
169                                                 __moveTimer.Cancel();
170                                                 __isStarted = false;
171                                                 __timerRes = CANCEL_SYS;
172                                                 __pMoveProBar->HideFileProgressingPopup();
173                                                 int scanCount = GetDirScanCount();
174                                                 if ( (scanCount > 0 && _actionId != FILE_ACTION_MOVE ) ||
175                                                                 (scanCount > 1 && _actionId == FILE_ACTION_MOVE ))
176                                                 {
177                                                         AlbumListPresentationModel::GetInstance()->AddTimerScanListener(this, scanCount);
178                                                         ScanDirectories();
179                                                         __pInvalidate->OnScanDirStart();
180                                                         FileListPresentationModel::GetInstance()->ShowAnimation();
181                                                         AlbumListPresentationModel::GetInstance()->ShowAnimation();
182                                                 }
183                                                 else
184                                                 {
185                                                         MessageBox messageBox;
186                                                         messageBox.Construct(L"", ResourceManager::GetString(L"IDS_COM_BODY_OPERATION_FAILED"),
187                                                                         MSGBOX_STYLE_NONE, 3000);
188                                                         int modalResult;
189                                                         messageBox.ShowAndWait(modalResult);
190                                                         OnOpCancelled(CANCEL_SYS_ERROR);
191                                                 }
192                                                 AppLogDebug("OnTimerExpired EXIT(%s)", GetErrorMessage(r));
193                                                 return;
194                                         }
195                                         //If the errors are file not found etc, skip this file and move to next.
196                                 }
197                         }
198                         ++__moveToCount;
199                         __pMoveProBar->IncProgress(__moveToCount);
200                         if (__pInvalidate)
201                         {
202                                 __pInvalidate->OnFileOpInvalidate(_actionId);
203                         }
204                         __moveTimer.Start(1);
205                         __isStarted = true;
206                 }
207                 else
208                 {
209                         __moveTimer.Cancel();
210                         __pMoveProBar->HideFileProgressingPopup();
211                         int scanCount = GetDirScanCount();
212                         if (scanCount == 0 && _actionId == FILE_ACTION_ROTATE)
213                         {
214                                 __moveToCount = 0;
215                                 __isStarted = false;
216                                 __timerRes = TIMER_COMPLETE;
217                                 OnOpComplete(COMPLETE_SUCCESS);
218                                 return;
219                         }
220                         if ((scanCount > 0 && _actionId != FILE_ACTION_MOVE ) ||
221                                         (scanCount > 1 && _actionId == FILE_ACTION_MOVE ))
222                         {
223                                 AlbumListPresentationModel::GetInstance()->AddTimerScanListener(this, scanCount);
224                                 ScanDirectories();
225                                 __pInvalidate->OnScanDirStart();
226                                 FileListPresentationModel::GetInstance()->ShowAnimation();
227                                 AlbumListPresentationModel::GetInstance()->ShowAnimation();
228                         }
229                         __isStarted = false;
230                         __timerRes = TIMER_COMPLETE;
231                         __moveToCount = 0;
232                 }
233         }
234         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
235 }
236
237 void
238 GlTimerBase::OnActionPerformed(const Control& source, int actionId)
239 {
240         AppLogDebug("ENTER");
241         switch (actionId)
242         {
243         case IDA_PROGRESSBAR_CANCEL:
244         {
245                 CancelTimer();
246         }
247         break;
248
249         default:
250                 break;
251         }
252         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
253 }
254
255 int
256 GlTimerBase::GetMovedCount() const
257 {
258         return __moveToCount;
259 }
260
261 void
262 GlTimerBase::OnOpCancelled(enum FileActionCancelRes res)
263 {
264         AppLogDebug("ENTER");
265         ContentUpdateEventListener* pContentListener = ContentUpdateEventListener::GetInstance();
266         pContentListener->ReSetTimerActive();
267         if (__pInvalidate)
268         {
269                 __pInvalidate->OnFileOpInvalidate(_actionId);
270         }
271         if (__moveToCount > 0)
272         {
273                 if (__pInvalidate)
274                 {
275                         if (res != CANCEL_USER)
276                         {
277                                 __pInvalidate->OnFileOpComplete(_actionId, COMPLETE_SYS_PARTIAL);
278                         }
279                         else
280                         {
281                                 __pInvalidate->OnFileOpComplete(_actionId, COMPLETE_SUCCESS);
282                         }
283                 }
284         }
285         else
286         {
287                 if (__pInvalidate)
288                 {
289                         if (res != CANCEL_USER)
290                         {
291                                 __pInvalidate->OnFileOpComplete(_actionId, COMPLETE_SYS_FAILURE);
292                         }
293                         else
294                         {
295                                 __pInvalidate->OnFileOpComplete(_actionId, COMPLETE_SUCCESS);
296                         }
297                 }
298         }
299         TimerCancel(__moveToCount, res);
300         AppLogDebug("EXIT");
301 }
302
303 void
304 GlTimerBase::OnOpComplete(enum FileActionCompleteRes res)
305 {
306         AppLogDebug("ENTER");
307         ContentUpdateEventListener* pContentListener = ContentUpdateEventListener::GetInstance();
308         pContentListener->ReSetTimerActive();
309         if (__pInvalidate)
310         {
311                 __pInvalidate->OnFileOpInvalidate(_actionId);
312                 __pInvalidate->OnFileOpComplete(_actionId, res);
313         }
314         TimerComplete(__moveToCount, res);
315         AppLogDebug("EXIT");
316 }
317
318 void
319 GlTimerBase::SetActionMode(const enum FileActionMode actionId)
320 {
321         AppLogDebug("ENTER");
322         _actionId = actionId;
323         AppLogDebug("EXIT");
324 }
325
326 bool
327 GlTimerBase::IsStarted(void)
328 {
329         AppLogDebug("ENTER");
330         return __isStarted;
331 }
332
333 void
334 GlTimerBase::Cancel(void)
335 {
336         AppLogDebug("ENTER");
337         if (__isStarted)
338         {
339                 __pMoveProBar->HideFileProgressingPopup();
340                 __moveTimer.Cancel();
341                 __isStarted = false;
342
343                 int scanCount = GetDirScanCount();
344                 if ((scanCount > 0 && _actionId != FILE_ACTION_MOVE ) ||
345                                 (scanCount > 1 && _actionId == FILE_ACTION_MOVE ))
346                 {
347                         AlbumListPresentationModel::GetInstance()->AddTimerScanListener(this, scanCount);
348                         ScanDirectories();
349                         __pInvalidate->OnScanDirStart();
350                         FileListPresentationModel::GetInstance()->ShowAnimation();
351                         AlbumListPresentationModel::GetInstance()->ShowAnimation();
352                 }
353                 else
354                 {
355                         OnOpCancelled(CANCEL_SYS_ERROR);
356                 }
357         }
358         AppLogDebug("EXIT");
359 }
360
361 bool
362 GlTimerBase::OnKeyReleased(Control& source, const Tizen::Ui::KeyEventInfo& keyEventInfo)
363 {
364         AppLogDebug("ENTER");
365
366         if(keyEventInfo.GetKeyCode() == KEY_BACK)
367         {
368                 CancelTimer();
369         }
370         return true;
371 }