[krb5-auth-dialog] Switch to an app_menu



commit 80da3657d00d37404dcddb9530584516d0efe7eb
Author: Guido Günther <agx sigxcpu org>
Date:   Mon Mar 25 07:29:05 2013 +0100

    Switch to an app_menu
    
    Use GtkApplicationWindow to make this simple.

 src/ka-applet-priv.h    |    1 +
 src/ka-applet.c         |  120 ++++++++++++++++++++++++++++++++++++++++-------
 src/ka-main-window.c    |   66 ++++----------------------
 src/ka-main-window.h    |    2 +-
 src/ka-preferences.c    |    9 ++-
 src/ka-preferences.h    |    2 +-
 src/ka-tools.c          |    6 ++-
 src/ka-tools.h          |    3 +-
 src/krb5-auth-dialog.ui |  115 ++++++++++-----------------------------------
 9 files changed, 151 insertions(+), 173 deletions(-)
---
diff --git a/src/ka-applet-priv.h b/src/ka-applet-priv.h
index 25fe281..ffcb4a4 100644
--- a/src/ka-applet-priv.h
+++ b/src/ka-applet-priv.h
@@ -55,6 +55,7 @@ GSettings *ka_applet_get_settings (const KaApplet *self);
 void ka_applet_signal_emit (KaApplet *self, KaAppletSignalNumber signum,
                             krb5_timestamp expiry);
 void ka_applet_set_msg (KaApplet *self, const char *msg);
+GtkWindow* ka_applet_last_focused_window(KaApplet *self);
 
 /* properties */
 #define KA_PROP_NAME_PRINCIPAL       "principal"
diff --git a/src/ka-applet.c b/src/ka-applet.c
index 669e9b5..2b48825 100644
--- a/src/ka-applet.c
+++ b/src/ka-applet.c
@@ -182,20 +182,102 @@ ka_applet_local_command_line (GApplication *application,
     return FALSE;
 }
 
+GtkWindow *ka_applet_last_focused_window (KaApplet *self)
+{
+    GList *l = gtk_application_get_windows (GTK_APPLICATION(self));
+
+    if (l != NULL )
+        return g_list_first (l)->data;
+
+    return NULL;
+}
+
+static void
+action_preferences (GSimpleAction *action G_GNUC_UNUSED,
+                   GVariant *parameter G_GNUC_UNUSED,
+                   gpointer userdata)
+{
+    KaApplet *self = userdata;
+
+    ka_preferences_window_show (self);
+}
+
+static void
+action_about (GSimpleAction *action G_GNUC_UNUSED,
+              GVariant *parameter G_GNUC_UNUSED,
+              gpointer userdata)
+{
+    KaApplet *self = KA_APPLET(userdata);
+
+    ka_show_about (self);
+}
+
+static void
+action_help (GSimpleAction *action G_GNUC_UNUSED,
+              GVariant *parameter G_GNUC_UNUSED,
+              gpointer userdata)
+{
+    KaApplet *self = KA_APPLET(userdata);
+    GtkWindow *window = ka_applet_last_focused_window (self);
+
+    ka_show_help (gtk_window_get_screen (window), NULL, NULL);
+}
+
+static void
+action_quit (GSimpleAction *action G_GNUC_UNUSED,
+             GVariant *parameter G_GNUC_UNUSED,
+             gpointer userdata)
+{
+    KaApplet *self = KA_APPLET (userdata);
+
+    ka_applet_destroy (self);
+}
+
+static GActionEntry app_entries[] = {
+    { "preferences", action_preferences, NULL, NULL, NULL, {0} },
+    { "about", action_about, NULL, NULL, NULL, {0} },
+    { "help", action_help, NULL, NULL, NULL, {0} },
+    { "quit", action_quit, NULL, NULL, NULL, {0} },
+};
+
+static void
+ka_applet_app_menu_create(KaApplet *self,
+                          GtkBuilder *uixml)
+{
+    const gchar *debug_no_app_menu;
+    GMenuModel *model = G_MENU_MODEL(gtk_builder_get_object (uixml,
+                                                             "app-menu"));
+    g_action_map_add_action_entries (G_ACTION_MAP (self),
+                                     app_entries, G_N_ELEMENTS (app_entries),
+                                     self);
+
+    g_assert (model != NULL);
+    gtk_application_set_app_menu (GTK_APPLICATION(self),
+                                  model);
+
+    debug_no_app_menu = g_getenv ("KRB5_AUTH_DIALOG_DEBUG_NO_APP_MENU");
+    if (debug_no_app_menu) {
+        KA_DEBUG ("Disabling app menu GtkSetting as requested...");
+        g_object_set (gtk_settings_get_default (),
+                      "gtk-shell-shows-app-menu", FALSE,
+                      NULL);
+    }
+}
+
 static void
 ka_applet_startup (GApplication *application)
 {
     KaApplet *self = KA_APPLET (application);
-    GtkWindow *main_window;
 
     KA_DEBUG ("Primary application");
 
+    G_APPLICATION_CLASS (ka_applet_parent_class)->startup (application);
+
     self->priv->startup_ccache = ka_kerberos_init (self);
-    main_window = ka_main_window_create (self, self->priv->uixml);
-    gtk_application_add_window (GTK_APPLICATION(self), main_window);
+    ka_main_window_create (self, self->priv->uixml);
     ka_preferences_window_create (self, self->priv->uixml);
 
-    G_APPLICATION_CLASS (ka_applet_parent_class)->startup (application);
+    ka_applet_app_menu_create(self, self->priv->uixml);
 }
 
 static void
@@ -821,7 +903,7 @@ ka_applet_update_status (KaApplet *applet, krb5_timestamp expiry)
 
 
 static void
-ka_applet_menu_add_separator_item (GtkWidget *menu)
+ka_applet_tray_icon_menu_add_separator_item (GtkWidget *menu)
 {
     GtkWidget *menu_item;
 
@@ -833,7 +915,8 @@ ka_applet_menu_add_separator_item (GtkWidget *menu)
 
 /* Free all resources and quit */
 static void
-ka_applet_quit_cb (GtkMenuItem *menuitem G_GNUC_UNUSED, gpointer user_data)
+ka_applet_tray_icon_quit_cb (GtkMenuItem *menuitem G_GNUC_UNUSED,
+                             gpointer user_data)
 {
     KaApplet *applet = KA_APPLET (user_data);
 
@@ -842,8 +925,8 @@ ka_applet_quit_cb (GtkMenuItem *menuitem G_GNUC_UNUSED, gpointer user_data)
 
 
 static void
-ka_applet_show_help_cb (GtkMenuItem *menuitem G_GNUC_UNUSED,
-                        gpointer user_data)
+ka_applet_tray_icon_show_help_cb (GtkMenuItem *menuitem G_GNUC_UNUSED,
+                                  gpointer user_data)
 {
     KaApplet *applet = KA_APPLET (user_data);
 
@@ -853,8 +936,8 @@ ka_applet_show_help_cb (GtkMenuItem *menuitem G_GNUC_UNUSED,
 
 
 static void
-ka_applet_destroy_ccache_cb (GtkMenuItem *menuitem G_GNUC_UNUSED,
-                             gpointer user_data)
+ka_applet_tray_icon_destroy_ccache_cb (GtkMenuItem *menuitem G_GNUC_UNUSED,
+                                       gpointer user_data)
 {
     KaApplet *applet = KA_APPLET (user_data);
 
@@ -862,8 +945,8 @@ ka_applet_destroy_ccache_cb (GtkMenuItem *menuitem G_GNUC_UNUSED,
 }
 
 static void
-ka_applet_show_tickets_cb (GtkMenuItem *menuitem G_GNUC_UNUSED,
-                           gpointer user_data)
+ka_applet_tray_icon_show_tickets_cb (GtkMenuItem *menuitem G_GNUC_UNUSED,
+                                     gpointer user_data)
 {
     ka_main_window_show (KA_APPLET(user_data));
 }
@@ -883,31 +966,32 @@ ka_applet_create_context_menu (KaApplet *applet)
     menu_item =
         gtk_image_menu_item_new_with_mnemonic (_("Remove Credentials _Cache"));
     g_signal_connect (G_OBJECT (menu_item), "activate",
-                      G_CALLBACK (ka_applet_destroy_ccache_cb), applet);
+                      G_CALLBACK (ka_applet_tray_icon_destroy_ccache_cb),
+                      applet);
     image = gtk_image_new_from_stock (GTK_STOCK_CANCEL, GTK_ICON_SIZE_MENU);
     gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menu_item), image);
     gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
 
-    ka_applet_menu_add_separator_item (menu);
+    ka_applet_tray_icon_menu_add_separator_item (menu);
 
     /* Ticket dialog */
     menu_item = gtk_image_menu_item_new_with_mnemonic (_("_List Tickets"));
     g_signal_connect (G_OBJECT (menu_item), "activate",
-                      G_CALLBACK (ka_applet_show_tickets_cb), applet);
+                      G_CALLBACK (ka_applet_tray_icon_show_tickets_cb), applet);
     gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
 
     /* Help item */
     menu_item = gtk_image_menu_item_new_from_stock (GTK_STOCK_HELP, NULL);
     g_signal_connect (G_OBJECT (menu_item), "activate",
-                      G_CALLBACK (ka_applet_show_help_cb), applet);
+                      G_CALLBACK (ka_applet_tray_icon_show_help_cb), applet);
     gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
 
-    ka_applet_menu_add_separator_item (menu);
+    ka_applet_tray_icon_menu_add_separator_item (menu);
 
     /* Quit */
     menu_item = gtk_image_menu_item_new_from_stock (GTK_STOCK_QUIT, NULL);
     g_signal_connect (G_OBJECT (menu_item), "activate",
-                      G_CALLBACK (ka_applet_quit_cb), applet);
+                      G_CALLBACK (ka_applet_tray_icon_quit_cb), applet);
     gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
 
     gtk_widget_show_all (menu);
diff --git a/src/ka-main-window.c b/src/ka-main-window.c
index 807512d..efcec63 100644
--- a/src/ka-main-window.c
+++ b/src/ka-main-window.c
@@ -31,7 +31,7 @@
 #include "ka-preferences.h"
 
 static GtkListStore *tickets;
-static GtkWindow *main_window;
+static GtkApplicationWindow *main_window;
 
 static void
 ccache_changed_cb (KaApplet* applet,
@@ -44,58 +44,8 @@ ccache_changed_cb (KaApplet* applet,
     ka_get_service_tickets (tickets, !conf_tickets);
 }
 
-static void
-menuitem_close_cb (GtkMenuItem *menuitem G_GNUC_UNUSED,
-                   gpointer user_data G_GNUC_UNUSED)
-{
-    ka_main_window_hide();
-}
-
-static void
-menuitem_preferences_cb (GtkMenuItem *menuitem G_GNUC_UNUSED,
-                         gpointer user_data G_GNUC_UNUSED)
-{
-    ka_preferences_window_show (main_window);
-}
 
-static void
-menuitem_about_cb (GtkMenuItem *menuitem G_GNUC_UNUSED,
-                   gpointer user_data G_GNUC_UNUSED)
-{
-    ka_show_about ();
-}
-
-static void
-menuitem_help_contents_cb (GtkMenuItem *menuitem G_GNUC_UNUSED,
-                   gpointer user_data G_GNUC_UNUSED)
-{
-    ka_show_help (gtk_window_get_screen (main_window), NULL, NULL);
-}
-
-
-static void
-ka_main_window_create_menu (KaApplet *applet, GtkBuilder *xml)
-{
-    GtkMenuItem *item;
-
-#define CONNECT_MENU(name)                                              \
-    item = GTK_MENU_ITEM (gtk_builder_get_object (xml,                  \
-                                                  "menuitem_" #name));  \
-    g_signal_connect (item,                                             \
-                      "activate",                                       \
-                      G_CALLBACK(menuitem_ ##name## _cb),               \
-                      applet)
-
-    CONNECT_MENU(close);
-    CONNECT_MENU(preferences);
-    CONNECT_MENU(about);
-    CONNECT_MENU(help_contents);
-
-#undef CONNECT_MENU  
-}
-
-
-GtkWindow *
+GtkApplicationWindow *
 ka_main_window_create (KaApplet *applet, GtkBuilder *xml)
 {
     GtkCellRenderer *text_renderer, *toggle_renderer;
@@ -109,7 +59,10 @@ ka_main_window_create (KaApplet *applet, GtkBuilder *xml)
                                   G_TYPE_BOOLEAN, G_TYPE_BOOLEAN);
 
     main_window =
-        GTK_WINDOW (gtk_builder_get_object (xml, "krb5_main_window"));
+        GTK_APPLICATION_WINDOW (gtk_builder_get_object (xml,
+                                                        "krb5_main_window"));
+    g_object_set(main_window, "application", applet, NULL);
+
     tickets_view =
         GTK_TREE_VIEW (gtk_builder_get_object (xml, "krb5_tickets_treeview"));
     gtk_tree_view_set_model (GTK_TREE_VIEW (tickets_view),
@@ -162,7 +115,6 @@ ka_main_window_create (KaApplet *applet, GtkBuilder *xml)
                       G_CALLBACK(ccache_changed_cb),
                       NULL);
 
-    ka_main_window_create_menu(applet, xml);
     return main_window;
 }
 
@@ -173,10 +125,10 @@ ka_main_window_show (KaApplet *applet)
 
     g_object_get(applet, KA_PROP_NAME_CONF_TICKETS, &conf_tickets, NULL);
     if (ka_get_service_tickets (tickets, !conf_tickets)) {
-        gtk_window_present (main_window);
+        gtk_window_present (GTK_WINDOW(main_window));
     } else {
         GtkWidget *message_dialog;
-        
+
         message_dialog = gtk_message_dialog_new (NULL,
                                                  GTK_DIALOG_DESTROY_WITH_PARENT,
                                                  GTK_MESSAGE_ERROR,
@@ -184,7 +136,7 @@ ka_main_window_show (KaApplet *applet)
                                                  _
                                                  ("Error displaying service ticket information"));
         gtk_window_set_resizable (GTK_WINDOW (message_dialog), FALSE);
-        
+
         g_signal_connect (message_dialog, "response",
                           G_CALLBACK (gtk_widget_destroy), NULL);
         gtk_widget_show (message_dialog);
diff --git a/src/ka-main-window.h b/src/ka-main-window.h
index 484a850..ee1ea46 100644
--- a/src/ka-main-window.h
+++ b/src/ka-main-window.h
@@ -34,7 +34,7 @@ enum ticket_columns {
 };
 
 
-GtkWindow *ka_main_window_create (KaApplet *applet, GtkBuilder *xml);
+GtkApplicationWindow *ka_main_window_create (KaApplet *applet, GtkBuilder *xml);
 void ka_main_window_show (KaApplet *applet);
 void ka_main_window_hide (void);
 
diff --git a/src/ka-preferences.c b/src/ka-preferences.c
index 204c6a6..987ef5c 100644
--- a/src/ka-preferences.c
+++ b/src/ka-preferences.c
@@ -513,10 +513,13 @@ ka_preferences_window_create (KaApplet *applet,
 }
 
 void
-ka_preferences_window_show (GtkWindow *main_window)
+ka_preferences_window_show (KaApplet *applet)
 {
-    if (main_window)
-        gtk_window_set_transient_for (GTK_WINDOW(prefs.dialog), main_window);
+    GtkWindow *parent = ka_applet_last_focused_window (applet);
+
+    if (parent)
+        gtk_window_set_transient_for (GTK_WINDOW(prefs.dialog),
+                                      GTK_WINDOW(parent));
     gtk_window_present (GTK_WINDOW(prefs.dialog));
     gtk_dialog_run (GTK_DIALOG (prefs.dialog));
     gtk_widget_hide (prefs.dialog);
diff --git a/src/ka-preferences.h b/src/ka-preferences.h
index d7facb6..6f767c5 100644
--- a/src/ka-preferences.h
+++ b/src/ka-preferences.h
@@ -11,7 +11,7 @@ G_BEGIN_DECLS
 
 void ka_preferences_window_create (KaApplet *applet, GtkBuilder *xml);
 void ka_preferences_window_destroy (void);
-void ka_preferences_window_show (GtkWindow *main_window);
+void ka_preferences_window_show (KaApplet *applet);
 
 
 G_END_DECLS
diff --git a/src/ka-tools.c b/src/ka-tools.c
index 227324a..18f9ba8 100644
--- a/src/ka-tools.c
+++ b/src/ka-tools.c
@@ -58,8 +58,10 @@ ka_show_help (GdkScreen* screen, const char* chapter, GtkWindow* window)
 
 
 void 
-ka_show_about ()
+ka_show_about (KaApplet *applet)
 {
+    GtkWindow *parent = ka_applet_last_focused_window (applet);
+
     const gchar *authors[] = {
         "Christopher Aillon <caillon redhat com>",
         "Jonathan Blandford <jrb redhat com>",
@@ -68,7 +70,7 @@ ka_show_about ()
         NULL
     };
 
-    gtk_show_about_dialog (NULL,
+    gtk_show_about_dialog (parent,
                            "authors", authors,
                            "version", VERSION,
                            "logo-icon-name", "krb-valid-ticket",
diff --git a/src/ka-tools.h b/src/ka-tools.h
index 4fef541..18ddf45 100644
--- a/src/ka-tools.h
+++ b/src/ka-tools.h
@@ -22,11 +22,12 @@
 #define KA_TOOLS
 
 #include <gtk/gtk.h>
+#include <ka-applet-priv.h>
 
 G_BEGIN_DECLS
 
 void ka_show_help (GdkScreen* screen, const char* section, GtkWindow* window);
-void ka_show_about (void);
+void ka_show_about (KaApplet *applet);
 
 G_END_DECLS
 
diff --git a/src/krb5-auth-dialog.ui b/src/krb5-auth-dialog.ui
index 946a3d7..3bbcdbf 100644
--- a/src/krb5-auth-dialog.ui
+++ b/src/krb5-auth-dialog.ui
@@ -1,6 +1,30 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <interface>
   <!-- interface-requires gtk+ 3.0 -->
+  <menu id="app-menu">
+    <section>
+      <item>
+        <attribute name="action">app.preferences</attribute>
+        <attribute name="label" translatable="yes">Prefere_nces</attribute>
+      </item>
+    </section>
+    <section>
+      <item>
+        <attribute name="action">app.about</attribute>
+        <attribute name="label" translatable="yes">_About</attribute>
+      </item>
+      <item>
+        <attribute name="action">app.help</attribute>
+        <attribute name="label" translatable="yes">_Help</attribute>
+      </item>
+    </section>
+    <section>
+      <item>
+        <attribute name="action">app.quit</attribute>
+        <attribute name="label" translatable="yes">_Quit</attribute>
+      </item>
+    </section>
+  </menu>
   <object class="GtkAdjustment" id="adjustment1">
     <property name="upper">100</property>
     <property name="step_increment">1</property>
@@ -179,7 +203,7 @@
       <action-widget response="-5">krb5_renew_button</action-widget>
     </action-widgets>
   </object>
-  <object class="GtkWindow" id="krb5_main_window">
+  <object class="GtkApplicationWindow" id="krb5_main_window">
     <property name="can_focus">False</property>
     <property name="title" translatable="yes">Service Tickets</property>
     <signal name="delete-event" handler="ka_main_window_hide" swapped="no"/>
@@ -188,95 +212,6 @@
         <property name="visible">True</property>
         <property name="can_focus">False</property>
         <child>
-          <object class="GtkMenuBar" id="menubar1">
-            <property name="visible">True</property>
-            <property name="can_focus">False</property>
-            <child>
-              <object class="GtkMenuItem" id="menuitem1">
-                <property name="visible">True</property>
-                <property name="can_focus">False</property>
-                <property name="label" translatable="yes">_File</property>
-                <property name="use_underline">True</property>
-                <child type="submenu">
-                  <object class="GtkMenu" id="menu1">
-                    <property name="visible">True</property>
-                    <property name="can_focus">False</property>
-                    <child>
-                      <object class="GtkImageMenuItem" id="menuitem_close">
-                        <property name="label">gtk-close</property>
-                        <property name="visible">True</property>
-                        <property name="can_focus">False</property>
-                        <property name="use_underline">True</property>
-                        <property name="use_stock">True</property>
-                      </object>
-                    </child>
-                  </object>
-                </child>
-              </object>
-            </child>
-            <child>
-              <object class="GtkMenuItem" id="menuitem2">
-                <property name="visible">True</property>
-                <property name="can_focus">False</property>
-                <property name="label" translatable="yes">_Edit</property>
-                <property name="use_underline">True</property>
-                <child type="submenu">
-                  <object class="GtkMenu" id="menu2">
-                    <property name="visible">True</property>
-                    <property name="can_focus">False</property>
-                    <child>
-                      <object class="GtkImageMenuItem" id="menuitem_preferences">
-                        <property name="label">gtk-preferences</property>
-                        <property name="visible">True</property>
-                        <property name="can_focus">False</property>
-                        <property name="use_underline">True</property>
-                        <property name="use_stock">True</property>
-                      </object>
-                    </child>
-                  </object>
-                </child>
-              </object>
-            </child>
-            <child>
-              <object class="GtkMenuItem" id="menuitem4">
-                <property name="visible">True</property>
-                <property name="can_focus">False</property>
-                <property name="label" translatable="yes">_Help</property>
-                <property name="use_underline">True</property>
-                <child type="submenu">
-                  <object class="GtkMenu" id="menu3">
-                    <property name="visible">True</property>
-                    <property name="can_focus">False</property>
-                    <child>
-                      <object class="GtkImageMenuItem" id="menuitem_help_contents">
-                        <property name="label">gtk-help</property>
-                        <property name="visible">True</property>
-                        <property name="can_focus">False</property>
-                        <property name="use_underline">True</property>
-                        <property name="use_stock">True</property>
-                      </object>
-                    </child>
-                    <child>
-                      <object class="GtkImageMenuItem" id="menuitem_about">
-                        <property name="label">gtk-about</property>
-                        <property name="visible">True</property>
-                        <property name="can_focus">False</property>
-                        <property name="use_underline">True</property>
-                        <property name="use_stock">True</property>
-                      </object>
-                    </child>
-                  </object>
-                </child>
-              </object>
-            </child>
-          </object>
-          <packing>
-            <property name="expand">False</property>
-            <property name="fill">True</property>
-            <property name="position">0</property>
-          </packing>
-        </child>
-        <child>
           <object class="GtkTreeView" id="krb5_tickets_treeview">
             <property name="visible">True</property>
             <property name="can_focus">True</property>


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