From 8e32891f81d0b9f0442e973f9ec4131dc72fc148 Mon Sep 17 00:00:00 2001 From: Christian Hammond Date: Sun, 27 Jun 2004 23:38:07 +0000 Subject: [PATCH] Added the initial header file and connection code. --- ChangeLog | 6 ++ libnotify/notify.c | 222 +++++++++++++++++++++++++++++++++++++++++++++ libnotify/notify.h | 197 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 425 insertions(+) diff --git a/ChangeLog b/ChangeLog index e69de29..33f7d89 100644 --- a/ChangeLog +++ b/ChangeLog @@ -0,0 +1,6 @@ +Sun Jun 27 16:37:30 PDT 2004 Christian Hammond + + * libnotify/notify.c: + * libnotify/notify.h: + - Added the initial header file and connection code. + diff --git a/libnotify/notify.c b/libnotify/notify.c index e69de29..24a6720 100644 --- a/libnotify/notify.c +++ b/libnotify/notify.c @@ -0,0 +1,222 @@ +/** + * @file libnotify/notify.c Notifications library + * + * @Copyright (C) 2004 Christian Hammond + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ +#define DBUS_API_SUBJECT_TO_CHANGE 1 + +#include "notify.h" +#include +#include +#include + +#define NOTIFY_DBUS_SERVICE "org.freedesktop.Notifications" +#define NOTIFY_DBUS_CORE_INTERFACE "org.freedesktop.Notifications" +#define NOTIFY_DBUS_CORE_OBJECT "/org/freedesktop/Notifications" + +static DBusConnection *_dbus_conn = NULL; +static gboolean _initted = FALSE; +static gboolean _filters_added = FALSE; +static guint32 _init_ref_count = 0; +static char *_app_name = NULL; + +static DBusHandlerResult +_filter_func(DBusConnection *dbus_conn, DBusMessage *message, void *user_data) +{ + return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; +} + +static gboolean +_notify_connect(void) +{ + DBusError error; + + dbus_error_init(&error); + + _dbus_conn = dbus_bus_get(DBUS_BUS_SESSION, &error); + + if (_dbus_conn == NULL) + { + fprintf(stderr, "Error connecting to session bus: %s\n", + error.message); + dbus_error_free(&error); + + return FALSE; + } + + dbus_connection_set_exit_on_disconnect(_dbus_conn, FALSE); + + if (!dbus_bus_activate_service(_dbus_conn, NOTIFY_DBUS_SERVICE, 0, + NULL, &error)) + { + fprintf(stderr, "Error activating %s service: %s\n", + NOTIFY_DBUS_SERVICE, error.message); + + dbus_error_free(&error); + + return FALSE; + } + + if (!dbus_connection_add_filter(_dbus_conn, _filter_func, NULL, NULL)) + { + fprintf(stderr, "Error creating D-BUS message filter.\n"); + + dbus_error_free(&error); + + return FALSE; + } + + dbus_bus_add_match(_dbus_conn, + "type='signal'," + "interface='" DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS "'," + "sender='" DBUS_SERVICE_ORG_FREEDESKTOP_DBUS "'", + &error); + + if (dbus_error_is_set(&error)) + { + fprintf(stderr, "Error subscribing to signals: %s\n", error.message); + + dbus_error_free(&error); + + return FALSE; + } + + _filters_added = TRUE; + + dbus_error_free(&error); + + return TRUE; +} + +static void +_notify_disconnect(void) +{ + if (_dbus_conn == NULL) + return; + + if (_filters_added) + { + dbus_connection_remove_filter(_dbus_conn, _filter_func, NULL); + + _filters_added = FALSE; + } + + dbus_connection_unref(_dbus_conn); +} + +gboolean +notify_init(const char *app_name) +{ + g_return_val_if_fail(app_name != NULL, FALSE); + g_return_val_if_fail(*app_name != '\0', FALSE); + + _init_ref_count++; + + if (_initted) + return TRUE; + + if (!_notify_connect()) + { + _notify_disconnect(); + + return FALSE; + } + + _app_name = g_strdup(app_name); + +#ifdef HAVE_ATEXIT + atexit(notify_uninit); +#endif /* HAVE_ATEXIT */ + + _initted = TRUE; + + return TRUE; +} + +void +notify_uninit(void) +{ + g_return_if_fail(notify_is_initted()); + + _init_ref_count--; + + if (_init_ref_count > 0) + return; + + if (_app_name != NULL) + { + g_free(_app_name); + _app_name = NULL; + } + + _notify_disconnect(); +} + +gboolean +notify_is_initted(void) +{ + return _initted; +} + +void +notify_close_notification(guint32 id) +{ +} + +void +notify_close_request(guint32 id) +{ +} + +guint32 +notify_send_notification(NotifyUrgency urgency, const char *summary, + const char *detailed, const char *icon_uri, + time_t timeout) +{ + return 0; +} + +guint32 +notify_send_notification_with_icon_data(NotifyUrgency urgency, + const char *summary, + const char *detailed, + size_t icon_len, void *icon_data, + time_t timeout) +{ + return 0; +} + +guint32 +notify_send_request(NotifyUrgency urgency, const char *summary, + const char *detailed, const char *icon_uri, + time_t timeout, gpointer user_data, + size_t default_button, size_t button_count, ...) +{ + return 0; +} + +guint32 +notify_send_request_with_icon_data(NotifyUrgency urgency, + const char *summary, const char *detailed, + size_t icon_len, guchar *icon_data, + time_t timeout, gpointer user_data, + gint32 default_button, size_t button_count, + ...) +{ + return 0; +} diff --git a/libnotify/notify.h b/libnotify/notify.h index e69de29..4cada9a 100644 --- a/libnotify/notify.h +++ b/libnotify/notify.h @@ -0,0 +1,197 @@ +/** + * @file libnotify/notify.h Notifications library + * + * @Copyright (C) 2004 Christian Hammond + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ +#ifndef _LIBNOTIFY_NOTIFY_H_ +#define _LIBNOTIFY_NOTIFY_H_ + +#include +#include + +/** + * Notification and request urgency levels. + */ +typedef enum +{ + NOTIFY_URGENCY_LOW, /**< Low urgency. */ + NOTIFY_URGENCY_NORMAL, /**< Normal urgency. */ + NOTIFY_URGENCY_HIGH, /**< High urgency. */ + NOTIFY_URGENCY_CRITICAL, /**< Critical urgency. */ + +} NotifyUrgency; + +/**************************************************************************/ +/** @name libnotify Base API */ +/**************************************************************************/ +/*@{*/ + +/** + * Initializes the notifications library. + * + * @param app_name The application name. + * + * @return TRUE if the library initialized properly and a connection to a + * notification server was made. + */ +gboolean notify_init(const char *app_name); + +/** + * Uninitializes the notifications library. + * + * This will be automatically called on exit unless previously called. + */ +void notify_uninit(void); + +/** + * Returns whether or not the notification library is initialized. + * + * @return TRUE if the library is initialized, or FALSE. + */ +gboolean notify_is_initted(void); + +/** + * Manually closes a notification. + * + * @param id The ID of the notification. + */ +void notify_close_notification(guint32 id); + +/** + * Automatically closes a request. + * + * The default button's callback, if any, will be called. + * + * @param id The ID of the request. + */ +void notify_close_request(guint32 id); + +/*@}*/ + +/**************************************************************************/ +/** @name Notifications API */ +/**************************************************************************/ +/*@{*/ + +/** + * Sends a standard notification. + * + * @param urgency The urgency level. + * @param summary The summary of the notification. + * @param detailed The optional detailed information. + * @param icon_uri The optional icon URI. + * @param timeout The optional time to automatically close the notification, + * or 0. + * + * @return A unique ID for the notification. + */ +guint32 notify_send_notification(NotifyUrgency urgency, const char *summary, + const char *detailed, const char *icon_uri, + time_t timeout); + +/** + * Sends a standard notification with raw icon data. + * + * @param urgency The urgency level. + * @param summary The summary of the notification. + * @param detailed The optional detailed information. + * @param icon_len The icon data length. + * @param icon_data The icon data. + * @param timeout The optional time to automatically close the notification, + * or 0. + * + * @return A unique ID for the notification. + */ +guint32 notify_send_notification_with_icon_data(NotifyUrgency urgency, + const char *summary, + const char *detailed, + size_t icon_len, + void *icon_data, + time_t timeout); + +/*@}*/ + +/**************************************************************************/ +/** @name Requests API */ +/**************************************************************************/ +/*@{*/ + +/** + * Sends a standard request. + * + * A callback has the following prototype: + * + * @code + * void callback(guint32 id, guint32 button, void *user_data); + * @endcode + * + * @param urgency The urgency level. + * @param summary The summary of the request. + * @param detailed The optional detailed information. + * @param icon_uri The optional icon URI. + * @param timeout The optional time to automatically close the request, + * or 0. + * @param user_data User-specified data to send to a callback. + * @param default_button The default button, or -1. + * @param button_count The number of buttons. + * @param ... The buttons in string/callback pairs. + * + * @return A unique ID for the request. + */ +guint32 notify_send_request(NotifyUrgency urgency, const char *summary, + const char *detailed, const char *icon_uri, + time_t timeout, gpointer user_data, + size_t default_button, + size_t button_count, ...); + +/** + * Sends a standard request with raw icon data. + * + * A callback has the following prototype: + * + * @code + * void callback(guint32 id, guint32 button, void *user_data); + * @endcode + * + * @param urgency The urgency level. + * @param summary The summary of the request. + * @param detailed The optional detailed information. + * @param icon_len The icon data length. + * @param icon_data The icon data. + * @param timeout The optional time to automatically close the request, + * or 0. + * @param user_data User-specified data to send to a callback. + * @param default_button The default button, or -1. + * @param button_count The number of buttons. + * @param ... The buttons in string/callback pairs. + * + * @return A unique ID for the request. + */ +guint32 notify_send_request_with_icon_data(NotifyUrgency urgency, + const char *summary, + const char *detailed, + size_t icon_len, + guchar *icon_data, + time_t timeout, + gpointer user_data, + gint32 default_button, + size_t button_count, ...); + +/*@}*/ + +#endif /* _LIBNOTIFY_NOTIFY_H_ */