Deduplicate cr3 image loader
[geeqie.git] / src / image-load-collection.cc
1 /*
2  * Copyright (C) 20018 - 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-collection.h"
22
23 #include <unistd.h>
24
25 #include <cstdio>
26
27 #include <gdk-pixbuf/gdk-pixbuf.h>
28 #include <glib-object.h>
29 #include <glib.h>
30
31 #include "cache.h"
32 #include "filedata.h"
33 #include "image-load.h"
34 #include "misc.h"
35 #include "options.h"
36 #include "ui-fileops.h"
37
38 struct ImageLoaderCOLLECTION {
39         ImageLoaderBackendCbAreaUpdated area_updated_cb;
40         ImageLoaderBackendCbSize size_cb;
41         ImageLoaderBackendCbAreaPrepared area_prepared_cb;
42         gpointer data;
43         GdkPixbuf *pixbuf;
44         guint requested_width;
45         guint requested_height;
46         gboolean abort;
47 };
48
49 static gboolean image_loader_collection_load(gpointer loader, const guchar *, gsize, GError **)
50 {
51         auto ld = static_cast<ImageLoaderCOLLECTION *>(loader);
52         auto il = static_cast<ImageLoader *>(ld->data);
53
54         #define LINE_LENGTH 1000
55
56         gboolean ret = FALSE;
57         gchar *randname;
58         gchar *cmd_line;
59         FILE *fp = nullptr;
60         gint line_count = 0;
61         GString *file_names = g_string_new(nullptr);
62         gchar line[LINE_LENGTH];
63         gchar **split_line = nullptr;
64         gchar *cache_found;
65         gchar *pathl;
66
67         if (runcmd("which montage >/dev/null 2>&1") == 0)
68                 {
69                 pathl = path_from_utf8(il->fd->path);
70                 fp = fopen(pathl, "r");
71                 g_free(pathl);
72                 if (fp)
73                         {
74                         while(fgets(line, LINE_LENGTH, fp) && line_count < options->thumbnails.collection_preview)
75                                 {
76                                 if (line[0] && line[0] != '#')
77                                         {
78                                         split_line = g_strsplit(line, "\"", 4);
79                                         cache_found = cache_find_location(CACHE_TYPE_THUMB, split_line[1]);
80                                         if (cache_found)
81                                                 {
82                                                 g_string_append_printf(file_names, "\"%s\" ", cache_found);
83                                                 line_count++;
84                                                 }
85                                         g_free(cache_found);
86                                         }
87                                         if (split_line)
88                                                 {
89                                                 g_strfreev(split_line);
90                                                 }
91                                         split_line = nullptr;
92                                 }
93                         fclose(fp);
94
95                         if (file_names->len > 0)
96                                 {
97                                 randname = g_strdup("/tmp/geeqie_collection_XXXXXX.png");
98                                 g_mkstemp(randname);
99
100                                 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);
101
102                                 runcmd(cmd_line);
103                                 ld->pixbuf = gdk_pixbuf_new_from_file(randname, nullptr);
104                                 if (ld->pixbuf)
105                                         {
106                                         ld->area_updated_cb(loader, 0, 0, gdk_pixbuf_get_width(ld->pixbuf), gdk_pixbuf_get_height(ld->pixbuf), ld->data);
107                                         }
108
109                                 unlink(randname);
110                                 g_free(randname);
111                                 g_free(cmd_line);
112
113                                 ret = TRUE;
114                                 }
115
116                         g_string_free(file_names, TRUE);
117                         }
118                 }
119         return ret;
120 }
121
122 static gpointer image_loader_collection_new(ImageLoaderBackendCbAreaUpdated area_updated_cb, ImageLoaderBackendCbSize size_cb, ImageLoaderBackendCbAreaPrepared area_prepared_cb, gpointer data)
123 {
124         auto loader = g_new0(ImageLoaderCOLLECTION, 1);
125         loader->area_updated_cb = area_updated_cb;
126         loader->size_cb = size_cb;
127         loader->area_prepared_cb = area_prepared_cb;
128         loader->data = data;
129         return loader;
130 }
131
132 static void image_loader_collection_set_size(gpointer loader, int width, int height)
133 {
134         auto ld = static_cast<ImageLoaderCOLLECTION *>(loader);
135         ld->requested_width = width;
136         ld->requested_height = height;
137 }
138
139 static GdkPixbuf* image_loader_collection_get_pixbuf(gpointer loader)
140 {
141         auto ld = static_cast<ImageLoaderCOLLECTION *>(loader);
142         return ld->pixbuf;
143 }
144
145 static gchar* image_loader_collection_get_format_name(gpointer)
146 {
147         return g_strdup("collection");
148 }
149 static gchar** image_loader_collection_get_format_mime_types(gpointer)
150 {
151         static const gchar *mime[] = {"image/png", nullptr};
152         return g_strdupv(const_cast<gchar **>(mime));
153 }
154
155 static gboolean image_loader_collection_close(gpointer, GError **)
156 {
157         return TRUE;
158 }
159
160 static void image_loader_collection_abort(gpointer loader)
161 {
162         auto ld = static_cast<ImageLoaderCOLLECTION *>(loader);
163         ld->abort = TRUE;
164 }
165
166 static void image_loader_collection_free(gpointer loader)
167 {
168         auto ld = static_cast<ImageLoaderCOLLECTION *>(loader);
169         if (ld->pixbuf) g_object_unref(ld->pixbuf);
170         g_free(ld);
171 }
172
173 void image_loader_backend_set_collection(ImageLoaderBackend *funcs)
174 {
175         funcs->loader_new = image_loader_collection_new;
176         funcs->set_size = image_loader_collection_set_size;
177         funcs->load = image_loader_collection_load;
178         funcs->write = nullptr;
179         funcs->get_pixbuf = image_loader_collection_get_pixbuf;
180         funcs->close = image_loader_collection_close;
181         funcs->abort = image_loader_collection_abort;
182         funcs->free = image_loader_collection_free;
183         funcs->get_format_name = image_loader_collection_get_format_name;
184         funcs->get_format_mime_types = image_loader_collection_get_format_mime_types;
185 }
186
187 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */