]> git.0d.be Git - empathy.git/blob - libempathy-gtk/gossip-accounts-dialog.c
848abb52b8572227b32091c89f2f7253a27b833d
[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
41 #include <libempathy/empathy-session.h>
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
51 #define DEBUG_DOMAIN "AccountDialog"
52
53 /* Flashing delay for icons (milliseconds). */
54 #define FLASH_TIMEOUT 500
55
56 typedef struct {
57         GtkWidget *window;
58
59         GtkWidget *alignment_settings;
60
61         GtkWidget *vbox_details;
62         GtkWidget *frame_no_account;
63         GtkWidget *label_no_account;
64         GtkWidget *label_no_account_blurb;
65
66         GtkWidget *treeview;
67
68         GtkWidget *button_remove;
69         GtkWidget *button_connect;
70
71         GtkWidget *frame_new_account;
72         GtkWidget *combobox_profile;
73         GtkWidget *entry_name;
74         GtkWidget *table_new_account;
75         GtkWidget *button_create;
76         GtkWidget *button_cancel;
77
78         GtkWidget *image_type;
79         GtkWidget *label_name;
80         GtkWidget *label_type;
81         GtkWidget *settings_widget;
82
83         gboolean   connecting_show;
84         guint      connecting_id;
85         gboolean   account_changed;
86 } GossipAccountsDialog;
87
88 enum {
89         COL_NAME,
90         COL_STATUS,
91         COL_ACCOUNT_POINTER,
92         COL_COUNT
93 };
94
95 static void       accounts_dialog_setup                     (GossipAccountsDialog            *dialog);
96 static void       accounts_dialog_update_account            (GossipAccountsDialog            *dialog,
97                                                              McAccount                       *account);
98 static void       accounts_dialog_model_setup               (GossipAccountsDialog            *dialog);
99 static void       accounts_dialog_model_add_columns         (GossipAccountsDialog            *dialog);
100 static void       accounts_dialog_model_select_first        (GossipAccountsDialog            *dialog);
101 static void       accounts_dialog_model_pixbuf_data_func    (GtkTreeViewColumn               *tree_column,
102                                                              GtkCellRenderer                 *cell,
103                                                              GtkTreeModel                    *model,
104                                                              GtkTreeIter                     *iter,
105                                                              GossipAccountsDialog            *dialog);
106 static McAccount *accounts_dialog_model_get_selected        (GossipAccountsDialog            *dialog);
107 static void       accounts_dialog_model_set_selected        (GossipAccountsDialog            *dialog,
108                                                              McAccount                       *account);
109 static gboolean   accounts_dialog_model_remove_selected     (GossipAccountsDialog            *dialog);
110 static void       accounts_dialog_model_selection_changed   (GtkTreeSelection                *selection,
111                                                              GossipAccountsDialog            *dialog);
112 static void       accounts_dialog_add_account               (GossipAccountsDialog            *dialog,
113                                                              McAccount                       *account);
114 static void       accounts_dialog_account_added_cb          (McAccountMonitor                *monitor,
115                                                              gchar                           *unique_name,
116                                                              GossipAccountsDialog            *dialog);
117 static void       accounts_dialog_account_removed_cb        (McAccountMonitor                *monitor,
118                                                              gchar                           *unique_name,
119                                                              GossipAccountsDialog            *dialog);
120 static gboolean   accounts_dialog_row_changed_foreach       (GtkTreeModel                    *model,
121                                                              GtkTreePath                     *path,
122                                                              GtkTreeIter                     *iter,
123                                                              gpointer                         user_data);
124 static gboolean   accounts_dialog_flash_connecting_cb       (GossipAccountsDialog            *dialog);
125 static void       accounts_dialog_status_changed_cb         (MissionControl                  *mc,
126                                                              TelepathyConnectionStatus        status,
127                                                              McPresence                       presence,
128                                                              TelepathyConnectionStatusReason  reason,
129                                                              const gchar                     *unique_name,
130                                                              GossipAccountsDialog            *dialog);
131 static void       accounts_dialog_entry_name_changed_cb     (GtkWidget                       *widget,
132                                                              GossipAccountsDialog            *dialog);
133 static void       accounts_dialog_button_create_clicked_cb  (GtkWidget                       *button,
134                                                              GossipAccountsDialog            *dialog);
135 static void       accounts_dialog_button_cancel_clicked_cb  (GtkWidget                       *button,
136                                                              GossipAccountsDialog            *dialog);
137 static void       accounts_dialog_button_connect_clicked_cb (GtkWidget                       *button,
138                                                              GossipAccountsDialog            *dialog);
139 static void       accounts_dialog_button_add_clicked_cb     (GtkWidget                       *button,
140                                                              GossipAccountsDialog            *dialog);
141 static void       accounts_dialog_remove_response_cb        (GtkWidget                       *dialog,
142                                                              gint                             response,
143                                                              McAccount                       *account);
144 static void       accounts_dialog_button_remove_clicked_cb  (GtkWidget                       *button,
145                                                              GossipAccountsDialog            *dialog);
146 static void       accounts_dialog_treeview_row_activated_cb (GtkTreeView                     *tree_view,
147                                                              GtkTreePath                     *path,
148                                                              GtkTreeViewColumn               *column,
149                                                              GossipAccountsDialog            *dialog);
150 static void       accounts_dialog_response_cb               (GtkWidget                       *widget,
151                                                              gint                             response,
152                                                              GossipAccountsDialog            *dialog);
153 static void       accounts_dialog_destroy_cb                (GtkWidget                       *widget,
154                                                              GossipAccountsDialog            *dialog);
155
156 static void
157 accounts_dialog_setup (GossipAccountsDialog *dialog)
158 {
159         GtkTreeView      *view;
160         GtkListStore     *store;
161         GtkTreeSelection *selection;
162         GtkTreeIter       iter;
163         GList            *accounts, *l;
164         MissionControl   *mc;
165
166         view = GTK_TREE_VIEW (dialog->treeview);
167         store = GTK_LIST_STORE (gtk_tree_view_get_model (view));
168         selection = gtk_tree_view_get_selection (view);
169
170         mc = empathy_session_get_mission_control ();
171         accounts = mc_accounts_list ();
172
173         for (l = accounts; l; l = l->next) {
174                 McAccount                 *account;
175                 const gchar               *name;
176                 TelepathyConnectionStatus  status;
177
178                 account = l->data;
179
180                 name = mc_account_get_display_name (account);
181                 if (!name) {
182                         continue;
183                 }
184
185                 status = mission_control_get_connection_status (mc, account, NULL);
186
187                 gtk_list_store_append (store, &iter);
188                 gtk_list_store_set (store, &iter,
189                                     COL_NAME, name,
190                                     COL_STATUS, status,
191                                     COL_ACCOUNT_POINTER, account,
192                                     -1);
193
194                 accounts_dialog_status_changed_cb (mc,
195                                                    status,
196                                                    MC_PRESENCE_UNSET,
197                                                    TP_CONN_STATUS_REASON_NONE_SPECIFIED,
198                                                    mc_account_get_unique_name (account),
199                                                    dialog);
200
201                 g_object_unref (account);
202         }
203
204         g_list_free (accounts);
205 }
206
207 static void
208 accounts_dialog_update_connect_button (GossipAccountsDialog *dialog)
209 {
210         McAccount   *account;
211         GtkWidget   *image;
212         const gchar *stock_id;
213         const gchar *label;
214
215         account = accounts_dialog_model_get_selected (dialog);
216         
217         if (!account) {
218                 gtk_widget_set_sensitive (dialog->button_connect, FALSE);
219                 return;
220         }
221
222         if (mc_account_is_enabled (account)) {
223                 label = _("Disable");
224                 stock_id = GTK_STOCK_DISCONNECT;
225         } else {
226                 label = _("Enable");
227                 stock_id = GTK_STOCK_CONNECT;
228         }
229
230         image = gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON);
231
232         gtk_button_set_label (GTK_BUTTON (dialog->button_connect), label);
233         gtk_button_set_image (GTK_BUTTON (dialog->button_connect), image);
234 }
235
236 static void
237 accounts_dialog_update_account (GossipAccountsDialog *dialog,
238                                 McAccount            *account)
239 {
240         if (dialog->settings_widget) {
241                 gtk_widget_destroy (dialog->settings_widget);
242                 dialog->settings_widget = NULL;
243         }
244
245         if (!account) {
246                 GtkTreeView  *view;
247                 GtkTreeModel *model;
248
249                 gtk_widget_show (dialog->frame_no_account);
250                 gtk_widget_hide (dialog->vbox_details);
251
252                 gtk_widget_set_sensitive (dialog->button_connect, FALSE);
253                 gtk_widget_set_sensitive (dialog->button_remove, FALSE);
254
255                 view = GTK_TREE_VIEW (dialog->treeview);
256                 model = gtk_tree_view_get_model (view);
257
258                 if (gtk_tree_model_iter_n_children (model, NULL) > 0) {
259                         gtk_label_set_markup (GTK_LABEL (dialog->label_no_account),
260                                               _("<b>No Account Selected</b>"));
261                         gtk_label_set_markup (GTK_LABEL (dialog->label_no_account_blurb),
262                                               _("To add a new account, you can click on the "
263                                                 "'Add' button and a new entry will be created "
264                                                 "for you to start configuring.\n"
265                                                 "\n"
266                                                 "If you do not want to add an account, simply "
267                                                 "click on the account you want to configure in "
268                                                 "the list on the left."));
269                 } else {
270                         gtk_label_set_markup (GTK_LABEL (dialog->label_no_account),
271                                               _("<b>No Accounts Configured</b>"));
272                         gtk_label_set_markup (GTK_LABEL (dialog->label_no_account_blurb),
273                                               _("To add a new account, you can click on the "
274                                                 "'Add' button and a new entry will be created "
275                                                 "for you to start configuring."));
276                 }
277         } else {
278                 McProfile *profile;
279                 const gchar *config_ui;
280
281                 gtk_widget_hide (dialog->frame_no_account);
282                 gtk_widget_show (dialog->vbox_details);
283
284                 profile = mc_account_get_profile (account);
285                 config_ui = mc_profile_get_configuration_ui (profile);
286
287                 if (strcmp (config_ui, "blah") == 0) {
288                 } else {
289                         dialog->settings_widget = 
290                                 gossip_account_widget_generic_new (account,
291                                                                    dialog->label_name);
292                 }
293                 
294                 gtk_widget_grab_focus (dialog->settings_widget);
295         }
296
297         if (dialog->settings_widget) {
298                 gtk_container_add (GTK_CONTAINER (dialog->alignment_settings),
299                                    dialog->settings_widget);
300         }
301
302         if (account) {
303                 McProfile *profile;
304                 GdkPixbuf *pixbuf;
305
306                 pixbuf = gossip_pixbuf_from_account (account, GTK_ICON_SIZE_DIALOG);
307                 gtk_image_set_from_pixbuf (GTK_IMAGE (dialog->image_type), pixbuf);
308                 if (pixbuf) {
309                         g_object_unref (pixbuf);
310                 }
311
312                 profile = mc_account_get_profile (account);
313
314                 gtk_label_set_text (GTK_LABEL (dialog->label_type),
315                                     mc_profile_get_display_name (profile));
316                 gtk_label_set_text (GTK_LABEL (dialog->label_name), 
317                                     mc_account_get_display_name (account));
318         }
319 }
320
321 static void
322 accounts_dialog_model_setup (GossipAccountsDialog *dialog)
323 {
324         GtkListStore     *store;
325         GtkTreeSelection *selection;
326
327         store = gtk_list_store_new (COL_COUNT,
328                                     G_TYPE_STRING,     /* name */
329                                     G_TYPE_UINT,       /* status */
330                                     MC_TYPE_ACCOUNT);  /* account */
331
332         gtk_tree_view_set_model (GTK_TREE_VIEW (dialog->treeview),
333                                  GTK_TREE_MODEL (store));
334
335         selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (dialog->treeview));
336         gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
337
338         g_signal_connect (selection, "changed",
339                           G_CALLBACK (accounts_dialog_model_selection_changed),
340                           dialog);
341
342         gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
343                                               COL_NAME, GTK_SORT_ASCENDING);
344
345         accounts_dialog_model_add_columns (dialog);
346
347         g_object_unref (store);
348 }
349
350 static void
351 accounts_dialog_model_add_columns (GossipAccountsDialog *dialog)
352 {
353         GtkTreeView       *view;
354         GtkTreeViewColumn *column;
355         GtkCellRenderer   *cell;
356
357         view = GTK_TREE_VIEW (dialog->treeview);
358         gtk_tree_view_set_headers_visible (view, TRUE);
359
360         /* account name/status */
361         column = gtk_tree_view_column_new ();
362         gtk_tree_view_column_set_title (column, _("Accounts"));
363
364         cell = gtk_cell_renderer_pixbuf_new ();
365         gtk_tree_view_column_pack_start (column, cell, FALSE);
366         gtk_tree_view_column_set_cell_data_func (column, cell,
367                                                  (GtkTreeCellDataFunc)
368                                                  accounts_dialog_model_pixbuf_data_func,
369                                                  dialog,
370                                                  NULL);
371
372         cell = gtk_cell_renderer_text_new ();
373         g_object_set (cell, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
374         gtk_tree_view_column_pack_start (column, cell, TRUE);
375         gtk_tree_view_column_add_attribute (column,
376                                             cell,
377                                             "text", COL_NAME);
378
379         gtk_tree_view_column_set_expand (column, TRUE);
380         gtk_tree_view_append_column (view, column);
381 }
382
383 static void
384 accounts_dialog_model_select_first (GossipAccountsDialog *dialog)
385 {
386         GtkTreeView      *view;
387         GtkTreeModel     *model;
388         GtkTreeSelection *selection;
389         GtkTreeIter       iter;
390
391         /* select first */
392         view = GTK_TREE_VIEW (dialog->treeview);
393         model = gtk_tree_view_get_model (view);
394         
395         if (gtk_tree_model_get_iter_first (model, &iter)) {
396                 selection = gtk_tree_view_get_selection (view);
397                 gtk_tree_selection_select_iter (selection, &iter);
398         } else {
399                 accounts_dialog_update_account (dialog, NULL);
400         }
401 }
402
403 static void
404 accounts_dialog_model_pixbuf_data_func (GtkTreeViewColumn    *tree_column,
405                                         GtkCellRenderer      *cell,
406                                         GtkTreeModel         *model,
407                                         GtkTreeIter          *iter,
408                                         GossipAccountsDialog *dialog)
409 {
410         McAccount                 *account;
411         GdkPixbuf                 *pixbuf;
412         TelepathyConnectionStatus  status;
413
414         gtk_tree_model_get (model, iter,
415                             COL_STATUS, &status,
416                             COL_ACCOUNT_POINTER, &account,
417                             -1);
418
419         pixbuf = gossip_pixbuf_from_account (account, GTK_ICON_SIZE_BUTTON);
420
421         if (pixbuf) {
422                 if (status == TP_CONN_STATUS_DISCONNECTED ||
423                     (status == TP_CONN_STATUS_CONNECTING && 
424                      !dialog->connecting_show)) {
425                         GdkPixbuf *modded_pixbuf;
426
427                         modded_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
428                                                         TRUE,
429                                                         8,
430                                                         gdk_pixbuf_get_width (pixbuf),
431                                                         gdk_pixbuf_get_height (pixbuf));
432
433                         gdk_pixbuf_saturate_and_pixelate (pixbuf,
434                                                           modded_pixbuf,
435                                                           1.0,
436                                                           TRUE);
437                         g_object_unref (pixbuf);
438                         pixbuf = modded_pixbuf;
439                 }
440         }
441
442         g_object_set (cell,
443                       "visible", TRUE,
444                       "pixbuf", pixbuf,
445                       NULL);
446
447         g_object_unref (account);
448         if (pixbuf) {
449                 g_object_unref (pixbuf);
450         }
451 }
452
453 static McAccount *
454 accounts_dialog_model_get_selected (GossipAccountsDialog *dialog)
455 {
456         GtkTreeView      *view;
457         GtkTreeModel     *model;
458         GtkTreeSelection *selection;
459         GtkTreeIter       iter;
460         McAccount        *account;
461
462         view = GTK_TREE_VIEW (dialog->treeview);
463         selection = gtk_tree_view_get_selection (view);
464
465         if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
466                 return NULL;
467         }
468
469         gtk_tree_model_get (model, &iter, COL_ACCOUNT_POINTER, &account, -1);
470
471         return account;
472 }
473
474 static void
475 accounts_dialog_model_set_selected (GossipAccountsDialog *dialog,
476                                     McAccount            *account)
477 {
478         GtkTreeView      *view;
479         GtkTreeSelection *selection;
480         GtkTreeModel     *model;
481         GtkTreeIter       iter;
482         gboolean          ok;
483
484         view = GTK_TREE_VIEW (dialog->treeview);
485         model = gtk_tree_view_get_model (view);
486         selection = gtk_tree_view_get_selection (view);
487
488         for (ok = gtk_tree_model_get_iter_first (model, &iter);
489              ok;
490              ok = gtk_tree_model_iter_next (model, &iter)) {
491                 McAccount *this_account;
492                 gboolean   equal;
493
494                 gtk_tree_model_get (model, &iter,
495                                     COL_ACCOUNT_POINTER, &this_account,
496                                     -1);
497
498                 equal = gossip_account_equal (this_account, account);
499                 g_object_unref (this_account);
500
501                 if (equal) {
502                         gtk_tree_selection_select_iter (selection, &iter);
503                         break;
504                 }
505         }
506 }
507
508 static gboolean
509 accounts_dialog_model_remove_selected (GossipAccountsDialog *dialog)
510 {
511         GtkTreeView      *view;
512         GtkTreeModel     *model;
513         GtkTreeSelection *selection;
514         GtkTreeIter       iter;
515
516         view = GTK_TREE_VIEW (dialog->treeview);
517         selection = gtk_tree_view_get_selection (view);
518
519         if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
520                 return FALSE;
521         }
522
523         return gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
524 }
525
526 static void
527 accounts_dialog_model_selection_changed (GtkTreeSelection     *selection,
528                                          GossipAccountsDialog *dialog)
529 {
530         McAccount    *account;
531         GtkTreeModel *model;
532         GtkTreeIter   iter;
533         gboolean      is_selection;
534
535         is_selection = gtk_tree_selection_get_selected (selection, &model, &iter);
536
537         gtk_widget_set_sensitive (dialog->button_remove, is_selection);
538         gtk_widget_set_sensitive (dialog->button_connect, is_selection);
539
540         accounts_dialog_update_connect_button (dialog);
541
542         account = accounts_dialog_model_get_selected (dialog);
543         accounts_dialog_update_account (dialog, account);
544
545         if (account) {
546                 g_object_unref (account);
547         }
548 }
549
550 static void
551 accounts_dialog_add_account (GossipAccountsDialog *dialog,
552                              McAccount            *account)
553 {
554         MissionControl            *mc;
555         TelepathyConnectionStatus  status;
556         const gchar               *name;
557         GtkTreeView               *view;
558         GtkTreeModel              *model;
559         GtkListStore              *store;
560         GtkTreeIter                iter;
561         gboolean                   ok;
562
563         view = GTK_TREE_VIEW (dialog->treeview);
564         model = gtk_tree_view_get_model (view);
565         store = GTK_LIST_STORE (model);
566
567         for (ok = gtk_tree_model_get_iter_first (model, &iter);
568              ok;
569              ok = gtk_tree_model_iter_next (model, &iter)) {
570                 McAccount *this_account;
571                 gboolean   equal;
572
573                 gtk_tree_model_get (model, &iter,
574                                     COL_ACCOUNT_POINTER, &this_account,
575                                     -1);
576
577                 equal =  gossip_account_equal (this_account, account);
578                 g_object_unref (this_account);
579
580                 if (equal) {
581                         return;
582                 }
583         }
584
585         mc = empathy_session_get_mission_control ();
586         status = mission_control_get_connection_status (mc, account, NULL);
587         name = mc_account_get_display_name (account);
588
589         g_return_if_fail (name != NULL);
590
591         gossip_debug (DEBUG_DOMAIN, "Adding new account: %s", name);
592
593         gtk_list_store_append (store, &iter);
594         gtk_list_store_set (store, &iter,
595                             COL_NAME, name,
596                             COL_STATUS, status,
597                             COL_ACCOUNT_POINTER, account,
598                             -1);
599 }
600
601 static void
602 accounts_dialog_account_added_cb (McAccountMonitor     *monitor,
603                                   gchar                *unique_name,
604                                   GossipAccountsDialog *dialog)
605 {
606         McAccount *account;
607
608         account = mc_account_lookup (unique_name);
609         accounts_dialog_add_account (dialog, account);
610         g_object_unref (account);
611 }
612
613 static void
614 accounts_dialog_account_removed_cb (McAccountMonitor     *monitor,
615                                     gchar                *unique_name,
616                                     GossipAccountsDialog *dialog)
617 {
618         MissionControl *mc;
619         McAccount      *account;
620
621         mc = empathy_session_get_mission_control ();
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         MissionControl   *mc;
903         McAccountMonitor *monitor;
904         GList            *accounts, *l;
905
906         mc = empathy_session_get_mission_control ();
907         monitor = mc_account_monitor_new ();
908
909         /* Disconnect signals */
910         g_signal_handlers_disconnect_by_func (monitor,
911                                               accounts_dialog_account_added_cb,
912                                               dialog);
913         g_signal_handlers_disconnect_by_func (monitor,
914                                               accounts_dialog_account_removed_cb,
915                                               dialog);
916         dbus_g_proxy_disconnect_signal (DBUS_G_PROXY (mc), "AccountStatusChanged",
917                                      G_CALLBACK (accounts_dialog_status_changed_cb),
918                                      dialog);
919
920         /* Delete incomplete accounts */
921         accounts = mc_accounts_list ();
922         for (l = accounts; l; l = l->next) {
923                 McAccount *account;
924
925                 account = l->data;
926                 if (!mc_account_is_complete (account)) {
927                         /* FIXME: Warn the user the account is not complete
928                          *        and is going to be removed. */
929                         mc_account_delete (account);
930                 }
931
932                 g_object_unref (account);
933         }
934         g_list_free (accounts);
935
936         if (dialog->connecting_id) {
937                 g_source_remove (dialog->connecting_id);
938         }
939
940         g_free (dialog);
941 }
942
943 GtkWidget *
944 gossip_accounts_dialog_show (void)
945 {
946         static GossipAccountsDialog *dialog = NULL;
947         MissionControl              *mc;
948         McAccountMonitor            *monitor;
949         GladeXML                    *glade;
950         GtkWidget                   *bbox;
951         GtkWidget                   *button_close;
952
953         if (dialog) {
954                 gtk_window_present (GTK_WINDOW (dialog->window));
955                 return dialog->window;
956         }
957
958         dialog = g_new0 (GossipAccountsDialog, 1);
959
960         glade = gossip_glade_get_file ("empathy-accounts.glade",
961                                        "accounts_dialog",
962                                        NULL,
963                                        "accounts_dialog", &dialog->window,
964                                        "vbox_details", &dialog->vbox_details,
965                                        "frame_no_account", &dialog->frame_no_account,
966                                        "label_no_account", &dialog->label_no_account,
967                                        "label_no_account_blurb", &dialog->label_no_account_blurb,
968                                        "alignment_settings", &dialog->alignment_settings,
969                                        "dialog-action_area", &bbox,
970                                        "treeview", &dialog->treeview,
971                                        "frame_new_account", &dialog->frame_new_account,
972                                        "entry_name", &dialog->entry_name,
973                                        "table_new_account", &dialog->table_new_account,
974                                        "button_create", &dialog->button_create,
975                                        "button_cancel", &dialog->button_cancel,
976                                        "image_type", &dialog->image_type,
977                                        "label_type", &dialog->label_type,
978                                        "label_name", &dialog->label_name,
979                                        "button_remove", &dialog->button_remove,
980                                        "button_connect", &dialog->button_connect,
981                                        "button_close", &button_close,
982                                        NULL);
983
984         gossip_glade_connect (glade,
985                               dialog,
986                               "accounts_dialog", "destroy", accounts_dialog_destroy_cb,
987                               "accounts_dialog", "response", accounts_dialog_response_cb,
988                               "button_create", "clicked", accounts_dialog_button_create_clicked_cb,
989                               "button_cancel", "clicked", accounts_dialog_button_cancel_clicked_cb,
990                               "entry_name", "changed", accounts_dialog_entry_name_changed_cb,
991                               "treeview", "row-activated", accounts_dialog_treeview_row_activated_cb,
992                               "button_connect", "clicked", accounts_dialog_button_connect_clicked_cb,
993                               "button_add", "clicked", accounts_dialog_button_add_clicked_cb,
994                               "button_remove", "clicked", accounts_dialog_button_remove_clicked_cb,
995                               NULL);
996
997         g_object_add_weak_pointer (G_OBJECT (dialog->window), (gpointer) &dialog);
998
999         g_object_unref (glade);
1000
1001         /* Create profile chooser */
1002         dialog->combobox_profile = gossip_profile_chooser_new ();
1003         gtk_table_attach_defaults (GTK_TABLE (dialog->table_new_account),
1004                                    dialog->combobox_profile,
1005                                    1, 2,
1006                                    0, 1);
1007         gtk_widget_show (dialog->combobox_profile);
1008
1009         /* Set up signalling */
1010         mc = empathy_session_get_mission_control ();
1011         monitor = mc_account_monitor_new ();
1012
1013         /* FIXME: connect account-enabled/disabled too */
1014         g_signal_connect (monitor, "account-created",
1015                           G_CALLBACK (accounts_dialog_account_added_cb),
1016                           dialog);
1017         g_signal_connect (monitor, "account-deleted",
1018                           G_CALLBACK (accounts_dialog_account_removed_cb),
1019                           dialog);
1020         dbus_g_proxy_connect_signal (DBUS_G_PROXY (mc), "AccountStatusChanged",
1021                                      G_CALLBACK (accounts_dialog_status_changed_cb),
1022                                      dialog, NULL);
1023
1024         accounts_dialog_model_setup (dialog);
1025         accounts_dialog_setup (dialog);
1026
1027         gtk_widget_show (dialog->window);
1028
1029         accounts_dialog_model_select_first (dialog);
1030
1031         return dialog->window;
1032 }
1033