[patch] notification for printer deletions



Hi,

Here is a patch by Matthias and I which allows one to receive
notifications when printers go away, without holding a reference
to every printer object (and thus polling their state).
Index: libgnomecups/gnome-cups-printer.h
===================================================================
RCS file: /cvs/gnome/libgnomecups/libgnomecups/gnome-cups-printer.h,v
retrieving revision 1.8
diff -u -r1.8 gnome-cups-printer.h
--- libgnomecups/gnome-cups-printer.h	17 Jun 2004 17:21:03 -0000	1.8
+++ libgnomecups/gnome-cups-printer.h	11 Aug 2004 15:27:26 -0000
@@ -27,6 +27,8 @@
 
 typedef void (*GnomeCupsPrinterAddedCallback) (const char *name, 
 					       gpointer user_data);
+typedef void (*GnomeCupsPrinterRemovedCallback) (const char *name, 
+						 gpointer user_data);
 
 struct _GnomeCupsPrinter {
 	GObject parent;
@@ -87,6 +89,10 @@
 guint             gnome_cups_printer_new_printer_notify_add    (GnomeCupsPrinterAddedCallback,
 								gpointer user_data);
 void              gnome_cups_printer_new_printer_notify_remove (guint id);
+
+guint             gnome_cups_printer_printer_removed_notify_add    (GnomeCupsPrinterAddedCallback,
+								    gpointer user_data);
+void              gnome_cups_printer_printer_removed_notify_remove (guint id);
 
 /* GnomeCupsPrinter */
 
Index: gnome-cups-printer.c
===================================================================
RCS file: /cvs/gnome/libgnomecups/libgnomecups/gnome-cups-printer.c,v
retrieving revision 1.21
diff -u -r1.21 gnome-cups-printer.c
--- gnome-cups-printer.c	3 Aug 2004 15:19:15 -0000	1.21
+++ gnome-cups-printer.c	11 Aug 2004 15:26:37 -0000
@@ -75,6 +75,12 @@
 	gpointer user_data;
 } AddNotify;
 
+typedef struct {
+	guint id;
+	GnomeCupsPrinterRemovedCallback func;
+	gpointer user_data;
+} RemovedNotify;
+
 static const char *printer_state_strings[] = {
 	N_("Ready"),
 	N_("Printing"),
@@ -95,6 +101,7 @@
 static char *default_printer = NULL;
 static GHashTable *printers = NULL;
 static GList *add_notifies = NULL;
+static GList *removed_notifies = NULL;
 static guint signals[LAST_SIGNAL];
 
 GList *
@@ -421,6 +428,7 @@
 printer_removed (const char *name)
 {
 	GnomeCupsPrinter *printer;
+	GList *l;
 	
 	printer = gnome_cups_printer_get_existing (name);
 	
@@ -429,6 +437,11 @@
 		g_signal_emit (printer, signals[GONE], 0);
 		g_object_unref (printer);
 	}
+
+	for (l = removed_notifies; l != NULL; l = l->next) {
+		RemovedNotify *notify = l->data;
+		notify->func (name, notify->user_data);
+	}
 }
 
 static gboolean
@@ -590,6 +603,43 @@
 	set_timeout ();
 }
 
+guint
+gnome_cups_printer_printer_removed_notify_add (GnomeCupsPrinterRemovedCallback,
+					       gpointer user_data)
+{
+	guint id = 0;
+	RemovedNotify *notify;
+
+	g_return_val_if_fail (cb != NULL, 0);
+
+	notify = g_new0 (RemovedNotify, 1);
+	
+	notify->id = ++id;
+	notify->func = cb;
+	notify->user_data = user_data;
+	
+	removed_notifies = g_list_append (removed_notifies, notify);
+
+	set_timeout ();
+
+	return notify->id;
+}
+
+void
+gnome_cups_printer_printer_removed_notify_remove (guint id)
+{
+	GList *l;
+	for (l = removed_notifies; l != NULL; l = l->next) {
+		RemovedNotify *notify = l->data;
+		if (notify->id == id) {
+			g_free (notify);
+			removed_notifies = g_list_remove_link (removed_notifies, l);
+		}
+	}
+
+	set_timeout ();
+}
+
 GnomeCupsPrinter *
 gnome_cups_printer_get_existing (const char *printer_name)
 {

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]