Add vf_selection_foreach()
authorArkadiy Illarionov <qarkai@gmail.com>
Tue, 16 Apr 2024 19:54:22 +0000 (22:54 +0300)
committerColin Clark <colin.clark@cclark.uk>
Wed, 17 Apr 2024 08:47:20 +0000 (09:47 +0100)
Hide implementation and deduplicate code.
Also simplify layout_menu_write_rotate() a bit.

src/layout-image.cc
src/layout-util.cc
src/view-file.h
src/view-file/view-file.cc

index 3d0c44c..3b413bf 100644 (file)
@@ -1242,44 +1242,9 @@ void layout_image_zoom_set_fill_geometry(LayoutWindow *lw, gboolean vertical, gb
 void layout_image_alter_orientation(LayoutWindow *lw, AlterType type)
 {
        if (!layout_valid(&lw)) return;
-
-       GtkTreeModel *store;
-       GList *work;
-       GtkTreeSelection *selection;
-       GtkTreePath *tpath;
-       FileData *fd_n;
-       GtkTreeIter iter;
-
        if (!lw || !lw->vf) return;
 
-       if (lw->vf->type == FILEVIEW_ICON)
-               {
-               if (!VFICON(lw->vf)->selection) return;
-               work = VFICON(lw->vf)->selection;
-               }
-       else
-               {
-               selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(lw->vf->listview));
-               work = gtk_tree_selection_get_selected_rows(selection, &store);
-               }
-
-       while (work)
-               {
-               if (lw->vf->type == FILEVIEW_ICON)
-                       {
-                       fd_n = static_cast<FileData *>(work->data);
-                       work = work->next;
-                       }
-               else
-                       {
-                       tpath = static_cast<GtkTreePath *>(work->data);
-                       gtk_tree_model_get_iter(store, &iter, tpath);
-                       gtk_tree_model_get(store, &iter, VIEW_FILE_COLUMN_POINTER, &fd_n, -1);
-                       work = work->next;
-                       }
-
-               image_alter_orientation(lw->image, fd_n, type);
-               }
+       vf_selection_foreach(lw->vf, [lw, type](FileData *fd_n) { image_alter_orientation(lw->image, fd_n, type); });
 }
 
 static void image_alter_rating(FileData *fd_n, const gchar *rating)
@@ -1291,44 +1256,9 @@ static void image_alter_rating(FileData *fd_n, const gchar *rating)
 void layout_image_rating(LayoutWindow *lw, const gchar *rating)
 {
        if (!layout_valid(&lw)) return;
-
-       GtkTreeModel *store;
-       GList *work;
-       GtkTreeSelection *selection;
-       GtkTreePath *tpath;
-       FileData *fd_n;
-       GtkTreeIter iter;
-
        if (!lw || !lw->vf) return;
 
-       if (lw->vf->type == FILEVIEW_ICON)
-               {
-               if (!VFICON(lw->vf)->selection) return;
-               work = VFICON(lw->vf)->selection;
-               }
-       else
-               {
-               selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(lw->vf->listview));
-               work = gtk_tree_selection_get_selected_rows(selection, &store);
-               }
-
-       while (work)
-               {
-               if (lw->vf->type == FILEVIEW_ICON)
-                       {
-                       fd_n = static_cast<FileData *>(work->data);
-                       work = work->next;
-                       }
-               else
-                       {
-                       tpath = static_cast<GtkTreePath *>(work->data);
-                       gtk_tree_model_get_iter(store, &iter, tpath);
-                       gtk_tree_model_get(store, &iter, VIEW_FILE_COLUMN_POINTER, &fd_n, -1);
-                       work = work->next;
-                       }
-
-               image_alter_rating(fd_n, rating);
-               }
+       vf_selection_foreach(lw->vf, [rating](FileData *fd_n) { image_alter_rating(fd_n, rating); });
 }
 
 void layout_image_reset_orientation(LayoutWindow *lw)
index 7d37164..c845c31 100644 (file)
@@ -610,61 +610,25 @@ static void layout_menu_select_overunderexposed_cb(GtkToggleAction *action, gpoi
 static void layout_menu_write_rotate(GtkToggleAction *, gpointer data, gboolean keep_date)
 {
        auto lw = static_cast<LayoutWindow *>(data);
-       GtkTreeModel *store;
-       GList *work;
-       GtkTreeSelection *selection;
-       GtkTreePath *tpath;
-       FileData *fd_n;
-       GtkTreeIter iter;
-       gchar *rotation;
-       gchar *command;
-       gint run_result;
-       GenericDialog *gd;
-       GString *message;
-       int cmdstatus;
 
        if (!layout_valid(&lw)) return;
-
        if (!lw || !lw->vf) return;
 
-       if (lw->vf->type == FILEVIEW_ICON)
-               {
-               if (!VFICON(lw->vf)->selection) return;
-               work = VFICON(lw->vf)->selection;
-               }
-       else
-               {
-               selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(lw->vf->listview));
-               work = gtk_tree_selection_get_selected_rows(selection, &store);
-               }
+       const gchar *keep_date_arg = keep_date ? "-t" : "";
 
-       while (work)
-               {
-               if (lw->vf->type == FILEVIEW_ICON)
-                       {
-                       fd_n = static_cast<FileData *>(work->data);
-                       work = work->next;
-                       }
-               else
-                       {
-                       tpath = static_cast<GtkTreePath *>(work->data);
-                       gtk_tree_model_get_iter(store, &iter, tpath);
-                       gtk_tree_model_get(store, &iter, VIEW_FILE_COLUMN_POINTER, &fd_n, -1);
-                       work = work->next;
-                       }
-
-               rotation = g_strdup_printf("%d", fd_n->user_orientation);
-               command = g_strconcat(gq_bindir, "/geeqie-rotate -r ", rotation,
-                                                               keep_date ? " -t \"" : " \"", fd_n->path, "\"", NULL);
-               cmdstatus = runcmd(command);
-               run_result = WEXITSTATUS(cmdstatus);
+       vf_selection_foreach(lw->vf, [keep_date_arg](FileData *fd_n)
+       {
+               gchar *command = g_strdup_printf("%s/geeqie-rotate -r %d %s \"%s\"",
+                                                gq_bindir, fd_n->user_orientation, keep_date_arg, fd_n->path);
+               int cmdstatus = runcmd(command);
+               gint run_result = WEXITSTATUS(cmdstatus);
                if (!run_result)
                        {
                        fd_n->user_orientation = 0;
                        }
                else
                        {
-                       message = g_string_new(_("Operation failed:\n"));
+                       GString *message = g_string_new(_("Operation failed:\n"));
 
                        if (run_result == 1)
                                message = g_string_append(message, _("No file extension\n"));
@@ -681,7 +645,7 @@ static void layout_menu_write_rotate(GtkToggleAction *, gpointer data, gboolean
 
                        message = g_string_append(message, fd_n->name);
 
-                       gd = generic_dialog_new(_("Image orientation"), "image_orientation", nullptr, TRUE, nullptr, nullptr);
+                       GenericDialog *gd = generic_dialog_new(_("Image orientation"), "image_orientation", nullptr, TRUE, nullptr, nullptr);
                        generic_dialog_add_message(gd, GQ_ICON_DIALOG_ERROR, _("Image orientation"), message->str, TRUE);
                        generic_dialog_add_button(gd, GQ_ICON_OK, "OK", nullptr, TRUE);
 
@@ -690,9 +654,8 @@ static void layout_menu_write_rotate(GtkToggleAction *, gpointer data, gboolean
                        g_string_free(message, TRUE);
                        }
 
-               g_free(rotation);
                g_free(command);
-               }
+       });
 }
 
 static void layout_menu_write_rotate_keep_date_cb(GtkToggleAction *action, gpointer data)
index 002e59c..ae857e5 100644 (file)
@@ -22,6 +22,7 @@
 #define VIEW_FILE_H
 
 #include <ctime>
+#include <functional>
 
 #include <glib.h>
 #include <gtk/gtk.h>
@@ -165,6 +166,8 @@ GList *vf_get_list(ViewFile *vf);
 guint vf_selection_count(ViewFile *vf, gint64 *bytes);
 GList *vf_selection_get_list(ViewFile *vf);
 GList *vf_selection_get_list_by_index(ViewFile *vf);
+using ViewFileSelectionCallback = std::function<void(FileData *)>;
+void vf_selection_foreach(ViewFile *vf, const ViewFileSelectionCallback &func);
 
 void vf_select_all(ViewFile *vf);
 void vf_select_none(ViewFile *vf);
index bca025c..929f4eb 100644 (file)
@@ -230,6 +230,43 @@ GList *vf_selection_get_list_by_index(ViewFile *vf)
        return ret;
 }
 
+void vf_selection_foreach(ViewFile *vf, const ViewFileSelectionCallback &func)
+{
+       GtkTreeModel *store;
+       GList *work;
+       FileData *fd_n;
+       GtkTreeIter iter;
+
+       if (!vf) return;
+
+       if (vf->type == FILEVIEW_ICON)
+               {
+               if (!VFICON(vf)->selection) return;
+               work = VFICON(vf)->selection;
+               }
+       else
+               {
+               GtkTreeSelection *selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(vf->listview));
+               work = gtk_tree_selection_get_selected_rows(selection, &store);
+               }
+
+       for (; work; work = work->next)
+               {
+               if (vf->type == FILEVIEW_ICON)
+                       {
+                       fd_n = static_cast<FileData *>(work->data);
+                       }
+               else
+                       {
+                       auto *tpath = static_cast<GtkTreePath *>(work->data);
+                       gtk_tree_model_get_iter(store, &iter, tpath);
+                       gtk_tree_model_get(store, &iter, VIEW_FILE_COLUMN_POINTER, &fd_n, -1);
+                       }
+
+               func(fd_n);
+               }
+}
+
 void vf_select_all(ViewFile *vf)
 {
        switch (vf->type)