4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>,
7 * Jaeho Lee <jaeho81.lee@samsung.com>
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
28 #include "syspopup_db.h"
29 #include "simple_util.h"
31 #define SYSPOPUP_DB_PATH "/opt/dbspace/.syspopup.db"
32 #define QUERY_MAXLEN 4096
34 #define SP_INFO_TBL "syspopup_info"
35 #define SP_INFO_TBL_F_NAME "name"
37 static sqlite3 *db = NULL;
41 * param[in] db handler
42 * param[in] query query
43 * return This method returns 0 (SUCCESS) or -1 (FAIL)
45 static int __exec(sqlite3 *db, char *query)
51 _E("DB handler is null");
54 rc = sqlite3_exec(db, query, NULL, 0, &errmsg);
56 if (rc != SQLITE_OK) {
57 _D("Query: [%s]", query);
58 _E("SQL error: %s\n", errmsg);
69 static int __init(void)
74 _D("Already initialized\n");
78 rc = sqlite3_open(SYSPOPUP_DB_PATH, &db);
80 _E("Can't open database: %s", sqlite3_errmsg(db));
83 /* Enable persist journal mode*/
84 rc = sqlite3_exec(db, "PRAGMA journal_mode = PERSIST", NULL, NULL,
86 if (SQLITE_OK != rc) {
87 _D("Fail to change journal mode\n");
97 static int __fini(void)
106 static int __delete_all(const char *tbl_name)
111 _sqlbuf = sqlite3_mprintf("DELETE FROM %s;", tbl_name);
112 rc = __exec(db, _sqlbuf);
113 sqlite3_free(_sqlbuf);
118 static int __delete_with_field(const char *tbl_name, const char *f_name,
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);
132 static int __count_with_field(const char *tbl_name, const char *f_name,
137 char **db_result = NULL;
143 _sqlbuf = sqlite3_mprintf("SELECT COUNT(*) FROM %s WHERE %s = '%s';",
144 tbl_name, f_name, val);
146 rc = sqlite3_get_table(db, _sqlbuf, &db_result, &nrows, &ncols,
148 if (rc == -1 || nrows == 0) {
149 _E("get count = 0 or fail");
150 sqlite3_free_table(db_result);
151 sqlite3_free(_sqlbuf);
154 cnt = atoi(db_result[1]);
155 sqlite3_free_table(db_result);
156 sqlite3_free(_sqlbuf);
162 int _syspopup_info_add(syspopup_info_t *pinfo)
168 if (pinfo->name == NULL) {
169 _E("Name is null\n");
176 cnt = __count_with_field(SP_INFO_TBL, SP_INFO_TBL_F_NAME, pinfo->name);
179 _sqlbuf = sqlite3_mprintf("INSERT INTO %s "
180 "(name,prio,focus,timeout,term_act,endkey_act,pkgname) values "
181 "(\"%s\", %d, %d, %d, \"%s\");",
183 pinfo->name, pinfo->prio,
184 pinfo->focus, pinfo->timeout,
185 pinfo->term_act, pinfo->endkey_act,
187 rc = __exec(db, _sqlbuf);
188 sqlite3_free(_sqlbuf);
190 _E("already exist - %s", pinfo->name);
195 _E("Fail to insert\n");
201 syspopup_info_t *_syspopup_info_get(const char *popup_name)
206 syspopup_info_t *pinfo = NULL;
208 if (popup_name == NULL) {
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);
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));
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));
238 pinfo->pkgname = NULL;
240 sqlite3_finalize(stmt);
243 sqlite3_free(_sqlbuf);
248 void _syspopup_info_free(syspopup_info_t *pinfo)
253 free(pinfo->pkgname);