[easytag/wip/libsoup-cddb: 21/21] Refactor reading of selection from the file list



commit bac7fd7ef76ebe8f9d0bd616a4246fc7a51b5147
Author: David King <amigadave amigadave com>
Date:   Sat Mar 12 16:11:06 2016 +0000

    Refactor reading of selection from the file list
    
    Add et_browser_get_selected_files() to simplify getting the list of
    selected files from the browser.

 src/application_window.c |   84 ++++++++++++++++++---------------------------
 src/application_window.h |    1 +
 src/browser.c            |   38 +++++++++++++++++++++
 src/browser.h            |    1 +
 src/cddb_dialog.c        |   17 +---------
 src/easytag.c            |   21 ++----------
 src/playlist_dialog.c    |   21 ++----------
 src/scan_dialog.c        |   25 +++++---------
 src/tag_area.c           |   20 +----------
 9 files changed, 92 insertions(+), 136 deletions(-)
---
diff --git a/src/application_window.c b/src/application_window.c
index 045113d..3b4095d 100644
--- a/src/application_window.c
+++ b/src/application_window.c
@@ -647,31 +647,24 @@ on_undo_file_changes (GSimpleAction *action,
                       gpointer user_data)
 {
     EtApplicationWindow *self;
-    EtApplicationWindowPrivate *priv;
-    GList *selfilelist = NULL;
+    GList *etfilelist = NULL;
     GList *l;
     gboolean state = FALSE;
-    ET_File *etfile;
-    GtkTreeSelection *selection;
 
     g_return_if_fail (ETCore->ETFileDisplayedList != NULL);
 
     self = ET_APPLICATION_WINDOW (user_data);
-    priv = et_application_window_get_instance_private (self);
 
     et_application_window_update_et_file_from_ui (self);
 
-    selection = et_application_window_browser_get_selection (self);
-    selfilelist = gtk_tree_selection_get_selected_rows(selection, NULL);
+    etfilelist = et_application_window_browser_get_selected_files (self);
 
-    for (l = selfilelist; l != NULL; l = g_list_next (l))
+    for (l = etfilelist; l != NULL; l = g_list_next (l))
     {
-        etfile = et_browser_get_et_file_from_path (ET_BROWSER (priv->browser),
-                                                   l->data);
-        state |= ET_Undo_File_Data(etfile);
+        state |= ET_Undo_File_Data ((ET_File *)l->data);
     }
 
-    g_list_free_full (selfilelist, (GDestroyNotify)gtk_tree_path_free);
+    g_list_free (etfilelist);
 
     /* Refresh the whole list (faster than file by file) to show changes. */
     et_application_window_browser_refresh_list (self);
@@ -679,8 +672,6 @@ on_undo_file_changes (GSimpleAction *action,
     /* Display the current file */
     et_application_window_display_et_file (self, ETCore->ETFileDisplayed);
     et_application_window_update_actions (self);
-
-    //ET_Debug_Print_File_List(ETCore->ETFileList,__FILE__,__LINE__,__FUNCTION__);
 }
 
 static void
@@ -689,31 +680,24 @@ on_redo_file_changes (GSimpleAction *action,
                       gpointer user_data)
 {
     EtApplicationWindow *self;
-    EtApplicationWindowPrivate *priv;
-    GList *selfilelist = NULL;
+    GList *etfilelist;
     GList *l;
     gboolean state = FALSE;
-    ET_File *etfile;
-    GtkTreeSelection *selection;
 
     g_return_if_fail (ETCore->ETFileDisplayedList != NULL);
 
     self = ET_APPLICATION_WINDOW (user_data);
-    priv = et_application_window_get_instance_private (self);
 
     et_application_window_update_et_file_from_ui (self);
 
-    selection = et_application_window_browser_get_selection (ET_APPLICATION_WINDOW (user_data));
-    selfilelist = gtk_tree_selection_get_selected_rows(selection, NULL);
+    etfilelist = et_application_window_browser_get_selected_files (self);
 
-    for (l = selfilelist; l != NULL; l = g_list_next (l))
+    for (l = etfilelist; l != NULL; l = g_list_next (l))
     {
-        etfile = et_browser_get_et_file_from_path (ET_BROWSER (priv->browser),
-                                                   l->data);
-        state |= ET_Redo_File_Data(etfile);
+        state |= ET_Redo_File_Data ((ET_File *)l->data);
     }
 
-    g_list_free_full (selfilelist, (GDestroyNotify)gtk_tree_path_free);
+    g_list_free (etfilelist);
 
     /* Refresh the whole list (faster than file by file) to show changes. */
     et_application_window_browser_refresh_list (ET_APPLICATION_WINDOW (user_data));
@@ -888,35 +872,28 @@ on_remove_tags (GSimpleAction *action,
                 gpointer user_data)
 {
     EtApplicationWindow *self;
-    EtApplicationWindowPrivate *priv;
-    GList *selfilelist = NULL;
+    GList *etfilelist;
     GList *l;
-    ET_File *etfile;
     File_Tag *FileTag;
     gint progress_bar_index;
     gint selectcount;
     double fraction;
-    GtkTreeSelection *selection;
 
     g_return_if_fail (ETCore->ETFileDisplayedList != NULL);
 
     self = ET_APPLICATION_WINDOW (user_data);
-    priv = et_application_window_get_instance_private (self);
 
     et_application_window_update_et_file_from_ui (self);
 
     /* Initialize status bar */
     et_application_window_progress_set_fraction (self, 0.0);
-    selection = et_application_window_browser_get_selection (self);
-    selectcount = gtk_tree_selection_count_selected_rows (selection);
+    etfilelist = et_application_window_browser_get_selected_files (self);
+    selectcount = g_list_length (etfilelist);
     progress_bar_index = 0;
 
-    selfilelist = gtk_tree_selection_get_selected_rows (selection, NULL);
-
-    for (l = selfilelist; l != NULL; l = g_list_next (l))
+    for (l = etfilelist; l != NULL; l = g_list_next (l))
     {
-        etfile = et_browser_get_et_file_from_path (ET_BROWSER (priv->browser),
-                                                   l->data);
+        ET_File *etfile = (ET_File *)l->data;
         FileTag = et_file_tag_new ();
         ET_Manage_Changes_Of_File_Data (etfile, NULL, FileTag);
 
@@ -929,7 +906,7 @@ on_remove_tags (GSimpleAction *action,
         }
     }
 
-    g_list_free_full (selfilelist, (GDestroyNotify)gtk_tree_path_free);
+    g_list_free (etfilelist);
 
     /* Refresh the whole list (faster than file by file) to show changes. */
     et_application_window_browser_refresh_list (self);
@@ -2620,12 +2597,9 @@ et_application_window_update_actions (EtApplicationWindow *self)
         return;
     }else
     {
-        GList *selfilelist = NULL;
-        ET_File *etfile;
         gboolean has_undo = FALSE;
         gboolean has_redo = FALSE;
         //gboolean has_to_save = FALSE;
-        GtkTreeSelection *selection;
 
         /* File and Tag frames */
         et_application_window_file_area_set_sensitive (self, TRUE);
@@ -2656,23 +2630,23 @@ et_application_window_update_actions (EtApplicationWindow *self)
 
         /* Check if one of the selected files has undo or redo data */
         {
+            GList *etfilelist;
             GList *l;
 
-            selection = et_application_window_browser_get_selection (self);
-            selfilelist = gtk_tree_selection_get_selected_rows(selection, NULL);
+            etfilelist = et_application_window_browser_get_selected_files (self);
 
-            for (l = selfilelist; l != NULL; l = g_list_next (l))
+            for (l = etfilelist; l != NULL; l = g_list_next (l))
             {
-                etfile = et_application_window_browser_get_et_file_from_path (self,
-                                                                              l->data);
-                has_undo    |= ET_File_Data_Has_Undo_Data(etfile);
-                has_redo    |= ET_File_Data_Has_Redo_Data(etfile);
+                const ET_File *etfile = (ET_File *)l->data;
+
+                has_undo |= ET_File_Data_Has_Undo_Data (etfile);
+                has_redo |= ET_File_Data_Has_Redo_Data (etfile);
                 /* has_to_save |= et_file_check_saved (etfile); */
                 if ((has_undo && has_redo /*&& has_to_save*/) || !l->next) // Useless to check the other 
files
                     break;
             }
 
-            g_list_free_full (selfilelist, (GDestroyNotify)gtk_tree_path_free);
+            g_list_free (etfilelist);
         }
 
         /* Enable undo commands if there are undo data */
@@ -2865,6 +2839,16 @@ et_application_window_browser_get_et_file_from_iter (EtApplicationWindow *self,
     return et_browser_get_et_file_from_iter (ET_BROWSER (priv->browser), iter);
 }
 
+GList *
+et_application_window_browser_get_selected_files (EtApplicationWindow *self)
+{
+    EtApplicationWindowPrivate *priv;
+
+    priv = et_application_window_get_instance_private (self);
+
+    return et_browser_get_selected_files (ET_BROWSER (priv->browser));
+}
+
 GtkTreeSelection *
 et_application_window_browser_get_selection (EtApplicationWindow *self)
 {
diff --git a/src/application_window.h b/src/application_window.h
index 7bf04a6..2b4f97d 100644
--- a/src/application_window.h
+++ b/src/application_window.h
@@ -75,6 +75,7 @@ void et_application_window_browser_entry_set_text (EtApplicationWindow *self, co
 void et_application_window_browser_label_set_text (EtApplicationWindow *self, const gchar *text);
 ET_File * et_application_window_browser_get_et_file_from_path (EtApplicationWindow *self, GtkTreePath *path);
 ET_File * et_application_window_browser_get_et_file_from_iter (EtApplicationWindow *self, GtkTreeIter *iter);
+GList * et_application_window_browser_get_selected_files (EtApplicationWindow *self);
 GtkTreeSelection * et_application_window_browser_get_selection (EtApplicationWindow *self);
 GtkTreeViewColumn *et_application_window_browser_get_column_for_column_id (EtApplicationWindow *self, gint 
column_id);
 GtkSortType et_application_window_browser_get_sort_order_for_column_id (EtApplicationWindow *self, gint 
column_id);
diff --git a/src/browser.c b/src/browser.c
index 799b290..b0ebf1d 100644
--- a/src/browser.c
+++ b/src/browser.c
@@ -514,6 +514,44 @@ et_browser_get_current_path (EtBrowser *self)
     return priv->current_path;
 }
 
+/*
+ * et_browser_get_selected_files:
+ * @self: an #EtBrowser from which to get a list of selected files
+ *
+ * Get a list of #ET_File of the current selection of the #EtBrowser @self.
+ *
+ * Returns: (element-type ET_File) (transfer container): a newly-allocated list
+ *          of the selected files in the browser
+ */
+GList *
+et_browser_get_selected_files (EtBrowser *self)
+{
+    EtBrowserPrivate *priv;
+    GtkTreeSelection *selection;
+    GList *selfilelist;
+    GList *l;
+    GList *files = NULL;
+
+    g_return_val_if_fail (ET_BROWSER (self), NULL);
+
+    priv = et_browser_get_instance_private (self);
+    selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->file_view));
+    selfilelist = gtk_tree_selection_get_selected_rows (selection, NULL);
+
+    for (l = selfilelist; l != NULL; l = g_list_next (l))
+    {
+        ET_File *etfile;
+
+        etfile = et_browser_get_et_file_from_path (self,
+                                                   (GtkTreePath *)l->data);
+        files = g_list_prepend (files, etfile);
+    }
+
+    g_list_free_full (selfilelist, (GDestroyNotify)gtk_tree_path_free);
+
+    return g_list_reverse (files);
+}
+
 GtkTreeSelection *
 et_browser_get_selection (EtBrowser *self)
 {
diff --git a/src/browser.h b/src/browser.h
index e982075..dac3f72 100644
--- a/src/browser.h
+++ b/src/browser.h
@@ -110,6 +110,7 @@ void et_browser_reload_directory (EtBrowser *self);
 void et_browser_set_current_path_default (EtBrowser *self);
 GFile * et_browser_get_current_path (EtBrowser *self);
 
+GList * et_browser_get_selected_files (EtBrowser *self);
 GtkTreeSelection * et_browser_get_selection (EtBrowser *self);
 
 GtkTreeViewColumn * et_browser_get_column_for_column_id (EtBrowser *self, gint column_id);
diff --git a/src/cddb_dialog.c b/src/cddb_dialog.c
index 2e38ed6..de7508d 100644
--- a/src/cddb_dialog.c
+++ b/src/cddb_dialog.c
@@ -2672,21 +2672,7 @@ et_cddb_dialog_search_from_selection (EtCDDBDialog *self)
      * selected. */
     if (n_files > 0)
     {
-        GList* selfilelist;
-
-        selfilelist = gtk_tree_selection_get_selected_rows (file_selection,
-                                                            NULL);
-
-        for (l = selfilelist; l != NULL; l = g_list_next (l))
-        {
-            ET_File *etfile;
-
-            etfile = et_application_window_browser_get_et_file_from_path (ET_APPLICATION_WINDOW (MainWindow),
-                                                                          l->data);
-            filelist = g_list_prepend (filelist, etfile);
-        }
-
-        g_list_free_full (selfilelist, (GDestroyNotify)gtk_tree_path_free);
+        filelist = et_application_window_browser_get_selected_files (ET_APPLICATION_WINDOW (MainWindow));
     }
     else /* No rows selected, use the whole list */
     {
@@ -2739,7 +2725,6 @@ et_cddb_dialog_search_from_selection (EtCDDBDialog *self)
     total_id = 0;
     num_tracks = n_files;
     query_string = g_string_new ("");
-    filelist = g_list_reverse (filelist);
 
     /* FIXME: Split this out to a separate function. */
     for (l = filelist; l != NULL; l = g_list_next (l))
diff --git a/src/easytag.c b/src/easytag.c
index fc26747..7fb3494 100644
--- a/src/easytag.c
+++ b/src/easytag.c
@@ -105,26 +105,11 @@ Save_Selected_Files_With_Answer (gboolean force_saving_files)
 {
     gint toreturn;
     GList *etfilelist = NULL;
-    GList *selfilelist = NULL;
-    GList *l;
-    ET_File *etfile;
-    GtkTreeSelection *selection;
-
-    selection = et_application_window_browser_get_selection (ET_APPLICATION_WINDOW (MainWindow));
-    selfilelist = gtk_tree_selection_get_selected_rows(selection, NULL);
-
-    for (l = selfilelist; l != NULL; l = g_list_next (l))
-    {
-        etfile = et_application_window_browser_get_et_file_from_path (ET_APPLICATION_WINDOW (MainWindow),
-                                                                      l->data);
-        etfilelist = g_list_prepend (etfilelist, etfile);
-    }
 
-    g_list_free_full (selfilelist, (GDestroyNotify)gtk_tree_path_free);
+    etfilelist = et_application_window_browser_get_selected_files (ET_APPLICATION_WINDOW (MainWindow));
+    toreturn = Save_List_Of_Files (etfilelist, force_saving_files);
+    g_list_free (etfilelist);
 
-    etfilelist = g_list_reverse (etfilelist);
-    toreturn = Save_List_Of_Files(etfilelist, force_saving_files);
-    g_list_free(etfilelist);
     return toreturn;
 }
 
diff --git a/src/playlist_dialog.c b/src/playlist_dialog.c
index 0044796..10dbf56 100644
--- a/src/playlist_dialog.c
+++ b/src/playlist_dialog.c
@@ -125,24 +125,9 @@ write_playlist (EtPlaylistDialog *self, GFile *file, GError **error)
 
     if (g_settings_get_boolean (MainSettings, "playlist-selected-only"))
     {
-        GList *selfilelist = NULL;
-        GtkTreeSelection *selection = et_application_window_browser_get_selection (ET_APPLICATION_WINDOW 
(MainWindow));
-
-        selfilelist = gtk_tree_selection_get_selected_rows(selection, NULL);
-
-        for (l = selfilelist; l != NULL; l = g_list_next (l))
-        {
-            ET_File *etfile;
-
-            etfile = et_application_window_browser_get_et_file_from_path (ET_APPLICATION_WINDOW (MainWindow),
-                                                                          l->data);
-            etfilelist = g_list_prepend (etfilelist, etfile);
-        }
-
-        etfilelist = g_list_reverse (etfilelist);
-
-        g_list_free_full (selfilelist, (GDestroyNotify)gtk_tree_path_free);
-    }else
+        etfilelist = et_application_window_browser_get_selected_files (ET_APPLICATION_WINDOW (MainWindow));
+    }
+    else
     {
         etfilelist = ETCore->ETFileList;
     }
diff --git a/src/scan_dialog.c b/src/scan_dialog.c
index 9ecfe7c..9dade34 100644
--- a/src/scan_dialog.c
+++ b/src/scan_dialog.c
@@ -2692,15 +2692,13 @@ entry_check_rename_file_mask (GtkEntry *entry, gpointer user_data)
 void
 et_scan_dialog_scan_selected_files (EtScanDialog *self)
 {
-    gint progress_bar_index;
-    gint selectcount;
+    EtApplicationWindow *window;
+    guint progress_bar_index = 0;
+    guint selectcount;
     gchar progress_bar_text[30];
     double fraction;
     GList *selfilelist = NULL;
     GList *l;
-    ET_File *etfile;
-    EtApplicationWindow *window;
-    GtkTreeSelection *selection;
 
     g_return_if_fail (ETCore->ETFileDisplayedList != NULL);
 
@@ -2708,24 +2706,19 @@ et_scan_dialog_scan_selected_files (EtScanDialog *self)
     et_application_window_update_et_file_from_ui (window);
 
     /* Initialize status bar */
-    selection = et_application_window_browser_get_selection (window);
-    selectcount = gtk_tree_selection_count_selected_rows (selection);
     et_application_window_progress_set_fraction (window, 0.0);
-    progress_bar_index = 0;
-    g_snprintf(progress_bar_text, 30, "%d/%d", progress_bar_index, selectcount);
+    selfilelist = et_application_window_browser_get_selected_files (window);
+    selectcount = g_list_length (selfilelist);
+    g_snprintf (progress_bar_text, 30, "%u/%u", progress_bar_index,
+                selectcount);
     et_application_window_progress_set_text (window, progress_bar_text);
 
     /* Set to unsensitive all command buttons (except Quit button) */
     et_application_window_disable_command_actions (window);
 
-    progress_bar_index = 0;
-
-    selfilelist = gtk_tree_selection_get_selected_rows(selection, NULL);
-
     for (l = selfilelist; l != NULL; l = g_list_next (l))
     {
-        etfile = et_application_window_browser_get_et_file_from_path (window,
-                                                                      l->data);
+        ET_File *etfile = l->data;
 
         /* Run the current scanner. */
         Scan_Select_Mode_And_Run_Scanner (self, etfile);
@@ -2740,7 +2733,7 @@ et_scan_dialog_scan_selected_files (EtScanDialog *self)
             gtk_main_iteration();
     }
 
-    g_list_free_full (selfilelist, (GDestroyNotify)gtk_tree_path_free);
+    g_list_free (selfilelist);
 
     /* Refresh the whole list (faster than file by file) to show changes. */
     et_application_window_browser_refresh_list (window);
diff --git a/src/tag_area.c b/src/tag_area.c
index 7e2a9e3..2dfd662 100644
--- a/src/tag_area.c
+++ b/src/tag_area.c
@@ -133,15 +133,13 @@ on_apply_to_selection (GObject *object,
 {
     EtTagAreaPrivate *priv;
     EtApplicationWindow *window;
-    GList *etfilelist = NULL;
-    GList *selection_filelist = NULL;
+    GList *etfilelist;
     GList *l;
     const gchar *string_to_set;
     const gchar *string_to_set1;
     gchar *msg = NULL;
     ET_File *etfile;
     File_Tag *FileTag;
-    GtkTreeSelection *selection;
 
     g_return_if_fail (ETCore->ETFileDisplayedList != NULL);
 
@@ -151,21 +149,7 @@ on_apply_to_selection (GObject *object,
 
     et_application_window_update_et_file_from_ui (window);
 
-    /* Warning : 'selection_filelist' is not a list of 'ETFile' items! */
-    selection = et_application_window_browser_get_selection (window);
-    selection_filelist = gtk_tree_selection_get_selected_rows (selection, NULL);
-
-    // Create an 'ETFile' list from 'selection_filelist'
-    for (l = selection_filelist; l != NULL; l = g_list_next (l))
-    {
-        etfile = et_application_window_browser_get_et_file_from_path (window,
-                                                                      l->data);
-        etfilelist = g_list_prepend (etfilelist, etfile);
-    }
-
-    etfilelist = g_list_reverse (etfilelist);
-    g_list_free_full (selection_filelist, (GDestroyNotify)gtk_tree_path_free);
-
+    etfilelist = et_application_window_browser_get_selected_files (window);
 
     if (object == G_OBJECT (priv->title_entry))
     {


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