Resolve SVACE issues with WGID - 427970,427940,427933,427844
[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                 free(info->appid);
111                 free(info->name);
112                 free(info->content_info);
113                 free(info->icon);
114                 free(info);
115                 continue;
116 APP_ERROR:
117                 if (info->appid) free(info->appid);
118                 if (info->name) free(info->name);
119                 if (info->content_info) free(info->content_info);
120                 if (info->icon) free(info->icon);
121                 if (info) free(info);
122         }
123
124         db_finalize(st);
125
126         return list;
127 }
128
129
130
131 HAPI void all_apps_db_unretrieve_info(db_info *info)
132 {
133         ret_if(NULL == info);
134         if (info->appid) free(info->appid);
135         if (info->name) free(info->name);
136         if (info->content_info) free(info->content_info);
137         if (info->icon) free(info->icon);
138         if (info) free(info);
139 }
140
141
142
143 HAPI void all_apps_db_unretrieve_all_info(Eina_List *list)
144 {
145         db_info *info = NULL;
146
147         EINA_LIST_FREE(list, info) {
148                 if (NULL == info) break;
149                 if (info->appid) free(info->appid);
150                 if (info->name) free(info->name);
151                 if (info->content_info) free(info->content_info);
152                 if (info->icon) free(info->icon);
153                 if (info) free(info);
154         }
155
156         eina_list_free(list);
157 }
158
159
160
161 HAPI int all_apps_db_count_shortcut(const char *appid, const char *name)
162 {
163         int count = -1;
164         char q[QUERY_LEN];
165         menu_screen_error_e ret = MENU_SCREEN_ERROR_FAIL;
166
167         retv_if(MENU_SCREEN_ERROR_OK != db_open(MENU_SCREEN_DB_FILE), -1);
168
169         snprintf(q, sizeof(q), QUERY_COUNT_SHORTCUT, appid, name);
170
171         stmt_h *st;
172         st = db_prepare(q);
173         retv_if(NULL == st, -1);
174
175         ret = db_next(st);
176         if (MENU_SCREEN_ERROR_FAIL == ret) {
177                 db_finalize(st);
178                 return -1;
179         }
180
181         count = db_get_int(st, 0);
182
183         db_finalize(st);
184
185         return count;
186 }
187
188
189
190 HAPI long long all_apps_db_insert_shortcut(const char *appid, const char *name, int type, const char *content_info, const char *icon)
191 {
192         char q[QUERY_LEN];
193
194         retv_if(MENU_SCREEN_ERROR_OK != db_open(MENU_SCREEN_DB_FILE), -1l);
195
196         snprintf(q, sizeof(q), QUERY_INSERT_SHORTCUT, appid, name, type, content_info, icon);
197         retv_if(db_exec(q) < 0, -1l);
198
199         long long id = -1l;
200         id = db_last_insert_rowid();
201
202         return id;
203 }
204
205
206
207 HAPI menu_screen_error_e all_apps_db_delete_shortcut(long long rowid)
208 {
209         char q[QUERY_LEN];
210
211         retv_if(MENU_SCREEN_ERROR_OK != db_open(MENU_SCREEN_DB_FILE), MENU_SCREEN_ERROR_FAIL);
212
213         snprintf(q, sizeof(q), QUERY_DELETE_SHORTCUT, rowid);
214         retv_if(db_exec(q) < 0, MENU_SCREEN_ERROR_FAIL);
215
216         return MENU_SCREEN_ERROR_OK;
217 }
218
219
220
221 // END