cf8daae048fa15f9282ad03f281a670fa2ccf350
[platform/core/api/notification.git] / src / notification_setting.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 <stdio.h>
18 #include <string.h>
19 #include <stdlib.h>
20 #include <db-util.h>
21 #include <package_manager.h>
22 #include <pkgmgr-info.h>
23 #include <tizen_type.h>
24 #include <tzplatform_config.h>
25
26 #include <notification.h>
27 #include <notification_db.h>
28 #include <notification_list.h>
29 #include <notification_noti.h>
30 #include <notification_debug.h>
31 #include <notification_ipc.h>
32 #include <notification_private.h>
33 #include <notification_setting.h>
34 #include <notification_setting_internal.h>
35
36 #define NOTIFICATION_PRIVILEGE "http://tizen.org/privilege/notification"
37
38 typedef struct {
39         uid_t uid;
40         sqlite3 *db;
41 } setting_local_info;
42
43 EXPORT_API int notification_setting_get_setting_array_for_uid(notification_setting_h *setting_array, int *count, uid_t uid)
44 {
45         int ret = NOTIFICATION_ERROR_NONE;
46         if (setting_array == NULL || count == NULL) {
47                 NOTIFICATION_ERR("NOTIFICATION_ERROR_INVALID_PARAMETER");
48                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
49         }
50         ret = notification_ipc_request_get_setting_array(setting_array, count, uid);
51         return ret;
52 }
53
54 EXPORT_API int notification_setting_get_setting_array(notification_setting_h *setting_array, int *count)
55 {
56         return notification_setting_get_setting_array_for_uid(setting_array, count, getuid());
57 }
58
59 EXPORT_API int notification_setting_get_setting_by_package_name_for_uid(const char *package_name, notification_setting_h *setting, uid_t uid)
60 {
61         int ret = NOTIFICATION_ERROR_NONE;
62         if (package_name == NULL || setting == NULL) {
63                 NOTIFICATION_ERR("NOTIFICATION_ERROR_INVALID_PARAMETER");
64                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
65         }
66         ret = notification_ipc_request_get_setting_by_package_name(package_name, setting, uid);
67         return ret;
68 }
69
70 /* LCOV_EXCL_START */
71 EXPORT_API int notification_setting_get_setting_by_package_name(const char *package_name, notification_setting_h *setting)
72 {
73         return notification_setting_get_setting_by_package_name_for_uid(package_name, setting, getuid());
74 }
75 /* LCOV_EXCL_STOP */
76
77 /* LCOV_EXCL_START */
78 EXPORT_API int notification_setting_get_setting(notification_setting_h *setting)
79 {
80         int ret;
81         char *package_name = NULL;
82
83         package_name = notification_get_pkgname_by_pid();
84
85         if (package_name == NULL)
86                 return NOTIFICATION_ERROR_NOT_EXIST_ID;
87
88         ret = notification_setting_get_setting_by_package_name(package_name, setting);
89
90         free(package_name);
91
92         return ret;
93 }
94 /* LCOV_EXCL_STOP */
95
96 EXPORT_API int notification_setting_get_package_name(notification_setting_h setting, char **value)
97 {
98         int err = NOTIFICATION_ERROR_NONE;
99
100         if (setting == NULL || value == NULL) {
101                 NOTIFICATION_ERR("Invalid parameter\n");
102                 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
103                 goto out;
104         }
105
106         if (setting->package_name == NULL) {
107                 NOTIFICATION_ERR("setting->package_name is null\n");
108                 err = NOTIFICATION_ERROR_NOT_EXIST_ID;
109                 goto out;
110         }
111
112         *value = SAFE_STRDUP(setting->package_name);
113
114 out:
115
116         return err;
117 }
118
119 EXPORT_API int notification_setting_set_package_name(notification_setting_h setting, char *value)
120 {
121         int err = NOTIFICATION_ERROR_NONE;
122
123         if (setting == NULL || value == NULL) {
124                 NOTIFICATION_ERR("Invalid parameter\n");
125                 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
126                 goto out;
127         }
128
129         if (setting->package_name != NULL)
130                 free(setting->package_name);
131
132         setting->package_name = SAFE_STRDUP(value);
133
134 out:
135
136         return err;
137 }
138
139 EXPORT_API int notification_setting_get_allow_to_notify(notification_setting_h setting, bool *value)
140 {
141         int err = NOTIFICATION_ERROR_NONE;
142
143         if (setting == NULL || value == NULL) {
144                 NOTIFICATION_ERR("Invalid parameter\n");
145                 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
146                 goto out;
147         }
148
149         *value = setting->allow_to_notify;
150
151 out:
152
153         return err;
154 }
155
156 EXPORT_API int notification_setting_set_allow_to_notify(notification_setting_h setting, bool value)
157 {
158         int err = NOTIFICATION_ERROR_NONE;
159
160         if (setting == NULL) {
161                 NOTIFICATION_ERR("Invalid parameter\n");
162                 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
163                 goto out;
164         }
165
166         setting->allow_to_notify = value;
167
168 out:
169
170         return err;
171 }
172
173 EXPORT_API int notification_setting_get_do_not_disturb_except(notification_setting_h setting, bool *value)
174 {
175         int err = NOTIFICATION_ERROR_NONE;
176
177         if (setting == NULL || value == NULL) {
178                 NOTIFICATION_ERR("Invalid parameter\n");
179                 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
180                 goto out;
181         }
182
183         *value = setting->do_not_disturb_except;
184
185 out:
186
187         return err;
188 }
189
190 EXPORT_API int notification_setting_set_do_not_disturb_except(notification_setting_h setting, bool value)
191 {
192         int err = NOTIFICATION_ERROR_NONE;
193
194         if (setting == NULL) {
195                 NOTIFICATION_ERR("Invalid parameter\n");
196                 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
197                 goto out;
198         }
199
200         setting->do_not_disturb_except = value;
201
202 out:
203
204         return err;
205 }
206
207 EXPORT_API int notification_setting_get_visibility_class(notification_setting_h setting, int *value)
208 {
209         int err = NOTIFICATION_ERROR_NONE;
210
211         if (setting == NULL || value == NULL) {
212                 NOTIFICATION_ERR("Invalid parameter\n");
213                 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
214                 goto out;
215         }
216
217         *value = setting->visibility_class;
218
219 out:
220
221         return err;
222 }
223
224 EXPORT_API int notification_setting_set_visibility_class(notification_setting_h setting, int value)
225 {
226         int err = NOTIFICATION_ERROR_NONE;
227
228         if (setting == NULL) {
229                 NOTIFICATION_ERR("Invalid parameter\n");
230                 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
231                 goto out;
232         }
233
234         setting->visibility_class = value;
235
236 out:
237
238         return err;
239 }
240
241 EXPORT_API int notification_setting_update_setting_for_uid(notification_setting_h setting, uid_t uid)
242 {
243         int err = NOTIFICATION_ERROR_NONE;
244
245         if (setting == NULL) {
246                 NOTIFICATION_ERR("Invalid parameter\n");
247                 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
248                 goto out;
249         }
250
251         err = notification_ipc_update_setting(setting, uid);
252         if (err != NOTIFICATION_ERROR_NONE) {
253                 NOTIFICATION_ERR("notification_setting_update_setting returns[%d]\n", err);
254                 goto out;
255         }
256
257 out:
258         return err;
259 }
260
261 EXPORT_API int notification_setting_update_setting(notification_setting_h setting)
262 {
263         return notification_setting_update_setting_for_uid(setting, getuid());
264 }
265
266 EXPORT_API int notification_setting_free_notification(notification_setting_h setting)
267 {
268         int err = NOTIFICATION_ERROR_NONE;
269
270         if (setting == NULL) {
271                         NOTIFICATION_ERR("Invalid parameter\n");
272                         err = NOTIFICATION_ERROR_INVALID_PARAMETER;
273                         goto out;
274                 }
275
276         SAFE_FREE(setting->package_name);
277
278         /* add codes to free all properties */
279
280         SAFE_FREE(setting);
281 out:
282
283         return err;
284 }
285
286 EXPORT_API int notification_setting_db_update(const char *package_name, int allow_to_notify, int do_not_disturb_except, int visibility_class, uid_t uid)
287 {
288         int err = NOTIFICATION_ERROR_NONE;
289         sqlite3 *db = NULL;
290         char *sqlbuf = NULL;
291         int sqlret;
292
293         if (package_name == NULL || strlen(package_name) == 0)
294                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
295
296         sqlret = db_util_open(DBPATH, &db, 0);
297         if (sqlret != SQLITE_OK || db == NULL) {
298                 NOTIFICATION_ERR("db_util_open failed [%s][%d]", DBPATH, sqlret);
299                 return NOTIFICATION_ERROR_FROM_DB;
300         }
301
302         sqlbuf = sqlite3_mprintf("UPDATE %s SET allow_to_notify = %d, do_not_disturb_except = %d, visibility_class = %d " \
303                         "WHERE package_name = %Q AND uid = %d",
304                         NOTIFICATION_SETTING_DB_TABLE, allow_to_notify, do_not_disturb_except, visibility_class, package_name, uid);
305         if (!sqlbuf) {
306                 NOTIFICATION_ERR("fail to alloc query");
307                 err = NOTIFICATION_ERROR_OUT_OF_MEMORY;
308                 goto return_close_db;
309         }
310
311         err = notification_db_exec(db, sqlbuf, NULL);
312
313 return_close_db:
314         if (sqlbuf)
315                 sqlite3_free(sqlbuf);
316
317         sqlret = db_util_close(db);
318         if (sqlret != SQLITE_OK)
319                 NOTIFICATION_WARN("fail to db_util_close - [%d]", sqlret);
320
321         return err;
322 }
323
324 static bool _is_package_in_setting_table(sqlite3 *db, const char *package_name, uid_t uid)
325 {
326         sqlite3_stmt *db_statement = NULL;
327         int sqlite3_ret = SQLITE_OK;
328         bool err = true;
329         int field_index = 1;
330
331         sqlite3_ret = sqlite3_prepare_v2(db, "SELECT package_name FROM notification_setting WHERE uid = ? AND package_name = ?", -1, &db_statement, NULL);
332
333         if (sqlite3_ret != SQLITE_OK) {
334                 NOTIFICATION_ERR("sqlite3_prepare_v2 failed [%d][%s]", sqlite3_ret, sqlite3_errmsg(db));
335                 err = false;
336                 goto out;
337         }
338
339         sqlite3_bind_int(db_statement, field_index++, uid);
340         sqlite3_bind_text(db_statement, field_index++, package_name, -1, SQLITE_TRANSIENT);
341
342         sqlite3_ret = sqlite3_step(db_statement);
343
344         if (sqlite3_ret == SQLITE_DONE) {
345                 NOTIFICATION_INFO("no matched package_name found[%s][%d]", package_name, sqlite3_ret);
346                 err = false;
347                 goto out;
348         }
349
350         if (sqlite3_ret != SQLITE_OK && sqlite3_ret != SQLITE_ROW) {
351                 NOTIFICATION_ERR("sqlite3_step failed [%d][%s]", sqlite3_ret, sqlite3_errmsg(db));
352                 err = false;
353                 goto out;
354         }
355 out:
356         if (db_statement)
357                 sqlite3_finalize(db_statement);
358
359         return err;
360 }
361
362 static int foreach_package_info_callback(const pkgmgrinfo_pkginfo_h package_info, void *user_data)
363 {
364         setting_local_info *info = (setting_local_info *)user_data;
365         sqlite3 *db = info->db;
366         sqlite3_stmt *db_statement = NULL;
367         char *package_name = NULL;
368         int pkgmgr_ret = PACKAGE_MANAGER_ERROR_NONE;
369         int sqlite3_ret = SQLITE_OK;
370         int field_index = 1;
371         int err = true;
372
373         if ((pkgmgr_ret = pkgmgrinfo_pkginfo_get_pkgname(package_info, &package_name)) != PACKAGE_MANAGER_ERROR_NONE) {
374                 NOTIFICATION_ERR("package_info_get_package failed [%d]", pkgmgr_ret);
375                 err = false;
376                 goto out;
377         }
378
379         if (_is_package_in_setting_table(db, package_name, info->uid) == true) {
380                 NOTIFICATION_INFO("uid %d [%s] is exist", info->uid, package_name);
381                 goto out;
382         }
383
384         NOTIFICATION_INFO("uid %d [%s] will be inserted", info->uid, package_name);
385         sqlite3_ret = sqlite3_prepare_v2(db, "INSERT INTO notification_setting (uid, package_name) VALUES (?, ?) ", -1, &db_statement, NULL);
386
387         if (sqlite3_ret != SQLITE_OK) {
388                 NOTIFICATION_ERR("sqlite3_prepare_v2 failed [%d][%s]", sqlite3_ret, sqlite3_errmsg(db));
389                 err = false;
390                 goto out;
391         }
392
393         sqlite3_bind_int(db_statement, field_index++, info->uid);
394         sqlite3_bind_text(db_statement, field_index++, package_name, -1, SQLITE_TRANSIENT);
395
396         sqlite3_ret = sqlite3_step(db_statement);
397
398         NOTIFICATION_INFO("sqlite3_step returns[%d]", sqlite3_ret);
399
400         if (sqlite3_ret != SQLITE_OK && sqlite3_ret != SQLITE_DONE) {
401                 NOTIFICATION_ERR("sqlite3_step failed [%d][%s]", sqlite3_ret, sqlite3_errmsg(db));
402                 err = false;
403         }
404
405 out:
406         if (db_statement)
407                 sqlite3_finalize(db_statement);
408
409         NOTIFICATION_INFO("foreach_package_info_callback returns[%d]", err);
410         return err;
411 }
412
413 EXPORT_API int notification_setting_refresh_setting_table(uid_t uid)
414 {
415         int err = NOTIFICATION_ERROR_NONE;
416         sqlite3 *db = NULL;
417         int sqlite3_ret = SQLITE_OK;
418         int pkgmgr_ret = PACKAGE_MANAGER_ERROR_NONE;
419         pkgmgrinfo_pkginfo_filter_h filter;
420         setting_local_info info;
421
422         NOTIFICATION_ERR("refresh seeting table [%d]", uid);
423         sqlite3_ret = sqlite3_open_v2(DBPATH, &db, SQLITE_OPEN_READWRITE, NULL);
424         if (sqlite3_ret != SQLITE_OK || db == NULL) {
425                 NOTIFICATION_ERR("db_util_open failed [%s][%d]", DBPATH, sqlite3_ret);
426                 err = NOTIFICATION_ERROR_FROM_DB;
427                 goto out;
428         }
429
430         sqlite3_exec(db, "BEGIN immediate;", NULL, NULL, NULL);
431
432         pkgmgr_ret = pkgmgrinfo_pkginfo_filter_create(&filter);
433         if (pkgmgr_ret != PMINFO_R_OK) {
434                 NOTIFICATION_ERR("pkgmgrinfo_pkginfo_filter_create failed [%d]", pkgmgr_ret);
435                 err = NOTIFICATION_ERROR_FROM_DB;
436                 goto out;
437         }
438
439         pkgmgr_ret = pkgmgrinfo_pkginfo_filter_add_string(filter, PMINFO_PKGINFO_PROP_PACKAGE_PRIVILEGE, NOTIFICATION_PRIVILEGE);
440         if (pkgmgr_ret != PMINFO_R_OK) {
441                 NOTIFICATION_ERR("pkgmgrinfo_pkginfo_filter_add_string failed [%d]", pkgmgr_ret);
442                 err = NOTIFICATION_ERROR_FROM_DB;
443                 goto out;
444         }
445
446         info.db = db;
447         info.uid = uid;
448         pkgmgr_ret = pkgmgrinfo_pkginfo_usr_filter_foreach_pkginfo(filter, foreach_package_info_callback, &info, uid);
449         if (pkgmgr_ret != PMINFO_R_OK) {
450                 NOTIFICATION_ERR("pkgmgrinfo_pkginfo_usr_filter_foreach_pkginfo failed [%d]", pkgmgr_ret);
451                 err = NOTIFICATION_ERROR_FROM_DB;
452                 goto out;
453         }
454
455         pkgmgrinfo_pkginfo_filter_destroy(filter);
456
457
458 out:
459
460         if (db) {
461                 if (err == NOTIFICATION_ERROR_NONE)
462                         sqlite3_exec(db, "END;", NULL, NULL, NULL);
463                 else
464                         sqlite3_exec(db, "ROLLBACK;", NULL, NULL, NULL);
465
466                 if ((sqlite3_ret = db_util_close(db)) != SQLITE_OK)
467                         NOTIFICATION_WARN("fail to db_util_close - [%d]", sqlite3_ret);
468         }
469
470         NOTIFICATION_INFO("notification_setting_refresh_setting_table returns [%08X]", err);
471
472         return err;
473 }
474
475 typedef enum {
476         OPERATION_TYPE_INSERT_RECORD = 0,
477         OPERATION_TYPE_DELETE_RECORD = 1,
478 } notification_setting_operation_type;
479
480 static int _notification_setting_alter_package_list(notification_setting_operation_type operation_type, const char *package_name, uid_t uid)
481 {
482         sqlite3 *db = NULL;
483         sqlite3_stmt *db_statement = NULL;
484         int sqlite3_ret = SQLITE_OK;
485         int field_index = 1;
486         bool is_package_in_setting_table = false;
487         int err = NOTIFICATION_ERROR_NONE;
488
489         sqlite3_ret = db_util_open(DBPATH, &db, 0);
490
491         if (sqlite3_ret != SQLITE_OK || db == NULL) {
492                 NOTIFICATION_ERR("db_util_open failed [%s][%d]", DBPATH, sqlite3_ret);
493                 err = NOTIFICATION_ERROR_FROM_DB;
494                 goto out;
495         }
496
497         sqlite3_exec(db, "BEGIN immediate;", NULL, NULL, NULL);
498
499         is_package_in_setting_table = _is_package_in_setting_table(db, package_name, uid);
500
501         switch (operation_type) {
502         case OPERATION_TYPE_INSERT_RECORD:
503                 if (is_package_in_setting_table == true) {
504                         NOTIFICATION_INFO("[%s] is already exist", package_name);
505                         goto out;
506                 }
507                 NOTIFICATION_INFO("[%s] will be inserted", package_name);
508                 sqlite3_ret = sqlite3_prepare_v2(db, "INSERT INTO notification_setting (uid, package_name) VALUES (?, ?) ", -1, &db_statement, NULL);
509                 break;
510
511         case OPERATION_TYPE_DELETE_RECORD:
512                 if (is_package_in_setting_table == false) {
513                         NOTIFICATION_INFO("[%s] is not exist", package_name);
514                         goto out;
515                 }
516                 NOTIFICATION_INFO("[%s] will be removed", package_name);
517                 sqlite3_ret = sqlite3_prepare_v2(db, "DELETE FROM notification_setting WHERE uid = ? AND package_name = ? ", -1, &db_statement, NULL);
518                 break;
519         default:
520                 break;
521         }
522
523         if (sqlite3_ret != SQLITE_OK) {
524                 NOTIFICATION_ERR("sqlite3_prepare_v2 failed [%d][%s]", sqlite3_ret, sqlite3_errmsg(db));
525                 err = NOTIFICATION_ERROR_FROM_DB;
526                 goto out;
527         }
528
529         sqlite3_bind_int(db_statement, field_index++, uid);
530         sqlite3_bind_text(db_statement, field_index++, package_name, -1, SQLITE_TRANSIENT);
531
532         sqlite3_ret = sqlite3_step(db_statement);
533
534         if (sqlite3_ret != SQLITE_OK && sqlite3_ret != SQLITE_DONE) {
535                 NOTIFICATION_ERR("sqlite3_step failed [%d][%s]", sqlite3_ret, sqlite3_errmsg(db));
536                 err = NOTIFICATION_ERROR_FROM_DB;
537         }
538
539 out:
540         if (db_statement)
541                 sqlite3_finalize(db_statement);
542
543         if (db) {
544                 NOTIFICATION_INFO("err [%d]", err);
545                 if (err == NOTIFICATION_ERROR_NONE)
546                         sqlite3_exec(db, "END;", NULL, NULL, NULL);
547                 else
548                         sqlite3_exec(db, "ROLLBACK;", NULL, NULL, NULL);
549
550
551                 if ((sqlite3_ret = db_util_close(db)) != SQLITE_OK)
552                         NOTIFICATION_WARN("fail to db_util_close - [%d]", sqlite3_ret);
553
554         }
555
556         return err;
557 }
558
559 /* LCOV_EXCL_START */
560 bool privilege_info_cb(const char *privilege_name, void *user_data)
561 {
562         bool *found = user_data;
563
564         if (privilege_name && strcmp(NOTIFICATION_PRIVILEGE, privilege_name) == 0) {
565                 *found = true;
566                 return false;
567         }
568
569         return true;
570 }
571 /* LCOV_EXCL_STOP */
572
573 EXPORT_API int notification_setting_insert_package_for_uid(const char *package_id, uid_t uid)
574 {
575         int err = NOTIFICATION_ERROR_NONE;
576         err = _notification_setting_alter_package_list(OPERATION_TYPE_INSERT_RECORD, package_id, uid);
577
578         return err;
579 }
580
581 EXPORT_API int notification_setting_delete_package_for_uid(const char *package_id, uid_t uid)
582 {
583         return _notification_setting_alter_package_list(OPERATION_TYPE_DELETE_RECORD, package_id, uid);
584 }
585
586 /* system setting --------------------------------*/
587
588 EXPORT_API int notification_system_setting_load_system_setting_for_uid(notification_system_setting_h *system_setting, uid_t uid)
589 {
590         int ret = NOTIFICATION_ERROR_NONE;
591         if (system_setting == NULL) {
592                 NOTIFICATION_ERR("NOTIFICATION_ERROR_INVALID_PARAMETER");
593                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
594         }
595         ret = notification_ipc_request_load_system_setting(system_setting, uid);
596
597         return ret;
598 }
599
600 EXPORT_API int notification_system_setting_load_system_setting(notification_system_setting_h *system_setting)
601 {
602         return notification_system_setting_load_system_setting_for_uid(system_setting, getuid());
603 }
604
605 EXPORT_API int notification_system_setting_update_system_setting_for_uid(notification_system_setting_h system_setting, uid_t uid)
606 {
607         int err = NOTIFICATION_ERROR_NONE;
608
609         if (system_setting == NULL) {
610                 NOTIFICATION_ERR("Invalid parameter\n");
611                 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
612                 goto out;
613         }
614
615         err = notification_ipc_update_system_setting(system_setting, uid);
616         if (err != NOTIFICATION_ERROR_NONE) {
617                 NOTIFICATION_ERR("notification_ipc_update_system_setting returns[%d]\n", err);
618                 goto out;
619         }
620
621 out:
622         return err;
623 }
624
625 EXPORT_API int notification_system_setting_update_system_setting(notification_system_setting_h system_setting)
626 {
627         return notification_system_setting_update_system_setting_for_uid(system_setting, getuid());
628 }
629
630 EXPORT_API int notification_system_setting_free_system_setting(notification_system_setting_h system_setting)
631 {
632         int err = NOTIFICATION_ERROR_NONE;
633
634         if (system_setting == NULL) {
635                         NOTIFICATION_ERR("Invalid parameter\n");
636                         err = NOTIFICATION_ERROR_INVALID_PARAMETER;
637                         goto out;
638                 }
639
640         /* add codes to free all properties */
641
642         SAFE_FREE(system_setting);
643
644 out:
645
646         return err;
647 }
648
649 EXPORT_API int notification_system_setting_get_do_not_disturb(notification_system_setting_h system_setting, bool *value)
650 {
651         int err = NOTIFICATION_ERROR_NONE;
652
653         if (system_setting == NULL || value == NULL) {
654                 NOTIFICATION_ERR("Invalid parameter\n");
655                 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
656                 goto out;
657         }
658
659         *value = system_setting->do_not_disturb;
660
661 out:
662
663         return err;
664 }
665
666 EXPORT_API int notification_system_setting_set_do_not_disturb(notification_system_setting_h system_setting, bool value)
667 {
668         int err = NOTIFICATION_ERROR_NONE;
669
670         if (system_setting == NULL) {
671                 NOTIFICATION_ERR("Invalid parameter\n");
672                 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
673                 goto out;
674         }
675
676         system_setting->do_not_disturb = value;
677
678 out:
679
680         return err;
681 }
682
683 EXPORT_API int notification_system_setting_get_visibility_class(notification_system_setting_h system_setting, int *value)
684 {
685         int err = NOTIFICATION_ERROR_NONE;
686
687         if (system_setting == NULL || value == NULL) {
688                 NOTIFICATION_ERR("Invalid parameter\n");
689                 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
690                 goto out;
691         }
692
693         *value = system_setting->visibility_class;
694
695 out:
696
697         return err;
698 }
699
700 EXPORT_API int notification_system_setting_set_visibility_class(notification_system_setting_h system_setting, int value)
701 {
702         int err = NOTIFICATION_ERROR_NONE;
703
704         if (system_setting == NULL) {
705                 NOTIFICATION_ERR("Invalid parameter\n");
706                 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
707                 goto out;
708         }
709
710         system_setting->visibility_class = value;
711
712 out:
713
714         return err;
715 }
716
717
718 EXPORT_API int notification_setting_db_update_system_setting(int do_not_disturb, int visibility_class, uid_t uid)
719 {
720         int err = NOTIFICATION_ERROR_NONE;
721         int sqlret;
722         int field_index = 1;
723         sqlite3 *db = NULL;
724         sqlite3_stmt *db_statement = NULL;
725
726         sqlret = db_util_open(DBPATH, &db, 0);
727
728         if (sqlret != SQLITE_OK || db == NULL) {
729                 NOTIFICATION_ERR("db_util_open failed [%s][%d][%s]", DBPATH, sqlret, sqlite3_errmsg(db));
730                 err =  NOTIFICATION_ERROR_FROM_DB;
731                 goto return_close_db;
732         }
733
734         sqlite3_exec(db, "BEGIN immediate;", NULL, NULL, NULL);
735         sqlret = sqlite3_prepare_v2(db, "INSERT OR REPLACE INTO notification_system_setting (uid, do_not_disturb, visibility_class) values(?, ?, ?);", -1, &db_statement, NULL);
736
737         if (sqlret != SQLITE_OK) {
738                 NOTIFICATION_ERR("sqlite3_prepare_v2 failed [%d][%s]", sqlret, sqlite3_errmsg(db));
739                 err =  NOTIFICATION_ERROR_FROM_DB;
740                 goto return_close_db;
741         }
742
743         sqlite3_bind_int(db_statement, field_index++, uid);
744         sqlite3_bind_int(db_statement, field_index++, do_not_disturb);
745         sqlite3_bind_int(db_statement, field_index++, visibility_class);
746
747         sqlret = sqlite3_step(db_statement);
748         if (sqlret != SQLITE_OK && sqlret != SQLITE_DONE) {
749                 NOTIFICATION_ERR("sqlite3_step failed [%d][%s]", sqlret, sqlite3_errmsg(db));
750                 err =  NOTIFICATION_ERROR_FROM_DB;
751                 goto return_close_db;
752         }
753
754         sqlret = sqlite3_changes(db);
755
756         if (sqlret == 0)
757                 NOTIFICATION_WARN("No changes on DB");
758
759
760 return_close_db:
761         if (db_statement)
762                 sqlite3_finalize(db_statement);
763
764
765         if (db) {
766                 if (err == NOTIFICATION_ERROR_NONE)
767                         sqlite3_exec(db, "END;", NULL, NULL, NULL);
768                 else
769                         sqlite3_exec(db, "ROLLBACK;", NULL, NULL, NULL);
770
771                 sqlret = db_util_close(db);
772         }
773
774         if (sqlret != SQLITE_OK)
775                 NOTIFICATION_WARN("fail to db_util_close - [%d]", sqlret);
776
777         return err;
778 }
779