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