[Xfce4-commits] <thunar:master> Easy adding bookmarks for remote mounts.

Nick Schermer noreply at xfce.org
Mon Oct 22 20:42:02 CEST 2012


Updating branch refs/heads/master
         to 7012c86e217454141e24f7d819c4a93178a01c1f (commit)
       from 0c5e3059464027ea7116b73f36a05b2965b089e6 (commit)

commit 7012c86e217454141e24f7d819c4a93178a01c1f
Author: Nick Schermer <nick at xfce.org>
Date:   Mon Oct 22 20:19:14 2012 +0200

    Easy adding bookmarks for remote mounts.
    
    Add right-click menu item to add a bookmark in the
    sidepane. Also use the function to generate nice
    remote names.

 thunar/thunar-device.c          |   23 +++++++++++-
 thunar/thunar-shortcuts-model.c |   72 ++++++++++++++++++++++++++++++++------
 thunar/thunar-shortcuts-model.h |    5 ++-
 thunar/thunar-shortcuts-pane.c  |    8 +---
 thunar/thunar-shortcuts-view.c  |   70 ++++++++++++++++++++++++++++++++++++-
 5 files changed, 155 insertions(+), 23 deletions(-)

diff --git a/thunar/thunar-device.c b/thunar/thunar-device.c
index 7f20a62..2697fa8 100644
--- a/thunar/thunar-device.c
+++ b/thunar/thunar-device.c
@@ -26,6 +26,7 @@
 #include <thunar/thunar-device.h>
 #include <thunar/thunar-device-monitor.h>
 #include <thunar/thunar-private.h>
+#include <thunar/thunar-file.h>
 
 
 
@@ -396,12 +397,30 @@ thunar_device_volume_eject_finish (GObject      *object,
 gchar *
 thunar_device_get_name (const ThunarDevice *device)
 {
+  GFile *mount_point;
+  gchar *display_name = NULL;
+
   _thunar_return_val_if_fail (THUNAR_IS_DEVICE (device), NULL);
 
   if (G_IS_VOLUME (device->device))
-    return g_volume_get_name (device->device);
+    {
+      return g_volume_get_name (device->device);
+    }
   else if (G_IS_MOUNT (device->device))
-   return g_mount_get_name (device->device);
+    {
+      /* probably we can make a nicer name for this mount */
+      mount_point = thunar_device_get_root (device);
+      if (mount_point != NULL)
+        {
+          display_name = thunar_g_file_get_display_name_remote (mount_point);
+          g_object_unref (mount_point);
+        }
+
+      if (display_name == NULL)
+        display_name = g_mount_get_name (device->device);
+
+      return display_name;
+    }
   else
     _thunar_assert_not_reached ();
 
diff --git a/thunar/thunar-shortcuts-model.c b/thunar/thunar-shortcuts-model.c
index d795e57..c117227 100644
--- a/thunar/thunar-shortcuts-model.c
+++ b/thunar/thunar-shortcuts-model.c
@@ -564,7 +564,7 @@ thunar_shortcuts_model_get_value (GtkTreeModel *tree_model,
       else if (shortcut->file != NULL)
         g_value_set_static_string (value, thunar_file_get_display_name (shortcut->file));
       else if (shortcut->location != NULL)
-        g_value_take_string (value, thunar_g_file_get_display_name (shortcut->location));
+        g_value_take_string (value, thunar_g_file_get_display_name_remote (shortcut->location));
       else
         g_value_set_static_string (value, "");
       break;
@@ -1617,6 +1617,49 @@ thunar_shortcuts_model_get_default (void)
 
 
 /**
+ * thunar_shortcuts_model_has_bookmark:
+ * @model : a #ThunarShortcutsModel instance.
+ * @file  : a #ThuanrFile instance.
+ *
+ * Returns %TRUE if there is a bookmark (not a mount or volume) with
+ * @file as destination.
+ *
+ * Return value: %TRUE if @file was found, else %FALSE.
+ **/
+gboolean
+thunar_shortcuts_model_has_bookmark (ThunarShortcutsModel *model,
+                                     GFile                *file)
+{
+  GList          *lp;
+  ThunarShortcut *shortcut;
+
+  _thunar_return_val_if_fail (THUNAR_IS_SHORTCUTS_MODEL (model), FALSE);
+  _thunar_return_val_if_fail (G_IS_FILE (file), FALSE);
+
+  for (lp = model->shortcuts; lp != NULL; lp = lp->next)
+    {
+      shortcut = lp->data;
+
+      /* only check bookmarks */
+      if (shortcut->group != THUNAR_SHORTCUT_GROUP_PLACES_BOOKMARKS
+          && shortcut->group != THUNAR_SHORTCUT_GROUP_NETWORK_BOOKMARKS)
+        continue;
+
+      if (shortcut->file != NULL
+          && g_file_equal (thunar_file_get_file (shortcut->file), file))
+        return TRUE;
+
+      if (shortcut->location != NULL
+          && g_file_equal (shortcut->location, file))
+        return TRUE;
+    }
+
+  return FALSE;
+}
+
+
+
+/**
  * thunar_shortcuts_model_iter_for_file:
  * @model : a #ThunarShortcutsModel instance.
  * @file  : a #ThuanrFile instance.
@@ -1736,27 +1779,32 @@ thunar_shortcuts_model_drop_possible (ThunarShortcutsModel *model,
 void
 thunar_shortcuts_model_add (ThunarShortcutsModel *model,
                             GtkTreePath          *dst_path,
-                            ThunarFile           *file)
+                            gpointer              file)
 {
   ThunarShortcut *shortcut;
-  GList           *lp;
+  GFile          *location;
 
   _thunar_return_if_fail (THUNAR_IS_SHORTCUTS_MODEL (model));
-  _thunar_return_if_fail (gtk_tree_path_get_depth (dst_path) > 0);
-  _thunar_return_if_fail (gtk_tree_path_get_indices (dst_path)[0] >= 0);
-  _thunar_return_if_fail (gtk_tree_path_get_indices (dst_path)[0] <= (gint) g_list_length (model->shortcuts));
-  _thunar_return_if_fail (THUNAR_IS_FILE (file));
+  _thunar_return_if_fail (dst_path == NULL || gtk_tree_path_get_depth (dst_path) > 0);
+  _thunar_return_if_fail (dst_path == NULL || gtk_tree_path_get_indices (dst_path)[0] >= 0);
+  _thunar_return_if_fail (dst_path == NULL || gtk_tree_path_get_indices (dst_path)[0] <= (gint) g_list_length (model->shortcuts));
+  _thunar_return_if_fail (THUNAR_IS_FILE (file) || G_IS_FILE (file));
+
+  location = G_IS_FILE (file) ? file : thunar_file_get_file (file);
 
   /* verify that the file is not already in use as shortcut */
-  for (lp = model->shortcuts; lp != NULL; lp = lp->next)
-    if (THUNAR_SHORTCUT (lp->data)->file == file)
-      return;
+  if (thunar_shortcuts_model_has_bookmark (model, location))
+    return;
 
   /* create the new shortcut that will be inserted */
   shortcut = g_slice_new0 (ThunarShortcut);
-  shortcut->file = g_object_ref (G_OBJECT (file));
 
-  if (thunar_file_is_local (file))
+  if (THUNAR_IS_FILE (file) && thunar_file_is_local (file))
+    shortcut->file = g_object_ref (G_OBJECT (file));
+  else
+    shortcut->location = g_object_ref (G_OBJECT (location));
+
+  if (g_file_has_uri_scheme (location, "file"))
     {
       shortcut->group = THUNAR_SHORTCUT_GROUP_PLACES_BOOKMARKS;
     }
diff --git a/thunar/thunar-shortcuts-model.h b/thunar/thunar-shortcuts-model.h
index 4c37459..fce6b68 100644
--- a/thunar/thunar-shortcuts-model.h
+++ b/thunar/thunar-shortcuts-model.h
@@ -98,6 +98,9 @@ GType                  thunar_shortcuts_model_get_type      (void) G_GNUC_CONST;
 
 ThunarShortcutsModel  *thunar_shortcuts_model_get_default   (void);
 
+gboolean               thunar_shortcuts_model_has_bookmark  (ThunarShortcutsModel *model,
+                                                             GFile                *file);
+
 gboolean               thunar_shortcuts_model_iter_for_file (ThunarShortcutsModel *model,
                                                              ThunarFile           *file,
                                                              GtkTreeIter          *iter);
@@ -107,7 +110,7 @@ gboolean               thunar_shortcuts_model_drop_possible (ThunarShortcutsMode
 
 void                   thunar_shortcuts_model_add           (ThunarShortcutsModel *model,
                                                              GtkTreePath          *dst_path,
-                                                             ThunarFile           *file);
+                                                             gpointer              file);
 void                   thunar_shortcuts_model_move          (ThunarShortcutsModel *model,
                                                              GtkTreePath          *src_path,
                                                              GtkTreePath          *dst_path);
diff --git a/thunar/thunar-shortcuts-pane.c b/thunar/thunar-shortcuts-pane.c
index ca32dba..cbc545c 100644
--- a/thunar/thunar-shortcuts-pane.c
+++ b/thunar/thunar-shortcuts-pane.c
@@ -326,7 +326,6 @@ thunar_shortcuts_pane_set_selected_files (ThunarComponent *component,
   ThunarShortcutsPane *shortcuts_pane = THUNAR_SHORTCUTS_PANE (component);
   GtkTreeModel        *model;
   GtkTreeModel        *child_model;
-  GtkTreeIter          iter;
   GtkAction           *action;
   GList               *lp;
   gint                 n;
@@ -353,7 +352,7 @@ thunar_shortcuts_pane_set_selected_files (ThunarComponent *component,
           /* check all selected folders */
           child_model = gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER (model));
           for (lp = selected_files; lp != NULL; lp = lp->next)
-            if (!thunar_shortcuts_model_iter_for_file (THUNAR_SHORTCUTS_MODEL (child_model), lp->data, &iter))
+            if (!thunar_shortcuts_model_has_bookmark (THUNAR_SHORTCUTS_MODEL (child_model), thunar_file_get_file (lp->data)))
               break;
         }
 
@@ -439,7 +438,6 @@ thunar_shortcuts_pane_action_shortcuts_add (GtkAction           *action,
 {
   GtkTreeModel *model;
   GtkTreeModel *child_model;
-  GtkTreePath  *path;
   GList        *lp;
 
   _thunar_return_if_fail (GTK_IS_ACTION (action));
@@ -455,9 +453,7 @@ thunar_shortcuts_pane_action_shortcuts_add (GtkAction           *action,
         if (G_LIKELY (thunar_file_is_directory (lp->data)))
           {
             /* append the folder to the shortcuts model */
-            path = gtk_tree_path_new_from_indices (gtk_tree_model_iter_n_children (child_model, NULL), -1);
-            thunar_shortcuts_model_add (THUNAR_SHORTCUTS_MODEL (child_model), path, lp->data);
-            gtk_tree_path_free (path);
+            thunar_shortcuts_model_add (THUNAR_SHORTCUTS_MODEL (child_model), NULL, lp->data);
           }
 
       /* update the user interface to reflect the new action state */
diff --git a/thunar/thunar-shortcuts-view.c b/thunar/thunar-shortcuts-view.c
index 3d19cae..2d62f78 100644
--- a/thunar/thunar-shortcuts-view.c
+++ b/thunar/thunar-shortcuts-view.c
@@ -45,6 +45,7 @@
 #include <thunar/thunar-shortcuts-model.h>
 #include <thunar/thunar-shortcuts-view.h>
 #include <thunar/thunar-device-monitor.h>
+#include <thunar/thunar-stock.h>
 
 
 
@@ -132,6 +133,7 @@ static void           thunar_shortcuts_view_open                         (Thunar
                                                                           gboolean                  new_window);
 static void           thunar_shortcuts_view_open_in_new_window_clicked   (ThunarShortcutsView      *view);
 static void           thunar_shortcuts_view_empty_trash                  (ThunarShortcutsView      *view);
+static void           thunar_shortcuts_view_create_shortcut              (ThunarShortcutsView      *view);
 static void           thunar_shortcuts_view_eject                        (ThunarShortcutsView      *view);
 static void           thunar_shortcuts_view_mount                        (ThunarShortcutsView      *view);
 static void           thunar_shortcuts_view_unmount                      (ThunarShortcutsView      *view);
@@ -1017,6 +1019,8 @@ thunar_shortcuts_view_context_menu (ThunarShortcutsView *view,
   GList               *actions = NULL, *tmp;
   ThunarShortcutGroup  group;
   gboolean             is_header;
+  GFile               *mount_point;
+  GtkTreeModel        *shortcuts_model;
 
   /* check if this is an item menu or a header menu */
   gtk_tree_model_get (model, iter, THUNAR_SHORTCUTS_MODEL_COLUMN_HEADER, &is_header, -1);
@@ -1085,19 +1089,46 @@ thunar_shortcuts_view_context_menu (ThunarShortcutsView *view,
         g_signal_connect_swapped (G_OBJECT (item), "activate", G_CALLBACK (thunar_shortcuts_view_eject), view);
         break;
 
-      case THUNAR_SHORTCUT_GROUP_DEVICES_MOUNTS:
       case THUNAR_SHORTCUT_GROUP_NETWORK_MOUNTS:
         /* append a menu separator */
         item = gtk_separator_menu_item_new ();
         gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
         gtk_widget_show (item);
 
+        /* get the mount point */
+        mount_point = thunar_device_get_root (device);
+        shortcuts_model = gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER (model));
+
+        /* append the "Disconnect" item */
+        item = gtk_image_menu_item_new_with_mnemonic (_("Create _Shortcut"));
+        gtk_widget_set_sensitive (item, mount_point != NULL && !thunar_shortcuts_model_has_bookmark (THUNAR_SHORTCUTS_MODEL (shortcuts_model), mount_point));
+        g_signal_connect_swapped (G_OBJECT (item), "activate", G_CALLBACK (thunar_shortcuts_view_create_shortcut), view);
+        gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
+        gtk_widget_show (item);
+
+        image = gtk_image_new_from_stock (THUNAR_STOCK_SHORTCUTS, GTK_ICON_SIZE_MENU);
+        gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
+
+        if (mount_point != NULL)
+          g_object_unref (mount_point);
+
+        /* fall-through */
+
+      case THUNAR_SHORTCUT_GROUP_DEVICES_MOUNTS:
+        /* append a menu separator */
+        item = gtk_separator_menu_item_new ();
+        gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
+        gtk_widget_show (item);
+
         /* append the "Disconnect" item */
         item = gtk_image_menu_item_new_with_mnemonic (_("Disconn_ect"));
         gtk_widget_set_sensitive (item, thunar_device_can_eject (device));
         g_signal_connect_swapped (G_OBJECT (item), "activate", G_CALLBACK (thunar_shortcuts_view_eject), view);
         gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
         gtk_widget_show (item);
+
+        image = gtk_image_new_from_stock (GTK_STOCK_DISCONNECT, GTK_ICON_SIZE_MENU);
+        gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
         break;
 
       case THUNAR_SHORTCUT_GROUP_PLACES_TRASH:
@@ -1569,7 +1600,7 @@ thunar_shortcuts_view_poke_location_finish (ThunarBrowser *browser,
     }
   else
     {
-      name = thunar_g_file_get_display_name (location);
+      name = thunar_g_file_get_display_name_remote (location);
       thunar_dialogs_show_error (GTK_WIDGET (browser), error, _("Failed to open \"%s\""), name);
       g_free (name);
     }
@@ -1705,6 +1736,41 @@ thunar_shortcuts_view_empty_trash (ThunarShortcutsView *view)
 
 
 static void
+thunar_shortcuts_view_create_shortcut (ThunarShortcutsView *view)
+{
+  GtkTreeSelection *selection;
+  GtkTreeModel     *model;
+  GtkTreeIter       iter;
+  ThunarDevice     *device;
+  GFile            *mount_point;
+  GtkTreeModel     *shortcuts_model;
+
+  _thunar_return_if_fail (THUNAR_IS_SHORTCUTS_VIEW (view));
+
+  /* determine the selected item */
+  selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (view));
+  if (gtk_tree_selection_get_selected (selection, &model, &iter))
+    {
+      /* determine the device/mount for the shortcut at the given tree iterator */
+      gtk_tree_model_get (model, &iter, THUNAR_SHORTCUTS_MODEL_COLUMN_DEVICE, &device, -1);
+      _thunar_return_if_fail (THUNAR_IS_DEVICE (device));
+
+      /* add the mount point to the model */
+      mount_point = thunar_device_get_root (device);
+      if (mount_point != NULL)
+        {
+          shortcuts_model = gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER (model));
+          thunar_shortcuts_model_add (THUNAR_SHORTCUTS_MODEL (shortcuts_model), NULL, mount_point);
+          g_object_unref (mount_point);
+        }
+
+      g_object_unref (G_OBJECT (device));
+    }
+}
+
+
+
+static void
 thunar_shortcuts_view_eject_finish (ThunarDevice *device,
                                     const GError *error,
                                     gpointer      user_data)


More information about the Xfce4-commits mailing list