Tizen 2.1 base
[apps/core/preloaded/calendar.git] / ug / list / ug.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://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 #include <libintl.h>
20 #include <vconf.h>
21
22 #include "cld.h"
23 #include "ug.h"
24
25 // TODO: get full path from caller
26 #define VFILE_PATH "/opt/usr/media/Others"
27
28 static char* __cal_ug_get_caller(service_h b, const char *key)
29 {
30         char *v;
31
32         int r = service_get_extra_data(b, key, &v);
33         c_retv_if(r != SERVICE_ERROR_NONE, NULL);
34
35         if (!v || v[0] == '\0')
36                 return strdup("Unknown");
37
38         return v;
39 }
40
41 static char* __cal_ug_get_file_name(service_h b, const char *key)
42 {
43         char *v;
44         char path[FILENAME_MAX];
45
46         int r = service_get_extra_data(b, key, &v);
47         c_retv_if(r != SERVICE_ERROR_NONE, NULL);
48
49         // TODO: check full path or not
50
51         snprintf(path, sizeof(path), "%s/%s", VFILE_PATH, v);
52
53         free(v);
54
55         r = access(path, R_OK);
56         if (r == -1)
57         {
58                 ERR("UG: Access failed: %s", path);
59                 return NULL;
60         }
61
62         return strdup(path);
63 }
64
65 static char* __cal_ug_get_file_path(service_h b, const char *key)
66 {
67         char *path = NULL;
68
69         int r = service_get_extra_data(b, key, &path);
70         c_retv_if(r != SERVICE_ERROR_NONE, NULL);
71
72         r = access(path, R_OK);
73         if (r == -1)
74         {
75                 ERR("UG: Access failed: %s", path);
76                 CAL_FREE(path);
77                 return NULL;
78         }
79
80         return path;
81 }
82
83 static enum cld_ug_mode __cal_ug_get_mode(service_h service, const char *key)
84 {
85         c_retv_if(!service, _MODE_UNKNOWN);
86         c_retv_if(!key, _MODE_UNKNOWN);
87
88         char *value = NULL;
89
90         int r = service_get_extra_data(service, key, &value);
91         c_retvm_if(r != SERVICE_ERROR_NONE, _MODE_GET_VCS, "%d", r);
92
93         enum cld_ug_mode ret;
94
95         if (!CAL_STRCMP(value, "text"))
96                 ret =  _MODE_GET_EVT;
97         else if (!CAL_STRCMP(value, "view"))
98                 ret =  _MODE_VIEW_VCS;
99
100         else
101                 ret =  _MODE_GET_VCS;
102         free(value);
103
104         return ret;
105 }
106
107 static inline int __cal_ug_set_ug_data(struct ug_data *ugd, service_h service)
108 {
109         c_retv_if(!service, -1);
110         int val;
111
112         ugd->mode = __cal_ug_get_mode(service, "mode");
113
114         char *selection_mode = NULL;
115         char *item_type = NULL;
116
117         service_get_extra_data(service, CAL_APPCONTROL_SELECTION_MODE, &selection_mode);
118
119         if (!CAL_STRCMP(selection_mode, CAL_APPCONTROL_SELECTION_MODE_SINGLE))
120                 ugd->selection_mode = SELECTION_MODE_SINGLE;
121         else
122                 ugd->selection_mode = SELECTION_MODE_MULTIPLE;
123
124         service_get_extra_data(service, CAL_APPCONTROL_ITEM_TYPE, &item_type);
125
126         if (!CAL_STRCMP(item_type, CAL_APPCONTROL_ITEM_TYPE_TODO))
127                 ugd->item_type = ITEM_TYPE_TODO;
128         else
129                 ugd->item_type = ITEM_TYPE_EVENT;
130
131         int r = vconf_get_int(VCONFKEY_REGIONFORMAT_TIME1224, &val);
132         if (r < 0)
133                 is_hour24 = 0;
134         else if( val == 1 || val == 2)
135                 is_hour24 = val -1;
136         else
137                 ERR("vconf_get_int(VCONFKEY_REGIONFORMAT_TIME1224, &val); error");
138
139         switch (ugd->mode) {
140         case _MODE_GET_VCS:
141                 ugd->caller = __cal_ug_get_caller(service, "caller");
142                 break;
143         case _MODE_VIEW_VCS:
144                 ugd->vcs_file = __cal_ug_get_file_name(service, "file");
145                 if (!ugd->vcs_file)
146                 {
147                         ERR("UG: Invalid filename");
148                         return -1;
149                 }
150                 break;
151         default:
152                 // do nothing
153                 break;
154         }
155
156         if (selection_mode)
157                 free(selection_mode);
158
159         if (item_type)
160                 free(item_type);
161
162         return 0;
163 }
164
165 static void __cal_ug_back_button_callback(void *data, Evas_Object *obj, void *ei)
166 {
167         c_ret_if(!data);
168
169         struct ug_data *ugd = data;
170
171         c_ret_if(!ugd->ug);
172
173         service_h service;
174
175         service_create(&service);
176
177         service_add_extra_data(service, CAL_APPCONTROL_RESULT, CAL_APPCONTROL_RESULT_CANCEL);
178
179         if (ugd->item_type == ITEM_TYPE_EVENT)
180                 service_add_extra_data(service, CAL_APPCONTROL_ITEM_TYPE, CAL_APPCONTROL_ITEM_TYPE_EVENT);
181         else
182                 service_add_extra_data(service, CAL_APPCONTROL_ITEM_TYPE, CAL_APPCONTROL_ITEM_TYPE_TODO);
183
184         ug_send_result(ugd->ug, service);
185
186         service_destroy(service);
187
188         ug_destroy_me(ugd->ug);
189 }
190
191 static void __cal_ug_add_back_button(struct ug_data *ugd)
192 {
193         Evas_Object *bt;
194
195         bt = elm_button_add(ugd->naviframe);
196         if (!bt)
197                 return;
198
199         elm_object_style_set(bt, "naviframe/back_btn/default");
200         // TODO: i18n
201         elm_object_text_set(bt, "Back");
202         evas_object_show(bt);
203
204         Elm_Object_Item* navi_item = elm_naviframe_top_item_get(ugd->naviframe);
205
206         elm_object_item_part_content_set(navi_item, "prev_btn", bt);
207
208         evas_object_smart_callback_add(bt, "clicked", __cal_ug_back_button_callback, ugd);
209 }
210
211 extern  void cal_vcs_viewer_ug_create(struct ug_data *ugd);
212
213 static Evas_Object *__cal_ug_create(Evas_Object *parent, struct ug_data *ugd)
214 {
215         CAL_FN_START;
216
217         ugd->base = cal_util_add_layout(parent, NULL);
218         CAL_ASSERT(ugd->base);
219
220         ugd->naviframe = elm_naviframe_add(parent);
221         CAL_ASSERT(ugd->naviframe);
222
223         ugd->bg = cal_util_add_bg(ugd->base, EINA_FALSE);
224         CAL_ASSERT(ugd->bg);
225
226         elm_object_theme_set(ugd->naviframe, ugd->theme);
227         elm_object_part_content_set(ugd->base, "elm.swallow.content", ugd->naviframe);
228         elm_object_part_content_set(ugd->base, "elm.swallow.bg", ugd->bg);
229
230         switch (ugd->mode) {
231         case _MODE_GET_EVT:
232         case _MODE_GET_VCS:
233
234                 cal_list_ug_create(S_("IDS_COM_BODY_CALENDAR"), ugd);
235                 break;
236         case _MODE_VIEW_VCS:
237                 cal_vcs_viewer_ug_create(ugd);
238                 break;
239         default:
240                 // do nothing
241                 break;
242         }
243
244         if (!ugd->cont) {
245                 evas_object_del(ugd->base);
246                 return NULL;
247         }
248
249         __cal_ug_add_back_button(ugd);
250
251         return ugd->base;
252 }
253
254 static void __cal_ug_free_data(struct ug_data *ugd)
255 {
256         free(ugd->caller);
257
258         free(ugd->vcs_file);
259 }
260
261 static void *__cal_ug_create_callback(ui_gadget_h ug, enum ug_mode mode, service_h service, void *priv)
262 {
263         CAL_FN_START;
264
265         c_retv_if(!ug, NULL);
266         c_retv_if(!service, NULL);
267         c_retv_if(!priv, NULL);
268
269         struct ug_data *ugd;
270         int r = 0;
271         Evas_Object *base;
272
273         ugd = priv;
274         ugd->ug = ug;
275
276         cal_util_connect_pattern_generator();
277
278         if (mode != UG_MODE_FULLVIEW)
279                 return NULL;
280
281         Evas_Object *parent = ug_get_parent_layout(ug);
282         c_retv_if(!parent, NULL);
283
284         r = __cal_ug_set_ug_data(ugd, service);
285         c_retv_if(r, NULL);
286         calendar_error_e err = CALENDAR_ERROR_NONE;
287         err = calendar_connect();
288         c_warn_if(err != CALENDAR_ERROR_NONE, "calendar_connect is failed(%d)", err);
289
290         _calendar_init_hash();
291
292         if (err != CALENDAR_ERROR_NONE) {
293                 ERR("UG: calendar service connect failed");
294                 __cal_ug_free_data(ugd);
295                 return NULL;
296         }
297
298         base = __cal_ug_create(parent, ugd);
299         if (!base) {
300                 calendar_error_e error = CALENDAR_ERROR_NONE;
301
302                 error = calendar_disconnect();
303                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_disconnect() is failed(%x)", error);
304                 __cal_ug_free_data(ugd);
305                 return NULL;
306         }
307
308         return base;
309 }
310
311 static void __cal_ug_destroy_callback(ui_gadget_h ug, service_h data, void *priv)
312 {
313         struct ug_data *ugd;
314
315         if (!ug || !priv)
316                 return;
317
318         ugd = priv;
319
320         calendar_error_e error = CALENDAR_ERROR_NONE;
321
322         error = calendar_disconnect();
323         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_disconnect() is failed(%x)", error);
324
325         cal_util_disconnect_pattern_generator();
326
327         __cal_ug_free_data(ugd);
328
329         cal_util_delete_evas_object(&ugd->base);
330 }
331
332 static void __cal_ug_key_callback(ui_gadget_h ug, enum ug_key_event evt,        service_h data, void *priv)
333 {
334         if (!ug)
335                 return;
336
337         DBG("UG: key event %d", evt);
338
339         switch (evt)
340         {
341         case UG_KEY_EVENT_END:
342                 ug_destroy_me(ug);
343                 break;
344         default:
345                 break;
346         }
347 }
348
349 API int UG_MODULE_INIT(struct ug_module_ops *ops)
350 {
351         CAL_FN_START;
352
353         struct ug_data *ugd;
354
355         if (!ops)
356                 return -1;
357
358         CAL_CALLOC(ugd, 1, struct ug_data);
359
360         ops->create = __cal_ug_create_callback;
361         ops->destroy = __cal_ug_destroy_callback;
362         ops->key_event = __cal_ug_key_callback;
363         ops->priv = ugd;
364         ops->opt = UG_OPT_INDICATOR_ENABLE;
365
366         const char *path = bindtextdomain(CALENDAR, LOCALEDIR);
367         c_warn_if(!path, "bindtextdomain(%s, %s) is failed.", CALENDAR, LOCALEDIR);
368
369         return 0;
370 }
371
372 API void UG_MODULE_EXIT(struct ug_module_ops *ops)
373 {
374         struct ug_data *ugd;
375
376         if (!ops)
377                 return;
378
379         ugd = ops->priv;
380         free(ugd);
381 }