Validate args before creating the DBus proxy, just for complete safety.
[mmediamanager.git] / src / mm-attribute-base-manager.c
blob955c0513114f6080befea7f23b9059578f33b8b8
1 /* MManager - a Desktop wide manager for multimedia applications.
3 * Copyright (C) 2008 Cosimo Cecchi <cosimoc@gnome.org>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library 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 GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
21 #include <glib.h>
22 #include <glib-object.h>
23 #include <glib/gi18n.h>
25 #include "mm-attribute-manager.h"
26 #include "mm-attribute-base-manager.h"
27 #include "mm-base-attributes.h"
29 static MMAttributeManager *base_manager = NULL;
30 static GHashTable *cached_attrs = NULL;
32 static void mm_attribute_manager_iface_init (MMAttributeManagerIface *iface);
34 G_DEFINE_TYPE_WITH_CODE (MMAttributeBaseManager, mm_attribute_base_manager,
35 G_TYPE_OBJECT,
36 G_IMPLEMENT_INTERFACE (MM_TYPE_ATTRIBUTE_MANAGER,
37 mm_attribute_manager_iface_init));
38 /* base attributes */
40 static const char * attribute_ids [] = {
41 MM_ATTRIBUTE_BASE_URI,
42 MM_ATTRIBUTE_BASE_NAME
45 static const GType attribute_types [] = {
46 G_TYPE_STRING,
47 G_TYPE_STRING
50 static const char * attribute_names [] = {
51 N_("URI"),
52 N_("Name")
55 static const char * attribute_descriptions [] = {
56 N_("URI of the resource"),
57 N_("Name of the resource")
60 /* MMAttributeManager interface implementation */
62 static MMAttribute *
63 impl_lookup_attribute (MMAttributeManager *m,
64 const char *id)
66 int idx;
67 gboolean found = FALSE;
68 MMAttribute *attr;
70 for (idx = 0; idx < G_N_ELEMENTS (attribute_ids); idx++) {
71 if (g_strcmp0 (attribute_ids[idx], id) == 0) {
72 found = TRUE;
73 break;
77 if (found) {
78 attr = g_hash_table_lookup (cached_attrs,
79 attribute_ids[idx]);
80 if (!attr) {
81 attr = mm_attribute_new (attribute_types[idx],
82 attribute_ids[idx],
83 attribute_names[idx],
84 attribute_descriptions[idx]);
85 g_hash_table_insert (cached_attrs, (char *) attribute_ids [idx],
86 attr);
88 return attr;
89 } else {
90 return NULL;
94 static GList *
95 impl_get_supported_attributes (MMAttributeManager *m)
97 GList *attributes = NULL;
98 int idx;
99 MMAttribute *attr;
101 for (idx = 0; idx < G_N_ELEMENTS (attribute_ids); idx++) {
102 attr = g_hash_table_lookup (cached_attrs,
103 attribute_ids[idx]);
104 if (!attr) {
105 attr = mm_attribute_new (attribute_types[idx],
106 attribute_ids[idx],
107 attribute_names[idx],
108 attribute_descriptions[idx]);
109 g_hash_table_insert (cached_attrs, (char *) attribute_ids[idx],
110 attr);
112 attributes = g_list_prepend (attributes, attr);
115 return g_list_reverse (attributes);
118 static void
119 mm_attribute_manager_iface_init (MMAttributeManagerIface *iface)
121 iface->lookup_attribute = impl_lookup_attribute;
122 iface->get_supported_attributes = impl_get_supported_attributes;
125 MMAttributeManager *
126 mm_attribute_base_manager_get (void)
128 if (!base_manager) {
129 base_manager = MM_ATTRIBUTE_MANAGER (g_object_new (MM_TYPE_ATTRIBUTE_BASE_MANAGER, NULL));
132 return base_manager;
135 static void
136 mm_attribute_base_manager_finalize (GObject *o)
138 g_hash_table_destroy (cached_attrs);
140 G_OBJECT_CLASS (mm_attribute_base_manager_parent_class)->finalize (o);
143 static void
144 mm_attribute_base_manager_init (MMAttributeBaseManager *abm)
146 if (!cached_attrs) {
147 cached_attrs = g_hash_table_new_full (g_str_hash, g_str_equal,
148 NULL, (GDestroyNotify) mm_attribute_free);
152 static void
153 mm_attribute_base_manager_class_init (MMAttributeBaseManagerClass *klass)
155 G_OBJECT_CLASS (klass)->finalize = mm_attribute_base_manager_finalize;