]> www.fi.muni.cz Git - evince.git/blob - libdocument/ev-module.c
[dualscreen] fix crash on ctrl+w and fix control window closing
[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., 51 Franklin Street, Fifth Floor, 
20  * Boston, MA 02110-1301, 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         gboolean resident;
58
59         gchar *path;
60         GType type;
61 };
62
63 typedef GType (*EvModuleRegisterFunc) (GTypeModule *);
64
65 static void ev_module_init       (EvModule *action);
66 static void ev_module_class_init (EvModuleClass *class);
67
68 G_DEFINE_TYPE (EvModule, ev_module, G_TYPE_TYPE_MODULE)
69
70 static gboolean
71 ev_module_load (GTypeModule *gmodule)
72 {
73         EvModule *module = EV_MODULE (gmodule);
74         EvModuleRegisterFunc register_func;
75
76         module->library = g_module_open (module->path, 0);
77
78         if (!module->library) {
79                 g_warning ("%s", g_module_error ());
80
81                 return FALSE;
82         }
83
84         /* extract symbols from the lib */
85         if (!g_module_symbol (module->library, "register_evince_backend",
86                               (void *) &register_func)) {
87                 g_warning ("%s", g_module_error ());
88                 g_module_close (module->library);
89
90                 return FALSE;
91         }
92
93         /* symbol can still be NULL even though g_module_symbol
94          * returned TRUE */
95         if (!register_func) {
96                 g_warning ("Symbol 'register_evince_backend' should not be NULL");
97                 g_module_close (module->library);
98
99                 return FALSE;
100         }
101
102         module->type = register_func (gmodule);
103
104         if (module->type == 0) {
105                 g_warning ("Invalid evince backend contained by module %s", module->path);
106                 
107                 return FALSE;
108         }
109
110         if (module->resident)
111                 g_module_make_resident (module->library);
112
113         return TRUE;
114 }
115
116 static void
117 ev_module_unload (GTypeModule *gmodule)
118 {
119         EvModule *module = EV_MODULE (gmodule);
120
121         g_module_close (module->library);
122
123         module->library = NULL;
124         module->type = 0;
125 }
126
127 const gchar *
128 ev_module_get_path (EvModule *module)
129 {
130         g_return_val_if_fail (EV_IS_MODULE (module), NULL);
131
132         return module->path;
133 }
134
135 GObject *
136 ev_module_new_object (EvModule *module)
137 {
138         g_return_val_if_fail (EV_IS_MODULE (module), NULL);
139         
140         if (module->type == 0)
141                 return NULL;
142
143         return g_object_new (module->type, NULL);
144 }
145
146 GType
147 ev_module_get_object_type (EvModule *module)
148 {
149         g_return_val_if_fail (EV_IS_MODULE (module), 0);
150
151         return module->type;
152 }
153
154 static void
155 ev_module_init (EvModule *module)
156 {
157 }
158
159 static void
160 ev_module_finalize (GObject *object)
161 {
162         EvModule *module = EV_MODULE (object);
163
164         g_free (module->path);
165
166         G_OBJECT_CLASS (ev_module_parent_class)->finalize (object);
167 }
168
169 static void
170 ev_module_class_init (EvModuleClass *class)
171 {
172         GObjectClass *object_class = G_OBJECT_CLASS (class);
173         GTypeModuleClass *module_class = G_TYPE_MODULE_CLASS (class);
174
175         object_class->finalize = ev_module_finalize;
176
177         module_class->load = ev_module_load;
178         module_class->unload = ev_module_unload;
179 }
180
181 EvModule *
182 ev_module_new (const gchar *path,
183                gboolean     resident)
184 {
185         EvModule *result;
186
187         g_return_val_if_fail (path != NULL && path[0] != '\0', NULL);
188
189         result = g_object_new (EV_TYPE_MODULE, NULL);
190
191         g_type_module_set_name (G_TYPE_MODULE (result), path);
192         result->path = g_strdup (path);
193         result->resident = resident;
194
195         return result;
196 }