Convert GET_{LEFT,RIGHT}_PIXBUF_OFFSET macro to function
[geeqie.git] / src / image-load-external.cc
1 /*
2  * Copyright (C) 2021 - The Geeqie Team
3  *
4  * Author: Colin Clark
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20
21 #include "image-load-external.h"
22
23 #include <gdk-pixbuf/gdk-pixbuf.h>
24 #include <glib-object.h>
25 #include <glib.h>
26
27 #include "filedata.h"
28 #include "image-load.h"
29 #include "misc.h"
30 #include "options.h"
31 #include "ui-fileops.h"
32
33 namespace
34 {
35
36 struct ImageLoaderExternal : public ImageLoaderBackend
37 {
38 public:
39         ~ImageLoaderExternal() override;
40
41         void init(AreaUpdatedCb area_updated_cb, SizePreparedCb size_prepared_cb, AreaPreparedCb area_prepared_cb, gpointer data) override;
42         gboolean write(const guchar *buf, gsize &chunk_size, gsize count, GError **error) override;
43         GdkPixbuf *get_pixbuf() override;
44         gchar *get_format_name() override;
45         gchar **get_format_mime_types() override;
46
47 private:
48         AreaUpdatedCb area_updated_cb;
49         gpointer data;
50
51         GdkPixbuf *pixbuf;
52 };
53
54 gboolean ImageLoaderExternal::write(const guchar *, gsize &chunk_size, gsize count, GError **)
55 {
56         auto il = static_cast<ImageLoader *>(data);
57         gchar *cmd_line;
58         gchar *randname;
59         gchar *tilde_filename;
60
61         tilde_filename = expand_tilde(options->external_preview.extract);
62
63         randname = g_strdup("/tmp/geeqie_external_preview_XXXXXX");
64         g_mkstemp(randname);
65
66         cmd_line = g_strdup_printf("\"%s\" \"%s\" \"%s\"" , tilde_filename, il->fd->path, randname);
67
68         runcmd(cmd_line);
69
70         pixbuf = gdk_pixbuf_new_from_file(randname, nullptr);
71
72         area_updated_cb(nullptr, 0, 0, gdk_pixbuf_get_width(pixbuf), gdk_pixbuf_get_height(pixbuf), data);
73
74         g_free(cmd_line);
75         unlink_file(randname);
76         g_free(randname);
77         g_free(tilde_filename);
78
79         chunk_size = count;
80         return TRUE;
81 }
82
83 void ImageLoaderExternal::init(AreaUpdatedCb area_updated_cb, SizePreparedCb, AreaPreparedCb, gpointer data)
84 {
85         this->area_updated_cb = area_updated_cb;
86         this->data = data;
87 }
88
89 GdkPixbuf *ImageLoaderExternal::get_pixbuf()
90 {
91         return pixbuf;
92 }
93
94 gchar *ImageLoaderExternal::get_format_name()
95 {
96         return g_strdup("external");
97 }
98
99 gchar **ImageLoaderExternal::get_format_mime_types()
100 {
101         static const gchar *mime[] = {"application/octet-stream", nullptr};
102         return g_strdupv(const_cast<gchar **>(mime));
103 }
104
105 ImageLoaderExternal::~ImageLoaderExternal()
106 {
107         if (pixbuf) g_object_unref(pixbuf);
108 }
109
110 } // namespace
111
112 std::unique_ptr<ImageLoaderBackend> get_image_loader_backend_external()
113 {
114         return std::make_unique<ImageLoaderExternal>();
115 }
116
117 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */