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