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