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 default_display_name =
206 g_strdup_printf (_("%s on %s"), login_id, server);
210 default_display_name = g_strdup (login_id);
213 else if (protocol != NULL)
215 /* To translators: The parameter is the protocol name. The resulting
216 * string will be something like: "Jabber Account" */
217 default_display_name = g_strdup_printf (_("%s Account"), protocol);
221 default_display_name = g_strdup (_("New account"));
224 return default_display_name;
228 empathy_account_dialog_account_created_cb (EmpathyAccountWidget *widget_object,
229 EmpathyAccountsDialog *dialog)
232 EmpathyAccountSettings *settings =
233 accounts_dialog_model_get_selected_settings (dialog);
235 display_name = get_default_display_name (settings);
237 empathy_account_settings_set_display_name_async (settings,
238 display_name, NULL, NULL);
240 g_free (display_name);
242 accounts_dialog_update_settings (dialog, settings);
245 g_object_unref (settings);
249 get_account_setup_widget (EmpathyAccountSettings *settings,
250 EmpathyAccountWidget **widget_object)
252 const gchar *proto = empathy_account_settings_get_protocol (settings);
253 EmpathyConnectionManagers *cm =
254 empathy_connection_managers_dup_singleton ();
255 GList *cms = empathy_connection_managers_get_cms (cm);
258 for (l = cms; l; l = l->next)
260 TpConnectionManager *tp_cm = l->data;
261 if (tp_connection_manager_has_protocol (tp_cm, proto))
264 *widget_object = empathy_account_widget_new_for_protocol (proto,
266 return empathy_account_widget_get_widget (*widget_object);
271 *widget_object = empathy_account_widget_new_for_protocol ("generic", settings,
273 return empathy_account_widget_get_widget (*widget_object);
277 account_dialog_create_settings_widget (EmpathyAccountsDialog *dialog,
278 EmpathyAccountSettings *settings)
280 EmpathyAccountWidget *widget_object = NULL;
281 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
284 priv->settings_widget = get_account_setup_widget (settings, &widget_object);
285 g_signal_connect (widget_object, "account-created",
286 G_CALLBACK (empathy_account_dialog_account_created_cb), dialog);
287 g_signal_connect (widget_object, "cancelled",
288 G_CALLBACK (empathy_account_dialog_widget_cancelled_cb), dialog);
290 gtk_container_add (GTK_CONTAINER (priv->alignment_settings),
291 priv->settings_widget);
292 gtk_widget_show (priv->settings_widget);
294 icon_name = empathy_account_settings_get_icon_name (settings);
296 gtk_image_set_from_icon_name (GTK_IMAGE (priv->image_type),
297 icon_name, GTK_ICON_SIZE_DIALOG);
298 gtk_widget_set_tooltip_text (priv->image_type,
299 empathy_protocol_name_to_display_name
300 (empathy_account_settings_get_protocol (settings)));
302 accounts_dialog_update_name_label (dialog,
303 empathy_account_settings_get_display_name (settings));
307 account_dialog_settings_ready_cb (EmpathyAccountSettings *settings,
309 EmpathyAccountsDialog *dialog)
311 if (empathy_account_settings_is_ready (settings))
312 account_dialog_create_settings_widget (dialog, settings);
316 accounts_dialog_model_select_first (EmpathyAccountsDialog *dialog)
320 GtkTreeSelection *selection;
322 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
325 view = GTK_TREE_VIEW (priv->treeview);
326 model = gtk_tree_view_get_model (view);
328 if (gtk_tree_model_get_iter_first (model, &iter))
330 selection = gtk_tree_view_get_selection (view);
331 gtk_tree_selection_select_iter (selection, &iter);
335 accounts_dialog_update_settings (dialog, NULL);
340 accounts_dialog_protocol_changed_cb (GtkWidget *widget,
341 EmpathyAccountsDialog *dialog)
343 TpConnectionManager *cm;
344 TpConnectionManagerProtocol *proto;
345 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
347 cm = empathy_protocol_chooser_dup_selected (
348 EMPATHY_PROTOCOL_CHOOSER (priv->combobox_protocol), &proto);
359 if (tp_connection_manager_protocol_can_register (proto))
361 gtk_widget_show (priv->radiobutton_register);
362 gtk_widget_show (priv->radiobutton_reuse);
366 gtk_widget_hide (priv->radiobutton_register);
367 gtk_widget_hide (priv->radiobutton_reuse);
373 accounts_dialog_button_add_clicked_cb (GtkWidget *button,
374 EmpathyAccountsDialog *dialog)
378 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
380 view = GTK_TREE_VIEW (priv->treeview);
381 model = gtk_tree_view_get_model (view);
383 gtk_widget_set_sensitive (priv->button_add, FALSE);
384 gtk_widget_hide (priv->vbox_details);
385 gtk_widget_hide (priv->frame_no_protocol);
386 gtk_widget_show (priv->frame_new_account);
388 /* If we have no account, no need of a back button */
389 if (gtk_tree_model_iter_n_children (model, NULL) > 0)
390 gtk_widget_show (priv->button_back);
392 gtk_widget_hide (priv->button_back);
394 accounts_dialog_protocol_changed_cb (priv->radiobutton_register, dialog);
395 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->radiobutton_reuse),
397 gtk_combo_box_set_active (GTK_COMBO_BOX (priv->combobox_protocol), 0);
398 gtk_widget_grab_focus (priv->combobox_protocol);
402 accounts_dialog_update_settings (EmpathyAccountsDialog *dialog,
403 EmpathyAccountSettings *settings)
405 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
407 if (priv->settings_ready != NULL)
409 g_signal_handler_disconnect (priv->settings_ready,
410 priv->settings_ready_id);
411 priv->settings_ready = NULL;
412 priv->settings_ready_id = 0;
420 view = GTK_TREE_VIEW (priv->treeview);
421 model = gtk_tree_view_get_model (view);
423 if (gtk_tree_model_iter_n_children (model, NULL) > 0)
425 /* We have configured accounts, select the first one */
426 accounts_dialog_model_select_first (dialog);
429 if (empathy_connection_managers_get_cms_num (priv->cms) > 0)
431 /* We have no account configured but we have some
432 * profiles installed. The user obviously wants to add
433 * an account. Click on the Add button for him. */
434 accounts_dialog_button_add_clicked_cb (priv->button_add,
439 /* No account and no profile, warn the user */
440 gtk_widget_hide (priv->vbox_details);
441 gtk_widget_hide (priv->frame_new_account);
442 gtk_widget_show (priv->frame_no_protocol);
443 gtk_widget_set_sensitive (priv->button_add, FALSE);
447 /* We have an account selected, destroy old settings and create a new
448 * one for the account selected */
449 gtk_widget_hide (priv->frame_new_account);
450 gtk_widget_hide (priv->frame_no_protocol);
451 gtk_widget_show (priv->vbox_details);
452 gtk_widget_set_sensitive (priv->button_add, TRUE);
454 if (priv->settings_widget)
456 gtk_widget_destroy (priv->settings_widget);
457 priv->settings_widget = NULL;
460 if (empathy_account_settings_is_ready (settings))
462 account_dialog_create_settings_widget (dialog, settings);
466 priv->settings_ready = settings;
467 priv->settings_ready_id =
468 g_signal_connect (settings, "notify::ready",
469 G_CALLBACK (account_dialog_settings_ready_cb), dialog);
475 accounts_dialog_name_editing_started_cb (GtkCellRenderer *renderer,
476 GtkCellEditable *editable,
478 EmpathyAccountsDialog *dialog)
480 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
482 if (priv->connecting_id)
483 g_source_remove (priv->connecting_id);
485 DEBUG ("Editing account name started; stopping flashing");
489 accounts_dialog_model_pixbuf_data_func (GtkTreeViewColumn *tree_column,
490 GtkCellRenderer *cell,
493 EmpathyAccountsDialog *dialog)
495 EmpathyAccountSettings *settings;
498 TpConnectionStatus status;
499 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
501 gtk_tree_model_get (model, iter,
503 COL_ACCOUNT_SETTINGS_POINTER, &settings,
506 icon_name = empathy_account_settings_get_icon_name (settings);
507 pixbuf = empathy_pixbuf_from_icon_name (icon_name, GTK_ICON_SIZE_BUTTON);
511 if (status == TP_CONNECTION_STATUS_DISCONNECTED ||
512 (status == TP_CONNECTION_STATUS_CONNECTING &&
513 !priv->connecting_show))
515 GdkPixbuf *modded_pixbuf;
517 modded_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
520 gdk_pixbuf_get_width (pixbuf),
521 gdk_pixbuf_get_height (pixbuf));
523 gdk_pixbuf_saturate_and_pixelate (pixbuf,
527 g_object_unref (pixbuf);
528 pixbuf = modded_pixbuf;
537 g_object_unref (settings);
540 g_object_unref (pixbuf);
544 accounts_dialog_row_changed_foreach (GtkTreeModel *model,
549 gtk_tree_model_row_changed (model, path, iter);
555 accounts_dialog_flash_connecting_cb (EmpathyAccountsDialog *dialog)
559 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
561 priv->connecting_show = !priv->connecting_show;
563 view = GTK_TREE_VIEW (priv->treeview);
564 model = gtk_tree_view_get_model (view);
566 gtk_tree_model_foreach (model, accounts_dialog_row_changed_foreach, NULL);
572 accounts_dialog_name_edited_cb (GtkCellRendererText *renderer,
575 EmpathyAccountsDialog *dialog)
577 EmpathyAccountSettings *settings;
579 GtkTreePath *treepath;
581 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
583 if (empathy_account_manager_get_connecting_accounts
584 (priv->account_manager) > 0)
586 priv->connecting_id = g_timeout_add (FLASH_TIMEOUT,
587 (GSourceFunc) accounts_dialog_flash_connecting_cb,
591 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
592 treepath = gtk_tree_path_new_from_string (path);
593 gtk_tree_model_get_iter (model, &iter, treepath);
594 gtk_tree_model_get (model, &iter,
595 COL_ACCOUNT_SETTINGS_POINTER, &settings,
597 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
600 gtk_tree_path_free (treepath);
602 empathy_account_settings_set_display_name_async (settings, new_text,
604 g_object_set (settings, "display-name-overridden", TRUE, NULL);
605 g_object_unref (settings);
609 accounts_dialog_delete_account_response_cb (GtkDialog *message_dialog,
613 EmpathyAccount *account;
616 GtkTreeSelection *selection;
617 EmpathyAccountsDialog *account_dialog = EMPATHY_ACCOUNTS_DIALOG (user_data);
618 EmpathyAccountsDialogPriv *priv = GET_PRIV (account_dialog);
620 if (response_id == GTK_RESPONSE_YES)
622 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
624 if (!gtk_tree_selection_get_selected (selection, &model, &iter))
627 gtk_tree_model_get (model, &iter, COL_ACCOUNT_POINTER, &account, -1);
631 g_signal_handlers_disconnect_by_func (account,
632 accounts_dialog_account_display_name_changed_cb, account_dialog);
633 empathy_account_remove_async (account, NULL, NULL);
634 g_object_unref (account);
637 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
638 accounts_dialog_model_select_first (account_dialog);
641 gtk_widget_destroy (GTK_WIDGET (message_dialog));
645 accounts_dialog_view_delete_activated_cb (EmpathyCellRendererActivatable *cell,
646 const gchar *path_string,
647 EmpathyAccountsDialog *dialog)
649 EmpathyAccount *account;
652 GtkWidget *message_dialog;
653 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
655 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
657 if (!gtk_tree_model_get_iter_from_string (model, &iter, path_string))
660 gtk_tree_model_get (model, &iter, COL_ACCOUNT_POINTER, &account, -1);
662 if (account == NULL || !empathy_account_is_valid (account))
664 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
665 accounts_dialog_model_select_first (dialog);
669 message_dialog = gtk_message_dialog_new
670 (GTK_WINDOW (priv->window),
671 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
672 GTK_MESSAGE_QUESTION,
674 _("You are about to remove your %s account!\n"
675 "Are you sure you want to proceed?"),
676 empathy_account_get_display_name (account));
678 gtk_message_dialog_format_secondary_text
679 (GTK_MESSAGE_DIALOG (message_dialog),
680 _("Any associated conversations and chat rooms will NOT be "
681 "removed if you decide to proceed.\n"
683 "Should you decide to add the account back at a later time, "
684 "they will still be available."));
686 gtk_dialog_add_button (GTK_DIALOG (message_dialog),
689 gtk_dialog_add_button (GTK_DIALOG (message_dialog),
693 g_signal_connect (message_dialog, "response",
694 G_CALLBACK (accounts_dialog_delete_account_response_cb), dialog);
696 gtk_widget_show (message_dialog);
699 g_object_unref (account);
703 accounts_dialog_model_add_columns (EmpathyAccountsDialog *dialog)
706 GtkTreeViewColumn *column;
707 GtkCellRenderer *cell;
708 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
710 view = GTK_TREE_VIEW (priv->treeview);
711 gtk_tree_view_set_headers_visible (view, FALSE);
714 column = gtk_tree_view_column_new ();
715 gtk_tree_view_column_set_expand (column, TRUE);
716 gtk_tree_view_append_column (view, column);
719 cell = gtk_cell_renderer_pixbuf_new ();
720 gtk_tree_view_column_pack_start (column, cell, FALSE);
721 gtk_tree_view_column_set_cell_data_func (column, cell,
722 (GtkTreeCellDataFunc)
723 accounts_dialog_model_pixbuf_data_func,
728 cell = gtk_cell_renderer_text_new ();
730 "ellipsize", PANGO_ELLIPSIZE_END,
734 gtk_tree_view_column_pack_start (column, cell, TRUE);
735 gtk_tree_view_column_add_attribute (column, cell, "text", COL_NAME);
736 g_signal_connect (cell, "edited",
737 G_CALLBACK (accounts_dialog_name_edited_cb),
739 g_signal_connect (cell, "editing-started",
740 G_CALLBACK (accounts_dialog_name_editing_started_cb),
744 cell = empathy_cell_renderer_activatable_new ();
745 gtk_tree_view_column_pack_start (column, cell, FALSE);
747 "icon-name", GTK_STOCK_DELETE,
750 g_signal_connect (cell, "path-activated",
751 G_CALLBACK (accounts_dialog_view_delete_activated_cb),
755 static EmpathyAccountSettings *
756 accounts_dialog_model_get_selected_settings (EmpathyAccountsDialog *dialog)
760 GtkTreeSelection *selection;
762 EmpathyAccountSettings *settings;
763 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
765 view = GTK_TREE_VIEW (priv->treeview);
766 selection = gtk_tree_view_get_selection (view);
768 if (!gtk_tree_selection_get_selected (selection, &model, &iter))
771 gtk_tree_model_get (model, &iter,
772 COL_ACCOUNT_SETTINGS_POINTER, &settings, -1);
778 accounts_dialog_model_selection_changed (GtkTreeSelection *selection,
779 EmpathyAccountsDialog *dialog)
781 EmpathyAccountSettings *settings;
784 gboolean is_selection;
786 is_selection = gtk_tree_selection_get_selected (selection, &model, &iter);
788 settings = accounts_dialog_model_get_selected_settings (dialog);
790 if (settings != NULL)
791 empathy_account_settings_discard_changes (settings);
793 accounts_dialog_update_settings (dialog, settings);
795 if (settings != NULL)
796 g_object_unref (settings);
800 accounts_dialog_model_setup (EmpathyAccountsDialog *dialog)
803 GtkTreeSelection *selection;
804 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
806 store = gtk_list_store_new (COL_COUNT,
807 G_TYPE_STRING, /* name */
808 G_TYPE_UINT, /* status */
809 EMPATHY_TYPE_ACCOUNT, /* account */
810 EMPATHY_TYPE_ACCOUNT_SETTINGS); /* settings */
812 gtk_tree_view_set_model (GTK_TREE_VIEW (priv->treeview),
813 GTK_TREE_MODEL (store));
815 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
816 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
818 g_signal_connect (selection, "changed",
819 G_CALLBACK (accounts_dialog_model_selection_changed),
822 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
823 COL_NAME, GTK_SORT_ASCENDING);
825 accounts_dialog_model_add_columns (dialog);
827 g_object_unref (store);
831 accounts_dialog_get_settings_iter (EmpathyAccountsDialog *dialog,
832 EmpathyAccountSettings *settings,
836 GtkTreeSelection *selection;
839 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
841 /* Update the status in the model */
842 view = GTK_TREE_VIEW (priv->treeview);
843 selection = gtk_tree_view_get_selection (view);
844 model = gtk_tree_view_get_model (view);
846 for (ok = gtk_tree_model_get_iter_first (model, iter);
848 ok = gtk_tree_model_iter_next (model, iter))
850 EmpathyAccountSettings *this_settings;
853 gtk_tree_model_get (model, iter,
854 COL_ACCOUNT_SETTINGS_POINTER, &this_settings,
857 equal = (this_settings == settings);
858 g_object_unref (this_settings);
868 accounts_dialog_get_account_iter (EmpathyAccountsDialog *dialog,
869 EmpathyAccount *account,
873 GtkTreeSelection *selection;
876 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
878 /* Update the status in the model */
879 view = GTK_TREE_VIEW (priv->treeview);
880 selection = gtk_tree_view_get_selection (view);
881 model = gtk_tree_view_get_model (view);
883 for (ok = gtk_tree_model_get_iter_first (model, iter);
885 ok = gtk_tree_model_iter_next (model, iter))
887 EmpathyAccountSettings *settings;
890 gtk_tree_model_get (model, iter,
891 COL_ACCOUNT_SETTINGS_POINTER, &settings,
894 equal = empathy_account_settings_has_account (settings, account);
895 g_object_unref (settings);
905 accounts_dialog_model_set_selected (EmpathyAccountsDialog *dialog,
906 EmpathyAccountSettings *settings)
908 GtkTreeSelection *selection;
910 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
912 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
913 if (accounts_dialog_get_settings_iter (dialog, settings, &iter))
914 gtk_tree_selection_select_iter (selection, &iter);
917 accounts_dialog_add (EmpathyAccountsDialog *dialog,
918 EmpathyAccountSettings *settings)
923 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
925 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
926 name = empathy_account_settings_get_display_name (settings);
928 gtk_list_store_append (GTK_LIST_STORE (model), &iter);
930 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
932 COL_STATUS, TP_CONNECTION_STATUS_DISCONNECTED,
933 COL_ACCOUNT_SETTINGS_POINTER, settings,
938 accounts_dialog_connection_changed_cb (EmpathyAccountManager *manager,
939 EmpathyAccount *account,
940 TpConnectionStatusReason reason,
941 TpConnectionStatus current,
942 TpConnectionStatus previous,
943 EmpathyAccountsDialog *dialog)
948 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
950 /* Update the status in the model */
951 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
953 if (accounts_dialog_get_account_iter (dialog, account, &iter))
957 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
961 path = gtk_tree_model_get_path (model, &iter);
962 gtk_tree_model_row_changed (model, path, &iter);
963 gtk_tree_path_free (path);
966 found = (empathy_account_manager_get_connecting_accounts (manager) > 0);
968 if (!found && priv->connecting_id)
970 g_source_remove (priv->connecting_id);
971 priv->connecting_id = 0;
974 if (found && !priv->connecting_id)
975 priv->connecting_id = g_timeout_add (FLASH_TIMEOUT,
976 (GSourceFunc) accounts_dialog_flash_connecting_cb,
981 accounts_dialog_account_display_name_changed_cb (EmpathyAccount *account,
985 const gchar *display_name;
988 EmpathyAccountSettings *settings;
989 EmpathyAccount *selected_account;
990 EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (user_data);
991 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
993 display_name = empathy_account_get_display_name (account);
994 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
995 settings = accounts_dialog_model_get_selected_settings (dialog);
996 selected_account = empathy_account_settings_get_account (settings);
998 if (accounts_dialog_get_account_iter (dialog, account, &iter))
1000 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
1001 COL_NAME, display_name,
1005 if (selected_account == account)
1006 accounts_dialog_update_name_label (dialog, display_name);
1008 g_object_unref (settings);
1012 accounts_dialog_add_account (EmpathyAccountsDialog *dialog,
1013 EmpathyAccount *account)
1015 EmpathyAccountSettings *settings;
1016 GtkTreeModel *model;
1018 TpConnectionStatus status;
1021 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1023 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1024 g_object_get (account, "connection-status", &status, NULL);
1025 name = empathy_account_get_display_name (account);
1026 enabled = empathy_account_is_enabled (account);
1028 if (!accounts_dialog_get_account_iter (dialog, account, &iter))
1029 gtk_list_store_append (GTK_LIST_STORE (model), &iter);
1031 settings = empathy_account_settings_new_for_account (account);
1033 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
1036 COL_ACCOUNT_POINTER, account,
1037 COL_ACCOUNT_SETTINGS_POINTER, settings,
1040 accounts_dialog_connection_changed_cb (priv->account_manager,
1042 TP_CONNECTION_STATUS_REASON_NONE_SPECIFIED,
1044 TP_CONNECTION_STATUS_DISCONNECTED,
1047 g_signal_connect (account, "notify::display-name",
1048 G_CALLBACK (accounts_dialog_account_display_name_changed_cb), dialog);
1050 g_object_unref (settings);
1054 accounts_dialog_update (EmpathyAccountsDialog *dialog,
1055 EmpathyAccountSettings *settings)
1057 GtkTreeModel *model;
1059 TpConnectionStatus status = TP_CONNECTION_STATUS_DISCONNECTED;
1061 gboolean enabled = FALSE;
1062 EmpathyAccount *account;
1063 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1065 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1066 name = empathy_account_settings_get_display_name (settings);
1068 account = empathy_account_settings_get_account (settings);
1069 if (account != NULL)
1071 enabled = empathy_account_is_enabled (account);
1072 g_object_get (account, "connection-status", &status, NULL);
1075 accounts_dialog_get_settings_iter (dialog, settings, &iter);
1076 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
1079 COL_ACCOUNT_POINTER, account,
1080 COL_ACCOUNT_SETTINGS_POINTER, settings,
1085 accounts_dialog_account_added_cb (EmpathyAccountManager *manager,
1086 EmpathyAccount *account,
1087 EmpathyAccountsDialog *dialog)
1089 accounts_dialog_add_account (dialog, account);
1094 accounts_dialog_account_removed_cb (EmpathyAccountManager *manager,
1095 EmpathyAccount *account,
1096 EmpathyAccountsDialog *dialog)
1099 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1101 if (accounts_dialog_get_account_iter (dialog, account, &iter))
1103 g_signal_handlers_disconnect_by_func (account,
1104 accounts_dialog_account_display_name_changed_cb, dialog);
1105 gtk_list_store_remove (GTK_LIST_STORE (
1106 gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview))), &iter);
1111 enable_or_disable_account (EmpathyAccountsDialog *dialog,
1112 EmpathyAccount *account,
1115 GtkTreeModel *model;
1116 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1118 /* Update the status in the model */
1119 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1121 DEBUG ("Account %s is now %s",
1122 empathy_account_get_display_name (account),
1123 enabled ? "enabled" : "disabled");
1127 accounts_dialog_account_disabled_cb (EmpathyAccountManager *manager,
1128 EmpathyAccount *account,
1129 EmpathyAccountsDialog *dialog)
1131 enable_or_disable_account (dialog, account, FALSE);
1135 accounts_dialog_account_enabled_cb (EmpathyAccountManager *manager,
1136 EmpathyAccount *account,
1137 EmpathyAccountsDialog *dialog)
1139 enable_or_disable_account (dialog, account, TRUE);
1143 accounts_dialog_account_changed_cb (EmpathyAccountManager *manager,
1144 EmpathyAccount *account,
1145 EmpathyAccountsDialog *dialog)
1147 EmpathyAccountSettings *settings, *selected_settings;
1148 GtkTreeModel *model;
1150 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1152 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
1154 if (!accounts_dialog_get_account_iter (dialog, account, &iter))
1157 gtk_tree_model_get (model, &iter,
1158 COL_ACCOUNT_SETTINGS_POINTER, &settings,
1161 accounts_dialog_update (dialog, settings);
1162 selected_settings = accounts_dialog_model_get_selected_settings (dialog);
1164 if (settings == selected_settings)
1165 accounts_dialog_update_name_label (dialog,
1166 empathy_account_settings_get_display_name (settings));
1169 g_object_unref (settings);
1171 if (selected_settings)
1172 g_object_unref (selected_settings);
1176 accounts_dialog_button_create_clicked_cb (GtkWidget *button,
1177 EmpathyAccountsDialog *dialog)
1179 EmpathyAccountSettings *settings;
1181 const gchar *display_name;
1182 TpConnectionManager *cm;
1183 TpConnectionManagerProtocol *proto;
1184 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1186 cm = empathy_protocol_chooser_dup_selected (
1187 EMPATHY_PROTOCOL_CHOOSER (priv->combobox_protocol), &proto);
1188 display_name = empathy_protocol_name_to_display_name (proto->name);
1190 if (display_name == NULL)
1191 display_name = proto->name;
1193 /* Create account */
1194 /* To translator: %s is the name of the protocol, such as "Google Talk" or
1197 str = g_strdup_printf (_("New %s account"), display_name);
1198 settings = empathy_account_settings_new (cm->name, proto->name, str);
1202 if (tp_connection_manager_protocol_can_register (proto))
1206 active = gtk_toggle_button_get_active
1207 (GTK_TOGGLE_BUTTON (priv->radiobutton_register));
1209 empathy_account_settings_set_boolean (settings, "register", TRUE);
1212 accounts_dialog_add (dialog, settings);
1213 accounts_dialog_model_set_selected (dialog, settings);
1215 g_object_unref (settings);
1216 g_object_unref (cm);
1220 accounts_dialog_button_back_clicked_cb (GtkWidget *button,
1221 EmpathyAccountsDialog *dialog)
1223 EmpathyAccountSettings *settings;
1225 settings = accounts_dialog_model_get_selected_settings (dialog);
1226 accounts_dialog_update_settings (dialog, settings);
1229 g_object_unref (settings);
1233 accounts_dialog_button_help_clicked_cb (GtkWidget *button,
1234 EmpathyAccountsDialog *dialog)
1236 empathy_url_show (button, "ghelp:empathy?empathy-create-account");
1240 accounts_dialog_response_cb (GtkWidget *widget,
1242 EmpathyAccountsDialog *dialog)
1244 if (response == GTK_RESPONSE_CLOSE)
1245 gtk_widget_destroy (widget);
1249 accounts_dialog_destroy_cb (GtkObject *obj,
1250 EmpathyAccountsDialog *dialog)
1254 g_object_unref (dialog);
1258 accounts_dialog_set_selected_account (EmpathyAccountsDialog *dialog,
1259 EmpathyAccount *account)
1261 GtkTreeSelection *selection;
1263 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1265 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
1266 if (accounts_dialog_get_account_iter (dialog, account, &iter))
1267 gtk_tree_selection_select_iter (selection, &iter);
1271 accounts_dialog_cms_ready_cb (EmpathyConnectionManagers *cms,
1273 EmpathyAccountsDialog *dialog)
1275 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1277 if (empathy_connection_managers_is_ready (cms))
1279 accounts_dialog_update_settings (dialog, NULL);
1281 if (priv->initial_selection != NULL)
1283 accounts_dialog_set_selected_account
1284 (dialog, priv->initial_selection);
1285 g_object_unref (priv->initial_selection);
1286 priv->initial_selection = NULL;
1292 accounts_dialog_build_ui (EmpathyAccountsDialog *dialog)
1296 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1298 filename = empathy_file_lookup ("empathy-accounts-dialog.ui", "src");
1300 gui = empathy_builder_get_file (filename,
1301 "accounts_dialog", &priv->window,
1302 "vbox_details", &priv->vbox_details,
1303 "frame_no_protocol", &priv->frame_no_protocol,
1304 "alignment_settings", &priv->alignment_settings,
1305 "treeview", &priv->treeview,
1306 "frame_new_account", &priv->frame_new_account,
1307 "hbox_type", &priv->hbox_type,
1308 "button_create", &priv->button_create,
1309 "button_back", &priv->button_back,
1310 "radiobutton_reuse", &priv->radiobutton_reuse,
1311 "radiobutton_register", &priv->radiobutton_register,
1312 "image_type", &priv->image_type,
1313 "label_name", &priv->label_name,
1314 "button_add", &priv->button_add,
1318 empathy_builder_connect (gui, dialog,
1319 "accounts_dialog", "response", accounts_dialog_response_cb,
1320 "accounts_dialog", "destroy", accounts_dialog_destroy_cb,
1321 "button_create", "clicked", accounts_dialog_button_create_clicked_cb,
1322 "button_back", "clicked", accounts_dialog_button_back_clicked_cb,
1323 "button_add", "clicked", accounts_dialog_button_add_clicked_cb,
1324 "button_help", "clicked", accounts_dialog_button_help_clicked_cb,
1327 g_object_unref (gui);
1329 priv->combobox_protocol = empathy_protocol_chooser_new ();
1330 gtk_box_pack_start (GTK_BOX (priv->hbox_type),
1331 priv->combobox_protocol,
1333 gtk_widget_show (priv->combobox_protocol);
1334 g_signal_connect (priv->combobox_protocol, "changed",
1335 G_CALLBACK (accounts_dialog_protocol_changed_cb),
1338 if (priv->parent_window)
1339 gtk_window_set_transient_for (GTK_WINDOW (priv->window),
1340 priv->parent_window);
1344 do_dispose (GObject *obj)
1346 EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (obj);
1347 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1349 /* Disconnect signals */
1350 g_signal_handlers_disconnect_by_func (priv->account_manager,
1351 accounts_dialog_account_added_cb,
1353 g_signal_handlers_disconnect_by_func (priv->account_manager,
1354 accounts_dialog_account_removed_cb,
1356 g_signal_handlers_disconnect_by_func (priv->account_manager,
1357 accounts_dialog_account_enabled_cb,
1359 g_signal_handlers_disconnect_by_func (priv->account_manager,
1360 accounts_dialog_account_disabled_cb,
1362 g_signal_handlers_disconnect_by_func (priv->account_manager,
1363 accounts_dialog_account_changed_cb,
1365 g_signal_handlers_disconnect_by_func (priv->account_manager,
1366 accounts_dialog_connection_changed_cb,
1369 if (priv->connecting_id)
1370 g_source_remove (priv->connecting_id);
1372 if (priv->account_manager != NULL)
1374 g_object_unref (priv->account_manager);
1375 priv->account_manager = NULL;
1378 if (priv->cms != NULL)
1380 g_object_unref (priv->cms);
1384 if (priv->initial_selection != NULL)
1385 g_object_unref (priv->initial_selection);
1386 priv->initial_selection = NULL;
1388 G_OBJECT_CLASS (empathy_accounts_dialog_parent_class)->dispose (obj);
1392 do_constructor (GType type,
1394 GObjectConstructParam *props)
1398 if (dialog_singleton)
1400 retval = G_OBJECT (dialog_singleton);
1401 g_object_ref (retval);
1406 G_OBJECT_CLASS (empathy_accounts_dialog_parent_class)->constructor
1407 (type, n_props, props);
1409 dialog_singleton = EMPATHY_ACCOUNTS_DIALOG (retval);
1410 g_object_add_weak_pointer (retval, (gpointer) &dialog_singleton);
1417 do_get_property (GObject *object,
1422 EmpathyAccountsDialogPriv *priv = GET_PRIV (object);
1424 switch (property_id)
1427 g_value_set_object (value, priv->parent_window);
1430 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1435 do_set_property (GObject *object,
1437 const GValue *value,
1440 EmpathyAccountsDialogPriv *priv = GET_PRIV (object);
1442 switch (property_id)
1445 priv->parent_window = g_value_get_object (value);
1448 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1453 do_constructed (GObject *object)
1455 EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (object);
1456 EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
1457 GList *accounts, *l;
1458 gboolean import_asked;
1460 accounts_dialog_build_ui (dialog);
1462 /* Set up signalling */
1463 priv->account_manager = empathy_account_manager_dup_singleton ();
1465 g_signal_connect (priv->account_manager, "account-created",
1466 G_CALLBACK (accounts_dialog_account_added_cb),
1468 g_signal_connect (priv->account_manager, "account-deleted",
1469 G_CALLBACK (accounts_dialog_account_removed_cb),
1471 g_signal_connect (priv->account_manager, "account-enabled",
1472 G_CALLBACK (accounts_dialog_account_enabled_cb),
1474 g_signal_connect (priv->account_manager, "account-disabled",
1475 G_CALLBACK (accounts_dialog_account_disabled_cb),
1477 g_signal_connect (priv->account_manager, "account-changed",
1478 G_CALLBACK (accounts_dialog_account_changed_cb),
1480 g_signal_connect (priv->account_manager, "account-connection-changed",
1481 G_CALLBACK (accounts_dialog_connection_changed_cb),
1484 accounts_dialog_model_setup (dialog);
1486 /* Add existing accounts */
1487 accounts = empathy_account_manager_dup_accounts (priv->account_manager);
1488 for (l = accounts; l; l = l->next)
1490 accounts_dialog_add_account (dialog, l->data);
1491 g_object_unref (l->data);
1493 g_list_free (accounts);
1495 priv->cms = empathy_connection_managers_dup_singleton ();
1496 if (!empathy_connection_managers_is_ready (priv->cms))
1497 g_signal_connect (priv->cms, "notify::ready",
1498 G_CALLBACK (accounts_dialog_cms_ready_cb), dialog);
1500 accounts_dialog_model_select_first (dialog);
1502 empathy_conf_get_bool (empathy_conf_get (),
1503 EMPATHY_PREFS_IMPORT_ASKED, &import_asked);
1506 if (empathy_import_accounts_to_import ())
1511 GtkWidget *import_dialog;
1513 empathy_conf_set_bool (empathy_conf_get (),
1514 EMPATHY_PREFS_IMPORT_ASKED, TRUE);
1515 import_dialog = empathy_import_dialog_new (GTK_WINDOW (priv->window),
1517 gtk_widget_show (import_dialog);
1523 empathy_accounts_dialog_class_init (EmpathyAccountsDialogClass *klass)
1525 GObjectClass *oclass = G_OBJECT_CLASS (klass);
1526 GParamSpec *param_spec;
1528 oclass->constructor = do_constructor;
1529 oclass->dispose = do_dispose;
1530 oclass->constructed = do_constructed;
1531 oclass->set_property = do_set_property;
1532 oclass->get_property = do_get_property;
1534 param_spec = g_param_spec_object ("parent",
1535 "parent", "The parent window",
1537 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
1538 g_object_class_install_property (oclass, PROP_PARENT, param_spec);
1540 g_type_class_add_private (klass, sizeof (EmpathyAccountsDialogPriv));
1544 empathy_accounts_dialog_init (EmpathyAccountsDialog *dialog)
1546 EmpathyAccountsDialogPriv *priv;
1548 priv = G_TYPE_INSTANCE_GET_PRIVATE ((dialog),
1549 EMPATHY_TYPE_ACCOUNTS_DIALOG,
1550 EmpathyAccountsDialogPriv);
1551 dialog->priv = priv;
1554 /* public methods */
1557 empathy_accounts_dialog_show (GtkWindow *parent,
1558 EmpathyAccount *selected_account)
1560 EmpathyAccountsDialog *dialog;
1561 EmpathyAccountsDialogPriv *priv;
1563 dialog = g_object_new (EMPATHY_TYPE_ACCOUNTS_DIALOG,
1564 "parent", parent, NULL);
1566 priv = GET_PRIV (dialog);
1568 if (selected_account)
1570 if (empathy_connection_managers_is_ready (priv->cms))
1571 accounts_dialog_set_selected_account (dialog, selected_account);
1573 /* save the selection to set it later when the cms
1576 priv->initial_selection = g_object_ref (selected_account);
1579 gtk_window_present (GTK_WINDOW (priv->window));
1581 return priv->window;