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
|
/* this file is part of evince, a gnome document viewer
*
* Copyright (C) 2007 Carlos Garcia Campos <carlosgc@gnome.org>
*
* Evince 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 of the License, or
* (at your option) any later version.
*
* Evince 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.
*/
#include <config.h>
#include <glib/gstdio.h>
#include "ev-module.h"
#include "ev-backends-manager.h"
static GList *ev_backends_list = NULL;
typedef struct _EvBackendInfo EvBackendInfo;
struct _EvBackendInfo {
gchar *module_name;
GTypeModule *module;
gboolean resident;
GType type_id;
gchar *type_desc;
gchar **mime_types;
};
#define EV_BACKENDS_GROUP "Evince Backend"
#define EV_BACKENDS_EXTENSION ".evince-backend"
static gchar *backendsdir = NULL;
static const gchar *
backends_dir (void)
{
if (!backendsdir) {
#ifdef G_OS_WIN32
gchar *dir;
dir = g_win32_get_package_installation_directory_of_module (NULL);
backendsdir = g_build_filename (dir, "lib", "evince",
EV_BACKENDSBINARYVERSION,
"backends", NULL);
g_free (dir);
#else
backendsdir = g_strdup (EV_BACKENDSDIR);
#endif
}
return backendsdir;
}
static void
ev_backend_info_free (EvBackendInfo *info)
{
g_free (info->module_name);
g_free (info->type_desc);
g_strfreev (info->mime_types);
g_free (info);
}
static EvBackendInfo *
ev_backends_manager_load_backend (const gchar *file)
{
EvBackendInfo *info;
GKeyFile *backend_file = NULL;
GError *error = NULL;
backend_file = g_key_file_new ();
if (!g_key_file_load_from_file (backend_file, file, G_KEY_FILE_NONE, &error)) {
g_warning ("Error opening backend file %s: %s",
file, error->message);
g_error_free (error);
g_key_file_free (backend_file);
return NULL;
}
info = g_new0 (EvBackendInfo, 1);
info->module_name = g_key_file_get_string (backend_file, EV_BACKENDS_GROUP,
"Module", NULL);
if (!info->module_name) {
g_warning ("Bad evince backend file %s: Could not find 'Module'",
file);
ev_backend_info_free (info);
g_key_file_free (backend_file);
return NULL;
}
info->resident = g_key_file_get_boolean (backend_file, EV_BACKENDS_GROUP,
"Resident", NULL);
info->type_desc = g_key_file_get_locale_string (backend_file, EV_BACKENDS_GROUP,
"TypeDescription", NULL, NULL);
if (!info->type_desc) {
g_warning ("Bad evince backend file %s: Could not find 'TypeDescription'",
file);
ev_backend_info_free (info);
g_key_file_free (backend_file);
return NULL;
}
info->mime_types = g_key_file_get_string_list (backend_file, EV_BACKENDS_GROUP,
"MimeType", NULL, NULL);
if (!info->mime_types) {
g_warning ("Bad evince backend file %s: Could not find 'MimeType'",
file);
ev_backend_info_free (info);
g_key_file_free (backend_file);
return NULL;
}
g_key_file_free (backend_file);
return info;
}
static gboolean
ev_backends_manager_load (void)
{
GDir *dir;
const gchar *dirent;
GError *error = NULL;
dir = g_dir_open (backends_dir(), 0, &error);
if (!dir) {
g_warning ("%s", error->message);
g_error_free (error);
return FALSE;
}
while ((dirent = g_dir_read_name (dir))) {
EvBackendInfo *info;
gchar *file;
if (!g_str_has_suffix (dirent, EV_BACKENDS_EXTENSION))
continue;
file = g_build_filename (backends_dir(), dirent, NULL);
info = ev_backends_manager_load_backend (file);
g_free (file);
if (!info)
continue;
ev_backends_list = g_list_prepend (ev_backends_list, info);
}
g_dir_close (dir);
return ev_backends_list != NULL;
}
/*
* _ev_backends_manager_init:
*
* Initializes the evince backends manager.
*
* Returns: %TRUE if there were any backends found; %FALSE otherwise
*/
gboolean
_ev_backends_manager_init (void)
{
if (ev_backends_list)
return TRUE;
return ev_backends_manager_load ();
}
/*
* _ev_backends_manager_shutdown:
*
* Shuts the evince backends manager down.
*/
void
_ev_backends_manager_shutdown (void)
{
g_list_foreach (ev_backends_list, (GFunc)ev_backend_info_free, NULL);
g_list_free (ev_backends_list);
ev_backends_list = NULL;
g_free (backendsdir);
}
static EvBackendInfo *
ev_backends_manager_get_backend_info (const gchar *mime_type)
{
GList *l;
for (l = ev_backends_list; l; l = g_list_next (l)) {
EvBackendInfo *info;
gint i = 0;
const char *mime;
info = (EvBackendInfo *)l->data;
while ((mime = info->mime_types[i++])) {
if (g_ascii_strcasecmp (mime, mime_type) == 0)
return info;
}
}
return NULL;
}
EvDocument *
ev_backends_manager_get_document (const gchar *mime_type)
{
EvDocument *document;
EvBackendInfo *info;
info = ev_backends_manager_get_backend_info (mime_type);
if (!info)
return NULL;
if (!info->module) {
gchar *path;
path = g_module_build_path (backends_dir(), info->module_name);
info->module = G_TYPE_MODULE (ev_module_new (path, info->resident));
g_free (path);
}
if (!g_type_module_use (info->module)) {
g_warning ("Cannot load backend '%s' since file '%s' cannot be read.",
info->module_name,
ev_module_get_path (EV_MODULE (info->module)));
g_object_unref (G_OBJECT (info->module));
info->module = NULL;
return NULL;
}
document = EV_DOCUMENT (ev_module_new_object (EV_MODULE (info->module)));
g_type_module_unuse (info->module);
return document;
}
static EvBackendInfo *
get_document_backend_info (EvDocument *document)
{
GList *l;
for (l = ev_backends_list; l; l = g_list_next (l)) {
EvBackendInfo *info;
GType type_id;
info = (EvBackendInfo *)l->data;
if (!info->module)
continue;
type_id = ev_module_get_object_type (EV_MODULE (info->module));
if (G_TYPE_CHECK_INSTANCE_TYPE (document, type_id)) {
return info;
}
}
return NULL;
}
const gchar *
ev_backends_manager_get_document_module_name (EvDocument *document)
{
EvBackendInfo *info;
info = get_document_backend_info (document);
return info ? info->module_name : NULL;
}
static EvTypeInfo *
ev_type_info_new (const gchar *desc, const gchar **mime_types)
{
EvTypeInfo *info;
info = g_new (EvTypeInfo, 1);
info->desc = desc;
info->mime_types = mime_types;
return info;
}
EvTypeInfo *
ev_backends_manager_get_document_type_info (EvDocument *document)
{
EvBackendInfo *info;
info = get_document_backend_info (document);
return info ?
ev_type_info_new (info->type_desc,
(const gchar **)info->mime_types)
: NULL;
}
GList *
ev_backends_manager_get_all_types_info (void)
{
GList *l;
GList *retval = NULL;
for (l = ev_backends_list; l; l = g_list_next (l)) {
EvBackendInfo *info;
EvTypeInfo *type_info;
info = (EvBackendInfo *)l->data;
type_info = ev_type_info_new (info->type_desc,
(const gchar **)info->mime_types);
retval = g_list_prepend (retval, type_info);
}
return retval;
}
|