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