[easytag/wip/glist: 9/9] Refactor list handling in misc.c



commit cb0d9ee9656a7ca935efd6884257625cd8622297
Author: David King <amigadave amigadave com>
Date:   Wed May 22 22:24:57 2013 +0100

    Refactor list handling in misc.c

 src/misc.c |   98 ++++++++++++++++++++---------------------------------------
 1 files changed, 33 insertions(+), 65 deletions(-)
---
diff --git a/src/misc.c b/src/misc.c
index f0da2d5..32f4ba6 100644
--- a/src/misc.c
+++ b/src/misc.c
@@ -905,6 +905,7 @@ void Run_Audio_Player_Using_Selection (void)
 {
     GList *etfilelist = NULL;
     GList *selfilelist = NULL;
+    GList *l;
     ET_File *etfile;
     GtkTreeSelection *selection;
 
@@ -912,17 +913,14 @@ void Run_Audio_Player_Using_Selection (void)
 
     selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(BrowserList));
     selfilelist = gtk_tree_selection_get_selected_rows(selection, NULL);
-    while (selfilelist)
+
+    for (l = selfilelist; l != NULL; l = g_list_next (l))
     {
         etfile = Browser_List_Get_ETFile_From_Path(selfilelist->data);
         etfilelist = g_list_append(etfilelist, etfile);
-
-        if (!selfilelist->next) break;
-        selfilelist = selfilelist->next;
     }
 
-    g_list_foreach(selfilelist, (GFunc) gtk_tree_path_free, NULL);
-    g_list_free(selfilelist);
+    g_list_free_full (selfilelist, (GDestroyNotify)gtk_tree_path_free);
 
     Run_Audio_Player_Using_File_List(etfilelist);
 
@@ -947,14 +945,12 @@ void Run_Audio_Player_Using_Browser_Artist_List (void)
                        ARTIST_ALBUM_LIST_POINTER, &AlbumList,
                        -1);
 
-    while (AlbumList)
+    for (; AlbumList != NULL; AlbumList = g_list_next (AlbumList))
     {
         etfilelist = g_list_copy((GList *)AlbumList->data);
-        if (!concatenated_list)
-            concatenated_list = etfilelist;
-        else
-            concatenated_list = g_list_concat(concatenated_list,etfilelist);
-        AlbumList = AlbumList->next;
+        concatenated_list = concatenated_list ? g_list_concat (concatenated_list,
+                                                               etfilelist)
+                                              : etfilelist;
     }
 
     Run_Audio_Player_Using_File_List(concatenated_list);
@@ -1637,20 +1633,18 @@ write_playlist (GFile *file, GError **error)
     if (PLAYLIST_ONLY_SELECTED_FILES)
     {
         GList *selfilelist = NULL;
+        GList *l;
         GtkTreeSelection *selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(BrowserList));
 
         selfilelist = gtk_tree_selection_get_selected_rows(selection, NULL);
-        while (selfilelist)
+
+        for (l = selfilelist; l != NULL; l = g_list_next (l))
         {
-            etfile = Browser_List_Get_ETFile_From_Path(selfilelist->data);
+            etfile = Browser_List_Get_ETFile_From_Path (l->data);
             etfilelist = g_list_append(etfilelist, etfile);
-
-            if (!selfilelist->next) break;
-            selfilelist = selfilelist->next;
         }
 
-        g_list_foreach(selfilelist, (GFunc) gtk_tree_path_free, NULL);
-        g_list_free(selfilelist);
+        g_list_free_full (selfilelist, (GDestroyNotify)gtk_tree_path_free);
     }else
     {
         etfilelist = g_list_first(ETCore->ETFileList);
@@ -2627,29 +2621,25 @@ static void
 Search_Result_List_Row_Selected (GtkTreeSelection *selection, gpointer data)
 {
     GList       *selectedRows;
-    GList       *selectedRowsCopy;
+    GList *l;
     ET_File     *ETFile;
     GtkTreeIter  currentFile;
-    gboolean     found;
 
     selectedRows = gtk_tree_selection_get_selected_rows(selection, NULL);
-    selectedRowsCopy = selectedRows;
 
     /* We might be called with no rows selected */
-    if (g_list_length(selectedRows) == 0)
+    if (!selectedRows)
     {
-        g_list_foreach(selectedRowsCopy, (GFunc) gtk_tree_path_free, NULL);
-        g_list_free(selectedRowsCopy);
         return;
     }
 
     // Unselect files in the main list before re-selecting them...
     Browser_List_Unselect_All_Files();
 
-    while (selectedRows)
+    for (l = selectedRows; l != NULL; l = g_list_next (l))
     {
-        found = gtk_tree_model_get_iter(GTK_TREE_MODEL(SearchResultListModel), &currentFile, 
(GtkTreePath*)selectedRows->data);
-        if (found)
+        if (gtk_tree_model_get_iter (GTK_TREE_MODEL (SearchResultListModel),
+                                     &currentFile, (GtkTreePath *)l->data))
         {
             gtk_tree_model_get(GTK_TREE_MODEL(SearchResultListModel), &currentFile, 
                                SEARCH_RESULT_POINTER, &ETFile, -1);
@@ -2659,11 +2649,9 @@ Search_Result_List_Row_Selected (GtkTreeSelection *selection, gpointer data)
             if (!selectedRows->next)
                 Action_Select_Nth_File_By_Etfile(ETFile);
         }
-        selectedRows = selectedRows->next;
     }
 
-    g_list_foreach(selectedRowsCopy, (GFunc) gtk_tree_path_free, NULL);
-    g_list_free(selectedRowsCopy);
+    g_list_free_full (selectedRows, (GDestroyNotify)gtk_tree_path_free);
 }
 
 
@@ -3514,12 +3502,11 @@ Load_Filename_List_Move_Up (GtkWidget *treeview)
 {
     GtkTreeSelection *selection;
     GList *selectedRows;
-    GList *selectedRowsCopy;
+    GList *l;
     GtkTreeIter currentFile;
     GtkTreeIter nextFile;
     GtkTreePath *currentPath;
     GtkTreeModel *treemodel;
-    gboolean valid;
 
     g_return_if_fail (treeview != NULL);
 
@@ -3527,20 +3514,16 @@ Load_Filename_List_Move_Up (GtkWidget *treeview)
     treemodel = gtk_tree_view_get_model(GTK_TREE_VIEW(treeview));
     selectedRows = gtk_tree_selection_get_selected_rows(selection, NULL);
 
-    if (g_list_length(selectedRows) == 0)
+    if (!selectedRows)
     {
-        g_list_foreach(selectedRows, (GFunc)gtk_tree_path_free, NULL);
-        g_list_free(selectedRows);
         return;
     }
 
-    selectedRowsCopy = selectedRows;
-
-    while (selectedRows)
+    for (l = selectedRows; l != NULL; l = g_list_next (l))
     {
-        currentPath = (GtkTreePath*) selectedRows->data;
-        valid = gtk_tree_model_get_iter(treemodel, &currentFile, currentPath);
-        if (valid)
+        currentPath = (GtkTreePath *)l->data;
+
+        if (gtk_tree_model_get_iter (treemodel, &currentFile, currentPath))
         {
             // Find the entry above the node...
             if (gtk_tree_path_prev(currentPath))
@@ -3550,14 +3533,9 @@ Load_Filename_List_Move_Up (GtkWidget *treeview)
                 gtk_list_store_swap(GTK_LIST_STORE(treemodel), &currentFile, &nextFile);
             }
         }
-
-        selectedRows = selectedRows->next;
-        if (!selectedRows) break;
     }
 
-    g_list_foreach(selectedRowsCopy, (GFunc)gtk_tree_path_free, NULL);
-    g_list_free(selectedRowsCopy);
-    
+    g_list_free_full (selectedRows, (GDestroyNotify)gtk_tree_path_free);
 }
 
 /*
@@ -3568,12 +3546,11 @@ Load_Filename_List_Move_Down (GtkWidget *treeview)
 {
     GtkTreeSelection *selection;
     GList *selectedRows;
-    GList *selectedRowsCopy;
+    GList *l;
     GtkTreeIter currentFile;
     GtkTreeIter nextFile;
     GtkTreePath *currentPath;
     GtkTreeModel *treemodel;
-    gboolean valid;
 
     g_return_if_fail (treeview != NULL);
 
@@ -3581,34 +3558,25 @@ Load_Filename_List_Move_Down (GtkWidget *treeview)
     treemodel = gtk_tree_view_get_model(GTK_TREE_VIEW(treeview));
     selectedRows = gtk_tree_selection_get_selected_rows(selection, NULL);
 
-    if (g_list_length(selectedRows) == 0)
+    if (!selectedRows)
     {
-        g_list_foreach(selectedRows, (GFunc)gtk_tree_path_free, NULL);
-        g_list_free(selectedRows);
         return;
     }
 
-    selectedRowsCopy = selectedRows;
-
-    while (selectedRows)
+    for (l = selectedRows; l != NULL; l = g_list_next (l))
     {
-        currentPath = (GtkTreePath*) selectedRows->data;
-        valid = gtk_tree_model_get_iter(treemodel, &currentFile, currentPath);
-        if (valid)
+        currentPath = (GtkTreePath *)l->data;
+
+        if (gtk_tree_model_get_iter (treemodel, &currentFile, currentPath))
         {
             // Find the entry below the node and swap the two nodes by iter
             gtk_tree_path_next(currentPath);
             if (gtk_tree_model_get_iter(treemodel, &nextFile, currentPath))
                 gtk_list_store_swap(GTK_LIST_STORE(treemodel), &currentFile, &nextFile);
         }
-
-        if (!selectedRows->next) break;
-        selectedRows = selectedRows->next;
     }
 
-    g_list_foreach(selectedRowsCopy, (GFunc)gtk_tree_path_free, NULL);
-    g_list_free(selectedRowsCopy);
-    
+    g_list_free_full (selectedRows, (GDestroyNotify)gtk_tree_path_free);
 }
 
 /*


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