Split out fastiv-io.h, move media types list

This commit is contained in:
Přemysl Eric Janouch 2021-11-01 04:40:58 +01:00
parent 7ca53b031e
commit ab283d3988
Signed by: p
GPG Key ID: A0420B94F92B9493
6 changed files with 49 additions and 25 deletions

View File

@ -18,6 +18,7 @@
#include <math.h>
#include "fastiv-browser.h"
#include "fastiv-io.h"
#include "fastiv-view.h"
typedef struct entry Entry;

View File

@ -37,6 +37,20 @@
#define WUFFS_CONFIG__MODULE__ZLIB
#include "wuffs-mirror-release-c/release/c/wuffs-v0.3.c"
// A subset of shared-mime-info that produces an appropriate list of
// file extensions. Chiefly motivated by the suckiness of RAW images:
// someone else will maintain the list of file extensions for us.
const char *fastiv_io_supported_media_types[] = {
"image/bmp",
"image/gif",
"image/png",
"image/jpeg",
#ifdef HAVE_LIBRAW
"image/x-dcraw",
#endif // HAVE_LIBRAW
NULL
};
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#define FASTIV_IO_ERROR fastiv_io_error_quark()

26
fastiv-io.h Normal file
View File

@ -0,0 +1,26 @@
//
// fastiv-io.h: image loaders
//
// Copyright (c) 2021, Přemysl Eric Janouch <p@janouch.name>
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
//
#pragma once
#include <cairo.h>
#include <glib.h>
extern const char *fastiv_io_supported_media_types[];
cairo_surface_t *fastiv_io_open(const gchar *path, GError **error);
cairo_surface_t *fastiv_io_lookup_thumbnail(const gchar *target);

View File

@ -17,6 +17,7 @@
#include <math.h>
#include "fastiv-io.h"
#include "fastiv-view.h"
struct _FastivView {

View File

@ -24,7 +24,3 @@ G_DECLARE_FINAL_TYPE(FastivView, fastiv_view, FASTIV, VIEW, GtkWidget)
/// Try to open the given file, synchronously, to be displayed by the widget.
gboolean fastiv_view_open(FastivView *self, const gchar *path, GError **error);
// Private, fastiv-io.c
cairo_surface_t *fastiv_io_open(const gchar *path, GError **error);
cairo_surface_t *fastiv_io_lookup_thumbnail(const gchar *target);

View File

@ -27,6 +27,7 @@
#include <fnmatch.h>
#include "config.h"
#include "fastiv-io.h"
#include "fastiv-view.h"
#include "fastiv-browser.h"
@ -115,21 +116,6 @@ get_xdg_data_dirs(void)
// Derived from shared-mime-info-spec 0.21.
// TODO(p): Move to fastiv-io.c, expose the prototype in a header file
// (perhaps finally start a new one for it).
// A subset of shared-mime-info that produces an appropriate list of
// file extensions. Chiefly motivated by the suckiness of RAW images:
// someone else will maintain the list of file extensions for us.
static const char *supported_media_types[] = {
"image/bmp",
"image/gif",
"image/png",
"image/jpeg",
#ifdef HAVE_LIBRAW
"image/x-dcraw",
#endif // HAVE_LIBRAW
};
static void
read_mime_subclasses(const gchar *path, GHashTable *subclass_sets)
{
@ -189,7 +175,7 @@ filter_mime_globs(const gchar *path, guint is_globs2, GHashTable *supported_set,
}
static gchar **
get_supported_globs (void)
get_supported_globs(const char **media_types)
{
gchar **data_dirs = get_xdg_data_dirs();
@ -208,8 +194,8 @@ get_supported_globs (void)
// but not aliases.
GHashTable *supported =
g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
for (gsize i = 0; i < G_N_ELEMENTS(supported_media_types); i++) {
add_applying_transitive_closure(supported_media_types[i],
while (*media_types) {
add_applying_transitive_closure(*media_types++,
subclass_sets, supported);
}
g_hash_table_destroy(subclass_sets);
@ -363,8 +349,8 @@ on_open(void)
// NOTE: gdk-pixbuf has gtk_file_filter_add_pixbuf_formats().
GtkFileFilter *filter = gtk_file_filter_new();
for (gsize i = 0; i < G_N_ELEMENTS(supported_media_types); i++)
gtk_file_filter_add_mime_type(filter, supported_media_types[i]);
for (const char **p = fastiv_io_supported_media_types; *p; p++)
gtk_file_filter_add_mime_type(filter, *p);
gtk_file_filter_set_name(filter, "Supported images");
gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter);
@ -496,7 +482,7 @@ main(int argc, char *argv[])
g_cclosure_new(G_CALLBACK(on_next), NULL, NULL));
gtk_window_add_accel_group(GTK_WINDOW(g.window), accel_group);
g.supported_globs = get_supported_globs();
g.supported_globs = get_supported_globs(fastiv_io_supported_media_types);
g.files = g_ptr_array_new_full(16, g_free);
gchar *cwd = g_get_current_dir();