merge with master
[apps/core/preloaded/calendar.git] / ug / edit / edit-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 <vconf.h>
20 #include <libintl.h>
21
22 #include "view.h"
23 #include "dialogue.h"
24 #include "edit-alarm.h"
25 #include "edit-repeat.h"
26 #include "reminder.h"
27 #include "acct-svc.h"
28 #include "gi-timepicker.h"
29 #include "save-cancel.h"
30 #include "external-ug.h"
31 #include "cld-images.h"
32 #include "dialogue.h"
33
34 #define INVALID_TIMEZONE_VALUE (100)
35 #define CAL_EDIT_UG_MAX_ATTACHMENT_COUNT 6
36
37 typedef struct {
38
39         ui_gadget_h ug;
40         cal_appcontrol_item_type item_type;
41
42         struct tm base_time;
43         Evas_Object *base_layout;
44         Evas_Object *naviframe;
45         Evas_Object *window;
46
47         Evas_Object *parent;
48         Evas_Object *popup;
49         Ecore_Idler *idler;
50
51         calendar_record_h event;
52         calendar_record_h todo;
53         calendar_record_h record;
54         calendar_record_h instance;
55
56         Eina_Bool is_edit_mode;
57
58         Evas_Object *dialogue;
59
60         Evas_Object *title_entry;
61         Evas_Object *title;
62         Evas_Object *start_date;
63         Evas_Object *start_date_title;
64         Evas_Object *end_date;
65         Evas_Object *end_date_title;
66         Evas_Object *allday;
67         Evas_Object *timezone;
68         Evas_Object *location;
69         Evas_Object *location_entry;
70
71         Evas_Object *note;
72         Evas_Object *note_entry;
73         Eina_List *alarm_item_list;
74         Evas_Object *alarm_plus_button;
75
76         Evas_Object *repeat;
77         Evas_Object *repeat_layout;
78
79         Evas_Object *saveto;
80
81         Evas_Object *multibuttonentry; // entry participants
82
83         Evas_Object *due_date;
84         Evas_Object *due_date_title;
85         Evas_Object *no_due_date;
86         Evas_Object *priority;
87
88         cal_save_cancel_h save_cancel;
89         Evas_Object *tabbar;
90
91         int repeat_freq; // repeat_freq off, daily, ...
92         calendar_range_type_e repeat_range;
93         int repeat_count;
94         struct tm repeat_until;
95
96         GList  *alarm_list;
97
98         int account_id;
99
100         ui_gadget_h ug_worldclock;
101
102         Eina_Bool is_selected_timezone;
103         char * timezone_path;           /* Asia/Seoul */
104         char * timezone_city;           /* IDS_WCL_BODY_CITYNAME_SEOUL */
105         char * timezone_offset; /* GMT+9 */
106
107         Eina_List *calendar_book_list; //For save to
108
109         Elm_Object_Item *event_item;
110         Elm_Object_Item *todo_item;
111
112         char *title_str;
113         char *address;
114         double latitude;
115         double longitude;
116         char *note_str;
117
118         struct tm stm; // start
119         struct tm etm; // end
120
121         Eina_Bool is_no_due_date;
122
123 }cal_edit_ug_data;
124
125 enum __cal_edit_wday {
126         CAL_EDIT_WDAY_SUNDAY,
127         CAL_EDIT_WDAY_MONDAY,
128         CAL_EDIT_WDAY_TUESDAY,
129         CAL_EDIT_WDAY_WEDNESDAY,
130         CAL_EDIT_WDAY_THURSDAY,
131         CAL_EDIT_WDAY_FRIDAY,
132         CAL_EDIT_WDAY_SATURDAY,
133         CAL_EDIT_WDAY_NODAY,
134 };
135
136 static int __cal_edit_ug_initialize(cal_edit_ug_data *data)
137 {
138         CAL_FN_START;
139
140         c_retv_if(!data, -1);
141
142         calendar_error_e error = CALENDAR_ERROR_NONE;
143
144         error = calendar_connect();
145         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_connect() is failed(%x)", error);
146
147         _calendar_init_hash();
148
149         const char *path = bindtextdomain(CALENDAR, LOCALEDIR);
150         c_warn_if(!path, "bindtextdomain(%s, %s) is failed.", CALENDAR, LOCALEDIR);
151
152         cal_util_connect_pattern_generator();
153
154         return 0;
155 }
156
157 static int __cal_edit_ug_finish(cal_edit_ug_data *data)
158 {
159         CAL_FN_START;
160
161         c_retv_if(!data, -1);
162
163         calendar_error_e error = CALENDAR_ERROR_NONE;
164
165         error = calendar_disconnect();
166         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_disconnect() is failed(%x)", error);
167
168         elm_theme_extension_del(NULL, EDJDIR "/calendar_theme.edj");
169         elm_theme_extension_del(NULL, EDJDIR "/calendar_theme2.edj");
170
171         cal_util_disconnect_pattern_generator();
172
173         int ret = 0;
174
175         if(data->item_type == ITEM_TYPE_TODO)
176                 ret = vconf_set_int(CAL_VCONFKEY_CREATE_EVENT_MODE, 0);
177         else
178                 ret = vconf_set_int(CAL_VCONFKEY_CREATE_EVENT_MODE, 1);
179         c_warn_if(ret != 0,"vconf_set_int(CAL_VCONFKEY_CREATE_EVENT_MODE, %d) is failed(%d)", 0, ret);
180
181         return 0;
182
183 }
184
185 static inline void __cal_edit_ug_set_record_title(calendar_record_h record, const char *title_str)
186 {
187         calendar_error_e error = CALENDAR_ERROR_NONE;
188
189         if (_calendar_is_task_record(record)) {
190                 error = calendar_record_set_str(record, _calendar_todo.summary, title_str);
191         } else {
192                 error = calendar_record_set_str(record, _calendar_event.summary, title_str);
193         }
194
195         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_set_str() is failed(%x)", error);
196 }
197
198 static inline void __cal_edit_ug_set_record_location(calendar_record_h record, const char *address, double latitude, double longitude)
199 {
200         calendar_error_e error = CALENDAR_ERROR_NONE;
201
202         if (_calendar_is_task_record(record)) {
203
204                 if (CAL_STRLEN(address))
205                         error = calendar_record_set_str(record, _calendar_todo.location, address);
206                 else
207                         error = calendar_record_set_str(record, _calendar_todo.location, "");
208
209                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_set_str() is failed(%x)", error);
210
211                 error = calendar_record_set_double(record, _calendar_todo.latitude, latitude);
212                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_set_double() is failed(%x)", error);
213
214                 error = calendar_record_set_double(record, _calendar_todo.longitude, longitude);
215                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_set_double() is failed(%x)", error);
216         } else {
217
218                 if (CAL_STRLEN(address))
219                         error = calendar_record_set_str(record, _calendar_event.location, address);
220                 else
221                         error = calendar_record_set_str(record, _calendar_event.location, "");
222
223                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_set_str() is failed(%x)", error);
224
225
226                 error = calendar_record_set_double(record, _calendar_event.latitude, latitude);
227                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_set_double() is failed(%x)", error);
228
229                 error = calendar_record_set_double(record, _calendar_event.longitude, longitude);
230                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_set_double() is failed(%x)", error);
231         }
232 }
233
234 static inline void __cal_edit_ug_set_record_note(calendar_record_h record, const char *note_str)
235 {
236         calendar_error_e error = CALENDAR_ERROR_NONE;
237
238         if (_calendar_is_task_record(record))
239                 error = calendar_record_set_str(record, _calendar_todo.description, note_str);
240         else
241                 error = calendar_record_set_str(record, _calendar_event.description, note_str);
242
243         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_set_str() is failed(%x)", error);
244 }
245
246 static void __cal_edit_ug_set_timezone(calendar_record_h record, const char *timezone_path, const char *timezone_city)
247 {
248         c_ret_if(!record);
249         c_ret_if(!CAL_STRLEN(timezone_path));
250         c_ret_if(!CAL_STRLEN(timezone_city));
251
252         calendar_error_e error = CALENDAR_ERROR_NONE;
253
254         if (_calendar_is_task_record(record)) {
255                 error = calendar_record_set_str(record, _calendar_todo.due_tzid, timezone_path);
256                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_set_str() is failed(%x)", error);
257         } else {
258                 error = calendar_record_set_str(record, _calendar_event.start_tzid, timezone_path);
259                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_set_str() is failed(%x)", error);
260
261                 error = calendar_record_set_str(record, _calendar_event.end_tzid, timezone_path);
262                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_set_str() is failed(%x)", error);
263         }
264
265         _calendar_set_timezone_city(record, timezone_city);
266 }
267
268 static void __cal_edit_ug_set_record_time(cal_edit_ug_data *p, calendar_record_h record)
269 {
270         c_ret_if(!p);
271         c_ret_if(!record);
272
273         calendar_error_e error = CALENDAR_ERROR_NONE;
274
275         calendar_time_s start_time = {0};
276
277         _calendar_get_start_time(record, &start_time);
278
279         calendar_time_s end_time = {0};
280
281         _calendar_get_end_time(record, &end_time);
282
283         if (_calendar_is_allday_record(record)) {
284
285                 start_time.time.date.year = p->stm.tm_year + 1900;
286                 start_time.time.date.month = p->stm.tm_mon + 1;
287                 start_time.time.date.mday = p->stm.tm_mday;
288
289                 end_time.time.date.year = p->stm.tm_year + 1900;
290                 end_time.time.date.month = p->stm.tm_mon + 1;
291                 end_time.time.date.mday = p->stm.tm_mday;
292
293         } else {
294                 cal_util_convert_tm_to_lli(NULL, &p->stm, &start_time.time.utime);
295                 cal_util_convert_tm_to_lli(NULL, &p->etm, &end_time.time.utime);
296         }
297
298         error = calendar_record_set_caltime(record, _calendar_event.start_time, start_time);
299         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_set_caltime() is failed(%x)", error);
300
301         error = calendar_record_set_caltime(record, _calendar_event.end_time, end_time);
302         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_set_caltime() is failed(%x)", error);
303
304         __cal_edit_ug_set_timezone(record, p->timezone_path, p->timezone_city);
305 }
306
307 static void __cal_edit_ug_set_rrule_wday(calendar_record_h record, enum __cal_edit_wday tm_wday)
308 {
309         c_ret_if(!record);
310
311         calendar_error_e error = CALENDAR_ERROR_NONE;
312
313         switch (tm_wday) {
314         case CAL_EDIT_WDAY_SUNDAY:
315                 error = calendar_record_set_str(record, _calendar_event.byday, "SU");
316                 break;
317
318         case CAL_EDIT_WDAY_MONDAY:
319                 error = calendar_record_set_str(record, _calendar_event.byday, "MO");
320                 break;
321
322         case CAL_EDIT_WDAY_TUESDAY:
323                 error = calendar_record_set_str(record, _calendar_event.byday, "TU");
324                 break;
325
326         case CAL_EDIT_WDAY_WEDNESDAY:
327                 error = calendar_record_set_str(record, _calendar_event.byday, "WE");
328                 break;
329
330         case CAL_EDIT_WDAY_THURSDAY:
331                 error = calendar_record_set_str(record, _calendar_event.byday, "TH");
332                 break;
333
334         case CAL_EDIT_WDAY_FRIDAY:
335                 error = calendar_record_set_str(record, _calendar_event.byday, "FR");
336                 break;
337
338         case CAL_EDIT_WDAY_SATURDAY:
339                 error = calendar_record_set_str(record, _calendar_event.byday, "SA");
340                 break;
341         default:
342                 ERR("Error!!");
343                 error = calendar_record_set_str(record, _calendar_event.byday, "SU");
344                 break;
345         }
346
347         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_set_str() is failed(%x)", error);
348 }
349
350 static void __cal_edit_ug_set_rrule_month(calendar_record_h record, int tm_mon)
351 {
352         c_ret_if(!record);
353
354         char buffer[8] = {0};
355
356         snprintf(buffer, sizeof(buffer), "%d", tm_mon + 1);
357
358         calendar_error_e error = CALENDAR_ERROR_NONE;
359
360         error = calendar_record_set_str(record, _calendar_event.bymonth, buffer);
361         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_set_str() is failed(%x)", error);
362 }
363
364 static void __cal_edit_ug_set_rrule_mday(calendar_record_h record, int tm_mday)
365 {
366         c_ret_if(!record);
367
368         char buffer[8] = {0};
369
370         snprintf(buffer, sizeof(buffer), "%d", tm_mday);
371
372         calendar_error_e error = CALENDAR_ERROR_NONE;
373
374         error = calendar_record_set_str(record, _calendar_event.bymonthday, buffer);
375         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_set_str() is failed(%x)", error);
376 }
377
378 static int __cal_edit_ug_g_list_compare(const void* data1, const void* data2)
379 {
380         const Cal_Reminder* reminder1 = (Cal_Reminder*)data1;
381         const Cal_Reminder* reminder2 = (Cal_Reminder*)data2;
382
383         if (reminder1->tick_unit == CALENDAR_ALARM_TIME_UNIT_SPECIFIC) {
384                 if (reminder2->tick_unit == CALENDAR_ALARM_TIME_UNIT_SPECIFIC) {
385                         long long int dt1, dt2;
386                         cal_util_convert_tm_to_lli(NULL, &reminder1->datetime, &dt1);
387                         cal_util_convert_tm_to_lli(NULL, &reminder2->datetime, &dt2);
388                         if (dt1 > dt2)
389                                 return 1;
390                         else if (dt1 < dt2)
391                                 return -1;
392                         else
393                                 return 0;
394                 } else {
395                         return 1;
396                 }
397         } else {
398                 if (reminder2->tick_unit == CALENDAR_ALARM_TIME_UNIT_SPECIFIC)
399                         return -1;
400                 else
401                         return reminder1->alarm_value - reminder2->alarm_value;
402         }
403 }
404
405 static int __cal_edit_ug_g_list_compare_with_data(const void* data1, const void* data2, void* user_data)
406 {
407         return __cal_edit_ug_g_list_compare(data1, data2);
408 }
409
410
411 static void __cal_edit_ug_set_record_alarm(cal_edit_ug_data *p, calendar_record_h record)
412 {
413         CAL_FN_START;
414
415         c_retm_if(!p, "p is null");
416         c_retm_if(!record, "record is null");
417
418         GList *temp  = NULL;
419
420         Eina_Bool is_task = _calendar_is_task_record(record);
421
422         if (is_task)
423                 _calendar_clear_child_record(record, _calendar_todo.calendar_alarm);
424         else
425                 _calendar_clear_child_record(record, _calendar_event.calendar_alarm);
426
427
428         Eina_List* list = NULL;
429         Evas_Object *item = NULL;
430         EINA_LIST_FOREACH(p->alarm_item_list, list, item)       {
431                 if(item)        {
432                         Cal_Reminder* reminder = evas_object_data_get(item, "data");
433
434                         if (g_list_find_custom(temp, reminder, __cal_edit_ug_g_list_compare))
435                                 continue;
436
437                         temp = g_list_insert_sorted_with_data(temp, reminder, __cal_edit_ug_g_list_compare_with_data, NULL);
438
439                         calendar_record_h calendar_alarm = cal_reminder_get_cal_val(reminder);
440
441                         calendar_error_e error = CALENDAR_ERROR_NONE;
442
443                         if (is_task)
444                                 error = calendar_record_add_child_record(record, _calendar_todo.calendar_alarm, calendar_alarm);
445                         else
446                                 error = calendar_record_add_child_record(record, _calendar_event.calendar_alarm, calendar_alarm);
447
448                         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_add_child_record() is failed(%x)", error);
449                 }
450         }
451 }
452
453 static void __cal_edit_ug_set_record_repeat(cal_edit_ug_data *p)
454 {
455         c_retm_if(!p, "p is null");
456
457         calendar_error_e error = CALENDAR_ERROR_NONE;
458
459         switch (p->repeat_freq) {
460                 case CALENDAR_RECURRENCE_NONE:
461                         break;
462
463                 case CALENDAR_RECURRENCE_DAILY:
464                         error = calendar_record_set_int(p->event, _calendar_event.interval, 1);
465                         break;
466
467                 case CAL_REPEAT_EVERY_3_DAY:
468                         p->repeat_freq = CALENDAR_RECURRENCE_DAILY;
469                         error = calendar_record_set_int(p->event, _calendar_event.interval, 3);
470                         break;
471
472                 case CALENDAR_RECURRENCE_WEEKLY:
473                         __cal_edit_ug_set_rrule_wday(p->event, p->stm.tm_wday);
474                         error = calendar_record_set_int(p->event, _calendar_event.interval, 1);
475                         break;
476
477                 case CAL_REPEAT_EVERY_2_WEEK:
478                         p->repeat_freq = CALENDAR_RECURRENCE_WEEKLY;
479                         __cal_edit_ug_set_rrule_wday(p->event, p->stm.tm_wday);
480                         error = calendar_record_set_int(p->event, _calendar_event.interval, 2);
481                         break;
482
483                 case CALENDAR_RECURRENCE_YEARLY:
484                         __cal_edit_ug_set_rrule_month(p->event, p->stm.tm_mon);
485                 case CALENDAR_RECURRENCE_MONTHLY:
486                         __cal_edit_ug_set_rrule_mday(p->event, p->stm.tm_mday);
487                         error = calendar_record_set_int(p->event, _calendar_event.interval, 1);
488                         break;
489
490                 default:
491                         break;
492         }
493
494         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_set_int() is failed(%x)", error);
495
496         error = calendar_record_set_int(p->event, _calendar_event.freq, p->repeat_freq);
497         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_set_int() is failed(%x)", error);
498
499         error = calendar_record_set_int(p->event, _calendar_event.range_type, p->repeat_range);
500         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_set_int() is failed(%x)", error);
501
502         if (CALENDAR_RECURRENCE_NONE != p->repeat_freq) {
503
504                 if (p->repeat_range == CALENDAR_RANGE_COUNT) {
505                         error = calendar_record_set_int(p->event, _calendar_event.count, p->repeat_count);
506                         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_set_int() is failed(%x)", error);
507                 } else if (p->repeat_range == CALENDAR_RANGE_UNTIL) {
508
509                         error = calendar_record_set_int(p->event, _calendar_event.count, 0);
510                         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_set_int() is failed(%x)", error);
511
512                         calendar_time_s until_time = {0};
513
514                         until_time.type = CALENDAR_TIME_UTIME;
515                         cal_util_convert_tm_to_lli(NULL, &p->repeat_until, &until_time.time.utime);
516
517                         error = calendar_record_set_caltime(p->event, _calendar_event.until_time, until_time);
518                         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_set_int() is failed(%x)", error);
519                 }
520         }
521 }
522
523 static void __cal_edit_ug_set_record_save_to( calendar_record_h record, int calendar_book_id)
524 {
525         calendar_error_e error = CALENDAR_ERROR_NONE;
526
527         if (_calendar_is_task_record(record)) {
528                 error = calendar_record_set_int(record, _calendar_todo.calendar_book_id, calendar_book_id);
529                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_set_int() is failed(%x)", error);
530         } else {
531                 error = calendar_record_set_int(record, _calendar_event.calendar_book_id, calendar_book_id);
532                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_set_int() is failed(%x)", error);
533
534                 calendar_record_h calendar_book = NULL;
535
536                 error = calendar_db_get_record(_calendar_book._uri, calendar_book_id, &calendar_book);
537                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_db_get_record() is failed(%x)", error);
538
539                 char *organizer_name = NULL;
540
541                 error = calendar_record_get_str(calendar_book, _calendar_book.description, &organizer_name);
542                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_str() is failed(%x)", error);
543
544                 error = calendar_record_set_str(record, _calendar_event.organizer_name, organizer_name);
545                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_set_str() is failed(%x)", error);
546
547                 CAL_FREE(organizer_name);
548
549                 error = calendar_record_destroy(calendar_book, true);
550                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_destroy() is failed(%x)", error);
551         }
552 }
553
554 static void  __cal_edit_ug_set_record_due_time(cal_edit_ug_data *p, calendar_record_h record)
555 {
556         c_retm_if(!p, "p is null");
557         c_retm_if(!record, "record is null");
558
559         calendar_error_e error = CALENDAR_ERROR_NONE;
560
561         calendar_time_s due_time = {0};
562
563         _calendar_get_end_time(record, &due_time);
564
565         error = calendar_record_set_str(record, _calendar_todo.due_tzid, p->timezone_path);
566         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_set_str() is failed(%x)", error);
567
568         if (p->is_no_due_date)  {
569                 due_time.time.utime = CALENDAR_TODO_NO_DUE_DATE;
570         } else {
571                 cal_util_convert_tm_to_lli(NULL, &p->etm, &due_time.time.utime);
572         }
573
574         error = calendar_record_set_caltime(record, _calendar_todo.due_time, due_time);
575         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_set_caltime() is failed(%x)", error);
576 }
577
578 static void __cal_edit_ug_set_record_priority(calendar_record_h record, int priority)
579 {
580         c_retm_if(!record, "record is null");
581
582         calendar_error_e error = CALENDAR_ERROR_NONE;
583
584         error = calendar_record_set_int(record, _calendar_todo.priority, priority);
585         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_set_int() is failed(%x)", error);
586 }
587
588 static void __cal_edit_ug_initialize_event_record(cal_edit_ug_data *p)
589 {
590         CAL_FN_START;
591
592         c_retm_if(!p, "p is null");
593
594         calendar_error_e error = CALENDAR_ERROR_NONE;
595
596         if(!p->event) {
597                 error = calendar_record_create(_calendar_event._uri, &p->event);
598                 c_retm_if(error != CALENDAR_ERROR_NONE, "calendar_record_create() is failed(%x)", error);
599         }
600
601         p->stm = p->base_time;
602         p->stm.tm_min = p->stm.tm_sec = 0;
603
604         if (0 <= p->base_time.tm_hour)
605                 p->stm.tm_hour = p->base_time.tm_hour;
606         else
607                 cal_util_update_tm_hour(&p->stm,1);
608
609         p->etm = p->stm;
610         cal_util_update_tm_hour(&p->etm,1);
611
612          __cal_edit_ug_set_record_time(p, p->event);
613
614         struct tm repeat_until = p->stm;
615         cal_util_update_tm_month (&repeat_until, 1);
616         p->repeat_freq = CALENDAR_RECURRENCE_NONE;
617         p->repeat_range = CALENDAR_RANGE_NONE;
618         p->repeat_count = 10;
619         __cal_edit_ug_set_record_repeat(p);
620
621         __cal_edit_ug_set_record_save_to(p->event, DEFAULT_EVENT_CALENDAR_BOOK_ID);
622
623         p->title_str = NULL;
624         p->note_str = NULL;
625         p->address = NULL;
626 }
627
628 static void __cal_edit_ug_initialize_todo_record(cal_edit_ug_data *p)
629 {
630         CAL_FN_START;
631
632         c_retm_if(!p, "p is null");
633
634         calendar_error_e error = CALENDAR_ERROR_NONE;
635
636         if(!p->todo) {
637                 error = calendar_record_create(_calendar_todo._uri, &p->todo);
638                 c_retm_if(error != CALENDAR_ERROR_NONE, "calendar_record_create() is failed(%x)", error);
639         }
640
641         p->stm = p->base_time;
642         p->stm.tm_min = p->stm.tm_sec = 0;
643
644         if (0 <= p->base_time.tm_hour)
645                 p->stm.tm_hour = p->base_time.tm_hour;
646         else
647                 cal_util_update_tm_hour(&p->stm,1);
648
649         p->etm = p->stm;
650         cal_util_update_tm_hour(&p->etm,1);
651
652         calendar_time_s end_time = {0};
653         end_time.type = CALENDAR_TIME_UTIME;
654
655         cal_util_convert_tm_to_lli(NULL, &p->etm, &end_time.time.utime);
656
657         error = calendar_record_set_caltime(p->todo, _calendar_todo.due_time, end_time);
658         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_set_caltime() is failed(%x)", error);
659
660         __cal_edit_ug_set_timezone(p->todo, p->timezone_path, p->timezone_city);
661
662         __cal_edit_ug_set_record_save_to(p->todo, DEFAULT_TODO_CALENDAR_BOOK_ID);
663
664         __cal_edit_ug_set_record_priority(p->todo, CALENDAR_TODO_PRIORITY_NORMAL);
665
666         p->title_str = NULL;
667         p->note_str = NULL;
668         p->address = NULL;
669 }
670
671 static inline char * __cal_edit_ug_get_record_title(calendar_record_h record)
672 {
673         c_retvm_if(!record, NULL, "record is null");
674
675         calendar_error_e error = CALENDAR_ERROR_NONE;
676         char *summary = NULL;
677
678         if (_calendar_is_task_record(record))
679                 error = calendar_record_get_str(record, _calendar_todo.summary, &summary);
680         else
681                 error = calendar_record_get_str(record, _calendar_event.summary, &summary);
682
683         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_str() is failed(%x)", error);
684
685         return summary;
686 }
687
688 static inline void __cal_edit_ug_get_record_location(cal_edit_ug_data *p, calendar_record_h record)
689 {
690         calendar_error_e error = CALENDAR_ERROR_NONE;
691
692         p->address = _calendar_get_location(record);
693
694         if (_calendar_is_task_record(record))
695         {
696                 error = calendar_record_get_double(record, _calendar_todo.latitude, &p->latitude);
697                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_double() is failed(%x)", error);
698                 error = calendar_record_get_double(record, _calendar_todo.longitude, &p->longitude);
699                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_double() is failed(%x)", error);
700         }
701         else
702         {
703                 error = calendar_record_get_double(record, _calendar_event.latitude, &p->latitude);
704                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_double() is failed(%x)", error);
705                 error = calendar_record_get_double(record, _calendar_event.longitude, &p->longitude);
706                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_double() is failed(%x)", error);
707         }
708 }
709
710 static inline char * __cal_edit_ug_get_record_note(calendar_record_h record)
711 {
712         c_retvm_if(!record, NULL, "record is null");
713
714         calendar_error_e error = CALENDAR_ERROR_NONE;
715         char *note = NULL;
716
717         if (_calendar_is_task_record(record))
718                 error = calendar_record_get_str(record, _calendar_todo.description, &note);
719         else
720                 error = calendar_record_get_str(record, _calendar_event.description, &note);
721
722         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_str() is failed(%x)", error);
723
724         return note;
725 }
726
727 static int __cal_edit_ug_get_timezone_offset_with_standard_name(const char *standard_name)
728 {
729         int timezone_offset = 0;
730
731         c_retv_if(!CAL_STRLEN(standard_name), timezone_offset);
732
733         calendar_error_e error = CALENDAR_ERROR_NONE;
734
735         calendar_query_h query = NULL;
736
737         error = calendar_query_create(_calendar_timezone._uri, &query);
738         c_retvm_if(error != CALENDAR_ERROR_NONE, timezone_offset, "calendar_query_create() is failed(%x)", error);
739
740         calendar_filter_h filter = NULL;
741
742         error = calendar_filter_create(_calendar_timezone._uri, &filter);
743         if (error != CALENDAR_ERROR_NONE) {
744
745                 ERR("calendar_filter_create() is failed(%x)", error);
746                 calendar_query_destroy(query);
747                 return 0;
748         }
749
750         error = calendar_filter_add_str(filter, _calendar_timezone.standard_name, CALENDAR_MATCH_EXACTLY, standard_name);
751         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_str() is failed(%x)", error);
752
753         error = calendar_query_set_filter(query, filter);
754         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_query_set_filter() is failed(%x)", error);
755
756         calendar_list_h list = NULL;
757
758         error = calendar_db_get_records_with_query(query, 0, 1, &list);
759         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_db_get_records_with_query() is failed(%x)", error);
760
761         error = calendar_list_first(list);
762         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_list_first() is failed(%x)", error);
763
764         calendar_record_h timezone = NULL;
765
766         error = calendar_list_get_current_record_p(list, &timezone);
767         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_list_get_current_record_p() is failed(%x)", error);
768
769         error = calendar_record_get_int(timezone, _calendar_timezone.tz_offset_from_gmt, &timezone_offset);
770         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_list_get_current_record_p() is failed(%x)", error);
771
772         error = calendar_list_destroy(list, true);
773         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_list_destroy() is failed(%x)", error);
774
775         error = calendar_filter_destroy(filter);
776         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_destroy() is failed(%x)", error);
777
778         error = calendar_query_destroy(query);
779         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_query_destroy() is failed(%x)", error);
780
781         return (-1)*timezone_offset;
782 }
783
784 static void __cal_edit_ug_get_timezone_for_eas(cal_edit_ug_data *p, calendar_record_h record)
785 {
786         c_ret_if(!p);
787         c_ret_if(!record);
788
789         calendar_error_e error = CALENDAR_ERROR_NONE;
790
791         char *standard_name = NULL;
792
793         if (_calendar_is_task_record(record))
794                 error = calendar_record_get_str_p(record, _calendar_todo.due_tzid, &standard_name);
795         else
796                 error = calendar_record_get_str_p(record, _calendar_event.start_tzid, &standard_name);
797
798         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_str() is failed(%x)", error);
799
800         c_ret_if(!CAL_STRLEN(standard_name));
801
802         int timezone_offset = __cal_edit_ug_get_timezone_offset_with_standard_name(standard_name);
803
804         cal_util_get_timezone_id(timezone_offset, &p->timezone_path);
805 }
806
807 static void  __cal_edit_ug_get_record_time_zone(cal_edit_ug_data *p, calendar_record_h record)
808 {
809         c_ret_if(!p);
810         c_ret_if(!record);
811
812         calendar_error_e error = CALENDAR_ERROR_NONE;
813
814         if (_calendar_is_eas_record(record))
815                 __cal_edit_ug_get_timezone_for_eas(p, record);
816         else {
817                 if (_calendar_is_task_record(record))
818                         error = calendar_record_get_str(record, _calendar_todo.due_tzid, &p->timezone_path);
819                 else
820                         error = calendar_record_get_str(record, _calendar_event.start_tzid, &p->timezone_path);
821
822                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_str() is failed(%x)", error);
823
824                 p->timezone_city = _calendar_get_timezone_city(record);
825         }
826
827         cal_util_get_timezone(&p->timezone_path, &p->timezone_city, &p->timezone_offset);
828
829         c_ret_if(!CAL_STRLEN(p->timezone_path));
830         c_ret_if(!CAL_STRLEN(p->timezone_city));
831 }
832
833 static void  __cal_edit_ug_get_record_alarm(cal_edit_ug_data *p, calendar_record_h record)
834 {
835         c_retm_if(!p, "p is null");
836         c_retm_if(!record, "record is null");
837
838         if (p->alarm_list) {
839                 g_list_free(p->alarm_list);
840                 p->alarm_list = NULL;
841         }
842
843         int alarm_count = 0;
844
845         Eina_Bool is_task = _calendar_is_task_record(record);
846         calendar_error_e error = CALENDAR_ERROR_NONE;
847
848         if (is_task)
849         {
850                 error = calendar_record_get_child_record_count(record, _calendar_todo.calendar_alarm, (unsigned int *)&alarm_count);
851                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_child_record_count() is failed(%x)", error);
852         }
853         else
854         {
855                 error = calendar_record_get_child_record_count(record, _calendar_event.calendar_alarm, (unsigned int *)&alarm_count);
856                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_child_record_count() is failed(%x)", error);
857         }
858
859         int i = 0;
860         for (i = 0; i < alarm_count; i++) {
861                 calendar_record_h calendar_alarm = NULL;
862
863                 if (is_task)
864                 {
865                         error = calendar_record_get_child_record_at_p(record, _calendar_todo.calendar_alarm, i, &calendar_alarm);
866                         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_child_record_at_p() is failed(%x)", error);
867                 }
868                 else
869                 {
870                         error = calendar_record_get_child_record_at_p(record, _calendar_event.calendar_alarm, i, &calendar_alarm);
871                         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_child_record_at_p() is failed(%x)", error);
872                 }
873
874                 Cal_Reminder* reminder = cal_reminder_create(calendar_alarm);
875                 p->alarm_list = g_list_append(p->alarm_list,(void *)reminder);
876         }
877
878         p->alarm_list = g_list_first(p->alarm_list);
879 }
880
881 static void  __cal_edit_ug_get_record_repeat(cal_edit_ug_data *p, calendar_record_h record)
882 {
883         c_ret_if(!p);
884         c_ret_if(!record);
885
886         calendar_record_h original_record = NULL;
887         int original_event_id = 0;
888         calendar_error_e error = CALENDAR_ERROR_NONE;
889
890         error = calendar_record_get_int(record, _calendar_event.original_event_id, &original_event_id);
891         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_int() is failed(%x)", error);
892
893         if (0 < original_event_id) {
894
895                 error = calendar_db_get_record(_calendar_event._uri, original_event_id, &original_record);
896                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_db_get_record() is failed(%x)", error);
897         }
898
899         if (!original_record)
900                 original_record = record;
901
902         error = calendar_record_get_int(record, _calendar_event.freq, &p->repeat_freq);
903         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_int() is failed(%x)", error);
904
905         if (p->repeat_freq == CALENDAR_RECURRENCE_WEEKLY) {
906
907                 int interval = 0;
908
909                 error = calendar_record_get_int(record, _calendar_event.interval, &interval);
910                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_int() is failed(%x)", error);
911
912                 if (2 == interval)
913                         p->repeat_freq = CAL_REPEAT_EVERY_2_WEEK;
914
915         } else if (p->repeat_freq == CALENDAR_RECURRENCE_DAILY) {
916
917                 int interval = 0;
918
919                 error = calendar_record_get_int(record, _calendar_event.interval, &interval);
920                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_int() is failed(%x)", error);
921
922                 if (3 == interval)
923                         p->repeat_freq = CAL_REPEAT_EVERY_3_DAY;
924         }
925
926         error = calendar_record_get_int(record, _calendar_event.range_type, (int *)&p->repeat_range);
927         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_int() is failed(%x)", error);
928
929         if (p->repeat_range == CALENDAR_RANGE_NONE) {
930                 p->repeat_count = 10;
931
932                 p->repeat_until = p->stm;
933                 cal_util_update_tm_month (&p->repeat_until, 1);
934
935         } else {
936                 error = calendar_record_get_int(record, _calendar_event.count, &p->repeat_count);
937                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_int() is failed(%x)", error);
938
939                 calendar_time_s until_utime = {0};
940
941                 error = calendar_record_get_caltime(record, _calendar_event.until_time, &until_utime);
942                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_caltime() is failed(%x)", error);
943
944                 cal_util_convert_lli_to_tm(NULL, until_utime.time.utime, &p->repeat_until);
945         }
946
947         if (original_record != record) {
948                 error = calendar_record_destroy(original_record, true);
949                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_destroy() is failed(%x)", error);
950         }
951 }
952
953 static int  __cal_edit_ug_get_record_priority(calendar_record_h record)
954 {
955         c_retv_if(!record, 0);
956
957         calendar_error_e error = CALENDAR_ERROR_NONE;
958
959         int priority;
960
961         error = calendar_record_get_int(record, _calendar_todo.priority, (int *)&priority);
962         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_int() is failed(%x)", error);
963
964         return priority;
965 }
966
967 static void __cal_edit_ug_get_record_due_time(cal_edit_ug_data *p, calendar_record_h record)
968 {
969         c_retm_if(!p, "p is null");
970         c_retm_if(!record, "record is null");
971
972         calendar_error_e error = CALENDAR_ERROR_NONE;
973
974         calendar_time_s due_time = {0};
975
976         _calendar_get_end_time(record, &due_time);
977
978         cal_util_convert_lli_to_tm(NULL, due_time.time.utime, &p->etm);
979
980         if (due_time.time.utime == CALENDAR_TODO_NO_DUE_DATE) {
981                 p->is_no_due_date = EINA_TRUE;
982                 //p->etm = p->base_time;
983         }
984
985         error = calendar_record_get_str(record, _calendar_todo.due_tzid, &p->timezone_path);
986         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_str() is failed(%x)", error);
987
988         c_ret_if(!p->timezone_path);
989
990         cal_util_get_timezone(&p->timezone_path, &p->timezone_city, &p->timezone_offset);
991 }
992
993 static void __cal_edit_ug_update_title_item(Evas_Object *entry, calendar_record_h record)
994 {
995         c_retm_if(!entry, "entry is null");
996         c_retm_if(!record, "record is null");
997
998         char* title = __cal_edit_ug_get_record_title(record);
999
1000         elm_entry_entry_set(entry, title);
1001
1002         CAL_FREE(title);
1003 }
1004
1005 static void __cal_edit_ug_update_note_item(Evas_Object *entry, calendar_record_h record)
1006 {
1007         c_retm_if(!entry, "entry is null");
1008         c_retm_if(!record, "record is null");
1009
1010         char* note = __cal_edit_ug_get_record_note(record);
1011
1012         elm_entry_entry_set(entry, note);
1013
1014         CAL_FREE(note);
1015 }
1016
1017 static void __cal_edit_ug_update_location_item(cal_edit_ug_data *p, calendar_record_h record)
1018 {
1019         c_retm_if(!p, "p is null");
1020         c_retm_if(!record, "record is null");
1021
1022         __cal_edit_ug_get_record_location(p, record);
1023         elm_entry_entry_set(p->location_entry, p->address);
1024 }
1025
1026 static void __cal_edit_ug_get_save_to_str(int calid, char *buf, int sz);
1027
1028 static void __cal_edit_ug_update_save_to_item(cal_edit_ug_data *p, calendar_record_h record)
1029 {
1030         c_retm_if(!p, "p is null");
1031
1032         int calendar_book_id = _calendar_get_calendar_index(record);
1033
1034         char buf[1024];
1035         char* saveto_text = NULL;
1036         if (calendar_book_id == DEFAULT_EVENT_CALENDAR_BOOK_ID)
1037                 saveto_text =  strdup(C_("IDS_CLD_BODY_MY_CALENDAR"));
1038         else if (calendar_book_id == DEFAULT_TODO_CALENDAR_BOOK_ID)
1039                 saveto_text =  strdup(C_("IDS_TASK_BODY_MY_TASK"));
1040         else {
1041                 __cal_edit_ug_get_save_to_str(calendar_book_id, buf, sizeof(buf));
1042                 saveto_text = strdup(buf);
1043         }
1044
1045         elm_object_part_text_set(p->saveto, "elm.text.1", saveto_text);
1046
1047         CAL_FREE(saveto_text);
1048 }
1049
1050 static void __cal_edit_ug_update_priority_item(cal_edit_ug_data *p, calendar_record_h record)
1051 {
1052         c_retm_if(!p, "p is null");
1053         c_retm_if(!record, "record is null");
1054
1055         char* priority_text = NULL;
1056         int priority = __cal_edit_ug_get_record_priority(record);
1057         switch (priority) {
1058                 case CALENDAR_TODO_PRIORITY_LOW:
1059                         priority_text = strdup(S_("IDS_COM_BODY_LOW"));
1060                         break;
1061                 case CALENDAR_TODO_PRIORITY_NORMAL:
1062                         priority_text =strdup(C_("IDS_IDLE_BODY_NORMAL"));
1063                         break;
1064                 case CALENDAR_TODO_PRIORITY_HIGH:
1065                         priority_text =strdup(S_("IDS_COM_OPT_HIGH_M_QUALITY"));
1066                         break;
1067                 default:
1068                         break;
1069         }
1070
1071         elm_object_part_text_set(p->priority, "elm.text.1", priority_text);
1072         CAL_FREE(priority_text);
1073
1074 }
1075
1076 static void __cal_edit_ug_update_start_language(Evas_Object *item)
1077 {
1078         c_ret_if(!item);
1079
1080         elm_object_part_text_set(item, "elm.text", C_("IDS_CLD_BODY_FROM"));
1081 }
1082
1083 static void __cal_edit_ug_update_end_language(Evas_Object *item)
1084 {
1085         c_ret_if(!item);
1086
1087         elm_object_part_text_set(item, "elm.text", C_("IDS_CLD_BODY_TO"));
1088 }
1089
1090 static void __cal_edit_ug_update_due_language(Evas_Object *item)
1091 {
1092         c_ret_if(!item);
1093
1094         elm_object_part_text_set(item, "elm.text", C_("IDS_CLD_BODY_DUE_DATE"));
1095 }
1096
1097 static Evas_Object *__cal_edit_ug_insert_padding_after(cal_edit_ug_data* p, const char* title, Evas_Object *after)
1098 {
1099         c_retv_if(!p, NULL);
1100         c_retv_if(!after, NULL);
1101
1102         Evas_Object *item = cal_dialogue_insert_item_after(p->dialogue, "title", after, NULL, NULL);
1103         c_retvm_if(!item, NULL, "cal_dialogue_append_item() is failed");
1104
1105         if (CAL_STRLEN(title))
1106                 elm_object_part_text_set(item, "elm.text", title);
1107
1108         return item;
1109 }
1110
1111 static void __cal_edit_ug_title_entry_changed_callback(void *data, Evas_Object *obj, void *event_info)
1112 {
1113         const char *str;
1114         cal_edit_ug_data* p = (cal_edit_ug_data *)data;
1115
1116         free(p->title_str);
1117
1118         str = elm_entry_entry_get(p->title_entry);
1119         if (!str || str[0] == '\0')
1120                 p->title_str = NULL;
1121         else
1122                 p->title_str = elm_entry_markup_to_utf8(str);
1123
1124         if(p->item_type == ITEM_TYPE_TODO)
1125                 __cal_edit_ug_set_record_title(p->todo, p->title_str);
1126         else
1127                 __cal_edit_ug_set_record_title(p->event, p->title_str);
1128 }
1129
1130 static Evas_Object * __cal_edit_ug_title_icon(cal_edit_ug_data* p, Evas_Object *obj)
1131 {
1132         c_retv_if(!p, NULL);
1133         c_retv_if(!obj, NULL);
1134
1135         Evas_Object *ef = cal_util_add_edit_field(obj, S_("IDS_COM_BODY_DETAILS_TITLE"), EINA_FALSE, EINA_TRUE);
1136         c_retvm_if(!ef, NULL, "ef is null");
1137
1138         Evas_Object *entry = elm_object_part_content_get(ef, "elm.swallow.content");
1139         c_retvm_if(!entry, ef, "entry is null");
1140
1141         elm_entry_cnp_mode_set(entry, ELM_CNP_MODE_PLAINTEXT);
1142
1143         p->title_entry = entry;
1144
1145         if (p->item_type == ITEM_TYPE_TODO)
1146                 __cal_edit_ug_update_title_item(p->title_entry, p->todo);
1147         else
1148                 __cal_edit_ug_update_title_item(p->title_entry, p->event);
1149
1150         evas_object_smart_callback_add(p->title_entry, "changed", __cal_edit_ug_title_entry_changed_callback, p);
1151         evas_object_smart_callback_add(p->title_entry, "preedit,changed", __cal_edit_ug_title_entry_changed_callback, p);
1152
1153         cal_save_cancel_set_input_panel_callback_for_ug(p->save_cancel, p->title_entry);
1154
1155         return ef;
1156
1157 }
1158
1159 static void __cal_edit_ug_update_title_language(Evas_Object *item)
1160 {
1161         c_ret_if(!item);
1162
1163         Evas_Object *edit_field = elm_object_part_content_get(item, "elm.icon");
1164         c_ret_if(!edit_field);
1165
1166         elm_object_part_text_set(edit_field, "elm.guidetext", S_("IDS_COM_BODY_DETAILS_TITLE"));
1167 }
1168
1169 static Evas_Object *__cal_edit_ug_add_title(cal_edit_ug_data* p)
1170 {
1171         c_retv_if(!p, NULL);
1172
1173         Evas_Object *item = cal_dialogue_append_item(p->dialogue, "1icon", NULL, NULL);
1174         c_retvm_if(!item, NULL, "cal_dialogue_append_item() is failed");
1175
1176         Evas_Object *editfield = __cal_edit_ug_title_icon(p, item);
1177         c_retvm_if(!editfield, NULL, "__cal_edit_ug_title_icon() is failed");
1178
1179         elm_object_part_content_set(item, "elm.icon", editfield);
1180
1181         return item;
1182 }
1183
1184 static void __cal_edit_ug_location_entry_changed_callback(void *data, Evas_Object *obj, void *event_info)
1185 {
1186         c_ret_if(!data);
1187
1188         cal_edit_ug_data *p = data;
1189         c_ret_if(!p);
1190
1191         CAL_FREE(p->address);
1192
1193         p->address = elm_entry_markup_to_utf8(elm_entry_entry_get(p->location_entry));
1194
1195         if (!CAL_STRLEN(p->address)) {
1196
1197                 p->longitude = p->latitude = CALENDAR_RECORD_NO_COORDINATE;
1198
1199         }
1200
1201         if (p->item_type == ITEM_TYPE_TODO)
1202                 __cal_edit_ug_set_record_location(p->todo, p->address, p->latitude, p->longitude);
1203         else
1204                 __cal_edit_ug_set_record_location(p->event, p->address, p->latitude, p->longitude);
1205 }
1206
1207 static Evas_Object * __cal_edit_ug_location_icon(cal_edit_ug_data* p, Evas_Object *obj)
1208 {
1209         c_retv_if(!p, NULL);
1210         c_retv_if(!obj, NULL);
1211
1212         Evas_Object *ly = NULL;
1213
1214         Evas_Object *ef = cal_util_add_edit_field(obj, C_("IDS_COM_BODY_DETAILS_LOCATION"), EINA_FALSE, EINA_TRUE);
1215         ly = ef;
1216
1217         p->location_entry = elm_object_part_content_get(ef, "elm.swallow.content");
1218         c_retv_if(!p->location_entry, ly);
1219
1220         evas_object_smart_callback_add(p->location_entry, "changed", __cal_edit_ug_location_entry_changed_callback, p);
1221         evas_object_smart_callback_add(p->location_entry, "preedit,changed", __cal_edit_ug_location_entry_changed_callback, p);
1222         cal_save_cancel_set_input_panel_callback_for_ug(p->save_cancel, p->location_entry);
1223
1224         elm_entry_cnp_mode_set(p->location_entry, ELM_CNP_MODE_PLAINTEXT);
1225
1226         if (p->item_type == ITEM_TYPE_TODO)
1227                 __cal_edit_ug_update_location_item(p, p->todo);
1228         else
1229                 __cal_edit_ug_update_location_item(p, p->event);
1230
1231         return ly;
1232 }
1233
1234 static void __cal_edit_ug_update_location_language(Evas_Object *item)
1235 {
1236         c_ret_if(!item);
1237
1238         Evas_Object *layout = elm_object_part_content_get(item, "elm.icon");
1239         c_ret_if(!layout);
1240
1241         Evas_Object *edit_field = elm_object_part_content_get(layout, "editfield/sw");
1242         c_ret_if(!edit_field);
1243
1244         elm_object_part_text_set(edit_field, "elm.guidetext", C_("IDS_COM_BODY_DETAILS_LOCATION"));
1245
1246         Evas_Object *button = elm_object_part_content_get(layout, "button/sw");
1247         c_ret_if(!button);
1248
1249         elm_object_text_set(button, C_("IDS_LBS_BODY_MAP"));
1250 }
1251
1252 static Evas_Object *__cal_edit_ug_add_location(cal_edit_ug_data* p)
1253 {
1254         c_retv_if(!p, NULL);
1255
1256         Evas_Object *item = cal_dialogue_append_item(p->dialogue, "1icon", NULL, NULL);
1257         c_retvm_if(!item, NULL, "cal_dialogue_append_item() is failed");
1258
1259         Evas_Object *editfield = __cal_edit_ug_location_icon(p, item);
1260         c_retvm_if(!editfield, NULL, "__cal_edit_ug_note_icon() is failed");
1261
1262         elm_object_part_content_set(item, "elm.icon", editfield);
1263
1264         __cal_edit_ug_insert_padding_after(p, NULL, item);
1265
1266         return item;
1267 }
1268
1269 static void __cal_edit_ug_note_entry_changed_callback(void *data, Evas_Object *obj, void *event_info)
1270 {
1271         char *email_link = NULL;
1272         cal_edit_ug_data *p = data;
1273         char *str = elm_entry_markup_to_utf8(elm_entry_entry_get(p->note_entry));
1274
1275         if (CAL_STRLEN(p->note_str))
1276                 email_link = strstr(p->note_str, EMAIL_LINK_FLAG);
1277
1278         if ((NULL != email_link)  && CAL_STRLEN(str) && (NULL != strstr(p->note_str, str))) {
1279                 char *strnote = NULL;
1280                 int strnote_len = 0;
1281                 strnote_len = strlen(str) + strlen(email_link);
1282                 CAL_CALLOC(strnote, strnote_len+1, char);
1283                 if (NULL != strnote)
1284                 {
1285                         snprintf(strnote, strnote_len + 1, "%s%s", str, email_link);
1286                         p->note_str = strdup(strnote);
1287
1288                         free(strnote);
1289                 }
1290                 else
1291                 {
1292                         p->note_str = NULL;
1293                 }
1294
1295         } else {
1296                 p->note_str = CAL_STRDUP(str);
1297         }
1298
1299         free(str);
1300
1301         if(p->item_type == ITEM_TYPE_TODO)
1302                 __cal_edit_ug_set_record_note(p->todo, p->note_str);
1303         else
1304                 __cal_edit_ug_set_record_note(p->event, p->note_str);
1305 }
1306
1307
1308 static Evas_Object * __cal_edit_ug_note_icon(cal_edit_ug_data* p, Evas_Object *obj)
1309 {
1310         c_retv_if(!p, NULL);
1311         c_retv_if(!obj, NULL);
1312
1313         Evas_Object *ef = cal_util_add_edit_field(obj, C_("IDS_CLD_BODY_CREATE_DESCRITION"), EINA_FALSE, EINA_TRUE );
1314
1315         if (ef) {
1316                 Evas_Object *entry = elm_object_part_content_get(ef, "elm.swallow.content");
1317                 c_retvm_if(!entry, ef, "entry is null");
1318
1319                 evas_object_size_hint_weight_set(entry, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
1320                 evas_object_size_hint_align_set(entry, EVAS_HINT_FILL, EVAS_HINT_EXPAND);
1321                 //elm_entry_autocapital_type_set(entry, EINA_TRUE);
1322                 elm_entry_cnp_mode_set(entry, ELM_CNP_MODE_PLAINTEXT);
1323
1324                 p->note_entry = entry;
1325
1326                 if (p->note_str) {
1327
1328                         char *email_link = NULL;
1329                         email_link = strstr(p->note_str, EMAIL_LINK_FLAG);
1330                         if(!email_link)
1331                                 elm_entry_entry_set(p->note_entry, elm_entry_utf8_to_markup(p->note_str));
1332                         else {
1333
1334                                 char *snote_display = NULL;
1335                                 int nsize = email_link - p->note_str + 1;
1336                                 snote_display = calloc(nsize, sizeof(char));
1337                                 c_retvm_if(!snote_display , NULL, "calloc(nsize, sizeof(char)) is failed");
1338
1339                                 CAL_STRNCPY(snote_display, p->note_str, nsize-1);
1340
1341                                 elm_entry_entry_set(p->note_entry, elm_entry_utf8_to_markup(snote_display));
1342                                 free(snote_display);
1343                         }
1344                 }
1345
1346                 if (p->item_type == ITEM_TYPE_TODO)
1347                         __cal_edit_ug_update_note_item(p->note_entry, p->todo);
1348                 else
1349                         __cal_edit_ug_update_note_item(p->note_entry, p->event);
1350
1351                 evas_object_smart_callback_add(p->note_entry, "changed", __cal_edit_ug_note_entry_changed_callback, p);
1352                 evas_object_smart_callback_add(p->note_entry, "preedit,changed", __cal_edit_ug_note_entry_changed_callback, p);
1353                 cal_save_cancel_set_input_panel_callback_for_ug(p->save_cancel, p->note_entry);
1354
1355                 return ef;
1356         }
1357
1358         return NULL;
1359 }
1360
1361 static void __cal_edit_ug_update_description_language(Evas_Object *item)
1362 {
1363         c_ret_if(!item);
1364
1365         Evas_Object *edit_field = elm_object_part_content_get(item, "elm.icon");
1366         c_ret_if(!edit_field);
1367
1368         elm_object_part_text_set(edit_field, "elm.guidetext", C_("IDS_CLD_BODY_CREATE_DESCRITION"));
1369 }
1370
1371 static Evas_Object *__cal_edit_ug_add_note(cal_edit_ug_data* p)
1372 {
1373         c_retv_if(!p, NULL);
1374
1375         Evas_Object *item = cal_dialogue_append_item(p->dialogue, "1icon",  NULL, NULL);
1376         c_retvm_if(!item, NULL, "cal_dialogue_append_item() is failed");
1377
1378         Evas_Object *editfield = __cal_edit_ug_note_icon(p, item);
1379         c_retvm_if(!editfield, NULL, "__cal_edit_ug_location_icon() is failed");
1380
1381
1382         elm_object_part_content_set(item, "elm.icon", editfield);
1383
1384         return item;
1385 }
1386
1387 static Evas_Object * __cal_edit_ug_edit_time_icon(Evas_Object *obj, struct tm* value, Eina_Bool allday)
1388 {
1389         c_retvm_if(!obj, NULL, "obj is null");
1390
1391         Evas_Object* icon_obj = cal_util_add_datetime(obj, NULL, value);
1392         c_retvm_if(!icon_obj, NULL, "icon_obj is null");
1393
1394         elm_object_part_content_set(obj, "datefield", icon_obj);
1395
1396         if (is_hour24)
1397                 elm_datetime_format_set(icon_obj, "%b %d , %Y %H : %M");
1398         else
1399                 elm_datetime_format_set(icon_obj, "%b %d , %Y %I : %M %p");
1400
1401         if (!allday)
1402         {
1403                 elm_datetime_field_visible_set(icon_obj, ELM_DATETIME_HOUR, EINA_TRUE);
1404                 elm_datetime_field_visible_set(icon_obj, ELM_DATETIME_MINUTE, EINA_TRUE);
1405                 if (is_hour24)
1406                         elm_datetime_field_visible_set(icon_obj, ELM_DATETIME_AMPM, EINA_FALSE);
1407                 else
1408                         elm_datetime_field_visible_set(icon_obj, ELM_DATETIME_AMPM, EINA_TRUE);
1409         }
1410         else
1411         {
1412                 elm_datetime_field_visible_set(icon_obj, ELM_DATETIME_HOUR, EINA_FALSE);
1413                 elm_datetime_field_visible_set(icon_obj, ELM_DATETIME_MINUTE, EINA_FALSE);
1414                 elm_datetime_field_visible_set(icon_obj, ELM_DATETIME_AMPM, EINA_FALSE);
1415         }
1416
1417         elm_datetime_field_limit_set(icon_obj, ELM_DATETIME_YEAR, 70, 136);
1418
1419         return icon_obj;
1420 }
1421
1422 static Eina_Bool __cal_edit_ug_check_date_validity(void *data)
1423 {
1424         c_retv_if(!data, EINA_FALSE);
1425         cal_edit_ug_data* p = data;
1426
1427         time_t start_t = mktime(&p->stm);
1428         time_t end_t = mktime(&p->etm);
1429
1430         if(start_t > end_t)
1431                 return EINA_FALSE;
1432
1433         return EINA_TRUE;
1434 }
1435
1436 static void __cal_edit_ug_start_date_changed_callback(void *data, Evas_Object* obj, void *ei)
1437 {
1438         c_retm_if(data == NULL, "data is null");
1439         cal_edit_ug_data *p = data;
1440
1441         struct tm value;
1442         time_t t = time(NULL);
1443         localtime_r(&t, &value); // to fill hidden field
1444
1445         elm_datetime_value_get(obj, &value);
1446
1447         // Remember the time difference in before updating start time
1448         time_t start_t = mktime(&p->stm);
1449         time_t end_t = mktime(&p->etm);
1450         time_t diff = end_t - start_t;
1451
1452         // Update start time variable
1453         p->stm = value;
1454
1455         Eina_Bool result =__cal_edit_ug_check_date_validity(p);
1456         if(!result)     {
1457                 // Calculate end time from start time to retain the time difference
1458                 end_t = mktime(&p->stm) + diff;
1459                 localtime_r(&end_t, &p->etm);
1460
1461                 // Update end time on the screen
1462                 Evas_Object *end_date_obj = elm_object_part_content_get(p->end_date, "elm.icon");
1463                 elm_datetime_value_set(end_date_obj, &p->etm);
1464         }
1465
1466         __cal_edit_ug_set_record_time(p, p->event);
1467 }
1468
1469 static void __cal_edit_ug_end_date_changed_callback(void *data, Evas_Object* obj, void *ei)
1470 {
1471         c_retm_if(!data, "data is null");
1472         cal_edit_ug_data *p = data;
1473
1474         struct tm value;
1475         time_t t = time(NULL);
1476         localtime_r(&t, &value); // to fill hidden field
1477
1478         elm_datetime_value_get(obj, &value);
1479
1480         p->etm = value;
1481
1482         Eina_Bool result = __cal_edit_ug_check_date_validity(p);
1483         if(!result)     {
1484                 int r = status_message_post(C_("IDS_CLD_POP_END_TIME_SHOULD_BE_SET_AFTER_START_TIME"));
1485                 c_warn_if(r != STATUS_ERROR_NONE, "status_message_post() is failed(%d)", r);
1486         } else
1487                 __cal_edit_ug_set_record_time(p, p->event);
1488
1489 }
1490
1491 static void __cal_edit_ug_due_date_changed_callback(void *data, Evas_Object* obj, void *ei)
1492 {
1493         c_retm_if(!data, "data is null");
1494         cal_edit_ug_data *p = data;
1495
1496         struct tm value;
1497         time_t t = time(NULL);
1498         localtime_r(&t, &value); // to fill hidden field
1499
1500         elm_datetime_value_get(obj, &value);
1501
1502         p->etm = value;
1503
1504         __cal_edit_ug_set_record_due_time(p, p->todo);
1505
1506 }
1507
1508 static void __cal_edit_ug_update_datetime_region(Evas_Object *item, struct tm *tm, Eina_Bool is_allday, Evas_Smart_Cb callback, void *callback_data)
1509 {
1510         c_ret_if(!item);
1511
1512         Evas_Object *datetime = NULL;
1513
1514         datetime = elm_object_part_content_unset(item, "elm.icon");
1515         if (datetime)
1516                 evas_object_del(datetime);
1517
1518         datetime = __cal_edit_ug_edit_time_icon(item, tm, is_allday);
1519         c_ret_if(!datetime);
1520
1521         elm_object_part_content_set(item, "elm.icon", datetime);
1522
1523         evas_object_smart_callback_add(datetime, "changed", callback, callback_data);
1524 }
1525
1526 static Evas_Object *__cal_edit_ug_insert_timepicker_after(cal_edit_ug_data* p, struct tm* value, Eina_Bool allday, Evas_Object *after, Evas_Smart_Cb callback)
1527 {
1528         c_retv_if(!p, NULL);
1529         c_retv_if(!after, NULL);
1530
1531         Evas_Object *item = cal_dialogue_insert_item_after(p->dialogue, "1icon", after, NULL, NULL);
1532         c_retvm_if(!item, NULL, "cal_dialogue_append_item() is failed");
1533
1534         __cal_edit_ug_update_datetime_region(item, value, allday, callback, p);
1535
1536         return item;
1537 }
1538
1539 static void __cal_edit_ug_all_day_update(Evas_Object* date_obj, Eina_Bool allday)
1540 {
1541         Evas_Object *edit_time_obj = elm_object_part_content_get(date_obj, "elm.icon");
1542
1543         if (!allday) {
1544                 elm_datetime_field_visible_set(edit_time_obj, ELM_DATETIME_HOUR, EINA_TRUE);
1545                 elm_datetime_field_visible_set(edit_time_obj, ELM_DATETIME_MINUTE, EINA_TRUE);
1546                 if (!is_hour24)
1547                         elm_datetime_field_visible_set(edit_time_obj, ELM_DATETIME_AMPM, EINA_TRUE);
1548         } else {
1549                 elm_datetime_field_visible_set(edit_time_obj, ELM_DATETIME_HOUR, EINA_FALSE);
1550                 elm_datetime_field_visible_set(edit_time_obj, ELM_DATETIME_MINUTE, EINA_FALSE);
1551                 elm_datetime_field_visible_set(edit_time_obj, ELM_DATETIME_AMPM, EINA_FALSE);
1552         }
1553 }
1554
1555 static void __cal_edit_ug_set_record_allday(calendar_record_h record, Eina_Bool is_allday, cal_edit_ug_data *p)
1556 {
1557         c_ret_if(!record);
1558         c_ret_if(!p);
1559
1560         if (is_allday == _calendar_is_allday_record(record))
1561                 return;
1562
1563         calendar_time_s start_time = {0};
1564
1565         _calendar_get_start_time(record, &start_time);
1566
1567         calendar_time_s end_time = {0};
1568
1569         _calendar_get_end_time(record, &end_time);
1570
1571         struct tm start_tm = {0};
1572         struct tm end_tm = {0};
1573
1574         if (is_allday) {
1575
1576                 cal_util_convert_lli_to_tm(NULL, start_time.time.utime, &start_tm);
1577                 cal_util_convert_lli_to_tm(NULL, end_time.time.utime, &end_tm);
1578
1579                 start_time.type = CALENDAR_TIME_LOCALTIME;
1580                 end_time.type = CALENDAR_TIME_LOCALTIME;
1581
1582                 start_time.time.date.year = start_tm.tm_year + 1900;
1583                 start_time.time.date.month = start_tm.tm_mon + 1;
1584                 start_time.time.date.mday = start_tm.tm_mday;
1585
1586                 end_time.time.date.year = end_tm.tm_year + 1900;
1587                 end_time.time.date.month = end_tm.tm_mon + 1;
1588                 end_time.time.date.mday = end_tm.tm_mday;
1589
1590         } else {
1591
1592                 start_time.type = CALENDAR_TIME_UTIME;
1593                 end_time.type = CALENDAR_TIME_UTIME;
1594
1595                 cal_util_convert_tm_to_lli(NULL, &p->stm, &start_time.time.utime);
1596                 cal_util_convert_tm_to_lli(NULL, &p->etm, &end_time.time.utime);
1597         }
1598
1599         _calendar_set_start_time(record, &start_time);
1600
1601         _calendar_set_end_time(record, &end_time);
1602 }
1603
1604 static void __cal_edit_ug_all_day_clicked_callback(Evas_Object *obj, void *data)
1605 {
1606         CAL_FN_START;
1607
1608         c_retm_if(!data, "data is null");
1609         cal_edit_ug_data *p = data;
1610
1611         Evas_Object *all_obj = elm_object_part_content_get(p->allday, "elm.icon");
1612         c_ret_if(!all_obj);
1613
1614         Eina_Bool is_allday = !elm_check_state_get(all_obj);
1615
1616         elm_check_state_set(all_obj, is_allday);
1617
1618         __cal_edit_ug_all_day_update(p->start_date, is_allday);
1619         __cal_edit_ug_all_day_update(p->end_date, is_allday);
1620
1621         __cal_edit_ug_set_record_allday(p->event, is_allday, p);
1622
1623 }
1624
1625 static void __cal_edit_ug_all_day_changed_callback(void *data, Evas_Object* obj, void *ei)
1626 {
1627         CAL_FN_START;
1628
1629         c_retm_if(!data, "data is null");
1630         c_retm_if(!obj, "obj is null");
1631
1632         cal_edit_ug_data *p = data;
1633
1634         Eina_Bool is_allday = elm_check_state_get(obj);
1635
1636         __cal_edit_ug_all_day_update(p->start_date, is_allday);
1637         __cal_edit_ug_all_day_update(p->end_date, is_allday);
1638
1639         __cal_edit_ug_set_record_allday(p->event, is_allday, p);
1640 }
1641
1642 static void __cal_edit_ug_update_allday_language(Evas_Object *item)
1643 {
1644         c_ret_if(!item);
1645
1646         elm_object_part_text_set(item, "elm.text", C_("IDS_COM_BODY_ALL_DAY"));
1647 }
1648
1649 static Evas_Object *__cal_edit_ug_add_all_day(cal_edit_ug_data* p, Eina_Bool allday)
1650 {
1651         c_retv_if(!p, NULL);
1652
1653         Evas_Object *item = cal_dialogue_insert_item_after(p->dialogue, "1text.1icon", p->timezone, __cal_edit_ug_all_day_clicked_callback, p);
1654         c_retvm_if(!item, NULL, "cal_dialogue_append_item() is failed");
1655
1656         Evas_Object *check_obj;
1657         check_obj = elm_check_add(item);
1658         elm_check_state_set(check_obj, allday);
1659         elm_object_style_set(check_obj, "on&off");
1660         evas_object_smart_callback_add(check_obj, "changed", __cal_edit_ug_all_day_changed_callback, p);
1661         elm_check_state_set(check_obj, allday);
1662         evas_object_propagate_events_set(check_obj, EINA_FALSE);
1663
1664         elm_object_part_content_set(item, "elm.icon", check_obj);
1665
1666         __cal_edit_ug_update_allday_language(item);
1667
1668         return item;
1669 }
1670
1671 static void __cal_edit_ug_worldclock_ug_destroy_callback(ui_gadget_h ug, void *priv)
1672 {
1673         CAL_FN_START;
1674
1675         cal_edit_ug_data* p = (cal_edit_ug_data*) priv;
1676
1677         if (p->ug_worldclock) {
1678                 ug_destroy(p->ug_worldclock);
1679                 p->ug_worldclock= NULL;
1680         }
1681
1682         Elm_Object_Item* navi_item = elm_naviframe_top_item_get(p->naviframe);
1683         Evas_Object *back_btn = elm_object_item_part_content_get(navi_item, "prev_btn");
1684         if (back_btn != NULL) {
1685                 elm_object_style_set(back_btn, "naviframe/back_btn/default");   /* take into effect */
1686         }
1687 }
1688
1689 static void __cal_edit_ug_worldclock_ug_result_callback(ui_gadget_h ug, service_h result, void *priv)
1690 {
1691         CAL_FN_START;
1692
1693         c_retm_if(!priv, "priv is null");
1694         c_retm_if(!result, "result is null");
1695
1696         cal_edit_ug_data *p = priv;
1697
1698         if (p->timezone_city) {
1699                 free(p->timezone_city);
1700                 p->timezone_city = NULL;
1701         }
1702
1703         if (p->timezone_path) {
1704                 free(p->timezone_path);
1705                 p->timezone_path = NULL;
1706         }
1707
1708         if (p->timezone_offset) {
1709                 free(p->timezone_offset);
1710                 p->timezone_offset = NULL;
1711         }
1712
1713         int r = service_get_extra_data(result, "tzpath", &p->timezone_path);
1714         c_warn_if(r != SERVICE_ERROR_NONE, "service_get_extra_data(tzpath) is failed(%x)", r);
1715
1716         r = service_get_extra_data(result, "city", &p->timezone_city);
1717         c_warn_if(r != SERVICE_ERROR_NONE, "service_get_extra_data(city) is failed(%x)", r);
1718
1719         __cal_edit_ug_set_timezone(p->record, p->timezone_path, p->timezone_city);
1720
1721         cal_util_get_timezone(&p->timezone_path, &p->timezone_city, &p->timezone_offset);
1722
1723         p->is_selected_timezone = EINA_TRUE;
1724
1725         char* timezone_text = NULL;
1726         timezone_text = g_strdup_printf("%s, %s", S_(p->timezone_city), p->timezone_offset);
1727
1728         elm_object_part_text_set(p->timezone, "elm.text.1", timezone_text);
1729
1730         CAL_FREE(timezone_text);
1731 }
1732
1733 static void __cal_edit_ug_worldclock_ug_layout_callback(ui_gadget_h ug, enum ug_mode mode, void *priv)
1734 {
1735         CAL_FN_START;
1736
1737         c_retm_if(!ug, "ug is null");
1738
1739         Evas_Object *base = ug_get_layout(ug);
1740         if (!base) {
1741                 ug_destroy(ug);
1742                 return;
1743         }
1744
1745         switch (mode)
1746         {
1747         case UG_MODE_FULLVIEW:
1748                 evas_object_size_hint_weight_set(base, EVAS_HINT_EXPAND,
1749                                 EVAS_HINT_EXPAND);
1750                 evas_object_show(base);
1751                 break;
1752         default:
1753                 DBG("Unsupported ug layout");
1754                 break;
1755         }
1756
1757 }
1758
1759 static void __cal_edit_ug_timezone_select_callback(Evas_Object *obj, void *data)
1760 {
1761         c_retm_if(!data, "data is null");
1762
1763         cal_edit_ug_data *p = data;
1764         struct ug_cbs uc;
1765         ui_gadget_h ug;
1766         memset(&uc, 0, sizeof(struct ug_cbs));
1767         uc.destroy_cb = __cal_edit_ug_worldclock_ug_destroy_callback;
1768         uc.layout_cb = __cal_edit_ug_worldclock_ug_layout_callback;
1769         uc.result_cb = __cal_edit_ug_worldclock_ug_result_callback;
1770         uc.priv = p;
1771
1772         ug = ug_create(NULL, "worldclock-efl", UG_MODE_FULLVIEW, NULL, &uc);
1773         p->ug_worldclock = ug;
1774 }
1775
1776 static void __cal_edit_ug_update_time_zone_language(Evas_Object *item, const char *timezone_city, const char *timezone_offset)
1777 {
1778         elm_object_part_text_set(item, "elm.text.2", C_("IDS_CLD_BODY_TIME_ZONE"));
1779
1780         char buffer[128] = {0};
1781
1782         snprintf(buffer, sizeof(buffer), "%s, %s", S_(timezone_city), timezone_offset);
1783
1784         elm_object_part_text_set(item, "elm.text.1", buffer);
1785 }
1786
1787 static Evas_Object *__cal_edit_ug_add_time_zone(cal_edit_ug_data* p)
1788 {
1789         c_retv_if(!p, NULL);
1790
1791         Evas_Object *item = __cal_edit_ug_insert_padding_after(p, NULL, p->end_date);
1792         c_retvm_if(!item, NULL, "__cal_edit_ug_insert_padding_after() is failed");
1793
1794         item = cal_dialogue_insert_item_after(p->dialogue, "2text.2", item, __cal_edit_ug_timezone_select_callback, p);
1795         c_retvm_if(!item, NULL, "cal_dialogue_append_item() is failed");
1796
1797         __cal_edit_ug_get_record_time_zone(p, p->event);
1798
1799         __cal_edit_ug_update_time_zone_language(item, p->timezone_city, p->timezone_offset);
1800
1801         return item;
1802 }
1803
1804 static void __cal_edit_ug_alarm_changed_callback(const Cal_Reminder* reminder, void* data)
1805 {
1806         CAL_FN_START;
1807
1808         c_retm_if(!data, "data is null");
1809
1810         Evas_Object *obj = data;
1811
1812         char* alarm_text = NULL;
1813         char buf[128] = {0};
1814         cal_reminder_get_string(reminder, buf, sizeof(buf));
1815         alarm_text = strdup(buf);
1816
1817         elm_object_part_text_set(obj, "elm.text.1", alarm_text);
1818         CAL_FREE(alarm_text);
1819
1820         Cal_Reminder* reminder_temp = evas_object_data_get(obj, "data");
1821         reminder_temp->alarm_value = reminder->alarm_value;
1822         reminder_temp->datetime = reminder->datetime;
1823         reminder_temp->tick_unit = reminder->tick_unit;
1824 }
1825
1826 static void __cal_edit_ug_alarm_select_callback(Evas_Object *obj, void *data)
1827 {
1828         CAL_FN_START;
1829
1830         c_retm_if(!data, "data is null");
1831
1832         cal_edit_ug_data* p = data;
1833         Cal_Reminder* reminder = (Cal_Reminder*)evas_object_data_get(obj, "data");
1834         cal_edit_alarm_type alarm_type = ALARM_TYPE_EVENT;
1835
1836         if(p->item_type == ITEM_TYPE_EVENT)
1837                 alarm_type = ALARM_TYPE_EVENT;
1838         else if(p->item_type == ITEM_TYPE_TODO && p->is_no_due_date)
1839                 alarm_type = ALARM_TYPE_TODO_CUSTOM_ONLY;
1840         else
1841                 alarm_type = ALARM_TYPE_TODO;
1842
1843         cal_edit_alarm_create_view(p->naviframe, reminder, alarm_type, __cal_edit_ug_alarm_changed_callback, obj);
1844
1845 }
1846
1847 static Evas_Object *__cal_edit_ug_add_alarm(cal_edit_ug_data* p, Cal_Reminder* reminder, Eina_Bool is_add_mode, Evas_Object *after);
1848
1849 static void __cal_edit_ug_delete_reminder_callback(void *data, Evas *e, Evas_Object *obj, void *ei)
1850 {
1851         CAL_FN_START;
1852
1853         c_ret_if(!data);
1854
1855         free(data);
1856 }
1857
1858
1859 static void __cal_edit_ug_alarm_plus_button_callback(void *data, Evas_Object *obj, void *event_info)
1860 {
1861         CAL_FN_START;
1862
1863         c_retm_if(!data, "data is null");
1864         c_retm_if(!obj, "obj is null");
1865
1866         cal_edit_ug_data* p = data;
1867
1868         if (eina_list_count(p->alarm_item_list)>=4) {
1869                 evas_object_del(p->alarm_plus_button);
1870                 p->alarm_plus_button = NULL;
1871         }
1872
1873         Cal_Reminder* reminder = calloc(1, sizeof(Cal_Reminder));
1874         c_retm_if(!reminder, "reminder is null");
1875         reminder->tick_unit = CALENDAR_ALARM_TIME_UNIT_MINUTE;
1876         reminder->datetime = p->stm;
1877
1878         Eina_List *list = eina_list_last(p->alarm_item_list);
1879         Evas_Object *last_item = eina_list_data_get(list);
1880          __cal_edit_ug_add_alarm(p, reminder, EINA_FALSE, last_item);
1881
1882 }
1883
1884 static Evas_Object * __cal_edit_ug_alarm_plus_button_icon(cal_edit_ug_data* p, Evas_Object *obj)
1885 {
1886         c_retv_if(!p, NULL);
1887         c_retv_if(!obj, NULL);
1888
1889         Evas_Object *button = NULL;
1890
1891         button = elm_button_add(obj);
1892         c_retvm_if(!button, NULL, "elm_button_add() is failed");
1893
1894         elm_object_style_set(button, "icon_plus");
1895         evas_object_propagate_events_set(button, EINA_FALSE);
1896         evas_object_smart_callback_add(button, "clicked", __cal_edit_ug_alarm_plus_button_callback, p);
1897         evas_object_show(button);
1898         p->alarm_plus_button = button;
1899
1900         return button;
1901 }
1902
1903 static void __cal_edit_ug_alarm_minus_button_callback(void *data, Evas_Object *obj, void *event_info)
1904 {
1905         CAL_FN_START;
1906
1907         c_retm_if(!data, "data is null");
1908         c_retm_if(!obj, "obj is null");
1909
1910         cal_edit_ug_data* p = data;
1911
1912         if (!p->alarm_plus_button) {
1913                 Evas_Object *first_item = eina_list_nth(p->alarm_item_list, 0);
1914                 p->alarm_plus_button = __cal_edit_ug_alarm_plus_button_icon(p, first_item);
1915                 elm_object_part_content_set(first_item, "elm.icon", p->alarm_plus_button);
1916         }
1917
1918         evas_object_smart_callback_del(obj, "clicked", __cal_edit_ug_alarm_minus_button_callback);
1919
1920         Evas_Object *parent_item = evas_object_data_get(obj, "button");
1921         p->alarm_item_list = eina_list_remove(p->alarm_item_list, parent_item);
1922
1923         cal_dialogue_delete_item(p->dialogue, parent_item);
1924         evas_object_del(obj);
1925
1926
1927 }
1928
1929
1930 static Evas_Object * __cal_edit_ug_alarm_minus_button_icon(cal_edit_ug_data* p, Evas_Object *obj)
1931 {
1932         c_retv_if(!p, NULL);
1933         c_retv_if(!obj, NULL);
1934
1935         Evas_Object *button = NULL;
1936
1937         button = elm_button_add(obj);
1938         c_retvm_if(!button, NULL, "elm_button_add() is failed");
1939
1940         elm_object_style_set(button, "icon_minus");
1941         evas_object_data_set(button, "button", obj);
1942         evas_object_propagate_events_set(button, EINA_FALSE);
1943         evas_object_smart_callback_add(button, "clicked", __cal_edit_ug_alarm_minus_button_callback, p);
1944         evas_object_show(button);
1945
1946         return button;
1947 }
1948
1949 static void __cal_edit_ug_update_reminder_language(Eina_List *item_list)
1950 {
1951         c_ret_if(!item_list);
1952
1953         Eina_List *cursor = NULL;
1954         Evas_Object *item = NULL;
1955
1956         EINA_LIST_FOREACH(item_list, cursor, item) {
1957                 if (!item)
1958                         continue;
1959
1960                 elm_object_part_text_set(item, "elm.text.2", C_("IDS_CLD_BODY_REMINDER"));
1961
1962                 Cal_Reminder *reminder = NULL;
1963
1964                 reminder = evas_object_data_get(item, "data");
1965
1966                 char buffer[128] = {0};
1967                 cal_reminder_get_string(reminder, buffer, sizeof(buffer));
1968
1969                 elm_object_part_text_set(item, "elm.text.1", buffer);
1970         }
1971 }
1972
1973 static Evas_Object *__cal_edit_ug_add_alarm(cal_edit_ug_data* p, Cal_Reminder* reminder, Eina_Bool is_add_mode, Evas_Object *after)
1974 {
1975         c_retv_if(!p, NULL);
1976         Evas_Object *item = NULL;
1977         if(after) {
1978                 item = cal_dialogue_insert_item_after(p->dialogue, "2text.1icon.5", after,  __cal_edit_ug_alarm_select_callback, p);
1979                 c_retvm_if(!item, NULL, "cal_dialogue_insert_item_after() is failed");
1980         }
1981         else {
1982                 item = cal_dialogue_append_item(p->dialogue, "2text.1icon.5",  __cal_edit_ug_alarm_select_callback, p);
1983                 c_retvm_if(!item, NULL, "cal_dialogue_append_item() is failed");
1984         }
1985
1986         elm_object_part_text_set(item, "elm.text.2", C_("IDS_CLD_BODY_REMINDER"));
1987
1988         const char* alarm_text = NULL;
1989         char buf[128] = {0};
1990         cal_reminder_get_string(reminder, buf, sizeof(buf));
1991         alarm_text = strdup(buf);
1992
1993         elm_object_part_text_set(item, "elm.text.1", alarm_text);
1994         p->alarm_item_list = eina_list_append(p->alarm_item_list, (void*)item);
1995
1996         if(p->item_type == ITEM_TYPE_EVENT)     {
1997                 Evas_Object *button_obj = NULL;
1998                 if(is_add_mode)
1999                         button_obj = __cal_edit_ug_alarm_plus_button_icon(p, item);
2000                 else
2001                         button_obj = __cal_edit_ug_alarm_minus_button_icon(p, item);
2002
2003                 elm_object_part_content_set(item, "elm.icon", button_obj);
2004         }
2005
2006         evas_object_data_set(item, "data", reminder);
2007         evas_object_event_callback_add(item, EVAS_CALLBACK_DEL, __cal_edit_ug_delete_reminder_callback, reminder);
2008
2009         return item;
2010 }
2011
2012 static void __cal_edit_ug_add_multi_alarm(cal_edit_ug_data* p)
2013 {
2014         c_retm_if(!p, "p is null");
2015
2016         Cal_Reminder* reminder = NULL;
2017         reminder = calloc(1, sizeof(Cal_Reminder));
2018         c_retm_if(!reminder, "reminder is null");
2019
2020         reminder->tick_unit = CALENDAR_ALARM_TIME_UNIT_MINUTE;
2021         reminder->datetime = p->stm;
2022         Evas_Object *after = NULL;
2023         Evas_Object *item = NULL;
2024
2025         if(p->is_edit_mode)     {
2026                 if (p->item_type == ITEM_TYPE_EVENT)
2027                         __cal_edit_ug_get_record_alarm(p, p->event);
2028                 else
2029                         __cal_edit_ug_get_record_alarm(p, p->todo);
2030
2031                 if(!p->alarm_list) {
2032                         reminder->tick_unit = CALENDAR_ALARM_NONE;\
2033                 }
2034         }
2035
2036         GList *temp = p->alarm_list;
2037         if (!temp) {
2038                  __cal_edit_ug_add_alarm(p, reminder, EINA_TRUE, NULL);
2039         }
2040         else {
2041                 free(reminder);
2042
2043                 while (temp) {
2044                         if(!after)      {
2045                                 item = __cal_edit_ug_add_alarm( p, (Cal_Reminder*)temp->data, EINA_TRUE, after);
2046                         }
2047                         else    {
2048                                 item = __cal_edit_ug_add_alarm( p, (Cal_Reminder*)temp->data, EINA_FALSE, after);
2049                         }
2050                         c_retm_if(!item, "item is null");
2051                         after = item;
2052                         temp = g_list_next(temp);
2053                 }
2054         }
2055 }
2056
2057 static void __cal_edit_ug_reponse_event_callback(void *data, Evas_Object *obj, void *ei)
2058 {
2059         c_retm_if(!data, "data is null");
2060
2061         Evas_Object *popup = data;
2062         evas_object_del(popup);
2063 }
2064
2065 static void __cal_edit_ug_repeat_callback(void *data)
2066 {
2067         c_retm_if(!data, "data is null");
2068
2069         cal_edit_ug_data *p = data;
2070
2071         calendar_range_type_e range = CALENDAR_RANGE_NONE;
2072         struct tm until = {0};
2073         int count = 0;
2074
2075         int repeat = cal_edit_repeat_get_repeat(p->repeat_layout, &range, &until, &count);
2076
2077         if (CALENDAR_RECURRENCE_NONE != repeat) {
2078
2079                 if (range == CALENDAR_RANGE_UNTIL) {
2080
2081                         if (timegm(&until) <= timegm(&p->stm)) {
2082                                 cal_util_add_popup(p->window, NULL, S_("IDS_COM_POP_WARNING"), C_("IDS_CLD_BODY_REPEAT_WARNING"),
2083                                         __cal_edit_ug_reponse_event_callback, NULL, NULL);
2084                                 return;
2085                         }
2086                 }
2087                 else if (range == CALENDAR_RANGE_COUNT) {
2088
2089                         if (!count) {
2090                                 cal_util_add_popup(p->window, NULL, S_("IDS_COM_POP_WARNING"), C_("IDS_CLD_BODY_REPEAT_WARNING"),
2091                                         __cal_edit_ug_reponse_event_callback, NULL, NULL);
2092                                 return;
2093                         }
2094                 }
2095         }
2096
2097         p->repeat_freq = repeat;
2098         p->repeat_range = range;
2099         p->repeat_until = until;
2100         p->repeat_count = count;
2101
2102         char buf[1024] = {0};
2103         _cal_edit_repeat_get_freq_str(p->repeat_freq, buf, sizeof(buf));
2104         elm_object_part_text_set(p->repeat, "elm.text.1", buf);
2105
2106         __cal_edit_ug_set_record_repeat(p);
2107
2108 }
2109
2110 static void __cal_edit_ug_repeat_select_callback(Evas_Object *obj, void *data)
2111 {
2112         c_retm_if(!data, "data is null");
2113
2114         cal_edit_ug_data* p = data;
2115
2116         p->repeat_layout = cal_edit_repeat_create_view(p->naviframe, p->repeat_freq, p->repeat_range, &(p->stm), &(p->repeat_until),&(p->repeat_count),
2117                         (cal_edit_repeat_destroy_callback)__cal_edit_ug_repeat_callback, p);
2118         c_retm_if(!p->repeat_layout, "p->repeat_layout is null");
2119
2120 }
2121
2122 static void __cal_edit_ug_update_recurrence_language(Evas_Object *item, int repeat_frequency)
2123 {
2124         c_ret_if(!item);
2125
2126         elm_object_part_text_set(item, "elm.text.2", C_("IDS_CLD_BODY_REPEAT"));
2127
2128         char buffer[128] = {0};
2129
2130         _cal_edit_repeat_get_freq_str(repeat_frequency, buffer, sizeof(buffer));
2131
2132         elm_object_part_text_set(item, "elm.text.1", buffer);
2133 }
2134
2135 static Evas_Object *__cal_edit_ug_add_repeat(cal_edit_ug_data* p)
2136 {
2137         c_retv_if(!p, NULL);
2138
2139         Eina_List *list = eina_list_last(p->alarm_item_list);
2140         Evas_Object *last_item = eina_list_data_get(list);
2141
2142         Evas_Object *item = NULL;
2143
2144         if (p->instance) {
2145
2146                 item = cal_dialogue_insert_item_after(p->dialogue, "2text.2", last_item, NULL, NULL);
2147                 elm_object_signal_emit(item, "elm,state,disabled", "elm");
2148
2149         } else
2150                 item = cal_dialogue_insert_item_after(p->dialogue, "2text.2", last_item, __cal_edit_ug_repeat_select_callback, p);
2151
2152         c_retvm_if(!item, NULL, "cal_dialogue_append_item() is failed");
2153
2154         __cal_edit_ug_get_record_repeat(p, p->event);
2155
2156         __cal_edit_ug_update_recurrence_language(item, p->repeat_freq);
2157
2158         return item;
2159 }
2160
2161 static void  __cal_edit_ug_popup_hide_callback(void *data, Evas_Object *obj, void *event_info)
2162 {
2163         c_retm_if(!data, "data is null");
2164         c_retm_if(!obj, "obj is null");
2165
2166         Evas_Object *popup = data;
2167
2168         cal_edit_ug_data *p = evas_object_data_get(popup, "data");
2169
2170         evas_object_del(popup);
2171
2172         c_retm_if(!p, "p is null");
2173
2174         p->popup = NULL;
2175 }
2176
2177 static char *__cal_edit_ug_get_genlist_save_to_item_label(void *data, Evas_Object *obj, const char *part)
2178 {
2179         c_retvm_if(!data, NULL, "data is null");
2180         c_retvm_if(!part, NULL, "part is null");
2181
2182         calendar_record_h record = (calendar_record_h)data;
2183         char *name;
2184         int cid;
2185         char* description;
2186
2187         calendar_error_e error = CALENDAR_ERROR_NONE;
2188
2189         calendar_record_h calendar_book = NULL;
2190
2191         _calendar_record_type record_type = _calendar_get_record_type(record);
2192
2193         if (record_type == _CALENDAR_RECORD_TYPE_CALENDARBOOK)
2194                 calendar_book = record;
2195         else {
2196                 error = calendar_record_get_int(record, _calendar_event.calendar_book_id, &cid);
2197                 c_retvm_if(error != CALENDAR_ERROR_NONE, NULL, "calendar_record_get_int() is failed(%x)", error);
2198
2199                 error = calendar_db_get_record(_calendar_book._uri, cid, &calendar_book);
2200                 c_retvm_if(error != CALENDAR_ERROR_NONE, NULL, "calendar_db_get_record() is failed(%x)", error);
2201         }
2202
2203         error = calendar_record_get_int(record, _calendar_book.id, &cid);
2204         c_retvm_if(error != CALENDAR_ERROR_NONE, NULL, "calendar_record_get_int() is failed(%x)", error);
2205
2206         if (!CAL_STRCMP(part,"elm.text.1")) {
2207
2208                 if (cid == DEFAULT_EVENT_CALENDAR_BOOK_ID)
2209                         return strdup(C_("IDS_CLD_BODY_MY_CALENDAR"));
2210                 else if (cid == DEFAULT_TODO_CALENDAR_BOOK_ID)
2211                         return strdup(C_("IDS_TASK_BODY_MY_TASK"));
2212
2213                 error = calendar_record_get_str(calendar_book, _calendar_book.name, &name);
2214                 c_retvm_if(error != CALENDAR_ERROR_NONE, NULL, "calendar_record_get_str() is failed(%x)", error);
2215
2216                 if (record != calendar_book) {
2217                         error = calendar_record_destroy(calendar_book, true);
2218                         c_retvm_if(error != CALENDAR_ERROR_NONE, NULL, "calendar_record_destroy() is failed(%x)", error);
2219                 }
2220
2221                 if (CAL_STRLEN(name))
2222                         return name;
2223                 else
2224                         CAL_FREE(name);
2225
2226                 return strdup(C_("IDS_CLD_BODY_NO_TITLE"));
2227
2228         } else if (!CAL_STRCMP(part,"elm.text.2")) {
2229
2230                 if (cid == DEFAULT_EVENT_CALENDAR_BOOK_ID || cid == DEFAULT_TODO_CALENDAR_BOOK_ID)
2231                         return strdup(C_("IDS_CLD_POP_MY_PHONE"));
2232
2233                 error = calendar_record_get_str(calendar_book, _calendar_book.description, &description);
2234                 c_retvm_if(error != CALENDAR_ERROR_NONE, NULL, "calendar_record_get_str() is failed(%x)", error);
2235
2236                 if (record != calendar_book) {
2237                         error = calendar_record_destroy(calendar_book, true);
2238                         c_retvm_if(error != CALENDAR_ERROR_NONE, NULL, "calendar_record_destroy() is failed(%x)", error);
2239                 }
2240
2241                 if (CAL_STRLEN(description))
2242                         return description;
2243                 else
2244                         CAL_FREE(description);
2245
2246                 return strdup(S_("IDS_COM_POP_NO_ACCOUNTS"));
2247         }
2248
2249         return strdup(C_("IDS_CLD_BODY_NO_TITLE"));
2250 }
2251
2252 static Evas_Object* __cal_edit_ug_add_save_to_icon(void *item_data, Evas_Object *obj, const char *part)
2253 {
2254         if (!item_data)
2255         {
2256                 ERR("item_data is null");
2257                 return NULL;
2258         }
2259
2260         Evas_Object *icon = NULL;
2261         char* icon_path = NULL;
2262         calendar_record_h record = (calendar_record_h)item_data;
2263         Eina_Bool r = EINA_FALSE;
2264         int cid = _calendar_get_calendar_index(record);
2265         int aid = _calendar_get_account_id(record);
2266
2267         if (!CAL_STRCMP(part, "elm.icon"))
2268         {
2269                 icon = elm_icon_add(obj);
2270
2271                 if (!icon)
2272                 {
2273                         ERR("elm_icon_add fail");
2274                         return NULL;
2275                 }
2276
2277                 if ( cid == DEFAULT_EVENT_CALENDAR_BOOK_ID || cid == DEFAULT_TODO_CALENDAR_BOOK_ID) {
2278                         r = elm_icon_file_set(icon, CAL_IMAGES_EDJ, CAL_IMAGES_PHONE);
2279                         c_warn_if(!r, "elm_icon_file_set(%s) is failed", CAL_IMAGES_PHONE);
2280                 } else {
2281                         icon_path = cal_account_svc_get_account_icon_path(aid);
2282                         if (icon_path) {
2283                                 r = elm_icon_file_set(icon, icon_path, NULL);
2284                                 c_warn_if(!r, "elm_icon_file_set(%s) is failed", CAL_IMAGES_PHONE);
2285                                 free(icon_path);
2286                         }
2287                         else {
2288                                 r = elm_icon_file_set(icon, CAL_IMAGES_EDJ, CAL_IMAGES_PHONE);
2289                                 c_warn_if(!r, "elm_icon_file_set(%s) is failed", CAL_IMAGES_PHONE);
2290                         }
2291
2292                 }
2293
2294                 if (!r) {
2295                         ERR("");
2296                         evas_object_del(icon);
2297                         icon = NULL;
2298                 }
2299                 else
2300                         evas_object_show(icon);
2301         }
2302
2303         return icon;
2304 }
2305
2306 static Eina_Bool __cal_edit_ug_popup_idler_callback(void *data)
2307 {
2308         c_retv_if(!data, ECORE_CALLBACK_CANCEL);
2309
2310         cal_edit_ug_data *p = data;
2311
2312         if (p->popup) {
2313                 evas_object_del(p->popup);
2314                 p->popup = NULL;
2315         }
2316
2317         p->idler = NULL;
2318
2319         return ECORE_CALLBACK_CANCEL;
2320 }
2321
2322 static void __cal_edit_ug_save_to_popup_list_sel(void *data, Evas_Object *obj, void *event_info)
2323 {
2324         CAL_FN_START;
2325
2326         c_retm_if(!data, "data is null");
2327         c_retm_if(!obj, "obj is null");
2328
2329         calendar_record_h calendar_book = data;
2330         int calendar_book_id = 0;
2331
2332         cal_edit_ug_data *p = CAL_UTIL_GET_PRIV_DATA(obj);
2333         c_retm_if(!p, "p is null");
2334
2335         Elm_Object_Item *item = elm_genlist_selected_item_get(obj);
2336         c_retm_if(!item, "elm_genlist_selected_item_get() returned null");
2337
2338         elm_genlist_item_selected_set(item, EINA_FALSE);
2339
2340         calendar_error_e error = CALENDAR_ERROR_NONE;
2341
2342         error = calendar_record_get_int(calendar_book, _calendar_book.id, &calendar_book_id);
2343         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_int() is failed(%x)", error);
2344
2345         if (p->item_type == ITEM_TYPE_TODO)     {
2346                 __cal_edit_ug_set_record_save_to(p->todo, calendar_book_id);
2347                 __cal_edit_ug_update_save_to_item(p, p->todo);
2348         }
2349         else    {
2350                 __cal_edit_ug_set_record_save_to(p->event, calendar_book_id);
2351                 __cal_edit_ug_update_save_to_item(p, p->event);
2352         }
2353
2354         if (p->idler) {
2355                 ecore_idler_del(p->idler);
2356                 p->idler = NULL;
2357         }
2358
2359         p->idler = ecore_idler_add(__cal_edit_ug_popup_idler_callback, p);
2360         c_ret_if(!p->idler);
2361
2362 }
2363
2364 static Elm_Genlist_Item_Class itc_2text_1icon_2 = {
2365         .item_style = "dialogue/2text.1icon.2",
2366         .func.text_get = __cal_edit_ug_get_genlist_save_to_item_label,
2367         .func.content_get = __cal_edit_ug_add_save_to_icon,
2368 };
2369
2370 static Evas_Object* __cal_edit_ug_create_save_to_popup(cal_edit_ug_data* p)
2371 {
2372         c_retv_if(!p, NULL);
2373
2374         Evas_Object *popup = NULL;
2375         Evas_Object *genlist = NULL;
2376
2377         Eina_List *l;
2378
2379         calendar_error_e error = CALENDAR_ERROR_NONE;
2380
2381         if (!p->calendar_book_list)
2382         {
2383                 p->calendar_book_list = _calendar_get_calendar_book_list_with_account_id(_CALENDAR_ALL_ACCOUNT_ID);
2384         }
2385
2386         popup = cal_util_add_popup(p->window, "liststyle", C_("IDS_ST_BODY_SAVE_TO"), NULL,
2387                         __cal_edit_ug_popup_hide_callback, p, S_("IDS_COM_BODY_CLOSE"), NULL);
2388         c_retvm_if(!popup, NULL, "popup is null");
2389
2390         genlist = elm_genlist_add(popup);
2391         if (!genlist) {
2392                 ERR("elm_genlist_add(popup) returned null");
2393                 evas_object_del(popup);
2394                 return NULL;
2395         }
2396
2397         evas_object_size_hint_weight_set(genlist, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
2398         evas_object_size_hint_align_set(genlist, EVAS_HINT_FILL, EVAS_HINT_FILL);
2399         evas_object_data_set(genlist, "priv", p);
2400
2401         calendar_record_h calendar_book = NULL;
2402
2403         EINA_LIST_FOREACH(p->calendar_book_list, l, calendar_book) {
2404
2405                 int store_type = 0;
2406
2407                 error = calendar_record_get_int(calendar_book, _calendar_book.store_type, &store_type);
2408                 if (error != CALENDAR_ERROR_NONE) {
2409                         ERR("calendar_record_get_int() is failed(%x)", calendar_book);
2410                         continue;
2411                 }
2412
2413                 int sync_event = 0;
2414                 error = calendar_record_get_int(calendar_book, _calendar_book.sync_event, &sync_event);
2415                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_int() is failed(%x)", error);
2416                 if(!sync_event)
2417                         continue;
2418
2419                 if (p->item_type == ITEM_TYPE_TODO) {
2420                         if (store_type == CALENDAR_BOOK_TYPE_TODO)
2421                                 elm_genlist_item_append(genlist, &itc_2text_1icon_2, calendar_book, NULL, ELM_GENLIST_ITEM_NONE, __cal_edit_ug_save_to_popup_list_sel, calendar_book);
2422                 } else {
2423                         if (store_type == CALENDAR_BOOK_TYPE_EVENT)
2424                                 elm_genlist_item_append(genlist, &itc_2text_1icon_2, calendar_book, NULL, ELM_GENLIST_ITEM_NONE, __cal_edit_ug_save_to_popup_list_sel, calendar_book);
2425                 }
2426
2427         }
2428
2429         elm_object_content_set(popup, genlist);
2430         evas_object_show(genlist);
2431
2432         return popup;
2433 }
2434
2435 static void __cal_edit_ug_saveto_select_callback(Evas_Object *obj, void *data)
2436 {
2437         c_retm_if(!data, "data is null");
2438
2439         cal_edit_ug_data* p = data;
2440         Evas_Object *popup = __cal_edit_ug_create_save_to_popup(p);
2441         c_retm_if(!popup, "popup is NULL");
2442
2443         p->popup = popup;
2444 }
2445
2446 static void __cal_edit_ug_get_save_to_str(int calid, char *buf, int sz)
2447 {
2448         calendar_record_h calendar_book = NULL;
2449         char *str;
2450
2451         calendar_error_e error = CALENDAR_ERROR_NONE;
2452
2453         error = calendar_db_get_record(_calendar_book._uri, calid, &calendar_book);
2454         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_db_get_record() is failed(%x)", error);
2455
2456         if (calid == DEFAULT_EVENT_CALENDAR_BOOK_ID)
2457                 str = C_("IDS_CLD_OPT_PHONE_CALENDAR");
2458         else
2459         {
2460                 error = calendar_record_get_str(calendar_book, _calendar_book.name, &str);
2461                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_str() is failed(%x)", error);
2462
2463         }
2464
2465         snprintf(buf, sz, "%s", str);
2466
2467         if (str)
2468                 free(str);
2469
2470         error = calendar_record_destroy(calendar_book, true);
2471         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_destroy() is failed(%x)", error);
2472 }
2473
2474 static void __cal_edit_ug_update_saveto_language(Evas_Object *item, calendar_record_h record)
2475 {
2476         c_ret_if(!item);
2477
2478         elm_object_part_text_set(item, "elm.text.2", C_("IDS_ST_BODY_SAVE_TO"));
2479
2480         int calendar_book_id = _calendar_get_calendar_index(record);
2481
2482         if (calendar_book_id == DEFAULT_EVENT_CALENDAR_BOOK_ID)
2483                 elm_object_part_text_set(item, "elm.text.1", C_("IDS_CLD_BODY_MY_CALENDAR"));
2484         else if (calendar_book_id == DEFAULT_TODO_CALENDAR_BOOK_ID)
2485                 elm_object_part_text_set(item, "elm.text.1", C_("IDS_TASK_BODY_MY_TASK"));
2486 }
2487
2488 static Evas_Object *__cal_edit_ug_add_saveto(cal_edit_ug_data* p)
2489 {
2490         c_retv_if(!p, NULL);
2491
2492         Evas_Object *item = cal_dialogue_append_item(p->dialogue, "2text.2", __cal_edit_ug_saveto_select_callback, p);
2493         c_retvm_if(!item, NULL, "cal_dialogue_append_item() is failed");
2494
2495         elm_object_part_text_set(item, "elm.text.2", C_("IDS_ST_BODY_SAVE_TO"));
2496
2497         char buf[1024];
2498         char* saveto_text = NULL;
2499         int calendar_book_id = 0;
2500         if(p->item_type == ITEM_TYPE_TODO)      {
2501                 calendar_book_id = _calendar_get_calendar_index(p->todo);
2502                 p->account_id = _calendar_get_account_id(p->todo);
2503         }
2504         else    {
2505                 calendar_book_id = _calendar_get_calendar_index(p->event);
2506                 p->account_id = _calendar_get_account_id(p->event);
2507         }
2508         if (calendar_book_id == DEFAULT_EVENT_CALENDAR_BOOK_ID)
2509                 saveto_text =  strdup(C_("IDS_CLD_BODY_MY_CALENDAR"));
2510         else if (calendar_book_id == DEFAULT_TODO_CALENDAR_BOOK_ID)
2511                 saveto_text =  strdup(C_("IDS_TASK_BODY_MY_TASK"));
2512         else {
2513                 __cal_edit_ug_get_save_to_str(calendar_book_id, buf, sizeof(buf));
2514                 saveto_text = strdup(buf);
2515         }
2516
2517         elm_object_part_text_set(item, "elm.text.1", saveto_text);
2518         CAL_FREE(saveto_text);
2519
2520         if (p->instance)
2521                 elm_object_signal_emit(item, "elm,state,disabled", "elm");
2522
2523         return item;
2524 }
2525
2526 static void __cal_edit_ug_no_due_date_clicked_callback(Evas_Object *obj, void *data)
2527 {
2528         c_retm_if(!data, "data is NULL.");
2529         c_retm_if(!obj, "obj is NULL.");
2530
2531         cal_edit_ug_data *p = data;
2532
2533         Evas_Object *check_obj = elm_object_part_content_get(p->no_due_date, "elm.icon");
2534
2535         if(p->is_no_due_date)
2536         {
2537                 elm_check_state_set(check_obj, EINA_FALSE);
2538                 p->is_no_due_date = elm_check_state_get(check_obj);
2539                 elm_object_signal_emit(p->due_date, "elm,state,enabled", "elm");
2540         }
2541         else
2542         {
2543                 elm_check_state_set(check_obj, EINA_TRUE);
2544                 p->is_no_due_date = elm_check_state_get(check_obj);
2545                 __cal_edit_ug_set_record_due_time(p, p->todo);
2546                 elm_object_signal_emit(p->due_date, "elm,state,disabled", "elm");
2547         }
2548
2549         Evas_Object *edit_time_obj = elm_object_part_content_get(p->due_date, "elm.icon");
2550         elm_object_disabled_set(edit_time_obj, p->is_no_due_date);
2551 }
2552
2553 static void __cal_edit_ug_no_due_date_changed_callback(void *data, Evas_Object *obj, void *ei)
2554 {
2555         c_retm_if(!data, "data is NULL.");
2556         c_retm_if(!obj, "obj is NULL.");
2557
2558         cal_edit_ug_data *p = data;
2559
2560         p->is_no_due_date = elm_check_state_get(obj);
2561
2562         Evas_Object *edit_time_obj = elm_object_part_content_get(p->due_date, "elm.icon");
2563         elm_object_disabled_set(edit_time_obj, p->is_no_due_date);
2564         if(p->is_no_due_date)
2565         {
2566                 __cal_edit_ug_set_record_due_time(p, p->todo);
2567                 elm_object_signal_emit(p->due_date, "elm,state,disabled", "elm");
2568         }
2569         else
2570         {
2571                 elm_object_signal_emit(p->due_date, "elm,state,enabled", "elm");
2572         }
2573 }
2574
2575 static void __cal_edit_ug_update_no_due_date_language(Evas_Object *item)
2576 {
2577         elm_object_part_text_set(item, "elm.text", C_("IDS_TASK_MBODY_NO_DUE_DATE"));
2578 }
2579
2580 static Evas_Object *__cal_edit_ug_add_no_due_date(cal_edit_ug_data* p)
2581 {
2582         c_retv_if(!p, NULL);
2583
2584         Evas_Object *item = cal_dialogue_insert_item_after(p->dialogue, "1text.1icon.2", p->due_date, __cal_edit_ug_no_due_date_clicked_callback, p);
2585         c_retvm_if(!item, NULL, "cal_dialogue_append_item() is failed");
2586
2587         Evas_Object *check_obj;
2588         check_obj = elm_check_add(item);
2589         __cal_edit_ug_get_record_due_time(p, p->todo);
2590         elm_check_state_set(check_obj, p->is_no_due_date);
2591
2592         evas_object_smart_callback_add(check_obj, "changed", __cal_edit_ug_no_due_date_changed_callback, p);
2593         elm_check_state_set(check_obj, p->is_no_due_date);
2594         evas_object_propagate_events_set(check_obj, EINA_FALSE);
2595
2596         __cal_edit_ug_update_no_due_date_language(item);
2597
2598         elm_object_part_content_set(item, "elm.icon", check_obj);
2599
2600         return item;
2601 }
2602
2603 static void __cal_edit_ug_priority_popup_list_sel(void *data, Evas_Object *obj, void *event_info)
2604 {
2605         c_ret_if(!data);
2606         c_ret_if(!obj);
2607
2608         int priority = (int)data;
2609
2610         cal_edit_ug_data *p = CAL_UTIL_GET_PRIV_DATA(obj);
2611         c_ret_if(!p);
2612
2613         __cal_edit_ug_set_record_priority(p->todo, priority);
2614         __cal_edit_ug_update_priority_item(p, p->todo);
2615
2616         evas_object_del(p->popup);
2617         p->popup = NULL;
2618 }
2619
2620 static Evas_Object* __cal_edit_ug_create_priority_popup(cal_edit_ug_data* p)
2621 {
2622         c_retv_if(!p, NULL);
2623
2624         Evas_Object *popup = cal_util_add_popup(p->window, NULL, C_("IDS_CLD_BODY_PRIORITY"), NULL,
2625                 __cal_edit_ug_popup_hide_callback, p, S_("IDS_COM_BODY_CLOSE"), NULL);
2626         c_retv_if(!popup, NULL);
2627
2628         evas_object_data_set(popup, "priv", p);
2629
2630         Elm_Object_Item *popup_item = elm_popup_item_append(popup, S_("IDS_COM_OPT_HIGH_M_QUALITY"), NULL, __cal_edit_ug_priority_popup_list_sel, (const void *)CALENDAR_TODO_PRIORITY_HIGH);
2631         c_retv_if(!popup_item, NULL);
2632
2633         popup_item = elm_popup_item_append(popup, S_("IDS_IDLE_BODY_NORMAL"), NULL, __cal_edit_ug_priority_popup_list_sel, (const void *)CALENDAR_TODO_PRIORITY_NORMAL);
2634         c_retv_if(!popup_item, NULL);
2635
2636         popup_item = elm_popup_item_append(popup, S_("IDS_COM_BODY_LOW"), NULL, __cal_edit_ug_priority_popup_list_sel, (const void *)CALENDAR_TODO_PRIORITY_LOW);
2637         c_retv_if(!popup_item, NULL);
2638
2639         return popup;
2640 }
2641
2642 static void __cal_edit_ug_priority_select_callback(Evas_Object *obj, void *data)
2643 {
2644         c_retm_if(!data, "data is null");
2645         cal_edit_ug_data* p = data;
2646
2647         Evas_Object *popup = __cal_edit_ug_create_priority_popup(p);
2648         c_retm_if(!popup, "popup is NULL");
2649
2650         p->popup = popup;
2651
2652 }
2653
2654 static void __cal_edit_ug_update_priority_language(Evas_Object *item, calendar_record_h record)
2655 {
2656         elm_object_part_text_set(item, "elm.text.2", C_("IDS_CLD_BODY_PRIORITY"));
2657
2658         char* priority_text = NULL;
2659         int priority = __cal_edit_ug_get_record_priority(record);
2660         switch (priority) {
2661                 case CALENDAR_TODO_PRIORITY_LOW:
2662                         priority_text = strdup(S_("IDS_COM_BODY_LOW"));
2663                         break;
2664                 case CALENDAR_TODO_PRIORITY_NORMAL:
2665                         priority_text =strdup(C_("IDS_IDLE_BODY_NORMAL"));
2666                         break;
2667                 case CALENDAR_TODO_PRIORITY_HIGH:
2668                         priority_text =strdup(S_("IDS_COM_OPT_HIGH_M_QUALITY"));
2669                         break;
2670                 default:
2671                         break;
2672         }
2673
2674         elm_object_part_text_set(item, "elm.text.1", priority_text);
2675         CAL_FREE(priority_text);
2676 }
2677
2678 static Evas_Object *__cal_edit_ug_add_priority(cal_edit_ug_data* p)
2679 {
2680         c_retv_if(!p, NULL);
2681
2682         Evas_Object *item = cal_dialogue_insert_item_after(p->dialogue, "2text.2", p->no_due_date, __cal_edit_ug_priority_select_callback, p);
2683         c_retvm_if(!item, NULL, "cal_dialogue_append_item() is failed");
2684
2685         __cal_edit_ug_update_priority_language(item, p->todo);
2686
2687         return item;
2688 }
2689
2690 static void __cal_edit_ug_show_result_popup(cal_edit_ug_data *p, calendar_error_e error)
2691 {
2692         char buf[100]={0,};
2693
2694         // TODO: i18n
2695         switch(error) {
2696         case CALENDAR_ERROR_DB_FAILED:
2697                 cal_util_add_popup(p->window, NULL, S_("IDS_COM_POP_WARNING"), C_("IDS_CLD_POP_CALENDAR_SYNC_IN_PROGRESS"),
2698                         __cal_edit_ug_reponse_event_callback, NULL, NULL);
2699                 break;
2700
2701         case CALENDAR_ERROR_OUT_OF_MEMORY:
2702                 cal_util_add_popup(p->window, NULL, S_("IDS_COM_POP_WARNING"), C_("IDS_CLD_POP_CALENDAR_SYNC_IN_PROGRESS"),
2703                                 __cal_edit_ug_reponse_event_callback, NULL, NULL);
2704                 break;
2705
2706         case CALENDAR_ERROR_INVALID_PARAMETER:
2707                 cal_util_add_popup(p->window, NULL, S_("IDS_COM_POP_WARNING"), C_("IDS_CLD_POP_INVALID_TIME"),
2708                                 __cal_edit_ug_reponse_event_callback, NULL, NULL);
2709                 break;
2710
2711         default:
2712                 snprintf(buf,100,"not saved(internal error-%x)!",error);
2713                 cal_util_add_popup(p->window, NULL, S_("IDS_COM_POP_WARNING"), buf,
2714                                 __cal_edit_ug_reponse_event_callback, NULL, NULL);
2715                 break;
2716         }
2717 }
2718
2719 int __cal_edit_ug_save(cal_edit_ug_data *p, calendar_record_h record)
2720 {
2721         CAL_FN_START;
2722
2723         calendar_error_e error = CALENDAR_ERROR_NONE;
2724
2725         int record_index = 0;
2726
2727         calendar_record_type_e record_type = CALENDAR_RECORD_TYPE_EVENT;
2728
2729         if (_calendar_is_task_record(record))
2730                 record_type = CALENDAR_RECORD_TYPE_TODO;
2731
2732         __cal_edit_ug_set_record_alarm(p, record);
2733
2734         if (p->is_edit_mode) {
2735                 if (p->instance) {
2736
2737                         calendar_record_h exception_record = NULL;
2738                         error = calendar_record_clone(record, &exception_record);
2739                         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_create() is failed(%x)", error);
2740
2741                         int index = _calendar_get_record_index(record);
2742                         error = calendar_record_set_int(exception_record, _calendar_event.original_event_id, index);
2743                         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_set_int() is failed(%x)", error);
2744
2745                         __cal_edit_ug_set_record_time(p, exception_record);
2746                         _calendar_edit_recurrence_instance(exception_record, index);
2747
2748                         error = calendar_db_insert_record(exception_record, &record_index);
2749                         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_db_insert_record() is failed(%x)", error);
2750
2751                         if (error != CALENDAR_ERROR_NONE) {
2752                                 __cal_edit_ug_show_result_popup(p, error);
2753
2754                                 error = calendar_record_destroy(exception_record, true);
2755                                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_destroy() is failed(%x)", error);
2756
2757                                 return -1;
2758                         }
2759
2760                         error = calendar_record_destroy(exception_record, true);
2761                         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_destroy() is failed(%x)", error);
2762
2763                 } else {
2764
2765                         if (p->account_id == _calendar_get_account_id(record))
2766                         {
2767                                 error = calendar_db_update_record(record);
2768                                 if (error != CALENDAR_ERROR_NONE) {
2769
2770                                         ERR("calendar_db_update_record() is failed(%x)", error);
2771
2772                                         __cal_edit_ug_show_result_popup(p, error);
2773
2774                                         return -1;
2775                                 }
2776                                 record_index = _calendar_get_record_index(record);
2777                         }
2778                         else
2779                         {
2780                                 _calendar_delete_record(record);
2781
2782                                 error = calendar_db_insert_record(record, &record_index);
2783                                 if (error != CALENDAR_ERROR_NONE) {
2784
2785                                         ERR("calendar_db_insert_record() is failed(%x)", error);
2786                                         __cal_edit_ug_show_result_popup(p, error);
2787
2788                                         return -1;
2789                                 }
2790                         }
2791                 }
2792
2793         } else {
2794
2795                 error = calendar_db_insert_record(record, &record_index);
2796                 if (error != CALENDAR_ERROR_NONE) {
2797
2798                         ERR("calendar_db_insert_record() is failed(%x)", error);
2799
2800                         __cal_edit_ug_show_result_popup(p, error);
2801
2802                         return -1;
2803                 }
2804         }
2805
2806         return record_index;
2807 }
2808
2809 static void __cal_edit_ug_send_result_to_detail_ug(cal_edit_ug_data *p, int record_index)
2810 {
2811         c_retm_if(!p, "p is null");
2812         c_retm_if(record_index < 0, "record_index < 0");
2813
2814         service_h service = NULL;
2815
2816         int ret = service_create(&service);
2817         c_ret_if(ret != SERVICE_ERROR_NONE);
2818
2819         char value[64] = {0};
2820         snprintf(value, sizeof(value), "%d", record_index);
2821
2822         ret = service_add_extra_data(service, "record_index", value);
2823         c_warn_if(ret != SERVICE_ERROR_NONE, "service_add_extra_data() is failed(%x)", ret);
2824
2825         memset(value, 0x00, sizeof(value));
2826
2827         long long int lli_time = 0;
2828         cal_util_convert_tm_to_lli(NULL, &p->stm, &lli_time);
2829
2830         snprintf(value, sizeof(value), "%lli", lli_time);
2831
2832         ret = service_add_extra_data(service, "base_time", value);
2833         c_warn_if(ret != SERVICE_ERROR_NONE, "service_add_extra_data() is failed(%x)", ret);
2834
2835         ug_send_result(p->ug, service);
2836
2837         ret = service_destroy(service);
2838         c_warn_if(ret != SERVICE_ERROR_NONE, "service_destroy() is failed(%x)", ret);
2839 }
2840
2841 static void __cal_edit_ug_save_button_callback(void *data, Evas_Object *obj, void *event_info)
2842 {
2843         CAL_FN_START;
2844
2845         c_retm_if(!data, "data is null");
2846         cal_edit_ug_data* p = data;
2847         int record_index;
2848
2849         Eina_Bool result = __cal_edit_ug_check_date_validity(p);
2850         if(!result)     {
2851                 cal_util_add_popup(p->window, NULL, S_("IDS_COM_POP_WARNING"), C_("IDS_CLD_POP_END_TIME_SHOULD_BE_SET_AFTER_START_TIME"),
2852                                 __cal_edit_ug_reponse_event_callback, NULL, NULL);
2853                 return;
2854         }
2855
2856         if (p->item_type == ITEM_TYPE_TODO)
2857                 record_index = __cal_edit_ug_save(p, p->todo);
2858         else
2859                 record_index = __cal_edit_ug_save(p, p->event);
2860
2861         c_retm_if(record_index < 0, "cal_edit_save() is failed");
2862
2863         int r = status_message_post(S_("IDS_COM_POP_SAVED"));
2864         c_warn_if(r != STATUS_ERROR_NONE, "status_message_post() is failed(%d)", r);
2865
2866         __cal_edit_ug_send_result_to_detail_ug(p, record_index);
2867
2868         ug_destroy_me(p->ug);
2869
2870 }
2871
2872
2873 static void __cal_edit_ug_back_button_callback(void *user_data, Evas_Object *obj, void *ei)
2874 {
2875         CAL_FN_START;
2876
2877         c_ret_if(!user_data);
2878
2879         cal_edit_ug_data *data = user_data;
2880
2881         c_ret_if(!data->ug);
2882
2883         ug_destroy_me(data->ug);
2884 }
2885
2886 static void __cal_edit_ug_show_multi_alarms (cal_edit_ug_data *p)
2887 {
2888         c_retm_if(!p, "p is null");
2889
2890         Evas_Object *first_item = eina_list_nth(p->alarm_item_list, 0);
2891
2892         if(eina_list_count(p->alarm_item_list)<5){
2893                         p->alarm_plus_button = __cal_edit_ug_alarm_plus_button_icon(p, first_item);
2894                         elm_object_part_content_set(first_item, "elm.icon", p->alarm_plus_button);
2895         }
2896         Eina_List *list = eina_list_next(p->alarm_item_list);
2897         Eina_List *l;
2898         Evas_Object *item = NULL;
2899
2900         EINA_LIST_FOREACH(list, l, item)        {
2901                 if(item){
2902                         cal_dialogue_show_item_after(p->dialogue, item, first_item);
2903                         first_item = item;
2904                 }
2905         }
2906 }
2907
2908 static void __cal_edit_ug_show_event_items (cal_edit_ug_data *p)
2909 {
2910         c_retm_if(!p, "p is null");
2911
2912         if (!p->start_date_title)
2913                 p->start_date_title = __cal_edit_ug_insert_padding_after(p, C_("IDS_CLD_BODY_FROM"), p->title);
2914         else
2915                 cal_dialogue_show_item_after(p->dialogue, p->start_date_title, p->title);
2916
2917         Eina_Bool is_allday = EINA_FALSE;
2918
2919         if (!p->start_date) {
2920
2921                 is_allday = _calendar_is_allday_record(p->event);
2922
2923                 p->start_date = __cal_edit_ug_insert_timepicker_after(p, &p->stm, is_allday, p->start_date_title, __cal_edit_ug_start_date_changed_callback);
2924
2925         } else
2926                 cal_dialogue_show_item_after(p->dialogue, p->start_date, p->start_date_title);
2927
2928         if (!p->end_date_title)
2929                 p->end_date_title = __cal_edit_ug_insert_padding_after(p, C_("IDS_CLD_BODY_TO"), p->start_date);
2930         else
2931                 cal_dialogue_show_item_after(p->dialogue, p->end_date_title, p->start_date);
2932
2933         if (!p->end_date)
2934                 p->end_date = __cal_edit_ug_insert_timepicker_after(p, &p->etm, is_allday, p->end_date_title, __cal_edit_ug_end_date_changed_callback);
2935         else
2936                 cal_dialogue_show_item_after(p->dialogue, p->end_date, p->end_date_title);
2937
2938         if (!p->timezone)
2939                 p->timezone = __cal_edit_ug_add_time_zone(p);
2940         else
2941                 cal_dialogue_show_item_after(p->dialogue, p->timezone, p->end_date);
2942
2943         if (!p->allday)
2944                 p->allday = __cal_edit_ug_add_all_day(p, is_allday);
2945         else
2946                 cal_dialogue_show_item_after(p->dialogue, p->allday, p->timezone);
2947
2948         __cal_edit_ug_show_multi_alarms(p);
2949
2950         Eina_List *list = eina_list_last(p->alarm_item_list);
2951         Evas_Object *last_item = eina_list_data_get(list);
2952
2953         if (!p->repeat)
2954                 p->repeat = __cal_edit_ug_add_repeat(p);
2955         else
2956                 cal_dialogue_show_item_after(p->dialogue, p->repeat, last_item);
2957
2958 }
2959
2960 static void __cal_edit_ug_show_todo_items (cal_edit_ug_data *p)
2961 {
2962         c_retm_if(!p, "p is null");
2963
2964         if (!p->due_date_title)
2965                 p->due_date_title = __cal_edit_ug_insert_padding_after(p, C_("IDS_CLD_BODY_DUE_DATE"), p->title);
2966         else
2967                 cal_dialogue_show_item_after(p->dialogue, p->due_date_title, p->title);
2968
2969         if (!p->due_date)
2970                 p->due_date = __cal_edit_ug_insert_timepicker_after(p, &p->etm, EINA_FALSE, p->due_date_title, __cal_edit_ug_due_date_changed_callback);
2971         else
2972                 cal_dialogue_show_item_after(p->dialogue, p->due_date, p->due_date_title);
2973
2974         if (!p->no_due_date)
2975                 p->no_due_date = __cal_edit_ug_add_no_due_date(p);
2976         else
2977                 cal_dialogue_show_item_after(p->dialogue, p->no_due_date, p->due_date);
2978
2979         Evas_Object *first_item = eina_list_nth(p->alarm_item_list, 0);
2980         if (!p->priority)
2981                 p->priority = __cal_edit_ug_add_priority(p);
2982         else
2983                 cal_dialogue_show_item_after(p->dialogue, p->priority, first_item);
2984 }
2985
2986 static void __cal_edit_ug_hide_multi_alarms (cal_edit_ug_data *p)
2987 {
2988         c_retm_if(!p, "p is null");
2989
2990         Evas_Object *first_item = eina_list_nth(p->alarm_item_list, 0);
2991         Evas_Object *button_obj = elm_object_part_content_get(first_item, "elm.icon");
2992         if(button_obj)  {
2993                 evas_object_del(button_obj);
2994                 button_obj = NULL;
2995         }
2996
2997         Eina_List *list = p->alarm_item_list;
2998         list = eina_list_next(list);
2999         Eina_List *l;
3000         Evas_Object *item = NULL;
3001
3002         EINA_LIST_FOREACH(list, l, item)        {
3003                 if(item){
3004                         cal_dialogue_hide_item(p->dialogue, item);
3005                 }
3006         }
3007 }
3008
3009 static void __cal_edit_ug_hide_event_items (cal_edit_ug_data *p)
3010 {
3011         c_retm_if(!p, "p is null");
3012
3013         cal_dialogue_hide_item(p->dialogue, p->start_date_title);
3014         cal_dialogue_hide_item(p->dialogue, p->start_date);
3015         cal_dialogue_hide_item(p->dialogue, p->end_date_title);
3016         cal_dialogue_hide_item(p->dialogue, p->end_date);
3017         cal_dialogue_hide_item(p->dialogue, p->allday);
3018         cal_dialogue_hide_item(p->dialogue, p->timezone);
3019         cal_dialogue_hide_item(p->dialogue, p->repeat);
3020         __cal_edit_ug_hide_multi_alarms(p);
3021
3022 }
3023
3024 static void __cal_edit_ug_hide_todo_items (cal_edit_ug_data *p)
3025 {
3026         c_retm_if(!p, "p is null");
3027
3028         cal_dialogue_hide_item(p->dialogue, p->due_date_title);
3029         cal_dialogue_hide_item(p->dialogue, p->due_date);
3030         cal_dialogue_hide_item(p->dialogue, p->no_due_date);
3031         cal_dialogue_hide_item(p->dialogue, p->priority);
3032
3033 }
3034
3035 static void __cal_edit_ug_update_event_info (cal_edit_ug_data *p)
3036 {
3037         c_retm_if(!p, "p is null");
3038
3039         __cal_edit_ug_update_title_item(p->title_entry, p->event);
3040         __cal_edit_ug_update_note_item(p->note_entry, p->event);
3041         __cal_edit_ug_update_location_item(p, p->event);
3042
3043         __cal_edit_ug_update_save_to_item(p, p->event);
3044 }
3045
3046 static void __cal_edit_ug_update_todo_info (cal_edit_ug_data *p)
3047 {
3048         c_retm_if(!p, "p is null");
3049
3050         __cal_edit_ug_update_title_item(p->title_entry, p->todo);
3051         __cal_edit_ug_update_note_item(p->note_entry, p->todo);
3052         __cal_edit_ug_update_location_item(p, p->todo);
3053
3054         __cal_edit_ug_update_save_to_item(p, p->todo);
3055
3056 }
3057
3058 static void __cal_edit_ug_new_event_clicked_callback (void *data, Evas_Object * obj, void *event_info)
3059 {
3060         CAL_FN_START;
3061
3062         c_retm_if(!data, "data null");
3063
3064         cal_edit_ug_data *p = data;
3065
3066         if(p->item_type == ITEM_TYPE_EVENT)
3067                 return;
3068
3069         p->item_type = ITEM_TYPE_EVENT;
3070
3071         __cal_edit_ug_hide_todo_items(p);
3072         __cal_edit_ug_show_event_items(p);
3073         __cal_edit_ug_update_event_info(p);
3074
3075 }
3076
3077 static void __cal_edit_ug_new_todo_clicked_callback (void *data, Evas_Object * obj, void *event_info)
3078 {
3079         CAL_FN_START;
3080
3081         c_retm_if(!data, "data null");
3082
3083         cal_edit_ug_data *p = data;
3084
3085         if(p->item_type == ITEM_TYPE_TODO)
3086                 return;
3087
3088         p->item_type = ITEM_TYPE_TODO;
3089
3090         __cal_edit_ug_hide_event_items(p);
3091         __cal_edit_ug_show_todo_items(p);
3092         __cal_edit_ug_update_todo_info(p);
3093
3094 }
3095
3096 static void __cal_edit_ug_add_dialogue_items(cal_edit_ug_data *p)
3097 {
3098         CAL_FN_START;
3099
3100         c_retm_if(!p, "p is null");
3101
3102         p->title = __cal_edit_ug_add_title(p);
3103
3104         calendar_time_s time = {0};
3105
3106         if (p->item_type == ITEM_TYPE_TODO) {
3107
3108                 p->due_date_title = __cal_edit_ug_insert_padding_after(p, C_("IDS_CLD_BODY_DUE_DATE"), p->title);
3109
3110                 _calendar_get_end_time(p->todo, &time);
3111                 _calendar_convert_calendar_time_to_tm(&time, &p->etm);
3112 //              __cal_edit_ug_get_record_due_time(p, p->todo);
3113
3114                 p->due_date = __cal_edit_ug_insert_timepicker_after(p, &p->etm, EINA_FALSE, p->due_date_title, __cal_edit_ug_due_date_changed_callback);
3115
3116                 p->no_due_date = __cal_edit_ug_add_no_due_date(p);
3117
3118                 p->priority = __cal_edit_ug_add_priority(p);
3119         } else {
3120
3121                 p->start_date_title = __cal_edit_ug_insert_padding_after(p, C_("IDS_CLD_BODY_FROM"), p->title);
3122
3123                 if (p->instance)
3124                         _calendar_get_start_time(p->instance, &time);
3125                 else
3126                         _calendar_get_start_time(p->event, &time);
3127
3128                 _calendar_convert_calendar_time_to_tm(&time, &p->stm);
3129                 Eina_Bool allday = _calendar_is_allday_record(p->event);
3130
3131                 p->start_date = __cal_edit_ug_insert_timepicker_after(p, &p->stm, allday, p->start_date_title, __cal_edit_ug_start_date_changed_callback);
3132
3133                 p->end_date_title = __cal_edit_ug_insert_padding_after(p, C_("IDS_CLD_BODY_TO"), p->start_date);
3134
3135                 if (p->instance)
3136                         _calendar_get_end_time(p->instance, &time);
3137                 else
3138                         _calendar_get_end_time(p->event, &time);
3139
3140                 _calendar_convert_calendar_time_to_tm(&time, &p->etm);
3141                 p->end_date = __cal_edit_ug_insert_timepicker_after(p, &p->etm, allday, p->end_date_title, __cal_edit_ug_end_date_changed_callback);
3142
3143                 p->timezone = __cal_edit_ug_add_time_zone(p);
3144
3145                 p->allday = __cal_edit_ug_add_all_day(p, allday);
3146
3147         }
3148
3149         p->note = __cal_edit_ug_add_note(p);
3150
3151         p->location = __cal_edit_ug_add_location(p);
3152
3153         __cal_edit_ug_add_multi_alarm(p);
3154
3155         if (p->item_type == ITEM_TYPE_EVENT) {
3156                 p->repeat = __cal_edit_ug_add_repeat(p);
3157         }
3158
3159         p->saveto = __cal_edit_ug_add_saveto(p);
3160 }
3161
3162 static void __cal_edit_ug_update_naviframe_language(cal_edit_ug_data *data)
3163 {
3164         c_ret_if(!data->naviframe);
3165
3166         Elm_Object_Item *item = elm_naviframe_top_item_get(data->naviframe);
3167         c_ret_if(!item);
3168
3169         if (data->is_edit_mode) {
3170
3171                 if (data->item_type == ITEM_TYPE_TODO)
3172                         elm_object_item_part_text_set(item, "default", C_("IDS_TASK_HEADER_EDIT_TASK"));
3173                 else
3174                         elm_object_item_part_text_set(item, "default", C_("IDS_CLD_HEADER_EDIT_EVENT"));
3175
3176         } else
3177                 elm_object_item_part_text_set(item, "default", S_("IDS_COM_BODY_CREATE"));
3178 }
3179
3180 static void __cal_edit_ug_update_tabbar_language(cal_edit_ug_data *data)
3181 {
3182         if (data->is_edit_mode)
3183                 return;
3184
3185         c_ret_if(!data->event_item);
3186         c_ret_if(!data->todo_item);
3187
3188         elm_object_item_part_text_set(data->event_item, "default", C_("IDS_CLD_SK3_CREATE_EVENT"));
3189         elm_object_item_part_text_set(data->todo_item, "default", C_("IDS_TASK_SK_CREATE_TASK"));
3190 }
3191
3192 static void __cal_edit_ug_add_toolbar(cal_edit_ug_data *data, Evas_Object *scroller)
3193 {
3194         CAL_FN_START;
3195
3196         c_retm_if(!data, "data is null");
3197
3198         Elm_Object_Item *navi_item = NULL;
3199
3200         if (data->is_edit_mode) {
3201                 navi_item = elm_naviframe_item_push(data->naviframe, NULL, NULL, NULL, scroller, NULL);
3202                 c_warn_if(!navi_item, "elm_naviframe_item_push() is failed");
3203         } else {
3204                 navi_item = elm_naviframe_item_push(data->naviframe, NULL, NULL, NULL, scroller, "tabbar");
3205                 c_warn_if(!navi_item, "elm_naviframe_item_push() is failed");
3206
3207                 data->tabbar = elm_toolbar_add(data->naviframe);
3208                 c_ret_if(!data->tabbar);
3209
3210                 elm_object_item_part_content_set(navi_item, "tabbar", data->tabbar);
3211                 elm_toolbar_reorder_mode_set(data->tabbar, EINA_FALSE);
3212                 elm_toolbar_transverse_expanded_set(data->tabbar, EINA_TRUE);
3213                 elm_toolbar_shrink_mode_set(data->tabbar, ELM_TOOLBAR_SHRINK_EXPAND);
3214                 elm_object_style_set(data->tabbar, "tabbar");
3215
3216                 if (data->item_type == ITEM_TYPE_TODO) {
3217                         data->todo_item = elm_toolbar_item_append(data->tabbar, NULL, NULL, __cal_edit_ug_new_todo_clicked_callback, data);
3218
3219                         elm_toolbar_select_mode_set(data->tabbar, ELM_OBJECT_SELECT_MODE_ALWAYS);
3220
3221                         data->event_item = elm_toolbar_item_insert_before(data->tabbar, data->todo_item, NULL, NULL, __cal_edit_ug_new_event_clicked_callback, data);
3222                 } else {
3223                         data->event_item = elm_toolbar_item_append(data->tabbar, NULL,  NULL, __cal_edit_ug_new_event_clicked_callback, data);
3224                         data->todo_item = elm_toolbar_item_append(data->tabbar, NULL, NULL, __cal_edit_ug_new_todo_clicked_callback, data);
3225
3226                         elm_toolbar_select_mode_set(data->tabbar, ELM_OBJECT_SELECT_MODE_ALWAYS);
3227                 }
3228
3229                 if (data->item_type == ITEM_TYPE_TODO)
3230                         elm_toolbar_item_selected_set(data->todo_item, EINA_TRUE);
3231                 else
3232                         elm_toolbar_item_selected_set(data->event_item, EINA_TRUE);
3233
3234                 __cal_edit_ug_update_tabbar_language(data);
3235
3236         }
3237
3238         __cal_edit_ug_update_naviframe_language(data);
3239
3240         data->save_cancel = cal_save_cancel_create(data->naviframe, NULL, __cal_edit_ug_save_button_callback, __cal_edit_ug_back_button_callback, data);
3241
3242 }
3243
3244 static void __cal_edit_ug_dialogue_item_clicked_callback(void *data, Evas *e, Evas_Object *obj, void *ei)
3245 {
3246         c_ret_if(!data);
3247         c_ret_if(!obj);
3248
3249         cal_edit_ug_data* p = data;
3250 }
3251
3252 static Evas_Object *__cal_edit_ug_create_dialogue(Evas_Object *parent, cal_edit_ug_data* p)
3253 {
3254         c_retv_if(!p, NULL);
3255
3256         Evas_Object *scroller = cal_dialogue_create(parent);
3257         c_retvm_if(!scroller, NULL, "cal_dialogue_create() is failed");
3258         __cal_edit_ug_add_toolbar(p, scroller);
3259
3260         Evas_Object *dialogue = cal_dialogue_get_box(scroller);
3261
3262         evas_object_event_callback_add(dialogue, EVAS_CALLBACK_MOUSE_UP, __cal_edit_ug_dialogue_item_clicked_callback, p);
3263         evas_object_event_callback_add(dialogue, EVAS_CALLBACK_MOUSE_DOWN, __cal_edit_ug_dialogue_item_clicked_callback, p);
3264         evas_object_event_callback_add(dialogue, EVAS_CALLBACK_MOUSE_MOVE, __cal_edit_ug_dialogue_item_clicked_callback, p);
3265
3266         return dialogue;
3267
3268 }
3269
3270 static Evas_Object *__cal_edit_ug_create_layout(Evas_Object *parent, cal_edit_ug_data *data)
3271 {
3272         CAL_FN_START;
3273
3274         c_retv_if(!parent, NULL);
3275         c_retv_if(!data, NULL);
3276
3277         data->parent = parent;
3278
3279         if (data->item_type == ITEM_TYPE_TODO)
3280                 data->todo = data->record;
3281         else
3282                 data->event = data->record;
3283
3284         if (data->is_edit_mode) {
3285                 __cal_edit_ug_get_record_time_zone(data, data->record);
3286         } else {
3287                 cal_util_get_timezone(&data->timezone_path, &data->timezone_city, &data->timezone_offset);
3288
3289                 __cal_edit_ug_initialize_todo_record(data);
3290                 __cal_edit_ug_initialize_event_record(data);
3291         }
3292
3293         Evas_Object *base_layout = cal_util_add_layout(parent, NULL);
3294         c_retv_if(!base_layout, NULL);
3295
3296         Evas_Object *naviframe = elm_naviframe_add(base_layout);
3297         if (!naviframe) {
3298                 ERR("elm_naviframe_add() is failed");
3299
3300                 evas_object_del(base_layout);
3301
3302                 return NULL;
3303         }
3304
3305         evas_object_show(naviframe);
3306
3307         Evas_Object *bg = cal_util_add_bg(base_layout, EINA_FALSE);
3308         if (!bg) {
3309                 ERR("cal_util_add_bg() is failed");
3310
3311                 evas_object_del(base_layout);
3312
3313                 return NULL;
3314         }
3315
3316         data->base_layout = base_layout;
3317         data->naviframe = naviframe;
3318
3319         cal_util_initialize_timezone();
3320
3321         data->dialogue = __cal_edit_ug_create_dialogue(naviframe, data);
3322         c_warn_if(!data->dialogue, "__cal_edit_ug_create_dialogue() is failed");
3323
3324         __cal_edit_ug_add_dialogue_items(data);
3325
3326
3327         elm_object_part_content_set(base_layout, "elm.swallow.content", naviframe);
3328         elm_object_part_content_set(base_layout, "elm.swallow.bg", bg);
3329
3330         return base_layout;
3331 }
3332
3333 static calendar_record_h inline __cal_edit_ug_get_origin_record(const calendar_record_h instance)
3334 {
3335         CAL_FN_START;
3336
3337         c_retv_if(!instance, NULL);
3338
3339         int index = _calendar_get_record_index(instance);
3340
3341         return _calendar_get_record_with_index(index);
3342 }
3343
3344 static void *__cal_edit_ug_create_callback(ui_gadget_h ug, enum ug_mode mode, service_h service, void *priv)
3345 {
3346         CAL_FN_START;
3347
3348         c_retv_if(!ug, NULL);
3349         c_retv_if(!service, NULL);
3350         c_retv_if(!priv, NULL);
3351         c_retv_if(mode != UG_MODE_FULLVIEW, NULL);
3352
3353         cal_edit_ug_data *data = priv;
3354
3355         data->ug = ug;
3356
3357         int cid = 0;
3358         char *item_type = NULL;
3359         char *index = NULL;
3360         char *path = NULL;
3361         calendar_error_e error = CALENDAR_ERROR_NONE;
3362
3363         int r = service_get_extra_data(service, "index", &index);
3364
3365         if (r == SERVICE_ERROR_NONE)
3366                 cid = atoi(index);
3367
3368         r = service_get_extra_data(service, CAL_APPCONTROL_ITEM_TYPE, &item_type);
3369         if (r == SERVICE_ERROR_NONE) {
3370
3371                 if (!CAL_STRCMP(item_type, CAL_APPCONTROL_ITEM_TYPE_EVENT)) {
3372                         data->item_type = ITEM_TYPE_EVENT;
3373                         data->is_edit_mode = EINA_TRUE;
3374
3375                         r = service_get_extra_data(service, CAL_APPCONTROL_EVENT_ID, &index);
3376                         if (r != SERVICE_ERROR_NONE) {
3377                                 ERR("service_get_extra_data(%s) is failed(%x)", CAL_APPCONTROL_EVENT_ID, r);
3378
3379                                 CAL_FREE(index);
3380
3381                                 ug_destroy_me(ug);
3382
3383                                 return NULL;
3384                         }
3385
3386                         cid = atoi(index);
3387                         calendar_record_h record = _calendar_get_record_with_index(cid);
3388                         c_retv_if(!record, NULL);
3389
3390                         calendar_record_destroy(record, true);
3391
3392                 } else if (!CAL_STRCMP(item_type, CAL_APPCONTROL_ITEM_TYPE_TODO)) {
3393
3394                         data->item_type = ITEM_TYPE_TODO;
3395                         data->is_edit_mode = EINA_TRUE;
3396
3397                         r = service_get_extra_data(service, CAL_APPCONTROL_TODO_ID, &index);
3398                         if (r != SERVICE_ERROR_NONE) {
3399                                 ERR("service_get_extra_data(%s) is failed(%x)", CAL_APPCONTROL_TODO_ID, r);
3400
3401                                 CAL_FREE(index);
3402
3403                                 ug_destroy_me(ug);
3404
3405                                 return NULL;
3406                         }
3407
3408                         cid = atoi(index);
3409                         calendar_record_h record = _calendar_get_record_with_index(cid);
3410                         c_retv_if(!record, NULL);
3411
3412                         calendar_record_destroy(record, true);
3413
3414                 }
3415                 else if (!CAL_STRCMP(item_type, CAL_APPCONTROL_ITEM_TYPE_VCS)) {
3416
3417                         data->item_type = ITEM_TYPE_VCS;
3418
3419                         r = service_get_extra_data(service, CAL_APPCONTROL_VCS_PATH, &path);
3420                         if (r != SERVICE_ERROR_NONE) {
3421                                 ERR("service_get_extra_data(%s) is failed(%x)", CAL_APPCONTROL_VCS_PATH, r);
3422
3423                                 CAL_FREE(path);
3424
3425                                 ug_destroy_me(ug);
3426
3427                                 return NULL;
3428                         }
3429
3430                         FILE * file = fopen(path, "r");
3431                         if (!file) {
3432                                 ERR("fopen(%s) is failed.", path);
3433
3434                                 ug_destroy_me(ug);
3435
3436                                 return NULL;
3437                         }
3438
3439                         struct stat st;
3440                         if (stat(path, &st) != 0) {
3441                                 ERR("Getting file information Error!");
3442                                 fclose(file);
3443                                 CAL_FREE(path);
3444                                 ug_destroy_me(ug);
3445
3446                                 return NULL;
3447                         }
3448
3449                         int size = st.st_size;
3450                         char *raw_data = calloc(size + 1, sizeof(char));
3451                         if(!raw_data){
3452                                 ERR("raw_data is NULL!");
3453                                 fclose(file);
3454                                 free(raw_data);
3455                                 CAL_FREE(path);
3456                                 ug_destroy_me(ug);
3457
3458                                 return NULL;
3459                         }
3460
3461                         r = fread(raw_data, 1, size, file);
3462                         fclose( file );
3463                         c_retv_if(r < 0, NULL);
3464
3465                         calendar_list_h list = NULL;
3466
3467                         error = calendar_vcalendar_parse_to_calendar(raw_data, &list);
3468                         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_vcalendar_parse_to_calendar() is failed(%x)", error);
3469
3470                         if (list) {
3471                                 calendar_record_h record = NULL;
3472                                 error = calendar_list_get_current_record_p(list, &record);
3473                                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_list_get_current_record_p() is failed(%x)", error);
3474
3475                                 error = calendar_record_clone(record, &data->record);
3476                                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_clone() is failed(%x)", error);
3477
3478                                 error = calendar_list_destroy(list, true);
3479                         }
3480
3481                         free(raw_data);
3482                 }
3483         }
3484
3485         if (!data->record) {
3486                 if (0 < cid) {
3487                         data->is_edit_mode = EINA_TRUE;
3488                         data->record = _calendar_get_record_with_index(cid);
3489                 }
3490                 else {
3491                         data->is_edit_mode = EINA_FALSE;
3492                         char *record_address = NULL;
3493
3494                         r = service_get_extra_data(service, "record", &record_address);
3495                         if(record_address){
3496                                 data->record = (calendar_record_h)strtoul(record_address, NULL, 16);
3497                                 c_warn_if(!data->record, "strtol(%s, NULL, 16) is failed", record_address);
3498                         }
3499                         else    {
3500                                 char *note_str = NULL;
3501                                 r = service_get_extra_data(service, "note", &note_str);
3502
3503                                 if(note_str){
3504                                         calendar_record_h record = NULL;
3505                                         calendar_error_e error = CALENDAR_ERROR_NONE;
3506                                         error = calendar_record_create(_calendar_event._uri, &record);
3507                                         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_create() is failed(%x)", error);
3508
3509                                         data->record = record;
3510                                         __cal_edit_ug_set_record_note(data->record, note_str);
3511
3512                                         time_t now = time(NULL);
3513                                         localtime_r(&now, &data->base_time);
3514
3515                                         CAL_FREE(note_str);
3516                                 }
3517                         }
3518
3519                         int index = _calendar_get_record_index(data->record);
3520                         if(index>0)     {
3521                                 data->is_edit_mode = EINA_TRUE;
3522                         }
3523                         _calendar_record_type record_type = _calendar_get_record_type(data->record);
3524                         ERR("record_type=%d", record_type);
3525
3526                         if (record_type != _CALENDAR_RECORD_TYPE_EVENT && record_type != _CALENDAR_RECORD_TYPE_TODO &&  record_type != _CALENDAR_RECORD_TYPE_SEARCH_TODO_CALENDAR) {
3527                                 data->instance = data->record;
3528                                 data->record = __cal_edit_ug_get_origin_record(data->instance);
3529                         }
3530
3531                         if(record_address)
3532                                 CAL_FREE(record_address);
3533                 }
3534
3535                 if (_calendar_is_task_record(data->record))
3536                         data->item_type = ITEM_TYPE_TODO;
3537                 else
3538                         data->item_type = ITEM_TYPE_EVENT;
3539         }
3540
3541
3542         //base_time
3543         char *base_time = NULL;
3544         r = service_get_extra_data(service, "base_time", &base_time);
3545         c_warn_if(SERVICE_ERROR_NONE != r,"service_get_extra_data is failed(%d).", r);
3546
3547         if(base_time){
3548                 long long int lli_time = strtoll(base_time, NULL, 10);
3549                 cal_util_convert_lli_to_tm(NULL, lli_time, &data->base_time);
3550                 CAL_FREE(base_time);
3551         }
3552
3553         if (error != CALENDAR_ERROR_NONE || data->item_type == ITEM_TYPE_UNKNOWN) {
3554                 ERR("ERROR!! Invaild parameter!!");
3555                 ug_destroy_me(ug);
3556         }
3557
3558         CAL_FREE(item_type);
3559
3560         CAL_FREE(index);
3561
3562         CAL_FREE(path);
3563
3564         Evas_Object *parent = ug_get_parent_layout(ug);
3565         c_retv_if(!parent, NULL);
3566
3567         data->window = ug_get_window();
3568         c_retv_if(!data->window, NULL);
3569
3570         elm_theme_extension_add(NULL, "/usr/apps/"CALENDAR_PACKAGE"/res/edje/theme.edj");
3571         elm_theme_extension_add(NULL, "/usr/apps/"CALENDAR_PACKAGE"/res/edje/theme2.edj");
3572
3573         Evas_Object *base = __cal_edit_ug_create_layout(parent, data);
3574         c_retv_if(!base, NULL);
3575
3576         return base;
3577 }
3578
3579 static void __cal_edit_ug_destroy_callback(ui_gadget_h ug, service_h service, void *priv)
3580 {
3581         CAL_FN_START;
3582         c_ret_if(!priv);
3583
3584         cal_edit_ug_data *data = priv;
3585
3586         cal_util_delete_evas_object(&data->base_layout);
3587
3588         if (data->timezone_city)
3589                 free(data->timezone_city);
3590         if (data->timezone_path)
3591                 free(data->timezone_path);
3592         if (data->timezone_offset)
3593                 free(data->timezone_offset);
3594
3595         _calendar_free_record_list(&data->calendar_book_list);
3596
3597         if (data->alarm_list) {
3598                 g_list_free(data->alarm_list);
3599                 data->alarm_list = NULL;
3600         }
3601
3602         if (data->alarm_item_list) {
3603                 eina_list_free(data->alarm_item_list);
3604                 data->alarm_item_list = NULL;
3605         }
3606
3607         CAL_FREE(data->title_str);
3608         CAL_FREE(data->address);
3609         CAL_FREE(data->note_str);
3610
3611         if (data->save_cancel)
3612                 cal_save_cancel_destroy(data->save_cancel);
3613
3614         if (data->tabbar)
3615                 evas_object_del(data->tabbar);
3616
3617         if (data->event)
3618                 calendar_record_destroy(data->event, true);
3619
3620         if (data->todo)
3621                 calendar_record_destroy(data->todo, true);
3622
3623         if (data->instance)
3624                 calendar_record_destroy(data->instance, true);
3625
3626 }
3627
3628 static void __cal_edit_ug_key_callback(ui_gadget_h ug, enum ug_key_event evt, service_h data, void *priv)
3629 {
3630         c_ret_if(!ug);
3631
3632         DBG("UG: key event %d", evt);
3633
3634         switch (evt) {
3635         case UG_KEY_EVENT_END:
3636                 ug_destroy_me(ug);
3637                 break;
3638         default:
3639                 break;
3640         }
3641 }
3642
3643 static void __cal_edit_ug_update_language(cal_edit_ug_data *data)
3644 {
3645         c_ret_if(!data);
3646
3647         __cal_edit_ug_update_naviframe_language(data);
3648
3649         __cal_edit_ug_update_tabbar_language(data);
3650
3651         cal_save_cancel_set_button_text(data->save_cancel, S_("IDS_COM_OPT_SAVE"), NULL);
3652
3653         __cal_edit_ug_update_title_language(data->title);
3654
3655         if (data->item_type == ITEM_TYPE_EVENT) {
3656
3657                 __cal_edit_ug_update_start_language(data->start_date_title);
3658
3659                 __cal_edit_ug_update_end_language(data->end_date_title);
3660         } else
3661                 __cal_edit_ug_update_due_language(data->due_date_title);
3662
3663         __cal_edit_ug_update_time_zone_language(data->timezone, data->timezone_city, data->timezone_offset);
3664
3665         if (data->item_type == ITEM_TYPE_TODO) {
3666
3667                 __cal_edit_ug_update_no_due_date_language(data->no_due_date);
3668
3669                 __cal_edit_ug_update_priority_language(data->priority, data->todo);
3670         }
3671
3672         if (data->item_type == ITEM_TYPE_EVENT)
3673                 __cal_edit_ug_update_allday_language(data->allday);
3674
3675         __cal_edit_ug_update_description_language(data->note);
3676
3677         __cal_edit_ug_update_location_language(data->location);
3678
3679         __cal_edit_ug_update_reminder_language(data->alarm_item_list);
3680
3681         if (data->item_type == ITEM_TYPE_EVENT)
3682                 __cal_edit_ug_update_recurrence_language(data->repeat, data->repeat_freq);
3683
3684         if (data->item_type == ITEM_TYPE_EVENT)
3685                 __cal_edit_ug_update_saveto_language(data->saveto, data->event);
3686         else
3687                 __cal_edit_ug_update_saveto_language(data->saveto, data->todo);
3688 }
3689
3690 static void __cal_edit_ug_update_region(cal_edit_ug_data *data)
3691 {
3692         c_ret_if(!data);
3693
3694         if (data->item_type == ITEM_TYPE_EVENT) {
3695
3696                 Eina_Bool is_allday = _calendar_is_allday_record(data->event);
3697
3698                 __cal_edit_ug_update_datetime_region(data->start_date, &data->stm, is_allday, __cal_edit_ug_start_date_changed_callback, data);
3699
3700                 __cal_edit_ug_update_datetime_region(data->end_date, &data->etm, is_allday, __cal_edit_ug_end_date_changed_callback, data);
3701
3702         } else
3703                 __cal_edit_ug_update_datetime_region(data->due_date, &data->etm, _calendar_is_allday_record(data->todo), __cal_edit_ug_due_date_changed_callback, data);
3704 }
3705
3706 static void __cal_edit_ug_event_callback(ui_gadget_h ug, enum ug_event event, service_h service, void *priv)
3707 {
3708         c_ret_if(!ug);
3709         c_ret_if(!priv);
3710
3711         switch (event) {
3712         case UG_EVENT_LANG_CHANGE:
3713                 __cal_edit_ug_update_language((cal_edit_ug_data *)priv);
3714                 break;
3715         case UG_EVENT_REGION_CHANGE:
3716                 __cal_edit_ug_update_region((cal_edit_ug_data *)priv);
3717                 break;
3718         default:
3719                 break;
3720         }
3721 }
3722
3723 API int UG_MODULE_INIT(struct ug_module_ops *ops)
3724 {
3725         CAL_FN_START;
3726
3727         c_retv_if(!ops, -1);
3728
3729         cal_edit_ug_data *data = calloc(1, sizeof(cal_edit_ug_data));
3730
3731         ops->create = __cal_edit_ug_create_callback;
3732         ops->destroy = __cal_edit_ug_destroy_callback;
3733         ops->key_event = __cal_edit_ug_key_callback;
3734         ops->event = __cal_edit_ug_event_callback;
3735         ops->priv = data;
3736         ops->opt = UG_OPT_INDICATOR_PORTRAIT_ONLY;
3737
3738         const char *path = bindtextdomain(CALENDAR, LOCALEDIR);
3739         c_warn_if(!path, "bindtextdomain(%s, %s) is failed.", CALENDAR, LOCALEDIR);
3740
3741         __cal_edit_ug_initialize(data);
3742
3743         return 0;
3744 }
3745
3746 API void UG_MODULE_EXIT(struct ug_module_ops *ops)
3747 {
3748         CAL_FN_START;
3749
3750         c_ret_if(!ops);
3751
3752         cal_edit_ug_data *data = ops->priv;
3753
3754         __cal_edit_ug_finish(data);
3755
3756         free(data);
3757 }