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