Add an implementation of a MMGtkHitStore.
[mmediamanager.git] / libmmanager-gtk / mm-gtk-hit-store.c
bloba4b099e0a2bd0ad9395d60014d2ad04ebaa351f5
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-hit-store.h"
23 #include "libmmanager/mm-attribute-manager.h"
24 #include "libmmanager/mm-attribute-base-manager.h"
25 #include "libmmanager/mm-attribute.h"
26 #include "libmmanager/mm-hit.h"
27 #include "libmmanager/mm-hit-collection.h"
29 #include <gtk/gtk.h>
30 #include <glib.h>
31 #include <glib-object.h>
33 /* our parent's model iface */
34 static GtkTreeModelIface parent_iface = { 0, };
35 static void mm_gtk_hit_store_tree_model_iface_init (GtkTreeModelIface *iface);
37 G_DEFINE_TYPE_EXTENDED (MMGtkHitStore, mm_gtk_hit_store,
38 GTK_TYPE_LIST_STORE, 0,
39 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_MODEL,
40 mm_gtk_hit_store_tree_model_iface_init));
42 static void
43 mm_gtk_hit_store_class_init (MMGtkHitStoreClass *klass)
45 /* do nothing */
48 static void
49 mm_gtk_hit_store_init (MMGtkHitStore *self)
51 GType types [] = { MM_TYPE_HIT };
53 gtk_tree_store_set_column_types (GTK_TREE_STORE (self), 1, types);
56 static void
57 populate_from_hit_collection (MMGtkHitStore *self,
58 MMHitCollection *collection)
63 /* interface implementation */
65 static GType
66 impl_get_column_type (GtkTreeModel *self, gint column)
68 GType types [] = {
69 MM_TYPE_HIT,
70 G_TYPE_FILE,
71 G_TYPE_STRING,
72 G_TYPE_ICON
75 g_return_val_if_fail (MM_GTK_IS_HIT_STORE (self), G_TYPE_INVALID);
76 g_return_val_if_fail (column >= 0 && column < MM_GTK_HIT_STORE_N_COL, G_TYPE_INVALID);
78 return types[column];
81 static int
82 impl_get_n_columns (GtkTreeModel *self)
84 g_return_val_if_fail (MM_GTK_IS_HIT_STORE (self), 0);
86 /* we have four columns:
87 * - hit
88 * - gfile
89 * - name
90 * - gicon
92 return MM_GTK_HIT_STORE_N_COL;
95 static MMHit *
96 get_hit (GtkTreeModel *self, GtkTreeIter *iter)
98 GValue val = { 0, };
99 MMHit *hit;
101 /* validate our parameters */
102 g_return_val_if_fail (MM_GTK_IS_HIT_STORE (self), NULL);
103 g_return_val_if_fail (iter != NULL, NULL);
105 /* retreive the object using our parent's interface */
106 parent_iface.get_value (self, iter, MM_GTK_HIT_STORE_HIT_COL, &val);
108 hit = MM_HIT (g_value_dup_object (&val));
109 g_value_unset (&val);
111 return hit;
114 static GFile *
115 get_gfile_from_hit (MMHit *hit)
117 GFile *file;
118 GHashTable *pairs;
119 GValue *uri_val;
120 MMAttribute *uri_attr;
121 const char *uri;
124 pairs = mm_hit_get_values (hit,
125 MM_ATTRIBUTE_BASE_URI);
126 uri_attr = mm_attribute_manager_lookup_attribute (mm_attribute_base_manager_get (),
127 MM_ATTRIBUTE_BASE_URI);
128 uri_val = g_hash_table_lookup (pairs, uri_attr);
129 uri = g_value_get_string (uri_val);
130 file = g_file_new_for_uri (uri);
132 g_hash_table_destroy (pairs);
134 return file;
137 static void
138 value_set_gicon_from_hit (GValue *val, MMHit *hit)
140 /* TODO: maybe we should support custom icons in some way... */
141 GFile *file;
142 GFileInfo *info;
144 file = get_gfile_from_hit (hit);
145 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_ICON,
146 0, NULL, NULL);
147 if (info) {
148 GIcon *icon;
150 icon = g_file_info_get_icon (info);
151 g_value_take_object (val, icon);
152 g_object_unref (info);
155 g_object_unref (file);
158 static void
159 value_set_name_from_hit (GValue *val, MMHit *hit)
161 GHashTable *pairs;
162 MMAttribute *name_attr;
163 GValue *name_val;
165 pairs = mm_hit_get_values (hit,
166 MM_ATTRIBUTE_BASE_NAME);
167 name_attr = mm_attribute_manager_lookup_attribute (mm_attribute_base_manager_get (),
168 MM_ATTRIBUTE_BASE_NAME);
169 name_val = g_hash_table_lookup (pairs, name_attr);
171 if (name_val) {
172 g_value_copy (name_val, val);
173 } else {
174 /* fallback */
175 GFile *file;
176 GFileInfo *info;
178 file = get_gfile_from_hit (hit);
179 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME,
180 0, NULL, NULL);
181 if (info) {
182 g_value_set_string (val, g_file_info_get_display_name (info));
183 g_object_unref (info);
185 g_object_unref (file);
188 g_hash_table_destroy (pairs);
191 static void
192 value_set_gfile_from_hit (GValue *val, MMHit *hit)
194 GFile *file;
196 file = get_gfile_from_hit (hit);
197 g_value_take_object (val, file);
200 static void
201 impl_get_value (GtkTreeModel *self, GtkTreeIter *iter,
202 gint column, GValue *value)
204 MMHit *hit;
206 g_return_if_fail (MM_GTK_IS_HIT_STORE (self));
207 g_return_if_fail (iter != NULL);
208 g_return_if_fail (column >= 0 && column < MM_GTK_HIT_STORE_N_COL);
209 g_return_if_fail (value != NULL);
211 hit = get_hit (self, iter);
212 value = g_value_init (value, impl_get_column_type (self, column));
214 switch (column) {
215 case MM_GTK_HIT_STORE_HIT_COL:
216 g_value_set_object (value, hit);
217 break;
218 case MM_GTK_HIT_STORE_FILE_COL:
219 value_set_gfile_from_hit (value, hit);
220 break;
221 case MM_GTK_HIT_STORE_NAME_COL:
222 value_set_name_from_hit (value, hit);
223 break;
224 case MM_GTK_HIT_STORE_ICON_COL:
225 value_set_gicon_from_hit (value, hit);
226 break;
227 default:
228 g_assert_not_reached ();
229 break;
232 g_object_unref (hit);
235 static void
236 mm_gtk_hit_store_tree_model_iface_init (GtkTreeModelIface *iface)
238 /* save a copy of our parent's iface */
239 parent_iface = *iface;
241 /* override the iface methods */
242 iface->get_n_columns = impl_get_n_columns;
243 iface->get_column_type = impl_get_column_type;
244 iface->get_value = impl_get_value;
247 /* public methods */
249 MMGtkHitStore*
250 mm_gtk_hit_store_new (MMHitCollection *hit_collection)
252 MMGtkHitStore * self;
254 self = MM_GTK_HIT_STORE (g_object_new (MM_GTK_TYPE_HIT_STORE, NULL));
255 populate_from_hit_collection (self, hit_collection);
257 return self;