1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
3 * LADI Session Handler (ladish)
5 * Copyright (C) 2010,2011 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
)
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");
50 if (cdbus_call(0, 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
);
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; */
69 DBusMessageIter array_iter
;
70 DBusMessageIter dict_iter
;
71 const char * str_value
;
72 uint32_t uint32_value
;
75 if (g_notify_app_name
== NULL
)
77 /* notifications are disabled */
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.");
88 dbus_message_iter_init_append(request_ptr
, &iter
);
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.");
100 if (!dbus_message_iter_append_basic(&iter
, DBUS_TYPE_UINT32
, &uint32_value
))
102 log_error("dbus_message_iter_append_basic() failed.");
108 if (!dbus_message_iter_append_basic(&iter
, DBUS_TYPE_STRING
, &str_value
))
110 log_error("dbus_message_iter_append_basic() failed.");
116 if (!dbus_message_iter_append_basic(&iter
, DBUS_TYPE_STRING
, &str_value
))
118 log_error("dbus_message_iter_append_basic() failed.");
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.");
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.");
137 if (!dbus_message_iter_close_container(&iter
, &array_iter
))
139 log_error("dbus_message_iter_close_container() failed.");
144 if (!dbus_message_iter_open_container(&iter
, DBUS_TYPE_ARRAY
, "{sv}", &dict_iter
))
146 log_error("dbus_message_iter_open_container() failed.");
150 if (!cdbus_iter_append_dict_entry(&dict_iter
, DBUS_TYPE_BYTE
, "urgency", &urgency
, 0))
152 log_error("dbus_iter_append_dict_entry() failed.");
156 if (!dbus_message_iter_close_container(&iter
, &dict_iter
))
158 log_error("dbus_message_iter_close_container() failed.");
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.");
170 if (!cdbus_call(0, NOTIFY_SERVICE
, NOTIFY_OBJECT
, NOTIFY_IFACE
, NOTIFY_METHOD_NOTIFY
, NULL
, request_ptr
, "u", &uint32_value
))
172 //log_error("Notify() dbus call failed.");
176 //log_info("notify ID is %"PRIu32, uint32_value);
179 dbus_message_unref(request_ptr
);