display the clock rate of the codec as well
[empathy-mirror.git] / src / empathy-import-widget.c
blob09898180a089e6d75da0990669e4c9ec4eb143a3
1 /*
2 * Copyright (C) 2008-2009 Collabora Ltd.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public
15 * License along with this program; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301 USA
19 * Authors: Jonny Lamb <jonny.lamb@collabora.co.uk>
20 * Cosimo Cecchi <cosimo.cecchi@collabora.co.uk>
23 /* empathy-import-widget.c */
25 #include <config.h>
27 #include "empathy-import-dialog.h"
28 #include "empathy-import-widget.h"
29 #include "empathy-import-pidgin.h"
31 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
32 #include <libempathy/empathy-debug.h>
33 #include <libempathy/empathy-connection-managers.h>
34 #include <libempathy/empathy-utils.h>
36 #include <libempathy-gtk/empathy-ui-utils.h>
38 #include <telepathy-glib/account-manager.h>
39 #include <telepathy-glib/util.h>
41 #include <glib/gi18n-lib.h>
43 G_DEFINE_TYPE (EmpathyImportWidget, empathy_import_widget, G_TYPE_OBJECT)
45 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyImportWidget)
47 enum
49 COL_IMPORT = 0,
50 COL_PROTOCOL,
51 COL_NAME,
52 COL_SOURCE,
53 COL_ACCOUNT_DATA,
54 COL_COUNT
57 enum {
58 PROP_APPLICATION_ID = 1
61 typedef struct {
62 GtkWidget *vbox;
63 GtkWidget *treeview;
65 GList *accounts;
66 EmpathyImportApplication app_id;
68 EmpathyConnectionManagers *cms;
70 gboolean dispose_run;
71 } EmpathyImportWidgetPriv;
73 static gboolean
74 import_widget_account_id_in_list (GList *accounts,
75 const gchar *account_id)
77 GList *l;
79 for (l = accounts; l; l = l->next)
81 TpAccount *account = l->data;
82 const GHashTable *parameters;
84 parameters = tp_account_get_parameters (account);
86 if (!tp_strdiff (tp_asv_get_string (parameters, "account"), account_id))
87 return TRUE;
90 return FALSE;
93 static void
94 account_manager_prepared_cb (GObject *source_object,
95 GAsyncResult *result,
96 gpointer user_data)
98 TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
99 EmpathyImportWidget *self = user_data;
100 GtkTreeModel *model;
101 GtkTreeIter iter;
102 GList *l;
103 EmpathyImportWidgetPriv *priv = GET_PRIV (self);
104 GError *error = NULL;
106 if (!tp_account_manager_prepare_finish (manager, result, &error))
108 DEBUG ("Failed to prepare account manager: %s", error->message);
109 g_error_free (error);
110 return;
113 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
115 for (l = priv->accounts; l; l = l->next)
117 GValue *value;
118 EmpathyImportAccountData *data = l->data;
119 gboolean import;
120 GList *accounts;
121 TpConnectionManager *cm = NULL;
123 if (!empathy_import_protocol_is_supported (data->protocol, &cm))
124 continue;
126 data->connection_manager = g_strdup (
127 tp_connection_manager_get_name (cm));
129 value = g_hash_table_lookup (data->settings, "account");
131 accounts = tp_account_manager_get_valid_accounts (manager);
133 /* Only set the "Import" cell to be active if there isn't already an
134 * account set up with the same account id. */
135 import = !import_widget_account_id_in_list (accounts,
136 g_value_get_string (value));
138 g_list_free (accounts);
140 gtk_list_store_append (GTK_LIST_STORE (model), &iter);
142 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
143 COL_IMPORT, import,
144 COL_PROTOCOL, data->protocol,
145 COL_NAME, g_value_get_string (value),
146 COL_SOURCE, data->source,
147 COL_ACCOUNT_DATA, data,
148 -1);
152 static void
153 import_widget_add_accounts_to_model (EmpathyImportWidget *self)
155 TpAccountManager *manager;
157 manager = tp_account_manager_dup ();
159 tp_account_manager_prepare_async (manager, NULL,
160 account_manager_prepared_cb, self);
162 g_object_unref (manager);
165 static void
166 import_widget_create_account_cb (GObject *source,
167 GAsyncResult *result,
168 gpointer user_data)
170 TpAccount *account;
171 GError *error = NULL;
172 EmpathyImportWidget *self = user_data;
174 account = tp_account_manager_create_account_finish (
175 TP_ACCOUNT_MANAGER (source), result, &error);
177 if (account == NULL)
179 DEBUG ("Failed to create account: %s",
180 error ? error->message : "No error given");
181 g_clear_error (&error);
182 return;
185 DEBUG ("account created\n");
187 g_object_unref (self);
190 static void
191 import_widget_add_account (EmpathyImportWidget *self,
192 EmpathyImportAccountData *data)
194 TpAccountManager *account_manager;
195 gchar *display_name;
196 GHashTable *properties;
197 GValue *username;
199 account_manager = tp_account_manager_dup ();
201 DEBUG ("connection_manager: %s\n", data->connection_manager);
203 /* Set the display name of the account */
204 username = g_hash_table_lookup (data->settings, "account");
205 display_name = g_strdup_printf ("%s (%s)",
206 data->protocol,
207 g_value_get_string (username));
209 DEBUG ("display name: %s\n", display_name);
211 properties = g_hash_table_new (NULL, NULL);
213 tp_account_manager_create_account_async (account_manager,
214 (const gchar*) data->connection_manager, data->protocol, display_name,
215 data->settings, properties, import_widget_create_account_cb,
216 g_object_ref (self));
218 g_hash_table_unref (properties);
219 g_free (display_name);
220 g_object_unref (account_manager);
223 static gboolean
224 import_widget_tree_model_foreach (GtkTreeModel *model,
225 GtkTreePath *path,
226 GtkTreeIter *iter,
227 gpointer user_data)
229 gboolean to_import;
230 EmpathyImportAccountData *data;
231 EmpathyImportWidget *self = user_data;
233 gtk_tree_model_get (model, iter,
234 COL_IMPORT, &to_import,
235 COL_ACCOUNT_DATA, &data,
236 -1);
238 if (to_import)
239 import_widget_add_account (self, data);
241 return FALSE;
244 static void
245 import_widget_cell_toggled_cb (GtkCellRendererToggle *cell_renderer,
246 const gchar *path_str,
247 EmpathyImportWidget *self)
249 GtkTreeModel *model;
250 GtkTreeIter iter;
251 GtkTreePath *path;
252 EmpathyImportWidgetPriv *priv = GET_PRIV (self);
254 path = gtk_tree_path_new_from_string (path_str);
255 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
257 gtk_tree_model_get_iter (model, &iter, path);
259 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
260 COL_IMPORT, !gtk_cell_renderer_toggle_get_active (cell_renderer),
261 -1);
263 gtk_tree_path_free (path);
266 static void
267 import_widget_set_up_account_list (EmpathyImportWidget *self)
269 EmpathyImportWidgetPriv *priv = GET_PRIV (self);
270 GtkListStore *store;
271 GtkTreeView *view;
272 GtkTreeViewColumn *column;
273 GtkCellRenderer *cell;
275 priv->accounts = empathy_import_accounts_load (priv->app_id);
277 store = gtk_list_store_new (COL_COUNT, G_TYPE_BOOLEAN, G_TYPE_STRING,
278 G_TYPE_STRING, G_TYPE_STRING, G_TYPE_POINTER);
280 gtk_tree_view_set_model (GTK_TREE_VIEW (priv->treeview),
281 GTK_TREE_MODEL (store));
283 g_object_unref (store);
285 view = GTK_TREE_VIEW (priv->treeview);
286 gtk_tree_view_set_headers_visible (view, TRUE);
288 /* Import column */
289 cell = gtk_cell_renderer_toggle_new ();
290 gtk_tree_view_insert_column_with_attributes (view, -1,
291 /* Translators: this is the header of a treeview column */
292 _("Import"), cell,
293 "active", COL_IMPORT,
294 NULL);
296 g_signal_connect (cell, "toggled",
297 G_CALLBACK (import_widget_cell_toggled_cb), self);
299 /* Protocol column */
300 column = gtk_tree_view_column_new ();
301 gtk_tree_view_column_set_title (column, _("Protocol"));
302 gtk_tree_view_column_set_expand (column, TRUE);
303 gtk_tree_view_append_column (view, column);
305 cell = gtk_cell_renderer_text_new ();
306 g_object_set (cell, "editable", FALSE, NULL);
307 gtk_tree_view_column_pack_start (column, cell, TRUE);
308 gtk_tree_view_column_add_attribute (column, cell, "text", COL_PROTOCOL);
310 /* Account column */
311 column = gtk_tree_view_column_new ();
312 gtk_tree_view_column_set_title (column, _("Account"));
313 gtk_tree_view_column_set_expand (column, TRUE);
314 gtk_tree_view_append_column (view, column);
316 cell = gtk_cell_renderer_text_new ();
317 g_object_set (cell, "editable", FALSE, NULL);
318 gtk_tree_view_column_pack_start (column, cell, TRUE);
319 gtk_tree_view_column_add_attribute (column, cell, "text", COL_NAME);
321 if (priv->app_id == EMPATHY_IMPORT_APPLICATION_ALL)
323 /* Source column */
324 column = gtk_tree_view_column_new ();
325 gtk_tree_view_column_set_title (column, _("Source"));
326 gtk_tree_view_column_set_expand (column, TRUE);
327 gtk_tree_view_append_column (view, column);
329 cell = gtk_cell_renderer_text_new ();
330 g_object_set (cell, "editable", FALSE, NULL);
331 gtk_tree_view_column_pack_start (column, cell, TRUE);
332 gtk_tree_view_column_add_attribute (column, cell, "text", COL_SOURCE);
335 import_widget_add_accounts_to_model (self);
338 static void
339 import_widget_cms_prepare_cb (GObject *source,
340 GAsyncResult *result,
341 gpointer user_data)
343 EmpathyImportWidget *self = user_data;
345 if (!empathy_connection_managers_prepare_finish (
346 EMPATHY_CONNECTION_MANAGERS (source), result, NULL))
347 return;
349 import_widget_set_up_account_list (self);
352 static void
353 import_widget_destroy_cb (GtkWidget *w,
354 EmpathyImportWidget *self)
356 g_object_unref (self);
359 static void
360 do_get_property (GObject *object,
361 guint property_id,
362 GValue *value,
363 GParamSpec *pspec)
365 EmpathyImportWidgetPriv *priv = GET_PRIV (object);
367 switch (property_id)
369 case PROP_APPLICATION_ID:
370 g_value_set_int (value, priv->app_id);
371 break;
372 default:
373 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
377 static void
378 do_set_property (GObject *object,
379 guint property_id,
380 const GValue *value,
381 GParamSpec *pspec)
383 EmpathyImportWidgetPriv *priv = GET_PRIV (object);
385 switch (property_id)
387 case PROP_APPLICATION_ID:
388 priv->app_id = g_value_get_int (value);
389 break;
390 default:
391 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
395 static void
396 do_finalize (GObject *obj)
398 EmpathyImportWidgetPriv *priv = GET_PRIV (obj);
400 g_list_foreach (priv->accounts, (GFunc) empathy_import_account_data_free,
401 NULL);
402 g_list_free (priv->accounts);
404 if (G_OBJECT_CLASS (empathy_import_widget_parent_class)->finalize != NULL)
405 G_OBJECT_CLASS (empathy_import_widget_parent_class)->finalize (obj);
408 static void
409 do_dispose (GObject *obj)
411 EmpathyImportWidgetPriv *priv = GET_PRIV (obj);
413 if (priv->dispose_run)
414 return;
416 priv->dispose_run = TRUE;
418 if (priv->cms != NULL)
420 g_object_unref (priv->cms);
421 priv->cms = NULL;
424 if (G_OBJECT_CLASS (empathy_import_widget_parent_class)->dispose != NULL)
425 G_OBJECT_CLASS (empathy_import_widget_parent_class)->dispose (obj);
428 static void
429 do_constructed (GObject *obj)
431 EmpathyImportWidget *self = EMPATHY_IMPORT_WIDGET (obj);
432 EmpathyImportWidgetPriv *priv = GET_PRIV (self);
433 GtkBuilder *gui;
434 gchar *filename;
436 filename = empathy_file_lookup ("empathy-import-dialog.ui", "src");
437 gui = empathy_builder_get_file (filename,
438 "widget_vbox", &priv->vbox,
439 "treeview", &priv->treeview,
440 NULL);
442 g_free (filename);
443 empathy_builder_unref_and_keep_widget (gui, priv->vbox);
445 g_signal_connect (priv->vbox, "destroy",
446 G_CALLBACK (import_widget_destroy_cb), self);
448 empathy_connection_managers_prepare_async (priv->cms,
449 import_widget_cms_prepare_cb, self);
452 static void
453 empathy_import_widget_class_init (EmpathyImportWidgetClass *klass)
455 GObjectClass *oclass = G_OBJECT_CLASS (klass);
456 GParamSpec *param_spec;
458 oclass->constructed = do_constructed;
459 oclass->finalize = do_finalize;
460 oclass->dispose = do_dispose;
461 oclass->set_property = do_set_property;
462 oclass->get_property = do_get_property;
464 param_spec = g_param_spec_int ("application-id",
465 "application-id", "The application id to import from",
466 0, EMPATHY_IMPORT_APPLICATION_INVALID, EMPATHY_IMPORT_APPLICATION_ALL,
467 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
468 g_object_class_install_property (oclass, PROP_APPLICATION_ID, param_spec);
470 g_type_class_add_private (klass, sizeof (EmpathyImportWidgetPriv));
473 static void
474 empathy_import_widget_init (EmpathyImportWidget *self)
476 EmpathyImportWidgetPriv *priv =
477 G_TYPE_INSTANCE_GET_PRIVATE (self, EMPATHY_TYPE_IMPORT_WIDGET,
478 EmpathyImportWidgetPriv);
480 self->priv = priv;
482 priv->cms = empathy_connection_managers_dup_singleton ();
485 EmpathyImportWidget *
486 empathy_import_widget_new (EmpathyImportApplication id)
488 return g_object_new (EMPATHY_TYPE_IMPORT_WIDGET, "application-id", id, NULL);
491 GtkWidget *
492 empathy_import_widget_get_widget (EmpathyImportWidget *self)
494 EmpathyImportWidgetPriv *priv = GET_PRIV (self);
496 return priv->vbox;
499 void
500 empathy_import_widget_add_selected_accounts (EmpathyImportWidget *self)
502 GtkTreeModel *model;
503 EmpathyImportWidgetPriv *priv = GET_PRIV (self);
505 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
506 gtk_tree_model_foreach (model, import_widget_tree_model_foreach, self);