aboutsummaryrefslogtreecommitdiff
path: root/fastiv.c
blob: ad558848540c1da3356fd2c48910a87d8dbbd0df (plain)
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
//
// fastiv.c: fast image viewer
//
// 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.
//

#include <gtk/gtk.h>
#include <glib.h>
#include <glib/gstdio.h>

#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <locale.h>

#include <fnmatch.h>

#include "config.h"
#include "fastiv-io.h"
#include "fastiv-view.h"
#include "fastiv-browser.h"

// --- Utilities ---------------------------------------------------------------

static void
exit_fatal(const gchar *format, ...) G_GNUC_PRINTF(1, 2);

static void
exit_fatal(const gchar *format, ...)
{
	va_list ap;
	va_start(ap, format);

	gchar *format_nl = g_strdup_printf("%s\n", format);
	vfprintf(stderr, format_nl, ap);
	free(format_nl);

	va_end(ap);
	exit(EXIT_FAILURE);
}

/// Add `element` to the `output` set. `relation` is a map of sets of strings
/// defining is-a relations, and is traversed recursively.
static void
add_applying_transitive_closure(const gchar *element, GHashTable *relation,
	GHashTable *output)
{
	// Stop condition.
	if (!g_hash_table_add(output, g_strdup(element)))
		return;

	// TODO(p): Iterate over all aliases of `element` in addition to
	// any direct match (and rename this no-longer-generic function).
	GHashTable *targets = g_hash_table_lookup(relation, element);
	if (!targets)
		return;

	GHashTableIter iter;
	g_hash_table_iter_init(&iter, targets);

	gpointer key = NULL, value = NULL;
	while (g_hash_table_iter_next(&iter, &key, &value))
		add_applying_transitive_closure(key, relation, output);
}

// --- XDG ---------------------------------------------------------------------

gchar *
get_xdg_home_dir(const char *var, const char *default_)
{
	const char *env = getenv(var);
	if (env && *env == '/')
		return g_strdup(env);

	// The specification doesn't handle a missing HOME variable explicitly.
	// Implicitly, assuming Bourne shell semantics, it simply resolves empty.
	const char *home = getenv("HOME");
	return g_build_filename(home ? home : "", default_, NULL);
}

static gchar **
get_xdg_data_dirs(void)
{
	// GStrvBuilder is too new, it would help a little bit.
	GPtrArray *output = g_ptr_array_new_with_free_func(g_free);
	g_ptr_array_add(output, get_xdg_home_dir("XDG_DATA_HOME", ".local/share"));

	const gchar *xdg_data_dirs;
	if (!(xdg_data_dirs = getenv("XDG_DATA_DIRS")) || !*xdg_data_dirs)
		xdg_data_dirs = "/usr/local/share/:/usr/share/";

	gchar **candidates = g_strsplit(xdg_data_dirs, ":", 0);
	for (gchar **p = candidates; *p; p++) {
		if (**p == '/')
			g_ptr_array_add(output, *p);
		else
			g_free(*p);
	}
	g_free(candidates);
	g_ptr_array_add(output, NULL);
	return (gchar **) g_ptr_array_free(output, FALSE);
}

// --- Filtering ---------------------------------------------------------------

// Derived from shared-mime-info-spec 0.21.

static void
read_mime_subclasses(const gchar *path, GHashTable *subclass_sets)
{
	gchar *data = NULL;
	if (!g_file_get_contents(path, &data, NULL /* length */, NULL /* error */))
		return;

	// The format of this file is unspecified,
	// but in practice it's a list of space-separated media types.
	gchar *datasave = NULL;
	for (gchar *line = strtok_r(data, "\r\n", &datasave); line;
			line = strtok_r(NULL, "\r\n", &datasave)) {
		gchar *linesave = NULL,
			*subclass = strtok_r(line, " ", &linesave),
			*superclass = strtok_r(NULL, " ", &linesave);

		// Nothing about comments is specified, we're being nice.
		if (!subclass || *subclass == '#' || !superclass)
			continue;

		GHashTable *set = NULL;
		if (!(set = g_hash_table_lookup(subclass_sets, superclass))) {
			set = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
			g_hash_table_insert(subclass_sets, g_strdup(superclass), set);
		}
		g_hash_table_add(set, g_strdup(subclass));
	}
	g_free(data);
}

static gboolean
filter_mime_globs(const gchar *path, guint is_globs2, GHashTable *supported_set,
	GHashTable *output_set)
{
	gchar *data = NULL;
	if (!g_file_get_contents(path, &data, NULL /* length */, NULL /* error */))
		return FALSE;

	gchar *datasave = NULL;
	for (const gchar *line = strtok_r(data, "\r\n", &datasave); line;
			line = strtok_r(NULL, "\r\n", &datasave)) {
		if (*line == '#')
			continue;

		// We do not support __NOGLOBS__, nor even parse out the "cs" flag.
		// The weight is irrelevant.
		gchar **f = g_strsplit(line, ":", 0);
		if (g_strv_length(f) >= is_globs2 + 2) {
			const gchar *type = f[is_globs2 + 0], *glob = f[is_globs2 + 1];
			if (g_hash_table_contains(supported_set, type))
				g_hash_table_add(output_set, g_utf8_strdown(glob, -1));
		}
		g_strfreev(f);
	}
	g_free(data);
	return TRUE;
}

static gchar **
get_supported_globs(const char **media_types)
{
	gchar **data_dirs = get_xdg_data_dirs();

	// The mime.cache format is inconvenient to parse,
	// we'll do it from the text files manually, and once only.
	GHashTable *subclass_sets = g_hash_table_new_full(g_str_hash, g_str_equal,
		g_free, (GDestroyNotify) g_hash_table_destroy);
	for (gsize i = 0; data_dirs[i]; i++) {
		gchar *path =
			g_build_filename(data_dirs[i], "mime", "subclasses", NULL);
		read_mime_subclasses(path, subclass_sets);
		g_free(path);
	}

	// A hash set of all supported media types, including subclasses,
	// but not aliases.
	GHashTable *supported =
		g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
	while (*media_types) {
		add_applying_transitive_closure(*media_types++,
			subclass_sets, supported);
	}
	g_hash_table_destroy(subclass_sets);

	// We do not support the distinction of case-sensitive globs (:cs).
	GHashTable *globs =
		g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
	for (gsize i = 0; data_dirs[i]; i++) {
		gchar *path2 = g_build_filename(data_dirs[i], "mime", "globs2", NULL);
		gchar *path1 = g_build_filename(data_dirs[i], "mime", "globs", NULL);
		if (!filter_mime_globs(path2, TRUE, supported, globs))
			filter_mime_globs(path1, FALSE, supported, globs);
		g_free(path2);
		g_free(path1);
	}
	g_strfreev(data_dirs);
	g_hash_table_destroy(supported);

	gchar **result = (gchar **) g_hash_table_get_keys_as_array(globs, NULL);
	g_hash_table_steal_all(globs);
	g_hash_table_destroy(globs);
	return result;
}

// --- Main --------------------------------------------------------------------

struct {
	gchar **supported_globs;

	gchar *directory;
	GPtrArray *files;
	gint files_index;

	gchar *basename;

	GtkWidget *window;
	GtkWidget *view;
	GtkWidget *browser;
	GtkWidget *browser_scroller;
} g;

static gboolean
is_supported(const gchar *filename)
{
	gchar *utf8 = g_filename_to_utf8(filename, -1, NULL, NULL, NULL);
	if (!utf8)
		return FALSE;

	gchar *lowercased = g_utf8_strdown(utf8, -1);
	g_free(utf8);

	// XXX: fnmatch() uses the /locale/ encoding, but who cares nowadays.
	for (gchar **p = g.supported_globs; *p; p++)
		if (!fnmatch(*p, lowercased, 0)) {
			g_free(lowercased);
			return TRUE;
		}

	g_free(lowercased);
	return FALSE;
}

static void
show_error_dialog(GError *error)
{
	GtkWidget *dialog = gtk_message_dialog_new(GTK_WINDOW(g.window),
		GTK_DIALOG_MODAL,
		GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, "%s", error->message);
	gtk_dialog_run(GTK_DIALOG(dialog));
	gtk_widget_destroy(dialog);
	g_error_free(error);
}

static void
load_directory(const gchar *dirname)
{
	free(g.directory);
	g.directory = g_strdup(dirname);
	g_ptr_array_set_size(g.files, 0);
	g.files_index = -1;

	fastiv_browser_load(FASTIV_BROWSER(g.browser), dirname);

	GError *error = NULL;
	GDir *dir = g_dir_open(dirname, 0, &error);
	if (dir) {
		for (const gchar *name = NULL; (name = g_dir_read_name(dir)); ) {
			// This really wants to make you use readdir() directly.
			char *absolute = g_canonicalize_filename(name, g.directory);
			gboolean is_dir = g_file_test(absolute, G_FILE_TEST_IS_DIR);
			g_free(absolute);
			if (is_dir || !is_supported(name))
				continue;

			// XXX: We presume that this basename is from the same directory.
			if (!g_strcmp0(g.basename, name))
				g.files_index = g.files->len;

			g_ptr_array_add(g.files, g_strdup(name));
		}
		g_dir_close(dir);
	} else {
		show_error_dialog(error);
	}
	g_ptr_array_add(g.files, NULL);

	// XXX: When something outside the filtered entries is open, the index is
	// kept at -1, and browsing doesn't work. How to behave here?
	// Should we add it to the pointer array as an exception?
}

static void
open(const gchar *path)
{
	g_return_if_fail(g_path_is_absolute(path));

	GError *error = NULL;
	if (!fastiv_view_open(FASTIV_VIEW(g.view), path, &error)) {
		show_error_dialog(error);
		return;
	}

	gtk_window_set_title(GTK_WINDOW(g.window), path);

	gchar *basename = g_path_get_basename(path);
	g_free(g.basename);
	g.basename = basename;

	// So that load_directory() itself can be used for reloading.
	gchar *dirname = g_path_get_dirname(path);
	if (!g.directory || strcmp(dirname, g.directory)) {
		load_directory(dirname);
	} else {
		g.files_index = -1;
		for (guint i = 0; i + 1 < g.files->len; i++) {
			if (!g_strcmp0(g.basename, g_ptr_array_index(g.files, i)))
				g.files_index = i;
		}
	}
	g_free(dirname);

}

static void
on_open(void)
{
	GtkWidget *dialog = gtk_file_chooser_dialog_new("Open file",
		GTK_WINDOW(g.window), GTK_FILE_CHOOSER_ACTION_OPEN,
		"_Cancel", GTK_RESPONSE_CANCEL,
		"_Open", GTK_RESPONSE_ACCEPT, NULL);

	// NOTE: gdk-pixbuf has gtk_file_filter_add_pixbuf_formats().
	GtkFileFilter *filter = gtk_file_filter_new();
	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);

	GtkFileFilter *all_files = gtk_file_filter_new();
	gtk_file_filter_set_name(all_files, "All files");
	gtk_file_filter_add_pattern(all_files, "*");
	gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), all_files);

	// The default is local-only, single item. Paths are returned absolute.
	if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
		gchar *path = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
		open(path);
		g_free(path);
	}

	gtk_widget_destroy(dialog);
}

static void
on_previous(void)
{
	if (g.files_index >= 0) {
		int previous =
			(g.files->len - 1 + g.files_index - 1) % (g.files->len - 1);
		char *absolute =
			g_canonicalize_filename(g_ptr_array_index(g.files, previous),
				g.directory);
		open(absolute);
		g_free(absolute);
	}
}

static void
on_next(void)
{
	if (g.files_index >= 0) {
		int next = (g.files_index + 1) % (g.files->len - 1);
		char *absolute =
			g_canonicalize_filename(g_ptr_array_index(g.files, next),
				g.directory);
		open(absolute);
		g_free(absolute);
	}
}

int
main(int argc, char *argv[])
{
	if (!setlocale(LC_CTYPE, ""))
		exit_fatal("cannot set locale");

	gboolean show_version = FALSE;
	gchar **path_args = NULL;
	const GOptionEntry options[] = {
		{G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &path_args,
			NULL, "[FILE | DIRECTORY]"},
		{"version", 'V', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE,
		 &show_version, "output version information and exit", NULL},
		{},
	};

	GError *error = NULL;
	if (!gtk_init_with_args(&argc, &argv, " - fast image viewer",
		options, NULL, &error))
		exit_fatal("%s", error->message);
	if (show_version) {
		printf(PROJECT_NAME " " PROJECT_VERSION "\n");
		return 0;
	}

	// NOTE: Firefox and Eye of GNOME both interpret multiple arguments
	// in a special way. This is problematic, because one-element lists
	// are unrepresentable.
	// TODO(p): Complain to the user if there's more than one argument.
	// Best show the help message, if we can figure that out.
	const gchar *path_arg = path_args ? path_args[0] : NULL;

	gtk_window_set_default_icon_name(PROJECT_NAME);

	const char *style = "fastiv-view, fastiv-browser { background: black; }";
	GtkCssProvider *provider = gtk_css_provider_new();
	gtk_css_provider_load_from_data(provider, style, strlen(style), NULL);
	gtk_style_context_add_provider_for_screen(gdk_screen_get_default(),
		GTK_STYLE_PROVIDER(provider), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);

	g.view = g_object_new(FASTIV_TYPE_VIEW, NULL);
	gtk_widget_show_all(g.view);

	g.browser_scroller = gtk_scrolled_window_new(NULL, NULL);
	g.browser = g_object_new(FASTIV_TYPE_BROWSER, NULL);
	gtk_widget_set_vexpand(g.browser, TRUE);
	gtk_widget_set_hexpand(g.browser, TRUE);
	gtk_container_add(GTK_CONTAINER(g.browser_scroller), g.browser);
	// TODO(p): Can we not do it here separately?
	gtk_widget_show_all(g.browser_scroller);

	GtkWidget *stack = gtk_stack_new();
	gtk_stack_set_transition_type(
		GTK_STACK(stack), GTK_STACK_TRANSITION_TYPE_NONE);
	gtk_container_add(GTK_CONTAINER(stack), g.view);
	gtk_container_add(GTK_CONTAINER(stack), g.browser_scroller);

	g.window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	g_signal_connect(g.window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
	gtk_container_add(GTK_CONTAINER(g.window), stack);

	// The references to closures are initially floating and sunk on connect.
	GtkAccelGroup *accel_group = gtk_accel_group_new();
	gtk_accel_group_connect(accel_group, GDK_KEY_Escape, 0, 0,
		g_cclosure_new(G_CALLBACK(gtk_main_quit), NULL, NULL));
	gtk_accel_group_connect(accel_group, GDK_KEY_q, 0, 0,
		g_cclosure_new(G_CALLBACK(gtk_main_quit), NULL, NULL));

	gtk_accel_group_connect(accel_group, GDK_KEY_o, 0, 0,
		g_cclosure_new(G_CALLBACK(on_open), NULL, NULL));
	gtk_accel_group_connect(accel_group, GDK_KEY_o, GDK_CONTROL_MASK, 0,
		g_cclosure_new(G_CALLBACK(on_open), NULL, NULL));

	// FIXME: The left/right arrows can't be bound this way at all.
	gtk_accel_group_connect(accel_group, GDK_KEY_Left, 0, 0,
		g_cclosure_new(G_CALLBACK(on_previous), NULL, NULL));
	gtk_accel_group_connect(accel_group, GDK_KEY_Page_Up, 0, 0,
		g_cclosure_new(G_CALLBACK(on_previous), NULL, NULL));
	gtk_accel_group_connect(accel_group, GDK_KEY_Right, 0, 0,
		g_cclosure_new(G_CALLBACK(on_next), NULL, NULL));
	gtk_accel_group_connect(accel_group, GDK_KEY_Page_Down, 0, 0,
		g_cclosure_new(G_CALLBACK(on_next), NULL, NULL));
	gtk_accel_group_connect(accel_group, GDK_KEY_space, 0, 0,
		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(fastiv_io_supported_media_types);
	g.files = g_ptr_array_new_full(16, g_free);
	gchar *cwd = g_get_current_dir();

	// TODO(p): Desired behaviour:
	//  - No arguments: show directory view of the current working directory.
	//  - File argument: load its directory for browsing, open the file.
	//  - Directory argument: load its directory for browsing, show the dir.
	GStatBuf st;
	if (!path_arg) {
		load_directory(cwd);
	} else if (g_stat(path_arg, &st)) {
		show_error_dialog(g_error_new(G_FILE_ERROR,
			g_file_error_from_errno(errno),
			"%s: %s", path_arg, g_strerror(errno)));
		load_directory(cwd);
	} else {
		gchar *path_arg_absolute = g_canonicalize_filename(path_arg, cwd);
		if (S_ISDIR(st.st_mode))
			load_directory(path_arg_absolute);
		else
			open(path_arg_absolute);
		g_free(path_arg_absolute);
	}
	g_free(cwd);

	if (g.files_index < 0)
		gtk_stack_set_visible_child(GTK_STACK(stack), g.browser_scroller);

	gtk_widget_show_all(g.window);
	gtk_main();
	return 0;
}