1 /* Copyright (c) 2008, Nokia Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * * Neither the name of the Nokia Corporation nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <modest-protocol.h>
39 typedef struct _ModestProtocolTranslation ModestProtocolTranslation;
40 struct _ModestProtocolTranslation {
41 TranslationFunc translation_func;
43 GDestroyNotify data_destroy_func;
46 typedef struct _ModestProtocolPrivate ModestProtocolPrivate;
47 struct _ModestProtocolPrivate {
48 ModestProtocolType type_id;
51 GHashTable *properties;
52 GHashTable *translations;
55 /* 'private'/'protected' functions */
56 static void modest_protocol_class_init (ModestProtocolClass *klass);
57 static void modest_protocol_finalize (GObject *obj);
58 static void modest_protocol_get_property (GObject *obj,
62 static void modest_protocol_set_property (GObject *obj,
66 static void modest_protocol_instance_init (ModestProtocol *obj);
67 static void translation_free (ModestProtocolTranslation *translation);
69 #define MODEST_PROTOCOL_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), \
70 MODEST_TYPE_PROTOCOL, \
71 ModestProtocolPrivate))
74 static GObjectClass *parent_class = NULL;
75 static ModestProtocolType next_type_id = 0;
76 static GMutex *next_type_id_mutex = NULL;
79 modest_protocol_get_type (void)
81 static GType my_type = 0;
84 static const GTypeInfo my_info = {
85 sizeof(ModestProtocolClass),
87 NULL, /* base finalize */
88 (GClassInitFunc) modest_protocol_class_init,
89 NULL, /* class finalize */
90 NULL, /* class data */
91 sizeof(ModestProtocol),
93 (GInstanceInitFunc) modest_protocol_instance_init,
97 my_type = g_type_register_static (G_TYPE_OBJECT,
105 modest_protocol_class_init (ModestProtocolClass *klass)
107 GObjectClass *object_class;
108 object_class = (GObjectClass *) klass;
110 parent_class = g_type_class_peek_parent (klass);
111 next_type_id_mutex = g_mutex_new ();
112 object_class->finalize = modest_protocol_finalize;
113 object_class->set_property = modest_protocol_set_property;
114 object_class->get_property = modest_protocol_get_property;
116 g_object_class_install_property (object_class,
118 g_param_spec_string ("name",
120 _("The unique name of the protocol"),
122 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT));
124 g_object_class_install_property (object_class,
126 g_param_spec_string ("display-name",
128 _("The name of the protocol that user will see"),
130 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT));
132 g_object_class_install_property (object_class,
134 g_param_spec_int ("type",
136 _("Protocol type unique id"),
137 G_MININT, G_MAXINT, MODEST_PROTOCOL_TYPE_INVALID,
140 g_type_class_add_private (object_class,
141 sizeof(ModestProtocolPrivate));
145 translation_free (ModestProtocolTranslation *translation)
147 if (translation->data_destroy_func)
148 translation->data_destroy_func (translation->userdata);
149 translation->data_destroy_func = NULL;
150 translation->userdata = NULL;
151 translation->translation_func = NULL;
152 g_slice_free (ModestProtocolTranslation, translation);
156 modest_protocol_instance_init (ModestProtocol *obj)
158 ModestProtocolPrivate *priv;
160 priv = MODEST_PROTOCOL_GET_PRIVATE (obj);
163 priv->display_name = NULL;
164 g_mutex_lock (next_type_id_mutex);
165 priv->type_id = next_type_id;
167 g_mutex_unlock (next_type_id_mutex);
169 priv->properties = g_hash_table_new_full (g_str_hash, g_str_equal,
171 priv->translations = g_hash_table_new_full (g_str_hash, g_str_equal,
172 g_free, (GDestroyNotify) translation_free);
176 modest_protocol_finalize (GObject *obj)
178 ModestProtocol *settings = MODEST_PROTOCOL (obj);
179 ModestProtocolPrivate *priv = MODEST_PROTOCOL_GET_PRIVATE (settings);
183 g_free (priv->display_name);
184 priv->display_name = NULL;
186 g_hash_table_unref (priv->properties);
187 priv->properties = NULL;
189 g_hash_table_unref (priv->translations);
190 priv->translations = NULL;
192 G_OBJECT_CLASS (parent_class)->finalize (obj);
196 modest_protocol_get_property (GObject *obj,
201 ModestProtocol *protocol = MODEST_PROTOCOL (obj);
202 ModestProtocolPrivate *priv = MODEST_PROTOCOL_GET_PRIVATE (protocol);
204 switch (property_id) {
206 g_value_set_static_string (value, priv->name);
208 case PROP_DISPLAY_NAME:
209 g_value_set_static_string (value, priv->name);
212 g_value_set_int (value, priv->type_id);
215 G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, property_id, pspec);
221 modest_protocol_set_property (GObject *obj,
226 ModestProtocol *protocol = MODEST_PROTOCOL (obj);
228 switch (property_id) {
230 modest_protocol_set_name (protocol, g_value_get_string (value));
232 case PROP_DISPLAY_NAME:
233 modest_protocol_set_display_name (protocol, g_value_get_string (value));
236 G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, property_id, pspec);
243 modest_protocol_new (const gchar *name, const gchar *display_name)
245 return g_object_new (MODEST_TYPE_PROTOCOL, "display-name", display_name, "name", name, NULL);
249 modest_protocol_get_name (ModestProtocol *self)
251 ModestProtocolPrivate *priv;
253 g_return_val_if_fail (MODEST_IS_PROTOCOL (self), NULL);
255 priv = MODEST_PROTOCOL_GET_PRIVATE (self);
260 modest_protocol_set_name (ModestProtocol *self,
263 ModestProtocolPrivate *priv;
265 g_return_if_fail (MODEST_IS_PROTOCOL (self));
267 priv = MODEST_PROTOCOL_GET_PRIVATE (self);
269 priv->name = g_strdup (name);
273 modest_protocol_get_display_name (ModestProtocol *self)
275 ModestProtocolPrivate *priv;
277 g_return_val_if_fail (MODEST_IS_PROTOCOL (self), NULL);
279 priv = MODEST_PROTOCOL_GET_PRIVATE (self);
280 return priv->display_name;
284 modest_protocol_set_display_name (ModestProtocol *self,
285 const gchar *display_name)
287 ModestProtocolPrivate *priv;
289 g_return_if_fail (MODEST_IS_PROTOCOL (self));
291 priv = MODEST_PROTOCOL_GET_PRIVATE (self);
292 g_free (priv->display_name);
293 priv->display_name = g_strdup (display_name);
297 modest_protocol_get_type_id (ModestProtocol *self)
299 ModestProtocolPrivate *priv;
301 g_return_val_if_fail (MODEST_IS_PROTOCOL (self), MODEST_PROTOCOL_TYPE_INVALID);
303 priv = MODEST_PROTOCOL_GET_PRIVATE (self);
304 return priv->type_id;
308 modest_protocol_set (ModestProtocol *self,
312 ModestProtocolPrivate *priv;
314 g_return_if_fail (MODEST_IS_PROTOCOL (self));
316 priv = MODEST_PROTOCOL_GET_PRIVATE (self);
318 g_hash_table_replace (priv->properties,g_strdup (key), g_strdup (value));
322 modest_protocol_get (ModestProtocol *self,
325 ModestProtocolPrivate *priv;
327 g_return_val_if_fail (MODEST_IS_PROTOCOL (self), NULL);
329 priv = MODEST_PROTOCOL_GET_PRIVATE (self);
331 return g_hash_table_lookup (priv->properties, key);
335 modest_protocol_set_translation (ModestProtocol *self,
337 TranslationFunc translation_func,
339 GDestroyNotify data_destroy_func)
341 ModestProtocolPrivate *priv;
342 ModestProtocolTranslation *translation;
344 g_return_if_fail (MODEST_IS_PROTOCOL (self));
346 priv = MODEST_PROTOCOL_GET_PRIVATE (self);
348 translation = g_slice_new0 (ModestProtocolTranslation);
349 translation->translation_func = translation_func;
350 translation->userdata = userdata;
351 translation->data_destroy_func = data_destroy_func;
353 g_hash_table_replace (priv->translations, g_strdup(id), (gpointer) translation);
357 modest_protocol_get_translation (ModestProtocol *self,
364 g_return_val_if_fail (MODEST_IS_PROTOCOL (self), NULL);
367 result = modest_protocol_va_get_translation (self, id, args);
374 modest_protocol_va_get_translation (ModestProtocol *self,
378 ModestProtocolPrivate *priv;
379 ModestProtocolTranslation *translation;
383 g_return_val_if_fail (MODEST_IS_PROTOCOL (self), NULL);
385 priv = MODEST_PROTOCOL_GET_PRIVATE (self);
387 translation = g_hash_table_lookup (priv->translations, id);
388 if (translation == NULL)
390 g_return_val_if_fail (translation->translation_func != NULL, NULL);
392 G_VA_COPY (dest, args);
393 result = translation->translation_func (translation->userdata, dest);