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