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