summaryrefslogtreecommitdiff
path: root/src/notification_setting_service.c
blob: d7f7fd27b58d957eec213eeb9038f49766afd7ad (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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
/*
 * Copyright (c) 2000 - 2016 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * 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 <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>

#include <sqlite3.h>
#include <db-util.h>
#include <tizen.h>

#include <notification.h>
#include <notification_db.h>
#include <notification_error.h>
#include <notification_debug.h>
#include <notification_private.h>
#include <notification_setting.h>
#include <notification_setting_internal.h>
#include <notification_setting_service.h>

static int _get_table_field_data_int(char  **table, int *buf, int index)
{
	if ((table == NULL) || (buf == NULL) || (index < 0))  {
		/* LCOV_EXCL_START */
		NOTIFICATION_ERR("table[%p], buf[%p], index[%d]", table, buf, index);
		return false;
		/* LCOV_EXCL_STOP */
	}

	if (table[index] != NULL) {
		*buf = atoi(table[index]);
		return true;
	}

	/* LCOV_EXCL_START */
	*buf = 0;
	return false;
	/* LCOV_EXCL_STOP */
}

static int _get_table_field_data_string(char **table, char **buf, int ucs2, int index)
{
	int ret = false;

	if ((table == NULL) || (buf == NULL) || (index < 0))  {
		/* LCOV_EXCL_START */
		NOTIFICATION_ERR("table[%p], buf[%p], index[%d]", table, buf, index);
		return false;
		/* LCOV_EXCL_STOP */
	}

	char *pTemp = table[index];
	int sLen = 0;
	if (pTemp == NULL) {
		*buf = NULL; /* LCOV_EXCL_LINE */
	} else {
		sLen = strlen(pTemp);
		if (sLen) {
			*buf = (char *) malloc(sLen + 1);
			if (*buf == NULL) {
				NOTIFICATION_ERR("malloc is failed"); /* LCOV_EXCL_LINE */
				goto out;
			}
			memset(*buf, 0, sLen + 1);
			strncpy(*buf, pTemp, sLen);
		} else {
			*buf = NULL; /* LCOV_EXCL_LINE */
		}
	}

	ret = true;
out:

	return ret;
}

EXPORT_API
int noti_setting_service_get_setting_by_appid(const char *appid, notification_setting_h *setting, uid_t uid)
{
	int err = NOTIFICATION_ERROR_NONE;
	sqlite3 *local_db_handle = NULL;
	char *sql_query = NULL;
	char **query_result = NULL;
	int sql_return;
	int row_count = 0;
	int column_count = 0;
	int i = 0;
	int col_index = 0;
	notification_setting_h result_setting_array = NULL;

	if (appid == NULL || setting == NULL) {
		NOTIFICATION_ERR("NOTIFICATION_ERROR_INVALID_PARAMETER"); /* LCOV_EXCL_LINE */
		err =  NOTIFICATION_ERROR_INVALID_PARAMETER;
		goto out;
	}

	sql_return = db_util_open(DBPATH, &local_db_handle, 0);

	if (sql_return != SQLITE_OK || local_db_handle == NULL) {
		NOTIFICATION_ERR("db_util_open failed [%d]", sql_return); /* LCOV_EXCL_LINE */
		err = NOTIFICATION_ERROR_FROM_DB;
		goto out;
	}

	sql_query = sqlite3_mprintf("SELECT package_name, appid, allow_to_notify, do_not_disturb_except, visibility_class, "
				    "pop_up_notification, lock_screen_content_level FROM %s "
				    "WHERE appid = %Q AND uid = %d", NOTIFICATION_SETTING_DB_TABLE, appid, uid);

	if (!sql_query) {
		NOTIFICATION_ERR("fail to alloc query"); /* LCOV_EXCL_LINE */
		err = NOTIFICATION_ERROR_OUT_OF_MEMORY;
		goto out;
	}

	sql_return = sqlite3_get_table(local_db_handle, sql_query, &query_result, &row_count, &column_count, NULL);
	if (sql_return != SQLITE_OK && sql_return != -1) {
		NOTIFICATION_ERR("sqlite3_get_table failed [%d][%s]", sql_return, sql_query); /* LCOV_EXCL_LINE */
		err = NOTIFICATION_ERROR_FROM_DB;
		goto out;
	}

	if (!row_count) {
		NOTIFICATION_DBG("No setting found for [%s]", appid); /* LCOV_EXCL_LINE */
		err = NOTIFICATION_ERROR_NOT_EXIST_ID;
		goto out;
	}

	NOTIFICATION_DBG("row_count [%d] column_count [%d]", row_count, column_count);

	row_count = 1;

	if (!(result_setting_array = (struct notification_setting *)malloc(sizeof(struct notification_setting) * row_count))) {
		NOTIFICATION_ERR("malloc failed..."); /* LCOV_EXCL_LINE */
		err = NOTIFICATION_ERROR_OUT_OF_MEMORY;
		goto out;
	}

	col_index = column_count;

	_get_table_field_data_string(query_result, &(result_setting_array[i].package_name), 1, col_index++);
	_get_table_field_data_string(query_result, &(result_setting_array[i].appid), 1, col_index++);
	_get_table_field_data_int(query_result, (int *)&(result_setting_array[i].allow_to_notify), col_index++);
	_get_table_field_data_int(query_result, (int *)&(result_setting_array[i].do_not_disturb_except), col_index++);
	_get_table_field_data_int(query_result, &(result_setting_array[i].visibility_class), col_index++);
	_get_table_field_data_int(query_result, (int *)&(result_setting_array[i].pop_up_notification), col_index++);
	_get_table_field_data_int(query_result, (int *)&(result_setting_array[i].lock_screen_content_level), col_index++);

	*setting = result_setting_array;

out:
	if (query_result)
		sqlite3_free_table(query_result);

	if (sql_query)
		sqlite3_free(sql_query);

	if (local_db_handle) {
		sql_return = db_util_close(local_db_handle);
		if (sql_return != SQLITE_OK)
			NOTIFICATION_WARN("fail to db_util_close - [%d]", sql_return); /* LCOV_EXCL_LINE */
	}

	return err;
}

EXPORT_API
int noti_setting_get_setting_array(notification_setting_h *setting_array, int *count, uid_t uid)
{
	int err = NOTIFICATION_ERROR_NONE;
	sqlite3 *local_db_handle = NULL;
	char *sql_query = NULL;
	char **query_result = NULL;
	int sql_return;
	int row_count = 0;
	int column_count = 0;
	int i = 0;
	int col_index = 0;
	notification_setting_h result_setting_array = NULL;

	if (setting_array == NULL || count == NULL) {
		NOTIFICATION_ERR("NOTIFICATION_ERROR_INVALID_PARAMETER"); /* LCOV_EXCL_LINE */
		err =  NOTIFICATION_ERROR_INVALID_PARAMETER;
		goto out;
	}

	sql_return = db_util_open(DBPATH, &local_db_handle, 0);

	if (sql_return != SQLITE_OK || local_db_handle == NULL) {
		NOTIFICATION_ERR("db_util_open failed [%d]", sql_return); /* LCOV_EXCL_LINE */
		err = NOTIFICATION_ERROR_FROM_DB;
		goto out;
	}

	sql_query = sqlite3_mprintf("SELECT package_name, appid, allow_to_notify, do_not_disturb_except, visibility_class, "
				    "pop_up_notification, lock_screen_content_level FROM %s WHERE uid = %d "
				    "ORDER BY package_name, appid ", NOTIFICATION_SETTING_DB_TABLE, uid);

	if (!sql_query) {
		NOTIFICATION_ERR("fail to alloc query"); /* LCOV_EXCL_LINE */
		err = NOTIFICATION_ERROR_OUT_OF_MEMORY;
		goto out;
	}

	sql_return = sqlite3_get_table(local_db_handle, sql_query, &query_result, &row_count, &column_count, NULL);

	if (sql_return != SQLITE_OK && sql_return != -1) {
		NOTIFICATION_ERR("NOTIFICATION_ERROR_FROM_DB failed [%d][%s]", sql_return, sql_query);	/* LCOV_EXCL_LINE */
		err = NOTIFICATION_ERROR_FROM_DB;
		goto out;
	}

	if (!row_count) {
		NOTIFICATION_DBG("No setting found..."); /* LCOV_EXCL_LINE */
		err = NOTIFICATION_ERROR_NOT_EXIST_ID;
		goto out;
	}

	NOTIFICATION_DBG("row_count [%d] column_count [%d]", row_count, column_count);
	if (!(result_setting_array = (struct notification_setting *)malloc(sizeof(struct notification_setting) * row_count))) {
		NOTIFICATION_ERR("malloc failed..."); /* LCOV_EXCL_LINE */
		err = NOTIFICATION_ERROR_OUT_OF_MEMORY;
		goto out;
	}

	col_index = column_count;

	for (i = 0; i < row_count; i++) {
		_get_table_field_data_string(query_result, &(result_setting_array[i].package_name), 1, col_index++);
		_get_table_field_data_string(query_result, &(result_setting_array[i].appid), 1, col_index++);
		_get_table_field_data_int(query_result, (int *)&(result_setting_array[i].allow_to_notify), col_index++);
		_get_table_field_data_int(query_result, (int *)&(result_setting_array[i].do_not_disturb_except), col_index++);
		_get_table_field_data_int(query_result, (int *)&(result_setting_array[i].visibility_class), col_index++);
		_get_table_field_data_int(query_result, (int *)&(result_setting_array[i].pop_up_notification), col_index++);
		_get_table_field_data_int(query_result, (int *)&(result_setting_array[i].lock_screen_content_level), col_index++);
	}

	*setting_array = result_setting_array;
	*count = row_count;

out:
	if (query_result)
		sqlite3_free_table(query_result);

	if (sql_query)
		sqlite3_free(sql_query);

	if (local_db_handle) {
		sql_return = db_util_close(local_db_handle);
		if (sql_return != SQLITE_OK)
			NOTIFICATION_WARN("fail to db_util_close - [%d]", sql_return); /* LCOV_EXCL_LINE */
	}

	return err;
}


EXPORT_API
int noti_system_setting_load_system_setting(notification_system_setting_h *system_setting, uid_t uid)
{
	int err = NOTIFICATION_ERROR_NONE;
	sqlite3 *local_db_handle = NULL;
	char *sql_query = NULL;
	char **query_result = NULL;
	int sql_return;
	int row_count = 0;
	int column_count = 0;
	int col_index = 0;
	notification_system_setting_h result_system_setting = NULL;

	if (system_setting == NULL) {
		NOTIFICATION_ERR("NOTIFICATION_ERROR_INVALID_PARAMETER"); /* LCOV_EXCL_LINE */
		err =  NOTIFICATION_ERROR_INVALID_PARAMETER;
		goto out;
	}

	sql_return = db_util_open(DBPATH, &local_db_handle, 0);

	if (sql_return != SQLITE_OK || local_db_handle == NULL) {
		NOTIFICATION_ERR("db_util_open failed [%d]", sql_return); /* LCOV_EXCL_LINE */
		err = NOTIFICATION_ERROR_FROM_DB;
		goto out;
	}

	sql_query = sqlite3_mprintf("SELECT 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 "
			"FROM %s WHERE uid = %d", NOTIFICATION_SYSTEM_SETTING_DB_TABLE, uid);

	if (!sql_query) {
		NOTIFICATION_ERR("fail to alloc query"); /* LCOV_EXCL_LINE */
		err = NOTIFICATION_ERROR_OUT_OF_MEMORY;
		goto out;
	}

	sql_return = sqlite3_get_table(local_db_handle, sql_query, &query_result, &row_count, &column_count, NULL);

	if (sql_return != SQLITE_OK && sql_return != -1) {
		NOTIFICATION_ERR("sqlite3_get_table failed [%d][%s]", sql_return, sql_query); /* LCOV_EXCL_LINE */
		err = NOTIFICATION_ERROR_FROM_DB;
		goto out;
	}

	NOTIFICATION_DBG("row_count [%d] column_count [%d]", row_count, column_count);
	if (!(result_system_setting = (struct notification_system_setting *)malloc(sizeof(struct notification_system_setting)))) {
		NOTIFICATION_ERR("malloc failed..."); /* LCOV_EXCL_LINE */
		err = NOTIFICATION_ERROR_OUT_OF_MEMORY;
		goto out;
	}

	/* no system setting record. allow everyting */
	if (!row_count) {
		NOTIFICATION_DBG("No setting found..."); /* LCOV_EXCL_LINE */
		result_system_setting->do_not_disturb = 0;
		result_system_setting->visibility_class = 0;
		result_system_setting->dnd_schedule_enabled = 0;
		result_system_setting->dnd_schedule_day = 0;
		result_system_setting->dnd_start_hour = 0;
		result_system_setting->dnd_start_min = 0;
		result_system_setting->dnd_end_hour = 0;
		result_system_setting->dnd_end_min = 0;
		result_system_setting->lock_screen_content_level = 0;
		result_system_setting->dnd_allow_exceptions = NULL;
	} else {
		/* LCOV_EXCL_START */
		col_index = column_count;
		_get_table_field_data_int(query_result, (int *)&(result_system_setting->do_not_disturb), col_index++);
		_get_table_field_data_int(query_result, &(result_system_setting->visibility_class), col_index++);
		_get_table_field_data_int(query_result, (int *)&(result_system_setting->dnd_schedule_enabled), col_index++);
		_get_table_field_data_int(query_result, &(result_system_setting->dnd_schedule_day), col_index++);
		_get_table_field_data_int(query_result, &(result_system_setting->dnd_start_hour), col_index++);
		_get_table_field_data_int(query_result, &(result_system_setting->dnd_start_min), col_index++);
		_get_table_field_data_int(query_result, &(result_system_setting->dnd_end_hour), col_index++);
		_get_table_field_data_int(query_result, &(result_system_setting->dnd_end_min), col_index++);
		_get_table_field_data_int(query_result, (int *)&(result_system_setting->lock_screen_content_level), col_index++);
		result_system_setting->dnd_allow_exceptions = NULL;
		/* LCOV_EXCL_STOP */
	}

	*system_setting = result_system_setting;
out:
	if (query_result)
			sqlite3_free_table(query_result);

	if (sql_query)
		sqlite3_free(sql_query);

	if (local_db_handle) {
		sql_return = db_util_close(local_db_handle);
		if (sql_return != SQLITE_OK)
			NOTIFICATION_WARN("fail to db_util_close - [%d]", sql_return); /* LCOV_EXCL_LINE */
	}

	return err;
}

EXPORT_API
int notification_setting_db_update(const char *package_name, const char *appid,
				int allow_to_notify, int do_not_disturb_except,
				int visibility_class, int pop_up_notification,
				int lock_screen_content_level, uid_t uid)
{
	int err = NOTIFICATION_ERROR_NONE;
	sqlite3 *db = NULL;
	char *sqlbuf = NULL;
	int sqlret;

	if (package_name == NULL || strlen(package_name) == 0
	    || appid == NULL || strlen(appid) == 0)
		return NOTIFICATION_ERROR_INVALID_PARAMETER;

	sqlret = db_util_open(DBPATH, &db, 0);
	if (sqlret != SQLITE_OK || db == NULL) {
		NOTIFICATION_ERR("db_util_open failed [%s][%d]", DBPATH, sqlret);
		return NOTIFICATION_ERROR_FROM_DB;
	}

	sqlbuf = sqlite3_mprintf("UPDATE %s SET allow_to_notify = %d, do_not_disturb_except = %d, visibility_class = %d, " \
				 "pop_up_notification = %d, lock_screen_content_level = %d " \
				 "WHERE appid = %Q AND package_name = %Q AND uid = %d",
			NOTIFICATION_SETTING_DB_TABLE, allow_to_notify, do_not_disturb_except, visibility_class,
			pop_up_notification, lock_screen_content_level, appid, package_name, uid);
	if (!sqlbuf) {
		NOTIFICATION_ERR("fail to alloc query");
		err = NOTIFICATION_ERROR_OUT_OF_MEMORY;
		goto return_close_db;
	}

	err = notification_db_exec(db, sqlbuf, NULL);

return_close_db:
	if (sqlbuf)
		sqlite3_free(sqlbuf);

	sqlret = db_util_close(db);
	if (sqlret != SQLITE_OK)
		NOTIFICATION_WARN("fail to db_util_close - [%d]", sqlret);

	return err;
}

EXPORT_API
int notification_setting_db_update_system_setting(int do_not_disturb, int visibility_class,
		int dnd_schedule_enabled, int dnd_schedule_day,
		int dnd_start_hour, int dnd_start_min,
		int dnd_end_hour, int dnd_end_min,
		int lock_screen_content_level, uid_t uid)
{
	int err = NOTIFICATION_ERROR_NONE;
	int sqlret;
	int field_index = 1;
	sqlite3 *db = NULL;
	sqlite3_stmt *db_statement = NULL;

	sqlret = db_util_open(DBPATH, &db, 0);

	if (sqlret != SQLITE_OK || db == NULL) {
		NOTIFICATION_ERR("db_util_open failed [%s][%d][%s]", DBPATH, sqlret, sqlite3_errmsg(db));
		err =  NOTIFICATION_ERROR_FROM_DB;
		goto return_close_db;
	}

	sqlite3_exec(db, "BEGIN immediate;", NULL, NULL, NULL);

	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);

	if (sqlret != SQLITE_OK) {
		NOTIFICATION_ERR("sqlite3_prepare_v2 failed [%d][%s]", sqlret, sqlite3_errmsg(db));
		err =  NOTIFICATION_ERROR_FROM_DB;
		goto return_close_db;
	}

	sqlite3_bind_int(db_statement, field_index++, uid);
	sqlite3_bind_int(db_statement, field_index++, do_not_disturb);
	sqlite3_bind_int(db_statement, field_index++, visibility_class);
	sqlite3_bind_int(db_statement, field_index++, dnd_schedule_enabled);
	sqlite3_bind_int(db_statement, field_index++, dnd_schedule_day);
	sqlite3_bind_int(db_statement, field_index++, dnd_start_hour);
	sqlite3_bind_int(db_statement, field_index++, dnd_start_min);
	sqlite3_bind_int(db_statement, field_index++, dnd_end_hour);
	sqlite3_bind_int(db_statement, field_index++, dnd_end_min);
	sqlite3_bind_int(db_statement, field_index++, lock_screen_content_level);

	sqlret = sqlite3_step(db_statement);
	if (sqlret != SQLITE_OK && sqlret != SQLITE_DONE) {
		NOTIFICATION_ERR("sqlite3_step failed [%d][%s]", sqlret, sqlite3_errmsg(db));
		err =  NOTIFICATION_ERROR_FROM_DB;
		goto return_close_db;
	}

	sqlret = sqlite3_changes(db);

	if (sqlret == 0)
		NOTIFICATION_WARN("No changes on DB");

return_close_db:
	if (db_statement)
		sqlite3_finalize(db_statement);


	if (db) {
		if (err == NOTIFICATION_ERROR_NONE)
			sqlite3_exec(db, "END;", NULL, NULL, NULL);
		else
			sqlite3_exec(db, "ROLLBACK;", NULL, NULL, NULL);

		sqlret = db_util_close(db);
	}

	if (sqlret != SQLITE_OK)
		NOTIFICATION_WARN("fail to db_util_close - [%d]", sqlret);

	return err;
}

/* LCOV_EXCL_START */
EXPORT_API
int notification_setting_db_update_do_not_disturb(int do_not_disturb, uid_t uid)
{
	int err = NOTIFICATION_ERROR_NONE;
	int sqlret;
	sqlite3 *db = NULL;
	char *sqlbuf = NULL;

	sqlret = db_util_open(DBPATH, &db, 0);
	if (sqlret != SQLITE_OK || db == NULL) {
		NOTIFICATION_ERR("db_util_open failed [%s][%d]", DBPATH, sqlret);
		return NOTIFICATION_ERROR_FROM_DB;
	}

	sqlbuf = sqlite3_mprintf("UPDATE notification_system_setting SET do_not_disturb = %d WHERE uid = %d", do_not_disturb, uid);
	if (!sqlbuf) {
		NOTIFICATION_ERR("fail to alloc query");
		err = NOTIFICATION_ERROR_OUT_OF_MEMORY;
		goto return_close_db;
	}

	err = notification_db_exec(db, sqlbuf, NULL);

return_close_db:
	if (sqlbuf)
		sqlite3_free(sqlbuf);

	sqlret = db_util_close(db);
	if (sqlret != SQLITE_OK)
		NOTIFICATION_WARN("fail to db_util_close - [%d]", sqlret);

	return err;
}
/* LCOV_EXCL_STOP */

EXPORT_API int notification_system_setting_get_dnd_schedule_enabled_uid(uid_t **uids, int *count)
{
	int ret, i;
	int row_count = 0;
	int column_count = 0;
	int column_index = 0;
	char *query = NULL;
	char **query_result = NULL;
	sqlite3 *db = NULL;
	uid_t *result_uids;

	db = notification_db_open(DBPATH);
	if (db == NULL)
		return get_last_result();

	query = sqlite3_mprintf("SELECT uid FROM %s WHERE dnd_schedule_enabled = 1",
				NOTIFICATION_SYSTEM_SETTING_DB_TABLE);
	if (query == NULL) {
		ret = NOTIFICATION_ERROR_OUT_OF_MEMORY;
		goto err;
	}

	ret = sqlite3_get_table(db, query, &query_result, &row_count, &column_count, NULL);
	if (ret != SQLITE_OK && ret != -1) {
		NOTIFICATION_ERR("NOTIFICATION_ERROR_FROM_DB failed [%d][%s]", ret, query);	/* LCOV_EXCL_LINE */
		ret = NOTIFICATION_ERROR_FROM_DB;
		goto err;
	}

	if (row_count == 0) {
		NOTIFICATION_DBG("No enabled do_not_disturb user");
		ret = NOTIFICATION_ERROR_NONE;
		goto err;
	}

	if (!(result_uids = (uid_t *)malloc(sizeof(int) * row_count))) {
		NOTIFICATION_ERR("Memory allocation fail");
		ret = NOTIFICATION_ERROR_OUT_OF_MEMORY;
		goto err;
	}

	column_index = column_count;
	for (i = 0; i < row_count; i++)
		_get_table_field_data_int(query_result, (int *)&(result_uids[i]), column_index++);

	*uids = result_uids;
	*count = row_count;

err:
	if (query_result)
		sqlite3_free_table(query_result);
	if (query)
		sqlite3_free(query);
	if (db)
		notification_db_close(&db);

	return ret;
}

EXPORT_API int notification_get_dnd_and_allow_to_notify(const char *appid,
							int *do_not_disturb,
							int *do_not_disturb_except,
							int *allow_to_notify,
							uid_t uid)
{
	int ret = NOTIFICATION_ERROR_NONE;
	int sql_ret;
	int row_count;
	int col_count;
	int col_index;
	sqlite3 *db;
	char *query_setting = NULL;
	char **query_setting_result = NULL;
	char *query_system_setting = NULL;
	char **query_system_setting_result = NULL;

	if (appid == NULL)
		return NOTIFICATION_ERROR_INVALID_PARAMETER;

	db = notification_db_open(DBPATH);
	if (db == NULL)
		return get_last_result();

	query_setting = sqlite3_mprintf("SELECT allow_to_notify, do_not_disturb_except "
					"FROM %s WHERE appid = %Q AND uid = %d",
					NOTIFICATION_SETTING_DB_TABLE, appid, uid);
	if (query_setting == NULL) {
		NOTIFICATION_ERR("fail to alloc query");
		ret = NOTIFICATION_ERROR_OUT_OF_MEMORY;
		goto out;
	}

	query_system_setting = sqlite3_mprintf("SELECT do_not_disturb FROM %s WHERE uid = %d",
					       NOTIFICATION_SYSTEM_SETTING_DB_TABLE, uid);

	if (query_system_setting == NULL) {
		NOTIFICATION_ERR("fail to alloc query");
		ret = NOTIFICATION_ERROR_OUT_OF_MEMORY;
		goto out;
	}

	sql_ret = sqlite3_get_table(db, query_setting, &query_setting_result, &row_count, &col_count, NULL);
	if (sql_ret != SQLITE_OK && sql_ret != -1) {
		NOTIFICATION_ERR("NOTIFICATION_ERROR_FROM_DB failed [%d][%s]", sql_ret, query_setting);
		ret = NOTIFICATION_ERROR_FROM_DB;
		goto out;
	}
	if (row_count == 0) {
		NOTIFICATION_ERR("Invalid uid [%d] or app id [%s]", uid, appid);
		ret = NOTIFICATION_ERROR_INVALID_PARAMETER;
		goto out;
	}

	col_index = col_count;
	_get_table_field_data_int(query_setting_result, (int *)allow_to_notify, col_index++);
	_get_table_field_data_int(query_setting_result, (int *)do_not_disturb_except, col_index++);

	sql_ret = sqlite3_get_table(db, query_system_setting, &query_system_setting_result, &row_count, &col_count, NULL);
	if (sql_ret != SQLITE_OK && sql_ret != -1) {
		NOTIFICATION_ERR("NOTIFICATION_ERROR_FROM_DB failed [%d][%s]", sql_ret, query_setting);
		ret = NOTIFICATION_ERROR_FROM_DB;
		goto out;
	}
	if (row_count == 0) {
		NOTIFICATION_ERR("Invalid uid [%d]", uid);
		ret = NOTIFICATION_ERROR_INVALID_PARAMETER;
		goto out;
	}

	col_index = col_count;
	_get_table_field_data_int(query_system_setting_result, (int *)do_not_disturb, col_index++);

out:
	if (query_setting_result)
		sqlite3_free_table(query_setting_result);
	if (query_system_setting_result)
		sqlite3_free_table(query_system_setting_result);
	if (query_setting)
		sqlite3_free(query_setting);
	if (query_system_setting)
		sqlite3_free(query_system_setting);
	if (db)
		notification_db_close(&db);

	return ret;
}

EXPORT_API int notification_system_setting_load_dnd_allow_exception(dnd_allow_exception_h *dnd_allow_exception, int *count, uid_t uid)
{
	int err = NOTIFICATION_ERROR_NONE;
	int sql_return;
	int row_count = 0;
	int column_count = 0;
	int col_index = 0;
	int i;
	char *sql_query = NULL;
	char **query_result = NULL;
	sqlite3 *local_db_handle = NULL;
	dnd_allow_exception_h dnd_allow_exception_data = NULL;

	if (dnd_allow_exception == NULL) {
		NOTIFICATION_ERR("NOTIFICATION_ERROR_INVALID_PARAMETER"); /* LCOV_EXCL_LINE */
		err =  NOTIFICATION_ERROR_INVALID_PARAMETER;
		goto out;
	}

	sql_return = db_util_open(DBPATH, &local_db_handle, 0);
	if (sql_return != SQLITE_OK || local_db_handle == NULL) {
		NOTIFICATION_ERR("db_util_open failed [%d]", sql_return); /* LCOV_EXCL_LINE */
		err = NOTIFICATION_ERROR_FROM_DB;
		goto out;
	}

	sql_query = sqlite3_mprintf("SELECT type, value FROM %s WHERE uid = %d",
			NOTIFICATION_DND_ALLOW_EXCEPTION, uid);
	if (!sql_query) {
		NOTIFICATION_ERR("fail to alloc query"); /* LCOV_EXCL_LINE */
		err = NOTIFICATION_ERROR_OUT_OF_MEMORY;
		goto out;
	}

	sql_return = sqlite3_get_table(local_db_handle, sql_query, &query_result, &row_count, &column_count, NULL);
	if (sql_return != SQLITE_OK && sql_return != -1) {
		NOTIFICATION_ERR("sqlite3_get_table failed [%d][%s]", sql_return, sql_query); /* LCOV_EXCL_LINE */
		err = NOTIFICATION_ERROR_FROM_DB;
		goto out;
	}

	if (!row_count) {
		NOTIFICATION_DBG("No setting found..."); /* LCOV_EXCL_LINE */
		goto out;
	} else {
		dnd_allow_exception_data = (dnd_allow_exception_h)malloc(sizeof(struct notification_system_setting_dnd_allow_exception) * row_count);
		if (dnd_allow_exception_data == NULL) {
			NOTIFICATION_ERR("failed to malloc");
			return NOTIFICATION_ERROR_OUT_OF_MEMORY;
		}

		col_index = column_count;

		for (i = 0; i < row_count; i++) {
			_get_table_field_data_int(query_result, (int *)&(dnd_allow_exception_data[i].type), col_index++);
			_get_table_field_data_int(query_result, (int *)&(dnd_allow_exception_data[i].value), col_index++);
		}
	}

	*dnd_allow_exception = dnd_allow_exception_data;
	*count = row_count;

out:
	if (query_result)
		sqlite3_free_table(query_result);

	if (sql_query)
		sqlite3_free(sql_query);

	if (local_db_handle) {
		sql_return = db_util_close(local_db_handle);
		if (sql_return != SQLITE_OK)
			NOTIFICATION_WARN("fail to db_util_close - [%d]", sql_return); /* LCOV_EXCL_LINE */
	}

	return err;
}

EXPORT_API int notification_system_setting_update_dnd_allow_exception(int type, int value, uid_t uid)
{
	int ret = NOTIFICATION_ERROR_NONE;
	int sqlret;
	int field_index = 1;
	sqlite3 *db = NULL;
	sqlite3_stmt *db_statement = NULL;

	db = notification_db_open(DBPATH);
	if (db == NULL)
		return get_last_result();

	sqlite3_exec(db, "BEGIN immediate;", NULL, NULL, NULL);

	sqlret = sqlite3_prepare_v2(db, "INSERT OR REPLACE INTO dnd_allow_exception (uid, type, value) VALUES(?, ?, ?) ", -1, &db_statement, NULL);

	if (sqlret != SQLITE_OK) {
		NOTIFICATION_ERR("sqlite3_prepare_v2 failed [%d][%s]", sqlret, sqlite3_errmsg(db));
		ret = NOTIFICATION_ERROR_FROM_DB;
		goto out;
	}

	sqlite3_bind_int(db_statement, field_index++, uid);
	sqlite3_bind_int(db_statement, field_index++, type);
	sqlite3_bind_int(db_statement, field_index++, value);

	sqlret = sqlite3_step(db_statement);
	if (sqlret != SQLITE_OK && sqlret != SQLITE_DONE) {
		NOTIFICATION_ERR("sqlite3_step failed [%d][%s]", sqlret, sqlite3_errmsg(db));
		ret = NOTIFICATION_ERROR_FROM_DB;
		goto out;
	}

	sqlret = sqlite3_changes(db);

	if (sqlret == 0)
		NOTIFICATION_WARN("No changes on DB");
out:
	if (db_statement)
		sqlite3_finalize(db_statement);
	if (ret == NOTIFICATION_ERROR_NONE)
		sqlite3_exec(db, "END;", NULL, NULL, NULL);
	else
		sqlite3_exec(db, "ROLLBACK;", NULL, NULL, NULL);
	notification_db_close(&db);

	return ret;
}

EXPORT_API int noti_system_setting_get_do_not_disturb(int *do_not_disturb, uid_t uid)
{
	int ret = NOTIFICATION_ERROR_NONE;
	int row_count = 0;
	int col_count = 0;
	int col_index = 0;
	char *query = NULL;
	char **query_result = NULL;
	sqlite3 *db = NULL;

	db = notification_db_open(DBPATH);
	if (db == NULL)
		return get_last_result();

	query = sqlite3_mprintf("SELECT do_not_disturb FROM %s WHERE uid = %d",
				NOTIFICATION_SYSTEM_SETTING_DB_TABLE, uid);

	if (query == NULL) {
		NOTIFICATION_ERR("fail to alloc query");
		ret = NOTIFICATION_ERROR_OUT_OF_MEMORY;
		goto out;
	}

	ret = sqlite3_get_table(db, query, &query_result, &row_count, &col_count, NULL);
	if (ret != SQLITE_OK && ret != -1) {
		NOTIFICATION_ERR("NOTIFICATION_ERROR_FROM_DB failed [%d][%s]", ret, query);
		ret = NOTIFICATION_ERROR_FROM_DB;
		goto out;
	}

	col_index = col_count;
	if (row_count == 0) {
		NOTIFICATION_ERR("No system setting found");
		ret = NOTIFICATION_ERROR_INVALID_PARAMETER;
	} else {
		if (_get_table_field_data_int(query_result, (int *)do_not_disturb, col_index++) == false)
			ret = NOTIFICATION_ERROR_FROM_DB;
	}

out:
	if (query_result)
		sqlite3_free_table(query_result);
	if (query)
		sqlite3_free(query);
	if (db)
		notification_db_close(&db);

	return ret;
}