Initialize Tizen 2.3
[framework/base/syspopup.git] / wearable / 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 / %d / %d", sqlite3_errmsg(db),
81                         rc, sqlite3_extended_errcode(db));
82                 return -1;
83         }
84
85         return 0;
86 }
87
88 static int __fini(void)
89 {
90         if (db) {
91                 sqlite3_close(db);
92                 db = NULL;
93         }
94         return 0;
95 }
96
97 static int __delete_all(const char *tbl_name)
98 {
99         char *_sqlbuf;
100         int rc;
101
102         _sqlbuf = sqlite3_mprintf("DELETE FROM %s;", tbl_name);
103         rc = __exec(db, _sqlbuf);
104         sqlite3_free(_sqlbuf);
105
106         return rc;
107 }
108
109 static int __delete_with_field(const char *tbl_name, const char *f_name,
110                                const char *val)
111 {
112         char *_sqlbuf;
113         int rc;
114
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);
119
120         return rc;
121 }
122
123 static int __count_with_field(const char *tbl_name, const char *f_name,
124                               const char *val)
125 {
126         char *_sqlbuf;
127         int rc;
128         char **db_result = NULL;
129         char *db_err = NULL;
130         int nrows = 0;
131         int ncols = 0;
132         int cnt;
133
134         _sqlbuf = sqlite3_mprintf("SELECT COUNT(*) FROM %s WHERE %s = '%s';",
135                                   tbl_name, f_name, val);
136
137         rc = sqlite3_get_table(db, _sqlbuf, &db_result, &nrows, &ncols,
138                                &db_err);
139         if (rc == -1 || nrows == 0) {
140                 _E("get count = 0 or fail");
141                 sqlite3_free_table(db_result);
142                 sqlite3_free(_sqlbuf);
143                 return 0;
144         } else {
145                 cnt = atoi(db_result[1]);
146                 sqlite3_free_table(db_result);
147                 sqlite3_free(_sqlbuf);
148         }
149
150         return cnt;
151 }
152
153 int _syspopup_info_add(syspopup_info_t *pinfo)
154 {
155         int rc = -1;
156         int cnt = 0;
157         char *_sqlbuf;
158
159         if (pinfo->name == NULL) {
160                 _E("Name is null\n");
161                 return -1;
162         }
163
164         if (__init() < 0)
165                 return -1;
166
167         cnt = __count_with_field(SP_INFO_TBL, SP_INFO_TBL_F_NAME, pinfo->name);
168
169         if (cnt == 0) {
170                 _sqlbuf = sqlite3_mprintf("INSERT INTO %s "
171                                           "(name,prio,focus,timeout,term_act,endkey_act,pkgname) values "
172                                           "(\"%s\", %d, %d, %d, \"%s\");",
173                                           SP_INFO_TBL,
174                                           pinfo->name, pinfo->prio,
175                                           pinfo->focus, pinfo->timeout,
176                                           pinfo->term_act, pinfo->endkey_act,
177                                           pinfo->pkgname);
178                 rc = __exec(db, _sqlbuf);
179                 sqlite3_free(_sqlbuf);
180         } else {
181                 _E("already exist - %s", pinfo->name);
182                 rc = -1;
183         }
184
185         if (rc < 0)
186                 _E("Fail to insert\n");
187
188         __fini();
189         return rc;
190 }
191
192 syspopup_info_t *_syspopup_info_get(const char *popup_name)
193 {
194         int rc = 0;
195         char sqlbuf[256] = {0,};
196         sqlite3_stmt *stmt;
197         syspopup_info_t *pinfo = NULL;
198
199         if (popup_name == NULL) {
200                 _E("no popup name");
201                 return NULL;
202         }
203
204         if (__init() < 0)
205                 return NULL;
206
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);
210
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));
216                         if (pinfo == NULL) {
217                                 _E("malloc error");
218                                 goto out;
219                         }
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));
228                         else
229                                 pinfo->pkgname = NULL;
230                 }
231                 sqlite3_finalize(stmt);
232         }
233
234         if(pinfo == NULL) {
235                 _E("error(%d) in prepare", rc);
236         }
237
238 out:
239         __fini();
240         return pinfo;
241 }
242
243 void _syspopup_info_free(syspopup_info_t *pinfo)
244 {
245         if (pinfo->name)
246                 free(pinfo->name);
247         if (pinfo->pkgname)
248                 free(pinfo->pkgname);
249         free(pinfo);
250 }
251