1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */
3 * Copyright (C) 2007-2009 Collabora Ltd.
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.1 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 Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Authors: Xavier Claessens <xclaesse@gmail.com>
20 * Jonny Lamb <jonny.lamb@collabora.co.uk>
28 #include <libmissioncontrol/mc-profile.h>
29 #include <libmissioncontrol/mc-protocol.h>
31 #include <libempathy/empathy-utils.h>
33 #include "empathy-profile-chooser.h"
34 #include "empathy-ui-utils.h"
37 * SECTION:empathy-profile-chooser
38 * @title: EmpathyProfileChooser
39 * @short_description: A widget used to choose from a list of profiles
40 * @include: libempathy-gtk/empathy-profile-chooser.h
42 * #EmpathyProfileChooser is a widget which extends #GtkComboBox to provides a
43 * chooser of available profiles.
47 * EmpathyProfileChooser:
48 * @parent: parent object
50 * Widget which extends #GtkComboBox to provide a chooser of available
54 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyProfileChooser)
59 } EmpathyProfileChooserPriv;
69 G_DEFINE_TYPE (EmpathyProfileChooser, empathy_profile_chooser,
73 profile_chooser_sort_profile_value (McProfile *profile)
76 const gchar *profile_name;
77 const gchar *names[] = {
84 profile_name = mc_profile_get_unique_name (profile);
86 for (i = 0 ; names[i]; i++)
88 if (strcmp (profile_name, names[i]) == 0)
96 profile_chooser_sort_func (GtkTreeModel *model,
101 McProfile *profile_a;
102 McProfile *profile_b;
105 gtk_tree_model_get (model, iter_a,
106 COL_PROFILE, &profile_a,
108 gtk_tree_model_get (model, iter_b,
109 COL_PROFILE, &profile_b,
112 cmp = profile_chooser_sort_profile_value (profile_a);
113 cmp -= profile_chooser_sort_profile_value (profile_b);
116 cmp = strcmp (mc_profile_get_display_name (profile_a),
117 mc_profile_get_display_name (profile_b));
120 g_object_unref (profile_a);
121 g_object_unref (profile_b);
127 profile_chooser_constructed (GObject *object)
129 EmpathyProfileChooser *profile_chooser;
130 EmpathyProfileChooserPriv *priv;
132 GList *profiles, *l, *seen;
133 GtkCellRenderer *renderer;
135 gboolean iter_set = FALSE;
138 priv = GET_PRIV (object);
139 profile_chooser = EMPATHY_PROFILE_CHOOSER (object);
141 /* set up combo box with new store */
142 priv->store = gtk_list_store_new (COL_COUNT,
143 G_TYPE_STRING, /* Icon name */
144 G_TYPE_STRING, /* Label */
145 MC_TYPE_PROFILE); /* Profile */
147 gtk_combo_box_set_model (GTK_COMBO_BOX (object),
148 GTK_TREE_MODEL (priv->store));
150 renderer = gtk_cell_renderer_pixbuf_new ();
151 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (object), renderer, FALSE);
152 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (object), renderer,
153 "icon-name", COL_ICON,
155 g_object_set (renderer, "stock-size", GTK_ICON_SIZE_BUTTON, NULL);
157 renderer = gtk_cell_renderer_text_new ();
158 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (object), renderer, TRUE);
159 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (object), renderer,
163 btf_cm = mc_manager_lookup ("butterfly");
164 profiles = mc_profiles_list ();
166 for (l = profiles; l; l = g_list_next (l))
169 McProtocol *protocol;
170 const gchar *unique_name;
174 /* Check if the CM is installed, otherwise skip that profile.
175 * Workaround SF bug #1688779 */
176 protocol = mc_profile_get_protocol (profile);
180 g_object_unref (protocol);
182 /* Skip MSN-Haze if we have butterfly */
183 unique_name = mc_profile_get_unique_name (profile);
184 if (btf_cm && strcmp (unique_name, "msn-haze") == 0)
187 if (g_list_find_custom (seen, unique_name, (GCompareFunc) strcmp))
190 seen = g_list_append (seen, (char *) unique_name);
192 gtk_list_store_insert_with_values (priv->store, &iter, 0,
193 COL_ICON, mc_profile_get_icon_name (profile),
194 COL_LABEL, mc_profile_get_display_name (profile),
195 COL_PROFILE, profile,
203 g_object_unref (btf_cm);
205 /* Set the profile sort function */
206 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (priv->store),
208 profile_chooser_sort_func,
210 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (priv->store),
215 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (object), &iter);
217 mc_profiles_free_list (profiles);
219 if (G_OBJECT_CLASS (empathy_profile_chooser_parent_class)->constructed)
220 G_OBJECT_CLASS (empathy_profile_chooser_parent_class)->constructed (object);
224 empathy_profile_chooser_init (EmpathyProfileChooser *profile_chooser)
226 EmpathyProfileChooserPriv *priv =
227 G_TYPE_INSTANCE_GET_PRIVATE (profile_chooser,
228 EMPATHY_TYPE_PROFILE_CHOOSER, EmpathyProfileChooserPriv);
230 priv->dispose_run = FALSE;
232 profile_chooser->priv = priv;
236 profile_chooser_dispose (GObject *object)
238 EmpathyProfileChooser *profile_chooser = EMPATHY_PROFILE_CHOOSER (object);
239 EmpathyProfileChooserPriv *priv = GET_PRIV (profile_chooser);
241 if (priv->dispose_run)
244 priv->dispose_run = TRUE;
248 g_object_unref (priv->store);
252 (G_OBJECT_CLASS (empathy_profile_chooser_parent_class)->dispose) (object);
256 empathy_profile_chooser_class_init (EmpathyProfileChooserClass *klass)
258 GObjectClass *object_class = G_OBJECT_CLASS (klass);
260 object_class->constructed = profile_chooser_constructed;
261 object_class->dispose = profile_chooser_dispose;
263 g_type_class_add_private (object_class, sizeof (EmpathyProfileChooserPriv));
267 * empathy_profile_chooser_dup_selected:
268 * @profile_chooser: an #EmpathyProfileChooser
270 * Returns a new reference to the selected #McProfile in @profile_chooser. The
271 * returned #McProfile should be unrefed with g_object_unref() when finished
274 * Return value: a new reference to the selected #McProfile
277 empathy_profile_chooser_dup_selected (EmpathyProfileChooser *profile_chooser)
279 EmpathyProfileChooserPriv *priv = GET_PRIV (profile_chooser);
281 McProfile *profile = NULL;
283 g_return_val_if_fail (EMPATHY_IS_PROFILE_CHOOSER (profile_chooser), NULL);
285 if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (profile_chooser), &iter))
287 gtk_tree_model_get (GTK_TREE_MODEL (priv->store), &iter,
288 COL_PROFILE, &profile,
296 * empathy_profile_chooser_n_profiles:
297 * @profile_chooser: an #EmpathyProfileChooser
299 * Returns the number of profiles in @profile_chooser.
301 * Return value: the number of profiles in @profile_chooser
304 empathy_profile_chooser_n_profiles (EmpathyProfileChooser *profile_chooser)
306 EmpathyProfileChooserPriv *priv = GET_PRIV (profile_chooser);
308 g_return_val_if_fail (EMPATHY_IS_PROFILE_CHOOSER (profile_chooser), 0);
310 return gtk_tree_model_iter_n_children (GTK_TREE_MODEL (priv->store), NULL);
314 * empathy_profile_chooser_new:
316 * Creates a new #EmpathyProfileChooser widget.
318 * Return value: a new #EmpathyProfileChooser widget
321 empathy_profile_chooser_new (void)
323 return GTK_WIDGET (g_object_new (EMPATHY_TYPE_PROFILE_CHOOSER, NULL));