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