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