[patch] dbus printer addition/removal notifications



Hi,

This patch for libgnomecups adds optional DBus support for getting
notification of printer additions and removals.  This avoids polling the
system cupsd every 5 seconds and filling up the logs.

Requires an updated DBus patch for cupsd which I'll publish in eggcups
soon.

--- libgnomecups-0.1.11/configure.in.dbus	2004-09-08 20:07:40.093144656 -0400
+++ libgnomecups-0.1.11/configure.in	2004-09-08 20:07:40.110142072 -0400
@@ -22,6 +22,26 @@
 AC_SUBST(LIBGNOMECUPS_CFLAGS)
 AC_SUBST(LIBGNOMECUPS_LIBS)
 
+AC_ARG_WITH(dbus, [--with-dbus	Use DBus], ac_dbus=$withval, ac_dbus=auto)
+if test x"$ac_dbus" != xno; then
+  PKG_CHECK_MODULES(DBUS, dbus-glib-1, have_dbus=yes, have_dbus=no)
+else
+  have_dbus=no
+fi 
+if test x"$have_dbus" = xno; then
+   if test x"$ac_dbus" = xyes; then
+      AC_MSG_ERROR([DBus explicitly requested but not found on system])
+   fi
+   ac_dbus=no
+else
+   if test x"$ac_dbus" != xno; then
+     AC_DEFINE(WITH_DBUS, 1, [Define if you have DBus])
+     DBUS_CFLAGS="-DDBUS_API_SUBJECT_TO_CHANGE $DBUS_CFLAGS"
+     AC_SUBST(DBUS_CFLAGS)
+     AC_SUBST(DBUS_LIBS)
+   fi
+fi
+
 CUPS_CFLAGS=`cups-config --cflags | sed 's/-O[0-9]*//' | sed 's/-m[^\t]*//g'`
 CUPS_LIBS=`cups-config --libs`
 
--- libgnomecups-0.1.11/libgnomecups/Makefile.am.dbus	2003-05-02 06:35:23.000000000 -0400
+++ libgnomecups-0.1.11/libgnomecups/Makefile.am	2004-09-08 20:07:40.114141464 -0400
@@ -2,6 +2,7 @@
 	-I$(top_srcdir) 				\
 	-I$(top_builddir) 				\
 	$(WARN_CFLAGS)                                  \
+	$(DBUS_CFLAGS)                                  \
 	$(CUPS_CFLAGS)                                  \
 	$(LIBGNOMECUPS_CFLAGS)                          \
         -DGNOMELOCALEDIR=\""$(prefix)/${DATADIRNAME}/locale"\"  \
@@ -12,6 +13,7 @@
 lib_LTLIBRARIES=libgnomecups-1.0.la
 libgnomecups_1_0_la_LDFLAGS = \
 	$(CUPS_LIBS) \
+	$(DBUS_LIBS) \
 	$(LIBGNOMECUPS_LIBS)
 
 libgnomecups_1_0_la_SOURCES = \
--- libgnomecups-0.1.11/libgnomecups/gnome-cups-printer.c.dbus	2004-09-08 20:07:40.079146784 -0400
+++ libgnomecups-0.1.11/libgnomecups/gnome-cups-printer.c	2004-09-08 20:11:42.407307304 -0400
@@ -25,6 +25,10 @@
 
 #include "gnome-cups-printer.h"
 
+#ifdef WITH_DBUS
+#include <dbus/dbus.h>
+#include <dbus/dbus-glib-lowlevel.h>
+#endif
 #include <cups/cups.h>
 #include <time.h>
 #include <stdlib.h>
@@ -98,6 +102,7 @@
 };
 
 static void update_printers (void);
+static gboolean init_dbus (void);
 static void set_timeout (void);
 
 static GList *printer_names = NULL;
@@ -608,7 +613,8 @@
 	char *printer_name = user_data;
 
 	g_hash_table_remove (printers, printer_name);
-	set_timeout ();
+	if (!init_dbus ())
+		set_timeout ();
 }
 
 static void
@@ -741,6 +747,96 @@
 	return TRUE;
 }
 
+#ifdef WITH_DBUS
+static DBusHandlerResult
+handle_system_dbus_message (DBusConnection     *connection,
+			    DBusMessage        *message,
+			    void               *user_data)
+{
+	DBusError error;
+	gboolean is_addition;
+	char *printer_name;
+
+	if (dbus_message_is_signal (message,
+				    "com.redhat.PrinterSpooler",
+				    "PrinterAdded")) {
+		is_addition = TRUE;
+	} else if (dbus_message_is_signal (message,
+				    "com.redhat.PrinterSpooler",
+				    "PrinterRemoved")) {
+		is_addition = FALSE;
+	} else {
+		return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+	}
+
+	dbus_error_init (&error);
+	if (!dbus_message_get_args (message, &error,
+				    DBUS_TYPE_STRING, &printer_name,
+				    DBUS_TYPE_INVALID)) {
+		g_message ("Couldn't parse arguments for DBus message");
+		dbus_error_free (&error);
+		return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+	}
+
+	if (is_addition) {
+		printer_names = g_list_append (printer_names,
+					       g_strdup (printer_name));
+		printer_added (printer_name);
+	} else {
+		GList *link = g_list_find_custom (printer_names,
+						  printer_name,
+						  (GCompareFunc) strcmp);
+		if (link) {
+			printer_names = g_list_remove_link (printer_names,
+							    link);
+			g_free (link->data);
+		}
+		printer_removed (printer_name);
+	}
+	g_free (printer_name);
+	
+	return DBUS_HANDLER_RESULT_HANDLED;
+}
+#endif
+
+static gboolean
+init_dbus (void)
+{
+#ifdef WITH_DBUS
+	static DBusConnection *dbus_connection = NULL;
+	DBusConnection *connection;
+	DBusError error;
+
+	if (dbus_connection &&
+	    dbus_connection_get_is_connected (dbus_connection))
+		return TRUE;
+
+	if (dbus_connection &&
+	    !dbus_connection_get_is_connected (dbus_connection)) {
+		dbus_connection_unref (dbus_connection);
+		dbus_connection = NULL;
+	}
+  
+	dbus_error_init (&error);
+	connection = dbus_bus_get (DBUS_BUS_SYSTEM, &error);
+	if (connection == NULL || dbus_error_is_set (&error)) {
+		g_warning ("Couldn't connect to system bus");
+		dbus_error_free (&error);
+		return FALSE;
+	}
+
+	dbus_bus_add_match (connection,
+			    "type='signal',"
+			    "interface='com.redhat.PrinterSpooler'",
+			    NULL);
+	dbus_connection_add_filter (connection, handle_system_dbus_message,
+				    NULL, NULL);
+	dbus_connection_setup_with_g_main (connection, NULL);
+	dbus_connection = connection;
+#endif
+	return TRUE;
+}
+
 static void
 set_timeout (void)
 {
@@ -775,7 +871,8 @@
 	
 	add_notifies = g_list_append (add_notifies, notify);
 
-	set_timeout ();
+	if (!init_dbus ())
+		set_timeout ();
 
 	return notify->id;
 }
@@ -792,7 +889,8 @@
 		}
 	}
 
-	set_timeout ();
+	if (!init_dbus ())
+		set_timeout ();
 }
 
 guint
@@ -812,7 +910,8 @@
 	
 	removed_notifies = g_list_append (removed_notifies, notify);
 
-	set_timeout ();
+	if (!init_dbus ())
+		set_timeout ();
 
 	return notify->id;
 }
@@ -829,7 +928,8 @@
 		}
 	}
 
-	set_timeout ();
+	if (!init_dbus ())
+		set_timeout ();
 }
 
 GnomeCupsPrinter *
@@ -884,7 +984,8 @@
 	key = g_strdup (printer_name);
 	g_hash_table_insert (printers, key, printer);
 	g_object_weak_ref (G_OBJECT (printer), remove_from_printers, key);
-	set_timeout ();
+	if (!init_dbus ())
+		set_timeout ();
 	
 	if (default_printer && !strcmp (printer_name, default_printer)) {
 		printer->details->is_default = TRUE;
@@ -1058,7 +1159,7 @@
 
 	gnome_cups_request_file (host, ppdpath, fd, &error);
 	if (error != NULL) {
-		g_warning ("Couldn't retrieve PPD for %s: %s",
+		g_message ("Couldn't retrieve PPD for %s: %s",
 			   printer->details->printer_name,
 			   error->message);
 		g_error_free (error);

Attachment: signature.asc
Description: This is a digitally signed message part



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]