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