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_tree_store_set_column_types (GTK_TREE_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);
71 /* interface implementation */
74 impl_get_column_type (GtkTreeModel
*self
, gint column
)
83 g_return_val_if_fail (MM_GTK_IS_HIT_STORE (self
), G_TYPE_INVALID
);
84 g_return_val_if_fail (column
>= 0 && column
< MM_GTK_HIT_STORE_N_COL
, G_TYPE_INVALID
);
90 impl_get_n_columns (GtkTreeModel
*self
)
92 g_return_val_if_fail (MM_GTK_IS_HIT_STORE (self
), 0);
94 /* we have four columns:
100 return MM_GTK_HIT_STORE_N_COL
;
104 get_hit (GtkTreeModel
*self
, GtkTreeIter
*iter
)
109 /* validate our parameters */
110 g_return_val_if_fail (MM_GTK_IS_HIT_STORE (self
), NULL
);
111 g_return_val_if_fail (iter
!= NULL
, NULL
);
113 /* retreive the object using our parent's interface */
114 parent_iface
.get_value (self
, iter
, MM_GTK_HIT_STORE_HIT_COL
, &val
);
116 hit
= MM_HIT (g_value_dup_object (&val
));
117 g_value_unset (&val
);
123 get_gfile_from_hit (MMHit
*hit
)
128 MMAttribute
*uri_attr
;
132 pairs
= mm_hit_get_values (hit
,
133 MM_ATTRIBUTE_BASE_URI
);
134 uri_attr
= mm_attribute_manager_lookup_attribute (mm_attribute_base_manager_get (),
135 MM_ATTRIBUTE_BASE_URI
);
136 uri_val
= g_hash_table_lookup (pairs
, uri_attr
);
137 uri
= g_value_get_string (uri_val
);
138 file
= g_file_new_for_uri (uri
);
140 g_hash_table_destroy (pairs
);
146 value_set_gicon_from_hit (GValue
*val
, MMHit
*hit
)
148 /* TODO: maybe we should support custom icons in some way... */
152 file
= get_gfile_from_hit (hit
);
153 info
= g_file_query_info (file
, G_FILE_ATTRIBUTE_STANDARD_ICON
,
158 icon
= g_file_info_get_icon (info
);
159 g_value_take_object (val
, icon
);
160 g_object_unref (info
);
163 g_object_unref (file
);
167 value_set_name_from_hit (GValue
*val
, MMHit
*hit
)
170 MMAttribute
*name_attr
;
173 pairs
= mm_hit_get_values (hit
,
174 MM_ATTRIBUTE_BASE_NAME
);
175 name_attr
= mm_attribute_manager_lookup_attribute (mm_attribute_base_manager_get (),
176 MM_ATTRIBUTE_BASE_NAME
);
177 name_val
= g_hash_table_lookup (pairs
, name_attr
);
180 g_value_copy (name_val
, val
);
186 file
= get_gfile_from_hit (hit
);
187 info
= g_file_query_info (file
, G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME
,
190 g_value_set_string (val
, g_file_info_get_display_name (info
));
191 g_object_unref (info
);
193 g_object_unref (file
);
196 g_hash_table_destroy (pairs
);
200 value_set_gfile_from_hit (GValue
*val
, MMHit
*hit
)
204 file
= get_gfile_from_hit (hit
);
205 g_value_take_object (val
, file
);
209 impl_get_value (GtkTreeModel
*self
, GtkTreeIter
*iter
,
210 gint column
, GValue
*value
)
214 g_return_if_fail (MM_GTK_IS_HIT_STORE (self
));
215 g_return_if_fail (iter
!= NULL
);
216 g_return_if_fail (column
>= 0 && column
< MM_GTK_HIT_STORE_N_COL
);
217 g_return_if_fail (value
!= NULL
);
219 hit
= get_hit (self
, iter
);
220 value
= g_value_init (value
, impl_get_column_type (self
, column
));
223 case MM_GTK_HIT_STORE_HIT_COL
:
224 g_value_set_object (value
, hit
);
226 case MM_GTK_HIT_STORE_FILE_COL
:
227 value_set_gfile_from_hit (value
, hit
);
229 case MM_GTK_HIT_STORE_NAME_COL
:
230 value_set_name_from_hit (value
, hit
);
232 case MM_GTK_HIT_STORE_ICON_COL
:
233 value_set_gicon_from_hit (value
, hit
);
236 g_assert_not_reached ();
240 g_object_unref (hit
);
244 mm_gtk_hit_store_tree_model_iface_init (GtkTreeModelIface
*iface
)
246 /* save a copy of our parent's iface */
247 parent_iface
= *iface
;
249 /* override the iface methods */
250 iface
->get_n_columns
= impl_get_n_columns
;
251 iface
->get_column_type
= impl_get_column_type
;
252 iface
->get_value
= impl_get_value
;
258 mm_gtk_hit_store_new (MMHitCollection
*hit_collection
)
260 MMGtkHitStore
* self
;
262 self
= MM_GTK_HIT_STORE (g_object_new (MM_GTK_TYPE_HIT_STORE
, NULL
));
263 populate_from_hit_collection (self
, hit_collection
);