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