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"
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
));
43 mm_gtk_hit_store_class_init (MMGtkHitStoreClass
*klass
)
49 mm_gtk_hit_store_init (MMGtkHitStore
*self
)
51 GType types
[] = { MM_TYPE_HIT
};
53 gtk_list_store_set_column_types (GTK_LIST_STORE (self
), 1, types
);
57 populate_from_hit_collection (MMGtkHitStore
*self
,
58 MMHitCollection
*collection
)
63 /* just append the hit collection to the model */
64 while ((hit
= mm_hit_collection_get_next_hit (collection
)) != NULL
) {
65 gtk_list_store_append (GTK_LIST_STORE (self
), &iter
);
66 gtk_list_store_set (GTK_LIST_STORE (self
), &iter
,
67 MM_GTK_HIT_STORE_HIT_COL
, hit
, -1);
68 /* the store keeps a reference of the object */
73 /* interface implementation */
76 impl_get_column_type (GtkTreeModel
*self
, gint column
)
85 g_return_val_if_fail (MM_GTK_IS_HIT_STORE (self
), G_TYPE_INVALID
);
86 g_return_val_if_fail (column
>= 0 && column
< MM_GTK_HIT_STORE_N_COL
, G_TYPE_INVALID
);
92 impl_get_n_columns (GtkTreeModel
*self
)
94 g_return_val_if_fail (MM_GTK_IS_HIT_STORE (self
), 0);
96 /* we have four columns:
102 return MM_GTK_HIT_STORE_N_COL
;
106 get_hit (GtkTreeModel
*self
, GtkTreeIter
*iter
)
111 /* validate our parameters */
112 g_return_val_if_fail (MM_GTK_IS_HIT_STORE (self
), NULL
);
113 g_return_val_if_fail (iter
!= NULL
, NULL
);
115 /* retreive the object using our parent's interface */
116 parent_iface
.get_value (self
, iter
, MM_GTK_HIT_STORE_HIT_COL
, &val
);
118 hit
= MM_HIT (g_value_dup_object (&val
));
119 g_value_unset (&val
);
125 get_gfile_from_hit (MMHit
*hit
)
130 MMAttribute
*uri_attr
;
133 pairs
= mm_hit_get_values (hit
,
134 MM_ATTRIBUTE_BASE_URI
);
135 uri_attr
= mm_attribute_manager_lookup_attribute (mm_attribute_base_manager_get (),
136 MM_ATTRIBUTE_BASE_URI
);
137 uri_val
= g_hash_table_lookup (pairs
, uri_attr
);
138 uri
= g_value_get_string (uri_val
);
139 file
= g_file_new_for_uri (uri
);
141 g_hash_table_destroy (pairs
);
147 value_set_gicon_from_hit (GValue
*val
, MMHit
*hit
)
149 /* TODO: maybe we should support custom icons in some way... */
154 file
= get_gfile_from_hit (hit
);
155 info
= g_file_query_info (file
, G_FILE_ATTRIBUTE_STANDARD_ICON
,
158 icon
= g_file_info_get_icon (info
);
159 g_object_unref (info
);
162 icon
= g_themed_icon_new ("document");
165 g_value_take_object (val
, icon
);
166 g_object_unref (file
);
170 value_set_name_from_hit (GValue
*val
, MMHit
*hit
)
173 MMAttribute
*name_attr
;
176 pairs
= mm_hit_get_values (hit
,
177 MM_ATTRIBUTE_BASE_NAME
);
178 name_attr
= mm_attribute_manager_lookup_attribute (mm_attribute_base_manager_get (),
179 MM_ATTRIBUTE_BASE_NAME
);
180 name_val
= g_hash_table_lookup (pairs
, name_attr
);
183 g_value_copy (name_val
, val
);
189 file
= get_gfile_from_hit (hit
);
190 info
= g_file_query_info (file
, G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME
,
193 g_value_set_string (val
, g_file_info_get_display_name (info
));
194 g_object_unref (info
);
196 g_object_unref (file
);
199 g_hash_table_destroy (pairs
);
203 value_set_gfile_from_hit (GValue
*val
, MMHit
*hit
)
207 file
= get_gfile_from_hit (hit
);
208 g_value_take_object (val
, file
);
212 impl_get_value (GtkTreeModel
*self
, GtkTreeIter
*iter
,
213 gint column
, GValue
*value
)
217 g_return_if_fail (MM_GTK_IS_HIT_STORE (self
));
218 g_return_if_fail (iter
!= NULL
);
219 g_return_if_fail (column
>= 0 && column
< MM_GTK_HIT_STORE_N_COL
);
220 g_return_if_fail (value
!= NULL
);
222 hit
= get_hit (self
, iter
);
223 value
= g_value_init (value
, impl_get_column_type (self
, column
));
226 case MM_GTK_HIT_STORE_HIT_COL
:
227 g_value_set_object (value
, hit
);
229 case MM_GTK_HIT_STORE_FILE_COL
:
230 value_set_gfile_from_hit (value
, hit
);
232 case MM_GTK_HIT_STORE_NAME_COL
:
233 value_set_name_from_hit (value
, hit
);
235 case MM_GTK_HIT_STORE_ICON_COL
:
236 value_set_gicon_from_hit (value
, hit
);
239 g_assert_not_reached ();
243 g_object_unref (hit
);
247 mm_gtk_hit_store_tree_model_iface_init (GtkTreeModelIface
*iface
)
249 /* save a copy of our parent's iface */
250 parent_iface
= *iface
;
252 /* override the iface methods */
253 iface
->get_n_columns
= impl_get_n_columns
;
254 iface
->get_column_type
= impl_get_column_type
;
255 iface
->get_value
= impl_get_value
;
261 * mm_gtk_hit_store_new:
263 * Builds a #GtkListStore that encapsulates a #MMHitCollection in a
264 * #GtkTreeModel fashion.
266 * Return value: an empty #MMGtkHitStore.
270 mm_gtk_hit_store_new (void)
272 MMGtkHitStore
* self
;
274 self
= MM_GTK_HIT_STORE (g_object_new (MM_GTK_TYPE_HIT_STORE
, NULL
));
280 * mm_gtk_hit_store_set_collection:
281 * @self: a #MMGtkHitStore.
282 * @collection: a #MMHitCollection.
284 * Sets the collection to be stored inside @self. If there's already a
285 * #MMHitCollection stored inside the model, the model will be cleared first.
289 mm_gtk_hit_store_set_collection (MMGtkHitStore
*self
, MMHitCollection
*collection
)
291 g_return_if_fail (MM_GTK_IS_HIT_STORE (self
));
292 g_return_if_fail (MM_IS_HIT_COLLECTION (collection
));
294 gtk_list_store_clear (GTK_LIST_STORE (self
));
295 populate_from_hit_collection (self
, collection
);