]> www.fi.muni.cz Git - evince.git/blob - libdocument/ev-module.c
Plugin system for backends. Fixes bug #351348.
[evince.git] / libdocument / ev-module.c
1 /*
2  * ev-module.c
3  * This file is part of Evince
4  *
5  * Copyright (C) 2005 - Paolo Maggi 
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, 
20  * Boston, MA 02111-1307, USA. 
21  */
22  
23 /* This is a modified version of ephy-module.c from Epiphany source code.
24  * Here the original copyright assignment:
25  *
26  *  Copyright (C) 2003 Marco Pesenti Gritti
27  *  Copyright (C) 2003, 2004 Christian Persch
28  *
29  */
30
31 /*
32  * Modified by the gedit Team, 2005. See the AUTHORS file for a 
33  * list of people on the gedit Team.  
34  * See the ChangeLog files for a list of changes. 
35  *
36  * $Id: gedit-module.c 5367 2006-12-17 14:29:49Z pborelli $
37  */
38
39 /* Modified by evince team */
40
41 #include "config.h"
42
43 #include "ev-module.h"
44
45 #include <gmodule.h>
46
47 typedef struct _EvModuleClass EvModuleClass;
48
49 struct _EvModuleClass {
50         GTypeModuleClass parent_class;
51 };
52
53 struct _EvModule {
54         GTypeModule parent_instance;
55
56         GModule *library;
57
58         gchar *path;
59         GType type;
60 };
61
62 typedef GType (*EvModuleRegisterFunc) (GTypeModule *);
63
64 static void ev_module_init       (EvModule *action);
65 static void ev_module_class_init (EvModuleClass *class);
66
67 G_DEFINE_TYPE (EvModule, ev_module, G_TYPE_TYPE_MODULE)
68
69 static gboolean
70 ev_module_load (GTypeModule *gmodule)
71 {
72         EvModule *module = EV_MODULE (gmodule);
73         EvModuleRegisterFunc register_func;
74
75         module->library = g_module_open (module->path, 0);
76
77         if (!module->library) {
78                 g_warning (g_module_error ());
79
80                 return FALSE;
81         }
82
83         /* extract symbols from the lib */
84         if (!g_module_symbol (module->library, "register_evince_backend",
85                               (void *) &register_func)) {
86                 g_warning (g_module_error ());
87                 g_module_close (module->library);
88
89                 return FALSE;
90         }
91
92         /* symbol can still be NULL even though g_module_symbol
93          * returned TRUE */
94         if (!register_func) {
95                 g_warning ("Symbol 'register_evince_backend' should not be NULL");
96                 g_module_close (module->library);
97
98                 return FALSE;
99         }
100
101         module->type = register_func (gmodule);
102
103         if (module->type == 0) {
104                 g_warning ("Invalid evince backend contained by module %s", module->path);
105                 
106                 return FALSE;
107         }
108
109         return TRUE;
110 }
111
112 static void
113 ev_module_unload (GTypeModule *gmodule)
114 {
115         EvModule *module = EV_MODULE (gmodule);
116
117         g_module_close (module->library);
118
119         module->library = NULL;
120         module->type = 0;
121 }
122
123 const gchar *
124 ev_module_get_path (EvModule *module)
125 {
126         g_return_val_if_fail (EV_IS_MODULE (module), NULL);
127
128         return module->path;
129 }
130
131 GObject *
132 ev_module_new_object (EvModule *module)
133 {
134         g_return_val_if_fail (EV_IS_MODULE (module), NULL);
135         
136         if (module->type == 0)
137                 return NULL;
138
139         return g_object_new (module->type, NULL);
140 }
141
142 GType
143 ev_module_get_object_type (EvModule *module)
144 {
145         g_return_val_if_fail (EV_IS_MODULE (module), 0);
146
147         return module->type;
148 }
149
150 static void
151 ev_module_init (EvModule *module)
152 {
153 }
154
155 static void
156 ev_module_finalize (GObject *object)
157 {
158         EvModule *module = EV_MODULE (object);
159
160         g_free (module->path);
161
162         G_OBJECT_CLASS (ev_module_parent_class)->finalize (object);
163 }
164
165 static void
166 ev_module_class_init (EvModuleClass *class)
167 {
168         GObjectClass *object_class = G_OBJECT_CLASS (class);
169         GTypeModuleClass *module_class = G_TYPE_MODULE_CLASS (class);
170
171         object_class->finalize = ev_module_finalize;
172
173         module_class->load = ev_module_load;
174         module_class->unload = ev_module_unload;
175 }
176
177 EvModule *
178 ev_module_new (const gchar *path)
179 {
180         EvModule *result;
181
182         g_return_val_if_fail (path != NULL && path[0] != '\0', NULL);
183
184         result = g_object_new (EV_TYPE_MODULE, NULL);
185
186         g_type_module_set_name (G_TYPE_MODULE (result), path);
187         result->path = g_strdup (path);
188
189         return result;
190 }