]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-profile-chooser.c
Set the sort function before begin to sort otherwise we get warnings.
[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 Collabora Ltd.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program 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  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * Authors: Xavier Claessens <xclaesse@gmail.com>
21  */
22
23 #include <config.h>
24
25 #include <string.h>
26
27 #include <gtk/gtk.h>
28 #include <libmissioncontrol/mc-profile.h>
29 #include <libmissioncontrol/mc-protocol.h>
30
31 #include "empathy-profile-chooser.h"
32 #include "empathy-ui-utils.h"
33
34 enum {
35         COL_ICON,
36         COL_LABEL,
37         COL_PROFILE,
38         COL_COUNT
39 };
40
41 McProfile*
42 empathy_profile_chooser_get_selected (GtkWidget *widget)
43 {
44         GtkTreeModel *model;
45         GtkTreeIter   iter;
46         McProfile    *profile = NULL;
47
48         model = gtk_combo_box_get_model (GTK_COMBO_BOX (widget));
49         if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (widget), &iter)) {
50                 gtk_tree_model_get (model, &iter,
51                                     COL_PROFILE, &profile,
52                                     -1);
53         }
54
55         return profile;
56 }
57
58 static gint
59 profile_chooser_sort_profile_value (McProfile *profile)
60 {
61         guint        i;
62         const gchar *profile_name;
63         const gchar *names[] = {"jabber",
64                                 "salut",
65                                 "gtalk",
66                                 NULL};
67
68         profile_name = mc_profile_get_unique_name (profile);
69
70         for (i = 0 ; names[i]; i++) {
71                 if (strcmp (profile_name, names[i]) == 0) {
72                         return i;
73                 }
74         }
75
76         return i;
77 }
78
79 static gint
80 profile_chooser_sort_func (GtkTreeModel *model,
81                            GtkTreeIter  *iter_a,
82                            GtkTreeIter  *iter_b,
83                            gpointer      user_data)
84 {
85         McProfile   *profile_a;
86         McProfile   *profile_b;
87         gint         cmp;
88
89         gtk_tree_model_get (model, iter_a,
90                             COL_PROFILE, &profile_a,
91                             -1);
92         gtk_tree_model_get (model, iter_b,
93                             COL_PROFILE, &profile_b,
94                             -1);
95
96         cmp = profile_chooser_sort_profile_value (profile_a);
97         cmp -= profile_chooser_sort_profile_value (profile_b);
98         if (cmp == 0) {
99                 cmp = strcmp (mc_profile_get_display_name (profile_a),
100                               mc_profile_get_display_name (profile_b));
101         }
102
103         g_object_unref (profile_a);
104         g_object_unref (profile_b);
105
106         return cmp;
107 }
108
109 GtkWidget *
110 empathy_profile_chooser_new (void)
111 {
112         GList           *profiles, *l;
113         GtkListStore    *store;
114         GtkCellRenderer *renderer;
115         GtkWidget       *combo_box;
116         GtkTreeIter      iter;
117         gboolean         iter_set = FALSE;
118
119         /* set up combo box with new store */
120         store = gtk_list_store_new (COL_COUNT,
121                                     G_TYPE_STRING,    /* Icon name */
122                                     G_TYPE_STRING,    /* Label     */
123                                     MC_TYPE_PROFILE); /* Profile   */
124         combo_box = gtk_combo_box_new_with_model (GTK_TREE_MODEL (store));
125
126
127         renderer = gtk_cell_renderer_pixbuf_new ();
128         gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), renderer, FALSE);
129         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box), renderer,
130                                         "icon-name", COL_ICON,
131                                         NULL);
132         g_object_set (renderer, "stock-size", GTK_ICON_SIZE_BUTTON, NULL);
133
134         renderer = gtk_cell_renderer_text_new ();
135         gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), renderer, TRUE);
136         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box), renderer,
137                                         "text", COL_LABEL,
138                                         NULL);
139
140         profiles = mc_profiles_list ();
141         for (l = profiles; l; l = l->next) {
142                 McProfile  *profile;
143                 McProtocol *protocol;
144
145                 profile = l->data;
146
147                 /* Check if the CM is installed, otherwise skip that profile.
148                  * Workaround SF bug #1688779 */
149                 protocol = mc_profile_get_protocol (profile);
150                 if (!protocol) {
151                         continue;
152                 }
153                 g_object_unref (protocol);
154
155                 gtk_list_store_insert_with_values (store, &iter, 0,
156                                                    COL_ICON, mc_profile_get_icon_name (profile),
157                                                    COL_LABEL, mc_profile_get_display_name (profile),
158                                                    COL_PROFILE, profile,
159                                                    -1);
160                 iter_set = TRUE;
161         }
162
163         /* Set the profile sort function */
164         gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (store),
165                                          COL_PROFILE,
166                                          profile_chooser_sort_func,
167                                          NULL, NULL);
168         gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
169                                               COL_PROFILE,
170                                               GTK_SORT_ASCENDING);
171
172         if (iter_set) {
173                 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (combo_box), &iter);
174         }
175
176         mc_profiles_free_list (profiles);
177         g_object_unref (store);
178
179         return combo_box;
180 }
181