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