ladishd: project name escaping
[ladish.git] / proxies / notify_proxy.c
blob096a104ba7c8c23eccab8c3b9cb5e446ff06190c
1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
2 /*
3 * LADI Session Handler (ladish)
5 * Copyright (C) 2010 Nedko Arnaudov <nedko@arnaudov.name>
7 **************************************************************************
8 * This file contains the code for sending notifications to user
9 **************************************************************************
11 * LADI Session Handler is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * LADI Session Handler is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with LADI Session Handler. If not, see <http://www.gnu.org/licenses/>
23 * or write to the Free Software Foundation, Inc.,
24 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include "notify_proxy.h"
29 #define NOTIFY_SERVICE "org.freedesktop.Notifications"
30 #define NOTIFY_OBJECT "/org/freedesktop/Notifications"
31 #define NOTIFY_IFACE "org.freedesktop.Notifications"
32 #define NOTIFY_METHOD_NOTIFY "Notify"
34 static char * g_notify_app_name;
36 bool ladish_notify_init(const char * app_name)
38 const char * name;
39 const char * vendor;
40 const char * version;
41 const char * spec_version;
43 g_notify_app_name = strdup(app_name);
44 if (g_notify_app_name == NULL)
46 log_error("strdup() failed for app name");
47 return false;
50 if (dbus_call(NOTIFY_SERVICE, NOTIFY_OBJECT, NOTIFY_IFACE, "GetServerInformation", "", "ssss", &name, &vendor, &version, &spec_version))
52 log_info("Sending notifications to '%s' '%s' (%s, %s)", vendor, name, version, spec_version);
55 return true;
58 void ladish_notify_uninit(void)
60 free(g_notify_app_name);
61 g_notify_app_name = NULL;
64 void ladish_notify_simple(uint8_t urgency, const char * summary, const char * body)
66 DBusMessage * request_ptr;
67 /* DBusMessage * reply_ptr; */
68 DBusMessageIter iter;
69 DBusMessageIter array_iter;
70 DBusMessageIter dict_iter;
71 const char * str_value;
72 uint32_t uint32_value;
73 int32_t int32_value;
75 if (g_notify_app_name == NULL)
77 /* notifications are disabled */
78 return;
81 request_ptr = dbus_message_new_method_call(NOTIFY_SERVICE, NOTIFY_OBJECT, NOTIFY_IFACE, NOTIFY_METHOD_NOTIFY);
82 if (request_ptr == NULL)
84 log_error("dbus_message_new_method_call() failed.");
85 goto exit;
88 dbus_message_iter_init_append(request_ptr, &iter);
90 /* app_name */
91 str_value = g_notify_app_name;
92 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &str_value))
94 log_error("dbus_message_iter_append_basic() failed.");
95 goto free_request;
98 /* replaces_id */
99 uint32_value = 0;
100 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_UINT32, &uint32_value))
102 log_error("dbus_message_iter_append_basic() failed.");
103 goto free_request;
106 /* app_icon */
107 str_value = "";
108 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &str_value))
110 log_error("dbus_message_iter_append_basic() failed.");
111 goto free_request;
114 /* summary */
115 str_value = summary;
116 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &str_value))
118 log_error("dbus_message_iter_append_basic() failed.");
119 goto free_request;
122 /* body */
123 str_value = body == NULL ? "" : body;
124 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &str_value))
126 log_error("dbus_message_iter_append_basic() failed.");
127 goto free_request;
130 /* actions */
131 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, DBUS_TYPE_STRING_AS_STRING, &array_iter))
133 log_error("dbus_message_iter_open_container() failed.");
134 goto free_request;
137 if (!dbus_message_iter_close_container(&iter, &array_iter))
139 log_error("dbus_message_iter_close_container() failed.");
140 goto free_request;
143 /* hints */
144 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}", &dict_iter))
146 log_error("dbus_message_iter_open_container() failed.");
147 goto free_request;
150 if (!dbus_iter_append_dict_entry(&dict_iter, DBUS_TYPE_BYTE, "urgency", &urgency, 0))
152 log_error("dbus_iter_append_dict_entry() failed.");
153 goto free_request;
156 if (!dbus_message_iter_close_container(&iter, &dict_iter))
158 log_error("dbus_message_iter_close_container() failed.");
159 goto free_request;
162 /* expire_timeout */
163 int32_value = urgency == LADISH_NOTIFY_URGENCY_HIGH ? 0 : -1;
164 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_INT32, &int32_value))
166 log_error("dbus_message_iter_append_basic() failed.");
167 goto free_request;
170 if (!dbus_call(NOTIFY_SERVICE, NOTIFY_OBJECT, NOTIFY_IFACE, NOTIFY_METHOD_NOTIFY, NULL, request_ptr, "u", &uint32_value))
172 //log_error("Notify() dbus call failed.");
173 goto free_request;
176 //log_info("notify ID is %"PRIu32, uint32_value);
178 free_request:
179 dbus_message_unref(request_ptr);
180 exit:
181 return;