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 /* interface implementation */
66 impl_get_column_type (GtkTreeModel
*self
, gint column
)
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
);
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:
92 return MM_GTK_HIT_STORE_N_COL
;
96 get_hit (GtkTreeModel
*self
, GtkTreeIter
*iter
)
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
);
115 get_gfile_from_hit (MMHit
*hit
)
120 MMAttribute
*uri_attr
;
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
);
138 value_set_gicon_from_hit (GValue
*val
, MMHit
*hit
)
140 /* TODO: maybe we should support custom icons in some way... */
144 file
= get_gfile_from_hit (hit
);
145 info
= g_file_query_info (file
, G_FILE_ATTRIBUTE_STANDARD_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
);
159 value_set_name_from_hit (GValue
*val
, MMHit
*hit
)
162 MMAttribute
*name_attr
;
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
);
172 g_value_copy (name_val
, val
);
178 file
= get_gfile_from_hit (hit
);
179 info
= g_file_query_info (file
, G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME
,
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
);
192 value_set_gfile_from_hit (GValue
*val
, MMHit
*hit
)
196 file
= get_gfile_from_hit (hit
);
197 g_value_take_object (val
, file
);
201 impl_get_value (GtkTreeModel
*self
, GtkTreeIter
*iter
,
202 gint column
, GValue
*value
)
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
));
215 case MM_GTK_HIT_STORE_HIT_COL
:
216 g_value_set_object (value
, hit
);
218 case MM_GTK_HIT_STORE_FILE_COL
:
219 value_set_gfile_from_hit (value
, hit
);
221 case MM_GTK_HIT_STORE_NAME_COL
:
222 value_set_name_from_hit (value
, hit
);
224 case MM_GTK_HIT_STORE_ICON_COL
:
225 value_set_gicon_from_hit (value
, hit
);
228 g_assert_not_reached ();
232 g_object_unref (hit
);
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
;
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
);