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