Move internal API set to internal header file.
[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
694 EXPORT_API int notification_setting_db_update_system_setting(int do_not_disturb, int visibility_class)
695 {
696         int err = NOTIFICATION_ERROR_NONE;
697         char *query_buffer = NULL;
698         int sqlret;
699         int field_index = 0;
700         sqlite3 *db = NULL;
701         sqlite3_stmt *db_statement = NULL;
702
703         sqlret = db_util_open(DBPATH, &db, 0);
704
705         if (sqlret != SQLITE_OK || db == NULL) {
706                 NOTIFICATION_ERR("db_util_open failed [%s][%d][%s]", DBPATH, sqlret, sqlite3_errmsg(db));
707                 err =  NOTIFICATION_ERROR_FROM_DB;
708                 goto return_close_db;
709         }
710
711         query_buffer = strdup("UPDATE ? SET do_not_disturb = ?, visibility_class = ? ");
712
713         if (query_buffer == NULL) {
714                 NOTIFICATION_ERR("fail to alloc query");
715                 err = NOTIFICATION_ERROR_OUT_OF_MEMORY;
716                 goto return_close_db;
717         }
718
719         sqlret = sqlite3_prepare_v2(db, query_buffer, strlen(query_buffer), &db_statement, NULL);
720
721         if (sqlret != SQLITE_OK) {
722                 NOTIFICATION_ERR("sqlite3_prepare_v2 failed [%d][%s]", sqlret, sqlite3_errmsg(db));
723                 err =  NOTIFICATION_ERROR_FROM_DB;
724                 goto return_close_db;
725         }
726
727         sqlite3_bind_text(db_statement, field_index++, NOTIFICATION_SYSTEM_SETTING_DB_TABLE, -1, SQLITE_STATIC);
728         sqlite3_bind_int(db_statement, field_index++, do_not_disturb);
729         sqlite3_bind_int(db_statement, field_index++, visibility_class);
730
731         sqlret = sqlite3_step(db_statement);
732
733         if (sqlret != SQLITE_OK && sqlret != SQLITE_DONE) {
734                 NOTIFICATION_ERR("sqlite3_step failed [%d][%s]", sqlret, sqlite3_errmsg(db));
735                 err =  NOTIFICATION_ERROR_FROM_DB;
736                 goto return_close_db;
737         }
738
739         sqlret = sqlite3_changes(db);
740
741         if (sqlret != SQLITE_OK && sqlret != SQLITE_DONE) {
742                 NOTIFICATION_ERR("sqlite3_changes failed [%d][%s]", sqlret, sqlite3_errmsg(db));
743                 err =  NOTIFICATION_ERROR_FROM_DB;
744                 goto return_close_db;
745         }
746
747 return_close_db:
748
749         if (query_buffer)
750                 free(query_buffer);
751
752         if (db_statement)
753                 sqlite3_finalize(db_statement);
754
755         if (db)
756                 sqlret = db_util_close(db);
757
758         if (sqlret != SQLITE_OK) {
759                 NOTIFICATION_WARN("fail to db_util_close - [%d]", sqlret);
760         }
761
762         return err;
763 }
764
765 /* OLD IMPLEMENTATION ----------------------------*/
766 #define NOTIFICATION_SETTING_DB "notification_setting"
767 #define NOTIFICATION_SETTING_DB_PATH "/opt/usr/dbspace/.notification_parser.db"
768
769 struct _notification_setting_h {
770         char *appid;
771         char *notification;
772         char *sounds;
773         char *contents;
774         char *badge;
775         char *pkgid;
776 };
777
778 struct prop_table {
779         const char *property;
780         const char *column;
781         const char *default_value;
782 };
783
784 static struct prop_table g_prop_table[] = {
785         {
786                 .property = "OPT_NOTIFICATION",
787                 .column = "notification",
788                 .default_value = "ON",
789         },
790         {
791                 .property = "OPT_SOUNDS",
792                 .column = "sounds",
793                 .default_value = "ON",
794         },
795         {
796                 .property = "OPT_CONTENTS",
797                 .column = "contents",
798                 .default_value = "ON",
799         },
800         {
801                 .property = "OPT_BADGE",
802                 .column = "badge",
803                 .default_value = "ON",
804         },
805         {
806                 .property = NULL,
807                 .column = NULL,
808                 .default_value = NULL,
809         }
810 };
811
812 static const char *_get_prop_column(const char *property)
813 {
814         int i;
815
816         for (i = 0; g_prop_table[i].property; i++) {
817                 if (strcmp(g_prop_table[i].property, property))
818                         continue;
819
820                 return g_prop_table[i].column;
821         }
822
823         return NULL;
824 }
825
826 #ifdef TBD
827 static const char *_get_prop_default_value(const char *property)
828 {
829         int i;
830
831         for (i = 0; g_prop_table[i].property; i++) {
832                 if (strcmp(g_prop_table[i].property, property))
833                         continue;
834
835                 return g_prop_table[i].default_value;
836         }
837
838         return NULL;
839 }
840 #endif
841
842 static int _is_record_exist(const char *pkgname, sqlite3 *db)
843 {
844         sqlite3_stmt *stmt = NULL;
845         int count = 0;
846         int result = NOTIFICATION_ERROR_NONE;
847         char *sqlbuf = NULL;
848         int sqlret;
849
850         if (!pkgname)
851                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
852
853         if (!db)
854                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
855
856         sqlbuf = sqlite3_mprintf("SELECT count(*) FROM %s WHERE " \
857                         "appid = %Q",
858                         NOTIFICATION_SETTING_DB, pkgname);
859
860         if (!sqlbuf) {
861                 NOTIFICATION_ERR("fail to alloc sql query");
862                 return NOTIFICATION_ERROR_OUT_OF_MEMORY;
863         }
864
865         sqlret = sqlite3_prepare_v2(db, sqlbuf, -1, &stmt, NULL);
866         if (sqlret != SQLITE_OK) {
867                 NOTIFICATION_ERR("DB err [%s]", sqlite3_errmsg(db));
868                 NOTIFICATION_ERR("query[%s]", sqlbuf);
869                 result = NOTIFICATION_ERROR_FROM_DB;
870                 goto free_and_return;
871         }
872
873         sqlret = sqlite3_step(stmt);
874         if (sqlret == SQLITE_ROW)
875                 count = sqlite3_column_int(stmt, 0);
876         else
877                 count = 0;
878
879         if (count > 0)
880                 result = NOTIFICATION_ERROR_ALREADY_EXIST_ID;
881         else
882                 result = NOTIFICATION_ERROR_NOT_EXIST_ID;
883
884         free_and_return:
885         if (sqlbuf)
886                 sqlite3_free(sqlbuf);
887
888         if (stmt)
889                 sqlite3_finalize(stmt);
890
891         return result;
892 }
893
894 EXPORT_API int notification_setting_db_set(const char *pkgname, const char *property, const char *value)
895 {
896         int ret = NOTIFICATION_ERROR_NONE;
897         int result = NOTIFICATION_ERROR_NONE;
898         sqlite3 *db = NULL;
899         char *sqlbuf = NULL;
900         int sqlret;
901         const char *column = NULL;
902
903         if (!pkgname)
904                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
905
906         if (!property)
907                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
908
909         if (!value)
910                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
911
912         column = _get_prop_column(property);
913         if (!column)
914                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
915
916         sqlret = db_util_open(NOTIFICATION_SETTING_DB_PATH, &db, 0);
917         if (sqlret != SQLITE_OK || !db) {
918                 NOTIFICATION_ERR("fail to db_util_open - [%d]", sqlret);
919                 return NOTIFICATION_ERROR_FROM_DB;
920         }
921
922         ret = _is_record_exist(pkgname, db);
923         if (ret != NOTIFICATION_ERROR_ALREADY_EXIST_ID) {
924                 result = ret;
925                 goto return_close_db;
926         }
927
928         sqlbuf = sqlite3_mprintf("UPDATE %s SET %s = %Q " \
929                         "WHERE appid = %Q",
930                         NOTIFICATION_SETTING_DB, column, value, pkgname);
931         if (!sqlbuf) {
932                 NOTIFICATION_ERR("fail to alloc query");
933                 result = NOTIFICATION_ERROR_OUT_OF_MEMORY;
934                 goto return_close_db;
935         }
936
937         result = notification_db_exec(db, sqlbuf, NULL);
938
939         return_close_db:
940         if (sqlbuf)
941                 sqlite3_free(sqlbuf);
942
943         sqlret = db_util_close(db);
944         if (sqlret != SQLITE_OK) {
945                 NOTIFICATION_WARN("fail to db_util_close - [%d]", sqlret);
946         }
947
948         return result;
949 }
950
951 EXPORT_API int notification_setting_db_get(const char *pkgname, const char *property, char **value)
952 {
953         int ret = NOTIFICATION_ERROR_NONE;
954         int result = NOTIFICATION_ERROR_NONE;
955         sqlite3 *db = NULL;
956         char *sqlbuf = NULL;
957         sqlite3_stmt *stmt = NULL;
958         int sqlret;
959         const char *column = NULL;
960
961         if (!pkgname)
962                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
963
964         if (!property)
965                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
966
967         if (!value)
968                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
969
970         column = _get_prop_column(property);
971         if (!column)
972                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
973
974         sqlret = db_util_open(NOTIFICATION_SETTING_DB_PATH, &db, 0);
975         if (sqlret != SQLITE_OK || !db) {
976                 NOTIFICATION_ERR("fail to db_util_open - [%d]", sqlret);
977                 return NOTIFICATION_ERROR_FROM_DB;
978         }
979
980         ret = _is_record_exist(pkgname, db);
981         if (ret != NOTIFICATION_ERROR_ALREADY_EXIST_ID) {
982                 result = ret;
983                 goto return_close_db;
984         }
985
986         sqlbuf = sqlite3_mprintf("SELECT %s FROM %s " \
987                         "WHERE appid = %Q",
988                         column, NOTIFICATION_SETTING_DB, pkgname);
989         if (!sqlbuf) {
990                 NOTIFICATION_ERR("fail to alloc query");
991                 result = NOTIFICATION_ERROR_OUT_OF_MEMORY;
992                 goto return_close_db;
993         }
994
995         sqlret = sqlite3_prepare_v2(db, sqlbuf, -1, &stmt, NULL);
996         if (sqlret != SQLITE_OK) {
997                 NOTIFICATION_ERR("fail to prepare %s - [%s]",
998                                 sqlbuf, sqlite3_errmsg(db));
999                 result = NOTIFICATION_ERROR_FROM_DB;
1000                 goto return_close_db;
1001         }
1002
1003         sqlret = sqlite3_step(stmt);
1004         if (sqlret == SQLITE_ROW) {
1005                 int get_bytes = sqlite3_column_bytes(stmt, 0);
1006                 char *get_data = (char *)calloc(get_bytes + 1, sizeof(char));
1007                 if (get_data != NULL) {
1008                         memcpy(get_data, sqlite3_column_text(stmt, 0),
1009                                         get_bytes * sizeof(char));
1010                         get_data[get_bytes] = '\0';
1011                         *value = get_data;
1012                 } else {
1013                         NOTIFICATION_ERR("fail to alloc query");
1014                         result = NOTIFICATION_ERROR_OUT_OF_MEMORY;
1015                         goto return_close_db;
1016                 }
1017         }
1018
1019         return_close_db:
1020         if (sqlbuf)
1021                 sqlite3_free(sqlbuf);
1022
1023         if (stmt)
1024                 sqlite3_finalize(stmt);
1025
1026         sqlret = db_util_close(db);
1027         if (sqlret != SQLITE_OK)
1028                 NOTIFICATION_WARN("fail to db_util_close - [%d]", sqlret);
1029
1030         return result;
1031 }
1032
1033 EXPORT_API int notification_setting_property_set(const char *pkgname, const char *property, const char *value)
1034 {
1035         int ret = 0;
1036
1037         if (!pkgname)
1038                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1039
1040         if (!property)
1041                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1042
1043         if (!value)
1044                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1045
1046         ret = notification_ipc_noti_setting_property_set(pkgname, property, value);
1047         if (ret != NOTIFICATION_ERROR_NONE) {
1048                 return ret;
1049         }
1050
1051         return NOTIFICATION_ERROR_NONE;
1052 }
1053
1054 EXPORT_API int notification_setting_property_get(const char *pkgname, const char *property, char **value)
1055 {
1056         int ret = 0;
1057
1058         if (!pkgname)
1059                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1060
1061         if (!property)
1062                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1063
1064         if (!value)
1065                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1066
1067         ret = notification_ipc_noti_setting_property_get(pkgname, property, value);
1068         if (ret != NOTIFICATION_ERROR_NONE) {
1069                 return ret;
1070         }
1071
1072         return NOTIFICATION_ERROR_NONE;
1073 }
1074