From eda55d25b59b4ed7c373f37eb9004832f9e26367 Mon Sep 17 00:00:00 2001 From: Lars Uebernickel Date: Thu, 12 Jan 2012 17:04:30 +0100 Subject: Move menu related code into a separate class Also removes the "System Settings" menu item (according to spec). Printer menu items are hardcoded for now. --- src/Makefile.am | 4 +- src/indicator-printers-menu.c | 123 +++++++++++++++++++++++++++++++++++++++ src/indicator-printers-menu.h | 53 +++++++++++++++++ src/indicator-printers-service.c | 88 +++------------------------- 4 files changed, 187 insertions(+), 81 deletions(-) create mode 100644 src/indicator-printers-menu.c create mode 100644 src/indicator-printers-menu.h diff --git a/src/Makefile.am b/src/Makefile.am index c5bdd52..5fc0107 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -13,7 +13,9 @@ libprintersmenu_la_LDFLAGS = -module -avoid-version libexec_PROGRAMS = indicator-printers-service indicator_printers_service_SOURCES = \ indicator-printers-service.c \ - indicator-printers-service.h + indicator-printers-service.h \ + indicator-printers-menu.c \ + indicator-printers-menu.h indicator_printers_service_CPPFLAGS = $(SERVICE_CFLAGS) indicator_printers_service_LDADD = $(SERVICE_LIBS) diff --git a/src/indicator-printers-menu.c b/src/indicator-printers-menu.c new file mode 100644 index 0000000..26ed526 --- /dev/null +++ b/src/indicator-printers-menu.c @@ -0,0 +1,123 @@ + +#include "indicator-printers-menu.h" + +#include + + +#define MENUITEM_PROP_PRINTER "indicator-printers-printer" + + +G_DEFINE_TYPE (IndicatorPrintersMenu, indicator_printers_menu, G_TYPE_OBJECT) + +#define PRINTERS_MENU_PRIVATE(o) \ + (G_TYPE_INSTANCE_GET_PRIVATE ((o), INDICATOR_TYPE_PRINTERS_MENU, IndicatorPrintersMenuPrivate)) + + +struct _IndicatorPrintersMenuPrivate +{ + DbusmenuMenuitem *root; +}; + + +static void +dispose (GObject *object) +{ + IndicatorPrintersMenuPrivate *priv = PRINTERS_MENU_PRIVATE (object); + + g_clear_object (priv->root); + + G_OBJECT_CLASS (indicator_printers_menu_parent_class)->dispose (object); +} + + +static void +indicator_printers_menu_class_init (IndicatorPrintersMenuClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + + g_type_class_add_private (klass, sizeof (IndicatorPrintersMenuPrivate)); + + object_class->dispose = dispose; +} + + +static void +show_system_settings (DbusmenuMenuitem *menuitem, + guint timestamp, + gpointer user_data) +{ + GAppInfo *appinfo; + GError *err = NULL; + const gchar *printer; + gchar *cmdline; + + printer = dbusmenu_menuitem_property_get (menuitem, MENUITEM_PROP_PRINTER); + cmdline = g_strdup_printf ("gnome-control-center printing show-printer %s", + printer); + + appinfo = g_app_info_create_from_commandline (cmdline, + "gnome-control-center", + G_APP_INFO_CREATE_SUPPORTS_STARTUP_NOTIFICATION, + &err); + g_free (cmdline); + + if (err) { + g_warning ("failed to create application info: %s", err->message); + g_error_free (err); + return; + } + + g_app_info_launch (appinfo, NULL, NULL, &err); + if (err) { + g_warning ("failed to launch gnome-control-center: %s", err->message); + g_error_free (err); + } + + g_object_unref (appinfo); +} + + +static void +add_printer_menuitem (IndicatorPrintersMenu *self, + const gchar *printer) +{ + IndicatorPrintersMenuPrivate *priv = PRINTERS_MENU_PRIVATE (self); + DbusmenuMenuitem *child; + + child = dbusmenu_menuitem_new (); + dbusmenu_menuitem_property_set (child, "label", printer); + g_signal_connect (child, + "item-activated", + G_CALLBACK (show_system_settings), + NULL); + + dbusmenu_menuitem_child_append(priv->root, child); + g_object_unref (child); +} + + +static void +indicator_printers_menu_init (IndicatorPrintersMenu *self) +{ + IndicatorPrintersMenuPrivate *priv = PRINTERS_MENU_PRIVATE (self); + + priv->root = dbusmenu_menuitem_new (); + add_printer_menuitem (self, "canon-mono-duplex"); + add_printer_menuitem (self, "kitchen"); +} + + +IndicatorPrintersMenu * +indicator_printers_menu_new (void) +{ + return g_object_new (INDICATOR_TYPE_PRINTERS_MENU, NULL); +} + + +DbusmenuMenuitem * +indicator_printers_menu_get_root (IndicatorPrintersMenu *self) +{ + IndicatorPrintersMenuPrivate *priv = PRINTERS_MENU_PRIVATE (self); + return priv->root; +} + diff --git a/src/indicator-printers-menu.h b/src/indicator-printers-menu.h new file mode 100644 index 0000000..bca9b0c --- /dev/null +++ b/src/indicator-printers-menu.h @@ -0,0 +1,53 @@ +#ifndef INDICATOR_PRINTERS_MENU_H +#define INDICATOR_PRINTERS_MENU_H + +#include +#include + +G_BEGIN_DECLS + +#define INDICATOR_TYPE_PRINTERS_MENU indicator_printers_menu_get_type() + +#define INDICATOR_PRINTERS_MENU(obj) \ + (G_TYPE_CHECK_INSTANCE_CAST ((obj), \ + INDICATOR_TYPE_PRINTERS_MENU, IndicatorPrintersMenu)) + +#define INDICATOR_PRINTERS_MENU_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_CAST ((klass), \ + INDICATOR_TYPE_PRINTERS_MENU, IndicatorPrintersMenuClass)) + +#define INDICATOR_IS_PRINTERS_MENU(obj) \ + (G_TYPE_CHECK_INSTANCE_TYPE ((obj), \ + INDICATOR_TYPE_PRINTERS_MENU)) + +#define INDICATOR_IS_PRINTERS_MENU_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_TYPE ((klass), \ + INDICATOR_TYPE_PRINTERS_MENU)) + +#define INDICATOR_PRINTERS_MENU_GET_CLASS(obj) \ + (G_TYPE_INSTANCE_GET_CLASS ((obj), \ + INDICATOR_TYPE_PRINTERS_MENU, IndicatorPrintersMenuClass)) + +typedef struct _IndicatorPrintersMenu IndicatorPrintersMenu; +typedef struct _IndicatorPrintersMenuClass IndicatorPrintersMenuClass; +typedef struct _IndicatorPrintersMenuPrivate IndicatorPrintersMenuPrivate; + +struct _IndicatorPrintersMenu +{ + GObject parent; +}; + +struct _IndicatorPrintersMenuClass +{ + GObjectClass parent_class; +}; + +GType indicator_printers_menu_get_type (void) G_GNUC_CONST; + +IndicatorPrintersMenu *indicator_printers_menu_new (void); +DbusmenuMenuitem * indicator_printers_menu_get_root (IndicatorPrintersMenu *menu); + +G_END_DECLS + +#endif + diff --git a/src/indicator-printers-service.c b/src/indicator-printers-service.c index c350ee0..f390f36 100644 --- a/src/indicator-printers-service.c +++ b/src/indicator-printers-service.c @@ -21,6 +21,8 @@ #include #include "dbus-names.h" +#include "indicator-printers-menu.h" + static void service_shutdown (IndicatorService *service, gpointer user_data) @@ -30,87 +32,11 @@ service_shutdown (IndicatorService *service, gpointer user_data) } -static void -show_system_settings (DbusmenuMenuitem *menuitem, - guint timestamp, - gpointer user_data) -{ - GAppInfo *appinfo; - GError *err = NULL; - - appinfo = g_app_info_create_from_commandline ("gnome-control-center printing", - "gnome-control-center", - G_APP_INFO_CREATE_SUPPORTS_STARTUP_NOTIFICATION, - &err); - if (err) { - g_warning ("failed to create application info: %s", err->message); - g_error_free (err); - return; - } - - g_app_info_launch (appinfo, NULL, NULL, &err); - if (err) { - g_warning ("failed to launch gnome-control-center: %s", err->message); - g_error_free (err); - } - - g_object_unref (appinfo); -} - - -static void -dbusmenu_menuitem_append_separator (DbusmenuMenuitem *item) -{ - DbusmenuMenuitem *separator; - - separator = dbusmenu_menuitem_new(); - dbusmenu_menuitem_property_set(separator, - "type", - DBUSMENU_CLIENT_TYPES_SEPARATOR); - - dbusmenu_menuitem_child_append(item, separator); - g_object_unref (separator); -} - - -static void -dbusmenu_menuitem_append_label (DbusmenuMenuitem *item, - const gchar *text, - GCallback activated, - gpointer user_data) -{ - DbusmenuMenuitem *child; - - child = dbusmenu_menuitem_new (); - dbusmenu_menuitem_property_set (child, "label", text); - g_signal_connect (child, "item-activated", activated, user_data); - - dbusmenu_menuitem_child_append(item, child); - g_object_unref (child); -} - - -static DbusmenuMenuitem * -create_menu () -{ - DbusmenuMenuitem *root; - - root = dbusmenu_menuitem_new (); - dbusmenu_menuitem_append_separator (root); - dbusmenu_menuitem_append_label (root, - "Printer Settingsā€¦", - G_CALLBACK(show_system_settings), - NULL); - return root; -} - - int main (int argc, char *argv[]) { IndicatorService *service; DbusmenuServer *menuserver; - DbusmenuMenuitem *root; - GError *err = NULL; + IndicatorPrintersMenu *menu; gtk_init (&argc, &argv); @@ -121,13 +47,15 @@ int main (int argc, char *argv[]) G_CALLBACK (service_shutdown), NULL); + menu = indicator_printers_menu_new (); + menuserver = dbusmenu_server_new (INDICATOR_PRINTERS_DBUS_OBJECT_PATH); - root = create_menu (); - dbusmenu_server_set_root (menuserver, root); - g_object_unref (root); + dbusmenu_server_set_root (menuserver, + indicator_printers_menu_get_root (menu)); gtk_main (); + g_object_unref (menu); g_object_unref (menuserver); g_object_unref (service); return 0; -- cgit v1.2.3