9f77c64d31c3b10cb8ea7de1107c788431cf9de9
[apps/native/menu-screen.git] / src / all_apps / db.c
1 /*
2  * MENU-SCREEN
3  *
4  * Copyright (c) 2009-2015 Samsung Electronics Co., Ltd All Rights Reserved
5  *
6  * Contact: Jin Yoon <jinny.yoon@samsung.com>
7  *          Junkyu Han <junkyu.han@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 #include <sys/stat.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <unistd.h>
27
28 #include "db.h"
29 #include "util.h"
30 #include "all_apps/db.h"
31
32 #define QUERY_LEN 1024
33
34 #define MENU_SCREEN_DB_FILE ".menu_screen.db"
35 #define SHORTCUT_TABLE "shortcut"
36 #define QUERY_INSERT_SHORTCUT "INSERT INTO "SHORTCUT_TABLE" ("\
37         "appid,"\
38         "name,"\
39         "type,"\
40         "content_info,"\
41         "icon"\
42         ") VALUES ("\
43         "'%s', '%s', %d, '%s', '%s');"
44 #define QUERY_DELETE_SHORTCUT "DELETE FROM "SHORTCUT_TABLE" WHERE ROWID=%lld"
45 #define QUERY_GET_ALL "SELECT ROWID, appid, name, type, content_info, icon FROM "SHORTCUT_TABLE
46 #define QUERY_COUNT_SHORTCUT "SELECT COUNT(*) FROM "SHORTCUT_TABLE" WHERE appid='%s' AND name='%s'"
47
48
49
50 HAPI menu_screen_error_e all_apps_db_init(void)
51 {
52         return db_open(MENU_SCREEN_DB_FILE);
53 }
54
55
56
57 HAPI void all_apps_db_fini(void)
58 {
59         db_close();
60 }
61
62
63
64 HAPI Eina_List *all_apps_db_retrieve_all_info(void)
65 {
66         stmt_h *st;
67         Eina_List *list = NULL;
68
69         retv_if(MENU_SCREEN_ERROR_OK != db_open(MENU_SCREEN_DB_FILE), NULL);
70
71         st = db_prepare(QUERY_GET_ALL);
72         retv_if(NULL == st, NULL);
73
74         menu_screen_error_e ret = MENU_SCREEN_ERROR_FAIL;
75         for (ret = db_next(st); MENU_SCREEN_ERROR_FAIL != ret && MENU_SCREEN_ERROR_NO_DATA != ret; ret = db_next(st)) {
76                 db_info *info;
77                 info = calloc(1, sizeof(db_info));
78                 break_if(NULL == info);
79
80                 info->rowid = db_get_long_long(st, 0); // 0 : ROWID
81
82                 char *tmp = NULL;
83                 tmp = (char *) db_get_str(st, 1); // 1 : appid
84                 if (tmp && strlen(tmp)) {
85                         info->appid = strdup(tmp);
86                         goto_if(NULL == info->appid, APP_ERROR);
87                 }
88
89                 tmp = (char *) db_get_str(st, 2); // 2 : name
90                 if (tmp && strlen(tmp)) {
91                         info->name = strdup(tmp);
92                         goto_if(NULL == info->name, APP_ERROR);
93                 }
94
95                 info->type = db_get_int(st, 3); // 3 : type
96
97                 tmp = (char *) db_get_str(st, 4); // 4 : content_info
98                 if (tmp && strlen(tmp)) {
99                         info->content_info = strdup(tmp);
100                         goto_if(NULL == info->content_info, APP_ERROR);
101                 }
102
103                 tmp = (char *) db_get_str(st, 5); // 5 : icon
104                 if (tmp && strlen(tmp)) {
105                         info->icon = strdup(tmp);
106                         goto_if(NULL == info->icon, APP_ERROR);
107                 }
108
109                 list = eina_list_append(list, info);
110
111                 continue;
112 APP_ERROR:
113                 if (info->appid) free(info->appid);
114                 if (info->name) free(info->name);
115                 if (info->content_info) free(info->content_info);
116                 if (info->icon) free(info->icon);
117                 if (info) free(info);
118         }
119
120         db_finalize(st);
121
122         return list;
123 }
124
125
126
127 HAPI void all_apps_db_unretrieve_info(db_info *info)
128 {
129         ret_if(NULL == info);
130         if (info->appid) free(info->appid);
131         if (info->name) free(info->name);
132         if (info->content_info) free(info->content_info);
133         if (info->icon) free(info->icon);
134         if (info) free(info);
135 }
136
137
138
139 HAPI void all_apps_db_unretrieve_all_info(Eina_List *list)
140 {
141         db_info *info = NULL;
142
143         EINA_LIST_FREE(list, info) {
144                 if (NULL == info) break;
145                 if (info->appid) free(info->appid);
146                 if (info->name) free(info->name);
147                 if (info->content_info) free(info->content_info);
148                 if (info->icon) free(info->icon);
149                 if (info) free(info);
150         }
151
152         eina_list_free(list);
153 }
154
155
156
157 HAPI int all_apps_db_count_shortcut(const char *appid, const char *name)
158 {
159         int count = -1;
160         char q[QUERY_LEN];
161         menu_screen_error_e ret = MENU_SCREEN_ERROR_FAIL;
162
163         retv_if(MENU_SCREEN_ERROR_OK != db_open(MENU_SCREEN_DB_FILE), -1);
164
165         snprintf(q, sizeof(q), QUERY_COUNT_SHORTCUT, appid, name);
166
167         stmt_h *st;
168         st = db_prepare(q);
169         retv_if(NULL == st, -1);
170
171         ret = db_next(st);
172         if (MENU_SCREEN_ERROR_FAIL == ret) {
173                 db_finalize(st);
174                 return -1;
175         }
176
177         count = db_get_int(st, 0);
178
179         db_finalize(st);
180
181         return count;
182 }
183
184
185
186 HAPI long long all_apps_db_insert_shortcut(const char *appid, const char *name, int type, const char *content_info, const char *icon)
187 {
188         char q[QUERY_LEN];
189
190         retv_if(MENU_SCREEN_ERROR_OK != db_open(MENU_SCREEN_DB_FILE), -1l);
191
192         snprintf(q, sizeof(q), QUERY_INSERT_SHORTCUT, appid, name, type, content_info, icon);
193         retv_if(db_exec(q) < 0, -1l);
194
195         long long id = -1l;
196         id = db_last_insert_rowid();
197
198         return id;
199 }
200
201
202
203 HAPI menu_screen_error_e all_apps_db_delete_shortcut(long long rowid)
204 {
205         char q[QUERY_LEN];
206
207         retv_if(MENU_SCREEN_ERROR_OK != db_open(MENU_SCREEN_DB_FILE), MENU_SCREEN_ERROR_FAIL);
208
209         snprintf(q, sizeof(q), QUERY_DELETE_SHORTCUT, rowid);
210         retv_if(db_exec(q) < 0, MENU_SCREEN_ERROR_FAIL);
211
212         return MENU_SCREEN_ERROR_OK;
213 }
214
215
216
217 // END