summaryrefslogtreecommitdiff
path: root/src/cts-favorite.c
blob: b43e43663188e5c0a7976baf5a643c800a16cf34 (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
/*
 * Contacts Service
 *
 * Copyright (c) 2010 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Contact: Youngjae Shin <yj99.shin@samsung.com>
 *
 * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
 *
 * 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 "cts-internal.h"
#include "cts-schema.h"
#include "cts-sqlite.h"
#include "cts-utils.h"
#include "cts-restriction.h"
#include "cts-favorite.h"

API int contacts_svc_set_favorite(cts_favor_type op, int related_id)
{
	int ret;
	double prio = 0.0;
	cts_stmt stmt;
	char query[CTS_SQL_MAX_LEN] = {0};

	retvm_if(CTS_FAVOR_PERSON != op && CTS_FAVOR_NUMBER != op, CTS_ERR_ARG_INVALID,
			"op(%d) is invalid", op);

	snprintf(query, sizeof(query),
			"SELECT MAX(favorite_prio) FROM %s", CTS_TABLE_FAVORITES);

	stmt = cts_query_prepare(query);
	retvm_if(NULL == stmt, CTS_ERR_DB_FAILED, "cts_query_prepare() Failed");

	ret = contacts_svc_begin_trans();
	retvm_if(ret, ret, "contacts_svc_begin_trans() Failed(%d)", ret);

	ret = cts_stmt_step(stmt);
	if (CTS_TRUE == ret) {
		prio = cts_stmt_get_dbl(stmt, 0);
	}
	else if (CTS_SUCCESS != ret) {
		ERR("cts_stmt_step() Failed(%d)", ret);
		cts_stmt_finalize(stmt);
		contacts_svc_end_trans(false);
		return ret;
	}
	cts_stmt_finalize(stmt);

	prio = prio + 1.0;
	snprintf(query, sizeof(query),
			"INSERT INTO %s SELECT NULL, %d, contact_id, %f FROM %s WHERE person_id = %d",
			CTS_TABLE_FAVORITES, op, prio, CTS_TABLE_CONTACTS, related_id);

	ret = cts_query_exec(query);
	if (CTS_SUCCESS != ret)
	{
		ERR("cts_query_exec() Failed(%d)", ret);
		contacts_svc_end_trans(false);
		return ret;
	}

	cts_set_favor_noti();

	ret = contacts_svc_end_trans(true);
	if (ret < CTS_SUCCESS)
		return ret;
	else
		return CTS_SUCCESS;
}

API int contacts_svc_unset_favorite(cts_favor_type op, int related_id)
{
	int ret;
	cts_stmt stmt;
	char query[CTS_SQL_MIN_LEN] = {0};

	if (CTS_FAVOR_PERSON == op) {
		snprintf(query, sizeof(query), "DELETE FROM %s WHERE type = %d AND related_id IN "
				"(SELECT contact_id FROM %s WHERE person_id = %d)",
				CTS_TABLE_FAVORITES, CTS_FAVOR_PERSON, CTS_TABLE_CONTACTS, related_id);
	} else if (CTS_FAVOR_NUMBER == op) {
		snprintf(query, sizeof(query), "DELETE FROM %s WHERE type = %d AND related_id = %d",
				CTS_TABLE_FAVORITES, CTS_FAVOR_NUMBER, related_id);
	} else {
		ERR("op(%d) is invalid", op);
		return CTS_ERR_ARG_INVALID;
	}

	stmt = cts_query_prepare(query);
	retvm_if(NULL == stmt, CTS_ERR_DB_FAILED, "cts_query_prepare() Failed");

	ret = contacts_svc_begin_trans();
	if (ret) {
		cts_stmt_finalize(stmt);
		ERR("contacts_svc_begin_trans() Failed(%d)", ret);
		return ret;
	}

	ret = cts_stmt_step(stmt);
	if (CTS_SUCCESS != ret)
	{
		ERR("cts_stmt_step() Failed(%d)", ret);
		cts_stmt_finalize(stmt);
		contacts_svc_end_trans(false);
		return ret;
	}
	ret = cts_db_change();
	cts_stmt_finalize(stmt);

	if (0 < ret) {
		cts_set_favor_noti();
		ret = contacts_svc_end_trans(true);
	}
	else {
		contacts_svc_end_trans(false);
		ret = CTS_ERR_NO_DATA;
	}

	if (ret < CTS_SUCCESS)
		return ret;
	else
		return CTS_SUCCESS;
}

API int contacts_svc_delete_favorite(int favorite_id)
{
	int ret;
	cts_stmt stmt;
	char query[CTS_SQL_MIN_LEN] = {0};

	snprintf(query, sizeof(query), "DELETE FROM %s WHERE id = %d",
			CTS_TABLE_FAVORITES, favorite_id);

	ret = contacts_svc_begin_trans();
	retvm_if(ret, ret, "contacts_svc_begin_trans() Failed(%d)", ret);

	stmt = cts_query_prepare(query);
	retvm_if(NULL == stmt, CTS_ERR_DB_FAILED, "cts_query_prepare() Failed");

	ret = cts_stmt_step(stmt);
	if (CTS_SUCCESS != ret)
	{
		ERR("cts_stmt_step() Failed(%d)", ret);
		cts_stmt_finalize(stmt);
		contacts_svc_end_trans(false);
		return ret;
	}
	ret = cts_db_change();
	cts_stmt_finalize(stmt);

	if (0 < ret) {
		cts_set_favor_noti();
		ret = contacts_svc_end_trans(true);
	}
	else {
		contacts_svc_end_trans(false);
		ret = CTS_ERR_NO_DATA;
	}

	if (ret < CTS_SUCCESS)
		return ret;
	else
		return CTS_SUCCESS;
}

static inline int cts_modify_favorite_prio(int favorite_id, int front, int back)
{
	int ret;
	cts_stmt stmt;
	double front_prio=0.0, back_prio=0.0, prio;
	char query[CTS_SQL_MIN_LEN] = {0};

	snprintf(query, sizeof(query), "SELECT favorite_prio FROM %s WHERE id = ?",
			CTS_TABLE_FAVORITES);

	stmt = cts_query_prepare(query);
	retvm_if(NULL == stmt, CTS_ERR_DB_FAILED, "cts_query_prepare() Failed");

	cts_stmt_bind_int(stmt, 1, front);
	ret = cts_stmt_step(stmt);
	if (CTS_TRUE == ret)
		front_prio = cts_stmt_get_dbl(stmt, 0);

	cts_stmt_reset(stmt);
	cts_stmt_bind_int(stmt, 1, back);
	ret = cts_stmt_step(stmt);
	if (CTS_TRUE == ret)
		back_prio = cts_stmt_get_dbl(stmt, 0);

	cts_stmt_finalize(stmt);

	retvm_if(0.0 == front_prio && 0.0 == back_prio, CTS_ERR_ARG_INVALID,
			"The indexes for front and back are invalid.");

	if (0.0 == back_prio)
		prio = front_prio + 1;
	else
		prio = (front_prio + back_prio) / 2;

	snprintf(query, sizeof(query),
			"UPDATE %s SET favorite_prio = %f WHERE id = %d",
			CTS_TABLE_FAVORITES, prio, favorite_id);

	ret = cts_query_exec(query);
	if (CTS_SUCCESS != ret)
	{
		ERR("cts_query_exec() Failed(%d)", ret);
		return ret;
	}

	return CTS_SUCCESS;
}

API int contacts_svc_favorite_order(int favorite_id,
		int front_favorite_id, int back_favorite_id)
{
	int ret;

	ret = contacts_svc_begin_trans();
	retvm_if(ret, ret, "contacts_svc_begin_trans() Failed(%d)", ret);

	ret = cts_modify_favorite_prio(favorite_id, front_favorite_id, back_favorite_id);

	if (CTS_SUCCESS != ret)
	{
		ERR("cts_modify_favorite_prio() Failed(%d)", ret);
		contacts_svc_end_trans(false);
		return ret;
	}
	cts_set_favor_noti();

	ret = contacts_svc_end_trans(true);
	if (ret < CTS_SUCCESS)
		return ret;
	else
		return CTS_SUCCESS;
}

API int contacts_svc_set_speeddial(int speed_num, int number_id)
{
	int ret;
	char query[CTS_SQL_MAX_LEN] = {0};

	ret = contacts_svc_begin_trans();
	retvm_if(ret, ret, "contacts_svc_begin_trans() Failed(%d)", ret);

	snprintf(query, sizeof(query),
			"INSERT INTO %s(speed_num, number_id) VALUES(%d, %d)",
			CTS_TABLE_SPEEDDIALS, speed_num, number_id);

	ret = cts_query_exec(query);
	if (CTS_SUCCESS != ret) {
		ERR("cts_query_exec() Failed(%d)", ret);
		contacts_svc_end_trans(false);
		return ret;
	}
	cts_set_speed_noti();

	ret = contacts_svc_end_trans(true);
	if (ret < CTS_SUCCESS)
		return ret;
	else
		return CTS_SUCCESS;
}

API int contacts_svc_unset_speeddial(int speed_num)
{
	int ret;
	cts_stmt stmt;
	char query[CTS_SQL_MIN_LEN] = {0};

	snprintf(query, sizeof(query), "DELETE FROM %s WHERE speed_num = %d",
			CTS_TABLE_SPEEDDIALS, speed_num);

	stmt = cts_query_prepare(query);
	retvm_if(NULL == stmt, CTS_ERR_DB_FAILED, "cts_query_prepare() Failed");

	ret = contacts_svc_begin_trans();
	if (ret) {
		cts_stmt_finalize(stmt);
		ERR("contacts_svc_begin_trans() Failed(%d)", ret);
		return ret;
	}

	ret = cts_stmt_step(stmt);
	if (CTS_SUCCESS != ret) {
		ERR("cts_stmt_step() Failed(%d)", ret);
		cts_stmt_finalize(stmt);
		contacts_svc_end_trans(false);
		return ret;
	}
	ret = cts_db_change();
	cts_stmt_finalize(stmt);

	if (0 < ret) {
		cts_set_speed_noti();
		ret = contacts_svc_end_trans(true);
	}
	else {
		contacts_svc_end_trans(false);
		ret = CTS_ERR_NO_DATA;
	}

	if (ret < CTS_SUCCESS)
		return ret;
	else
		return CTS_SUCCESS;
}

API int contacts_svc_get_speeddial(int speed_num, CTSvalue **value)
{
	int ret;
	cts_stmt stmt;
	const char *data;
	cts_number *number;
	char query[CTS_SQL_MAX_LEN] = {0};

	if (cts_restriction_get_permit())
		data = CTS_TABLE_DATA;
	else
		data = CTS_TABLE_RESTRICTED_DATA_VIEW;

	snprintf(query, sizeof(query),
			"SELECT A.id, A.data1, A.data2, A.contact_id FROM %s A, %s B "
			"WHERE A.datatype = %d AND B.speed_num = %d AND A.id = B.number_id",
			data, CTS_TABLE_SPEEDDIALS, CTS_DATA_NUMBER, speed_num);

	stmt = cts_query_prepare(query);
	retvm_if(NULL == stmt, CTS_ERR_DB_FAILED, "cts_query_prepare() Failed");

	ret = cts_stmt_step(stmt);
	if (CTS_TRUE != ret)
	{
		ERR("cts_stmt_step() Failed(%d)", ret);
		cts_stmt_finalize(stmt);
		return CTS_ERR_DB_RECORD_NOT_FOUND;
	}

	number = (cts_number *)contacts_svc_value_new(CTS_VALUE_NUMBER);
	if (number) {
		ret = CTS_SUCCESS;
		number->v_type = CTS_VALUE_RDONLY_NUMBER;
		number->embedded = true;
		number->id = cts_stmt_get_int(stmt, 0);
		number->type = cts_stmt_get_int(stmt, 1);
		number->number = SAFE_STRDUP(cts_stmt_get_text(stmt, 2));
		ret = cts_stmt_get_int(stmt, 3);

		*value = (CTSvalue*) number;

		cts_stmt_finalize(stmt);
		return ret;
	}
	else {
		ERR("contacts_svc_value_new() Failed(%d)", ret);
		cts_stmt_finalize(stmt);
		return CTS_ERR_OUT_OF_MEMORY;
	}
}