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