[gnome-disk-utility/udisks2-port] Show LUNs



commit f4c7a5be2876a75d60e44ffb84c00c51a4553c75
Author: David Zeuthen <davidz redhat com>
Date:   Fri Mar 4 16:31:58 2011 -0500

    Show LUNs
    
    Signed-off-by: David Zeuthen <davidz redhat com>

 data/ui/palimpsest.ui           |   29 ++--
 src/palimpsest/gduapplication.c |  324 ++++++++++++++++++++++++++++++++++++++-
 src/palimpsest/gdutypes.h       |    2 +
 3 files changed, 339 insertions(+), 16 deletions(-)
---
diff --git a/data/ui/palimpsest.ui b/data/ui/palimpsest.ui
index 9bff725..97d57a9 100644
--- a/data/ui/palimpsest.ui
+++ b/data/ui/palimpsest.ui
@@ -11,6 +11,7 @@
       <object class="GtkHBox" id="palimpsest-hbox">
         <property name="visible">True</property>
         <property name="can_focus">False</property>
+        <property name="spacing">12</property>
         <child>
           <object class="GtkVBox" id="lunlist-vbox">
             <property name="visible">True</property>
@@ -26,6 +27,8 @@
                     <property name="visible">True</property>
                     <property name="can_focus">True</property>
                     <property name="headers_visible">False</property>
+                    <property name="show_expanders">False</property>
+                    <property name="level_indentation">12</property>
                     <child internal-child="selection">
                       <object class="GtkTreeSelection" id="lunlist-treeview-selection"/>
                     </child>
@@ -82,7 +85,7 @@
           </packing>
         </child>
         <child>
-          <object class="GtkNotebook" id="notebook1">
+          <object class="GtkNotebook" id="palimpsest-notebook">
             <property name="visible">True</property>
             <property name="can_focus">True</property>
             <child>
@@ -178,18 +181,6 @@
                       </packing>
                     </child>
                     <child>
-                      <placeholder/>
-                    </child>
-                    <child>
-                      <placeholder/>
-                    </child>
-                    <child>
-                      <placeholder/>
-                    </child>
-                    <child>
-                      <placeholder/>
-                    </child>
-                    <child>
                       <object class="GtkLabel" id="lun-serial-number-value-label">
                         <property name="visible">True</property>
                         <property name="can_focus">False</property>
@@ -232,6 +223,18 @@
                         <property name="y_options"></property>
                       </packing>
                     </child>
+                    <child>
+                      <placeholder/>
+                    </child>
+                    <child>
+                      <placeholder/>
+                    </child>
+                    <child>
+                      <placeholder/>
+                    </child>
+                    <child>
+                      <placeholder/>
+                    </child>
                   </object>
                   <packing>
                     <property name="expand">True</property>
diff --git a/src/palimpsest/gduapplication.c b/src/palimpsest/gduapplication.c
index 58b5cc4..574e031 100644
--- a/src/palimpsest/gduapplication.c
+++ b/src/palimpsest/gduapplication.c
@@ -32,6 +32,14 @@ struct _GduApplication
   GtkBuilder *builder;
   GtkWindow *window;
   gboolean running_from_source_tree;
+
+  GDBusProxyManager *proxy_manager;
+
+  GtkTreeStore *lun_tree_store;
+
+  GtkTreeIter das_iter;
+  GtkTreeIter remote_iter;
+  GtkTreeIter other_iter;
 };
 
 typedef struct
@@ -39,6 +47,17 @@ typedef struct
   GtkApplicationClass parent_class;
 } GduApplicationClass;
 
+enum
+{
+  LUN_TREE_COLUMN_SORT_KEY,
+  LUN_TREE_COLUMN_IS_HEADING,
+  LUN_TREE_COLUMN_HEADING_TEXT,
+  LUN_TREE_COLUMN_ICON,
+  LUN_TREE_COLUMN_NAME,
+  LUN_TREE_COLUMN_OBJECT_PROXY,
+  LUN_TREE_N_COLUMNS
+};
+
 G_DEFINE_TYPE (GduApplication, gdu_application, GTK_TYPE_APPLICATION);
 
 static void
@@ -52,8 +71,13 @@ gdu_application_finalize (GObject *object)
   GduApplication *app = GDU_APPLICATION (object);
 
   if (app->builder != NULL)
-    g_object_unref (app->builder);
+    goto out;
+
+  g_object_unref (app->lun_tree_store);
+  g_object_unref (app->proxy_manager);
+  g_object_unref (app->builder);
 
+ out:
   G_OBJECT_CLASS (gdu_application_parent_class)->finalize (object);
 }
 
@@ -74,16 +98,199 @@ gdu_application_local_command_line (GApplication    *_app,
                                                                                  exit_status);
 }
 
+static gboolean
+dont_select_headings (GtkTreeSelection *selection,
+                      GtkTreeModel     *model,
+                      GtkTreePath      *path,
+                      gboolean          selected,
+                      gpointer          data)
+{
+  GtkTreeIter iter;
+  gboolean is_heading;
+
+  gtk_tree_model_get_iter (model,
+                           &iter,
+                           path);
+  gtk_tree_model_get (model,
+                      &iter,
+                      LUN_TREE_COLUMN_IS_HEADING,
+                      &is_heading,
+                      -1);
+
+  return !is_heading;
+}
+
+typedef struct
+{
+  GDBusObjectProxy *object;
+  GtkTreeIter iter;
+  gboolean found;
+} FindIterData;
+
+static gboolean
+find_iter_for_object_proxy_cb (GtkTreeModel  *model,
+                               GtkTreePath   *path,
+                               GtkTreeIter   *iter,
+                               gpointer       user_data)
+{
+  FindIterData *data = user_data;
+  GDBusObjectProxy *iter_object;
+
+  iter_object = NULL;
+
+  gtk_tree_model_get (model,
+                      iter,
+                      LUN_TREE_COLUMN_OBJECT_PROXY, &iter_object,
+                      -1);
+  if (iter_object == NULL)
+    goto out;
+
+  if (iter_object == data->object)
+    {
+      data->iter = *iter;
+      data->found = TRUE;
+      goto out;
+    }
+
+ out:
+  if (iter_object != NULL)
+    g_object_unref (iter_object);
+  return data->found;
+}
+
+static gboolean
+find_iter_for_object_proxy (GduApplication   *app,
+                            GDBusObjectProxy *object,
+                            GtkTreeIter      *out_iter)
+{
+  FindIterData data;
+
+  memset (&data, 0, sizeof (data));
+  data.object = object;
+  data.found = FALSE;
+  gtk_tree_model_foreach (GTK_TREE_MODEL (app->lun_tree_store),
+                          find_iter_for_object_proxy_cb,
+                          &data);
+  if (data.found)
+    {
+      if (out_iter != NULL)
+        *out_iter = data.iter;
+    }
+
+  return data.found;
+}
+
+static void
+add_lun (GduApplication   *app,
+         GDBusObjectProxy *object)
+{
+  UDisksLun *lun;
+  gchar *name;
+  gchar *sort_key;
+  GIcon *icon;
+  const gchar *model;
+  const gchar *vendor;
+
+  lun = UDISKS_PEEK_LUN (object);
+
+  model = udisks_lun_get_model (lun);
+  vendor = udisks_lun_get_vendor (lun);
+  if (strlen (vendor) == 0)
+    name = g_strdup (model);
+  else if (strlen (model) == 0)
+    name = g_strdup (vendor);
+  else
+    name = g_strconcat (vendor, " ", model, NULL);
+
+  icon = g_themed_icon_new ("drive-harddisk"); /* for now */
+  sort_key = g_strdup (name); /* for now */
+
+  gtk_tree_store_insert_with_values (app->lun_tree_store,
+                                     NULL,
+                                     &app->das_iter,
+                                     0,
+                                     LUN_TREE_COLUMN_ICON, icon,
+                                     LUN_TREE_COLUMN_NAME, name,
+                                     LUN_TREE_COLUMN_SORT_KEY, sort_key,
+                                     LUN_TREE_COLUMN_OBJECT_PROXY, object,
+                                     -1);
+  g_object_unref (icon);
+  g_free (sort_key);
+  g_free (name);
+}
+
+static void
+remove_lun (GduApplication   *app,
+            GDBusObjectProxy *object)
+{
+  GtkTreeIter iter;
+
+  if (!find_iter_for_object_proxy (app,
+                                   object,
+                                   &iter))
+    {
+      g_warning ("Unable to find iter for %s",
+                 g_dbus_object_proxy_get_object_path (object));
+      goto out;
+    }
+
+  gtk_tree_store_remove (app->lun_tree_store, &iter);
+
+ out:
+  ;
+}
+
+static void
+on_object_proxy_added (GDBusProxyManager *manager,
+                       GDBusObjectProxy  *object,
+                       gpointer           user_data)
+{
+  GduApplication *app = GDU_APPLICATION (user_data);
+  if (UDISKS_PEEK_LUN (object) != NULL)
+    add_lun (app, object);
+}
+
+static void
+on_object_proxy_removed (GDBusProxyManager *manager,
+                         GDBusObjectProxy  *object,
+                         gpointer           user_data)
+{
+  GduApplication *app = GDU_APPLICATION (user_data);
+  if (UDISKS_PEEK_LUN (object) != NULL)
+    remove_lun (app, object);
+}
+
 static void
 gdu_application_activate (GApplication *_app)
 {
   GduApplication *app = GDU_APPLICATION (_app);
   GError *error;
+  GtkNotebook *notebook;
+  GtkTreeView *tree_view;
+  GtkTreeViewColumn *column;
+  GtkCellRenderer *renderer;
+  GtkTreeSelection *selection;
   const gchar *path;
+  gchar *s;
+  GList *objects;
+  GList *l;
 
   if (app->builder != NULL)
     return;
 
+  error = NULL;
+  app->proxy_manager = udisks_proxy_manager_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
+                                                              G_DBUS_PROXY_MANAGER_FLAGS_NONE,
+                                                              "org.freedesktop.UDisks2",
+                                                              "/org/freedesktop/UDisks2",
+                                                              NULL, /* GCancellable* */
+                                                              &error);
+  if (app->proxy_manager == NULL)
+    {
+      g_error ("Error getting objects from udisks: %s", error->message);
+      g_error_free (error);
+    }
+
   app->builder = gtk_builder_new ();
   error = NULL;
   path = app->running_from_source_tree ? "../../data/ui/palimpsest.ui" :
@@ -96,9 +303,120 @@ gdu_application_activate (GApplication *_app)
       g_error_free (error);
     }
 
-  app->window = GTK_WINDOW (gtk_builder_get_object (app->builder, "palimpsest-window"));
-  gtk_window_set_application (app->window, GTK_APPLICATION (app));
+  app->window = GTK_WINDOW (gdu_application_get_widget (app, "palimpsest-window"));
+  gtk_application_add_window (GTK_APPLICATION (app), app->window);
   gtk_widget_show_all (GTK_WIDGET (app->window));
+
+  notebook = GTK_NOTEBOOK (gdu_application_get_widget (app, "palimpsest-notebook"));
+  gtk_notebook_set_show_tabs (notebook, FALSE);
+  gtk_notebook_set_show_border (notebook, FALSE);
+
+  GtkStyleContext *context;
+  context = gtk_widget_get_style_context (gdu_application_get_widget (app, "lunlist-scrolledwindow"));
+  gtk_style_context_set_junction_sides (context, GTK_JUNCTION_BOTTOM);
+  context = gtk_widget_get_style_context (gdu_application_get_widget (app, "lunlist-add-remove-toolbar"));
+  gtk_style_context_set_junction_sides (context, GTK_JUNCTION_TOP);
+
+  app->lun_tree_store = gtk_tree_store_new (6,
+                                            G_TYPE_STRING,
+                                            G_TYPE_BOOLEAN,
+                                            G_TYPE_STRING,
+                                            G_TYPE_ICON,
+                                            G_TYPE_STRING,
+                                            G_TYPE_DBUS_OBJECT_PROXY);
+  G_STATIC_ASSERT (6 == LUN_TREE_N_COLUMNS);
+
+  tree_view = GTK_TREE_VIEW (gdu_application_get_widget (app, "lunlist-treeview"));
+  gtk_tree_view_set_model (tree_view, GTK_TREE_MODEL (app->lun_tree_store));
+  gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (app->lun_tree_store),
+                                        LUN_TREE_COLUMN_SORT_KEY,
+                                        GTK_SORT_ASCENDING);
+
+  selection = gtk_tree_view_get_selection (tree_view);
+  gtk_tree_selection_set_select_function (selection, dont_select_headings, NULL, NULL);
+
+  s = g_strdup_printf ("<small><span foreground=\"#555555\">%s</span></small>",
+                       _("Direct-Attached Storage"));
+  gtk_tree_store_insert_with_values (app->lun_tree_store,
+                                     &app->das_iter,
+                                     NULL,
+                                     0,
+                                     LUN_TREE_COLUMN_IS_HEADING, TRUE,
+                                     LUN_TREE_COLUMN_HEADING_TEXT, s,
+                                     LUN_TREE_COLUMN_SORT_KEY, "0_das",
+                                     -1);
+  g_free (s);
+  s = g_strdup_printf ("<small><span foreground=\"#555555\">%s</span></small>",
+                       _("Remote Storage"));
+  gtk_tree_store_insert_with_values (app->lun_tree_store,
+                                     &app->remote_iter,
+                                     NULL,
+                                     0,
+                                     LUN_TREE_COLUMN_IS_HEADING, TRUE,
+                                     LUN_TREE_COLUMN_HEADING_TEXT, s,
+                                     LUN_TREE_COLUMN_SORT_KEY, "1_remote",
+                                     -1);
+  g_free (s);
+  s = g_strdup_printf ("<small><span foreground=\"#555555\">%s</span></small>",
+                       _("Other Devices"));
+  gtk_tree_store_insert_with_values (app->lun_tree_store,
+                                     &app->other_iter,
+                                     NULL,
+                                     0,
+                                     LUN_TREE_COLUMN_IS_HEADING, TRUE,
+                                     LUN_TREE_COLUMN_HEADING_TEXT, s,
+                                     LUN_TREE_COLUMN_SORT_KEY, "2_other",
+                                     -1);
+  g_free (s);
+
+  column = gtk_tree_view_column_new ();
+  gtk_tree_view_append_column (tree_view, column);
+
+  renderer = gtk_cell_renderer_text_new ();
+  gtk_tree_view_column_pack_start (column, renderer, FALSE);
+  gtk_tree_view_column_set_attributes (column,
+                                       renderer,
+                                       "markup", LUN_TREE_COLUMN_HEADING_TEXT,
+                                       "visible", LUN_TREE_COLUMN_IS_HEADING,
+                                       NULL);
+
+  renderer = gtk_cell_renderer_pixbuf_new ();
+  g_object_set (G_OBJECT (renderer),
+                "stock-size", GTK_ICON_SIZE_DND,
+                NULL);
+  gtk_tree_view_column_pack_start (column, renderer, FALSE);
+  gtk_tree_view_column_set_attributes (column,
+                                       renderer,
+                                       "gicon", LUN_TREE_COLUMN_ICON,
+                                       NULL);
+  renderer = gtk_cell_renderer_text_new ();
+  gtk_tree_view_column_pack_start (column, renderer, FALSE);
+  gtk_tree_view_column_set_attributes (column,
+                                       renderer,
+                                       "text", LUN_TREE_COLUMN_NAME,
+                                       NULL);
+
+  /* coldplug */
+  objects = g_dbus_proxy_manager_get_all (app->proxy_manager);
+  for (l = objects; l != NULL; l = l->next)
+    {
+      GDBusObjectProxy *object = G_DBUS_OBJECT_PROXY (l->data);
+      on_object_proxy_added (app->proxy_manager, object, app);
+    }
+  g_list_foreach (objects, (GFunc) g_object_unref, NULL);
+  g_list_free (objects);
+
+  g_signal_connect (app->proxy_manager,
+                    "object-proxy-added",
+                    G_CALLBACK (on_object_proxy_added),
+                    app);
+
+  g_signal_connect (app->proxy_manager,
+                    "object-proxy-removed",
+                    G_CALLBACK (on_object_proxy_removed),
+                    app);
+
+  gtk_tree_view_expand_all (tree_view);
 }
 
 static void
diff --git a/src/palimpsest/gdutypes.h b/src/palimpsest/gdutypes.h
index 0f10947..21da58c 100644
--- a/src/palimpsest/gdutypes.h
+++ b/src/palimpsest/gdutypes.h
@@ -24,6 +24,8 @@
 #define __GDU_TYPES_H__
 
 #include <gtk/gtk.h>
+#define UDISKS_API_IS_SUBJECT_TO_CHANGE
+#include <udisks/udisks.h>
 
 G_BEGIN_DECLS
 



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