]> git.0d.be Git - empathy.git/blob - src/empathy-accounts-dialog.c
Initial port of Account and AccountManager to MC5
[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         const 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 }
667
668 static void
669 accounts_dialog_account_removed_cb (EmpathyAccountManager *manager,
670                                     EmpathyAccount       *account,
671                                     EmpathyAccountsDialog *dialog)
672 {
673
674         accounts_dialog_model_set_selected (dialog, account);
675         accounts_dialog_model_remove_selected (dialog);
676 }
677
678 static gboolean
679 accounts_dialog_row_changed_foreach (GtkTreeModel *model,
680                                      GtkTreePath  *path,
681                                      GtkTreeIter  *iter,
682                                      gpointer      user_data)
683 {
684         gtk_tree_model_row_changed (model, path, iter);
685
686         return FALSE;
687 }
688
689 static gboolean
690 accounts_dialog_flash_connecting_cb (EmpathyAccountsDialog *dialog)
691 {
692         GtkTreeView  *view;
693         GtkTreeModel *model;
694
695         dialog->connecting_show = !dialog->connecting_show;
696
697         view = GTK_TREE_VIEW (dialog->treeview);
698         model = gtk_tree_view_get_model (view);
699
700         gtk_tree_model_foreach (model, accounts_dialog_row_changed_foreach, NULL);
701
702         return TRUE;
703 }
704
705 static void
706 accounts_dialog_connection_changed_cb     (EmpathyAccountManager    *manager,
707                                            EmpathyAccount           *account,
708                                            TpConnectionStatusReason  reason,
709                                            TpConnectionStatus        current,
710                                            TpConnectionStatus        previous,
711                                            EmpathyAccountsDialog    *dialog)
712 {
713         GtkTreeModel *model;
714         GtkTreeIter   iter;
715         gboolean      found;
716
717         /* Update the status in the model */
718         model = gtk_tree_view_get_model (GTK_TREE_VIEW (dialog->treeview));
719
720         if (accounts_dialog_get_account_iter (dialog, account, &iter)) {
721                 GtkTreePath *path;
722
723                 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
724                                     COL_STATUS, current,
725                                     -1);
726
727                 path = gtk_tree_model_get_path (model, &iter);
728                 gtk_tree_model_row_changed (model, path, &iter);
729                 gtk_tree_path_free (path);
730         }
731
732         found = (empathy_account_manager_get_connecting_accounts (manager) > 0);
733
734         if (!found && dialog->connecting_id) {
735                 g_source_remove (dialog->connecting_id);
736                 dialog->connecting_id = 0;
737         }
738
739         if (found && !dialog->connecting_id) {
740                 dialog->connecting_id = g_timeout_add (FLASH_TIMEOUT,
741                                                        (GSourceFunc) accounts_dialog_flash_connecting_cb,
742                                                        dialog);
743         }
744 }
745
746 static void
747 enable_or_disable_account (EmpathyAccountsDialog *dialog,
748                            EmpathyAccount *account,
749                            gboolean enabled)
750 {
751         GtkTreeModel *model;
752         GtkTreeIter   iter;
753
754         /* Update the status in the model */
755         model = gtk_tree_view_get_model (GTK_TREE_VIEW (dialog->treeview));
756
757         DEBUG ("Account %s is now %s",
758                 empathy_account_get_display_name (account),
759                 enabled ? "enabled" : "disabled");
760
761         if (accounts_dialog_get_account_iter (dialog, account, &iter)) {
762                 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
763                                     COL_ENABLED, enabled,
764                                     -1);
765         }
766 }
767
768 static void
769 accounts_dialog_account_disabled_cb (EmpathyAccountManager *manager,
770                                      EmpathyAccount        *account,
771                                      EmpathyAccountsDialog *dialog)
772 {
773         enable_or_disable_account (dialog, account, FALSE);
774 }
775
776 static void
777 accounts_dialog_account_enabled_cb (EmpathyAccountManager *manager,
778                                     EmpathyAccount        *account,
779                                     EmpathyAccountsDialog *dialog)
780 {
781         enable_or_disable_account (dialog, account, TRUE);
782 }
783
784 static void
785 accounts_dialog_account_changed_cb (EmpathyAccountManager *manager,
786                                     EmpathyAccount        *account,
787                                     EmpathyAccountsDialog  *dialog)
788 {
789         EmpathyAccount *selected_account;
790
791         accounts_dialog_add_or_update_account (dialog, account);
792         selected_account = accounts_dialog_model_get_selected (dialog);
793         if (empathy_account_equal (account, selected_account)) {
794                 accounts_dialog_update_name_label (dialog, account);
795         }
796 }
797
798 static void
799 accounts_dialog_button_create_clicked_cb (GtkWidget             *button,
800                                           EmpathyAccountsDialog  *dialog)
801 {
802         EmpathyAccount *account;
803         gchar     *str;
804         TpConnectionManager *cm;
805         TpConnectionManagerProtocol *proto;
806
807         cm = empathy_protocol_chooser_dup_selected (
808             EMPATHY_PROTOCOL_CHOOSER (dialog->combobox_protocol), &proto);
809
810         /* Create account */
811         /* To translator: %s is the protocol name */
812         str = g_strdup_printf (_("New %s account"), proto->name);
813
814         account = empathy_account_manager_create (dialog->account_manager,
815                 cm->name, proto->name, str);
816
817         g_free (str);
818
819         if (account == NULL) {
820                 /* We can't display an error to the user as MC doesn't give us
821                  * any clue about the reason of the failure... */
822                 return;
823         }
824
825         if (tp_connection_manager_protocol_can_register (proto)) {
826                 gboolean active;
827
828                 active = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->radiobutton_register));
829                 if (active) {
830                         empathy_account_set_param_boolean (account, "register", TRUE);
831                 }
832         }
833
834         accounts_dialog_add_or_update_account (dialog, account);
835         accounts_dialog_model_set_selected (dialog, account);
836
837         g_object_unref (account);
838         g_object_unref (cm);
839 }
840
841 static void
842 accounts_dialog_button_back_clicked_cb (GtkWidget             *button,
843                                         EmpathyAccountsDialog  *dialog)
844 {
845         EmpathyAccount *account;
846
847         account = accounts_dialog_model_get_selected (dialog);
848         accounts_dialog_update_account (dialog, account);
849 }
850
851 static void
852 accounts_dialog_protocol_changed_cb (GtkWidget             *widget,
853                                     EmpathyAccountsDialog *dialog)
854 {
855         TpConnectionManager *cm;
856         TpConnectionManagerProtocol *proto;
857
858         cm = empathy_protocol_chooser_dup_selected (
859             EMPATHY_PROTOCOL_CHOOSER (dialog->combobox_protocol), &proto);
860
861         if (tp_connection_manager_protocol_can_register (proto)) {
862                 gtk_widget_show (dialog->radiobutton_register);
863                 gtk_widget_show (dialog->radiobutton_reuse);
864         } else {
865                 gtk_widget_hide (dialog->radiobutton_register);
866                 gtk_widget_hide (dialog->radiobutton_reuse);
867         }
868         g_object_unref (cm);
869 }
870
871 static void
872 accounts_dialog_button_add_clicked_cb (GtkWidget             *button,
873                                        EmpathyAccountsDialog *dialog)
874 {
875         GtkTreeView      *view;
876         GtkTreeSelection *selection;
877         GtkTreeModel     *model;
878
879         view = GTK_TREE_VIEW (dialog->treeview);
880         model = gtk_tree_view_get_model (view);
881         selection = gtk_tree_view_get_selection (view);
882         gtk_tree_selection_unselect_all (selection);
883
884         gtk_widget_set_sensitive (dialog->button_add, FALSE);
885         gtk_widget_set_sensitive (dialog->button_remove, FALSE);
886         gtk_widget_hide (dialog->vbox_details);
887         gtk_widget_hide (dialog->frame_no_protocol);
888         gtk_widget_show (dialog->frame_new_account);
889
890         /* If we have no account, no need of a back button */
891         if (gtk_tree_model_iter_n_children (model, NULL) > 0) {
892                 gtk_widget_show (dialog->button_back);
893         } else {
894                 gtk_widget_hide (dialog->button_back);
895         }
896
897         accounts_dialog_protocol_changed_cb (dialog->radiobutton_register, dialog);
898         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->radiobutton_reuse),
899                                       TRUE);
900         gtk_combo_box_set_active (GTK_COMBO_BOX (dialog->combobox_protocol), 0);
901         gtk_widget_grab_focus (dialog->combobox_protocol);
902 }
903
904 static void
905 accounts_dialog_button_help_clicked_cb (GtkWidget             *button,
906                                         EmpathyAccountsDialog *dialog)
907 {
908         empathy_url_show (button, "ghelp:empathy?empathy-create-account");
909 }
910
911 static void
912 accounts_dialog_button_remove_clicked_cb (GtkWidget            *button,
913                                           EmpathyAccountsDialog *dialog)
914 {
915         EmpathyAccount *account;
916         GtkWidget *message_dialog;
917         gint       res;
918
919         account = accounts_dialog_model_get_selected (dialog);
920
921         if (!empathy_account_is_valid (account)) {
922                 accounts_dialog_model_remove_selected (dialog);
923                 accounts_dialog_model_select_first (dialog);
924                 return;
925         }
926         message_dialog = gtk_message_dialog_new
927                 (GTK_WINDOW (dialog->window),
928                  GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
929                  GTK_MESSAGE_QUESTION,
930                  GTK_BUTTONS_NONE,
931                  _("You are about to remove your %s account!\n"
932                    "Are you sure you want to proceed?"),
933                  empathy_account_get_display_name (account));
934
935         gtk_message_dialog_format_secondary_text
936                 (GTK_MESSAGE_DIALOG (message_dialog),
937                  _("Any associated conversations and chat rooms will NOT be "
938                    "removed if you decide to proceed.\n"
939                    "\n"
940                    "Should you decide to add the account back at a later time, "
941                    "they will still be available."));
942
943         gtk_dialog_add_button (GTK_DIALOG (message_dialog),
944                                GTK_STOCK_CANCEL,
945                                GTK_RESPONSE_NO);
946         gtk_dialog_add_button (GTK_DIALOG (message_dialog),
947                                GTK_STOCK_REMOVE,
948                                GTK_RESPONSE_YES);
949
950         gtk_widget_show (message_dialog);
951         res = gtk_dialog_run (GTK_DIALOG (message_dialog));
952
953         if (res == GTK_RESPONSE_YES) {
954                 empathy_account_manager_remove (dialog->account_manager, account);
955                 accounts_dialog_model_select_first (dialog);
956         }
957         gtk_widget_destroy (message_dialog);
958 }
959
960 static void
961 accounts_dialog_button_import_clicked_cb (GtkWidget             *button,
962                                           EmpathyAccountsDialog *dialog)
963 {
964         empathy_import_dialog_show (GTK_WINDOW (dialog->window), TRUE);
965 }
966
967 static void
968 accounts_dialog_response_cb (GtkWidget            *widget,
969                              gint                  response,
970                              EmpathyAccountsDialog *dialog)
971 {
972         if (response == GTK_RESPONSE_CLOSE) {
973                 gtk_widget_destroy (widget);
974         }
975 }
976
977 static void
978 accounts_dialog_destroy_cb (GtkWidget            *widget,
979                             EmpathyAccountsDialog *dialog)
980 {
981         GList *accounts, *l;
982
983         /* Disconnect signals */
984         g_signal_handlers_disconnect_by_func (dialog->account_manager,
985                                               accounts_dialog_account_added_cb,
986                                               dialog);
987         g_signal_handlers_disconnect_by_func (dialog->account_manager,
988                                               accounts_dialog_account_removed_cb,
989                                               dialog);
990         g_signal_handlers_disconnect_by_func (dialog->account_manager,
991                                               accounts_dialog_account_enabled_cb,
992                                               dialog);
993         g_signal_handlers_disconnect_by_func (dialog->account_manager,
994                                               accounts_dialog_account_disabled_cb,
995                                               dialog);
996         g_signal_handlers_disconnect_by_func (dialog->account_manager,
997                                               accounts_dialog_account_changed_cb,
998                                               dialog);
999         g_signal_handlers_disconnect_by_func (dialog->account_manager,
1000                                               accounts_dialog_connection_changed_cb,
1001                                               dialog);
1002
1003         /* Delete incomplete accounts */
1004         accounts = empathy_account_manager_dup_accounts (dialog->account_manager);
1005         for (l = accounts; l; l = l->next) {
1006                 EmpathyAccount *account;
1007
1008                 account = l->data;
1009                 if (!empathy_account_is_valid (account)) {
1010                         /* FIXME: Warn the user the account is not complete
1011                          *        and is going to be removed. */
1012                         empathy_account_manager_remove (dialog->account_manager, account);
1013                 }
1014
1015                 g_object_unref (account);
1016         }
1017         g_list_free (accounts);
1018
1019         if (dialog->connecting_id) {
1020                 g_source_remove (dialog->connecting_id);
1021         }
1022
1023         g_object_unref (dialog->account_manager);
1024         g_object_unref (dialog->mc);
1025
1026         g_free (dialog);
1027 }
1028
1029 GtkWidget *
1030 empathy_accounts_dialog_show (GtkWindow *parent,
1031                               EmpathyAccount *selected_account)
1032 {
1033         static EmpathyAccountsDialog *dialog = NULL;
1034         GtkBuilder                   *gui;
1035         gchar                        *filename;
1036         GList                        *accounts, *l;
1037         gboolean                      import_asked;
1038
1039         if (dialog) {
1040                 gtk_window_present (GTK_WINDOW (dialog->window));
1041                 return dialog->window;
1042         }
1043
1044         dialog = g_new0 (EmpathyAccountsDialog, 1);
1045
1046         filename = empathy_file_lookup ("empathy-accounts-dialog.ui",
1047                                         "src");
1048         gui = empathy_builder_get_file (filename,
1049                                        "accounts_dialog", &dialog->window,
1050                                        "vbox_details", &dialog->vbox_details,
1051                                        "frame_no_protocol", &dialog->frame_no_protocol,
1052                                        "alignment_settings", &dialog->alignment_settings,
1053                                        "treeview", &dialog->treeview,
1054                                        "frame_new_account", &dialog->frame_new_account,
1055                                        "hbox_type", &dialog->hbox_type,
1056                                        "button_create", &dialog->button_create,
1057                                        "button_back", &dialog->button_back,
1058                                        "radiobutton_reuse", &dialog->radiobutton_reuse,
1059                                        "radiobutton_register", &dialog->radiobutton_register,
1060                                        "image_type", &dialog->image_type,
1061                                        "label_name", &dialog->label_name,
1062                                        "button_add", &dialog->button_add,
1063                                        "button_remove", &dialog->button_remove,
1064                                        "button_import", &dialog->button_import,
1065                                        NULL);
1066         g_free (filename);
1067
1068         empathy_builder_connect (gui, dialog,
1069                               "accounts_dialog", "destroy", accounts_dialog_destroy_cb,
1070                               "accounts_dialog", "response", accounts_dialog_response_cb,
1071                               "button_create", "clicked", accounts_dialog_button_create_clicked_cb,
1072                               "button_back", "clicked", accounts_dialog_button_back_clicked_cb,
1073                               "button_add", "clicked", accounts_dialog_button_add_clicked_cb,
1074                               "button_remove", "clicked", accounts_dialog_button_remove_clicked_cb,
1075                               "button_import", "clicked", accounts_dialog_button_import_clicked_cb,
1076                               "button_help", "clicked", accounts_dialog_button_help_clicked_cb,
1077                               NULL);
1078
1079         g_object_add_weak_pointer (G_OBJECT (dialog->window), (gpointer) &dialog);
1080
1081         g_object_unref (gui);
1082
1083         /* Create protocol chooser */
1084         dialog->combobox_protocol = empathy_protocol_chooser_new ();
1085         gtk_box_pack_end (GTK_BOX (dialog->hbox_type),
1086                           dialog->combobox_protocol,
1087                           TRUE, TRUE, 0);
1088         gtk_widget_show (dialog->combobox_protocol);
1089         g_signal_connect (dialog->combobox_protocol, "changed",
1090                           G_CALLBACK (accounts_dialog_protocol_changed_cb),
1091                           dialog);
1092
1093         /* Set up signalling */
1094         dialog->account_manager = empathy_account_manager_dup_singleton ();
1095         dialog->mc = empathy_mission_control_dup_singleton ();
1096
1097         g_signal_connect (dialog->account_manager, "account-created",
1098                           G_CALLBACK (accounts_dialog_account_added_cb),
1099                           dialog);
1100         g_signal_connect (dialog->account_manager, "account-deleted",
1101                           G_CALLBACK (accounts_dialog_account_removed_cb),
1102                           dialog);
1103         g_signal_connect (dialog->account_manager, "account-enabled",
1104                           G_CALLBACK (accounts_dialog_account_enabled_cb),
1105                           dialog);
1106         g_signal_connect (dialog->account_manager, "account-disabled",
1107                           G_CALLBACK (accounts_dialog_account_disabled_cb),
1108                           dialog);
1109         g_signal_connect (dialog->account_manager, "account-changed",
1110                           G_CALLBACK (accounts_dialog_account_changed_cb),
1111                           dialog);
1112         g_signal_connect (dialog->account_manager, "account-connection-changed",
1113                           G_CALLBACK (accounts_dialog_connection_changed_cb),
1114                           dialog);
1115
1116         accounts_dialog_model_setup (dialog);
1117
1118         /* Add existing accounts */
1119         accounts = empathy_account_manager_dup_accounts (dialog->account_manager);
1120         for (l = accounts; l; l = l->next) {
1121                 accounts_dialog_add_or_update_account (dialog, l->data);
1122                 g_object_unref (l->data);
1123         }
1124         g_list_free (accounts);
1125
1126         if (selected_account) {
1127                 accounts_dialog_model_set_selected (dialog, selected_account);
1128         } else {
1129                 accounts_dialog_model_select_first (dialog);
1130         }
1131
1132         if (parent) {
1133                 gtk_window_set_transient_for (GTK_WINDOW (dialog->window),
1134                                               GTK_WINDOW (parent));
1135         }
1136
1137         gtk_widget_show (dialog->window);
1138
1139         empathy_conf_get_bool (empathy_conf_get (),
1140                                EMPATHY_PREFS_IMPORT_ASKED, &import_asked);
1141
1142
1143         if (empathy_import_dialog_accounts_to_import ()) {
1144
1145                 if (!import_asked) {
1146                         empathy_conf_set_bool (empathy_conf_get (),
1147                                                EMPATHY_PREFS_IMPORT_ASKED, TRUE);
1148                         empathy_import_dialog_show (GTK_WINDOW (dialog->window),
1149                                                     FALSE);
1150                 }
1151         } else {
1152                 gtk_widget_set_sensitive (dialog->button_import, FALSE);
1153         }
1154
1155         return dialog->window;
1156 }
1157