EmpathyChat: inherit from a GtkBox instead of a GtkBin
[empathy-mirror.git] / src / empathy-new-chatroom-dialog.c
bloba09a5f7106d4939e1fb6ab7f5f45d10f076ad841
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-2008 Collabora Ltd.
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.
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.
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., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301 USA
21 * Authors: Martyn Russell <martyn@imendio.com>
22 * Xavier Claessens <xclaesse@gmail.com>
25 #include <config.h>
27 #include <string.h>
29 #include <gtk/gtk.h>
30 #include <glib.h>
31 #include <glib/gi18n.h>
32 #include <glib/gprintf.h>
34 #include <telepathy-glib/interfaces.h>
36 #include <libempathy/empathy-tp-roomlist.h>
37 #include <libempathy/empathy-chatroom.h>
38 #include <libempathy/empathy-utils.h>
39 #include <libempathy/empathy-request-util.h>
41 #include <libempathy-gtk/empathy-account-chooser.h>
42 #include <libempathy-gtk/empathy-ui-utils.h>
44 #include "empathy-new-chatroom-dialog.h"
46 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
47 #include <libempathy/empathy-debug.h>
49 typedef struct {
50 EmpathyTpRoomlist *room_list;
51 /* Currently selected account */
52 TpAccount *account;
53 /* Signal id of the "status-changed" signal connected on the currently
54 * selected account */
55 gulong status_changed_id;
57 GtkWidget *window;
58 GtkWidget *vbox_widgets;
59 GtkWidget *table_info;
60 GtkWidget *label_account;
61 GtkWidget *account_chooser;
62 GtkWidget *label_server;
63 GtkWidget *entry_server;
64 GtkWidget *label_room;
65 GtkWidget *entry_room;
66 GtkWidget *expander_browse;
67 GtkWidget *hbox_expander;
68 GtkWidget *throbber;
69 GtkWidget *treeview;
70 GtkTreeModel *model;
71 GtkWidget *button_join;
72 GtkWidget *label_error_message;
73 GtkWidget *viewport_error;
74 } EmpathyNewChatroomDialog;
76 enum {
77 COL_NEED_PASSWORD,
78 COL_INVITE_ONLY,
79 COL_NAME,
80 COL_ROOM,
81 COL_MEMBERS,
82 COL_MEMBERS_INT,
83 COL_TOOLTIP,
84 COL_COUNT
87 static void new_chatroom_dialog_response_cb (GtkWidget *widget,
88 gint response,
89 EmpathyNewChatroomDialog *dialog);
90 static void new_chatroom_dialog_destroy_cb (GtkWidget *widget,
91 EmpathyNewChatroomDialog *dialog);
92 static void new_chatroom_dialog_model_setup (EmpathyNewChatroomDialog *dialog);
93 static void new_chatroom_dialog_model_add_columns (EmpathyNewChatroomDialog *dialog);
94 static void new_chatroom_dialog_update_widgets (EmpathyNewChatroomDialog *dialog);
95 static void new_chatroom_dialog_account_changed_cb (GtkComboBox *combobox,
96 EmpathyNewChatroomDialog *dialog);
97 static void new_chatroom_dialog_roomlist_destroy_cb (EmpathyTpRoomlist *room_list,
98 EmpathyNewChatroomDialog *dialog);
99 static void new_chatroom_dialog_new_room_cb (EmpathyTpRoomlist *room_list,
100 EmpathyChatroom *chatroom,
101 EmpathyNewChatroomDialog *dialog);
102 static void new_chatroom_dialog_listing_cb (EmpathyTpRoomlist *room_list,
103 gpointer unused,
104 EmpathyNewChatroomDialog *dialog);
105 static void start_listing_error_cb (EmpathyTpRoomlist *room_list,
106 GError *error,
107 EmpathyNewChatroomDialog *dialog);
108 static void stop_listing_error_cb (EmpathyTpRoomlist *room_list,
109 GError *error,
110 EmpathyNewChatroomDialog *dialog);
111 static void new_chatroom_dialog_model_clear (EmpathyNewChatroomDialog *dialog);
112 static void new_chatroom_dialog_model_row_activated_cb (GtkTreeView *tree_view,
113 GtkTreePath *path,
114 GtkTreeViewColumn *column,
115 EmpathyNewChatroomDialog *dialog);
116 static void new_chatroom_dialog_model_selection_changed (GtkTreeSelection *selection,
117 EmpathyNewChatroomDialog *dialog);
118 static void new_chatroom_dialog_join (EmpathyNewChatroomDialog *dialog);
119 static void new_chatroom_dialog_entry_changed_cb (GtkWidget *entry,
120 EmpathyNewChatroomDialog *dialog);
121 static void new_chatroom_dialog_browse_start (EmpathyNewChatroomDialog *dialog);
122 static void new_chatroom_dialog_browse_stop (EmpathyNewChatroomDialog *dialog);
123 static void new_chatroom_dialog_entry_server_activate_cb (GtkWidget *widget,
124 EmpathyNewChatroomDialog *dialog);
125 static void new_chatroom_dialog_expander_browse_activate_cb (GtkWidget *widget,
126 EmpathyNewChatroomDialog *dialog);
127 static gboolean new_chatroom_dialog_entry_server_focus_out_cb (GtkWidget *widget,
128 GdkEventFocus *event,
129 EmpathyNewChatroomDialog *dialog);
130 static void new_chatroom_dialog_button_close_error_clicked_cb (GtkButton *button,
131 EmpathyNewChatroomDialog *dialog);
133 static EmpathyNewChatroomDialog *dialog_p = NULL;
136 void
137 empathy_new_chatroom_dialog_show (GtkWindow *parent)
139 EmpathyNewChatroomDialog *dialog;
140 GtkBuilder *gui;
141 GtkSizeGroup *size_group;
142 gchar *filename;
144 if (dialog_p) {
145 gtk_window_present (GTK_WINDOW (dialog_p->window));
146 return;
149 dialog_p = dialog = g_new0 (EmpathyNewChatroomDialog, 1);
151 filename = empathy_file_lookup ("empathy-new-chatroom-dialog.ui", "src");
152 gui = empathy_builder_get_file (filename,
153 "new_chatroom_dialog", &dialog->window,
154 "table_info", &dialog->table_info,
155 "label_account", &dialog->label_account,
156 "label_server", &dialog->label_server,
157 "label_room", &dialog->label_room,
158 "entry_server", &dialog->entry_server,
159 "entry_room", &dialog->entry_room,
160 "treeview", &dialog->treeview,
161 "button_join", &dialog->button_join,
162 "expander_browse", &dialog->expander_browse,
163 "hbox_expander", &dialog->hbox_expander,
164 "label_error_message", &dialog->label_error_message,
165 "viewport_error", &dialog->viewport_error,
166 NULL);
167 g_free (filename);
169 empathy_builder_connect (gui, dialog,
170 "new_chatroom_dialog", "response", new_chatroom_dialog_response_cb,
171 "new_chatroom_dialog", "destroy", new_chatroom_dialog_destroy_cb,
172 "entry_server", "changed", new_chatroom_dialog_entry_changed_cb,
173 "entry_server", "activate", new_chatroom_dialog_entry_server_activate_cb,
174 "entry_server", "focus-out-event", new_chatroom_dialog_entry_server_focus_out_cb,
175 "entry_room", "changed", new_chatroom_dialog_entry_changed_cb,
176 "expander_browse", "activate", new_chatroom_dialog_expander_browse_activate_cb,
177 "button_close_error", "clicked", new_chatroom_dialog_button_close_error_clicked_cb,
178 NULL);
180 g_object_unref (gui);
182 g_object_add_weak_pointer (G_OBJECT (dialog->window), (gpointer) &dialog_p);
184 /* Label alignment */
185 size_group = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
187 gtk_size_group_add_widget (size_group, dialog->label_account);
188 gtk_size_group_add_widget (size_group, dialog->label_server);
189 gtk_size_group_add_widget (size_group, dialog->label_room);
191 g_object_unref (size_group);
193 /* Set up chatrooms treeview */
194 new_chatroom_dialog_model_setup (dialog);
196 /* Add throbber */
197 dialog->throbber = gtk_spinner_new ();
198 gtk_box_pack_start (GTK_BOX (dialog->hbox_expander), dialog->throbber,
199 TRUE, TRUE, 0);
201 /* Account chooser for custom */
202 dialog->account_chooser = empathy_account_chooser_new ();
203 empathy_account_chooser_set_filter (EMPATHY_ACCOUNT_CHOOSER (dialog->account_chooser),
204 empathy_account_chooser_filter_supports_chatrooms,
205 NULL);
206 gtk_table_attach_defaults (GTK_TABLE (dialog->table_info),
207 dialog->account_chooser,
208 1, 2, 0, 1);
209 gtk_widget_show (dialog->account_chooser);
211 g_signal_connect (GTK_COMBO_BOX (dialog->account_chooser), "changed",
212 G_CALLBACK (new_chatroom_dialog_account_changed_cb),
213 dialog);
214 new_chatroom_dialog_account_changed_cb (GTK_COMBO_BOX (dialog->account_chooser),
215 dialog);
217 if (parent) {
218 gtk_window_set_transient_for (GTK_WINDOW (dialog->window),
219 GTK_WINDOW (parent));
222 gtk_widget_show (dialog->window);
225 static void
226 new_chatroom_dialog_response_cb (GtkWidget *widget,
227 gint response,
228 EmpathyNewChatroomDialog *dialog)
230 if (response == GTK_RESPONSE_OK) {
231 new_chatroom_dialog_join (dialog);
234 gtk_widget_destroy (widget);
237 static void
238 new_chatroom_dialog_destroy_cb (GtkWidget *widget,
239 EmpathyNewChatroomDialog *dialog)
241 if (dialog->room_list) {
242 g_object_unref (dialog->room_list);
244 g_object_unref (dialog->model);
246 if (dialog->account != NULL) {
247 g_signal_handler_disconnect (dialog->account, dialog->status_changed_id);
248 g_object_unref (dialog->account);
251 g_free (dialog);
254 static void
255 new_chatroom_dialog_model_setup (EmpathyNewChatroomDialog *dialog)
257 GtkTreeView *view;
258 GtkListStore *store;
259 GtkTreeSelection *selection;
261 /* View */
262 view = GTK_TREE_VIEW (dialog->treeview);
264 g_signal_connect (view, "row-activated",
265 G_CALLBACK (new_chatroom_dialog_model_row_activated_cb),
266 dialog);
268 /* Store/Model */
269 store = gtk_list_store_new (COL_COUNT,
270 G_TYPE_STRING, /* Invite */
271 G_TYPE_STRING, /* Password */
272 G_TYPE_STRING, /* Name */
273 G_TYPE_STRING, /* Room */
274 G_TYPE_STRING, /* Member count */
275 G_TYPE_INT, /* Member count int */
276 G_TYPE_STRING); /* Tool tip */
278 dialog->model = GTK_TREE_MODEL (store);
279 gtk_tree_view_set_model (view, dialog->model);
280 gtk_tree_view_set_tooltip_column (view, COL_TOOLTIP);
281 gtk_tree_view_set_search_column (view, COL_NAME);
283 /* Selection */
284 selection = gtk_tree_view_get_selection (view);
285 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
286 COL_NAME, GTK_SORT_ASCENDING);
288 g_signal_connect (selection, "changed",
289 G_CALLBACK (new_chatroom_dialog_model_selection_changed),
290 dialog);
292 /* Columns */
293 new_chatroom_dialog_model_add_columns (dialog);
296 static void
297 new_chatroom_dialog_model_add_columns (EmpathyNewChatroomDialog *dialog)
299 GtkTreeView *view;
300 GtkTreeViewColumn *column;
301 GtkCellRenderer *cell;
302 gint width, height;
304 gtk_icon_size_lookup (GTK_ICON_SIZE_MENU, &width, &height);
306 view = GTK_TREE_VIEW (dialog->treeview);
308 cell = gtk_cell_renderer_pixbuf_new ();
309 g_object_set (cell,
310 "width", width,
311 "height", height,
312 "stock-size", GTK_ICON_SIZE_MENU,
313 NULL);
314 column = gtk_tree_view_column_new_with_attributes (NULL,
315 cell,
316 "stock-id", COL_INVITE_ONLY,
317 NULL);
319 gtk_tree_view_column_set_sort_column_id (column, COL_INVITE_ONLY);
320 gtk_tree_view_append_column (view, column);
322 column = gtk_tree_view_column_new_with_attributes (NULL,
323 cell,
324 "stock-id", COL_NEED_PASSWORD,
325 NULL);
327 gtk_tree_view_column_set_sort_column_id (column, COL_NEED_PASSWORD);
328 gtk_tree_view_append_column (view, column);
330 cell = gtk_cell_renderer_text_new ();
331 g_object_set (cell,
332 "xpad", (guint) 4,
333 "ypad", (guint) 2,
334 "ellipsize", PANGO_ELLIPSIZE_END,
335 NULL);
337 column = gtk_tree_view_column_new_with_attributes (_("Chat Room"),
338 cell,
339 "text", COL_NAME,
340 NULL);
342 gtk_tree_view_column_set_sort_column_id (column, COL_NAME);
343 gtk_tree_view_column_set_expand (column, TRUE);
344 gtk_tree_view_append_column (view, column);
346 cell = gtk_cell_renderer_text_new ();
347 g_object_set (cell,
348 "xpad", (guint) 4,
349 "ypad", (guint) 2,
350 "ellipsize", PANGO_ELLIPSIZE_END,
351 "alignment", PANGO_ALIGN_RIGHT,
352 NULL);
353 column = gtk_tree_view_column_new_with_attributes (_("Members"),
354 cell,
355 "text", COL_MEMBERS,
356 NULL);
358 gtk_tree_view_column_set_sort_column_id (column, COL_MEMBERS_INT);
359 gtk_tree_view_append_column (view, column);
362 static void
363 update_join_button_sensitivity (EmpathyNewChatroomDialog *dialog)
365 const gchar *room;
366 gboolean sensitive = FALSE;
369 room = gtk_entry_get_text (GTK_ENTRY (dialog->entry_room));
370 if (EMP_STR_EMPTY (room))
371 goto out;
373 if (dialog->account == NULL)
374 goto out;
376 if (tp_account_get_connection_status (dialog->account, NULL) !=
377 TP_CONNECTION_STATUS_CONNECTED)
378 goto out;
380 sensitive = TRUE;
382 out:
383 gtk_widget_set_sensitive (dialog->button_join, sensitive);
386 static void
387 new_chatroom_dialog_update_widgets (EmpathyNewChatroomDialog *dialog)
389 const gchar *protocol;
391 if (dialog->account == NULL)
392 return;
394 protocol = tp_account_get_protocol (dialog->account);
396 gtk_entry_set_text (GTK_ENTRY (dialog->entry_server), "");
398 /* hardcode here known protocols */
399 if (strcmp (protocol, "jabber") == 0) {
400 gtk_widget_set_sensitive (dialog->entry_server, TRUE);
402 else if (strcmp (protocol, "local-xmpp") == 0) {
403 gtk_widget_set_sensitive (dialog->entry_server, FALSE);
405 else if (strcmp (protocol, "irc") == 0) {
406 gtk_widget_set_sensitive (dialog->entry_server, FALSE);
408 else {
409 gtk_widget_set_sensitive (dialog->entry_server, TRUE);
412 update_join_button_sensitivity (dialog);
414 /* Final set up of the dialog */
415 gtk_widget_grab_focus (dialog->entry_room);
418 static void
419 account_status_changed_cb (TpAccount *account,
420 guint old_status,
421 guint new_status,
422 guint reason,
423 gchar *dbus_error_name,
424 GHashTable *details,
425 EmpathyNewChatroomDialog *self)
427 update_join_button_sensitivity (self);
430 static void
431 new_chatroom_dialog_account_changed_cb (GtkComboBox *combobox,
432 EmpathyNewChatroomDialog *dialog)
434 EmpathyAccountChooser *account_chooser;
435 gboolean listing = FALSE;
436 gboolean expanded = FALSE;
437 TpConnection *connection;
438 TpCapabilities *caps;
440 if (dialog->room_list) {
441 g_object_unref (dialog->room_list);
442 dialog->room_list = NULL;
445 gtk_spinner_stop (GTK_SPINNER (dialog->throbber));
446 gtk_widget_hide (dialog->throbber);
447 new_chatroom_dialog_model_clear (dialog);
449 if (dialog->account != NULL) {
450 g_signal_handler_disconnect (dialog->account, dialog->status_changed_id);
451 g_object_unref (dialog->account);
454 account_chooser = EMPATHY_ACCOUNT_CHOOSER (dialog->account_chooser);
455 dialog->account = empathy_account_chooser_dup_account (account_chooser);
456 connection = empathy_account_chooser_get_connection (account_chooser);
457 if (dialog->account == NULL)
458 goto out;
460 dialog->status_changed_id = g_signal_connect (dialog->account,
461 "status-changed", G_CALLBACK (account_status_changed_cb), dialog);
463 /* empathy_account_chooser_filter_supports_chatrooms ensures that the
464 * account has a connection and CAPABILITIES has been prepared. */
465 g_assert (connection != NULL);
466 g_assert (tp_proxy_is_prepared (connection,
467 TP_CONNECTION_FEATURE_CAPABILITIES));
468 caps = tp_connection_get_capabilities (connection);
470 if (tp_capabilities_supports_room_list (caps, NULL)) {
471 /* Roomlist channels are supported */
472 dialog->room_list = empathy_tp_roomlist_new (dialog->account);
474 else {
475 dialog->room_list = NULL;
478 if (dialog->room_list) {
479 g_signal_connect (dialog->room_list, "destroy",
480 G_CALLBACK (new_chatroom_dialog_roomlist_destroy_cb),
481 dialog);
482 g_signal_connect (dialog->room_list, "new-room",
483 G_CALLBACK (new_chatroom_dialog_new_room_cb),
484 dialog);
485 g_signal_connect (dialog->room_list, "notify::is-listing",
486 G_CALLBACK (new_chatroom_dialog_listing_cb),
487 dialog);
488 g_signal_connect (dialog->room_list, "error::start",
489 G_CALLBACK (start_listing_error_cb),
490 dialog);
491 g_signal_connect (dialog->room_list, "error::stop",
492 G_CALLBACK (stop_listing_error_cb),
493 dialog);
495 expanded = gtk_expander_get_expanded (GTK_EXPANDER (dialog->expander_browse));
496 if (expanded) {
497 gtk_widget_hide (dialog->viewport_error);
498 gtk_widget_set_sensitive (dialog->treeview, TRUE);
499 new_chatroom_dialog_browse_start (dialog);
502 listing = empathy_tp_roomlist_is_listing (dialog->room_list);
503 if (listing) {
504 gtk_spinner_start (GTK_SPINNER (dialog->throbber));
505 gtk_widget_show (dialog->throbber);
509 gtk_widget_set_sensitive (dialog->expander_browse, dialog->room_list != NULL);
511 out:
512 new_chatroom_dialog_update_widgets (dialog);
515 static void
516 new_chatroom_dialog_button_close_error_clicked_cb (GtkButton *button,
517 EmpathyNewChatroomDialog *dialog)
519 gtk_widget_hide (dialog->viewport_error);
522 static void
523 new_chatroom_dialog_roomlist_destroy_cb (EmpathyTpRoomlist *room_list,
524 EmpathyNewChatroomDialog *dialog)
526 g_object_unref (dialog->room_list);
527 dialog->room_list = NULL;
530 static void
531 new_chatroom_dialog_new_room_cb (EmpathyTpRoomlist *room_list,
532 EmpathyChatroom *chatroom,
533 EmpathyNewChatroomDialog *dialog)
535 GtkListStore *store;
536 GtkTreeIter iter;
537 gchar *members;
538 gchar *tooltip;
539 const gchar *need_password;
540 const gchar *invite_only;
541 gchar *tmp;
543 DEBUG ("New chatroom listed: %s (%s)",
544 empathy_chatroom_get_name (chatroom),
545 empathy_chatroom_get_room (chatroom));
547 /* Add to model */
548 store = GTK_LIST_STORE (dialog->model);
549 members = g_strdup_printf ("%d", empathy_chatroom_get_members_count (chatroom));
550 tmp = g_strdup_printf ("<b>%s</b>", empathy_chatroom_get_name (chatroom));
551 /* Translators: Room/Join's roomlist tooltip. Parameters are a channel name,
552 yes/no, yes/no and a number. */
553 tooltip = g_strdup_printf (_("%s\nInvite required: %s\nPassword required: %s\nMembers: %s"),
554 tmp,
555 empathy_chatroom_get_invite_only (chatroom) ? _("Yes") : _("No"),
556 empathy_chatroom_get_need_password (chatroom) ? _("Yes") : _("No"),
557 members);
558 g_free (tmp);
559 invite_only = (empathy_chatroom_get_invite_only (chatroom) ?
560 GTK_STOCK_INDEX : NULL);
561 need_password = (empathy_chatroom_get_need_password (chatroom) ?
562 GTK_STOCK_DIALOG_AUTHENTICATION : NULL);
564 gtk_list_store_append (store, &iter);
565 gtk_list_store_set (store, &iter,
566 COL_NEED_PASSWORD, need_password,
567 COL_INVITE_ONLY, invite_only,
568 COL_NAME, empathy_chatroom_get_name (chatroom),
569 COL_ROOM, empathy_chatroom_get_room (chatroom),
570 COL_MEMBERS, members,
571 COL_MEMBERS_INT, empathy_chatroom_get_members_count (chatroom),
572 COL_TOOLTIP, tooltip,
573 -1);
575 g_free (members);
576 g_free (tooltip);
579 static void
580 start_listing_error_cb (EmpathyTpRoomlist *room_list,
581 GError *error,
582 EmpathyNewChatroomDialog *dialog)
584 gtk_label_set_text (GTK_LABEL (dialog->label_error_message), _("Could not start room listing"));
585 gtk_widget_show_all (dialog->viewport_error);
586 gtk_widget_set_sensitive (dialog->treeview, FALSE);
589 static void
590 stop_listing_error_cb (EmpathyTpRoomlist *room_list,
591 GError *error,
592 EmpathyNewChatroomDialog *dialog)
594 gtk_label_set_text (GTK_LABEL (dialog->label_error_message), _("Could not stop room listing"));
595 gtk_widget_show_all (dialog->viewport_error);
596 gtk_widget_set_sensitive (dialog->treeview, FALSE);
599 static void
600 new_chatroom_dialog_listing_cb (EmpathyTpRoomlist *room_list,
601 gpointer unused,
602 EmpathyNewChatroomDialog *dialog)
604 gboolean listing;
606 listing = empathy_tp_roomlist_is_listing (room_list);
608 /* Update the throbber */
609 if (listing) {
610 gtk_spinner_start (GTK_SPINNER (dialog->throbber));
611 gtk_widget_show (dialog->throbber);
612 } else {
613 gtk_spinner_stop (GTK_SPINNER (dialog->throbber));
614 gtk_widget_hide (dialog->throbber);
618 static void
619 new_chatroom_dialog_model_clear (EmpathyNewChatroomDialog *dialog)
621 GtkListStore *store;
623 store = GTK_LIST_STORE (dialog->model);
624 gtk_list_store_clear (store);
627 static void
628 new_chatroom_dialog_model_row_activated_cb (GtkTreeView *tree_view,
629 GtkTreePath *path,
630 GtkTreeViewColumn *column,
631 EmpathyNewChatroomDialog *dialog)
633 gtk_widget_activate (dialog->button_join);
636 static void
637 new_chatroom_dialog_model_selection_changed (GtkTreeSelection *selection,
638 EmpathyNewChatroomDialog *dialog)
640 GtkTreeModel *model;
641 GtkTreeIter iter;
642 gchar *room = NULL;
643 gchar *server = NULL;
645 if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
646 return;
649 gtk_tree_model_get (model, &iter, COL_ROOM, &room, -1);
650 server = strstr (room, "@");
651 if (server) {
652 *server = '\0';
653 server++;
656 gtk_entry_set_text (GTK_ENTRY (dialog->entry_server), server ? server : "");
657 gtk_entry_set_text (GTK_ENTRY (dialog->entry_room), room ? room : "");
659 g_free (room);
662 static void
663 new_chatroom_dialog_join (EmpathyNewChatroomDialog *dialog)
665 EmpathyAccountChooser *account_chooser;
666 TpAccount *account;
667 const gchar *room;
668 const gchar *server = NULL;
669 gchar *room_name = NULL;
671 room = gtk_entry_get_text (GTK_ENTRY (dialog->entry_room));
672 server = gtk_entry_get_text (GTK_ENTRY (dialog->entry_server));
674 account_chooser = EMPATHY_ACCOUNT_CHOOSER (dialog->account_chooser);
675 account = empathy_account_chooser_get_account (account_chooser);
677 if (!EMP_STR_EMPTY (server)) {
678 room_name = g_strconcat (room, "@", server, NULL);
679 } else {
680 room_name = g_strdup (room);
683 g_strstrip (room_name);
685 DEBUG ("Requesting channel for '%s'", room_name);
687 empathy_join_muc (account, room_name, empathy_get_current_action_time ());
689 g_free (room_name);
692 static void
693 new_chatroom_dialog_entry_changed_cb (GtkWidget *entry,
694 EmpathyNewChatroomDialog *dialog)
696 if (entry == dialog->entry_room) {
697 update_join_button_sensitivity (dialog);
699 /* FIXME: Select the room in the list */
703 static void
704 new_chatroom_dialog_browse_start (EmpathyNewChatroomDialog *dialog)
706 new_chatroom_dialog_model_clear (dialog);
707 if (dialog->room_list) {
708 empathy_tp_roomlist_start (dialog->room_list);
712 static void
713 new_chatroom_dialog_browse_stop (EmpathyNewChatroomDialog *dialog)
715 if (dialog->room_list) {
716 empathy_tp_roomlist_stop (dialog->room_list);
720 static void
721 new_chatroom_dialog_entry_server_activate_cb (GtkWidget *widget,
722 EmpathyNewChatroomDialog *dialog)
724 new_chatroom_dialog_browse_start (dialog);
727 static void
728 new_chatroom_dialog_expander_browse_activate_cb (GtkWidget *widget,
729 EmpathyNewChatroomDialog *dialog)
731 gboolean expanded;
733 expanded = gtk_expander_get_expanded (GTK_EXPANDER (widget));
734 if (expanded) {
735 new_chatroom_dialog_browse_stop (dialog);
736 gtk_window_set_resizable (GTK_WINDOW (dialog->window), FALSE);
737 } else {
738 gtk_widget_hide (dialog->viewport_error);
739 gtk_widget_set_sensitive (dialog->treeview, TRUE);
740 new_chatroom_dialog_browse_start (dialog);
741 gtk_window_set_resizable (GTK_WINDOW (dialog->window), TRUE);
745 static gboolean
746 new_chatroom_dialog_entry_server_focus_out_cb (GtkWidget *widget,
747 GdkEventFocus *event,
748 EmpathyNewChatroomDialog *dialog)
750 gboolean expanded;
752 expanded = gtk_expander_get_expanded (GTK_EXPANDER (dialog->expander_browse));
753 if (expanded) {
754 new_chatroom_dialog_browse_start (dialog);
756 return FALSE;