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