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