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