summaryrefslogtreecommitdiff
path: root/src/eom-pixbuf-util.c
blob: 837bb42a139a0ec7d86f4cd24b7009831f9ad948 (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
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <string.h>
#include "eom-pixbuf-util.h"

GSList*
eom_pixbuf_get_savable_formats (void)
{
	GSList *list;
	GSList *write_list = NULL;
	GSList *it;

	list = gdk_pixbuf_get_formats ();

	for (it = list; it != NULL; it = it->next) {
		GdkPixbufFormat *format;

		format = (GdkPixbufFormat*) it->data;
		if (gdk_pixbuf_format_is_writable (format)) {
			write_list = g_slist_prepend (write_list, format);
		}
	}

	g_slist_free (list);
	write_list = g_slist_reverse (write_list);

	return write_list;
}

GdkPixbufFormat*
eom_pixbuf_get_format_by_suffix (const char *suffix)
{
	GSList *list;
	GSList *it;
	GdkPixbufFormat *result = NULL;

	g_return_val_if_fail (suffix != NULL, NULL);

	list = gdk_pixbuf_get_formats ();

	for (it = list; (it != NULL) && (result == NULL); it = it->next) {
		GdkPixbufFormat *format;
		gchar **extensions;
		int i;

		format = (GdkPixbufFormat*) it->data;

		extensions = gdk_pixbuf_format_get_extensions (format);
		for (i = 0; extensions[i] != NULL; i++) {
			/* g_print ("check extension: %s against %s\n", extensions[i], suffix); */
			if (g_ascii_strcasecmp (suffix, extensions[i]) == 0) {
				result = format;
				break;
			}
		}

		g_strfreev (extensions);
	}

	g_slist_free (list);

	return result;
}

char*
eom_pixbuf_get_common_suffix (GdkPixbufFormat *format)
{
	char **extensions;
	int i;
	char *result = NULL;

	if (format == NULL) return NULL;

	extensions = gdk_pixbuf_format_get_extensions (format);
	if (extensions[0] == NULL) return NULL;

	/* try to find 3-char suffix first, use the last occurence */
	for (i = 0; extensions [i] != NULL; i++) {
		if (strlen (extensions[i]) <= 3) {
			g_free (result);
			result = g_ascii_strdown (extensions[i], -1);
		}
	}

	/* otherwise take the first one */
	if (result == NULL) {
		result = g_ascii_strdown (extensions[0], -1);
	}

	g_strfreev (extensions);

	return result;
}

static char*
get_suffix_from_basename (const char *basename)
{
	char *suffix;
	char *suffix_start;
	guint len;

	/* FIXME: does this work for all locales? */
	suffix_start = g_utf8_strrchr (basename, -1, '.');

	if (suffix_start == NULL)
		return NULL;

	len = strlen (suffix_start) - 1;
	suffix = g_strndup (suffix_start+1, len);

	return suffix;

}

GdkPixbufFormat *
eom_pixbuf_get_format (GFile *file)
{
	GdkPixbufFormat *format;
	char *path, *basename, *suffix;
	g_return_val_if_fail (file != NULL, NULL);

	path = g_file_get_path (file);
	basename = g_path_get_basename (path);
	suffix = get_suffix_from_basename (basename);

	format = eom_pixbuf_get_format_by_suffix (suffix);

	g_free (path);
	g_free (basename);
	g_free (suffix);

	return format;
}