Initialize Tizen 2.3
[framework/base/syspopup.git] / mobile / src / syspopup_db.c
1 /*
2  * syspopup
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>,
7  * Jaeho Lee <jaeho81.lee@samsung.com>
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  */
22
23
24 #include <stdio.h>
25 #include <string.h>
26 #include <stdlib.h>
27 #include <sqlite3.h>
28 #include "syspopup_db.h"
29 #include "simple_util.h"
30
31 #define SYSPOPUP_DB_PATH        "/opt/dbspace/.syspopup.db"
32 #define QUERY_MAXLEN    4096
33
34 #define SP_INFO_TBL             "syspopup_info"
35 #define SP_INFO_TBL_F_NAME      "name"
36
37 static sqlite3 *db = NULL;
38
39 /**
40  * exec  
41  * param[in] db handler
42  * param[in] query query
43  * return This method returns 0 (SUCCESS) or -1 (FAIL)
44  */
45 static int __exec(sqlite3 *db, char *query)
46 {
47         int rc = 0;
48         char *errmsg = NULL;
49
50         if (db == NULL) {
51                 _E("DB handler is null");
52                 return -1;
53         }
54         rc = sqlite3_exec(db, query, NULL, 0, &errmsg);
55
56         if (rc != SQLITE_OK) {
57                 _D("Query: [%s]", query);
58                 _E("SQL error: %s\n", errmsg);
59                 sqlite3_free(errmsg);
60                 return (-1);
61         }
62
63         return 0;
64 }
65
66 /**
67  * db initialize
68  */
69 static int __init(void)
70 {
71         int rc;
72
73         if (db) {
74                 _D("Already initialized\n");
75                 return 0;
76         }
77
78         rc = sqlite3_open(SYSPOPUP_DB_PATH, &db);
79         if (rc) {
80                 _E("Can't open database: %s", sqlite3_errmsg(db));
81                 goto err;
82         }
83         /* Enable persist journal mode*/
84         rc = sqlite3_exec(db, "PRAGMA journal_mode = PERSIST", NULL, NULL,
85                           NULL);
86         if (SQLITE_OK != rc) {
87                 _D("Fail to change journal mode\n");
88                 goto err;
89         }
90
91         return 0;
92 err:
93         sqlite3_close(db);
94         return -1;
95 }
96
97 static int __fini(void)
98 {
99         if (db) {
100                 sqlite3_close(db);
101                 db = NULL;
102         }
103         return 0;
104 }
105
106 static int __delete_all(const char *tbl_name)
107 {
108         char *_sqlbuf;
109         int rc;
110
111         _sqlbuf = sqlite3_mprintf("DELETE FROM %s;", tbl_name);
112         rc = __exec(db, _sqlbuf);
113         sqlite3_free(_sqlbuf);
114
115         return rc;
116 }
117
118 static int __delete_with_field(const char *tbl_name, const char *f_name,
119                                const char *val)
120 {
121         char *_sqlbuf;
122         int rc;
123
124         _sqlbuf = sqlite3_mprintf("DELETE FROM %s WHERE %s = '%s';",
125                                   tbl_name, f_name, val);
126         rc = __exec(db, _sqlbuf);
127         sqlite3_free(_sqlbuf);
128
129         return rc;
130 }
131
132 static int __count_with_field(const char *tbl_name, const char *f_name,
133                               const char *val)
134 {
135         char *_sqlbuf;
136         int rc;
137         char **db_result = NULL;
138         char *db_err = NULL;
139         int nrows = 0;
140         int ncols = 0;
141         int cnt;
142
143         _sqlbuf = sqlite3_mprintf("SELECT COUNT(*) FROM %s WHERE %s = '%s';",
144                                   tbl_name, f_name, val);
145
146         rc = sqlite3_get_table(db, _sqlbuf, &db_result, &nrows, &ncols,
147                                &db_err);
148         if (rc == -1 || nrows == 0) {
149                 _E("get count = 0 or fail");
150                 sqlite3_free_table(db_result);
151                 sqlite3_free(_sqlbuf);
152                 return 0;
153         } else {
154                 cnt = atoi(db_result[1]);
155                 sqlite3_free_table(db_result);
156                 sqlite3_free(_sqlbuf);
157         }
158
159         return cnt;
160 }
161
162 int _syspopup_info_add(syspopup_info_t *pinfo)
163 {
164         int rc = -1;
165         int cnt = 0;
166         char *_sqlbuf;
167
168         if (pinfo->name == NULL) {
169                 _E("Name is null\n");
170                 return -1;
171         }
172
173         if (__init() < 0)
174                 return -1;
175
176         cnt = __count_with_field(SP_INFO_TBL, SP_INFO_TBL_F_NAME, pinfo->name);
177
178         if (cnt == 0) {
179                 _sqlbuf = sqlite3_mprintf("INSERT INTO %s "
180                                           "(name,prio,focus,timeout,term_act,endkey_act,pkgname) values "
181                                           "(\"%s\", %d, %d, %d, \"%s\");",
182                                           SP_INFO_TBL,
183                                           pinfo->name, pinfo->prio,
184                                           pinfo->focus, pinfo->timeout,
185                                           pinfo->term_act, pinfo->endkey_act,
186                                           pinfo->pkgname);
187                 rc = __exec(db, _sqlbuf);
188                 sqlite3_free(_sqlbuf);
189         } else {
190                 _E("already exist - %s", pinfo->name);
191                 rc = -1;
192         }
193
194         if (rc < 0)
195                 _E("Fail to insert\n");
196
197         __fini();
198         return rc;
199 }
200
201 syspopup_info_t *_syspopup_info_get(const char *popup_name)
202 {
203         int rc = 0;
204         char *_sqlbuf;
205         sqlite3_stmt *stmt;
206         syspopup_info_t *pinfo = NULL;
207
208         if (popup_name == NULL) {
209                 _E("no popup name");
210                 return NULL;
211         }
212
213         if (__init() < 0)
214                 return NULL;
215
216         _sqlbuf = sqlite3_mprintf("SELECT name,prio,focus,timeout,term_act,endkey_act,pkgname "
217                                   "FROM %s WHERE %s='%s';",
218                                   SP_INFO_TBL, SP_INFO_TBL_F_NAME, popup_name);
219
220         rc = sqlite3_prepare_v2(db, _sqlbuf, -1, &stmt, NULL);
221         if (rc == SQLITE_OK) {
222                 rc = sqlite3_step(stmt);
223                 if (rc == SQLITE_ROW) {
224                         pinfo = (syspopup_info_t *) malloc(sizeof(syspopup_info_t));
225                         if (pinfo == NULL) {
226                                 __fini();
227                                 return NULL;
228                         }
229                         pinfo->name = strdup((char *) sqlite3_column_text(stmt, 0));
230                         pinfo->prio = sqlite3_column_int(stmt, 1);
231                         pinfo->focus = sqlite3_column_int(stmt, 2);
232                         pinfo->timeout = sqlite3_column_int(stmt, 3);
233                         pinfo->term_act = sqlite3_column_int(stmt, 4);
234                         pinfo->endkey_act = sqlite3_column_int(stmt, 5);
235                         if (sqlite3_column_text(stmt, 6) != NULL)
236                                 pinfo->pkgname = strdup((char *) sqlite3_column_text(stmt, 6));
237                         else
238                                 pinfo->pkgname = NULL;
239                 }
240                 sqlite3_finalize(stmt);
241         }
242
243         sqlite3_free(_sqlbuf);
244         __fini();
245         return pinfo;
246 }
247
248 void _syspopup_info_free(syspopup_info_t *pinfo)
249 {
250         if (pinfo->name)
251                 free(pinfo->name);
252         if (pinfo->pkgname)
253                 free(pinfo->pkgname);
254         free(pinfo);
255 }
256