Updated for release
[empathy-mirror.git] / libempathy-gtk / empathy-accounts-dialog.c
blobf171664b35f3678fe1160fb2b67917f6b807c1e8
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3 * Copyright (C) 2005-2007 Imendio AB
4 * Copyright (C) 2007 Collabora Ltd.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public
17 * License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
21 * Authors: Martyn Russell <martyn@imendio.com>
22 * Xavier Claessens <xclaesse@gmail.com>
25 #include <config.h>
27 #include <string.h>
28 #include <stdlib.h>
30 #include <gtk/gtk.h>
31 #include <glade/glade.h>
32 #include <glib/gi18n.h>
33 #include <dbus/dbus-glib.h>
35 #include <libmissioncontrol/mc-account.h>
36 #include <libmissioncontrol/mc-profile.h>
37 #include <libmissioncontrol/mission-control.h>
38 #include <libmissioncontrol/mc-account-monitor.h>
39 #include <libtelepathy/tp-constants.h>
41 #include <libempathy/empathy-debug.h>
42 #include <libempathy/empathy-utils.h>
43 #include <libempathy-gtk/empathy-ui-utils.h>
45 #include "empathy-accounts-dialog.h"
46 #include "empathy-profile-chooser.h"
47 #include "empathy-account-widget-generic.h"
48 #include "empathy-account-widget-jabber.h"
49 #include "empathy-account-widget-msn.h"
50 #include "empathy-account-widget-salut.h"
52 #define DEBUG_DOMAIN "AccountDialog"
54 /* Flashing delay for icons (milliseconds). */
55 #define FLASH_TIMEOUT 500
57 typedef struct {
58 GtkWidget *window;
60 GtkWidget *alignment_settings;
62 GtkWidget *vbox_details;
63 GtkWidget *frame_no_account;
64 GtkWidget *label_no_account;
65 GtkWidget *label_no_account_blurb;
67 GtkWidget *treeview;
69 GtkWidget *button_add;
70 GtkWidget *button_remove;
71 GtkWidget *button_connect;
73 GtkWidget *frame_new_account;
74 GtkWidget *combobox_profile;
75 GtkWidget *hbox_type;
76 GtkWidget *button_create;
77 GtkWidget *button_back;
79 GtkWidget *image_type;
80 GtkWidget *label_name;
81 GtkWidget *label_type;
82 GtkWidget *settings_widget;
84 gboolean connecting_show;
85 guint connecting_id;
86 gboolean account_changed;
88 MissionControl *mc;
89 McAccountMonitor *monitor;
90 } EmpathyAccountsDialog;
92 enum {
93 COL_NAME,
94 COL_STATUS,
95 COL_ACCOUNT_POINTER,
96 COL_COUNT
99 static void accounts_dialog_setup (EmpathyAccountsDialog *dialog);
100 static void accounts_dialog_update_account (EmpathyAccountsDialog *dialog,
101 McAccount *account);
102 static void accounts_dialog_model_setup (EmpathyAccountsDialog *dialog);
103 static void accounts_dialog_model_add_columns (EmpathyAccountsDialog *dialog);
104 static void accounts_dialog_model_select_first (EmpathyAccountsDialog *dialog);
105 static void accounts_dialog_model_pixbuf_data_func (GtkTreeViewColumn *tree_column,
106 GtkCellRenderer *cell,
107 GtkTreeModel *model,
108 GtkTreeIter *iter,
109 EmpathyAccountsDialog *dialog);
110 static McAccount *accounts_dialog_model_get_selected (EmpathyAccountsDialog *dialog);
111 static void accounts_dialog_model_set_selected (EmpathyAccountsDialog *dialog,
112 McAccount *account);
113 static gboolean accounts_dialog_model_remove_selected (EmpathyAccountsDialog *dialog);
114 static void accounts_dialog_model_selection_changed (GtkTreeSelection *selection,
115 EmpathyAccountsDialog *dialog);
116 static void accounts_dialog_add_account (EmpathyAccountsDialog *dialog,
117 McAccount *account);
118 static void accounts_dialog_account_added_cb (McAccountMonitor *monitor,
119 gchar *unique_name,
120 EmpathyAccountsDialog *dialog);
121 static void accounts_dialog_account_removed_cb (McAccountMonitor *monitor,
122 gchar *unique_name,
123 EmpathyAccountsDialog *dialog);
124 static gboolean accounts_dialog_row_changed_foreach (GtkTreeModel *model,
125 GtkTreePath *path,
126 GtkTreeIter *iter,
127 gpointer user_data);
128 static gboolean accounts_dialog_flash_connecting_cb (EmpathyAccountsDialog *dialog);
129 static void accounts_dialog_status_changed_cb (MissionControl *mc,
130 TelepathyConnectionStatus status,
131 McPresence presence,
132 TelepathyConnectionStatusReason reason,
133 const gchar *unique_name,
134 EmpathyAccountsDialog *dialog);
135 static void accounts_dialog_button_create_clicked_cb (GtkWidget *button,
136 EmpathyAccountsDialog *dialog);
137 static void accounts_dialog_button_back_clicked_cb (GtkWidget *button,
138 EmpathyAccountsDialog *dialog);
139 static void accounts_dialog_button_connect_clicked_cb (GtkWidget *button,
140 EmpathyAccountsDialog *dialog);
141 static void accounts_dialog_button_add_clicked_cb (GtkWidget *button,
142 EmpathyAccountsDialog *dialog);
143 static void accounts_dialog_remove_response_cb (GtkWidget *dialog,
144 gint response,
145 McAccount *account);
146 static void accounts_dialog_button_remove_clicked_cb (GtkWidget *button,
147 EmpathyAccountsDialog *dialog);
148 static void accounts_dialog_treeview_row_activated_cb (GtkTreeView *tree_view,
149 GtkTreePath *path,
150 GtkTreeViewColumn *column,
151 EmpathyAccountsDialog *dialog);
152 static void accounts_dialog_response_cb (GtkWidget *widget,
153 gint response,
154 EmpathyAccountsDialog *dialog);
155 static void accounts_dialog_destroy_cb (GtkWidget *widget,
156 EmpathyAccountsDialog *dialog);
158 static void
159 accounts_dialog_setup (EmpathyAccountsDialog *dialog)
161 GtkTreeView *view;
162 GtkListStore *store;
163 GtkTreeIter iter;
164 GList *accounts, *l;
166 view = GTK_TREE_VIEW (dialog->treeview);
167 store = GTK_LIST_STORE (gtk_tree_view_get_model (view));
169 accounts = mc_accounts_list ();
171 for (l = accounts; l; l = l->next) {
172 McAccount *account;
173 const gchar *name;
174 TelepathyConnectionStatus status;
176 account = l->data;
178 name = mc_account_get_display_name (account);
179 if (!name) {
180 continue;
183 status = mission_control_get_connection_status (dialog->mc, account, NULL);
185 gtk_list_store_insert_with_values (store, &iter,
187 COL_NAME, name,
188 COL_STATUS, status,
189 COL_ACCOUNT_POINTER, account,
190 -1);
192 accounts_dialog_status_changed_cb (dialog->mc,
193 status,
194 MC_PRESENCE_UNSET,
195 TP_CONN_STATUS_REASON_NONE_SPECIFIED,
196 mc_account_get_unique_name (account),
197 dialog);
199 g_object_unref (account);
202 g_list_free (accounts);
205 static void
206 accounts_dialog_update_connect_button (EmpathyAccountsDialog *dialog)
208 McAccount *account;
209 GtkWidget *image;
210 const gchar *stock_id;
211 const gchar *label;
213 account = accounts_dialog_model_get_selected (dialog);
215 gtk_widget_set_sensitive (dialog->button_connect, account != NULL);
217 if (account && mc_account_is_enabled (account)) {
218 label = _("Disable");
219 stock_id = GTK_STOCK_DISCONNECT;
220 } else {
221 label = _("Enable");
222 stock_id = GTK_STOCK_CONNECT;
225 image = gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON);
227 gtk_button_set_label (GTK_BUTTON (dialog->button_connect), label);
228 gtk_button_set_image (GTK_BUTTON (dialog->button_connect), image);
231 static void
232 accounts_dialog_update_account (EmpathyAccountsDialog *dialog,
233 McAccount *account)
235 if (dialog->settings_widget) {
236 gtk_widget_destroy (dialog->settings_widget);
237 dialog->settings_widget = NULL;
240 if (!account) {
241 GtkTreeView *view;
242 GtkTreeModel *model;
243 GString *string;
244 gchar *str;
246 gtk_widget_show (dialog->frame_no_account);
247 gtk_widget_hide (dialog->vbox_details);
249 gtk_widget_set_sensitive (dialog->button_connect, FALSE);
250 gtk_widget_set_sensitive (dialog->button_remove, FALSE);
252 view = GTK_TREE_VIEW (dialog->treeview);
253 model = gtk_tree_view_get_model (view);
255 if (empathy_profile_chooser_n_profiles (dialog->combobox_profile) > 0) {
256 string = g_string_new (_("To add a new account, you can click on the "
257 "'Add' button and a new entry will be created "
258 "for you to start configuring."));
259 } else {
260 string = g_string_new (_("To add a new account, you first have to "
261 "install a backend for each protocol "
262 "you want to use."));
265 if (gtk_tree_model_iter_n_children (model, NULL) > 0) {
266 gtk_label_set_markup (GTK_LABEL (dialog->label_no_account),
267 _("<b>No Account Selected</b>"));
268 g_string_append (string, _("\n\n"
269 "If you do not want to add an account, simply "
270 "click on the account you want to configure in "
271 "the list on the left."));
272 } else {
273 gtk_label_set_markup (GTK_LABEL (dialog->label_no_account),
274 _("<b>No Accounts Configured</b>"));
277 str = g_string_free (string, FALSE);
278 gtk_label_set_markup (GTK_LABEL (dialog->label_no_account_blurb),
279 str);
280 g_free (str);
281 } else {
282 McProfile *profile;
283 const gchar *config_ui;
285 gtk_widget_hide (dialog->frame_no_account);
286 gtk_widget_show (dialog->vbox_details);
288 profile = mc_account_get_profile (account);
289 config_ui = mc_profile_get_configuration_ui (profile);
290 g_object_unref (profile);
292 if (!empathy_strdiff (config_ui, "jabber")) {
293 dialog->settings_widget =
294 empathy_account_widget_jabber_new (account);
296 else if (!empathy_strdiff (config_ui, "msn")) {
297 dialog ->settings_widget =
298 empathy_account_widget_msn_new (account);
300 else if (!empathy_strdiff (config_ui, "local-xmpp")) {
301 dialog->settings_widget =
302 empathy_account_widget_salut_new (account);
304 else {
305 dialog->settings_widget =
306 empathy_account_widget_generic_new (account);
309 gtk_widget_grab_focus (dialog->settings_widget);
312 if (dialog->settings_widget) {
313 gtk_container_add (GTK_CONTAINER (dialog->alignment_settings),
314 dialog->settings_widget);
317 if (account) {
318 McProfile *profile;
319 gchar *text;
321 profile = mc_account_get_profile (account);
322 gtk_image_set_from_icon_name (GTK_IMAGE (dialog->image_type),
323 mc_profile_get_icon_name (profile),
324 GTK_ICON_SIZE_DIALOG);
325 gtk_widget_set_tooltip_text (dialog->image_type,
326 mc_profile_get_display_name (profile));
328 text = g_strdup_printf ("<big><b>%s</b></big>", mc_account_get_display_name (account));
329 gtk_label_set_markup (GTK_LABEL (dialog->label_name), text);
330 g_free (text);
334 static void
335 accounts_dialog_model_setup (EmpathyAccountsDialog *dialog)
337 GtkListStore *store;
338 GtkTreeSelection *selection;
340 store = gtk_list_store_new (COL_COUNT,
341 G_TYPE_STRING, /* name */
342 G_TYPE_UINT, /* status */
343 MC_TYPE_ACCOUNT); /* account */
345 gtk_tree_view_set_model (GTK_TREE_VIEW (dialog->treeview),
346 GTK_TREE_MODEL (store));
348 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (dialog->treeview));
349 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
351 g_signal_connect (selection, "changed",
352 G_CALLBACK (accounts_dialog_model_selection_changed),
353 dialog);
355 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
356 COL_NAME, GTK_SORT_ASCENDING);
358 accounts_dialog_model_add_columns (dialog);
360 g_object_unref (store);
363 static void
364 accounts_dialog_name_edited_cb (GtkCellRendererText *renderer,
365 gchar *path,
366 gchar *new_text,
367 EmpathyAccountsDialog *dialog)
369 McAccount *account;
370 GtkTreeModel *model;
371 GtkTreePath *treepath;
372 GtkTreeIter iter;
374 model = gtk_tree_view_get_model (GTK_TREE_VIEW (dialog->treeview));
375 treepath = gtk_tree_path_new_from_string (path);
376 gtk_tree_model_get_iter (model, &iter, treepath);
377 gtk_tree_model_get (model, &iter,
378 COL_ACCOUNT_POINTER, &account,
379 -1);
380 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
381 COL_NAME, new_text,
382 -1);
383 gtk_tree_path_free (treepath);
385 mc_account_set_display_name (account, new_text);
386 g_object_unref (account);
389 static void
390 accounts_dialog_model_add_columns (EmpathyAccountsDialog *dialog)
392 GtkTreeView *view;
393 GtkTreeViewColumn *column;
394 GtkCellRenderer *cell;
396 view = GTK_TREE_VIEW (dialog->treeview);
397 gtk_tree_view_set_headers_visible (view, TRUE);
399 /* account name/status */
400 column = gtk_tree_view_column_new ();
401 gtk_tree_view_column_set_title (column, _("Accounts"));
403 cell = gtk_cell_renderer_pixbuf_new ();
404 gtk_tree_view_column_pack_start (column, cell, FALSE);
405 gtk_tree_view_column_set_cell_data_func (column, cell,
406 (GtkTreeCellDataFunc)
407 accounts_dialog_model_pixbuf_data_func,
408 dialog,
409 NULL);
411 cell = gtk_cell_renderer_text_new ();
412 g_object_set (cell,
413 "ellipsize", PANGO_ELLIPSIZE_END,
414 "editable", TRUE,
415 NULL);
416 gtk_tree_view_column_pack_start (column, cell, TRUE);
417 gtk_tree_view_column_add_attribute (column,
418 cell,
419 "text", COL_NAME);
420 g_signal_connect (cell, "edited",
421 G_CALLBACK (accounts_dialog_name_edited_cb),
422 dialog);
424 gtk_tree_view_column_set_expand (column, TRUE);
425 gtk_tree_view_append_column (view, column);
428 static void
429 accounts_dialog_model_select_first (EmpathyAccountsDialog *dialog)
431 GtkTreeView *view;
432 GtkTreeModel *model;
433 GtkTreeSelection *selection;
434 GtkTreeIter iter;
436 /* select first */
437 view = GTK_TREE_VIEW (dialog->treeview);
438 model = gtk_tree_view_get_model (view);
440 if (gtk_tree_model_get_iter_first (model, &iter)) {
441 selection = gtk_tree_view_get_selection (view);
442 gtk_tree_selection_select_iter (selection, &iter);
443 } else {
444 accounts_dialog_update_account (dialog, NULL);
448 static void
449 accounts_dialog_model_pixbuf_data_func (GtkTreeViewColumn *tree_column,
450 GtkCellRenderer *cell,
451 GtkTreeModel *model,
452 GtkTreeIter *iter,
453 EmpathyAccountsDialog *dialog)
455 McAccount *account;
456 const gchar *icon_name;
457 GdkPixbuf *pixbuf;
458 TelepathyConnectionStatus status;
460 gtk_tree_model_get (model, iter,
461 COL_STATUS, &status,
462 COL_ACCOUNT_POINTER, &account,
463 -1);
465 icon_name = empathy_icon_name_from_account (account);
466 pixbuf = empathy_pixbuf_from_icon_name (icon_name, GTK_ICON_SIZE_BUTTON);
468 if (pixbuf) {
469 if (status == TP_CONN_STATUS_DISCONNECTED ||
470 (status == TP_CONN_STATUS_CONNECTING &&
471 !dialog->connecting_show)) {
472 GdkPixbuf *modded_pixbuf;
474 modded_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
475 TRUE,
477 gdk_pixbuf_get_width (pixbuf),
478 gdk_pixbuf_get_height (pixbuf));
480 gdk_pixbuf_saturate_and_pixelate (pixbuf,
481 modded_pixbuf,
482 1.0,
483 TRUE);
484 g_object_unref (pixbuf);
485 pixbuf = modded_pixbuf;
489 g_object_set (cell,
490 "visible", TRUE,
491 "pixbuf", pixbuf,
492 NULL);
494 g_object_unref (account);
495 if (pixbuf) {
496 g_object_unref (pixbuf);
500 static McAccount *
501 accounts_dialog_model_get_selected (EmpathyAccountsDialog *dialog)
503 GtkTreeView *view;
504 GtkTreeModel *model;
505 GtkTreeSelection *selection;
506 GtkTreeIter iter;
507 McAccount *account;
509 view = GTK_TREE_VIEW (dialog->treeview);
510 selection = gtk_tree_view_get_selection (view);
512 if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
513 return NULL;
516 gtk_tree_model_get (model, &iter, COL_ACCOUNT_POINTER, &account, -1);
518 return account;
521 static void
522 accounts_dialog_model_set_selected (EmpathyAccountsDialog *dialog,
523 McAccount *account)
525 GtkTreeView *view;
526 GtkTreeSelection *selection;
527 GtkTreeModel *model;
528 GtkTreeIter iter;
529 gboolean ok;
531 view = GTK_TREE_VIEW (dialog->treeview);
532 model = gtk_tree_view_get_model (view);
533 selection = gtk_tree_view_get_selection (view);
535 for (ok = gtk_tree_model_get_iter_first (model, &iter);
537 ok = gtk_tree_model_iter_next (model, &iter)) {
538 McAccount *this_account;
539 gboolean equal;
541 gtk_tree_model_get (model, &iter,
542 COL_ACCOUNT_POINTER, &this_account,
543 -1);
545 equal = empathy_account_equal (this_account, account);
546 g_object_unref (this_account);
548 if (equal) {
549 gtk_tree_selection_select_iter (selection, &iter);
550 break;
555 static gboolean
556 accounts_dialog_model_remove_selected (EmpathyAccountsDialog *dialog)
558 GtkTreeView *view;
559 GtkTreeModel *model;
560 GtkTreeSelection *selection;
561 GtkTreeIter iter;
563 view = GTK_TREE_VIEW (dialog->treeview);
564 selection = gtk_tree_view_get_selection (view);
566 if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
567 return FALSE;
570 return gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
573 static void
574 accounts_dialog_model_selection_changed (GtkTreeSelection *selection,
575 EmpathyAccountsDialog *dialog)
577 McAccount *account;
578 GtkTreeModel *model;
579 GtkTreeIter iter;
580 gboolean is_selection;
582 is_selection = gtk_tree_selection_get_selected (selection, &model, &iter);
584 gtk_widget_set_sensitive (dialog->button_add, TRUE);
585 gtk_widget_set_sensitive (dialog->button_remove, is_selection);
586 gtk_widget_set_sensitive (dialog->button_connect, is_selection);
588 accounts_dialog_update_connect_button (dialog);
590 account = accounts_dialog_model_get_selected (dialog);
591 accounts_dialog_update_account (dialog, account);
593 if (account) {
594 g_object_unref (account);
597 /* insure new account frame is hidden when a row is selected*/
598 gtk_widget_hide (dialog->frame_new_account);
601 static void
602 accounts_dialog_add_account (EmpathyAccountsDialog *dialog,
603 McAccount *account)
605 TelepathyConnectionStatus status;
606 const gchar *name;
607 GtkTreeView *view;
608 GtkTreeModel *model;
609 GtkListStore *store;
610 GtkTreeIter iter;
611 gboolean ok;
613 view = GTK_TREE_VIEW (dialog->treeview);
614 model = gtk_tree_view_get_model (view);
615 store = GTK_LIST_STORE (model);
617 for (ok = gtk_tree_model_get_iter_first (model, &iter);
619 ok = gtk_tree_model_iter_next (model, &iter)) {
620 McAccount *this_account;
621 gboolean equal;
623 gtk_tree_model_get (model, &iter,
624 COL_ACCOUNT_POINTER, &this_account,
625 -1);
627 equal = empathy_account_equal (this_account, account);
628 g_object_unref (this_account);
630 if (equal) {
631 return;
635 status = mission_control_get_connection_status (dialog->mc, account, NULL);
636 name = mc_account_get_display_name (account);
638 g_return_if_fail (name != NULL);
640 empathy_debug (DEBUG_DOMAIN, "Adding new account: %s", name);
642 gtk_list_store_insert_with_values (store, &iter,
644 COL_NAME, name,
645 COL_STATUS, status,
646 COL_ACCOUNT_POINTER, account,
647 -1);
650 static void
651 accounts_dialog_account_added_cb (McAccountMonitor *monitor,
652 gchar *unique_name,
653 EmpathyAccountsDialog *dialog)
655 McAccount *account;
657 account = mc_account_lookup (unique_name);
658 accounts_dialog_add_account (dialog, account);
659 g_object_unref (account);
662 static void
663 accounts_dialog_account_removed_cb (McAccountMonitor *monitor,
664 gchar *unique_name,
665 EmpathyAccountsDialog *dialog)
667 McAccount *account;
669 account = mc_account_lookup (unique_name);
671 accounts_dialog_model_set_selected (dialog, account);
672 accounts_dialog_model_remove_selected (dialog);
674 g_object_unref (account);
677 static gboolean
678 accounts_dialog_row_changed_foreach (GtkTreeModel *model,
679 GtkTreePath *path,
680 GtkTreeIter *iter,
681 gpointer user_data)
683 gtk_tree_model_row_changed (model, path, iter);
685 return FALSE;
688 static gboolean
689 accounts_dialog_flash_connecting_cb (EmpathyAccountsDialog *dialog)
691 GtkTreeView *view;
692 GtkTreeModel *model;
694 dialog->connecting_show = !dialog->connecting_show;
696 view = GTK_TREE_VIEW (dialog->treeview);
697 model = gtk_tree_view_get_model (view);
699 gtk_tree_model_foreach (model, accounts_dialog_row_changed_foreach, NULL);
701 return TRUE;
704 static void
705 accounts_dialog_status_changed_cb (MissionControl *mc,
706 TelepathyConnectionStatus status,
707 McPresence presence,
708 TelepathyConnectionStatusReason reason,
709 const gchar *unique_name,
710 EmpathyAccountsDialog *dialog)
712 GtkTreeView *view;
713 GtkTreeSelection *selection;
714 GtkTreeModel *model;
715 GtkTreeIter iter;
716 gboolean ok;
717 McAccount *account;
718 GList *accounts, *l;
719 gboolean found = FALSE;
721 /* Update the status in the model */
722 view = GTK_TREE_VIEW (dialog->treeview);
723 selection = gtk_tree_view_get_selection (view);
724 model = gtk_tree_view_get_model (view);
725 account = mc_account_lookup (unique_name);
727 empathy_debug (DEBUG_DOMAIN, "Status changed for account %s: "
728 "status=%d presence=%d",
729 unique_name, status, presence);
731 for (ok = gtk_tree_model_get_iter_first (model, &iter);
733 ok = gtk_tree_model_iter_next (model, &iter)) {
734 McAccount *this_account;
735 gboolean equal;
737 gtk_tree_model_get (model, &iter,
738 COL_ACCOUNT_POINTER, &this_account,
739 -1);
741 equal = empathy_account_equal (this_account, account);
742 g_object_unref (this_account);
744 if (equal) {
745 GtkTreePath *path;
747 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
748 COL_STATUS, status,
749 -1);
751 path = gtk_tree_model_get_path (model, &iter);
752 gtk_tree_model_row_changed (model, path, &iter);
753 gtk_tree_path_free (path);
755 break;
758 g_object_unref (account);
760 /* Check if there is still accounts in CONNECTING state */
761 accounts = mc_accounts_list ();
762 for (l = accounts; l; l = l->next) {
763 McAccount *this_account;
764 TelepathyConnectionStatus status;
766 this_account = l->data;
768 status = mission_control_get_connection_status (mc, this_account, NULL);
769 if (status == TP_CONN_STATUS_CONNECTING) {
770 found = TRUE;
771 break;
774 g_object_unref (this_account);
776 g_list_free (accounts);
778 if (!found && dialog->connecting_id) {
779 g_source_remove (dialog->connecting_id);
780 dialog->connecting_id = 0;
782 if (found && !dialog->connecting_id) {
783 dialog->connecting_id = g_timeout_add (FLASH_TIMEOUT,
784 (GSourceFunc) accounts_dialog_flash_connecting_cb,
785 dialog);
789 static void
790 accounts_dialog_button_create_clicked_cb (GtkWidget *button,
791 EmpathyAccountsDialog *dialog)
793 McProfile *profile;
794 McAccount *account;
795 const gchar *str;
797 /* Update widgets */
798 gtk_widget_show (dialog->vbox_details);
799 gtk_widget_hide (dialog->frame_no_account);
800 gtk_widget_hide (dialog->frame_new_account);
802 profile = empathy_profile_chooser_get_selected (dialog->combobox_profile);
804 /* Create account */
805 account = mc_account_create (profile);
807 str = mc_account_get_unique_name (account);
808 mc_account_set_display_name (account, str);
810 accounts_dialog_add_account (dialog, account);
811 accounts_dialog_model_set_selected (dialog, account);
813 g_object_unref (account);
814 g_object_unref (profile);
817 static void
818 accounts_dialog_button_back_clicked_cb (GtkWidget *button,
819 EmpathyAccountsDialog *dialog)
821 McAccount *account;
823 gtk_widget_hide (dialog->vbox_details);
824 gtk_widget_hide (dialog->frame_no_account);
825 gtk_widget_hide (dialog->frame_new_account);
827 gtk_widget_set_sensitive (dialog->button_add, TRUE);
829 account = accounts_dialog_model_get_selected (dialog);
830 accounts_dialog_update_account (dialog, account);
833 static void
834 accounts_dialog_button_connect_clicked_cb (GtkWidget *button,
835 EmpathyAccountsDialog *dialog)
837 McAccount *account;
838 gboolean enable;
840 account = accounts_dialog_model_get_selected (dialog);
841 enable = (!mc_account_is_enabled (account));
842 mc_account_set_enabled (account, enable);
843 accounts_dialog_update_connect_button (dialog);
845 g_object_unref (account);
848 static void
849 accounts_dialog_button_add_clicked_cb (GtkWidget *button,
850 EmpathyAccountsDialog *dialog)
852 GtkTreeView *view;
853 GtkTreeSelection *selection;
855 view = GTK_TREE_VIEW (dialog->treeview);
856 selection = gtk_tree_view_get_selection (view);
857 gtk_tree_selection_unselect_all (selection);
859 gtk_widget_set_sensitive (dialog->button_add, FALSE);
860 gtk_widget_hide (dialog->vbox_details);
861 gtk_widget_hide (dialog->frame_no_account);
862 gtk_widget_show (dialog->frame_new_account);
864 gtk_combo_box_set_active (GTK_COMBO_BOX (dialog->combobox_profile), 0);
865 gtk_widget_grab_focus (dialog->combobox_profile);
868 static void
869 accounts_dialog_remove_response_cb (GtkWidget *dialog,
870 gint response,
871 McAccount *account)
873 if (response == GTK_RESPONSE_YES) {
874 mc_account_delete (account);
877 gtk_widget_destroy (dialog);
880 static void
881 accounts_dialog_button_remove_clicked_cb (GtkWidget *button,
882 EmpathyAccountsDialog *dialog)
884 McAccount *account;
885 GtkWidget *message_dialog;
887 account = accounts_dialog_model_get_selected (dialog);
889 if (!mc_account_is_complete (account)) {
890 accounts_dialog_model_remove_selected (dialog);
891 return;
893 message_dialog = gtk_message_dialog_new
894 (GTK_WINDOW (dialog->window),
895 GTK_DIALOG_DESTROY_WITH_PARENT,
896 GTK_MESSAGE_QUESTION,
897 GTK_BUTTONS_NONE,
898 _("You are about to remove your %s account!\n"
899 "Are you sure you want to proceed?"),
900 mc_account_get_display_name (account));
902 gtk_message_dialog_format_secondary_text
903 (GTK_MESSAGE_DIALOG (message_dialog),
904 _("Any associated conversations and chat rooms will NOT be "
905 "removed if you decide to proceed.\n"
906 "\n"
907 "Should you decide to add the account back at a later time, "
908 "they will still be available."));
910 gtk_dialog_add_button (GTK_DIALOG (message_dialog),
911 GTK_STOCK_CANCEL,
912 GTK_RESPONSE_NO);
913 gtk_dialog_add_button (GTK_DIALOG (message_dialog),
914 GTK_STOCK_REMOVE,
915 GTK_RESPONSE_YES);
917 g_signal_connect (message_dialog, "response",
918 G_CALLBACK (accounts_dialog_remove_response_cb),
919 account);
921 gtk_widget_show (message_dialog);
924 static void
925 accounts_dialog_treeview_row_activated_cb (GtkTreeView *tree_view,
926 GtkTreePath *path,
927 GtkTreeViewColumn *column,
928 EmpathyAccountsDialog *dialog)
931 accounts_dialog_button_connect_clicked_cb (dialog->button_connect,
932 dialog);
935 static void
936 accounts_dialog_response_cb (GtkWidget *widget,
937 gint response,
938 EmpathyAccountsDialog *dialog)
940 gtk_widget_destroy (widget);
943 static void
944 accounts_dialog_destroy_cb (GtkWidget *widget,
945 EmpathyAccountsDialog *dialog)
947 GList *accounts, *l;
949 /* Disconnect signals */
950 g_signal_handlers_disconnect_by_func (dialog->monitor,
951 accounts_dialog_account_added_cb,
952 dialog);
953 g_signal_handlers_disconnect_by_func (dialog->monitor,
954 accounts_dialog_account_removed_cb,
955 dialog);
956 g_signal_handlers_disconnect_by_func (dialog->monitor,
957 accounts_dialog_update_connect_button,
958 dialog);
959 dbus_g_proxy_disconnect_signal (DBUS_G_PROXY (dialog->mc),
960 "AccountStatusChanged",
961 G_CALLBACK (accounts_dialog_status_changed_cb),
962 dialog);
964 /* Delete incomplete accounts */
965 accounts = mc_accounts_list ();
966 for (l = accounts; l; l = l->next) {
967 McAccount *account;
969 account = l->data;
970 if (!mc_account_is_complete (account)) {
971 /* FIXME: Warn the user the account is not complete
972 * and is going to be removed. */
973 mc_account_delete (account);
976 g_object_unref (account);
978 g_list_free (accounts);
980 if (dialog->connecting_id) {
981 g_source_remove (dialog->connecting_id);
984 g_object_unref (dialog->mc);
985 g_object_unref (dialog->monitor);
987 g_free (dialog);
990 GtkWidget *
991 empathy_accounts_dialog_show (GtkWindow *parent)
993 static EmpathyAccountsDialog *dialog = NULL;
994 GladeXML *glade;
995 GtkWidget *bbox;
996 GtkWidget *button_close;
998 if (dialog) {
999 gtk_window_present (GTK_WINDOW (dialog->window));
1000 return dialog->window;
1003 dialog = g_new0 (EmpathyAccountsDialog, 1);
1005 glade = empathy_glade_get_file ("empathy-accounts-dialog.glade",
1006 "accounts_dialog",
1007 NULL,
1008 "accounts_dialog", &dialog->window,
1009 "vbox_details", &dialog->vbox_details,
1010 "frame_no_account", &dialog->frame_no_account,
1011 "label_no_account", &dialog->label_no_account,
1012 "label_no_account_blurb", &dialog->label_no_account_blurb,
1013 "alignment_settings", &dialog->alignment_settings,
1014 "dialog-action_area", &bbox,
1015 "treeview", &dialog->treeview,
1016 "frame_new_account", &dialog->frame_new_account,
1017 "hbox_type", &dialog->hbox_type,
1018 "button_create", &dialog->button_create,
1019 "button_back", &dialog->button_back,
1020 "image_type", &dialog->image_type,
1021 "label_name", &dialog->label_name,
1022 "button_add", &dialog->button_add,
1023 "button_remove", &dialog->button_remove,
1024 "button_connect", &dialog->button_connect,
1025 "button_close", &button_close,
1026 NULL);
1028 empathy_glade_connect (glade,
1029 dialog,
1030 "accounts_dialog", "destroy", accounts_dialog_destroy_cb,
1031 "accounts_dialog", "response", accounts_dialog_response_cb,
1032 "button_create", "clicked", accounts_dialog_button_create_clicked_cb,
1033 "button_back", "clicked", accounts_dialog_button_back_clicked_cb,
1034 "treeview", "row-activated", accounts_dialog_treeview_row_activated_cb,
1035 "button_connect", "clicked", accounts_dialog_button_connect_clicked_cb,
1036 "button_add", "clicked", accounts_dialog_button_add_clicked_cb,
1037 "button_remove", "clicked", accounts_dialog_button_remove_clicked_cb,
1038 NULL);
1040 g_object_add_weak_pointer (G_OBJECT (dialog->window), (gpointer) &dialog);
1042 g_object_unref (glade);
1044 /* Create profile chooser */
1045 dialog->combobox_profile = empathy_profile_chooser_new ();
1046 gtk_box_pack_end (GTK_BOX (dialog->hbox_type),
1047 dialog->combobox_profile,
1048 TRUE, TRUE, 0);
1049 gtk_widget_show (dialog->combobox_profile);
1050 if (empathy_profile_chooser_n_profiles (dialog->combobox_profile) <= 0) {
1051 gtk_widget_set_sensitive (dialog->button_add, FALSE);
1054 /* Set up signalling */
1055 dialog->mc = empathy_mission_control_new ();
1056 dialog->monitor = mc_account_monitor_new ();
1058 g_signal_connect (dialog->monitor, "account-created",
1059 G_CALLBACK (accounts_dialog_account_added_cb),
1060 dialog);
1061 g_signal_connect (dialog->monitor, "account-deleted",
1062 G_CALLBACK (accounts_dialog_account_removed_cb),
1063 dialog);
1064 g_signal_connect_swapped (dialog->monitor, "account-enabled",
1065 G_CALLBACK (accounts_dialog_update_connect_button),
1066 dialog);
1067 g_signal_connect_swapped (dialog->monitor, "account-disabled",
1068 G_CALLBACK (accounts_dialog_update_connect_button),
1069 dialog);
1070 dbus_g_proxy_connect_signal (DBUS_G_PROXY (dialog->mc), "AccountStatusChanged",
1071 G_CALLBACK (accounts_dialog_status_changed_cb),
1072 dialog, NULL);
1074 accounts_dialog_model_setup (dialog);
1075 accounts_dialog_setup (dialog);
1076 accounts_dialog_model_select_first (dialog);
1078 if (parent) {
1079 gtk_window_set_transient_for (GTK_WINDOW (dialog->window),
1080 GTK_WINDOW (parent));
1083 gtk_widget_show (dialog->window);
1085 return dialog->window;