-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathimage-load-collection.cc
150 lines (123 loc) · 3.91 KB
/
image-load-collection.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
* Copyright (C) 20018 - The Geeqie Team
*
* Author: Colin Clark
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "image-load-collection.h"
#include <cstdio>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <glib-object.h>
#include <glib.h>
#include "cache.h"
#include "filedata.h"
#include "image-load.h"
#include "misc.h"
#include "options.h"
#include "ui-fileops.h"
namespace
{
struct ImageLoaderCOLLECTION : public ImageLoaderBackend
{
public:
~ImageLoaderCOLLECTION() override;
void init(AreaUpdatedCb area_updated_cb, SizePreparedCb size_prepared_cb, AreaPreparedCb area_prepared_cb, gpointer data) override;
gboolean write(const guchar *buf, gsize &chunk_size, gsize count, GError **error) override;
GdkPixbuf *get_pixbuf() override;
gchar *get_format_name() override;
gchar **get_format_mime_types() override;
private:
AreaUpdatedCb area_updated_cb;
gpointer data;
GdkPixbuf *pixbuf;
};
gboolean ImageLoaderCOLLECTION::write(const guchar *, gsize &chunk_size, gsize count, GError **)
{
auto il = static_cast<ImageLoader *>(data);
#define LINE_LENGTH 1000
gboolean ret = FALSE;
FILE *fp = nullptr;
gint line_count = 0;
g_autoptr(GString) file_names = g_string_new(nullptr);
gchar line[LINE_LENGTH];
if (runcmd("which montage >/dev/null 2>&1") == 0)
{
g_autofree gchar *pathl = path_from_utf8(il->fd->path);
fp = fopen(pathl, "r");
if (fp)
{
while(fgets(line, LINE_LENGTH, fp) && line_count < options->thumbnails.collection_preview)
{
if (line[0] && line[0] != '#')
{
g_auto(GStrv) split_line = g_strsplit(line, "\"", 4);
g_autofree gchar *cache_found = cache_find_location(CACHE_TYPE_THUMB, split_line[1]);
if (cache_found)
{
g_string_append_printf(file_names, "\"%s\" ", cache_found);
line_count++;
}
}
}
fclose(fp);
if (file_names->len > 0)
{
g_autofree gchar *randname = g_strdup("/tmp/geeqie_collection_XXXXXX.png");
g_mkstemp(randname);
g_autofree gchar *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);
runcmd(cmd_line);
pixbuf = gdk_pixbuf_new_from_file(randname, nullptr);
if (pixbuf)
{
area_updated_cb(nullptr, 0, 0, gdk_pixbuf_get_width(pixbuf), gdk_pixbuf_get_height(pixbuf), data);
}
std::remove(randname);
ret = TRUE;
}
}
}
chunk_size = count;
return ret;
}
void ImageLoaderCOLLECTION::init(AreaUpdatedCb area_updated_cb, SizePreparedCb, AreaPreparedCb, gpointer data)
{
this->area_updated_cb = area_updated_cb;
this->data = data;
}
GdkPixbuf *ImageLoaderCOLLECTION::get_pixbuf()
{
return pixbuf;
}
gchar *ImageLoaderCOLLECTION::get_format_name()
{
return g_strdup("collection");
}
gchar **ImageLoaderCOLLECTION::get_format_mime_types()
{
static const gchar *mime[] = {"image/png", nullptr};
return g_strdupv(const_cast<gchar **>(mime));
}
ImageLoaderCOLLECTION::~ImageLoaderCOLLECTION()
{
if (pixbuf) g_object_unref(pixbuf);
}
} // namespace
std::unique_ptr<ImageLoaderBackend> get_image_loader_backend_collection()
{
return std::make_unique<ImageLoaderCOLLECTION>();
}
/* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */