Applied latest source code
[apps/native/preloaded/Clock.git] / src / ClkTimerPresentationModel.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        ClkTimerPresentationModel.cpp
20  * @brief       This file contains the definitions of the TimerPresentationModel Class.
21  */
22
23 #include <cstdlib>
24 #include <FApp.h>
25 #include <FBase.h>
26
27 #include "ClkAlarmPresentationModel.h"
28 #include "ClkTimerPresentationModel.h"
29
30 using namespace Tizen::App;
31 using namespace Tizen::Base;
32
33 TimerPresentationModel* TimerPresentationModel::__pInstance = NULL;
34
35 TimerPresentationModel::TimerPresentationModel()
36 :__focusKey("focusValue")
37  ,__nameKey("timerTime")
38  ,__pAppRegistry(null)
39  ,__remainingTimeKey("remainingTime")
40  ,__restartTickKey("restartTick")
41  ,__statusKey("timerStatus")
42  ,__startTickKey("startTick")
43  ,__stopTickKey("stopTick")
44  ,__tempTickKey("tempTick")
45  ,__userSetValueKey("userSetValue")
46 ,__registeredValueKey("registeredValue")
47 ,userValue("00:01:00")
48 {
49
50 }
51
52 TimerPresentationModel::~TimerPresentationModel()
53 {
54
55 }
56
57 void
58 TimerPresentationModel::SetTimerValue(const DateTime& remainingTime, int& status, String& startTick, String& tempTick, String& restartTick, String& stopTick, String& userSetValue, String& registeredValue)
59 {
60         result r = E_FAILURE;
61         DateTime ringingTime;
62         String time;
63         DataServiceProvider* pDataServiceProvider= DataServiceProvider::GetInstance();
64
65         if ( pDataServiceProvider == NULL )
66         {
67                 return;
68         }
69
70         pDataServiceProvider->GetCurrentDateTime(ringingTime);
71         ringingTime.AddHours(remainingTime.GetHour());
72         ringingTime.AddMinutes(remainingTime.GetMinute());
73         ringingTime.AddSeconds(remainingTime.GetSecond());
74         AppLog("remainingTime = %ls ringing time = %ls", remainingTime.ToString().GetPointer(), ringingTime.ToString().GetPointer());
75
76         if(__pAppRegistry == null)
77         {
78                 __pAppRegistry = UiApp::GetInstance()->GetAppRegistry();
79         }
80
81         r = __pAppRegistry->Set(__remainingTimeKey, remainingTime.ToString());
82
83         if (r == E_KEY_NOT_FOUND)
84         {
85                 __pAppRegistry->Add(__remainingTimeKey, remainingTime.ToString());
86         }
87
88         time = ringingTime.ToString();
89         r = __pAppRegistry->Set(__nameKey, time);
90
91         if (r == E_KEY_NOT_FOUND)
92         {
93                 __pAppRegistry->Add(__nameKey, time);
94         }
95
96         r = __pAppRegistry->Set(__statusKey, status);
97
98         if (r == E_KEY_NOT_FOUND)
99         {
100                 __pAppRegistry->Add(__statusKey, status);
101         }
102
103         r = __pAppRegistry->Set(__startTickKey, startTick);
104
105         if (r == E_KEY_NOT_FOUND)
106         {
107                 __pAppRegistry->Add(__startTickKey, startTick);
108         }
109
110         r = __pAppRegistry->Set(__tempTickKey, tempTick);
111
112         if (r == E_KEY_NOT_FOUND)
113         {
114                 __pAppRegistry->Add(__tempTickKey, tempTick);
115         }
116
117         r = __pAppRegistry->Set(__restartTickKey, restartTick);
118
119         if (r == E_KEY_NOT_FOUND)
120         {
121                 __pAppRegistry->Add(__restartTickKey, restartTick);
122         }
123
124         r = __pAppRegistry->Set(__stopTickKey, stopTick);
125
126         if (r == E_KEY_NOT_FOUND)
127         {
128                 __pAppRegistry->Add(__stopTickKey, stopTick);
129         }
130
131         r = __pAppRegistry->Set(__userSetValueKey, userSetValue);
132
133         if (r == E_KEY_NOT_FOUND)
134         {
135                 __pAppRegistry->Add(__userSetValueKey, userSetValue);
136         }
137
138         r = __pAppRegistry->Set(__registeredValueKey, registeredValue);
139
140         if (r == E_KEY_NOT_FOUND)
141         {
142                 __pAppRegistry->Add(__registeredValueKey, registeredValue);
143         }
144
145         __pAppRegistry->Save();
146 }
147
148 void
149 TimerPresentationModel::GetTimerValue(DateTime& timerValue, String& remainingTime, int& status, String& startTick, String& tempTick, String& restartTick, String& stopTick, String& userSetValue, String& registeredValue)
150 {
151         AppLog("TimerPresentationModel::GetTimerValue");
152         result r = E_FAILURE;
153         DateTime currentTime;
154         DateTime time;
155         String ringingTime;
156         DataServiceProvider* pDataServiceProvider= DataServiceProvider::GetInstance();
157
158         if(__pAppRegistry == null)
159         {
160                 __pAppRegistry = UiApp::GetInstance()->GetAppRegistry();
161         }
162
163         pDataServiceProvider->GetCurrentDateTime(currentTime);
164         r = __pAppRegistry->Get(__nameKey, ringingTime);
165
166         if(IsFailed(r))
167         {
168                 AppLog("TimerPresentationModel::GetTimerValue result = %s", GetErrorMessage(r));
169         }
170
171         DateTime::Parse(ringingTime, time);
172         AppLog("ringing time = %ls remainningTime = %ls currentTime = %ls",ringingTime.GetPointer(), time.ToString().GetPointer(), currentTime.ToString().GetPointer());
173
174         timerValue.AddHours(time.GetHour() - currentTime.GetHour());
175         timerValue.AddMinutes(time.GetMinute() - currentTime.GetMinute());
176         timerValue.AddSeconds(time.GetSecond() - currentTime.GetSecond());
177         r = __pAppRegistry->Get(__statusKey, status);
178
179         if( IsFailed(r))
180         {
181                 AppLog("TimerPresentationModel::GetTimerValue result = %s", GetErrorMessage(r));
182         }
183
184         AppLog("TimerPresentationModel::GetTimerValue end timerValue = %ls status = %d", timerValue.ToString().GetPointer(), status);
185
186         r = __pAppRegistry->Get(__userSetValueKey, userSetValue);
187
188         if ( IsFailed(r) )
189         {
190                 AppLog("TimerPresentationModel::GetTimerValue result = %s", GetErrorMessage(r));
191         }
192
193         r = __pAppRegistry->Get(__remainingTimeKey, remainingTime);
194
195         if ( IsFailed(r) )
196         {
197                 AppLog("TimerPresentationModel::GetTimerValue result = %s", GetErrorMessage(r));
198         }
199
200         if (status != 0)
201         {
202                 r = __pAppRegistry->Get(__startTickKey, startTick);
203
204                 if ( IsFailed(r) )
205                 {
206                         AppLog("TimerPresentationModel::GetTimerValue result = %s", GetErrorMessage(r));
207                 }
208
209                 r = __pAppRegistry->Get(__tempTickKey, tempTick);
210
211                 if( IsFailed(r) )
212                 {
213                         AppLog("TimerPresentationModel::GetTimerValue result = %s", GetErrorMessage(r));
214                 }
215
216                 r = __pAppRegistry->Get(__restartTickKey, restartTick);
217
218                 if ( IsFailed(r) )
219                 {
220                         AppLog("TimerPresentationModel::GetTimerValue result = %s", GetErrorMessage(r));
221                 }
222
223                 r = __pAppRegistry->Get(__stopTickKey, stopTick);
224
225                 if ( IsFailed(r) )
226                 {
227                         AppLog("TimerPresentationModel::GetTimerValue result = %s", GetErrorMessage(r));
228                 }
229
230                 r = __pAppRegistry->Get(__registeredValueKey, registeredValue);
231
232                 if ( IsFailed(r) )
233                 {
234                         AppLog("TimerPresentationModel::GetTimerValue result = %s", GetErrorMessage(r));
235                 }
236
237         }
238         AppLog("TimerPresentationModel::GetTimerValue end");
239 }
240
241 void
242 TimerPresentationModel::SetStatus(int& status)
243 {
244         result r = E_FAILURE;
245
246         if (__pAppRegistry == null)
247         {
248                 __pAppRegistry = UiApp::GetInstance()->GetAppRegistry();
249         }
250
251         r = __pAppRegistry->Set(__statusKey, status);
252
253         if (r == E_KEY_NOT_FOUND)
254         {
255                 __pAppRegistry->Add(__statusKey, status);
256         }
257 }
258
259 void
260 TimerPresentationModel::SetTimerTime(const DateTime& dateTime)
261 {
262         result r = E_FAILURE;
263
264         if (__pAppRegistry == null)
265         {
266                 __pAppRegistry = UiApp::GetInstance()->GetAppRegistry();
267         }
268
269         r = __pAppRegistry->Set(__remainingTimeKey, dateTime.ToString());
270
271         if (r == E_KEY_NOT_FOUND)
272         {
273                 __pAppRegistry->Add(__remainingTimeKey, dateTime.ToString());
274         }
275 }
276
277 void
278 TimerPresentationModel::SetFocus(const int& value)
279 {
280         result r = E_FAILURE;
281
282         if (__pAppRegistry == null)
283         {
284                 __pAppRegistry = UiApp::GetInstance()->GetAppRegistry();
285         }
286
287         r = __pAppRegistry->Set(__focusKey, value);
288
289         if (r == E_KEY_NOT_FOUND)
290         {
291                 __pAppRegistry->Add(__focusKey, value);
292         }
293 }
294 void
295 TimerPresentationModel::SetRegisteredTime(DateTime& dateTime)
296 {
297         result r = E_FAILURE;
298         String time;
299
300         if (__pAppRegistry == null)
301         {
302                 __pAppRegistry = UiApp::GetInstance()->GetAppRegistry();
303         }
304
305         r = __pAppRegistry->Set(__registeredValueKey, dateTime.ToString());
306
307         if (r == E_KEY_NOT_FOUND)
308         {
309                 __pAppRegistry->Add(__registeredValueKey, dateTime.ToString());
310         }
311
312         __pAppRegistry->Save();
313 }
314
315 TimerPresentationModel*
316 TimerPresentationModel::GetInstance(void)
317 {
318         if (__pInstance == null)
319         {
320                 CreateInstance();
321         }
322
323         return __pInstance;
324 }
325
326 result
327 TimerPresentationModel::Construct(void)
328 {
329         return E_SUCCESS;
330 }
331
332 void
333 TimerPresentationModel::CreateInstance(void)
334 {
335         __pInstance = new (std::nothrow) TimerPresentationModel();
336         result r = __pInstance->Construct();
337         if (IsFailed(r))
338         {
339                 delete __pInstance;
340                 __pInstance = null;
341                 return;
342         }
343
344         std::atexit(DestroyInstance);
345 }
346
347 void
348 TimerPresentationModel::DestroyInstance(void)
349 {
350         delete __pInstance;
351 }