2012/08/21: tizen 2.0 beta
[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://www.tizenopensource.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-svc.h>
27 #include <contacts-ug.h>
28
29 #ifndef UG_MODULE_API
30 #define UG_MODULE_API __attribute__ ((visibility("default")))
31 #endif
32
33 UG_MODULE_API char *cst_reject_msg_get(int index)
34 {
35         ENTER(cst_reject_msg_get);
36         bindtextdomain(UGNAME, CST_LOCALE);
37
38         char *message = _cst_get_reject_message(index);
39         return message;
40 }
41
42 void _cst_vconf_get_bool(const char *in_key, int *boolval)
43 {
44         if ((vconf_get_bool(in_key, boolval)) < 0)
45                 DBG("vconf get error : %s", in_key);
46 }
47
48 void _cst_vconf_get_int(const char *in_key, int *intval)
49 {
50         if ((vconf_get_int(in_key, intval)) < 0)
51                 DBG("vconf get error : %s", in_key);
52 }
53
54 char *_cst_vconf_get_str(const char *in_key)
55 {
56         char *result = NULL;
57         result = vconf_get_str(in_key);
58         if (result == NULL)
59                 DBG("vconf get error : %s", in_key);
60
61         return result;
62 }
63
64 void _cst_vconf_set_bool(const char *in_key, const int boolval)
65 {
66         if (vconf_set_bool(in_key, boolval) < 0)
67                 DBG("vconf set error : %s", in_key);
68         else
69                 DBG("vconf set : %d(%s)", boolval, in_key);
70 }
71
72 void _cst_vconf_set_int(const char *in_key, const int intval)
73 {
74         if (vconf_set_int(in_key, intval) < 0)
75                 DBG("vconf set error : %s", in_key);
76         else
77                 DBG("vconf set : %d(%s)", intval, in_key);
78 }
79
80 void _cst_vconf_set_str(const char *in_key, const char *strval)
81 {
82         if (vconf_set_str(in_key, strval) < 0)
83                 DBG("vconf set error : %s", in_key);
84         else
85                 DBG("vconf set : %s(%s)", strval, in_key);
86 }
87
88 Eina_Bool _cst_check_flight_mode(void)
89 {
90         ENTER(_cst_check_flight_mode);
91         int flight_mode = EINA_FALSE;
92
93         _cst_vconf_get_bool(VCONFKEY_SETAPPL_FLIGHT_MODE_BOOL, &flight_mode);
94         return flight_mode;
95 }
96
97 Eina_Bool _cst_check_sim_status(void)
98 {
99         ENTER(_cst_check_sim_status);
100         int sim_status = VCONFKEY_TELEPHONY_SIM_UNKNOWN;
101         Eina_Bool sim_avail = EINA_FALSE;
102
103         _cst_vconf_get_int(VCONFKEY_TELEPHONY_SIM_SLOT, &sim_status);
104         if (sim_status == VCONFKEY_TELEPHONY_SIM_INSERTED) {
105                 sim_avail = EINA_TRUE;
106         }
107         return sim_avail;
108 }
109
110 int _cst_parse_bundle(service_h service)
111 {
112         int type = CST_UG_REQ_NONE;
113         char* val = NULL;
114
115         if (service) {
116                 service_get_extra_data(service, CST_UG_BUNDLE_TYPE, &val);
117                 if (val) type = atoi(val);
118                 DBG("Bundle type = %d", type);
119         }
120
121         return type;
122 }
123
124 static void __cst_contact_list_view_ug_destroy_cb(ui_gadget_h ug, void *priv)
125 {
126         ENTER(__cst_contact_list_view_ug_destroy_cb);
127         if (ug)
128                 ug_destroy(ug);
129 }
130
131 static void __cst_contact_list_view_ug_result_cb(ui_gadget_h ug, service_h service, void *priv)
132 {
133         ENTER(__cst_contact_list_view_ug_result_cb);
134         CstUgData_t *ugd = (CstUgData_t *)priv;
135
136         char *temp_p = NULL;
137         int contact_index;
138         int error_code = CTS_SUCCESS;
139         CTSvalue *number = NULL;
140         CTSvalue *name = NULL;
141         const char *first = NULL, *last = NULL, *display_name = NULL, *return_name = NULL, *return_number = NULL;
142         Evas_Object *error;
143         CTSstruct *contact = NULL;
144
145         service_get_extra_data(service, CT_UG_BUNDLE_RESULT_NUMBER_ID, &temp_p);
146         contact_index = atoi(temp_p);
147
148         contacts_svc_connect();
149         error_code = contacts_svc_get_contact_value(CTS_GET_NUMBER_VALUE, contact_index, &number);
150
151         if (error_code >= CTS_SUCCESS) {
152                 return_number = contacts_svc_value_get_str(number, CTS_NUM_VAL_NUMBER_STR);
153                 if (ugd->dg_entry_contact_number)
154                         elm_entry_entry_set(ugd->dg_entry_contact_number, return_number);
155                 else
156                         elm_entry_entry_set(ugd->dg_entry, return_number);
157                 DBG("contact number : %s", return_number);
158         } else {
159                 DBG(" contact API has a problem \n");
160                 error = _cst_create_error_popup(ugd->nf, CST_ERROR_SELECT_VALID_ENTRY);
161                 ugd->popup = error;
162                 evas_object_show(error);
163                 return;
164         }
165
166         if (ugd->dg_entry_contact_name) {
167                 service_get_extra_data(service, CT_UG_BUNDLE_RESULT_CONTACT_ID, &temp_p);
168                 contact_index = atoi(temp_p);
169
170                 if (contact_index >= 0 && contacts_svc_get_contact(contact_index, &contact) == CTS_SUCCESS) {
171                         contacts_svc_struct_get_value(contact, CTS_CF_NAME_VALUE, &name);
172                         DBG("after  contacts_svc_struct_get_value\n");
173                         display_name = contacts_svc_value_get_str(name, CTS_NAME_VAL_DISPLAY_STR);
174                         DBG("display name: %s\n", return_name);
175                         first = contacts_svc_value_get_str(name, CTS_NAME_VAL_FIRST_STR);
176                         DBG("first name: %s\n", return_name);
177                         last = contacts_svc_value_get_str(name, CTS_NAME_VAL_LAST_STR);
178                         DBG("last name : %s\n", return_name);
179
180                         if (display_name != NULL) {
181                                 return_name = g_strdup(display_name);
182                         } else if (first != NULL && last != NULL) {
183                                 if (CTS_ORDER_NAME_FIRSTLAST == contacts_svc_get_order(CTS_ORDER_OF_DISPLAY))
184                                         return_name = g_strdup_printf("%s %s", first, last);
185                                 else
186                                         return_name = g_strdup_printf("%s %s", last, first);
187                                 DBG("after  contacts_svc_get_order\n");
188                         } else if (first != NULL) {
189                                 return_name = g_strdup_printf("%s", first);
190                         } else if (last != NULL) {
191                                 return_name = g_strdup_printf("%s", last);
192                         } else
193                                 DBG("No name available");
194
195                         if (return_name) {
196                                 elm_entry_entry_set(ugd->dg_entry_contact_name, return_name);
197                         }
198                         DBG("return_name : %s\n", return_name);
199                         g_free((gpointer)return_name);
200                 }
201         }
202
203
204         contacts_svc_disconnect();
205
206         ug_destroy(ug);
207 }
208
209 static void __cst_contact_list_view_ug_layout_cb(ui_gadget_h ug, enum ug_mode mode, void *priv)
210 {
211         ENTER(__cst_contact_list_view_ug_layout_cb);
212         Evas_Object *base = ug_get_layout(ug);
213
214         ret_if(NULL == base);
215
216         evas_object_size_hint_weight_set(base, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
217         elm_win_resize_object_add(ug_get_window(), base);
218         evas_object_show(base);
219 }
220
221 static ui_gadget_h __cst_invoke_contact_uigadget(void *data)
222 {
223         ENTER(__cst_invoke_contact_uigadget);
224         ui_gadget_h ug;
225         struct ug_cbs cbs = {0, };
226         char buf[16];
227         retv_if(NULL == data, NULL);
228         CstUgData_t *ugd = (CstUgData_t *)data;
229
230         service_h service = NULL;
231
232         service_create(&service);
233
234         retv_if(NULL == service, NULL);
235
236         service_set_operation(service, SERVICE_OPERATION_CREATE_CONTENT);
237
238         snprintf(buf, sizeof(buf), "%d", CT_UG_REQUEST_SELECT_NUMBER);
239
240         service_add_extra_data(service, CT_UG_BUNDLE_TYPE, buf);
241
242         cbs.layout_cb = __cst_contact_list_view_ug_layout_cb;
243         cbs.result_cb = __cst_contact_list_view_ug_result_cb;
244         cbs.destroy_cb = __cst_contact_list_view_ug_destroy_cb;
245         cbs.priv = (void *)ugd;
246
247         ug = ug_create((ui_gadget_h)ugd->ug, UG_CONTACTS_LIST, UG_MODE_FULLVIEW, service, &cbs);
248
249         service_destroy(service);
250
251         return ug;
252 }
253
254 void _cst_on_click_ime_contact_btn(void *data, Evas_Object *obj, void *event_info)
255 {
256         ENTER(_cst_on_click_ime_contact_btn);
257         ret_if(NULL == data);
258         CstUgData_t *ugd = (CstUgData_t *)data;
259
260         __cst_invoke_contact_uigadget(ugd);
261 }
262
263 void _cst_connect_reject_list_handle(CstUgData_t *ugd)
264 {
265     ENTER(_cst_connect_reject_list_handle);
266     ret_if(NULL == ugd);
267
268     ugd->reject_handle = phone_misc_connect();
269     if (NULL == ugd->reject_handle) {
270                 DBG("phone_misc_connect Failed");
271                 return;
272         }
273 }
274
275 void _cst_disconnect_reject_list_handle(CstUgData_t *ugd)
276 {
277     ENTER(_cst_connect_reject_list_handle);
278     ret_if(NULL == ugd);
279
280     int phone_misc_ret = phone_misc_disconnect(ugd->reject_handle);
281         if (phone_misc_ret < 0) {
282                 DBG("phone_misc_disconnect() Failed(%d)", phone_misc_ret);
283                 return;
284         }
285 }
286