Main Page | Data Structures | File List | Data Fields | Globals

rpc-statusbar.c

Go to the documentation of this file.
00001 /**
00002  * This file is part of alarmd
00003  *
00004  * Contact Person: David Weinehall <david.weinehall@nokia.com>
00005  *
00006  * Copyright (C) 2006 Nokia Corporation
00007  * alarmd and libalarm are free software; you can redistribute them
00008  * and/or modify them under the terms of the GNU Lesser General Public
00009  * License version 2.1 as published by the Free Software Foundation.
00010  *
00011  * alarmd and libalarm are distributed in the hope that they will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this software; if not, write to the Free
00018  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
00019  * 02110-1301 USA
00020  */
00021 
00022 #include <dbus/dbus.h>
00023 #include <glib/gtypes.h>
00024 #include <glib/gthread.h>
00025 #include <glib/gmain.h>
00026 #include <string.h>
00027 #include "rpc-dbus.h"
00028 #include "rpc-statusbar.h"
00029 #include "debug.h"
00030 
00031 static guint users = 0;
00032 static DBusConnection *conn = NULL;
00033 static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
00034 
00035 #define STATUSBAR_SERVICE "com.nokia.statusbar"
00036 #define STATUSBAR_OBJECT_PATH "/com/nokia/statusbar"
00037 #define STATUSBAR_INTERFACE "com.nokia.statusbar"
00038 static const gchar * const STATUSBAR_ALARM_NAME = "alarm";
00039 static const gint STATUSBAR_MAGIC_ON = 1;
00040 static const gint STATUSBAR_MAGIC_OFF = 0;
00041 #define STATUSBAR_STARTED_SIGNAL "type='signal',sender='org.freedesktop.DBus',interface='org.freedesktop.DBus',member='NameOwnerChanged',arg0='com.nokia.statusbar'"
00042 static const gchar * const EMPTY = "";
00043 
00044 static DBusHandlerResult _statusbar_started(DBusConnection *connection, DBusMessage *message, void *user_data);
00045 static gboolean _display_icon(gpointer user_data);
00046 
00047 void statusbar_show_icon(void)
00048 {
00049         ENTER_FUNC;
00050         g_static_mutex_lock(&mutex);
00051         users++;
00052         if (users == 1) {
00053                 conn = get_dbus_connection(DBUS_BUS_SYSTEM);
00054                 if (conn) {
00055                         dbus_do_call(conn, NULL, FALSE, STATUSBAR_SERVICE, STATUSBAR_OBJECT_PATH, STATUSBAR_INTERFACE,
00056                                         "event", DBUS_TYPE_STRING, &STATUSBAR_ALARM_NAME, DBUS_TYPE_INT32, &STATUSBAR_MAGIC_ON,
00057                                         DBUS_TYPE_INT32, &STATUSBAR_MAGIC_ON, DBUS_TYPE_STRING, &EMPTY, DBUS_TYPE_INVALID);
00058                         dbus_bus_add_match(conn, STATUSBAR_STARTED_SIGNAL, NULL);
00059                         dbus_connection_add_filter(conn, _statusbar_started, NULL, NULL);
00060                 }
00061         }
00062         g_static_mutex_unlock(&mutex);
00063         LEAVE_FUNC;
00064 }
00065 
00066 void statusbar_hide_icon(void)
00067 {
00068         ENTER_FUNC;
00069         g_static_mutex_lock(&mutex);
00070         users--;
00071         if (users == 0) {
00072                 if (conn) {
00073                         dbus_do_call(conn, NULL, FALSE, STATUSBAR_SERVICE, STATUSBAR_OBJECT_PATH, STATUSBAR_INTERFACE,
00074                                         "event", DBUS_TYPE_STRING, &STATUSBAR_ALARM_NAME, DBUS_TYPE_INT32, &STATUSBAR_MAGIC_ON,
00075                                         DBUS_TYPE_INT32, &STATUSBAR_MAGIC_OFF, DBUS_TYPE_STRING, &EMPTY, DBUS_TYPE_INVALID);
00076                         dbus_connection_remove_filter(conn, _statusbar_started, NULL);
00077                         dbus_bus_remove_match(conn, STATUSBAR_STARTED_SIGNAL, NULL);
00078                         dbus_connection_unref(conn);
00079                         conn = NULL;
00080                 }
00081         }
00082         g_static_mutex_unlock(&mutex);
00083         LEAVE_FUNC;
00084 }
00085 
00086 static gboolean _display_icon(gpointer user_data)
00087 {
00088         ENTER_FUNC;
00089         (void)user_data;
00090         g_static_mutex_lock(&mutex);
00091         if (users > 0) {
00092                 dbus_do_call(conn, NULL, FALSE, STATUSBAR_SERVICE, STATUSBAR_OBJECT_PATH, STATUSBAR_INTERFACE,
00093                                 "event", DBUS_TYPE_STRING, &STATUSBAR_ALARM_NAME, DBUS_TYPE_INT32, &STATUSBAR_MAGIC_ON,
00094                                 DBUS_TYPE_INT32, &STATUSBAR_MAGIC_ON, DBUS_TYPE_STRING, &EMPTY, DBUS_TYPE_INVALID);
00095         }
00096         g_static_mutex_unlock(&mutex);
00097         LEAVE_FUNC;
00098         return FALSE;
00099 }
00100 
00101 static DBusHandlerResult _statusbar_started(DBusConnection *connection, DBusMessage *message, void *user_data)
00102 {
00103         ENTER_FUNC;
00104         (void)connection;
00105         (void)user_data;
00106         if (dbus_message_is_signal(message, DBUS_SERVICE_DBUS, "NameOwnerChanged")) {
00107                 const gchar *name = NULL;
00108                 dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
00109                 if (name != NULL && strcmp(name, STATUSBAR_SERVICE) == 0) {
00110                         g_timeout_add(1000, _display_icon, NULL); 
00111                 }
00112         }
00113         LEAVE_FUNC;
00114         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
00115 }

Generated on Thu Dec 21 18:23:30 2006 for Alarmd by  doxygen 1.4.2