[Release] livebox.web-provider-1.9
[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 // TODO this default type should be retrieved more automatically
28 static const std::string defaultBoxType("app");
29
30 const char* web_provider_livebox_get_default_type()
31 {
32     return defaultBoxType.c_str();
33 }
34
35 const char* web_provider_livebox_get_box_type(const char* box_id)
36 {
37     if (!box_id) {
38         return NULL;
39     }
40
41     std::shared_ptr<WebProviderDB> handle(new WebProviderDB());
42     if (!handle->openDB()) {
43         return NULL;
44     }
45
46     std::string query = "select * from " + infoTable + " where box_id = ?";
47     if (!handle->setCommand(query, "s", box_id)) {
48         handle->closeDB();
49         return NULL;
50     }
51
52     if (!handle->executeCommand()) {
53         handle->closeDB();
54         return NULL;
55     }
56     
57     const char* box_type = handle->getText(2);
58     const char* box_type_dup = NULL;
59
60     if (box_type) {
61         box_type_dup = strdup(box_type);
62     }
63
64     handle->closeDB();
65
66     return box_type_dup;
67 }
68
69 const char* web_provider_livebox_get_app_id(const char* box_id)
70 {
71     if (!box_id) {
72         return NULL;
73     }
74
75     std::shared_ptr<WebProviderDB> handle(new WebProviderDB());
76     if (!handle->openDB()) {
77         return NULL;
78     }
79
80     std::string query = "select * from " + infoTable + " where box_id = ?";
81     if (!handle->setCommand(query, "s", box_id)) {
82         handle->closeDB();
83         return NULL;
84     }
85
86     if (!handle->executeCommand()) {
87         handle->closeDB();
88         return NULL;
89     }
90
91     const char* app_id = handle->getText(1);
92     const char* app_id_dup = NULL;
93
94     if (app_id) {
95         app_id_dup = strdup(app_id);
96     }
97
98     handle->closeDB();
99
100     return app_id_dup;
101 }
102
103 int web_provider_livebox_get_auto_launch(const char* box_id)
104 {
105     if (!box_id) {
106         return NULL;
107     }
108
109     std::shared_ptr<WebProviderDB> handle(new WebProviderDB());
110     if (!handle->openDB()) {
111         return NULL;
112     }
113
114     std::string query = "select * from " + infoTable + " where box_id = ?";
115     if (!handle->setCommand(query, "s", box_id)) {
116         handle->closeDB();
117         return NULL;
118     }
119
120     if (!handle->executeCommand()) {
121         handle->closeDB();
122         return NULL;
123     }
124
125     int autoLaunch  = handle->getInt(3);
126     handle->closeDB();
127
128     return autoLaunch;
129 }
130
131 int web_provider_livebox_insert_box_info(
132         const char* box_id, 
133         const char* app_id, 
134         const char* box_type,
135         int auto_launch)
136 {
137     if (!box_id || !app_id || !box_type) {
138         return -1;
139     }
140
141     std::shared_ptr<WebProviderDB> handle(new WebProviderDB());
142     if (!handle->openDB()) {
143         return -1;
144     }
145
146     std::string query = 
147         "insert into " + infoTable + 
148         " (box_id, app_id, box_type, auto_launch) values (?,?,?,?)";
149
150     if (!handle->setCommand(query, "sssi", box_id, app_id, box_type, auto_launch)) {
151         handle->closeDB();
152         return -1;
153     }
154
155     if (!handle->executeCommand()) {
156         handle->closeDB();
157         return -1;
158     }
159
160     handle->closeDB();
161     return 0;
162 }
163
164 int web_provider_livebox_insert_box_type(
165         const char* box_id,
166         const char* app_id,
167         const char* box_type)
168 {
169     return web_provider_livebox_insert_box_info(box_id, app_id, box_type, 0);
170 }
171
172 int web_provider_livebox_delete_by_box_id(const char* box_id)
173 {
174     if (!box_id) {
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         "delete from " + infoTable + " where box_id=?";
185
186     if (!handle->setCommand(query, "s", box_id)) {
187         handle->closeDB();
188         return -1;
189     }
190
191     if (!handle->executeCommand()) {
192         handle->closeDB();
193         return -1;
194     }
195
196     handle->closeDB();
197     return 0;
198 }
199
200 int web_provider_livebox_delete_by_app_id(const char* app_id)
201 {
202     if (!app_id) {
203         return -1;
204     }
205
206     std::shared_ptr<WebProviderDB> handle(new WebProviderDB());
207     if (!handle->openDB()) {
208         return -1;
209     }
210
211     std::string query = 
212         "delete from " + infoTable + " where app_id=?";
213
214     if (!handle->setCommand(query, "s", app_id)) {
215         handle->closeDB();
216         return -1;
217     }
218
219     if (!handle->executeCommand()) {
220         handle->closeDB();
221         return -1;
222     }
223
224     handle->closeDB();
225     return 0;
226 }
227
228 int web_provider_livebox_delete_by_type(const char* type)
229 {
230     if (!type) {
231         return -1;
232     }
233
234     std::shared_ptr<WebProviderDB> handle(new WebProviderDB());
235     if (!handle->openDB()) {
236         return -1;
237     }
238
239     std::string query = 
240         "delete from " + infoTable + " where type=?";
241
242     if (!handle->setCommand(query, "s", type)) {
243         handle->closeDB();
244         return -1;
245     }
246
247     if (!handle->executeCommand()) {
248         handle->closeDB();
249         return -1;
250     }
251
252     handle->closeDB();
253     return 0;
254 }
255
256 const char* web_provider_info_get_default_type()
257 {
258     return web_provider_livebox_get_default_type();
259 }
260
261 const char* web_provider_info_get_box_type(const char* box_id)
262 {
263     return web_provider_livebox_get_box_type(box_id);
264 }
265
266 int web_provider_info_insert_box_type(
267         const char* box_id,
268         const char* app_id,
269         const char* box_type)
270 {
271     return web_provider_livebox_insert_box_info(box_id, app_id, box_type, 0);
272 }
273
274 int web_provider_info_delete_by_box_id(const char* box_id)
275 {
276     return web_provider_livebox_delete_by_box_id(box_id);
277 }
278
279 int web_provider_info_delete_by_app_id(const char* app_id)
280 {
281     return web_provider_livebox_delete_by_app_id(app_id);
282 }
283
284 int web_provider_info_delete_by_type(const char* type)
285 {
286     return web_provider_livebox_delete_by_type(type);
287 }