From a8a9261069f447429016bdbb9c3abdbaffd6266d Mon Sep 17 00:00:00 2001
From: Alexander Schwinn <alexxcons@xfce.org>
Date: Mon, 8 Jun 2020 23:07:42 +0200
Subject: [PATCH] move duplicated code from concrete views into a single
 standard-view method (Merge Request !12)

---
 thunar/thunar-abstract-icon-view.c | 31 ++-----------------
 thunar/thunar-details-view.c       | 31 ++-----------------
 thunar/thunar-standard-view.c      | 48 ++++++++++++++++++++++++++++++
 thunar/thunar-standard-view.h      | 36 +++++++++++-----------
 4 files changed, 71 insertions(+), 75 deletions(-)

diff --git a/thunar/thunar-abstract-icon-view.c b/thunar/thunar-abstract-icon-view.c
index 0ac7c2911..97aa94678 100644
--- a/thunar/thunar-abstract-icon-view.c
+++ b/thunar/thunar-abstract-icon-view.c
@@ -580,12 +580,6 @@ thunar_abstract_icon_view_button_press_event (ExoIconView            *view,
                                               ThunarAbstractIconView *abstract_icon_view)
 {
   GtkTreePath       *path;
-  GtkTreeIter        iter;
-  ThunarFile        *file;
-  ThunarPreferences *preferences;
-  gboolean           in_tab;
-  ThunarLauncher    *launcher;
-  GtkWidget         *window;
 
   abstract_icon_view->priv->button_pressed = TRUE;
 
@@ -631,29 +625,8 @@ thunar_abstract_icon_view_button_press_event (ExoIconView            *view,
           /* select only the path to the item on which the user clicked */
           exo_icon_view_select_path (view, path);
 
-          /* determine the file for the path */
-          gtk_tree_model_get_iter (GTK_TREE_MODEL (THUNAR_STANDARD_VIEW (abstract_icon_view)->model), &iter, path);
-          file = thunar_list_model_get_file (THUNAR_STANDARD_VIEW (abstract_icon_view)->model, &iter);
-          if (G_LIKELY (file != NULL))
-            {
-              if (thunar_file_is_directory (file))
-                {
-                  /* lookup setting if we should open in a tab or a window */
-                  preferences = thunar_preferences_get ();
-                  g_object_get (preferences, "misc-middle-click-in-tab", &in_tab, NULL);
-                  g_object_unref (preferences);
-
-                  /* holding ctrl inverts the action */
-                  if ((event->state & GDK_CONTROL_MASK) != 0)
-                      in_tab = !in_tab;
-
-                  window = gtk_widget_get_toplevel (GTK_WIDGET (abstract_icon_view));
-                  launcher = thunar_window_get_launcher (THUNAR_WINDOW (window));
-                  thunar_launcher_open_selected_folders (launcher, in_tab);
-                }
-              /* release the file reference */
-              g_object_unref (G_OBJECT (file));
-            }
+          /* try to open the path as new window/tab, if possible */
+          _thunar_standard_view_open_on_middle_click (THUNAR_STANDARD_VIEW (abstract_icon_view), path, event->state);
 
           /* cleanup */
           gtk_tree_path_free (path);
diff --git a/thunar/thunar-details-view.c b/thunar/thunar-details-view.c
index 733399ef5..1af743282 100644
--- a/thunar/thunar-details-view.c
+++ b/thunar/thunar-details-view.c
@@ -640,14 +640,8 @@ thunar_details_view_button_press_event (GtkTreeView       *tree_view,
 {
   GtkTreeSelection  *selection;
   GtkTreePath       *path = NULL;
-  GtkTreeIter        iter;
   GtkTreeViewColumn *column;
   GtkTreeViewColumn *name_column;
-  ThunarFile        *file;
-  ThunarPreferences *preferences;
-  gboolean           in_tab;
-  ThunarLauncher    *launcher;
-  GtkWidget         *window;
 
   /* check if the event is for the bin window */
   if (G_UNLIKELY (event->window != gtk_tree_view_get_bin_window (tree_view)))
@@ -746,29 +740,8 @@ thunar_details_view_button_press_event (GtkTreeView       *tree_view,
           gtk_tree_selection_unselect_all (selection);
           gtk_tree_selection_select_path (selection, path);
 
-          /* determine the file for the path */
-          gtk_tree_model_get_iter (GTK_TREE_MODEL (THUNAR_STANDARD_VIEW (details_view)->model), &iter, path);
-          file = thunar_list_model_get_file (THUNAR_STANDARD_VIEW (details_view)->model, &iter);
-          if (G_LIKELY (file != NULL))
-            {
-              if (thunar_file_is_directory (file))
-                {
-                  /* lookup setting if we should open in a tab or a window */
-                  preferences = thunar_preferences_get ();
-                  g_object_get (preferences, "misc-middle-click-in-tab", &in_tab, NULL);
-                  g_object_unref (preferences);
-
-                  /* holding ctrl inverts the action */
-                  if ((event->state & GDK_CONTROL_MASK) != 0)
-                      in_tab = !in_tab;
-
-                  window = gtk_widget_get_toplevel (GTK_WIDGET (details_view));
-                  launcher = thunar_window_get_launcher (THUNAR_WINDOW (window));
-                  thunar_launcher_open_selected_folders (launcher, in_tab);
-                }
-              /* release the file reference */
-              g_object_unref (G_OBJECT (file));
-            }
+          /* try to open the path as new window/tab, if possible */
+          _thunar_standard_view_open_on_middle_click (THUNAR_STANDARD_VIEW (details_view), path, event->state);
 
           /* cleanup */
           gtk_tree_path_free (path);
diff --git a/thunar/thunar-standard-view.c b/thunar/thunar-standard-view.c
index b1c30ef77..597d5a532 100644
--- a/thunar/thunar-standard-view.c
+++ b/thunar/thunar-standard-view.c
@@ -3714,3 +3714,51 @@ thunar_standard_view_connect_accelerators (ThunarStandardView *standard_view)
   /* as well append accelerators of derived widgets */
   (*THUNAR_STANDARD_VIEW_GET_CLASS (standard_view)->connect_accelerators) (standard_view, standard_view->accel_group);
 }
+
+
+
+/**
+ * _thunar_standard_view_open_on_middle_click:
+ * @standard_view : a #ThunarStandardView.
+ * @tree_path : the #GtkTreePath to open.
+ * @event_state : The event_state of the pressed #GdkEventButton
+ *
+ * Method only should be used by child widgets.
+ * The method will attempt to find a thunar file for the given #GtkTreePath and open it as window/tab, if it is a directory
+ * Note that this method only should be used after pressing the middle-mouse button
+ **/
+void
+_thunar_standard_view_open_on_middle_click (ThunarStandardView *standard_view,
+                                            GtkTreePath        *tree_path,
+                                            guint               event_state)
+{
+  GtkTreeIter     iter;
+  ThunarFile     *file;
+  gboolean        in_tab;
+  GtkWidget      *window;
+  ThunarLauncher *launcher;
+
+  _thunar_return_if_fail (THUNAR_IS_STANDARD_VIEW (standard_view));
+
+  /* determine the file for the path */
+  gtk_tree_model_get_iter (GTK_TREE_MODEL (standard_view->model), &iter, tree_path);
+  file = thunar_list_model_get_file (standard_view->model, &iter);
+  if (G_LIKELY (file != NULL))
+    {
+      if (thunar_file_is_directory (file))
+        {
+          /* lookup setting if we should open in a tab or a window */
+          g_object_get (G_OBJECT (standard_view->preferences), "misc-middle-click-in-tab", &in_tab, NULL);
+
+          /* holding ctrl inverts the action */
+          if ((event_state & GDK_CONTROL_MASK) != 0)
+              in_tab = !in_tab;
+
+          window = gtk_widget_get_toplevel (GTK_WIDGET (standard_view));
+          launcher = thunar_window_get_launcher (THUNAR_WINDOW (window));
+          thunar_launcher_open_selected_folders (launcher, in_tab);
+        }
+      /* release the file reference */
+      g_object_unref (G_OBJECT (file));
+    }
+}
diff --git a/thunar/thunar-standard-view.h b/thunar/thunar-standard-view.h
index 31cb44507..0051b8435 100644
--- a/thunar/thunar-standard-view.h
+++ b/thunar/thunar-standard-view.h
@@ -148,23 +148,25 @@ struct _ThunarStandardView
   ThunarStandardViewPrivate *priv;
 };
 
-GType          thunar_standard_view_get_type            (void) G_GNUC_CONST;
-
-void           thunar_standard_view_context_menu        (ThunarStandardView       *standard_view);
-void           thunar_standard_view_queue_popup         (ThunarStandardView       *standard_view,
-                                                         GdkEventButton           *event);
-void           thunar_standard_view_selection_changed   (ThunarStandardView       *standard_view);
-void           thunar_standard_view_set_history         (ThunarStandardView       *standard_view,
-                                                         ThunarHistory            *history);
-ThunarHistory *thunar_standard_view_get_history         (ThunarStandardView       *standard_view);
-ThunarHistory *thunar_standard_view_copy_history        (ThunarStandardView       *standard_view);
-void           thunar_standard_view_append_menu_items   (ThunarStandardView       *standard_view,
-                                                         GtkMenu                  *menu,
-                                                         GtkAccelGroup            *accel_group);
-void           thunar_standard_view_append_menu_item    (ThunarStandardView       *standard_view,
-                                                         GtkMenu                  *menu,
-                                                         ThunarStandardViewAction  action);
-
+GType          thunar_standard_view_get_type              (void) G_GNUC_CONST;
+
+void           thunar_standard_view_context_menu          (ThunarStandardView       *standard_view);
+void           thunar_standard_view_queue_popup           (ThunarStandardView       *standard_view,
+                                                           GdkEventButton           *event);
+void           thunar_standard_view_selection_changed     (ThunarStandardView       *standard_view);
+void           thunar_standard_view_set_history           (ThunarStandardView       *standard_view,
+                                                           ThunarHistory            *history);
+ThunarHistory *thunar_standard_view_get_history           (ThunarStandardView       *standard_view);
+ThunarHistory *thunar_standard_view_copy_history          (ThunarStandardView       *standard_view);
+void           thunar_standard_view_append_menu_items     (ThunarStandardView       *standard_view,
+                                                           GtkMenu                  *menu,
+                                                           GtkAccelGroup            *accel_group);
+void           thunar_standard_view_append_menu_item      (ThunarStandardView       *standard_view,
+                                                           GtkMenu                  *menu,
+                                                           ThunarStandardViewAction  action);
+void           _thunar_standard_view_open_on_middle_click (ThunarStandardView       *standard_view,
+                                                           GtkTreePath              *tree_path,
+                                                           guint                     event_state);
 G_END_DECLS;
 
 #endif /* !__THUNAR_STANDARD_VIEW_H__ */
-- 
GitLab