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