]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-accounts-dialog.c
Make "Add account" button insensitive when we are creating one. Fixes bug
[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 <libtelepathy/tp-constants.h>
40
41 #include <libempathy/empathy-debug.h>
42 #include <libempathy/empathy-utils.h>
43 #include <libempathy-gtk/empathy-ui-utils.h>
44
45 #include "empathy-accounts-dialog.h"
46 #include "empathy-profile-chooser.h"
47 #include "empathy-account-widget-generic.h"
48 #include "empathy-account-widget-jabber.h"
49 #include "empathy-account-widget-msn.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         GtkWidget        *button_connect;
71
72         GtkWidget        *frame_new_account;
73         GtkWidget        *combobox_profile;
74         GtkWidget        *hbox_type;
75         GtkWidget        *button_create;
76         GtkWidget        *button_back;
77
78         GtkWidget        *image_type;
79         GtkWidget        *label_name;
80         GtkWidget        *label_type;
81         GtkWidget        *settings_widget;
82
83         gboolean          connecting_show;
84         guint             connecting_id;
85         gboolean          account_changed;
86
87         MissionControl   *mc;
88         McAccountMonitor *monitor;
89 } EmpathyAccountsDialog;
90
91 enum {
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                                                              TelepathyConnectionStatus        status,
130                                                              McPresence                       presence,
131                                                              TelepathyConnectionStatusReason  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_connect_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_treeview_row_activated_cb (GtkTreeView                     *tree_view,
148                                                              GtkTreePath                     *path,
149                                                              GtkTreeViewColumn               *column,
150                                                              EmpathyAccountsDialog            *dialog);
151 static void       accounts_dialog_response_cb               (GtkWidget                       *widget,
152                                                              gint                             response,
153                                                              EmpathyAccountsDialog            *dialog);
154 static void       accounts_dialog_destroy_cb                (GtkWidget                       *widget,
155                                                              EmpathyAccountsDialog            *dialog);
156
157 static void
158 accounts_dialog_setup (EmpathyAccountsDialog *dialog)
159 {
160         GtkTreeView  *view;
161         GtkListStore *store;
162         GtkTreeIter   iter;
163         GList        *accounts, *l;
164
165         view = GTK_TREE_VIEW (dialog->treeview);
166         store = GTK_LIST_STORE (gtk_tree_view_get_model (view));
167
168         accounts = mc_accounts_list ();
169
170         for (l = accounts; l; l = l->next) {
171                 McAccount                 *account;
172                 const gchar               *name;
173                 TelepathyConnectionStatus  status;
174
175                 account = l->data;
176
177                 name = mc_account_get_display_name (account);
178                 if (!name) {
179                         continue;
180                 }
181
182                 status = mission_control_get_connection_status (dialog->mc, account, NULL);
183
184                 gtk_list_store_insert_with_values (store, &iter,
185                                                    -1,
186                                                    COL_NAME, name,
187                                                    COL_STATUS, status,
188                                                    COL_ACCOUNT_POINTER, account,
189                                                    -1);
190
191                 accounts_dialog_status_changed_cb (dialog->mc,
192                                                    status,
193                                                    MC_PRESENCE_UNSET,
194                                                    TP_CONN_STATUS_REASON_NONE_SPECIFIED,
195                                                    mc_account_get_unique_name (account),
196                                                    dialog);
197
198                 g_object_unref (account);
199         }
200
201         g_list_free (accounts);
202 }
203
204 static void
205 accounts_dialog_update_connect_button (EmpathyAccountsDialog *dialog)
206 {
207         McAccount   *account;
208         GtkWidget   *image;
209         const gchar *stock_id;
210         const gchar *label;
211
212         account = accounts_dialog_model_get_selected (dialog);
213         
214         gtk_widget_set_sensitive (dialog->button_connect, account != NULL);
215
216         if (account && mc_account_is_enabled (account)) {
217                 label = _("Disable");
218                 stock_id = GTK_STOCK_DISCONNECT;
219         } else {
220                 label = _("Enable");
221                 stock_id = GTK_STOCK_CONNECT;
222         }
223
224         image = gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON);
225
226         gtk_button_set_label (GTK_BUTTON (dialog->button_connect), label);
227         gtk_button_set_image (GTK_BUTTON (dialog->button_connect), image);
228 }
229
230 static void
231 accounts_dialog_update_account (EmpathyAccountsDialog *dialog,
232                                 McAccount            *account)
233 {
234         if (dialog->settings_widget) {
235                 gtk_widget_destroy (dialog->settings_widget);
236                 dialog->settings_widget = NULL;
237         }
238
239         if (!account) {
240                 GtkTreeView  *view;
241                 GtkTreeModel *model;
242
243                 gtk_widget_show (dialog->frame_no_account);
244                 gtk_widget_hide (dialog->vbox_details);
245
246                 gtk_widget_set_sensitive (dialog->button_connect, FALSE);
247                 gtk_widget_set_sensitive (dialog->button_remove, FALSE);
248
249                 view = GTK_TREE_VIEW (dialog->treeview);
250                 model = gtk_tree_view_get_model (view);
251
252                 if (gtk_tree_model_iter_n_children (model, NULL) > 0) {
253                         gtk_label_set_markup (GTK_LABEL (dialog->label_no_account),
254                                               _("<b>No Account Selected</b>"));
255                         gtk_label_set_markup (GTK_LABEL (dialog->label_no_account_blurb),
256                                               _("To add a new account, you can click on the "
257                                                 "'Add' button and a new entry will be created "
258                                                 "for you to start configuring.\n"
259                                                 "\n"
260                                                 "If you do not want to add an account, simply "
261                                                 "click on the account you want to configure in "
262                                                 "the list on the left."));
263                 } else {
264                         gtk_label_set_markup (GTK_LABEL (dialog->label_no_account),
265                                               _("<b>No Accounts Configured</b>"));
266                         gtk_label_set_markup (GTK_LABEL (dialog->label_no_account_blurb),
267                                               _("To add a new account, you can click on the "
268                                                 "'Add' button and a new entry will be created "
269                                                 "for you to start configuring."));
270                 }
271         } else {
272                 McProfile *profile;
273                 const gchar *config_ui;
274
275                 gtk_widget_hide (dialog->frame_no_account);
276                 gtk_widget_show (dialog->vbox_details);
277
278                 profile = mc_account_get_profile (account);
279                 config_ui = mc_profile_get_configuration_ui (profile);
280                 g_object_unref (profile);
281
282                 if (!empathy_strdiff (config_ui, "jabber")) {
283                         dialog->settings_widget = 
284                                 empathy_account_widget_jabber_new (account);
285                 } 
286                 else if (!empathy_strdiff (config_ui, "msn")) {
287                         dialog ->settings_widget =
288                                 empathy_account_widget_msn_new (account);
289                 }
290                 else {
291                         dialog->settings_widget = 
292                                 empathy_account_widget_generic_new (account);
293                 }
294                 
295                 gtk_widget_grab_focus (dialog->settings_widget);
296         }
297
298         if (dialog->settings_widget) {
299                 gtk_container_add (GTK_CONTAINER (dialog->alignment_settings),
300                                    dialog->settings_widget);
301         }
302
303         if (account) {
304                 McProfile *profile;
305                 gchar     *text;
306
307                 profile = mc_account_get_profile (account);
308                 gtk_image_set_from_icon_name (GTK_IMAGE (dialog->image_type),
309                                               mc_profile_get_icon_name (profile),
310                                               GTK_ICON_SIZE_DIALOG);
311                 /* FIXME: Uncomment once we depend on GTK+ 2.12
312                 gtk_widget_set_tooltip_text (dialog->image_type,
313                                              mc_profile_get_display_name (profile));
314                 */
315
316                 text = g_strdup_printf ("<big><b>%s</b></big>", mc_account_get_display_name (account));
317                 gtk_label_set_markup (GTK_LABEL (dialog->label_name), text);
318                 g_free (text);
319         }
320 }
321
322 static void
323 accounts_dialog_model_setup (EmpathyAccountsDialog *dialog)
324 {
325         GtkListStore     *store;
326         GtkTreeSelection *selection;
327
328         store = gtk_list_store_new (COL_COUNT,
329                                     G_TYPE_STRING,     /* name */
330                                     G_TYPE_UINT,       /* status */
331                                     MC_TYPE_ACCOUNT);  /* account */
332
333         gtk_tree_view_set_model (GTK_TREE_VIEW (dialog->treeview),
334                                  GTK_TREE_MODEL (store));
335
336         selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (dialog->treeview));
337         gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
338
339         g_signal_connect (selection, "changed",
340                           G_CALLBACK (accounts_dialog_model_selection_changed),
341                           dialog);
342
343         gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
344                                               COL_NAME, GTK_SORT_ASCENDING);
345
346         accounts_dialog_model_add_columns (dialog);
347
348         g_object_unref (store);
349 }
350
351 static void
352 accounts_dialog_name_edited_cb (GtkCellRendererText   *renderer,
353                                 gchar                 *path,
354                                 gchar                 *new_text,
355                                 EmpathyAccountsDialog *dialog)
356 {
357         McAccount    *account;
358         GtkTreeModel *model;
359         GtkTreePath  *treepath;
360         GtkTreeIter   iter;
361
362         model = gtk_tree_view_get_model (GTK_TREE_VIEW (dialog->treeview));
363         treepath = gtk_tree_path_new_from_string (path);
364         gtk_tree_model_get_iter (model, &iter, treepath);
365         gtk_tree_model_get (model, &iter,
366                             COL_ACCOUNT_POINTER, &account,
367                             -1);
368         gtk_list_store_set (GTK_LIST_STORE (model), &iter,
369                             COL_NAME, new_text,
370                             -1);
371         gtk_tree_path_free (treepath);
372
373         mc_account_set_display_name (account, new_text);
374         g_object_unref (account);
375 }
376
377 static void
378 accounts_dialog_model_add_columns (EmpathyAccountsDialog *dialog)
379 {
380         GtkTreeView       *view;
381         GtkTreeViewColumn *column;
382         GtkCellRenderer   *cell;
383
384         view = GTK_TREE_VIEW (dialog->treeview);
385         gtk_tree_view_set_headers_visible (view, TRUE);
386
387         /* account name/status */
388         column = gtk_tree_view_column_new ();
389         gtk_tree_view_column_set_title (column, _("Accounts"));
390
391         cell = gtk_cell_renderer_pixbuf_new ();
392         gtk_tree_view_column_pack_start (column, cell, FALSE);
393         gtk_tree_view_column_set_cell_data_func (column, cell,
394                                                  (GtkTreeCellDataFunc)
395                                                  accounts_dialog_model_pixbuf_data_func,
396                                                  dialog,
397                                                  NULL);
398
399         cell = gtk_cell_renderer_text_new ();
400         g_object_set (cell,
401                       "ellipsize", PANGO_ELLIPSIZE_END,
402                       "editable", TRUE,
403                       NULL);
404         gtk_tree_view_column_pack_start (column, cell, TRUE);
405         gtk_tree_view_column_add_attribute (column,
406                                             cell,
407                                             "text", COL_NAME);
408         g_signal_connect (cell, "edited",
409                           G_CALLBACK (accounts_dialog_name_edited_cb),
410                           dialog);
411
412         gtk_tree_view_column_set_expand (column, TRUE);
413         gtk_tree_view_append_column (view, column);
414 }
415
416 static void
417 accounts_dialog_model_select_first (EmpathyAccountsDialog *dialog)
418 {
419         GtkTreeView      *view;
420         GtkTreeModel     *model;
421         GtkTreeSelection *selection;
422         GtkTreeIter       iter;
423
424         /* select first */
425         view = GTK_TREE_VIEW (dialog->treeview);
426         model = gtk_tree_view_get_model (view);
427         
428         if (gtk_tree_model_get_iter_first (model, &iter)) {
429                 selection = gtk_tree_view_get_selection (view);
430                 gtk_tree_selection_select_iter (selection, &iter);
431         } else {
432                 accounts_dialog_update_account (dialog, NULL);
433         }
434 }
435
436 static void
437 accounts_dialog_model_pixbuf_data_func (GtkTreeViewColumn    *tree_column,
438                                         GtkCellRenderer      *cell,
439                                         GtkTreeModel         *model,
440                                         GtkTreeIter          *iter,
441                                         EmpathyAccountsDialog *dialog)
442 {
443         McAccount                 *account;
444         const gchar               *icon_name;
445         GdkPixbuf                 *pixbuf;
446         TelepathyConnectionStatus  status;
447
448         gtk_tree_model_get (model, iter,
449                             COL_STATUS, &status,
450                             COL_ACCOUNT_POINTER, &account,
451                             -1);
452
453         icon_name = empathy_icon_name_from_account (account);
454         pixbuf = empathy_pixbuf_from_icon_name (icon_name, GTK_ICON_SIZE_BUTTON);
455
456         if (pixbuf) {
457                 if (status == TP_CONN_STATUS_DISCONNECTED ||
458                     (status == TP_CONN_STATUS_CONNECTING && 
459                      !dialog->connecting_show)) {
460                         GdkPixbuf *modded_pixbuf;
461
462                         modded_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
463                                                         TRUE,
464                                                         8,
465                                                         gdk_pixbuf_get_width (pixbuf),
466                                                         gdk_pixbuf_get_height (pixbuf));
467
468                         gdk_pixbuf_saturate_and_pixelate (pixbuf,
469                                                           modded_pixbuf,
470                                                           1.0,
471                                                           TRUE);
472                         g_object_unref (pixbuf);
473                         pixbuf = modded_pixbuf;
474                 }
475         }
476
477         g_object_set (cell,
478                       "visible", TRUE,
479                       "pixbuf", pixbuf,
480                       NULL);
481
482         g_object_unref (account);
483         if (pixbuf) {
484                 g_object_unref (pixbuf);
485         }
486 }
487
488 static McAccount *
489 accounts_dialog_model_get_selected (EmpathyAccountsDialog *dialog)
490 {
491         GtkTreeView      *view;
492         GtkTreeModel     *model;
493         GtkTreeSelection *selection;
494         GtkTreeIter       iter;
495         McAccount        *account;
496
497         view = GTK_TREE_VIEW (dialog->treeview);
498         selection = gtk_tree_view_get_selection (view);
499
500         if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
501                 return NULL;
502         }
503
504         gtk_tree_model_get (model, &iter, COL_ACCOUNT_POINTER, &account, -1);
505
506         return account;
507 }
508
509 static void
510 accounts_dialog_model_set_selected (EmpathyAccountsDialog *dialog,
511                                     McAccount            *account)
512 {
513         GtkTreeView      *view;
514         GtkTreeSelection *selection;
515         GtkTreeModel     *model;
516         GtkTreeIter       iter;
517         gboolean          ok;
518
519         view = GTK_TREE_VIEW (dialog->treeview);
520         model = gtk_tree_view_get_model (view);
521         selection = gtk_tree_view_get_selection (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                         gtk_tree_selection_select_iter (selection, &iter);
538                         break;
539                 }
540         }
541 }
542
543 static gboolean
544 accounts_dialog_model_remove_selected (EmpathyAccountsDialog *dialog)
545 {
546         GtkTreeView      *view;
547         GtkTreeModel     *model;
548         GtkTreeSelection *selection;
549         GtkTreeIter       iter;
550
551         view = GTK_TREE_VIEW (dialog->treeview);
552         selection = gtk_tree_view_get_selection (view);
553
554         if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
555                 return FALSE;
556         }
557
558         return gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
559 }
560
561 static void
562 accounts_dialog_model_selection_changed (GtkTreeSelection     *selection,
563                                          EmpathyAccountsDialog *dialog)
564 {
565         McAccount    *account;
566         GtkTreeModel *model;
567         GtkTreeIter   iter;
568         gboolean      is_selection;
569
570         is_selection = gtk_tree_selection_get_selected (selection, &model, &iter);
571
572         gtk_widget_set_sensitive (dialog->button_add, TRUE);
573         gtk_widget_set_sensitive (dialog->button_remove, is_selection);
574         gtk_widget_set_sensitive (dialog->button_connect, is_selection);
575
576         accounts_dialog_update_connect_button (dialog);
577
578         account = accounts_dialog_model_get_selected (dialog);
579         accounts_dialog_update_account (dialog, account);
580
581         if (account) {
582                 g_object_unref (account);
583         }
584
585         /* insure new account frame is hidden when a row is selected*/
586         gtk_widget_hide (dialog->frame_new_account);
587 }
588
589 static void
590 accounts_dialog_add_account (EmpathyAccountsDialog *dialog,
591                              McAccount            *account)
592 {
593         TelepathyConnectionStatus  status;
594         const gchar               *name;
595         GtkTreeView               *view;
596         GtkTreeModel              *model;
597         GtkListStore              *store;
598         GtkTreeIter                iter;
599         gboolean                   ok;
600
601         view = GTK_TREE_VIEW (dialog->treeview);
602         model = gtk_tree_view_get_model (view);
603         store = GTK_LIST_STORE (model);
604
605         for (ok = gtk_tree_model_get_iter_first (model, &iter);
606              ok;
607              ok = gtk_tree_model_iter_next (model, &iter)) {
608                 McAccount *this_account;
609                 gboolean   equal;
610
611                 gtk_tree_model_get (model, &iter,
612                                     COL_ACCOUNT_POINTER, &this_account,
613                                     -1);
614
615                 equal =  empathy_account_equal (this_account, account);
616                 g_object_unref (this_account);
617
618                 if (equal) {
619                         return;
620                 }
621         }
622
623         status = mission_control_get_connection_status (dialog->mc, account, NULL);
624         name = mc_account_get_display_name (account);
625
626         g_return_if_fail (name != NULL);
627
628         empathy_debug (DEBUG_DOMAIN, "Adding new account: %s", name);
629
630         gtk_list_store_insert_with_values (store, &iter,
631                                            -1,
632                                            COL_NAME, name,
633                                            COL_STATUS, status,
634                                            COL_ACCOUNT_POINTER, account,
635                                            -1);
636 }
637
638 static void
639 accounts_dialog_account_added_cb (McAccountMonitor     *monitor,
640                                   gchar                *unique_name,
641                                   EmpathyAccountsDialog *dialog)
642 {
643         McAccount *account;
644
645         account = mc_account_lookup (unique_name);
646         accounts_dialog_add_account (dialog, account);
647         g_object_unref (account);
648 }
649
650 static void
651 accounts_dialog_account_removed_cb (McAccountMonitor     *monitor,
652                                     gchar                *unique_name,
653                                     EmpathyAccountsDialog *dialog)
654 {
655         McAccount *account;
656
657         account = mc_account_lookup (unique_name);
658
659         accounts_dialog_model_set_selected (dialog, account);
660         accounts_dialog_model_remove_selected (dialog);
661
662         g_object_unref (account);
663 }
664
665 static gboolean
666 accounts_dialog_row_changed_foreach (GtkTreeModel *model,
667                                      GtkTreePath  *path,
668                                      GtkTreeIter  *iter,
669                                      gpointer      user_data)
670 {
671         gtk_tree_model_row_changed (model, path, iter);
672
673         return FALSE;
674 }
675
676 static gboolean
677 accounts_dialog_flash_connecting_cb (EmpathyAccountsDialog *dialog)
678 {
679         GtkTreeView  *view;
680         GtkTreeModel *model;
681
682         dialog->connecting_show = !dialog->connecting_show;
683
684         view = GTK_TREE_VIEW (dialog->treeview);
685         model = gtk_tree_view_get_model (view);
686
687         gtk_tree_model_foreach (model, accounts_dialog_row_changed_foreach, NULL);
688
689         return TRUE;
690 }
691
692 static void
693 accounts_dialog_status_changed_cb (MissionControl                  *mc,
694                                    TelepathyConnectionStatus        status,
695                                    McPresence                       presence,
696                                    TelepathyConnectionStatusReason  reason,
697                                    const gchar                     *unique_name,
698                                    EmpathyAccountsDialog            *dialog)
699 {
700         GtkTreeView      *view;
701         GtkTreeSelection *selection;
702         GtkTreeModel     *model;
703         GtkTreeIter       iter;
704         gboolean          ok;
705         McAccount        *account;
706         GList            *accounts, *l;
707         gboolean          found = FALSE;
708         
709         /* Update the status in the model */
710         view = GTK_TREE_VIEW (dialog->treeview);
711         selection = gtk_tree_view_get_selection (view);
712         model = gtk_tree_view_get_model (view);
713         account = mc_account_lookup (unique_name);
714
715         empathy_debug (DEBUG_DOMAIN, "Status changed for account %s: "
716                       "status=%d presence=%d",
717                       unique_name, status, presence);
718
719         for (ok = gtk_tree_model_get_iter_first (model, &iter);
720              ok;
721              ok = gtk_tree_model_iter_next (model, &iter)) {
722                 McAccount *this_account;
723                 gboolean   equal;
724
725                 gtk_tree_model_get (model, &iter,
726                                     COL_ACCOUNT_POINTER, &this_account,
727                                     -1);
728
729                 equal = empathy_account_equal (this_account, account);
730                 g_object_unref (this_account);
731
732                 if (equal) {
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                         break;
744                 }
745         }
746
747         g_object_unref (account);
748
749         /* Start to flash account if status is connecting */
750         if (status == TP_CONN_STATUS_CONNECTING) {
751                 if (!dialog->connecting_id) {
752                         dialog->connecting_id = g_timeout_add (FLASH_TIMEOUT,
753                                                                (GSourceFunc) accounts_dialog_flash_connecting_cb,
754                                                                dialog);
755                 }
756
757                 return;
758         }
759
760         /* Stop to flash if no account is connecting */
761         accounts = mc_accounts_list ();
762         for (l = accounts; l; l = l->next) {
763                 McAccount *this_account;
764
765                 this_account = l->data;
766
767                 if (mission_control_get_connection_status (mc, this_account, NULL) == TP_CONN_STATUS_CONNECTING) {
768                         found = TRUE;
769                         break;
770                 }
771
772                 g_object_unref (this_account);
773         }
774         g_list_free (accounts);
775
776         if (!found && dialog->connecting_id) {
777                 g_source_remove (dialog->connecting_id);
778                 dialog->connecting_id = 0;
779         }
780
781         gtk_widget_show (dialog->window);
782 }
783
784 static void
785 accounts_dialog_button_create_clicked_cb (GtkWidget             *button,
786                                           EmpathyAccountsDialog  *dialog)
787 {
788         McProfile   *profile;
789         McAccount   *account;
790         const gchar *str;
791
792         /* Update widgets */
793         gtk_widget_show (dialog->vbox_details);
794         gtk_widget_hide (dialog->frame_no_account);
795         gtk_widget_hide (dialog->frame_new_account);
796
797         profile = empathy_profile_chooser_get_selected (dialog->combobox_profile);
798
799         /* Create account */
800         account = mc_account_create (profile);
801
802         str = mc_account_get_unique_name (account);
803         mc_account_set_display_name (account, str);
804
805         accounts_dialog_add_account (dialog, account);
806         accounts_dialog_model_set_selected (dialog, account);
807
808         g_object_unref (account);
809         g_object_unref (profile);
810 }
811
812 static void
813 accounts_dialog_button_back_clicked_cb (GtkWidget             *button,
814                                         EmpathyAccountsDialog  *dialog)
815 {
816         McAccount *account;
817
818         gtk_widget_hide (dialog->vbox_details);
819         gtk_widget_hide (dialog->frame_no_account);
820         gtk_widget_hide (dialog->frame_new_account);
821
822         gtk_widget_set_sensitive (dialog->button_add, TRUE);
823
824         account = accounts_dialog_model_get_selected (dialog);
825         accounts_dialog_update_account (dialog, account);
826 }
827
828 static void
829 accounts_dialog_button_connect_clicked_cb (GtkWidget            *button,
830                                            EmpathyAccountsDialog *dialog)
831 {
832         McAccount *account;
833         gboolean   enable;
834
835         account = accounts_dialog_model_get_selected (dialog);
836         enable = (!mc_account_is_enabled (account));
837         mc_account_set_enabled (account, enable);
838         accounts_dialog_update_connect_button (dialog);
839
840         g_object_unref (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_treeview_row_activated_cb (GtkTreeView          *tree_view,
921                                            GtkTreePath          *path,
922                                            GtkTreeViewColumn    *column,
923                                            EmpathyAccountsDialog *dialog)
924 {
925
926         accounts_dialog_button_connect_clicked_cb (dialog->button_connect,
927                                                    dialog);
928 }
929
930 static void
931 accounts_dialog_response_cb (GtkWidget            *widget,
932                              gint                  response,
933                              EmpathyAccountsDialog *dialog)
934 {
935         gtk_widget_destroy (widget);
936 }
937
938 static void
939 accounts_dialog_destroy_cb (GtkWidget            *widget,
940                             EmpathyAccountsDialog *dialog)
941 {
942         GList *accounts, *l;
943
944         /* Disconnect signals */
945         g_signal_handlers_disconnect_by_func (dialog->monitor,
946                                               accounts_dialog_account_added_cb,
947                                               dialog);
948         g_signal_handlers_disconnect_by_func (dialog->monitor,
949                                               accounts_dialog_account_removed_cb,
950                                               dialog);
951         dbus_g_proxy_disconnect_signal (DBUS_G_PROXY (dialog->mc),
952                                         "AccountStatusChanged",
953                                         G_CALLBACK (accounts_dialog_status_changed_cb),
954                                         dialog);
955
956         /* Delete incomplete accounts */
957         accounts = mc_accounts_list ();
958         for (l = accounts; l; l = l->next) {
959                 McAccount *account;
960
961                 account = l->data;
962                 if (!mc_account_is_complete (account)) {
963                         /* FIXME: Warn the user the account is not complete
964                          *        and is going to be removed. */
965                         mc_account_delete (account);
966                 }
967
968                 g_object_unref (account);
969         }
970         g_list_free (accounts);
971
972         if (dialog->connecting_id) {
973                 g_source_remove (dialog->connecting_id);
974         }
975
976         g_object_unref (dialog->mc);
977         g_object_unref (dialog->monitor);
978         
979         g_free (dialog);
980 }
981
982 GtkWidget *
983 empathy_accounts_dialog_show (GtkWindow *parent)
984 {
985         static EmpathyAccountsDialog *dialog = NULL;
986         GladeXML                    *glade;
987         GtkWidget                   *bbox;
988         GtkWidget                   *button_close;
989
990         if (dialog) {
991                 gtk_window_present (GTK_WINDOW (dialog->window));
992                 return dialog->window;
993         }
994
995         dialog = g_new0 (EmpathyAccountsDialog, 1);
996
997         glade = empathy_glade_get_file ("empathy-accounts-dialog.glade",
998                                        "accounts_dialog",
999                                        NULL,
1000                                        "accounts_dialog", &dialog->window,
1001                                        "vbox_details", &dialog->vbox_details,
1002                                        "frame_no_account", &dialog->frame_no_account,
1003                                        "label_no_account", &dialog->label_no_account,
1004                                        "label_no_account_blurb", &dialog->label_no_account_blurb,
1005                                        "alignment_settings", &dialog->alignment_settings,
1006                                        "dialog-action_area", &bbox,
1007                                        "treeview", &dialog->treeview,
1008                                        "frame_new_account", &dialog->frame_new_account,
1009                                        "hbox_type", &dialog->hbox_type,
1010                                        "button_create", &dialog->button_create,
1011                                        "button_back", &dialog->button_back,
1012                                        "image_type", &dialog->image_type,
1013                                        "label_name", &dialog->label_name,
1014                                        "button_add", &dialog->button_add,
1015                                        "button_remove", &dialog->button_remove,
1016                                        "button_connect", &dialog->button_connect,
1017                                        "button_close", &button_close,
1018                                        NULL);
1019
1020         empathy_glade_connect (glade,
1021                               dialog,
1022                               "accounts_dialog", "destroy", accounts_dialog_destroy_cb,
1023                               "accounts_dialog", "response", accounts_dialog_response_cb,
1024                               "button_create", "clicked", accounts_dialog_button_create_clicked_cb,
1025                               "button_back", "clicked", accounts_dialog_button_back_clicked_cb,
1026                               "treeview", "row-activated", accounts_dialog_treeview_row_activated_cb,
1027                               "button_connect", "clicked", accounts_dialog_button_connect_clicked_cb,
1028                               "button_add", "clicked", accounts_dialog_button_add_clicked_cb,
1029                               "button_remove", "clicked", accounts_dialog_button_remove_clicked_cb,
1030                               NULL);
1031
1032         g_object_add_weak_pointer (G_OBJECT (dialog->window), (gpointer) &dialog);
1033
1034         g_object_unref (glade);
1035
1036         /* Create profile chooser */
1037         dialog->combobox_profile = empathy_profile_chooser_new ();
1038         gtk_box_pack_end (GTK_BOX (dialog->hbox_type),
1039                           dialog->combobox_profile,
1040                           TRUE, TRUE, 0);
1041         gtk_widget_show (dialog->combobox_profile);
1042
1043         /* Set up signalling */
1044         dialog->mc = empathy_mission_control_new ();
1045         dialog->monitor = mc_account_monitor_new ();
1046
1047         /* FIXME: connect account-enabled/disabled too */
1048         g_signal_connect (dialog->monitor, "account-created",
1049                           G_CALLBACK (accounts_dialog_account_added_cb),
1050                           dialog);
1051         g_signal_connect (dialog->monitor, "account-deleted",
1052                           G_CALLBACK (accounts_dialog_account_removed_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