Clean up file mode
[platform/core/base/syspopup.git] / src / syspopup_db.c
1 /*
2  * Copyright (c) 2000 - 2017 Samsung Electronics Co., Ltd. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17
18 #include <stdio.h>
19 #include <string.h>
20 #include <stdlib.h>
21 #include <sqlite3.h>
22
23 /* For multi-user support */
24 #include <tzplatform_config.h>
25
26 #include "syspopup_db.h"
27 #include "simple_util.h"
28
29 #define SYSPOPUP_DB_PATH tzplatform_mkpath(TZ_SYS_DB, ".syspopup.db")
30 #define QUERY_MAXLEN 4096
31
32 #define SP_INFO_TBL "syspopup_info"
33 #define SP_INFO_TBL_F_NAME "name"
34
35 static sqlite3 *db;
36
37 /* db initialize */
38 static int __init(void)
39 {
40         int rc;
41
42         if (db) {
43                 _D("Already initialized\n");
44                 return 0;
45         }
46
47         rc = sqlite3_open_v2(SYSPOPUP_DB_PATH, &db, SQLITE_OPEN_READONLY, NULL);
48         if (rc != SQLITE_OK) {
49                 _E("Can't open database: %s / %d / %d", sqlite3_errmsg(db),
50                                 rc, sqlite3_extended_errcode(db));
51                 return -1;
52         } else {
53                 _D("db open success");
54         }
55
56         return 0;
57 }
58
59 static int __fini(void)
60 {
61         if (db) {
62                 sqlite3_close_v2(db);
63                 db = NULL;
64         }
65
66         return 0;
67 }
68
69 syspopup_info_t *_syspopup_info_get(const char *popup_name)
70 {
71         int rc;
72         char sqlbuf[256] = {0,};
73         sqlite3_stmt *stmt = NULL;
74         syspopup_info_t *pinfo = NULL;
75
76         if (popup_name == NULL) {
77                 _E("no popup name");
78                 return NULL;
79         }
80
81         if (__init() < 0)
82                 return NULL;
83
84         snprintf(sqlbuf, sizeof(sqlbuf),
85                 "SELECT name,prio,focus,timeout,term_act,endkey_act,pkgname FROM %s WHERE %s = ?;",
86                 SP_INFO_TBL, SP_INFO_TBL_F_NAME);
87
88         rc = sqlite3_prepare_v2(db, sqlbuf, -1, &stmt, NULL);
89         if (rc != SQLITE_OK) {
90                 _E("popup info prepare error(%d)", rc);
91                 goto out;
92         }
93
94         rc = sqlite3_bind_text(stmt, 1, popup_name, strlen(popup_name), SQLITE_STATIC);
95         if (rc != SQLITE_OK) {
96                 _E("popup name bind error(%d)", rc);
97                 goto out;
98         }
99
100         rc = sqlite3_step(stmt);
101         if (rc != SQLITE_ROW) {
102                 _E("error(%d) in prepare", rc);
103                 goto out;
104         }
105
106         pinfo = (syspopup_info_t *)malloc(sizeof(syspopup_info_t));
107         if (pinfo == NULL) {
108                 _E("malloc error");
109                 goto out;
110         }
111
112         pinfo->name = strdup((char *)sqlite3_column_text(stmt, 0));
113         pinfo->prio = sqlite3_column_int(stmt, 1);
114         pinfo->focus = sqlite3_column_int(stmt, 2);
115         pinfo->timeout = sqlite3_column_int(stmt, 3);
116         pinfo->term_act = sqlite3_column_int(stmt, 4);
117         pinfo->endkey_act = sqlite3_column_int(stmt, 5);
118
119         if (sqlite3_column_text(stmt, 6) != NULL)
120                 pinfo->pkgname = strdup((char *) sqlite3_column_text(stmt, 6));
121         else
122                 pinfo->pkgname = NULL;
123
124 out:
125         sqlite3_finalize(stmt);
126         __fini();
127
128         return pinfo;
129 }
130
131 void _syspopup_info_free(syspopup_info_t *pinfo)
132 {
133         if (pinfo->name)
134                 free(pinfo->name);
135
136         if (pinfo->pkgname)
137                 free(pinfo->pkgname);
138
139         free(pinfo);
140 }
141
142 int _syspopup_info_foreach(int (*callback)(syspopup_info_t *, void *),
143                 void *user_data)
144 {
145         const char query[] = "SELECT name, prio, focus, timeout, term_act, "
146                 "endkey_act, pkgname FROM syspopup_info;";
147         sqlite3_stmt *stmt;
148         syspopup_info_t info;
149         int r;
150         int idx;
151
152         if (callback == NULL) {
153                 _E("Invalid parameter");
154                 return -1;
155         }
156
157         if (__init() < 0)
158                 return -1;
159
160         r = sqlite3_prepare_v2(db, query, sizeof(query), &stmt, NULL);
161         if (r != SQLITE_OK) {
162                 _E("sqlite3_prepare_v2() is failed - %d(%s)",
163                                 r, sqlite3_errmsg(db));
164                 __fini();
165                 return -1;
166         }
167
168         while (sqlite3_step(stmt) == SQLITE_ROW) {
169                 idx = 0;
170                 info.name = (char *)sqlite3_column_text(stmt, idx++);
171                 info.prio = sqlite3_column_int(stmt, idx++);
172                 info.focus = sqlite3_column_int(stmt, idx++);
173                 info.timeout = sqlite3_column_int(stmt, idx++);
174                 info.term_act = sqlite3_column_int(stmt, idx++);
175                 info.endkey_act = sqlite3_column_int(stmt, idx++);
176                 info.pkgname = (char *)sqlite3_column_text(stmt, idx);
177
178                 if (callback(&info, user_data) < 0)
179                         break;
180         }
181         sqlite3_finalize(stmt);
182         __fini();
183
184         return 0;
185 }