b95e51249abce9e21e392d0878faaf23f2449a48
[apps/core/preloaded/calendar.git] / src / detail-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 "detail-view.h"
21
22 #define CAL_DETAIL_UG "calendar-detail-efl"
23
24 typedef struct {
25         struct appdata *ad;
26         cal_detail_view_destroy_callback callback;
27         void *callback_data;
28 } cal_detail_view_data;
29
30 static void __cal_detail_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_detail_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_detail_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_detail_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_detail_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
100 void cal_detail_create_view(struct appdata *ad, calendar_record_h record, cal_detail_view_destroy_callback callback, void *callback_data)
101 {
102         CAL_FN_START;
103
104         c_ret_if(!ad);
105         c_ret_if(!record);
106
107         if (ad->ug)
108                 return;
109
110         struct ug_cbs ug_callback = {0};
111
112         cal_detail_view_data *view_data = NULL;
113
114         view_data = calloc(1, sizeof(cal_detail_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_detail_layout_callback;
125         ug_callback.destroy_cb = __cal_detail_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_detail_clone_record(record, &cloned_record);
144
145         char record_address[16] = {0};
146
147         snprintf(record_address, sizeof(record_address), "%p", (void *)cloned_record);
148
149         error = service_add_extra_data(service, "record", record_address);
150         c_warn_if(error != SERVICE_ERROR_NONE, "service_add_extra_data(%s,%s) is failed(%x)", "record", record_address, error);
151
152         ui_gadget_h ug = ug_create(NULL, CAL_DETAIL_UG, UG_MODE_FULLVIEW, service, &ug_callback);
153         if (!ug) {
154                 ERR("ug_create(%s) is failed.", CAL_DETAIL_UG);
155
156                 __cal_detail_destroy_record(cloned_record);
157
158                 service_destroy(service);
159
160                 CAL_FREE(view_data);
161
162                 return;
163         }
164
165         service_destroy(service);
166
167         ad->ug = ug;
168 }