64876ee6e02dc8ee1d6707cf3ada170a1ae6af25
[apps/core/preloaded/calendar.git] / ug / edit / ug-edit.c
1 /*
2   *
3   *  Copyright 2012  Samsung Electronics Co., Ltd
4   *
5   *  Licensed under the Flora License, Version 1.0 (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://www.tizenopensource.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
20
21
22
23 #include <ui-gadget-module.h>
24 #include <libintl.h>
25 #include <appcore-common.h>
26
27 #include "view.h"
28
29 #define MAX_STR_LEN                             1024
30
31 // TODO: get full path from caller
32 #define VFILE_PATH "/opt/data/ug-calendar"
33
34 static void __cal_edit_ug_init_base_time(ug_data *ugd)
35 {
36         if (NULL == ugd)
37         {
38                 return;
39         }
40         struct tm tm;
41         time_t now;
42
43         now = time(NULL);
44         localtime_r(&now, &tm);
45         tm.tm_min = 0; // min, sec reset for mktime()
46         tm.tm_sec = 0;
47
48         ugd->today_tm = tm;
49         ugd->base_tm = ugd->today_tm;
50 }
51
52 static Evas_Object *__cal_edit_ug_create_layout(Evas_Object *parent, ug_data *ugd)
53 {
54         ugd->base = cal_util_add_layout(parent, NULL);
55         CAL_ASSERT(ugd->base);
56
57         ugd->nv = elm_naviframe_add(parent);
58         CAL_ASSERT(ugd->nv);
59
60         ugd->bg = cal_util_add_bg(ugd->base, EINA_FALSE);
61         CAL_ASSERT(ugd->bg);
62
63         elm_object_part_content_set(ugd->base, "elm.swallow.content", ugd->nv);
64         elm_object_part_content_set(ugd->base, "elm.swallow.bg", ugd->bg);
65         elm_object_theme_set(ugd->nv, ugd->theme);
66
67         ugd->u_type = UG_EDIT;
68
69         __cal_edit_ug_init_base_time(ugd);
70
71         cal_edit_create_view(ugd, ugd->nv);
72
73         return ugd->base;
74 }
75
76 static void __cal_edit_ug_get_strnote(ug_data *ugd, bundle *data)
77 {
78         if (NULL == ugd || NULL == data)
79         {
80                 return;
81         }
82
83         const char *str = bundle_get_val(data, "note");
84         if (str)
85                 ugd->strnote = strdup(str);
86         else
87                 ugd->strnote = NULL;
88 }
89 static void __cal_edit_ug_get_bundle_parameter(ug_data *ugd, bundle *data)
90 {
91         const char *str = NULL;
92         time_t t = time(NULL);
93         struct tm cur_tm;
94         struct tm* returned_tm = gmtime_r(&t, &cur_tm);
95         if (!returned_tm)
96         {
97                 ERR("gmtime return null");
98                 return;
99         }
100
101         if (NULL != data)
102         {
103                 //year
104                 str = bundle_get_val(data, "year");
105                 if (NULL != str)
106                 {
107                         ugd->tm_year = atoi(str);
108                 }
109                 else
110                 {
111                         ugd->tm_year = cur_tm.tm_year;
112                 }
113
114                 //month
115                 str = bundle_get_val(data, "month");
116                 if (NULL != str)
117                 {
118                         ugd->tm_mon = atoi(str);
119                 }
120                 else
121                 {
122                         ugd->tm_mon = cur_tm.tm_mon;
123                 }
124
125                 //day
126                 str = bundle_get_val(data, "day");
127                 if (NULL != str)
128                 {
129                         ugd->tm_mday = atoi(str);
130                 }
131                 else
132                 {
133                         ugd->tm_mday = cur_tm.tm_mday;
134                 }
135
136
137                 //day
138                 str = bundle_get_val(data, "hour");
139                 if (NULL != str)
140                 {
141                         ugd->base_hour = atoi(str);
142                 }
143                 else
144                 {
145                         ugd->base_hour = -1;
146                 }
147
148                 //index
149                 str = bundle_get_val(data, "index");
150                 if (NULL != str)
151                 {
152                         ugd->cid = atoi(str);
153                 }
154                 else
155                 {
156                         ugd->cid = 0;
157                 }
158
159                 str = bundle_get_val(data, "account_id");
160                 if (NULL != str)
161                 {
162                         ugd->account_id =  atoi(str);
163                 }
164                 else
165                 {
166                         ugd->account_id = 0;
167                 }
168
169                 str = bundle_get_val(data, "mail_id");
170                 if (NULL != str)
171                 {
172                         ugd->mail_id =  atoi(str);
173                 }
174                 else
175                 {
176                         ugd->mail_id = 0;
177                 }
178
179                 str = bundle_get_val(data, "src_box");
180                 if (NULL != str)
181                 {
182                         ugd->src_box = strdup(str);
183                 }
184                 else
185                 {
186                         ugd->src_box = NULL;
187                 }
188
189                 __cal_edit_ug_get_strnote(ugd, data);
190         }
191
192 }
193
194 static void *__cal_edit_ug_create_callback(struct ui_gadget *ug, enum ug_mode mode, bundle *data, void *priv)
195 {
196         if (NULL == ug || NULL == priv)
197         {
198                 return NULL;
199         }
200
201         Evas_Object *parent = NULL;
202         ug_data *ugd = NULL;
203         Evas_Object *base = NULL;
204
205         ugd = priv;
206         ugd->ug = ug;
207
208         __cal_edit_ug_get_bundle_parameter(ugd,data);
209
210         if (UG_MODE_FULLVIEW != mode)
211                 return NULL;
212
213         parent = ug_get_parent_layout(ug);
214         CAL_ASSERT(parent);
215
216         ugd->win = ug_get_window();
217         CAL_ASSERT(ugd->win);
218
219         base = __cal_edit_ug_create_layout(parent, ugd);
220         if (NULL == base)
221         {
222                 CALENDAR_SVC_DISCONNECT();
223                 return NULL;
224         }
225
226         return base;
227 }
228
229 static void __cal_edit_ug_destroy_callback(struct ui_gadget *ug, bundle *data, void *priv)
230 {
231         CAL_FN_START;
232
233         ug_data *ugd;
234
235         if (!ug || !priv)
236                 return;
237
238         ugd = priv;
239
240         cal_util_delete_evas_object(&ugd->base);
241
242         free(ugd->strnote);
243 }
244
245 static void __cal_edit_ug_key_callback(struct ui_gadget *ug, enum ug_key_event evt, bundle *data, void *priv)
246 {
247         if (!ug)
248                 return;
249
250         DBG("UG: key event %d", evt);
251
252         switch (evt)
253         {
254         case UG_KEY_EVENT_END:
255                 ug_destroy_me(ug);
256                 break;
257
258         default:
259                 break;
260         }
261 }
262
263 static int __cal_edit_ug_initialize(ug_data *ugd)
264 {
265         if (NULL == ugd)
266         {
267                 return -1;
268         }
269
270         int r;
271
272         r = CALENDAR_SVC_CONNECT();
273         if (r != CAL_SUCCESS)
274         {
275                 ERR("calendar service connect failed");
276                 return -1;
277         }
278
279         r = account_connect();
280         c_retvm_if(r != ACCOUNT_ERROR_NONE, -1, "account_connect is failed(%x)", r);
281
282         appcore_set_i18n(CALENDAR_PACKAGE, LOCALEDIR);
283
284         ugd->theme = elm_theme_new();
285         if (NULL == ugd->theme)
286         {
287                 return -1;
288         }
289
290         elm_theme_ref_set(ugd->theme, NULL);     //refer to default theme(NULL)
291
292         cal_util_connect_pattern_generator();
293
294         return 0;
295 }
296
297 static int __cal_edit_ug_finish(ug_data *ugd)
298 {
299         int r;
300
301         r = CALENDAR_SVC_DISCONNECT();
302         if (r != CAL_SUCCESS)
303         {
304                 ERR("calendar service close failed");
305                 return -1;
306         }
307
308         r = account_disconnect();
309         c_retvm_if(r != ACCOUNT_ERROR_NONE, -1, "account_disconnect is failed(%x)", r);
310
311         if(NULL != ugd->theme)
312         {
313                 elm_theme_extension_del(ugd->theme, EDJDIR "/calendar_theme.edj");              //delete extension to the new theme (optional)
314                 elm_theme_free(ugd->theme);
315                 ugd->theme = NULL;
316         }
317
318         cal_util_disconnect_pattern_generator();
319
320         return 0;
321 }
322
323
324 static inline void __cal_edit_ug_bind_text_domain()
325 {
326         const char *r;
327
328         r = bindtextdomain(UGSETTINGS, UGLOCALE);
329         if (!r)
330                 ERR("UG: bindtextdomain error");
331 }
332
333 API int UG_MODULE_INIT(struct ug_module_ops *ops)
334 {
335         ug_data *ugd;
336
337         if (!ops)
338                 return -1;
339
340         CAL_CALLOC(ugd, 1, ug_data);
341
342         ops->create = __cal_edit_ug_create_callback;
343         ops->destroy = __cal_edit_ug_destroy_callback;
344         ops->key_event = __cal_edit_ug_key_callback;
345         ops->priv = ugd;
346         ops->opt = UG_OPT_INDICATOR_ENABLE;
347
348         __cal_edit_ug_bind_text_domain();
349
350         __cal_edit_ug_initialize(ugd);
351
352         return 0;
353 }
354
355 API void UG_MODULE_EXIT(struct ug_module_ops *ops)
356 {
357         ug_data *ugd;
358
359         if (!ops)
360                 return;
361
362         ugd = ops->priv;
363         if (ugd)
364         {
365                 __cal_edit_ug_finish(ugd);
366                 free(ugd);
367         }
368 }