Use filelist_copy to reduce duplication
authorArkadiy Illarionov <qarkai@gmail.com>
Sat, 26 Aug 2023 11:35:22 +0000 (14:35 +0300)
committerColin Clark <colin.clark@cclark.uk>
Sat, 26 Aug 2023 13:41:09 +0000 (14:41 +0100)
Simplify related code.

src/filedata.cc
src/view-file/view-file-icon.cc
src/view-file/view-file-list.cc
src/view-file/view-file.cc

index 4aed1ae..5a73a10 100644 (file)
@@ -1541,15 +1541,10 @@ void filelist_free(GList *list)
 GList *filelist_copy(GList *list)
 {
        GList *new_list = nullptr;
-       GList *work;
 
-       work = list;
-       while (work)
+       for (GList *work = list; work; work = work->next)
                {
-               FileData *fd;
-
-               fd = static_cast<FileData *>(work->data);
-               work = work->next;
+               auto fd = static_cast<FileData *>(work->data);
 
                new_list = g_list_prepend(new_list, file_data_ref(fd));
                }
index b2a97cb..d2987a9 100644 (file)
@@ -842,29 +842,16 @@ guint vficon_selection_count(ViewFile *vf, gint64 *bytes)
 GList *vficon_selection_get_list(ViewFile *vf)
 {
        GList *list = nullptr;
-       GList *work, *work2;
 
-       work = VFICON(vf)->selection;
-       while (work)
+       for (GList *work = g_list_last(VFICON(vf)->selection); work; work = work->prev)
                {
                auto fd = static_cast<FileData *>(work->data);
                g_assert(fd->magick == FD_MAGICK);
 
+               list = g_list_prepend(list, filelist_copy(fd->sidecar_files));
                list = g_list_prepend(list, file_data_ref(fd));
-
-               work2 = fd->sidecar_files;
-               while (work2)
-                       {
-                       fd = static_cast<FileData *>(work2->data);
-                       list = g_list_prepend(list, file_data_ref(fd));
-                       work2 = work2->next;
-                       }
-
-               work = work->next;
                }
 
-       list = g_list_reverse(list);
-
        return list;
 }
 
index e70f993..0979cde 100644 (file)
@@ -86,7 +86,7 @@ static void vflist_set_expanded(ViewFile *vf, GtkTreeIter *iter, gboolean expand
  *-----------------------------------------------------------------------------
  */
 struct ViewFileFindRowData {
-       FileData *fd;
+       const FileData *fd;
        GtkTreeIter *iter;
        gboolean found;
        gint row;
@@ -107,7 +107,7 @@ static gboolean vflist_find_row_cb(GtkTreeModel *model, GtkTreePath *, GtkTreeIt
        return FALSE;
 }
 
-static gint vflist_find_row(ViewFile *vf, FileData *fd, GtkTreeIter *iter)
+static gint vflist_find_row(const ViewFile *vf, const FileData *fd, GtkTreeIter *iter)
 {
        GtkTreeModel *store;
        ViewFileFindRowData data = {fd, iter, FALSE, 0};
@@ -315,7 +315,7 @@ void vflist_dnd_init(ViewFile *vf)
 
 GList *vflist_selection_get_one(ViewFile *vf, FileData *fd)
 {
-       GList *list = g_list_append(nullptr, file_data_ref(fd));
+       GList *list = nullptr;
 
        if (fd->sidecar_files)
                {
@@ -332,20 +332,13 @@ GList *vflist_selection_get_one(ViewFile *vf, FileData *fd)
                        if (!gtk_tree_view_row_expanded(GTK_TREE_VIEW(vf->listview), tpath))
                                {
                                /* unexpanded - add whole group */
-                               GList *work = fd->sidecar_files;
-                               while (work)
-                                       {
-                                       auto sfd = static_cast<FileData *>(work->data);
-                                       list = g_list_prepend(list, file_data_ref(sfd));
-                                       work = work->next;
-                                       }
+                               list = filelist_copy(fd->sidecar_files);
                                }
                        gtk_tree_path_free(tpath);
                        }
-               list = g_list_reverse(list);
                }
 
-       return list;
+       return g_list_prepend(list, file_data_ref(fd));
 }
 
 GList *vflist_pop_menu_file_list(ViewFile *vf)
@@ -1477,12 +1470,10 @@ GList *vflist_selection_get_list(ViewFile *vf)
        GtkTreeSelection *selection;
        GList *slist;
        GList *list = nullptr;
-       GList *work;
 
        selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(vf->listview));
        slist = gtk_tree_selection_get_selected_rows(selection, &store);
-       work = slist;
-       while (work)
+       for (GList *work = g_list_last(slist); work; work = work->prev)
                {
                auto tpath = static_cast<GtkTreePath *>(work->data);
                FileData *fd;
@@ -1491,25 +1482,17 @@ GList *vflist_selection_get_list(ViewFile *vf)
                gtk_tree_model_get_iter(store, &iter, tpath);
                gtk_tree_model_get(store, &iter, FILE_COLUMN_POINTER, &fd, -1);
 
-               list = g_list_prepend(list, file_data_ref(fd));
-
                if (!fd->parent && !gtk_tree_view_row_expanded(GTK_TREE_VIEW(vf->listview), tpath))
                        {
                        /* unexpanded - add whole group */
-                       GList *work2 = fd->sidecar_files;
-                       while (work2)
-                               {
-                               auto sfd = static_cast<FileData *>(work2->data);
-                               list = g_list_prepend(list, file_data_ref(sfd));
-                               work2 = work2->next;
-                               }
+                       list = g_list_prepend(list, filelist_copy(fd->sidecar_files));
                        }
 
-               work = work->next;
+               list = g_list_prepend(list, file_data_ref(fd));
                }
        g_list_free_full(slist, reinterpret_cast<GDestroyNotify>(gtk_tree_path_free));
 
-       return g_list_reverse(list);
+       return list;
 }
 
 GList *vflist_selection_get_list_by_index(ViewFile *vf)
index 878c328..2aa62a2 100644 (file)
@@ -111,15 +111,7 @@ guint vf_count(ViewFile *vf, gint64 *bytes)
 
 GList *vf_get_list(ViewFile *vf)
 {
-       GList *list = nullptr;
-       GList *work;
-       for (work = vf->list; work; work = work->next)
-               {
-               auto fd = static_cast<FileData *>(work->data);
-               list = g_list_prepend(list, file_data_ref(fd));
-               }
-
-       return g_list_reverse(list);
+       return filelist_copy(vf->list);
 }
 
 /*