2 * Copyright (C) 2005-2007 Imendio AB
3 * Copyright (C) 2007-2009 Collabora Ltd.
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.
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.
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., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301 USA
20 * Authors: Martyn Russell <martyn@imendio.com>
21 * Xavier Claessens <xclaesse@gmail.com>
22 * Cosimo Cecchi <cosimo.cecchi@collabora.co.uk>
23 * Jonathan Tellier <jonathan.tellier@gmail.com>
32 #include <glib/gi18n.h>
33 #include <dbus/dbus-glib.h>
35 #include <telepathy-glib/util.h>
37 #include <libempathy/empathy-utils.h>
38 #include <libempathy/empathy-account-manager.h>
39 #include <libempathy/empathy-connection-managers.h>
40 #include <libempathy-gtk/empathy-ui-utils.h>
42 #include <libempathy-gtk/empathy-protocol-chooser.h>
43 #include <libempathy-gtk/empathy-account-widget.h>
44 #include <libempathy-gtk/empathy-account-widget-irc.h>
45 #include <libempathy-gtk/empathy-account-widget-sip.h>
46 #include <libempathy-gtk/empathy-cell-renderer-activatable.h>
47 #include <libempathy-gtk/empathy-conf.h>
48 #include <libempathy-gtk/empathy-images.h>
50 #include "empathy-accounts-dialog.h"
51 #include "empathy-import-dialog.h"
52 #include "empathy-import-utils.h"
54 #define DEBUG_FLAG EMPATHY_DEBUG_ACCOUNT
55 #include <libempathy/empathy-debug.h>
57 /* Flashing delay for icons (milliseconds). */
58 #define FLASH_TIMEOUT 500
60 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyAccountsDialog)
61 G_DEFINE_TYPE (EmpathyAccountsDialog, empathy_accounts_dialog, G_TYPE_OBJECT);
63 static EmpathyAccountsDialog *dialog_singleton = NULL;
68 GtkWidget *alignment_settings;
70 GtkWidget *vbox_details;
71 GtkWidget *frame_no_protocol;
75 GtkWidget *button_add;
77 GtkWidget *frame_new_account;
78 GtkWidget *combobox_protocol;
80 GtkWidget *button_create;
81 GtkWidget *button_back;
82 GtkWidget *radiobutton_reuse;
83 GtkWidget *radiobutton_register;
85 GtkWidget *image_type;
86 GtkWidget *label_name;
87 GtkWidget *label_type;
88 GtkWidget *settings_widget;
90 gboolean connecting_show;
93 gulong settings_ready_id;
94 EmpathyAccountSettings *settings_ready;
96 EmpathyAccountManager *account_manager;
97 EmpathyConnectionManagers *cms;
99 GtkWindow *parent_window;
100 EmpathyAccount *initial_selection;
101 } EmpathyAccountsDialogPriv;
107 COL_ACCOUNT_SETTINGS_POINTER,
115 static EmpathyAccountSettings * accounts_dialog_model_get_selected_settings (
116 EmpathyAccountsDialog *dialog);
118 static void accounts_dialog_model_select_first (EmpathyAccountsDialog *dialog);
120 static void accounts_dialog_update (EmpathyAccountsDialog *dialog,
121 EmpathyAccountSettings *settings);
123 static void accounts_dialog_update_settings (EmpathyAccountsDialog *dialog,
124 EmpathyAccountSettings *settings);
127 accounts_dialog_update_name_label (EmpathyAccountsDialog *dialog,
128 const gchar *display_name)
131 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
133 text = g_markup_printf_escaped ("<big><b>%s</b></big>", display_name);
134 gtk_label_set_markup (GTK_LABEL (priv->label_name), text);
140 empathy_account_dialog_widget_cancelled_cb (EmpathyAccountWidget *widget_object,
141 EmpathyAccountsDialog *dialog)
145 GtkTreeSelection *selection;
147 EmpathyAccountSettings *settings;
148 EmpathyAccount *account;
149 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
151 view = GTK_TREE_VIEW (priv->treeview);
152 selection = gtk_tree_view_get_selection (view);
154 if (!gtk_tree_selection_get_selected (selection, &model, &iter))
157 gtk_tree_model_get (model, &iter,
158 COL_ACCOUNT_SETTINGS_POINTER, &settings,
159 COL_ACCOUNT_POINTER, &account, -1);
161 empathy_account_settings_discard_changes (settings);
165 /* We were creating an account. We remove the selected row */
166 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
170 /* We were modifying an account. We discard the changes by reloading the
171 * settings and the UI. */
172 accounts_dialog_update_settings (dialog, settings);
173 g_object_unref (account);
176 if (settings != NULL)
177 g_object_unref (settings);
181 empathy_account_dialog_account_created_cb (EmpathyAccountWidget *widget_object,
182 EmpathyAccountsDialog *dialog)
184 EmpathyAccountSettings *settings =
185 accounts_dialog_model_get_selected_settings (dialog);
186 accounts_dialog_update_settings (dialog, settings);
189 g_object_unref (settings);
193 get_account_setup_widget (EmpathyAccountSettings *settings,
194 EmpathyAccountWidget **widget_object)
196 const gchar *proto = empathy_account_settings_get_protocol (settings);
197 EmpathyConnectionManagers *cm =
198 empathy_connection_managers_dup_singleton ();
199 GList *cms = empathy_connection_managers_get_cms (cm);
202 for (l = cms; l; l = l->next)
204 TpConnectionManager *tp_cm = l->data;
205 if (tp_connection_manager_has_protocol (tp_cm, proto))
208 *widget_object = empathy_account_widget_new_for_protocol (proto,
210 return empathy_account_widget_get_widget (*widget_object);
215 *widget_object = empathy_account_widget_new_for_protocol ("generic", settings,
217 return empathy_account_widget_get_widget (*widget_object);
221 account_dialog_create_settings_widget (EmpathyAccountsDialog *dialog,
222 EmpathyAccountSettings *settings)
224 EmpathyAccountWidget *widget_object = NULL;
225 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
228 priv->settings_widget = get_account_setup_widget (settings, &widget_object);
229 g_signal_connect (widget_object, "account-created",
230 G_CALLBACK (empathy_account_dialog_account_created_cb), dialog);
231 g_signal_connect (widget_object, "cancelled",
232 G_CALLBACK (empathy_account_dialog_widget_cancelled_cb), dialog);
234 gtk_container_add (GTK_CONTAINER (priv->alignment_settings),
235 priv->settings_widget);
236 gtk_widget_show (priv->settings_widget);
238 icon_name = empathy_account_settings_get_icon_name (settings);
240 gtk_image_set_from_icon_name (GTK_IMAGE (priv->image_type),
241 icon_name, GTK_ICON_SIZE_DIALOG);
242 gtk_widget_set_tooltip_text (priv->image_type,
243 empathy_account_settings_get_protocol (settings));
245 accounts_dialog_update_name_label (dialog,
246 empathy_account_settings_get_display_name (settings));
250 account_dialog_settings_ready_cb (EmpathyAccountSettings *settings,
252 EmpathyAccountsDialog *dialog)
254 if (empathy_account_settings_is_ready (settings))
255 account_dialog_create_settings_widget (dialog, settings);
259 accounts_dialog_model_select_first (EmpathyAccountsDialog *dialog)
263 GtkTreeSelection *selection;
265 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
268 view = GTK_TREE_VIEW (priv->treeview);
269 model = gtk_tree_view_get_model (view);
271 if (gtk_tree_model_get_iter_first (model, &iter))
273 selection = gtk_tree_view_get_selection (view);
274 gtk_tree_selection_select_iter (selection, &iter);
278 accounts_dialog_update_settings (dialog, NULL);
283 accounts_dialog_protocol_changed_cb (GtkWidget *widget,
284 EmpathyAccountsDialog *dialog)
286 TpConnectionManager *cm;
287 TpConnectionManagerProtocol *proto;
288 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
290 cm = empathy_protocol_chooser_dup_selected (
291 EMPATHY_PROTOCOL_CHOOSER (priv->combobox_protocol), &proto);
293 if (tp_connection_manager_protocol_can_register (proto))
295 gtk_widget_show (priv->radiobutton_register);
296 gtk_widget_show (priv->radiobutton_reuse);
300 gtk_widget_hide (priv->radiobutton_register);
301 gtk_widget_hide (priv->radiobutton_reuse);
307 accounts_dialog_button_add_clicked_cb (GtkWidget *button,
308 EmpathyAccountsDialog *dialog)
312 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
314 view = GTK_TREE_VIEW (priv->treeview);
315 model = gtk_tree_view_get_model (view);
317 gtk_widget_set_sensitive (priv->button_add, FALSE);
318 gtk_widget_hide (priv->vbox_details);
319 gtk_widget_hide (priv->frame_no_protocol);
320 gtk_widget_show (priv->frame_new_account);
322 /* If we have no account, no need of a back button */
323 if (gtk_tree_model_iter_n_children (model, NULL) > 0)
324 gtk_widget_show (priv->button_back);
326 gtk_widget_hide (priv->button_back);
328 accounts_dialog_protocol_changed_cb (priv->radiobutton_register, dialog);
329 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->radiobutton_reuse),
331 gtk_combo_box_set_active (GTK_COMBO_BOX (priv->combobox_protocol), 0);
332 gtk_widget_grab_focus (priv->combobox_protocol);
336 accounts_dialog_update_settings (EmpathyAccountsDialog *dialog,
337 EmpathyAccountSettings *settings)
339 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
341 if (priv->settings_ready != NULL)
343 g_signal_handler_disconnect (priv->settings_ready,
344 priv->settings_ready_id);
345 priv->settings_ready = NULL;
346 priv->settings_ready_id = 0;
354 view = GTK_TREE_VIEW (priv->treeview);
355 model = gtk_tree_view_get_model (view);
357 if (gtk_tree_model_iter_n_children (model, NULL) > 0)
359 /* We have configured accounts, select the first one */
360 accounts_dialog_model_select_first (dialog);
363 if (empathy_connection_managers_get_cms_num (priv->cms) > 0)
365 /* We have no account configured but we have some
366 * profiles installed. The user obviously wants to add
367 * an account. Click on the Add button for him. */
368 accounts_dialog_button_add_clicked_cb (priv->button_add,
373 /* No account and no profile, warn the user */
374 gtk_widget_hide (priv->vbox_details);
375 gtk_widget_hide (priv->frame_new_account);
376 gtk_widget_show (priv->frame_no_protocol);
377 gtk_widget_set_sensitive (priv->button_add, FALSE);
381 /* We have an account selected, destroy old settings and create a new
382 * one for the account selected */
383 gtk_widget_hide (priv->frame_new_account);
384 gtk_widget_hide (priv->frame_no_protocol);
385 gtk_widget_show (priv->vbox_details);
386 gtk_widget_set_sensitive (priv->button_add, TRUE);
388 if (priv->settings_widget)
390 gtk_widget_destroy (priv->settings_widget);
391 priv->settings_widget = NULL;
394 if (empathy_account_settings_is_ready (settings))
396 account_dialog_create_settings_widget (dialog, settings);
400 priv->settings_ready = settings;
401 priv->settings_ready_id =
402 g_signal_connect (settings, "notify::ready",
403 G_CALLBACK (account_dialog_settings_ready_cb), dialog);
409 accounts_dialog_name_editing_started_cb (GtkCellRenderer *renderer,
410 GtkCellEditable *editable,
412 EmpathyAccountsDialog *dialog)
414 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
416 if (priv->connecting_id)
417 g_source_remove (priv->connecting_id);
419 DEBUG ("Editing account name started; stopping flashing");
423 accounts_dialog_model_pixbuf_data_func (GtkTreeViewColumn *tree_column,
424 GtkCellRenderer *cell,
427 EmpathyAccountsDialog *dialog)
429 EmpathyAccountSettings *settings;
432 TpConnectionStatus status;
433 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
435 gtk_tree_model_get (model, iter,
437 COL_ACCOUNT_SETTINGS_POINTER, &settings,
440 icon_name = empathy_account_settings_get_icon_name (settings);
441 pixbuf = empathy_pixbuf_from_icon_name (icon_name, GTK_ICON_SIZE_BUTTON);
445 if (status == TP_CONNECTION_STATUS_DISCONNECTED ||
446 (status == TP_CONNECTION_STATUS_CONNECTING &&
447 !priv->connecting_show))
449 GdkPixbuf *modded_pixbuf;
451 modded_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
454 gdk_pixbuf_get_width (pixbuf),
455 gdk_pixbuf_get_height (pixbuf));
457 gdk_pixbuf_saturate_and_pixelate (pixbuf,
461 g_object_unref (pixbuf);
462 pixbuf = modded_pixbuf;
471 g_object_unref (settings);
474 g_object_unref (pixbuf);
478 accounts_dialog_row_changed_foreach (GtkTreeModel *model,
483 gtk_tree_model_row_changed (model, path, iter);
489 accounts_dialog_flash_connecting_cb (EmpathyAccountsDialog *dialog)
493 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
495 priv->connecting_show = !priv->connecting_show;
497 view = GTK_TREE_VIEW (priv->treeview);
498 model = gtk_tree_view_get_model (view);
500 gtk_tree_model_foreach (model, accounts_dialog_row_changed_foreach, NULL);
506 accounts_dialog_name_edited_cb (GtkCellRendererText *renderer,
509 EmpathyAccountsDialog *dialog)
511 EmpathyAccountSettings *settings;
513 GtkTreePath *treepath;
515 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
517 if (empathy_account_manager_get_connecting_accounts
518 (priv->account_manager) > 0)
520 priv->connecting_id = g_timeout_add (FLASH_TIMEOUT,
521 (GSourceFunc) accounts_dialog_flash_connecting_cb,
525 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
526 treepath = gtk_tree_path_new_from_string (path);
527 gtk_tree_model_get_iter (model, &iter, treepath);
528 gtk_tree_model_get (model, &iter,
529 COL_ACCOUNT_SETTINGS_POINTER, &settings,
531 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
534 gtk_tree_path_free (treepath);
536 empathy_account_settings_set_display_name_async (settings, new_text,
538 g_object_set (settings, "display-name-overridden", TRUE, NULL);
539 g_object_unref (settings);
543 accounts_dialog_delete_account_response_cb (GtkDialog *message_dialog,
547 EmpathyAccount *account;
550 GtkTreeSelection *selection;
551 EmpathyAccountsDialog *account_dialog = EMPATHY_ACCOUNTS_DIALOG (user_data);
552 EmpathyAccountsDialogPriv *priv = GET_PRIV (account_dialog);
554 if (response_id == GTK_RESPONSE_YES)
556 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
558 if (!gtk_tree_selection_get_selected (selection, &model, &iter))
561 gtk_tree_model_get (model, &iter, COL_ACCOUNT_POINTER, &account, -1);
565 empathy_account_remove_async (account, NULL, NULL);
566 g_object_unref (account);
569 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
570 accounts_dialog_model_select_first (account_dialog);
573 gtk_widget_destroy (GTK_WIDGET (message_dialog));
577 accounts_dialog_view_delete_activated_cb (EmpathyCellRendererActivatable *cell,
578 const gchar *path_string,
579 EmpathyAccountsDialog *dialog)
581 EmpathyAccount *account;
584 GtkWidget *message_dialog;
585 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
587 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
589 if (!gtk_tree_model_get_iter_from_string (model, &iter, path_string))
592 gtk_tree_model_get (model, &iter, COL_ACCOUNT_POINTER, &account, -1);
594 if (account == NULL || !empathy_account_is_valid (account))
596 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
597 accounts_dialog_model_select_first (dialog);
601 message_dialog = gtk_message_dialog_new
602 (GTK_WINDOW (priv->window),
603 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
604 GTK_MESSAGE_QUESTION,
606 _("You are about to remove your %s account!\n"
607 "Are you sure you want to proceed?"),
608 empathy_account_get_display_name (account));
610 gtk_message_dialog_format_secondary_text
611 (GTK_MESSAGE_DIALOG (message_dialog),
612 _("Any associated conversations and chat rooms will NOT be "
613 "removed if you decide to proceed.\n"
615 "Should you decide to add the account back at a later time, "
616 "they will still be available."));
618 gtk_dialog_add_button (GTK_DIALOG (message_dialog),
621 gtk_dialog_add_button (GTK_DIALOG (message_dialog),
625 g_signal_connect (message_dialog, "response",
626 G_CALLBACK (accounts_dialog_delete_account_response_cb), dialog);
628 gtk_widget_show (message_dialog);
631 g_object_unref (account);
635 accounts_dialog_model_add_columns (EmpathyAccountsDialog *dialog)
638 GtkTreeViewColumn *column;
639 GtkCellRenderer *cell;
640 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
642 view = GTK_TREE_VIEW (priv->treeview);
643 gtk_tree_view_set_headers_visible (view, FALSE);
646 column = gtk_tree_view_column_new ();
647 gtk_tree_view_column_set_expand (column, TRUE);
648 gtk_tree_view_append_column (view, column);
651 cell = gtk_cell_renderer_pixbuf_new ();
652 gtk_tree_view_column_pack_start (column, cell, FALSE);
653 gtk_tree_view_column_set_cell_data_func (column, cell,
654 (GtkTreeCellDataFunc)
655 accounts_dialog_model_pixbuf_data_func,
660 cell = gtk_cell_renderer_text_new ();
662 "ellipsize", PANGO_ELLIPSIZE_END,
666 gtk_tree_view_column_pack_start (column, cell, TRUE);
667 gtk_tree_view_column_add_attribute (column, cell, "text", COL_NAME);
668 g_signal_connect (cell, "edited",
669 G_CALLBACK (accounts_dialog_name_edited_cb),
671 g_signal_connect (cell, "editing-started",
672 G_CALLBACK (accounts_dialog_name_editing_started_cb),
676 cell = empathy_cell_renderer_activatable_new ();
677 gtk_tree_view_column_pack_start (column, cell, FALSE);
679 "icon-name", GTK_STOCK_DELETE,
682 g_signal_connect (cell, "path-activated",
683 G_CALLBACK (accounts_dialog_view_delete_activated_cb),
687 static EmpathyAccountSettings *
688 accounts_dialog_model_get_selected_settings (EmpathyAccountsDialog *dialog)
692 GtkTreeSelection *selection;
694 EmpathyAccountSettings *settings;
695 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
697 view = GTK_TREE_VIEW (priv->treeview);
698 selection = gtk_tree_view_get_selection (view);
700 if (!gtk_tree_selection_get_selected (selection, &model, &iter))
703 gtk_tree_model_get (model, &iter,
704 COL_ACCOUNT_SETTINGS_POINTER, &settings, -1);
710 accounts_dialog_model_selection_changed (GtkTreeSelection *selection,
711 EmpathyAccountsDialog *dialog)
713 EmpathyAccountSettings *settings;
716 gboolean is_selection;
718 is_selection = gtk_tree_selection_get_selected (selection, &model, &iter);
720 settings = accounts_dialog_model_get_selected_settings (dialog);
722 if (settings != NULL)
723 empathy_account_settings_discard_changes (settings);
725 accounts_dialog_update_settings (dialog, settings);
727 if (settings != NULL)
728 g_object_unref (settings);
732 accounts_dialog_model_setup (EmpathyAccountsDialog *dialog)
735 GtkTreeSelection *selection;
736 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
738 store = gtk_list_store_new (COL_COUNT,
739 G_TYPE_STRING, /* name */
740 G_TYPE_UINT, /* status */
741 EMPATHY_TYPE_ACCOUNT, /* account */
742 EMPATHY_TYPE_ACCOUNT_SETTINGS); /* settings */
744 gtk_tree_view_set_model (GTK_TREE_VIEW (priv->treeview),
745 GTK_TREE_MODEL (store));
747 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
748 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
750 g_signal_connect (selection, "changed",
751 G_CALLBACK (accounts_dialog_model_selection_changed),
754 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
755 COL_NAME, GTK_SORT_ASCENDING);
757 accounts_dialog_model_add_columns (dialog);
759 g_object_unref (store);
763 accounts_dialog_get_settings_iter (EmpathyAccountsDialog *dialog,
764 EmpathyAccountSettings *settings,
768 GtkTreeSelection *selection;
771 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
773 /* Update the status in the model */
774 view = GTK_TREE_VIEW (priv->treeview);
775 selection = gtk_tree_view_get_selection (view);
776 model = gtk_tree_view_get_model (view);
778 for (ok = gtk_tree_model_get_iter_first (model, iter);
780 ok = gtk_tree_model_iter_next (model, iter))
782 EmpathyAccountSettings *this_settings;
785 gtk_tree_model_get (model, iter,
786 COL_ACCOUNT_SETTINGS_POINTER, &this_settings,
789 equal = (this_settings == settings);
790 g_object_unref (this_settings);
800 accounts_dialog_get_account_iter (EmpathyAccountsDialog *dialog,
801 EmpathyAccount *account,
805 GtkTreeSelection *selection;
808 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
810 /* Update the status in the model */
811 view = GTK_TREE_VIEW (priv->treeview);
812 selection = gtk_tree_view_get_selection (view);
813 model = gtk_tree_view_get_model (view);
815 for (ok = gtk_tree_model_get_iter_first (model, iter);
817 ok = gtk_tree_model_iter_next (model, iter))
819 EmpathyAccountSettings *settings;
822 gtk_tree_model_get (model, iter,
823 COL_ACCOUNT_SETTINGS_POINTER, &settings,
826 equal = empathy_account_settings_has_account (settings, account);
827 g_object_unref (settings);
837 accounts_dialog_model_set_selected (EmpathyAccountsDialog *dialog,
838 EmpathyAccountSettings *settings)
840 GtkTreeSelection *selection;
842 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
844 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
845 if (accounts_dialog_get_settings_iter (dialog, settings, &iter))
846 gtk_tree_selection_select_iter (selection, &iter);
849 accounts_dialog_add (EmpathyAccountsDialog *dialog,
850 EmpathyAccountSettings *settings)
855 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
857 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
858 name = empathy_account_settings_get_display_name (settings);
860 gtk_list_store_append (GTK_LIST_STORE (model), &iter);
862 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
864 COL_STATUS, TP_CONNECTION_STATUS_DISCONNECTED,
865 COL_ACCOUNT_SETTINGS_POINTER, settings,
870 accounts_dialog_connection_changed_cb (EmpathyAccountManager *manager,
871 EmpathyAccount *account,
872 TpConnectionStatusReason reason,
873 TpConnectionStatus current,
874 TpConnectionStatus previous,
875 EmpathyAccountsDialog *dialog)
880 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
882 /* Update the status in the model */
883 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
885 if (accounts_dialog_get_account_iter (dialog, account, &iter))
889 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
893 path = gtk_tree_model_get_path (model, &iter);
894 gtk_tree_model_row_changed (model, path, &iter);
895 gtk_tree_path_free (path);
898 found = (empathy_account_manager_get_connecting_accounts (manager) > 0);
900 if (!found && priv->connecting_id)
902 g_source_remove (priv->connecting_id);
903 priv->connecting_id = 0;
906 if (found && !priv->connecting_id)
907 priv->connecting_id = g_timeout_add (FLASH_TIMEOUT,
908 (GSourceFunc) accounts_dialog_flash_connecting_cb,
913 accounts_dialog_add_account (EmpathyAccountsDialog *dialog,
914 EmpathyAccount *account)
916 EmpathyAccountSettings *settings;
919 TpConnectionStatus status;
922 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
924 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
925 g_object_get (account, "connection-status", &status, NULL);
926 name = empathy_account_get_display_name (account);
927 enabled = empathy_account_is_enabled (account);
929 if (!accounts_dialog_get_account_iter (dialog, account, &iter))
930 gtk_list_store_append (GTK_LIST_STORE (model), &iter);
932 settings = empathy_account_settings_new_for_account (account);
934 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
937 COL_ACCOUNT_POINTER, account,
938 COL_ACCOUNT_SETTINGS_POINTER, settings,
941 accounts_dialog_connection_changed_cb (priv->account_manager,
943 TP_CONNECTION_STATUS_REASON_NONE_SPECIFIED,
945 TP_CONNECTION_STATUS_DISCONNECTED,
948 g_object_unref (settings);
952 accounts_dialog_update (EmpathyAccountsDialog *dialog,
953 EmpathyAccountSettings *settings)
957 TpConnectionStatus status = TP_CONNECTION_STATUS_DISCONNECTED;
959 gboolean enabled = FALSE;
960 EmpathyAccount *account;
961 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
963 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
964 name = empathy_account_settings_get_display_name (settings);
966 account = empathy_account_settings_get_account (settings);
969 enabled = empathy_account_is_enabled (account);
970 g_object_get (account, "connection-status", &status, NULL);
973 accounts_dialog_get_settings_iter (dialog, settings, &iter);
974 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
977 COL_ACCOUNT_POINTER, account,
978 COL_ACCOUNT_SETTINGS_POINTER, settings,
983 accounts_dialog_account_added_cb (EmpathyAccountManager *manager,
984 EmpathyAccount *account,
985 EmpathyAccountsDialog *dialog)
987 accounts_dialog_add_account (dialog, account);
992 accounts_dialog_account_removed_cb (EmpathyAccountManager *manager,
993 EmpathyAccount *account,
994 EmpathyAccountsDialog *dialog)
997 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
999 if (accounts_dialog_get_account_iter (dialog, account, &iter))
1000 gtk_list_store_remove (GTK_LIST_STORE (
1001 gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview))), &iter);
1005 enable_or_disable_account (EmpathyAccountsDialog *dialog,
1006 EmpathyAccount *account,
1009 GtkTreeModel *model;
1010 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1012 /* Update the status in the model */
1013 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1015 DEBUG ("Account %s is now %s",
1016 empathy_account_get_display_name (account),
1017 enabled ? "enabled" : "disabled");
1021 accounts_dialog_account_disabled_cb (EmpathyAccountManager *manager,
1022 EmpathyAccount *account,
1023 EmpathyAccountsDialog *dialog)
1025 enable_or_disable_account (dialog, account, FALSE);
1029 accounts_dialog_account_enabled_cb (EmpathyAccountManager *manager,
1030 EmpathyAccount *account,
1031 EmpathyAccountsDialog *dialog)
1033 enable_or_disable_account (dialog, account, TRUE);
1037 accounts_dialog_account_changed_cb (EmpathyAccountManager *manager,
1038 EmpathyAccount *account,
1039 EmpathyAccountsDialog *dialog)
1041 EmpathyAccountSettings *settings, *selected_settings;
1042 GtkTreeModel *model;
1044 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1046 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1048 if (!accounts_dialog_get_account_iter (dialog, account, &iter))
1051 gtk_tree_model_get (model, &iter,
1052 COL_ACCOUNT_SETTINGS_POINTER, &settings,
1055 accounts_dialog_update (dialog, settings);
1056 selected_settings = accounts_dialog_model_get_selected_settings (dialog);
1058 if (settings == selected_settings)
1059 accounts_dialog_update_name_label (dialog,
1060 empathy_account_settings_get_display_name (settings));
1063 g_object_unref (settings);
1065 if (selected_settings)
1066 g_object_unref (selected_settings);
1070 accounts_dialog_button_create_clicked_cb (GtkWidget *button,
1071 EmpathyAccountsDialog *dialog)
1073 EmpathyAccountSettings *settings;
1075 TpConnectionManager *cm;
1076 TpConnectionManagerProtocol *proto;
1077 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1079 cm = empathy_protocol_chooser_dup_selected (
1080 EMPATHY_PROTOCOL_CHOOSER (priv->combobox_protocol), &proto);
1082 /* Create account */
1083 /* To translator: %s is the protocol name */
1084 str = g_strdup_printf (_("New %s account"), proto->name);
1086 settings = empathy_account_settings_new (cm->name, proto->name, str);
1090 if (tp_connection_manager_protocol_can_register (proto))
1094 active = gtk_toggle_button_get_active
1095 (GTK_TOGGLE_BUTTON (priv->radiobutton_register));
1097 empathy_account_settings_set_boolean (settings, "register", TRUE);
1100 accounts_dialog_add (dialog, settings);
1101 accounts_dialog_model_set_selected (dialog, settings);
1103 g_object_unref (settings);
1104 g_object_unref (cm);
1108 accounts_dialog_button_back_clicked_cb (GtkWidget *button,
1109 EmpathyAccountsDialog *dialog)
1111 EmpathyAccountSettings *settings;
1113 settings = accounts_dialog_model_get_selected_settings (dialog);
1114 accounts_dialog_update_settings (dialog, settings);
1117 g_object_unref (settings);
1121 accounts_dialog_button_help_clicked_cb (GtkWidget *button,
1122 EmpathyAccountsDialog *dialog)
1124 empathy_url_show (button, "ghelp:empathy?empathy-create-account");
1128 accounts_dialog_response_cb (GtkWidget *widget,
1130 EmpathyAccountsDialog *dialog)
1132 if (response == GTK_RESPONSE_CLOSE)
1133 gtk_widget_destroy (widget);
1137 accounts_dialog_destroy_cb (GtkObject *obj,
1138 EmpathyAccountsDialog *dialog)
1142 g_object_unref (dialog);
1146 accounts_dialog_set_selected_account (EmpathyAccountsDialog *dialog,
1147 EmpathyAccount *account)
1149 GtkTreeSelection *selection;
1151 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1153 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
1154 if (accounts_dialog_get_account_iter (dialog, account, &iter))
1155 gtk_tree_selection_select_iter (selection, &iter);
1159 accounts_dialog_cms_ready_cb (EmpathyConnectionManagers *cms,
1161 EmpathyAccountsDialog *dialog)
1163 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1165 if (empathy_connection_managers_is_ready (cms))
1167 accounts_dialog_update_settings (dialog, NULL);
1169 if (priv->initial_selection != NULL)
1171 accounts_dialog_set_selected_account
1172 (dialog, priv->initial_selection);
1173 g_object_unref (priv->initial_selection);
1174 priv->initial_selection = NULL;
1180 accounts_dialog_build_ui (EmpathyAccountsDialog *dialog)
1184 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1186 filename = empathy_file_lookup ("empathy-accounts-dialog.ui", "src");
1188 gui = empathy_builder_get_file (filename,
1189 "accounts_dialog", &priv->window,
1190 "vbox_details", &priv->vbox_details,
1191 "frame_no_protocol", &priv->frame_no_protocol,
1192 "alignment_settings", &priv->alignment_settings,
1193 "treeview", &priv->treeview,
1194 "frame_new_account", &priv->frame_new_account,
1195 "hbox_type", &priv->hbox_type,
1196 "button_create", &priv->button_create,
1197 "button_back", &priv->button_back,
1198 "radiobutton_reuse", &priv->radiobutton_reuse,
1199 "radiobutton_register", &priv->radiobutton_register,
1200 "image_type", &priv->image_type,
1201 "label_name", &priv->label_name,
1202 "button_add", &priv->button_add,
1206 empathy_builder_connect (gui, dialog,
1207 "accounts_dialog", "response", accounts_dialog_response_cb,
1208 "accounts_dialog", "destroy", accounts_dialog_destroy_cb,
1209 "button_create", "clicked", accounts_dialog_button_create_clicked_cb,
1210 "button_back", "clicked", accounts_dialog_button_back_clicked_cb,
1211 "button_add", "clicked", accounts_dialog_button_add_clicked_cb,
1212 "button_help", "clicked", accounts_dialog_button_help_clicked_cb,
1215 g_object_unref (gui);
1217 priv->combobox_protocol = empathy_protocol_chooser_new ();
1218 gtk_box_pack_start (GTK_BOX (priv->hbox_type),
1219 priv->combobox_protocol,
1221 gtk_widget_show (priv->combobox_protocol);
1222 g_signal_connect (priv->combobox_protocol, "changed",
1223 G_CALLBACK (accounts_dialog_protocol_changed_cb),
1226 if (priv->parent_window)
1227 gtk_window_set_transient_for (GTK_WINDOW (priv->window),
1228 priv->parent_window);
1232 do_dispose (GObject *obj)
1234 EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (obj);
1235 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1237 /* Disconnect signals */
1238 g_signal_handlers_disconnect_by_func (priv->account_manager,
1239 accounts_dialog_account_added_cb,
1241 g_signal_handlers_disconnect_by_func (priv->account_manager,
1242 accounts_dialog_account_removed_cb,
1244 g_signal_handlers_disconnect_by_func (priv->account_manager,
1245 accounts_dialog_account_enabled_cb,
1247 g_signal_handlers_disconnect_by_func (priv->account_manager,
1248 accounts_dialog_account_disabled_cb,
1250 g_signal_handlers_disconnect_by_func (priv->account_manager,
1251 accounts_dialog_account_changed_cb,
1253 g_signal_handlers_disconnect_by_func (priv->account_manager,
1254 accounts_dialog_connection_changed_cb,
1257 if (priv->connecting_id)
1258 g_source_remove (priv->connecting_id);
1260 if (priv->account_manager != NULL)
1262 g_object_unref (priv->account_manager);
1263 priv->account_manager = NULL;
1266 if (priv->cms != NULL)
1268 g_object_unref (priv->cms);
1272 if (priv->initial_selection != NULL)
1273 g_object_unref (priv->initial_selection);
1274 priv->initial_selection = NULL;
1276 G_OBJECT_CLASS (empathy_accounts_dialog_parent_class)->dispose (obj);
1280 do_constructor (GType type,
1282 GObjectConstructParam *props)
1286 if (dialog_singleton)
1288 retval = G_OBJECT (dialog_singleton);
1289 g_object_ref (retval);
1294 G_OBJECT_CLASS (empathy_accounts_dialog_parent_class)->constructor
1295 (type, n_props, props);
1297 dialog_singleton = EMPATHY_ACCOUNTS_DIALOG (retval);
1298 g_object_add_weak_pointer (retval, (gpointer) &dialog_singleton);
1305 do_get_property (GObject *object,
1310 EmpathyAccountsDialogPriv *priv = GET_PRIV (object);
1312 switch (property_id)
1315 g_value_set_object (value, priv->parent_window);
1318 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1323 do_set_property (GObject *object,
1325 const GValue *value,
1328 EmpathyAccountsDialogPriv *priv = GET_PRIV (object);
1330 switch (property_id)
1333 priv->parent_window = g_value_get_object (value);
1336 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1341 do_constructed (GObject *object)
1343 EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (object);
1344 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1345 GList *accounts, *l;
1346 gboolean import_asked;
1348 accounts_dialog_build_ui (dialog);
1350 /* Set up signalling */
1351 priv->account_manager = empathy_account_manager_dup_singleton ();
1353 g_signal_connect (priv->account_manager, "account-created",
1354 G_CALLBACK (accounts_dialog_account_added_cb),
1356 g_signal_connect (priv->account_manager, "account-deleted",
1357 G_CALLBACK (accounts_dialog_account_removed_cb),
1359 g_signal_connect (priv->account_manager, "account-enabled",
1360 G_CALLBACK (accounts_dialog_account_enabled_cb),
1362 g_signal_connect (priv->account_manager, "account-disabled",
1363 G_CALLBACK (accounts_dialog_account_disabled_cb),
1365 g_signal_connect (priv->account_manager, "account-changed",
1366 G_CALLBACK (accounts_dialog_account_changed_cb),
1368 g_signal_connect (priv->account_manager, "account-connection-changed",
1369 G_CALLBACK (accounts_dialog_connection_changed_cb),
1372 accounts_dialog_model_setup (dialog);
1374 /* Add existing accounts */
1375 accounts = empathy_account_manager_dup_accounts (priv->account_manager);
1376 for (l = accounts; l; l = l->next)
1378 accounts_dialog_add_account (dialog, l->data);
1379 g_object_unref (l->data);
1381 g_list_free (accounts);
1383 priv->cms = empathy_connection_managers_dup_singleton ();
1384 if (!empathy_connection_managers_is_ready (priv->cms))
1385 g_signal_connect (priv->cms, "notify::ready",
1386 G_CALLBACK (accounts_dialog_cms_ready_cb), dialog);
1388 accounts_dialog_model_select_first (dialog);
1390 empathy_conf_get_bool (empathy_conf_get (),
1391 EMPATHY_PREFS_IMPORT_ASKED, &import_asked);
1394 if (empathy_import_accounts_to_import ())
1399 GtkWidget *import_dialog;
1401 empathy_conf_set_bool (empathy_conf_get (),
1402 EMPATHY_PREFS_IMPORT_ASKED, TRUE);
1403 import_dialog = empathy_import_dialog_new (GTK_WINDOW (priv->window),
1405 gtk_widget_show (import_dialog);
1411 empathy_accounts_dialog_class_init (EmpathyAccountsDialogClass *klass)
1413 GObjectClass *oclass = G_OBJECT_CLASS (klass);
1414 GParamSpec *param_spec;
1416 oclass->constructor = do_constructor;
1417 oclass->dispose = do_dispose;
1418 oclass->constructed = do_constructed;
1419 oclass->set_property = do_set_property;
1420 oclass->get_property = do_get_property;
1422 param_spec = g_param_spec_object ("parent",
1423 "parent", "The parent window",
1425 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
1426 g_object_class_install_property (oclass, PROP_PARENT, param_spec);
1428 g_type_class_add_private (klass, sizeof (EmpathyAccountsDialogPriv));
1432 empathy_accounts_dialog_init (EmpathyAccountsDialog *dialog)
1434 EmpathyAccountsDialogPriv *priv;
1436 priv = G_TYPE_INSTANCE_GET_PRIVATE ((dialog),
1437 EMPATHY_TYPE_ACCOUNTS_DIALOG,
1438 EmpathyAccountsDialogPriv);
1439 dialog->priv = priv;
1442 /* public methods */
1445 empathy_accounts_dialog_show (GtkWindow *parent,
1446 EmpathyAccount *selected_account)
1448 EmpathyAccountsDialog *dialog;
1449 EmpathyAccountsDialogPriv *priv;
1451 dialog = g_object_new (EMPATHY_TYPE_ACCOUNTS_DIALOG,
1452 "parent", parent, NULL);
1454 priv = GET_PRIV (dialog);
1456 if (selected_account)
1458 if (empathy_connection_managers_is_ready (priv->cms))
1459 accounts_dialog_set_selected_account (dialog, selected_account);
1461 /* save the selection to set it later when the cms
1464 priv->initial_selection = g_object_ref (selected_account);
1467 gtk_window_present (GTK_WINDOW (priv->window));
1469 return priv->window;