]> git.0d.be Git - empathy.git/blob - libempathy-gtk/gossip-new-chatroom-dialog.c
838f90a7436925f189b1c2f1e6bbb5b33995b9b7
[empathy.git] / libempathy-gtk / gossip-new-chatroom-dialog.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2006-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 <stdio.h>
29
30 #include <gtk/gtk.h>
31 #include <glade/glade.h>
32 #include <glib.h>
33 #include <glib/gi18n.h>
34
35 #include <libmissioncontrol/mission-control.h>
36 #include <libmissioncontrol/mc-account.h>
37 #include <libmissioncontrol/mc-profile.h>
38
39 #include <libempathy/gossip-utils.h>
40 #include <libempathy/gossip-debug.h>
41
42 #include "gossip-new-chatroom-dialog.h"
43 #include "gossip-account-chooser.h"
44 //#include "gossip-chatrooms-window.h"
45 #include "gossip-ui-utils.h"
46 #include "ephy-spinner.h"
47
48 #define DEBUG_DOMAIN "NewChatroomDialog"
49
50 typedef struct {
51         GtkWidget        *window;
52
53         GtkWidget        *vbox_widgets;
54
55         GtkWidget        *hbox_account;
56         GtkWidget        *label_account;
57         GtkWidget        *account_chooser;
58
59         GtkWidget        *hbox_server;
60         GtkWidget        *label_server;
61         GtkWidget        *entry_server;
62         GtkWidget        *togglebutton_refresh;
63         
64         GtkWidget        *hbox_room;
65         GtkWidget        *label_room;
66         GtkWidget        *entry_room;
67
68         GtkWidget        *hbox_nick;
69         GtkWidget        *label_nick;
70         GtkWidget        *entry_nick;
71
72         GtkWidget        *vbox_browse;
73         GtkWidget        *image_status;
74         GtkWidget        *label_status;
75         GtkWidget        *hbox_status;
76         GtkWidget        *throbber;
77         GtkWidget        *treeview;
78         GtkTreeModel     *model;
79         GtkTreeModel     *filter;
80
81         GtkWidget        *button_join;
82         GtkWidget        *button_close;
83 } GossipNewChatroomDialog;
84
85 typedef struct {
86         guint  handle;
87         gchar *channel_type;
88         gchar *name;
89         gchar *id;
90 } EmpathyRoomListItem;
91
92 enum {
93         COL_IMAGE,
94         COL_NAME,
95         COL_POINTER,
96         COL_COUNT
97 };
98
99 static void     new_chatroom_dialog_response_cb                     (GtkWidget               *widget,
100                                                                      gint                     response,
101                                                                      GossipNewChatroomDialog *dialog);
102 static void     new_chatroom_dialog_destroy_cb                      (GtkWidget               *widget,
103                                                                      GossipNewChatroomDialog *dialog);
104 static void     new_chatroom_dialog_model_setup                     (GossipNewChatroomDialog *dialog);
105 static void     new_chatroom_dialog_model_add_columns               (GossipNewChatroomDialog *dialog);
106 static void     new_chatroom_dialog_update_buttons                  (GossipNewChatroomDialog *dialog);
107 static void     new_chatroom_dialog_update_widgets                  (GossipNewChatroomDialog *dialog);
108 static void     new_chatroom_dialog_account_changed_cb              (GtkComboBox             *combobox,
109                                                                      GossipNewChatroomDialog *dialog);
110 static void     new_chatroom_dialog_model_add                       (GossipNewChatroomDialog *dialog,
111                                                                      EmpathyRoomListItem     *item);
112 static void     new_chatroom_dialog_model_clear                     (GossipNewChatroomDialog *dialog);
113 static GList *  new_chatroom_dialog_model_get_selected              (GossipNewChatroomDialog *dialog);
114 static gboolean new_chatroom_dialog_model_filter_func               (GtkTreeModel            *model,
115                                                                      GtkTreeIter             *iter,
116                                                                      GossipNewChatroomDialog *dialog);
117 static void     new_chatroom_dialog_model_row_activated_cb          (GtkTreeView             *tree_view,
118                                                                      GtkTreePath             *path,
119                                                                      GtkTreeViewColumn       *column,
120                                                                      GossipNewChatroomDialog *dialog);
121 static void     new_chatroom_dialog_model_row_inserted_cb           (GtkTreeModel            *model,
122                                                                      GtkTreePath             *path,
123                                                                      GtkTreeIter             *iter,
124                                                                      GossipNewChatroomDialog *dialog);
125 static void     new_chatroom_dialog_model_row_deleted_cb            (GtkTreeModel            *model,
126                                                                      GtkTreePath             *path,
127                                                                      GossipNewChatroomDialog *dialog);
128 static void     new_chatroom_dialog_model_selection_changed         (GtkTreeSelection        *selection,
129                                                                      GossipNewChatroomDialog *dialog);
130 static void     new_chatroom_dialog_join                            (GossipNewChatroomDialog *dialog);
131 static void     new_chatroom_dialog_request_handles_cb              (DBusGProxy              *proxy,
132                                                                      GArray                  *handles,
133                                                                      GError                  *error,
134                                                                      McAccount               *account);
135 static void     new_chatroom_dialog_entry_changed_cb                (GtkWidget               *entry,
136                                                                      GossipNewChatroomDialog *dialog);
137 static void     new_chatroom_dialog_browse_start                    (GossipNewChatroomDialog *dialog);
138 static void     new_chatroom_dialog_browse_stop                     (GossipNewChatroomDialog *dialog);
139 static void     new_chatroom_dialog_entry_server_activate_cb        (GtkWidget               *widget,
140                                                                      GossipNewChatroomDialog *dialog);
141 static void     new_chatroom_dialog_togglebutton_refresh_toggled_cb (GtkWidget               *widget,
142                                                                      GossipNewChatroomDialog *dialog);
143
144 static GossipNewChatroomDialog *dialog_p = NULL;
145
146 void
147 gossip_new_chatroom_dialog_show (GtkWindow *parent)
148 {
149         GossipNewChatroomDialog *dialog;
150         GladeXML                *glade;
151         GList                   *accounts;
152         gint                     account_num;
153         GtkSizeGroup            *size_group;
154
155         if (dialog_p) {
156                 gtk_window_present (GTK_WINDOW (dialog_p->window));
157                 return;
158         }
159
160         dialog_p = dialog = g_new0 (GossipNewChatroomDialog, 1);
161
162         glade = gossip_glade_get_file ("gossip-new-chatroom-dialog.glade",
163                                        "new_chatroom_dialog",
164                                        NULL,
165                                        "new_chatroom_dialog", &dialog->window,
166                                        "hbox_account", &dialog->hbox_account,
167                                        "label_account", &dialog->label_account,
168                                        "vbox_widgets", &dialog->vbox_widgets,
169                                        "label_server", &dialog->label_server,
170                                        "label_room", &dialog->label_room,
171                                        "label_nick", &dialog->label_nick,
172                                        "hbox_server", &dialog->hbox_server,
173                                        "hbox_room", &dialog->hbox_room,
174                                        "hbox_nick", &dialog->hbox_nick,
175                                        "entry_server", &dialog->entry_server,
176                                        "entry_room", &dialog->entry_room,
177                                        "entry_nick", &dialog->entry_nick,
178                                        "togglebutton_refresh", &dialog->togglebutton_refresh,
179                                        "vbox_browse", &dialog->vbox_browse,
180                                        "image_status", &dialog->image_status,
181                                        "label_status", &dialog->label_status,
182                                        "hbox_status", &dialog->hbox_status,
183                                        "treeview", &dialog->treeview,
184                                        "button_join", &dialog->button_join,
185                                        NULL);
186
187         gossip_glade_connect (glade,
188                               dialog,
189                               "new_chatroom_dialog", "response", new_chatroom_dialog_response_cb,
190                               "new_chatroom_dialog", "destroy", new_chatroom_dialog_destroy_cb,
191                               "entry_nick", "changed", new_chatroom_dialog_entry_changed_cb,
192                               "entry_server", "changed", new_chatroom_dialog_entry_changed_cb,
193                               "entry_server", "activate", new_chatroom_dialog_entry_server_activate_cb,
194                               "entry_room", "changed", new_chatroom_dialog_entry_changed_cb,
195                               "togglebutton_refresh", "toggled", new_chatroom_dialog_togglebutton_refresh_toggled_cb,
196                               NULL);
197
198         g_object_unref (glade);
199
200         g_object_add_weak_pointer (G_OBJECT (dialog->window), (gpointer) &dialog_p);
201
202         /* Label alignment */
203         size_group = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
204
205         gtk_size_group_add_widget (size_group, dialog->label_account);
206         gtk_size_group_add_widget (size_group, dialog->label_server);
207         gtk_size_group_add_widget (size_group, dialog->label_room);
208         gtk_size_group_add_widget (size_group, dialog->label_nick);
209
210         g_object_unref (size_group);
211
212         /* Account chooser for custom */
213         dialog->account_chooser = gossip_account_chooser_new ();
214         gtk_box_pack_start (GTK_BOX (dialog->hbox_account),
215                             dialog->account_chooser,
216                             TRUE, TRUE, 0);
217         gtk_widget_show (dialog->account_chooser);
218
219         g_signal_connect (GTK_COMBO_BOX (dialog->account_chooser), "changed",
220                           G_CALLBACK (new_chatroom_dialog_account_changed_cb),
221                           dialog);
222
223         /* Populate */
224         accounts = mc_accounts_list ();
225         account_num = g_list_length (accounts);
226
227         g_list_foreach (accounts, (GFunc) g_object_unref, NULL);
228         g_list_free (accounts);
229
230         if (account_num > 1) {
231                 gtk_widget_show (dialog->hbox_account);
232         } else {
233                 /* Show no accounts combo box */
234                 gtk_widget_hide (dialog->hbox_account);
235         }
236
237         /* Add throbber */
238         dialog->throbber = ephy_spinner_new ();
239         ephy_spinner_set_size (EPHY_SPINNER (dialog->throbber), GTK_ICON_SIZE_LARGE_TOOLBAR);
240         gtk_widget_show (dialog->throbber);
241
242         gtk_box_pack_start (GTK_BOX (dialog->hbox_status), dialog->throbber, 
243                             FALSE, FALSE, 0);
244
245         /* Set up chatrooms treeview */
246         new_chatroom_dialog_model_setup (dialog);
247
248         /* Set things up according to the account type */
249         new_chatroom_dialog_update_widgets (dialog);
250
251         if (parent) {
252                 gtk_window_set_transient_for (GTK_WINDOW (dialog->window),
253                                               GTK_WINDOW (parent));
254         }
255
256         gtk_widget_show (dialog->window);
257 }
258
259 static void
260 new_chatroom_dialog_response_cb (GtkWidget               *widget,
261                                  gint                     response,
262                                  GossipNewChatroomDialog *dialog)
263 {
264         if (response == GTK_RESPONSE_OK) {
265                 new_chatroom_dialog_join (dialog);
266         }
267
268         gtk_widget_destroy (widget);
269 }
270
271 static void
272 new_chatroom_dialog_destroy_cb (GtkWidget               *widget,
273                                 GossipNewChatroomDialog *dialog)
274 {
275         g_object_unref (dialog->model);  
276         g_object_unref (dialog->filter); 
277
278         g_free (dialog);
279 }
280
281 static void
282 new_chatroom_dialog_model_setup (GossipNewChatroomDialog *dialog)
283 {
284         GtkTreeView      *view;
285         GtkListStore     *store;
286         GtkTreeSelection *selection;
287
288         /* View */
289         view = GTK_TREE_VIEW (dialog->treeview);
290
291         g_signal_connect (view, "row-activated",
292                           G_CALLBACK (new_chatroom_dialog_model_row_activated_cb),
293                           dialog);
294
295         /* Store/Model */
296         store = gtk_list_store_new (COL_COUNT,
297                                     G_TYPE_STRING,       /* Image */
298                                     G_TYPE_STRING,       /* Text */
299                                     G_TYPE_POINTER);     /* infos */
300
301         dialog->model = GTK_TREE_MODEL (store);
302
303         /* Filter */
304         dialog->filter = gtk_tree_model_filter_new (dialog->model, NULL);
305
306         gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (dialog->filter),
307                                                 (GtkTreeModelFilterVisibleFunc)
308                                                 new_chatroom_dialog_model_filter_func,
309                                                 dialog,
310                                                 NULL);
311
312         gtk_tree_view_set_model (view, dialog->filter);
313
314         g_signal_connect (dialog->filter, "row-inserted",
315                           G_CALLBACK (new_chatroom_dialog_model_row_inserted_cb),
316                           dialog);
317         g_signal_connect (dialog->filter, "row-deleted",
318                           G_CALLBACK (new_chatroom_dialog_model_row_deleted_cb),
319                           dialog);
320
321         /* Selection */
322         selection = gtk_tree_view_get_selection (view);
323         gtk_tree_selection_set_mode (selection, GTK_SELECTION_MULTIPLE);
324         gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
325                                               COL_NAME, GTK_SORT_ASCENDING);
326
327         g_signal_connect (selection, "changed",
328                           G_CALLBACK (new_chatroom_dialog_model_selection_changed), dialog);
329
330         /* Columns */
331         new_chatroom_dialog_model_add_columns (dialog);
332 }
333
334 static void
335 new_chatroom_dialog_model_add_columns (GossipNewChatroomDialog *dialog)
336 {
337         GtkTreeView       *view;
338         GtkTreeViewColumn *column;
339         GtkCellRenderer   *cell;
340
341         view = GTK_TREE_VIEW (dialog->treeview);
342         gtk_tree_view_set_headers_visible (view, FALSE);
343
344         /* Chatroom pointer */
345         column = gtk_tree_view_column_new ();
346         gtk_tree_view_column_set_title (column, _("Chat Rooms"));
347
348         cell = gtk_cell_renderer_pixbuf_new ();
349         gtk_tree_view_column_pack_start (column, cell, FALSE);
350
351         cell = gtk_cell_renderer_text_new ();
352         g_object_set (cell,
353                       "xpad", (guint) 4,
354                       "ypad", (guint) 1,
355                       "ellipsize", PANGO_ELLIPSIZE_END,
356                       NULL);
357
358         gtk_tree_view_column_pack_start (column, cell, TRUE);
359
360         gtk_tree_view_column_set_expand (column, TRUE);
361         gtk_tree_view_append_column (view, column);
362 }
363
364 static void
365 new_chatroom_dialog_update_buttons (GossipNewChatroomDialog *dialog)
366 {
367         GtkButton            *button;
368         GtkWidget            *image;
369         GtkTreeView          *view;
370         GtkTreeModel         *model;
371         guint                 items;
372         const gchar          *room;
373
374         /* Sort out Join button. */
375         button = GTK_BUTTON (dialog->button_join);
376         
377         image = gtk_button_get_image (button);
378         if (!image) {
379                 image = gtk_image_new ();
380                 gtk_button_set_image (button, image);
381         }
382         //gtk_button_set_use_stock (button, FALSE);
383
384         room = gtk_entry_get_text (GTK_ENTRY (dialog->entry_room));
385
386         /* Collect necessary information first. */
387         view = GTK_TREE_VIEW (dialog->treeview);
388         model = gtk_tree_view_get_model (view);
389         items = gtk_tree_model_iter_n_children (model, NULL);
390                 
391         if (items < 1 && !G_STR_EMPTY (room)) {
392                 gtk_button_set_label (button, _("Create"));
393                 gtk_image_set_from_stock (GTK_IMAGE (image),
394                                           GTK_STOCK_NEW,
395                                           GTK_ICON_SIZE_BUTTON);
396         } else {
397                 gtk_button_set_label (button, _("Join"));
398                 gtk_image_set_from_stock (GTK_IMAGE (image),
399                                           GTK_STOCK_EXECUTE,
400                                           GTK_ICON_SIZE_BUTTON);
401         }
402
403         gtk_widget_set_sensitive (dialog->button_join, !G_STR_EMPTY (room));
404 }
405
406 static void
407 new_chatroom_dialog_update_widgets (GossipNewChatroomDialog *dialog)
408 {
409         GossipAccountChooser *account_chooser;
410         McAccount            *account;
411         McProfile            *profile;
412         const gchar          *protocol;
413         
414         account_chooser = GOSSIP_ACCOUNT_CHOOSER (dialog->account_chooser);
415         account = gossip_account_chooser_get_account (account_chooser);
416         profile = mc_account_get_profile (account);
417         protocol = mc_profile_get_protocol_name (profile);
418
419         /* hardcode here known protocols */
420         if (strcmp (protocol, "jabber") == 0) {
421                 const gchar *server;
422
423                 server = mc_profile_get_default_account_domain (profile);
424                 if (server) {
425                         gchar *conference_server;
426
427                         conference_server = g_strconcat ("conference.",
428                                                          server, NULL);
429                         gtk_entry_set_text (GTK_ENTRY (dialog->entry_server),
430                                                        conference_server);
431                         g_free (conference_server);
432                 }
433
434                 gtk_widget_show (dialog->hbox_server);
435                 gtk_widget_show (dialog->hbox_nick);
436                 gtk_widget_show (dialog->vbox_browse);
437
438         }
439         else if (strcmp (protocol, "salut") == 0) {
440                 gtk_widget_hide (dialog->hbox_server);
441                 gtk_widget_show (dialog->hbox_nick);
442                 gtk_widget_show (dialog->vbox_browse);          
443         }
444         else if (strcmp (protocol, "irc") == 0) {
445                 gtk_widget_hide (dialog->hbox_server);
446                 gtk_widget_hide (dialog->hbox_nick);
447                 gtk_widget_show (dialog->vbox_browse);          
448         } else {
449                 gtk_widget_hide (dialog->hbox_server);
450                 gtk_widget_hide (dialog->hbox_nick);
451                 gtk_widget_hide (dialog->vbox_browse);
452         }
453
454         new_chatroom_dialog_update_buttons (dialog);
455
456         /* Final set up of the dialog */
457         gtk_widget_grab_focus (dialog->entry_room);
458
459         g_object_unref (account);
460         g_object_unref (profile);
461 }
462
463 static void
464 new_chatroom_dialog_account_changed_cb (GtkComboBox             *combobox,
465                                         GossipNewChatroomDialog *dialog)
466 {
467         new_chatroom_dialog_update_widgets (dialog);
468 }
469
470 static void
471 new_chatroom_dialog_model_add (GossipNewChatroomDialog *dialog,
472                                EmpathyRoomListItem     *item)
473 {
474         GtkTreeView      *view;
475         GtkTreeSelection *selection;
476         GtkListStore     *store;
477         GtkTreeIter       iter;
478
479         /* Add to model */
480         view = GTK_TREE_VIEW (dialog->treeview);
481         selection = gtk_tree_view_get_selection (view);
482         store = GTK_LIST_STORE (dialog->model);
483
484         gtk_list_store_append (store, &iter);
485
486         gtk_list_store_set (store, &iter,
487                             COL_NAME, item->name,
488                             COL_POINTER, item,
489                             -1);
490 }
491
492 static void
493 new_chatroom_dialog_model_clear (GossipNewChatroomDialog *dialog)
494 {
495         GtkListStore *store;
496
497         store = GTK_LIST_STORE (dialog->model);
498         gtk_list_store_clear (store);
499 }
500
501 static GList *
502 new_chatroom_dialog_model_get_selected (GossipNewChatroomDialog *dialog)
503 {
504         GtkTreeView      *view;
505         GtkTreeModel     *model;
506         GtkTreeSelection *selection;
507         GList            *rows, *l;
508         GList            *chatrooms = NULL;
509
510         view = GTK_TREE_VIEW (dialog->treeview);
511         selection = gtk_tree_view_get_selection (view);
512         model = gtk_tree_view_get_model (view);
513
514         rows = gtk_tree_selection_get_selected_rows (selection, NULL);
515         for (l = rows; l; l = l->next) {
516                 GtkTreeIter          iter;
517                 EmpathyRoomListItem *chatroom;
518
519                 if (!gtk_tree_model_get_iter (model, &iter, l->data)) {
520                         continue;
521                 }
522
523                 gtk_tree_model_get (model, &iter, COL_POINTER, &chatroom, -1);
524                 chatrooms = g_list_append (chatrooms, chatroom);
525         }
526
527         g_list_foreach (rows, (GFunc) gtk_tree_path_free, NULL);
528         g_list_free (rows);
529
530         return chatrooms;
531 }
532
533 static gboolean
534 new_chatroom_dialog_model_filter_func (GtkTreeModel            *model,
535                                        GtkTreeIter             *iter,
536                                        GossipNewChatroomDialog *dialog)
537 {
538         EmpathyRoomListItem *chatroom;
539         const gchar         *text;
540         gchar               *room_nocase;
541         gchar               *text_nocase;
542         gboolean             found = FALSE;
543
544         gtk_tree_model_get (model, iter, COL_POINTER, &chatroom, -1);
545
546         if (!chatroom) {
547                 return TRUE;
548         }
549
550         text = gtk_entry_get_text (GTK_ENTRY (dialog->entry_room));
551
552         /* Casefold */
553         room_nocase = g_utf8_casefold (chatroom->id, -1);
554         text_nocase = g_utf8_casefold (text, -1);
555
556         /* Compare */
557         if (g_utf8_strlen (text_nocase, -1) < 1 ||
558             strstr (room_nocase, text_nocase)) {
559                 found = TRUE;
560         }
561
562         g_free (room_nocase);
563         g_free (text_nocase);
564
565         return found;
566 }
567
568 static void
569 new_chatroom_dialog_model_row_activated_cb (GtkTreeView             *tree_view,
570                                             GtkTreePath             *path,
571                                             GtkTreeViewColumn       *column,
572                                             GossipNewChatroomDialog *dialog)
573 {
574         gtk_widget_activate (dialog->button_join);
575 }
576
577 static void
578 new_chatroom_dialog_model_row_inserted_cb (GtkTreeModel            *model,
579                                            GtkTreePath             *path,
580                                            GtkTreeIter             *iter,
581                                            GossipNewChatroomDialog *dialog)
582 {
583         new_chatroom_dialog_update_buttons (dialog);
584 }
585
586 static void
587 new_chatroom_dialog_model_row_deleted_cb (GtkTreeModel            *model,
588                                           GtkTreePath             *path,
589                                           GossipNewChatroomDialog *dialog)
590 {
591         new_chatroom_dialog_update_buttons (dialog);
592 }
593
594 static void
595 new_chatroom_dialog_model_selection_changed (GtkTreeSelection      *selection,
596                                              GossipNewChatroomDialog *dialog)
597 {
598         new_chatroom_dialog_update_buttons (dialog);
599 }
600
601 static void
602 new_chatroom_dialog_join (GossipNewChatroomDialog *dialog)
603 {
604         McAccount            *account;
605         GossipAccountChooser *account_chooser;
606         MissionControl       *mc;
607         TpConn               *tp_conn;
608         GList                *chatrooms, *l;
609         const gchar          *room;
610         const gchar          *server = NULL;
611         gchar                *room_name = NULL;
612         const gchar          *room_names[2] = {NULL, NULL};
613
614         chatrooms = new_chatroom_dialog_model_get_selected (dialog);
615         if (chatrooms) {
616                 for (l = chatrooms; l; l = l->next) {
617                         /* Join it */
618                 }
619                 g_list_free (chatrooms);
620                 return;
621         }
622
623         room = gtk_entry_get_text (GTK_ENTRY (dialog->entry_room));
624         if (GTK_WIDGET_VISIBLE (dialog->hbox_server)) {
625                 server = gtk_entry_get_text (GTK_ENTRY (dialog->entry_server));
626         }
627         account_chooser = GOSSIP_ACCOUNT_CHOOSER (dialog->account_chooser);
628         account = gossip_account_chooser_get_account (account_chooser);
629         mc = gossip_mission_control_new ();
630         tp_conn = mission_control_get_connection (mc, account, NULL);
631
632         if (!tp_conn) {
633                 g_object_unref (mc);
634                 return;
635         }
636
637         if (!G_STR_EMPTY (server)) {
638                 room_name = g_strconcat (room, "@", server, NULL);
639                 room_names[0] = room_name;
640         } else {
641                 room_names[0] = room;
642         }
643
644         gossip_debug (DEBUG_DOMAIN, "Requesting handle for room '%s'",
645                       room_names[0]);
646
647         /* Gives the ref of account/tp_conn to the callback */
648         tp_conn_request_handles_async (DBUS_G_PROXY (tp_conn),
649                                        TP_HANDLE_TYPE_ROOM,
650                                        room_names,
651                                        (tp_conn_request_handles_reply)
652                                        new_chatroom_dialog_request_handles_cb,
653                                        account);
654         g_free (room_name);
655         g_object_unref (mc);
656 }
657
658 static void
659 new_chatroom_dialog_request_handles_cb (DBusGProxy *proxy,
660                                         GArray     *handles,
661                                         GError     *error,
662                                         McAccount  *account)
663 {
664         MissionControl *mc;
665         guint           handle;
666
667         if (error) {
668                 gossip_debug (DEBUG_DOMAIN,
669                               "Error requesting room handle: %s",
670                               error ? error->message : "No error given");
671                 goto OUT;
672         }
673
674         mc = gossip_mission_control_new ();
675         handle = g_array_index (handles, guint, 0);
676
677         gossip_debug (DEBUG_DOMAIN, "Got handle %d, requesting channel", handle);
678         mission_control_request_channel (mc,
679                                          account,
680                                          TP_IFACE_CHANNEL_TYPE_TEXT,
681                                          handle,
682                                          TP_HANDLE_TYPE_ROOM,
683                                          NULL, NULL);   
684         g_object_unref (mc);
685
686 OUT:
687         g_object_unref (account);
688         g_object_unref (proxy);
689 }
690
691 static void
692 new_chatroom_dialog_entry_changed_cb (GtkWidget               *entry,
693                                       GossipNewChatroomDialog *dialog)
694 {
695         if (entry == dialog->entry_room) {
696                 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (dialog->filter));
697         } 
698
699         new_chatroom_dialog_update_buttons (dialog);
700 }
701
702 static void
703 new_chatroom_dialog_browse_start (GossipNewChatroomDialog *dialog)
704 {
705         if (0) {
706                 new_chatroom_dialog_model_clear (dialog);
707                 new_chatroom_dialog_model_add (dialog, NULL);
708         }
709 }
710
711 static void
712 new_chatroom_dialog_browse_stop (GossipNewChatroomDialog *dialog)
713 {
714 }
715
716 static void
717 new_chatroom_dialog_entry_server_activate_cb (GtkWidget                *widget,
718                                               GossipNewChatroomDialog  *dialog)
719 {
720         new_chatroom_dialog_togglebutton_refresh_toggled_cb (dialog->togglebutton_refresh, 
721                                                              dialog);
722 }
723
724 static void
725 new_chatroom_dialog_togglebutton_refresh_toggled_cb (GtkWidget               *widget,
726                                                      GossipNewChatroomDialog *dialog)
727 {
728         gboolean toggled;
729
730         toggled = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget));
731         
732         if (toggled) {
733                 new_chatroom_dialog_browse_start (dialog);
734         } else {
735                 new_chatroom_dialog_browse_stop (dialog);
736         }
737 }
738