apply FSL(Flora Software License)
[apps/home/message-app.git] / composer / src / external / msg-ui-composer-external.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*/
18 #include "msg-ui-composer-data.h"
19 #include "msg-ui-composer-external.h"
20 #include "msg-ui-composer-common.h"
21 #include "msg-ui-composer-util.h"
22 #include "msg-ui-composer-popup.h"
23 #include "msg-ui-composer-body.h"
24 #include "msg-ui-composer-recipient.h"
25 #include <appsvc.h>
26 #include <ui-gadget.h>
27 #include <Ecore_X.h>
28
29 #include <contacts-svc.h>
30 #include <contacts-ug.h>
31
32
33 static void __msg_ui_composer_external_closed(MSG_COMPOSER_VIEW_DATA_S *cd)
34 {
35         D_ENTER;
36
37         if (cd->loaded_aul || cd->state == COMPOSER_STATE_TYPE_RUNNING) {
38                 if(!msg_ui_composer_popup_exist(cd)) {
39                         msg_ui_composer_last_focus_load(cd);
40                 }
41         }
42
43         D_LEAVE;
44 }
45
46 static void __ug_calllog_return_cb(struct ui_gadget *ug, bundle *result, void *priv)
47 {
48         D_ENTER;
49
50         MSG_COMPOSER_VIEW_DATA_S *cd;
51
52         bool bMaxCount = false;
53         bool bInvalidNum = false;
54         bool bDup = false;
55
56         if (!ug || !priv || !result) {
57                 D_EMSG("parameter error ug = %p, priv = %p, result = %p", ug, priv, result);
58                 return;
59         }
60
61         cd = (MSG_COMPOSER_VIEW_DATA_S *)priv;
62         D_MSG_RETM_IF(cd->recipient == NULL, "Recipient Data is NULL");
63
64         int *list;
65         int len;
66         int i;
67         char *encoded_str;
68
69         encoded_str = (char *)bundle_get_val(result, MSG_BUNDLE_KEY_GET_SEL_LIST);
70         list = (int *)g_base64_decode(encoded_str, (gsize *)&len);
71
72         for (i = 0; i < len / sizeof(int); i++) {
73
74                 if (msg_ui_composer_recipient_count_get(cd->recipient) >= COMPOSER_RECIPIENT_COUNT_MAX) {
75                         bMaxCount = true;
76                         break;
77                 }
78
79                 CTSvalue *plog = NULL;
80                 int ret = contacts_svc_get_phonelog(list[i], &plog);
81                 if (ret == CTS_SUCCESS) {
82                         const char *number = contacts_svc_value_get_str(plog, CTS_PLOG_VAL_NUMBER_STR);
83                         if (number) {
84                                 if (msg_ui_composer_recipient_duplicate_check(cd->recipient, number) == COMPOSER_RETURN_RECIPIENT_DUPLICATE) {
85                                         bDup = true;
86                                         contacts_svc_value_free(plog);
87                                         continue;
88                                 }
89
90                                 if (msg_ui_composer_recipient_vaild_check(number) == EINA_FALSE) {
91                                         bInvalidNum = true;
92                                 }
93
94                                 int contact_id = contacts_svc_value_get_int(plog, CTS_PLOG_VAL_RELATED_ID_INT);
95
96                                 msg_ui_composer_recipient_append(cd->recipient, number, contact_id);
97                         }
98
99                         contacts_svc_value_free(plog);
100                 }
101         }
102
103         D_MSG("bMaxCount = %d, bInvalidNum = %d, bDup = %d", bMaxCount, bInvalidNum, bDup);
104         if (bMaxCount) {
105                 msg_ui_composer_common_tickernoti(cd, COMPOSER_TICKERNOTI_COUNT_MAX);
106         } else if (bInvalidNum) {
107                 msg_ui_composer_common_tickernoti(cd, COMPOSER_TICKERNOTI_INVALID_RECP);
108         } else if (bDup) {
109                 msg_ui_composer_common_tickernoti(cd, COMPOSER_TICKERNOTI_DUP_RECP);
110         }
111
112         g_free(list);
113         D_LEAVE;
114 }
115
116 static void __ug_layout_cb(struct ui_gadget *ug, enum ug_mode mode, void *priv)
117 {
118         D_ENTER;
119         MSG_COMPOSER_VIEW_DATA_S *cd;
120         Evas_Object *base;
121
122         if (!ug || !priv) {
123                 D_MSG("");
124                 return;
125         }
126
127         cd = (MSG_COMPOSER_VIEW_DATA_S *)priv;
128         base = ug_get_layout(ug);
129
130         if (!base) {
131                 ug_destroy(ug);
132                 D_MSG("");
133                 return;
134         }
135
136         switch (mode) {
137         case UG_MODE_FULLVIEW:
138
139                 evas_object_size_hint_weight_set(base, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
140                 elm_win_resize_object_add(ug_get_window(), base);
141                 evas_object_show(base);
142                 break;
143         case UG_MODE_FRAMEVIEW:
144                 break;
145         default:
146                 break;
147         }
148
149         D_LEAVE;
150 }
151
152 static void __ug_closed_cb(struct ui_gadget *ug, void *priv)
153 {
154         D_ENTER;
155
156         MSG_COMPOSER_VIEW_DATA_S *cd = (MSG_COMPOSER_VIEW_DATA_S *)priv;
157
158         if (cd->loaded_ug) {
159                 ug_destroy(cd->loaded_ug);
160                 cd->loaded_ug = NULL;
161         }
162
163         __msg_ui_composer_external_closed(cd);
164
165         D_LEAVE;
166 }
167
168 /*
169 * Create External UG
170 */
171 struct ui_gadget * msg_ui_composer_launch_contact_detail_ug(MSG_COMPOSER_VIEW_DATA_S *cd, int ct_id)
172 {
173         D_ENTER;
174         D_MSG_RETVM_IF(cd == NULL, NULL, "Composer data is NULL");
175
176         struct ui_gadget *ug;
177         struct ug_cbs cbs = { 0, };
178
179         bundle *kb = bundle_create();
180
181         if (!kb)
182                 return NULL;
183
184         char contactId[DEF_BUF_LEN_S] = { 0, };
185         char buf[DEF_BUF_LEN_D] = { 0, };
186
187         snprintf(contactId, sizeof(contactId), "%d", ct_id);
188         snprintf(buf, sizeof(buf), "%d", CT_UG_REQUEST_DETAIL);
189
190         bundle_add(kb, CT_UG_BUNDLE_TYPE, buf);
191         bundle_add(kb, CT_UG_BUNDLE_ID, contactId);
192
193         cbs.layout_cb = __ug_layout_cb;
194         cbs.result_cb = NULL;
195         cbs.destroy_cb = __ug_closed_cb;
196         cbs.priv = cd;
197
198         ug = ug_create(cd->ug, UG_CONTACTS_DETAILS, UG_MODE_FULLVIEW, kb, &cbs);
199         cd->loaded_ug = ug;
200         bundle_free(kb);
201
202         D_LEAVE;
203         return ug;
204 }
205
206 struct ui_gadget * msg_ui_composer_launch_contact_list_ug(MSG_COMPOSER_VIEW_DATA_S *cd, int type, const char *str)
207 {
208         D_ENTER;
209         D_MSG_RETVM_IF(cd == NULL, NULL, "Composer data is NULL");
210         struct ui_gadget *ug;
211         struct ug_cbs cbs = { 0, };
212
213         bundle *kb = bundle_create();
214
215         if (!kb)
216                 return NULL;
217
218         char buf[DEF_BUF_LEN_D] = { 0, };
219
220         if (type == CT_UG_REQUEST_UPDATE_WITH_EMAIL) {
221                 snprintf(buf, sizeof(buf), "%d", CT_UG_REQUEST_UPDATE_WITH_EMAIL);
222                 bundle_add(kb, CT_UG_BUNDLE_TYPE, buf);
223                 bundle_add(kb, CT_UG_BUNDLE_EMAIL, str);
224         } else if (type == CT_UG_REQUEST_UPDATE_WITH_NUM) {
225                 snprintf(buf, sizeof(buf), "%d", CT_UG_REQUEST_UPDATE_WITH_NUM);
226                 bundle_add(kb, CT_UG_BUNDLE_TYPE, buf);
227                 bundle_add(kb, CT_UG_BUNDLE_NUM, str);
228         }
229         else {
230                 bundle_free(kb);
231                 return NULL;
232         }
233
234         cbs.layout_cb = __ug_layout_cb;
235         cbs.result_cb = NULL;
236         cbs.destroy_cb = __ug_closed_cb;
237         cbs.priv = cd;
238
239         ug = ug_create(cd->ug, UG_CONTACTS_LIST, UG_MODE_FULLVIEW, kb, &cbs);
240         bundle_free(kb);
241         cd->loaded_ug = ug;
242
243         D_LEAVE;
244         return ug;
245 }
246
247 struct ui_gadget *ug_calllog_create(void *data)
248 {
249         D_ENTER;
250
251         struct ui_gadget *ug;
252         struct ug_cbs cbs = { 0, };
253
254         MSG_COMPOSER_VIEW_DATA_S *cd = (MSG_COMPOSER_VIEW_DATA_S *)data;
255
256         bundle *kb = bundle_create();
257
258         cbs.layout_cb = __ug_layout_cb;
259         cbs.result_cb = __ug_calllog_return_cb;
260         cbs.destroy_cb = __ug_closed_cb;
261         cbs.priv = data;
262         ug = ug_create(cd->ug, TXT_UG_NAME_LOG, UG_MODE_FULLVIEW, kb, &cbs);
263
264         bundle_free(kb);
265
266         return ug;
267 }
268
269 static void __ug_contacts_checklist_result_cb(struct ui_gadget *ug, bundle *result, void *priv)
270 {
271         D_ENTER;
272         D_MSG_RETM_IF(!ug,"ug data is NULL");
273         D_MSG_RETM_IF(!result,"bundle result data is NULL");
274         D_MSG_RETM_IF(!priv,"priv data is NULL");
275
276         MSG_COMPOSER_VIEW_DATA_S *cd = (MSG_COMPOSER_VIEW_DATA_S *)priv;
277         D_MSG_RETM_IF(cd->recipient == NULL,"Recipient data is NULL");
278
279         int contact_index;
280         int *num_id_list = NULL;
281         int len, i;
282
283         const char *number_str;
284         const char *bundle_val;
285
286         bool bMaxCount = false;
287         bool bInvalidNum = false;
288         bool bDup = false;
289
290         bundle_val = bundle_get_val(result, CT_UG_BUNDLE_RESULT_NUMBER_ID_LIST);
291         D_MSG_RETM_IF(!bundle_val,"bundle_val data is NULL");
292
293         num_id_list = (int *)g_base64_decode(bundle_val, (gsize *)&len);
294         D_MSG_RETM_IF(!num_id_list,"number_id list is NULL");
295
296         for (i = 0; i < len / sizeof(int); i++) {
297
298                 if (msg_ui_composer_recipient_count_get(cd->recipient) >= COMPOSER_RECIPIENT_COUNT_MAX) {
299                         bMaxCount = true;
300                         break;
301                 }
302
303                 CTSvalue *value = NULL;
304                 contact_index = contacts_svc_get_contact_value(CTS_GET_NUMBER_VALUE, num_id_list[i], &value);
305                 if (contact_index <= 0 ) {
306                         D_EMSG("Fail to get contact value");
307                         continue;
308                 }
309
310                 number_str = contacts_svc_value_get_str(value, CTS_NUM_VAL_NUMBER_STR);
311                 D_MSG("selected number_id = %d, contact_id = %d, number = %s", num_id_list[i], contact_index, number_str);
312
313                 if (number_str) {
314                         if (msg_ui_composer_recipient_duplicate_check(cd->recipient, number_str) == COMPOSER_RETURN_RECIPIENT_DUPLICATE) {
315                                 bDup = true;
316                                 contacts_svc_value_free(value);
317                                 continue;
318                         }
319
320                         if (msg_ui_composer_recipient_vaild_check(number_str) == EINA_FALSE) {
321                                 bInvalidNum = true;
322                         }
323
324                         msg_ui_composer_recipient_append(cd->recipient, number_str, contact_index);
325
326                 } else {
327                         D_EMSG("contact index[%d] is unuseable !!", contact_index);
328                 }
329
330                 contacts_svc_value_free(value);
331         }
332
333         D_MSG("bMaxCount = %d, bInvalidNum = %d, bDup = %d", bMaxCount, bInvalidNum, bDup);
334         if (bMaxCount) {
335                 msg_ui_composer_common_tickernoti(cd, COMPOSER_TICKERNOTI_COUNT_MAX);
336         } else if (bInvalidNum) {
337                 msg_ui_composer_common_tickernoti(cd, COMPOSER_TICKERNOTI_INVALID_RECP);
338         } else if (bDup) {
339                 msg_ui_composer_common_tickernoti(cd, COMPOSER_TICKERNOTI_DUP_RECP);
340         }
341
342         g_free(num_id_list);
343
344         D_LEAVE;
345 }
346
347 struct ui_gadget *ug_contact_checklist_create(void *data)
348 {
349         D_ENTER;
350
351         struct ui_gadget *ug;
352         struct ug_cbs cbs = {0,};
353
354         bundle *b;
355
356         b = bundle_create();
357         if (!b)
358                 return NULL;
359
360         char contact_ug[DEF_BUF_LEN] = { 0, };
361
362         snprintf(contact_ug, sizeof(contact_ug)-1, "%d", CT_UG_REQUEST_CHECK_FOR_NUMBER);
363         bundle_add(b, MSG_BUNDLE_KEY_TYPE, contact_ug);
364         bundle_add(b, MSG_BUNDLE_KEY_REQUEST_TYPE, MSG_BUNDLE_VALUE_CONTACT_CHECK_LIST);
365
366         cbs.layout_cb = __ug_layout_cb;
367         cbs.result_cb = __ug_contacts_checklist_result_cb;
368         cbs.destroy_cb = __ug_closed_cb;
369         cbs.priv = data;
370
371         MSG_COMPOSER_VIEW_DATA_S *cd = (MSG_COMPOSER_VIEW_DATA_S *)data;
372
373         ug = ug_create(cd->ug, TXT_UG_NAME_CONTACT, UG_MODE_FULLVIEW, b, &cbs);
374
375         bundle_free(b);
376
377         D_LEAVE;
378         return ug;
379 }
380
381 struct ui_gadget *msg_ui_composer_launch_composer_create(MSG_COMPOSER_VIEW_DATA_S *cd, bundle *b)
382 {
383         D_ENTER;
384         D_MSG_RETVM_IF(cd == NULL, NULL, "Composer data is NULL");
385
386         struct ui_gadget *ug;
387         struct ug_cbs cbs;
388
389         cbs.layout_cb = __ug_layout_cb;
390         cbs.result_cb = NULL;
391         cbs.destroy_cb = __ug_closed_cb;
392         cbs.priv = cd;
393
394         ug = ug_create(cd->ug, MSG_COMPOSER_UG_NAME, UG_MODE_FULLVIEW, b, &cbs);
395         cd->loaded_ug = ug;
396         D_LEAVE;
397         return ug;
398 }
399
400 COMPOSER_RETURN_TYPE_E msg_ui_composer_external_ug_call(MSG_COMPOSER_VIEW_DATA_S *cd, COMPOSER_EXT_TYPE_E ext_type, void *reserved)
401 {
402         D_ENTER;
403
404         COMPOSER_RETURN_TYPE_E ret = COMPOSER_RETURN_SUCCESS;
405         struct ui_gadget *external_ug = NULL;
406
407         switch (ext_type) {
408         case COMPOSER_EXT_TYPE_LOG:
409                 external_ug = ug_calllog_create(cd);
410                 break;
411
412         case COMPOSER_EXT_TYPE_CONTACTLIST:
413                 external_ug = ug_contact_checklist_create(cd);
414                 break;
415         default:
416                 D_MSG("ERROR ext type = %d!!!!!!!!!!!!", ext_type);
417                 ret = COMPOSER_RETURN_FAIL;
418                 break;
419         }
420
421         if (external_ug) {
422                 cd->loaded_ug = external_ug;
423                 elm_object_focus_set(msg_ui_composer_last_focused_entry_get(cd), EINA_FALSE);
424         }
425
426         D_LEAVE;
427         return ret;
428 }
429
430 COMPOSER_RETURN_TYPE_E msg_ui_composer_external_call(MSG_COMPOSER_VIEW_DATA_S *cd, COMPOSER_EXT_TYPE_E ext_type, void *reserved)
431 {
432         D_ENTER;
433
434         COMPOSER_RETURN_TYPE_E ret = COMPOSER_RETURN_SUCCESS;
435         D_MSG("Launch external call type[%d]",ext_type);
436
437         switch (ext_type) {
438         case COMPOSER_EXT_TYPE_LOG:
439         case COMPOSER_EXT_TYPE_CONTACTLIST:
440                 ret = msg_ui_composer_external_ug_call(cd, ext_type, reserved);
441                 break;
442         default:
443                 D_MSG("ERROR ext type = %d!!!!!!!!!!!!", ext_type);
444                 ret = COMPOSER_RETURN_FAIL;
445                 break;
446         }
447
448         D_LEAVE;
449         return ret;
450 }
451