Tizen 2.1 base
[apps/core/preloaded/calendar.git] / ug / detail / detail-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 #include <ui-gadget-module.h>
19 #include <libintl.h>
20
21 #include "cld.h"
22 #include "dialogue.h"
23
24 #include "reminder.h"
25 #include "external-ug.h"
26 #include "detail-common.h"
27 #include "detail-attendee.h"
28
29 typedef struct {
30
31         ui_gadget_h ug;
32
33         Evas_Object *base_layout;
34         Evas_Object *naviframe;
35         Evas_Object *window;
36         Evas_Object *dialogue;
37
38         Elm_Object_Item *navi_item;
39
40         calendar_record_h instance;
41         calendar_record_h record;
42
43         Ecore_Event_Handler *mouse_down_event_handler;
44         Evas_Coord mouse_down_x;
45         Evas_Coord mouse_down_y;
46
47         Evas_Object *title;
48         Evas_Object *from;
49         Evas_Object *to;
50         Evas_Object *due;
51         Evas_Object *reminder;
52         Evas_Object *recurrence;
53         Evas_Object *note;
54         Evas_Object *attendee;
55         Evas_Object *show_me_as;
56         Evas_Object *privacy;
57         Evas_Object *snote;
58         Evas_Object *photo;
59         Evas_Object *save_to;
60 } cal_detail_ug_data;
61
62 static int __cal_detail_ug_initialize(cal_detail_ug_data *data)
63 {
64         CAL_FN_START;
65
66         calendar_error_e error = CALENDAR_ERROR_NONE;
67
68         error = calendar_connect();
69         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_connect() is failed(%x)", error);
70
71         _calendar_init_hash();
72
73         const char *path = bindtextdomain(CALENDAR, LOCALEDIR);
74         c_warn_if(!path, "bindtextdomain(%s, %s) is failed.", CALENDAR, LOCALEDIR);
75
76         cal_util_connect_pattern_generator();
77
78         return 0;
79 }
80
81 static void __cal_detail_ug_finish()
82 {
83         CAL_FN_START;
84
85         calendar_error_e error = CALENDAR_ERROR_NONE;
86
87         error = calendar_disconnect();
88         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_disconnect() is failed(%x)", error);
89
90         elm_theme_extension_del(NULL, EDJDIR "/calendar_theme.edj");
91
92         cal_util_disconnect_pattern_generator();
93 }
94
95 static void __cal_detail_ug_back_button_callback(void *user_data, Evas_Object *obj, void *ei)
96 {
97         CAL_FN_START;
98
99         c_ret_if(!user_data);
100
101         cal_detail_ug_data *data = user_data;
102
103         c_ret_if(!data->ug);
104
105         ug_destroy_me(data->ug);
106 }
107
108 static Evas_Object * __cal_detail_ug_add_back_button(cal_detail_ug_data *data)
109 {
110         CAL_FN_START;
111
112         Evas_Object *back_button = elm_button_add(data->naviframe);
113         c_retv_if(!back_button, NULL);
114
115         evas_object_smart_callback_add(back_button, "clicked", __cal_detail_ug_back_button_callback, data);
116
117         elm_object_style_set(back_button, "naviframe/end_btn/default");
118
119         return back_button;
120 }
121
122 static void __cal_detail_ug_update_title_language(Evas_Object *item, calendar_record_h record)
123 {
124         c_ret_if(!item);
125
126         char *summary = _calendar_get_summary(record);
127
128         elm_object_part_text_set(item, "elm.text.1", summary);
129
130         free(summary);
131 }
132
133 static Evas_Object * __cal_detail_ug_add_title(Evas_Object *box, calendar_record_h record)
134 {
135         CAL_FN_START;
136
137         c_retv_if(!box, NULL);
138         c_retv_if(!record, NULL);
139
140         Evas_Object *item = cal_dialogue_append_item(box, "2text", NULL, NULL);
141
142         __cal_detail_ug_update_title_language(item, record);
143
144         char *location = _calendar_get_location(record);
145
146         elm_object_part_text_set(item, "elm.text.2", location);
147
148         CAL_FREE(location);
149
150         return item;
151 }
152
153 static void __cal_detail_ug_update_from_language(Evas_Object *item)
154 {
155         c_ret_if(!item);
156
157         elm_object_part_text_set(item, "elm.text.1", C_("IDS_CLD_BODY_FROM"));
158 }
159
160 static void __cal_detail_ug_update_from_region(Evas_Object *item, calendar_record_h record)
161 {
162         c_ret_if(!item);
163
164         const char *time_format = NULL;
165
166         if (!_calendar_is_allday_record(record)) {
167                 if (is_hour24)
168                         time_format = CAL_UTIL_TIME_FORMAT_6;
169                 else
170                         time_format = CAL_UTIL_TIME_FORMAT_1;
171         }
172
173         calendar_time_s time = {0};
174
175         _calendar_get_start_time(record, &time);
176
177         char *from_time = _calendar_get_time_str(&time, CAL_UTIL_DATE_FORMAT_1, time_format);
178
179         elm_object_part_text_set(item, "elm.text.2", from_time);
180
181         free(from_time);
182 }
183
184 static Evas_Object * __cal_detail_ug_add_start_time(Evas_Object *box, calendar_record_h record)
185 {
186         CAL_FN_START;
187
188         c_retv_if(!box, NULL);
189         c_retv_if(!record, NULL);
190
191         Evas_Object *item = cal_dialogue_append_item(box, "2text", NULL, NULL);
192
193         __cal_detail_ug_update_from_language(item);
194
195         __cal_detail_ug_update_from_region(item, record);
196
197         return item;
198 }
199
200 static void __cal_detail_ug_update_to_language(Evas_Object *item)
201 {
202         c_ret_if(!item);
203
204         elm_object_part_text_set(item, "elm.text.1", C_("IDS_CLD_BODY_TO"));
205 }
206
207 static void __cal_detail_ug_update_to_region(Evas_Object *item, calendar_record_h record)
208 {
209         c_ret_if(!item);
210
211         const char *time_format = NULL;
212
213         if (!_calendar_is_allday_record(record)) {
214                 if (is_hour24)
215                         time_format = CAL_UTIL_TIME_FORMAT_6;
216                 else
217                         time_format = CAL_UTIL_TIME_FORMAT_1;
218         }
219
220         calendar_time_s time = {0};
221
222         _calendar_get_end_time(record, &time);
223
224         char *to_time = _calendar_get_time_str(&time, CAL_UTIL_DATE_FORMAT_1, time_format);
225
226         elm_object_part_text_set(item, "elm.text.2", to_time);
227
228         free(to_time);
229 }
230
231 static Evas_Object * __cal_detail_ug_add_end_time(Evas_Object *box, calendar_record_h record)
232 {
233         CAL_FN_START;
234
235         c_retv_if(!box, NULL);
236         c_retv_if(!record, NULL);
237
238         Evas_Object *item = cal_dialogue_append_item(box, "2text", NULL, NULL);
239
240         __cal_detail_ug_update_to_language(item);
241
242         __cal_detail_ug_update_to_region(item, record);
243
244         return item;
245 }
246
247 static void __cal_detail_ug_update_due_language(Evas_Object *item)
248 {
249         c_ret_if(!item);
250
251         elm_object_part_text_set(item, "elm.text.1", C_("IDS_CLD_BODY_DUE_DATE"));
252 }
253
254 static void __cal_detail_ug_update_due_region(Evas_Object *item, calendar_record_h record)
255 {
256         c_ret_if(!item);
257
258         const char *time_format = NULL;
259
260         if (!_calendar_is_allday_record(record)) {
261                 if (is_hour24)
262                         time_format = CAL_UTIL_TIME_FORMAT_6;
263                 else
264                         time_format = CAL_UTIL_TIME_FORMAT_1;
265         }
266
267         calendar_time_s due_time = {0};
268
269         _calendar_get_end_time(record, &due_time);
270
271         if (due_time.time.utime == CALENDAR_TODO_NO_DUE_DATE) {
272                 elm_object_part_text_set(item, "elm.text.1", C_("IDS_CLD_BODY_NO_DUE_DATE_M_NOUN"));
273         }
274         else
275         {
276                 char *due_time_text = _calendar_get_time_str(&due_time, CAL_UTIL_DATE_FORMAT_1, time_format);
277
278                 elm_object_part_text_set(item, "elm.text.2", due_time_text);
279                 free(due_time_text);
280         }
281
282 }
283
284 static Evas_Object * __cal_detail_ug_add_due_time(Evas_Object *box, calendar_record_h record)
285 {
286         CAL_FN_START;
287
288         c_retv_if(!box, NULL);
289         c_retv_if(!record, NULL);
290
291         Evas_Object *item = cal_dialogue_append_item(box, "2text", NULL, NULL);
292
293         __cal_detail_ug_update_due_language(item);
294
295         __cal_detail_ug_update_due_region(item, record);
296
297         return item;
298 }
299
300 static char * __cal_detail_ug_get_recurrence_text(calendar_record_h record)
301 {
302         CAL_FN_START;
303
304         c_retv_if(!record, NULL);
305
306         calendar_error_e error = CALENDAR_ERROR_NONE;
307
308         int freq = 0;
309
310         error = calendar_record_get_int(record, _calendar_event.freq, &freq);
311         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_int() is failed(%x)", error);
312
313         int interval = 0;
314
315         error = calendar_record_get_int(record, _calendar_event.interval, &interval);
316         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_int() is failed(%x)", error);
317
318         if (freq == CALENDAR_RECURRENCE_WEEKLY) {
319                 if (2 == interval)
320                         freq = CAL_REPEAT_EVERY_2_WEEK;
321
322         } else if (freq == CALENDAR_RECURRENCE_DAILY) {
323                 if (3 == interval)
324                         freq = CAL_REPEAT_EVERY_3_DAY;
325         }
326
327         char buf[128] = {0};
328
329         _calendar_get_recurrence_frequency_str(freq, buf, sizeof(buf));
330
331         return strdup(buf);
332 }
333
334 static GList * __cal_detail_ug_get_reminder_list(calendar_record_h record)
335 {
336         CAL_FN_START;
337
338         c_retv_if(!record, NULL);
339
340         GList *reminder_list = NULL;
341
342         int reminder_count = 0;
343         calendar_error_e error = CALENDAR_ERROR_NONE;
344
345         if (_calendar_is_task_record(record))
346                 error = calendar_record_get_child_record_count(record, _calendar_todo.calendar_alarm, (unsigned int *)&reminder_count);
347         else
348                 error = calendar_record_get_child_record_count(record, _calendar_event.calendar_alarm, (unsigned int *)&reminder_count);
349
350         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_child_record_count() is failed(%x)", error);
351
352         int i = 0;
353         for (i = 0; i < reminder_count; i++) {
354                 calendar_record_h calendar_alarm = NULL;
355
356                 if (_calendar_is_task_record(record))
357                         error = calendar_record_get_child_record_at_p(record, _calendar_todo.calendar_alarm, i, &calendar_alarm);
358                 else
359                         error = calendar_record_get_child_record_at_p(record, _calendar_event.calendar_alarm, i, &calendar_alarm);
360
361                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_child_record_at_p() is failed(%x)", error);
362
363                 Cal_Reminder* reminder = cal_reminder_create(calendar_alarm);
364                 reminder_list = g_list_append(reminder_list, (void *)reminder);
365         }
366
367         if (reminder_list)
368                 reminder_list = g_list_first(reminder_list);
369
370         return reminder_list;
371 }
372
373 static void __cal_detail_ug_update_reminder_language(Evas_Object *item, calendar_record_h record)
374 {
375         c_ret_if(!item);
376
377         elm_object_part_text_set(item, "elm.text.1", C_("IDS_CLD_BODY_REMINDER"));
378
379         GList *reminder_list = __cal_detail_ug_get_reminder_list(record);
380         if (reminder_list) {
381
382                 char reminder_text[50] = {0};
383
384                 cal_reminder_get_string(reminder_list->data, reminder_text, sizeof(reminder_text));
385
386                 elm_object_part_text_set(item, "elm.text.2", reminder_text);
387
388                 g_list_free(reminder_list);
389         } else
390                 elm_object_part_text_set(item, "elm.text.2", S_("IDS_COM_BODY_OFF"));
391 }
392
393 static Evas_Object * __cal_detail_ug_add_reminder(Evas_Object *box, calendar_record_h record)
394 {
395         CAL_FN_START;
396
397         c_retv_if(!box, NULL);
398         c_retv_if(!record, NULL);
399
400         Evas_Object *item = cal_dialogue_append_item(box, "2text", NULL, NULL);
401
402         __cal_detail_ug_update_reminder_language(item, record);
403
404         return item;
405 }
406
407 static void __cal_detail_ug_update_recurrence_language(Evas_Object *item, calendar_record_h record)
408 {
409         c_ret_if(!item);
410
411         char *recurrence = __cal_detail_ug_get_recurrence_text(record);
412
413         elm_object_part_text_set(item, "elm.text.1", C_("IDS_CLD_BODY_REPEAT"));
414         elm_object_part_text_set(item, "elm.text.2", recurrence);
415
416         free(recurrence);
417 }
418
419 static Evas_Object * __cal_detail_ug_add_recurrence(Evas_Object *box, calendar_record_h record)
420 {
421         CAL_FN_START;
422
423         c_retv_if(!box, NULL);
424         c_retv_if(!record, NULL);
425
426         Evas_Object *item = cal_dialogue_append_item(box, "2text", NULL, NULL);
427
428         __cal_detail_ug_update_recurrence_language(item, record);
429
430         return item;
431 }
432
433 static inline char* __cal_detail_get_note_text(calendar_record_h record)
434 {
435         c_retv_if(!record, NULL);
436
437         char *note = NULL;
438
439         calendar_error_e error = CALENDAR_ERROR_NONE;
440
441         if (_calendar_is_task_record(record))
442                 error = calendar_record_get_str_p(record, _calendar_todo.description, &note);
443         else
444                 error = calendar_record_get_str_p(record, _calendar_event.description, &note);
445
446         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_str() is failed(%x)", error);
447
448         return note;
449 }
450
451 static Evas_Object * __cal_detail_ug_add_note(Evas_Object *box, calendar_record_h record)
452 {
453         CAL_FN_START;
454
455         c_retv_if(!box, NULL);
456         c_retv_if(!record, NULL);
457
458         char *note = __cal_detail_get_note_text(record);
459
460         if (!CAL_STRLEN(note))
461                 return NULL;
462
463         Evas_Object *item = cal_dialogue_append_item(box, "1icon", NULL, NULL);
464
465         Evas_Object *edit_field = cal_util_add_edit_field(item, NULL, EINA_FALSE, EINA_FALSE);
466         if (!edit_field) {
467                 ERR("cal_util_add_edit_field() is failed.");
468
469                 evas_object_del(item);
470
471                 return NULL;
472         }
473
474         Evas_Object *entry = elm_object_part_content_get(edit_field, "elm.swallow.content");
475         if (!edit_field) {
476                 ERR("cal_util_add_edit_field() is failed.");
477
478                 evas_object_del(item);
479
480                 return NULL;
481         }
482
483         evas_object_size_hint_weight_set(entry, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
484         evas_object_size_hint_align_set(entry, EVAS_HINT_FILL, EVAS_HINT_EXPAND);
485         elm_entry_input_panel_enabled_set(entry, EINA_FALSE);
486         elm_entry_autocapital_type_set(entry, EINA_TRUE);
487
488         elm_entry_entry_set(entry, note);
489
490         elm_object_part_content_set(item, "elm.icon", edit_field);
491
492         return item;
493 }
494
495 static inline void __cal_detail_append_multibutton_entry_item(Evas_Object *mbe, calendar_record_h record)
496 {
497         cal_participant *part = calloc(1, sizeof(cal_participant));
498         c_ret_if(!part);
499
500         char *name = NULL;
501
502         calendar_error_e error = CALENDAR_ERROR_NONE;
503         error = calendar_record_get_str(record, _calendar_attendee.name, &name);
504         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_str() is failed(%x)", error);
505         if (!CAL_STRLEN(name)) {
506                 ERR("Attendee name is NULL!!");
507                 name = S_("IDS_COM_BODY_UNKNOWN");
508         }
509
510         part->name = CAL_STRDUP(name);
511
512         char *email = NULL;
513         error = calendar_record_get_str(record, _calendar_attendee.email, &email);
514         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_str() is failed(%x)", error);
515
516         if (CAL_STRLEN(email))
517                 part->email = strdup(email);
518         else
519                 ERR("Attendee email is mandatory data!! There is no email for %s.", part->name);
520
521         error = calendar_record_get_int(record, _calendar_attendee.person_id, &part->person_id);
522         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_int() is failed(%x)", error);
523
524         error = calendar_record_get_int(record, _calendar_attendee.status, &part->status);
525         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_int() is failed(%x)", error);
526
527         error = calendar_record_get_int(record, _calendar_attendee.role, &part->role);
528         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_int() is failed(%x)", error);
529
530         error = calendar_record_get_int(record, _calendar_attendee.rsvp, &part->response);
531         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_int() is failed(%x)", error);
532
533         elm_multibuttonentry_item_append(mbe, name, NULL, part);
534 }
535
536 static inline void __cal_detail_set_multibutton_entry_item(Evas_Object *multibutton_entry, calendar_record_h record)
537 {
538         c_ret_if(!multibutton_entry);
539         c_ret_if(!record);
540
541         int attendee_count = 0;
542
543         calendar_error_e error = CALENDAR_ERROR_NONE;
544
545         error = calendar_record_get_child_record_count(record, _calendar_event.calendar_attendee, (unsigned int *)&attendee_count);
546         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_child_record_count() is failed(%x)", error);
547
548         int i = 0;
549
550         for (i = 0; i < attendee_count; i++) {
551                 calendar_record_h calendar_attendee = NULL;
552
553                 error = calendar_record_get_child_record_at_p(record, _calendar_event.calendar_attendee, i, &calendar_attendee);
554                 if (error != CALENDAR_ERROR_NONE) {
555                         ERR("calendar_record_get_child_record_at_p() is failed(%x)", error);
556                         continue;
557                 }
558
559                 __cal_detail_append_multibutton_entry_item(multibutton_entry, calendar_attendee);
560         }
561 }
562
563 static void __cal_detail_popup_hide_callback(void *data, Evas_Object *obj, void *ei)
564 {
565         evas_object_del(obj);
566 }
567
568 static void __cal_detail_show_ctx_popup(cal_detail_ug_data *data, cal_participant *part)
569 {
570         c_ret_if(!data);
571         c_ret_if(!part);
572
573         Evas_Object *ctx_popup = elm_ctxpopup_add(data->naviframe);
574         c_ret_if(!ctx_popup);
575
576         int person_id = part->person_id;
577         char *name = part->name;
578
579         if (0 < person_id) {
580                 cal_detail_common_get_person_default_number(person_id, &part->number);
581
582                 char *number = part->number;
583
584                 evas_object_smart_callback_add(ctx_popup, "dismissed", __cal_detail_popup_hide_callback, data);
585
586                 if (CAL_STRLEN(number)) {
587                         elm_ctxpopup_item_append(ctx_popup, S_("IDS_COM_BODY_VOICE_CALL"), NULL, cal_detail_common_ctx_callback_for_voice_call, number);
588
589                         elm_ctxpopup_item_append(ctx_popup, S_("IDS_COM_BODY_MESSAGE"), NULL, cal_detail_common_ctx_callback_for_message, number);
590                 }
591
592                 if (CAL_STRLEN(part->email)) {
593                         elm_ctxpopup_item_append(ctx_popup, S_("IDS_COM_BODY_EMAIL"), NULL, cal_detail_common_ctx_callback_for_email, part->email);
594                 }
595         }
596         else {
597                 evas_object_smart_callback_add(ctx_popup, "dismissed", __cal_detail_popup_hide_callback, NULL);
598
599                 elm_ctxpopup_item_append(ctx_popup, S_("IDS_COM_BODY_EMAIL"), NULL, cal_detail_common_ctx_callback_for_email, name);
600                 elm_ctxpopup_item_append(ctx_popup, S_("IDS_COM_OPT_ADD_TO_CONTACTS"), NULL, cal_detail_common_ctx_callback_for_contact, name);
601         }
602
603         evas_object_data_set(ctx_popup, "record", data->record);
604         evas_object_move(ctx_popup, data->mouse_down_x, data->mouse_down_y);
605         evas_object_show(ctx_popup);
606 }
607
608 static void __cal_detail_multibuttonentry_item_selected_callback(void *user_data, Evas_Object *obj, void *ei)
609 {
610         c_ret_if(!ei);
611         c_ret_if(!user_data);
612
613         Elm_Object_Item *it = ei;
614         cal_detail_ug_data *data = user_data;
615
616         cal_participant *part = elm_object_item_data_get(it);
617         c_ret_if(!part);
618
619         __cal_detail_show_ctx_popup(data, part);
620 }
621
622 static void __cal_detail_multibuttonentry_item_deleted_callback(void *data, Evas_Object *obj, void *ei)
623 {
624         c_ret_if(!ei);
625
626         Elm_Object_Item *item = ei;
627
628         cal_participant *part = elm_object_item_data_get(item);
629         c_ret_if(!part);
630
631         CAL_FREE(part->name);
632
633         free(part);
634 }
635
636 static void __cal_detail_multibuttonentry_unfocused_callback(void *data, Evas_Object *obj, void *ei)
637 {
638         c_ret_if(!obj);
639
640         elm_multibuttonentry_item_selected_set(elm_multibuttonentry_selected_item_get(obj), EINA_FALSE);
641 }
642
643 static Eina_Bool __cal_detail_mouse_down_callback(void *user_data, int type, void *event)
644 {
645         c_retv_if(!user_data, ECORE_CALLBACK_RENEW);
646         c_retv_if(!event, ECORE_CALLBACK_RENEW);
647
648         cal_detail_ug_data *data = user_data;
649
650         Ecore_Event_Mouse_Button *ev= event;
651
652
653         data->mouse_down_x = ev->x;
654         data->mouse_down_y = ev->y;
655
656         return ECORE_CALLBACK_RENEW;
657 }
658
659 static Evas_Object * __cal_detail_add_multibutton_entry(Evas_Object *item, cal_detail_ug_data *data)
660 {
661         c_retv_if(!item, NULL);
662         c_retv_if(!data, NULL);
663
664         Evas_Object *multibutton_entry = elm_multibuttonentry_add(item);
665         c_retv_if(!multibutton_entry, NULL);
666
667         evas_object_size_hint_weight_set(multibutton_entry, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
668         evas_object_size_hint_align_set(multibutton_entry, EVAS_HINT_FILL, EVAS_HINT_FILL);
669
670         evas_object_smart_callback_add(multibutton_entry, "item,selected", __cal_detail_multibuttonentry_item_selected_callback, data);
671         evas_object_smart_callback_add(multibutton_entry, "item,deleted", __cal_detail_multibuttonentry_item_deleted_callback, NULL);
672         evas_object_smart_callback_add(multibutton_entry, "unfocused", __cal_detail_multibuttonentry_unfocused_callback, NULL);
673
674         Evas_Object *entry = elm_multibuttonentry_entry_get(multibutton_entry);
675         if (entry)
676                 elm_entry_editable_set(entry, EINA_FALSE);
677
678         evas_object_show(multibutton_entry);
679
680         if (!data->mouse_down_event_handler)
681                 data->mouse_down_event_handler = ecore_event_handler_add(ECORE_EVENT_MOUSE_BUTTON_DOWN, __cal_detail_mouse_down_callback, data);
682
683         return multibutton_entry;
684 }
685
686 static void __cal_detail_attendee_status_button_callback(void *user_data, Evas_Object *obj, void *event_info)
687 {
688         CAL_FN_START;
689
690         c_ret_if(!user_data);
691
692         cal_detail_ug_data *data = user_data;
693
694         cal_detail_attendee_create_view(data->naviframe, data->record);
695 }
696
697 static Evas_Object * __cal_detail_ug_add_attendee(Evas_Object *box, calendar_record_h record, cal_detail_ug_data *data)
698 {
699         CAL_FN_START;
700
701         c_retv_if(!box, NULL);
702         c_retv_if(!record, NULL);
703         c_retv_if(!data, NULL);
704
705         int attendee_count = 0;
706
707         calendar_error_e error = CALENDAR_ERROR_NONE;
708
709         error = calendar_record_get_child_record_count(record, _calendar_event.calendar_attendee, (unsigned int *)&attendee_count);
710         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_child_record_count() is failed(%x)", error);
711
712         if (!attendee_count)
713                 return NULL;
714
715         Evas_Object *item = cal_dialogue_append_item(box, "1icon", NULL, NULL);
716
717         Evas_Object *multibutton_entry = __cal_detail_add_multibutton_entry(item, data);
718
719         __cal_detail_set_multibutton_entry_item(multibutton_entry, record);
720
721         elm_object_part_content_set(item, "elm.icon", multibutton_entry);
722
723         Evas_Object *attendee_status_button = elm_button_add(item);
724         c_retv_if(!attendee_status_button, item);
725
726         cal_util_set_controlbar_button(attendee_status_button, NULL, "reveal/extended", __cal_detail_attendee_status_button_callback, data);
727
728         elm_object_part_content_set(item, "elm.icon.2", attendee_status_button);
729
730         return item;
731 }
732
733 static void __cal_detail_get_save_to_text(int calendar_book_id, char *buf, int sz)
734 {
735         char *str = NULL;
736         calendar_error_e error = CALENDAR_ERROR_NONE;
737         calendar_record_h calendar = NULL;
738
739         error = calendar_db_get_record(_calendar_book._uri, calendar_book_id, &calendar);
740         c_ret_if(error != CALENDAR_ERROR_NONE);
741
742         if (calendar_book_id == DEFAULT_EVENT_CALENDAR_BOOK_ID)
743                 str =  C_("IDS_CLD_BODY_MY_CALENDAR");
744         else if (calendar_book_id == DEFAULT_TODO_CALENDAR_BOOK_ID)
745                 str =  C_("IDS_TASK_BODY_MY_TASK");
746         else {
747                 error = calendar_record_get_str_p(calendar, _calendar_book.name, &str);
748                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_str() is failed(%x)", error);
749         }
750
751         snprintf(buf, sz, "%s", str);
752
753         error = calendar_record_destroy(calendar, true);
754         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_destroy() is failed(%x)", error);
755 }
756
757 static void __cal_detail_ug_update_saveto_language(Evas_Object *item, calendar_record_h record)
758 {
759         c_ret_if(!item);
760
761         elm_object_part_text_set(item, "elm.text.1", C_("IDS_ST_BODY_SAVE_TO"));
762
763         char calendar_name[128] = {0};
764
765         __cal_detail_get_save_to_text(_calendar_get_calendar_index(record), calendar_name, sizeof(calendar_name));
766
767         elm_object_part_text_set(item, "elm.text.2", calendar_name);
768 }
769
770 static Evas_Object * __cal_detail_ug_add_save_to(Evas_Object *box, calendar_record_h record)
771 {
772         CAL_FN_START;
773
774         c_retv_if(!box, NULL);
775         c_retv_if(!record, NULL);
776
777         Evas_Object *item = cal_dialogue_append_item(box, "2text", NULL, NULL);
778
779         __cal_detail_ug_update_saveto_language(item, record);
780
781         return item;
782 }
783
784 static void __cal_detail_ug_add_dialogue_item(cal_detail_ug_data *data)
785 {
786         c_ret_if(!data);
787
788         Evas_Object *dialogue = data->dialogue;
789
790         calendar_record_h record = data->record;
791
792         calendar_record_h instance = data->instance;
793
794         Evas_Object *box = cal_dialogue_get_box(dialogue);
795
796         data->title = __cal_detail_ug_add_title(box, record);
797
798         if (!instance)
799                 instance = record;
800
801         if (!_calendar_is_task_record(instance)) {
802                 data->from = __cal_detail_ug_add_start_time(box, instance);
803
804                 data->to = __cal_detail_ug_add_end_time(box, instance);
805         } else
806                 data->due = __cal_detail_ug_add_due_time(box, instance);
807
808         data->reminder = __cal_detail_ug_add_reminder(box, record);
809
810         if (!_calendar_is_task_record(record))
811                 data->recurrence = __cal_detail_ug_add_recurrence(box, record);
812
813         data->note = __cal_detail_ug_add_note(box, record);
814
815         if (_calendar_is_eas_record(record)) {
816                 if (!_calendar_is_task_record(record))
817                         data->attendee = __cal_detail_ug_add_attendee(box, record, data);
818
819         }
820
821         data->save_to = __cal_detail_ug_add_save_to(box, record);
822 }
823
824 static Evas_Object * __cal_detail_ug_create_dialogue(cal_detail_ug_data *data)
825 {
826         CAL_FN_START;
827
828         c_retv_if(!data, NULL);
829
830         Evas_Object *parent = data->naviframe;
831
832         Evas_Object *dialogue = cal_dialogue_create(parent);
833
834         data->dialogue = dialogue;
835
836         __cal_detail_ug_add_dialogue_item(data);
837
838         return dialogue;
839 }
840
841 static void inline __cal_detail_ug_clone_record(const calendar_record_h source_record, calendar_record_h *target_record)
842 {
843         CAL_FN_START;
844
845         c_ret_if(!source_record);
846         c_ret_if(!target_record);
847
848         calendar_error_e error = CALENDAR_ERROR_NONE;
849
850         error = calendar_record_clone(source_record, target_record);
851         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_clone() is failed(%x)", error);
852 }
853
854 static inline void __cal_detail_ug_update_record(calendar_record_h *record)
855 {
856         calendar_error_e error = CALENDAR_ERROR_NONE;
857
858         int record_index = _calendar_get_record_index(*record);
859         c_ret_if(record_index < 0);
860
861         error = calendar_record_destroy(*record, true);
862         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_destroy() is failed(%x)", error);
863
864         *record = _calendar_get_record_with_index(record_index);
865 }
866
867 static void __cal_detail_update_view(const char* view_uri, void* user_data)
868 {
869         CAL_FN_START;
870
871         c_ret_if(!user_data);
872
873         cal_detail_ug_data *data = user_data;
874
875         __cal_detail_ug_update_record(&data->record);
876
877         Evas_Object *dialogue = data->dialogue;
878
879         cal_dialogue_delete_all_items(dialogue);
880
881         __cal_detail_ug_add_dialogue_item(data);
882 }
883
884 static void __cal_detail_remove_db_changed_callback(cal_detail_ug_data *data)
885 {
886         CAL_FN_START;
887
888         c_ret_if(!data);
889
890         calendar_error_e error = CALENDAR_ERROR_NONE;
891
892         error = calendar_db_remove_changed_cb(_calendar_event._uri, __cal_detail_update_view, data);
893         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_db_remove_changed_cb() is failed(%x)", error);
894
895         error = calendar_db_remove_changed_cb(_calendar_todo._uri, __cal_detail_update_view, data);
896         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_db_remove_changed_cb() is failed(%x)", error);
897 }
898
899 static void __cal_detail_add_db_changed_callback(cal_detail_ug_data *data)
900 {
901         CAL_FN_START;
902
903         c_ret_if(!data);
904
905         calendar_error_e error = CALENDAR_ERROR_NONE;
906
907         error = calendar_db_add_changed_cb(_calendar_event._uri, __cal_detail_update_view, data);
908         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_db_remove_changed_cb() is failed(%x)", error);
909
910         error = calendar_db_add_changed_cb(_calendar_todo._uri, __cal_detail_update_view, data);
911         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_db_remove_changed_cb() is failed(%x)", error);
912 }
913
914 static void __cal_detail_ug_edit_ug_result_callback(ui_gadget_h ug, service_h result, void *priv)
915 {
916         CAL_FN_START;
917
918         c_ret_if(!result);
919         c_ret_if(!priv);
920
921         int record_index = 0;
922         long long int base_time = 0;
923
924         char *value = NULL;
925
926         int ret = service_get_extra_data(result, "record_index", &value);
927         c_warn_if(ret != SERVICE_ERROR_NONE, "service_get_extra_data() is failed(%x)", ret);
928
929         if (CAL_STRLEN(value))
930                 record_index = atoi(value);
931
932         CAL_FREE(value);
933
934         ret = service_get_extra_data(result, "base_time", &value);
935         c_warn_if(ret != SERVICE_ERROR_NONE, "service_get_extra_data() is failed(%x)", ret);
936
937         base_time = strtoll(value, NULL, 10);
938
939         CAL_FREE(value);
940
941         cal_detail_ug_data *data = priv;
942
943         calendar_error_e error = calendar_record_destroy(data->record, true);
944         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_destroy() is failed(%x)", error);
945
946         data->record = _calendar_get_record_with_index(record_index);
947
948         if (data->instance) {
949                 error = calendar_record_destroy(data->instance, true);
950                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_destroy() is failed(%x)", error);
951
952                 data->instance = NULL;
953         }
954
955         if(!_calendar_is_task_record(data->record))
956         {
957                 struct tm tm_start = {0};
958
959                 cal_util_convert_lli_to_tm(NULL, base_time, &tm_start);
960
961                 struct tm tm_end = tm_start;
962
963                 cal_util_update_tm_day(&tm_end, 1);
964
965                 data->instance = _calendar_get_instance_from_record(data->record, &tm_start, &tm_end);
966                 c_ret_if(!data->instance);
967         }
968
969         Evas_Object *dialogue = data->dialogue;
970
971         cal_dialogue_delete_all_items(dialogue);
972
973         __cal_detail_ug_add_dialogue_item(data);
974
975         __cal_detail_add_db_changed_callback(data);
976 }
977
978 static void __cal_detail_ug_edit_ug_destroy_callback(ui_gadget_h ug, void *priv)
979 {
980         CAL_FN_START;
981
982         c_ret_if(!priv);
983
984         cal_detail_ug_data *data = priv;
985
986         Evas_Object *edit_button = elm_object_item_part_content_get(data->navi_item, "toolbar_button1");
987         if(elm_object_disabled_get(edit_button))
988                 elm_object_disabled_set(edit_button, EINA_FALSE);
989
990         ug_destroy(ug);
991 }
992
993 static void __cal_detail_launch_edit_ug(cal_detail_ug_data *data, calendar_record_h record)
994 {
995         c_ret_if(!data);
996
997         calendar_record_h cloned_record = NULL;
998
999         __cal_detail_ug_clone_record(record, &cloned_record);
1000
1001         char record_address[16] = {0};
1002
1003         snprintf(record_address, sizeof(record_address), "%p", (void *)cloned_record);
1004
1005         struct ug_cbs cbs = {0};
1006
1007         cbs.result_cb = __cal_detail_ug_edit_ug_result_callback;
1008         cbs.destroy_cb = __cal_detail_ug_edit_ug_destroy_callback;
1009         cbs.priv = data;
1010
1011         cal_launch_ug_with_var(data->ug, "calendar-edit-efl", &cbs, "record", record_address, NULL);
1012
1013         __cal_detail_remove_db_changed_callback(data);
1014 }
1015
1016 static void __cal_detail_event_edit_popup_response_callback(void *user_data, Evas_Object *obj, void *event_info)
1017 {
1018         c_ret_if(!user_data);
1019         c_ret_if(!obj);
1020
1021         Evas_Object *popup = user_data;
1022         Evas_Object *button = obj;
1023
1024         cal_detail_ug_data *data = evas_object_data_get(popup, "data");
1025
1026         if (!strcmp(elm_object_text_get(button), C_("IDS_CLD_BODY_ONLY_THIS_EVENT")))
1027                 __cal_detail_launch_edit_ug(data, data->instance);
1028         else if (!strcmp(elm_object_text_get(button), C_("IDS_CLD_POP_ALL_REPETITIVE_EVENTS"))
1029                 || !strcmp(elm_object_text_get(button), S_("IDS_COM_SK_OK")))
1030                 __cal_detail_launch_edit_ug(data, data->record);
1031
1032         evas_object_del(popup);
1033 }
1034
1035 static void __cal_detail_edit_button_callback(void *user_data, Evas_Object *obj, void *ei)
1036 {
1037         CAL_FN_START;
1038
1039         c_ret_if(!user_data);
1040
1041         cal_detail_ug_data *data = user_data;
1042
1043         calendar_record_h record = data->record;
1044
1045         if (_calendar_is_recurrent_record(record)) {
1046                 cal_util_add_popup(data->window, "verticalbuttonstyle", NULL, C_("IDS_CLD_HEADER_EDIT_EVENT"), __cal_detail_event_edit_popup_response_callback, data,
1047                         C_("IDS_CLD_BODY_ONLY_THIS_EVENT"), C_("IDS_CLD_POP_ALL_REPETITIVE_EVENTS"),
1048                         S_("IDS_COM_SK_CANCEL"), NULL);
1049         } else {
1050                 __cal_detail_launch_edit_ug(data, record);
1051                 elm_object_disabled_set(obj, EINA_TRUE);
1052         }
1053 }
1054
1055 static void __cal_detail_save_button_callback(void *user_data, Evas_Object *obj, void *ei)
1056 {
1057         CAL_FN_START;
1058
1059         c_ret_if(!user_data);
1060
1061         cal_detail_ug_data *data = user_data;
1062
1063         calendar_record_h record = data->record;
1064
1065         calendar_error_e error = CALENDAR_ERROR_NONE;
1066
1067         error = calendar_db_insert_record(record, NULL);
1068         if (error != CALENDAR_ERROR_NONE) {
1069                 ERR("calendar_db_insert_record() is failed(%x)", error);
1070         }
1071 }
1072
1073 static void __cal_detail_delete_event(cal_detail_ug_data *data, Eina_Bool is_delete_all)
1074 {
1075         CAL_FN_START;
1076
1077         c_ret_if(!data);
1078
1079         calendar_record_h record = data->record;
1080
1081         if (!is_delete_all)
1082                 _calendar_delete_recurrence_instance(data->instance);
1083         else
1084                 _calendar_delete_record(record);
1085
1086         ug_destroy_me(data->ug);
1087 }
1088
1089 static void __cal_detail_delete_popup_response_event_callback(void *user_data, Evas_Object *obj, void *event_info)
1090 {
1091         c_ret_if(!user_data);
1092         c_ret_if(!obj);
1093
1094         Evas_Object *popup = user_data;
1095         Evas_Object *button = obj;
1096
1097         cal_detail_ug_data *data = evas_object_data_get(popup, "data");
1098         c_ret_if(!data);
1099
1100         if (NULL != strstr(elm_object_text_get(button), S_("IDS_COM_BODY_DELETE")))
1101                 __cal_detail_delete_event(data, EINA_TRUE);
1102         else if (!strcmp(elm_object_text_get(button), C_("IDS_CLD_BODY_ONLY_THIS_EVENT")))
1103                 __cal_detail_delete_event(data, EINA_FALSE);
1104         else if (!strcmp(elm_object_text_get(button), C_("IDS_CLD_POP_ALL_REPETITIVE_EVENTS")))
1105                 __cal_detail_delete_event(data, EINA_TRUE);
1106
1107         evas_object_del(popup);
1108 }
1109
1110 static inline void __cal_detail_show_task_delete_popup(cal_detail_ug_data *data, calendar_record_h record)
1111 {
1112         CAL_FN_START;
1113
1114         cal_util_add_popup(data->window, NULL, NULL, S_("IDS_COM_POP_DELETE_Q"), __cal_detail_delete_popup_response_event_callback, data,
1115                                 S_("IDS_COM_BODY_DELETE"), S_("IDS_COM_SK_CANCEL"), NULL);
1116 }
1117
1118 static inline void __cal_detail_show_event_delete_popup(cal_detail_ug_data *data, calendar_record_h record)
1119 {
1120         CAL_FN_START;
1121
1122         if (_calendar_is_recurrent_record(record)) {
1123                 cal_util_add_popup(data->window, "verticalbuttonstyle", NULL, C_("IDS_CLD_OPT_DELETE_EVENT"), __cal_detail_delete_popup_response_event_callback, data,
1124                         C_("IDS_CLD_BODY_ONLY_THIS_EVENT"), C_("IDS_CLD_POP_ALL_REPETITIVE_EVENTS"),S_("IDS_COM_SK_CANCEL"), NULL);
1125         }
1126         else {
1127                 cal_util_add_popup(data->window, NULL, NULL, S_("IDS_COM_POP_DELETE_Q"), __cal_detail_delete_popup_response_event_callback, data,
1128                         S_("IDS_COM_BODY_DELETE"), S_("IDS_COM_SK_CANCEL"), NULL);
1129         }
1130 }
1131
1132 static void __cal_detail_delete_button_callback(void *user_data, Evas_Object *obj, void *ei)
1133 {
1134         CAL_FN_START;
1135
1136         c_ret_if(!user_data);
1137         c_ret_if(!obj);
1138
1139         cal_detail_ug_data *data = user_data;
1140
1141         Evas_Object *ctx_popup = obj;
1142
1143         calendar_record_h record = data->record;
1144         c_ret_if(!record);
1145
1146         evas_object_del(ctx_popup);
1147
1148         if (_calendar_is_task_record(record))
1149                 __cal_detail_show_task_delete_popup(data, record);
1150         else
1151                 __cal_detail_show_event_delete_popup(data, record);
1152 }
1153
1154 static void  __cal_detail_share_popup_hide_callback(void *data, Evas_Object *obj, void *event_info)
1155 {
1156         c_ret_if(!data);
1157
1158         Evas_Object *popup = data;
1159
1160         evas_object_del(popup);
1161 }
1162
1163 static void __cal_detail_via_message_callback(void *user_data, Evas_Object *obj, void *ei)
1164 {
1165         c_ret_if(!user_data);
1166         c_ret_if(!obj);
1167
1168         cal_detail_ug_data *data = user_data;
1169
1170         _calendar_export_record_to_vcs(data->record, CAL_MESSAGE_VCS);
1171
1172         cal_launch_ug_with_var(data->ug, CAL_MESSAGE_COMPOSER_UG, NULL, "ATTACHFILE", CAL_MESSAGE_VCS, NULL);
1173
1174         evas_object_del(obj);
1175 }
1176
1177 static void __cal_detail_via_email_callback(void *user_data, Evas_Object *obj, void *ei)
1178 {
1179         c_ret_if(!user_data);
1180         c_ret_if(!obj);
1181
1182         cal_detail_ug_data *data = user_data;
1183
1184         _calendar_export_record_to_vcs(data->record, CAL_EMAIL_VCS);
1185
1186         cal_launch_ug_with_var(data->ug, CAL_EMAIL_COMPOSER_UG, (struct ug_cbs *)data, "RUN_TYPE", "5", "ATTACHMENT", CAL_EMAIL_VCS, NULL);
1187
1188         evas_object_del(obj);
1189 }
1190
1191 static void __cal_detail_via_bluetooth_callback(void *user_data, Evas_Object *obj, void *ei)
1192 {
1193         c_ret_if(!user_data);
1194         c_ret_if(!obj);
1195
1196         cal_detail_ug_data *data = user_data;
1197
1198         _calendar_export_record_to_vcs(data->record, CAL_BLUETOOTH_VCS);
1199
1200         cal_launch_ug_with_var(data->ug, CAL_BLUETOOTH_UG, NULL, "launch-type", "send", "filecount", "1", "files", CAL_BLUETOOTH_VCS, NULL);
1201
1202         evas_object_del(obj);
1203 }
1204
1205 static void __cal_detail_share_button_callback(void *user_data, Evas_Object *obj, void *ei)
1206 {
1207         c_ret_if(!user_data);
1208         c_ret_if(!obj);
1209
1210         cal_detail_ug_data *data = user_data;
1211
1212         Evas_Object *ctx_popup = obj;
1213
1214         evas_object_del(ctx_popup);
1215
1216         Evas_Object *popup = cal_util_add_popup(data->window, NULL, S_("IDS_COM_BUTTON_SHARE"), NULL, __cal_detail_share_popup_hide_callback, data, S_("IDS_COM_BODY_CLOSE"), NULL);
1217         c_ret_if(!popup);
1218
1219         evas_object_data_set(popup, "priv", data);
1220
1221         Elm_Object_Item *popup_item = elm_popup_item_append(popup, S_("IDS_COM_BODY_MESSAGE"), NULL, __cal_detail_via_message_callback, data);
1222         c_ret_if(!popup_item);
1223
1224         popup_item = elm_popup_item_append(popup, S_("IDS_COM_BODY_EMAIL"), NULL, __cal_detail_via_email_callback, data);
1225         c_ret_if(!popup_item);
1226
1227         popup_item = elm_popup_item_append(popup, S_("IDS_COM_BODY_BLUETOOTH"), NULL, __cal_detail_via_bluetooth_callback, data);
1228         c_ret_if(!popup_item);
1229 }
1230
1231 static void __cal_detail_more_button_callback(void *user_data, Evas_Object *obj, void *event_info)
1232 {
1233         c_ret_if(!user_data);
1234         c_ret_if(!obj);
1235
1236         cal_detail_ug_data *data = user_data;
1237
1238         Evas_Object *ctx_popup = elm_ctxpopup_add(data->naviframe);
1239         c_ret_if(!ctx_popup);
1240
1241         elm_ctxpopup_direction_priority_set(ctx_popup, ELM_CTXPOPUP_DIRECTION_UP, ELM_CTXPOPUP_DIRECTION_UP, ELM_CTXPOPUP_DIRECTION_UP, ELM_CTXPOPUP_DIRECTION_UP);
1242
1243         Elm_Object_Item *item = NULL;
1244
1245         item = elm_ctxpopup_item_append(ctx_popup, S_("IDS_COM_BODY_DELETE"), NULL, __cal_detail_delete_button_callback, data);
1246         c_ret_if(!item);
1247
1248         item = elm_ctxpopup_item_append(ctx_popup, S_("IDS_COM_BUTTON_SHARE"), NULL, __cal_detail_share_button_callback, data);
1249         c_ret_if(!item);
1250
1251         Evas_Coord_Rectangle r = {0};
1252
1253         cal_util_get_geometry(&r, obj);
1254
1255         evas_object_move(ctx_popup, r.x + r.w/2, r.y + r.h/2);
1256
1257         evas_object_show(ctx_popup);
1258 }
1259
1260 static Evas_Object *__cal_detail_ug_create_layout(Evas_Object *parent, cal_detail_ug_data *data)
1261 {
1262         CAL_FN_START;
1263
1264         c_retv_if(!parent, NULL);
1265         c_retv_if(!data, NULL);
1266
1267         Evas_Object *base_layout = cal_util_add_layout(parent, NULL);
1268         c_retv_if(!base_layout, NULL);
1269
1270         Evas_Object *naviframe = elm_naviframe_add(base_layout);
1271         if (!naviframe) {
1272                 ERR("elm_naviframe_add() is failed");
1273
1274                 evas_object_del(base_layout);
1275
1276                 return NULL;
1277         }
1278
1279         evas_object_show(naviframe);
1280
1281         Evas_Object *bg = cal_util_add_bg(base_layout, EINA_FALSE);
1282         if (!bg) {
1283                 ERR("cal_util_add_bg() is failed");
1284
1285                 evas_object_del(base_layout);
1286
1287                 return NULL;
1288         }
1289
1290         data->base_layout = base_layout;
1291         data->naviframe = naviframe;
1292
1293         cal_util_initialize_timezone();
1294
1295         Evas_Object *dialogue = __cal_detail_ug_create_dialogue(data);
1296
1297         Evas_Object *back_button = __cal_detail_ug_add_back_button(data);
1298
1299         Elm_Object_Item *item = NULL;
1300
1301         if (_calendar_is_task_record(data->record))
1302                 item = elm_naviframe_item_push(naviframe, C_("IDS_CLD_BODY_TASK_DETAILS"), back_button, NULL, dialogue, NULL);
1303         else
1304                 item = elm_naviframe_item_push(naviframe, C_("IDS_CLD_BODY_EVENT_DETAILS"), back_button, NULL, dialogue, NULL);
1305         c_warn_if(!item, "elm_naviframe_item_push() is failed");
1306         data->navi_item = item;
1307
1308         cal_util_add_toolbar_button(naviframe, "toolbar_button1", S_("IDS_COM_BODY_EDIT"), __cal_detail_edit_button_callback, data);
1309
1310         if (0 < _calendar_get_record_index(data->record)) {
1311
1312                 Evas_Object *more_button = cal_util_add_toolbar_button(naviframe, "toolbar_more_btn", NULL, __cal_detail_more_button_callback, data);
1313                 elm_object_style_set(more_button, "naviframe/more/default");
1314         } else {
1315                 cal_util_add_toolbar_button(naviframe, "toolbar_button1", S_("IDS_COM_OPT_SAVE"), __cal_detail_save_button_callback, data);
1316         }
1317
1318         elm_object_part_content_set(base_layout, "elm.swallow.content", naviframe);
1319         elm_object_part_content_set(base_layout, "elm.swallow.bg", bg);
1320
1321         return base_layout;
1322
1323 }
1324
1325 static calendar_record_h inline __cal_detail_ug_get_origin_record(const calendar_record_h instance)
1326 {
1327         CAL_FN_START;
1328
1329         c_retv_if(!instance, NULL);
1330
1331         int index = _calendar_get_record_index(instance);
1332
1333         return _calendar_get_record_with_index(index);
1334 }
1335
1336 static void *__cal_detail_ug_create_callback(ui_gadget_h ug, enum ug_mode mode, service_h service, void *priv)
1337 {
1338         CAL_FN_START;
1339
1340         c_retv_if(!ug, NULL);
1341         c_retv_if(!service, NULL);
1342         c_retv_if(!priv, NULL);
1343         c_retv_if(mode != UG_MODE_FULLVIEW, NULL);
1344
1345         cal_detail_ug_data *data = priv;
1346
1347         data->ug = ug;
1348
1349         int cid = 0;
1350         char *item_type = NULL;
1351         char *index = NULL;
1352         char *path = NULL;
1353         calendar_error_e error = CALENDAR_ERROR_NONE;
1354
1355         int r = service_get_extra_data(service, "index", &index);
1356         if (r == SERVICE_ERROR_NONE)
1357                 cid = atoi(index);
1358
1359         r = service_get_extra_data(service, CAL_APPCONTROL_ITEM_TYPE, &item_type);
1360         if (r == SERVICE_ERROR_NONE) {
1361
1362                 if (!CAL_STRCMP(item_type, CAL_APPCONTROL_ITEM_TYPE_EVENT)) {
1363
1364                         r = service_get_extra_data(service, CAL_APPCONTROL_EVENT_ID, &index);
1365                         if (r != SERVICE_ERROR_NONE) {
1366                                 ERR("service_get_extra_data(%s) is failed(%x)", CAL_APPCONTROL_EVENT_ID, r);
1367
1368                                 CAL_FREE(index);
1369
1370                                 ug_destroy_me(ug);
1371
1372                                 return NULL;
1373                         }
1374
1375                         cid = atoi(index);
1376                         calendar_record_h record = _calendar_get_record_with_index(cid);
1377                         c_retv_if(!record, NULL);
1378
1379                         calendar_record_destroy(record, true);
1380
1381                 } else if (!CAL_STRCMP(item_type, CAL_APPCONTROL_ITEM_TYPE_TODO)) {
1382
1383                         r = service_get_extra_data(service, CAL_APPCONTROL_TODO_ID, &index);
1384                         if (r != SERVICE_ERROR_NONE) {
1385                                 ERR("service_get_extra_data(%s) is failed(%x)", CAL_APPCONTROL_TODO_ID, r);
1386
1387                                 CAL_FREE(index);
1388
1389                                 ug_destroy_me(ug);
1390
1391                                 return NULL;
1392                         }
1393
1394                         cid = atoi(index);
1395                         calendar_record_h record = _calendar_get_record_with_index(cid);
1396                         c_retv_if(!record, NULL);
1397
1398                         calendar_record_destroy(record, true);
1399
1400                 } else if (!CAL_STRCMP(item_type, CAL_APPCONTROL_ITEM_TYPE_VCS)) {
1401
1402                         r = service_get_extra_data(service, CAL_APPCONTROL_VCS_PATH, &path);
1403                         if (r != SERVICE_ERROR_NONE) {
1404                                 ERR("service_get_extra_data(%s) is failed(%x)", CAL_APPCONTROL_VCS_PATH, r);
1405
1406                                 CAL_FREE(path);
1407
1408                                 ug_destroy_me(ug);
1409
1410                                 return NULL;
1411                         }
1412
1413                         FILE * file = fopen(path, "r");
1414                         if (!file) {
1415                                 ERR("fopen(%s) is failed.", path);
1416
1417                                 ug_destroy_me(ug);
1418
1419                                 return NULL;
1420                         }
1421
1422                         struct stat st;
1423                         r = stat(path, &st);
1424                         c_warn_if(r == -1, "stat(%s) is failed(%d)", path, errno);
1425
1426                         int size = st.st_size;
1427                         char *raw_data = calloc(size + 1, sizeof(char));
1428                         if (!raw_data) {
1429                                 ERR("calloc is failed.");
1430
1431                                 fclose( file );
1432
1433                                 ug_destroy_me(ug);
1434
1435                                 return NULL;
1436                         }
1437
1438                         r = fread(raw_data, 1, size, file);
1439                         fclose( file );
1440                         c_retv_if(r < 0, NULL);
1441
1442                         calendar_list_h list = NULL;
1443
1444                         error = calendar_vcalendar_parse_to_calendar(raw_data, &list);
1445                         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_vcalendar_parse_to_calendar() is failed(%x)", error);
1446
1447                         if (list) {
1448                                 calendar_record_h record = NULL;
1449                                 error = calendar_list_get_current_record_p(list, &record);
1450                                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_list_get_current_record_p() is failed(%x)", error);
1451
1452                                 error = calendar_record_clone(record, &data->record);
1453                                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_clone() is failed(%x)", error);
1454
1455                                 error = calendar_list_destroy(list, true);
1456                         }
1457
1458                         free(raw_data);
1459                 }
1460         }
1461
1462         if (!data->record) {
1463                 if (0 < cid)
1464                         data->record = _calendar_get_record_with_index(cid);
1465                 else {
1466                         char *record_address = NULL;
1467
1468                         r = service_get_extra_data(service, "record", &record_address);
1469                         if (r != SERVICE_ERROR_NONE) {
1470                                 ERR("service_get_extra_data(record) is failed(%x)", r);
1471                         } else {
1472                                 data->record = (calendar_record_h)strtoul(record_address, NULL, 16);
1473                                 c_warn_if(!data->record, "strtol(%s, NULL, 16) is failed", record_address);
1474                         }
1475
1476                         _calendar_record_type record_type = _calendar_get_record_type(data->record);
1477
1478                         if (record_type != _CALENDAR_RECORD_TYPE_EVENT && record_type != _CALENDAR_RECORD_TYPE_TODO) {
1479                                 data->instance = data->record;
1480                                 data->record = __cal_detail_ug_get_origin_record(data->instance);
1481                         }
1482
1483                         CAL_FREE(record_address);
1484                 }
1485
1486         }
1487
1488         CAL_FREE(item_type);
1489
1490         CAL_FREE(index);
1491
1492         CAL_FREE(path);
1493
1494         Evas_Object *parent = ug_get_parent_layout(ug);
1495         c_retv_if(!parent, NULL);
1496
1497         data->window = ug_get_window();
1498         c_retv_if(!data->window, NULL);
1499
1500         elm_theme_extension_add(NULL, "/usr/apps/"CALENDAR_PACKAGE"/res/edje/theme.edj");
1501
1502         Evas_Object *base = __cal_detail_ug_create_layout(parent, data);
1503         c_retv_if(!base, NULL);
1504
1505         __cal_detail_add_db_changed_callback(data);
1506
1507         return base;
1508 }
1509
1510 static void __cal_detail_ug_destroy_callback(ui_gadget_h ug, service_h service, void *priv)
1511 {
1512         CAL_FN_START;
1513
1514         c_ret_if(!priv);
1515
1516         cal_detail_ug_data *data = priv;
1517
1518         __cal_detail_remove_db_changed_callback(data);
1519
1520         if (data->mouse_down_event_handler)
1521                 ecore_event_handler_del(data->mouse_down_event_handler);
1522
1523         cal_util_delete_evas_object(&data->base_layout);
1524 }
1525
1526 static void __cal_detail_ug_key_callback(ui_gadget_h ug, enum ug_key_event evt, service_h data, void *priv)
1527 {
1528         c_ret_if(!ug);
1529
1530         switch (evt) {
1531         case UG_KEY_EVENT_END:
1532                 ug_destroy_me(ug);
1533                 break;
1534         default:
1535                 break;
1536         }
1537 }
1538
1539 static void __cal_detail_ug_update_button_language(Evas_Object *naviframe, calendar_record_h record)
1540 {
1541         c_ret_if(!naviframe);
1542
1543         Elm_Object_Item *item = elm_naviframe_top_item_get(naviframe);
1544         c_ret_if(!item);
1545
1546         Evas_Object *button = elm_object_item_part_content_get(item, "toolbar_button1");
1547         c_ret_if(!button);
1548
1549         elm_object_text_set(button, S_("IDS_COM_BODY_EDIT"));
1550
1551         if (_calendar_get_record_index(record) <= 0)
1552                 elm_object_text_set(button, S_("IDS_COM_OPT_SAVE"));
1553 }
1554
1555 static void __cal_detail_ug_update_language(cal_detail_ug_data *data)
1556 {
1557         c_ret_if(!data);
1558         c_ret_if(!data->record);
1559
1560
1561
1562         calendar_record_h record = data->record;
1563
1564         __cal_detail_ug_update_title_language(data->title, record);
1565
1566         Eina_Bool is_task_record = _calendar_is_task_record(record);
1567
1568         if (!is_task_record) {
1569
1570                 __cal_detail_ug_update_from_language(data->from);
1571
1572                 __cal_detail_ug_update_to_language(data->to);
1573
1574         } else
1575                 __cal_detail_ug_update_due_language(data->due);
1576
1577         __cal_detail_ug_update_reminder_language(data->reminder, record);
1578
1579         if (!is_task_record)
1580                 __cal_detail_ug_update_recurrence_language(data->recurrence, record);
1581
1582         __cal_detail_ug_update_saveto_language(data->save_to, record);
1583
1584 }
1585
1586 static void __cal_detail_ug_update_region(cal_detail_ug_data *data)
1587 {
1588         c_ret_if(!data);
1589         c_ret_if(!data->record);
1590
1591         calendar_record_h record = data->record;
1592
1593         if (!_calendar_is_task_record(data->record)) {
1594
1595                 __cal_detail_ug_update_from_region(data->from, record);
1596
1597                 __cal_detail_ug_update_to_region(data->to, record);
1598
1599         } else
1600                 __cal_detail_ug_update_due_region(data->to, record);
1601 }
1602
1603 static void __cal_detail_ug_event_callback(ui_gadget_h ug, enum ug_event event, service_h service, void *priv)
1604 {
1605         c_ret_if(!ug);
1606         c_ret_if(!priv);
1607
1608         switch (event) {
1609         case UG_EVENT_LANG_CHANGE:
1610                 __cal_detail_ug_update_language((cal_detail_ug_data *)priv);
1611                 break;
1612         case UG_EVENT_REGION_CHANGE:
1613                 __cal_detail_ug_update_region((cal_detail_ug_data *)priv);
1614                 break;
1615
1616         case UG_EVENT_ROTATE_PORTRAIT:
1617         case UG_EVENT_ROTATE_PORTRAIT_UPSIDEDOWN:
1618                 break;
1619         case UG_EVENT_ROTATE_LANDSCAPE:
1620         case UG_EVENT_ROTATE_LANDSCAPE_UPSIDEDOWN:
1621                 break;
1622
1623         default:
1624                 break;
1625         }
1626 }
1627
1628 API int UG_MODULE_INIT(struct ug_module_ops *ops)
1629 {
1630         CAL_FN_START;
1631
1632         c_retv_if(!ops, -1);
1633
1634         cal_detail_ug_data *data = calloc(1, sizeof(cal_detail_ug_data));
1635
1636         ops->create = __cal_detail_ug_create_callback;
1637         ops->destroy = __cal_detail_ug_destroy_callback;
1638         ops->key_event = __cal_detail_ug_key_callback;
1639         ops->event = __cal_detail_ug_event_callback;
1640         ops->priv = data;
1641         ops->opt = UG_OPT_INDICATOR_PORTRAIT_ONLY;
1642
1643         const char *path = bindtextdomain(CALENDAR, LOCALEDIR);
1644         c_warn_if(!path, "bindtextdomain(%s, %s) is failed.", CALENDAR, LOCALEDIR);
1645
1646         __cal_detail_ug_initialize(data);
1647
1648         return 0;
1649 }
1650
1651 API void UG_MODULE_EXIT(struct ug_module_ops *ops)
1652 {
1653         CAL_FN_START;
1654
1655         c_ret_if(!ops);
1656
1657         __cal_detail_ug_finish();
1658
1659         cal_detail_ug_data *data = ops->priv;
1660
1661         free(data);
1662 }