Fixed prevent issue
[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("EXIT");
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)
105         {
106                 AlbumListPresentationModel::GetInstance()->AddTimerScanListener(this, scanCount);
107                 ScanDirectories();
108                 __pInvalidate->OnScanDirStart();
109                 FileListPresentationModel::GetInstance()->ShowAnimation();
110                 AlbumListPresentationModel::GetInstance()->ShowAnimation();
111         }
112         else
113         {
114                 OnOpCancelled(CANCEL_USER);
115         }
116         __isStarted = false;
117         __moveToCount = 0;
118         __timerRes = CANCEL_BY_USER;
119         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
120 }
121
122 void
123 GlTimerBase::OnTimerScanDirComplete()
124 {
125         AppLogDebug("OnTimerScanDirComplete ENTER");
126
127         AlbumListPresentationModel::GetInstance()->RemoveTimerScanListener();
128         FileListPresentationModel::GetInstance()->StopAnimation();
129         AlbumListPresentationModel::GetInstance()->StopAnimation();
130         __pInvalidate->OnScanDirComplete();
131         switch(__timerRes)
132         {
133         case CANCEL_SYS:
134                 AppLogDebug("CANCEL_SYS");
135                 OnOpCancelled(CANCEL_SYS_ERROR);
136                 break;
137         case CANCEL_BY_USER:
138                 AppLogDebug("CANCEL_BY_USER");
139                 OnOpCancelled(CANCEL_USER);
140                 break;
141         case TIMER_COMPLETE:
142                 AppLogDebug("TIMER_COMPLETE");
143                 OnOpComplete(COMPLETE_SUCCESS);
144                 break;
145         default:
146                 break;
147         }
148         AppLogDebug("ENTER");
149 }
150
151 void
152 GlTimerBase::OnTimerExpired(Timer& timer)
153 {
154         AppLogDebug("ENTER");
155         if (&timer == &__moveTimer)
156         {
157                 AppLogDebug("__pContentIdList count is %d", __pContentIdList->GetCount());
158                 if (__moveToCount < __pContentIdList->GetCount())
159                 {
160                         AppLogDebug("__pContentIdList count is %d", __pContentIdList->GetCount());
161                         if (__pContentIdList->GetAt(__moveToCount) != null)
162                         {
163                                 result r = TimerExpired(__pContentIdList->GetAt(__moveToCount));
164                                 if (IsFailed(r))
165                                 {
166                                         if (r == E_STORAGE_FULL || r == E_IO)
167                                         {
168                                                 __moveTimer.Cancel();
169                                                 __isStarted = false;
170                                                 __timerRes = CANCEL_SYS;
171                                                 __pMoveProBar->HideFileProgressingPopup();
172                                                 int scanCount = GetDirScanCount();
173                                                 if ( scanCount > 0)
174                                                 {
175                                                         AlbumListPresentationModel::GetInstance()->AddTimerScanListener(this, scanCount);
176                                                         ScanDirectories();
177                                                         __pInvalidate->OnScanDirStart();
178                                                         FileListPresentationModel::GetInstance()->ShowAnimation();
179                                                         AlbumListPresentationModel::GetInstance()->ShowAnimation();
180                                                 }
181                                                 else
182                                                 {
183                                                         MessageBox messageBox;
184                                                         messageBox.Construct(L"", ResourceManager::GetString(L"IDS_COM_BODY_OPERATION_FAILED"),
185                                                                         MSGBOX_STYLE_NONE, 3000);
186                                                         int modalResult;
187                                                         messageBox.ShowAndWait(modalResult);
188                                                         OnOpCancelled(CANCEL_SYS_ERROR);
189                                                 }
190                                                 AppLogDebug("OnTimerExpired EXIT(%s)", GetErrorMessage(r));
191                                                 return;
192                                         }
193                                         //If the errors are file not found etc, skip this file and move to next.
194                                 }
195                         }
196                         ++__moveToCount;
197                         __pMoveProBar->IncProgress(__moveToCount);
198                         if (__pInvalidate)
199                         {
200                                 __pInvalidate->OnFileOpInvalidate(_actionId);
201                         }
202                         __moveTimer.Start(1);
203                         __isStarted = true;
204                 }
205                 else
206                 {
207                         __moveTimer.Cancel();
208                         __pMoveProBar->HideFileProgressingPopup();
209                         int scanCount = GetDirScanCount();
210                         if (scanCount == 0)
211                         {
212                                 __moveToCount = 0;
213                                 __isStarted = false;
214                                 __timerRes = TIMER_COMPLETE;
215                                 OnOpComplete(COMPLETE_SUCCESS);
216                                 return;
217                         }
218                         AlbumListPresentationModel::GetInstance()->AddTimerScanListener(this, scanCount);
219                         ScanDirectories();
220                         __pInvalidate->OnScanDirStart();
221                         FileListPresentationModel::GetInstance()->ShowAnimation();
222                         AlbumListPresentationModel::GetInstance()->ShowAnimation();
223                         __isStarted = false;
224                         __timerRes = TIMER_COMPLETE;
225                         __moveToCount = 0;
226                 }
227         }
228         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
229 }
230
231 void
232 GlTimerBase::OnActionPerformed(const Control& source, int actionId)
233 {
234         AppLogDebug("ENTER");
235         switch (actionId)
236         {
237         case IDA_PROGRESSBAR_CANCEL:
238         {
239                 CancelTimer();
240         }
241         break;
242
243         default:
244                 break;
245         }
246         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
247 }
248
249 int
250 GlTimerBase::GetMovedCount() const
251 {
252         return __moveToCount;
253 }
254
255 void
256 GlTimerBase::OnOpCancelled(enum FileActionCancelRes res)
257 {
258         AppLogDebug("ENTER");
259         ContentUpdateEventListener* pContentListener = ContentUpdateEventListener::GetInstance();
260         pContentListener->ReSetTimerActive();
261         if (__pInvalidate)
262         {
263                 __pInvalidate->OnFileOpInvalidate(_actionId);
264         }
265         if (__moveToCount > 0)
266         {
267                 if (__pInvalidate)
268                 {
269                         if (res != CANCEL_USER)
270                         {
271                                 __pInvalidate->OnFileOpComplete(_actionId, COMPLETE_SYS_PARTIAL);
272                         }
273                         else
274                         {
275                                 __pInvalidate->OnFileOpComplete(_actionId, COMPLETE_SUCCESS);
276                         }
277                 }
278         }
279         else
280         {
281                 if (__pInvalidate)
282                 {
283                         if (res != CANCEL_USER)
284                         {
285                                 __pInvalidate->OnFileOpComplete(_actionId, COMPLETE_SYS_FAILURE);
286                         }
287                         else
288                         {
289                                 __pInvalidate->OnFileOpComplete(_actionId, COMPLETE_SUCCESS);
290                         }
291                 }
292         }
293         TimerCancel(__moveToCount, res);
294         AppLogDebug("EXIT");
295 }
296
297 void
298 GlTimerBase::OnOpComplete(enum FileActionCompleteRes res)
299 {
300         AppLogDebug("ENTER");
301         ContentUpdateEventListener* pContentListener = ContentUpdateEventListener::GetInstance();
302         pContentListener->ReSetTimerActive();
303         if (__pInvalidate)
304         {
305                 __pInvalidate->OnFileOpInvalidate(_actionId);
306                 __pInvalidate->OnFileOpComplete(_actionId, res);
307         }
308         TimerComplete(__moveToCount, res);
309         AppLogDebug("EXIT");
310 }
311
312 void
313 GlTimerBase::SetActionMode(const enum FileActionMode actionId)
314 {
315         AppLogDebug("ENTER");
316         _actionId = actionId;
317         AppLogDebug("EXIT");
318 }
319
320 bool
321 GlTimerBase::IsStarted(void)
322 {
323         AppLogDebug("ENTER");
324         return __isStarted;
325 }
326
327 void
328 GlTimerBase::Cancel(void)
329 {
330         AppLogDebug("ENTER");
331         if (__isStarted)
332         {
333                 __pMoveProBar->HideFileProgressingPopup();
334                 __moveTimer.Cancel();
335                 __isStarted = false;
336
337                 int scanCount = GetDirScanCount();
338                 if (scanCount > 0)
339                 {
340                         AlbumListPresentationModel::GetInstance()->AddTimerScanListener(this, scanCount);
341                         ScanDirectories();
342                         __pInvalidate->OnScanDirStart();
343                         FileListPresentationModel::GetInstance()->ShowAnimation();
344                         AlbumListPresentationModel::GetInstance()->ShowAnimation();
345                 }
346                 else
347                 {
348                         OnOpCancelled(CANCEL_SYS_ERROR);
349                 }
350         }
351         AppLogDebug("EXIT");
352 }
353
354 bool
355 GlTimerBase::OnKeyReleased(Control& source, const Tizen::Ui::KeyEventInfo& keyEventInfo)
356 {
357         AppLogDebug("ENTER");
358
359         if(keyEventInfo.GetKeyCode() == KEY_BACK)
360         {
361                 CancelTimer();
362         }
363         return true;
364 }