aboutsummaryrefslogtreecommitdiff
path: root/fastiv-io.c
diff options
context:
space:
mode:
authorPřemysl Eric Janouch <p@janouch.name>2021-11-21 16:03:54 +0100
committerPřemysl Eric Janouch <p@janouch.name>2021-11-21 20:19:25 +0100
commit2b17ed838afb1ac1d63ca6e4d60945844263ca40 (patch)
tree08838b996451b11fdab01139bf6542893c55607d /fastiv-io.c
parentf4b727589bab39283ac7517c30442f519e6762b9 (diff)
downloadfiv-2b17ed838afb1ac1d63ca6e4d60945844263ca40.tar.gz
fiv-2b17ed838afb1ac1d63ca6e4d60945844263ca40.tar.xz
fiv-2b17ed838afb1ac1d63ca6e4d60945844263ca40.zip
Add ability to use different thumbnail sizes
Diffstat (limited to 'fastiv-io.c')
-rw-r--r--fastiv-io.c50
1 files changed, 45 insertions, 5 deletions
diff --git a/fastiv-io.c b/fastiv-io.c
index e1bb60a..60c0292 100644
--- a/fastiv-io.c
+++ b/fastiv-io.c
@@ -102,6 +102,29 @@ fastiv_io_all_supported_media_types(void)
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+GType
+fastiv_io_thumbnail_size_get_type(void)
+{
+ static gsize guard;
+ if (g_once_init_enter(&guard)) {
+#define XX(name, value, dir) {FASTIV_IO_THUMBNAIL_SIZE_ ## name, \
+ "FASTIV_IO_THUMBNAIL_SIZE_" #name, #name},
+ static const GEnumValue values[] = {FASTIV_IO_THUMBNAIL_SIZES(XX) {}};
+#undef XX
+ GType type = g_enum_register_static(
+ g_intern_static_string("FastivIoThumbnailSize"), values);
+ g_once_init_leave(&guard, type);
+ }
+ return guard;
+}
+
+#define XX(name, value, dir) {FASTIV_IO_THUMBNAIL_SIZE_ ## name, dir},
+FastivIoThumbnailSizeInfo fastiv_io_thumbnail_sizes[] = {
+ FASTIV_IO_THUMBNAIL_SIZES(XX) {}};
+#undef XX
+
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+
#define FASTIV_IO_ERROR fastiv_io_error_quark()
G_DEFINE_QUARK(fastiv-io-error-quark, fastiv_io_error)
@@ -981,8 +1004,20 @@ fail_init:
}
cairo_surface_t *
-fastiv_io_lookup_thumbnail(const gchar *target)
+fastiv_io_lookup_thumbnail(const gchar *target, FastivIoThumbnailSize size)
{
+ // TODO(p): Consider having linear enum constants,
+ // and using a trivial lookup table. This is ridiculous.
+ int size_index = 0;
+ while (fastiv_io_thumbnail_sizes[size_index].size &&
+ fastiv_io_thumbnail_sizes[size_index].size < size)
+ size_index++;
+ int size_count = size_index;
+ while (fastiv_io_thumbnail_sizes[size_count].size)
+ size_count++;
+
+ g_return_val_if_fail(fastiv_io_thumbnail_sizes[size_index].size, NULL);
+
GStatBuf st;
if (g_stat(target, &st))
return NULL;
@@ -997,17 +1032,22 @@ fastiv_io_lookup_thumbnail(const gchar *target)
gchar *cache_dir = get_xdg_home_dir("XDG_CACHE_HOME", ".cache");
cairo_surface_t *result = NULL;
- const gchar *sizes[] = {"large", "x-large", "xx-large", "normal"};
GError *error = NULL;
- for (gsize i = 0; !result && i < G_N_ELEMENTS(sizes); i++) {
- gchar *path = g_strdup_printf(
- "%s/thumbnails/%s/%s.png", cache_dir, sizes[i], sum);
+ for (int i = 0; i < size_count; i++) {
+ int size_use = size_index + i;
+ if (size_use >= size_count)
+ size_use = size_count - i - 1;
+
+ gchar *path = g_strdup_printf("%s/thumbnails/%s/%s.png", cache_dir,
+ fastiv_io_thumbnail_sizes[size_use].directory_name, sum);
result = read_spng_thumbnail(path, uri, st.st_mtim.tv_sec, &error);
if (error) {
g_debug("%s: %s", path, error->message);
g_clear_error(&error);
}
g_free(path);
+ if (result)
+ break;
}
g_free(cache_dir);