GitLab repository storage has been migrated to hashed layout. Please contact Infrastructure team if you notice any issues with repositories or hooks.

ev-document-factory.c 9.22 KB
Newer Older
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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; c-indent-level: 8 -*- */
/*
 *  Copyright (C) 2005, Red Hat, Inc. 
 *
 *  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, 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <string.h>
26

27
#include <gio/gio.h>
28
#include <glib/gstdio.h>
29
#include <glib/gi18n.h>
30
#include <gtk/gtk.h>
31

32 33
#include "ev-backends-manager.h"
#include "ev-document-factory.h"
34 35
#include "ev-file-helpers.h"

36
#ifdef ENABLE_PIXBUF
37 38
static GList*
gdk_pixbuf_mime_type_list ()
39 40
{
	GSList *formats, *list;
41
	GList *result = NULL;
42 43

	formats = gdk_pixbuf_get_formats ();
44
	for (list = formats; list != NULL; list = list->next) {
45
		GdkPixbufFormat *format = list->data;
46
		gchar          **mime_types;
47 48 49 50 51

		if (gdk_pixbuf_format_is_disabled (format))
			continue;

		mime_types = gdk_pixbuf_format_get_mime_types (format);
52
		result = g_list_prepend (result, mime_types); 
53 54
	}
	g_slist_free (formats);
55

56 57
	return result;
}
58

59 60 61 62 63 64 65
/* Would be nice to have this in gdk-pixbuf */
static gboolean
mime_type_supported_by_gdk_pixbuf (const gchar *mime_type)
{
	GList *mime_types;
	GList *list;
	gboolean retval = FALSE;
66

67 68
	mime_types = gdk_pixbuf_mime_type_list ();
	for (list = mime_types; list; list = list->next) {
69 70 71 72 73 74 75 76 77
		gchar      **mtypes = (gchar **)list->data;
		const gchar *mtype;
		gint         i = 0;

		while ((mtype = mtypes[i++])) {
			if (strcmp (mtype, mime_type) == 0) {
				retval = TRUE;
				break;
			}
78
		}
79
	}
80 81

	g_list_foreach (mime_types, (GFunc)g_strfreev, NULL);
82
	g_list_free (mime_types);
83 84 85

	return retval;
}
86
#endif /* ENABLE_PIXBUF */
87

88 89
static EvCompressionType
get_compression_from_mime_type (const gchar *mime_type)
90
{
91 92
	gchar type[3];
	gchar *p;
93

94 95
	if (!(p = g_strrstr (mime_type, "/")))
		return EV_COMPRESSION_NONE;
96

97 98 99 100 101
	if (sscanf (++p, "x-%2s%*s", type) == 1) {
		if (g_ascii_strcasecmp (type, "gz") == 0)
			return EV_COMPRESSION_GZIP;
		else if (g_ascii_strcasecmp (type, "bz") == 0)
			return EV_COMPRESSION_BZIP2;
102 103
	}

104
	return EV_COMPRESSION_NONE;
105
}
106

107
static gchar *
108
get_mime_type_from_uri (const gchar *uri, GError **error)
109
{
110 111 112
	GFile     *file;
	GFileInfo *file_info;
	gchar     *mime_type;
113 114 115 116

	file = g_file_new_for_uri (uri);
	file_info = g_file_query_info (file,
				       G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
117
				       0, NULL, error);
118 119 120 121 122
	g_object_unref (file);

	if (file_info == NULL)
		return NULL;

123
	mime_type = g_strdup (g_file_info_get_content_type (file_info));
124 125
	g_object_unref (file_info);

126
	return mime_type;
127 128
}

129
static gchar *
130
get_mime_type_from_data (const gchar *uri, GError **error)
131
{
132 133 134 135 136 137 138
	GFile            *file;
	GFileInputStream *input_stream;
	gssize            size_read;
	guchar            buffer[1024];

	file = g_file_new_for_uri (uri);
	
139
	input_stream = g_file_read (file, NULL, error);
140 141 142 143 144 145 146
	if (!input_stream) {
		g_object_unref (file);
		return NULL;
	}

	size_read = g_input_stream_read (G_INPUT_STREAM (input_stream),
					 buffer, 1024, NULL, NULL);
147
	g_input_stream_close (G_INPUT_STREAM (input_stream), NULL, error);
148 149 150 151 152 153 154 155 156

	g_object_unref (file);

	if (size_read == -1)
		return NULL;

	return g_content_type_guess (NULL, /* no filename */
				     buffer, 1024,
				     NULL);
157 158
}

159
static EvDocument *
160 161 162 163
get_document_from_uri (const char        *uri,
		       gboolean           slow,
		       EvCompressionType *compression,
		       GError           **error)
164 165
{
	EvDocument *document = NULL;
166
	gchar      *mime_type = NULL;
167

168 169
	*compression = EV_COMPRESSION_NONE;

170
	mime_type = slow ?
171 172
		get_mime_type_from_data (uri, error) :
		get_mime_type_from_uri (uri, error);
173

174
	if (mime_type == NULL) {
175
		g_free (mime_type);
176 177 178 179 180 181 182

		if (*error == NULL) {
			g_set_error (error,
				     EV_DOCUMENT_ERROR,
				     0,
				     _("Unknown MIME Type"));
		}
183
		
184 185
		return NULL;
	}
186

187 188
	document = ev_backends_manager_get_document (mime_type);
	
189
#ifdef ENABLE_PIXBUF
190
	if (!document && mime_type_supported_by_gdk_pixbuf (mime_type))
191 192 193
		document = ev_backends_manager_get_document ("image/*");
#endif /* ENABLE_PIXBUF */

194
	if (document == NULL) {
Carlos Garcia Campos's avatar
Carlos Garcia Campos committed
195 196 197
		gchar *mime_desc;

		mime_desc = g_content_type_get_description (mime_type);
198 199 200
		g_set_error (error,
			     EV_DOCUMENT_ERROR,	
			     0,
201
			     _("File type %s (%s) is not supported"),
Carlos Garcia Campos's avatar
Carlos Garcia Campos committed
202 203
			     mime_desc, mime_type);
		g_free (mime_desc);
204
		g_free (mime_type);
205

206
		return NULL;
207
	}
208

209
	*compression = get_compression_from_mime_type (mime_type);
210

211
	g_free (mime_type);
212 213 214 215
	
        return document;
}

216 217 218 219 220 221
static void
free_uncompressed_uri (gchar *uri_unc)
{
	if (!uri_unc)
		return;

222
	ev_tmp_uri_unlink (uri_unc);
223 224 225
	g_free (uri_unc);
}

226
EvDocument *
227
ev_document_factory_get_document (const char *uri, GError **error)
228 229
{
	EvDocument *document;
230
	int result;
231 232
	EvCompressionType compression;
	gchar *uri_unc = NULL;
233

234
	document = get_document_from_uri (uri, FALSE, &compression, error);
235
	if (*error == NULL) {
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
		uri_unc = ev_file_uncompress (uri, compression, error);
		if (uri_unc) {
			g_object_set_data_full (G_OBJECT (document),
						"uri-uncompressed",
						uri_unc,
						(GDestroyNotify) free_uncompressed_uri);
		}

		if (*error != NULL) {
			/* Error uncompressing file */
			if (document)
				g_object_unref (document);
			return NULL;
		}

		result = ev_document_load (document, uri_unc ? uri_unc : uri, error);
252 253 254 255 256 257 258

		if (result == FALSE || *error) {
			if (*error &&
			    (*error)->domain == EV_DOCUMENT_ERROR &&
			    (*error)->code == EV_DOCUMENT_ERROR_ENCRYPTED)
				return document;
		} else {
259
			return document;
260
		}
261
	}
262
	
263 264 265 266 267
	/* Try again with slow mime detection */
	if (document)
		g_object_unref (document);
	document = NULL;

268 269 270 271
	if (*error)
		g_error_free (*error);
	*error = NULL;

272 273 274
	uri_unc = NULL;

	document = get_document_from_uri (uri, TRUE, &compression, error);
275 276 277 278 279

	if (*error != NULL) {
		return NULL;
	}

280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
	uri_unc = ev_file_uncompress (uri, compression, error);
	if (uri_unc) {
		g_object_set_data_full (G_OBJECT (document),
					"uri-uncompressed",
					uri_unc,
					(GDestroyNotify) free_uncompressed_uri);
	}

	if (*error != NULL) {
		/* Error uncompressing file */
		if (document)
			g_object_unref (document);
		return NULL;
	}
	
	result = ev_document_load (document, uri_unc ? uri_unc : uri, error);
296 297 298 299 300 301 302 303 304 305 306 307

	if (result == FALSE) {
		if (*error == NULL) {
			g_set_error (error,
				     EV_DOCUMENT_ERROR,
				     0,
				     _("Unknown MIME Type"));
		} else if ((*error)->domain == EV_DOCUMENT_ERROR &&
			   (*error)->code == EV_DOCUMENT_ERROR_ENCRYPTED) {
			return document;
		}

308 309 310 311
		if (document)
			g_object_unref (document);
		document = NULL;
	}
312
	
313 314 315 316
	return document;
}

static void
317
file_filter_add_mime_types (EvTypeInfo *info, GtkFileFilter *filter)
318
{
319 320
	const gchar *mime_type;
	gint         i = 0;
321

322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
#ifdef ENABLE_PIXBUF
	if (g_ascii_strcasecmp (info->mime_types[0], "image/*") == 0) {
		GList *pixbuf_types, *l;

		pixbuf_types = gdk_pixbuf_mime_type_list ();
		for (l = pixbuf_types; l; l = g_list_next (l)) {
			gchar **mime_types = (gchar **)l->data;
			gint    j = 0;
			
			while ((mime_type = mime_types[j++]))
				gtk_file_filter_add_mime_type (filter, mime_type);
			
			g_strfreev (mime_types);
		}
		g_list_free (pixbuf_types);
337

338 339 340 341 342 343
		return;
	}
#endif /* ENABLE_PIXBUF */
	
	while ((mime_type = info->mime_types[i++]))
		gtk_file_filter_add_mime_type (filter, mime_type);
344 345 346 347 348
}

void 
ev_document_factory_add_filters (GtkWidget *chooser, EvDocument *document)
{
349
	GList         *all_types;
350 351 352 353
	GtkFileFilter *filter;
	GtkFileFilter *default_filter;
	GtkFileFilter *document_filter;

354 355
	all_types = ev_backends_manager_get_all_types_info ();
	
356 357
	default_filter = document_filter = filter = gtk_file_filter_new ();
	gtk_file_filter_set_name (filter, _("All Documents"));
358
	g_list_foreach (all_types, (GFunc)file_filter_add_mime_types, filter);
359 360
	gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);

361 362
	if (document) {
		EvTypeInfo *info;
363

364
		info = ev_backends_manager_get_document_type_info (document);
365
		default_filter = filter = gtk_file_filter_new ();
366 367 368
		gtk_file_filter_set_name (filter, info->desc);
		file_filter_add_mime_types (info, filter);
		g_free (info);
369
		gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
370 371
	} else {
		GList *l;
372

373 374
		for (l = all_types; l; l = g_list_next (l)){
			EvTypeInfo *info;
375

376
			info = (EvTypeInfo *)l->data;
377

378 379 380 381 382
			default_filter = filter = gtk_file_filter_new ();
			gtk_file_filter_set_name (filter, info->desc);
			file_filter_add_mime_types (info, filter);
			gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
		}
383 384
	}

385 386
	g_list_foreach (all_types, (GFunc)g_free, NULL);
	g_list_free (all_types);
387

388 389 390 391 392 393 394 395
	filter = gtk_file_filter_new ();
	gtk_file_filter_set_name (filter, _("All Files"));
	gtk_file_filter_add_pattern (filter, "*");
	gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);

	gtk_file_chooser_set_filter (GTK_FILE_CHOOSER (chooser),
				     document == NULL ? document_filter : default_filter);
}