Add doc files to dist and fix version number in NEWS.
[mmediamanager.git] / libmmanager-gtk / mm-gtk-attribute-store.c
blob28cef7e97293ace38c39bdf19fa235c19a11757e
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 "mm-gtk-attribute-store.h"
23 #include "libmmanager/mm-attribute-manager.h"
24 #include "libmmanager/mm-attribute.h"
26 #include <gtk/gtk.h>
27 #include <glib.h>
28 #include <glib-object.h>
30 /* our parent's model iface */
31 static GtkTreeModelIface parent_iface = { 0, };
32 static void mm_gtk_attribute_store_tree_model_iface_init (GtkTreeModelIface *iface);
34 G_DEFINE_TYPE_EXTENDED (MMGtkAttributeStore, mm_gtk_attribute_store,
35 GTK_TYPE_LIST_STORE, 0,
36 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_MODEL,
37 mm_gtk_attribute_store_tree_model_iface_init));
39 static void
40 mm_gtk_attribute_store_class_init (MMGtkAttributeStoreClass *klass)
42 /* do nothing */
45 static void
46 mm_gtk_attribute_store_init (MMGtkAttributeStore *self)
48 GType types[] = { G_TYPE_POINTER };
50 gtk_list_store_set_column_types (GTK_LIST_STORE (self), 1,
51 types);
54 /* interface methods implementations */
56 static GType
57 impl_get_column_type (GtkTreeModel *self,
58 gint column)
60 GType types [] = {
61 G_TYPE_POINTER,
62 G_TYPE_STRING,
63 G_TYPE_STRING,
64 G_TYPE_STRING,
65 G_TYPE_GTYPE
68 g_return_val_if_fail (MM_GTK_IS_ATTRIBUTE_STORE (self), G_TYPE_INVALID);
69 g_return_val_if_fail (column >= 0 && column < MM_GTK_ATTR_STORE_N_COL, G_TYPE_INVALID);
71 return types[column];
74 static MMAttribute *
75 get_attribute_from_store (GtkTreeModel *self, GtkTreeIter *iter)
77 GValue val = { 0, };
78 MMAttribute *attr;
80 /* validate our parameters */
81 g_return_val_if_fail (MM_GTK_IS_ATTRIBUTE_STORE (self), NULL);
82 g_return_val_if_fail (iter != NULL, NULL);
84 /* retreive the object using our parent's interface */
85 parent_iface.get_value (self, iter, MM_GTK_ATTR_STORE_ATTR_COL, &val);
87 attr = (MMAttribute *) g_value_get_pointer (&val);
88 g_value_unset (&val);
90 return attr;
93 static void
94 impl_get_value (GtkTreeModel *self, GtkTreeIter *iter,
95 gint column, GValue *value)
97 MMAttribute *attr;
99 value = g_value_init (value, impl_get_column_type (self, column));
100 attr = get_attribute_from_store (self, iter);
102 switch (column) {
103 case MM_GTK_ATTR_STORE_ATTR_COL:
104 /* return the attr itself */
105 g_value_set_pointer (value, attr);
106 break;
107 case MM_GTK_ATTR_STORE_ID_COL:
108 g_value_set_string (value, mm_attribute_get_id (attr));
109 break;
110 case MM_GTK_ATTR_STORE_NAME_COL:
111 g_value_set_string (value, mm_attribute_get_name (attr));
112 break;
113 case MM_GTK_ATTR_STORE_DESC_COL:
114 g_value_set_string (value, mm_attribute_get_description (attr));
115 break;
116 case MM_GTK_ATTR_STORE_GTYPE_COL:
117 g_value_set_gtype (value, mm_attribute_get_value_type (attr));
118 break;
119 default:
120 g_assert_not_reached ();
121 break;
125 static gint
126 impl_get_n_columns (GtkTreeModel *self)
128 g_return_val_if_fail (MM_GTK_IS_ATTRIBUTE_STORE (self), 0);
130 return MM_GTK_ATTR_STORE_N_COL;
133 static void
134 mm_gtk_attribute_store_tree_model_iface_init (GtkTreeModelIface *iface)
136 /* save a copy of our parent's iface */
137 parent_iface = *iface;
139 /* override the iface methods */
140 iface->get_n_columns = impl_get_n_columns;
141 iface->get_column_type = impl_get_column_type;
142 iface->get_value = impl_get_value;
145 /* public methods */
148 * mm_gtk_attribute_store_new:
150 * Builds an empty #GtkListStore, which will allow to store #MMAttributes
151 * in a #GtkTreeModel fashion.
153 * Return value: an empty #MMGtkAttributeStore.
156 MMGtkAttributeStore*
157 mm_gtk_attribute_store_new (void)
159 return g_object_new (MM_GTK_TYPE_ATTRIBUTE_STORE, NULL);
163 * mm_gtk_attribute_store_add_attribute:
164 * @self: a #MMGtkAttributeStore.
165 * @attr: a #MMAttriubte.
167 * Adds @attr to the model pointed by @self. This isn't very useful for
168 * clients, you might want to use
169 * #mm_gtk_attribute_store_set_from_attribute_manager instead.
172 void
173 mm_gtk_attribute_store_add_attribute (MMGtkAttributeStore *self,
174 MMAttribute *attr)
176 GtkTreeIter iter;
178 g_return_if_fail (MM_GTK_IS_ATTRIBUTE_STORE (self));
180 gtk_list_store_append (GTK_LIST_STORE (self), &iter);
181 gtk_list_store_set (GTK_LIST_STORE (self), &iter,
182 MM_GTK_ATTR_STORE_ATTR_COL, attr, -1);
186 * mm_gtk_attribute_store_set_from_attribute_manager:
187 * @self: a #MMGtkAttributeStore.
188 * @attr_manager: a #MMAttributeManager.
190 * Encapsulates all the attributes supported by @attr_manager into the
191 * #GtkTreeModel implemented by @self.
194 void
195 mm_gtk_attribute_store_set_from_attribute_manager (MMGtkAttributeStore *self,
196 MMAttributeManager *attr_manager)
198 GList *attrs, *l;
200 g_return_if_fail (MM_GTK_IS_ATTRIBUTE_STORE (self));
201 g_return_if_fail (MM_IS_ATTRIBUTE_MANAGER (attr_manager));
203 gtk_list_store_clear (GTK_LIST_STORE (self));
205 attrs = mm_attribute_manager_get_supported_attributes (attr_manager);
206 for (l = attrs; l; l = l->next) {
207 mm_gtk_attribute_store_add_attribute (self, l->data);