[gnome-logs/wip/gl-journal: 17/17] Refactor category, view and toolbar state changes



commit 4e02dfea06b0135e6e3c5128421b47dcd61a4976
Author: David King <davidk gnome org>
Date:   Fri Oct 4 13:47:50 2013 +0100

    Refactor category, view and toolbar state changes

 data/gl-eventtoolbar.ui |    2 +-
 src/gl-eventtoolbar.c   |   61 +++++++++++++++++++++++++++++-----
 src/gl-eventtoolbar.h   |    2 +
 src/gl-eventview.c      |    6 ++--
 src/gl-window.c         |   83 ++++++++++++++++++++++++++++++++++++++++------
 5 files changed, 130 insertions(+), 24 deletions(-)
---
diff --git a/data/gl-eventtoolbar.ui b/data/gl-eventtoolbar.ui
index c28e80f..f2b8c3b 100644
--- a/data/gl-eventtoolbar.ui
+++ b/data/gl-eventtoolbar.ui
@@ -8,7 +8,7 @@
             <object class="GtkButton" id="back_button">
                 <property name="tooltip-text" translatable="yes">Go back to the event view</property>
                 <property name="valign">center</property>
-                <signal name="clicked" handler="on_gl_event_toolbar_back_button_clicked"/>
+                <signal name="clicked" handler="on_gl_event_toolbar_back_button_clicked" 
object="GlEventToolbar" swapped="yes"/>
                 <style>
                     <class name="image-button"/>
                 </style>
diff --git a/src/gl-eventtoolbar.c b/src/gl-eventtoolbar.c
index 10aec39..13a8aca 100644
--- a/src/gl-eventtoolbar.c
+++ b/src/gl-eventtoolbar.c
@@ -21,6 +21,7 @@
 #include <glib/gi18n.h>
 
 #include "gl-enums.h"
+#include "gl-eventview.h"
 
 enum
 {
@@ -42,8 +43,8 @@ G_DEFINE_TYPE_WITH_PRIVATE (GlEventToolbar, gl_event_toolbar, GTK_TYPE_HEADER_BA
 static GParamSpec *obj_properties[N_PROPERTIES] = { NULL, };
 
 static void
-on_gl_event_toolbar_back_button_clicked (GtkButton *button,
-                                         gpointer user_data)
+on_gl_event_toolbar_back_button_clicked (GlEventToolbar *toolbar,
+                                         GtkButton *button)
 {
     GtkWidget *toplevel;
 
@@ -51,17 +52,30 @@ on_gl_event_toolbar_back_button_clicked (GtkButton *button,
 
     if (gtk_widget_is_toplevel (toplevel))
     {
-        GAction *mode;
+        GAction *category;
         GEnumClass *eclass;
         GEnumValue *evalue;
-
-        mode = g_action_map_lookup_action (G_ACTION_MAP (toplevel), "mode");
-        eclass = g_type_class_ref (GL_TYPE_EVENT_TOOLBAR_MODE);
-        evalue = g_enum_get_value (eclass, GL_EVENT_TOOLBAR_MODE_LIST);
-
-        g_action_activate (mode, g_variant_new_string (evalue->value_nick));
+        GVariant *state;
+
+        category = g_action_map_lookup_action (G_ACTION_MAP (toplevel),
+                                               "category");
+        state = g_action_get_state (category);
+        eclass = g_type_class_ref (GL_TYPE_EVENT_VIEW_FILTER);
+        evalue = g_enum_get_value_by_nick (eclass,
+                                           g_variant_get_string (state, NULL));
+
+        if (evalue->value < GL_EVENT_VIEW_FILTER_APPLICATIONS)
+        {
+            gl_event_toolbar_set_mode (toolbar, GL_EVENT_TOOLBAR_MODE_LIST);
+        }
+        else
+        {
+            gl_event_toolbar_set_mode (toolbar,
+                                       GL_EVENT_TOOLBAR_MODE_PRIORITY_LIST);
+        }
 
         g_type_class_unref (eclass);
+        g_variant_unref (state);
     }
     else
     {
@@ -75,6 +89,7 @@ on_notify_mode (GlEventToolbar *toolbar,
                 gpointer user_data)
 {
     GlEventToolbarPrivate *priv;
+    GtkWidget *toplevel;
 
     priv = gl_event_toolbar_get_instance_private (toolbar);
 
@@ -84,6 +99,11 @@ on_notify_mode (GlEventToolbar *toolbar,
             gtk_widget_hide (priv->back_button);
             gtk_widget_show (priv->search_button);
             break;
+        case GL_EVENT_TOOLBAR_MODE_PRIORITY_LIST:
+            /* TODO: Show priority switcher. */
+            gtk_widget_hide (priv->back_button);
+            gtk_widget_show (priv->search_button);
+            break;
         case GL_EVENT_TOOLBAR_MODE_DETAIL:
             gtk_widget_show (priv->back_button);
             gtk_widget_hide (priv->search_button);
@@ -92,6 +112,29 @@ on_notify_mode (GlEventToolbar *toolbar,
             g_assert_not_reached ();
             break;
     }
+
+    /* Propagate change to GlWindow. */
+    toplevel = gtk_widget_get_toplevel (GTK_WIDGET (toolbar));
+
+    if (gtk_widget_is_toplevel (toplevel))
+    {
+        GAction *mode;
+        GEnumClass *eclass;
+        GEnumValue *evalue;
+
+        mode = g_action_map_lookup_action (G_ACTION_MAP (toplevel),
+                                           "toolbar-mode");
+        eclass = g_type_class_ref (GL_TYPE_EVENT_TOOLBAR_MODE);
+        evalue = g_enum_get_value (eclass, priv->mode);
+
+        g_action_activate (mode, g_variant_new_string (evalue->value_nick));
+
+        g_type_class_unref (eclass);
+    }
+    else
+    {
+        g_error ("Widget not in toplevel window, not switching toolbar mode");
+    }
 }
 
 static void
diff --git a/src/gl-eventtoolbar.h b/src/gl-eventtoolbar.h
index 31f4bb6..98e45b0 100644
--- a/src/gl-eventtoolbar.h
+++ b/src/gl-eventtoolbar.h
@@ -38,6 +38,7 @@ typedef struct
 /*
  * GlEventToolbarMode:
  * @GL_EVENT_TOOLBAR_MODE_LIST:
+ * @GL_EVENT_TOOLBAR_MODE_PRIORITY_LIST:
  * @GL_EVENT_TOOLBAR_MODE_DETAIL:
  *
  * The mode, selected in #GlEventView, to show in the toolbar.
@@ -45,6 +46,7 @@ typedef struct
 typedef enum
 {
     GL_EVENT_TOOLBAR_MODE_LIST,
+    GL_EVENT_TOOLBAR_MODE_PRIORITY_LIST,
     GL_EVENT_TOOLBAR_MODE_DETAIL
 } GlEventToolbarMode;
 
diff --git a/src/gl-eventview.c b/src/gl-eventview.c
index 7add812..d8da2b6 100644
--- a/src/gl-eventview.c
+++ b/src/gl-eventview.c
@@ -180,9 +180,9 @@ on_listbox_row_activated (GtkListBox *listbox,
         GEnumClass *eclass;
         GEnumValue *evalue;
 
-        mode = g_action_map_lookup_action (G_ACTION_MAP (toplevel), "mode");
-        eclass = g_type_class_ref (GL_TYPE_EVENT_TOOLBAR_MODE);
-        evalue = g_enum_get_value (eclass, GL_EVENT_TOOLBAR_MODE_DETAIL);
+        mode = g_action_map_lookup_action (G_ACTION_MAP (toplevel), "view-mode");
+        eclass = g_type_class_ref (GL_TYPE_EVENT_VIEW_MODE);
+        evalue = g_enum_get_value (eclass, GL_EVENT_VIEW_MODE_DETAIL);
 
         g_action_activate (mode, g_variant_new_string (evalue->value_nick));
 
diff --git a/src/gl-window.c b/src/gl-window.c
index 8545ad1..805ec44 100644
--- a/src/gl-window.c
+++ b/src/gl-window.c
@@ -82,27 +82,24 @@ on_category (GSimpleAction *action,
 }
 
 static void
-on_mode (GSimpleAction *action,
-         GVariant *variant,
-         gpointer user_data)
+on_toolbar_mode (GSimpleAction *action,
+                 GVariant *variant,
+                 gpointer user_data)
 {
     GlWindowPrivate *priv;
     const gchar *mode;
-    GAction *search;
-    GlEventToolbar *toolbar;
     GEnumClass *eclass;
     GEnumValue *evalue;
+    GAction *search;
 
     priv = gl_window_get_instance_private (GL_WINDOW (user_data));
     mode = g_variant_get_string (variant, NULL);
-    search = g_action_map_lookup_action (G_ACTION_MAP (user_data), "search");
-    toolbar = GL_EVENT_TOOLBAR (priv->right_toolbar);
     eclass = g_type_class_ref (GL_TYPE_EVENT_TOOLBAR_MODE);
     evalue = g_enum_get_value_by_nick (eclass, mode);
+    search = g_action_map_lookup_action (G_ACTION_MAP (user_data), "search");
 
-    gl_event_toolbar_set_mode (toolbar, evalue->value);
-
-    if (evalue->value == GL_EVENT_TOOLBAR_MODE_LIST)
+    if (evalue->value == GL_EVENT_TOOLBAR_MODE_LIST
+        || evalue->value == GL_EVENT_TOOLBAR_MODE_PRIORITY_LIST)
     {
         /* Switch the event view back to list mode if the back button is
          * clicked. */
@@ -111,6 +108,7 @@ on_mode (GSimpleAction *action,
         view = GL_EVENT_VIEW (priv->events);
 
         gl_event_view_set_mode (view, GL_EVENT_VIEW_MODE_LIST);
+
         g_simple_action_set_enabled (G_SIMPLE_ACTION (search), TRUE);
     }
     else
@@ -124,6 +122,68 @@ on_mode (GSimpleAction *action,
 }
 
 static void
+on_view_mode (GSimpleAction *action,
+              GVariant *variant,
+              gpointer user_data)
+{
+    GlWindowPrivate *priv;
+    const gchar *mode;
+    GlEventToolbar *toolbar;
+    GEnumClass *eclass;
+    GEnumValue *evalue;
+
+    priv = gl_window_get_instance_private (GL_WINDOW (user_data));
+    mode = g_variant_get_string (variant, NULL);
+    toolbar = GL_EVENT_TOOLBAR (priv->right_toolbar);
+    eclass = g_type_class_ref (GL_TYPE_EVENT_VIEW_MODE);
+    evalue = g_enum_get_value_by_nick (eclass, mode);
+
+    switch (evalue->value)
+    {
+        case GL_EVENT_VIEW_MODE_LIST:
+            /* Choose the event toolbar mode based on the category and the
+             * event view mode. */
+            {
+                GAction *category;
+                GEnumClass *category_eclass;
+                GEnumValue *category_evalue;
+                GVariant *state;
+
+                category = g_action_map_lookup_action (G_ACTION_MAP (user_data),
+                                                       "category");
+                state = g_action_get_state (category);
+                category_eclass = g_type_class_ref (GL_TYPE_EVENT_VIEW_FILTER);
+                category_evalue = g_enum_get_value_by_nick (category_eclass,
+                                                            g_variant_get_string (state, NULL));
+
+                if (category_evalue->value < GL_EVENT_VIEW_FILTER_APPLICATIONS)
+                {
+                    gl_event_toolbar_set_mode (toolbar,
+                                               GL_EVENT_TOOLBAR_MODE_LIST);
+                }
+                else
+                {
+                    gl_event_toolbar_set_mode (toolbar,
+                                               GL_EVENT_TOOLBAR_MODE_PRIORITY_LIST);
+                }
+
+                g_type_class_unref (eclass);
+                g_variant_unref (state);
+            }
+
+            break;
+        case GL_EVENT_VIEW_MODE_DETAIL:
+            gl_event_toolbar_set_mode (toolbar,
+                                       GL_EVENT_TOOLBAR_MODE_DETAIL);
+            break;
+    }
+
+    g_simple_action_set_state (action, variant);
+
+    g_type_class_unref (eclass);
+}
+
+static void
 on_search (GSimpleAction *action,
            GVariant *variant,
            gpointer user_data)
@@ -211,7 +271,8 @@ on_provider_parsing_error (GtkCssProvider *provider,
 
 static GActionEntry actions[] = {
     { "category", on_action_radio, "s", "'all'", on_category },
-    { "mode", on_action_radio, "s", "'list'", on_mode },
+    { "view-mode", on_action_radio, "s", "'list'", on_view_mode },
+    { "toolbar-mode", on_action_radio, "s", "'list'", on_toolbar_mode },
     { "search", on_action_toggle, NULL, "false", on_search }
 };
 


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