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