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