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