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