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