]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-profile-chooser.c
Updated Oriya Translation
[empathy.git] / libempathy-gtk / empathy-profile-chooser.c
1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */
2 /*
3  * Copyright (C) 2007-2009 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  *          Jonny Lamb <jonny.lamb@collabora.co.uk>
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 <libempathy/empathy-utils.h>
32
33 #include "empathy-profile-chooser.h"
34 #include "empathy-ui-utils.h"
35
36 /**
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
41  *
42  * #EmpathyProfileChooser is a widget which extends #GtkComboBox to provides a
43  * chooser of available profiles.
44  */
45
46 /**
47  * EmpathyProfileChooser:
48  * @parent: parent object
49  *
50  * Widget which extends #GtkComboBox to provide a chooser of available
51  * profiles.
52  */
53
54 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyProfileChooser)
55 typedef struct
56 {
57   GtkListStore *store;
58   gboolean dispose_run;
59 } EmpathyProfileChooserPriv;
60
61 enum
62 {
63   COL_ICON,
64   COL_LABEL,
65   COL_PROFILE,
66   COL_COUNT
67 };
68
69 G_DEFINE_TYPE (EmpathyProfileChooser, empathy_profile_chooser,
70     GTK_TYPE_COMBO_BOX);
71
72 static gint
73 profile_chooser_sort_profile_value (McProfile *profile)
74 {
75   guint i;
76   const gchar *profile_name;
77   const gchar *names[] = {
78     "jabber",
79     "salut",
80     "gtalk",
81     NULL
82   };
83
84   profile_name = mc_profile_get_unique_name (profile);
85
86   for (i = 0 ; names[i]; i++)
87     {
88       if (strcmp (profile_name, names[i]) == 0)
89         return i;
90     }
91
92   return i;
93 }
94
95 static gint
96 profile_chooser_sort_func (GtkTreeModel *model,
97     GtkTreeIter  *iter_a,
98     GtkTreeIter  *iter_b,
99     gpointer      user_data)
100 {
101   McProfile *profile_a;
102   McProfile *profile_b;
103   gint cmp;
104
105   gtk_tree_model_get (model, iter_a,
106       COL_PROFILE, &profile_a,
107       -1);
108   gtk_tree_model_get (model, iter_b,
109       COL_PROFILE, &profile_b,
110       -1);
111
112   cmp = profile_chooser_sort_profile_value (profile_a);
113   cmp -= profile_chooser_sort_profile_value (profile_b);
114   if (cmp == 0)
115     {
116       cmp = strcmp (mc_profile_get_display_name (profile_a),
117           mc_profile_get_display_name (profile_b));
118     }
119
120   g_object_unref (profile_a);
121   g_object_unref (profile_b);
122
123   return cmp;
124 }
125
126 static void
127 profile_chooser_constructed (GObject *object)
128 {
129   EmpathyProfileChooser *profile_chooser;
130   EmpathyProfileChooserPriv *priv;
131
132   GList *profiles, *l, *seen;
133   GtkCellRenderer *renderer;
134   GtkTreeIter iter;
135   gboolean iter_set = FALSE;
136   McManager *btf_cm;
137
138   priv = GET_PRIV (object);
139   profile_chooser = EMPATHY_PROFILE_CHOOSER (object);
140
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   */
146
147   gtk_combo_box_set_model (GTK_COMBO_BOX (object),
148       GTK_TREE_MODEL (priv->store));
149
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,
154       NULL);
155   g_object_set (renderer, "stock-size", GTK_ICON_SIZE_BUTTON, NULL);
156
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,
160       "text", COL_LABEL,
161       NULL);
162
163   btf_cm = mc_manager_lookup ("butterfly");
164   profiles = mc_profiles_list ();
165   seen = NULL;
166   for (l = profiles; l; l = g_list_next (l))
167     {
168       McProfile   *profile;
169       McProtocol  *protocol;
170       const gchar *unique_name;
171
172       profile = l->data;
173
174       /* Check if the CM is installed, otherwise skip that profile.
175        * Workaround SF bug #1688779 */
176       protocol = mc_profile_get_protocol (profile);
177       if (!protocol)
178         continue;
179
180       g_object_unref (protocol);
181
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)
185         continue;
186
187       if (g_list_find_custom (seen, unique_name, (GCompareFunc) strcmp))
188         continue;
189
190       seen = g_list_append (seen, (char *) unique_name);
191
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,
196           -1);
197       iter_set = TRUE;
198     }
199
200   g_list_free (seen);
201
202   if (btf_cm)
203     g_object_unref (btf_cm);
204
205   /* Set the profile sort function */
206   gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (priv->store),
207       COL_PROFILE,
208       profile_chooser_sort_func,
209       NULL, NULL);
210   gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (priv->store),
211       COL_PROFILE,
212       GTK_SORT_ASCENDING);
213
214   if (iter_set)
215     gtk_combo_box_set_active_iter (GTK_COMBO_BOX (object), &iter);
216
217   mc_profiles_free_list (profiles);
218
219   if (G_OBJECT_CLASS (empathy_profile_chooser_parent_class)->constructed)
220     G_OBJECT_CLASS (empathy_profile_chooser_parent_class)->constructed (object);
221 }
222
223 static void
224 empathy_profile_chooser_init (EmpathyProfileChooser *profile_chooser)
225 {
226   EmpathyProfileChooserPriv *priv =
227     G_TYPE_INSTANCE_GET_PRIVATE (profile_chooser,
228         EMPATHY_TYPE_PROFILE_CHOOSER, EmpathyProfileChooserPriv);
229
230   priv->dispose_run = FALSE;
231
232   profile_chooser->priv = priv;
233 }
234
235 static void
236 profile_chooser_dispose (GObject *object)
237 {
238   EmpathyProfileChooser *profile_chooser = EMPATHY_PROFILE_CHOOSER (object);
239   EmpathyProfileChooserPriv *priv = GET_PRIV (profile_chooser);
240
241   if (priv->dispose_run)
242     return;
243
244   priv->dispose_run = TRUE;
245
246   if (priv->store)
247     {
248       g_object_unref (priv->store);
249       priv->store = NULL;
250     }
251
252   (G_OBJECT_CLASS (empathy_profile_chooser_parent_class)->dispose) (object);
253 }
254
255 static void
256 empathy_profile_chooser_class_init (EmpathyProfileChooserClass *klass)
257 {
258   GObjectClass *object_class = G_OBJECT_CLASS (klass);
259
260   object_class->constructed = profile_chooser_constructed;
261   object_class->dispose = profile_chooser_dispose;
262
263   g_type_class_add_private (object_class, sizeof (EmpathyProfileChooserPriv));
264 }
265
266 /**
267  * empathy_profile_chooser_dup_selected:
268  * @profile_chooser: an #EmpathyProfileChooser
269  *
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
272  * with.
273  *
274  * Return value: a new reference to the selected #McProfile
275  */
276 McProfile *
277 empathy_profile_chooser_dup_selected (EmpathyProfileChooser *profile_chooser)
278 {
279   EmpathyProfileChooserPriv *priv = GET_PRIV (profile_chooser);
280   GtkTreeIter iter;
281   McProfile *profile = NULL;
282
283   g_return_val_if_fail (EMPATHY_IS_PROFILE_CHOOSER (profile_chooser), NULL);
284
285   if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (profile_chooser), &iter))
286     {
287       gtk_tree_model_get (GTK_TREE_MODEL (priv->store), &iter,
288           COL_PROFILE, &profile,
289           -1);
290     }
291
292   return profile;
293 }
294
295 /**
296  * empathy_profile_chooser_n_profiles:
297  * @profile_chooser: an #EmpathyProfileChooser
298  *
299  * Returns the number of profiles in @profile_chooser.
300  *
301  * Return value: the number of profiles in @profile_chooser
302  */
303 gint
304 empathy_profile_chooser_n_profiles (EmpathyProfileChooser *profile_chooser)
305 {
306   EmpathyProfileChooserPriv *priv = GET_PRIV (profile_chooser);
307
308   g_return_val_if_fail (EMPATHY_IS_PROFILE_CHOOSER (profile_chooser), 0);
309
310   return gtk_tree_model_iter_n_children (GTK_TREE_MODEL (priv->store), NULL);
311 }
312
313 /**
314  * empathy_profile_chooser_new:
315  *
316  * Creates a new #EmpathyProfileChooser widget.
317  *
318  * Return value: a new #EmpathyProfileChooser widget
319  */
320 GtkWidget *
321 empathy_profile_chooser_new (void)
322 {
323   return GTK_WIDGET (g_object_new (EMPATHY_TYPE_PROFILE_CHOOSER, NULL));
324 }