Move notification_get_text_input_max_length to internal
[platform/core/api/notification.git] / src / notification_setting_service.c
1 /*
2  * Copyright (c) 2000 - 2016 Samsung Electronics Co., Ltd. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
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 <errno.h>
18 #include <unistd.h>
19 #include <stdio.h>
20 #include <string.h>
21
22 #include <sqlite3.h>
23 #include <db-util.h>
24 #include <tizen.h>
25
26 #include <notification.h>
27 #include <notification_db.h>
28 #include <notification_error.h>
29 #include <notification_debug.h>
30 #include <notification_private.h>
31 #include <notification_setting.h>
32 #include <notification_setting_internal.h>
33 #include <notification_setting_service.h>
34
35 static int _get_table_field_data_int(char  **table, int *buf, int index)
36 {
37         if ((table == NULL) || (buf == NULL) || (index < 0))  {
38                 /* LCOV_EXCL_START */
39                 NOTIFICATION_ERR("table[%p], buf[%p], index[%d]", table, buf, index);
40                 return false;
41                 /* LCOV_EXCL_STOP */
42         }
43
44         if (table[index] != NULL) {
45                 *buf = atoi(table[index]);
46                 return true;
47         }
48
49         /* LCOV_EXCL_START */
50         *buf = 0;
51         return false;
52         /* LCOV_EXCL_STOP */
53 }
54
55 static int _get_table_field_data_string(char **table, char **buf, int ucs2, int index)
56 {
57         int ret = false;
58
59         if ((table == NULL) || (buf == NULL) || (index < 0))  {
60                 /* LCOV_EXCL_START */
61                 NOTIFICATION_ERR("table[%p], buf[%p], index[%d]", table, buf, index);
62                 return false;
63                 /* LCOV_EXCL_STOP */
64         }
65
66         char *pTemp = table[index];
67         int sLen = 0;
68         if (pTemp == NULL) {
69                 *buf = NULL; /* LCOV_EXCL_LINE */
70         } else {
71                 sLen = strlen(pTemp);
72                 if (sLen) {
73                         *buf = (char *) malloc(sLen + 1);
74                         if (*buf == NULL) {
75                                 NOTIFICATION_ERR("malloc is failed"); /* LCOV_EXCL_LINE */
76                                 goto out;
77                         }
78                         memset(*buf, 0, sLen + 1);
79                         strncpy(*buf, pTemp, sLen);
80                 } else {
81                         *buf = NULL; /* LCOV_EXCL_LINE */
82                 }
83         }
84
85         ret = true;
86 out:
87
88         return ret;
89 }
90
91 EXPORT_API
92 int noti_setting_service_get_setting_by_package_name(const char *package_name, notification_setting_h *setting, uid_t uid)
93 {
94         int err = NOTIFICATION_ERROR_NONE;
95         sqlite3 *local_db_handle = NULL;
96         char *sql_query = NULL;
97         char **query_result = NULL;
98         int sql_return;
99         int row_count = 0;
100         int column_count = 0;
101         int i = 0;
102         int col_index = 0;
103         notification_setting_h result_setting_array = NULL;
104
105         if (package_name == NULL || setting == NULL) {
106                 NOTIFICATION_ERR("NOTIFICATION_ERROR_INVALID_PARAMETER"); /* LCOV_EXCL_LINE */
107                 err =  NOTIFICATION_ERROR_INVALID_PARAMETER;
108                 goto out;
109         }
110
111         sql_return = db_util_open(DBPATH, &local_db_handle, 0);
112
113         if (sql_return != SQLITE_OK || local_db_handle == NULL) {
114                 NOTIFICATION_ERR("db_util_open failed [%d]", sql_return); /* LCOV_EXCL_LINE */
115                 err = NOTIFICATION_ERROR_FROM_DB;
116                 goto out;
117         }
118
119         sql_query = sqlite3_mprintf("SELECT package_name, allow_to_notify, do_not_disturb_except, visibility_class, "
120                                     "pop_up_notification, lock_screen_content_level FROM %s "
121                                     "WHERE package_name = %Q AND uid = %d", NOTIFICATION_SETTING_DB_TABLE, package_name, uid);
122
123         if (!sql_query) {
124                 NOTIFICATION_ERR("fail to alloc query"); /* LCOV_EXCL_LINE */
125                 err = NOTIFICATION_ERROR_OUT_OF_MEMORY;
126                 goto out;
127         }
128
129         sql_return = sqlite3_get_table(local_db_handle, sql_query, &query_result, &row_count, &column_count, NULL);
130
131         if (sql_return != SQLITE_OK && sql_return != -1) {
132                 NOTIFICATION_ERR("sqlite3_get_table failed [%d][%s]", sql_return, sql_query); /* LCOV_EXCL_LINE */
133                 err = NOTIFICATION_ERROR_FROM_DB;
134                 goto out;
135         }
136
137         if (!row_count) {
138                 NOTIFICATION_DBG("No setting found for [%s]", package_name); /* LCOV_EXCL_LINE */
139                 err = NOTIFICATION_ERROR_NOT_EXIST_ID;
140                 goto out;
141         }
142
143         NOTIFICATION_DBG("row_count [%d] column_count [%d]", row_count, column_count);
144
145         row_count = 1;
146
147         if (!(result_setting_array = (struct notification_setting *)malloc(sizeof(struct notification_setting) * row_count))) {
148                 NOTIFICATION_ERR("malloc failed..."); /* LCOV_EXCL_LINE */
149                 err = NOTIFICATION_ERROR_OUT_OF_MEMORY;
150                 goto out;
151         }
152
153         col_index = column_count;
154
155         _get_table_field_data_string(query_result, &(result_setting_array[i].package_name), 1, col_index++);
156         _get_table_field_data_int(query_result, (int *)&(result_setting_array[i].allow_to_notify), col_index++);
157         _get_table_field_data_int(query_result, (int *)&(result_setting_array[i].do_not_disturb_except), col_index++);
158         _get_table_field_data_int(query_result, &(result_setting_array[i].visibility_class), col_index++);
159         _get_table_field_data_int(query_result, (int *)&(result_setting_array[i].pop_up_notification), col_index++);
160         _get_table_field_data_int(query_result, (int *)&(result_setting_array[i].lock_screen_content_level), col_index++);
161
162         *setting = result_setting_array;
163
164 out:
165         if (query_result)
166                 sqlite3_free_table(query_result);
167
168         if (sql_query)
169                 sqlite3_free(sql_query);
170
171         if (local_db_handle) {
172                 sql_return = db_util_close(local_db_handle);
173                 if (sql_return != SQLITE_OK)
174                         NOTIFICATION_WARN("fail to db_util_close - [%d]", sql_return); /* LCOV_EXCL_LINE */
175         }
176
177         return err;
178 }
179
180 EXPORT_API
181 int noti_setting_get_setting_array(notification_setting_h *setting_array, int *count, uid_t uid)
182 {
183         int err = NOTIFICATION_ERROR_NONE;
184         sqlite3 *local_db_handle = NULL;
185         char *sql_query = NULL;
186         char **query_result = NULL;
187         int sql_return;
188         int row_count = 0;
189         int column_count = 0;
190         int i = 0;
191         int col_index = 0;
192         notification_setting_h result_setting_array = NULL;
193
194         if (setting_array == NULL || count == NULL) {
195                 NOTIFICATION_ERR("NOTIFICATION_ERROR_INVALID_PARAMETER"); /* LCOV_EXCL_LINE */
196                 err =  NOTIFICATION_ERROR_INVALID_PARAMETER;
197                 goto out;
198         }
199
200         sql_return = db_util_open(DBPATH, &local_db_handle, 0);
201
202         if (sql_return != SQLITE_OK || local_db_handle == NULL) {
203                 NOTIFICATION_ERR("db_util_open failed [%d]", sql_return); /* LCOV_EXCL_LINE */
204                 err = NOTIFICATION_ERROR_FROM_DB;
205                 goto out;
206         }
207
208         sql_query = sqlite3_mprintf("SELECT package_name, allow_to_notify, do_not_disturb_except, visibility_class, "
209                                     "pop_up_notification, lock_screen_content_level FROM %s WHERE uid = %d "
210                                     "ORDER BY package_name", NOTIFICATION_SETTING_DB_TABLE, uid);
211
212         if (!sql_query) {
213                 NOTIFICATION_ERR("fail to alloc query"); /* LCOV_EXCL_LINE */
214                 err = NOTIFICATION_ERROR_OUT_OF_MEMORY;
215                 goto out;
216         }
217
218         sql_return = sqlite3_get_table(local_db_handle, sql_query, &query_result, &row_count, &column_count, NULL);
219
220         if (sql_return != SQLITE_OK && sql_return != -1) {
221                 NOTIFICATION_ERR("NOTIFICATION_ERROR_FROM_DB failed [%d][%s]", sql_return, sql_query);  /* LCOV_EXCL_LINE */
222                 err = NOTIFICATION_ERROR_FROM_DB;
223                 goto out;
224         }
225
226         if (!row_count) {
227                 NOTIFICATION_DBG("No setting found..."); /* LCOV_EXCL_LINE */
228                 err = NOTIFICATION_ERROR_NOT_EXIST_ID;
229                 goto out;
230         }
231
232         NOTIFICATION_DBG("row_count [%d] column_count [%d]", row_count, column_count);
233         if (!(result_setting_array = (struct notification_setting *)malloc(sizeof(struct notification_setting) * row_count))) {
234                 NOTIFICATION_ERR("malloc failed..."); /* LCOV_EXCL_LINE */
235                 err = NOTIFICATION_ERROR_OUT_OF_MEMORY;
236                 goto out;
237         }
238
239         col_index = column_count;
240
241         for (i = 0; i < row_count; i++) {
242                 _get_table_field_data_string(query_result, &(result_setting_array[i].package_name), 1, col_index++);
243                 _get_table_field_data_int(query_result, (int *)&(result_setting_array[i].allow_to_notify), col_index++);
244                 _get_table_field_data_int(query_result, (int *)&(result_setting_array[i].do_not_disturb_except), col_index++);
245                 _get_table_field_data_int(query_result, &(result_setting_array[i].visibility_class), col_index++);
246                 _get_table_field_data_int(query_result, (int *)&(result_setting_array[i].pop_up_notification), col_index++);
247                 _get_table_field_data_int(query_result, (int *)&(result_setting_array[i].lock_screen_content_level), col_index++);
248         }
249
250         *setting_array = result_setting_array;
251         *count = row_count;
252
253 out:
254         if (query_result)
255                 sqlite3_free_table(query_result);
256
257         if (sql_query)
258                 sqlite3_free(sql_query);
259
260         if (local_db_handle) {
261                 sql_return = db_util_close(local_db_handle);
262                 if (sql_return != SQLITE_OK)
263                         NOTIFICATION_WARN("fail to db_util_close - [%d]", sql_return); /* LCOV_EXCL_LINE */
264         }
265
266         return err;
267 }
268
269
270 EXPORT_API
271 int noti_system_setting_load_system_setting(notification_system_setting_h *system_setting, uid_t uid)
272 {
273         int err = NOTIFICATION_ERROR_NONE;
274         sqlite3 *local_db_handle = NULL;
275         char *sql_query = NULL;
276         char **query_result = NULL;
277         int sql_return;
278         int row_count = 0;
279         int column_count = 0;
280         int col_index = 0;
281         notification_system_setting_h result_system_setting = NULL;
282
283         if (system_setting == NULL) {
284                 NOTIFICATION_ERR("NOTIFICATION_ERROR_INVALID_PARAMETER"); /* LCOV_EXCL_LINE */
285                 err =  NOTIFICATION_ERROR_INVALID_PARAMETER;
286                 goto out;
287         }
288
289         sql_return = db_util_open(DBPATH, &local_db_handle, 0);
290
291         if (sql_return != SQLITE_OK || local_db_handle == NULL) {
292                 NOTIFICATION_ERR("db_util_open failed [%d]", sql_return); /* LCOV_EXCL_LINE */
293                 err = NOTIFICATION_ERROR_FROM_DB;
294                 goto out;
295         }
296
297         sql_query = sqlite3_mprintf("SELECT do_not_disturb, visibility_class, "
298                         "dnd_schedule_enabled, dnd_schedule_day, "
299                         "dnd_start_hour, dnd_start_min, dnd_end_hour, dnd_end_min, "
300                         "lock_screen_content_level "
301                         "FROM %s WHERE uid = %d", NOTIFICATION_SYSTEM_SETTING_DB_TABLE, uid);
302
303         if (!sql_query) {
304                 NOTIFICATION_ERR("fail to alloc query"); /* LCOV_EXCL_LINE */
305                 err = NOTIFICATION_ERROR_OUT_OF_MEMORY;
306                 goto out;
307         }
308
309         sql_return = sqlite3_get_table(local_db_handle, sql_query, &query_result, &row_count, &column_count, NULL);
310
311         if (sql_return != SQLITE_OK && sql_return != -1) {
312                 NOTIFICATION_ERR("sqlite3_get_table failed [%d][%s]", sql_return, sql_query); /* LCOV_EXCL_LINE */
313                 err = NOTIFICATION_ERROR_FROM_DB;
314                 goto out;
315         }
316
317         NOTIFICATION_DBG("row_count [%d] column_count [%d]", row_count, column_count);
318         if (!(result_system_setting = (struct notification_system_setting *)malloc(sizeof(struct notification_system_setting)))) {
319                 NOTIFICATION_ERR("malloc failed..."); /* LCOV_EXCL_LINE */
320                 err = NOTIFICATION_ERROR_OUT_OF_MEMORY;
321                 goto out;
322         }
323
324         /* no system setting record. allow everyting */
325         if (!row_count) {
326                 NOTIFICATION_DBG("No setting found..."); /* LCOV_EXCL_LINE */
327                 result_system_setting->do_not_disturb = 0;
328                 result_system_setting->visibility_class = 0;
329                 result_system_setting->dnd_schedule_enabled = 0;
330                 result_system_setting->dnd_schedule_day = 0;
331                 result_system_setting->dnd_start_hour = 0;
332                 result_system_setting->dnd_start_min = 0;
333                 result_system_setting->dnd_end_hour = 0;
334                 result_system_setting->dnd_end_min = 0;
335                 result_system_setting->lock_screen_content_level = 0;
336                 result_system_setting->dnd_allow_exceptions = NULL;
337         } else {
338                 /* LCOV_EXCL_START */
339                 col_index = column_count;
340                 _get_table_field_data_int(query_result, (int *)&(result_system_setting->do_not_disturb), col_index++);
341                 _get_table_field_data_int(query_result, &(result_system_setting->visibility_class), col_index++);
342                 _get_table_field_data_int(query_result, (int *)&(result_system_setting->dnd_schedule_enabled), col_index++);
343                 _get_table_field_data_int(query_result, &(result_system_setting->dnd_schedule_day), col_index++);
344                 _get_table_field_data_int(query_result, &(result_system_setting->dnd_start_hour), col_index++);
345                 _get_table_field_data_int(query_result, &(result_system_setting->dnd_start_min), col_index++);
346                 _get_table_field_data_int(query_result, &(result_system_setting->dnd_end_hour), col_index++);
347                 _get_table_field_data_int(query_result, &(result_system_setting->dnd_end_min), col_index++);
348                 _get_table_field_data_int(query_result, (int *)&(result_system_setting->lock_screen_content_level), col_index++);
349                 result_system_setting->dnd_allow_exceptions = NULL;
350                 /* LCOV_EXCL_STOP */
351         }
352
353         *system_setting = result_system_setting;
354 out:
355         if (query_result)
356                         sqlite3_free_table(query_result);
357
358         if (sql_query)
359                 sqlite3_free(sql_query);
360
361         if (local_db_handle) {
362                 sql_return = db_util_close(local_db_handle);
363                 if (sql_return != SQLITE_OK)
364                         NOTIFICATION_WARN("fail to db_util_close - [%d]", sql_return); /* LCOV_EXCL_LINE */
365         }
366
367         return err;
368 }
369
370 EXPORT_API
371 int notification_setting_db_update(const char *package_name, int allow_to_notify,
372                                    int do_not_disturb_except, int visibility_class,
373                                    int pop_up_notification, int lock_screen_content_level,
374                                    uid_t uid)
375 {
376         int err = NOTIFICATION_ERROR_NONE;
377         sqlite3 *db = NULL;
378         char *sqlbuf = NULL;
379         int sqlret;
380
381         if (package_name == NULL || strlen(package_name) == 0)
382                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
383
384         sqlret = db_util_open(DBPATH, &db, 0);
385         if (sqlret != SQLITE_OK || db == NULL) {
386                 NOTIFICATION_ERR("db_util_open failed [%s][%d]", DBPATH, sqlret);
387                 return NOTIFICATION_ERROR_FROM_DB;
388         }
389
390         sqlbuf = sqlite3_mprintf("UPDATE %s SET allow_to_notify = %d, do_not_disturb_except = %d, visibility_class = %d, " \
391                                  "pop_up_notification = %d, lock_screen_content_level = %d " \
392                         "WHERE package_name = %Q AND uid = %d",
393                         NOTIFICATION_SETTING_DB_TABLE, allow_to_notify, do_not_disturb_except, visibility_class,
394                         pop_up_notification, lock_screen_content_level, package_name, uid);
395         if (!sqlbuf) {
396                 NOTIFICATION_ERR("fail to alloc query");
397                 err = NOTIFICATION_ERROR_OUT_OF_MEMORY;
398                 goto return_close_db;
399         }
400
401         err = notification_db_exec(db, sqlbuf, NULL);
402
403 return_close_db:
404         if (sqlbuf)
405                 sqlite3_free(sqlbuf);
406
407         sqlret = db_util_close(db);
408         if (sqlret != SQLITE_OK)
409                 NOTIFICATION_WARN("fail to db_util_close - [%d]", sqlret);
410
411         return err;
412 }
413
414 EXPORT_API
415 int notification_setting_db_update_system_setting(int do_not_disturb, int visibility_class,
416                 int dnd_schedule_enabled, int dnd_schedule_day,
417                 int dnd_start_hour, int dnd_start_min,
418                 int dnd_end_hour, int dnd_end_min,
419                 int lock_screen_content_level, uid_t uid)
420 {
421         int err = NOTIFICATION_ERROR_NONE;
422         int sqlret;
423         int field_index = 1;
424         sqlite3 *db = NULL;
425         sqlite3_stmt *db_statement = NULL;
426
427         sqlret = db_util_open(DBPATH, &db, 0);
428
429         if (sqlret != SQLITE_OK || db == NULL) {
430                 NOTIFICATION_ERR("db_util_open failed [%s][%d][%s]", DBPATH, sqlret, sqlite3_errmsg(db));
431                 err =  NOTIFICATION_ERROR_FROM_DB;
432                 goto return_close_db;
433         }
434
435         sqlite3_exec(db, "BEGIN immediate;", NULL, NULL, NULL);
436
437         sqlret = sqlite3_prepare_v2(db, "INSERT OR REPLACE INTO notification_system_setting (uid, do_not_disturb, visibility_class, dnd_schedule_enabled, dnd_schedule_day, dnd_start_hour, dnd_start_min, dnd_end_hour, dnd_end_min, lock_screen_content_level) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ", -1, &db_statement, NULL);
438
439         if (sqlret != SQLITE_OK) {
440                 NOTIFICATION_ERR("sqlite3_prepare_v2 failed [%d][%s]", sqlret, sqlite3_errmsg(db));
441                 err =  NOTIFICATION_ERROR_FROM_DB;
442                 goto return_close_db;
443         }
444
445         sqlite3_bind_int(db_statement, field_index++, uid);
446         sqlite3_bind_int(db_statement, field_index++, do_not_disturb);
447         sqlite3_bind_int(db_statement, field_index++, visibility_class);
448         sqlite3_bind_int(db_statement, field_index++, dnd_schedule_enabled);
449         sqlite3_bind_int(db_statement, field_index++, dnd_schedule_day);
450         sqlite3_bind_int(db_statement, field_index++, dnd_start_hour);
451         sqlite3_bind_int(db_statement, field_index++, dnd_start_min);
452         sqlite3_bind_int(db_statement, field_index++, dnd_end_hour);
453         sqlite3_bind_int(db_statement, field_index++, dnd_end_min);
454         sqlite3_bind_int(db_statement, field_index++, lock_screen_content_level);
455
456         sqlret = sqlite3_step(db_statement);
457         if (sqlret != SQLITE_OK && sqlret != SQLITE_DONE) {
458                 NOTIFICATION_ERR("sqlite3_step failed [%d][%s]", sqlret, sqlite3_errmsg(db));
459                 err =  NOTIFICATION_ERROR_FROM_DB;
460                 goto return_close_db;
461         }
462
463         sqlret = sqlite3_changes(db);
464
465         if (sqlret == 0)
466                 NOTIFICATION_WARN("No changes on DB");
467
468 return_close_db:
469         if (db_statement)
470                 sqlite3_finalize(db_statement);
471
472
473         if (db) {
474                 if (err == NOTIFICATION_ERROR_NONE)
475                         sqlite3_exec(db, "END;", NULL, NULL, NULL);
476                 else
477                         sqlite3_exec(db, "ROLLBACK;", NULL, NULL, NULL);
478
479                 sqlret = db_util_close(db);
480         }
481
482         if (sqlret != SQLITE_OK)
483                 NOTIFICATION_WARN("fail to db_util_close - [%d]", sqlret);
484
485         return err;
486 }
487
488 /* LCOV_EXCL_START */
489 EXPORT_API
490 int notification_setting_db_update_do_not_disturb(int do_not_disturb, uid_t uid)
491 {
492         int err = NOTIFICATION_ERROR_NONE;
493         int sqlret;
494         sqlite3 *db = NULL;
495         char *sqlbuf = NULL;
496
497         sqlret = db_util_open(DBPATH, &db, 0);
498         if (sqlret != SQLITE_OK || db == NULL) {
499                 NOTIFICATION_ERR("db_util_open failed [%s][%d]", DBPATH, sqlret);
500                 return NOTIFICATION_ERROR_FROM_DB;
501         }
502
503         sqlbuf = sqlite3_mprintf("UPDATE notification_system_setting SET do_not_disturb = %d WHERE uid = %d", do_not_disturb, uid);
504         if (!sqlbuf) {
505                 NOTIFICATION_ERR("fail to alloc query");
506                 err = NOTIFICATION_ERROR_OUT_OF_MEMORY;
507                 goto return_close_db;
508         }
509
510         err = notification_db_exec(db, sqlbuf, NULL);
511
512 return_close_db:
513         if (sqlbuf)
514                 sqlite3_free(sqlbuf);
515
516         sqlret = db_util_close(db);
517         if (sqlret != SQLITE_OK)
518                 NOTIFICATION_WARN("fail to db_util_close - [%d]", sqlret);
519
520         return err;
521 }
522 /* LCOV_EXCL_STOP */
523
524 EXPORT_API int notification_system_setting_get_dnd_schedule_enabled_uid(uid_t **uids, int *count)
525 {
526         int ret, i;
527         int row_count = 0;
528         int column_count = 0;
529         int column_index = 0;
530         char *query = NULL;
531         char **query_result = NULL;
532         sqlite3 *db = NULL;
533         uid_t *result_uids;
534
535         db = notification_db_open(DBPATH);
536         if (db == NULL)
537                 return get_last_result();
538
539         query = sqlite3_mprintf("SELECT uid FROM %s WHERE dnd_schedule_enabled = 1",
540                                 NOTIFICATION_SYSTEM_SETTING_DB_TABLE);
541         if (query == NULL) {
542                 ret = NOTIFICATION_ERROR_OUT_OF_MEMORY;
543                 goto err;
544         }
545
546         ret = sqlite3_get_table(db, query, &query_result, &row_count, &column_count, NULL);
547         if (ret != SQLITE_OK && ret != -1) {
548                 NOTIFICATION_ERR("NOTIFICATION_ERROR_FROM_DB failed [%d][%s]", ret, query);     /* LCOV_EXCL_LINE */
549                 ret = NOTIFICATION_ERROR_FROM_DB;
550                 goto err;
551         }
552
553         if (row_count == 0) {
554                 NOTIFICATION_DBG("No enabled do_not_disturb user");
555                 ret = NOTIFICATION_ERROR_NONE;
556                 goto err;
557         }
558
559         if (!(result_uids = (uid_t *)malloc(sizeof(int) * row_count))) {
560                 NOTIFICATION_ERR("Memory allocation fail");
561                 ret = NOTIFICATION_ERROR_OUT_OF_MEMORY;
562                 goto err;
563         }
564
565         column_index = column_count;
566         for (i = 0; i < row_count; i++)
567                 _get_table_field_data_int(query_result, (int *)&(result_uids[i]), column_index++);
568
569         *uids = result_uids;
570         *count = row_count;
571
572 err:
573         if (query_result)
574                 sqlite3_free_table(query_result);
575         if (query)
576                 sqlite3_free(query);
577         if (db)
578                 notification_db_close(&db);
579
580         return ret;
581 }
582
583 EXPORT_API int notification_get_dnd_and_allow_to_notify(const char *pkgname,
584                                                         int *do_not_disturb,
585                                                         int *do_not_disturb_except,
586                                                         int *allow_to_notify,
587                                                         uid_t uid)
588 {
589         int ret = NOTIFICATION_ERROR_NONE;
590         int sql_ret;
591         int row_count;
592         int col_count;
593         int col_index;
594         sqlite3 *db;
595         char *query_setting = NULL;
596         char **query_setting_result = NULL;
597         char *query_system_setting = NULL;
598         char **query_system_setting_result = NULL;
599
600         if (pkgname == NULL)
601                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
602
603         db = notification_db_open(DBPATH);
604         if (db == NULL)
605                 return get_last_result();
606
607         query_setting = sqlite3_mprintf("SELECT allow_to_notify, do_not_disturb_except "
608                                         "FROM %s WHERE package_name = %Q AND uid = %d",
609                                         NOTIFICATION_SETTING_DB_TABLE, pkgname, uid);
610         if (query_setting == NULL) {
611                 NOTIFICATION_ERR("fail to alloc query");
612                 ret = NOTIFICATION_ERROR_OUT_OF_MEMORY;
613                 goto out;
614         }
615
616         query_system_setting = sqlite3_mprintf("SELECT do_not_disturb FROM %s WHERE uid = %d",
617                                                NOTIFICATION_SYSTEM_SETTING_DB_TABLE, uid);
618
619         if (query_system_setting == NULL) {
620                 NOTIFICATION_ERR("fail to alloc query");
621                 ret = NOTIFICATION_ERROR_OUT_OF_MEMORY;
622                 goto out;
623         }
624
625         sql_ret = sqlite3_get_table(db, query_setting, &query_setting_result, &row_count, &col_count, NULL);
626         if (sql_ret != SQLITE_OK && sql_ret != -1) {
627                 NOTIFICATION_ERR("NOTIFICATION_ERROR_FROM_DB failed [%d][%s]", sql_ret, query_setting);
628                 ret = NOTIFICATION_ERROR_FROM_DB;
629                 goto out;
630         }
631
632         col_index = col_count;
633         _get_table_field_data_int(query_setting_result, (int *)allow_to_notify, col_index++);
634         _get_table_field_data_int(query_setting_result, (int *)do_not_disturb_except, col_index++);
635
636         sql_ret = sqlite3_get_table(db, query_system_setting, &query_system_setting_result, &row_count, &col_count, NULL);
637         if (sql_ret != SQLITE_OK && sql_ret != -1) {
638                 NOTIFICATION_ERR("NOTIFICATION_ERROR_FROM_DB failed [%d][%s]", sql_ret, query_setting);
639                 ret = NOTIFICATION_ERROR_FROM_DB;
640                 goto out;
641         }
642
643         col_index = col_count;
644         _get_table_field_data_int(query_system_setting_result, (int *)do_not_disturb, col_index++);
645
646 out:
647         if (query_setting_result)
648                 sqlite3_free_table(query_setting_result);
649         if (query_system_setting_result)
650                 sqlite3_free_table(query_system_setting_result);
651         if (query_setting)
652                 sqlite3_free(query_setting);
653         if (query_system_setting)
654                 sqlite3_free(query_system_setting);
655         if (db)
656                 notification_db_close(&db);
657
658         return ret;
659 }
660
661 EXPORT_API int notification_system_setting_load_dnd_allow_exception(dnd_allow_exception_h *dnd_allow_exception, int *count, uid_t uid)
662 {
663         int err = NOTIFICATION_ERROR_NONE;
664         int sql_return;
665         int row_count = 0;
666         int column_count = 0;
667         int col_index = 0;
668         int i;
669         char *sql_query = NULL;
670         char **query_result = NULL;
671         sqlite3 *local_db_handle = NULL;
672         dnd_allow_exception_h dnd_allow_exception_data = NULL;
673
674         if (dnd_allow_exception == NULL) {
675                 NOTIFICATION_ERR("NOTIFICATION_ERROR_INVALID_PARAMETER"); /* LCOV_EXCL_LINE */
676                 err =  NOTIFICATION_ERROR_INVALID_PARAMETER;
677                 goto out;
678         }
679
680         sql_return = db_util_open(DBPATH, &local_db_handle, 0);
681         if (sql_return != SQLITE_OK || local_db_handle == NULL) {
682                 NOTIFICATION_ERR("db_util_open failed [%d]", sql_return); /* LCOV_EXCL_LINE */
683                 err = NOTIFICATION_ERROR_FROM_DB;
684                 goto out;
685         }
686
687         sql_query = sqlite3_mprintf("SELECT type, value FROM %s WHERE uid = %d",
688                         NOTIFICATION_DND_ALLOW_EXCEPTION, uid);
689         if (!sql_query) {
690                 NOTIFICATION_ERR("fail to alloc query"); /* LCOV_EXCL_LINE */
691                 err = NOTIFICATION_ERROR_OUT_OF_MEMORY;
692                 goto out;
693         }
694
695         sql_return = sqlite3_get_table(local_db_handle, sql_query, &query_result, &row_count, &column_count, NULL);
696         if (sql_return != SQLITE_OK && sql_return != -1) {
697                 NOTIFICATION_ERR("sqlite3_get_table failed [%d][%s]", sql_return, sql_query); /* LCOV_EXCL_LINE */
698                 err = NOTIFICATION_ERROR_FROM_DB;
699                 goto out;
700         }
701
702         if (!row_count) {
703                 NOTIFICATION_DBG("No setting found..."); /* LCOV_EXCL_LINE */
704                 goto out;
705         } else {
706                 dnd_allow_exception_data = (dnd_allow_exception_h)malloc(sizeof(struct notification_system_setting_dnd_allow_exception) * row_count);
707                 if (dnd_allow_exception_data == NULL) {
708                         NOTIFICATION_ERR("failed to malloc");
709                         return NOTIFICATION_ERROR_OUT_OF_MEMORY;
710                 }
711
712                 col_index = column_count;
713
714                 for (i = 0; i < row_count; i++) {
715                         _get_table_field_data_int(query_result, (int *)&(dnd_allow_exception_data[i].type), col_index++);
716                         _get_table_field_data_int(query_result, (int *)&(dnd_allow_exception_data[i].value), col_index++);
717                 }
718         }
719
720         *dnd_allow_exception = dnd_allow_exception_data;
721         *count = row_count;
722
723 out:
724         if (query_result)
725                 sqlite3_free_table(query_result);
726
727         if (sql_query)
728                 sqlite3_free(sql_query);
729
730         if (local_db_handle) {
731                 sql_return = db_util_close(local_db_handle);
732                 if (sql_return != SQLITE_OK)
733                         NOTIFICATION_WARN("fail to db_util_close - [%d]", sql_return); /* LCOV_EXCL_LINE */
734         }
735
736         return err;
737 }
738
739 EXPORT_API int notification_system_setting_update_dnd_allow_exception(int type, int value, uid_t uid)
740 {
741         int ret = NOTIFICATION_ERROR_NONE;
742         int sqlret;
743         sqlite3 *db = NULL;
744         char *sqlbuf = NULL;
745
746         sqlret = db_util_open(DBPATH, &db, 0);
747         if (sqlret != SQLITE_OK || db == NULL) {
748                 NOTIFICATION_ERR("db_util_open failed [%s][%d]", DBPATH, sqlret);
749                 return NOTIFICATION_ERROR_FROM_DB;
750         }
751
752         sqlbuf = sqlite3_mprintf("UPDATE dnd_allow_exception SET value = %d WHERE type = %d AND uid = %d", value, type, uid);
753         if (!sqlbuf) {
754                 NOTIFICATION_ERR("fail to alloc query");
755                 ret = NOTIFICATION_ERROR_OUT_OF_MEMORY;
756                 goto return_close_db;
757         }
758
759         ret = notification_db_exec(db, sqlbuf, NULL);
760
761 return_close_db:
762         if (sqlbuf)
763                 sqlite3_free(sqlbuf);
764
765         sqlret = db_util_close(db);
766         if (sqlret != SQLITE_OK)
767                 NOTIFICATION_WARN("fail to db_util_close - [%d]", sqlret);
768
769         return ret;
770 }