Fix #1241: 'Go to directory view' uses hard-coded default values
[geeqie.git] / src / image-load-collection.cc
index 4bea593..dbd8a02 100644 (file)
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
-#include "main.h"
-#include "image-load.h"
 #include "image-load-collection.h"
 
+#include <unistd.h>
+
+#include <cstdio>
+
+#include <gdk-pixbuf/gdk-pixbuf.h>
+#include <glib-object.h>
+#include <glib.h>
+
 #include "cache.h"
+#include "filedata.h"
+#include "image-load.h"
 #include "misc.h"
+#include "options.h"
 #include "ui-fileops.h"
 
-typedef struct _ImageLoaderCOLLECTION ImageLoaderCOLLECTION;
-struct _ImageLoaderCOLLECTION {
-       ImageLoaderBackendCbAreaUpdated area_updated_cb;
-       ImageLoaderBackendCbSize size_cb;
-       ImageLoaderBackendCbAreaPrepared area_prepared_cb;
+namespace
+{
+
+struct ImageLoaderCOLLECTION : public ImageLoaderBackend
+{
+public:
+       ~ImageLoaderCOLLECTION() override;
+
+       void init(AreaUpdatedCb area_updated_cb, SizePreparedCb size_prepared_cb, AreaPreparedCb area_prepared_cb, gpointer data) override;
+       gboolean write(const guchar *buf, gsize &chunk_size, gsize count, GError **error) override;
+       GdkPixbuf *get_pixbuf() override;
+       gchar *get_format_name() override;
+       gchar **get_format_mime_types() override;
+
+private:
+       AreaUpdatedCb area_updated_cb;
        gpointer data;
+
        GdkPixbuf *pixbuf;
-       guint requested_width;
-       guint requested_height;
-       gboolean abort;
 };
 
-static gboolean image_loader_collection_load(gpointer loader, const guchar *UNUSED(buf), gsize UNUSED(count), GError **UNUSED(error))
+gboolean ImageLoaderCOLLECTION::write(const guchar *, gsize &chunk_size, gsize count, GError **)
 {
-       auto ld = (ImageLoaderCOLLECTION *) loader;
-       auto il = static_cast<ImageLoader *>(ld->data);
+       auto il = static_cast<ImageLoader *>(data);
 
        #define LINE_LENGTH 1000
 
        gboolean ret = FALSE;
        gchar *randname;
        gchar *cmd_line;
-       FILE *fp = NULL;
+       FILE *fp = nullptr;
        gint line_count = 0;
-       GString *file_names = g_string_new(NULL);
+       GString *file_names = g_string_new(nullptr);
        gchar line[LINE_LENGTH];
-       gchar **split_line = NULL;
+       gchar **split_line = nullptr;
        gchar *cache_found;
        gchar *pathl;
 
@@ -71,7 +88,7 @@ static gboolean image_loader_collection_load(gpointer loader, const guchar *UNUS
                                        cache_found = cache_find_location(CACHE_TYPE_THUMB, split_line[1]);
                                        if (cache_found)
                                                {
-                                               file_names = g_string_append(file_names, g_strconcat("\"", cache_found,"\" ", NULL));
+                                               g_string_append_printf(file_names, "\"%s\" ", cache_found);
                                                line_count++;
                                                }
                                        g_free(cache_found);
@@ -80,7 +97,7 @@ static gboolean image_loader_collection_load(gpointer loader, const guchar *UNUS
                                                {
                                                g_strfreev(split_line);
                                                }
-                                       split_line = NULL;
+                                       split_line = nullptr;
                                }
                        fclose(fp);
 
@@ -92,91 +109,58 @@ static gboolean image_loader_collection_load(gpointer loader, const guchar *UNUS
                                cmd_line = g_strdup_printf("montage %s -geometry %dx%d+1+1 %s >/dev/null 2>&1", file_names->str, options->thumbnails.max_width, options->thumbnails.max_height, randname);
 
                                runcmd(cmd_line);
-                               ld->pixbuf = gdk_pixbuf_new_from_file(randname, NULL);
-                               if (ld->pixbuf)
+                               pixbuf = gdk_pixbuf_new_from_file(randname, nullptr);
+                               if (pixbuf)
                                        {
-                                       ld->area_updated_cb(loader, 0, 0, gdk_pixbuf_get_width(ld->pixbuf), gdk_pixbuf_get_height(ld->pixbuf), ld->data);
+                                       area_updated_cb(nullptr, 0, 0, gdk_pixbuf_get_width(pixbuf), gdk_pixbuf_get_height(pixbuf), data);
                                        }
 
                                unlink(randname);
                                g_free(randname);
-                               g_string_free(file_names, TRUE);
                                g_free(cmd_line);
 
                                ret = TRUE;
                                }
-                       else
-                               {
-                               g_string_free(file_names, TRUE);
-                               }
+
+                       g_string_free(file_names, TRUE);
                        }
                }
+       chunk_size = count;
        return ret;
 }
 
-static gpointer image_loader_collection_new(ImageLoaderBackendCbAreaUpdated area_updated_cb, ImageLoaderBackendCbSize size_cb, ImageLoaderBackendCbAreaPrepared area_prepared_cb, gpointer data)
-{
-       auto loader = g_new0(ImageLoaderCOLLECTION, 1);
-       loader->area_updated_cb = area_updated_cb;
-       loader->size_cb = size_cb;
-       loader->area_prepared_cb = area_prepared_cb;
-       loader->data = data;
-       return (gpointer) loader;
-}
-
-static void image_loader_collection_set_size(gpointer loader, int width, int height)
+void ImageLoaderCOLLECTION::init(AreaUpdatedCb area_updated_cb, SizePreparedCb, AreaPreparedCb, gpointer data)
 {
-       auto ld = (ImageLoaderCOLLECTION *) loader;
-       ld->requested_width = width;
-       ld->requested_height = height;
+       this->area_updated_cb = area_updated_cb;
+       this->data = data;
 }
 
-static GdkPixbuf* image_loader_collection_get_pixbuf(gpointer loader)
+GdkPixbuf *ImageLoaderCOLLECTION::get_pixbuf()
 {
-       auto ld = (ImageLoaderCOLLECTION *) loader;
-       return ld->pixbuf;
+       return pixbuf;
 }
 
-static gchar* image_loader_collection_get_format_name(gpointer UNUSED(loader))
+gchar *ImageLoaderCOLLECTION::get_format_name()
 {
        return g_strdup("collection");
 }
-static gchar** image_loader_collection_get_format_mime_types(gpointer UNUSED(loader))
-{
-       static const gchar *mime[] = {"image/png", NULL};
-       return g_strdupv(const_cast<gchar **>(mime));
-}
 
-static gboolean image_loader_collection_close(gpointer UNUSED(loader), GError **UNUSED(error))
+gchar **ImageLoaderCOLLECTION::get_format_mime_types()
 {
-       return TRUE;
+       static const gchar *mime[] = {"image/png", nullptr};
+       return g_strdupv(const_cast<gchar **>(mime));
 }
 
-static void image_loader_collection_abort(gpointer loader)
+ImageLoaderCOLLECTION::~ImageLoaderCOLLECTION()
 {
-       auto ld = (ImageLoaderCOLLECTION *) loader;
-       ld->abort = TRUE;
+       if (pixbuf) g_object_unref(pixbuf);
 }
 
-static void image_loader_collection_free(gpointer loader)
-{
-       auto ld = (ImageLoaderCOLLECTION *) loader;
-       if (ld->pixbuf) g_object_unref(ld->pixbuf);
-       g_free(ld);
-}
+} // namespace
 
-void image_loader_backend_set_collection(ImageLoaderBackend *funcs)
+std::unique_ptr<ImageLoaderBackend> get_image_loader_backend_collection()
 {
-       funcs->loader_new = image_loader_collection_new;
-       funcs->set_size = image_loader_collection_set_size;
-       funcs->load = image_loader_collection_load;
-       funcs->write = NULL;
-       funcs->get_pixbuf = image_loader_collection_get_pixbuf;
-       funcs->close = image_loader_collection_close;
-       funcs->abort = image_loader_collection_abort;
-       funcs->free = image_loader_collection_free;
-       funcs->get_format_name = image_loader_collection_get_format_name;
-       funcs->get_format_mime_types = image_loader_collection_get_format_mime_types;
+       return std::make_unique<ImageLoaderCOLLECTION>();
 }
 
 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */