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