6f61dfb36191d216b2a7836d6ad760197ac80b11
[empathy.git] / libempathy-gtk / gossip-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-session.h>
42 #include <libempathy/gossip-debug.h>
43 #include <libempathy/gossip-paths.h>
44 #include <libempathy/gossip-utils.h>
45 #include <libempathy-gtk/gossip-ui-utils.h>
46
47 #include "gossip-accounts-dialog.h"
48 #include "gossip-profile-chooser.h"
49 #include "gossip-account-widget-generic.h"
50 #include "gossip-account-widget-jabber.h"
51
52 #define DEBUG_DOMAIN "AccountDialog"
53
54 /* Flashing delay for icons (milliseconds). */
55 #define FLASH_TIMEOUT 500
56
57 typedef struct {
58         GtkWidget *window;
59
60         GtkWidget *alignment_settings;
61
62         GtkWidget *vbox_details;
63         GtkWidget *frame_no_account;
64         GtkWidget *label_no_account;
65         GtkWidget *label_no_account_blurb;
66
67         GtkWidget *treeview;
68
69         GtkWidget *button_remove;
70         GtkWidget *button_connect;
71
72         GtkWidget *frame_new_account;
73         GtkWidget *combobox_profile;
74         GtkWidget *entry_name;
75         GtkWidget *table_new_account;
76         GtkWidget *button_create;
77         GtkWidget *button_cancel;
78
79         GtkWidget *image_type;
80         GtkWidget *label_name;
81         GtkWidget *label_type;
82         GtkWidget *settings_widget;
83
84         gboolean   connecting_show;
85         guint      connecting_id;
86         gboolean   account_changed;
87 } GossipAccountsDialog;
88
89 enum {
90         COL_NAME,
91         COL_STATUS,
92         COL_ACCOUNT_POINTER,
93         COL_COUNT
94 };
95
96 static void       accounts_dialog_setup                     (GossipAccountsDialog            *dialog);
97 static void       accounts_dialog_update_account            (GossipAccountsDialog            *dialog,
98                                                              McAccount                       *account);
99 static void       accounts_dialog_model_setup               (GossipAccountsDialog            *dialog);
100 static void       accounts_dialog_model_add_columns         (GossipAccountsDialog            *dialog);
101 static void       accounts_dialog_model_select_first        (GossipAccountsDialog            *dialog);
102 static void       accounts_dialog_model_pixbuf_data_func    (GtkTreeViewColumn               *tree_column,
103                                                              GtkCellRenderer                 *cell,
104                                                              GtkTreeModel                    *model,
105                                                              GtkTreeIter                     *iter,
106                                                              GossipAccountsDialog            *dialog);
107 static McAccount *accounts_dialog_model_get_selected        (GossipAccountsDialog            *dialog);
108 static void       accounts_dialog_model_set_selected        (GossipAccountsDialog            *dialog,
109                                                              McAccount                       *account);
110 static gboolean   accounts_dialog_model_remove_selected     (GossipAccountsDialog            *dialog);
111 static void       accounts_dialog_model_selection_changed   (GtkTreeSelection                *selection,
112                                                              GossipAccountsDialog            *dialog);
113 static void       accounts_dialog_add_account               (GossipAccountsDialog            *dialog,
114                                                              McAccount                       *account);
115 static void       accounts_dialog_account_added_cb          (McAccountMonitor                *monitor,
116                                                              gchar                           *unique_name,
117                                                              GossipAccountsDialog            *dialog);
118 static void       accounts_dialog_account_removed_cb        (McAccountMonitor                *monitor,
119                                                              gchar                           *unique_name,
120                                                              GossipAccountsDialog            *dialog);
121 static gboolean   accounts_dialog_row_changed_foreach       (GtkTreeModel                    *model,
122                                                              GtkTreePath                     *path,
123                                                              GtkTreeIter                     *iter,
124                                                              gpointer                         user_data);
125 static gboolean   accounts_dialog_flash_connecting_cb       (GossipAccountsDialog            *dialog);
126 static void       accounts_dialog_status_changed_cb         (MissionControl                  *mc,
127                                                              TelepathyConnectionStatus        status,
128                                                              McPresence                       presence,
129                                                              TelepathyConnectionStatusReason  reason,
130                                                              const gchar                     *unique_name,
131                                                              GossipAccountsDialog            *dialog);
132 static void       accounts_dialog_entry_name_changed_cb     (GtkWidget                       *widget,
133                                                              GossipAccountsDialog            *dialog);
134 static void       accounts_dialog_button_create_clicked_cb  (GtkWidget                       *button,
135                                                              GossipAccountsDialog            *dialog);
136 static void       accounts_dialog_button_cancel_clicked_cb  (GtkWidget                       *button,
137                                                              GossipAccountsDialog            *dialog);
138 static void       accounts_dialog_button_connect_clicked_cb (GtkWidget                       *button,
139                                                              GossipAccountsDialog            *dialog);
140 static void       accounts_dialog_button_add_clicked_cb     (GtkWidget                       *button,
141                                                              GossipAccountsDialog            *dialog);
142 static void       accounts_dialog_remove_response_cb        (GtkWidget                       *dialog,
143                                                              gint                             response,
144                                                              McAccount                       *account);
145 static void       accounts_dialog_button_remove_clicked_cb  (GtkWidget                       *button,
146                                                              GossipAccountsDialog            *dialog);
147 static void       accounts_dialog_treeview_row_activated_cb (GtkTreeView                     *tree_view,
148                                                              GtkTreePath                     *path,
149                                                              GtkTreeViewColumn               *column,
150                                                              GossipAccountsDialog            *dialog);
151 static void       accounts_dialog_response_cb               (GtkWidget                       *widget,
152                                                              gint                             response,
153                                                              GossipAccountsDialog            *dialog);
154 static void       accounts_dialog_destroy_cb                (GtkWidget                       *widget,
155                                                              GossipAccountsDialog            *dialog);
156
157 static void
158 accounts_dialog_setup (GossipAccountsDialog *dialog)
159 {
160         GtkTreeView      *view;
161         GtkListStore     *store;
162         GtkTreeSelection *selection;
163         GtkTreeIter       iter;
164         GList            *accounts, *l;
165         MissionControl   *mc;
166
167         view = GTK_TREE_VIEW (dialog->treeview);
168         store = GTK_LIST_STORE (gtk_tree_view_get_model (view));
169         selection = gtk_tree_view_get_selection (view);
170
171         mc = empathy_session_get_mission_control ();
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 (mc, account, NULL);
187
188                 gtk_list_store_append (store, &iter);
189                 gtk_list_store_set (store, &iter,
190                                     COL_NAME, name,
191                                     COL_STATUS, status,
192                                     COL_ACCOUNT_POINTER, account,
193                                     -1);
194
195                 accounts_dialog_status_changed_cb (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 (GossipAccountsDialog *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 (GossipAccountsDialog *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                                 gossip_account_widget_jabber_new (account);
291                 } else {
292                         dialog->settings_widget = 
293                                 gossip_account_widget_generic_new (account,
294                                                                    dialog->label_name);
295                 }
296                 
297                 gtk_widget_grab_focus (dialog->settings_widget);
298         }
299
300         if (dialog->settings_widget) {
301                 gtk_container_add (GTK_CONTAINER (dialog->alignment_settings),
302                                    dialog->settings_widget);
303         }
304
305         if (account) {
306                 McProfile *profile;
307                 GdkPixbuf *pixbuf;
308
309                 pixbuf = gossip_pixbuf_from_account (account, GTK_ICON_SIZE_DIALOG);
310                 gtk_image_set_from_pixbuf (GTK_IMAGE (dialog->image_type), pixbuf);
311                 if (pixbuf) {
312                         g_object_unref (pixbuf);
313                 }
314
315                 profile = mc_account_get_profile (account);
316
317                 gtk_label_set_text (GTK_LABEL (dialog->label_type),
318                                     mc_profile_get_display_name (profile));
319                 gtk_label_set_text (GTK_LABEL (dialog->label_name), 
320                                     mc_account_get_display_name (account));
321         }
322 }
323
324 static void
325 accounts_dialog_model_setup (GossipAccountsDialog *dialog)
326 {
327         GtkListStore     *store;
328         GtkTreeSelection *selection;
329
330         store = gtk_list_store_new (COL_COUNT,
331                                     G_TYPE_STRING,     /* name */
332                                     G_TYPE_UINT,       /* status */
333                                     MC_TYPE_ACCOUNT);  /* account */
334
335         gtk_tree_view_set_model (GTK_TREE_VIEW (dialog->treeview),
336                                  GTK_TREE_MODEL (store));
337
338         selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (dialog->treeview));
339         gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
340
341         g_signal_connect (selection, "changed",
342                           G_CALLBACK (accounts_dialog_model_selection_changed),
343                           dialog);
344
345         gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
346                                               COL_NAME, GTK_SORT_ASCENDING);
347
348         accounts_dialog_model_add_columns (dialog);
349
350         g_object_unref (store);
351 }
352
353 static void
354 accounts_dialog_model_add_columns (GossipAccountsDialog *dialog)
355 {
356         GtkTreeView       *view;
357         GtkTreeViewColumn *column;
358         GtkCellRenderer   *cell;
359
360         view = GTK_TREE_VIEW (dialog->treeview);
361         gtk_tree_view_set_headers_visible (view, TRUE);
362
363         /* account name/status */
364         column = gtk_tree_view_column_new ();
365         gtk_tree_view_column_set_title (column, _("Accounts"));
366
367         cell = gtk_cell_renderer_pixbuf_new ();
368         gtk_tree_view_column_pack_start (column, cell, FALSE);
369         gtk_tree_view_column_set_cell_data_func (column, cell,
370                                                  (GtkTreeCellDataFunc)
371                                                  accounts_dialog_model_pixbuf_data_func,
372                                                  dialog,
373                                                  NULL);
374
375         cell = gtk_cell_renderer_text_new ();
376         g_object_set (cell, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
377         gtk_tree_view_column_pack_start (column, cell, TRUE);
378         gtk_tree_view_column_add_attribute (column,
379                                             cell,
380                                             "text", COL_NAME);
381
382         gtk_tree_view_column_set_expand (column, TRUE);
383         gtk_tree_view_append_column (view, column);
384 }
385
386 static void
387 accounts_dialog_model_select_first (GossipAccountsDialog *dialog)
388 {
389         GtkTreeView      *view;
390         GtkTreeModel     *model;
391         GtkTreeSelection *selection;
392         GtkTreeIter       iter;
393
394         /* select first */
395         view = GTK_TREE_VIEW (dialog->treeview);
396         model = gtk_tree_view_get_model (view);
397         
398         if (gtk_tree_model_get_iter_first (model, &iter)) {
399                 selection = gtk_tree_view_get_selection (view);
400                 gtk_tree_selection_select_iter (selection, &iter);
401         } else {
402                 accounts_dialog_update_account (dialog, NULL);
403         }
404 }
405
406 static void
407 accounts_dialog_model_pixbuf_data_func (GtkTreeViewColumn    *tree_column,
408                                         GtkCellRenderer      *cell,
409                                         GtkTreeModel         *model,
410                                         GtkTreeIter          *iter,
411                                         GossipAccountsDialog *dialog)
412 {
413         McAccount                 *account;
414         GdkPixbuf                 *pixbuf;
415         TelepathyConnectionStatus  status;
416
417         gtk_tree_model_get (model, iter,
418                             COL_STATUS, &status,
419                             COL_ACCOUNT_POINTER, &account,
420                             -1);
421
422         pixbuf = gossip_pixbuf_from_account (account, GTK_ICON_SIZE_BUTTON);
423
424         if (pixbuf) {
425                 if (status == TP_CONN_STATUS_DISCONNECTED ||
426                     (status == TP_CONN_STATUS_CONNECTING && 
427                      !dialog->connecting_show)) {
428                         GdkPixbuf *modded_pixbuf;
429
430                         modded_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
431                                                         TRUE,
432                                                         8,
433                                                         gdk_pixbuf_get_width (pixbuf),
434                                                         gdk_pixbuf_get_height (pixbuf));
435
436                         gdk_pixbuf_saturate_and_pixelate (pixbuf,
437                                                           modded_pixbuf,
438                                                           1.0,
439                                                           TRUE);
440                         g_object_unref (pixbuf);
441                         pixbuf = modded_pixbuf;
442                 }
443         }
444
445         g_object_set (cell,
446                       "visible", TRUE,
447                       "pixbuf", pixbuf,
448                       NULL);
449
450         g_object_unref (account);
451         if (pixbuf) {
452                 g_object_unref (pixbuf);
453         }
454 }
455
456 static McAccount *
457 accounts_dialog_model_get_selected (GossipAccountsDialog *dialog)
458 {
459         GtkTreeView      *view;
460         GtkTreeModel     *model;
461         GtkTreeSelection *selection;
462         GtkTreeIter       iter;
463         McAccount        *account;
464
465         view = GTK_TREE_VIEW (dialog->treeview);
466         selection = gtk_tree_view_get_selection (view);
467
468         if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
469                 return NULL;
470         }
471
472         gtk_tree_model_get (model, &iter, COL_ACCOUNT_POINTER, &account, -1);
473
474         return account;
475 }
476
477 static void
478 accounts_dialog_model_set_selected (GossipAccountsDialog *dialog,
479                                     McAccount            *account)
480 {
481         GtkTreeView      *view;
482         GtkTreeSelection *selection;
483         GtkTreeModel     *model;
484         GtkTreeIter       iter;
485         gboolean          ok;
486
487         view = GTK_TREE_VIEW (dialog->treeview);
488         model = gtk_tree_view_get_model (view);
489         selection = gtk_tree_view_get_selection (view);
490
491         for (ok = gtk_tree_model_get_iter_first (model, &iter);
492              ok;
493              ok = gtk_tree_model_iter_next (model, &iter)) {
494                 McAccount *this_account;
495                 gboolean   equal;
496
497                 gtk_tree_model_get (model, &iter,
498                                     COL_ACCOUNT_POINTER, &this_account,
499                                     -1);
500
501                 equal = gossip_account_equal (this_account, account);
502                 g_object_unref (this_account);
503
504                 if (equal) {
505                         gtk_tree_selection_select_iter (selection, &iter);
506                         break;
507                 }
508         }
509 }
510
511 static gboolean
512 accounts_dialog_model_remove_selected (GossipAccountsDialog *dialog)
513 {
514         GtkTreeView      *view;
515         GtkTreeModel     *model;
516         GtkTreeSelection *selection;
517         GtkTreeIter       iter;
518
519         view = GTK_TREE_VIEW (dialog->treeview);
520         selection = gtk_tree_view_get_selection (view);
521
522         if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
523                 return FALSE;
524         }
525
526         return gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
527 }
528
529 static void
530 accounts_dialog_model_selection_changed (GtkTreeSelection     *selection,
531                                          GossipAccountsDialog *dialog)
532 {
533         McAccount    *account;
534         GtkTreeModel *model;
535         GtkTreeIter   iter;
536         gboolean      is_selection;
537
538         is_selection = gtk_tree_selection_get_selected (selection, &model, &iter);
539
540         gtk_widget_set_sensitive (dialog->button_remove, is_selection);
541         gtk_widget_set_sensitive (dialog->button_connect, is_selection);
542
543         accounts_dialog_update_connect_button (dialog);
544
545         account = accounts_dialog_model_get_selected (dialog);
546         accounts_dialog_update_account (dialog, account);
547
548         if (account) {
549                 g_object_unref (account);
550         }
551 }
552
553 static void
554 accounts_dialog_add_account (GossipAccountsDialog *dialog,
555                              McAccount            *account)
556 {
557         MissionControl            *mc;
558         TelepathyConnectionStatus  status;
559         const gchar               *name;
560         GtkTreeView               *view;
561         GtkTreeModel              *model;
562         GtkListStore              *store;
563         GtkTreeIter                iter;
564         gboolean                   ok;
565
566         view = GTK_TREE_VIEW (dialog->treeview);
567         model = gtk_tree_view_get_model (view);
568         store = GTK_LIST_STORE (model);
569
570         for (ok = gtk_tree_model_get_iter_first (model, &iter);
571              ok;
572              ok = gtk_tree_model_iter_next (model, &iter)) {
573                 McAccount *this_account;
574                 gboolean   equal;
575
576                 gtk_tree_model_get (model, &iter,
577                                     COL_ACCOUNT_POINTER, &this_account,
578                                     -1);
579
580                 equal =  gossip_account_equal (this_account, account);
581                 g_object_unref (this_account);
582
583                 if (equal) {
584                         return;
585                 }
586         }
587
588         mc = empathy_session_get_mission_control ();
589         status = mission_control_get_connection_status (mc, account, NULL);
590         name = mc_account_get_display_name (account);
591
592         g_return_if_fail (name != NULL);
593
594         gossip_debug (DEBUG_DOMAIN, "Adding new account: %s", name);
595
596         gtk_list_store_append (store, &iter);
597         gtk_list_store_set (store, &iter,
598                             COL_NAME, name,
599                             COL_STATUS, status,
600                             COL_ACCOUNT_POINTER, account,
601                             -1);
602 }
603
604 static void
605 accounts_dialog_account_added_cb (McAccountMonitor     *monitor,
606                                   gchar                *unique_name,
607                                   GossipAccountsDialog *dialog)
608 {
609         McAccount *account;
610
611         account = mc_account_lookup (unique_name);
612         accounts_dialog_add_account (dialog, account);
613         g_object_unref (account);
614 }
615
616 static void
617 accounts_dialog_account_removed_cb (McAccountMonitor     *monitor,
618                                     gchar                *unique_name,
619                                     GossipAccountsDialog *dialog)
620 {
621         MissionControl *mc;
622         McAccount      *account;
623
624         mc = empathy_session_get_mission_control ();
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 (GossipAccountsDialog *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                                    GossipAccountsDialog            *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         for (ok = gtk_tree_model_get_iter_first (model, &iter);
684              ok;
685              ok = gtk_tree_model_iter_next (model, &iter)) {
686                 McAccount *this_account;
687                 gboolean   equal;
688
689                 gtk_tree_model_get (model, &iter,
690                                     COL_ACCOUNT_POINTER, &this_account,
691                                     -1);
692
693                 equal = gossip_account_equal (this_account, account);
694                 g_object_unref (this_account);
695
696                 if (equal) {
697                         GtkTreePath *path;
698
699                         gtk_list_store_set (GTK_LIST_STORE (model), &iter,
700                                             COL_STATUS, status,
701                                             -1);
702
703                         path = gtk_tree_model_get_path (model, &iter);
704                         gtk_tree_model_row_changed (model, path, &iter);
705                         gtk_tree_path_free (path);
706
707                         break;
708                 }
709         }
710
711         g_object_unref (account);
712
713         /* Start to flash account if status is connecting */
714         if (status == TP_CONN_STATUS_CONNECTING) {
715                 if (!dialog->connecting_id) {
716                         dialog->connecting_id = g_timeout_add (FLASH_TIMEOUT,
717                                                                (GSourceFunc) accounts_dialog_flash_connecting_cb,
718                                                                dialog);
719                 }
720
721                 return;
722         }
723
724         /* Stop to flash if no account is connecting */
725         accounts = mc_accounts_list ();
726         for (l = accounts; l; l = l->next) {
727                 McAccount *this_account;
728
729                 this_account = l->data;
730
731                 if (mission_control_get_connection_status (mc, this_account, NULL) == TP_CONN_STATUS_CONNECTING) {
732                         found = TRUE;
733                         break;
734                 }
735
736                 g_object_unref (this_account);
737         }
738         g_list_free (accounts);
739
740         if (!found && dialog->connecting_id) {
741                 g_source_remove (dialog->connecting_id);
742                 dialog->connecting_id = 0;
743         }
744 }
745
746 static void          
747 accounts_dialog_entry_name_changed_cb (GtkWidget             *widget,
748                                        GossipAccountsDialog  *dialog)
749 {
750         const gchar *str;
751         
752         str = gtk_entry_get_text (GTK_ENTRY (widget));
753         gtk_widget_set_sensitive (dialog->button_create, !G_STR_EMPTY (str));
754 }
755
756 static void
757 accounts_dialog_button_create_clicked_cb (GtkWidget             *button,
758                                           GossipAccountsDialog  *dialog)
759 {
760         McProfile   *profile;
761         McAccount   *account;
762         const gchar *str;
763
764         /* Update widgets */
765         gtk_widget_show (dialog->vbox_details);
766         gtk_widget_hide (dialog->frame_no_account);
767         gtk_widget_hide (dialog->frame_new_account);
768
769         profile = gossip_profile_chooser_get_selected (dialog->combobox_profile);
770
771         /* Create account */
772         account = mc_account_create (profile);
773
774         str = gtk_entry_get_text (GTK_ENTRY (dialog->entry_name));
775         mc_account_set_display_name (account, str);
776
777         accounts_dialog_add_account (dialog, account);
778         accounts_dialog_model_set_selected (dialog, account);
779
780         g_object_unref (account);
781         g_object_unref (profile);
782 }
783
784 static void
785 accounts_dialog_button_cancel_clicked_cb (GtkWidget             *button,
786                                           GossipAccountsDialog  *dialog)
787 {
788         McAccount *account;
789
790         gtk_widget_hide (dialog->vbox_details);
791         gtk_widget_hide (dialog->frame_no_account);
792         gtk_widget_hide (dialog->frame_new_account);
793
794         account = accounts_dialog_model_get_selected (dialog);
795         accounts_dialog_update_account (dialog, account);
796 }
797
798 static void
799 accounts_dialog_button_connect_clicked_cb (GtkWidget            *button,
800                                            GossipAccountsDialog *dialog)
801 {
802         McAccount *account;
803         gboolean   enable;
804
805         account = accounts_dialog_model_get_selected (dialog);
806         enable = (!mc_account_is_enabled (account));
807         mc_account_set_enabled (account, enable);
808         accounts_dialog_update_connect_button (dialog);
809
810         g_object_unref (account);
811 }
812
813 static void
814 accounts_dialog_button_add_clicked_cb (GtkWidget            *button,
815                                        GossipAccountsDialog *dialog)
816 {
817         gtk_widget_hide (dialog->vbox_details);
818         gtk_widget_hide (dialog->frame_no_account);
819         gtk_widget_show (dialog->frame_new_account);
820
821         gtk_combo_box_set_active (GTK_COMBO_BOX (dialog->combobox_profile), 0);
822         gtk_entry_set_text (GTK_ENTRY (dialog->entry_name), "");
823         gtk_widget_grab_focus (dialog->entry_name);
824 }
825
826 static void
827 accounts_dialog_remove_response_cb (GtkWidget *dialog,
828                                     gint       response,
829                                     McAccount *account)
830 {
831         if (response == GTK_RESPONSE_YES) {
832                 mc_account_delete (account);
833         }
834
835         gtk_widget_destroy (dialog);
836 }
837
838 static void
839 accounts_dialog_button_remove_clicked_cb (GtkWidget            *button,
840                                           GossipAccountsDialog *dialog)
841 {
842         McAccount *account;
843         GtkWidget *message_dialog;
844
845         account = accounts_dialog_model_get_selected (dialog);
846
847         if (!mc_account_is_complete (account)) {
848                 accounts_dialog_model_remove_selected (dialog);
849                 return;
850         }
851         message_dialog = gtk_message_dialog_new
852                 (GTK_WINDOW (dialog->window),
853                  GTK_DIALOG_DESTROY_WITH_PARENT,
854                  GTK_MESSAGE_QUESTION,
855                  GTK_BUTTONS_NONE,
856                  _("You are about to remove your %s account!\n"
857                    "Are you sure you want to proceed?"),
858                  mc_account_get_display_name (account));
859
860         gtk_message_dialog_format_secondary_text
861                 (GTK_MESSAGE_DIALOG (message_dialog),
862                  _("Any associated conversations and chat rooms will NOT be "
863                    "removed if you decide to proceed.\n"
864                    "\n"
865                    "Should you decide to add the account back at a later time, "
866                    "they will still be available."));
867
868         gtk_dialog_add_button (GTK_DIALOG (message_dialog),
869                                GTK_STOCK_CANCEL, 
870                                GTK_RESPONSE_NO);
871         gtk_dialog_add_button (GTK_DIALOG (message_dialog),
872                                GTK_STOCK_REMOVE, 
873                                GTK_RESPONSE_YES);
874
875         g_signal_connect (message_dialog, "response",
876                           G_CALLBACK (accounts_dialog_remove_response_cb),
877                           account);
878
879         gtk_widget_show (message_dialog);
880 }
881
882 static void
883 accounts_dialog_treeview_row_activated_cb (GtkTreeView          *tree_view,
884                                            GtkTreePath          *path,
885                                            GtkTreeViewColumn    *column,
886                                            GossipAccountsDialog *dialog)
887 {
888
889         accounts_dialog_button_connect_clicked_cb (dialog->button_connect,
890                                                    dialog);
891 }
892
893 static void
894 accounts_dialog_response_cb (GtkWidget            *widget,
895                              gint                  response,
896                              GossipAccountsDialog *dialog)
897 {
898         gtk_widget_destroy (widget);
899 }
900
901 static void
902 accounts_dialog_destroy_cb (GtkWidget            *widget,
903                             GossipAccountsDialog *dialog)
904 {
905         MissionControl   *mc;
906         McAccountMonitor *monitor;
907         GList            *accounts, *l;
908
909         mc = empathy_session_get_mission_control ();
910         monitor = mc_account_monitor_new ();
911
912         /* Disconnect signals */
913         g_signal_handlers_disconnect_by_func (monitor,
914                                               accounts_dialog_account_added_cb,
915                                               dialog);
916         g_signal_handlers_disconnect_by_func (monitor,
917                                               accounts_dialog_account_removed_cb,
918                                               dialog);
919         dbus_g_proxy_disconnect_signal (DBUS_G_PROXY (mc), "AccountStatusChanged",
920                                      G_CALLBACK (accounts_dialog_status_changed_cb),
921                                      dialog);
922
923         /* Delete incomplete accounts */
924         accounts = mc_accounts_list ();
925         for (l = accounts; l; l = l->next) {
926                 McAccount *account;
927
928                 account = l->data;
929                 if (!mc_account_is_complete (account)) {
930                         /* FIXME: Warn the user the account is not complete
931                          *        and is going to be removed. */
932                         mc_account_delete (account);
933                 }
934
935                 g_object_unref (account);
936         }
937         g_list_free (accounts);
938
939         if (dialog->connecting_id) {
940                 g_source_remove (dialog->connecting_id);
941         }
942
943         g_free (dialog);
944 }
945
946 GtkWidget *
947 gossip_accounts_dialog_show (void)
948 {
949         static GossipAccountsDialog *dialog = NULL;
950         MissionControl              *mc;
951         McAccountMonitor            *monitor;
952         GladeXML                    *glade;
953         GtkWidget                   *bbox;
954         GtkWidget                   *button_close;
955
956         if (dialog) {
957                 gtk_window_present (GTK_WINDOW (dialog->window));
958                 return dialog->window;
959         }
960
961         dialog = g_new0 (GossipAccountsDialog, 1);
962
963         glade = gossip_glade_get_file ("gossip-accounts-dialog.glade",
964                                        "accounts_dialog",
965                                        NULL,
966                                        "accounts_dialog", &dialog->window,
967                                        "vbox_details", &dialog->vbox_details,
968                                        "frame_no_account", &dialog->frame_no_account,
969                                        "label_no_account", &dialog->label_no_account,
970                                        "label_no_account_blurb", &dialog->label_no_account_blurb,
971                                        "alignment_settings", &dialog->alignment_settings,
972                                        "dialog-action_area", &bbox,
973                                        "treeview", &dialog->treeview,
974                                        "frame_new_account", &dialog->frame_new_account,
975                                        "entry_name", &dialog->entry_name,
976                                        "table_new_account", &dialog->table_new_account,
977                                        "button_create", &dialog->button_create,
978                                        "button_cancel", &dialog->button_cancel,
979                                        "image_type", &dialog->image_type,
980                                        "label_type", &dialog->label_type,
981                                        "label_name", &dialog->label_name,
982                                        "button_remove", &dialog->button_remove,
983                                        "button_connect", &dialog->button_connect,
984                                        "button_close", &button_close,
985                                        NULL);
986
987         gossip_glade_connect (glade,
988                               dialog,
989                               "accounts_dialog", "destroy", accounts_dialog_destroy_cb,
990                               "accounts_dialog", "response", accounts_dialog_response_cb,
991                               "button_create", "clicked", accounts_dialog_button_create_clicked_cb,
992                               "button_cancel", "clicked", accounts_dialog_button_cancel_clicked_cb,
993                               "entry_name", "changed", accounts_dialog_entry_name_changed_cb,
994                               "treeview", "row-activated", accounts_dialog_treeview_row_activated_cb,
995                               "button_connect", "clicked", accounts_dialog_button_connect_clicked_cb,
996                               "button_add", "clicked", accounts_dialog_button_add_clicked_cb,
997                               "button_remove", "clicked", accounts_dialog_button_remove_clicked_cb,
998                               NULL);
999
1000         g_object_add_weak_pointer (G_OBJECT (dialog->window), (gpointer) &dialog);
1001
1002         g_object_unref (glade);
1003
1004         /* Create profile chooser */
1005         dialog->combobox_profile = gossip_profile_chooser_new ();
1006         gtk_table_attach_defaults (GTK_TABLE (dialog->table_new_account),
1007                                    dialog->combobox_profile,
1008                                    1, 2,
1009                                    0, 1);
1010         gtk_widget_show (dialog->combobox_profile);
1011
1012         /* Set up signalling */
1013         mc = empathy_session_get_mission_control ();
1014         monitor = mc_account_monitor_new ();
1015
1016         /* FIXME: connect account-enabled/disabled too */
1017         g_signal_connect (monitor, "account-created",
1018                           G_CALLBACK (accounts_dialog_account_added_cb),
1019                           dialog);
1020         g_signal_connect (monitor, "account-deleted",
1021                           G_CALLBACK (accounts_dialog_account_removed_cb),
1022                           dialog);
1023         dbus_g_proxy_connect_signal (DBUS_G_PROXY (mc), "AccountStatusChanged",
1024                                      G_CALLBACK (accounts_dialog_status_changed_cb),
1025                                      dialog, NULL);
1026
1027         accounts_dialog_model_setup (dialog);
1028         accounts_dialog_setup (dialog);
1029
1030         gtk_widget_show (dialog->window);
1031
1032         accounts_dialog_model_select_first (dialog);
1033
1034         return dialog->window;
1035 }
1036