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