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