2 * Copyright (c) 2011 - 2017 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 static int __shortcut_init_ipc_process(void)
41 if (ret != SHORTCUT_ERROR_NONE) {
43 SHORTCUT_ERR("Can't init dbus %d", ret);
48 ret = _dbus_signal_init();
49 if (ret != SHORTCUT_ERROR_NONE) {
51 SHORTCUT_ERR("Can't init dbus_signal %d", ret);
56 ret = _send_service_register();
57 if (ret != SHORTCUT_ERROR_NONE) {
59 SHORTCUT_ERR("Can't init ipc_monitor_register %d", ret);
64 ret = _dbus_set_watch_name();
65 if (ret != SHORTCUT_ERROR_NONE) {
67 SHORTCUT_ERR("Can't init _dbus_set_watch_name %d", ret);
72 return SHORTCUT_ERROR_NONE;
75 EAPI int shortcut_set_request_cb(shortcut_request_cb request_cb, void *data)
79 CHECK_SHORTCUT_FEATURE();
80 if (request_cb == NULL)
81 return SHORTCUT_ERROR_INVALID_PARAMETER; /* LCOV_EXCL_LINE */
83 ret = __shortcut_init_ipc_process();
84 if (ret != SHORTCUT_ERROR_NONE)
87 _set_request_cb(request_cb, data);
89 return SHORTCUT_ERROR_NONE;
92 EAPI void shortcut_unset_request_cb(void)
96 CHECK_SHORTCUT_FEATURE_RET_LAST_RESULT();
98 ret = _check_privilege();
99 if (ret == SHORTCUT_ERROR_PERMISSION_DENIED) {
100 set_last_result(SHORTCUT_ERROR_PERMISSION_DENIED);
105 set_last_result(SHORTCUT_ERROR_NONE);
108 EAPI int shortcut_set_remove_cb(shortcut_remove_cb remove_cb, void *data)
112 CHECK_SHORTCUT_FEATURE();
113 if (remove_cb == NULL)
114 return SHORTCUT_ERROR_INVALID_PARAMETER;
116 ret = __shortcut_init_ipc_process();
117 if (ret != SHORTCUT_ERROR_NONE)
120 _set_remove_cb(remove_cb, data);
122 return SHORTCUT_ERROR_NONE;
125 EAPI void shortcut_unset_remove_cb(void)
129 CHECK_SHORTCUT_FEATURE_RET_LAST_RESULT();
131 ret = _check_privilege();
132 if (ret == SHORTCUT_ERROR_PERMISSION_DENIED) {
133 set_last_result(SHORTCUT_ERROR_PERMISSION_DENIED);
138 set_last_result(SHORTCUT_ERROR_NONE);
141 EAPI int shortcut_add_to_home(const char *name, shortcut_type type,
142 const char *uri, const char *icon, int allow_duplicate,
143 result_cb cb, void *data)
145 struct result_cb_item *item = NULL;
148 GVariant *body = NULL;
149 char *request_id = NULL;
151 CHECK_SHORTCUT_FEATURE();
153 if (ADD_TO_HOME_IS_DYNAMICBOX(type)) {
154 /* LCOV_EXCL_START */
155 SHORTCUT_ERR("Invalid type used for adding a shortcut\n");
156 return SHORTCUT_ERROR_INVALID_PARAMETER;
160 ret = _ready_to_send(&appid, &request_id);
161 if (ret != SHORTCUT_ERROR_NONE) {
162 /* LCOV_EXCL_START */
163 SHORTCUT_ERR("ready to send error [%d]", ret);
168 item = malloc(sizeof(struct result_cb_item));
170 /* LCOV_EXCL_START */
171 SHORTCUT_ERR("Heap: %d\n", errno);
172 ret = SHORTCUT_ERROR_OUT_OF_MEMORY;
177 item->result_cb = cb;
178 item->result_internal_cb = NULL;
190 body = g_variant_new("(sississi)", request_id, getpid(), appid, name,
191 type, uri, icon, allow_duplicate);
193 ret = _send_async_shortcut(body, item, "add_shortcut");
199 g_variant_unref(body);
202 if (ret != SHORTCUT_ERROR_NONE && item)
208 EAPI int shortcut_add_to_home_widget(const char *name, shortcut_widget_size_e size,
209 const char *widget_id, const char *icon, double period,
210 int allow_duplicate, result_cb cb, void *data)
212 struct result_cb_item *item = NULL;
215 GVariant *body = NULL;
216 char *request_id = NULL;
218 CHECK_SHORTCUT_FEATURE();
221 SHORTCUT_ERR("AppID is null\n");
222 return SHORTCUT_ERROR_INVALID_PARAMETER;
225 if (!SHORTCUT_IS_WIDGET_SIZE(size)) {
226 /* LCOV_EXCL_START */
227 SHORTCUT_ERR("Invalid type used for adding a widget\n");
228 return SHORTCUT_ERROR_INVALID_PARAMETER;
232 ret = _ready_to_send(&appid, &request_id);
233 if (ret != SHORTCUT_ERROR_NONE) {
234 /* LCOV_EXCL_START */
235 SHORTCUT_ERR("ready to send error [%d]", ret);
240 item = malloc(sizeof(struct result_cb_item));
242 /* LCOV_EXCL_START */
243 SHORTCUT_ERR("Heap: %d\n", errno);
244 ret = SHORTCUT_ERROR_OUT_OF_MEMORY;
249 item->result_cb = cb;
250 item->result_internal_cb = NULL;
253 body = g_variant_new("(sississdi)", request_id, getpid(), widget_id,
254 name, size, "", icon, period, allow_duplicate);
256 ret = _send_async_shortcut(body, item, "add_shortcut_widget");
262 g_variant_unref(body);
265 if (ret != SHORTCUT_ERROR_NONE && item)
271 EAPI int shortcut_remove_from_home(const char *name, result_cb cb, void *user_data)
273 struct result_cb_item *item = NULL;
276 GVariant *body = NULL;
277 char *request_id = NULL;
279 CHECK_SHORTCUT_FEATURE();
282 SHORTCUT_ERR("name is NULL.");
283 return SHORTCUT_ERROR_INVALID_PARAMETER;
286 ret = _ready_to_send(&appid, &request_id);
287 if (ret != SHORTCUT_ERROR_NONE) {
288 /* LCOV_EXCL_START */
289 SHORTCUT_ERR("ready to send error [%d]", ret);
294 item = malloc(sizeof(struct result_cb_item));
296 /* LCOV_EXCL_START */
297 SHORTCUT_ERR("Heap: %d\n", errno);
298 ret = SHORTCUT_ERROR_OUT_OF_MEMORY;
303 item->result_cb = cb;
304 item->result_internal_cb = NULL;
305 item->data = user_data;
307 body = g_variant_new("(siss)", request_id, getpid(), appid, name);
309 ret = _send_async_shortcut(body, item, "remove_shortcut");
315 g_variant_unref(body);
318 if (ret != SHORTCUT_ERROR_NONE && item)
323 EAPI int shortcut_get_list(const char *package_name, shortcut_list_cb list_cb, void *data)
325 GDBusMessage *reply = NULL;
329 GVariant *reply_body;
333 shortcut_info_s shortcut;
335 CHECK_SHORTCUT_FEATURE();
338 return SHORTCUT_ERROR_INVALID_PARAMETER;
340 result = _dbus_init();
341 if (result != SHORTCUT_ERROR_NONE) {
342 /* LCOV_EXCL_START */
343 SHORTCUT_ERR("Can't init dbus %d", result);
348 b = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
350 g_variant_builder_add(b, "{sv}", "package_name", g_variant_new_string(package_name));
351 body = g_variant_builder_end(b);
352 result = _send_sync_shortcut(g_variant_new("(v)", body), &reply, "get_list");
354 if (result == SHORTCUT_ERROR_NONE) {
355 reply_body = g_dbus_message_get_body(reply);
356 g_variant_get(reply_body, "(ia(v))", &ret, &iter);
357 SHORTCUT_DBG("shortcut count : %d", ret);
358 while (g_variant_iter_loop(iter, "(v)", &iter_body)) {
359 g_variant_get(iter_body, "(&s&s&s&s&s)",
360 &shortcut.package_name, &shortcut.icon, &shortcut.name, &shortcut.extra_key, &shortcut.extra_data);
361 SHORTCUT_DBG("call calback : %s", shortcut.package_name);
362 list_cb(shortcut.package_name, shortcut.icon, shortcut.name, shortcut.extra_key, shortcut.extra_data, data);
364 g_variant_iter_free(iter);
366 ret = result; /* LCOV_EXCL_LINE */
370 g_object_unref(reply);
375 /* LCOV_EXCL_START */
376 EAPI int add_to_home_shortcut(const char *appid, const char *name, int type, const char *content,
377 const char *icon, int allow_duplicate, result_internal_cb result_cb, void *data)
379 CHECK_SHORTCUT_FEATURE();
381 return SHORTCUT_ERROR_NONE;
385 /* LCOV_EXCL_START */
386 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 result_cb, void *data)
388 CHECK_SHORTCUT_FEATURE();
390 return SHORTCUT_ERROR_NONE;