]> git.0d.be Git - empathy.git/blob - src/empathy-accounts-dialog.c
When creating an account set its display name to "New Jabber account", when the accou...
[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         const gchar *current_name;
640         gchar       *account_param = NULL;
641
642         account = mc_account_lookup (unique_name);
643         accounts_dialog_add_or_update_account (dialog, account);
644
645         /* Change the display name to "%s (%s)" % (protocol, account).
646          *  - The protocol is the display name of the profile.
647          *  - The account should be the normalized name of the McAccount but
648          *    it's not set until first connection, so we get the "account"
649          *    parameter for CM that have it. */
650         current_name = mc_account_get_display_name (account);
651         mc_account_get_param_string (account, "account", &account_param);
652         if (!G_STR_EMPTY (account_param)) {
653                 McProfile   *profile;
654                 const gchar *profile_name;
655                 gchar       *new_name;
656
657                 profile = mc_account_get_profile (account);
658                 profile_name = mc_profile_get_display_name (profile);
659                 new_name = g_strdup_printf ("%s (%s)", profile_name,
660                                             account_param);
661
662                 DEBUG ("Setting new display name for account %s: '%s'",
663                        unique_name, new_name);
664
665                 mc_account_set_display_name (account, new_name);
666                 g_free (new_name);
667                 g_object_unref (profile);
668         } else {
669                 /* FIXME: This CM has no account parameter, what can be done? */
670         }
671         g_free (account_param);
672         g_object_unref (account);
673 }
674
675 static void
676 accounts_dialog_account_removed_cb (McAccountMonitor     *monitor,
677                                     gchar                *unique_name,
678                                     EmpathyAccountsDialog *dialog)
679 {
680         McAccount *account;
681
682         account = mc_account_lookup (unique_name);
683
684         accounts_dialog_model_set_selected (dialog, account);
685         accounts_dialog_model_remove_selected (dialog);
686
687         g_object_unref (account);
688 }
689
690 static gboolean
691 accounts_dialog_row_changed_foreach (GtkTreeModel *model,
692                                      GtkTreePath  *path,
693                                      GtkTreeIter  *iter,
694                                      gpointer      user_data)
695 {
696         gtk_tree_model_row_changed (model, path, iter);
697
698         return FALSE;
699 }
700
701 static gboolean
702 accounts_dialog_flash_connecting_cb (EmpathyAccountsDialog *dialog)
703 {
704         GtkTreeView  *view;
705         GtkTreeModel *model;
706
707         dialog->connecting_show = !dialog->connecting_show;
708
709         view = GTK_TREE_VIEW (dialog->treeview);
710         model = gtk_tree_view_get_model (view);
711
712         gtk_tree_model_foreach (model, accounts_dialog_row_changed_foreach, NULL);
713
714         return TRUE;
715 }
716
717 static gboolean
718 accounts_dialog_are_accounts_connecting (MissionControl *mc)
719 {
720         GList    *accounts, *l;
721         gboolean  found = FALSE;
722
723         /* Check if there is still accounts in CONNECTING state */
724         accounts = mc_accounts_list_by_enabled (TRUE);
725         for (l = accounts; l; l = l->next) {
726                 McAccount          *this_account = l->data;
727                 TpConnectionStatus  status;
728
729                 status = mission_control_get_connection_status (mc, this_account,
730                                                                 NULL);
731                 if (status == TP_CONNECTION_STATUS_CONNECTING) {
732                         found = TRUE;
733                         break;
734                 }
735         }
736         mc_accounts_list_free (accounts);
737
738         return found;
739 }
740
741 static void
742 accounts_dialog_status_changed_cb (MissionControl           *mc,
743                                    TpConnectionStatus        status,
744                                    McPresence                presence,
745                                    TpConnectionStatusReason  reason,
746                                    const gchar              *unique_name,
747                                    EmpathyAccountsDialog    *dialog)
748 {
749         GtkTreeModel *model;
750         GtkTreeIter   iter;
751         McAccount    *account;
752         gboolean      found = FALSE;
753         
754         /* Update the status in the model */
755         model = gtk_tree_view_get_model (GTK_TREE_VIEW (dialog->treeview));
756         account = mc_account_lookup (unique_name);
757         if (!account) {
758                 return;
759         }
760
761         DEBUG ("Status changed for account %s: status=%d presence=%d",
762                 unique_name, status, presence);
763
764         if (accounts_dialog_get_account_iter (dialog, account, &iter)) {
765                 GtkTreePath *path;
766
767                 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
768                                     COL_STATUS, status,
769                                     -1);
770
771                 path = gtk_tree_model_get_path (model, &iter);
772                 gtk_tree_model_row_changed (model, path, &iter);
773                 gtk_tree_path_free (path);
774         }
775         g_object_unref (account);
776
777         found = accounts_dialog_are_accounts_connecting (mc);
778
779         if (!found && dialog->connecting_id) {
780                 g_source_remove (dialog->connecting_id);
781                 dialog->connecting_id = 0;
782         }
783         if (found && !dialog->connecting_id) {
784                 dialog->connecting_id = g_timeout_add (FLASH_TIMEOUT,
785                                                        (GSourceFunc) accounts_dialog_flash_connecting_cb,
786                                                        dialog);
787         }
788 }
789
790 static void
791 accounts_dialog_account_enabled_cb (McAccountMonitor      *monitor,
792                                     gchar                 *unique_name,
793                                     EmpathyAccountsDialog *dialog)
794 {
795         GtkTreeModel *model;
796         GtkTreeIter   iter;
797         McAccount    *account;
798         gboolean      enabled;
799
800         /* Update the status in the model */
801         model = gtk_tree_view_get_model (GTK_TREE_VIEW (dialog->treeview));
802         account = mc_account_lookup (unique_name);
803         enabled = mc_account_is_enabled (account);
804
805         DEBUG ("Account %s is now %s",
806                 mc_account_get_display_name (account),
807                 enabled ? "enabled" : "disabled");
808
809         if (accounts_dialog_get_account_iter (dialog, account, &iter)) {
810                 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
811                                     COL_ENABLED, enabled,
812                                     -1);
813         }
814
815         g_object_unref (account);
816 }
817
818 static void
819 accounts_dialog_button_create_clicked_cb (GtkWidget             *button,
820                                           EmpathyAccountsDialog  *dialog)
821 {
822         McProfile *profile;
823         McAccount *account;
824         gchar     *str;
825         McProfileCapabilityFlags cap;
826
827         profile = empathy_profile_chooser_get_selected (dialog->combobox_profile);
828
829         /* Create account */
830         account = mc_account_create (profile);
831         /* To translator: %s is the protocol name */
832         str = g_strdup_printf (_("New %s account"),
833                                mc_profile_get_display_name (profile));
834         mc_account_set_display_name (account, str);
835         g_free (str);
836
837         cap = mc_profile_get_capabilities (profile);
838         if (cap & MC_PROFILE_CAPABILITY_REGISTRATION_UI) {
839                 gboolean active;
840
841                 active = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->checkbutton_register));
842                 if (!active) {
843                         mc_account_set_param_boolean (account, "register", TRUE);
844                 }
845         }
846
847         accounts_dialog_add_or_update_account (dialog, account);
848         accounts_dialog_model_set_selected (dialog, account);
849
850         g_object_unref (account);
851         g_object_unref (profile);
852 }
853
854 static void
855 accounts_dialog_button_back_clicked_cb (GtkWidget             *button,
856                                         EmpathyAccountsDialog  *dialog)
857 {
858         McAccount *account;
859
860         account = accounts_dialog_model_get_selected (dialog);
861         accounts_dialog_update_account (dialog, account);
862 }
863
864 static void
865 accounts_dialog_profile_changed_cb (GtkWidget             *widget,
866                                     EmpathyAccountsDialog *dialog)
867 {
868         McProfile *profile;
869         McProfileCapabilityFlags cap;
870
871         profile = empathy_profile_chooser_get_selected (dialog->combobox_profile);
872         cap = mc_profile_get_capabilities (profile);
873
874         if (cap & MC_PROFILE_CAPABILITY_REGISTRATION_UI) {
875                 gtk_widget_show (dialog->checkbutton_register);
876         } else {
877                 gtk_widget_hide (dialog->checkbutton_register);
878         }
879         g_object_unref (profile);
880 }
881
882 static void
883 accounts_dialog_button_add_clicked_cb (GtkWidget             *button,
884                                        EmpathyAccountsDialog *dialog)
885 {
886         GtkTreeView      *view;
887         GtkTreeSelection *selection;
888         GtkTreeModel     *model;
889
890         view = GTK_TREE_VIEW (dialog->treeview);
891         model = gtk_tree_view_get_model (view);
892         selection = gtk_tree_view_get_selection (view);
893         gtk_tree_selection_unselect_all (selection);
894
895         gtk_widget_set_sensitive (dialog->button_add, FALSE);
896         gtk_widget_set_sensitive (dialog->button_remove, FALSE);
897         gtk_widget_hide (dialog->vbox_details);
898         gtk_widget_hide (dialog->frame_no_profile);
899         gtk_widget_show (dialog->frame_new_account);
900
901         /* If we have no account, no need of a back button */
902         if (gtk_tree_model_iter_n_children (model, NULL) > 0) {
903                 gtk_widget_show (dialog->button_back);
904         } else {
905                 gtk_widget_hide (dialog->button_back);
906         }
907
908         accounts_dialog_profile_changed_cb (dialog->checkbutton_register, dialog);
909         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->checkbutton_register),
910                                       TRUE);
911         gtk_combo_box_set_active (GTK_COMBO_BOX (dialog->combobox_profile), 0);
912         gtk_widget_grab_focus (dialog->combobox_profile);
913 }
914
915 static void
916 accounts_dialog_button_help_clicked_cb (GtkWidget             *button,
917                                         EmpathyAccountsDialog *dialog)
918 {
919         empathy_url_show ("ghelp:empathy?empathy-create-account");
920 }
921
922 static void
923 accounts_dialog_button_remove_clicked_cb (GtkWidget            *button,
924                                           EmpathyAccountsDialog *dialog)
925 {
926         McAccount *account;
927         GtkWidget *message_dialog;
928         gint       res;
929
930         account = accounts_dialog_model_get_selected (dialog);
931
932         if (!mc_account_is_complete (account)) {
933                 accounts_dialog_model_remove_selected (dialog);
934                 accounts_dialog_model_select_first (dialog);
935                 return;
936         }
937         message_dialog = gtk_message_dialog_new
938                 (GTK_WINDOW (dialog->window),
939                  GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
940                  GTK_MESSAGE_QUESTION,
941                  GTK_BUTTONS_NONE,
942                  _("You are about to remove your %s account!\n"
943                    "Are you sure you want to proceed?"),
944                  mc_account_get_display_name (account));
945
946         gtk_message_dialog_format_secondary_text
947                 (GTK_MESSAGE_DIALOG (message_dialog),
948                  _("Any associated conversations and chat rooms will NOT be "
949                    "removed if you decide to proceed.\n"
950                    "\n"
951                    "Should you decide to add the account back at a later time, "
952                    "they will still be available."));
953
954         gtk_dialog_add_button (GTK_DIALOG (message_dialog),
955                                GTK_STOCK_CANCEL, 
956                                GTK_RESPONSE_NO);
957         gtk_dialog_add_button (GTK_DIALOG (message_dialog),
958                                GTK_STOCK_REMOVE, 
959                                GTK_RESPONSE_YES);
960
961         gtk_widget_show (message_dialog);
962         res = gtk_dialog_run (GTK_DIALOG (message_dialog));
963
964         if (res == GTK_RESPONSE_YES) {
965                 mc_account_delete (account);
966                 accounts_dialog_model_select_first (dialog);
967         }
968         gtk_widget_destroy (message_dialog);
969 }
970
971 static void
972 accounts_dialog_response_cb (GtkWidget            *widget,
973                              gint                  response,
974                              EmpathyAccountsDialog *dialog)
975 {
976         if (response == GTK_RESPONSE_CLOSE) {
977                 gtk_widget_destroy (widget);
978         }
979 }
980
981 static void
982 accounts_dialog_destroy_cb (GtkWidget            *widget,
983                             EmpathyAccountsDialog *dialog)
984 {
985         GList *accounts, *l;
986
987         /* Disconnect signals */
988         g_signal_handlers_disconnect_by_func (dialog->monitor,
989                                               accounts_dialog_account_added_cb,
990                                               dialog);
991         g_signal_handlers_disconnect_by_func (dialog->monitor,
992                                               accounts_dialog_account_removed_cb,
993                                               dialog);
994         g_signal_handlers_disconnect_by_func (dialog->monitor,
995                                               accounts_dialog_account_enabled_cb,
996                                               dialog);
997         empathy_disconnect_account_status_changed (dialog->token);
998
999         /* Delete incomplete accounts */
1000         accounts = mc_accounts_list ();
1001         for (l = accounts; l; l = l->next) {
1002                 McAccount *account;
1003
1004                 account = l->data;
1005                 if (!mc_account_is_complete (account)) {
1006                         /* FIXME: Warn the user the account is not complete
1007                          *        and is going to be removed. */
1008                         mc_account_delete (account);
1009                 }
1010
1011                 g_object_unref (account);
1012         }
1013         g_list_free (accounts);
1014
1015         if (dialog->connecting_id) {
1016                 g_source_remove (dialog->connecting_id);
1017         }
1018
1019         g_object_unref (dialog->mc);
1020         g_object_unref (dialog->monitor);
1021         
1022         g_free (dialog);
1023 }
1024
1025 GtkWidget *
1026 empathy_accounts_dialog_show (GtkWindow *parent,
1027                               McAccount *selected_account)
1028 {
1029         static EmpathyAccountsDialog *dialog = NULL;
1030         GladeXML                     *glade;
1031         gchar                        *filename;
1032         GList                        *accounts, *l;
1033
1034         if (dialog) {
1035                 gtk_window_present (GTK_WINDOW (dialog->window));
1036                 return dialog->window;
1037         }
1038
1039         dialog = g_new0 (EmpathyAccountsDialog, 1);
1040
1041         filename = empathy_file_lookup ("empathy-accounts-dialog.glade",
1042                                         "src");
1043         glade = empathy_glade_get_file (filename,
1044                                        "accounts_dialog",
1045                                        NULL,
1046                                        "accounts_dialog", &dialog->window,
1047                                        "vbox_details", &dialog->vbox_details,
1048                                        "frame_no_profile", &dialog->frame_no_profile,
1049                                        "alignment_settings", &dialog->alignment_settings,
1050                                        "treeview", &dialog->treeview,
1051                                        "frame_new_account", &dialog->frame_new_account,
1052                                        "hbox_type", &dialog->hbox_type,
1053                                        "button_create", &dialog->button_create,
1054                                        "button_back", &dialog->button_back,
1055                                        "checkbutton_register", &dialog->checkbutton_register,
1056                                        "image_type", &dialog->image_type,
1057                                        "label_name", &dialog->label_name,
1058                                        "button_add", &dialog->button_add,
1059                                        "button_remove", &dialog->button_remove,
1060                                        NULL);
1061         g_free (filename);
1062
1063         empathy_glade_connect (glade,
1064                               dialog,
1065                               "accounts_dialog", "destroy", accounts_dialog_destroy_cb,
1066                               "accounts_dialog", "response", accounts_dialog_response_cb,
1067                               "button_create", "clicked", accounts_dialog_button_create_clicked_cb,
1068                               "button_back", "clicked", accounts_dialog_button_back_clicked_cb,
1069                               "button_add", "clicked", accounts_dialog_button_add_clicked_cb,
1070                               "button_remove", "clicked", accounts_dialog_button_remove_clicked_cb,
1071                               "button_help", "clicked", accounts_dialog_button_help_clicked_cb,
1072                               NULL);
1073
1074         g_object_add_weak_pointer (G_OBJECT (dialog->window), (gpointer) &dialog);
1075
1076         g_object_unref (glade);
1077
1078         /* Create profile chooser */
1079         dialog->combobox_profile = empathy_profile_chooser_new ();
1080         gtk_box_pack_end (GTK_BOX (dialog->hbox_type),
1081                           dialog->combobox_profile,
1082                           TRUE, TRUE, 0);
1083         gtk_widget_show (dialog->combobox_profile);
1084         g_signal_connect (dialog->combobox_profile, "changed",
1085                           G_CALLBACK (accounts_dialog_profile_changed_cb),
1086                           dialog);
1087
1088         /* Set up signalling */
1089         dialog->mc = empathy_mission_control_new ();
1090         dialog->monitor = mc_account_monitor_new ();
1091
1092         g_signal_connect (dialog->monitor, "account-created",
1093                           G_CALLBACK (accounts_dialog_account_added_cb),
1094                           dialog);
1095         g_signal_connect (dialog->monitor, "account-deleted",
1096                           G_CALLBACK (accounts_dialog_account_removed_cb),
1097                           dialog);
1098         g_signal_connect (dialog->monitor, "account-enabled",
1099                           G_CALLBACK (accounts_dialog_account_enabled_cb),
1100                           dialog);
1101         g_signal_connect (dialog->monitor, "account-disabled",
1102                           G_CALLBACK (accounts_dialog_account_enabled_cb),
1103                           dialog);
1104         dialog->token = empathy_connect_to_account_status_changed (dialog->mc,
1105                                                    G_CALLBACK (accounts_dialog_status_changed_cb),
1106                                                    dialog, NULL);
1107
1108         accounts_dialog_model_setup (dialog);
1109
1110         /* Add existing accounts */
1111         accounts = mc_accounts_list ();
1112         for (l = accounts; l; l = l->next) {
1113                 accounts_dialog_add_or_update_account (dialog, l->data);
1114                 g_object_unref (l->data);
1115         }
1116         g_list_free (accounts);
1117
1118         if (selected_account) {
1119                 accounts_dialog_model_set_selected (dialog, selected_account);
1120         } else {
1121                 accounts_dialog_model_select_first (dialog);
1122         }
1123
1124         if (parent) {
1125                 gtk_window_set_transient_for (GTK_WINDOW (dialog->window),
1126                                               GTK_WINDOW (parent));
1127         }
1128
1129         gtk_widget_show (dialog->window);
1130
1131         return dialog->window;
1132 }
1133