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 / %d / %d", sqlite3_errmsg(db),
81 rc, sqlite3_extended_errcode(db));
88 static int __fini(void)
97 static int __delete_all(const char *tbl_name)
102 _sqlbuf = sqlite3_mprintf("DELETE FROM %s;", tbl_name);
103 rc = __exec(db, _sqlbuf);
104 sqlite3_free(_sqlbuf);
109 static int __delete_with_field(const char *tbl_name, const char *f_name,
115 _sqlbuf = sqlite3_mprintf("DELETE FROM %s WHERE %s = '%s';",
116 tbl_name, f_name, val);
117 rc = __exec(db, _sqlbuf);
118 sqlite3_free(_sqlbuf);
123 static int __count_with_field(const char *tbl_name, const char *f_name,
128 char **db_result = NULL;
134 _sqlbuf = sqlite3_mprintf("SELECT COUNT(*) FROM %s WHERE %s = '%s';",
135 tbl_name, f_name, val);
137 rc = sqlite3_get_table(db, _sqlbuf, &db_result, &nrows, &ncols,
139 if (rc == -1 || nrows == 0) {
140 _E("get count = 0 or fail");
141 sqlite3_free_table(db_result);
142 sqlite3_free(_sqlbuf);
145 cnt = atoi(db_result[1]);
146 sqlite3_free_table(db_result);
147 sqlite3_free(_sqlbuf);
153 int _syspopup_info_add(syspopup_info_t *pinfo)
159 if (pinfo->name == NULL) {
160 _E("Name is null\n");
167 cnt = __count_with_field(SP_INFO_TBL, SP_INFO_TBL_F_NAME, pinfo->name);
170 _sqlbuf = sqlite3_mprintf("INSERT INTO %s "
171 "(name,prio,focus,timeout,term_act,endkey_act,pkgname) values "
172 "(\"%s\", %d, %d, %d, \"%s\");",
174 pinfo->name, pinfo->prio,
175 pinfo->focus, pinfo->timeout,
176 pinfo->term_act, pinfo->endkey_act,
178 rc = __exec(db, _sqlbuf);
179 sqlite3_free(_sqlbuf);
181 _E("already exist - %s", pinfo->name);
186 _E("Fail to insert\n");
192 syspopup_info_t *_syspopup_info_get(const char *popup_name)
195 char sqlbuf[256] = {0,};
197 syspopup_info_t *pinfo = NULL;
199 if (popup_name == NULL) {
207 snprintf(sqlbuf, 256,
208 "SELECT name,prio,focus,timeout,term_act,endkey_act,pkgname FROM %s WHERE %s='%s';",
209 SP_INFO_TBL, SP_INFO_TBL_F_NAME, popup_name);
211 rc = sqlite3_prepare_v2(db, sqlbuf, -1, &stmt, NULL);
212 if (rc == SQLITE_OK) {
213 rc = sqlite3_step(stmt);
214 if (rc == SQLITE_ROW) {
215 pinfo = (syspopup_info_t *) malloc(sizeof(syspopup_info_t));
220 pinfo->name = strdup((char *) sqlite3_column_text(stmt, 0));
221 pinfo->prio = sqlite3_column_int(stmt, 1);
222 pinfo->focus = sqlite3_column_int(stmt, 2);
223 pinfo->timeout = sqlite3_column_int(stmt, 3);
224 pinfo->term_act = sqlite3_column_int(stmt, 4);
225 pinfo->endkey_act = sqlite3_column_int(stmt, 5);
226 if (sqlite3_column_text(stmt, 6) != NULL)
227 pinfo->pkgname = strdup((char *) sqlite3_column_text(stmt, 6));
229 pinfo->pkgname = NULL;
231 sqlite3_finalize(stmt);
235 _E("error(%d) in prepare", rc);
243 void _syspopup_info_free(syspopup_info_t *pinfo)
248 free(pinfo->pkgname);