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