]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-profile-chooser.c
212869da4ec92d8352380fadf96384d4a192bf4a
[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 <gtk/gtk.h>
26 #include <libmissioncontrol/mc-profile.h>
27
28 #include "empathy-profile-chooser.h"
29 #include "empathy-ui-utils.h"
30
31 enum {
32         COL_ICON,
33         COL_LABEL,
34         COL_PROFILE,
35         COL_COUNT
36 };
37
38 McProfile*
39 empathy_profile_chooser_get_selected (GtkWidget *widget)
40 {
41         GtkTreeModel *model;
42         GtkTreeIter   iter;
43         McProfile    *profile = NULL;
44
45         model = gtk_combo_box_get_model (GTK_COMBO_BOX (widget));
46         if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (widget), &iter)) {
47                 gtk_tree_model_get (model, &iter,
48                                     COL_PROFILE, &profile,
49                                     -1);
50         }
51
52         return profile;
53 }
54
55 static gint
56 profile_chooser_sort_profile_value (McProfile *profile)
57 {
58         guint        i;
59         const gchar *profile_name;
60         const gchar *names[] = {"jabber",
61                                 "salut",
62                                 "gtalk",
63                                 NULL};
64
65         profile_name = mc_profile_get_unique_name (profile);
66
67         for (i = 0 ; names[i]; i++) {
68                 if (strcmp (profile_name, names[i]) == 0) {
69                         return i;
70                 }
71         }
72
73         return i;
74 }
75
76 static gint
77 profile_chooser_sort_func (GtkTreeModel *model,
78                            GtkTreeIter  *iter_a,
79                            GtkTreeIter  *iter_b,
80                            gpointer      user_data)
81 {
82         McProfile   *profile_a;
83         McProfile   *profile_b;
84         gint         cmp;
85
86         gtk_tree_model_get (model, iter_a,
87                             COL_PROFILE, &profile_a,
88                             -1);
89         gtk_tree_model_get (model, iter_b,
90                             COL_PROFILE, &profile_b,
91                             -1);
92
93         cmp = profile_chooser_sort_profile_value (profile_a);
94         cmp -= profile_chooser_sort_profile_value (profile_b);
95         if (cmp == 0) {
96                 cmp = strcmp (mc_profile_get_display_name (profile_a),
97                               mc_profile_get_display_name (profile_b));
98         }
99
100         g_object_unref (profile_a);
101         g_object_unref (profile_b);
102
103         return cmp;
104 }
105
106 GtkWidget *
107 empathy_profile_chooser_new (void)
108 {
109         GList           *profiles, *l;
110         GtkListStore    *store;
111         GtkCellRenderer *renderer;
112         GtkWidget       *combo_box;
113         GtkTreeIter      iter;
114
115         /* set up combo box with new store */
116         store = gtk_list_store_new (COL_COUNT,
117                                     G_TYPE_STRING,    /* Icon name */
118                                     G_TYPE_STRING,    /* Label     */
119                                     MC_TYPE_PROFILE); /* Profile   */
120         combo_box = gtk_combo_box_new_with_model (GTK_TREE_MODEL (store));
121
122
123         renderer = gtk_cell_renderer_pixbuf_new ();
124         gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), renderer, FALSE);
125         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box), renderer,
126                                         "icon-name", COL_ICON,
127                                         NULL);
128         g_object_set (renderer, "stock-size", GTK_ICON_SIZE_BUTTON, NULL);
129
130         renderer = gtk_cell_renderer_text_new ();
131         gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), renderer, TRUE);
132         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box), renderer,
133                                         "text", COL_LABEL,
134                                         NULL);
135
136         /* Set the profile sort function */
137         gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
138                                               COL_PROFILE,
139                                               GTK_SORT_ASCENDING);
140         gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (store),
141                                          COL_PROFILE,
142                                          profile_chooser_sort_func,
143                                          NULL, NULL);
144
145         profiles = mc_profiles_list ();
146         for (l = profiles; l; l = l->next) {
147                 McProfile *profile;
148
149                 profile = l->data;
150
151                 gtk_list_store_append (store, &iter);
152                 gtk_list_store_set (store, &iter,
153                                     COL_ICON, mc_profile_get_icon_name (profile),
154                                     COL_LABEL, mc_profile_get_display_name (profile),
155                                     COL_PROFILE, profile,
156                                     -1);
157         }
158
159         gtk_combo_box_set_active_iter (GTK_COMBO_BOX (combo_box), &iter);
160
161         mc_profiles_free_list (profiles);
162         g_object_unref (store);
163
164         return combo_box;
165 }
166