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