]> git.0d.be Git - empathy.git/blob - libempathy-gtk/gossip-accounts-dialog.c
5155ced12b96b312f105d8a6d008050f9698f5fd
[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_back;
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_back_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_insert_with_values (store, &iter,
190                                                    -1,
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
309                 profile = mc_account_get_profile (account);
310                 gtk_image_set_from_icon_name (GTK_IMAGE (dialog->image_type),
311                                               mc_profile_get_icon_name (profile),
312                                               GTK_ICON_SIZE_DIALOG);
313                 
314
315                 gtk_label_set_text (GTK_LABEL (dialog->label_type),
316                                     mc_profile_get_display_name (profile));
317                 gtk_label_set_text (GTK_LABEL (dialog->label_name), 
318                                     mc_account_get_display_name (account));
319         }
320 }
321
322 static void
323 accounts_dialog_model_setup (GossipAccountsDialog *dialog)
324 {
325         GtkListStore     *store;
326         GtkTreeSelection *selection;
327
328         store = gtk_list_store_new (COL_COUNT,
329                                     G_TYPE_STRING,     /* name */
330                                     G_TYPE_UINT,       /* status */
331                                     MC_TYPE_ACCOUNT);  /* account */
332
333         gtk_tree_view_set_model (GTK_TREE_VIEW (dialog->treeview),
334                                  GTK_TREE_MODEL (store));
335
336         selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (dialog->treeview));
337         gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
338
339         g_signal_connect (selection, "changed",
340                           G_CALLBACK (accounts_dialog_model_selection_changed),
341                           dialog);
342
343         gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
344                                               COL_NAME, GTK_SORT_ASCENDING);
345
346         accounts_dialog_model_add_columns (dialog);
347
348         g_object_unref (store);
349 }
350
351 static void
352 accounts_dialog_model_add_columns (GossipAccountsDialog *dialog)
353 {
354         GtkTreeView       *view;
355         GtkTreeViewColumn *column;
356         GtkCellRenderer   *cell;
357
358         view = GTK_TREE_VIEW (dialog->treeview);
359         gtk_tree_view_set_headers_visible (view, TRUE);
360
361         /* account name/status */
362         column = gtk_tree_view_column_new ();
363         gtk_tree_view_column_set_title (column, _("Accounts"));
364
365         cell = gtk_cell_renderer_pixbuf_new ();
366         gtk_tree_view_column_pack_start (column, cell, FALSE);
367         gtk_tree_view_column_set_cell_data_func (column, cell,
368                                                  (GtkTreeCellDataFunc)
369                                                  accounts_dialog_model_pixbuf_data_func,
370                                                  dialog,
371                                                  NULL);
372
373         cell = gtk_cell_renderer_text_new ();
374         g_object_set (cell, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
375         gtk_tree_view_column_pack_start (column, cell, TRUE);
376         gtk_tree_view_column_add_attribute (column,
377                                             cell,
378                                             "text", COL_NAME);
379
380         gtk_tree_view_column_set_expand (column, TRUE);
381         gtk_tree_view_append_column (view, column);
382 }
383
384 static void
385 accounts_dialog_model_select_first (GossipAccountsDialog *dialog)
386 {
387         GtkTreeView      *view;
388         GtkTreeModel     *model;
389         GtkTreeSelection *selection;
390         GtkTreeIter       iter;
391
392         /* select first */
393         view = GTK_TREE_VIEW (dialog->treeview);
394         model = gtk_tree_view_get_model (view);
395         
396         if (gtk_tree_model_get_iter_first (model, &iter)) {
397                 selection = gtk_tree_view_get_selection (view);
398                 gtk_tree_selection_select_iter (selection, &iter);
399         } else {
400                 accounts_dialog_update_account (dialog, NULL);
401         }
402 }
403
404 static void
405 accounts_dialog_model_pixbuf_data_func (GtkTreeViewColumn    *tree_column,
406                                         GtkCellRenderer      *cell,
407                                         GtkTreeModel         *model,
408                                         GtkTreeIter          *iter,
409                                         GossipAccountsDialog *dialog)
410 {
411         McAccount                 *account;
412         const gchar               *icon_name;
413         GdkPixbuf                 *pixbuf;
414         TelepathyConnectionStatus  status;
415
416         gtk_tree_model_get (model, iter,
417                             COL_STATUS, &status,
418                             COL_ACCOUNT_POINTER, &account,
419                             -1);
420
421         icon_name = gossip_icon_name_from_account (account);
422         pixbuf = gossip_pixbuf_from_icon_name (icon_name, GTK_ICON_SIZE_BUTTON);
423
424         if (pixbuf) {
425                 if (status == TP_CONN_STATUS_DISCONNECTED ||
426                     (status == TP_CONN_STATUS_CONNECTING && 
427                      !dialog->connecting_show)) {
428                         GdkPixbuf *modded_pixbuf;
429
430                         modded_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
431                                                         TRUE,
432                                                         8,
433                                                         gdk_pixbuf_get_width (pixbuf),
434                                                         gdk_pixbuf_get_height (pixbuf));
435
436                         gdk_pixbuf_saturate_and_pixelate (pixbuf,
437                                                           modded_pixbuf,
438                                                           1.0,
439                                                           TRUE);
440                         g_object_unref (pixbuf);
441                         pixbuf = modded_pixbuf;
442                 }
443         }
444
445         g_object_set (cell,
446                       "visible", TRUE,
447                       "pixbuf", pixbuf,
448                       NULL);
449
450         g_object_unref (account);
451         if (pixbuf) {
452                 g_object_unref (pixbuf);
453         }
454 }
455
456 static McAccount *
457 accounts_dialog_model_get_selected (GossipAccountsDialog *dialog)
458 {
459         GtkTreeView      *view;
460         GtkTreeModel     *model;
461         GtkTreeSelection *selection;
462         GtkTreeIter       iter;
463         McAccount        *account;
464
465         view = GTK_TREE_VIEW (dialog->treeview);
466         selection = gtk_tree_view_get_selection (view);
467
468         if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
469                 return NULL;
470         }
471
472         gtk_tree_model_get (model, &iter, COL_ACCOUNT_POINTER, &account, -1);
473
474         return account;
475 }
476
477 static void
478 accounts_dialog_model_set_selected (GossipAccountsDialog *dialog,
479                                     McAccount            *account)
480 {
481         GtkTreeView      *view;
482         GtkTreeSelection *selection;
483         GtkTreeModel     *model;
484         GtkTreeIter       iter;
485         gboolean          ok;
486
487         view = GTK_TREE_VIEW (dialog->treeview);
488         model = gtk_tree_view_get_model (view);
489         selection = gtk_tree_view_get_selection (view);
490
491         for (ok = gtk_tree_model_get_iter_first (model, &iter);
492              ok;
493              ok = gtk_tree_model_iter_next (model, &iter)) {
494                 McAccount *this_account;
495                 gboolean   equal;
496
497                 gtk_tree_model_get (model, &iter,
498                                     COL_ACCOUNT_POINTER, &this_account,
499                                     -1);
500
501                 equal = gossip_account_equal (this_account, account);
502                 g_object_unref (this_account);
503
504                 if (equal) {
505                         gtk_tree_selection_select_iter (selection, &iter);
506                         break;
507                 }
508         }
509 }
510
511 static gboolean
512 accounts_dialog_model_remove_selected (GossipAccountsDialog *dialog)
513 {
514         GtkTreeView      *view;
515         GtkTreeModel     *model;
516         GtkTreeSelection *selection;
517         GtkTreeIter       iter;
518
519         view = GTK_TREE_VIEW (dialog->treeview);
520         selection = gtk_tree_view_get_selection (view);
521
522         if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
523                 return FALSE;
524         }
525
526         return gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
527 }
528
529 static void
530 accounts_dialog_model_selection_changed (GtkTreeSelection     *selection,
531                                          GossipAccountsDialog *dialog)
532 {
533         McAccount    *account;
534         GtkTreeModel *model;
535         GtkTreeIter   iter;
536         gboolean      is_selection;
537
538         is_selection = gtk_tree_selection_get_selected (selection, &model, &iter);
539
540         gtk_widget_set_sensitive (dialog->button_remove, is_selection);
541         gtk_widget_set_sensitive (dialog->button_connect, is_selection);
542
543         accounts_dialog_update_connect_button (dialog);
544
545         account = accounts_dialog_model_get_selected (dialog);
546         accounts_dialog_update_account (dialog, account);
547
548         if (account) {
549                 g_object_unref (account);
550         }
551 }
552
553 static void
554 accounts_dialog_add_account (GossipAccountsDialog *dialog,
555                              McAccount            *account)
556 {
557         TelepathyConnectionStatus  status;
558         const gchar               *name;
559         GtkTreeView               *view;
560         GtkTreeModel              *model;
561         GtkListStore              *store;
562         GtkTreeIter                iter;
563         gboolean                   ok;
564
565         view = GTK_TREE_VIEW (dialog->treeview);
566         model = gtk_tree_view_get_model (view);
567         store = GTK_LIST_STORE (model);
568
569         for (ok = gtk_tree_model_get_iter_first (model, &iter);
570              ok;
571              ok = gtk_tree_model_iter_next (model, &iter)) {
572                 McAccount *this_account;
573                 gboolean   equal;
574
575                 gtk_tree_model_get (model, &iter,
576                                     COL_ACCOUNT_POINTER, &this_account,
577                                     -1);
578
579                 equal =  gossip_account_equal (this_account, account);
580                 g_object_unref (this_account);
581
582                 if (equal) {
583                         return;
584                 }
585         }
586
587         status = mission_control_get_connection_status (dialog->mc, account, NULL);
588         name = mc_account_get_display_name (account);
589
590         g_return_if_fail (name != NULL);
591
592         gossip_debug (DEBUG_DOMAIN, "Adding new account: %s", name);
593
594         gtk_list_store_insert_with_values (store, &iter,
595                                            -1,
596                                            COL_NAME, name,
597                                            COL_STATUS, status,
598                                            COL_ACCOUNT_POINTER, account,
599                                            -1);
600 }
601
602 static void
603 accounts_dialog_account_added_cb (McAccountMonitor     *monitor,
604                                   gchar                *unique_name,
605                                   GossipAccountsDialog *dialog)
606 {
607         McAccount *account;
608
609         account = mc_account_lookup (unique_name);
610         accounts_dialog_add_account (dialog, account);
611         g_object_unref (account);
612 }
613
614 static void
615 accounts_dialog_account_removed_cb (McAccountMonitor     *monitor,
616                                     gchar                *unique_name,
617                                     GossipAccountsDialog *dialog)
618 {
619         McAccount *account;
620
621         account = mc_account_lookup (unique_name);
622
623         accounts_dialog_model_set_selected (dialog, account);
624         accounts_dialog_model_remove_selected (dialog);
625
626         g_object_unref (account);
627 }
628
629 static gboolean
630 accounts_dialog_row_changed_foreach (GtkTreeModel *model,
631                                      GtkTreePath  *path,
632                                      GtkTreeIter  *iter,
633                                      gpointer      user_data)
634 {
635         gtk_tree_model_row_changed (model, path, iter);
636
637         return FALSE;
638 }
639
640 static gboolean
641 accounts_dialog_flash_connecting_cb (GossipAccountsDialog *dialog)
642 {
643         GtkTreeView  *view;
644         GtkTreeModel *model;
645
646         dialog->connecting_show = !dialog->connecting_show;
647
648         view = GTK_TREE_VIEW (dialog->treeview);
649         model = gtk_tree_view_get_model (view);
650
651         gtk_tree_model_foreach (model, accounts_dialog_row_changed_foreach, NULL);
652
653         return TRUE;
654 }
655
656 static void
657 accounts_dialog_status_changed_cb (MissionControl                  *mc,
658                                    TelepathyConnectionStatus        status,
659                                    McPresence                       presence,
660                                    TelepathyConnectionStatusReason  reason,
661                                    const gchar                     *unique_name,
662                                    GossipAccountsDialog            *dialog)
663 {
664         GtkTreeView      *view;
665         GtkTreeSelection *selection;
666         GtkTreeModel     *model;
667         GtkTreeIter       iter;
668         gboolean          ok;
669         McAccount        *account;
670         GList            *accounts, *l;
671         gboolean          found = FALSE;
672         
673         /* Update the status in the model */
674         view = GTK_TREE_VIEW (dialog->treeview);
675         selection = gtk_tree_view_get_selection (view);
676         model = gtk_tree_view_get_model (view);
677         account = mc_account_lookup (unique_name);
678
679         for (ok = gtk_tree_model_get_iter_first (model, &iter);
680              ok;
681              ok = gtk_tree_model_iter_next (model, &iter)) {
682                 McAccount *this_account;
683                 gboolean   equal;
684
685                 gtk_tree_model_get (model, &iter,
686                                     COL_ACCOUNT_POINTER, &this_account,
687                                     -1);
688
689                 equal = gossip_account_equal (this_account, account);
690                 g_object_unref (this_account);
691
692                 if (equal) {
693                         GtkTreePath *path;
694
695                         gtk_list_store_set (GTK_LIST_STORE (model), &iter,
696                                             COL_STATUS, status,
697                                             -1);
698
699                         path = gtk_tree_model_get_path (model, &iter);
700                         gtk_tree_model_row_changed (model, path, &iter);
701                         gtk_tree_path_free (path);
702
703                         break;
704                 }
705         }
706
707         g_object_unref (account);
708
709         /* Start to flash account if status is connecting */
710         if (status == TP_CONN_STATUS_CONNECTING) {
711                 if (!dialog->connecting_id) {
712                         dialog->connecting_id = g_timeout_add (FLASH_TIMEOUT,
713                                                                (GSourceFunc) accounts_dialog_flash_connecting_cb,
714                                                                dialog);
715                 }
716
717                 return;
718         }
719
720         /* Stop to flash if no account is connecting */
721         accounts = mc_accounts_list ();
722         for (l = accounts; l; l = l->next) {
723                 McAccount *this_account;
724
725                 this_account = l->data;
726
727                 if (mission_control_get_connection_status (mc, this_account, NULL) == TP_CONN_STATUS_CONNECTING) {
728                         found = TRUE;
729                         break;
730                 }
731
732                 g_object_unref (this_account);
733         }
734         g_list_free (accounts);
735
736         if (!found && dialog->connecting_id) {
737                 g_source_remove (dialog->connecting_id);
738                 dialog->connecting_id = 0;
739         }
740
741         gtk_widget_show (dialog->window);
742 }
743
744 static void          
745 accounts_dialog_entry_name_changed_cb (GtkWidget             *widget,
746                                        GossipAccountsDialog  *dialog)
747 {
748         const gchar *str;
749         
750         str = gtk_entry_get_text (GTK_ENTRY (widget));
751         gtk_widget_set_sensitive (dialog->button_create, !G_STR_EMPTY (str));
752 }
753
754 static void
755 accounts_dialog_button_create_clicked_cb (GtkWidget             *button,
756                                           GossipAccountsDialog  *dialog)
757 {
758         McProfile   *profile;
759         McAccount   *account;
760         const gchar *str;
761
762         /* Update widgets */
763         gtk_widget_show (dialog->vbox_details);
764         gtk_widget_hide (dialog->frame_no_account);
765         gtk_widget_hide (dialog->frame_new_account);
766
767         profile = gossip_profile_chooser_get_selected (dialog->combobox_profile);
768
769         /* Create account */
770         account = mc_account_create (profile);
771
772         str = gtk_entry_get_text (GTK_ENTRY (dialog->entry_name));
773         mc_account_set_display_name (account, str);
774
775         accounts_dialog_add_account (dialog, account);
776         accounts_dialog_model_set_selected (dialog, account);
777
778         g_object_unref (account);
779         g_object_unref (profile);
780 }
781
782 static void
783 accounts_dialog_button_back_clicked_cb (GtkWidget             *button,
784                                         GossipAccountsDialog  *dialog)
785 {
786         McAccount *account;
787
788         gtk_widget_hide (dialog->vbox_details);
789         gtk_widget_hide (dialog->frame_no_account);
790         gtk_widget_hide (dialog->frame_new_account);
791
792         account = accounts_dialog_model_get_selected (dialog);
793         accounts_dialog_update_account (dialog, account);
794 }
795
796 static void
797 accounts_dialog_button_connect_clicked_cb (GtkWidget            *button,
798                                            GossipAccountsDialog *dialog)
799 {
800         McAccount *account;
801         gboolean   enable;
802
803         account = accounts_dialog_model_get_selected (dialog);
804         enable = (!mc_account_is_enabled (account));
805         mc_account_set_enabled (account, enable);
806         accounts_dialog_update_connect_button (dialog);
807
808         g_object_unref (account);
809 }
810
811 static void
812 accounts_dialog_button_add_clicked_cb (GtkWidget            *button,
813                                        GossipAccountsDialog *dialog)
814 {
815         gtk_widget_hide (dialog->vbox_details);
816         gtk_widget_hide (dialog->frame_no_account);
817         gtk_widget_show (dialog->frame_new_account);
818
819         gtk_combo_box_set_active (GTK_COMBO_BOX (dialog->combobox_profile), 0);
820         gtk_entry_set_text (GTK_ENTRY (dialog->entry_name), "");
821         gtk_widget_grab_focus (dialog->entry_name);
822 }
823
824 static void
825 accounts_dialog_remove_response_cb (GtkWidget *dialog,
826                                     gint       response,
827                                     McAccount *account)
828 {
829         if (response == GTK_RESPONSE_YES) {
830                 mc_account_delete (account);
831         }
832
833         gtk_widget_destroy (dialog);
834 }
835
836 static void
837 accounts_dialog_button_remove_clicked_cb (GtkWidget            *button,
838                                           GossipAccountsDialog *dialog)
839 {
840         McAccount *account;
841         GtkWidget *message_dialog;
842
843         account = accounts_dialog_model_get_selected (dialog);
844
845         if (!mc_account_is_complete (account)) {
846                 accounts_dialog_model_remove_selected (dialog);
847                 return;
848         }
849         message_dialog = gtk_message_dialog_new
850                 (GTK_WINDOW (dialog->window),
851                  GTK_DIALOG_DESTROY_WITH_PARENT,
852                  GTK_MESSAGE_QUESTION,
853                  GTK_BUTTONS_NONE,
854                  _("You are about to remove your %s account!\n"
855                    "Are you sure you want to proceed?"),
856                  mc_account_get_display_name (account));
857
858         gtk_message_dialog_format_secondary_text
859                 (GTK_MESSAGE_DIALOG (message_dialog),
860                  _("Any associated conversations and chat rooms will NOT be "
861                    "removed if you decide to proceed.\n"
862                    "\n"
863                    "Should you decide to add the account back at a later time, "
864                    "they will still be available."));
865
866         gtk_dialog_add_button (GTK_DIALOG (message_dialog),
867                                GTK_STOCK_CANCEL, 
868                                GTK_RESPONSE_NO);
869         gtk_dialog_add_button (GTK_DIALOG (message_dialog),
870                                GTK_STOCK_REMOVE, 
871                                GTK_RESPONSE_YES);
872
873         g_signal_connect (message_dialog, "response",
874                           G_CALLBACK (accounts_dialog_remove_response_cb),
875                           account);
876
877         gtk_widget_show (message_dialog);
878 }
879
880 static void
881 accounts_dialog_treeview_row_activated_cb (GtkTreeView          *tree_view,
882                                            GtkTreePath          *path,
883                                            GtkTreeViewColumn    *column,
884                                            GossipAccountsDialog *dialog)
885 {
886
887         accounts_dialog_button_connect_clicked_cb (dialog->button_connect,
888                                                    dialog);
889 }
890
891 static void
892 accounts_dialog_response_cb (GtkWidget            *widget,
893                              gint                  response,
894                              GossipAccountsDialog *dialog)
895 {
896         gtk_widget_destroy (widget);
897 }
898
899 static void
900 accounts_dialog_destroy_cb (GtkWidget            *widget,
901                             GossipAccountsDialog *dialog)
902 {
903         GList *accounts, *l;
904
905         /* Disconnect signals */
906         g_signal_handlers_disconnect_by_func (dialog->monitor,
907                                               accounts_dialog_account_added_cb,
908                                               dialog);
909         g_signal_handlers_disconnect_by_func (dialog->monitor,
910                                               accounts_dialog_account_removed_cb,
911                                               dialog);
912         dbus_g_proxy_disconnect_signal (DBUS_G_PROXY (dialog->mc),
913                                         "AccountStatusChanged",
914                                         G_CALLBACK (accounts_dialog_status_changed_cb),
915                                         dialog);
916
917         /* Delete incomplete accounts */
918         accounts = mc_accounts_list ();
919         for (l = accounts; l; l = l->next) {
920                 McAccount *account;
921
922                 account = l->data;
923                 if (!mc_account_is_complete (account)) {
924                         /* FIXME: Warn the user the account is not complete
925                          *        and is going to be removed. */
926                         mc_account_delete (account);
927                 }
928
929                 g_object_unref (account);
930         }
931         g_list_free (accounts);
932
933         if (dialog->connecting_id) {
934                 g_source_remove (dialog->connecting_id);
935         }
936
937         g_object_unref (dialog->mc);
938         g_object_unref (dialog->monitor);
939         
940         g_free (dialog);
941 }
942
943 GtkWidget *
944 gossip_accounts_dialog_show (void)
945 {
946         static GossipAccountsDialog *dialog = NULL;
947         GladeXML                    *glade;
948         GtkWidget                   *bbox;
949         GtkWidget                   *button_close;
950
951         if (dialog) {
952                 gtk_window_present (GTK_WINDOW (dialog->window));
953                 return dialog->window;
954         }
955
956         dialog = g_new0 (GossipAccountsDialog, 1);
957
958         glade = gossip_glade_get_file ("gossip-accounts-dialog.glade",
959                                        "accounts_dialog",
960                                        NULL,
961                                        "accounts_dialog", &dialog->window,
962                                        "vbox_details", &dialog->vbox_details,
963                                        "frame_no_account", &dialog->frame_no_account,
964                                        "label_no_account", &dialog->label_no_account,
965                                        "label_no_account_blurb", &dialog->label_no_account_blurb,
966                                        "alignment_settings", &dialog->alignment_settings,
967                                        "dialog-action_area", &bbox,
968                                        "treeview", &dialog->treeview,
969                                        "frame_new_account", &dialog->frame_new_account,
970                                        "entry_name", &dialog->entry_name,
971                                        "table_new_account", &dialog->table_new_account,
972                                        "button_create", &dialog->button_create,
973                                        "button_back", &dialog->button_back,
974                                        "image_type", &dialog->image_type,
975                                        "label_type", &dialog->label_type,
976                                        "label_name", &dialog->label_name,
977                                        "button_remove", &dialog->button_remove,
978                                        "button_connect", &dialog->button_connect,
979                                        "button_close", &button_close,
980                                        NULL);
981
982         gossip_glade_connect (glade,
983                               dialog,
984                               "accounts_dialog", "destroy", accounts_dialog_destroy_cb,
985                               "accounts_dialog", "response", accounts_dialog_response_cb,
986                               "button_create", "clicked", accounts_dialog_button_create_clicked_cb,
987                               "button_back", "clicked", accounts_dialog_button_back_clicked_cb,
988                               "entry_name", "changed", accounts_dialog_entry_name_changed_cb,
989                               "treeview", "row-activated", accounts_dialog_treeview_row_activated_cb,
990                               "button_connect", "clicked", accounts_dialog_button_connect_clicked_cb,
991                               "button_add", "clicked", accounts_dialog_button_add_clicked_cb,
992                               "button_remove", "clicked", accounts_dialog_button_remove_clicked_cb,
993                               NULL);
994
995         g_object_add_weak_pointer (G_OBJECT (dialog->window), (gpointer) &dialog);
996
997         g_object_unref (glade);
998
999         /* Create profile chooser */
1000         dialog->combobox_profile = gossip_profile_chooser_new ();
1001         gtk_table_attach_defaults (GTK_TABLE (dialog->table_new_account),
1002                                    dialog->combobox_profile,
1003                                    1, 2,
1004                                    0, 1);
1005         gtk_widget_show (dialog->combobox_profile);
1006
1007         /* Set up signalling */
1008         dialog->mc = mission_control_new (tp_get_bus ());
1009         dialog->monitor = mc_account_monitor_new ();
1010
1011         /* FIXME: connect account-enabled/disabled too */
1012         g_signal_connect (dialog->monitor, "account-created",
1013                           G_CALLBACK (accounts_dialog_account_added_cb),
1014                           dialog);
1015         g_signal_connect (dialog->monitor, "account-deleted",
1016                           G_CALLBACK (accounts_dialog_account_removed_cb),
1017                           dialog);
1018         dbus_g_proxy_connect_signal (DBUS_G_PROXY (dialog->mc), "AccountStatusChanged",
1019                                      G_CALLBACK (accounts_dialog_status_changed_cb),
1020                                      dialog, NULL);
1021
1022         accounts_dialog_model_setup (dialog);
1023         accounts_dialog_setup (dialog);
1024         accounts_dialog_model_select_first (dialog);
1025
1026         gtk_widget_show (dialog->window);
1027
1028         return dialog->window;
1029 }
1030