-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathev-document-factory.c
726 lines (614 loc) · 22.5 KB
/
ev-document-factory.c
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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
/*
* Copyright (C) 2005, Red Hat, Inc.
* Copyright © 2018 Christian Persch
*
* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <string.h>
#include <gio/gio.h>
#include <glib/gstdio.h>
#include <glib/gi18n-lib.h>
#include <gtk/gtk.h>
#include "ev-backend-info.h"
#include "ev-document-factory.h"
#include "ev-file-helpers.h"
#include "ev-module.h"
#include "ev-backends-manager.h"
/* Backends manager */
#define BACKEND_DATA_KEY "ev-backend-info"
static GList *ev_backends_list = NULL;
static GHashTable *ev_module_hash = NULL;
static gchar *ev_backends_dir = NULL;
static EvDocument* ev_document_factory_new_document_for_mime_type (const char *mime_type,
GError **error);
static EvBackendInfo *
get_backend_info_for_mime_type (const gchar *mime_type)
{
GList *l;
gchar *content_type = g_content_type_from_mime_type (mime_type);
for (l = ev_backends_list; l; l = l->next) {
EvBackendInfo *info = (EvBackendInfo *) l->data;
char **mime_types = info->mime_types;
guint i;
for (i = 0; mime_types[i] != NULL; ++i) {
if (g_content_type_is_mime_type (content_type, mime_types[i])) {
g_free (content_type);
return info;
}
}
}
g_free (content_type);
return NULL;
}
static EvBackendInfo *
get_backend_info_for_document (EvDocument *document)
{
EvBackendInfo *info;
info = g_object_get_data (G_OBJECT (document), BACKEND_DATA_KEY);
g_warn_if_fail (info != NULL);
return info;
}
static EvDocument *
ev_document_factory_new_document_for_mime_type (const gchar *mime_type,
GError **error)
{
EvDocument *document;
EvBackendInfo *info;
GTypeModule *module = NULL;
g_return_val_if_fail (mime_type != NULL, NULL);
info = get_backend_info_for_mime_type (mime_type);
if (info == NULL) {
char *content_type, *mime_desc = NULL;
content_type = g_content_type_from_mime_type (mime_type);
if (content_type)
mime_desc = g_content_type_get_description (content_type);
g_set_error (error,
EV_DOCUMENT_ERROR,
EV_DOCUMENT_ERROR_INVALID,
_("File type %s (%s) is not supported"),
mime_desc ? mime_desc : "(unknown)", mime_type);
g_free (mime_desc);
g_free (content_type);
return NULL;
}
if (ev_module_hash != NULL) {
module = g_hash_table_lookup (ev_module_hash, info->module_name);
}
if (module == NULL) {
gchar *path;
path = g_module_build_path (ev_backends_dir, info->module_name);
module = G_TYPE_MODULE (_ev_module_new (path, info->resident));
g_free (path);
if (ev_module_hash == NULL) {
ev_module_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
g_free,
NULL /* leaked on purpose */);
}
g_hash_table_insert (ev_module_hash, g_strdup (info->module_name), module);
}
if (!g_type_module_use (module)) {
const char *err;
err = g_module_error ();
g_set_error (error, EV_DOCUMENT_ERROR, EV_DOCUMENT_ERROR_INVALID,
"Failed to load backend for '%s': %s",
mime_type, err ? err : "unknown error");
return NULL;
}
document = EV_DOCUMENT (_ev_module_new_object (EV_MODULE (module)));
g_type_module_unuse (module);
g_object_set_data_full (G_OBJECT (document), BACKEND_DATA_KEY,
_ev_backend_info_ref (info),
(GDestroyNotify) _ev_backend_info_unref);
return document;
}
static EvCompressionType
get_compression_from_mime_type (const gchar *mime_type)
{
gchar type[3];
gchar *p;
if (!(p = g_strrstr (mime_type, "/")))
return EV_COMPRESSION_NONE;
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;
else if (g_ascii_strcasecmp (type, "xz") == 0)
return EV_COMPRESSION_LZMA;
}
return EV_COMPRESSION_NONE;
}
/*
* new_document_for_uri:
* @uri: the document URI
* @fast: whether to use fast MIME type detection
* @compression: a location to store the document's compression type
* @error: a #GError location to store an error, or %NULL
*
* Creates a #EvDocument instance for the document at @uri, using either
* fast or slow MIME type detection. If a document could be created,
* @compression is filled in with the document's compression type.
* On error, %NULL is returned and @error filled in.
*
* Returns: a new #EvDocument instance, or %NULL on error with @error filled in
*/
static EvDocument *
new_document_for_uri (const char *uri,
gboolean fast,
EvCompressionType *compression,
GError **error)
{
EvDocument *document = NULL;
gchar *mime_type = NULL;
*compression = EV_COMPRESSION_NONE;
mime_type = ev_file_get_mime_type (uri, fast, error);
if (mime_type == NULL)
return NULL;
document = ev_document_factory_new_document_for_mime_type (mime_type, error);
if (document == NULL)
return NULL;
*compression = get_compression_from_mime_type (mime_type);
g_free (mime_type);
return document;
}
static void
free_uncompressed_uri (gchar *uri_unc)
{
if (!uri_unc)
return;
ev_tmp_uri_unlink (uri_unc);
g_free (uri_unc);
}
/*
* _ev_document_factory_init:
*
* Initializes the evince document factory.
*
* Returns: %TRUE if there were any backends found; %FALSE otherwise
*/
gboolean
_ev_document_factory_init (void)
{
if (ev_backends_list)
return TRUE;
if (g_getenv ("EV_BACKENDS_DIR") != NULL)
ev_backends_dir = g_strdup (g_getenv ("EV_BACKENDS_DIR"));
if (!ev_backends_dir) {
#ifdef G_OS_WIN32
{
gchar *dir;
dir = g_win32_get_package_installation_directory_of_module (NULL);
ev_backends_dir = g_build_filename (dir, "lib", "evince",
EV_BACKENDSBINARYVERSION,
"backends", NULL);
g_free (dir);
}
#else
ev_backends_dir = g_strdup (EV_BACKENDSDIR);
#endif
}
ev_backends_list = _ev_backend_info_load_from_dir (ev_backends_dir);
return ev_backends_list != NULL;
}
/*
* _ev_document_factory_shutdown:
*
* Shuts the evince document factory down.
*/
void
_ev_document_factory_shutdown (void)
{
g_list_foreach (ev_backends_list, (GFunc) _ev_backend_info_unref, NULL);
g_list_free (ev_backends_list);
ev_backends_list = NULL;
if (ev_module_hash != NULL) {
g_hash_table_unref (ev_module_hash);
ev_module_hash = NULL;
}
g_free (ev_backends_dir);
ev_backends_dir = NULL;
}
/**
* ev_document_factory_get_document_full:
* @uri: an URI
* @flags: flags from #EvDocumentLoadFlags
* @error: a #GError location to store an error, or %NULL
*
* Creates a #EvDocument for the document at @uri; or, if no backend handling
* the document's type is found, or an error occurred on opening the document,
* returns %NULL and fills in @error.
* If the document is encrypted, it is returned but also @error is set to
* %EV_DOCUMENT_ERROR_ENCRYPTED.
*
* Returns: (transfer full): a new #EvDocument, or %NULL
*/
EvDocument *
ev_document_factory_get_document_full (const char *uri,
EvDocumentLoadFlags flags,
GError **error)
{
EvDocument *document;
int result;
EvCompressionType compression;
gchar *uri_unc = NULL;
GError *err = NULL;
g_return_val_if_fail (uri != NULL, NULL);
document = new_document_for_uri (uri, TRUE, &compression, &err);
g_assert (document != NULL || err != NULL);
if (document != NULL) {
uri_unc = ev_file_uncompress (uri, compression, &err);
if (uri_unc) {
g_object_set_data_full (G_OBJECT (document),
"uri-uncompressed",
uri_unc,
(GDestroyNotify) free_uncompressed_uri);
} else if (err != NULL) {
/* Error uncompressing file */
g_object_unref (document);
g_propagate_error (error, err);
return NULL;
}
result = ev_document_load_full (document, uri_unc ? uri_unc : uri, flags, &err);
if (result == FALSE || err) {
if (err &&
(g_error_matches (err, EV_DOCUMENT_ERROR, EV_DOCUMENT_ERROR_ENCRYPTED) ||
g_error_matches (err, EV_DOCUMENT_ERROR, EV_DOCUMENT_ERROR_UNSUPPORTED_CONTENT))) {
g_propagate_error (error, err);
return document;
}
/* else fall through to slow mime code section below */
} else {
return document;
}
g_object_unref (document);
document = NULL;
}
/* Try again with slow mime detection */
g_clear_error (&err);
uri_unc = NULL;
document = new_document_for_uri (uri, FALSE, &compression, &err);
if (document == NULL) {
g_assert (err != NULL);
g_propagate_error (error, err);
return NULL;
}
uri_unc = ev_file_uncompress (uri, compression, &err);
if (uri_unc) {
g_object_set_data_full (G_OBJECT (document),
"uri-uncompressed",
uri_unc,
(GDestroyNotify) free_uncompressed_uri);
} else if (err != NULL) {
/* Error uncompressing file */
g_propagate_error (error, err);
g_object_unref (document);
return NULL;
}
result = ev_document_load_full (document, uri_unc ? uri_unc : uri,
EV_DOCUMENT_LOAD_FLAG_NONE, &err);
if (result == FALSE) {
if (err == NULL) {
/* FIXME: this really should not happen; the backend should
* always return a meaningful error.
*/
g_set_error_literal (&err,
EV_DOCUMENT_ERROR,
EV_DOCUMENT_ERROR_INVALID,
_("Unknown MIME Type"));
} else if (g_error_matches (err, EV_DOCUMENT_ERROR, EV_DOCUMENT_ERROR_ENCRYPTED)) {
g_propagate_error (error, err);
return document;
}
g_object_unref (document);
document = NULL;
g_propagate_error (error, err);
}
return document;
}
/**
* ev_document_factory_get_document:
* @uri: an URI
* @error: a #GError location to store an error, or %NULL
*
* Creates a #EvDocument for the document at @uri; or, if no backend handling
* the document's type is found, or an error occurred on opening the document,
* returns %NULL and fills in @error.
* If the document is encrypted, it is returned but also @error is set to
* %EV_DOCUMENT_ERROR_ENCRYPTED.
*
* Returns: (transfer full): a new #EvDocument, or %NULL
*/
EvDocument *
ev_document_factory_get_document (const char *uri, GError **error)
{
return ev_document_factory_get_document_full (uri,
EV_DOCUMENT_LOAD_FLAG_NONE,
error);
}
/**
* ev_document_factory_get_document_for_gfile:
* @file: a #GFile
* @flags: flags from #EvDocumentLoadFlags
* @cancellable: (allow-none): a #GCancellable, or %NULL
* @error: (allow-none): a #GError location to store an error, or %NULL
*
* Synchronously creates a #EvDocument for the document at @file; or, if no
* backend handling the document's type is found, or an error occurred on
* opening the document, returns %NULL and fills in @error.
* If the document is encrypted, it is returned but also @error is set to
* %EV_DOCUMENT_ERROR_ENCRYPTED.
*
* Returns: (transfer full): a new #EvDocument, or %NULL
*
* Since: 3.6
*/
EvDocument*
ev_document_factory_get_document_for_gfile (GFile *file,
EvDocumentLoadFlags flags,
GCancellable *cancellable,
GError **error)
{
EvDocument *document;
GFileInfo *file_info;
const char *content_type;
char *mime_type = NULL;
g_return_val_if_fail (G_IS_FILE (file), NULL);
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
file_info = g_file_query_info (file,
G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
G_FILE_QUERY_INFO_NONE,
cancellable,
error);
if (file_info == NULL)
return NULL;
content_type = g_file_info_get_content_type (file_info);
if (content_type == NULL) {
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
"Failed to query file mime type");
return NULL;
}
mime_type = g_content_type_get_mime_type (content_type);
g_object_unref (file_info);
document = ev_document_factory_new_document_for_mime_type (mime_type, error);
g_free (mime_type);
if (document == NULL)
return NULL;
if (!ev_document_load_gfile (document, file, flags, cancellable, error)) {
g_object_unref (document);
return NULL;
}
return document;
}
/**
* ev_document_factory_get_document_for_stream:
* @stream: a #GInputStream
* @mime_type: (allow-none): a mime type hint
* @flags: flags from #EvDocumentLoadFlags
* @cancellable: (allow-none): a #GCancellable, or %NULL
* @error: (allow-none): a #GError location to store an error, or %NULL
*
* Synchronously creates a #EvDocument for the document from @stream; or, if no
* backend handling the document's type is found, or an error occurred
* on opening the document, returns %NULL and fills in @error.
* If the document is encrypted, it is returned but also @error is set to
* %EV_DOCUMENT_ERROR_ENCRYPTED.
*
* If @mime_type is non-%NULL, this overrides any type inferred from the stream.
* If the mime type cannot be inferred from the stream, and @mime_type is %NULL,
* an error is returned.
*
* Returns: (transfer full): a new #EvDocument, or %NULL
*
* Since: 3.6
*/
EvDocument*
ev_document_factory_get_document_for_stream (GInputStream *stream,
const char *mime_type,
EvDocumentLoadFlags flags,
GCancellable *cancellable,
GError **error)
{
EvDocument *document;
char *mime = NULL;
g_return_val_if_fail (G_IS_INPUT_STREAM (stream), NULL);
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
if (mime_type == NULL && G_IS_FILE_INPUT_STREAM (stream)) {
GFileInfo *file_info;
const char *content_type;
file_info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (stream),
G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
cancellable,
error);
if (file_info != NULL) {
content_type = g_file_info_get_content_type (file_info);
if (content_type)
mime_type = mime = g_content_type_get_mime_type (content_type);
g_object_unref (file_info);
}
}
if (mime_type == NULL) {
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
"Cannot query mime type from stream");
return NULL;
}
document = ev_document_factory_new_document_for_mime_type (mime_type, error);
g_free (mime);
if (document == NULL)
return NULL;
if (!ev_document_load_stream (document, stream, flags, cancellable, error)) {
g_object_unref (document);
return NULL;
}
return document;
}
/**
* ev_document_factory_get_document_for_fd:
* @fd: a file descriptor
* @mime_type: the mime type
* @flags: flags from #EvDocumentLoadFlags
* @cancellable: (allow-none): a #GCancellable, or %NULL
* @error: (allow-none): a #GError location to store an error, or %NULL
*
* Synchronously creates a #EvDocument for the document from @fd using the backend
* for loading documents of type @mime_type; or, if the backend does not support
* loading from file descriptors, or an error occurred on opening the document,
* returns %NULL and fills in @error.
* If the document is encrypted, it is returned but also @error is set to
* %EV_DOCUMENT_ERROR_ENCRYPTED.
*
* If the mime type cannot be inferred from the file descriptor, and @mime_type is %NULL,
* an error is returned.
*
* Note that this function takes ownership of @fd; you must not ever
* operate on it again. It will be closed automatically if the document
* is destroyed, or if this function returns %NULL.
*
* Returns: (transfer full): a new #EvDocument, or %NULL
*
* Since: 42.0
*/
EvDocument*
ev_document_factory_get_document_for_fd (int fd,
const char *mime_type,
EvDocumentLoadFlags flags,
GCancellable *cancellable,
GError **error)
{
EvDocument *document;
g_return_val_if_fail (fd != -1, NULL);
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
if (mime_type == NULL) {
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
"Cannot query mime type from file descriptor");
close (fd);
return NULL;
}
document = ev_document_factory_new_document_for_mime_type (mime_type, error);
if (document == NULL) {
close (fd);
return NULL;
}
if (!ev_document_load_fd (document, fd, flags, cancellable, error)) {
/* fd is now consumed */
g_object_unref (document);
return NULL;
}
return document;
}
static void
file_filter_add_mime_types (EvBackendInfo *info, GtkFileFilter *filter)
{
char **mime_types;
guint i;
mime_types = info->mime_types;
if (mime_types == NULL)
return;
for (i = 0; mime_types[i] != NULL; ++i)
gtk_file_filter_add_mime_type (filter, mime_types[i]);
}
/**
* ev_document_factory_add_filters:
* @chooser: a #GtkFileChooser
* @document: a #EvDocument, or %NULL
*
* Adds some file filters to @chooser.
* Always add a "All documents" format.
*
* If @document is not %NULL, adds a #GtkFileFilter for @document's MIME type.
*
* If @document is %NULL, adds a #GtkFileFilter for each document type that evince
* can handle.
*/
void
ev_document_factory_add_filters (GtkWidget *chooser, EvDocument *document)
{
GtkFileFilter *filter;
GtkFileFilter *default_filter;
GtkFileFilter *document_filter;
g_return_if_fail (GTK_IS_FILE_CHOOSER (chooser));
g_return_if_fail (document == NULL || EV_IS_DOCUMENT (document));
default_filter = document_filter = filter = gtk_file_filter_new ();
gtk_file_filter_set_name (filter, _("All Documents"));
g_list_foreach (ev_backends_list, (GFunc)file_filter_add_mime_types, filter);
gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
if (document) {
EvBackendInfo *info;
info = get_backend_info_for_document (document);
g_assert (info != NULL);
default_filter = filter = gtk_file_filter_new ();
gtk_file_filter_set_name (filter, info->type_desc);
file_filter_add_mime_types (info, filter);
gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
} else {
GList *l;
for (l = ev_backends_list; l; l = l->next) {
EvBackendInfo *info = (EvBackendInfo *) l->data;
default_filter = filter = gtk_file_filter_new ();
gtk_file_filter_set_name (filter, info->type_desc);
file_filter_add_mime_types (info, filter);
gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
}
}
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);
}
/* Deprecated API/ABI compatibility wrappers */
/**
* ev_backends_manager_get_document:
* @mime_type: a mime type hint
*
* Returns: (transfer full): a new #EvDocument
*/
EvDocument *ev_backends_manager_get_document (const gchar *mime_type)
{
return ev_document_factory_new_document_for_mime_type (mime_type, NULL);
}
const gchar *
ev_backends_manager_get_document_module_name (EvDocument *document)
{
EvBackendInfo *info = get_backend_info_for_document (document);
if (info == NULL)
return NULL;
return info->module_name;
}
/**
* ev_backends_manager_get_document_type_info: (skip)
* @document: a #EvDocument
*
* Returns: a EvTypeInfo
*/
EvTypeInfo *ev_backends_manager_get_document_type_info (EvDocument *document)
{
return (EvTypeInfo *) get_backend_info_for_document (document);
}
/**
* ev_backends_manager_get_all_types_info: (skip)
*
* Returns: @list: (element-type EvBackendInfo): a shallow copy of #GList
* containing #EvBackendInfo objects
*/
GList *
ev_backends_manager_get_all_types_info (void)
{
return g_list_copy (ev_backends_list);
}