Use std::swap instead of temporary values
[geeqie.git] / src / collect.cc
index 8042d62..5b9a62f 100644 (file)
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
-#include "main.h"
 #include "collect.h"
 
+#include <sys/stat.h>
+
+#include <cstdlib>
+#include <cstring>
+#include <ctime>
+#include <utility>
+
+#include <glib-object.h>
+
 #include "collect-dlg.h"
 #include "collect-io.h"
 #include "collect-table.h"
+#include "compat.h"
+#include "debug.h"
 #include "filedata.h"
 #include "img-view.h"
+#include "intl.h"
 #include "layout-image.h"
 #include "layout-util.h"
+#include "layout.h"
+#include "main-defines.h"
 #include "misc.h"
+#include "options.h"
 #include "pixbuf-util.h"
 #include "print.h"
 #include "ui-fileops.h"
 #include "ui-tree-edit.h"
+#include "ui-utildlg.h"
 #include "utilops.h"
 #include "window.h"
 
-#define COLLECT_DEF_WIDTH 440
-#define COLLECT_DEF_HEIGHT 450
+enum {
+       COLLECT_DEF_WIDTH = 440,
+       COLLECT_DEF_HEIGHT = 450
+};
 
 /**
  *  list of paths to collections */
@@ -75,7 +92,7 @@ static void collection_notify_cb(FileData *fd, NotifyType type, gpointer data);
  *-------------------------------------------------------------------
  */
 
-CollectInfo *collection_info_new(FileData *fd, struct stat *UNUSED(st), GdkPixbuf *pixbuf)
+CollectInfo *collection_info_new(FileData *fd, struct stat *, GdkPixbuf *pixbuf)
 {
        CollectInfo *ci;
 
@@ -125,18 +142,6 @@ gboolean collection_info_load_thumb_unused(CollectInfo *ci)
 }
 #pragma GCC diagnostic pop
 
-void collection_list_free(GList *list)
-{
-       GList *work;
-       work = list;
-       while (work)
-               {
-               collection_info_free(static_cast<CollectInfo *>(work->data));
-               work = work->next;
-               }
-       g_list_free(list);
-}
-
 /* an ugly static var, well what ya gonna do ? */
 static SortType collection_list_sort_method = SORT_NAME;
 
@@ -192,8 +197,8 @@ static gint collection_list_sort_cb(gconstpointer a, gconstpointer b)
 
        if (options->file_sort.case_sensitive)
                return strcmp(cia->fd->collate_key_name, cib->fd->collate_key_name);
-       else
-               return strcmp(cia->fd->collate_key_name_nocase, cib->fd->collate_key_name_nocase);
+
+       return strcmp(cia->fd->collate_key_name_nocase, cib->fd->collate_key_name_nocase);
 }
 
 GList *collection_list_sort(GList *list, SortType method)
@@ -207,9 +212,11 @@ GList *collection_list_sort(GList *list, SortType method)
 
 GList *collection_list_randomize(GList *list)
 {
-       guint random, length, i;
-       gpointer tmp;
-       GList *nlist, *olist;
+       guint random;
+       guint length;
+       guint i;
+       GList *nlist;
+       GList *olist;
 
        length = g_list_length(list);
        if (!length) return nullptr;
@@ -221,9 +228,7 @@ GList *collection_list_randomize(GList *list)
                random = static_cast<guint>(1.0 * length * rand()/(RAND_MAX + 1.0));
                olist = g_list_nth(list, i);
                nlist = g_list_nth(list, random);
-               tmp = olist->data;
-               olist->data = nlist->data;
-               nlist->data = tmp;
+               std::swap(olist->data, nlist->data);
                }
 
        return list;
@@ -408,7 +413,7 @@ void collection_contents(const gchar *name, GString **contents)
                        {
                        ci = static_cast<CollectInfo *>(work->data);
                        fd = ci->fd;
-                       *contents = g_string_append(*contents, g_strdup(fd->path));
+                       *contents = g_string_append(*contents, fd->path);
                        *contents = g_string_append(*contents, "\n");
 
                        work = work->next;
@@ -467,8 +472,8 @@ CollectionData *collection_new(const gchar *path)
 
        cd->ref = 1;    /* starts with a ref of 1 */
        cd->sort_method = SORT_NONE;
-       cd->window_w = COLLECT_DEF_WIDTH;
-       cd->window_h = COLLECT_DEF_HEIGHT;
+       cd->window.width = COLLECT_DEF_WIDTH;
+       cd->window.height = COLLECT_DEF_HEIGHT;
        cd->existence = g_hash_table_new(nullptr, nullptr);
 
        if (path)
@@ -506,7 +511,7 @@ void collection_free(CollectionData *cd)
        DEBUG_1("collection \"%s\" freed", cd->name);
 
        collection_load_stop(cd);
-       collection_list_free(cd->list);
+       g_list_free_full(cd->list, reinterpret_cast<GDestroyNotify>(collection_info_free));
 
        file_data_unregister_notify_func(collection_notify_cb, cd);
 
@@ -514,6 +519,7 @@ void collection_free(CollectionData *cd)
 
        g_hash_table_destroy(cd->existence);
 
+       g_free(cd->collection_path);
        g_free(cd->path);
        g_free(cd->name);
 
@@ -586,8 +592,8 @@ CollectionData *collection_from_dnd_data(const gchar *data, GList **list, GList
                while (*ptr != '\n' && *ptr != '\0') ptr++;
                if (*ptr == '\0')
                        break;
-               else
-                       while (*ptr == '\n') ptr++;
+
+               while (*ptr == '\n') ptr++;
 
                info = static_cast<CollectInfo *>(g_list_nth_data(cd->list, item_number));
                if (!info) continue;
@@ -741,7 +747,10 @@ static CollectInfo *collection_info_new_if_not_exists(CollectionData *cd, struct
 {
        CollectInfo *ci;
 
-       if (g_hash_table_lookup(cd->existence, fd->path)) return nullptr;
+       if (!options->collections_duplicates)
+               {
+               if (g_hash_table_lookup(cd->existence, fd->path)) return nullptr;
+               }
 
        ci = collection_info_new(fd, st, nullptr);
        if (ci) g_hash_table_insert(cd->existence, fd->path, g_strdup(""));
@@ -935,7 +944,7 @@ static void collection_notify_cb(FileData *fd, NotifyType type, gpointer data)
  *-------------------------------------------------------------------
  */
 
-static gboolean collection_window_keypress(GtkWidget *UNUSED(widget), GdkEventKey *event, gpointer data)
+static gboolean collection_window_keypress(GtkWidget *, GdkEventKey *event, gpointer data)
 {
        auto cw = static_cast<CollectWindow *>(data);
        gboolean stop_signal = FALSE;
@@ -989,7 +998,7 @@ static gboolean collection_window_keypress(GtkWidget *UNUSED(widget), GdkEventKe
                                file_util_delete(nullptr, collection_table_selection_get_list(cw->table), cw->window);
                                break;
                        case 'S': case 's':
-                               collection_dialog_save_as(nullptr, cw->cd);
+                               collection_dialog_save_as(cw->cd);
                                break;
                        case 'W': case 'w':
                                collection_window_close(cw);
@@ -1015,7 +1024,7 @@ static gboolean collection_window_keypress(GtkWidget *UNUSED(widget), GdkEventKe
                        case 'S': case 's':
                                if (!cw->cd->path)
                                        {
-                                       collection_dialog_save_as(nullptr, cw->cd);
+                                       collection_dialog_save_as(cw->cd);
                                        }
                                else if (!collection_save(cw->cd, cw->cd->path))
                                        {
@@ -1023,7 +1032,7 @@ static gboolean collection_window_keypress(GtkWidget *UNUSED(widget), GdkEventKe
                                        }
                                break;
                        case 'A': case 'a':
-                               collection_dialog_append(nullptr, cw->cd);
+                               collection_dialog_append(cw->cd);
                                break;
                        case 'N': case 'n':
                                collection_set_sort_method(cw->cd, SORT_NAME);
@@ -1060,6 +1069,7 @@ static gboolean collection_window_keypress(GtkWidget *UNUSED(widget), GdkEventKe
                                if (list)
                                        {
                                        collection_remove_by_info_list(cw->cd, list);
+                                       collection_table_refresh(cw->table);
                                        g_list_free(list);
                                        }
                                else
@@ -1095,9 +1105,9 @@ static void collection_window_get_geometry(CollectWindow *cw)
 
        cd = cw->cd;
        window = gtk_widget_get_window(cw->window);
-       gdk_window_get_position(window, &cd->window_x, &cd->window_y);
-       cd->window_w = gdk_window_get_width(window);
-       cd->window_h = gdk_window_get_height(window);
+       gdk_window_get_position(window, &cd->window.x, &cd->window.y);
+       cd->window.width = gdk_window_get_width(window);
+       cd->window.height = gdk_window_get_height(window);
        cd->window_read = TRUE;
 }
 
@@ -1132,7 +1142,7 @@ static void collection_window_update_title(CollectWindow *cw)
        g_free(buf);
 }
 
-static void collection_window_update_info(CollectionData *UNUSED(cd), CollectInfo *ci, gpointer data)
+static void collection_window_update_info(CollectionData *, CollectInfo *ci, gpointer data)
 {
        auto cw = static_cast<CollectWindow *>(data);
 
@@ -1178,7 +1188,7 @@ static void collection_window_close_final(CollectWindow *cw)
        collection_window_list = g_list_remove(collection_window_list, cw);
        collection_window_get_geometry(cw);
 
-       gtk_widget_destroy(cw->window);
+       gq_gtk_widget_destroy(cw->window);
 
        collection_set_update_info_func(cw->cd, nullptr, nullptr);
        collection_unref(cw->cd);
@@ -1195,14 +1205,15 @@ static void collection_close_save_cb(GenericDialog *gd, gpointer data)
 
        if (!cw->cd->path)
                {
-               collection_dialog_save_close(nullptr, cw->cd);
+               collection_dialog_save_close(cw->cd);
                return;
                }
-       else if (!collection_save(cw->cd, cw->cd->path))
+
+       if (!collection_save(cw->cd, cw->cd->path))
                {
                gchar *buf;
                buf = g_strdup_printf(_("Failed to save the collection:\n%s"), cw->cd->path);
-               warning_dialog(_("Save Failed"), buf, GTK_STOCK_DIALOG_ERROR, cw->window);
+               warning_dialog(_("Save Failed"), buf, GQ_ICON_DIALOG_ERROR, cw->window);
                g_free(buf);
                return;
                }
@@ -1241,12 +1252,12 @@ static void collection_close_dlg_show(CollectWindow *cw)
        gd = generic_dialog_new(_("Close collection"),
                                "close_collection", cw->window, FALSE,
                                collection_close_cancel_cb, cw);
-       generic_dialog_add_message(gd, GTK_STOCK_DIALOG_QUESTION,
+       generic_dialog_add_message(gd, GQ_ICON_DIALOG_QUESTION,
                                   _("Close collection"),
                                   _("Collection has been modified.\nSave first?"), TRUE);
 
-       generic_dialog_add_button(gd, GTK_STOCK_SAVE, nullptr, collection_close_save_cb, TRUE);
-       generic_dialog_add_button(gd, GTK_STOCK_DELETE, _("_Discard"), collection_close_close_cb, FALSE);
+       generic_dialog_add_button(gd, GQ_ICON_SAVE, _("Save"), collection_close_save_cb, TRUE);
+       generic_dialog_add_button(gd, GQ_ICON_DELETE, _("_Discard"), collection_close_close_cb, FALSE);
 
        cw->close_dialog = gd->dialog;
 
@@ -1307,7 +1318,7 @@ gboolean collection_window_modified_exists()
        return ret;
 }
 
-static gboolean collection_window_delete(GtkWidget *UNUSED(widget), GdkEvent *UNUSED(event), gpointer data)
+static gboolean collection_window_delete(GtkWidget *, GdkEvent *, gpointer data)
 {
        auto cw = static_cast<CollectWindow *>(data);
        collection_window_close(cw);
@@ -1337,7 +1348,7 @@ CollectWindow *collection_window_new(const gchar *path)
 
        cw->cd = collection_new(path);
 
-       cw->window = window_new(GTK_WINDOW_TOPLEVEL, "collection", PIXBUF_INLINE_ICON_BOOK, nullptr, nullptr);
+       cw->window = window_new("collection", PIXBUF_INLINE_ICON_BOOK, nullptr, nullptr);
        DEBUG_NAME(cw->window);
 
        geometry.min_width = DEFAULT_MINIMAL_WINDOW_SIZE;
@@ -1349,13 +1360,13 @@ CollectWindow *collection_window_new(const gchar *path)
 
        if (options->collections_on_top)
                {
-               gtk_window_set_keep_above(GTK_WINDOW(cw->window), TRUE);
+               gq_gtk_window_set_keep_above(GTK_WINDOW(cw->window), TRUE);
                }
 
        if (options->save_window_positions && path && collection_load_only_geometry(cw->cd, path))
                {
-               gtk_window_set_default_size(GTK_WINDOW(cw->window), cw->cd->window_w, cw->cd->window_h);
-               gtk_window_move(GTK_WINDOW(cw->window), cw->cd->window_x, cw->cd->window_y);
+               gtk_window_set_default_size(GTK_WINDOW(cw->window), cw->cd->window.width, cw->cd->window.height);
+               gq_gtk_window_move(GTK_WINDOW(cw->window), cw->cd->window.x, cw->cd->window.y);
                }
        else
                {
@@ -1373,25 +1384,25 @@ CollectWindow *collection_window_new(const gchar *path)
                         G_CALLBACK(collection_window_keypress), cw);
 
        vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
-       gtk_container_add(GTK_CONTAINER(cw->window), vbox);
+       gq_gtk_container_add(GTK_WIDGET(cw->window), vbox);
        gtk_widget_show(vbox);
 
        cw->table = collection_table_new(cw->cd);
-       gtk_box_pack_start(GTK_BOX(vbox), cw->table->scrolled, TRUE, TRUE, 0);
+       gq_gtk_box_pack_start(GTK_BOX(vbox), cw->table->scrolled, TRUE, TRUE, 0);
        gtk_widget_show(cw->table->scrolled);
 
        cw->status_box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
-       gtk_box_pack_start(GTK_BOX(vbox), cw->status_box, FALSE, FALSE, 0);
+       gq_gtk_box_pack_start(GTK_BOX(vbox), cw->status_box, FALSE, FALSE, 0);
        gtk_widget_show(cw->status_box);
 
        frame = gtk_frame_new(nullptr);
        DEBUG_NAME(frame);
-       gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_IN);
-       gtk_box_pack_start(GTK_BOX(cw->status_box), frame, TRUE, TRUE, 0);
+       gq_gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_IN);
+       gq_gtk_box_pack_start(GTK_BOX(cw->status_box), frame, TRUE, TRUE, 0);
        gtk_widget_show(frame);
 
        status_label = gtk_label_new("");
-       gtk_container_add(GTK_CONTAINER(frame), status_label);
+       gq_gtk_container_add(GTK_WIDGET(frame), status_label);
        gtk_widget_show(status_label);
 
        extra_label = gtk_progress_bar_new();
@@ -1399,7 +1410,7 @@ CollectWindow *collection_window_new(const gchar *path)
        gtk_progress_bar_set_text(GTK_PROGRESS_BAR(extra_label), "");
        gtk_progress_bar_set_show_text(GTK_PROGRESS_BAR(extra_label), TRUE);
 
-       gtk_box_pack_start(GTK_BOX(cw->status_box), extra_label, TRUE, TRUE, 0);
+       gq_gtk_box_pack_start(GTK_BOX(cw->status_box), extra_label, TRUE, TRUE, 0);
        gtk_widget_show(extra_label);
 
        collection_table_set_labels(cw->table, status_label, extra_label);