2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.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://www.apache.org/licenses/LICENSE-2.0
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
22 #include "app-proxy.h"
23 #include "app-info-internal.h"
26 #include "rmi/app-proxy.h"
28 using namespace Krate;
30 struct krate_app_proxy_s {
31 Krate::AppProxy proxy;
32 std::string krateName;
35 static inline krate_app_proxy_s* getInstance(krate_app_proxy_h handle)
37 return reinterpret_cast<krate_app_proxy_s *>(handle);
40 static app_info_h make_app_info_handle(const Krate::AppProxy::AppInfo& info)
42 if (info.id.empty()) {
46 application_x* app = (application_x*)::calloc(1, sizeof(application_x));
51 pkgmgr_appinfo_x* pkgappinfo = (pkgmgr_appinfo_x*)::calloc(1, sizeof(pkgmgr_appinfo_x));
52 if (pkgappinfo == NULL) {
57 app_info_s* appinfo = (app_info_s*)::calloc(1, sizeof(struct app_info_s));
58 if (appinfo == NULL) {
64 appinfo->app_id = ::strdup(info.id.c_str());
65 appinfo->pkg_app_info = pkgappinfo;
67 pkgappinfo->package = ::strdup(info.package.c_str());
68 pkgappinfo->locale = ::strdup(info.locale.c_str());
69 pkgappinfo->app_component = info.componentType;
70 pkgappinfo->app_info = app;
72 app->appid = ::strdup(info.id.c_str());
73 app->nodisplay = ::strdup(info.isNoDisplayed? "true":"false");
74 app->taskmanage = ::strdup(info.isTaskManaged? "true":"false");
75 app->type = ::strdup(info.type.c_str());
77 icon_x* icon = (icon_x*)::calloc(1, sizeof(icon_x));
79 icon->text = ::strdup(info.icon.c_str());
80 icon->lang = ::strdup(info.locale.c_str());
81 app->icon = ::g_list_append(NULL, icon);
84 label_x* label = (label_x*)::calloc(1, sizeof(label_x));
86 label->text = ::strdup(info.label.c_str());
87 label->lang = ::strdup(info.locale.c_str());
88 app->label = ::g_list_append(NULL, label);
91 return reinterpret_cast<app_info_h>(appinfo);
94 int krate_app_proxy_create(krate_manager_h manager, const char* name, krate_app_proxy_h *handle)
96 RET_ON_FAILURE(manager, KRATE_ERROR_INVALID_PARAMETER);
97 RET_ON_FAILURE(name, KRATE_ERROR_INVALID_PARAMETER);
98 RET_ON_FAILURE(handle, KRATE_ERROR_INVALID_PARAMETER);
100 krate_app_proxy_s* instance = new krate_app_proxy_s {
101 GetKrateContext(manager).createKrateInterface<Krate::AppProxy>(),
105 *handle = reinterpret_cast<krate_app_proxy_h>(instance);
106 return KRATE_ERROR_NONE;
109 int krate_app_proxy_destroy(krate_app_proxy_h handle)
111 RET_ON_FAILURE(handle, KRATE_ERROR_INVALID_PARAMETER);
113 delete reinterpret_cast<Krate::AppProxy*>(handle);
115 return KRATE_ERROR_NONE;
118 int krate_app_proxy_get_app_info(krate_app_proxy_h handle, const char* app_id, app_info_h* app_info)
120 RET_ON_FAILURE(handle, KRATE_ERROR_INVALID_PARAMETER);
121 RET_ON_FAILURE(app_id, KRATE_ERROR_INVALID_PARAMETER);
122 RET_ON_FAILURE(app_info, KRATE_ERROR_INVALID_PARAMETER);
124 auto instance = getInstance(handle);
125 auto& proxy = instance->proxy;
126 const std::string& name = instance->krateName;
128 const auto info = proxy.getAppInfo(name, app_id);
129 app_info_h ret = make_app_info_handle(info);
131 return KRATE_ERROR_INVALID_PARAMETER;
136 return KRATE_ERROR_NONE;
139 int krate_app_proxy_foreach_app_info(krate_app_proxy_h handle, app_manager_app_info_cb callback, void *user_data)
141 RET_ON_FAILURE(handle, KRATE_ERROR_INVALID_PARAMETER);
142 RET_ON_FAILURE(callback, KRATE_ERROR_INVALID_PARAMETER);
144 auto instance = getInstance(handle);
145 auto& proxy = instance->proxy;
146 const std::string& name = instance->krateName;
148 int iter = proxy.createIterator(name);
150 app_info_h info = make_app_info_handle(proxy.getIteratorValue(iter));
151 int ret = callback(info, user_data);
152 app_info_destroy(info);
156 } while (proxy.nextIterator(iter));
157 proxy.destroyIterator(iter);
159 return KRATE_ERROR_NONE;
162 static bool appExtraCallback(app_control_h app_control, const char *key, void *user_data)
164 Krate::AppProxy::Bundle *bundle = reinterpret_cast<Krate::AppProxy::Bundle*>(user_data);
165 Krate::AppProxy::Bundle::Extra extra;
170 app_control_is_extra_data_array(app_control, key, &isArray);
174 app_control_get_extra_data_array(app_control, key, &values, &length);
175 for (int i = 0; i < length; i++) {
176 extra.value.push_back(values[i]);
182 app_control_get_extra_data(app_control, key, &value);
183 extra.value.push_back(value);
186 bundle->extraData.push_back(extra);
191 int krate_app_proxy_send_launch_request(krate_app_proxy_h handle, app_control_h app_control)
193 RET_ON_FAILURE(handle, KRATE_ERROR_INVALID_PARAMETER);
194 RET_ON_FAILURE(app_control, KRATE_ERROR_INVALID_PARAMETER);
196 auto instance = getInstance(handle);
197 auto& proxy = instance->proxy;
198 const std::string& name = instance->krateName;
201 Krate::AppProxy::Bundle bundle;
202 app_control_get_operation(app_control, &str);
204 bundle.operation = str;
208 app_control_get_uri(app_control, &str);
214 app_control_get_mime(app_control, &str);
220 app_control_get_category(app_control, &str);
222 bundle.category = str;
226 app_control_get_app_id(app_control, &str);
232 app_control_foreach_extra_data(app_control, appExtraCallback, &bundle);
234 return proxy.launch(name, bundle);
237 int krate_app_proxy_terminate(krate_app_proxy_h handle, const char* app_id)
239 RET_ON_FAILURE(handle, KRATE_ERROR_INVALID_PARAMETER);
240 RET_ON_FAILURE(app_id, KRATE_ERROR_INVALID_PARAMETER);
242 auto instance = getInstance(handle);
243 auto& proxy = instance->proxy;
244 const std::string& name = instance->krateName;
246 return proxy.terminate(name, app_id);
249 int krate_app_proxy_resume(krate_app_proxy_h handle, const char* app_id)
251 RET_ON_FAILURE(handle, KRATE_ERROR_INVALID_PARAMETER);
252 RET_ON_FAILURE(app_id, KRATE_ERROR_INVALID_PARAMETER);
254 auto instance = getInstance(handle);
255 auto& proxy = instance->proxy;
256 const std::string& name = instance->krateName;
258 return proxy.resume(name, app_id);
261 int krate_app_proxy_is_running(krate_app_proxy_h handle, const char* app_id, int *result)
263 RET_ON_FAILURE(handle, KRATE_ERROR_INVALID_PARAMETER);
264 RET_ON_FAILURE(app_id, KRATE_ERROR_INVALID_PARAMETER);
265 RET_ON_FAILURE(result, KRATE_ERROR_INVALID_PARAMETER);
267 auto instance = getInstance(handle);
268 auto& proxy = instance->proxy;
269 const std::string& name = instance->krateName;
271 *result = proxy.isRunning(name, app_id);
272 return KRATE_ERROR_NONE;