]> git.0d.be Git - empathy.git/blob - libempathy-gtk/gossip-accounts-dialog.c
6d7934544c908031ef40911d7cade12d50cf23b9
[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/gossip-debug.h>
42 #include <libempathy/gossip-paths.h>
43 #include <libempathy/gossip-utils.h>
44 #include <libempathy-gtk/gossip-ui-utils.h>
45
46 #include "gossip-accounts-dialog.h"
47 #include "gossip-profile-chooser.h"
48 #include "gossip-account-widget-generic.h"
49 #include "gossip-account-widget-jabber.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_back;
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
87         MissionControl   *mc;
88         McAccountMonitor *monitor;
89 } GossipAccountsDialog;
90
91 enum {
92         COL_NAME,
93         COL_STATUS,
94         COL_ACCOUNT_POINTER,
95         COL_COUNT
96 };
97
98 static void       accounts_dialog_setup                     (GossipAccountsDialog            *dialog);
99 static void       accounts_dialog_update_account            (GossipAccountsDialog            *dialog,
100                                                              McAccount                       *account);
101 static void       accounts_dialog_model_setup               (GossipAccountsDialog            *dialog);
102 static void       accounts_dialog_model_add_columns         (GossipAccountsDialog            *dialog);
103 static void       accounts_dialog_model_select_first        (GossipAccountsDialog            *dialog);
104 static void       accounts_dialog_model_pixbuf_data_func    (GtkTreeViewColumn               *tree_column,
105                                                              GtkCellRenderer                 *cell,
106                                                              GtkTreeModel                    *model,
107                                                              GtkTreeIter                     *iter,
108                                                              GossipAccountsDialog            *dialog);
109 static McAccount *accounts_dialog_model_get_selected        (GossipAccountsDialog            *dialog);
110 static void       accounts_dialog_model_set_selected        (GossipAccountsDialog            *dialog,
111                                                              McAccount                       *account);
112 static gboolean   accounts_dialog_model_remove_selected     (GossipAccountsDialog            *dialog);
113 static void       accounts_dialog_model_selection_changed   (GtkTreeSelection                *selection,
114                                                              GossipAccountsDialog            *dialog);
115 static void       accounts_dialog_add_account               (GossipAccountsDialog            *dialog,
116                                                              McAccount                       *account);
117 static void       accounts_dialog_account_added_cb          (McAccountMonitor                *monitor,
118                                                              gchar                           *unique_name,
119                                                              GossipAccountsDialog            *dialog);
120 static void       accounts_dialog_account_removed_cb        (McAccountMonitor                *monitor,
121                                                              gchar                           *unique_name,
122                                                              GossipAccountsDialog            *dialog);
123 static gboolean   accounts_dialog_row_changed_foreach       (GtkTreeModel                    *model,
124                                                              GtkTreePath                     *path,
125                                                              GtkTreeIter                     *iter,
126                                                              gpointer                         user_data);
127 static gboolean   accounts_dialog_flash_connecting_cb       (GossipAccountsDialog            *dialog);
128 static void       accounts_dialog_status_changed_cb         (MissionControl                  *mc,
129                                                              TelepathyConnectionStatus        status,
130                                                              McPresence                       presence,
131                                                              TelepathyConnectionStatusReason  reason,
132                                                              const gchar                     *unique_name,
133                                                              GossipAccountsDialog            *dialog);
134 static void       accounts_dialog_entry_name_changed_cb     (GtkWidget                       *widget,
135                                                              GossipAccountsDialog            *dialog);
136 static void       accounts_dialog_button_create_clicked_cb  (GtkWidget                       *button,
137                                                              GossipAccountsDialog            *dialog);
138 static void       accounts_dialog_button_back_clicked_cb    (GtkWidget                       *button,
139                                                              GossipAccountsDialog            *dialog);
140 static void       accounts_dialog_button_connect_clicked_cb (GtkWidget                       *button,
141                                                              GossipAccountsDialog            *dialog);
142 static void       accounts_dialog_button_add_clicked_cb     (GtkWidget                       *button,
143                                                              GossipAccountsDialog            *dialog);
144 static void       accounts_dialog_remove_response_cb        (GtkWidget                       *dialog,
145                                                              gint                             response,
146                                                              McAccount                       *account);
147 static void       accounts_dialog_button_remove_clicked_cb  (GtkWidget                       *button,
148                                                              GossipAccountsDialog            *dialog);
149 static void       accounts_dialog_treeview_row_activated_cb (GtkTreeView                     *tree_view,
150                                                              GtkTreePath                     *path,
151                                                              GtkTreeViewColumn               *column,
152                                                              GossipAccountsDialog            *dialog);
153 static void       accounts_dialog_response_cb               (GtkWidget                       *widget,
154                                                              gint                             response,
155                                                              GossipAccountsDialog            *dialog);
156 static void       accounts_dialog_destroy_cb                (GtkWidget                       *widget,
157                                                              GossipAccountsDialog            *dialog);
158
159 static void
160 accounts_dialog_setup (GossipAccountsDialog *dialog)
161 {
162         GtkTreeView      *view;
163         GtkListStore     *store;
164         GtkTreeSelection *selection;
165         GtkTreeIter       iter;
166         GList            *accounts, *l;
167
168         view = GTK_TREE_VIEW (dialog->treeview);
169         store = GTK_LIST_STORE (gtk_tree_view_get_model (view));
170         selection = gtk_tree_view_get_selection (view);
171
172         accounts = mc_accounts_list ();
173
174         for (l = accounts; l; l = l->next) {
175                 McAccount                 *account;
176                 const gchar               *name;
177                 TelepathyConnectionStatus  status;
178
179                 account = l->data;
180
181                 name = mc_account_get_display_name (account);
182                 if (!name) {
183                         continue;
184                 }
185
186                 status = mission_control_get_connection_status (dialog->mc, account, NULL);
187
188                 gtk_list_store_insert_with_values (store, &iter,
189                                                    -1,
190                                                    COL_NAME, name,
191                                                    COL_STATUS, status,
192                                                    COL_ACCOUNT_POINTER, account,
193                                                    -1);
194
195                 accounts_dialog_status_changed_cb (dialog->mc,
196                                                    status,
197                                                    MC_PRESENCE_UNSET,
198                                                    TP_CONN_STATUS_REASON_NONE_SPECIFIED,
199                                                    mc_account_get_unique_name (account),
200                                                    dialog);
201
202                 g_object_unref (account);
203         }
204
205         g_list_free (accounts);
206 }
207
208 static void
209 accounts_dialog_update_connect_button (GossipAccountsDialog *dialog)
210 {
211         McAccount   *account;
212         GtkWidget   *image;
213         const gchar *stock_id;
214         const gchar *label;
215
216         account = accounts_dialog_model_get_selected (dialog);
217         
218         if (!account) {
219                 gtk_widget_set_sensitive (dialog->button_connect, FALSE);
220                 return;
221         }
222
223         if (mc_account_is_enabled (account)) {
224                 label = _("Disable");
225                 stock_id = GTK_STOCK_DISCONNECT;
226         } else {
227                 label = _("Enable");
228                 stock_id = GTK_STOCK_CONNECT;
229         }
230
231         image = gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON);
232
233         gtk_button_set_label (GTK_BUTTON (dialog->button_connect), label);
234         gtk_button_set_image (GTK_BUTTON (dialog->button_connect), image);
235 }
236
237 static void
238 accounts_dialog_update_account (GossipAccountsDialog *dialog,
239                                 McAccount            *account)
240 {
241         if (dialog->settings_widget) {
242                 gtk_widget_destroy (dialog->settings_widget);
243                 dialog->settings_widget = NULL;
244         }
245
246         if (!account) {
247                 GtkTreeView  *view;
248                 GtkTreeModel *model;
249
250                 gtk_widget_show (dialog->frame_no_account);
251                 gtk_widget_hide (dialog->vbox_details);
252
253                 gtk_widget_set_sensitive (dialog->button_connect, FALSE);
254                 gtk_widget_set_sensitive (dialog->button_remove, FALSE);
255
256                 view = GTK_TREE_VIEW (dialog->treeview);
257                 model = gtk_tree_view_get_model (view);
258
259                 if (gtk_tree_model_iter_n_children (model, NULL) > 0) {
260                         gtk_label_set_markup (GTK_LABEL (dialog->label_no_account),
261                                               _("<b>No Account Selected</b>"));
262                         gtk_label_set_markup (GTK_LABEL (dialog->label_no_account_blurb),
263                                               _("To add a new account, you can click on the "
264                                                 "'Add' button and a new entry will be created "
265                                                 "for you to start configuring.\n"
266                                                 "\n"
267                                                 "If you do not want to add an account, simply "
268                                                 "click on the account you want to configure in "
269                                                 "the list on the left."));
270                 } else {
271                         gtk_label_set_markup (GTK_LABEL (dialog->label_no_account),
272                                               _("<b>No Accounts Configured</b>"));
273                         gtk_label_set_markup (GTK_LABEL (dialog->label_no_account_blurb),
274                                               _("To add a new account, you can click on the "
275                                                 "'Add' button and a new entry will be created "
276                                                 "for you to start configuring."));
277                 }
278         } else {
279                 McProfile *profile;
280                 const gchar *config_ui;
281
282                 gtk_widget_hide (dialog->frame_no_account);
283                 gtk_widget_show (dialog->vbox_details);
284
285                 profile = mc_account_get_profile (account);
286                 config_ui = mc_profile_get_configuration_ui (profile);
287
288                 if (strcmp (config_ui, "jabber") == 0) {
289                         dialog->settings_widget = 
290                                 gossip_account_widget_jabber_new (account);
291                 } else {
292                         dialog->settings_widget = 
293                                 gossip_account_widget_generic_new (account,
294                                                                    dialog->label_name);
295                 }
296                 
297                 gtk_widget_grab_focus (dialog->settings_widget);
298         }
299
300         if (dialog->settings_widget) {
301                 gtk_container_add (GTK_CONTAINER (dialog->alignment_settings),
302                                    dialog->settings_widget);
303         }
304
305         if (account) {
306                 McProfile *profile;
307
308                 profile = mc_account_get_profile (account);
309                 gtk_image_set_from_icon_name (GTK_IMAGE (dialog->image_type),
310                                               mc_profile_get_icon_name (profile),
311                                               GTK_ICON_SIZE_DIALOG);
312                 
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         const gchar               *icon_name;
412         GdkPixbuf                 *pixbuf;
413         TelepathyConnectionStatus  status;
414
415         gtk_tree_model_get (model, iter,
416                             COL_STATUS, &status,
417                             COL_ACCOUNT_POINTER, &account,
418                             -1);
419
420         icon_name = gossip_icon_name_from_account (account);
421         pixbuf = gossip_pixbuf_from_icon_name (icon_name, GTK_ICON_SIZE_BUTTON);
422
423         if (pixbuf) {
424                 if (status == TP_CONN_STATUS_DISCONNECTED ||
425                     (status == TP_CONN_STATUS_CONNECTING && 
426                      !dialog->connecting_show)) {
427                         GdkPixbuf *modded_pixbuf;
428
429                         modded_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
430                                                         TRUE,
431                                                         8,
432                                                         gdk_pixbuf_get_width (pixbuf),
433                                                         gdk_pixbuf_get_height (pixbuf));
434
435                         gdk_pixbuf_saturate_and_pixelate (pixbuf,
436                                                           modded_pixbuf,
437                                                           1.0,
438                                                           TRUE);
439                         g_object_unref (pixbuf);
440                         pixbuf = modded_pixbuf;
441                 }
442         }
443
444         g_object_set (cell,
445                       "visible", TRUE,
446                       "pixbuf", pixbuf,
447                       NULL);
448
449         g_object_unref (account);
450         if (pixbuf) {
451                 g_object_unref (pixbuf);
452         }
453 }
454
455 static McAccount *
456 accounts_dialog_model_get_selected (GossipAccountsDialog *dialog)
457 {
458         GtkTreeView      *view;
459         GtkTreeModel     *model;
460         GtkTreeSelection *selection;
461         GtkTreeIter       iter;
462         McAccount        *account;
463
464         view = GTK_TREE_VIEW (dialog->treeview);
465         selection = gtk_tree_view_get_selection (view);
466
467         if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
468                 return NULL;
469         }
470
471         gtk_tree_model_get (model, &iter, COL_ACCOUNT_POINTER, &account, -1);
472
473         return account;
474 }
475
476 static void
477 accounts_dialog_model_set_selected (GossipAccountsDialog *dialog,
478                                     McAccount            *account)
479 {
480         GtkTreeView      *view;
481         GtkTreeSelection *selection;
482         GtkTreeModel     *model;
483         GtkTreeIter       iter;
484         gboolean          ok;
485
486         view = GTK_TREE_VIEW (dialog->treeview);
487         model = gtk_tree_view_get_model (view);
488         selection = gtk_tree_view_get_selection (view);
489
490         for (ok = gtk_tree_model_get_iter_first (model, &iter);
491              ok;
492              ok = gtk_tree_model_iter_next (model, &iter)) {
493                 McAccount *this_account;
494                 gboolean   equal;
495
496                 gtk_tree_model_get (model, &iter,
497                                     COL_ACCOUNT_POINTER, &this_account,
498                                     -1);
499
500                 equal = gossip_account_equal (this_account, account);
501                 g_object_unref (this_account);
502
503                 if (equal) {
504                         gtk_tree_selection_select_iter (selection, &iter);
505                         break;
506                 }
507         }
508 }
509
510 static gboolean
511 accounts_dialog_model_remove_selected (GossipAccountsDialog *dialog)
512 {
513         GtkTreeView      *view;
514         GtkTreeModel     *model;
515         GtkTreeSelection *selection;
516         GtkTreeIter       iter;
517
518         view = GTK_TREE_VIEW (dialog->treeview);
519         selection = gtk_tree_view_get_selection (view);
520
521         if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
522                 return FALSE;
523         }
524
525         return gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
526 }
527
528 static void
529 accounts_dialog_model_selection_changed (GtkTreeSelection     *selection,
530                                          GossipAccountsDialog *dialog)
531 {
532         McAccount    *account;
533         GtkTreeModel *model;
534         GtkTreeIter   iter;
535         gboolean      is_selection;
536
537         is_selection = gtk_tree_selection_get_selected (selection, &model, &iter);
538
539         gtk_widget_set_sensitive (dialog->button_remove, is_selection);
540         gtk_widget_set_sensitive (dialog->button_connect, is_selection);
541
542         accounts_dialog_update_connect_button (dialog);
543
544         account = accounts_dialog_model_get_selected (dialog);
545         accounts_dialog_update_account (dialog, account);
546
547         if (account) {
548                 g_object_unref (account);
549         }
550 }
551
552 static void
553 accounts_dialog_add_account (GossipAccountsDialog *dialog,
554                              McAccount            *account)
555 {
556         TelepathyConnectionStatus  status;
557         const gchar               *name;
558         GtkTreeView               *view;
559         GtkTreeModel              *model;
560         GtkListStore              *store;
561         GtkTreeIter                iter;
562         gboolean                   ok;
563
564         view = GTK_TREE_VIEW (dialog->treeview);
565         model = gtk_tree_view_get_model (view);
566         store = GTK_LIST_STORE (model);
567
568         for (ok = gtk_tree_model_get_iter_first (model, &iter);
569              ok;
570              ok = gtk_tree_model_iter_next (model, &iter)) {
571                 McAccount *this_account;
572                 gboolean   equal;
573
574                 gtk_tree_model_get (model, &iter,
575                                     COL_ACCOUNT_POINTER, &this_account,
576                                     -1);
577
578                 equal =  gossip_account_equal (this_account, account);
579                 g_object_unref (this_account);
580
581                 if (equal) {
582                         return;
583                 }
584         }
585
586         status = mission_control_get_connection_status (dialog->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_insert_with_values (store, &iter,
594                                            -1,
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         McAccount *account;
619
620         account = mc_account_lookup (unique_name);
621
622         accounts_dialog_model_set_selected (dialog, account);
623         accounts_dialog_model_remove_selected (dialog);
624
625         g_object_unref (account);
626 }
627
628 static gboolean
629 accounts_dialog_row_changed_foreach (GtkTreeModel *model,
630                                      GtkTreePath  *path,
631                                      GtkTreeIter  *iter,
632                                      gpointer      user_data)
633 {
634         gtk_tree_model_row_changed (model, path, iter);
635
636         return FALSE;
637 }
638
639 static gboolean
640 accounts_dialog_flash_connecting_cb (GossipAccountsDialog *dialog)
641 {
642         GtkTreeView  *view;
643         GtkTreeModel *model;
644
645         dialog->connecting_show = !dialog->connecting_show;
646
647         view = GTK_TREE_VIEW (dialog->treeview);
648         model = gtk_tree_view_get_model (view);
649
650         gtk_tree_model_foreach (model, accounts_dialog_row_changed_foreach, NULL);
651
652         return TRUE;
653 }
654
655 static void
656 accounts_dialog_status_changed_cb (MissionControl                  *mc,
657                                    TelepathyConnectionStatus        status,
658                                    McPresence                       presence,
659                                    TelepathyConnectionStatusReason  reason,
660                                    const gchar                     *unique_name,
661                                    GossipAccountsDialog            *dialog)
662 {
663         GtkTreeView      *view;
664         GtkTreeSelection *selection;
665         GtkTreeModel     *model;
666         GtkTreeIter       iter;
667         gboolean          ok;
668         McAccount        *account;
669         GList            *accounts, *l;
670         gboolean          found = FALSE;
671         
672         /* Update the status in the model */
673         view = GTK_TREE_VIEW (dialog->treeview);
674         selection = gtk_tree_view_get_selection (view);
675         model = gtk_tree_view_get_model (view);
676         account = mc_account_lookup (unique_name);
677
678         gossip_debug (DEBUG_DOMAIN, "Status changed for account %s: "
679                       "status=%d presence=%d",
680                       unique_name, status, presence);
681
682         for (ok = gtk_tree_model_get_iter_first (model, &iter);
683              ok;
684              ok = gtk_tree_model_iter_next (model, &iter)) {
685                 McAccount *this_account;
686                 gboolean   equal;
687
688                 gtk_tree_model_get (model, &iter,
689                                     COL_ACCOUNT_POINTER, &this_account,
690                                     -1);
691
692                 equal = gossip_account_equal (this_account, account);
693                 g_object_unref (this_account);
694
695                 if (equal) {
696                         GtkTreePath *path;
697
698                         gtk_list_store_set (GTK_LIST_STORE (model), &iter,
699                                             COL_STATUS, status,
700                                             -1);
701
702                         path = gtk_tree_model_get_path (model, &iter);
703                         gtk_tree_model_row_changed (model, path, &iter);
704                         gtk_tree_path_free (path);
705
706                         break;
707                 }
708         }
709
710         g_object_unref (account);
711
712         /* Start to flash account if status is connecting */
713         if (status == TP_CONN_STATUS_CONNECTING) {
714                 if (!dialog->connecting_id) {
715                         dialog->connecting_id = g_timeout_add (FLASH_TIMEOUT,
716                                                                (GSourceFunc) accounts_dialog_flash_connecting_cb,
717                                                                dialog);
718                 }
719
720                 return;
721         }
722
723         /* Stop to flash if no account is connecting */
724         accounts = mc_accounts_list ();
725         for (l = accounts; l; l = l->next) {
726                 McAccount *this_account;
727
728                 this_account = l->data;
729
730                 if (mission_control_get_connection_status (mc, this_account, NULL) == TP_CONN_STATUS_CONNECTING) {
731                         found = TRUE;
732                         break;
733                 }
734
735                 g_object_unref (this_account);
736         }
737         g_list_free (accounts);
738
739         if (!found && dialog->connecting_id) {
740                 g_source_remove (dialog->connecting_id);
741                 dialog->connecting_id = 0;
742         }
743
744         gtk_widget_show (dialog->window);
745 }
746
747 static void          
748 accounts_dialog_entry_name_changed_cb (GtkWidget             *widget,
749                                        GossipAccountsDialog  *dialog)
750 {
751         const gchar *str;
752         
753         str = gtk_entry_get_text (GTK_ENTRY (widget));
754         gtk_widget_set_sensitive (dialog->button_create, !G_STR_EMPTY (str));
755 }
756
757 static void
758 accounts_dialog_button_create_clicked_cb (GtkWidget             *button,
759                                           GossipAccountsDialog  *dialog)
760 {
761         McProfile   *profile;
762         McAccount   *account;
763         const gchar *str;
764
765         /* Update widgets */
766         gtk_widget_show (dialog->vbox_details);
767         gtk_widget_hide (dialog->frame_no_account);
768         gtk_widget_hide (dialog->frame_new_account);
769
770         profile = gossip_profile_chooser_get_selected (dialog->combobox_profile);
771
772         /* Create account */
773         account = mc_account_create (profile);
774
775         str = gtk_entry_get_text (GTK_ENTRY (dialog->entry_name));
776         mc_account_set_display_name (account, str);
777
778         accounts_dialog_add_account (dialog, account);
779         accounts_dialog_model_set_selected (dialog, account);
780
781         g_object_unref (account);
782         g_object_unref (profile);
783 }
784
785 static void
786 accounts_dialog_button_back_clicked_cb (GtkWidget             *button,
787                                         GossipAccountsDialog  *dialog)
788 {
789         McAccount *account;
790
791         gtk_widget_hide (dialog->vbox_details);
792         gtk_widget_hide (dialog->frame_no_account);
793         gtk_widget_hide (dialog->frame_new_account);
794
795         account = accounts_dialog_model_get_selected (dialog);
796         accounts_dialog_update_account (dialog, account);
797 }
798
799 static void
800 accounts_dialog_button_connect_clicked_cb (GtkWidget            *button,
801                                            GossipAccountsDialog *dialog)
802 {
803         McAccount *account;
804         gboolean   enable;
805
806         account = accounts_dialog_model_get_selected (dialog);
807         enable = (!mc_account_is_enabled (account));
808         mc_account_set_enabled (account, enable);
809         accounts_dialog_update_connect_button (dialog);
810
811         g_object_unref (account);
812 }
813
814 static void
815 accounts_dialog_button_add_clicked_cb (GtkWidget            *button,
816                                        GossipAccountsDialog *dialog)
817 {
818         gtk_widget_hide (dialog->vbox_details);
819         gtk_widget_hide (dialog->frame_no_account);
820         gtk_widget_show (dialog->frame_new_account);
821
822         gtk_combo_box_set_active (GTK_COMBO_BOX (dialog->combobox_profile), 0);
823         gtk_entry_set_text (GTK_ENTRY (dialog->entry_name), "");
824         gtk_widget_grab_focus (dialog->entry_name);
825 }
826
827 static void
828 accounts_dialog_remove_response_cb (GtkWidget *dialog,
829                                     gint       response,
830                                     McAccount *account)
831 {
832         if (response == GTK_RESPONSE_YES) {
833                 mc_account_delete (account);
834         }
835
836         gtk_widget_destroy (dialog);
837 }
838
839 static void
840 accounts_dialog_button_remove_clicked_cb (GtkWidget            *button,
841                                           GossipAccountsDialog *dialog)
842 {
843         McAccount *account;
844         GtkWidget *message_dialog;
845
846         account = accounts_dialog_model_get_selected (dialog);
847
848         if (!mc_account_is_complete (account)) {
849                 accounts_dialog_model_remove_selected (dialog);
850                 return;
851         }
852         message_dialog = gtk_message_dialog_new
853                 (GTK_WINDOW (dialog->window),
854                  GTK_DIALOG_DESTROY_WITH_PARENT,
855                  GTK_MESSAGE_QUESTION,
856                  GTK_BUTTONS_NONE,
857                  _("You are about to remove your %s account!\n"
858                    "Are you sure you want to proceed?"),
859                  mc_account_get_display_name (account));
860
861         gtk_message_dialog_format_secondary_text
862                 (GTK_MESSAGE_DIALOG (message_dialog),
863                  _("Any associated conversations and chat rooms will NOT be "
864                    "removed if you decide to proceed.\n"
865                    "\n"
866                    "Should you decide to add the account back at a later time, "
867                    "they will still be available."));
868
869         gtk_dialog_add_button (GTK_DIALOG (message_dialog),
870                                GTK_STOCK_CANCEL, 
871                                GTK_RESPONSE_NO);
872         gtk_dialog_add_button (GTK_DIALOG (message_dialog),
873                                GTK_STOCK_REMOVE, 
874                                GTK_RESPONSE_YES);
875
876         g_signal_connect (message_dialog, "response",
877                           G_CALLBACK (accounts_dialog_remove_response_cb),
878                           account);
879
880         gtk_widget_show (message_dialog);
881 }
882
883 static void
884 accounts_dialog_treeview_row_activated_cb (GtkTreeView          *tree_view,
885                                            GtkTreePath          *path,
886                                            GtkTreeViewColumn    *column,
887                                            GossipAccountsDialog *dialog)
888 {
889
890         accounts_dialog_button_connect_clicked_cb (dialog->button_connect,
891                                                    dialog);
892 }
893
894 static void
895 accounts_dialog_response_cb (GtkWidget            *widget,
896                              gint                  response,
897                              GossipAccountsDialog *dialog)
898 {
899         gtk_widget_destroy (widget);
900 }
901
902 static void
903 accounts_dialog_destroy_cb (GtkWidget            *widget,
904                             GossipAccountsDialog *dialog)
905 {
906         GList *accounts, *l;
907
908         /* Disconnect signals */
909         g_signal_handlers_disconnect_by_func (dialog->monitor,
910                                               accounts_dialog_account_added_cb,
911                                               dialog);
912         g_signal_handlers_disconnect_by_func (dialog->monitor,
913                                               accounts_dialog_account_removed_cb,
914                                               dialog);
915         dbus_g_proxy_disconnect_signal (DBUS_G_PROXY (dialog->mc),
916                                         "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_object_unref (dialog->mc);
941         g_object_unref (dialog->monitor);
942         
943         g_free (dialog);
944 }
945
946 GtkWidget *
947 gossip_accounts_dialog_show (void)
948 {
949         static GossipAccountsDialog *dialog = NULL;
950         GladeXML                    *glade;
951         GtkWidget                   *bbox;
952         GtkWidget                   *button_close;
953
954         if (dialog) {
955                 gtk_window_present (GTK_WINDOW (dialog->window));
956                 return dialog->window;
957         }
958
959         dialog = g_new0 (GossipAccountsDialog, 1);
960
961         glade = gossip_glade_get_file ("gossip-accounts-dialog.glade",
962                                        "accounts_dialog",
963                                        NULL,
964                                        "accounts_dialog", &dialog->window,
965                                        "vbox_details", &dialog->vbox_details,
966                                        "frame_no_account", &dialog->frame_no_account,
967                                        "label_no_account", &dialog->label_no_account,
968                                        "label_no_account_blurb", &dialog->label_no_account_blurb,
969                                        "alignment_settings", &dialog->alignment_settings,
970                                        "dialog-action_area", &bbox,
971                                        "treeview", &dialog->treeview,
972                                        "frame_new_account", &dialog->frame_new_account,
973                                        "entry_name", &dialog->entry_name,
974                                        "table_new_account", &dialog->table_new_account,
975                                        "button_create", &dialog->button_create,
976                                        "button_back", &dialog->button_back,
977                                        "image_type", &dialog->image_type,
978                                        "label_type", &dialog->label_type,
979                                        "label_name", &dialog->label_name,
980                                        "button_remove", &dialog->button_remove,
981                                        "button_connect", &dialog->button_connect,
982                                        "button_close", &button_close,
983                                        NULL);
984
985         gossip_glade_connect (glade,
986                               dialog,
987                               "accounts_dialog", "destroy", accounts_dialog_destroy_cb,
988                               "accounts_dialog", "response", accounts_dialog_response_cb,
989                               "button_create", "clicked", accounts_dialog_button_create_clicked_cb,
990                               "button_back", "clicked", accounts_dialog_button_back_clicked_cb,
991                               "entry_name", "changed", accounts_dialog_entry_name_changed_cb,
992                               "treeview", "row-activated", accounts_dialog_treeview_row_activated_cb,
993                               "button_connect", "clicked", accounts_dialog_button_connect_clicked_cb,
994                               "button_add", "clicked", accounts_dialog_button_add_clicked_cb,
995                               "button_remove", "clicked", accounts_dialog_button_remove_clicked_cb,
996                               NULL);
997
998         g_object_add_weak_pointer (G_OBJECT (dialog->window), (gpointer) &dialog);
999
1000         g_object_unref (glade);
1001
1002         /* Create profile chooser */
1003         dialog->combobox_profile = gossip_profile_chooser_new ();
1004         gtk_table_attach_defaults (GTK_TABLE (dialog->table_new_account),
1005                                    dialog->combobox_profile,
1006                                    1, 2,
1007                                    0, 1);
1008         gtk_widget_show (dialog->combobox_profile);
1009
1010         /* Set up signalling */
1011         dialog->mc = gossip_mission_control_new ();
1012         dialog->monitor = mc_account_monitor_new ();
1013
1014         /* FIXME: connect account-enabled/disabled too */
1015         g_signal_connect (dialog->monitor, "account-created",
1016                           G_CALLBACK (accounts_dialog_account_added_cb),
1017                           dialog);
1018         g_signal_connect (dialog->monitor, "account-deleted",
1019                           G_CALLBACK (accounts_dialog_account_removed_cb),
1020                           dialog);
1021         dbus_g_proxy_connect_signal (DBUS_G_PROXY (dialog->mc), "AccountStatusChanged",
1022                                      G_CALLBACK (accounts_dialog_status_changed_cb),
1023                                      dialog, NULL);
1024
1025         accounts_dialog_model_setup (dialog);
1026         accounts_dialog_setup (dialog);
1027         accounts_dialog_model_select_first (dialog);
1028
1029         gtk_widget_show (dialog->window);
1030
1031         return dialog->window;
1032 }
1033