Tizen 2.1 base
[apps/home/call-setting.git] / src / cst-util.c
1 /*
2  * Copyright 2012  Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://floralicense.org/license/
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "cst-debug.h"
18 #include "cst-util.h"
19 #include "cst-common.h"
20 #include "cst-reject-msg.h"
21 #include "cst-widget.h"
22 #include <Evas.h>
23 #include <vconf.h>
24 #include <msg_types.h>
25 #include <msg.h>
26 #include <contacts.h>
27
28 #define CST_CT_UG_CONTACTS_LIST "contacts-list-efl"
29 #define CST_CT_UG_REQUEST_SELECT_NUMBER         12
30 #define CST_CT_UG_BUNDLE_TYPE           "type"
31 #define CST_CT_UG_BUNDLE_RESULT_NUMBER_ID       "num_id"
32 #define CST_CT_UG_BUNDLE_RESULT_PERSON_ID       "person_id"
33
34 #ifndef UG_MODULE_API
35 #define UG_MODULE_API __attribute__ ((visibility("default")))
36 #endif
37
38 UG_MODULE_API char *cst_reject_msg_get(int index)
39 {
40         ENTER(cst_reject_msg_get);
41         bindtextdomain(UGNAME, CST_LOCALE);
42
43         char *message = _cst_get_reject_message(index);
44         return message;
45 }
46
47 void _cst_vconf_get_bool(const char *in_key, int *boolval)
48 {
49         if ((vconf_get_bool(in_key, boolval)) < 0) {
50                 DBG("vconf get error : %s", in_key);
51         }
52 }
53
54 void _cst_vconf_get_int(const char *in_key, int *intval)
55 {
56         if ((vconf_get_int(in_key, intval)) < 0) {
57                 DBG("vconf get error : %s", in_key);
58         }
59 }
60
61 char *_cst_vconf_get_str(const char *in_key)
62 {
63         char *result = NULL;
64         result = vconf_get_str(in_key);
65         if (result == NULL) {
66                 DBG("vconf get error : %s", in_key);
67         }
68
69         return result;
70 }
71
72 void _cst_vconf_set_bool(const char *in_key, const int boolval)
73 {
74         if (vconf_set_bool(in_key, boolval) < 0) {
75                 DBG("vconf set error : %s", in_key);
76         } else {
77                 DBG("vconf set : %d(%s)", boolval, in_key);
78         }
79 }
80
81 void _cst_vconf_set_int(const char *in_key, const int intval)
82 {
83         if (vconf_set_int(in_key, intval) < 0) {
84                 DBG("vconf set error : %s", in_key);
85         } else {
86                 DBG("vconf set : %d(%s)", intval, in_key);
87         }
88 }
89
90 void _cst_vconf_set_str(const char *in_key, const char *strval)
91 {
92         if (vconf_set_str(in_key, strval) < 0) {
93                 DBG("vconf set error : %s", in_key);
94         } else {
95                 DBG("vconf set : %s(%s)", strval, in_key);
96         }
97 }
98
99 Eina_Bool _cst_check_flight_mode(void)
100 {
101         ENTER(_cst_check_flight_mode);
102         int flight_mode = EINA_FALSE;
103
104         _cst_vconf_get_bool(VCONFKEY_SETAPPL_FLIGHT_MODE_BOOL, &flight_mode);
105         return flight_mode;
106 }
107
108 Eina_Bool _cst_check_sim_status(void)
109 {
110         ENTER(_cst_check_sim_status);
111         int sim_status = VCONFKEY_TELEPHONY_SIM_UNKNOWN;
112         Eina_Bool sim_avail = EINA_FALSE;
113
114         _cst_vconf_get_int(VCONFKEY_TELEPHONY_SIM_SLOT, &sim_status);
115         if (sim_status == VCONFKEY_TELEPHONY_SIM_INSERTED) {
116                 sim_avail = EINA_TRUE;
117         }
118         return sim_avail;
119 }
120
121 int _cst_parse_bundle(service_h service)
122 {
123         int type = CST_UG_REQ_NONE;
124         char *val = NULL;
125
126         if (service) {
127                 service_get_extra_data(service, CST_UG_BUNDLE_TYPE, &val);
128                 if (val) {
129                         type = atoi(val);
130                 }
131                 DBG("Bundle type = %d", type);
132         }
133
134         return type;
135 }
136
137 static void __cst_contact_list_view_ug_destroy_cb(ui_gadget_h ug, void *priv)
138 {
139         ENTER(__cst_contact_list_view_ug_destroy_cb);
140         if (ug) {
141                 ug_destroy(ug);
142         }
143 }
144
145 static void __cst_contact_list_view_ug_result_cb(ui_gadget_h ug, service_h service, void *priv)
146 {
147         ENTER(__cst_contact_list_view_ug_result_cb);
148         CstUgData_t *ugd = (CstUgData_t *)priv;
149
150         int number_id = -1;
151         int person_id = -1;
152         char *temp_p = NULL;
153         char *number = NULL;
154         char *displayname = NULL;
155         contacts_record_h numbers_record = NULL;
156         contacts_record_h contacts_record = NULL;
157
158         service_get_extra_data(service, CST_CT_UG_BUNDLE_RESULT_NUMBER_ID, &temp_p);
159         number_id = atoi(temp_p);
160
161         service_get_extra_data(service, CST_CT_UG_BUNDLE_RESULT_PERSON_ID, &temp_p);
162         person_id = atoi(temp_p);
163
164         if (contacts_connect2() != CONTACTS_ERROR_NONE) {
165                 DBG("contacts_connect2 failed");
166                 ugd->popup = _cst_create_error_popup(ugd->nf, CST_ERROR_UNKNOWN);
167                 evas_object_show(ugd->popup);
168                 return;
169         }
170
171         DBG("number_id = %d, person_id = %d", number_id, person_id);
172
173         /* get and set number */
174         if (contacts_db_get_record(_contacts_number._uri, number_id, &numbers_record) != CONTACTS_ERROR_NONE) {
175                 DBG("contacts_db_get_record for number failed");
176                 ugd->popup = _cst_create_error_popup(ugd->nf, CST_ERROR_SELECT_VALID_ENTRY);
177                 evas_object_show(ugd->popup);
178         } else {
179                 if (contacts_record_get_str_p(numbers_record, _contacts_number.number, (char**)&number) == CONTACTS_ERROR_NONE) {
180                         DBG("number = %s", number);
181
182                         if (ugd->dg_entry_contact_number) {
183                                 elm_entry_entry_set(ugd->dg_entry_contact_number, number);
184                         } else {
185                                 elm_entry_entry_set(ugd->dg_entry, number);
186                         }
187                 }
188         }
189         contacts_record_destroy(numbers_record, true);
190
191         /* get and set display name if needed */
192         if (ugd->dg_entry_contact_name) {
193                 if (contacts_db_get_record(_contacts_person._uri, person_id, &contacts_record) != CONTACTS_ERROR_NONE) {
194                         DBG("contacts_db_get_record for display name failed");
195                         ugd->popup = _cst_create_error_popup(ugd->nf, CST_ERROR_SELECT_VALID_ENTRY);
196                         evas_object_show(ugd->popup);
197                 } else {
198                         if (contacts_record_get_str_p(contacts_record, _contacts_person.display_name, (char**)&displayname) == CONTACTS_ERROR_NONE) {
199                                 DBG("displayname = %s", displayname);
200
201                                 elm_entry_entry_set(ugd->dg_entry_contact_name, displayname);
202                         }
203                 }
204                 contacts_record_destroy(contacts_record, true);
205         }
206
207         if (contacts_disconnect2() != CONTACTS_ERROR_NONE) {
208                 DBG("contacts_disconnect2 failed");
209         }
210 }
211
212 static void __cst_contact_list_view_ug_layout_cb(ui_gadget_h ug, enum ug_mode mode, void *priv)
213 {
214         ENTER(__cst_contact_list_view_ug_layout_cb);
215         Evas_Object *base = ug_get_layout(ug);
216
217         ret_if(NULL == base);
218
219         evas_object_size_hint_weight_set(base, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
220         elm_win_resize_object_add(ug_get_window(), base);
221         evas_object_show(base);
222 }
223
224 static ui_gadget_h __cst_invoke_contact_uigadget(void *data)
225 {
226         ENTER(__cst_invoke_contact_uigadget);
227         ui_gadget_h ug;
228         struct ug_cbs cbs = {0, };
229         char buf[16];
230         retv_if(NULL == data, NULL);
231         CstUgData_t *ugd = (CstUgData_t *)data;
232
233         service_h service = NULL;
234
235         service_create(&service);
236
237         retv_if(NULL == service, NULL);
238
239         service_set_operation(service, SERVICE_OPERATION_CREATE_CONTENT);
240
241         snprintf(buf, sizeof(buf), "%d", CST_CT_UG_REQUEST_SELECT_NUMBER);
242
243         service_add_extra_data(service, CST_CT_UG_BUNDLE_TYPE, buf);
244
245         cbs.layout_cb = __cst_contact_list_view_ug_layout_cb;
246         cbs.result_cb = __cst_contact_list_view_ug_result_cb;
247         cbs.destroy_cb = __cst_contact_list_view_ug_destroy_cb;
248         cbs.priv = (void *)ugd;
249
250         ug = ug_create((ui_gadget_h)ugd->ug, CST_CT_UG_CONTACTS_LIST, UG_MODE_FULLVIEW, service, &cbs);
251
252         service_destroy(service);
253
254         return ug;
255 }
256
257 void _cst_on_click_ime_contact_btn(void *data, Evas_Object *obj, void *event_info)
258 {
259         ENTER(_cst_on_click_ime_contact_btn);
260         ret_if(NULL == data);
261         CstUgData_t *ugd = (CstUgData_t *)data;
262
263         __cst_invoke_contact_uigadget(ugd);
264 }
265
266 Eina_Bool _cst_naviframe_item_pop_cb(void *data)
267 {
268         CstUgData_t *ugd = (CstUgData_t *)data;
269         elm_naviframe_item_pop(ugd->nf);
270
271         ugd->back_button = _cst_get_navifr_prev_btn(ugd->nf);
272
273         return ECORE_CALLBACK_CANCEL;
274 }