Initial import of ephy (rev# 7126) from svn
[ephy-soc.git] / lib / ephy-shlib-loader.c
blob3cbc31582ddf29df6ce62a9f2abae2a805d69aad
1 /*
2 * Copyright © 2003 Marco Pesenti Gritti
3 * Copyright © 2003, 2004 Christian Persch
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
8 * any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * $Id: ephy-shlib-loader.c 6952 2007-03-11 19:42:02Z chpe $
22 #include "config.h"
24 #include "ephy-shlib-loader.h"
25 #include "ephy-loader.h"
26 #include "ephy-module.h"
27 #include "ephy-debug.h"
29 #include <string.h>
31 #define DATA_KEY "EphyShlibLoader::LoaderData"
33 typedef struct
35 EphyModule *module;
36 GObject *object;
37 } LoaderData;
39 #define EPHY_SHLIB_LOADER_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), EPHY_TYPE_SHLIB_LOADER, EphyShlibLoaderPrivate))
41 struct _EphyShlibLoaderPrivate
43 GSList *data;
46 static void ephy_shlib_loader_class_init (EphyShlibLoaderClass *klass);
47 static void ephy_shlib_loader_iface_init (EphyLoaderIface *iface);
48 static void ephy_shlib_loader_init (EphyShlibLoader *loader);
50 static GObjectClass *parent_class = NULL;
52 GType
53 ephy_shlib_loader_get_type (void)
55 static GType type = 0;
57 if (G_UNLIKELY (type == 0))
59 const GTypeInfo our_info =
61 sizeof (EphyShlibLoaderClass),
62 NULL, /* base_init */
63 NULL, /* base_finalize */
64 (GClassInitFunc) ephy_shlib_loader_class_init,
65 NULL,
66 NULL, /* class_data */
67 sizeof (EphyShlibLoader),
68 0, /* n_preallocs */
69 (GInstanceInitFunc) ephy_shlib_loader_init
72 const GInterfaceInfo loader_info =
74 (GInterfaceInitFunc) ephy_shlib_loader_iface_init,
75 NULL,
76 NULL
79 type = g_type_register_static (G_TYPE_OBJECT,
80 "EphyShlibLoader",
81 &our_info, 0);
83 g_type_add_interface_static (type,
84 EPHY_TYPE_LOADER,
85 &loader_info);
88 return type;
91 static void
92 free_loader_data (LoaderData *data)
94 g_return_if_fail (data != NULL);
96 /* data->module must NOT be unreffed! */
98 if (data->object != NULL)
100 g_object_unref (data->object);
103 g_free (data);
106 static void
107 ephy_shlib_loader_init (EphyShlibLoader *loader)
109 loader->priv = EPHY_SHLIB_LOADER_GET_PRIVATE (loader);
111 LOG ("EphyShlibLoader initialising");
114 static void
115 ephy_shlib_loader_finalize (GObject *object)
117 EphyShlibLoader *loader = EPHY_SHLIB_LOADER (object);
119 LOG ("EphyShlibLoader finalising");
121 g_slist_foreach (loader->priv->data, (GFunc) free_loader_data, NULL);
122 g_slist_free (loader->priv->data);
124 parent_class->finalize (object);
127 static int
128 find_library (const LoaderData *data,
129 const char *library)
131 return strcmp (ephy_module_get_path (data->module), library);
134 static int
135 find_object (const LoaderData *data,
136 const GObject *object)
138 return data->object != object;
141 static GObject *
142 impl_get_object (EphyLoader *eloader,
143 GKeyFile *keyfile)
145 EphyShlibLoader *loader = EPHY_SHLIB_LOADER (eloader);
146 GSList *l;
147 LoaderData *data = NULL;
148 char *library;
149 gboolean resident;
151 g_return_val_if_fail (keyfile != NULL, NULL);
153 library = g_key_file_get_string (keyfile, "Loader", "Library", NULL);
154 if (library == NULL)
156 g_warning ("NULL library name!\n");
157 return NULL;
160 resident = g_key_file_get_boolean (keyfile, "Loader", "Resident", NULL);
162 l = g_slist_find_custom (loader->priv->data, library,
163 (GCompareFunc) find_library);
165 if (l != NULL)
167 data = l->data;
168 g_return_val_if_fail (data != NULL, NULL);
170 if (data->object != NULL)
172 g_free (library);
173 return g_object_ref (data->object);
176 else
178 data = g_new0 (LoaderData, 1);
179 loader->priv->data = g_slist_prepend (loader->priv->data, data);
182 if (data->module == NULL)
184 data->module = ephy_module_new (library, resident);
187 g_return_val_if_fail (data->object == NULL, data->object);
189 if (g_type_module_use (G_TYPE_MODULE (data->module)) == FALSE)
191 g_free (library);
192 g_warning ("Could not load extension file at %s\n",
193 ephy_module_get_path (data->module));
194 return NULL;
197 data->object = ephy_module_new_object (data->module);
199 g_type_module_unuse (G_TYPE_MODULE (data->module));
201 if (data->object != NULL)
203 g_object_set_data (G_OBJECT (data->object), DATA_KEY, data);
206 g_free (library);
208 return data->object;
211 static void
212 impl_release_object (EphyLoader *eloader,
213 GObject *object)
215 EphyShlibLoader *loader = EPHY_SHLIB_LOADER (eloader);
216 GSList *l;
217 LoaderData *data;
219 l = g_slist_find_custom (loader->priv->data, object,
220 (GCompareFunc) find_object);
221 g_return_if_fail (l != NULL);
222 data = l->data;
224 g_object_unref (data->object);
225 data->object = NULL;
228 static void
229 ephy_shlib_loader_iface_init (EphyLoaderIface *iface)
231 iface->type = "shlib";
232 iface->get_object = impl_get_object;
233 iface->release_object = impl_release_object;
236 static void
237 ephy_shlib_loader_class_init (EphyShlibLoaderClass *klass)
239 GObjectClass *object_class = G_OBJECT_CLASS (klass);
241 parent_class = g_type_class_peek_parent (klass);
243 object_class->finalize = ephy_shlib_loader_finalize;
245 g_type_class_add_private (object_class, sizeof (EphyShlibLoaderPrivate));