25fbace8065ca9421ed9a45df5105419fd91df6a
[platform/framework/web/web-provider.git] / src / API / web_provider_livebox_info.cpp
1 /*
2  * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Flora License, Version 1.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://floralicense.org/license/
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /**
17  * @file    web_provider_livebox_info.cpp 
18  * @author  Yunchan Cho (yunchan.cho@samsung.com)
19  */
20 #include <cstring>
21 #include <memory>
22 #include "WebProviderDB.h"
23 #include "web_provider_livebox_info.h"
24
25 static const std::string infoTable("LiveboxInfo");
26
27 enum InfoTableField {
28     BOX_ID = 0,
29     APP_ID,
30     BOX_TYPE,
31     AUTO_LAUNCH,
32     MOUSE_EVENT,
33 };
34
35 // TODO this default type should be retrieved more automatically
36 static const std::string defaultBoxType("app");
37
38 const char* web_provider_livebox_get_default_type()
39 {
40     return defaultBoxType.c_str();
41 }
42
43 const char* web_provider_livebox_get_box_type(const char* box_id)
44 {
45     if (!box_id) {
46         return NULL;
47     }
48
49     std::shared_ptr<WebProviderDB> handle(new WebProviderDB());
50     if (!handle->openDB()) {
51         return NULL;
52     }
53
54     std::string query = "select * from " + infoTable + " where box_id = ?";
55     if (!handle->setCommand(query, "s", box_id)) {
56         handle->closeDB();
57         return NULL;
58     }
59
60     if (!handle->executeCommand()) {
61         handle->closeDB();
62         return NULL;
63     }
64     
65     const char* box_type = handle->getText(InfoTableField::BOX_TYPE);
66     const char* box_type_dup = NULL;
67
68     if (box_type) {
69         box_type_dup = strdup(box_type);
70     }
71
72     handle->closeDB();
73
74     return box_type_dup;
75 }
76
77 const char* web_provider_livebox_get_app_id(const char* box_id)
78 {
79     if (!box_id) {
80         return NULL;
81     }
82
83     std::shared_ptr<WebProviderDB> handle(new WebProviderDB());
84     if (!handle->openDB()) {
85         return NULL;
86     }
87
88     std::string query = "select * from " + infoTable + " where box_id = ?";
89     if (!handle->setCommand(query, "s", box_id)) {
90         handle->closeDB();
91         return NULL;
92     }
93
94     if (!handle->executeCommand()) {
95         handle->closeDB();
96         return NULL;
97     }
98
99     const char* app_id = handle->getText(InfoTableField::APP_ID);
100     const char* app_id_dup = NULL;
101
102     if (app_id) {
103         app_id_dup = strdup(app_id);
104     }
105
106     handle->closeDB();
107
108     return app_id_dup;
109 }
110
111 int web_provider_livebox_get_auto_launch(const char* box_id)
112 {
113     if (!box_id) {
114         return NULL;
115     }
116
117     std::shared_ptr<WebProviderDB> handle(new WebProviderDB());
118     if (!handle->openDB()) {
119         return NULL;
120     }
121
122     std::string query = "select * from " + infoTable + " where box_id = ?";
123     if (!handle->setCommand(query, "s", box_id)) {
124         handle->closeDB();
125         return NULL;
126     }
127
128     if (!handle->executeCommand()) {
129         handle->closeDB();
130         return NULL;
131     }
132
133     int autoLaunch  = handle->getInt(InfoTableField::AUTO_LAUNCH);
134     handle->closeDB();
135
136     return autoLaunch;
137 }
138
139 int web_provider_livebox_get_mouse_event(const char* box_id)
140 {
141     if (!box_id) {
142         return NULL;
143     }
144
145     std::shared_ptr<WebProviderDB> handle(new WebProviderDB());
146     if (!handle->openDB()) {
147         return NULL;
148     }
149
150     std::string query = "select * from " + infoTable + " where box_id = ?";
151     if (!handle->setCommand(query, "s", box_id)) {
152         handle->closeDB();
153         return NULL;
154     }
155
156     if (!handle->executeCommand()) {
157         handle->closeDB();
158         return NULL;
159     }
160
161     int mouseEvent = handle->getInt(InfoTableField::MOUSE_EVENT);
162     handle->closeDB();
163
164     return mouseEvent;
165 }
166
167 int web_provider_livebox_insert_box_info(
168         const char* box_id, 
169         const char* app_id, 
170         const char* box_type,
171         int auto_launch,
172         int mouse_event)
173 {
174     if (!box_id || !app_id || !box_type) {
175         return -1;
176     }
177
178     std::shared_ptr<WebProviderDB> handle(new WebProviderDB());
179     if (!handle->openDB()) {
180         return -1;
181     }
182
183     std::string query = 
184         "insert into " + infoTable + 
185         " (box_id, app_id, box_type, auto_launch, mouse_event) values (?,?,?,?,?)";
186
187     if (!handle->setCommand(
188                 query, "sssii", 
189                 box_id, app_id, box_type, auto_launch, mouse_event)) {
190         handle->closeDB();
191         return -1;
192     }
193
194     if (!handle->executeCommand()) {
195         handle->closeDB();
196         return -1;
197     }
198
199     handle->closeDB();
200     return 0;
201 }
202
203 int web_provider_livebox_insert_box_type(
204         const char* box_id,
205         const char* app_id,
206         const char* box_type)
207 {
208     return web_provider_livebox_insert_box_info(box_id, app_id, box_type, 0, 0);
209 }
210
211 int web_provider_livebox_delete_by_box_id(const char* box_id)
212 {
213     if (!box_id) {
214         return -1;
215     }
216
217     std::shared_ptr<WebProviderDB> handle(new WebProviderDB());
218     if (!handle->openDB()) {
219         return -1;
220     }
221
222     std::string query = 
223         "delete from " + infoTable + " where box_id=?";
224
225     if (!handle->setCommand(query, "s", box_id)) {
226         handle->closeDB();
227         return -1;
228     }
229
230     if (!handle->executeCommand()) {
231         handle->closeDB();
232         return -1;
233     }
234
235     handle->closeDB();
236     return 0;
237 }
238
239 int web_provider_livebox_delete_by_app_id(const char* app_id)
240 {
241     if (!app_id) {
242         return -1;
243     }
244
245     std::shared_ptr<WebProviderDB> handle(new WebProviderDB());
246     if (!handle->openDB()) {
247         return -1;
248     }
249
250     std::string query = 
251         "delete from " + infoTable + " where app_id=?";
252
253     if (!handle->setCommand(query, "s", app_id)) {
254         handle->closeDB();
255         return -1;
256     }
257
258     if (!handle->executeCommand()) {
259         handle->closeDB();
260         return -1;
261     }
262
263     handle->closeDB();
264     return 0;
265 }
266
267 int web_provider_livebox_delete_by_type(const char* type)
268 {
269     if (!type) {
270         return -1;
271     }
272
273     std::shared_ptr<WebProviderDB> handle(new WebProviderDB());
274     if (!handle->openDB()) {
275         return -1;
276     }
277
278     std::string query = 
279         "delete from " + infoTable + " where type=?";
280
281     if (!handle->setCommand(query, "s", type)) {
282         handle->closeDB();
283         return -1;
284     }
285
286     if (!handle->executeCommand()) {
287         handle->closeDB();
288         return -1;
289     }
290
291     handle->closeDB();
292     return 0;
293 }
294
295 const char* web_provider_info_get_default_type()
296 {
297     return web_provider_livebox_get_default_type();
298 }
299
300 const char* web_provider_info_get_box_type(const char* box_id)
301 {
302     return web_provider_livebox_get_box_type(box_id);
303 }
304
305 int web_provider_info_insert_box_type(
306         const char* box_id,
307         const char* app_id,
308         const char* box_type)
309 {
310     return web_provider_livebox_insert_box_info(box_id, app_id, box_type, 0, 0);
311 }
312
313 int web_provider_info_delete_by_box_id(const char* box_id)
314 {
315     return web_provider_livebox_delete_by_box_id(box_id);
316 }
317
318 int web_provider_info_delete_by_app_id(const char* app_id)
319 {
320     return web_provider_livebox_delete_by_app_id(app_id);
321 }
322
323 int web_provider_info_delete_by_type(const char* type)
324 {
325     return web_provider_livebox_delete_by_type(type);
326 }