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