Arrange code
[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 "GlContentUpdateEventListener.h"
26 #include "GlProgressBar.h"
27 #include "GlTimerBase.h"
28 #include "GlTypes.h"
29
30 using namespace Tizen::Ui;
31 using namespace Tizen::Base::Runtime;
32
33 GlTimerBase::GlTimerBase(IFileOpInvalidateListener* invalidateListener, enum FileActionMode actionId)
34         : _actionId(actionId)
35         , __pContentIdList(null)
36         , __moveToCount(0)
37         , __pMoveProBar(null)
38         , __pInvalidate(invalidateListener)
39         , __isStarted(false)
40 {
41         AppLogDebug("ENTER");
42         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
43 }
44
45 GlTimerBase::~GlTimerBase(void)
46 {
47         AppLogDebug("ENTER");
48         __pInvalidate = null;
49         if (__pContentIdList != null && __pContentIdList->GetCount() > 0 && __isStarted)
50         {
51                 __moveTimer.Cancel();
52                 __isStarted = false;
53         }
54         delete __pContentIdList;
55         if (__pMoveProBar)
56         {
57                 __pMoveProBar->HideFileProgressingPopup();
58                 delete __pMoveProBar;
59         }
60         AppLogDebug("ENTER");
61 }
62
63 bool
64 GlTimerBase::StartTimer(void)
65 {
66         AppLogDebug("ENTER");
67         __pContentIdList = TimerStart();
68         if (__pContentIdList != null && __pContentIdList->GetCount() > 0)
69         {
70                 AppLogDebug("Starting timer");
71                 __pMoveProBar = new (std::nothrow) GlProgressBar(static_cast<IActionEventListener*>(this),
72                                 static_cast<IPropagatedKeyEventListener*>(this));
73                 __pMoveProBar->ShowFileProgressingPopup(__pContentIdList->GetCount(), _actionId);
74                 __moveToCount = 0;
75                 __moveTimer.Construct(*this);
76                 __moveTimer.Start(1);
77                 __isStarted = true;
78                 ContentUpdateEventListener* pContentListener = ContentUpdateEventListener::GetInstance();
79                 pContentListener->RemoveContentListener();
80                 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
81                 return E_SUCCESS;
82         }
83         AppLogDebug("EXIT1(%s)", GetErrorMessage(GetLastResult()));
84         return E_FAILURE;
85 }
86
87 void
88 GlTimerBase::CancelTimer(void)
89 {
90         AppLogDebug("ENTER");
91         __moveTimer.Cancel();
92         __isStarted = false;
93         OnOpCancelled(CANCEL_USER);
94         __moveToCount = 0;
95         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
96 }
97
98 void
99 GlTimerBase::OnTimerExpired(Timer& timer)
100 {
101         AppLogDebug("ENTER");
102         if (&timer == &__moveTimer)
103         {
104                 AppLogDebug("__pContentIdList count is %d", __pContentIdList->GetCount());
105                 if (__moveToCount < __pContentIdList->GetCount())
106                 {
107                         AppLogDebug("__pContentIdList count is %d", __pContentIdList->GetCount());
108                         if (__pContentIdList->GetAt(__moveToCount) != null)
109                         {
110                                 result r = TimerExpired(__pContentIdList->GetAt(__moveToCount));
111                                 if (IsFailed(r))
112                                 {
113                                         AppLogDebug("OnTimerExpired EXIT(%s)", GetErrorMessage(r));
114                                         OnOpCancelled(CANCEL_SYS_ERROR);
115                                         return;
116                                 }
117                         }
118                         ++__moveToCount;
119                         __pMoveProBar->IncProgress(__moveToCount);
120                         if (__pInvalidate)
121                         {
122                                 __pInvalidate->OnFileOpInvalidate(_actionId);
123                         }
124                         __moveTimer.Start(1);
125                         __isStarted = true;
126                 }
127                 else
128                 {
129                         __moveTimer.Cancel();
130                         __isStarted = false;
131                         OnOpComplete(COMPLETE_SUCCESS);
132                         __moveToCount = 0;
133                 }
134         }
135         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
136 }
137
138 void
139 GlTimerBase::OnActionPerformed(const Control& source, int actionId)
140 {
141         AppLogDebug("ENTER");
142         switch (actionId)
143         {
144         case IDA_PROGRESSBAR_CANCEL:
145         {
146                 CancelTimer();
147         }
148         break;
149
150         default:
151                 break;
152         }
153         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
154 }
155
156 int
157 GlTimerBase::GetMovedCount() const
158 {
159         return __moveToCount;
160 }
161
162 void
163 GlTimerBase::OnOpCancelled(enum FileActionCancelRes res)
164 {
165         AppLogDebug("ENTER");
166         __pMoveProBar->HideFileProgressingPopup();
167         ContentUpdateEventListener* pContentListener = ContentUpdateEventListener::GetInstance();
168         pContentListener->AddContentListener();
169         if (__pInvalidate)
170         {
171                 __pInvalidate->OnFileOpInvalidate(_actionId);
172         }
173         if (__moveToCount > 0)
174         {
175                 if (__pInvalidate)
176                 {
177                         if (res != CANCEL_USER)
178                         {
179                                 __pInvalidate->OnFileOpComplete(_actionId, COMPLETE_SYS_PARTIAL);
180                         }
181                         else
182                         {
183                                 __pInvalidate->OnFileOpComplete(_actionId, COMPLETE_SUCCESS);
184                         }
185                 }
186         }
187         else
188         {
189                 if (__pInvalidate)
190                 {
191                         if (res != CANCEL_USER)
192                         {
193                                 __pInvalidate->OnFileOpComplete(_actionId, COMPLETE_SYS_FAILURE);
194                         }
195                         else
196                         {
197                                 __pInvalidate->OnFileOpComplete(_actionId, COMPLETE_SUCCESS);
198                         }
199                 }
200         }
201         TimerCancel(__moveToCount, res);
202         AppLogDebug("EXIT");
203 }
204
205 void
206 GlTimerBase::OnOpComplete(enum FileActionCompleteRes res)
207 {
208         AppLogDebug("ENTER");
209         ContentUpdateEventListener* pContentListener = ContentUpdateEventListener::GetInstance();
210         pContentListener->AddContentListener();
211         __pMoveProBar->HideFileProgressingPopup();
212         if (__pInvalidate)
213         {
214                 __pInvalidate->OnFileOpInvalidate(_actionId);
215                 __pInvalidate->OnFileOpComplete(_actionId, res);
216         }
217         TimerComplete(__moveToCount, res);
218         AppLogDebug("EXIT");
219 }
220
221 void
222 GlTimerBase::SetActionMode(const enum FileActionMode actionId)
223 {
224         _actionId = actionId;
225 }
226
227 bool
228 GlTimerBase::IsStarted(void)
229 {
230         return __isStarted;
231 }
232
233 void
234 GlTimerBase::Cancel(void)
235 {
236         if (__isStarted)
237         {
238                 ContentUpdateEventListener* pContentListener = ContentUpdateEventListener::GetInstance();
239                 pContentListener->AddContentListener();
240                 __pMoveProBar->HideFileProgressingPopup();
241                 __moveTimer.Cancel();
242                 __isStarted = false;
243         }
244 }
245
246 bool
247 GlTimerBase::OnKeyReleased(Control& source, const Tizen::Ui::KeyEventInfo& keyEventInfo)
248 {
249         AppLogDebug("ENTER");
250
251         if(keyEventInfo.GetKeyCode() == KEY_BACK)
252         {
253                 CancelTimer();
254         }
255         return true;
256 }