[gnome-disk-utility/udisks2-port] Rework device tree headers and ensure something is always selected



commit 831aec234e4d06028ec3a1a334b121c3b00ef1f1
Author: David Zeuthen <davidz redhat com>
Date:   Mon Aug 15 05:07:27 2011 -0400

    Rework device tree headers and ensure something is always selected
    
    Signed-off-by: David Zeuthen <davidz redhat com>

 data/ui/palimpsest.ui               |    1 +
 src/palimpsest/gdudevicetreemodel.c |   54 ++++++++++++++++++++++++++---
 src/palimpsest/gduvolumegrid.c      |    2 +-
 src/palimpsest/gduwindow.c          |   64 +++++++++++++++++++++++++++++++----
 4 files changed, 108 insertions(+), 13 deletions(-)
---
diff --git a/data/ui/palimpsest.ui b/data/ui/palimpsest.ui
index 19c1e42..e74c107 100644
--- a/data/ui/palimpsest.ui
+++ b/data/ui/palimpsest.ui
@@ -1013,6 +1013,7 @@
                   <object class="GtkTreeView" id="device-tree-treeview">
                     <property name="visible">True</property>
                     <property name="can_focus">True</property>
+                    <property name="headers_visible">False</property>
                     <property name="headers_clickable">False</property>
                     <property name="show_expanders">False</property>
                     <property name="level_indentation">12</property>
diff --git a/src/palimpsest/gdudevicetreemodel.c b/src/palimpsest/gdudevicetreemodel.c
index 2f168de..bc05c46 100644
--- a/src/palimpsest/gdudevicetreemodel.c
+++ b/src/palimpsest/gdudevicetreemodel.c
@@ -33,6 +33,8 @@ struct _GduDeviceTreeModel
   UDisksClient *client;
 
   GList *current_drives;
+  GtkTreeIter drive_iter;
+  gboolean drive_iter_valid;
 
   GList *current_blocks;
   GtkTreeIter block_iter;
@@ -426,6 +428,42 @@ gdu_device_tree_model_get_client (GduDeviceTreeModel *model)
 
 /* ---------------------------------------------------------------------------------------------------- */
 
+static GtkTreeIter *
+get_drive_header_iter (GduDeviceTreeModel *model)
+{
+  gchar *s;
+
+  if (model->drive_iter_valid)
+    goto out;
+
+  s = g_strdup_printf ("<small><span foreground=\"#555555\">%s</span></small>",
+                       _("Disk Drives"));
+  gtk_tree_store_insert_with_values (GTK_TREE_STORE (model),
+                                     &model->drive_iter,
+                                     NULL, /* GtkTreeIter *parent */
+                                     0,
+                                     GDU_DEVICE_TREE_MODEL_COLUMN_IS_HEADING, TRUE,
+                                     GDU_DEVICE_TREE_MODEL_COLUMN_HEADING_TEXT, s,
+                                     GDU_DEVICE_TREE_MODEL_COLUMN_SORT_KEY, "00_drives_0",
+                                     -1);
+  g_free (s);
+
+  model->drive_iter_valid = TRUE;
+
+ out:
+  return &model->drive_iter;
+}
+
+static void
+nuke_drive_header (GduDeviceTreeModel *model)
+{
+  if (model->drive_iter_valid)
+    {
+      gtk_tree_store_remove (GTK_TREE_STORE (model), &model->drive_iter);
+      model->drive_iter_valid = FALSE;
+    }
+}
+
 static void
 add_drive (GduDeviceTreeModel *model,
            UDisksObject       *object,
@@ -491,8 +529,9 @@ update_drive (GduDeviceTreeModel *model,
                        "<small><span foreground=\"#555555\">%s</span></small>",
                        description,
                        name);
-  sort_key = g_strdup_printf ("00_drives_%s",
-                              g_dbus_object_get_object_path (G_DBUS_OBJECT (object))); /* for now */
+  /* for now */
+  sort_key = g_strdup_printf ("00_drives_1_%s",
+                              g_dbus_object_get_object_path (G_DBUS_OBJECT (object)));
 
   gtk_tree_store_set (GTK_TREE_STORE (model),
                       &iter,
@@ -559,7 +598,7 @@ update_drives (GduDeviceTreeModel *model)
     {
       UDisksObject *object = UDISKS_OBJECT (l->data);
       model->current_drives = g_list_prepend (model->current_drives, g_object_ref (object));
-      add_drive (model, object, NULL);
+      add_drive (model, object, get_drive_header_iter (model));
     }
 
   for (l = model->current_drives; l != NULL; l = l->next)
@@ -568,6 +607,9 @@ update_drives (GduDeviceTreeModel *model)
       update_drive (model, object);
     }
 
+  if (g_list_length (model->current_drives) == 0)
+    nuke_drive_header (model);
+
   g_list_free (added_drives);
   g_list_free (removed_drives);
   g_list_foreach (drives, (GFunc) g_object_unref, NULL);
@@ -595,7 +637,7 @@ get_block_header_iter (GduDeviceTreeModel *model)
                                      0,
                                      GDU_DEVICE_TREE_MODEL_COLUMN_IS_HEADING, TRUE,
                                      GDU_DEVICE_TREE_MODEL_COLUMN_HEADING_TEXT, s,
-                                     GDU_DEVICE_TREE_MODEL_COLUMN_SORT_KEY, "01_block",
+                                     GDU_DEVICE_TREE_MODEL_COLUMN_SORT_KEY, "01_block_0",
                                      -1);
   g_free (s);
 
@@ -716,7 +758,9 @@ update_block (GduDeviceTreeModel  *model,
                            preferred_device);
     }
 
-  sort_key = g_strdup (g_dbus_object_get_object_path (G_DBUS_OBJECT (object))); /* for now */
+  /* for now */
+  sort_key = g_strdup_printf ("01_block_1_%s",
+                              g_dbus_object_get_object_path (G_DBUS_OBJECT (object)));
 
   gtk_tree_store_set (GTK_TREE_STORE (model),
                       &iter,
diff --git a/src/palimpsest/gduvolumegrid.c b/src/palimpsest/gduvolumegrid.c
index 137e883..d1936fc 100644
--- a/src/palimpsest/gduvolumegrid.c
+++ b/src/palimpsest/gduvolumegrid.c
@@ -553,7 +553,7 @@ gdu_volume_grid_get_preferred_height (GtkWidget *widget,
                                       gint      *minimal_height,
                                       gint      *natural_height)
 {
-  *minimal_height = *natural_height = 100;
+  *minimal_height = *natural_height = 120;
 }
 
 static void
diff --git a/src/palimpsest/gduwindow.c b/src/palimpsest/gduwindow.c
index d9569bc..1517e17 100644
--- a/src/palimpsest/gduwindow.c
+++ b/src/palimpsest/gduwindow.c
@@ -238,6 +238,10 @@ gdu_window_finalize (GObject *object)
   GduWindow *window = GDU_WINDOW (object);
   GDBusObjectManager *object_manager;
 
+  gtk_window_remove_mnemonic (GTK_WINDOW (window),
+                              'd',
+                              window->device_treeview);
+
   object_manager = udisks_client_get_object_manager (window->client);
   g_signal_handlers_disconnect_by_func (object_manager,
                                         G_CALLBACK (on_object_added),
@@ -346,9 +350,16 @@ set_selected_object (GduWindow    *window,
 
   if (gdu_device_tree_model_get_iter_for_object (window->model, object, &iter))
     {
+      GtkTreePath *path;
       GtkTreeSelection *tree_selection;
       tree_selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (window->device_treeview));
       gtk_tree_selection_select_iter (tree_selection, &iter);
+      path = gtk_tree_model_get_path (GTK_TREE_MODEL (window->model), &iter);
+      gtk_tree_view_set_cursor (GTK_TREE_VIEW (window->device_treeview),
+                                path,
+                                NULL,
+                                FALSE);
+      gtk_tree_path_free (path);
     }
 
   show_flags = SHOW_FLAGS_NONE;
@@ -372,6 +383,35 @@ set_selected_object (GduWindow    *window,
   update_for_show_flags (window, show_flags);
 }
 
+static gboolean
+ensure_something_selected_foreach_cb (GtkTreeModel  *model,
+                                      GtkTreePath   *path,
+                                      GtkTreeIter   *iter,
+                                      gpointer       user_data)
+{
+  UDisksObject **object = user_data;
+  gtk_tree_model_get (model, iter,
+                      GDU_DEVICE_TREE_MODEL_COLUMN_OBJECT, object,
+                      -1);
+  if (*object != NULL)
+    return TRUE;
+  return FALSE;
+}
+
+static void
+ensure_something_selected (GduWindow *window)
+{
+  UDisksObject *object = NULL;
+  gtk_tree_model_foreach (GTK_TREE_MODEL (window->model),
+                          ensure_something_selected_foreach_cb,
+                          &object);
+  if (object != NULL)
+    {
+      set_selected_object (window, object);
+      g_object_unref (object);
+    }
+}
+
 static void
 on_tree_selection_changed (GtkTreeSelection *tree_selection,
                            gpointer          user_data)
@@ -394,6 +434,7 @@ on_tree_selection_changed (GtkTreeSelection *tree_selection,
   else
     {
       set_selected_object (window, NULL);
+      ensure_something_selected (window);
     }
 }
 
@@ -628,6 +669,16 @@ init_css (GduWindow *window)
   ;
 }
 
+
+static gboolean
+on_constructed_in_idle (gpointer user_data)
+{
+  GduWindow *window = GDU_WINDOW (user_data);
+  /* select something sensible */
+  ensure_something_selected (window);
+  return FALSE; /* remove source */
+}
+
 static void
 gdu_window_constructed (GObject *object)
 {
@@ -639,7 +690,6 @@ gdu_window_constructed (GObject *object)
   GtkStyleContext *context;
   GDBusObjectManager *object_manager;
   GList *children, *l;
-  GtkWidget *headers_label;
   guint n;
 
   init_css (window);
@@ -699,11 +749,10 @@ gdu_window_constructed (GObject *object)
 
   window->model = gdu_device_tree_model_new (window->client);
 
-  headers_label = gtk_label_new (NULL);
-  gtk_label_set_markup_with_mnemonic (GTK_LABEL (headers_label), _("_Devices"));
-  gtk_misc_set_alignment (GTK_MISC (headers_label), 0.0, 0.5);
-  gtk_label_set_mnemonic_widget (GTK_LABEL (headers_label), window->device_treeview);
-  gtk_widget_show_all (headers_label);
+  /* set up mnemonic */
+  gtk_window_add_mnemonic (GTK_WINDOW (window),
+                           'd',
+                           window->device_treeview);
 
   gtk_tree_view_set_model (GTK_TREE_VIEW (window->device_treeview), GTK_TREE_MODEL (window->model));
   gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (window->model),
@@ -718,7 +767,6 @@ gdu_window_constructed (GObject *object)
                     window);
 
   column = gtk_tree_view_column_new ();
-  gtk_tree_view_column_set_widget (column, headers_label);
   gtk_tree_view_append_column (GTK_TREE_VIEW (window->device_treeview), column);
 
   renderer = gtk_cell_renderer_text_new ();
@@ -858,6 +906,8 @@ gdu_window_constructed (GObject *object)
                     "activate",
                     G_CALLBACK (on_generic_menu_item_edit_partition),
                     window);
+
+  g_idle_add (on_constructed_in_idle, g_object_ref (window));
 }
 
 static void



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