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