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