[darcs-to-svn @ Connect accounts in empathy-launcher, not in empathy-contact-list]
[empathy.git] / libempathy-gtk / gossip-accounts-dialog.c
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.
5  *
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.
10  *
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.
15  *
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.
20  * 
21  * Authors: Martyn Russell <martyn@imendio.com>
22  *          Xavier Claessens <xclaesse@gmail.com>
23  */
24
25 #include <config.h>
26
27 #include <string.h>
28 #include <stdlib.h>
29
30 #include <gtk/gtk.h>
31 #include <glade/glade.h>
32 #include <glib/gi18n.h>
33 #include <dbus/dbus-glib.h>
34
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>
40 #include <libtelepathy/tp-helpers.h>
41
42 #include <libempathy/gossip-debug.h>
43 #include <libempathy/gossip-paths.h>
44 #include <libempathy/gossip-utils.h>
45 #include <libempathy-gtk/gossip-ui-utils.h>
46
47 #include "gossip-accounts-dialog.h"
48 #include "gossip-profile-chooser.h"
49 #include "gossip-account-widget-generic.h"
50 #include "gossip-account-widget-jabber.h"
51
52 #define DEBUG_DOMAIN "AccountDialog"
53
54 /* Flashing delay for icons (milliseconds). */
55 #define FLASH_TIMEOUT 500
56
57 typedef struct {
58         GtkWidget        *window;
59
60         GtkWidget        *alignment_settings;
61
62         GtkWidget        *vbox_details;
63         GtkWidget        *frame_no_account;
64         GtkWidget        *label_no_account;
65         GtkWidget        *label_no_account_blurb;
66
67         GtkWidget        *treeview;
68
69         GtkWidget        *button_remove;
70         GtkWidget        *button_connect;
71
72         GtkWidget        *frame_new_account;
73         GtkWidget        *combobox_profile;
74         GtkWidget        *entry_name;
75         GtkWidget        *table_new_account;
76         GtkWidget        *button_create;
77         GtkWidget        *button_cancel;
78
79         GtkWidget        *image_type;
80         GtkWidget        *label_name;
81         GtkWidget        *label_type;
82         GtkWidget        *settings_widget;
83
84         gboolean          connecting_show;
85         guint             connecting_id;
86         gboolean          account_changed;
87
88         MissionControl   *mc;
89         McAccountMonitor *monitor;
90 } GossipAccountsDialog;
91
92 enum {
93         COL_NAME,
94         COL_STATUS,
95         COL_ACCOUNT_POINTER,
96         COL_COUNT
97 };
98
99 static void       accounts_dialog_setup                     (GossipAccountsDialog            *dialog);
100 static void       accounts_dialog_update_account            (GossipAccountsDialog            *dialog,
101                                                              McAccount                       *account);
102 static void       accounts_dialog_model_setup               (GossipAccountsDialog            *dialog);
103 static void       accounts_dialog_model_add_columns         (GossipAccountsDialog            *dialog);
104 static void       accounts_dialog_model_select_first        (GossipAccountsDialog            *dialog);
105 static void       accounts_dialog_model_pixbuf_data_func    (GtkTreeViewColumn               *tree_column,
106                                                              GtkCellRenderer                 *cell,
107                                                              GtkTreeModel                    *model,
108                                                              GtkTreeIter                     *iter,
109                                                              GossipAccountsDialog            *dialog);
110 static McAccount *accounts_dialog_model_get_selected        (GossipAccountsDialog            *dialog);
111 static void       accounts_dialog_model_set_selected        (GossipAccountsDialog            *dialog,
112                                                              McAccount                       *account);
113 static gboolean   accounts_dialog_model_remove_selected     (GossipAccountsDialog            *dialog);
114 static void       accounts_dialog_model_selection_changed   (GtkTreeSelection                *selection,
115                                                              GossipAccountsDialog            *dialog);
116 static void       accounts_dialog_add_account               (GossipAccountsDialog            *dialog,
117                                                              McAccount                       *account);
118 static void       accounts_dialog_account_added_cb          (McAccountMonitor                *monitor,
119                                                              gchar                           *unique_name,
120                                                              GossipAccountsDialog            *dialog);
121 static void       accounts_dialog_account_removed_cb        (McAccountMonitor                *monitor,
122                                                              gchar                           *unique_name,
123                                                              GossipAccountsDialog            *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       (GossipAccountsDialog            *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                                                              GossipAccountsDialog            *dialog);
135 static void       accounts_dialog_entry_name_changed_cb     (GtkWidget                       *widget,
136                                                              GossipAccountsDialog            *dialog);
137 static void       accounts_dialog_button_create_clicked_cb  (GtkWidget                       *button,
138                                                              GossipAccountsDialog            *dialog);
139 static void       accounts_dialog_button_cancel_clicked_cb  (GtkWidget                       *button,
140                                                              GossipAccountsDialog            *dialog);
141 static void       accounts_dialog_button_connect_clicked_cb (GtkWidget                       *button,
142                                                              GossipAccountsDialog            *dialog);
143 static void       accounts_dialog_button_add_clicked_cb     (GtkWidget                       *button,
144                                                              GossipAccountsDialog            *dialog);
145 static void       accounts_dialog_remove_response_cb        (GtkWidget                       *dialog,
146                                                              gint                             response,
147                                                              McAccount                       *account);
148 static void       accounts_dialog_button_remove_clicked_cb  (GtkWidget                       *button,
149                                                              GossipAccountsDialog            *dialog);
150 static void       accounts_dialog_treeview_row_activated_cb (GtkTreeView                     *tree_view,
151                                                              GtkTreePath                     *path,
152                                                              GtkTreeViewColumn               *column,
153                                                              GossipAccountsDialog            *dialog);
154 static void       accounts_dialog_response_cb               (GtkWidget                       *widget,
155                                                              gint                             response,
156                                                              GossipAccountsDialog            *dialog);
157 static void       accounts_dialog_destroy_cb                (GtkWidget                       *widget,
158                                                              GossipAccountsDialog            *dialog);
159
160 static void
161 accounts_dialog_setup (GossipAccountsDialog *dialog)
162 {
163         GtkTreeView      *view;
164         GtkListStore     *store;
165         GtkTreeSelection *selection;
166         GtkTreeIter       iter;
167         GList            *accounts, *l;
168
169         view = GTK_TREE_VIEW (dialog->treeview);
170         store = GTK_LIST_STORE (gtk_tree_view_get_model (view));
171         selection = gtk_tree_view_get_selection (view);
172
173         accounts = mc_accounts_list ();
174
175         for (l = accounts; l; l = l->next) {
176                 McAccount                 *account;
177                 const gchar               *name;
178                 TelepathyConnectionStatus  status;
179
180                 account = l->data;
181
182                 name = mc_account_get_display_name (account);
183                 if (!name) {
184                         continue;
185                 }
186
187                 status = mission_control_get_connection_status (dialog->mc, account, NULL);
188
189                 gtk_list_store_append (store, &iter);
190                 gtk_list_store_set (store, &iter,
191                                     COL_NAME, name,
192                                     COL_STATUS, status,
193                                     COL_ACCOUNT_POINTER, account,
194                                     -1);
195
196                 accounts_dialog_status_changed_cb (dialog->mc,
197                                                    status,
198                                                    MC_PRESENCE_UNSET,
199                                                    TP_CONN_STATUS_REASON_NONE_SPECIFIED,
200                                                    mc_account_get_unique_name (account),
201                                                    dialog);
202
203                 g_object_unref (account);
204         }
205
206         g_list_free (accounts);
207 }
208
209 static void
210 accounts_dialog_update_connect_button (GossipAccountsDialog *dialog)
211 {
212         McAccount   *account;
213         GtkWidget   *image;
214         const gchar *stock_id;
215         const gchar *label;
216
217         account = accounts_dialog_model_get_selected (dialog);
218         
219         if (!account) {
220                 gtk_widget_set_sensitive (dialog->button_connect, FALSE);
221                 return;
222         }
223
224         if (mc_account_is_enabled (account)) {
225                 label = _("Disable");
226                 stock_id = GTK_STOCK_DISCONNECT;
227         } else {
228                 label = _("Enable");
229                 stock_id = GTK_STOCK_CONNECT;
230         }
231
232         image = gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON);
233
234         gtk_button_set_label (GTK_BUTTON (dialog->button_connect), label);
235         gtk_button_set_image (GTK_BUTTON (dialog->button_connect), image);
236 }
237
238 static void
239 accounts_dialog_update_account (GossipAccountsDialog *dialog,
240                                 McAccount            *account)
241 {
242         if (dialog->settings_widget) {
243                 gtk_widget_destroy (dialog->settings_widget);
244                 dialog->settings_widget = NULL;
245         }
246
247         if (!account) {
248                 GtkTreeView  *view;
249                 GtkTreeModel *model;
250
251                 gtk_widget_show (dialog->frame_no_account);
252                 gtk_widget_hide (dialog->vbox_details);
253
254                 gtk_widget_set_sensitive (dialog->button_connect, FALSE);
255                 gtk_widget_set_sensitive (dialog->button_remove, FALSE);
256
257                 view = GTK_TREE_VIEW (dialog->treeview);
258                 model = gtk_tree_view_get_model (view);
259
260                 if (gtk_tree_model_iter_n_children (model, NULL) > 0) {
261                         gtk_label_set_markup (GTK_LABEL (dialog->label_no_account),
262                                               _("<b>No Account Selected</b>"));
263                         gtk_label_set_markup (GTK_LABEL (dialog->label_no_account_blurb),
264                                               _("To add a new account, you can click on the "
265                                                 "'Add' button and a new entry will be created "
266                                                 "for you to start configuring.\n"
267                                                 "\n"
268                                                 "If you do not want to add an account, simply "
269                                                 "click on the account you want to configure in "
270                                                 "the list on the left."));
271                 } else {
272                         gtk_label_set_markup (GTK_LABEL (dialog->label_no_account),
273                                               _("<b>No Accounts Configured</b>"));
274                         gtk_label_set_markup (GTK_LABEL (dialog->label_no_account_blurb),
275                                               _("To add a new account, you can click on the "
276                                                 "'Add' button and a new entry will be created "
277                                                 "for you to start configuring."));
278                 }
279         } else {
280                 McProfile *profile;
281                 const gchar *config_ui;
282
283                 gtk_widget_hide (dialog->frame_no_account);
284                 gtk_widget_show (dialog->vbox_details);
285
286                 profile = mc_account_get_profile (account);
287                 config_ui = mc_profile_get_configuration_ui (profile);
288
289                 if (strcmp (config_ui, "jabber") == 0) {
290                         dialog->settings_widget = 
291                                 gossip_account_widget_jabber_new (account);
292                 } else {
293                         dialog->settings_widget = 
294                                 gossip_account_widget_generic_new (account,
295                                                                    dialog->label_name);
296                 }
297                 
298                 gtk_widget_grab_focus (dialog->settings_widget);
299         }
300
301         if (dialog->settings_widget) {
302                 gtk_container_add (GTK_CONTAINER (dialog->alignment_settings),
303                                    dialog->settings_widget);
304         }
305
306         if (account) {
307                 McProfile *profile;
308                 GdkPixbuf *pixbuf;
309
310                 pixbuf = gossip_pixbuf_from_account (account, GTK_ICON_SIZE_DIALOG);
311                 gtk_image_set_from_pixbuf (GTK_IMAGE (dialog->image_type), pixbuf);
312                 if (pixbuf) {
313                         g_object_unref (pixbuf);
314                 }
315
316                 profile = mc_account_get_profile (account);
317
318                 gtk_label_set_text (GTK_LABEL (dialog->label_type),
319                                     mc_profile_get_display_name (profile));
320                 gtk_label_set_text (GTK_LABEL (dialog->label_name), 
321                                     mc_account_get_display_name (account));
322         }
323 }
324
325 static void
326 accounts_dialog_model_setup (GossipAccountsDialog *dialog)
327 {
328         GtkListStore     *store;
329         GtkTreeSelection *selection;
330
331         store = gtk_list_store_new (COL_COUNT,
332                                     G_TYPE_STRING,     /* name */
333                                     G_TYPE_UINT,       /* status */
334                                     MC_TYPE_ACCOUNT);  /* account */
335
336         gtk_tree_view_set_model (GTK_TREE_VIEW (dialog->treeview),
337                                  GTK_TREE_MODEL (store));
338
339         selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (dialog->treeview));
340         gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
341
342         g_signal_connect (selection, "changed",
343                           G_CALLBACK (accounts_dialog_model_selection_changed),
344                           dialog);
345
346         gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
347                                               COL_NAME, GTK_SORT_ASCENDING);
348
349         accounts_dialog_model_add_columns (dialog);
350
351         g_object_unref (store);
352 }
353
354 static void
355 accounts_dialog_model_add_columns (GossipAccountsDialog *dialog)
356 {
357         GtkTreeView       *view;
358         GtkTreeViewColumn *column;
359         GtkCellRenderer   *cell;
360
361         view = GTK_TREE_VIEW (dialog->treeview);
362         gtk_tree_view_set_headers_visible (view, TRUE);
363
364         /* account name/status */
365         column = gtk_tree_view_column_new ();
366         gtk_tree_view_column_set_title (column, _("Accounts"));
367
368         cell = gtk_cell_renderer_pixbuf_new ();
369         gtk_tree_view_column_pack_start (column, cell, FALSE);
370         gtk_tree_view_column_set_cell_data_func (column, cell,
371                                                  (GtkTreeCellDataFunc)
372                                                  accounts_dialog_model_pixbuf_data_func,
373                                                  dialog,
374                                                  NULL);
375
376         cell = gtk_cell_renderer_text_new ();
377         g_object_set (cell, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
378         gtk_tree_view_column_pack_start (column, cell, TRUE);
379         gtk_tree_view_column_add_attribute (column,
380                                             cell,
381                                             "text", COL_NAME);
382
383         gtk_tree_view_column_set_expand (column, TRUE);
384         gtk_tree_view_append_column (view, column);
385 }
386
387 static void
388 accounts_dialog_model_select_first (GossipAccountsDialog *dialog)
389 {
390         GtkTreeView      *view;
391         GtkTreeModel     *model;
392         GtkTreeSelection *selection;
393         GtkTreeIter       iter;
394
395         /* select first */
396         view = GTK_TREE_VIEW (dialog->treeview);
397         model = gtk_tree_view_get_model (view);
398         
399         if (gtk_tree_model_get_iter_first (model, &iter)) {
400                 selection = gtk_tree_view_get_selection (view);
401                 gtk_tree_selection_select_iter (selection, &iter);
402         } else {
403                 accounts_dialog_update_account (dialog, NULL);
404         }
405 }
406
407 static void
408 accounts_dialog_model_pixbuf_data_func (GtkTreeViewColumn    *tree_column,
409                                         GtkCellRenderer      *cell,
410                                         GtkTreeModel         *model,
411                                         GtkTreeIter          *iter,
412                                         GossipAccountsDialog *dialog)
413 {
414         McAccount                 *account;
415         GdkPixbuf                 *pixbuf;
416         TelepathyConnectionStatus  status;
417
418         gtk_tree_model_get (model, iter,
419                             COL_STATUS, &status,
420                             COL_ACCOUNT_POINTER, &account,
421                             -1);
422
423         pixbuf = gossip_pixbuf_from_account (account, GTK_ICON_SIZE_BUTTON);
424
425         if (pixbuf) {
426                 if (status == TP_CONN_STATUS_DISCONNECTED ||
427                     (status == TP_CONN_STATUS_CONNECTING && 
428                      !dialog->connecting_show)) {
429                         GdkPixbuf *modded_pixbuf;
430
431                         modded_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
432                                                         TRUE,
433                                                         8,
434                                                         gdk_pixbuf_get_width (pixbuf),
435                                                         gdk_pixbuf_get_height (pixbuf));
436
437                         gdk_pixbuf_saturate_and_pixelate (pixbuf,
438                                                           modded_pixbuf,
439                                                           1.0,
440                                                           TRUE);
441                         g_object_unref (pixbuf);
442                         pixbuf = modded_pixbuf;
443                 }
444         }
445
446         g_object_set (cell,
447                       "visible", TRUE,
448                       "pixbuf", pixbuf,
449                       NULL);
450
451         g_object_unref (account);
452         if (pixbuf) {
453                 g_object_unref (pixbuf);
454         }
455 }
456
457 static McAccount *
458 accounts_dialog_model_get_selected (GossipAccountsDialog *dialog)
459 {
460         GtkTreeView      *view;
461         GtkTreeModel     *model;
462         GtkTreeSelection *selection;
463         GtkTreeIter       iter;
464         McAccount        *account;
465
466         view = GTK_TREE_VIEW (dialog->treeview);
467         selection = gtk_tree_view_get_selection (view);
468
469         if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
470                 return NULL;
471         }
472
473         gtk_tree_model_get (model, &iter, COL_ACCOUNT_POINTER, &account, -1);
474
475         return account;
476 }
477
478 static void
479 accounts_dialog_model_set_selected (GossipAccountsDialog *dialog,
480                                     McAccount            *account)
481 {
482         GtkTreeView      *view;
483         GtkTreeSelection *selection;
484         GtkTreeModel     *model;
485         GtkTreeIter       iter;
486         gboolean          ok;
487
488         view = GTK_TREE_VIEW (dialog->treeview);
489         model = gtk_tree_view_get_model (view);
490         selection = gtk_tree_view_get_selection (view);
491
492         for (ok = gtk_tree_model_get_iter_first (model, &iter);
493              ok;
494              ok = gtk_tree_model_iter_next (model, &iter)) {
495                 McAccount *this_account;
496                 gboolean   equal;
497
498                 gtk_tree_model_get (model, &iter,
499                                     COL_ACCOUNT_POINTER, &this_account,
500                                     -1);
501
502                 equal = gossip_account_equal (this_account, account);
503                 g_object_unref (this_account);
504
505                 if (equal) {
506                         gtk_tree_selection_select_iter (selection, &iter);
507                         break;
508                 }
509         }
510 }
511
512 static gboolean
513 accounts_dialog_model_remove_selected (GossipAccountsDialog *dialog)
514 {
515         GtkTreeView      *view;
516         GtkTreeModel     *model;
517         GtkTreeSelection *selection;
518         GtkTreeIter       iter;
519
520         view = GTK_TREE_VIEW (dialog->treeview);
521         selection = gtk_tree_view_get_selection (view);
522
523         if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
524                 return FALSE;
525         }
526
527         return gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
528 }
529
530 static void
531 accounts_dialog_model_selection_changed (GtkTreeSelection     *selection,
532                                          GossipAccountsDialog *dialog)
533 {
534         McAccount    *account;
535         GtkTreeModel *model;
536         GtkTreeIter   iter;
537         gboolean      is_selection;
538
539         is_selection = gtk_tree_selection_get_selected (selection, &model, &iter);
540
541         gtk_widget_set_sensitive (dialog->button_remove, is_selection);
542         gtk_widget_set_sensitive (dialog->button_connect, is_selection);
543
544         accounts_dialog_update_connect_button (dialog);
545
546         account = accounts_dialog_model_get_selected (dialog);
547         accounts_dialog_update_account (dialog, account);
548
549         if (account) {
550                 g_object_unref (account);
551         }
552 }
553
554 static void
555 accounts_dialog_add_account (GossipAccountsDialog *dialog,
556                              McAccount            *account)
557 {
558         TelepathyConnectionStatus  status;
559         const gchar               *name;
560         GtkTreeView               *view;
561         GtkTreeModel              *model;
562         GtkListStore              *store;
563         GtkTreeIter                iter;
564         gboolean                   ok;
565
566         view = GTK_TREE_VIEW (dialog->treeview);
567         model = gtk_tree_view_get_model (view);
568         store = GTK_LIST_STORE (model);
569
570         for (ok = gtk_tree_model_get_iter_first (model, &iter);
571              ok;
572              ok = gtk_tree_model_iter_next (model, &iter)) {
573                 McAccount *this_account;
574                 gboolean   equal;
575
576                 gtk_tree_model_get (model, &iter,
577                                     COL_ACCOUNT_POINTER, &this_account,
578                                     -1);
579
580                 equal =  gossip_account_equal (this_account, account);
581                 g_object_unref (this_account);
582
583                 if (equal) {
584                         return;
585                 }
586         }
587
588         status = mission_control_get_connection_status (dialog->mc, account, NULL);
589         name = mc_account_get_display_name (account);
590
591         g_return_if_fail (name != NULL);
592
593         gossip_debug (DEBUG_DOMAIN, "Adding new account: %s", name);
594
595         gtk_list_store_append (store, &iter);
596         gtk_list_store_set (store, &iter,
597                             COL_NAME, name,
598                             COL_STATUS, status,
599                             COL_ACCOUNT_POINTER, account,
600                             -1);
601 }
602
603 static void
604 accounts_dialog_account_added_cb (McAccountMonitor     *monitor,
605                                   gchar                *unique_name,
606                                   GossipAccountsDialog *dialog)
607 {
608         McAccount *account;
609
610         account = mc_account_lookup (unique_name);
611         accounts_dialog_add_account (dialog, account);
612         g_object_unref (account);
613 }
614
615 static void
616 accounts_dialog_account_removed_cb (McAccountMonitor     *monitor,
617                                     gchar                *unique_name,
618                                     GossipAccountsDialog *dialog)
619 {
620         McAccount *account;
621
622         account = mc_account_lookup (unique_name);
623
624         accounts_dialog_model_set_selected (dialog, account);
625         accounts_dialog_model_remove_selected (dialog);
626
627         g_object_unref (account);
628 }
629
630 static gboolean
631 accounts_dialog_row_changed_foreach (GtkTreeModel *model,
632                                      GtkTreePath  *path,
633                                      GtkTreeIter  *iter,
634                                      gpointer      user_data)
635 {
636         gtk_tree_model_row_changed (model, path, iter);
637
638         return FALSE;
639 }
640
641 static gboolean
642 accounts_dialog_flash_connecting_cb (GossipAccountsDialog *dialog)
643 {
644         GtkTreeView  *view;
645         GtkTreeModel *model;
646
647         dialog->connecting_show = !dialog->connecting_show;
648
649         view = GTK_TREE_VIEW (dialog->treeview);
650         model = gtk_tree_view_get_model (view);
651
652         gtk_tree_model_foreach (model, accounts_dialog_row_changed_foreach, NULL);
653
654         return TRUE;
655 }
656
657 static void
658 accounts_dialog_status_changed_cb (MissionControl                  *mc,
659                                    TelepathyConnectionStatus        status,
660                                    McPresence                       presence,
661                                    TelepathyConnectionStatusReason  reason,
662                                    const gchar                     *unique_name,
663                                    GossipAccountsDialog            *dialog)
664 {
665         GtkTreeView      *view;
666         GtkTreeSelection *selection;
667         GtkTreeModel     *model;
668         GtkTreeIter       iter;
669         gboolean          ok;
670         McAccount        *account;
671         GList            *accounts, *l;
672         gboolean          found = FALSE;
673         
674         /* Update the status in the model */
675         view = GTK_TREE_VIEW (dialog->treeview);
676         selection = gtk_tree_view_get_selection (view);
677         model = gtk_tree_view_get_model (view);
678         account = mc_account_lookup (unique_name);
679
680         for (ok = gtk_tree_model_get_iter_first (model, &iter);
681              ok;
682              ok = gtk_tree_model_iter_next (model, &iter)) {
683                 McAccount *this_account;
684                 gboolean   equal;
685
686                 gtk_tree_model_get (model, &iter,
687                                     COL_ACCOUNT_POINTER, &this_account,
688                                     -1);
689
690                 equal = gossip_account_equal (this_account, account);
691                 g_object_unref (this_account);
692
693                 if (equal) {
694                         GtkTreePath *path;
695
696                         gtk_list_store_set (GTK_LIST_STORE (model), &iter,
697                                             COL_STATUS, status,
698                                             -1);
699
700                         path = gtk_tree_model_get_path (model, &iter);
701                         gtk_tree_model_row_changed (model, path, &iter);
702                         gtk_tree_path_free (path);
703
704                         break;
705                 }
706         }
707
708         g_object_unref (account);
709
710         /* Start to flash account if status is connecting */
711         if (status == TP_CONN_STATUS_CONNECTING) {
712                 if (!dialog->connecting_id) {
713                         dialog->connecting_id = g_timeout_add (FLASH_TIMEOUT,
714                                                                (GSourceFunc) accounts_dialog_flash_connecting_cb,
715                                                                dialog);
716                 }
717
718                 return;
719         }
720
721         /* Stop to flash if no account is connecting */
722         accounts = mc_accounts_list ();
723         for (l = accounts; l; l = l->next) {
724                 McAccount *this_account;
725
726                 this_account = l->data;
727
728                 if (mission_control_get_connection_status (mc, this_account, NULL) == TP_CONN_STATUS_CONNECTING) {
729                         found = TRUE;
730                         break;
731                 }
732
733                 g_object_unref (this_account);
734         }
735         g_list_free (accounts);
736
737         if (!found && dialog->connecting_id) {
738                 g_source_remove (dialog->connecting_id);
739                 dialog->connecting_id = 0;
740         }
741 }
742
743 static void          
744 accounts_dialog_entry_name_changed_cb (GtkWidget             *widget,
745                                        GossipAccountsDialog  *dialog)
746 {
747         const gchar *str;
748         
749         str = gtk_entry_get_text (GTK_ENTRY (widget));
750         gtk_widget_set_sensitive (dialog->button_create, !G_STR_EMPTY (str));
751 }
752
753 static void
754 accounts_dialog_button_create_clicked_cb (GtkWidget             *button,
755                                           GossipAccountsDialog  *dialog)
756 {
757         McProfile   *profile;
758         McAccount   *account;
759         const gchar *str;
760
761         /* Update widgets */
762         gtk_widget_show (dialog->vbox_details);
763         gtk_widget_hide (dialog->frame_no_account);
764         gtk_widget_hide (dialog->frame_new_account);
765
766         profile = gossip_profile_chooser_get_selected (dialog->combobox_profile);
767
768         /* Create account */
769         account = mc_account_create (profile);
770
771         str = gtk_entry_get_text (GTK_ENTRY (dialog->entry_name));
772         mc_account_set_display_name (account, str);
773
774         accounts_dialog_add_account (dialog, account);
775         accounts_dialog_model_set_selected (dialog, account);
776
777         g_object_unref (account);
778         g_object_unref (profile);
779 }
780
781 static void
782 accounts_dialog_button_cancel_clicked_cb (GtkWidget             *button,
783                                           GossipAccountsDialog  *dialog)
784 {
785         McAccount *account;
786
787         gtk_widget_hide (dialog->vbox_details);
788         gtk_widget_hide (dialog->frame_no_account);
789         gtk_widget_hide (dialog->frame_new_account);
790
791         account = accounts_dialog_model_get_selected (dialog);
792         accounts_dialog_update_account (dialog, account);
793 }
794
795 static void
796 accounts_dialog_button_connect_clicked_cb (GtkWidget            *button,
797                                            GossipAccountsDialog *dialog)
798 {
799         McAccount *account;
800         gboolean   enable;
801
802         account = accounts_dialog_model_get_selected (dialog);
803         enable = (!mc_account_is_enabled (account));
804         mc_account_set_enabled (account, enable);
805         accounts_dialog_update_connect_button (dialog);
806
807         g_object_unref (account);
808 }
809
810 static void
811 accounts_dialog_button_add_clicked_cb (GtkWidget            *button,
812                                        GossipAccountsDialog *dialog)
813 {
814         gtk_widget_hide (dialog->vbox_details);
815         gtk_widget_hide (dialog->frame_no_account);
816         gtk_widget_show (dialog->frame_new_account);
817
818         gtk_combo_box_set_active (GTK_COMBO_BOX (dialog->combobox_profile), 0);
819         gtk_entry_set_text (GTK_ENTRY (dialog->entry_name), "");
820         gtk_widget_grab_focus (dialog->entry_name);
821 }
822
823 static void
824 accounts_dialog_remove_response_cb (GtkWidget *dialog,
825                                     gint       response,
826                                     McAccount *account)
827 {
828         if (response == GTK_RESPONSE_YES) {
829                 mc_account_delete (account);
830         }
831
832         gtk_widget_destroy (dialog);
833 }
834
835 static void
836 accounts_dialog_button_remove_clicked_cb (GtkWidget            *button,
837                                           GossipAccountsDialog *dialog)
838 {
839         McAccount *account;
840         GtkWidget *message_dialog;
841
842         account = accounts_dialog_model_get_selected (dialog);
843
844         if (!mc_account_is_complete (account)) {
845                 accounts_dialog_model_remove_selected (dialog);
846                 return;
847         }
848         message_dialog = gtk_message_dialog_new
849                 (GTK_WINDOW (dialog->window),
850                  GTK_DIALOG_DESTROY_WITH_PARENT,
851                  GTK_MESSAGE_QUESTION,
852                  GTK_BUTTONS_NONE,
853                  _("You are about to remove your %s account!\n"
854                    "Are you sure you want to proceed?"),
855                  mc_account_get_display_name (account));
856
857         gtk_message_dialog_format_secondary_text
858                 (GTK_MESSAGE_DIALOG (message_dialog),
859                  _("Any associated conversations and chat rooms will NOT be "
860                    "removed if you decide to proceed.\n"
861                    "\n"
862                    "Should you decide to add the account back at a later time, "
863                    "they will still be available."));
864
865         gtk_dialog_add_button (GTK_DIALOG (message_dialog),
866                                GTK_STOCK_CANCEL, 
867                                GTK_RESPONSE_NO);
868         gtk_dialog_add_button (GTK_DIALOG (message_dialog),
869                                GTK_STOCK_REMOVE, 
870                                GTK_RESPONSE_YES);
871
872         g_signal_connect (message_dialog, "response",
873                           G_CALLBACK (accounts_dialog_remove_response_cb),
874                           account);
875
876         gtk_widget_show (message_dialog);
877 }
878
879 static void
880 accounts_dialog_treeview_row_activated_cb (GtkTreeView          *tree_view,
881                                            GtkTreePath          *path,
882                                            GtkTreeViewColumn    *column,
883                                            GossipAccountsDialog *dialog)
884 {
885
886         accounts_dialog_button_connect_clicked_cb (dialog->button_connect,
887                                                    dialog);
888 }
889
890 static void
891 accounts_dialog_response_cb (GtkWidget            *widget,
892                              gint                  response,
893                              GossipAccountsDialog *dialog)
894 {
895         gtk_widget_destroy (widget);
896 }
897
898 static void
899 accounts_dialog_destroy_cb (GtkWidget            *widget,
900                             GossipAccountsDialog *dialog)
901 {
902         GList *accounts, *l;
903
904         /* Disconnect signals */
905         g_signal_handlers_disconnect_by_func (dialog->monitor,
906                                               accounts_dialog_account_added_cb,
907                                               dialog);
908         g_signal_handlers_disconnect_by_func (dialog->monitor,
909                                               accounts_dialog_account_removed_cb,
910                                               dialog);
911         dbus_g_proxy_disconnect_signal (DBUS_G_PROXY (dialog->mc),
912                                         "AccountStatusChanged",
913                                         G_CALLBACK (accounts_dialog_status_changed_cb),
914                                         dialog);
915
916         /* Delete incomplete accounts */
917         accounts = mc_accounts_list ();
918         for (l = accounts; l; l = l->next) {
919                 McAccount *account;
920
921                 account = l->data;
922                 if (!mc_account_is_complete (account)) {
923                         /* FIXME: Warn the user the account is not complete
924                          *        and is going to be removed. */
925                         mc_account_delete (account);
926                 }
927
928                 g_object_unref (account);
929         }
930         g_list_free (accounts);
931
932         if (dialog->connecting_id) {
933                 g_source_remove (dialog->connecting_id);
934         }
935
936         g_object_unref (dialog->mc);
937         g_object_unref (dialog->monitor);
938         
939         g_free (dialog);
940 }
941
942 GtkWidget *
943 gossip_accounts_dialog_show (void)
944 {
945         static GossipAccountsDialog *dialog = NULL;
946         GladeXML                    *glade;
947         GtkWidget                   *bbox;
948         GtkWidget                   *button_close;
949
950         if (dialog) {
951                 gtk_window_present (GTK_WINDOW (dialog->window));
952                 return dialog->window;
953         }
954
955         dialog = g_new0 (GossipAccountsDialog, 1);
956
957         glade = gossip_glade_get_file ("gossip-accounts-dialog.glade",
958                                        "accounts_dialog",
959                                        NULL,
960                                        "accounts_dialog", &dialog->window,
961                                        "vbox_details", &dialog->vbox_details,
962                                        "frame_no_account", &dialog->frame_no_account,
963                                        "label_no_account", &dialog->label_no_account,
964                                        "label_no_account_blurb", &dialog->label_no_account_blurb,
965                                        "alignment_settings", &dialog->alignment_settings,
966                                        "dialog-action_area", &bbox,
967                                        "treeview", &dialog->treeview,
968                                        "frame_new_account", &dialog->frame_new_account,
969                                        "entry_name", &dialog->entry_name,
970                                        "table_new_account", &dialog->table_new_account,
971                                        "button_create", &dialog->button_create,
972                                        "button_cancel", &dialog->button_cancel,
973                                        "image_type", &dialog->image_type,
974                                        "label_type", &dialog->label_type,
975                                        "label_name", &dialog->label_name,
976                                        "button_remove", &dialog->button_remove,
977                                        "button_connect", &dialog->button_connect,
978                                        "button_close", &button_close,
979                                        NULL);
980
981         gossip_glade_connect (glade,
982                               dialog,
983                               "accounts_dialog", "destroy", accounts_dialog_destroy_cb,
984                               "accounts_dialog", "response", accounts_dialog_response_cb,
985                               "button_create", "clicked", accounts_dialog_button_create_clicked_cb,
986                               "button_cancel", "clicked", accounts_dialog_button_cancel_clicked_cb,
987                               "entry_name", "changed", accounts_dialog_entry_name_changed_cb,
988                               "treeview", "row-activated", accounts_dialog_treeview_row_activated_cb,
989                               "button_connect", "clicked", accounts_dialog_button_connect_clicked_cb,
990                               "button_add", "clicked", accounts_dialog_button_add_clicked_cb,
991                               "button_remove", "clicked", accounts_dialog_button_remove_clicked_cb,
992                               NULL);
993
994         g_object_add_weak_pointer (G_OBJECT (dialog->window), (gpointer) &dialog);
995
996         g_object_unref (glade);
997
998         /* Create profile chooser */
999         dialog->combobox_profile = gossip_profile_chooser_new ();
1000         gtk_table_attach_defaults (GTK_TABLE (dialog->table_new_account),
1001                                    dialog->combobox_profile,
1002                                    1, 2,
1003                                    0, 1);
1004         gtk_widget_show (dialog->combobox_profile);
1005
1006         /* Set up signalling */
1007         dialog->mc = mission_control_new (tp_get_bus ());
1008         dialog->monitor = mc_account_monitor_new ();
1009
1010         /* FIXME: connect account-enabled/disabled too */
1011         g_signal_connect (dialog->monitor, "account-created",
1012                           G_CALLBACK (accounts_dialog_account_added_cb),
1013                           dialog);
1014         g_signal_connect (dialog->monitor, "account-deleted",
1015                           G_CALLBACK (accounts_dialog_account_removed_cb),
1016                           dialog);
1017         dbus_g_proxy_connect_signal (DBUS_G_PROXY (dialog->mc), "AccountStatusChanged",
1018                                      G_CALLBACK (accounts_dialog_status_changed_cb),
1019                                      dialog, NULL);
1020
1021         accounts_dialog_model_setup (dialog);
1022         accounts_dialog_setup (dialog);
1023
1024         gtk_widget_show (dialog->window);
1025
1026         accounts_dialog_model_select_first (dialog);
1027
1028         return dialog->window;
1029 }
1030