]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-profile-chooser.c
Misc. gtk-doc updates, including being more explicit about refs.
[empathy.git] / libempathy-gtk / empathy-profile-chooser.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2007-2008 Collabora Ltd.
4  *
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.
9  *
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.
14  *
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
18  *
19  * Authors: Xavier Claessens <xclaesse@gmail.com>
20  */
21
22 #include <config.h>
23
24 #include <string.h>
25
26 #include <gtk/gtk.h>
27 #include <libmissioncontrol/mc-profile.h>
28 #include <libmissioncontrol/mc-protocol.h>
29
30 #include "empathy-profile-chooser.h"
31 #include "empathy-ui-utils.h"
32
33 /**
34  * SECTION:empathy-profile-chooser
35  * @title: EmpathyProfileChooser
36  * @short_description: A widget used to choose from a list of profiles
37  * @include: libempathy-gtk/empathy-account-chooser.h
38  *
39  * #EmpathyProfileChooser is a widget which provides a chooser of available
40  * profiles.
41  */
42
43 enum {
44         COL_ICON,
45         COL_LABEL,
46         COL_PROFILE,
47         COL_COUNT
48 };
49
50 /**
51  * empathy_profile_chooser_dup_selected:
52  * @widget: an #EmpathyProfileChooser
53  *
54  * Returns a new reference to the selected #McProfile in @widget. The returned
55  * #McProfile should be unrefed with g_object_unref() when finished with.
56  *
57  * Return value: a new reference to the selected #McProfile
58  */
59 McProfile*
60 empathy_profile_chooser_dup_selected (GtkWidget *widget)
61 {
62         GtkTreeModel *model;
63         GtkTreeIter   iter;
64         McProfile    *profile = NULL;
65
66         model = gtk_combo_box_get_model (GTK_COMBO_BOX (widget));
67         if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (widget), &iter)) {
68                 gtk_tree_model_get (model, &iter,
69                                     COL_PROFILE, &profile,
70                                     -1);
71         }
72
73         return profile;
74 }
75
76 /**
77  * empathy_profile_chooser_n_profiles:
78  * @widget: an #EmpathyProfileChooser
79  *
80  * Returns the number of profiles in @widget.
81  *
82  * Return value: the number of profiles in @widget
83  */
84 gint
85 empathy_profile_chooser_n_profiles (GtkWidget *widget)
86 {
87         GtkTreeModel *model;
88
89         model = gtk_combo_box_get_model (GTK_COMBO_BOX (widget));
90
91         return gtk_tree_model_iter_n_children (model, NULL);
92 }
93
94 static gint
95 profile_chooser_sort_profile_value (McProfile *profile)
96 {
97         guint        i;
98         const gchar *profile_name;
99         const gchar *names[] = {"jabber",
100                                 "salut",
101                                 "gtalk",
102                                 NULL};
103
104         profile_name = mc_profile_get_unique_name (profile);
105
106         for (i = 0 ; names[i]; i++) {
107                 if (strcmp (profile_name, names[i]) == 0) {
108                         return i;
109                 }
110         }
111
112         return i;
113 }
114
115 static gint
116 profile_chooser_sort_func (GtkTreeModel *model,
117                            GtkTreeIter  *iter_a,
118                            GtkTreeIter  *iter_b,
119                            gpointer      user_data)
120 {
121         McProfile   *profile_a;
122         McProfile   *profile_b;
123         gint         cmp;
124
125         gtk_tree_model_get (model, iter_a,
126                             COL_PROFILE, &profile_a,
127                             -1);
128         gtk_tree_model_get (model, iter_b,
129                             COL_PROFILE, &profile_b,
130                             -1);
131
132         cmp = profile_chooser_sort_profile_value (profile_a);
133         cmp -= profile_chooser_sort_profile_value (profile_b);
134         if (cmp == 0) {
135                 cmp = strcmp (mc_profile_get_display_name (profile_a),
136                               mc_profile_get_display_name (profile_b));
137         }
138
139         g_object_unref (profile_a);
140         g_object_unref (profile_b);
141
142         return cmp;
143 }
144
145 /**
146  * empathy_profile_chooser_new:
147  *
148  * Creates a new #EmpathyProfileChooser widget.
149  *
150  * Return value: a new #EmpathyProfileChooser widget
151  */
152 GtkWidget *
153 empathy_profile_chooser_new (void)
154 {
155         GList           *profiles, *l, *seen;
156         GtkListStore    *store;
157         GtkCellRenderer *renderer;
158         GtkWidget       *combo_box;
159         GtkTreeIter      iter;
160         gboolean         iter_set = FALSE;
161         McManager       *btf_cm;
162
163         /* set up combo box with new store */
164         store = gtk_list_store_new (COL_COUNT,
165                                     G_TYPE_STRING,    /* Icon name */
166                                     G_TYPE_STRING,    /* Label     */
167                                     MC_TYPE_PROFILE); /* Profile   */
168         combo_box = gtk_combo_box_new_with_model (GTK_TREE_MODEL (store));
169
170
171         renderer = gtk_cell_renderer_pixbuf_new ();
172         gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), renderer, FALSE);
173         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box), renderer,
174                                         "icon-name", COL_ICON,
175                                         NULL);
176         g_object_set (renderer, "stock-size", GTK_ICON_SIZE_BUTTON, NULL);
177
178         renderer = gtk_cell_renderer_text_new ();
179         gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), renderer, TRUE);
180         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box), renderer,
181                                         "text", COL_LABEL,
182                                         NULL);
183
184         btf_cm = mc_manager_lookup ("butterfly");
185         profiles = mc_profiles_list ();
186         seen = NULL;
187         for (l = profiles; l; l = l->next) {
188                 McProfile   *profile;
189                 McProtocol  *protocol;
190                 const gchar *unique_name;
191
192                 profile = l->data;
193
194                 /* Check if the CM is installed, otherwise skip that profile.
195                  * Workaround SF bug #1688779 */
196                 protocol = mc_profile_get_protocol (profile);
197                 if (!protocol) {
198                         continue;
199                 }
200                 g_object_unref (protocol);
201
202                 /* Skip MSN-Haze if we have butterfly */
203                 unique_name = mc_profile_get_unique_name (profile);
204                 if (btf_cm && strcmp (unique_name, "msn-haze") == 0) {
205                         continue;
206                 }
207
208                 if (g_list_find_custom (seen, unique_name, (GCompareFunc) strcmp)) {
209                         continue;
210                 }
211                 seen = g_list_append (seen, (char*) unique_name);
212
213                 gtk_list_store_insert_with_values (store, &iter, 0,
214                                                    COL_ICON, mc_profile_get_icon_name (profile),
215                                                    COL_LABEL, mc_profile_get_display_name (profile),
216                                                    COL_PROFILE, profile,
217                                                    -1);
218                 iter_set = TRUE;
219         }
220
221         g_list_free (seen);
222
223         if (btf_cm) {
224                 g_object_unref (btf_cm);
225         }
226
227         /* Set the profile sort function */
228         gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (store),
229                                          COL_PROFILE,
230                                          profile_chooser_sort_func,
231                                          NULL, NULL);
232         gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
233                                               COL_PROFILE,
234                                               GTK_SORT_ASCENDING);
235
236         if (iter_set) {
237                 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (combo_box), &iter);
238         }
239
240         mc_profiles_free_list (profiles);
241         g_object_unref (store);
242
243         return combo_box;
244 }
245