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 void accounts_dialog_account_display_name_changed_cb (
116 EmpathyAccount *account,
120 static EmpathyAccountSettings * accounts_dialog_model_get_selected_settings (
121 EmpathyAccountsDialog *dialog);
123 static void accounts_dialog_model_select_first (EmpathyAccountsDialog *dialog);
125 static void accounts_dialog_update (EmpathyAccountsDialog *dialog,
126 EmpathyAccountSettings *settings);
128 static void accounts_dialog_update_settings (EmpathyAccountsDialog *dialog,
129 EmpathyAccountSettings *settings);
132 accounts_dialog_update_name_label (EmpathyAccountsDialog *dialog,
133 const gchar *display_name)
136 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
138 text = g_markup_printf_escaped ("<big><b>%s</b></big>", display_name);
139 gtk_label_set_markup (GTK_LABEL (priv->label_name), text);
145 empathy_account_dialog_widget_cancelled_cb (EmpathyAccountWidget *widget_object,
146 EmpathyAccountsDialog *dialog)
150 GtkTreeSelection *selection;
152 EmpathyAccountSettings *settings;
153 EmpathyAccount *account;
154 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
156 view = GTK_TREE_VIEW (priv->treeview);
157 selection = gtk_tree_view_get_selection (view);
159 if (!gtk_tree_selection_get_selected (selection, &model, &iter))
162 gtk_tree_model_get (model, &iter,
163 COL_ACCOUNT_SETTINGS_POINTER, &settings,
164 COL_ACCOUNT_POINTER, &account, -1);
166 empathy_account_settings_discard_changes (settings);
170 /* We were creating an account. We remove the selected row */
171 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
175 /* We were modifying an account. We discard the changes by reloading the
176 * settings and the UI. */
177 accounts_dialog_update_settings (dialog, settings);
178 g_object_unref (account);
181 if (settings != NULL)
182 g_object_unref (settings);
186 get_default_display_name (EmpathyAccountSettings *settings)
188 const gchar *login_id;
189 const gchar *protocol;
190 gchar *default_display_name;
192 login_id = empathy_account_settings_get_string (settings, "account");
193 protocol = empathy_account_settings_get_protocol (settings);
195 if (login_id != NULL)
197 if (!tp_strdiff(protocol, "irc"))
200 server = empathy_account_settings_get_string (settings, "server");
202 /* To translators: The first parameter is the login id and the
203 * second one is the server. The resulting string will be something
204 * like: "MyUserName on chat.freenode.net".
205 * You should reverse the order of these arguments if the
206 * server should come before the login id in your locale.*/
207 default_display_name = g_strdup_printf (_("%1$s on %2$s"),
212 default_display_name = g_strdup (login_id);
215 else if (protocol != NULL)
217 /* To translators: The parameter is the protocol name. The resulting
218 * string will be something like: "Jabber Account" */
219 default_display_name = g_strdup_printf (_("%s Account"), protocol);
223 default_display_name = g_strdup (_("New account"));
226 return default_display_name;
230 empathy_account_dialog_account_created_cb (EmpathyAccountWidget *widget_object,
231 EmpathyAccountsDialog *dialog)
234 EmpathyAccountSettings *settings =
235 accounts_dialog_model_get_selected_settings (dialog);
237 display_name = get_default_display_name (settings);
239 empathy_account_settings_set_display_name_async (settings,
240 display_name, NULL, NULL);
242 g_free (display_name);
244 accounts_dialog_update_settings (dialog, settings);
247 g_object_unref (settings);
251 get_account_setup_widget (EmpathyAccountSettings *settings,
252 EmpathyAccountWidget **widget_object)
254 const gchar *proto = empathy_account_settings_get_protocol (settings);
255 EmpathyConnectionManagers *cm =
256 empathy_connection_managers_dup_singleton ();
257 GList *cms = empathy_connection_managers_get_cms (cm);
260 for (l = cms; l; l = l->next)
262 TpConnectionManager *tp_cm = l->data;
263 if (tp_connection_manager_has_protocol (tp_cm, proto))
266 *widget_object = empathy_account_widget_new_for_protocol (proto,
268 return empathy_account_widget_get_widget (*widget_object);
273 *widget_object = empathy_account_widget_new_for_protocol ("generic", settings,
275 return empathy_account_widget_get_widget (*widget_object);
279 account_dialog_create_settings_widget (EmpathyAccountsDialog *dialog,
280 EmpathyAccountSettings *settings)
282 EmpathyAccountWidget *widget_object = NULL;
283 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
286 priv->settings_widget = get_account_setup_widget (settings, &widget_object);
287 g_signal_connect (widget_object, "account-created",
288 G_CALLBACK (empathy_account_dialog_account_created_cb), dialog);
289 g_signal_connect (widget_object, "cancelled",
290 G_CALLBACK (empathy_account_dialog_widget_cancelled_cb), dialog);
292 gtk_container_add (GTK_CONTAINER (priv->alignment_settings),
293 priv->settings_widget);
294 gtk_widget_show (priv->settings_widget);
296 icon_name = empathy_account_settings_get_icon_name (settings);
298 gtk_image_set_from_icon_name (GTK_IMAGE (priv->image_type),
299 icon_name, GTK_ICON_SIZE_DIALOG);
300 gtk_widget_set_tooltip_text (priv->image_type,
301 empathy_protocol_name_to_display_name
302 (empathy_account_settings_get_protocol (settings)));
304 accounts_dialog_update_name_label (dialog,
305 empathy_account_settings_get_display_name (settings));
309 account_dialog_settings_ready_cb (EmpathyAccountSettings *settings,
311 EmpathyAccountsDialog *dialog)
313 if (empathy_account_settings_is_ready (settings))
314 account_dialog_create_settings_widget (dialog, settings);
318 accounts_dialog_model_select_first (EmpathyAccountsDialog *dialog)
322 GtkTreeSelection *selection;
324 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
327 view = GTK_TREE_VIEW (priv->treeview);
328 model = gtk_tree_view_get_model (view);
330 if (gtk_tree_model_get_iter_first (model, &iter))
332 selection = gtk_tree_view_get_selection (view);
333 gtk_tree_selection_select_iter (selection, &iter);
337 accounts_dialog_update_settings (dialog, NULL);
342 accounts_dialog_protocol_changed_cb (GtkWidget *widget,
343 EmpathyAccountsDialog *dialog)
345 TpConnectionManager *cm;
346 TpConnectionManagerProtocol *proto;
347 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
349 cm = empathy_protocol_chooser_dup_selected (
350 EMPATHY_PROTOCOL_CHOOSER (priv->combobox_protocol), &proto);
361 if (tp_connection_manager_protocol_can_register (proto))
363 gtk_widget_show (priv->radiobutton_register);
364 gtk_widget_show (priv->radiobutton_reuse);
368 gtk_widget_hide (priv->radiobutton_register);
369 gtk_widget_hide (priv->radiobutton_reuse);
375 accounts_dialog_button_add_clicked_cb (GtkWidget *button,
376 EmpathyAccountsDialog *dialog)
380 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
382 view = GTK_TREE_VIEW (priv->treeview);
383 model = gtk_tree_view_get_model (view);
385 gtk_widget_set_sensitive (priv->button_add, FALSE);
386 gtk_widget_hide (priv->vbox_details);
387 gtk_widget_hide (priv->frame_no_protocol);
388 gtk_widget_show (priv->frame_new_account);
390 /* If we have no account, no need of a back button */
391 if (gtk_tree_model_iter_n_children (model, NULL) > 0)
392 gtk_widget_show (priv->button_back);
394 gtk_widget_hide (priv->button_back);
396 accounts_dialog_protocol_changed_cb (priv->radiobutton_register, dialog);
397 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->radiobutton_reuse),
399 gtk_combo_box_set_active (GTK_COMBO_BOX (priv->combobox_protocol), 0);
400 gtk_widget_grab_focus (priv->combobox_protocol);
404 accounts_dialog_update_settings (EmpathyAccountsDialog *dialog,
405 EmpathyAccountSettings *settings)
407 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
409 if (priv->settings_ready != NULL)
411 g_signal_handler_disconnect (priv->settings_ready,
412 priv->settings_ready_id);
413 priv->settings_ready = NULL;
414 priv->settings_ready_id = 0;
422 view = GTK_TREE_VIEW (priv->treeview);
423 model = gtk_tree_view_get_model (view);
425 if (gtk_tree_model_iter_n_children (model, NULL) > 0)
427 /* We have configured accounts, select the first one */
428 accounts_dialog_model_select_first (dialog);
431 if (empathy_connection_managers_get_cms_num (priv->cms) > 0)
433 /* We have no account configured but we have some
434 * profiles installed. The user obviously wants to add
435 * an account. Click on the Add button for him. */
436 accounts_dialog_button_add_clicked_cb (priv->button_add,
441 /* No account and no profile, warn the user */
442 gtk_widget_hide (priv->vbox_details);
443 gtk_widget_hide (priv->frame_new_account);
444 gtk_widget_show (priv->frame_no_protocol);
445 gtk_widget_set_sensitive (priv->button_add, FALSE);
449 /* We have an account selected, destroy old settings and create a new
450 * one for the account selected */
451 gtk_widget_hide (priv->frame_new_account);
452 gtk_widget_hide (priv->frame_no_protocol);
453 gtk_widget_show (priv->vbox_details);
454 gtk_widget_set_sensitive (priv->button_add, TRUE);
456 if (priv->settings_widget)
458 gtk_widget_destroy (priv->settings_widget);
459 priv->settings_widget = NULL;
462 if (empathy_account_settings_is_ready (settings))
464 account_dialog_create_settings_widget (dialog, settings);
468 priv->settings_ready = settings;
469 priv->settings_ready_id =
470 g_signal_connect (settings, "notify::ready",
471 G_CALLBACK (account_dialog_settings_ready_cb), dialog);
477 accounts_dialog_name_editing_started_cb (GtkCellRenderer *renderer,
478 GtkCellEditable *editable,
480 EmpathyAccountsDialog *dialog)
482 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
484 if (priv->connecting_id)
485 g_source_remove (priv->connecting_id);
487 DEBUG ("Editing account name started; stopping flashing");
491 accounts_dialog_model_pixbuf_data_func (GtkTreeViewColumn *tree_column,
492 GtkCellRenderer *cell,
495 EmpathyAccountsDialog *dialog)
497 EmpathyAccountSettings *settings;
500 TpConnectionStatus status;
501 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
503 gtk_tree_model_get (model, iter,
505 COL_ACCOUNT_SETTINGS_POINTER, &settings,
508 icon_name = empathy_account_settings_get_icon_name (settings);
509 pixbuf = empathy_pixbuf_from_icon_name (icon_name, GTK_ICON_SIZE_BUTTON);
513 if (status == TP_CONNECTION_STATUS_DISCONNECTED ||
514 (status == TP_CONNECTION_STATUS_CONNECTING &&
515 !priv->connecting_show))
517 GdkPixbuf *modded_pixbuf;
519 modded_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
522 gdk_pixbuf_get_width (pixbuf),
523 gdk_pixbuf_get_height (pixbuf));
525 gdk_pixbuf_saturate_and_pixelate (pixbuf,
529 g_object_unref (pixbuf);
530 pixbuf = modded_pixbuf;
539 g_object_unref (settings);
542 g_object_unref (pixbuf);
546 accounts_dialog_row_changed_foreach (GtkTreeModel *model,
551 gtk_tree_model_row_changed (model, path, iter);
557 accounts_dialog_flash_connecting_cb (EmpathyAccountsDialog *dialog)
561 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
563 priv->connecting_show = !priv->connecting_show;
565 view = GTK_TREE_VIEW (priv->treeview);
566 model = gtk_tree_view_get_model (view);
568 gtk_tree_model_foreach (model, accounts_dialog_row_changed_foreach, NULL);
574 accounts_dialog_name_edited_cb (GtkCellRendererText *renderer,
577 EmpathyAccountsDialog *dialog)
579 EmpathyAccountSettings *settings;
581 GtkTreePath *treepath;
583 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
585 if (empathy_account_manager_get_connecting_accounts
586 (priv->account_manager) > 0)
588 priv->connecting_id = g_timeout_add (FLASH_TIMEOUT,
589 (GSourceFunc) accounts_dialog_flash_connecting_cb,
593 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
594 treepath = gtk_tree_path_new_from_string (path);
595 gtk_tree_model_get_iter (model, &iter, treepath);
596 gtk_tree_model_get (model, &iter,
597 COL_ACCOUNT_SETTINGS_POINTER, &settings,
599 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
602 gtk_tree_path_free (treepath);
604 empathy_account_settings_set_display_name_async (settings, new_text,
606 g_object_set (settings, "display-name-overridden", TRUE, NULL);
607 g_object_unref (settings);
611 accounts_dialog_delete_account_response_cb (GtkDialog *message_dialog,
615 EmpathyAccount *account;
618 GtkTreeSelection *selection;
619 EmpathyAccountsDialog *account_dialog = EMPATHY_ACCOUNTS_DIALOG (user_data);
620 EmpathyAccountsDialogPriv *priv = GET_PRIV (account_dialog);
622 if (response_id == GTK_RESPONSE_YES)
624 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
626 if (!gtk_tree_selection_get_selected (selection, &model, &iter))
629 gtk_tree_model_get (model, &iter, COL_ACCOUNT_POINTER, &account, -1);
633 g_signal_handlers_disconnect_by_func (account,
634 accounts_dialog_account_display_name_changed_cb, account_dialog);
635 empathy_account_remove_async (account, NULL, NULL);
636 g_object_unref (account);
639 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
640 accounts_dialog_model_select_first (account_dialog);
643 gtk_widget_destroy (GTK_WIDGET (message_dialog));
647 accounts_dialog_view_delete_activated_cb (EmpathyCellRendererActivatable *cell,
648 const gchar *path_string,
649 EmpathyAccountsDialog *dialog)
651 EmpathyAccount *account;
654 GtkWidget *message_dialog;
655 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
657 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
659 if (!gtk_tree_model_get_iter_from_string (model, &iter, path_string))
662 gtk_tree_model_get (model, &iter, COL_ACCOUNT_POINTER, &account, -1);
664 if (account == NULL || !empathy_account_is_valid (account))
666 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
667 accounts_dialog_model_select_first (dialog);
671 message_dialog = gtk_message_dialog_new
672 (GTK_WINDOW (priv->window),
673 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
674 GTK_MESSAGE_QUESTION,
676 _("You are about to remove your %s account!\n"
677 "Are you sure you want to proceed?"),
678 empathy_account_get_display_name (account));
680 gtk_message_dialog_format_secondary_text
681 (GTK_MESSAGE_DIALOG (message_dialog),
682 _("Any associated conversations and chat rooms will NOT be "
683 "removed if you decide to proceed.\n"
685 "Should you decide to add the account back at a later time, "
686 "they will still be available."));
688 gtk_dialog_add_button (GTK_DIALOG (message_dialog),
691 gtk_dialog_add_button (GTK_DIALOG (message_dialog),
695 g_signal_connect (message_dialog, "response",
696 G_CALLBACK (accounts_dialog_delete_account_response_cb), dialog);
698 gtk_widget_show (message_dialog);
701 g_object_unref (account);
705 accounts_dialog_model_add_columns (EmpathyAccountsDialog *dialog)
708 GtkTreeViewColumn *column;
709 GtkCellRenderer *cell;
710 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
712 view = GTK_TREE_VIEW (priv->treeview);
713 gtk_tree_view_set_headers_visible (view, FALSE);
716 column = gtk_tree_view_column_new ();
717 gtk_tree_view_column_set_expand (column, TRUE);
718 gtk_tree_view_append_column (view, column);
721 cell = gtk_cell_renderer_pixbuf_new ();
722 gtk_tree_view_column_pack_start (column, cell, FALSE);
723 gtk_tree_view_column_set_cell_data_func (column, cell,
724 (GtkTreeCellDataFunc)
725 accounts_dialog_model_pixbuf_data_func,
730 cell = gtk_cell_renderer_text_new ();
732 "ellipsize", PANGO_ELLIPSIZE_END,
736 gtk_tree_view_column_pack_start (column, cell, TRUE);
737 gtk_tree_view_column_add_attribute (column, cell, "text", COL_NAME);
738 g_signal_connect (cell, "edited",
739 G_CALLBACK (accounts_dialog_name_edited_cb),
741 g_signal_connect (cell, "editing-started",
742 G_CALLBACK (accounts_dialog_name_editing_started_cb),
746 cell = empathy_cell_renderer_activatable_new ();
747 gtk_tree_view_column_pack_start (column, cell, FALSE);
749 "icon-name", GTK_STOCK_DELETE,
752 g_signal_connect (cell, "path-activated",
753 G_CALLBACK (accounts_dialog_view_delete_activated_cb),
757 static EmpathyAccountSettings *
758 accounts_dialog_model_get_selected_settings (EmpathyAccountsDialog *dialog)
762 GtkTreeSelection *selection;
764 EmpathyAccountSettings *settings;
765 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
767 view = GTK_TREE_VIEW (priv->treeview);
768 selection = gtk_tree_view_get_selection (view);
770 if (!gtk_tree_selection_get_selected (selection, &model, &iter))
773 gtk_tree_model_get (model, &iter,
774 COL_ACCOUNT_SETTINGS_POINTER, &settings, -1);
780 accounts_dialog_model_selection_changed (GtkTreeSelection *selection,
781 EmpathyAccountsDialog *dialog)
783 EmpathyAccountSettings *settings;
786 gboolean is_selection;
788 is_selection = gtk_tree_selection_get_selected (selection, &model, &iter);
790 settings = accounts_dialog_model_get_selected_settings (dialog);
792 if (settings != NULL)
793 empathy_account_settings_discard_changes (settings);
795 accounts_dialog_update_settings (dialog, settings);
797 if (settings != NULL)
798 g_object_unref (settings);
802 accounts_dialog_model_setup (EmpathyAccountsDialog *dialog)
805 GtkTreeSelection *selection;
806 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
808 store = gtk_list_store_new (COL_COUNT,
809 G_TYPE_STRING, /* name */
810 G_TYPE_UINT, /* status */
811 EMPATHY_TYPE_ACCOUNT, /* account */
812 EMPATHY_TYPE_ACCOUNT_SETTINGS); /* settings */
814 gtk_tree_view_set_model (GTK_TREE_VIEW (priv->treeview),
815 GTK_TREE_MODEL (store));
817 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
818 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
820 g_signal_connect (selection, "changed",
821 G_CALLBACK (accounts_dialog_model_selection_changed),
824 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
825 COL_NAME, GTK_SORT_ASCENDING);
827 accounts_dialog_model_add_columns (dialog);
829 g_object_unref (store);
833 accounts_dialog_get_settings_iter (EmpathyAccountsDialog *dialog,
834 EmpathyAccountSettings *settings,
838 GtkTreeSelection *selection;
841 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
843 /* Update the status in the model */
844 view = GTK_TREE_VIEW (priv->treeview);
845 selection = gtk_tree_view_get_selection (view);
846 model = gtk_tree_view_get_model (view);
848 for (ok = gtk_tree_model_get_iter_first (model, iter);
850 ok = gtk_tree_model_iter_next (model, iter))
852 EmpathyAccountSettings *this_settings;
855 gtk_tree_model_get (model, iter,
856 COL_ACCOUNT_SETTINGS_POINTER, &this_settings,
859 equal = (this_settings == settings);
860 g_object_unref (this_settings);
870 accounts_dialog_get_account_iter (EmpathyAccountsDialog *dialog,
871 EmpathyAccount *account,
875 GtkTreeSelection *selection;
878 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
880 /* Update the status in the model */
881 view = GTK_TREE_VIEW (priv->treeview);
882 selection = gtk_tree_view_get_selection (view);
883 model = gtk_tree_view_get_model (view);
885 for (ok = gtk_tree_model_get_iter_first (model, iter);
887 ok = gtk_tree_model_iter_next (model, iter))
889 EmpathyAccountSettings *settings;
892 gtk_tree_model_get (model, iter,
893 COL_ACCOUNT_SETTINGS_POINTER, &settings,
896 equal = empathy_account_settings_has_account (settings, account);
897 g_object_unref (settings);
907 accounts_dialog_model_set_selected (EmpathyAccountsDialog *dialog,
908 EmpathyAccountSettings *settings)
910 GtkTreeSelection *selection;
912 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
914 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
915 if (accounts_dialog_get_settings_iter (dialog, settings, &iter))
916 gtk_tree_selection_select_iter (selection, &iter);
919 accounts_dialog_add (EmpathyAccountsDialog *dialog,
920 EmpathyAccountSettings *settings)
925 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
927 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
928 name = empathy_account_settings_get_display_name (settings);
930 gtk_list_store_append (GTK_LIST_STORE (model), &iter);
932 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
934 COL_STATUS, TP_CONNECTION_STATUS_DISCONNECTED,
935 COL_ACCOUNT_SETTINGS_POINTER, settings,
940 accounts_dialog_connection_changed_cb (EmpathyAccountManager *manager,
941 EmpathyAccount *account,
942 TpConnectionStatusReason reason,
943 TpConnectionStatus current,
944 TpConnectionStatus previous,
945 EmpathyAccountsDialog *dialog)
950 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
952 /* Update the status in the model */
953 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
955 if (accounts_dialog_get_account_iter (dialog, account, &iter))
959 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
963 path = gtk_tree_model_get_path (model, &iter);
964 gtk_tree_model_row_changed (model, path, &iter);
965 gtk_tree_path_free (path);
968 found = (empathy_account_manager_get_connecting_accounts (manager) > 0);
970 if (!found && priv->connecting_id)
972 g_source_remove (priv->connecting_id);
973 priv->connecting_id = 0;
976 if (found && !priv->connecting_id)
977 priv->connecting_id = g_timeout_add (FLASH_TIMEOUT,
978 (GSourceFunc) accounts_dialog_flash_connecting_cb,
983 accounts_dialog_account_display_name_changed_cb (EmpathyAccount *account,
987 const gchar *display_name;
990 EmpathyAccountSettings *settings;
991 EmpathyAccount *selected_account;
992 EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (user_data);
993 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
995 display_name = empathy_account_get_display_name (account);
996 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
997 settings = accounts_dialog_model_get_selected_settings (dialog);
998 selected_account = empathy_account_settings_get_account (settings);
1000 if (accounts_dialog_get_account_iter (dialog, account, &iter))
1002 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
1003 COL_NAME, display_name,
1007 if (selected_account == account)
1008 accounts_dialog_update_name_label (dialog, display_name);
1010 g_object_unref (settings);
1014 accounts_dialog_add_account (EmpathyAccountsDialog *dialog,
1015 EmpathyAccount *account)
1017 EmpathyAccountSettings *settings;
1018 GtkTreeModel *model;
1020 TpConnectionStatus status;
1023 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1025 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1026 g_object_get (account, "connection-status", &status, NULL);
1027 name = empathy_account_get_display_name (account);
1028 enabled = empathy_account_is_enabled (account);
1030 if (!accounts_dialog_get_account_iter (dialog, account, &iter))
1031 gtk_list_store_append (GTK_LIST_STORE (model), &iter);
1033 settings = empathy_account_settings_new_for_account (account);
1035 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
1038 COL_ACCOUNT_POINTER, account,
1039 COL_ACCOUNT_SETTINGS_POINTER, settings,
1042 accounts_dialog_connection_changed_cb (priv->account_manager,
1044 TP_CONNECTION_STATUS_REASON_NONE_SPECIFIED,
1046 TP_CONNECTION_STATUS_DISCONNECTED,
1049 g_signal_connect (account, "notify::display-name",
1050 G_CALLBACK (accounts_dialog_account_display_name_changed_cb), dialog);
1052 g_object_unref (settings);
1056 accounts_dialog_update (EmpathyAccountsDialog *dialog,
1057 EmpathyAccountSettings *settings)
1059 GtkTreeModel *model;
1061 TpConnectionStatus status = TP_CONNECTION_STATUS_DISCONNECTED;
1063 gboolean enabled = FALSE;
1064 EmpathyAccount *account;
1065 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1067 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1068 name = empathy_account_settings_get_display_name (settings);
1070 account = empathy_account_settings_get_account (settings);
1071 if (account != NULL)
1073 enabled = empathy_account_is_enabled (account);
1074 g_object_get (account, "connection-status", &status, NULL);
1077 accounts_dialog_get_settings_iter (dialog, settings, &iter);
1078 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
1081 COL_ACCOUNT_POINTER, account,
1082 COL_ACCOUNT_SETTINGS_POINTER, settings,
1087 accounts_dialog_account_added_cb (EmpathyAccountManager *manager,
1088 EmpathyAccount *account,
1089 EmpathyAccountsDialog *dialog)
1091 accounts_dialog_add_account (dialog, account);
1096 accounts_dialog_account_removed_cb (EmpathyAccountManager *manager,
1097 EmpathyAccount *account,
1098 EmpathyAccountsDialog *dialog)
1101 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1103 if (accounts_dialog_get_account_iter (dialog, account, &iter))
1105 g_signal_handlers_disconnect_by_func (account,
1106 accounts_dialog_account_display_name_changed_cb, dialog);
1107 gtk_list_store_remove (GTK_LIST_STORE (
1108 gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview))), &iter);
1113 enable_or_disable_account (EmpathyAccountsDialog *dialog,
1114 EmpathyAccount *account,
1117 GtkTreeModel *model;
1118 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1120 /* Update the status in the model */
1121 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1123 DEBUG ("Account %s is now %s",
1124 empathy_account_get_display_name (account),
1125 enabled ? "enabled" : "disabled");
1129 accounts_dialog_account_disabled_cb (EmpathyAccountManager *manager,
1130 EmpathyAccount *account,
1131 EmpathyAccountsDialog *dialog)
1133 enable_or_disable_account (dialog, account, FALSE);
1137 accounts_dialog_account_enabled_cb (EmpathyAccountManager *manager,
1138 EmpathyAccount *account,
1139 EmpathyAccountsDialog *dialog)
1141 enable_or_disable_account (dialog, account, TRUE);
1145 accounts_dialog_account_changed_cb (EmpathyAccountManager *manager,
1146 EmpathyAccount *account,
1147 EmpathyAccountsDialog *dialog)
1149 EmpathyAccountSettings *settings, *selected_settings;
1150 GtkTreeModel *model;
1152 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1154 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1156 if (!accounts_dialog_get_account_iter (dialog, account, &iter))
1159 gtk_tree_model_get (model, &iter,
1160 COL_ACCOUNT_SETTINGS_POINTER, &settings,
1163 accounts_dialog_update (dialog, settings);
1164 selected_settings = accounts_dialog_model_get_selected_settings (dialog);
1166 if (settings == selected_settings)
1167 accounts_dialog_update_name_label (dialog,
1168 empathy_account_settings_get_display_name (settings));
1171 g_object_unref (settings);
1173 if (selected_settings)
1174 g_object_unref (selected_settings);
1178 accounts_dialog_button_create_clicked_cb (GtkWidget *button,
1179 EmpathyAccountsDialog *dialog)
1181 EmpathyAccountSettings *settings;
1183 const gchar *display_name;
1184 TpConnectionManager *cm;
1185 TpConnectionManagerProtocol *proto;
1186 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1188 cm = empathy_protocol_chooser_dup_selected (
1189 EMPATHY_PROTOCOL_CHOOSER (priv->combobox_protocol), &proto);
1190 display_name = empathy_protocol_name_to_display_name (proto->name);
1192 if (display_name == NULL)
1193 display_name = proto->name;
1195 /* Create account */
1196 /* To translator: %s is the name of the protocol, such as "Google Talk" or
1199 str = g_strdup_printf (_("New %s account"), display_name);
1200 settings = empathy_account_settings_new (cm->name, proto->name, str);
1204 if (tp_connection_manager_protocol_can_register (proto))
1208 active = gtk_toggle_button_get_active
1209 (GTK_TOGGLE_BUTTON (priv->radiobutton_register));
1211 empathy_account_settings_set_boolean (settings, "register", TRUE);
1214 accounts_dialog_add (dialog, settings);
1215 accounts_dialog_model_set_selected (dialog, settings);
1217 g_object_unref (settings);
1218 g_object_unref (cm);
1222 accounts_dialog_button_back_clicked_cb (GtkWidget *button,
1223 EmpathyAccountsDialog *dialog)
1225 EmpathyAccountSettings *settings;
1227 settings = accounts_dialog_model_get_selected_settings (dialog);
1228 accounts_dialog_update_settings (dialog, settings);
1231 g_object_unref (settings);
1235 accounts_dialog_button_help_clicked_cb (GtkWidget *button,
1236 EmpathyAccountsDialog *dialog)
1238 empathy_url_show (button, "ghelp:empathy?empathy-create-account");
1242 accounts_dialog_response_cb (GtkWidget *widget,
1244 EmpathyAccountsDialog *dialog)
1246 if (response == GTK_RESPONSE_CLOSE)
1247 gtk_widget_destroy (widget);
1251 accounts_dialog_destroy_cb (GtkObject *obj,
1252 EmpathyAccountsDialog *dialog)
1256 g_object_unref (dialog);
1260 accounts_dialog_set_selected_account (EmpathyAccountsDialog *dialog,
1261 EmpathyAccount *account)
1263 GtkTreeSelection *selection;
1265 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1267 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
1268 if (accounts_dialog_get_account_iter (dialog, account, &iter))
1269 gtk_tree_selection_select_iter (selection, &iter);
1273 accounts_dialog_cms_ready_cb (EmpathyConnectionManagers *cms,
1275 EmpathyAccountsDialog *dialog)
1277 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1279 if (empathy_connection_managers_is_ready (cms))
1281 accounts_dialog_update_settings (dialog, NULL);
1283 if (priv->initial_selection != NULL)
1285 accounts_dialog_set_selected_account
1286 (dialog, priv->initial_selection);
1287 g_object_unref (priv->initial_selection);
1288 priv->initial_selection = NULL;
1294 accounts_dialog_build_ui (EmpathyAccountsDialog *dialog)
1298 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1300 filename = empathy_file_lookup ("empathy-accounts-dialog.ui", "src");
1302 gui = empathy_builder_get_file (filename,
1303 "accounts_dialog", &priv->window,
1304 "vbox_details", &priv->vbox_details,
1305 "frame_no_protocol", &priv->frame_no_protocol,
1306 "alignment_settings", &priv->alignment_settings,
1307 "treeview", &priv->treeview,
1308 "frame_new_account", &priv->frame_new_account,
1309 "hbox_type", &priv->hbox_type,
1310 "button_create", &priv->button_create,
1311 "button_back", &priv->button_back,
1312 "radiobutton_reuse", &priv->radiobutton_reuse,
1313 "radiobutton_register", &priv->radiobutton_register,
1314 "image_type", &priv->image_type,
1315 "label_name", &priv->label_name,
1316 "button_add", &priv->button_add,
1320 empathy_builder_connect (gui, dialog,
1321 "accounts_dialog", "response", accounts_dialog_response_cb,
1322 "accounts_dialog", "destroy", accounts_dialog_destroy_cb,
1323 "button_create", "clicked", accounts_dialog_button_create_clicked_cb,
1324 "button_back", "clicked", accounts_dialog_button_back_clicked_cb,
1325 "button_add", "clicked", accounts_dialog_button_add_clicked_cb,
1326 "button_help", "clicked", accounts_dialog_button_help_clicked_cb,
1329 g_object_unref (gui);
1331 priv->combobox_protocol = empathy_protocol_chooser_new ();
1332 gtk_box_pack_start (GTK_BOX (priv->hbox_type),
1333 priv->combobox_protocol,
1335 gtk_widget_show (priv->combobox_protocol);
1336 g_signal_connect (priv->combobox_protocol, "changed",
1337 G_CALLBACK (accounts_dialog_protocol_changed_cb),
1340 if (priv->parent_window)
1341 gtk_window_set_transient_for (GTK_WINDOW (priv->window),
1342 priv->parent_window);
1346 do_dispose (GObject *obj)
1348 EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (obj);
1349 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1351 /* Disconnect signals */
1352 g_signal_handlers_disconnect_by_func (priv->account_manager,
1353 accounts_dialog_account_added_cb,
1355 g_signal_handlers_disconnect_by_func (priv->account_manager,
1356 accounts_dialog_account_removed_cb,
1358 g_signal_handlers_disconnect_by_func (priv->account_manager,
1359 accounts_dialog_account_enabled_cb,
1361 g_signal_handlers_disconnect_by_func (priv->account_manager,
1362 accounts_dialog_account_disabled_cb,
1364 g_signal_handlers_disconnect_by_func (priv->account_manager,
1365 accounts_dialog_account_changed_cb,
1367 g_signal_handlers_disconnect_by_func (priv->account_manager,
1368 accounts_dialog_connection_changed_cb,
1371 if (priv->connecting_id)
1372 g_source_remove (priv->connecting_id);
1374 if (priv->account_manager != NULL)
1376 g_object_unref (priv->account_manager);
1377 priv->account_manager = NULL;
1380 if (priv->cms != NULL)
1382 g_object_unref (priv->cms);
1386 if (priv->initial_selection != NULL)
1387 g_object_unref (priv->initial_selection);
1388 priv->initial_selection = NULL;
1390 G_OBJECT_CLASS (empathy_accounts_dialog_parent_class)->dispose (obj);
1394 do_constructor (GType type,
1396 GObjectConstructParam *props)
1400 if (dialog_singleton)
1402 retval = G_OBJECT (dialog_singleton);
1403 g_object_ref (retval);
1408 G_OBJECT_CLASS (empathy_accounts_dialog_parent_class)->constructor
1409 (type, n_props, props);
1411 dialog_singleton = EMPATHY_ACCOUNTS_DIALOG (retval);
1412 g_object_add_weak_pointer (retval, (gpointer) &dialog_singleton);
1419 do_get_property (GObject *object,
1424 EmpathyAccountsDialogPriv *priv = GET_PRIV (object);
1426 switch (property_id)
1429 g_value_set_object (value, priv->parent_window);
1432 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1437 do_set_property (GObject *object,
1439 const GValue *value,
1442 EmpathyAccountsDialogPriv *priv = GET_PRIV (object);
1444 switch (property_id)
1447 priv->parent_window = g_value_get_object (value);
1450 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1455 do_constructed (GObject *object)
1457 EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (object);
1458 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1459 GList *accounts, *l;
1460 gboolean import_asked;
1462 accounts_dialog_build_ui (dialog);
1464 /* Set up signalling */
1465 priv->account_manager = empathy_account_manager_dup_singleton ();
1467 g_signal_connect (priv->account_manager, "account-created",
1468 G_CALLBACK (accounts_dialog_account_added_cb),
1470 g_signal_connect (priv->account_manager, "account-deleted",
1471 G_CALLBACK (accounts_dialog_account_removed_cb),
1473 g_signal_connect (priv->account_manager, "account-enabled",
1474 G_CALLBACK (accounts_dialog_account_enabled_cb),
1476 g_signal_connect (priv->account_manager, "account-disabled",
1477 G_CALLBACK (accounts_dialog_account_disabled_cb),
1479 g_signal_connect (priv->account_manager, "account-changed",
1480 G_CALLBACK (accounts_dialog_account_changed_cb),
1482 g_signal_connect (priv->account_manager, "account-connection-changed",
1483 G_CALLBACK (accounts_dialog_connection_changed_cb),
1486 accounts_dialog_model_setup (dialog);
1488 /* Add existing accounts */
1489 accounts = empathy_account_manager_dup_accounts (priv->account_manager);
1490 for (l = accounts; l; l = l->next)
1492 accounts_dialog_add_account (dialog, l->data);
1493 g_object_unref (l->data);
1495 g_list_free (accounts);
1497 priv->cms = empathy_connection_managers_dup_singleton ();
1498 if (!empathy_connection_managers_is_ready (priv->cms))
1499 g_signal_connect (priv->cms, "notify::ready",
1500 G_CALLBACK (accounts_dialog_cms_ready_cb), dialog);
1502 accounts_dialog_model_select_first (dialog);
1504 empathy_conf_get_bool (empathy_conf_get (),
1505 EMPATHY_PREFS_IMPORT_ASKED, &import_asked);
1508 if (empathy_import_accounts_to_import ())
1513 GtkWidget *import_dialog;
1515 empathy_conf_set_bool (empathy_conf_get (),
1516 EMPATHY_PREFS_IMPORT_ASKED, TRUE);
1517 import_dialog = empathy_import_dialog_new (GTK_WINDOW (priv->window),
1519 gtk_widget_show (import_dialog);
1525 empathy_accounts_dialog_class_init (EmpathyAccountsDialogClass *klass)
1527 GObjectClass *oclass = G_OBJECT_CLASS (klass);
1528 GParamSpec *param_spec;
1530 oclass->constructor = do_constructor;
1531 oclass->dispose = do_dispose;
1532 oclass->constructed = do_constructed;
1533 oclass->set_property = do_set_property;
1534 oclass->get_property = do_get_property;
1536 param_spec = g_param_spec_object ("parent",
1537 "parent", "The parent window",
1539 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
1540 g_object_class_install_property (oclass, PROP_PARENT, param_spec);
1542 g_type_class_add_private (klass, sizeof (EmpathyAccountsDialogPriv));
1546 empathy_accounts_dialog_init (EmpathyAccountsDialog *dialog)
1548 EmpathyAccountsDialogPriv *priv;
1550 priv = G_TYPE_INSTANCE_GET_PRIVATE ((dialog),
1551 EMPATHY_TYPE_ACCOUNTS_DIALOG,
1552 EmpathyAccountsDialogPriv);
1553 dialog->priv = priv;
1556 /* public methods */
1559 empathy_accounts_dialog_show (GtkWindow *parent,
1560 EmpathyAccount *selected_account)
1562 EmpathyAccountsDialog *dialog;
1563 EmpathyAccountsDialogPriv *priv;
1565 dialog = g_object_new (EMPATHY_TYPE_ACCOUNTS_DIALOG,
1566 "parent", parent, NULL);
1568 priv = GET_PRIV (dialog);
1570 if (selected_account)
1572 if (empathy_connection_managers_is_ready (priv->cms))
1573 accounts_dialog_set_selected_account (dialog, selected_account);
1575 /* save the selection to set it later when the cms
1578 priv->initial_selection = g_object_ref (selected_account);
1581 gtk_window_present (GTK_WINDOW (priv->window));
1583 return priv->window;