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