[gtk+/wip/csoriano/networks: 2/4] gtkplacesview: add networks in network:///



commit 8347a293e7857e03c5e77a98f0883de57a8b4eab
Author: Carlos Soriano <csoriano gnome org>
Date:   Tue Aug 18 17:58:41 2015 +0200

    gtkplacesview: add networks in network:///
    
    Previously we had a network item in the sidebar, which now
    is replaced by the network section on other-locations view.
    However we were not exposing the networks in network:///.
    
    Fetch them and add them in the network section of other-locations
    view.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=753786

 gtk/gtkplacesview.c |  169 +++++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 156 insertions(+), 13 deletions(-)
---
diff --git a/gtk/gtkplacesview.c b/gtk/gtkplacesview.c
index e593d3d..1b10f12 100644
--- a/gtk/gtkplacesview.c
+++ b/gtk/gtkplacesview.c
@@ -73,11 +73,15 @@ struct _GtkPlacesViewPrivate
   GtkEntryCompletion            *address_entry_completion;
   GtkListStore                  *completion_store;
 
+  GList                         *detected_networks;
+  GCancellable                  *networks_fetching_cancellable;
+
   guint                          local_only : 1;
   guint                          should_open_location : 1;
   guint                          should_pulse_entry : 1;
   guint                          entry_pulse_timeout_id;
   guint                          connecting_to_server : 1;
+  guint                          fetching_networks : 1;
 };
 
 static void        mount_volume                                  (GtkPlacesView *view,
@@ -378,12 +382,14 @@ gtk_places_view_finalize (GObject *object)
     g_source_remove (priv->entry_pulse_timeout_id);
 
   g_cancellable_cancel (priv->cancellable);
+  g_cancellable_cancel (priv->networks_fetching_cancellable);
 
   g_clear_pointer (&priv->search_query, g_free);
   g_clear_object (&priv->server_list_file);
   g_clear_object (&priv->server_list_monitor);
   g_clear_object (&priv->volume_monitor);
   g_clear_object (&priv->cancellable);
+  g_clear_object (&priv->networks_fetching_cancellable);
 
   G_OBJECT_CLASS (gtk_places_view_parent_class)->finalize (object);
 }
@@ -784,28 +790,154 @@ add_drive (GtkPlacesView *view,
 }
 
 static void
-add_computer (GtkPlacesView *view)
+add_file (GtkPlacesView *view,
+          GFile         *file,
+          GIcon         *icon,
+          const gchar   *display_name,
+          const gchar   *path,
+          gboolean       is_network)
 {
   GtkWidget *row;
-  GIcon *icon;
-  GFile *file;
-
-  file = g_file_new_for_path ("/");
-  icon = g_themed_icon_new_with_default_fallbacks ("drive-harddisk");
-
   row = g_object_new (GTK_TYPE_PLACES_VIEW_ROW,
                       "icon", icon,
-                      "name", _("Computer"),
-                      "path", "/",
+                      "name", display_name,
+                      "path", path,
                       "volume", NULL,
                       "mount", NULL,
                       "file", file,
+                      "is_network", is_network,
                       NULL);
 
-  insert_row (view, row, FALSE);
+  insert_row (view, row, is_network);
+}
+
+static void
+populate_networks (GtkPlacesView   *view,
+                   GFileEnumerator *enumerator)
+{
+  GList *l;
+  GtkPlacesViewPrivate *priv;
+  GFile *file;
+  GFile *activatable_file;
+  gchar *uri;
+  GFileType type;
+  GIcon *icon;
+  gchar *display_name;
+
+
+  priv = gtk_places_view_get_instance_private (view);
+
+  for (l = priv->detected_networks; l != NULL; l = l->next)
+    {
+      file = g_file_enumerator_get_child (enumerator, l->data);
+      type = g_file_info_get_file_type (l->data);
+      if (type == G_FILE_TYPE_SHORTCUT || type == G_FILE_TYPE_MOUNTABLE)
+        uri = g_file_info_get_attribute_as_string (l->data, G_FILE_ATTRIBUTE_STANDARD_TARGET_URI);
+      else
+        uri = g_file_get_uri (file);
+      activatable_file = g_file_new_for_uri (uri);
+      display_name = g_file_info_get_attribute_as_string (l->data, G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME);
+      icon = g_file_info_get_icon (l->data);
 
-  g_object_unref (icon);
-  g_object_unref (file);
+      add_file (view, activatable_file, icon, display_name, NULL, TRUE);
+
+      g_free (uri);
+      g_free (display_name);
+      g_clear_object (&file);
+      g_clear_object (&activatable_file);
+    }
+
+  g_clear_object (&enumerator);
+}
+
+static void
+network_enumeration_next_files_finished (GObject      *source_object,
+                                         GAsyncResult *res,
+                                         gpointer      user_data)
+{
+  GtkPlacesViewPrivate *priv;
+  GtkPlacesView *view;
+  GError *error;
+
+  view = GTK_PLACES_VIEW (user_data);
+  priv = gtk_places_view_get_instance_private (view);
+  error = NULL;
+
+  /* clean previous fetched networks */
+  g_list_free_full (priv->detected_networks, g_object_unref);
+  priv->fetching_networks = FALSE;
+
+  priv->detected_networks = g_file_enumerator_next_files_finish (G_FILE_ENUMERATOR (source_object),
+                                                                 res, &error);
+  if (error)
+    {
+      if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
+        g_warning ("Failed to fetch network locations: %s", error->message);
+
+      g_clear_error (&error);
+      g_clear_object (&source_object);
+    }
+  else
+    {
+      populate_networks (view, G_FILE_ENUMERATOR (source_object));
+    }
+}
+
+static void
+network_enumeration_finished (GObject      *source_object,
+                              GAsyncResult *res,
+                              gpointer      user_data)
+{
+  GtkPlacesViewPrivate *priv;
+  GFileEnumerator *enumerator;
+  GtkPlacesView *view;
+  GError *error;
+
+  view = GTK_PLACES_VIEW (user_data);
+  priv = gtk_places_view_get_instance_private (view);
+  error = NULL;
+  enumerator = g_file_enumerate_children_finish (G_FILE (source_object), res, &error);
+
+  if (error)
+    {
+      if (error->code != G_IO_ERROR_CANCELLED)
+        g_warning ("Failed to fetch network locations: %s", error->message);
+
+      g_clear_error (&error);
+    }
+  else
+    {
+      g_file_enumerator_next_files_async (enumerator,
+                                          G_MAXINT32,
+                                          G_PRIORITY_DEFAULT,
+                                          priv->networks_fetching_cancellable,
+                                          network_enumeration_next_files_finished,
+                                          user_data);
+    }
+}
+
+static void
+fetch_networks (GtkPlacesView *view)
+{
+  GtkPlacesViewPrivate *priv;
+  GFile *network_file;
+
+  priv = gtk_places_view_get_instance_private (view);
+  network_file = g_file_new_for_uri ("network:///");
+
+  g_cancellable_cancel (priv->networks_fetching_cancellable);
+  g_clear_object (&priv->networks_fetching_cancellable);
+  priv->networks_fetching_cancellable = g_cancellable_new ();
+
+  g_file_enumerate_children_async (network_file,
+                                   
"standard::type,standard::target-uri,standard::name,standard::display-name,standard::icon",
+                                   G_FILE_QUERY_INFO_NONE,
+                                   G_PRIORITY_DEFAULT,
+                                   priv->networks_fetching_cancellable,
+                                   network_enumeration_finished,
+                                   view);
+
+  g_clear_object (&network_file);
 }
 
 static void
@@ -817,6 +949,8 @@ update_places (GtkPlacesView *view)
   GList *volumes;
   GList *drives;
   GList *l;
+  GIcon *icon;
+  GFile *file;
 
   priv = gtk_places_view_get_instance_private (view);
 
@@ -825,7 +959,13 @@ update_places (GtkPlacesView *view)
   g_list_free_full (children, (GDestroyNotify) gtk_widget_destroy);
 
   /* Add "Computer" row */
-  add_computer (view);
+  file = g_file_new_for_path ("/");
+  icon = g_themed_icon_new_with_default_fallbacks ("drive-harddisk");
+
+  add_file (view, file, icon, _("Computer"), "/", FALSE);
+
+  g_clear_object (&file);
+  g_clear_object (&icon);
 
   /* Add currently connected drives */
   drives = g_volume_monitor_get_connected_drives (priv->volume_monitor);
@@ -889,6 +1029,9 @@ update_places (GtkPlacesView *view)
   /* load saved servers */
   populate_servers (view);
 
+  /* fetch networks and add them asynchronously */
+  fetch_networks (view);
+
   update_view_mode (view);
 }
 


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