summaryrefslogtreecommitdiff
path: root/lib/list/ct-list-utils.c
blob: de6eb072a28452e59ee22278282456ff74f53972 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
/*
 * Copyright 2012  Samsung Electronics Co., Ltd
 *
 * Licensed under the Flora License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.tizenopensource.org/license
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include <errno.h>
#include <fcntl.h>
#include <ui-gadget-module.h>
#include "phone.h"
#include "ct-detail.h"
#include "ct-list.h"
#include "ct-list-contact-view.h"
#include "ct-list-utils.h"

int ct_list_parse_bundle(service_h service, ct_list_data* clist_d)
{
	char* val = NULL;

	if (service) {
		service_get_extra_data(service, CT_UG_BUNDLE_TYPE, &val);
		if (val) clist_d->ug_request = atoi(val);
		PH_DBG("type = %d", clist_d->ug_request);
		free(val);

		switch (clist_d->ug_request) {
		case CT_UG_REQUEST_LIST:
		case CT_UG_REQUEST_LIST_WITH_BACK:
		case CT_UG_REQUEST_SELECT_CONTACT:
		case CT_UG_REQUEST_SELECT_VCARD:
			break;
		case CT_UG_REQUEST_SELECT_NUMBER:
			clist_d->list_op = CT_GET_LIST_ALL_CONTACT_HAD_NUMBER;
			break;
		case CT_UG_REQUEST_SELECT_EMAIL:
			clist_d->list_op = CT_GET_LIST_ALL_CONTACT_HAD_EMAIL;
			break;
		case CT_UG_REQUEST_UPDATE_CONTACT:
			service_get_extra_data(service, CT_UG_BUNDLE_NUM, &clist_d->ug_data);
			if(clist_d->ug_data)
				break;
			service_get_extra_data(service, CT_UG_BUNDLE_EMAIL, &clist_d->ug_data);
			if(clist_d->ug_data)
				break;
			service_get_extra_data(service, CT_UG_BUNDLE_WEB, &clist_d->ug_data);
			if(clist_d->ug_data)
				break;
			break;
		case CT_UG_REQUEST_UPDATE_WITH_NUM:
			service_get_extra_data(service, CT_UG_BUNDLE_NUM, &clist_d->ug_data);
			break;
		case CT_UG_REQUEST_UPDATE_WITH_EMAIL:
			service_get_extra_data(service, CT_UG_BUNDLE_EMAIL, &clist_d->ug_data);
			break;
		case CT_UG_REQUEST_UPDATE_WITH_WEB:
			service_get_extra_data(service, CT_UG_BUNDLE_WEB, &clist_d->ug_data);
			break;
		case CT_UG_REQUEST_CHECK:
			val = NULL;
			service_get_extra_data(service, CT_UG_BUNDLE_MAX, &val);
			if (val) clist_d->cnt_max = atoi(val);
			free(val);
			break;
		case CT_UG_REQUEST_CHECK_FOR_NUMBER:
			val = NULL;
			clist_d->list_op = CT_GET_LIST_ALL_CONTACT_HAD_NUMBER;
			service_get_extra_data(service, CT_UG_BUNDLE_MAX, &val);
			if (val) clist_d->cnt_max = atoi(val);
			free(val);
			break;
		case CT_UG_REQUEST_CHECK_FOR_EMAIL:
			val = NULL;
			clist_d->list_op = CT_GET_LIST_ALL_CONTACT_HAD_EMAIL;
			service_get_extra_data(service, CT_UG_BUNDLE_MAX, &val);
			if (val) clist_d->cnt_max = atoi(val);
			free(val);
			break;
		case PH_UG_REQUEST_SET_WITH_IMG:
		case PH_UG_REQUEST_SET_WITH_RINGTONE:
		case CT_UG_REQUEST_SAVE_IMG:
		case CT_UG_REQUEST_SAVE_RINGTONE:
			service_get_extra_data(service, CT_UG_BUNDLE_PATH, &clist_d->ug_data);
			if (-1 == access(clist_d->ug_data, R_OK)) {
				ERR("Invalid filepath(%s)", clist_d->ug_data);
				return -2;
			}
			break;
		default:
			ERR("Invalid request(%d)", clist_d->ug_request);
			return -1;
		}
	}
	return 0;
}

static void list_img_crop_result_for_add(ui_gadget_h ug,service_h service, void *data)
{
	CTSvalue *ct_val;
	ct_detail_data *cdetail_d = data;

	p_retm_if(NULL == ug || NULL == data, "ug or data is NULL");

	if (service) {
		char *normal = NULL;
		char *full = NULL;

		service_get_extra_data(service, "crop_image_path", &normal);
		p_retm_if(!normal, "return value is NULL.\n");

		service_get_extra_data(service, "image_path", &full);

		contacts_svc_struct_get_value(cdetail_d->contact, CTS_CF_BASE_INFO_VALUE, &ct_val);
		if (NULL == ct_val)
			ct_val = contacts_svc_value_new(CTS_VALUE_CONTACT_BASE_INFO);
		contacts_svc_value_set_str(ct_val, CTS_BASE_VAL_IMG_PATH_STR, normal);
		contacts_svc_value_set_str(ct_val, CTS_BASE_VAL_FULL_IMG_PATH_STR, full);
		contacts_svc_struct_store_value(cdetail_d->contact, CTS_CF_BASE_INFO_VALUE, ct_val);

		free(normal);
		free(full);
	}

	ug_destroy(ug);
	cdetail_d->child_ug = NULL;
	ctui_create_input_view(cdetail_d);
}

static void list_destory_detail_child_ug(ui_gadget_h ug, void *priv)
{
	ct_detail_data *cdetail_d = priv;
	p_retm_if(NULL == ug, "ug is NULL");
	ug_destroy(ug);

	p_retm_if(NULL == priv, "priv is NULL");
	cdetail_d->child_ug = NULL;
}

void ct_list_destroy_child_ug(void *data)
{
	ct_list_data *clist_d = (ct_list_data*)data;
	clist_d->child_ug = NULL;
}

API void ctui_list_update_with_data(ph_view_info *view_info, void *data,
	int ct_id, int grp_id, int ab_id)
{
	int ret;
	ct_detail_data *cdetail_d;
	GSList *ct_list;
	CTSvalue *ct_val;
	struct ug_cbs cbs = {0};

	cdetail_d = calloc(1, sizeof(ct_detail_data));
	p_retm_if(!cdetail_d, "cdetail_d is NULL, Not enough memory");

	cdetail_d->win = view_info->win;
	cdetail_d->navi = view_info->navi;
	cdetail_d->ug = view_info->ug;
	cdetail_d->prev_navi_item = elm_naviframe_top_item_get(view_info->navi);
	cdetail_d->prev_view_data = data;
	cdetail_d->contact_id = ct_id;
	cdetail_d->base_grp = grp_id;
	cdetail_d->base_ab = ab_id;
	cdetail_d->ug_request = view_info->ug_request;

	if (ct_id)
		ret = contacts_svc_get_contact(ct_id, &(cdetail_d->contact));
	else
		ret = -1;

	if (CTS_SUCCESS != ret) {
		ERR("contacts_svc_get_contact() Failed(%d)", ret);
		cdetail_d->contact = contacts_svc_struct_new(CTS_STRUCT_CONTACT);
	}

	switch (view_info->ug_request) {
	//case PH_UG_REQUEST_SET_WITH_NUM:
	case CT_UG_REQUEST_UPDATE_WITH_NUM:
		ct_list = NULL;
		ct_val = contacts_svc_value_new(CTS_VALUE_NUMBER);
		contacts_svc_value_set_str(ct_val, CTS_NUM_VAL_NUMBER_STR, view_info->ug_data);
		ct_list = g_slist_append(ct_list, ct_val);
		contacts_svc_struct_store_list(cdetail_d->contact, CTS_CF_NUMBER_LIST, ct_list);
		g_slist_free(ct_list);
		break;
	//case PH_UG_REQUEST_SET_WITH_EMAIL:
	case CT_UG_REQUEST_UPDATE_WITH_EMAIL:
		ct_list = NULL;
		ct_val = contacts_svc_value_new(CTS_VALUE_EMAIL);
		contacts_svc_value_set_str(ct_val, CTS_EMAIL_VAL_ADDR_STR, view_info->ug_data);
		ct_list = g_slist_append(ct_list, ct_val);
		contacts_svc_struct_store_list(cdetail_d->contact, CTS_CF_EMAIL_LIST, ct_list);
		g_slist_free(ct_list);
		break;
	//case PH_UG_REQUEST_SET_WITH_WEB:
	case CT_UG_REQUEST_UPDATE_WITH_WEB:
		ct_list = NULL;
		ct_val = contacts_svc_value_new(CTS_VALUE_WEB);
		contacts_svc_value_set_str(ct_val, CTS_WEB_VAL_ADDR_STR, view_info->ug_data);
		ct_list = g_slist_append(ct_list, ct_val);
		contacts_svc_struct_store_list(cdetail_d->contact, CTS_CF_WEB_ADDR_LIST, ct_list);
		g_slist_free(ct_list);
		break;
	case CT_UG_REQUEST_UPDATE_CONTACT:
		break;
	case PH_UG_REQUEST_SET_WITH_IMG:
	case CT_UG_REQUEST_SAVE_IMG: //only handle for add
		cbs.result_cb = list_img_crop_result_for_add;
		cbs.destroy_cb = list_destory_detail_child_ug;
		cbs.priv = cdetail_d;

		cdetail_d->child_ug = phone_image_crop_ug(cdetail_d->ug, view_info->ug_data, &cbs);
		return;
	case PH_UG_REQUEST_SET_WITH_RINGTONE:
	case CT_UG_REQUEST_SAVE_RINGTONE: //only handle for add
		contacts_svc_struct_get_value(cdetail_d->contact, CTS_CF_BASE_INFO_VALUE, &ct_val);
		if (NULL == ct_val)
			ct_val = contacts_svc_value_new(CTS_VALUE_CONTACT_BASE_INFO);
		contacts_svc_value_set_str(ct_val, CTS_BASE_VAL_RINGTONE_PATH_STR, view_info->ug_data);
		contacts_svc_struct_store_value(cdetail_d->contact, CTS_CF_BASE_INFO_VALUE, ct_val);
		break;
	default:
		ERR("Invalid type(%d)", view_info->ug_request);
		break;
	}
	ctui_create_input_view(cdetail_d);
}

void ct_list_call(void *data, Evas_Object *obj, void *event_info)
{
	ct_contact_list *item = data;
	p_retm_if(NULL == item, "parameter(item) is NULL");
	p_retm_if(NULL == item->number || '\0' == *item->number, "number is NULL");
	phone_launch_voice_call(item->id, item->number);
}

void ct_list_msg(void *data, Evas_Object *obj, void *event_info)
{
	ct_list_data *clist_d;
	ct_contact_list *item = data;

	p_retm_if(NULL == data, "parameter(item) is NULL");
	p_retm_if(NULL == item->number || '\0' == *item->number, "number is NULL");

	clist_d = (ct_list_data *)evas_object_data_get(obj, "clist_d");
	p_retm_if(NULL == clist_d, "evas_object_data_get() return NULL");

	clist_d->child_ug = ctui_msg_ug(clist_d->ug, item->number, item->id, NULL,
			ct_list_destroy_child_ug, clist_d);
}

void ct_list_popup_hide_cb(void *data, Evas_Object *obj, void *event_info)
{
	ct_list_data *clist_d;
	Elm_Object_Item *item;
	ct_contact_list *contact;

	clist_d = evas_object_data_get(data, "clist_d");
	p_retm_if(NULL == clist_d, "evas_object_data_get() return NULL");
	evas_object_del(clist_d->popup);
	clist_d->popup = NULL;

	if (NULL != obj) {		// Click "Close"
		item = evas_object_data_get(data, "item");
		p_retm_if(NULL == item, "evas_object_data_get() return NULL");

		contact = elm_object_item_data_get(item);
		p_retm_if(NULL == contact, "elm_object_item_data_get() return NULL");

		contact->checked = EINA_FALSE;
		clist_d->cnt_checked--;
		elm_genlist_item_update(item);
	}
}

static void list_sweep_delete_contact(void *data, Evas_Object *obj, void *event_info)
{
	int person_id;
	int ret;
	ct_list_data *clist_d;

	clist_d = evas_object_data_get(obj, "clist_d");
	p_retm_if(NULL == clist_d, "evas_object_data_get() return NULL");
	evas_object_del(clist_d->popup);
	clist_d->popup = NULL;

	person_id = ctui_get_person_id((int)data);
	evas_object_data_set(clist_d->navi, "ct_list_update_ui", (void *)1);

	ret =  contacts_svc_delete_person(person_id);

	if (CTS_SUCCESS == ret)
		phone_show_tickernoti(S_(CT_SYS_POP_SUCCESS));
	else
		phone_show_tickernoti(S_(PH_SYS_POP_FAILED));
}


void ct_list_sweep_del_cb(void *data, Evas_Object *obj, void *event_info)
{
	int id;
	ct_list_data *clist_d;
	Evas_Object *btn;

	clist_d = evas_object_data_get(obj, "clist_d");
	p_retm_if(NULL == clist_d, "evas_object_data_get() return NULL");

	id = (int)data;
	btn = phone_create_confirm_popup(&clist_d->popup, clist_d->win, T_(CT_GET_TEXT_BASE_OP,
				CTTEXT_CONFIRM_DELETE_CONTACT), list_sweep_delete_contact, (void *)id);
	evas_object_data_set(btn,"clist_d", clist_d);
}

void ct_list_get_title(ct_list_data *clist_d, int list_op, int id_ab, int id_grp)
{
	switch(list_op) {
	case CT_GET_LIST_ALL_CONTACTS:
	case CT_GET_LIST_ALL_CONTACT_HAD_EMAIL:
	case CT_GET_LIST_ALL_CONTACT_HAD_NUMBER:
	{
		char buf[PH_TEXT_SHORT_LEN];
		snprintf(buf, sizeof(buf), "%s (%d)", T_(CT_GET_TEXT_BASIC, CTTEXT_ALL_CONTACTS), clist_d->cnt_list);
		clist_d->title = strdup(buf);
	}
		break;
	}
}

static PTHREAD_FN list_add_contacts_to_favorite(void *data)
{
	int ret;
	ct_list_data *clist_d = data;
	ph_progress_info *p_info = clist_d->p_info;
	Eina_List *l;
	ct_contact_list *contact;

	EINA_LIST_FOREACH(clist_d->list, l, contact) {
		if (!p_info->alive) {
			p_info->result = FALSE;
			break;
		}
		if (contact && contact->checked) {
			ret = contacts_svc_set_favorite(CTS_FAVOR_CONTACT, contact->id);
			if (ret < CTS_SUCCESS) {
				p_info->result = FALSE;
				p_info->completed = true;
				break;
			}
			p_info->cnt++;
		}
	}
	pthread_exit(NULL);
}

static Eina_Bool list_add_contacts_timer_cb(void *data)
{
	ct_list_data *clist_d = data;
	ph_progress_info *p_info = clist_d->p_info;

	p_retvm_if(NULL == p_info, ECORE_CALLBACK_CANCEL, "parameter(clist_d) is NULL");

	if (!p_info->completed) {
		char count[PH_TEXT_SHORT_LEN];
		char percent[PH_TEXT_SHORT_LEN];
		double value = (double)p_info->cnt / (double)p_info->cnt_checked_total;

		if (!p_info->alive) {
			p_info->result = FALSE;
			p_info->completed = true;
			return ECORE_CALLBACK_RENEW;
		}

		elm_progressbar_value_set(p_info->progressbar, value);
		snprintf(percent, sizeof(percent), "%d%%", (int)(100.0 * (double)p_info->cnt/(double)p_info->cnt_checked_total));
		snprintf(count, sizeof(count), "%d/%d", p_info->cnt, p_info->cnt_checked_total);
		edje_object_part_text_set(elm_layout_edje_get(p_info->layout), "elm.text.subtext1", percent);
		edje_object_part_text_set(elm_layout_edje_get(p_info->layout), "elm.text.subtext2", count);

		if (p_info->cnt == p_info->cnt_checked_total)
			p_info->completed = true;

		return ECORE_CALLBACK_RENEW;
	}
	else {
		int ret;
		const char *message;

		pthread_join(p_info->thread, NULL);

		if (TRUE == p_info->result) {
			message = S_(CT_SYS_POP_SUCCESS);
			ret = contacts_svc_end_trans(true);
		}
		else {
			message = S_(PH_SYS_POP_FAILED);
			ret = contacts_svc_end_trans(false);
		}
		p_warn_if(ret < CTS_SUCCESS, "contacts_svc_end_trans() Failed(%d)", ret);

		evas_object_del(clist_d->popup);
		clist_d->popup = NULL;

		phone_show_tickernoti(message);
		elm_naviframe_item_pop_to(evas_object_data_get(clist_d->navi, "navi_it_pop_to"));
		return ECORE_CALLBACK_CANCEL;
	}
}

void ct_list_add_contacts_to_favorite(Evas_Object *win, Evas_Object *navi, int cnt)
{
	int ret;
	ct_list_data *clist_d;
	ph_progress_info *p_info;

	clist_d = evas_object_data_get(navi, "favor_list_d");
	p_retm_if(NULL == clist_d, "evas_object_data_get return NULL");

	p_retm_if(clist_d->p_info, "thread is running");

	p_info = calloc(1, sizeof(ph_progress_info));
	p_retm_if(NULL == p_info, "calloc return NULL");

	clist_d->p_info = p_info;
	p_info->cnt_checked_total = cnt;
	p_info->alive = true;
	p_info->result = TRUE;

	ret = contacts_svc_begin_trans();
	if (CTS_SUCCESS != ret) {
		ERR("contacts_svc_begin_trans() Failed(%d)", ret);
		free(p_info);
		clist_d->p_info = NULL;
		return;
	}

	ret = pthread_create(&p_info->thread, NULL, list_add_contacts_to_favorite, clist_d);
	if (0 != ret) {
		ERR("Thread creation failed(%d)", ret);
		free(p_info);
		clist_d->p_info = NULL;
		ret = contacts_svc_end_trans(false);
		p_warn_if(ret < CTS_SUCCESS, "contacts_svc_end_trans() Failed(%d)", ret);
		return;
	}

	p_info->timer = ecore_timer_add(0.2, list_add_contacts_timer_cb, clist_d);
	if (NULL == p_info->timer) {
		p_info->alive = false;
		pthread_join(p_info->thread, NULL);
		free(p_info);
		clist_d->p_info = NULL;
		ret = contacts_svc_end_trans(false);
		p_warn_if(ret < CTS_SUCCESS, "contacts_svc_end_trans() Failed(%d)", ret);
		return;
	}
	clist_d->popup = phone_progressbar_popup(win, p_info, T_(CT_GET_TEXT_BASIC, CTTEXT_ADDING_ING));
}

bool ct_list_make_vcard(int contactid, char *path, int path_length)
{
	int ret, fd;
	char display[PH_TEXT_MAX_LEN];
	char *vcard = NULL;
	CTSvalue *name;
	CTSstruct *contact;
	ret = contacts_svc_get_contact(contactid, &contact);
	if (ret < CTS_SUCCESS) {
		ERR("contacts_svc_get_contact() Failed(%d)", ret);
		return false;
	}

	ret = contacts_svc_struct_get_value(contact, CTS_CF_NAME_VALUE, &name);
	if (ret < CTS_SUCCESS) {
		ERR("contacts_svc_struct_get_value() Failed(%d)", ret);
		return false;
	}
	//display = contacts_svc_value_get_str(name, CTS_NAME_VAL_DISPLAY_STR);

	ctui_get_display_name(name, display, sizeof(display));

	if (display && '\0' == display[0]) {
		snprintf(display, sizeof(display), "%s", T_(CT_GET_TEXT_ERR, CTTEXT_NONAME));
	}
	snprintf(path, path_length, "%s/%s.vcf", UGDATAIDR, display);

	ret = contacts_svc_get_vcard_from_contact(contact, &vcard);
	if (ret < CTS_SUCCESS) {
		ERR("contacts_svc_get_vcard_from_contact() Failed(%d)", ret);
		return false;
	}
	contacts_svc_struct_free(contact);

	fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, 0644);

	if (-1 == write(fd, vcard, strlen(vcard))) {
		ERR("write()... Failed(%s)", strerror(errno));
		free(vcard);
		return false;
	}
	free(vcard);
	return true;


}