3.12.12
[nijm-empathy.git] / libempathy-gtk / empathy-protocol-chooser.c
blobff8dac123f653bf09be12c684dbfe01e3dfda5b3
1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */
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>
23 #include "config.h"
24 #include "empathy-protocol-chooser.h"
26 #include <glib/gi18n-lib.h>
27 #include <tp-account-widgets/tpaw-connection-managers.h>
28 #include <tp-account-widgets/tpaw-pixbuf-utils.h>
29 #include <tp-account-widgets/tpaw-utils.h>
31 #include "empathy-ui-utils.h"
32 #include "empathy-utils.h"
34 #define DEBUG_FLAG EMPATHY_DEBUG_ACCOUNT
35 #include "empathy-debug.h"
37 /**
38 * SECTION:empathy-protocol-chooser
39 * @title: EmpathyProtocolChooser
40 * @short_description: A widget used to choose from a list of protocols
41 * @include: libempathy-gtk/empathy-protocol-chooser.h
43 * #EmpathyProtocolChooser is a widget which extends #GtkComboBox to provides a
44 * chooser of available protocols.
47 /**
48 * EmpathyProtocolChooser:
49 * @parent: parent object
51 * Widget which extends #GtkComboBox to provide a chooser of available
52 * protocols.
55 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyProtocolChooser)
56 typedef struct
58 GtkListStore *store;
60 gboolean dispose_run;
62 EmpathyProtocolChooserFilterFunc filter_func;
63 gpointer filter_user_data;
64 } EmpathyProtocolChooserPriv;
66 enum
68 COL_ICON,
69 COL_LABEL,
70 COL_PROTOCOL,
71 COL_COUNT
74 G_DEFINE_TYPE (EmpathyProtocolChooser, empathy_protocol_chooser,
75 GTK_TYPE_COMBO_BOX);
77 static void
78 protocol_chooser_add_protocol (EmpathyProtocolChooser *chooser,
79 TpawProtocol *protocol)
81 EmpathyProtocolChooserPriv *priv = GET_PRIV (chooser);
82 GdkPixbuf *pixbuf;
84 pixbuf = tpaw_pixbuf_from_icon_name (tpaw_protocol_get_icon_name (protocol),
85 GTK_ICON_SIZE_BUTTON);
87 gtk_list_store_insert_with_values (priv->store,
88 NULL, -1,
89 COL_ICON, pixbuf,
90 COL_LABEL, tpaw_protocol_get_display_name (protocol),
91 COL_PROTOCOL, protocol,
92 -1);
94 g_clear_object (&pixbuf);
97 static void
98 protocol_chooser_get_protocols_cb (GObject *source,
99 GAsyncResult *result,
100 gpointer user_data)
102 EmpathyProtocolChooser *protocol_chooser = user_data;
103 GList *protocols = NULL;
104 GList *l;
106 if (!tpaw_protocol_get_all_finish (&protocols, result, NULL))
107 return;
109 for (l = protocols; l != NULL; l = l->next)
110 protocol_chooser_add_protocol (protocol_chooser, l->data);
112 gtk_combo_box_set_active (GTK_COMBO_BOX (protocol_chooser), 0);
114 g_list_free_full (protocols, g_object_unref);
117 static void
118 protocol_chooser_constructed (GObject *object)
120 EmpathyProtocolChooser *protocol_chooser;
121 EmpathyProtocolChooserPriv *priv;
122 GtkCellRenderer *renderer;
124 priv = GET_PRIV (object);
125 protocol_chooser = EMPATHY_PROTOCOL_CHOOSER (object);
127 /* set up combo box with new store */
128 priv->store = gtk_list_store_new (COL_COUNT,
129 GDK_TYPE_PIXBUF, /* Icon */
130 G_TYPE_STRING, /* Label */
131 G_TYPE_OBJECT); /* protocol */
133 gtk_combo_box_set_model (GTK_COMBO_BOX (object),
134 GTK_TREE_MODEL (priv->store));
136 renderer = gtk_cell_renderer_pixbuf_new ();
137 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (object), renderer, FALSE);
138 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (object), renderer,
139 "pixbuf", COL_ICON,
140 NULL);
142 renderer = gtk_cell_renderer_text_new ();
143 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (object), renderer, TRUE);
144 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (object), renderer,
145 "text", COL_LABEL,
146 NULL);
148 tpaw_protocol_get_all_async (protocol_chooser_get_protocols_cb, protocol_chooser);
150 if (G_OBJECT_CLASS (empathy_protocol_chooser_parent_class)->constructed)
151 G_OBJECT_CLASS
152 (empathy_protocol_chooser_parent_class)->constructed (object);
155 static void
156 empathy_protocol_chooser_init (EmpathyProtocolChooser *protocol_chooser)
158 EmpathyProtocolChooserPriv *priv =
159 G_TYPE_INSTANCE_GET_PRIVATE (protocol_chooser,
160 EMPATHY_TYPE_PROTOCOL_CHOOSER, EmpathyProtocolChooserPriv);
162 priv->dispose_run = FALSE;
163 protocol_chooser->priv = priv;
166 static void
167 protocol_chooser_dispose (GObject *object)
169 EmpathyProtocolChooser *protocol_chooser = EMPATHY_PROTOCOL_CHOOSER (object);
170 EmpathyProtocolChooserPriv *priv = GET_PRIV (protocol_chooser);
172 if (priv->dispose_run)
173 return;
175 priv->dispose_run = TRUE;
177 if (priv->store)
179 g_object_unref (priv->store);
180 priv->store = NULL;
183 (G_OBJECT_CLASS (empathy_protocol_chooser_parent_class)->dispose) (object);
186 static void
187 empathy_protocol_chooser_class_init (EmpathyProtocolChooserClass *klass)
189 GObjectClass *object_class = G_OBJECT_CLASS (klass);
191 object_class->constructed = protocol_chooser_constructed;
192 object_class->dispose = protocol_chooser_dispose;
194 g_type_class_add_private (object_class, sizeof (EmpathyProtocolChooserPriv));
197 static gboolean
198 protocol_chooser_filter_visible_func (GtkTreeModel *model,
199 GtkTreeIter *iter,
200 gpointer user_data)
202 EmpathyProtocolChooser *protocol_chooser = user_data;
203 EmpathyProtocolChooserPriv *priv = GET_PRIV (protocol_chooser);
204 TpawProtocol *protocol;
205 TpProtocol *tp_protocol;
206 gboolean visible = FALSE;
208 gtk_tree_model_get (model, iter,
209 COL_PROTOCOL, &protocol,
210 -1);
212 tp_protocol = tp_connection_manager_get_protocol_object (
213 tpaw_protocol_get_cm (protocol),
214 tpaw_protocol_get_protocol_name (protocol));
216 if (tp_protocol != NULL)
218 visible = priv->filter_func (tpaw_protocol_get_cm (protocol),
219 tp_protocol, tpaw_protocol_get_service_name (protocol),
220 priv->filter_user_data);
223 return visible;
226 /* public methods */
229 * empathy_protocol_chooser_dup_selected:
230 * @protocol_chooser: an #EmpathyProtocolChooser
232 * Returns a pointer to the selected #TpawProtocol in
233 * @protocol_chooser.
235 * Return value: a pointer to the selected #TpawProtocol
237 TpawProtocol *
238 empathy_protocol_chooser_dup_selected (
239 EmpathyProtocolChooser *protocol_chooser)
241 GtkTreeIter iter;
242 GtkTreeModel *cur_model;
243 TpawProtocol *protocol = NULL;
245 g_return_val_if_fail (EMPATHY_IS_PROTOCOL_CHOOSER (protocol_chooser), NULL);
247 /* get the current model from the chooser, as we could either be filtering
248 * or not.
250 cur_model = gtk_combo_box_get_model (GTK_COMBO_BOX (protocol_chooser));
252 if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (protocol_chooser), &iter))
254 gtk_tree_model_get (GTK_TREE_MODEL (cur_model), &iter,
255 COL_PROTOCOL, &protocol,
256 -1);
259 return protocol;
263 * empathy_protocol_chooser_new:
265 * Triggers the creation of a new #EmpathyProtocolChooser.
267 * Return value: a new #EmpathyProtocolChooser widget
270 GtkWidget *
271 empathy_protocol_chooser_new (void)
273 return GTK_WIDGET (g_object_new (EMPATHY_TYPE_PROTOCOL_CHOOSER, NULL));
276 void
277 empathy_protocol_chooser_set_visible (EmpathyProtocolChooser *protocol_chooser,
278 EmpathyProtocolChooserFilterFunc func,
279 gpointer user_data)
281 EmpathyProtocolChooserPriv *priv;
282 GtkTreeModel *filter_model;
284 g_return_if_fail (EMPATHY_IS_PROTOCOL_CHOOSER (protocol_chooser));
286 priv = GET_PRIV (protocol_chooser);
287 priv->filter_func = func;
288 priv->filter_user_data = user_data;
290 filter_model = gtk_tree_model_filter_new (GTK_TREE_MODEL (priv->store),
291 NULL);
292 gtk_combo_box_set_model (GTK_COMBO_BOX (protocol_chooser), filter_model);
293 g_object_unref (filter_model);
295 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER
296 (filter_model), protocol_chooser_filter_visible_func,
297 protocol_chooser, NULL);
299 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (filter_model));
301 gtk_combo_box_set_active (GTK_COMBO_BOX (protocol_chooser), 0);
304 TpawAccountSettings *
305 empathy_protocol_chooser_create_account_settings (EmpathyProtocolChooser *self)
307 TpawProtocol *protocol;
308 TpawAccountSettings *settings;
310 protocol = empathy_protocol_chooser_dup_selected (self);
311 if (protocol == NULL)
312 return NULL;
314 settings = tpaw_protocol_create_account_settings (protocol);
316 tp_clear_object (&protocol);
318 return settings;