Initialize Tizen 2.3
[framework/base/syspopup.git] / 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  * db initialize
41  */
42 static int __init(void)
43 {
44         int rc;
45
46         if (db) {
47                 _D("Already initialized\n");
48                 return 0;
49         }
50
51         rc = sqlite3_open(SYSPOPUP_DB_PATH, &db);
52         if (rc != SQLITE_OK) {
53                 _E("Can't open database: %s / %d / %d", sqlite3_errmsg(db),
54                         rc, sqlite3_extended_errcode(db));
55                 return -1;
56         } else {
57                 _D("db open sucess");
58         }
59
60         return 0;
61 }
62
63 static int __fini(void)
64 {
65         if (db) {
66                 sqlite3_close(db);
67                 db = NULL;
68         }
69         return 0;
70 }
71
72 syspopup_info_t *_syspopup_info_get(const char *popup_name)
73 {
74         int rc = 0;
75         char sqlbuf[256] = {0,};
76         sqlite3_stmt *stmt = NULL;
77         syspopup_info_t *pinfo = NULL;
78
79         if (popup_name == NULL) {
80                 _E("no popup name");
81                 return NULL;
82         }
83
84         if (__init() < 0)
85                 return NULL;
86
87         snprintf(sqlbuf, 256,
88                 "SELECT name,prio,focus,timeout,term_act,endkey_act,pkgname FROM %s WHERE %s = ?;",
89                 SP_INFO_TBL, SP_INFO_TBL_F_NAME);
90
91         rc = sqlite3_prepare_v2(db, sqlbuf, -1, &stmt, NULL);
92         if (rc == SQLITE_OK) {
93                 rc =sqlite3_bind_text(stmt, 1, popup_name, strlen(popup_name), SQLITE_STATIC);
94                 if(rc != SQLITE_OK) {
95                         _E("popup name bind error(%d) \n", rc);
96                         sqlite3_finalize(stmt);
97                         goto out;
98                 }
99
100                 rc = sqlite3_step(stmt);
101                 if (rc == SQLITE_ROW) {
102                         pinfo = (syspopup_info_t *) malloc(sizeof(syspopup_info_t));
103                         if (pinfo == NULL) {
104                                 _E("malloc error");
105                                 sqlite3_finalize(stmt);
106                                 goto out;
107                         }
108                         pinfo->name = strdup((char *) sqlite3_column_text(stmt, 0));
109                         pinfo->prio = sqlite3_column_int(stmt, 1);
110                         pinfo->focus = sqlite3_column_int(stmt, 2);
111                         pinfo->timeout = sqlite3_column_int(stmt, 3);
112                         pinfo->term_act = sqlite3_column_int(stmt, 4);
113                         pinfo->endkey_act = sqlite3_column_int(stmt, 5);
114                         if (sqlite3_column_text(stmt, 6) != NULL)
115                                 pinfo->pkgname = strdup((char *) sqlite3_column_text(stmt, 6));
116                         else
117                                 pinfo->pkgname = NULL;
118                 }
119                 sqlite3_finalize(stmt);
120         }
121
122         if(pinfo == NULL) {
123                 _E("error(%d) in prepare", rc);
124         }
125
126 out:
127         __fini();
128         return pinfo;
129 }
130
131 void _syspopup_info_free(syspopup_info_t *pinfo)
132 {
133         if (pinfo->name)
134                 free(pinfo->name);
135         if (pinfo->pkgname)
136                 free(pinfo->pkgname);
137         free(pinfo);
138 }
139