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