test
[apps/core/preloaded/calendar.git] / viewer / viewer.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 #include <stdio.h>
20 #include <appcore-efl.h>
21 #include <aul.h>
22 #include <Ecore_X.h>
23 #include <ui-gadget.h>
24 #include <app.h>
25
26 #include "detail.h"
27
28 #define SERVICE_CALENDAR_VIEWER_CAL_ID_NAME "cal_id"
29 #define SERVICE_CALENDAR_VIEWER_MIME_NAME AUL_K_MIME_CONTENT
30 #define VCALENDAR_LABLE_STRING_LENGTH 128
31
32 #if !defined(CV_PACKAGE)
33 #  define CV_PACKAGE "calendar-viewer"
34 #endif
35
36 typedef struct {
37         struct appdata *ad;
38         unsigned int check_num;
39         Evas_Object *genlist;
40         Evas_Object *notify;
41         Evas_Object *notify_layout;
42         Elm_Object_Item *save_to_calendar;
43 }cal_viewer_data;
44
45 typedef struct {
46         cal_struct *cs;
47         Eina_Bool checked;
48 }vcs_item_data;
49
50 static char *__cal_viewer_get_vcs_genlist_item_label(void *data, Evas_Object *obj, const char *part);
51 static Evas_Object* __cal_viewer_get_vcs_genlist_icon(void *item_data, Evas_Object *obj, const char *part);
52 static void __cal_viewer_create_view(struct appdata *ad, Evas_Object *parent, GList *glist);
53 static void __cal_viewer_show_small_info(cal_viewer_data *p, int count);
54
55 static Elm_Genlist_Item_Class itc_3text_1icon_2 = {
56         .item_style = "3text.1icon.2",
57         .func.text_get = __cal_viewer_get_vcs_genlist_item_label,
58         .func.content_get = __cal_viewer_get_vcs_genlist_icon,
59         .func.del = NULL,
60         .decorate_all_item_style = "edit_default",
61 };
62
63 static char* __cal_viewer_vcs_get_time_str(cal_struct *cs)
64 {
65         c_retvm_if(!cs, NULL, "cs is null");
66
67         char buf[512];
68         char stime[512];
69
70         struct tm tm;
71         const char* time = CAL_UTIL_TIME_FORMAT_1;
72
73         cal_util_convert_lli_to_tm(NULL, CALENDAR_SVC_STRUCT_GET_LLI(cs, CALS_VALUE_LLI_DTSTART_UTIME), &tm);
74
75         cal_util_get_time_text(stime, sizeof(stime), NULL, time, &tm);
76         snprintf(buf, sizeof(buf), "%s", stime);
77
78         return strdup(buf);
79 }
80
81 static char *__cal_viewer_get_vcs_genlist_item_label(void *data, Evas_Object *obj, const char *part)
82 {
83         c_retvm_if(!data, NULL, "data is null");
84         c_retvm_if(!part, NULL, "part is null");
85
86         vcs_item_data *item_data = (vcs_item_data*)data;
87         c_retvm_if(!item_data->cs, NULL, "part is null");
88
89         const char *name = NULL;
90
91         if (!CAL_STRCMP(part,"elm.text.1")) {
92                 name = CALENDAR_SVC_STRUCT_GET_STR(item_data->cs, CAL_VALUE_TXT_SUMMARY);
93                 if (name)
94                         return strdup(name);
95
96                 return strdup(C_("IDS_CLD_BODY_NO_TITLE"));
97         } else if (!CAL_STRCMP(part,"elm.text.2")) {
98                 name = CALENDAR_SVC_STRUCT_GET_STR(item_data->cs, CAL_VALUE_TXT_LOCATION);
99                 if (CAL_STRLEN(name))
100                         return strdup(name);
101
102                 return strdup(C_("IDS_CLD_BODY_NO_LOCATION_SELECTED"));
103         } else if (!CAL_STRCMP(part, "elm.text.3")) {
104                 char *name = __cal_viewer_vcs_get_time_str(item_data->cs);
105                 return name;
106         }
107
108         return NULL;
109 }
110
111 static void __cal_viewer_show_small_info(cal_viewer_data *p, int count)
112 {
113         c_retm_if(!p, "p is null");
114         c_ret_if(count < 0);
115
116         struct appdata *ad = p->ad;
117         c_retm_if(!ad, "ad is null");
118
119         Evas_Object *nv = ad->nv;
120         c_retm_if(!nv, "nv is null");
121
122         Evas_Object *notify = NULL;
123         Evas_Object *notify_layout = NULL;
124
125         char lable_str[VCALENDAR_LABLE_STRING_LENGTH] = { '\0'};
126
127         if (!p->notify) {
128                 notify = elm_notify_add(nv);
129                 c_retm_if(!notify, "notify is null");
130
131                 elm_notify_orient_set(notify, ELM_NOTIFY_ORIENT_BOTTOM);
132                 p->notify = notify;
133
134                 notify_layout = elm_layout_add(notify);
135                 if (!notify_layout) {
136                         evas_object_del(p->notify);
137                         p->notify = NULL;
138                         return;
139                 }
140
141                 p->notify_layout = notify_layout;
142                 elm_layout_theme_set(notify_layout, "standard", "selectioninfo", "vertical/bottom_64");
143                 elm_object_content_set(notify, notify_layout);
144         }
145         snprintf(lable_str, sizeof(lable_str), "%s (%d)", S_("IDS_COM_POP_SELECTED"), count);
146         edje_object_part_text_set(CAL_UTIL_GET_EDJ_DATA(p->notify_layout), "elm.text", lable_str);
147
148         if (0 < count)
149                 evas_object_show(p->notify);
150         else
151                 evas_object_hide(p->notify);
152
153 }
154
155 static void __cal_viewer_vcs_check_changed_callback(void *data, Evas_Object *obj, void *event_info)
156 {
157         c_retm_if(!data, "data is null");
158
159         vcs_item_data *item_data= data;
160
161         Evas_Object *genlist = elm_object_parent_widget_get(obj);
162         c_retm_if(!genlist, "genlist is null");
163
164         cal_viewer_data *p = evas_object_data_get(genlist, "priv");
165         c_retm_if(!p, "p is null");
166
167         Eina_Bool checked = elm_check_state_get(obj);
168         if (checked) {
169                 p->check_num++;
170                 item_data->checked = EINA_TRUE;
171         } else {
172                 p->check_num--;
173                 item_data->checked = EINA_FALSE;
174         }
175
176         if (0 < p->check_num)
177                 elm_object_item_disabled_set(p->save_to_calendar, EINA_FALSE);
178         else
179                 elm_object_item_disabled_set(p->save_to_calendar, EINA_TRUE);
180
181         __cal_viewer_show_small_info(p, p->check_num);
182 }
183
184 static Evas_Object* __cal_viewer_get_vcs_genlist_icon(void *item_data, Evas_Object *obj, const char *part)
185 {
186         c_retvm_if(!part, NULL, "part is null");
187         c_retvm_if(!obj, NULL, "obj is null");
188         c_retvm_if(!item_data, NULL, "data is null");
189
190         Evas_Object *icon = NULL;
191         vcs_item_data *item = item_data;
192         c_retvm_if(!item->cs, NULL, "item->cs is null");
193
194         if (!CAL_STRCMP(part, "elm.icon") ) {
195                 icon = elm_check_add(obj);
196                 c_retvm_if(!icon, NULL, "elm_check_add returned null");
197
198                 elm_check_state_pointer_set(icon, &item->checked);
199                 evas_object_smart_callback_add(icon, "changed", __cal_viewer_vcs_check_changed_callback, item);
200                 evas_object_propagate_events_set(icon, EINA_FALSE);
201                 return icon;
202         }
203
204         return NULL;
205 }
206
207 static bool viewer_create(void *data)
208 {
209         struct appdata *ad = data;
210         Evas_Object *win;
211         Evas_Object *bg;
212         Evas_Object *nv;
213         Evas_Object *ly;
214         int r;
215
216         CALENDAR_SVC_CONNECT();
217
218         /* create window */
219         win = cal_util_add_window(CV_PACKAGE, NULL, NULL);
220         c_retvm_if(!win, false, "cal_util_add_window() Failed");
221         ad->win = win;
222
223         bg = cal_util_add_bg(win, EINA_TRUE);
224         c_retvm_if(NULL == bg, false, "cal_util_add_bg() Failed");
225
226         ly = cal_util_add_layout(win, NULL);
227         if (ly) {
228                 elm_win_resize_object_add(win, ly);
229         }
230         ad->base = ly;
231
232         nv = elm_naviframe_add(ad->win);
233         c_retvm_if(NULL == win, false, "elm_naviframe_add() Failed");
234         ad->nv = nv;
235         elm_object_part_content_set(ad->base, "elm.swallow.content", ad->nv);
236
237         /* init internationalization */
238         r = appcore_set_i18n(CALENDAR, LOCALEDIR);
239         c_retvm_if(r, false, "appcore_set_i18n() Failed");
240
241         cal_util_connect_pattern_generator();
242
243         evas_object_show(win);
244
245         UG_INIT_EFL(ad->win, UG_OPT_INDICATOR_PORTRAIT_ONLY);
246
247         return true;
248 }
249
250 static void viewer_terminate(void *data)
251 {
252         int ret;
253         struct appdata *ad = data;
254
255         cal_util_disconnect_pattern_generator();
256
257         ug_destroy_all();
258
259         if (ad->win)
260                 evas_object_del(ad->win);
261
262         ret = CALENDAR_SVC_DISCONNECT();
263         c_warn_if(ret != CAL_SUCCESS, "CALENDAR_SVC_DISCONNECT() Failed(%d)", ret);
264
265         return;
266 }
267
268 static void viewer_pause(void *data)
269 {
270         ug_pause();
271
272         return;
273 }
274
275 static void viewer_resume(void *data)
276 {
277         ug_resume();
278
279         return;
280 }
281
282 static void viewer_service(service_h service, void *data)
283 {
284         c_retm_if(!service, "service is null.");
285         c_retm_if(!data, "data is null.");
286
287         int cal_id;
288         int r;
289         char *operation = NULL;
290         char *val_cal_id = NULL;
291         char *val_ical = NULL;
292         struct appdata *ad = data;
293
294         if (NULL == ad->win) {
295                 ERR("win is NULL");
296                 elm_exit();
297                 return;
298         }
299
300         r = service_get_extra_data(service, SERVICE_CALENDAR_VIEWER_CAL_ID_NAME, &val_cal_id);
301         c_warn_if(SERVICE_ERROR_NONE != r, "service_get_extra_data is failed(%d).", r);
302
303         if (val_cal_id && *val_cal_id) {
304                 cal_id = atoi(val_cal_id);
305                 if (cal_id <= 0) {
306                         ERR("bundle value(cal_id) is wrong(%d)", cal_id);
307                 }
308                 else {
309                         ad->cid= cal_id;
310                         cal_detail_create_view(ad, ad->nv);
311                 }
312
313                 free(val_cal_id);
314                 val_cal_id = NULL;
315         } else {
316                 r = service_get_operation(service, &operation);
317                 c_retm_if(SERVICE_ERROR_NONE != r, "service_get_operation is failed.");
318
319                 r = service_get_uri(service, &val_ical);
320                 c_retm_if(SERVICE_ERROR_NONE != r, "service_get_uri is failed.");
321
322                 /*MIME CONTENT FOR VCS*/
323                 if (val_ical) {
324                         char *raw_data = NULL;
325                         int size = 0;
326                         struct stat st;
327
328                         FILE * file = fopen(val_ical, "r");
329                         if(!file) {
330                                 free(raw_data);
331                                 ERR("Failed to open file!");
332                                 return;
333                         }
334
335                         stat(val_ical, &st);
336                         size = st.st_size;
337                         CAL_CALLOC(raw_data, (size+1), char);
338                         CAL_ASSERT(raw_data);
339                         memset(raw_data,0,(size+1));
340
341                         r = fread(raw_data, 1, size, file);
342                         if (r < 0) {
343                                 free(raw_data);
344                                 ERR("Failed to read file!");
345                                 return;
346                         }
347
348                         fclose( file );
349
350                         ad->is_aul = EINA_TRUE;
351                         ad->request_view = CV_DETAIL;
352
353                         GList *schedules = NULL;
354                         int r = CALENDAR_SVC_READ_SCHEDULES(raw_data, &schedules);
355                         if (r != CAL_SUCCESS || NULL == schedules) {
356                                 ERR("CALENDAR_SVC_READ_SCHEDULES fail. : %d", r);
357                                 return;
358                         }
359
360                         __cal_viewer_create_view(ad, ad->nv, schedules);
361
362                         free(raw_data);
363                         free(val_ical);
364                         val_ical = NULL;
365                 }
366                 else {
367                         ERR("bundle value is NULL");
368                         elm_exit();
369                         return;
370                 }
371         }
372
373         elm_win_activate(ad->win);
374         return;
375 }
376
377 static void __cal_viewer_genlist_select_callback(void *data, Evas_Object *obj, void *event_info)
378 {
379         c_retm_if(!data, "data is null");
380
381         vcs_item_data *item = data;
382         c_retm_if(!item->cs, "__cal_viewer_genlist_sel param error");
383
384         cal_viewer_data *p = evas_object_data_get(obj, "priv");
385         c_retm_if(!p || !p->ad, "p is null");
386
387         if (p->notify)
388                 evas_object_hide(p->notify);
389
390         elm_genlist_item_selected_set((Elm_Object_Item *)event_info, EINA_FALSE);
391         cal_detail_create_viewer(p->ad, p->ad->nv, item->cs);
392 }
393
394 static void __cal_viewer_save_to_calendar_button_callback(void *data, Evas_Object *obj, void *ei)
395 {
396         CAL_FN_START;
397
398         cal_viewer_data *p = data;
399         c_retm_if(!p, "p is null");
400
401         Elm_Object_Item *it = NULL;
402         Elm_Object_Item *temp = NULL;
403         vcs_item_data *item = NULL;
404         int r;
405         it = elm_genlist_first_item_get(p->genlist);
406         while (it) {
407                 item = elm_object_item_data_get(it);
408                 temp = elm_genlist_item_next_get(it);
409                 if (NULL == item) {
410                         it = temp;
411                         continue;
412                 }
413
414                 if (item && item->cs && EINA_TRUE == item->checked) {
415                         r = CALENDAR_SVC_INSERT(item->cs);
416                         if (CAL_SUCCESS != r) {
417                                 ERR("Calendar service delete failed: %d", r);
418                         }
419                 }
420                 it = temp;
421         }
422 }
423
424 static void __cal_viewer_back_button_callback(void *data, Evas_Object *obj, void *ei)
425 {
426         CAL_FN_START;
427
428         cal_viewer_data *p = data;
429         c_retm_if(!p || !p->genlist,  "p is null");
430
431         elm_genlist_clear(p->genlist);
432         elm_exit();
433 }
434
435 static void __cal_viewer_create_view(struct appdata *ad, Evas_Object *parent, GList *glist)
436 {
437         CAL_FN_START;
438
439         c_retm_if(!ad || !parent || !glist, "Input parameter is null");
440
441         cal_viewer_data *p = calloc(1, sizeof(cal_viewer_data));
442         c_retm_if(!p, "calloc is null");
443
444         p->ad = ad;
445
446         Evas_Object *genlist = elm_genlist_add(parent);
447         c_retm_if(!genlist, "genlist is null");
448
449         Evas_Object *back_button;
450         Elm_Object_Item* navi_item;
451         cal_struct *cs = NULL;
452         while (glist) {
453                 cs = glist->data;
454                 if (cs == NULL) {
455                         ERR("Not cal struct\n");
456                         break;
457                 }
458                 vcs_item_data *item_data = calloc(1, sizeof(vcs_item_data));
459                 c_retm_if(!item_data, "calloc is null");
460
461                 item_data->checked = EINA_FALSE;
462                 item_data->cs = cs;
463                 elm_genlist_item_append(genlist, &itc_3text_1icon_2, item_data, NULL, ELM_GENLIST_ITEM_NONE, __cal_viewer_genlist_select_callback, item_data);
464                 glist = g_list_next(glist);
465         }
466         evas_object_data_set(genlist, "priv", p);
467         p->genlist = genlist;
468
469         //TODO: i18n
470         navi_item = elm_naviframe_item_push(ad->nv, _("VCS Viewer"), NULL, NULL, genlist, NULL);
471         if (!navi_item) {
472                 ERR("elm_naviframe_item_push is failed");
473                 evas_object_del(genlist);
474                 return;
475         }
476
477         Evas_Object *controlbar = cal_util_add_controlbar(ad->nv);
478         if (!controlbar) {
479                 ERR("cal_util_add_controlbar(ad->nv) is failed");
480                 elm_naviframe_item_pop(ad->nv);
481                 return;
482         }
483
484         p->save_to_calendar = elm_toolbar_item_append(controlbar, NULL, S_("IDS_COM_OPT_SAVE"), __cal_viewer_save_to_calendar_button_callback, p);
485         elm_object_item_disabled_set(p->save_to_calendar, EINA_TRUE);
486         elm_object_item_part_content_set(navi_item, "controlbar", controlbar);
487
488         back_button = elm_object_item_part_content_get(navi_item, "prev_btn");
489         if (!back_button) {
490                 back_button = elm_button_add(ad->nv);
491                 c_retm_if(!back_button, "elm_button_add is failed.");
492
493                 elm_object_style_set(back_button, "naviframe/back_btn/default");
494                 elm_object_item_part_content_set(navi_item, "prev_btn", back_button);
495         }
496         elm_object_style_set(back_button, "naviframe/back_btn/default");
497         evas_object_smart_callback_add(back_button, "clicked",  __cal_viewer_back_button_callback, p);
498 }
499
500 API int main(int argc, char *argv[])
501 {
502         struct appdata ad;
503
504         app_event_callback_s event_callback;
505
506         event_callback.create = viewer_create;
507         event_callback.terminate = viewer_terminate;
508         event_callback.pause = viewer_pause;
509         event_callback.resume = viewer_resume;
510         event_callback.service = viewer_service;
511         event_callback.low_memory = NULL;
512         event_callback.low_battery = NULL;
513         event_callback.device_orientation = NULL;
514         event_callback.language_changed = NULL;
515         event_callback.region_format_changed = NULL;
516
517         memset(&ad, 0x0, sizeof(struct appdata));
518
519         return app_efl_main(&argc, &argv, &event_callback, &ad);
520 }