Deduplicate cr3 image loader
[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 struct ImageLoaderExternal {
34         ImageLoaderBackendCbAreaUpdated area_updated_cb;
35         ImageLoaderBackendCbSize size_cb;
36         ImageLoaderBackendCbAreaPrepared area_prepared_cb;
37         gpointer data;
38         GdkPixbuf *pixbuf;
39         guint requested_width;
40         guint requested_height;
41         gboolean abort;
42 };
43
44 static gboolean image_loader_external_load(gpointer loader, const guchar *, gsize, GError **)
45 {
46         auto ld = static_cast<ImageLoaderExternal *>(loader);
47         auto il = static_cast<ImageLoader *>(ld->data);
48         gchar *cmd_line;
49         gchar *randname;
50         gchar *tilde_filename;
51
52         tilde_filename = expand_tilde(options->external_preview.extract);
53
54         randname = g_strdup("/tmp/geeqie_external_preview_XXXXXX");
55         g_mkstemp(randname);
56
57         cmd_line = g_strdup_printf("\"%s\" \"%s\" \"%s\"" , tilde_filename, il->fd->path, randname);
58
59         runcmd(cmd_line);
60
61         ld->pixbuf = gdk_pixbuf_new_from_file(randname, nullptr);
62
63         ld->area_updated_cb(loader, 0, 0, gdk_pixbuf_get_width(ld->pixbuf), gdk_pixbuf_get_height(ld->pixbuf), ld->data);
64
65         g_free(cmd_line);
66         unlink_file(randname);
67         g_free(randname);
68         g_free(tilde_filename);
69
70         return TRUE;
71 }
72
73 static gpointer image_loader_external_new(ImageLoaderBackendCbAreaUpdated area_updated_cb, ImageLoaderBackendCbSize size_cb, ImageLoaderBackendCbAreaPrepared area_prepared_cb, gpointer data)
74 {
75         auto loader = g_new0(ImageLoaderExternal, 1);
76         loader->area_updated_cb = area_updated_cb;
77         loader->size_cb = size_cb;
78         loader->area_prepared_cb = area_prepared_cb;
79         loader->data = data;
80         return loader;
81 }
82
83 static void image_loader_external_set_size(gpointer loader, int width, int height)
84 {
85         auto ld = static_cast<ImageLoaderExternal *>(loader);
86         ld->requested_width = width;
87         ld->requested_height = height;
88 }
89
90 static GdkPixbuf* image_loader_external_get_pixbuf(gpointer loader)
91 {
92         auto ld = static_cast<ImageLoaderExternal *>(loader);
93         return ld->pixbuf;
94 }
95
96 static gchar* image_loader_external_get_format_name(gpointer)
97 {
98         return g_strdup("external");
99 }
100
101 static gchar** image_loader_external_get_format_mime_types(gpointer)
102 {
103         static const gchar *mime[] = {"application/octet-stream", nullptr};
104         return g_strdupv(const_cast<gchar **>(mime));
105 }
106
107 static gboolean image_loader_external_close(gpointer, GError **)
108 {
109         return TRUE;
110 }
111
112 static void image_loader_external_abort(gpointer loader)
113 {
114         auto ld = static_cast<ImageLoaderExternal *>(loader);
115         ld->abort = TRUE;
116 }
117
118 static void image_loader_external_free(gpointer loader)
119 {
120         auto ld = static_cast<ImageLoaderExternal *>(loader);
121         if (ld->pixbuf) g_object_unref(ld->pixbuf);
122         g_free(ld);
123 }
124
125 void image_loader_backend_set_external(ImageLoaderBackend *funcs)
126 {
127         funcs->loader_new = image_loader_external_new;
128         funcs->set_size = image_loader_external_set_size;
129         funcs->load = image_loader_external_load;
130         funcs->write = nullptr;
131         funcs->get_pixbuf = image_loader_external_get_pixbuf;
132         funcs->close = image_loader_external_close;
133         funcs->abort = image_loader_external_abort;
134         funcs->free = image_loader_external_free;
135         funcs->get_format_name = image_loader_external_get_format_name;
136         funcs->get_format_mime_types = image_loader_external_get_format_mime_types;
137 }
138
139
140 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */