a98d1fb05ddc5632872f274dac51da87b9e2c24e
[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
27 #include <notification.h>
28 #include <notification_db.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
37 static int _get_table_field_data_int(char  **table, int *buf, int index)
38 {
39         if ((table == NULL) || (buf == NULL) || (index < 0))  {
40                 NOTIFICATION_ERR("table[%p], buf[%p], index[%d]", table, buf, index);
41                 return false;
42         }
43
44         if (table[index] != NULL) {
45                 *buf = atoi(table[index]);
46                 return true;
47         }
48
49         *buf = 0;
50         return false;
51 }
52
53 static int _get_table_field_data_string(char **table, char **buf, int ucs2, int index)
54 {
55         int ret = false;
56
57         if ((table == NULL) || (buf == NULL) || (index < 0))  {
58                 NOTIFICATION_ERR("table[%p], buf[%p], index[%d]", table, buf, index);
59                 return false;
60         }
61
62         char *pTemp = table[index];
63         int sLen = 0;
64         if (pTemp == NULL)
65                 *buf = NULL;
66         else {
67                 sLen = strlen(pTemp);
68                 if(sLen) {
69                         *buf = (char *) malloc(sLen + 1);
70                         if (*buf == NULL) {
71                                 NOTIFICATION_ERR("malloc is failed");
72                                 goto out;
73                         }
74                         memset(*buf, 0, sLen + 1);
75                         strncpy(*buf, pTemp, sLen);
76                 }
77                 else
78                         *buf = NULL;
79         }
80
81         ret = true;
82 out:
83
84         return ret;
85 }
86
87
88
89 EXPORT_API int notification_setting_get_setting_array(notification_setting_h *setting_array, int *count)
90 {
91         int err = NOTIFICATION_ERROR_NONE;
92         sqlite3 *local_db_handle = NULL;
93         char *sql_query = NULL;
94         char **query_result = NULL;
95         int sql_return;
96         int row_count = 0;
97         int column_count = 0;
98         int i = 0;
99         int col_index = 0;
100         notification_setting_h result_setting_array= NULL;
101
102         if (setting_array == NULL || count == NULL) {
103                 NOTIFICATION_ERR("NOTIFICATION_ERROR_INVALID_PARAMETER");
104                 err =  NOTIFICATION_ERROR_INVALID_PARAMETER;
105                 goto out;
106         }
107
108         sql_return = db_util_open(DBPATH, &local_db_handle, 0);
109
110         if (sql_return != SQLITE_OK || local_db_handle == NULL) {
111                 NOTIFICATION_ERR("db_util_open failed [%d]", sql_return);
112                 err = NOTIFICATION_ERROR_FROM_DB;
113                 goto out;
114         }
115
116         sql_query = sqlite3_mprintf("SELECT package_name, allow_to_notify, do_not_disturb_except, visibility_class "
117                         "FROM %s "
118                         "ORDER BY package_name", NOTIFICATION_SETTING_DB_TABLE);
119
120         if (!sql_query) {
121                 NOTIFICATION_ERR("fail to alloc query");
122                 err = NOTIFICATION_ERROR_OUT_OF_MEMORY;
123                 goto out;
124         }
125
126         sql_return = sqlite3_get_table(local_db_handle, sql_query, &query_result, &row_count, &column_count, NULL);
127
128         if (sql_return != SQLITE_OK && sql_return != -1) {
129                 NOTIFICATION_ERR("NOTIFICATION_ERROR_FROM_DB failed [%d][%s]", sql_return, sql_query);
130                 err = NOTIFICATION_ERROR_FROM_DB;
131                 goto out;
132         }
133
134         if (!row_count) {
135                 NOTIFICATION_DBG ("No setting found...");
136                 err= NOTIFICATION_ERROR_NOT_EXIST_ID;
137                 goto out;
138         }
139
140         NOTIFICATION_DBG ("row_count [%d] column_count [%d]", row_count, column_count);
141         if (!(result_setting_array = (struct notification_setting*)malloc(sizeof(struct notification_setting) * row_count))) {
142                 NOTIFICATION_ERR("malloc failed...");
143                 err = NOTIFICATION_ERROR_OUT_OF_MEMORY;
144                 goto out;
145         }
146
147         col_index = column_count;
148
149         for (i = 0; i < row_count; i++) {
150                 _get_table_field_data_string(query_result, &(result_setting_array[i].package_name), 1, col_index++);
151                 _get_table_field_data_int(query_result, (int*)&(result_setting_array[i].allow_to_notify), col_index++);
152                 _get_table_field_data_int(query_result, (int*)&(result_setting_array[i].do_not_disturb_except), col_index++);
153                 _get_table_field_data_int(query_result, &(result_setting_array[i].visibility_class), col_index++);
154         }
155
156         *setting_array = result_setting_array;
157         *count = row_count;
158
159 out:
160         if (query_result)
161                         sqlite3_free_table(query_result);
162
163         if (sql_query)
164                 sqlite3_free(sql_query);
165
166         if (local_db_handle) {
167                 sql_return = db_util_close(local_db_handle);
168                 if (sql_return != SQLITE_OK) {
169                         NOTIFICATION_WARN("fail to db_util_close - [%d]", sql_return);
170                 }
171         }
172
173         return err;
174 }
175
176 EXPORT_API int notification_setting_get_setting_by_package_name(const char *package_name, notification_setting_h *setting)
177 {
178         int err = NOTIFICATION_ERROR_NONE;
179         sqlite3 *local_db_handle = NULL;
180         char *sql_query = NULL;
181         char **query_result = NULL;
182         int sql_return;
183         int row_count = 0;
184         int column_count = 0;
185         int i = 0;
186         int col_index = 0;
187         notification_setting_h result_setting_array= NULL;
188
189         if (package_name == NULL || setting == NULL) {
190                 NOTIFICATION_ERR("NOTIFICATION_ERROR_INVALID_PARAMETER");
191                 err =  NOTIFICATION_ERROR_INVALID_PARAMETER;
192                 goto out;
193         }
194
195         sql_return = db_util_open(DBPATH, &local_db_handle, 0);
196
197         if (sql_return != SQLITE_OK || local_db_handle == NULL) {
198                 NOTIFICATION_ERR("db_util_open failed [%d]", sql_return);
199                 err = NOTIFICATION_ERROR_FROM_DB;
200                 goto out;
201         }
202
203         sql_query = sqlite3_mprintf("SELECT package_name, allow_to_notify, do_not_disturb_except, visibility_class "
204                         "FROM %s "
205                         "WHERE package_name = %Q ", NOTIFICATION_SETTING_DB_TABLE, package_name);
206
207         if (!sql_query) {
208                 NOTIFICATION_ERR("fail to alloc query");
209                 err = NOTIFICATION_ERROR_OUT_OF_MEMORY;
210                 goto out;
211         }
212
213         sql_return = sqlite3_get_table(local_db_handle, sql_query, &query_result, &row_count, &column_count, NULL);
214
215         if (sql_return != SQLITE_OK && sql_return != -1) {
216                 NOTIFICATION_ERR("sqlite3_get_table failed [%d][%s]", sql_return, sql_query);
217                 err = NOTIFICATION_ERROR_FROM_DB;
218                 goto out;
219         }
220
221         if (!row_count) {
222                 NOTIFICATION_DBG ("No setting found...");
223                 err= NOTIFICATION_ERROR_NOT_EXIST_ID;
224                 goto out;
225         }
226
227         NOTIFICATION_DBG ("row_count [%d] column_count [%d]", row_count, column_count);
228
229         row_count = 1;
230
231         if (!(result_setting_array = (struct notification_setting*)malloc(sizeof(struct notification_setting) * row_count))) {
232                 NOTIFICATION_ERR("malloc failed...");
233                 err = NOTIFICATION_ERROR_OUT_OF_MEMORY;
234                 goto out;
235         }
236
237         col_index = column_count;
238
239         _get_table_field_data_string(query_result, &(result_setting_array[i].package_name), 1, col_index++);
240         _get_table_field_data_int(query_result, (int*)&(result_setting_array[i].allow_to_notify), col_index++);
241         _get_table_field_data_int(query_result, (int*)&(result_setting_array[i].do_not_disturb_except), col_index++);
242         _get_table_field_data_int(query_result, &(result_setting_array[i].visibility_class), col_index++);
243
244         *setting = result_setting_array;
245
246 out:
247         if (query_result)
248                         sqlite3_free_table(query_result);
249
250         if (sql_query)
251                 sqlite3_free(sql_query);
252
253         if (local_db_handle) {
254                 sql_return = db_util_close(local_db_handle);
255                 if (sql_return != SQLITE_OK) {
256                         NOTIFICATION_WARN("fail to db_util_close - [%d]", sql_return);
257                 }
258         }
259
260         return err;
261 }
262
263 EXPORT_API int notification_setting_get_setting(notification_setting_h *setting)
264 {
265         char *package_name = NULL;
266
267         package_name = notification_get_pkgname_by_pid();
268
269         if (package_name == NULL)
270                 return NOTIFICATION_ERROR_NOT_EXIST_ID;
271
272         return notification_setting_get_setting_by_package_name(package_name, setting);
273 }
274
275 EXPORT_API int notification_setting_get_package_name(notification_setting_h setting, char **value)
276 {
277         int err = NOTIFICATION_ERROR_NONE;
278
279         if (setting == NULL || value == NULL) {
280                 NOTIFICATION_ERR("Invalid parameter\n");
281                 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
282                 goto out;
283         }
284
285         if (setting->package_name == NULL) {
286                 NOTIFICATION_ERR("setting->package_name is null\n");
287                 err = NOTIFICATION_ERROR_NOT_EXIST_ID;
288                 goto out;
289         }
290
291         *value = SAFE_STRDUP(setting->package_name);
292
293 out:
294
295         return err;
296 }
297
298 EXPORT_API int notification_setting_set_package_name(notification_setting_h setting, char *value)
299 {
300         int err = NOTIFICATION_ERROR_NONE;
301
302         if (setting == NULL || value == NULL) {
303                 NOTIFICATION_ERR("Invalid parameter\n");
304                 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
305                 goto out;
306         }
307
308         if (setting->package_name != NULL) {
309                 free(setting->package_name);
310         }
311
312         setting->package_name = SAFE_STRDUP(value);
313
314 out:
315
316         return err;
317 }
318
319 EXPORT_API int notification_setting_get_allow_to_notify(notification_setting_h setting, bool *value)
320 {
321         int err = NOTIFICATION_ERROR_NONE;
322
323         if (setting == NULL || value == NULL) {
324                 NOTIFICATION_ERR("Invalid parameter\n");
325                 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
326                 goto out;
327         }
328
329         *value = setting->allow_to_notify;
330
331 out:
332
333         return err;
334 }
335
336 EXPORT_API int notification_setting_set_allow_to_notify(notification_setting_h setting, bool value)
337 {
338         int err = NOTIFICATION_ERROR_NONE;
339
340         if (setting == NULL) {
341                 NOTIFICATION_ERR("Invalid parameter\n");
342                 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
343                 goto out;
344         }
345
346         setting->allow_to_notify = value;
347
348 out:
349
350         return err;
351 }
352
353 EXPORT_API int notification_setting_get_do_not_disturb_except(notification_setting_h setting, bool *value)
354 {
355         int err = NOTIFICATION_ERROR_NONE;
356
357         if (setting == NULL || value == NULL) {
358                 NOTIFICATION_ERR("Invalid parameter\n");
359                 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
360                 goto out;
361         }
362
363         *value = setting->do_not_disturb_except;
364
365 out:
366
367         return err;
368 }
369
370 EXPORT_API int notification_setting_set_do_not_disturb_except(notification_setting_h setting, bool value)
371 {
372         int err = NOTIFICATION_ERROR_NONE;
373
374         if (setting == NULL) {
375                 NOTIFICATION_ERR("Invalid parameter\n");
376                 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
377                 goto out;
378         }
379
380         setting->do_not_disturb_except = value;
381
382 out:
383
384         return err;
385 }
386
387 EXPORT_API int notification_setting_get_visibility_class(notification_setting_h setting, int *value)
388 {
389         int err = NOTIFICATION_ERROR_NONE;
390
391         if (setting == NULL || value == NULL) {
392                 NOTIFICATION_ERR("Invalid parameter\n");
393                 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
394                 goto out;
395         }
396
397         *value = setting->visibility_class;
398
399 out:
400
401         return err;
402 }
403
404 EXPORT_API int notification_setting_set_visibility_class(notification_setting_h setting, int value)
405 {
406         int err = NOTIFICATION_ERROR_NONE;
407
408         if (setting == NULL) {
409                 NOTIFICATION_ERR("Invalid parameter\n");
410                 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
411                 goto out;
412         }
413
414         setting->visibility_class = value;
415
416 out:
417
418         return err;
419 }
420
421 EXPORT_API int notification_setting_update_setting(notification_setting_h setting)
422 {
423         int err = NOTIFICATION_ERROR_NONE;
424
425         if (setting == NULL) {
426                 NOTIFICATION_ERR("Invalid parameter\n");
427                 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
428                 goto out;
429         }
430
431         err = notification_ipc_update_setting(setting);
432         if (err != NOTIFICATION_ERROR_NONE) {
433                 NOTIFICATION_ERR("notification_setting_update_setting returns[%d]\n", err);
434                 goto out;
435         }
436
437 out:
438         return err;
439 }
440
441 EXPORT_API int notification_setting_free_notification(notification_setting_h setting)
442 {
443         int err = NOTIFICATION_ERROR_NONE;
444
445         if (setting == NULL) {
446                         NOTIFICATION_ERR("Invalid parameter\n");
447                         err = NOTIFICATION_ERROR_INVALID_PARAMETER;
448                         goto out;
449                 }
450
451         SAFE_FREE(setting->package_name);
452
453
454         /* add codes to free all properties */
455
456         SAFE_FREE(setting);
457 out:
458
459         return err;
460 }
461
462 EXPORT_API int notification_setting_db_update(const char *package_name, int allow_to_notify, int do_not_disturb_except, int visibility_class)
463 {
464         int err = NOTIFICATION_ERROR_NONE;
465         sqlite3 *db = NULL;
466         char *sqlbuf = NULL;
467         int sqlret;
468
469         if (package_name == NULL)
470                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
471
472         sqlret = db_util_open(DBPATH, &db, 0);
473         if (sqlret != SQLITE_OK || db == NULL) {
474                 NOTIFICATION_ERR("db_util_open failed [%s][%d]", DBPATH, sqlret);
475                 return NOTIFICATION_ERROR_FROM_DB;
476         }
477
478         sqlbuf = sqlite3_mprintf("UPDATE %s SET allow_to_notify = %d, do_not_disturb_except = %d, visibility_class = %d " \
479                         "WHERE package_name = %Q",
480                         NOTIFICATION_SETTING_DB_TABLE, allow_to_notify, do_not_disturb_except, visibility_class, package_name);
481         if (!sqlbuf) {
482                 NOTIFICATION_ERR("fail to alloc query");
483                 err = NOTIFICATION_ERROR_OUT_OF_MEMORY;
484                 goto return_close_db;
485         }
486
487         err = notification_db_exec(db, sqlbuf, NULL);
488
489         return_close_db:
490         if (sqlbuf)
491                 sqlite3_free(sqlbuf);
492
493         sqlret = db_util_close(db);
494         if (sqlret != SQLITE_OK) {
495                 NOTIFICATION_WARN("fail to db_util_close - [%d]", sqlret);
496         }
497
498         return err;
499 }
500
501 /* system setting --------------------------------*/
502
503 EXPORT_API int notification_system_setting_load_system_setting(notification_system_setting_h *system_setting)
504 {
505         int err = NOTIFICATION_ERROR_NONE;
506         sqlite3 *local_db_handle = NULL;
507         char *sql_query = NULL;
508         char **query_result = NULL;
509         int sql_return;
510         int row_count = 0;
511         int column_count = 0;
512         int col_index = 0;
513         notification_system_setting_h result_system_setting= NULL;
514
515         if (system_setting == NULL) {
516                 NOTIFICATION_ERR("NOTIFICATION_ERROR_INVALID_PARAMETER");
517                 err =  NOTIFICATION_ERROR_INVALID_PARAMETER;
518                 goto out;
519         }
520
521         sql_return = db_util_open(DBPATH, &local_db_handle, 0);
522
523         if (sql_return != SQLITE_OK || local_db_handle == NULL) {
524                 NOTIFICATION_ERR("db_util_open failed [%d]", sql_return);
525                 err = NOTIFICATION_ERROR_FROM_DB;
526                 goto out;
527         }
528
529         sql_query = sqlite3_mprintf("SELECT do_not_disturb, visibility_class "
530                         "FROM %s ", NOTIFICATION_SYSTEM_SETTING_DB_TABLE);
531
532         if (!sql_query) {
533                 NOTIFICATION_ERR("fail to alloc query");
534                 err = NOTIFICATION_ERROR_OUT_OF_MEMORY;
535                 goto out;
536         }
537
538         sql_return = sqlite3_get_table(local_db_handle, sql_query, &query_result, &row_count, &column_count, NULL);
539
540         if (sql_return != SQLITE_OK && sql_return != -1) {
541                 NOTIFICATION_ERR("sqlite3_get_table failed [%d][%s]", sql_return, sql_query);
542                 err = NOTIFICATION_ERROR_FROM_DB;
543                 goto out;
544         }
545
546         if (!row_count) {
547                 NOTIFICATION_DBG ("No setting found...");
548                 err= NOTIFICATION_ERROR_NOT_EXIST_ID;
549                 goto out;
550         }
551
552         NOTIFICATION_DBG ("row_count [%d] column_count [%d]", row_count, column_count);
553
554         row_count = 1;
555
556         if (!(result_system_setting = (struct notification_system_setting*)malloc(sizeof(struct notification_system_setting)))) {
557                 NOTIFICATION_ERR("malloc failed...");
558                 err = NOTIFICATION_ERROR_OUT_OF_MEMORY;
559                 goto out;
560         }
561
562         col_index = column_count;
563
564         _get_table_field_data_int(query_result, (int*)&(result_system_setting->do_not_disturb), col_index++);
565         _get_table_field_data_int(query_result, &(result_system_setting->visibility_class), col_index++);
566
567         *system_setting = result_system_setting;
568
569 out:
570         if (query_result)
571                         sqlite3_free_table(query_result);
572
573         if (sql_query)
574                 sqlite3_free(sql_query);
575
576         if (local_db_handle) {
577                 sql_return = db_util_close(local_db_handle);
578                 if (sql_return != SQLITE_OK) {
579                         NOTIFICATION_WARN("fail to db_util_close - [%d]", sql_return);
580                 }
581         }
582
583         return err;
584 }
585
586 EXPORT_API int notification_system_setting_update_system_setting(notification_system_setting_h system_setting)
587 {
588         int err = NOTIFICATION_ERROR_NONE;
589
590         if (system_setting == NULL) {
591                 NOTIFICATION_ERR("Invalid parameter\n");
592                 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
593                 goto out;
594         }
595
596         err = notification_ipc_update_system_setting(system_setting);
597         if (err != NOTIFICATION_ERROR_NONE) {
598                 NOTIFICATION_ERR("notification_ipc_update_system_setting returns[%d]\n", err);
599                 goto out;
600         }
601
602 out:
603         return err;
604 }
605
606 EXPORT_API int notification_system_setting_free_system_setting(notification_system_setting_h system_setting)
607 {
608         int err = NOTIFICATION_ERROR_NONE;
609
610         if (system_setting == NULL) {
611                         NOTIFICATION_ERR("Invalid parameter\n");
612                         err = NOTIFICATION_ERROR_INVALID_PARAMETER;
613                         goto out;
614                 }
615
616         /* add codes to free all properties */
617
618         SAFE_FREE(system_setting);
619
620 out:
621
622         return err;
623 }
624
625 EXPORT_API int notification_system_setting_get_do_not_disturb(notification_system_setting_h system_setting, bool *value)
626 {
627         int err = NOTIFICATION_ERROR_NONE;
628
629         if (system_setting == NULL || value == NULL) {
630                 NOTIFICATION_ERR("Invalid parameter\n");
631                 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
632                 goto out;
633         }
634
635         *value = system_setting->do_not_disturb;
636
637 out:
638
639         return err;
640 }
641
642 EXPORT_API int notification_system_setting_set_do_not_disturb(notification_system_setting_h system_setting, bool value)
643 {
644         int err = NOTIFICATION_ERROR_NONE;
645
646         if (system_setting == NULL) {
647                 NOTIFICATION_ERR("Invalid parameter\n");
648                 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
649                 goto out;
650         }
651
652         system_setting->do_not_disturb = value;
653
654 out:
655
656         return err;
657 }
658
659 EXPORT_API int notification_system_setting_get_visibility_class(notification_system_setting_h system_setting, int *value)
660 {
661         int err = NOTIFICATION_ERROR_NONE;
662
663         if (system_setting == NULL || value == NULL) {
664                 NOTIFICATION_ERR("Invalid parameter\n");
665                 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
666                 goto out;
667         }
668
669         *value = system_setting->visibility_class;
670
671 out:
672
673         return err;
674 }
675
676 EXPORT_API int notification_system_setting_set_visibility_class(notification_system_setting_h system_setting, int value)
677 {
678         int err = NOTIFICATION_ERROR_NONE;
679
680         if (system_setting == NULL) {
681                 NOTIFICATION_ERR("Invalid parameter\n");
682                 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
683                 goto out;
684         }
685
686         system_setting->visibility_class = value;
687
688 out:
689
690         return err;
691 }
692
693 EXPORT_API int notification_setting_db_update_system_setting(int do_not_disturb, int visibility_class)
694 {
695         int err = NOTIFICATION_ERROR_NONE;
696         sqlite3 *db = NULL;
697         char *sqlbuf = NULL;
698         int sqlret;
699
700         sqlret = db_util_open(DBPATH, &db, 0);
701         if (sqlret != SQLITE_OK || db == NULL) {
702                 NOTIFICATION_ERR("db_util_open failed [%s][%d]", DBPATH, sqlret);
703                 return NOTIFICATION_ERROR_FROM_DB;
704         }
705
706         sqlbuf = sqlite3_mprintf("UPDATE %s SET do_not_disturb = %d, visibility_class = %d ",
707                         NOTIFICATION_SYSTEM_SETTING_DB_TABLE, do_not_disturb, visibility_class);
708
709         if (!sqlbuf) {
710                 NOTIFICATION_ERR("fail to alloc query");
711                 err = NOTIFICATION_ERROR_OUT_OF_MEMORY;
712                 goto return_close_db;
713         }
714
715         err = notification_db_exec(db, sqlbuf, NULL);
716
717         return_close_db:
718         if (sqlbuf)
719                 sqlite3_free(sqlbuf);
720
721         sqlret = db_util_close(db);
722         if (sqlret != SQLITE_OK) {
723                 NOTIFICATION_WARN("fail to db_util_close - [%d]", sqlret);
724         }
725
726         return err;
727 }
728
729 /* OLD IMPLEMENTATION ----------------------------*/
730 #define NOTIFICATION_SETTING_DB "notification_setting"
731 #define NOTIFICATION_SETTING_DB_PATH "/opt/usr/dbspace/.notification_parser.db"
732
733 struct _notification_setting_h {
734         char *appid;
735         char *notification;
736         char *sounds;
737         char *contents;
738         char *badge;
739         char *pkgid;
740 };
741
742 struct prop_table {
743         const char *property;
744         const char *column;
745         const char *default_value;
746 };
747
748 static struct prop_table g_prop_table[] = {
749         {
750                 .property = "OPT_NOTIFICATION",
751                 .column = "notification",
752                 .default_value = "ON",
753         },
754         {
755                 .property = "OPT_SOUNDS",
756                 .column = "sounds",
757                 .default_value = "ON",
758         },
759         {
760                 .property = "OPT_CONTENTS",
761                 .column = "contents",
762                 .default_value = "ON",
763         },
764         {
765                 .property = "OPT_BADGE",
766                 .column = "badge",
767                 .default_value = "ON",
768         },
769         {
770                 .property = NULL,
771                 .column = NULL,
772                 .default_value = NULL,
773         }
774 };
775
776 static const char *_get_prop_column(const char *property)
777 {
778         int i;
779
780         for (i = 0; g_prop_table[i].property; i++) {
781                 if (strcmp(g_prop_table[i].property, property))
782                         continue;
783
784                 return g_prop_table[i].column;
785         }
786
787         return NULL;
788 }
789
790 #ifdef TBD
791 static const char *_get_prop_default_value(const char *property)
792 {
793         int i;
794
795         for (i = 0; g_prop_table[i].property; i++) {
796                 if (strcmp(g_prop_table[i].property, property))
797                         continue;
798
799                 return g_prop_table[i].default_value;
800         }
801
802         return NULL;
803 }
804 #endif
805
806 static int _is_record_exist(const char *pkgname, sqlite3 *db)
807 {
808         sqlite3_stmt *stmt = NULL;
809         int count = 0;
810         int result = NOTIFICATION_ERROR_NONE;
811         char *sqlbuf = NULL;
812         int sqlret;
813
814         if (!pkgname)
815                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
816
817         if (!db)
818                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
819
820         sqlbuf = sqlite3_mprintf("SELECT count(*) FROM %s WHERE " \
821                         "appid = %Q",
822                         NOTIFICATION_SETTING_DB, pkgname);
823
824         if (!sqlbuf) {
825                 NOTIFICATION_ERR("fail to alloc sql query");
826                 return NOTIFICATION_ERROR_OUT_OF_MEMORY;
827         }
828
829         sqlret = sqlite3_prepare_v2(db, sqlbuf, -1, &stmt, NULL);
830         if (sqlret != SQLITE_OK) {
831                 NOTIFICATION_ERR("DB err [%s]", sqlite3_errmsg(db));
832                 NOTIFICATION_ERR("query[%s]", sqlbuf);
833                 result = NOTIFICATION_ERROR_FROM_DB;
834                 goto free_and_return;
835         }
836
837         sqlret = sqlite3_step(stmt);
838         if (sqlret == SQLITE_ROW)
839                 count = sqlite3_column_int(stmt, 0);
840         else
841                 count = 0;
842
843         if (count > 0)
844                 result = NOTIFICATION_ERROR_ALREADY_EXIST_ID;
845         else
846                 result = NOTIFICATION_ERROR_NOT_EXIST_ID;
847
848         free_and_return:
849         if (sqlbuf)
850                 sqlite3_free(sqlbuf);
851
852         if (stmt)
853                 sqlite3_finalize(stmt);
854
855         return result;
856 }
857
858 EXPORT_API int notification_setting_db_set(const char *pkgname, const char *property, const char *value)
859 {
860         int ret = NOTIFICATION_ERROR_NONE;
861         int result = NOTIFICATION_ERROR_NONE;
862         sqlite3 *db = NULL;
863         char *sqlbuf = NULL;
864         int sqlret;
865         const char *column = NULL;
866
867         if (!pkgname)
868                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
869
870         if (!property)
871                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
872
873         if (!value)
874                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
875
876         column = _get_prop_column(property);
877         if (!column)
878                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
879
880         sqlret = db_util_open(NOTIFICATION_SETTING_DB_PATH, &db, 0);
881         if (sqlret != SQLITE_OK || !db) {
882                 NOTIFICATION_ERR("fail to db_util_open - [%d]", sqlret);
883                 return NOTIFICATION_ERROR_FROM_DB;
884         }
885
886         ret = _is_record_exist(pkgname, db);
887         if (ret != NOTIFICATION_ERROR_ALREADY_EXIST_ID) {
888                 result = ret;
889                 goto return_close_db;
890         }
891
892         sqlbuf = sqlite3_mprintf("UPDATE %s SET %s = %Q " \
893                         "WHERE appid = %Q",
894                         NOTIFICATION_SETTING_DB, column, value, pkgname);
895         if (!sqlbuf) {
896                 NOTIFICATION_ERR("fail to alloc query");
897                 result = NOTIFICATION_ERROR_OUT_OF_MEMORY;
898                 goto return_close_db;
899         }
900
901         result = notification_db_exec(db, sqlbuf, NULL);
902
903         return_close_db:
904         if (sqlbuf)
905                 sqlite3_free(sqlbuf);
906
907         sqlret = db_util_close(db);
908         if (sqlret != SQLITE_OK) {
909                 NOTIFICATION_WARN("fail to db_util_close - [%d]", sqlret);
910         }
911
912         return result;
913 }
914
915 EXPORT_API int notification_setting_db_get(const char *pkgname, const char *property, char **value)
916 {
917         int ret = NOTIFICATION_ERROR_NONE;
918         int result = NOTIFICATION_ERROR_NONE;
919         sqlite3 *db = NULL;
920         char *sqlbuf = NULL;
921         sqlite3_stmt *stmt = NULL;
922         int sqlret;
923         const char *column = NULL;
924
925         if (!pkgname)
926                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
927
928         if (!property)
929                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
930
931         if (!value)
932                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
933
934         column = _get_prop_column(property);
935         if (!column)
936                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
937
938         sqlret = db_util_open(NOTIFICATION_SETTING_DB_PATH, &db, 0);
939         if (sqlret != SQLITE_OK || !db) {
940                 NOTIFICATION_ERR("fail to db_util_open - [%d]", sqlret);
941                 return NOTIFICATION_ERROR_FROM_DB;
942         }
943
944         ret = _is_record_exist(pkgname, db);
945         if (ret != NOTIFICATION_ERROR_ALREADY_EXIST_ID) {
946                 result = ret;
947                 goto return_close_db;
948         }
949
950         sqlbuf = sqlite3_mprintf("SELECT %s FROM %s " \
951                         "WHERE appid = %Q",
952                         column, NOTIFICATION_SETTING_DB, pkgname);
953         if (!sqlbuf) {
954                 NOTIFICATION_ERR("fail to alloc query");
955                 result = NOTIFICATION_ERROR_OUT_OF_MEMORY;
956                 goto return_close_db;
957         }
958
959         sqlret = sqlite3_prepare_v2(db, sqlbuf, -1, &stmt, NULL);
960         if (sqlret != SQLITE_OK) {
961                 NOTIFICATION_ERR("fail to prepare %s - [%s]",
962                                 sqlbuf, sqlite3_errmsg(db));
963                 result = NOTIFICATION_ERROR_FROM_DB;
964                 goto return_close_db;
965         }
966
967         sqlret = sqlite3_step(stmt);
968         if (sqlret == SQLITE_ROW) {
969                 int get_bytes = sqlite3_column_bytes(stmt, 0);
970                 char *get_data = (char *)calloc(get_bytes + 1, sizeof(char));
971                 if (get_data != NULL) {
972                         memcpy(get_data, sqlite3_column_text(stmt, 0),
973                                         get_bytes * sizeof(char));
974                         get_data[get_bytes] = '\0';
975                         *value = get_data;
976                 } else {
977                         NOTIFICATION_ERR("fail to alloc query");
978                         result = NOTIFICATION_ERROR_OUT_OF_MEMORY;
979                         goto return_close_db;
980                 }
981         }
982
983         return_close_db:
984         if (sqlbuf)
985                 sqlite3_free(sqlbuf);
986
987         if (stmt)
988                 sqlite3_finalize(stmt);
989
990         sqlret = db_util_close(db);
991         if (sqlret != SQLITE_OK)
992                 NOTIFICATION_WARN("fail to db_util_close - [%d]", sqlret);
993
994         return result;
995 }
996
997 EXPORT_API int notification_setting_property_set(const char *pkgname, const char *property, const char *value)
998 {
999         int ret = 0;
1000
1001         if (!pkgname)
1002                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1003
1004         if (!property)
1005                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1006
1007         if (!value)
1008                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1009
1010         ret = notification_ipc_noti_setting_property_set(pkgname, property, value);
1011         if (ret != NOTIFICATION_ERROR_NONE) {
1012                 return ret;
1013         }
1014
1015         return NOTIFICATION_ERROR_NONE;
1016 }
1017
1018 EXPORT_API int notification_setting_property_get(const char *pkgname, const char *property, char **value)
1019 {
1020         int ret = 0;
1021
1022         if (!pkgname)
1023                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1024
1025         if (!property)
1026                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1027
1028         if (!value)
1029                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1030
1031         ret = notification_ipc_noti_setting_property_get(pkgname, property, value);
1032         if (ret != NOTIFICATION_ERROR_NONE) {
1033                 return ret;
1034         }
1035
1036         return NOTIFICATION_ERROR_NONE;
1037 }
1038