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