2 * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://floralicense.org/license/
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.
17 * @file web_provider_livebox_info.cpp
18 * @author Yunchan Cho (yunchan.cho@samsung.com)
23 #include <sys/types.h>
30 #include <glib-object.h>
31 #include <json-glib/json-glib.h>
32 #include <Core/Util/Log.h>
33 #include "web_provider_plugin_info.h"
37 static web_provider_plugin_info* get_parsed_json_data(std::string& configPath);
38 static bool web_provider_plugin_release_info(web_provider_plugin_info* info);
40 static const std::string installedPluginDirPath("/usr/lib/web-provider/");
42 // Json's content for plugin is the following (example)
45 // "path" : "/usr/lib/web-provider/libweb-provider-plugin-clipbox.so",
46 // "service_boxid" : "org.tizen.browser"
49 // "service_boxid" is only optional member in json file
51 static const std::string jsonMemberType("type");
52 static const std::string jsonMemberPath("path");
53 static const std::string jsonMemberBoxId("service_boxid");
54 static const std::string jsonMemberBoxScrollable("box_scrollable");
56 static const std::string jsonValueBoolTrue("true");
57 static const std::string jsonValueBoolFalse("false");
58 static const std::string jsonFileExtension(".json");
60 web_provider_plugin_info** web_provider_plugin_get_installed_list(int* count)
64 // open directory of web provider plugin
65 DIR* dir = opendir(installedPluginDirPath.c_str());
67 LogD("failed to open directory for web livebox plugins");
72 // read plugin directory and store plugin config path
73 std::list<std::string> configList;
75 struct stat configStat;
76 std::string configPath;
77 while (entry = readdir(dir)) {
78 if (!strcmp(entry->d_name, ".") ||
79 !strcmp(entry->d_name, "..")) {
83 configPath = installedPluginDirPath + entry->d_name;
85 if (stat(configPath.c_str(), &configStat) < 0) {
86 LogD("Failed to open file");
90 if (S_ISDIR(configStat.st_mode)) {
94 LogD("config file: %s", configPath.c_str());
95 configList.push_back(configPath);
97 // close directory of web provider plugin
100 if (configList.size() == 0) {
105 // parse available each plugin json file
106 std::list<web_provider_plugin_info*> pluginList;
107 for (auto it = configList.begin();
108 it != configList.end(); it++) {
109 web_provider_plugin_info* info = get_parsed_json_data(*it) ;
114 pluginList.push_back(info);
116 *count = pluginList.size();
117 LogD("read plugin count: %d", *count);
119 // c style array allocation for return of result
120 web_provider_plugin_info** info_list =
121 static_cast<web_provider_plugin_info**>(
122 malloc((*count) * sizeof(web_provider_plugin_info*)));
124 // copy from members in std::list to one in c style array
126 for (auto it = pluginList.begin();
127 it != pluginList.end(); it++) {
128 LogD("type: %s", (*it)->type);
129 LogD("path: %s", (*it)->path);
130 if ((*it)->service_boxid) {
131 LogD("service_boxid: %s", (*it)->service_boxid);
133 info_list[idx] = *it;
137 LogD("success to return plugin information");
141 void web_provider_plugin_release_installed_list(
142 web_provider_plugin_info** info_list,
149 for (int i = 0; i < count; i++) {
150 web_provider_plugin_release_info(info_list[i]);
154 int web_provider_plugin_get_box_scrollable(const char* plugin_type)
160 std::string configPath;
161 configPath = installedPluginDirPath;
162 configPath += plugin_type;
163 configPath += jsonFileExtension;
164 web_provider_plugin_info* info = get_parsed_json_data(configPath);
169 int ret = info->box_scrollable;
170 web_provider_plugin_release_info(info);
172 LogD("box_scrollable: %d", ret);
176 static web_provider_plugin_info* get_parsed_json_data(std::string& configPath)
180 web_provider_plugin_info* info;
181 JsonParser* parser = json_parser_new();
182 GError* error = NULL;
184 json_parser_load_from_file(parser, configPath.c_str(), &error);
186 LogD("failed to parse json file: %s -> %s", configPath.c_str(), error->message);
188 g_object_unref(parser);
192 JsonNode* root = json_parser_get_root(parser);
193 JsonObject* object = json_node_get_object(root);
195 // check if type member exists on this json file
197 static_cast<const char*>(
198 json_object_get_string_member(object, jsonMemberType.c_str()));
201 static_cast<const char*>(
202 json_object_get_string_member(object, jsonMemberPath.c_str()));
204 if (!type || !path) {
205 LogD("mandatory members don't exist");
207 g_object_unref(parser);
211 // allocate instance of plugin info struct
212 info = static_cast<web_provider_plugin_info*>(
213 malloc(sizeof(web_provider_plugin_info)));
214 memset(info, 0, sizeof(web_provider_plugin_info));
216 info->type = strdup(type);
217 info->path = strdup(path);
219 gboolean hasBoxId = json_object_has_member(object, jsonMemberBoxId.c_str());
220 if (hasBoxId == TRUE) {
222 static_cast<const char*>(
223 json_object_get_string_member(object, jsonMemberBoxId.c_str()));
225 info->service_boxid = strdup(boxId);
229 gboolean hasBoxScrollable =
230 json_object_has_member(object, jsonMemberBoxScrollable.c_str());
231 if (hasBoxScrollable == TRUE) {
232 const char* boxScrollable =
233 static_cast<const char*>(
234 json_object_get_string_member(object, jsonMemberBoxScrollable.c_str()));
235 if (boxScrollable && (jsonValueBoolTrue == boxScrollable)) {
236 info->box_scrollable = 1;
238 info->box_scrollable = 0;
242 LogD("type: %s", info->type);
243 LogD("path: %s", info->path);
244 if (info->service_boxid) {
245 LogD("service_boxid: %s", info->service_boxid);
247 LogD("box_scrollable: %d", info->box_scrollable);
249 json_node_free(root);
251 g_object_unref(parser);
256 bool web_provider_plugin_release_info(web_provider_plugin_info* info)
260 LogD("empty struct");
264 // only members with buffer are released
267 delete info->service_boxid;