Make the app store a TreeStore instead of a ListStore and update the gtk
[mmediamanager.git] / libmmanager-gtk / mm-gtk-application-store.c
blob6338d8514e7a6656b9d4e4329b4beeb5c0b0c502
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-application-store.h"
23 #include <libmmanager/mm.h>
24 #include <libmmanager/mm-type-builtins.h>
25 #include <gtk/gtk.h>
27 /* our parent's model iface */
28 static GtkTreeModelIface parent_iface = { 0, };
29 static void mm_gtk_application_store_tree_model_iface_init (GtkTreeModelIface *iface);
31 G_DEFINE_TYPE_EXTENDED (MMGtkApplicationStore, mm_gtk_application_store,
32 GTK_TYPE_TREE_STORE, 0,
33 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_MODEL,
34 mm_gtk_application_store_tree_model_iface_init));
36 static void
37 mm_gtk_application_store_finalize (GObject *object)
39 if (G_OBJECT_CLASS (mm_gtk_application_store_parent_class)->finalize)
40 G_OBJECT_CLASS (mm_gtk_application_store_parent_class)->finalize (object);
43 static void
44 mm_gtk_application_store_class_init (MMGtkApplicationStoreClass *klass)
46 GObjectClass *object_class = G_OBJECT_CLASS (klass);
48 object_class->finalize = mm_gtk_application_store_finalize;
51 static void
52 populate_store (MMGtkApplicationStore *self)
54 MMManager *manager;
55 GList *application_list, *l;
56 GtkTreeIter iter;
58 manager = mm_manager_get ();
59 application_list = mm_manager_get_application_list (manager);
60 for (l = application_list; l; l = l->next) {
61 gtk_tree_store_append (GTK_TREE_STORE (self), &iter, NULL);
62 gtk_tree_store_set (GTK_TREE_STORE (self), &iter, MM_GTK_APP_STORE_APP_COLUMN, l->data, -1);
66 static void
67 mm_gtk_application_store_init (MMGtkApplicationStore *self)
69 GType types [] = { MM_TYPE_APPLICATION };
71 gtk_tree_store_set_column_types (GTK_TREE_STORE (self), 1, types);
72 populate_store (self);
75 /* retreive an object from our parent's data storage,
76 * unref the returned object when done */
77 static MMApplication *
78 mm_gtk_application_store_get_object (MMGtkApplicationStore *self, GtkTreeIter *iter)
80 GValue value = { 0, };
81 MMApplication *app;
83 /* validate our parameters */
84 g_return_val_if_fail (MM_GTK_IS_APPLICATION_STORE (self), NULL);
85 g_return_val_if_fail (iter != NULL, NULL);
87 /* retreive the object using our parent's interface, take our own
88 * reference to it */
89 parent_iface.get_value (GTK_TREE_MODEL (self), iter, 0, &value);
90 app = MM_APPLICATION (g_value_dup_object (&value));
92 g_value_unset (&value);
93 return app;
96 static void
97 value_set_gicon_from_app (GValue *val, MMApplication *app)
99 GAppInfo *app_info;
100 GIcon *icon;
102 app_info = mm_application_get_app_info (app);
103 icon = g_app_info_get_icon (app_info);
104 if (!icon || !G_IS_ICON (icon)) {
105 /* fallback to a default app icon */
106 icon = g_themed_icon_new ("application-default-icon");
109 g_value_take_object (val, icon);
111 g_object_unref (app_info);
114 static void
115 value_set_name_from_app (GValue *value, MMApplication *app)
117 GAppInfo *app_info;
119 app_info = mm_application_get_app_info (app);
120 g_value_set_string (value, g_app_info_get_name (app_info));
122 g_object_unref (app_info);
125 static void
126 value_set_supp_type_from_app (GValue *val, MMApplication *app)
128 MMApplicationType supp_type;
130 g_object_get (app, "supported-type", &supp_type, NULL);
132 g_value_set_enum (val, supp_type);
135 static GType
136 impl_get_column_type (GtkTreeModel *self, gint column)
138 GType types [] = {
139 MM_TYPE_APPLICATION,
140 G_TYPE_ICON,
141 G_TYPE_STRING,
142 MM_TYPE_MAPPLICATION_TYPE
145 g_return_val_if_fail (MM_GTK_IS_APPLICATION_STORE (self), G_TYPE_INVALID);
146 g_return_val_if_fail (column >= 0 && column < MM_GTK_APP_STORE_N_COLUMNS, G_TYPE_INVALID);
148 return types[column];
151 static void
152 impl_get_value (GtkTreeModel *self, GtkTreeIter *iter,
153 gint column, GValue *value)
155 MMApplication *app;
157 g_return_if_fail (MM_GTK_IS_APPLICATION_STORE (self));
158 g_return_if_fail (iter != NULL);
159 g_return_if_fail (column >= 0 && column < MM_GTK_APP_STORE_N_COLUMNS);
160 g_return_if_fail (value != NULL);
162 app = mm_gtk_application_store_get_object (MM_GTK_APPLICATION_STORE (self), iter);
163 value = g_value_init (value, impl_get_column_type (self, column));
165 switch (column) {
166 case MM_GTK_APP_STORE_APP_COLUMN:
167 /* return the object itself */
168 g_value_set_object (value, app);
169 break;
170 case MM_GTK_APP_STORE_GICON_COLUMN:
171 value_set_gicon_from_app (value, app);
172 break;
173 case MM_GTK_APP_STORE_NAME_COLUMN:
174 value_set_name_from_app (value, app);
175 break;
176 case MM_GTK_APP_STORE_TYPE_COLUMN:
177 value_set_supp_type_from_app (value, app);
178 break;
179 default:
180 g_assert_not_reached ();
181 break;
184 g_object_unref (app);
187 static int
188 impl_get_n_columns (GtkTreeModel *self)
190 g_return_val_if_fail (MM_GTK_IS_APPLICATION_STORE (self), 0);
192 /* we have four columns:
193 * - app itself
194 * - gicon
195 * - name
196 * - supported types
198 return MM_GTK_APP_STORE_N_COLUMNS;
201 static void
202 mm_gtk_application_store_tree_model_iface_init (GtkTreeModelIface *iface)
204 /* save a copy of our parent's iface */
205 parent_iface = *iface;
207 /* override the iface methods */
208 iface->get_n_columns = impl_get_n_columns;
209 iface->get_column_type = impl_get_column_type;
210 iface->get_value = impl_get_value;
213 /* public methods */
214 MMGtkApplicationStore*
215 mm_gtk_application_store_new (void)
217 return g_object_new (MM_GTK_TYPE_APPLICATION_STORE, NULL);