--- desktop-printing-0.1.10/eggcups-0.0.2/configure.in.dbus 2003-04-13 00:45:16.000000000 -0400 +++ desktop-printing-0.1.10/eggcups-0.0.2/configure.in 2003-04-13 00:45:16.000000000 -0400 @@ -48,6 +48,7 @@ gobject-2.0 gtk+-2.0 >= $GTK_REQUIRED libgnomeui-2.0 >= $LIBGNOMEUI_REQUIRED + dbus-glib-1.0 ]) AC_SUBST(EGGCUPS_CFLAGS) --- desktop-printing-0.1.10/eggcups-0.0.2/main.c.dbus 2002-12-08 05:04:44.000000000 -0500 +++ desktop-printing-0.1.10/eggcups-0.0.2/main.c 2003-04-13 00:45:16.000000000 -0400 @@ -2,7 +2,7 @@ /* * Copyright (C) 2002 CodeFactory AB * Copyright (C) 2002 Richard Hult - * Copyright (C) 2002 Tim Waugh + * Copyright (C) 2002, 2003 Tim Waugh * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as @@ -31,6 +31,8 @@ #include "lp-intl.h" #include "lpegg.h" +gboolean debug = 0; + gboolean session_die (GnomeClient *client, gpointer client_data) { @@ -74,7 +76,8 @@ if (strcmp (arg, "-?") == 0) { g_printerr ("Usage: %s [--debug]\n", argv[0]); return 0; - } + } else if (strcmp (arg, "--debug") == 0) + debug = 1; ++i; } --- desktop-printing-0.1.10/eggcups-0.0.2/lpegg.c.dbus 2002-12-08 06:55:27.000000000 -0500 +++ desktop-printing-0.1.10/eggcups-0.0.2/lpegg.c 2003-04-13 01:08:20.000000000 -0400 @@ -2,7 +2,7 @@ /* * Copyright (C) 2002 CodeFactory AB * Copyright (C) 2002 Richard Hult - * Copyright (C) 2002 Tim Waugh + * Copyright (C) 2002, 2003 Tim Waugh * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as @@ -33,12 +33,18 @@ #include #include #include +#include #include "eggtrayicon.h" #include "lp-intl.h" #include "lpcups.h" #include "lpegg.h" +#include + struct _LpEgg { + /* D-BUS */ + DBusConnection *dbus; + /* Widgets. */ EggTrayIcon *icon; GtkWidget *icon_image; @@ -51,6 +57,8 @@ GtkItemFactory *popup_factory; GtkTooltips *tips; gchar *tip; + + unsigned int timer; }; static void @@ -144,6 +152,61 @@ extern gboolean debug; +static gboolean maybe_change_state (LpEgg *lp); + +static DBusHandlerResult +handle_dbus_message (DBusMessageHandler *handler, + DBusConnection *connection, + DBusMessage *message, + void *data) +{ + LpEgg *lp = (LpEgg *) data; + if (debug) printf ("eggcups: Received message: %s\n", + dbus_message_get_name (message)); + if (dbus_message_name_is (message, DBUS_MESSAGE_LOCAL_DISCONNECT)) { + /* Fall back to polling. */ + if (debug) puts ("eggcups: Falling back to polling.."); + lp->dbus = NULL; + dbus_connection_unref (connection); + lp->timer = g_timeout_add (5000, + (GSourceFunc) maybe_change_state, + lp); + } else if (dbus_message_name_is (message, "com.redhat.PrinterSpooler.QueueChanged")) + maybe_change_state (lp); + + return DBUS_HANDLER_RESULT_ALLOW_MORE_HANDLERS; +} + +static void leak (void *nothing) { } + +static int +init_dbus (LpEgg *lp) +{ + DBusMessageHandler *handler; + DBusConnection *dbus; + DBusError error; + DBusMessage *message, *reply; + + lp->dbus = NULL; + dbus_error_init (&error); + dbus = dbus_bus_get (DBUS_BUS_SYSTEM, &error); + if (dbus == NULL) { + if (debug) printf ("eggcups: %s\n", error.message); + dbus_error_free (&error); + return -1; + } + + /* FIXME "handler" is leaked if we later unref the connection. + * (the way this works may change in libdbus, it's kind + * of annoying) - hp + */ + handler = dbus_message_handler_new (handle_dbus_message, lp, leak); + dbus_connection_add_filter (dbus, handler); + dbus_connection_setup_with_g_main (dbus); + lp->dbus = dbus; + return 0; +} + static gboolean maybe_change_state (LpEgg *lp) { @@ -176,6 +239,12 @@ } else g_free (tip); + /* Try to connect to the system D-BUS */ + if (lp->timer != -1 && !lp->dbus) { + if (!init_dbus (lp)) + g_source_remove (lp->timer); + } + return TRUE; } @@ -335,11 +404,14 @@ G_CALLBACK (icon_destroy_cb), lp); + lp->timer = -1; maybe_change_state (lp); - g_timeout_add (5000, - (GSourceFunc) maybe_change_state, - lp); + if (init_dbus (lp)) + /* Fall back to polling. */ + lp->timer = g_timeout_add (5000, + (GSourceFunc) maybe_change_state, + lp); return lp; }