Update Greek translation
[empathy-mirror.git] / src / empathy-import-widget.c
blob708370bae6ea4e08d1f019ee14635f6b5bee0f48
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"
26 #include "empathy-import-widget.h"
28 #include <glib/gi18n-lib.h>
29 #include <tp-account-widgets/tpaw-builder.h>
30 #include <tp-account-widgets/tpaw-utils.h>
31 #include <telepathy-glib/telepathy-glib-dbus.h>
33 #include "empathy-ui-utils.h"
34 #include "empathy-utils.h"
36 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
37 #include "empathy-debug.h"
39 G_DEFINE_TYPE (EmpathyImportWidget, empathy_import_widget, G_TYPE_OBJECT)
41 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyImportWidget)
43 enum
45 COL_IMPORT = 0,
46 COL_PROTOCOL,
47 COL_NAME,
48 COL_SOURCE,
49 COL_ACCOUNT_DATA,
50 COL_COUNT
53 enum {
54 PROP_APPLICATION_ID = 1,
55 PROP_CMS
58 typedef struct {
59 GtkWidget *vbox;
60 GtkWidget *treeview;
61 GtkWidget *scrolledwindow;
63 GList *accounts;
64 EmpathyImportApplication app_id;
66 TpawConnectionManagers *cms;
68 gboolean dispose_run;
69 } EmpathyImportWidgetPriv;
71 static gboolean
72 import_widget_account_id_in_list (GList *accounts,
73 const gchar *account_id)
75 GList *l;
77 for (l = accounts; l; l = l->next)
79 TpAccount *account = l->data;
80 const GHashTable *parameters;
82 parameters = tp_account_get_parameters (account);
84 if (!tp_strdiff (tp_asv_get_string (parameters, "account"), account_id))
85 return TRUE;
88 return FALSE;
91 #define MAX_TREEVIEW_HEIGHT 300
93 static void
94 import_widget_add_accounts_to_model (EmpathyImportWidget *self)
96 TpAccountManager *manager;
97 GtkTreeModel *model;
98 GList *l;
99 EmpathyImportWidgetPriv *priv = GET_PRIV (self);
100 gint min, natural;
102 manager = tp_account_manager_dup ();
104 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
106 for (l = priv->accounts; l; l = l->next)
108 GValue *value;
109 EmpathyImportAccountData *data = l->data;
110 gboolean import;
111 GList *accounts;
112 TpConnectionManager *cm = NULL;
114 if (!empathy_import_protocol_is_supported (data->protocol, &cm))
115 continue;
117 data->connection_manager = g_strdup (
118 tp_connection_manager_get_name (cm));
120 value = g_hash_table_lookup (data->settings, "account");
122 accounts = tp_account_manager_dup_valid_accounts (manager);
124 /* Only set the "Import" cell to be active if there isn't already an
125 * account set up with the same account id. */
126 import = !import_widget_account_id_in_list (accounts,
127 g_value_get_string (value));
129 g_list_free_full (accounts, g_object_unref);
131 gtk_list_store_insert_with_values (GTK_LIST_STORE (model), NULL, -1,
132 COL_IMPORT, import,
133 COL_PROTOCOL, data->protocol,
134 COL_NAME, g_value_get_string (value),
135 COL_SOURCE, data->source,
136 COL_ACCOUNT_DATA, data,
137 -1);
141 /* Display as much rows as possible */
142 gtk_widget_get_preferred_height (priv->treeview, &min, &natural);
143 gtk_widget_set_size_request (priv->scrolledwindow, -1,
144 MIN (natural, MAX_TREEVIEW_HEIGHT));
146 g_object_unref (manager);
149 static void
150 import_widget_create_account_cb (GObject *source,
151 GAsyncResult *result,
152 gpointer user_data)
154 TpAccountManager *account_manager;
155 TpAccount *account;
156 GError *error = NULL;
157 EmpathyImportWidget *self = user_data;
159 account = tp_account_manager_create_account_finish (
160 TP_ACCOUNT_MANAGER (source), result, &error);
162 if (account == NULL)
164 DEBUG ("Failed to create account: %s",
165 error ? error->message : "No error given");
166 g_clear_error (&error);
167 return;
170 DEBUG ("account created\n");
172 if (tp_account_is_enabled (account))
174 account_manager = tp_account_manager_dup ();
175 tpaw_connect_new_account (account, account_manager);
176 g_object_unref (account_manager);
179 g_object_unref (self);
182 static void
183 import_widget_add_account (EmpathyImportWidget *self,
184 EmpathyImportAccountData *data)
186 TpAccountManager *account_manager;
187 gchar *display_name = NULL;
188 GHashTable *properties;
189 GValue *username;
191 account_manager = tp_account_manager_dup ();
193 DEBUG ("connection_manager: %s\n", data->connection_manager);
195 /* Set the display name of the account */
196 username = g_hash_table_lookup (data->settings, "account");
198 if (!tp_strdiff (data->protocol, "irc"))
200 const gchar *server;
202 server = tp_asv_get_string (data->settings, "server");
204 if (server != NULL)
205 display_name = g_strdup_printf ("%s on %s",
206 g_value_get_string (username), server);
209 if (display_name == NULL)
211 display_name = g_strdup_printf ("%s (%s)",
212 data->protocol, g_value_get_string (username));
215 DEBUG ("display name: %s\n", display_name);
217 properties = tp_asv_new (NULL, NULL);
218 tp_asv_set_boolean (properties, TP_IFACE_ACCOUNT ".Enabled", data->enabled);
220 tp_account_manager_create_account_async (account_manager,
221 (const gchar*) data->connection_manager, data->protocol, display_name,
222 data->settings, properties, import_widget_create_account_cb,
223 g_object_ref (self));
225 g_hash_table_unref (properties);
226 g_free (display_name);
227 g_object_unref (account_manager);
230 static gboolean
231 import_widget_tree_model_foreach (GtkTreeModel *model,
232 GtkTreePath *path,
233 GtkTreeIter *iter,
234 gpointer user_data)
236 gboolean to_import;
237 EmpathyImportAccountData *data;
238 EmpathyImportWidget *self = user_data;
240 gtk_tree_model_get (model, iter,
241 COL_IMPORT, &to_import,
242 COL_ACCOUNT_DATA, &data,
243 -1);
245 if (to_import)
246 import_widget_add_account (self, data);
248 return FALSE;
251 static void
252 import_widget_cell_toggled_cb (GtkCellRendererToggle *cell_renderer,
253 const gchar *path_str,
254 EmpathyImportWidget *self)
256 GtkTreeModel *model;
257 GtkTreeIter iter;
258 GtkTreePath *path;
259 EmpathyImportWidgetPriv *priv = GET_PRIV (self);
261 path = gtk_tree_path_new_from_string (path_str);
262 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
264 gtk_tree_model_get_iter (model, &iter, path);
266 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
267 COL_IMPORT, !gtk_cell_renderer_toggle_get_active (cell_renderer),
268 -1);
270 gtk_tree_path_free (path);
273 static void
274 import_widget_set_up_account_list (EmpathyImportWidget *self)
276 EmpathyImportWidgetPriv *priv = GET_PRIV (self);
277 GtkListStore *store;
278 GtkTreeView *view;
279 GtkTreeViewColumn *column;
280 GtkCellRenderer *cell;
282 priv->accounts = empathy_import_accounts_load (priv->app_id);
284 store = gtk_list_store_new (COL_COUNT, G_TYPE_BOOLEAN, G_TYPE_STRING,
285 G_TYPE_STRING, G_TYPE_STRING, G_TYPE_POINTER);
287 gtk_tree_view_set_model (GTK_TREE_VIEW (priv->treeview),
288 GTK_TREE_MODEL (store));
290 g_object_unref (store);
292 view = GTK_TREE_VIEW (priv->treeview);
293 gtk_tree_view_set_headers_visible (view, TRUE);
295 /* Import column */
296 cell = gtk_cell_renderer_toggle_new ();
297 gtk_tree_view_insert_column_with_attributes (view, -1,
298 /* Translators: this is the header of a treeview column */
299 _("Import"), cell,
300 "active", COL_IMPORT,
301 NULL);
303 g_signal_connect (cell, "toggled",
304 G_CALLBACK (import_widget_cell_toggled_cb), self);
306 /* Protocol column */
307 column = gtk_tree_view_column_new ();
308 gtk_tree_view_column_set_title (column, _("Protocol"));
309 gtk_tree_view_column_set_expand (column, TRUE);
310 gtk_tree_view_append_column (view, column);
312 cell = gtk_cell_renderer_text_new ();
313 g_object_set (cell, "editable", FALSE, NULL);
314 gtk_tree_view_column_pack_start (column, cell, TRUE);
315 gtk_tree_view_column_add_attribute (column, cell, "text", COL_PROTOCOL);
317 /* Account column */
318 column = gtk_tree_view_column_new ();
319 gtk_tree_view_column_set_title (column, _("Account"));
320 gtk_tree_view_column_set_expand (column, TRUE);
321 gtk_tree_view_append_column (view, column);
323 cell = gtk_cell_renderer_text_new ();
324 g_object_set (cell, "editable", FALSE, NULL);
325 gtk_tree_view_column_pack_start (column, cell, TRUE);
326 gtk_tree_view_column_add_attribute (column, cell, "text", COL_NAME);
328 if (priv->app_id == EMPATHY_IMPORT_APPLICATION_ALL)
330 /* Source column */
331 column = gtk_tree_view_column_new ();
332 gtk_tree_view_column_set_title (column, _("Source"));
333 gtk_tree_view_column_set_expand (column, TRUE);
334 gtk_tree_view_append_column (view, column);
336 cell = gtk_cell_renderer_text_new ();
337 g_object_set (cell, "editable", FALSE, NULL);
338 gtk_tree_view_column_pack_start (column, cell, TRUE);
339 gtk_tree_view_column_add_attribute (column, cell, "text", COL_SOURCE);
342 import_widget_add_accounts_to_model (self);
345 static void
346 import_widget_destroy_cb (GtkWidget *w,
347 EmpathyImportWidget *self)
349 g_object_unref (self);
352 static void
353 do_get_property (GObject *object,
354 guint property_id,
355 GValue *value,
356 GParamSpec *pspec)
358 EmpathyImportWidgetPriv *priv = GET_PRIV (object);
360 switch (property_id)
362 case PROP_APPLICATION_ID:
363 g_value_set_int (value, priv->app_id);
364 break;
365 case PROP_CMS:
366 g_value_set_object (value, priv->cms);
367 break;
368 default:
369 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
373 static void
374 do_set_property (GObject *object,
375 guint property_id,
376 const GValue *value,
377 GParamSpec *pspec)
379 EmpathyImportWidgetPriv *priv = GET_PRIV (object);
381 switch (property_id)
383 case PROP_APPLICATION_ID:
384 priv->app_id = g_value_get_int (value);
385 break;
386 case PROP_CMS:
387 priv->cms = g_value_dup_object (value);
388 break;
389 default:
390 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
394 static void
395 do_finalize (GObject *obj)
397 EmpathyImportWidgetPriv *priv = GET_PRIV (obj);
399 g_list_foreach (priv->accounts, (GFunc) empathy_import_account_data_free,
400 NULL);
401 g_list_free (priv->accounts);
403 if (G_OBJECT_CLASS (empathy_import_widget_parent_class)->finalize != NULL)
404 G_OBJECT_CLASS (empathy_import_widget_parent_class)->finalize (obj);
407 static void
408 do_dispose (GObject *obj)
410 EmpathyImportWidgetPriv *priv = GET_PRIV (obj);
412 if (priv->dispose_run)
413 return;
415 priv->dispose_run = TRUE;
417 if (priv->cms != NULL)
419 g_object_unref (priv->cms);
420 priv->cms = NULL;
423 if (G_OBJECT_CLASS (empathy_import_widget_parent_class)->dispose != NULL)
424 G_OBJECT_CLASS (empathy_import_widget_parent_class)->dispose (obj);
427 static void
428 do_constructed (GObject *obj)
430 EmpathyImportWidget *self = EMPATHY_IMPORT_WIDGET (obj);
431 EmpathyImportWidgetPriv *priv = GET_PRIV (self);
432 GtkBuilder *gui;
433 gchar *filename;
435 filename = empathy_file_lookup ("empathy-import-dialog.ui", "src");
436 gui = tpaw_builder_get_file (filename,
437 "widget_vbox", &priv->vbox,
438 "treeview", &priv->treeview,
439 "scrolledwindow", &priv->scrolledwindow,
440 NULL);
442 g_free (filename);
443 tpaw_builder_unref_and_keep_widget (gui, priv->vbox);
445 g_signal_connect (priv->vbox, "destroy",
446 G_CALLBACK (import_widget_destroy_cb), self);
448 import_widget_set_up_account_list (self);
451 static void
452 empathy_import_widget_class_init (EmpathyImportWidgetClass *klass)
454 GObjectClass *oclass = G_OBJECT_CLASS (klass);
455 GParamSpec *param_spec;
457 oclass->constructed = do_constructed;
458 oclass->finalize = do_finalize;
459 oclass->dispose = do_dispose;
460 oclass->set_property = do_set_property;
461 oclass->get_property = do_get_property;
463 param_spec = g_param_spec_int ("application-id",
464 "application-id", "The application id to import from",
465 0, EMPATHY_IMPORT_APPLICATION_INVALID, EMPATHY_IMPORT_APPLICATION_ALL,
466 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
467 g_object_class_install_property (oclass, PROP_APPLICATION_ID, param_spec);
469 param_spec = g_param_spec_object ("cms",
470 "TpawConnectionManagers", "TpawConnectionManagers",
471 TPAW_TYPE_CONNECTION_MANAGERS,
472 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
473 g_object_class_install_property (oclass, PROP_CMS, param_spec);
475 g_type_class_add_private (klass, sizeof (EmpathyImportWidgetPriv));
478 static void
479 empathy_import_widget_init (EmpathyImportWidget *self)
481 EmpathyImportWidgetPriv *priv =
482 G_TYPE_INSTANCE_GET_PRIVATE (self, EMPATHY_TYPE_IMPORT_WIDGET,
483 EmpathyImportWidgetPriv);
485 self->priv = priv;
488 EmpathyImportWidget *
489 empathy_import_widget_new (EmpathyImportApplication id,
490 TpawConnectionManagers *cms)
492 g_return_val_if_fail (TPAW_IS_CONNECTION_MANAGERS (cms), NULL);
494 return g_object_new (EMPATHY_TYPE_IMPORT_WIDGET,
495 "application-id", id,
496 "cms", cms,
497 NULL);
500 GtkWidget *
501 empathy_import_widget_get_widget (EmpathyImportWidget *self)
503 EmpathyImportWidgetPriv *priv = GET_PRIV (self);
505 return priv->vbox;
508 void
509 empathy_import_widget_add_selected_accounts (EmpathyImportWidget *self)
511 GtkTreeModel *model;
512 EmpathyImportWidgetPriv *priv = GET_PRIV (self);
514 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
515 gtk_tree_model_foreach (model, import_widget_tree_model_foreach, self);