Fixed build errors caused by 2.4 merge.
[platform/core/api/notification.git] / src / notification_setting.c
1 /*
2  *  libnotification
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Seungtaek Chung <seungtaek.chung@samsung.com>, Mi-Ju Lee <miju52.lee@samsung.com>, Xi Zhichan <zhichan.xi@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <db-util.h>
26 #include <package_manager.h>
27 #include <pkgmgr-info.h>
28 #include <tizen_type.h>
29
30 #include <notification.h>
31 #include <notification_db.h>
32 #include <notification_list.h>
33 #include <notification_noti.h>
34 #include <notification_debug.h>
35 #include <notification_ipc.h>
36 #include <notification_private.h>
37 #include <notification_setting.h>
38 #include <notification_setting_internal.h>
39
40 #define NOTIFICATION_PRIVILEGE "http://tizen.org/privilege/notification"
41
42
43 static int _get_table_field_data_int(char  **table, int *buf, int index)
44 {
45         if ((table == NULL) || (buf == NULL) || (index < 0))  {
46                 NOTIFICATION_ERR("table[%p], buf[%p], index[%d]", table, buf, index);
47                 return false;
48         }
49
50         if (table[index] != NULL) {
51                 *buf = atoi(table[index]);
52                 return true;
53         }
54
55         *buf = 0;
56         return false;
57 }
58
59 static int _get_table_field_data_string(char **table, char **buf, int ucs2, int index)
60 {
61         int ret = false;
62
63         if ((table == NULL) || (buf == NULL) || (index < 0))  {
64                 NOTIFICATION_ERR("table[%p], buf[%p], index[%d]", table, buf, index);
65                 return false;
66         }
67
68         char *pTemp = table[index];
69         int sLen = 0;
70         if (pTemp == NULL)
71                 *buf = NULL;
72         else {
73                 sLen = strlen(pTemp);
74                 if(sLen) {
75                         *buf = (char *) malloc(sLen + 1);
76                         if (*buf == NULL) {
77                                 NOTIFICATION_ERR("malloc is failed");
78                                 goto out;
79                         }
80                         memset(*buf, 0, sLen + 1);
81                         strncpy(*buf, pTemp, sLen);
82                 }
83                 else
84                         *buf = NULL;
85         }
86
87         ret = true;
88 out:
89
90         return ret;
91 }
92
93
94
95 EXPORT_API int notification_setting_get_setting_array(notification_setting_h *setting_array, int *count)
96 {
97         int err = NOTIFICATION_ERROR_NONE;
98         sqlite3 *local_db_handle = NULL;
99         char *sql_query = NULL;
100         char **query_result = NULL;
101         int sql_return;
102         int row_count = 0;
103         int column_count = 0;
104         int i = 0;
105         int col_index = 0;
106         notification_setting_h result_setting_array= NULL;
107
108         if (setting_array == NULL || count == NULL) {
109                 NOTIFICATION_ERR("NOTIFICATION_ERROR_INVALID_PARAMETER");
110                 err =  NOTIFICATION_ERROR_INVALID_PARAMETER;
111                 goto out;
112         }
113
114         sql_return = db_util_open(DBPATH, &local_db_handle, 0);
115
116         if (sql_return != SQLITE_OK || local_db_handle == NULL) {
117                 NOTIFICATION_ERR("db_util_open failed [%d]", sql_return);
118                 err = NOTIFICATION_ERROR_FROM_DB;
119                 goto out;
120         }
121
122         sql_query = sqlite3_mprintf("SELECT package_name, allow_to_notify, do_not_disturb_except, visibility_class "
123                         "FROM %s "
124                         "ORDER BY package_name", NOTIFICATION_SETTING_DB_TABLE);
125
126         if (!sql_query) {
127                 NOTIFICATION_ERR("fail to alloc query");
128                 err = NOTIFICATION_ERROR_OUT_OF_MEMORY;
129                 goto out;
130         }
131
132         sql_return = sqlite3_get_table(local_db_handle, sql_query, &query_result, &row_count, &column_count, NULL);
133
134         if (sql_return != SQLITE_OK && sql_return != -1) {
135                 NOTIFICATION_ERR("NOTIFICATION_ERROR_FROM_DB failed [%d][%s]", sql_return, sql_query);
136                 err = NOTIFICATION_ERROR_FROM_DB;
137                 goto out;
138         }
139
140         if (!row_count) {
141                 NOTIFICATION_DBG ("No setting found...");
142                 err= NOTIFICATION_ERROR_NOT_EXIST_ID;
143                 goto out;
144         }
145
146         NOTIFICATION_DBG ("row_count [%d] column_count [%d]", row_count, column_count);
147         if (!(result_setting_array = (struct notification_setting*)malloc(sizeof(struct notification_setting) * row_count))) {
148                 NOTIFICATION_ERR("malloc failed...");
149                 err = NOTIFICATION_ERROR_OUT_OF_MEMORY;
150                 goto out;
151         }
152
153         col_index = column_count;
154
155         for (i = 0; i < row_count; i++) {
156                 _get_table_field_data_string(query_result, &(result_setting_array[i].package_name), 1, col_index++);
157                 _get_table_field_data_int(query_result, (int*)&(result_setting_array[i].allow_to_notify), col_index++);
158                 _get_table_field_data_int(query_result, (int*)&(result_setting_array[i].do_not_disturb_except), col_index++);
159                 _get_table_field_data_int(query_result, &(result_setting_array[i].visibility_class), col_index++);
160         }
161
162         *setting_array = result_setting_array;
163         *count = row_count;
164
165 out:
166         if (query_result)
167                         sqlite3_free_table(query_result);
168
169         if (sql_query)
170                 sqlite3_free(sql_query);
171
172         if (local_db_handle) {
173                 sql_return = db_util_close(local_db_handle);
174                 if (sql_return != SQLITE_OK) {
175                         NOTIFICATION_WARN("fail to db_util_close - [%d]", sql_return);
176                 }
177         }
178
179         return err;
180 }
181
182 EXPORT_API int notification_setting_get_setting_by_package_name(const char *package_name, notification_setting_h *setting)
183 {
184         int err = NOTIFICATION_ERROR_NONE;
185         sqlite3 *local_db_handle = NULL;
186         char *sql_query = NULL;
187         char **query_result = NULL;
188         int sql_return;
189         int row_count = 0;
190         int column_count = 0;
191         int i = 0;
192         int col_index = 0;
193         notification_setting_h result_setting_array= NULL;
194
195         if (package_name == NULL || setting == NULL) {
196                 NOTIFICATION_ERR("NOTIFICATION_ERROR_INVALID_PARAMETER");
197                 err =  NOTIFICATION_ERROR_INVALID_PARAMETER;
198                 goto out;
199         }
200
201         sql_return = db_util_open(DBPATH, &local_db_handle, 0);
202
203         if (sql_return != SQLITE_OK || local_db_handle == NULL) {
204                 NOTIFICATION_ERR("db_util_open failed [%d]", sql_return);
205                 err = NOTIFICATION_ERROR_FROM_DB;
206                 goto out;
207         }
208
209         sql_query = sqlite3_mprintf("SELECT package_name, allow_to_notify, do_not_disturb_except, visibility_class "
210                         "FROM %s "
211                         "WHERE package_name = %Q ", NOTIFICATION_SETTING_DB_TABLE, package_name);
212
213         if (!sql_query) {
214                 NOTIFICATION_ERR("fail to alloc query");
215                 err = NOTIFICATION_ERROR_OUT_OF_MEMORY;
216                 goto out;
217         }
218
219         sql_return = sqlite3_get_table(local_db_handle, sql_query, &query_result, &row_count, &column_count, NULL);
220
221         if (sql_return != SQLITE_OK && sql_return != -1) {
222                 NOTIFICATION_ERR("sqlite3_get_table failed [%d][%s]", sql_return, sql_query);
223                 err = NOTIFICATION_ERROR_FROM_DB;
224                 goto out;
225         }
226
227         if (!row_count) {
228                 NOTIFICATION_DBG ("No setting found for [%s]", package_name);
229                 err= NOTIFICATION_ERROR_NOT_EXIST_ID;
230                 goto out;
231         }
232
233         NOTIFICATION_DBG ("row_count [%d] column_count [%d]", row_count, column_count);
234
235         row_count = 1;
236
237         if (!(result_setting_array = (struct notification_setting*)malloc(sizeof(struct notification_setting) * row_count))) {
238                 NOTIFICATION_ERR("malloc failed...");
239                 err = NOTIFICATION_ERROR_OUT_OF_MEMORY;
240                 goto out;
241         }
242
243         col_index = column_count;
244
245         _get_table_field_data_string(query_result, &(result_setting_array[i].package_name), 1, col_index++);
246         _get_table_field_data_int(query_result, (int*)&(result_setting_array[i].allow_to_notify), col_index++);
247         _get_table_field_data_int(query_result, (int*)&(result_setting_array[i].do_not_disturb_except), col_index++);
248         _get_table_field_data_int(query_result, &(result_setting_array[i].visibility_class), col_index++);
249
250         *setting = result_setting_array;
251
252 out:
253         if (query_result)
254                         sqlite3_free_table(query_result);
255
256         if (sql_query)
257                 sqlite3_free(sql_query);
258
259         if (local_db_handle) {
260                 sql_return = db_util_close(local_db_handle);
261                 if (sql_return != SQLITE_OK) {
262                         NOTIFICATION_WARN("fail to db_util_close - [%d]", sql_return);
263                 }
264         }
265
266         return err;
267 }
268
269 EXPORT_API int notification_setting_get_setting(notification_setting_h *setting)
270 {
271         int ret;
272         char *package_name = NULL;
273
274         package_name = notification_get_pkgname_by_pid();
275
276         if (package_name == NULL)
277                 return NOTIFICATION_ERROR_NOT_EXIST_ID;
278
279         ret = notification_setting_get_setting_by_package_name(package_name, setting);
280
281         free(package_name);
282
283         return ret;
284 }
285
286 EXPORT_API int notification_setting_get_package_name(notification_setting_h setting, char **value)
287 {
288         int err = NOTIFICATION_ERROR_NONE;
289
290         if (setting == NULL || value == NULL) {
291                 NOTIFICATION_ERR("Invalid parameter\n");
292                 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
293                 goto out;
294         }
295
296         if (setting->package_name == NULL) {
297                 NOTIFICATION_ERR("setting->package_name is null\n");
298                 err = NOTIFICATION_ERROR_NOT_EXIST_ID;
299                 goto out;
300         }
301
302         *value = SAFE_STRDUP(setting->package_name);
303
304 out:
305
306         return err;
307 }
308
309 EXPORT_API int notification_setting_set_package_name(notification_setting_h setting, char *value)
310 {
311         int err = NOTIFICATION_ERROR_NONE;
312
313         if (setting == NULL || value == NULL) {
314                 NOTIFICATION_ERR("Invalid parameter\n");
315                 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
316                 goto out;
317         }
318
319         if (setting->package_name != NULL) {
320                 free(setting->package_name);
321         }
322
323         setting->package_name = SAFE_STRDUP(value);
324
325 out:
326
327         return err;
328 }
329
330 EXPORT_API int notification_setting_get_allow_to_notify(notification_setting_h setting, bool *value)
331 {
332         int err = NOTIFICATION_ERROR_NONE;
333
334         if (setting == NULL || value == NULL) {
335                 NOTIFICATION_ERR("Invalid parameter\n");
336                 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
337                 goto out;
338         }
339
340         *value = setting->allow_to_notify;
341
342 out:
343
344         return err;
345 }
346
347 EXPORT_API int notification_setting_set_allow_to_notify(notification_setting_h setting, bool value)
348 {
349         int err = NOTIFICATION_ERROR_NONE;
350
351         if (setting == NULL) {
352                 NOTIFICATION_ERR("Invalid parameter\n");
353                 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
354                 goto out;
355         }
356
357         setting->allow_to_notify = value;
358
359 out:
360
361         return err;
362 }
363
364 EXPORT_API int notification_setting_get_do_not_disturb_except(notification_setting_h setting, bool *value)
365 {
366         int err = NOTIFICATION_ERROR_NONE;
367
368         if (setting == NULL || value == NULL) {
369                 NOTIFICATION_ERR("Invalid parameter\n");
370                 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
371                 goto out;
372         }
373
374         *value = setting->do_not_disturb_except;
375
376 out:
377
378         return err;
379 }
380
381 EXPORT_API int notification_setting_set_do_not_disturb_except(notification_setting_h setting, bool value)
382 {
383         int err = NOTIFICATION_ERROR_NONE;
384
385         if (setting == NULL) {
386                 NOTIFICATION_ERR("Invalid parameter\n");
387                 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
388                 goto out;
389         }
390
391         setting->do_not_disturb_except = value;
392
393 out:
394
395         return err;
396 }
397
398 EXPORT_API int notification_setting_get_visibility_class(notification_setting_h setting, int *value)
399 {
400         int err = NOTIFICATION_ERROR_NONE;
401
402         if (setting == NULL || value == NULL) {
403                 NOTIFICATION_ERR("Invalid parameter\n");
404                 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
405                 goto out;
406         }
407
408         *value = setting->visibility_class;
409
410 out:
411
412         return err;
413 }
414
415 EXPORT_API int notification_setting_set_visibility_class(notification_setting_h setting, int value)
416 {
417         int err = NOTIFICATION_ERROR_NONE;
418
419         if (setting == NULL) {
420                 NOTIFICATION_ERR("Invalid parameter\n");
421                 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
422                 goto out;
423         }
424
425         setting->visibility_class = value;
426
427 out:
428
429         return err;
430 }
431
432 EXPORT_API int notification_setting_update_setting(notification_setting_h setting)
433 {
434         int err = NOTIFICATION_ERROR_NONE;
435
436         if (setting == NULL) {
437                 NOTIFICATION_ERR("Invalid parameter\n");
438                 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
439                 goto out;
440         }
441
442         err = notification_ipc_update_setting(setting);
443         if (err != NOTIFICATION_ERROR_NONE) {
444                 NOTIFICATION_ERR("notification_setting_update_setting returns[%d]\n", err);
445                 goto out;
446         }
447
448 out:
449         return err;
450 }
451
452 EXPORT_API int notification_setting_free_notification(notification_setting_h setting)
453 {
454         int err = NOTIFICATION_ERROR_NONE;
455
456         if (setting == NULL) {
457                         NOTIFICATION_ERR("Invalid parameter\n");
458                         err = NOTIFICATION_ERROR_INVALID_PARAMETER;
459                         goto out;
460                 }
461
462         SAFE_FREE(setting->package_name);
463
464         /* add codes to free all properties */
465
466         SAFE_FREE(setting);
467 out:
468
469         return err;
470 }
471
472 EXPORT_API int notification_setting_db_update(const char *package_name, int allow_to_notify, int do_not_disturb_except, int visibility_class)
473 {
474         int err = NOTIFICATION_ERROR_NONE;
475         sqlite3 *db = NULL;
476         char *sqlbuf = NULL;
477         int sqlret;
478
479         if (package_name == NULL)
480                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
481
482         sqlret = db_util_open(DBPATH, &db, 0);
483         if (sqlret != SQLITE_OK || db == NULL) {
484                 NOTIFICATION_ERR("db_util_open failed [%s][%d]", DBPATH, sqlret);
485                 return NOTIFICATION_ERROR_FROM_DB;
486         }
487
488         sqlbuf = sqlite3_mprintf("UPDATE %s SET allow_to_notify = %d, do_not_disturb_except = %d, visibility_class = %d " \
489                         "WHERE package_name = %Q",
490                         NOTIFICATION_SETTING_DB_TABLE, allow_to_notify, do_not_disturb_except, visibility_class, package_name);
491         if (!sqlbuf) {
492                 NOTIFICATION_ERR("fail to alloc query");
493                 err = NOTIFICATION_ERROR_OUT_OF_MEMORY;
494                 goto return_close_db;
495         }
496
497         err = notification_db_exec(db, sqlbuf, NULL);
498
499         return_close_db:
500         if (sqlbuf)
501                 sqlite3_free(sqlbuf);
502
503         sqlret = db_util_close(db);
504         if (sqlret != SQLITE_OK) {
505                 NOTIFICATION_WARN("fail to db_util_close - [%d]", sqlret);
506         }
507
508         return err;
509 }
510
511 static bool _is_package_in_setting_table(sqlite3 *db, const char *package_name)
512 {
513         sqlite3_stmt *db_statement = NULL;
514         int sqlite3_ret = SQLITE_OK;
515         bool err = true;
516         int field_index = 1;
517
518         sqlite3_ret = sqlite3_prepare_v2(db, "SELECT package_name FROM notification_setting WHERE package_name = ?", -1, &db_statement, NULL);
519
520         if (sqlite3_ret != SQLITE_OK) {
521                 NOTIFICATION_ERR("sqlite3_prepare_v2 failed [%d][%s]", sqlite3_ret, sqlite3_errmsg(db));
522                 err = false;
523                 goto out;
524         }
525
526         sqlite3_bind_text(db_statement, field_index++, package_name, -1, SQLITE_TRANSIENT);
527
528         sqlite3_ret = sqlite3_step(db_statement);
529
530         if (sqlite3_ret == SQLITE_DONE) {
531                 NOTIFICATION_INFO("no matched package_name found[%s][%d]", package_name, sqlite3_ret);
532                 err = false;
533                 goto out;
534         }
535
536         if (sqlite3_ret != SQLITE_OK) {
537                 NOTIFICATION_ERR("sqlite3_step failed [%d][%s]", sqlite3_ret, sqlite3_errmsg(db));
538                 err = false;
539                 goto out;
540         }
541 out:
542         if (db_statement) {
543                 sqlite3_finalize(db_statement);
544         }
545
546         return err;
547 }
548
549 static int foreach_package_info_callback(const pkgmgrinfo_pkginfo_h package_info, void *user_data)
550 {
551         sqlite3 *db = user_data;
552         sqlite3_stmt *db_statement = NULL;
553         char *package_name = NULL;
554         int pkgmgr_ret = PACKAGE_MANAGER_ERROR_NONE;
555         int sqlite3_ret = SQLITE_OK;
556         int field_index = 1;
557         int err = true;
558
559         if ((pkgmgr_ret = pkgmgrinfo_pkginfo_get_pkgname(package_info, &package_name)) != PACKAGE_MANAGER_ERROR_NONE) {
560                 NOTIFICATION_ERR("package_info_get_package failed [%d]", pkgmgr_ret);
561                 err = false;
562                 goto out;
563         }
564
565         if (_is_package_in_setting_table(db, package_name) == true) {
566                 NOTIFICATION_INFO("[%s] is exist", package_name);
567                 goto out;
568         }
569
570         NOTIFICATION_INFO("[%s] will be inserted", package_name);
571
572         sqlite3_ret = sqlite3_prepare_v2(db, "INSERT INTO notification_setting (package_name) VALUES (?) ", -1, &db_statement, NULL);
573
574         if (sqlite3_ret != SQLITE_OK) {
575                 NOTIFICATION_ERR("sqlite3_prepare_v2 failed [%d][%s]", sqlite3_ret, sqlite3_errmsg(db));
576                 err = false;
577                 goto out;
578         }
579
580         sqlite3_bind_text(db_statement, field_index++, package_name, -1, SQLITE_TRANSIENT);
581
582         sqlite3_ret = sqlite3_step(db_statement);
583
584         NOTIFICATION_INFO("sqlite3_step returns[%d]", sqlite3_ret);
585
586         if (sqlite3_ret != SQLITE_OK && sqlite3_ret != SQLITE_DONE) {
587                 NOTIFICATION_ERR("sqlite3_step failed [%d][%s]", sqlite3_ret, sqlite3_errmsg(db));
588                 err = false;
589         }
590
591 out:
592         if (db_statement) {
593                 sqlite3_finalize(db_statement);
594         }
595
596         NOTIFICATION_INFO("foreach_package_info_callback returns[%d]", err);
597         return err;
598 }
599
600 EXPORT_API int notification_setting_refresh_setting_table()
601 {
602         int err = NOTIFICATION_ERROR_NONE;
603         sqlite3 *db = NULL;
604         int sqlite3_ret = SQLITE_OK;
605         int pkgmgr_ret = PACKAGE_MANAGER_ERROR_NONE;
606         pkgmgrinfo_pkginfo_filter_h filter;
607         uid_t current_uid;
608
609         sqlite3_ret = db_util_open(DBPATH, &db, 0);
610
611         if (sqlite3_ret != SQLITE_OK || db == NULL) {
612                 NOTIFICATION_ERR("db_util_open failed [%s][%d]", DBPATH, sqlite3_ret);
613                 err = NOTIFICATION_ERROR_FROM_DB;
614                 goto out;
615         }
616
617         sqlite3_exec(db, "BEGIN immediate;", NULL, NULL, NULL);
618
619         pkgmgr_ret = pkgmgrinfo_pkginfo_filter_create(&filter);
620         if (pkgmgr_ret != PMINFO_R_OK) {
621                 NOTIFICATION_ERR("pkgmgrinfo_pkginfo_filter_create failed [%d]", pkgmgr_ret);
622                 err = NOTIFICATION_ERROR_FROM_DB;
623                 goto out;
624         }
625
626         pkgmgr_ret = pkgmgrinfo_pkginfo_filter_add_string(filter, PMINFO_PKGINFO_PROP_PACKAGE_PRIVILEGE, NOTIFICATION_PRIVILEGE);
627         if (pkgmgr_ret != PMINFO_R_OK) {
628                 NOTIFICATION_ERR("pkgmgrinfo_pkginfo_filter_add_string failed [%d]", pkgmgr_ret);
629                 err = NOTIFICATION_ERROR_FROM_DB;
630                 goto out;
631         }
632
633         current_uid = getuid();
634
635         pkgmgr_ret = pkgmgrinfo_pkginfo_usr_filter_foreach_pkginfo(filter, foreach_package_info_callback, db, current_uid);
636         if (pkgmgr_ret != PMINFO_R_OK) {
637                 NOTIFICATION_ERR("pkgmgrinfo_pkginfo_usr_filter_foreach_pkginfo failed [%d]", pkgmgr_ret);
638                 err = NOTIFICATION_ERROR_FROM_DB;
639                 goto out;
640         }
641
642         pkgmgrinfo_pkginfo_filter_destroy(filter);
643
644
645 out:
646
647         if (db) {
648                 if (err == NOTIFICATION_ERROR_NONE) {
649                         sqlite3_exec(db, "END;", NULL, NULL, NULL);
650                 }
651                 else {
652                         sqlite3_exec(db, "ROLLBACK;", NULL, NULL, NULL);
653                 }
654
655                 if ((sqlite3_ret = db_util_close(db)) != SQLITE_OK) {
656                         NOTIFICATION_WARN("fail to db_util_close - [%d]", sqlite3_ret);
657                 }
658         }
659
660         NOTIFICATION_INFO("notification_setting_refresh_setting_table returns [%08X]", err);
661
662         return err;
663 }
664
665 /* system setting --------------------------------*/
666
667 EXPORT_API int notification_system_setting_load_system_setting(notification_system_setting_h *system_setting)
668 {
669         int err = NOTIFICATION_ERROR_NONE;
670         sqlite3 *local_db_handle = NULL;
671         char *sql_query = NULL;
672         char **query_result = NULL;
673         int sql_return;
674         int row_count = 0;
675         int column_count = 0;
676         int col_index = 0;
677         notification_system_setting_h result_system_setting= NULL;
678
679         if (system_setting == NULL) {
680                 NOTIFICATION_ERR("NOTIFICATION_ERROR_INVALID_PARAMETER");
681                 err =  NOTIFICATION_ERROR_INVALID_PARAMETER;
682                 goto out;
683         }
684
685         sql_return = db_util_open(DBPATH, &local_db_handle, 0);
686
687         if (sql_return != SQLITE_OK || local_db_handle == NULL) {
688                 NOTIFICATION_ERR("db_util_open failed [%d]", sql_return);
689                 err = NOTIFICATION_ERROR_FROM_DB;
690                 goto out;
691         }
692
693         sql_query = sqlite3_mprintf("SELECT do_not_disturb, visibility_class "
694                         "FROM %s ", NOTIFICATION_SYSTEM_SETTING_DB_TABLE);
695
696         if (!sql_query) {
697                 NOTIFICATION_ERR("fail to alloc query");
698                 err = NOTIFICATION_ERROR_OUT_OF_MEMORY;
699                 goto out;
700         }
701
702         sql_return = sqlite3_get_table(local_db_handle, sql_query, &query_result, &row_count, &column_count, NULL);
703
704         if (sql_return != SQLITE_OK && sql_return != -1) {
705                 NOTIFICATION_ERR("sqlite3_get_table failed [%d][%s]", sql_return, sql_query);
706                 err = NOTIFICATION_ERROR_FROM_DB;
707                 goto out;
708         }
709
710         if (!row_count) {
711                 NOTIFICATION_DBG ("No setting found...");
712                 err= NOTIFICATION_ERROR_NOT_EXIST_ID;
713                 goto out;
714         }
715
716         NOTIFICATION_DBG ("row_count [%d] column_count [%d]", row_count, column_count);
717
718         row_count = 1;
719
720         if (!(result_system_setting = (struct notification_system_setting*)malloc(sizeof(struct notification_system_setting)))) {
721                 NOTIFICATION_ERR("malloc failed...");
722                 err = NOTIFICATION_ERROR_OUT_OF_MEMORY;
723                 goto out;
724         }
725
726         col_index = column_count;
727
728         _get_table_field_data_int(query_result, (int*)&(result_system_setting->do_not_disturb), col_index++);
729         _get_table_field_data_int(query_result, &(result_system_setting->visibility_class), col_index++);
730
731         *system_setting = result_system_setting;
732
733 out:
734         if (query_result)
735                         sqlite3_free_table(query_result);
736
737         if (sql_query)
738                 sqlite3_free(sql_query);
739
740         if (local_db_handle) {
741                 sql_return = db_util_close(local_db_handle);
742                 if (sql_return != SQLITE_OK) {
743                         NOTIFICATION_WARN("fail to db_util_close - [%d]", sql_return);
744                 }
745         }
746
747         return err;
748 }
749
750 EXPORT_API int notification_system_setting_update_system_setting(notification_system_setting_h system_setting)
751 {
752         int err = NOTIFICATION_ERROR_NONE;
753
754         if (system_setting == NULL) {
755                 NOTIFICATION_ERR("Invalid parameter\n");
756                 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
757                 goto out;
758         }
759
760         err = notification_ipc_update_system_setting(system_setting);
761         if (err != NOTIFICATION_ERROR_NONE) {
762                 NOTIFICATION_ERR("notification_ipc_update_system_setting returns[%d]\n", err);
763                 goto out;
764         }
765
766 out:
767         return err;
768 }
769
770 EXPORT_API int notification_system_setting_free_system_setting(notification_system_setting_h system_setting)
771 {
772         int err = NOTIFICATION_ERROR_NONE;
773
774         if (system_setting == NULL) {
775                         NOTIFICATION_ERR("Invalid parameter\n");
776                         err = NOTIFICATION_ERROR_INVALID_PARAMETER;
777                         goto out;
778                 }
779
780         /* add codes to free all properties */
781
782         SAFE_FREE(system_setting);
783
784 out:
785
786         return err;
787 }
788
789 EXPORT_API int notification_system_setting_get_do_not_disturb(notification_system_setting_h system_setting, bool *value)
790 {
791         int err = NOTIFICATION_ERROR_NONE;
792
793         if (system_setting == NULL || value == NULL) {
794                 NOTIFICATION_ERR("Invalid parameter\n");
795                 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
796                 goto out;
797         }
798
799         *value = system_setting->do_not_disturb;
800
801 out:
802
803         return err;
804 }
805
806 EXPORT_API int notification_system_setting_set_do_not_disturb(notification_system_setting_h system_setting, bool value)
807 {
808         int err = NOTIFICATION_ERROR_NONE;
809
810         if (system_setting == NULL) {
811                 NOTIFICATION_ERR("Invalid parameter\n");
812                 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
813                 goto out;
814         }
815
816         system_setting->do_not_disturb = value;
817
818 out:
819
820         return err;
821 }
822
823 EXPORT_API int notification_system_setting_get_visibility_class(notification_system_setting_h system_setting, int *value)
824 {
825         int err = NOTIFICATION_ERROR_NONE;
826
827         if (system_setting == NULL || value == NULL) {
828                 NOTIFICATION_ERR("Invalid parameter\n");
829                 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
830                 goto out;
831         }
832
833         *value = system_setting->visibility_class;
834
835 out:
836
837         return err;
838 }
839
840 EXPORT_API int notification_system_setting_set_visibility_class(notification_system_setting_h system_setting, int value)
841 {
842         int err = NOTIFICATION_ERROR_NONE;
843
844         if (system_setting == NULL) {
845                 NOTIFICATION_ERR("Invalid parameter\n");
846                 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
847                 goto out;
848         }
849
850         system_setting->visibility_class = value;
851
852 out:
853
854         return err;
855 }
856
857
858 EXPORT_API int notification_setting_db_update_system_setting(int do_not_disturb, int visibility_class)
859 {
860         int err = NOTIFICATION_ERROR_NONE;
861         int sqlret;
862         int field_index = 1;
863         sqlite3 *db = NULL;
864         sqlite3_stmt *db_statement = NULL;
865
866         sqlret = db_util_open(DBPATH, &db, 0);
867
868         if (sqlret != SQLITE_OK || db == NULL) {
869                 NOTIFICATION_ERR("db_util_open failed [%s][%d][%s]", DBPATH, sqlret, sqlite3_errmsg(db));
870                 err =  NOTIFICATION_ERROR_FROM_DB;
871                 goto return_close_db;
872         }
873
874         sqlite3_exec(db, "BEGIN immediate;", NULL, NULL, NULL);
875
876         sqlret = sqlite3_prepare_v2(db, "UPDATE notification_system_setting SET do_not_disturb = ?, visibility_class = ?;", -1, &db_statement, NULL);
877
878         if (sqlret != SQLITE_OK) {
879                 NOTIFICATION_ERR("sqlite3_prepare_v2 failed [%d][%s]", sqlret, sqlite3_errmsg(db));
880                 err =  NOTIFICATION_ERROR_FROM_DB;
881                 goto return_close_db;
882         }
883
884         sqlite3_bind_int(db_statement, field_index++, do_not_disturb);
885         sqlite3_bind_int(db_statement, field_index++, visibility_class);
886
887         sqlret = sqlite3_step(db_statement);
888
889         if (sqlret != SQLITE_OK && sqlret != SQLITE_DONE) {
890                 NOTIFICATION_ERR("sqlite3_step failed [%d][%s]", sqlret, sqlite3_errmsg(db));
891                 err =  NOTIFICATION_ERROR_FROM_DB;
892                 goto return_close_db;
893         }
894
895         sqlret = sqlite3_changes(db);
896
897         if (sqlret == 0) {
898                 NOTIFICATION_WARN("No changes on DB");
899         }
900
901 return_close_db:
902         if (db_statement) {
903                 sqlite3_finalize(db_statement);
904         }
905
906         if (db) {
907                 if (err == NOTIFICATION_ERROR_NONE) {
908                         sqlite3_exec(db, "END;", NULL, NULL, NULL);
909                 }
910                 else {
911                         sqlite3_exec(db, "ROLLBACK;", NULL, NULL, NULL);
912                 }
913                 sqlret = db_util_close(db);
914         }
915
916         if (sqlret != SQLITE_OK) {
917                 NOTIFICATION_WARN("fail to db_util_close - [%d]", sqlret);
918         }
919
920         return err;
921 }
922
923 /* OLD IMPLEMENTATION ----------------------------*/
924 #define NOTIFICATION_SETTING_DB "notification_setting"
925 #define NOTIFICATION_SETTING_DB_PATH "/opt/usr/dbspace/.notification_parser.db"
926
927 struct _notification_setting_h {
928         char *appid;
929         char *notification;
930         char *sounds;
931         char *contents;
932         char *badge;
933         char *pkgid;
934 };
935
936 struct prop_table {
937         const char *property;
938         const char *column;
939         const char *default_value;
940 };
941
942 static struct prop_table g_prop_table[] = {
943         {
944                 .property = "OPT_NOTIFICATION",
945                 .column = "notification",
946                 .default_value = "ON",
947         },
948         {
949                 .property = "OPT_SOUNDS",
950                 .column = "sounds",
951                 .default_value = "ON",
952         },
953         {
954                 .property = "OPT_CONTENTS",
955                 .column = "contents",
956                 .default_value = "ON",
957         },
958         {
959                 .property = "OPT_BADGE",
960                 .column = "badge",
961                 .default_value = "ON",
962         },
963         {
964                 .property = NULL,
965                 .column = NULL,
966                 .default_value = NULL,
967         }
968 };
969
970 static const char *_get_prop_column(const char *property)
971 {
972         int i;
973
974         for (i = 0; g_prop_table[i].property; i++) {
975                 if (strcmp(g_prop_table[i].property, property))
976                         continue;
977
978                 return g_prop_table[i].column;
979         }
980
981         return NULL;
982 }
983
984 #ifdef TBD
985 static const char *_get_prop_default_value(const char *property)
986 {
987         int i;
988
989         for (i = 0; g_prop_table[i].property; i++) {
990                 if (strcmp(g_prop_table[i].property, property))
991                         continue;
992
993                 return g_prop_table[i].default_value;
994         }
995
996         return NULL;
997 }
998 #endif
999
1000 static int _is_record_exist(const char *pkgname, sqlite3 *db)
1001 {
1002         sqlite3_stmt *stmt = NULL;
1003         int count = 0;
1004         int result = NOTIFICATION_ERROR_NONE;
1005         char *sqlbuf = NULL;
1006         int sqlret;
1007
1008         if (!pkgname)
1009                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1010
1011         if (!db)
1012                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1013
1014         sqlbuf = sqlite3_mprintf("SELECT count(*) FROM %s WHERE " \
1015                         "appid = %Q",
1016                         NOTIFICATION_SETTING_DB, pkgname);
1017
1018         if (!sqlbuf) {
1019                 NOTIFICATION_ERR("fail to alloc sql query");
1020                 return NOTIFICATION_ERROR_OUT_OF_MEMORY;
1021         }
1022
1023         sqlret = sqlite3_prepare_v2(db, sqlbuf, -1, &stmt, NULL);
1024         if (sqlret != SQLITE_OK) {
1025                 NOTIFICATION_ERR("DB err [%s]", sqlite3_errmsg(db));
1026                 NOTIFICATION_ERR("query[%s]", sqlbuf);
1027                 result = NOTIFICATION_ERROR_FROM_DB;
1028                 goto free_and_return;
1029         }
1030
1031         sqlret = sqlite3_step(stmt);
1032         if (sqlret == SQLITE_ROW)
1033                 count = sqlite3_column_int(stmt, 0);
1034         else
1035                 count = 0;
1036
1037         if (count > 0)
1038                 result = NOTIFICATION_ERROR_ALREADY_EXIST_ID;
1039         else
1040                 result = NOTIFICATION_ERROR_NOT_EXIST_ID;
1041
1042         free_and_return:
1043         if (sqlbuf)
1044                 sqlite3_free(sqlbuf);
1045
1046         if (stmt)
1047                 sqlite3_finalize(stmt);
1048
1049         return result;
1050 }
1051
1052 EXPORT_API int notification_setting_db_set(const char *pkgname, const char *property, const char *value)
1053 {
1054         int ret = NOTIFICATION_ERROR_NONE;
1055         int result = NOTIFICATION_ERROR_NONE;
1056         sqlite3 *db = NULL;
1057         char *sqlbuf = NULL;
1058         int sqlret;
1059         const char *column = NULL;
1060
1061         if (!pkgname)
1062                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1063
1064         if (!property)
1065                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1066
1067         if (!value)
1068                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1069
1070         column = _get_prop_column(property);
1071         if (!column)
1072                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1073
1074         sqlret = db_util_open(NOTIFICATION_SETTING_DB_PATH, &db, 0);
1075         if (sqlret != SQLITE_OK || !db) {
1076                 NOTIFICATION_ERR("fail to db_util_open - [%d]", sqlret);
1077                 return NOTIFICATION_ERROR_FROM_DB;
1078         }
1079
1080         ret = _is_record_exist(pkgname, db);
1081         if (ret != NOTIFICATION_ERROR_ALREADY_EXIST_ID) {
1082                 result = ret;
1083                 goto return_close_db;
1084         }
1085
1086         sqlbuf = sqlite3_mprintf("UPDATE %s SET %s = %Q " \
1087                         "WHERE appid = %Q",
1088                         NOTIFICATION_SETTING_DB, column, value, pkgname);
1089         if (!sqlbuf) {
1090                 NOTIFICATION_ERR("fail to alloc query");
1091                 result = NOTIFICATION_ERROR_OUT_OF_MEMORY;
1092                 goto return_close_db;
1093         }
1094
1095         result = notification_db_exec(db, sqlbuf, NULL);
1096
1097         return_close_db:
1098         if (sqlbuf)
1099                 sqlite3_free(sqlbuf);
1100
1101         sqlret = db_util_close(db);
1102         if (sqlret != SQLITE_OK) {
1103                 NOTIFICATION_WARN("fail to db_util_close - [%d]", sqlret);
1104         }
1105
1106         return result;
1107 }
1108
1109 EXPORT_API int notification_setting_db_get(const char *pkgname, const char *property, char **value)
1110 {
1111         int ret = NOTIFICATION_ERROR_NONE;
1112         int result = NOTIFICATION_ERROR_NONE;
1113         sqlite3 *db = NULL;
1114         char *sqlbuf = NULL;
1115         sqlite3_stmt *stmt = NULL;
1116         int sqlret;
1117         const char *column = NULL;
1118
1119         if (!pkgname)
1120                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1121
1122         if (!property)
1123                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1124
1125         if (!value)
1126                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1127
1128         column = _get_prop_column(property);
1129         if (!column)
1130                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1131
1132         sqlret = db_util_open(NOTIFICATION_SETTING_DB_PATH, &db, 0);
1133         if (sqlret != SQLITE_OK || !db) {
1134                 NOTIFICATION_ERR("fail to db_util_open - [%d]", sqlret);
1135                 return NOTIFICATION_ERROR_FROM_DB;
1136         }
1137
1138         ret = _is_record_exist(pkgname, db);
1139         if (ret != NOTIFICATION_ERROR_ALREADY_EXIST_ID) {
1140                 result = ret;
1141                 goto return_close_db;
1142         }
1143
1144         sqlbuf = sqlite3_mprintf("SELECT %s FROM %s " \
1145                         "WHERE appid = %Q",
1146                         column, NOTIFICATION_SETTING_DB, pkgname);
1147         if (!sqlbuf) {
1148                 NOTIFICATION_ERR("fail to alloc query");
1149                 result = NOTIFICATION_ERROR_OUT_OF_MEMORY;
1150                 goto return_close_db;
1151         }
1152
1153         sqlret = sqlite3_prepare_v2(db, sqlbuf, -1, &stmt, NULL);
1154         if (sqlret != SQLITE_OK) {
1155                 NOTIFICATION_ERR("fail to prepare %s - [%s]",
1156                                 sqlbuf, sqlite3_errmsg(db));
1157                 result = NOTIFICATION_ERROR_FROM_DB;
1158                 goto return_close_db;
1159         }
1160
1161         sqlret = sqlite3_step(stmt);
1162         if (sqlret == SQLITE_ROW) {
1163                 int get_bytes = sqlite3_column_bytes(stmt, 0);
1164                 char *get_data = (char *)calloc(get_bytes + 1, sizeof(char));
1165                 if (get_data != NULL) {
1166                         memcpy(get_data, sqlite3_column_text(stmt, 0),
1167                                         get_bytes * sizeof(char));
1168                         get_data[get_bytes] = '\0';
1169                         *value = get_data;
1170                 } else {
1171                         NOTIFICATION_ERR("fail to alloc query");
1172                         result = NOTIFICATION_ERROR_OUT_OF_MEMORY;
1173                         goto return_close_db;
1174                 }
1175         }
1176
1177         return_close_db:
1178         if (sqlbuf)
1179                 sqlite3_free(sqlbuf);
1180
1181         if (stmt)
1182                 sqlite3_finalize(stmt);
1183
1184         sqlret = db_util_close(db);
1185         if (sqlret != SQLITE_OK)
1186                 NOTIFICATION_WARN("fail to db_util_close - [%d]", sqlret);
1187
1188         return result;
1189 }
1190
1191 EXPORT_API int notification_setting_property_set(const char *pkgname, const char *property, const char *value)
1192 {
1193         int ret = 0;
1194
1195         if (!pkgname)
1196                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1197
1198         if (!property)
1199                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1200
1201         if (!value)
1202                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1203
1204         ret = notification_ipc_noti_setting_property_set(pkgname, property, value);
1205         if (ret != NOTIFICATION_ERROR_NONE) {
1206                 return ret;
1207         }
1208
1209         return NOTIFICATION_ERROR_NONE;
1210 }
1211
1212 EXPORT_API int notification_setting_property_get(const char *pkgname, const char *property, char **value)
1213 {
1214         int ret = 0;
1215
1216         if (!pkgname)
1217                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1218
1219         if (!property)
1220                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1221
1222         if (!value)
1223                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1224
1225         ret = notification_ipc_noti_setting_property_get(pkgname, property, value);
1226         if (ret != NOTIFICATION_ERROR_NONE) {
1227                 return ret;
1228         }
1229
1230         return NOTIFICATION_ERROR_NONE;
1231 }
1232