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