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