Compile modules based on HAVE_* features
[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 namespace
39 {
40
41 struct ImageLoaderCOLLECTION : public ImageLoaderBackend
42 {
43 public:
44         ~ImageLoaderCOLLECTION() override;
45
46         void init(AreaUpdatedCb area_updated_cb, SizePreparedCb size_prepared_cb, AreaPreparedCb area_prepared_cb, gpointer data) override;
47         gboolean write(const guchar *buf, gsize &chunk_size, gsize count, GError **error) override;
48         GdkPixbuf *get_pixbuf() override;
49         gchar *get_format_name() override;
50         gchar **get_format_mime_types() override;
51
52 private:
53         AreaUpdatedCb area_updated_cb;
54         gpointer data;
55
56         GdkPixbuf *pixbuf;
57 };
58
59 gboolean ImageLoaderCOLLECTION::write(const guchar *, gsize &chunk_size, gsize count, GError **)
60 {
61         auto il = static_cast<ImageLoader *>(data);
62
63         #define LINE_LENGTH 1000
64
65         gboolean ret = FALSE;
66         gchar *randname;
67         gchar *cmd_line;
68         FILE *fp = nullptr;
69         gint line_count = 0;
70         GString *file_names = g_string_new(nullptr);
71         gchar line[LINE_LENGTH];
72         gchar **split_line = nullptr;
73         gchar *cache_found;
74         gchar *pathl;
75
76         if (runcmd("which montage >/dev/null 2>&1") == 0)
77                 {
78                 pathl = path_from_utf8(il->fd->path);
79                 fp = fopen(pathl, "r");
80                 g_free(pathl);
81                 if (fp)
82                         {
83                         while(fgets(line, LINE_LENGTH, fp) && line_count < options->thumbnails.collection_preview)
84                                 {
85                                 if (line[0] && line[0] != '#')
86                                         {
87                                         split_line = g_strsplit(line, "\"", 4);
88                                         cache_found = cache_find_location(CACHE_TYPE_THUMB, split_line[1]);
89                                         if (cache_found)
90                                                 {
91                                                 g_string_append_printf(file_names, "\"%s\" ", cache_found);
92                                                 line_count++;
93                                                 }
94                                         g_free(cache_found);
95                                         }
96                                         if (split_line)
97                                                 {
98                                                 g_strfreev(split_line);
99                                                 }
100                                         split_line = nullptr;
101                                 }
102                         fclose(fp);
103
104                         if (file_names->len > 0)
105                                 {
106                                 randname = g_strdup("/tmp/geeqie_collection_XXXXXX.png");
107                                 g_mkstemp(randname);
108
109                                 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);
110
111                                 runcmd(cmd_line);
112                                 pixbuf = gdk_pixbuf_new_from_file(randname, nullptr);
113                                 if (pixbuf)
114                                         {
115                                         area_updated_cb(nullptr, 0, 0, gdk_pixbuf_get_width(pixbuf), gdk_pixbuf_get_height(pixbuf), data);
116                                         }
117
118                                 unlink(randname);
119                                 g_free(randname);
120                                 g_free(cmd_line);
121
122                                 ret = TRUE;
123                                 }
124
125                         g_string_free(file_names, TRUE);
126                         }
127                 }
128         chunk_size = count;
129         return ret;
130 }
131
132 void ImageLoaderCOLLECTION::init(AreaUpdatedCb area_updated_cb, SizePreparedCb, AreaPreparedCb, gpointer data)
133 {
134         this->area_updated_cb = area_updated_cb;
135         this->data = data;
136 }
137
138 GdkPixbuf *ImageLoaderCOLLECTION::get_pixbuf()
139 {
140         return pixbuf;
141 }
142
143 gchar *ImageLoaderCOLLECTION::get_format_name()
144 {
145         return g_strdup("collection");
146 }
147
148 gchar **ImageLoaderCOLLECTION::get_format_mime_types()
149 {
150         static const gchar *mime[] = {"image/png", nullptr};
151         return g_strdupv(const_cast<gchar **>(mime));
152 }
153
154 ImageLoaderCOLLECTION::~ImageLoaderCOLLECTION()
155 {
156         if (pixbuf) g_object_unref(pixbuf);
157 }
158
159 } // namespace
160
161 std::unique_ptr<ImageLoaderBackend> get_image_loader_backend_collection()
162 {
163         return std::make_unique<ImageLoaderCOLLECTION>();
164 }
165
166 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */