2 * Copyright (c) 2011 - 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.
30 #include "shortcut_private.h"
31 #include "shortcut_db.h"
32 #include "shortcut_manager.h"
34 #define SHORTCUT_PKGNAME_LEN 512
35 #define SHORTCUT_IS_WIDGET_SIZE(size) (!!((size) & WIDGET_SIZE_DEFAULT))
37 #define PROVIDER_BUS_NAME "org.tizen.data_provider_service"
38 #define PROVIDER_OBJECT_PATH "/org/tizen/data_provider_service"
39 #define PROVIDER_SHORTCUT_INTERFACE_NAME "org.tizen.data_provider_shortcut_service"
41 #define DBUS_SERVICE_DBUS "org.freedesktop.DBus"
42 #define DBUS_PATH_DBUS "/org/freedesktop/DBus"
43 #define DBUS_INTERFACE_DBUS "org.freedesktop.DBus"
45 static GDBusConnection *_gdbus_conn = NULL;
46 static int monitor_id = 0;
47 static int provider_monitor_id = 0;
49 static const GDBusErrorEntry dbus_error_entries[] =
51 {SHORTCUT_ERROR_INVALID_PARAMETER, "org.freedesktop.Shortcut.Error.INVALID_PARAMETER"},
52 {SHORTCUT_ERROR_OUT_OF_MEMORY, "org.freedesktop.Shortcut.Error.OUT_OF_MEMORY"},
53 {SHORTCUT_ERROR_IO_ERROR, "org.freedesktop.Shortcut.Error.IO_ERROR"},
54 {SHORTCUT_ERROR_PERMISSION_DENIED, "org.freedesktop.Shortcut.Error.PERMISSION_DENIED"},
55 {SHORTCUT_ERROR_NOT_SUPPORTED, "org.freedesktop.Shortcut.Error.NOT_SUPPORTED"},
56 {SHORTCUT_ERROR_RESOURCE_BUSY, "org.freedesktop.Shortcut.Error.RESOURCE_BUSY"},
57 {SHORTCUT_ERROR_NO_SPACE, "org.freedesktop.Shortcut.Error.NO_SPACE"},
58 {SHORTCUT_ERROR_EXIST, "org.freedesktop.Shortcut.Error.EXIST"},
59 {SHORTCUT_ERROR_FAULT, "org.freedesktop.Shortcut.Error.FAULT"},
60 {SHORTCUT_ERROR_COMM, "org.freedesktop.Shortcut.Error.COMM"},
63 struct result_cb_item {
64 result_internal_cb_t result_internal_cb;
65 result_cb_t result_cb;
69 typedef struct _shortcut_cb_info {
70 int (*request_cb)(const char *appid, const char *name, int type, const char *content, const char *icon, pid_t pid, double period, int allow_duplicate, void *data);
74 static shortcut_cb_info _callback_info;
75 static int _dbus_init();
76 static char *_shortcut_get_pkgname_by_pid(void);
78 EXPORT_API GQuark shortcut_error_quark (void)
80 static volatile gsize quark_volatile = 0;
81 g_dbus_error_register_error_domain ("shortcut-error-quark",
84 G_N_ELEMENTS(dbus_error_entries));
85 return (GQuark) quark_volatile;
88 static void _add_shortcut_notify(GVariant *parameters)
98 g_variant_get(parameters, "(i&s&si&s&si)", &sender_pid, &appid, &name, &type, &content, &icon, &allow_duplicate);
99 DbgPrint("_add_shortcut_notify sender_pid: %d ", sender_pid);
100 _callback_info.request_cb(appid, name, type, content, icon, sender_pid, -1.0f, allow_duplicate, _callback_info.data);
103 static void _add_shortcut_widget_notify(GVariant *parameters)
114 g_variant_get(parameters, "(i&s&si&s&sdi)", &sender_pid, &appid, &name, &type, &content, &icon, &period, &allow_duplicate);
115 _callback_info.request_cb(appid, name, type, content, icon, sender_pid, period, allow_duplicate, _callback_info.data);
118 static void _handle_shortcut_notify(GDBusConnection *connection,
119 const gchar *sender_name,
120 const gchar *object_path,
121 const gchar *interface_name,
122 const gchar *signal_name,
123 GVariant *parameters,
126 DbgPrint("signal_name: %s", signal_name);
127 if (g_strcmp0(signal_name, "add_shortcut_notify") == 0)
128 _add_shortcut_notify(parameters);
129 else if (g_strcmp0(signal_name, "add_shortcut_widget_notify") == 0)
130 _add_shortcut_widget_notify(parameters);
133 static int _dbus_init(void)
135 GError *error = NULL;
137 if (_gdbus_conn == NULL) {
138 _gdbus_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
140 if (_gdbus_conn == NULL) {
142 ErrPrint("Failed to get dbus [%s]", error->message);
145 return SHORTCUT_ERROR_IO_ERROR;
147 shortcut_error_quark();
150 return SHORTCUT_ERROR_NONE;
153 static int _dbus_signal_init()
155 int ret = SHORTCUT_ERROR_NONE;
158 if (monitor_id == 0) {
159 DbgPrint("get dbus connection success");
160 id = g_dbus_connection_signal_subscribe(
163 PROVIDER_SHORTCUT_INTERFACE_NAME, /* interface */
165 PROVIDER_OBJECT_PATH, /* path */
167 G_DBUS_SIGNAL_FLAGS_NONE,
168 _handle_shortcut_notify,
172 DbgPrint("subscribe id : %d", id);
174 ret = SHORTCUT_ERROR_IO_ERROR;
175 ErrPrint("Failed to _register_noti_dbus_interface");
184 static char *_shortcut_get_pkgname_by_pid(void)
186 char pkgname[SHORTCUT_PKGNAME_LEN + 1] = { 0, };
187 char buf[SHORTCUT_PKGNAME_LEN + 1] = { 0, };
194 ret = aul_app_get_pkgname_bypid(pid, pkgname, sizeof(pkgname));
196 snprintf(buf, sizeof(buf), "/proc/%d/cmdline", pid);
198 fd = open(buf, O_RDONLY);
202 ret = read(fd, pkgname, sizeof(pkgname) - 1);
211 * "ret" is not able to be larger than "sizeof(pkgname) - 1",
212 * if the system is not going wrong.
215 if (strlen(pkgname) <= 0)
219 dup_pkgname = strdup(pkgname);
221 ErrPrint("Heap: %d\n", errno);
227 * implement user request
229 static int _send_sync_shortcut(GVariant *body, GDBusMessage **reply, char *cmd)
233 int ret = SHORTCUT_ERROR_NONE;
235 msg = g_dbus_message_new_method_call(
237 PROVIDER_OBJECT_PATH,
238 PROVIDER_SHORTCUT_INTERFACE_NAME,
241 ErrPrint("Can't allocate new method call");
243 g_variant_unref(body);
244 return SHORTCUT_ERROR_OUT_OF_MEMORY;
248 g_dbus_message_set_body(msg, body);
250 *reply = g_dbus_connection_send_message_with_reply_sync(
253 G_DBUS_SEND_MESSAGE_FLAGS_NONE,
262 ret = SHORTCUT_ERROR_COMM;
264 ErrPrint("No reply. cmd = %s, error = %s", cmd, err->message);
265 if (err->code == G_DBUS_ERROR_ACCESS_DENIED)
266 ret = SHORTCUT_ERROR_PERMISSION_DENIED;
272 if (g_dbus_message_to_gerror(*reply, &err)) {
274 ErrPrint("_send_sync_shortcut error %s", err->message);
278 DbgPrint("_send_sync_shortcut done !!");
279 return SHORTCUT_ERROR_NONE;
282 static int _send_service_register()
284 GDBusMessage *reply = NULL;
287 result = _send_sync_shortcut(NULL, &reply, "shortcut_service_register");
288 ErrPrint("_send_service_register done");
292 static void _send_message_with_reply_sync_cb(GDBusConnection *connection,
296 int result = SHORTCUT_ERROR_NONE;
298 GDBusMessage *reply = NULL;
299 struct result_cb_item *cb_item = (struct result_cb_item *)user_data;
301 if (cb_item == NULL) {
302 ErrPrint("Failed to get a callback item");
306 reply = g_dbus_connection_send_message_with_reply_finish(
313 ErrPrint("No reply. error = %s", err->message);
316 result = SHORTCUT_ERROR_COMM;
318 } else if (g_dbus_message_to_gerror(reply, &err)) {
321 ErrPrint("_send_async_noti error %s", err->message);
324 if (cb_item->result_internal_cb)
325 result = cb_item->result_internal_cb(result, getpid(), cb_item->data);
326 else if (cb_item->result_cb)
327 result = cb_item->result_cb(result, cb_item->data);
330 g_object_unref(reply);
335 static int _send_async_noti(GVariant *body, struct result_cb_item *cb_item, char *cmd)
338 msg = g_dbus_message_new_method_call(
340 PROVIDER_OBJECT_PATH,
341 PROVIDER_SHORTCUT_INTERFACE_NAME,
344 ErrPrint("Can't allocate new method call");
345 return SHORTCUT_ERROR_OUT_OF_MEMORY;
349 g_dbus_message_set_body(msg, body);
351 g_dbus_connection_send_message_with_reply(
354 G_DBUS_SEND_MESSAGE_FLAGS_NONE,
358 (GAsyncReadyCallback)_send_message_with_reply_sync_cb,
363 DbgPrint("_send_async_noti done !!");
364 return SHORTCUT_ERROR_NONE;
367 static void _on_name_appeared(GDBusConnection *connection,
369 const gchar *name_owner,
372 DbgPrint("name appeared : %s", name);
373 _send_service_register();
376 static void _on_name_vanished(GDBusConnection *connection,
380 DbgPrint("name vanished : %s", name);
383 EAPI int shortcut_set_request_cb(shortcut_request_cb request_cb, void *data)
385 int ret = _dbus_init();
387 if (ret != SHORTCUT_ERROR_NONE) {
388 ErrPrint("Can't init dbus %d", ret);
392 ret = _dbus_signal_init();
393 if (ret != SHORTCUT_ERROR_NONE) {
394 ErrPrint("Can't init dbus_signal %d", ret);
398 ret = _send_service_register();
399 if (ret != SHORTCUT_ERROR_NONE) {
400 ErrPrint("Can't init ipc_monitor_register %d", ret);
404 if (request_cb == NULL)
405 return SHORTCUT_ERROR_INVALID_PARAMETER;
407 if (provider_monitor_id == 0) {
408 provider_monitor_id = g_bus_watch_name_on_connection(
411 G_BUS_NAME_WATCHER_FLAGS_NONE,
417 if (provider_monitor_id == 0) {
418 ErrPrint("watch on name fail");
419 return SHORTCUT_ERROR_IO_ERROR;
423 _callback_info.request_cb = request_cb;
424 _callback_info.data = data;
426 return SHORTCUT_ERROR_NONE;
429 EAPI int shortcut_add_to_home(const char *name, shortcut_type type, const char *uri,
430 const char *icon, int allow_duplicate, result_cb_t result_cb, void *data)
432 struct result_cb_item *item;
437 if (ADD_TO_HOME_IS_DYNAMICBOX(type)) {
438 ErrPrint("Invalid type used for adding a shortcut\n");
439 return SHORTCUT_ERROR_INVALID_PARAMETER;
443 if (ret != SHORTCUT_ERROR_NONE) {
444 ErrPrint("Can't init dbus %d", ret);
448 appid = _shortcut_get_pkgname_by_pid();
449 item = malloc(sizeof(struct result_cb_item));
454 ErrPrint("Heap: %d\n", errno);
455 return SHORTCUT_ERROR_OUT_OF_MEMORY;
458 item->result_cb = result_cb;
459 item->result_internal_cb = NULL;
471 body = g_variant_new("(ississi)", getpid(), appid, name, type, uri, icon, allow_duplicate);
472 ret = _send_async_noti(body, item, "add_shortcut");
473 if (ret != SHORTCUT_ERROR_NONE) {
481 g_variant_unref(body);
486 EAPI int shortcut_add_to_home_widget(const char *name, shortcut_widget_size_e size, const char *widget_id,
487 const char *icon, double period, int allow_duplicate, result_cb_t result_cb, void *data)
489 struct result_cb_item *item;
495 ErrPrint("AppID is null\n");
496 return SHORTCUT_ERROR_INVALID_PARAMETER;
499 if (!SHORTCUT_IS_WIDGET_SIZE(size)) {
500 ErrPrint("Invalid type used for adding a widget\n");
501 return SHORTCUT_ERROR_INVALID_PARAMETER;
505 if (ret != SHORTCUT_ERROR_NONE) {
506 ErrPrint("Can't init dbus %d", ret);
510 appid = _shortcut_get_pkgname_by_pid();
511 item = malloc(sizeof(struct result_cb_item));
516 ErrPrint("Heap: %d\n", errno);
517 return SHORTCUT_ERROR_OUT_OF_MEMORY;
520 item->result_cb = result_cb;
521 item->result_internal_cb = NULL;
524 body = g_variant_new("(ississdi)", getpid(), widget_id, name, size, NULL, icon, period, allow_duplicate);
525 ret = _send_async_noti(body, item, "add_shortcut_widget");
527 if (ret != SHORTCUT_ERROR_NONE) {
535 g_variant_unref(body);
540 EAPI int add_to_home_shortcut(const char *appid, const char *name, int type, const char *content,
541 const char *icon, int allow_duplicate, result_internal_cb_t result_cb, void *data)
544 return SHORTCUT_ERROR_NONE;
547 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)
550 return SHORTCUT_ERROR_NONE;
554 EAPI int shortcut_get_list(const char *package_name, shortcut_list_cb list_cb, void *data)
556 GDBusMessage *reply = NULL;
560 GVariant *reply_body;
564 shortcut_info_s shortcut;
567 return SHORTCUT_ERROR_INVALID_PARAMETER;
569 result = _dbus_init();
570 if (result != SHORTCUT_ERROR_NONE) {
571 ErrPrint("Can't init dbus %d", result);
575 b = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
577 g_variant_builder_add(b, "{sv}", "package_name", g_variant_new_string(package_name));
578 body = g_variant_builder_end(b);
579 result = _send_sync_shortcut(g_variant_new("(v)", body), &reply, "get_list");
581 if (result == SHORTCUT_ERROR_NONE) {
582 reply_body = g_dbus_message_get_body(reply);
583 g_variant_get(reply_body, "(ia(v))", &count, &iter);
585 while (g_variant_iter_loop(iter, "(v)", &iter_body)) {
586 g_variant_get(reply_body, "(&s&s&s&s&s)",
587 &shortcut.package_name, &shortcut.icon, &shortcut.name, &shortcut.extra_key, &shortcut.extra_data);
588 list_cb(shortcut.package_name, shortcut.icon, shortcut.name, shortcut.extra_key, shortcut.extra_data, data);
590 g_variant_iter_free(iter);
594 g_object_unref(reply);