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 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-extension
20 * @include: libebackend/libebackend.h
21 * @short_description: An abstract base class for extensions
23 * #EExtension provides a way to extend the functionality of objects
24 * that implement the #EExtensible interface. #EExtension subclasses
25 * can target a particular extensible object type. New instances of
26 * an extensible object type get paired with a new instance of each
27 * #EExtension subclass that targets the extensible object type.
29 * The first steps of writing a new extension are as follows:
31 * 1. Subclass #EExtension.
33 * 2. In the class initialization function, specify the #GType being
34 * extended. The #GType must implement the #EExtensible interface.
36 * 3. Register the extension's own #GType. If the extension is to
37 * be loaded dynamically using #GTypeModule, the type should be
38 * registered in the library module's e_module_load() function.
41 #include "e-extension.h"
45 #define E_EXTENSION_GET_PRIVATE(obj) \
46 (G_TYPE_INSTANCE_GET_PRIVATE \
47 ((obj), E_TYPE_EXTENSION, EExtensionPrivate))
49 struct _EExtensionPrivate {
50 gpointer extensible; /* weak pointer */
58 G_DEFINE_ABSTRACT_TYPE (
64 extension_set_extensible (EExtension *extension,
65 EExtensible *extensible)
67 EExtensionClass *class;
68 GType extensible_type;
70 g_return_if_fail (E_IS_EXTENSIBLE (extensible));
71 g_return_if_fail (extension->priv->extensible == NULL);
73 class = E_EXTENSION_GET_CLASS (extension);
74 extensible_type = G_OBJECT_TYPE (extensible);
76 /* Verify the EExtensible object is the type we want. */
77 if (!g_type_is_a (extensible_type, class->extensible_type)) {
79 "%s is meant to extend %s but was given an %s",
80 G_OBJECT_TYPE_NAME (extension),
81 g_type_name (class->extensible_type),
82 g_type_name (extensible_type));
86 extension->priv->extensible = extensible;
88 g_object_add_weak_pointer (
89 G_OBJECT (extensible), &extension->priv->extensible);
93 extension_set_property (GObject *object,
98 switch (property_id) {
100 extension_set_extensible (
101 E_EXTENSION (object),
102 g_value_get_object (value));
106 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
110 extension_get_property (GObject *object,
115 switch (property_id) {
116 case PROP_EXTENSIBLE:
118 value, e_extension_get_extensible (
119 E_EXTENSION (object)));
123 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
127 extension_dispose (GObject *object)
129 EExtensionPrivate *priv;
131 priv = E_EXTENSION_GET_PRIVATE (object);
133 if (priv->extensible != NULL) {
134 g_object_remove_weak_pointer (
135 G_OBJECT (priv->extensible), &priv->extensible);
136 priv->extensible = NULL;
139 /* Chain up to parent's dispose() method. */
140 G_OBJECT_CLASS (e_extension_parent_class)->dispose (object);
144 e_extension_class_init (EExtensionClass *class)
146 GObjectClass *object_class;
148 g_type_class_add_private (class, sizeof (EExtensionPrivate));
150 object_class = G_OBJECT_CLASS (class);
151 object_class->set_property = extension_set_property;
152 object_class->get_property = extension_get_property;
153 object_class->dispose = extension_dispose;
155 g_object_class_install_property (
158 g_param_spec_object (
161 "The object being extended",
164 G_PARAM_CONSTRUCT_ONLY));
168 e_extension_init (EExtension *extension)
170 extension->priv = E_EXTENSION_GET_PRIVATE (extension);
174 * e_extension_get_extensible:
175 * @extension: an #EExtension
177 * Returns the object that @extension extends.
179 * Returns: the object being extended
184 e_extension_get_extensible (EExtension *extension)
186 g_return_val_if_fail (E_IS_EXTENSION (extension), NULL);
188 return E_EXTENSIBLE (extension->priv->extensible);