4
* This library is free software: you can redistribute it and/or modify it
5
* under the terms of the GNU Lesser General Public License as published by
6
* the Free Software Foundation.
8
* This library is distributed in the hope that it will be useful, but
9
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
13
* You should have received a copy of the GNU Lesser General Public License
14
* along with this library. If not, see <http://www.gnu.org/licenses/>.
19
* SECTION: e-extensible
20
* @include: libedataserver/libedataserver.h
21
* @short_description: An interface for extending objects
23
* #EExtension objects can be tacked on to any #GObject instance that
24
* implements the #EExtensible interface. A #GObject type can be made
25
* extensible in two steps:
27
* 1. Add the #EExtensible interface when registering the #GType.
28
* There are no methods to implement.
32
* #include <libedataserver/libedataserver.h>
34
* G_DEFINE_TYPE_WITH_CODE (
35
* ECustomWidget, e_custom_widget, GTK_TYPE_WIDGET,
36
* G_IMPLEMENT_INTERFACE (E_TYPE_EXTENSIBLE, NULL))
40
* 2. Load extensions for the class at some point during #GObject
41
* initialization. Generally this should be done toward the end of
42
* the initialization code, so extensions get a fully initialized
43
* object to work with.
48
* e_custom_widget_constructed (ECustomWidget *widget)
50
* Construction code goes here, same as call to parent's 'constructed'...
52
* e_extensible_load_extensions (E_EXTENSIBLE (widget));
62
#include "e-extension.h"
63
#include "e-data-server-util.h"
65
#include "e-extensible.h"
67
#define IS_AN_EXTENSION_TYPE(type) \
68
(g_type_is_a ((type), E_TYPE_EXTENSION))
70
static GQuark extensible_quark;
78
extensible_get_extensions (EExtensible *extensible)
80
return g_object_get_qdata (G_OBJECT (extensible), extensible_quark);
84
extensible_load_extension (GType extension_type,
85
EExtensible *extensible)
87
EExtensionClass *extension_class;
88
GType extensible_type;
89
GPtrArray *extensions;
90
EExtension *extension;
92
extensible_type = G_OBJECT_TYPE (extensible);
93
extension_class = g_type_class_ref (extension_type);
95
/* Only load extensions that extend the given extensible object. */
96
if (!g_type_is_a (extensible_type, extension_class->extensible_type))
99
extension = g_object_new (
100
extension_type, "extensible", extensible, NULL);
102
extensions = extensible_get_extensions (extensible);
103
g_ptr_array_add (extensions, extension);
106
g_type_class_unref (extension_class);
110
e_extensible_default_init (EExtensibleInterface *iface)
112
extensible_quark = g_quark_from_static_string ("e-extensible-quark");
116
* e_extensible_load_extensions:
117
* @extensible: an #EExtensible
119
* Creates an instance of all instantiable subtypes of #EExtension which
120
* target the class of @extensible. The lifetimes of these newly created
121
* #EExtension objects are bound to @extensible such that they are finalized
122
* when @extensible is finalized.
127
e_extensible_load_extensions (EExtensible *extensible)
129
GPtrArray *extensions;
131
g_return_if_fail (E_IS_EXTENSIBLE (extensible));
133
if (extensible_get_extensions (extensible) != NULL)
136
extensions = g_ptr_array_new_with_free_func (
137
(GDestroyNotify) g_object_unref);
139
g_object_set_qdata_full (
140
G_OBJECT (extensible), extensible_quark,
141
g_ptr_array_ref (extensions),
142
(GDestroyNotify) g_ptr_array_unref);
145
E_TYPE_EXTENSION, (ETypeFunc)
146
extensible_load_extension, extensible);
148
/* If the extension array is still empty, remove it from the
149
* extensible object. It may be that no extension types have
150
* been registered yet, so this allows for trying again later. */
151
if (extensions->len == 0)
153
G_OBJECT (extensible),
154
extensible_quark, NULL);
156
g_ptr_array_unref (extensions);
160
* e_extensible_list_extensions:
161
* @extensible: an #EExtensible
162
* @extension_type: the type of extensions to list
164
* Returns a list of #EExtension objects bound to @extensible whose
165
* types are ancestors of @extension_type. For a complete list of
166
* extension objects bound to @extensible, pass %E_TYPE_EXTENSION.
168
* The list itself should be freed with g_list_free(). The extension
169
* objects are owned by @extensible and should not be unreferenced.
171
* Returns: a list of extension objects derived from @extension_type
176
e_extensible_list_extensions (EExtensible *extensible,
177
GType extension_type)
179
GPtrArray *extensions;
183
g_return_val_if_fail (E_IS_EXTENSIBLE (extensible), NULL);
184
g_return_val_if_fail (IS_AN_EXTENSION_TYPE (extension_type), NULL);
186
e_extensible_load_extensions (extensible);
188
extensions = extensible_get_extensions (extensible);
190
/* This will be NULL if no extensions are present. */
191
if (extensions == NULL)
194
for (ii = 0; ii < extensions->len; ii++) {
197
object = g_ptr_array_index (extensions, ii);
198
if (g_type_is_a (G_OBJECT_TYPE (object), extension_type))
199
list = g_list_prepend (list, object);
202
return g_list_reverse (list);