Tizen 2.1 base
[apps/core/preloaded/calendar.git] / src / edit-view.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
20 #include "edit-view.h"
21
22 #define CAL_EDIT_UG "calendar-edit-efl"
23
24 typedef struct {
25         struct appdata *ad;
26         cal_edit_view_destroy_callback callback;
27         void *callback_data;
28 } cal_edit_view_data;
29
30 static void __cal_edit_layout_callback(ui_gadget_h ug, enum ug_mode mode, void *priv)
31 {
32         CAL_FN_START;
33
34         c_retm_if(!ug, "ug is null");
35
36         Evas_Object *base = ug_get_layout(ug);
37         if (!base) {
38                 ug_destroy(ug);
39                 return;
40         }
41
42         switch (mode)
43         {
44         case UG_MODE_FULLVIEW:
45                 evas_object_size_hint_weight_set(base, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
46                 evas_object_show(base);
47                 break;
48         default:
49                 DBG("Unsupported ug layout");
50                 break;
51         }
52
53 }
54
55 static void __cal_edit_destroy_callback(ui_gadget_h ug, void *priv)
56 {
57         CAL_FN_START;
58
59         c_ret_if(!ug);
60
61         ug_destroy(ug);
62
63         c_ret_if(!priv);
64
65         cal_edit_view_data *view_data = priv;
66
67         if (view_data->callback)
68                 view_data->callback(view_data->callback_data);
69
70         view_data->ad->ug = NULL;
71
72         free(view_data);
73 }
74
75 static void inline __cal_edit_clone_record(const calendar_record_h source_record, calendar_record_h *target_record)
76 {
77         CAL_FN_START;
78
79         c_ret_if(!source_record);
80         c_ret_if(!target_record);
81
82         calendar_error_e error = CALENDAR_ERROR_NONE;
83
84         error = calendar_record_clone(source_record, target_record);
85         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_clone() is failed(%x)", error);
86 }
87
88 static void inline __cal_edit_destroy_record(calendar_record_h record)
89 {
90         CAL_FN_START;
91
92         c_ret_if(!record);
93
94         calendar_error_e error = CALENDAR_ERROR_NONE;
95
96         error = calendar_record_destroy(record, true);
97         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_destroy() is failed(%x)", error);
98 }
99 void cal_edit_create_view(struct appdata *ad, calendar_record_h record, cal_edit_view_destroy_callback callback, void *callback_data)
100 {
101         CAL_FN_START;
102
103         c_ret_if(!ad);
104
105         if (ad->ug)
106                 return;
107
108         c_ret_if(!record);
109
110         struct ug_cbs ug_callback = {0};
111
112         cal_edit_view_data *view_data = NULL;
113
114         view_data = calloc(1, sizeof(cal_edit_view_data));
115
116         if (callback) {
117
118                 view_data->callback = callback;
119                 view_data->callback_data = callback_data;
120         }
121
122         view_data->ad = ad;
123
124         ug_callback.layout_cb = __cal_edit_layout_callback;
125         ug_callback.destroy_cb = __cal_edit_destroy_callback;
126         ug_callback.priv = view_data;
127
128         service_h service = NULL;
129
130         service_error_e error = SERVICE_ERROR_NONE;
131
132         error = service_create(&service);
133         if (error != SERVICE_ERROR_NONE) {
134                 ERR("service_create() is failed(%x)", error);
135
136                 CAL_FREE(view_data);
137
138                 return;
139         }
140
141         calendar_record_h cloned_record = NULL;
142
143         __cal_edit_clone_record(record, &cloned_record);
144
145         char record_address[16] = {0};
146         snprintf(record_address, sizeof(record_address), "%p", (void *)cloned_record);
147
148         error = service_add_extra_data(service, "record", record_address);
149         if(error != SERVICE_ERROR_NONE) {
150                 ERR("service_add_extra_data(%s,%s) is failed(%x)", "record", record_address, error);
151
152                 service_destroy(service);
153                 CAL_FREE(view_data);
154                 return;
155         }
156
157         //send base time
158         char time[30] = {0};
159         long long int lli_time = 0;
160         cal_util_convert_tm_to_lli(NULL, &ad->base_tm, &lli_time);
161         snprintf(time, sizeof(time), "%lli", lli_time);
162
163         error = service_add_extra_data(service, "base_time", time);
164         if(error != SERVICE_ERROR_NONE) {
165                 ERR("service_add_extra_data(%s,%s) is failed(%x)", "base_time", time, error);
166
167                 service_destroy(service);
168                 CAL_FREE(view_data);
169                 return;
170         }
171
172         ui_gadget_h ug = ug_create(NULL, CAL_EDIT_UG, UG_MODE_FULLVIEW, service, &ug_callback);
173         if (!ug) {
174                 ERR("ug_create(%s) is failed.", CAL_EDIT_UG);
175
176                 service_destroy(service);
177
178                 CAL_FREE(view_data);
179
180                 return;
181         }
182
183         service_destroy(service);
184
185         ad->ug = ug;
186 }