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