Tizen 2.1 base
[apps/core/preloaded/calendar.git] / viewer / viewer.c
1
2
3 /*
4   *
5   *  Copyright 2012  Samsung Electronics Co., Ltd
6   *
7   *  Licensed under the Flora License, Version 1.0 (the "License");
8   *  you may not use this file except in compliance with the License.
9   *  You may obtain a copy of the License at
10   *
11   *       http://floralicense.org/license/
12   *
13   *  Unless required by applicable law or agreed to in writing, software
14   *  distributed under the License is distributed on an "AS IS" BASIS,
15   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   *  See the License for the specific language governing permissions and
17   *  limitations under the License.
18   */
19
20
21 #include <stdio.h>
22 #include <aul.h>
23 #include <Ecore_X.h>
24 #include <ui-gadget.h>
25 #include <app.h>
26
27 #include "detail-view.h"
28
29 #define SERVICE_CALENDAR_VIEWER_CAL_ID_NAME "cal_id"
30 #define SERVICE_CALENDAR_VIEWER_MIME_NAME AUL_K_MIME_CONTENT
31 #define VCALENDAR_LABLE_STRING_LENGTH 128
32
33 #if !defined(CV_PACKAGE)
34 #  define CV_PACKAGE "calendar-viewer"
35 #endif
36
37 typedef struct {
38         struct appdata *ad;
39         Evas_Object *layout;
40         Evas_Object *genlist;
41         Evas_Object *save_to_calendar;
42         int selected_item_count;
43 }cal_viewer_data;
44
45 typedef struct {
46         calendar_record_h record;
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, calendar_list_h list);
53 static void __cal_viewer_update_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(calendar_record_h record)
64 {
65         c_retv_if(!record, NULL);
66
67         char stime[512] = {0};
68
69         struct tm tm = {0};
70         const char* time = CAL_UTIL_TIME_FORMAT_1;
71
72         calendar_time_s start_time = {0};
73
74         _calendar_get_start_time(record, &start_time);
75
76         cal_util_convert_lli_to_tm(NULL, start_time.time.utime, &tm);
77
78         cal_util_get_time_text(stime, sizeof(stime), NULL, time, &tm);
79
80         return g_strdup_printf("%s", stime);
81 }
82
83 static char *__cal_viewer_get_vcs_genlist_item_label(void *data, Evas_Object *obj, const char *part)
84 {
85         c_retvm_if(!data, NULL, "data is null");
86         c_retvm_if(!part, NULL, "part is null");
87
88         vcs_item_data *item_data = (vcs_item_data*)data;
89         c_retv_if(!item_data->record, NULL);
90
91         if (!CAL_STRCMP(part,"elm.text.1")) {
92                 return _calendar_get_summary(item_data->record);
93
94         } else if (!CAL_STRCMP(part,"elm.text.2")) {
95                 return _calendar_get_location(item_data->record);
96
97         } else if (!CAL_STRCMP(part, "elm.text.3")) {
98
99                 return __cal_viewer_vcs_get_time_str(item_data->record);
100         }
101
102         return NULL;
103 }
104
105 static void __cal_viewer_update_small_info(cal_viewer_data *p, int count)
106 {
107         c_retm_if(!p, "p is null");
108
109         if (count <= 0) {
110                 cal_util_hide_small_information(p->layout);
111                 return;
112         }
113
114         struct appdata *ad = p->ad;
115         c_retm_if(!ad, "ad is null");
116
117         Evas_Object *nv = ad->naviframe;
118         c_retm_if(!nv, "nv is null");
119
120         char lable_str[VCALENDAR_LABLE_STRING_LENGTH] = { '\0'};
121
122         snprintf(lable_str, sizeof(lable_str), "%s (%d)", S_("IDS_COM_POP_SELECTED"), count);
123
124         cal_util_show_small_information(p->layout, lable_str, 0.0);
125 }
126
127 static void __cal_viewer_vcs_check_changed_callback(void *data, Evas_Object *obj, void *event_info)
128 {
129         c_retm_if(!data, "data is null");
130
131         vcs_item_data *item_data= data;
132
133         Evas_Object *genlist = elm_object_parent_widget_get(obj);
134         c_retm_if(!genlist, "genlist is null");
135
136         cal_viewer_data *p = evas_object_data_get(genlist, "priv");
137         c_retm_if(!p, "p is null");
138
139         Eina_Bool checked = elm_check_state_get(obj);
140         if (checked) {
141                 p->selected_item_count++;
142                 item_data->checked = EINA_TRUE;
143         } else {
144                 p->selected_item_count--;
145                 item_data->checked = EINA_FALSE;
146         }
147
148         if (0 < p->selected_item_count)
149                 elm_object_disabled_set(p->save_to_calendar, EINA_FALSE);
150         else
151                 elm_object_disabled_set(p->save_to_calendar, EINA_TRUE);
152
153         __cal_viewer_update_small_info(p, p->selected_item_count);
154 }
155
156 static Evas_Object* __cal_viewer_get_vcs_genlist_icon(void *item_data, Evas_Object *obj, const char *part)
157 {
158         c_retvm_if(!part, NULL, "part is null");
159         c_retvm_if(!obj, NULL, "obj is null");
160         c_retvm_if(!item_data, NULL, "data is null");
161
162         Evas_Object *icon = NULL;
163         vcs_item_data *item = item_data;
164         c_retvm_if(!item->record, NULL, "item->record is null");
165
166         if (!CAL_STRCMP(part, "elm.icon") ) {
167                 icon = elm_check_add(obj);
168                 c_retvm_if(!icon, NULL, "elm_check_add returned null");
169
170                 elm_check_state_pointer_set(icon, &item->checked);
171                 evas_object_smart_callback_add(icon, "changed", __cal_viewer_vcs_check_changed_callback, item);
172                 evas_object_propagate_events_set(icon, EINA_FALSE);
173                 return icon;
174         }
175
176         return NULL;
177 }
178
179 static bool viewer_create(void *data)
180 {
181         struct appdata *ad = data;
182         Evas_Object *win;
183         Evas_Object *bg;
184         Evas_Object *nv;
185
186         calendar_error_e error = CALENDAR_ERROR_NONE;
187
188         error = calendar_connect();
189         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_connect() is failed(%x)", error);
190
191         _calendar_init_hash();
192
193         elm_theme_extension_add(NULL, "/usr/apps/"CALENDAR_PACKAGE"/res/edje/theme.edj");
194
195         /* create window */
196         win = cal_util_add_window(CV_PACKAGE, NULL, NULL);
197         c_retvm_if(!win, false, "cal_util_add_window() Failed");
198         ad->win = win;
199
200         bg = cal_util_add_bg(win, EINA_TRUE);
201         c_retvm_if(NULL == bg, false, "cal_util_add_bg() Failed");
202
203         Evas_Object *conformant = elm_conformant_add(win);
204         c_retv_if(!conformant, false);
205
206         evas_object_size_hint_weight_set(conformant, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
207         elm_win_resize_object_add(win, conformant);
208         evas_object_show(conformant);
209
210         Evas_Object *base_layout = elm_layout_add(conformant);
211         c_retv_if(!base_layout, false);
212
213         Eina_Bool r = elm_layout_theme_set(base_layout, "layout", "application", "default");
214         c_warn_if(!r, "elm_layout_theme_set is failed");
215
216         evas_object_size_hint_weight_set(base_layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
217
218         elm_object_content_set(conformant, base_layout);
219
220         ad->base = base_layout;
221
222         nv = elm_naviframe_add(ad->base);
223         c_retvm_if(NULL == win, false, "elm_naviframe_add() Failed");
224         ad->naviframe = nv;
225         elm_object_part_content_set(ad->base, "elm.swallow.content", ad->naviframe);
226
227         const char *path = bindtextdomain(CALENDAR, LOCALEDIR);
228         c_warn_if(!path, "bindtextdomain(%s, %s) is failed.", CALENDAR, LOCALEDIR);
229
230         cal_util_connect_pattern_generator();
231
232         evas_object_show(win);
233
234         UG_INIT_EFL(ad->win, UG_OPT_INDICATOR_PORTRAIT_ONLY);
235
236         return true;
237 }
238
239 static void viewer_terminate(void *data)
240 {
241         struct appdata *ad = data;
242
243         cal_util_disconnect_pattern_generator();
244
245         ug_destroy_all();
246
247         if (ad->win)
248                 evas_object_del(ad->win);
249
250         calendar_error_e error = CALENDAR_ERROR_NONE;
251
252         error = calendar_disconnect();
253         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_disconnect() is failed(%x)", error);
254
255         return;
256 }
257
258 static void viewer_pause(void *data)
259 {
260         ug_pause();
261
262         return;
263 }
264
265 static void viewer_resume(void *data)
266 {
267         ug_resume();
268
269         return;
270 }
271
272 static void viewer_service(service_h service, void *data)
273 {
274         c_retm_if(!service, "service is null.");
275         c_retm_if(!data, "data is null.");
276
277         int cal_id;
278         int r;
279         char *operation = NULL;
280         char *val_cal_id = NULL;
281         char *val_ical = NULL;
282         struct appdata *ad = data;
283
284         if (NULL == ad->win) {
285                 ERR("win is NULL");
286                 elm_exit();
287                 return;
288         }
289
290         r = service_get_extra_data(service, SERVICE_CALENDAR_VIEWER_CAL_ID_NAME, &val_cal_id);
291         c_warn_if(SERVICE_ERROR_NONE != r, "service_get_extra_data is failed(%d).", r);
292
293         if (val_cal_id && *val_cal_id) {
294                 cal_id = atoi(val_cal_id);
295                 if (cal_id <= 0) {
296                         ERR("bundle value(cal_id) is wrong(%d)", cal_id);
297                 }
298                 else {
299                         ad->cid= cal_id;
300
301                         calendar_record_h record = _calendar_get_record_with_index(cal_id);
302
303                         cal_detail_create_view(ad, record, NULL, NULL);
304                 }
305
306                 free(val_cal_id);
307                 val_cal_id = NULL;
308         } else {
309                 r = service_get_operation(service, &operation);
310                 c_retm_if(SERVICE_ERROR_NONE != r, "service_get_operation is failed.");
311
312                 r = service_get_uri(service, &val_ical);
313                 c_retm_if(SERVICE_ERROR_NONE != r, "service_get_uri is failed.");
314
315                 /*MIME CONTENT FOR VCS*/
316                 if (val_ical) {
317                         char *raw_data = NULL;
318                         int size = 0;
319                         struct stat st;
320
321                         FILE * file = fopen(val_ical, "r");
322                         if(!file) {
323                                 free(raw_data);
324                                 ERR("Failed to open file!");
325                                 return;
326                         }
327
328                         if(stat(val_ical, &st) != 0) {
329                                 free(raw_data);
330                                 ERR("stat() is failed!");
331                                 fclose( file );
332                                 return;
333                         }
334
335                         size = st.st_size;
336                         CAL_CALLOC(raw_data, (size+1), char);
337                         CAL_ASSERT(raw_data);
338                         memset(raw_data,0,(size+1));
339
340                         r = fread(raw_data, 1, size, file);
341                         if (r < 0) {
342                                 free(raw_data);
343                                 ERR("Failed to read file!");
344                                 return;
345                         }
346
347                         fclose( file );
348
349                         ad->is_aul = EINA_TRUE;
350                         ad->request_view = CV_DETAIL;
351
352                         calendar_error_e error = CALENDAR_ERROR_NONE;
353
354                         calendar_list_h list = NULL;
355
356                         error = calendar_vcalendar_parse_to_calendar(raw_data, &list);
357                         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_vcalendar_parse_to_calendar() is failed(%x)", error);
358
359                         __cal_viewer_create_view(ad, ad->naviframe, list);
360
361                         error = calendar_list_destroy(list, false);
362                         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_list_destroy() is failed(%x)", error);
363
364                         free(raw_data);
365                         free(val_ical);
366                         val_ical = NULL;
367                 }
368                 else {
369                         ERR("bundle value is NULL");
370                         elm_exit();
371                         return;
372                 }
373         }
374
375         elm_win_activate(ad->win);
376         return;
377 }
378
379 static void __cal_viewer_genlist_select_callback(void *data, Evas_Object *obj, void *event_info)
380 {
381         c_retm_if(!data, "data is null");
382
383         vcs_item_data *item = data;
384         c_retm_if(!item->record, "__cal_viewer_genlist_sel param error");
385
386         cal_viewer_data *p = evas_object_data_get(obj, "priv");
387         c_retm_if(!p || !p->ad, "p is null");
388
389         elm_genlist_item_selected_set((Elm_Object_Item *)event_info, EINA_FALSE);
390
391         cal_detail_create_view(p->ad, item->record, NULL, NULL);
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         it = elm_genlist_first_item_get(p->genlist);
405
406         int saved_item_count = 0;
407
408         while (it) {
409                 item = elm_object_item_data_get(it);
410                 temp = elm_genlist_item_next_get(it);
411                 if (NULL == item) {
412                         it = temp;
413                         continue;
414                 }
415
416                 if (item && item->record && EINA_TRUE == item->checked) {
417                         calendar_error_e error = CALENDAR_ERROR_NONE;
418
419                         error = calendar_db_insert_record(item->record, NULL);
420                         if (error == CALENDAR_ERROR_NONE)
421                                 saved_item_count++;
422                         else
423                                 ERR("calendar_db_insert_record() is failed(%x)", error);
424                 }
425                 it = temp;
426         }
427
428 }
429
430 static void __cal_viewer_back_button_callback(void *data, Evas_Object *obj, void *ei)
431 {
432         CAL_FN_START;
433
434         cal_viewer_data *p = data;
435         c_retm_if(!p || !p->genlist,  "p is null");
436
437         elm_genlist_clear(p->genlist);
438         elm_exit();
439 }
440
441 static void __cal_viewer_create_view(struct appdata *ad, Evas_Object *parent, calendar_list_h list)
442 {
443         CAL_FN_START;
444
445         c_retm_if(!ad || !parent || !list, "Input parameter is null");
446
447         cal_viewer_data *p = calloc(1, sizeof(cal_viewer_data));
448         c_retm_if(!p, "calloc is null");
449
450         p->ad = ad;
451
452         Evas_Object *layout = cal_util_add_layout(parent, "selectioninfo");
453         if (!layout){
454                 ERR("layout is NULL!");
455                 free(p);
456                 return;
457         }
458
459         p->layout = layout;
460
461         Evas_Object *genlist = elm_genlist_add(layout);
462         if(!genlist){
463                 ERR("genlist is NULL!");
464                 evas_object_del(p->layout);
465                 free(p);
466                 return;
467         }
468
469         elm_object_part_content_set(layout, "gen.swallow.contents", genlist);
470
471         Evas_Object *back_button;
472         Elm_Object_Item* navi_item;
473
474         int i = 0;
475         int count = 0;
476
477         calendar_error_e error = CALENDAR_ERROR_NONE;
478
479         error = calendar_list_get_count(list, &count);
480         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_list_get_count() is failed(%x)", error);
481
482         error = calendar_list_first(list);
483         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_list_first() is failed(%x)", error);
484
485         calendar_record_h record = NULL;
486
487         for (i = 0; i < count; i++) {
488                 error = calendar_list_get_current_record_p(list, &record);
489                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_list_get_current_record_p() is failed(%x)", error);
490
491                 vcs_item_data *item_data = calloc(1, sizeof(vcs_item_data));
492                 if(!item_data)
493                 {
494                         ERR("calloc is null!");
495                         free(p);
496                         return;
497                 }
498
499                 item_data->checked = EINA_FALSE;
500                 item_data->record = record;
501                 elm_genlist_item_append(genlist, &itc_3text_1icon_2, item_data, NULL, ELM_GENLIST_ITEM_NONE, __cal_viewer_genlist_select_callback, item_data);
502
503                 calendar_list_next(list);
504         }
505
506         evas_object_data_set(genlist, "priv", p);
507         p->genlist = genlist;
508
509         //TODO: i18n
510         navi_item = elm_naviframe_item_push(ad->naviframe, _("VCS Viewer"), NULL, NULL, layout, NULL);
511         if (!navi_item) {
512                 ERR("elm_naviframe_item_push is failed");
513                 evas_object_del(genlist);
514                 return;
515         }
516
517         p->save_to_calendar = cal_util_add_toolbar_button(ad->naviframe, "toolbar_button1", S_("IDS_COM_OPT_SAVE"), __cal_viewer_save_to_calendar_button_callback, p);
518         elm_object_disabled_set(p->save_to_calendar, EINA_TRUE);
519
520         back_button = elm_object_item_part_content_get(navi_item, "prev_btn");
521         if (!back_button) {
522                 back_button = elm_button_add(ad->naviframe);
523                 c_retm_if(!back_button, "elm_button_add is failed.");
524
525                 elm_object_style_set(back_button, "naviframe/back_btn/default");
526                 elm_object_item_part_content_set(navi_item, "prev_btn", back_button);
527         }
528         elm_object_style_set(back_button, "naviframe/back_btn/default");
529         evas_object_smart_callback_add(back_button, "clicked",  __cal_viewer_back_button_callback, p);
530 }
531
532 API int main(int argc, char *argv[])
533 {
534         struct appdata ad;
535
536         app_event_callback_s event_callback;
537
538         event_callback.create = viewer_create;
539         event_callback.terminate = viewer_terminate;
540         event_callback.pause = viewer_pause;
541         event_callback.resume = viewer_resume;
542         event_callback.service = viewer_service;
543         event_callback.low_memory = NULL;
544         event_callback.low_battery = NULL;
545         event_callback.device_orientation = NULL;
546         event_callback.language_changed = NULL;
547         event_callback.region_format_changed = NULL;
548
549         memset(&ad, 0x0, sizeof(struct appdata));
550
551         return app_efl_main(&argc, &argv, &event_callback, &ad);
552 }