1 /* this file is part of evince, a gnome document viewer
3 * Copyright (C) 2007 Carlos Garcia Campos <carlosgc@gnome.org>
5 * Evince is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * Evince is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22 #include <glib/gstdio.h>
24 #include "ev-module.h"
25 #include "ev-backends-manager.h"
27 static GList *ev_backends_list = NULL;
29 typedef struct _EvBackendInfo EvBackendInfo;
30 struct _EvBackendInfo {
41 #define EV_BACKENDS_GROUP "Evince Backend"
42 #define EV_BACKENDS_EXTENSION ".evince-backend"
44 static gchar *backendsdir = NULL;
53 dir = g_win32_get_package_installation_directory_of_module (NULL);
54 backendsdir = g_build_filename (dir, "lib", "evince",
55 EV_BACKENDSBINARYVERSION,
59 backendsdir = g_strdup (EV_BACKENDSDIR);
67 ev_backend_info_free (EvBackendInfo *info)
69 g_free (info->module_name);
70 g_free (info->type_desc);
71 g_strfreev (info->mime_types);
75 static EvBackendInfo *
76 ev_backends_manager_load_backend (const gchar *file)
79 GKeyFile *backend_file = NULL;
82 backend_file = g_key_file_new ();
83 if (!g_key_file_load_from_file (backend_file, file, G_KEY_FILE_NONE, &error)) {
84 g_warning ("Error opening backend file %s: %s",
85 file, error->message);
87 g_key_file_free (backend_file);
92 info = g_new0 (EvBackendInfo, 1);
93 info->module_name = g_key_file_get_string (backend_file, EV_BACKENDS_GROUP,
95 if (!info->module_name) {
96 g_warning ("Bad evince backend file %s: Could not find 'Module'",
98 ev_backend_info_free (info);
99 g_key_file_free (backend_file);
104 info->resident = g_key_file_get_boolean (backend_file, EV_BACKENDS_GROUP,
107 info->type_desc = g_key_file_get_locale_string (backend_file, EV_BACKENDS_GROUP,
108 "TypeDescription", NULL, NULL);
109 if (!info->type_desc) {
110 g_warning ("Bad evince backend file %s: Could not find 'TypeDescription'",
112 ev_backend_info_free (info);
113 g_key_file_free (backend_file);
118 info->mime_types = g_key_file_get_string_list (backend_file, EV_BACKENDS_GROUP,
119 "MimeType", NULL, NULL);
120 if (!info->mime_types) {
121 g_warning ("Bad evince backend file %s: Could not find 'MimeType'",
123 ev_backend_info_free (info);
124 g_key_file_free (backend_file);
129 g_key_file_free (backend_file);
135 ev_backends_manager_load (void)
139 GError *error = NULL;
141 dir = g_dir_open (backends_dir(), 0, &error);
143 g_warning ("%s", error->message);
144 g_error_free (error);
149 while ((dirent = g_dir_read_name (dir))) {
153 if (!g_str_has_suffix (dirent, EV_BACKENDS_EXTENSION))
156 file = g_build_filename (backends_dir(), dirent, NULL);
157 info = ev_backends_manager_load_backend (file);
163 ev_backends_list = g_list_prepend (ev_backends_list, info);
168 return ev_backends_list != NULL;
172 * _ev_backends_manager_init:
174 * Initializes the evince backends manager.
176 * Returns: %TRUE if there were any backends found; %FALSE otherwise
179 _ev_backends_manager_init (void)
181 if (ev_backends_list)
184 return ev_backends_manager_load ();
188 * _ev_backends_manager_shutdown:
190 * Shuts the evince backends manager down.
193 _ev_backends_manager_shutdown (void)
195 g_list_foreach (ev_backends_list, (GFunc)ev_backend_info_free, NULL);
196 g_list_free (ev_backends_list);
197 ev_backends_list = NULL;
199 g_free (backendsdir);
202 static EvBackendInfo *
203 ev_backends_manager_get_backend_info (const gchar *mime_type)
207 for (l = ev_backends_list; l; l = g_list_next (l)) {
212 info = (EvBackendInfo *)l->data;
214 while ((mime = info->mime_types[i++])) {
215 if (g_ascii_strcasecmp (mime, mime_type) == 0)
224 ev_backends_manager_get_document (const gchar *mime_type)
226 EvDocument *document;
229 info = ev_backends_manager_get_backend_info (mime_type);
236 path = g_module_build_path (backends_dir(), info->module_name);
237 info->module = G_TYPE_MODULE (ev_module_new (path, info->resident));
241 if (!g_type_module_use (info->module)) {
242 g_warning ("Cannot load backend '%s' since file '%s' cannot be read.",
244 ev_module_get_path (EV_MODULE (info->module)));
245 g_object_unref (G_OBJECT (info->module));
251 document = EV_DOCUMENT (ev_module_new_object (EV_MODULE (info->module)));
252 g_type_module_unuse (info->module);
257 static EvBackendInfo *
258 get_document_backend_info (EvDocument *document)
262 for (l = ev_backends_list; l; l = g_list_next (l)) {
266 info = (EvBackendInfo *)l->data;
271 type_id = ev_module_get_object_type (EV_MODULE (info->module));
273 if (G_TYPE_CHECK_INSTANCE_TYPE (document, type_id)) {
282 ev_backends_manager_get_document_module_name (EvDocument *document)
286 info = get_document_backend_info (document);
287 return info ? info->module_name : NULL;
291 ev_type_info_new (const gchar *desc, const gchar **mime_types)
295 info = g_new (EvTypeInfo, 1);
298 info->mime_types = mime_types;
304 ev_backends_manager_get_document_type_info (EvDocument *document)
308 info = get_document_backend_info (document);
310 ev_type_info_new (info->type_desc,
311 (const gchar **)info->mime_types)
316 ev_backends_manager_get_all_types_info (void)
319 GList *retval = NULL;
321 for (l = ev_backends_list; l; l = g_list_next (l)) {
323 EvTypeInfo *type_info;
325 info = (EvBackendInfo *)l->data;
327 type_info = ev_type_info_new (info->type_desc,
328 (const gchar **)info->mime_types);
329 retval = g_list_prepend (retval, type_info);