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