Applied latest source code
[apps/native/preloaded/Clock.git] / src / ClkStopWatchPresentationModel.cpp
1 //
2 // Tizen Native SDK
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Flora License, Version 1.1 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://floralicense.org/license/
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an AS IS BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19  * @file        ClkStopWatchPresentationModel.cpp
20  * @brief       Describes how the StopWatch works.
21  */
22
23 #include <cstdlib>
24 #include <FApp.h>
25 #include <FBase.h>
26
27 #include "ClkAlarmPresentationModel.h"
28 #include "ClkStopWatchPresentationModel.h"
29
30 using namespace Tizen::App;
31 using namespace Tizen::Base;
32 using namespace Tizen::Base::Collection;
33
34 StopWatchPresentationModel* StopWatchPresentationModel::__pInstance = NULL;
35
36 StopWatchPresentationModel::StopWatchPresentationModel()
37 :__checkFlagKey("lapCheckFlag")
38 ,__LapCountKey("stopWatchLapCount")
39 ,__millisecondTextKey("stopWatchMilliSecond")
40 ,__pAppRegistry(null)
41 ,__restartTick("stopWatchRestartTick")
42 ,__startTick("stopWatchStartTick")
43 ,__statusKey("stopWatchStatus")
44 ,__stopTick("stopWatchStopTick")
45 ,__tempTick("stopWatchTempTick")
46 ,__tmpTicker("stopWatchTmpTicker")
47 ,__watchTextKey("stopWatchText")
48 {
49
50 }
51
52 StopWatchPresentationModel::~StopWatchPresentationModel()
53 {
54
55 }
56
57 void
58 StopWatchPresentationModel::SetStopWatchValue(int& status, String& startTick, String& tempTick, String& restartTick, String& stopTick, String& milliSecondText, String& watchText)
59 {
60         result r = E_FAILURE;
61         DateTime currentTime;
62         String currentStopWatchValue;
63         String currentTimeValue;
64         DataServiceProvider* pDataServiceProvider= DataServiceProvider::GetInstance();
65         pDataServiceProvider->GetCurrentDateTime(currentTime);
66
67         if (__pAppRegistry == null)
68         {
69                 __pAppRegistry = UiApp::GetInstance()->GetAppRegistry();
70         }
71
72         r = __pAppRegistry->Set(__statusKey, status);
73
74         if (r == E_KEY_NOT_FOUND)
75         {
76                 __pAppRegistry->Add(__statusKey, status);
77         }
78
79         r = __pAppRegistry->Set(__startTick, startTick);
80
81         if (r == E_KEY_NOT_FOUND)
82         {
83                 __pAppRegistry->Add(__startTick, startTick);
84         }
85
86         r = __pAppRegistry->Set(__tempTick, tempTick);
87
88         if (r == E_KEY_NOT_FOUND)
89         {
90                 __pAppRegistry->Add(__tempTick, tempTick);
91         }
92
93         r = __pAppRegistry->Set(__restartTick, restartTick);
94
95         if (r == E_KEY_NOT_FOUND)
96         {
97                 __pAppRegistry->Add(__restartTick, restartTick);
98         }
99
100         r = __pAppRegistry->Set(__stopTick, stopTick);
101
102         if (r == E_KEY_NOT_FOUND)
103         {
104                 __pAppRegistry->Add(__stopTick, stopTick);
105         }
106
107         r = __pAppRegistry->Set(__millisecondTextKey, milliSecondText);
108
109         if (r == E_KEY_NOT_FOUND)
110         {
111                 __pAppRegistry->Add(__millisecondTextKey, milliSecondText);
112         }
113
114         r = __pAppRegistry->Set(__watchTextKey, watchText);
115
116         if (r == E_KEY_NOT_FOUND)
117         {
118                 __pAppRegistry->Add(__watchTextKey, watchText);
119         }
120
121         __pAppRegistry->Save();
122 }
123
124 void
125 StopWatchPresentationModel::GetStopWatchValue(int& status, String& startTick, String& tempTick, String& restartTick, String& stopTick, String& milliSecondText, String& watchText)
126 {
127         AppLog("StopWatchPresentationModel::GetStopWatchValue");
128         result r = E_FAILURE;
129         String oldTime;
130
131         if (__pAppRegistry == null)
132         {
133                 __pAppRegistry = UiApp::GetInstance()->GetAppRegistry();
134         }
135
136         r = __pAppRegistry->Get(__statusKey, status);
137
138         if (IsFailed(r))
139         {
140                 AppLog("StopWatchPresentationModel::GetStopWatchValue result = %s", GetErrorMessage(r));
141         }
142
143         r = __pAppRegistry->Get(__startTick, startTick);
144
145         if (IsFailed(r))
146         {
147                 AppLog("StopWatchPresentationModel::GetStopWatchValue result = %s", GetErrorMessage(r));
148         }
149
150         r = __pAppRegistry->Get(__tempTick, tempTick);
151
152         if (IsFailed(r))
153         {
154                 AppLog("StopWatchPresentationModel::GetStopWatchValue result = %s", GetErrorMessage(r));
155         }
156
157         r = __pAppRegistry->Get(__restartTick, restartTick);
158
159         if (IsFailed(r))
160         {
161                 AppLog("StopWatchPresentationModel::GetStopWatchValue result = %s", GetErrorMessage(r));
162         }
163
164         r = __pAppRegistry->Get(__stopTick, stopTick);
165
166         if (IsFailed(r))
167         {
168                 AppLog("StopWatchPresentationModel::GetStopWatchValue result = %s", GetErrorMessage(r));
169         }
170
171         r = __pAppRegistry->Get(__millisecondTextKey, milliSecondText);
172
173         if (IsFailed(r))
174         {
175                 AppLog("StopWatchPresentationModel::GetStopWatchValue result = %s", GetErrorMessage(r));
176         }
177
178         r = __pAppRegistry->Get(__watchTextKey, watchText);
179
180         if (IsFailed(r))
181         {
182                 AppLog("StopWatchPresentationModel::GetStopWatchValue result = %s", GetErrorMessage(r));
183         }
184
185 }
186
187 void
188 StopWatchPresentationModel::SetStopWatchLaps(ArrayList& watchTextList, ArrayList& lapTimeTextList, String& tempTicker, int& checkFlag)
189 {
190         result r = E_FAILURE;
191         int watchTextCount = watchTextList.GetCount();
192         String* watchText = null;
193         String watchTextKey ;
194         String* lapTimeText = null;
195         String lapTimeTextKey;
196
197         for (int count = 0; count < watchTextCount; count++)
198         {
199                 watchTextKey.Clear();
200                 lapTimeTextKey.Clear();
201
202                 watchText = static_cast<String *>(watchTextList.GetAt(count));
203                 watchTextKey.Append("watchText");
204                 watchTextKey.Append(count);
205
206                 lapTimeText = static_cast<String *>(lapTimeTextList.GetAt(count));
207                 lapTimeTextKey.Append("lapTimeText");
208                 lapTimeTextKey.Append(count);
209
210                 r = __pAppRegistry->Set(watchTextKey, *watchText);
211
212                 if (r == E_KEY_NOT_FOUND)
213                 {
214                         __pAppRegistry->Add(watchTextKey, *watchText);
215                 }
216
217                 r = __pAppRegistry->Set(lapTimeTextKey, *lapTimeText);
218
219                 if (r == E_KEY_NOT_FOUND)
220                 {
221                         __pAppRegistry->Add(lapTimeTextKey, *lapTimeText);
222                 }
223         }
224
225         r = __pAppRegistry->Set(__LapCountKey, watchTextCount);
226
227         if (r == E_KEY_NOT_FOUND)
228         {
229                 __pAppRegistry->Add(__LapCountKey, watchTextCount);
230         }
231
232         r = __pAppRegistry->Set(__tmpTicker, tempTicker);
233
234         if (r == E_KEY_NOT_FOUND)
235         {
236                 __pAppRegistry->Add(__tmpTicker, tempTicker);
237         }
238
239         r = __pAppRegistry->Set(__checkFlagKey, checkFlag);
240
241         if (r == E_KEY_NOT_FOUND)
242         {
243                 __pAppRegistry->Add(__checkFlagKey, checkFlag);
244         }
245         __pAppRegistry->Save();
246 }
247
248 void
249 StopWatchPresentationModel::GetStopWatchLaps(ArrayList& lapsList, ArrayList& lapTimeTextList, String& tempTicker, int& checkFlag)
250 {
251         result r = E_FAILURE;
252         int lapCount = 0;
253         String watchTextKey ;
254         String lapTimeTextKey;
255         String *watchText = new String();
256         String *lapTimeText = new String();
257         r = __pAppRegistry->Get(__LapCountKey, lapCount);
258
259         if(IsFailed(r))
260         {
261                 AppLog("StopWatchPresentationModel::GetStopWatchLaps result = %s", GetErrorMessage(r));
262         }
263
264         for(int count = 0; count < lapCount; count++)
265         {
266                 watchTextKey.Clear();
267                 lapTimeTextKey.Clear();
268
269                 watchTextKey.Append("watchText");
270                 watchTextKey.Append(count);
271
272                 lapTimeTextKey.Append("lapTimeText");
273                 lapTimeTextKey.Append(count);
274
275                 r = __pAppRegistry->Get(watchTextKey, *watchText);
276
277                 if(r == E_SUCCESS)
278                 {
279                         lapsList.InsertAt((new (std::nothrow) String(*watchText)), count);
280                 }
281
282                 r = __pAppRegistry->Get(lapTimeTextKey, *lapTimeText);
283
284                 if(r == E_SUCCESS)
285                 {
286                         lapTimeTextList.InsertAt((new (std::nothrow) String(*lapTimeText)), count);
287                 }
288         }
289         r = __pAppRegistry->Get(__tmpTicker, tempTicker);
290
291         if(IsFailed(r))
292         {
293                 AppLog("StopWatchPresentationModel::GetStopWatchValue result = %s", GetErrorMessage(r));
294         }
295
296         r = __pAppRegistry->Get(__checkFlagKey, checkFlag);
297
298         if(IsFailed(r))
299         {
300                 AppLog("StopWatchPresentationModel::GetStopWatchValue result = %s", GetErrorMessage(r));
301         }
302
303         delete watchText;
304         watchText = null;
305
306         delete lapTimeText;
307         lapTimeText = null;
308 }
309
310 StopWatchPresentationModel*
311 StopWatchPresentationModel::GetInstance(void)
312 {
313         if (__pInstance == null)
314         {
315                 CreateInstance();
316         }
317
318         return __pInstance;
319 }
320
321 void
322 StopWatchPresentationModel::SetStatus(int& status)
323 {
324         result r = E_FAILURE;
325
326         if (__pAppRegistry == null)
327         {
328                 __pAppRegistry = UiApp::GetInstance()->GetAppRegistry();
329         }
330
331         r = __pAppRegistry->Set(__statusKey, status);
332
333         if (r == E_KEY_NOT_FOUND)
334         {
335                 __pAppRegistry->Add(__statusKey, status);
336         }
337 }
338
339 result
340 StopWatchPresentationModel::Construct(void)
341 {
342         return E_SUCCESS;
343 }
344
345 void
346 StopWatchPresentationModel::CreateInstance(void)
347 {
348         __pInstance = new (std::nothrow) StopWatchPresentationModel();
349         result r = __pInstance->Construct();
350
351         if (IsFailed(r))
352         {
353                 delete __pInstance;
354                 __pInstance = null;
355                 return;
356         }
357
358         std::atexit(DestroyInstance);
359 }
360
361 void
362 StopWatchPresentationModel::DestroyInstance(void)
363 {
364         delete __pInstance;
365 }