2 * Copyright (c) 2011 - 2016 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.
31 #include "shortcut_private.h"
32 #include "shortcut_db.h"
33 #include "shortcut_manager.h"
34 #include "shortcut_internal.h"
36 #define SHORTCUT_IS_WIDGET_SIZE(size) (!!((size) & WIDGET_SIZE_DEFAULT))
37 #define REQUEST_ID_LEN 40
39 EAPI int shortcut_set_request_cb(shortcut_request_cb request_cb, void *data)
43 if (request_cb == NULL)
44 return SHORTCUT_ERROR_INVALID_PARAMETER; /* LCOV_EXCL_LINE */
47 if (ret != SHORTCUT_ERROR_NONE) {
49 SHORTCUT_ERR("Can't init dbus %d", ret);
54 ret = _dbus_signal_init();
55 if (ret != SHORTCUT_ERROR_NONE) {
57 SHORTCUT_ERR("Can't init dbus_signal %d", ret);
62 ret = _send_service_register();
63 if (ret != SHORTCUT_ERROR_NONE) {
65 SHORTCUT_ERR("Can't init ipc_monitor_register %d", ret);
70 ret = _dbus_set_watch_name();
71 if (ret != SHORTCUT_ERROR_NONE) {
73 SHORTCUT_ERR("Can't init _dbus_set_watch_name %d", ret);
78 _set_request_cb(request_cb, data);
80 return SHORTCUT_ERROR_NONE;
83 EAPI void shortcut_unset_request_cb(void)
88 EAPI int shortcut_set_remove_cb(shortcut_remove_cb remove_cb, void *data)
92 if (remove_cb == NULL)
93 return SHORTCUT_ERROR_INVALID_PARAMETER;
96 if (ret != SHORTCUT_ERROR_NONE) {
98 SHORTCUT_ERR("Can't init dbus %d", ret);
103 ret = _dbus_signal_init();
104 if (ret != SHORTCUT_ERROR_NONE) {
105 /* LCOV_EXCL_START */
106 SHORTCUT_ERR("Can't init dbus_signal %d", ret);
111 ret = _send_service_register();
112 if (ret != SHORTCUT_ERROR_NONE) {
113 /* LCOV_EXCL_START */
114 SHORTCUT_ERR("Can't init ipc_monitor_register %d", ret);
119 ret = _dbus_set_watch_name();
120 if (ret != SHORTCUT_ERROR_NONE) {
121 /* LCOV_EXCL_START */
122 SHORTCUT_ERR("Can't init _dbus_set_watch_name %d", ret);
127 _set_remove_cb(remove_cb, data);
129 return SHORTCUT_ERROR_NONE;
132 EAPI void shortcut_unset_remove_cb(void)
137 static char *_make_request_id()
140 char request_id[REQUEST_ID_LEN] = { 0, };
142 g_atomic_int_inc(&id);
143 snprintf(request_id, sizeof(request_id), "%d@%d", getpid(), id);
145 SHORTCUT_DBG("The request_id of shortcut is [%s]", request_id);
147 return strdup(request_id);
150 EAPI int shortcut_add_to_home(const char *name, shortcut_type type, const char *uri,
151 const char *icon, int allow_duplicate, result_cb_t result_cb, void *data)
153 struct result_cb_item *item;
157 char *request_id = NULL;
159 if (ADD_TO_HOME_IS_DYNAMICBOX(type)) {
160 /* LCOV_EXCL_START */
161 SHORTCUT_ERR("Invalid type used for adding a shortcut\n");
162 return SHORTCUT_ERROR_INVALID_PARAMETER;
167 if (ret != SHORTCUT_ERROR_NONE) {
168 /* LCOV_EXCL_START */
169 SHORTCUT_ERR("Can't init dbus %d", ret);
174 ret = _check_privilege();
175 if (ret != SHORTCUT_ERROR_NONE)
178 appid = _shortcut_get_pkgname_by_pid();
180 /* LCOV_EXCL_START */
181 SHORTCUT_ERR("Can't get appid");
182 return SHORTCUT_ERROR_IO_ERROR;
186 item = malloc(sizeof(struct result_cb_item));
188 /* LCOV_EXCL_START */
192 SHORTCUT_ERR("Heap: %d\n", errno);
193 return SHORTCUT_ERROR_OUT_OF_MEMORY;
197 item->result_cb = result_cb;
198 item->result_internal_cb = NULL;
210 request_id = _make_request_id();
211 body = g_variant_new("(sississi)", request_id, getpid(), appid, name, type, uri, icon, allow_duplicate);
213 ret = _send_async_shortcut(body, item, "add_shortcut");
214 if (ret != SHORTCUT_ERROR_NONE) {
215 /* LCOV_EXCL_START */
224 g_variant_unref(body);
231 EAPI int shortcut_add_to_home_widget(const char *name, shortcut_widget_size_e size, const char *widget_id,
232 const char *icon, double period, int allow_duplicate, result_cb_t result_cb, void *data)
234 struct result_cb_item *item;
238 char *request_id = NULL;
241 SHORTCUT_ERR("AppID is null\n");
242 return SHORTCUT_ERROR_INVALID_PARAMETER;
245 if (!SHORTCUT_IS_WIDGET_SIZE(size)) {
246 /* LCOV_EXCL_START */
247 SHORTCUT_ERR("Invalid type used for adding a widget\n");
248 return SHORTCUT_ERROR_INVALID_PARAMETER;
253 if (ret != SHORTCUT_ERROR_NONE) {
254 /* LCOV_EXCL_START */
255 SHORTCUT_ERR("Can't init dbus %d", ret);
260 ret = _check_privilege();
261 if (ret != SHORTCUT_ERROR_NONE)
264 appid = _shortcut_get_pkgname_by_pid();
266 /* LCOV_EXCL_START */
267 SHORTCUT_ERR("Can't get appid");
268 return SHORTCUT_ERROR_IO_ERROR;
272 item = malloc(sizeof(struct result_cb_item));
274 /* LCOV_EXCL_START */
278 SHORTCUT_ERR("Heap: %d\n", errno);
279 return SHORTCUT_ERROR_OUT_OF_MEMORY;
283 item->result_cb = result_cb;
284 item->result_internal_cb = NULL;
287 request_id = _make_request_id();
288 body = g_variant_new("(sississdi)", request_id, getpid(), widget_id, name, size, NULL, icon, period, allow_duplicate);
289 ret = _send_async_shortcut(body, item, "add_shortcut_widget");
291 if (ret != SHORTCUT_ERROR_NONE) {
292 /* LCOV_EXCL_START */
301 g_variant_unref(body);
308 EAPI int shortcut_remove_from_home(const char *name, result_cb_t result_cb, void *user_data)
310 struct result_cb_item *item;
314 char *request_id = NULL;
317 SHORTCUT_ERR("name is NULL.");
318 return SHORTCUT_ERROR_INVALID_PARAMETER;
322 if (ret != SHORTCUT_ERROR_NONE) {
323 /* LCOV_EXCL_START */
324 SHORTCUT_ERR("Can't init dbus %d", ret);
329 ret = _check_privilege();
330 if (ret != SHORTCUT_ERROR_NONE)
333 appid = _shortcut_get_pkgname_by_pid();
335 /* LCOV_EXCL_START */
336 SHORTCUT_ERR("Can't get appid");
337 return SHORTCUT_ERROR_IO_ERROR;
341 item = malloc(sizeof(struct result_cb_item));
343 /* LCOV_EXCL_START */
347 SHORTCUT_ERR("Heap: %d\n", errno);
348 return SHORTCUT_ERROR_OUT_OF_MEMORY;
352 item->result_cb = result_cb;
353 item->result_internal_cb = NULL;
354 item->data = user_data;
356 request_id = _make_request_id();
357 body = g_variant_new("(siss)", request_id, getpid(), appid, name);
359 ret = _send_async_shortcut(body, item, "remove_shortcut");
360 if (ret != SHORTCUT_ERROR_NONE) {
361 /* LCOV_EXCL_START */
370 g_variant_unref(body);
377 EAPI int shortcut_get_list(const char *package_name, shortcut_list_cb list_cb, void *data)
379 GDBusMessage *reply = NULL;
383 GVariant *reply_body;
387 shortcut_info_s shortcut;
390 return SHORTCUT_ERROR_INVALID_PARAMETER;
392 result = _dbus_init();
393 if (result != SHORTCUT_ERROR_NONE) {
394 /* LCOV_EXCL_START */
395 SHORTCUT_ERR("Can't init dbus %d", result);
400 b = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
402 g_variant_builder_add(b, "{sv}", "package_name", g_variant_new_string(package_name));
403 body = g_variant_builder_end(b);
404 result = _send_sync_shortcut(g_variant_new("(v)", body), &reply, "get_list");
406 if (result == SHORTCUT_ERROR_NONE) {
407 reply_body = g_dbus_message_get_body(reply);
408 g_variant_get(reply_body, "(ia(v))", &ret, &iter);
409 SHORTCUT_DBG("shortcut count : %d", ret);
410 while (g_variant_iter_loop(iter, "(v)", &iter_body)) {
411 g_variant_get(iter_body, "(&s&s&s&s&s)",
412 &shortcut.package_name, &shortcut.icon, &shortcut.name, &shortcut.extra_key, &shortcut.extra_data);
413 SHORTCUT_DBG("call calback : %s", shortcut.package_name);
414 list_cb(shortcut.package_name, shortcut.icon, shortcut.name, shortcut.extra_key, shortcut.extra_data, data);
416 g_variant_iter_free(iter);
418 ret = result; /* LCOV_EXCL_LINE */
422 g_object_unref(reply);
427 /* LCOV_EXCL_START */
428 EAPI int add_to_home_shortcut(const char *appid, const char *name, int type, const char *content,
429 const char *icon, int allow_duplicate, result_internal_cb_t result_cb, void *data)
432 return SHORTCUT_ERROR_NONE;
436 /* LCOV_EXCL_START */
437 EAPI int add_to_home_dynamicbox(const char *appid, const char *name, int type, const char *content, const char *icon, double period, int allow_duplicate, result_internal_cb_t result_cb, void *data)
440 return SHORTCUT_ERROR_NONE;