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