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