]> git.0d.be Git - empathy.git/blob - src/empathy-new-chatroom-dialog.c
Merge branch 'part-reasons'
[empathy.git] / src / empathy-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-2008 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., 51 Franklin St, Fifth Floor,
19  * Boston, MA  02110-1301  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 <glib.h>
32 #include <glib/gi18n.h>
33 #include <glib/gprintf.h>
34
35 #include <libmissioncontrol/mission-control.h>
36 #include <libmissioncontrol/mc-profile.h>
37
38 #include <libempathy/empathy-tp-roomlist.h>
39 #include <libempathy/empathy-chatroom.h>
40 #include <libempathy/empathy-utils.h>
41 #include <libempathy/empathy-dispatcher.h>
42
43 #include <libempathy-gtk/empathy-account-chooser.h>
44 #include <libempathy-gtk/empathy-ui-utils.h>
45
46 #include "empathy-new-chatroom-dialog.h"
47 #include "ephy-spinner.h"
48
49 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
50 #include <libempathy/empathy-debug.h>
51
52 typedef struct {
53         EmpathyTpRoomlist *room_list;
54
55         GtkWidget         *window;
56         GtkWidget         *vbox_widgets;
57         GtkWidget         *table_info;
58         GtkWidget         *label_account;
59         GtkWidget         *account_chooser;
60         GtkWidget         *label_server;
61         GtkWidget         *entry_server;
62         GtkWidget         *label_room;
63         GtkWidget         *entry_room;
64         GtkWidget         *expander_browse;
65         GtkWidget         *throbber;
66         GtkWidget         *treeview;
67         GtkTreeModel      *model;
68         GtkWidget         *button_join;
69         GtkWidget         *label_error_message;
70         GtkWidget         *viewport_error;
71 } EmpathyNewChatroomDialog;
72
73 enum {
74         COL_NEED_PASSWORD,
75         COL_INVITE_ONLY,
76         COL_NAME,
77         COL_ROOM,
78         COL_MEMBERS,
79         COL_MEMBERS_INT,
80         COL_TOOLTIP,
81         COL_COUNT
82 };
83
84 static void     new_chatroom_dialog_response_cb                     (GtkWidget               *widget,
85                                                                      gint                     response,
86                                                                      EmpathyNewChatroomDialog *dialog);
87 static void     new_chatroom_dialog_destroy_cb                      (GtkWidget               *widget,
88                                                                      EmpathyNewChatroomDialog *dialog);
89 static void     new_chatroom_dialog_model_setup                     (EmpathyNewChatroomDialog *dialog);
90 static void     new_chatroom_dialog_model_add_columns               (EmpathyNewChatroomDialog *dialog);
91 static void     new_chatroom_dialog_update_widgets                  (EmpathyNewChatroomDialog *dialog);
92 static void     new_chatroom_dialog_account_changed_cb              (GtkComboBox              *combobox,
93                                                                      EmpathyNewChatroomDialog *dialog);
94 static void     new_chatroom_dialog_roomlist_destroy_cb             (EmpathyTpRoomlist        *room_list,
95                                                                      EmpathyNewChatroomDialog *dialog);
96 static void     new_chatroom_dialog_new_room_cb                     (EmpathyTpRoomlist        *room_list,
97                                                                      EmpathyChatroom          *chatroom,
98                                                                      EmpathyNewChatroomDialog *dialog);
99 static void     new_chatroom_dialog_listing_cb                      (EmpathyTpRoomlist        *room_list,
100                                                                      gpointer                  unused,
101                                                                      EmpathyNewChatroomDialog *dialog);
102 static void     start_listing_error_cb                              (EmpathyTpRoomlist        *room_list,
103                                                                      GError                   *error,
104                                                                      EmpathyNewChatroomDialog *dialog);
105 static void     stop_listing_error_cb                               (EmpathyTpRoomlist        *room_list,
106                                                                      GError                   *error,
107                                                                      EmpathyNewChatroomDialog *dialog);
108 static void     new_chatroom_dialog_model_clear                     (EmpathyNewChatroomDialog *dialog);
109 static void     new_chatroom_dialog_model_row_activated_cb          (GtkTreeView             *tree_view,
110                                                                      GtkTreePath             *path,
111                                                                      GtkTreeViewColumn       *column,
112                                                                      EmpathyNewChatroomDialog *dialog);
113 static void     new_chatroom_dialog_model_selection_changed         (GtkTreeSelection        *selection,
114                                                                      EmpathyNewChatroomDialog *dialog);
115 static void     new_chatroom_dialog_join                            (EmpathyNewChatroomDialog *dialog);
116 static void     new_chatroom_dialog_entry_changed_cb                (GtkWidget               *entry,
117                                                                      EmpathyNewChatroomDialog *dialog);
118 static void     new_chatroom_dialog_browse_start                    (EmpathyNewChatroomDialog *dialog);
119 static void     new_chatroom_dialog_browse_stop                     (EmpathyNewChatroomDialog *dialog);
120 static void     new_chatroom_dialog_entry_server_activate_cb        (GtkWidget               *widget,
121                                                                      EmpathyNewChatroomDialog *dialog);
122 static void     new_chatroom_dialog_expander_browse_activate_cb     (GtkWidget               *widget,
123                                                                      EmpathyNewChatroomDialog *dialog);
124 static gboolean new_chatroom_dialog_entry_server_focus_out_cb       (GtkWidget               *widget,
125                                                                      GdkEventFocus           *event,
126                                                                      EmpathyNewChatroomDialog *dialog);
127 static void     new_chatroom_dialog_button_close_error_clicked_cb   (GtkButton                *button,
128                                                                      EmpathyNewChatroomDialog *dialog);
129
130 static EmpathyNewChatroomDialog *dialog_p = NULL;
131
132 void
133 empathy_new_chatroom_dialog_show (GtkWindow *parent)
134 {
135         EmpathyNewChatroomDialog *dialog;
136         GtkBuilder               *gui;
137         GtkSizeGroup             *size_group;
138         gchar                    *filename;
139
140         if (dialog_p) {
141                 gtk_window_present (GTK_WINDOW (dialog_p->window));
142                 return;
143         }
144
145         dialog_p = dialog = g_new0 (EmpathyNewChatroomDialog, 1);
146
147         filename = empathy_file_lookup ("empathy-new-chatroom-dialog.ui", "src");
148         gui = empathy_builder_get_file (filename,
149                                        "new_chatroom_dialog", &dialog->window,
150                                        "table_info", &dialog->table_info,
151                                        "label_account", &dialog->label_account,
152                                        "label_server", &dialog->label_server,
153                                        "label_room", &dialog->label_room,
154                                        "entry_server", &dialog->entry_server,
155                                        "entry_room", &dialog->entry_room,
156                                        "treeview", &dialog->treeview,
157                                        "button_join", &dialog->button_join,
158                                        "expander_browse", &dialog->expander_browse,
159                                        "label_error_message", &dialog->label_error_message,
160                                        "viewport_error", &dialog->viewport_error,
161                                        NULL);
162         g_free (filename);
163
164         empathy_builder_connect (gui, dialog,
165                               "new_chatroom_dialog", "response", new_chatroom_dialog_response_cb,
166                               "new_chatroom_dialog", "destroy", new_chatroom_dialog_destroy_cb,
167                               "entry_server", "changed", new_chatroom_dialog_entry_changed_cb,
168                               "entry_server", "activate", new_chatroom_dialog_entry_server_activate_cb,
169                               "entry_server", "focus-out-event", new_chatroom_dialog_entry_server_focus_out_cb,
170                               "entry_room", "changed", new_chatroom_dialog_entry_changed_cb,
171                               "expander_browse", "activate", new_chatroom_dialog_expander_browse_activate_cb,
172                               "button_close_error", "clicked", new_chatroom_dialog_button_close_error_clicked_cb,
173                               NULL);
174
175         g_object_unref (gui);
176
177         g_object_add_weak_pointer (G_OBJECT (dialog->window), (gpointer) &dialog_p);
178
179         /* Label alignment */
180         size_group = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
181
182         gtk_size_group_add_widget (size_group, dialog->label_account);
183         gtk_size_group_add_widget (size_group, dialog->label_server);
184         gtk_size_group_add_widget (size_group, dialog->label_room);
185
186         g_object_unref (size_group);
187
188         /* Set up chatrooms treeview */
189         new_chatroom_dialog_model_setup (dialog);
190
191         /* Add throbber */
192         dialog->throbber = ephy_spinner_new ();
193         ephy_spinner_set_size (EPHY_SPINNER (dialog->throbber), GTK_ICON_SIZE_LARGE_TOOLBAR);
194         gtk_widget_show (dialog->throbber);
195         gtk_table_attach (GTK_TABLE (dialog->table_info),
196                           dialog->throbber,
197                           2, 3, 0, 1,
198                           0, 0, 0, 0);
199
200         /* Account chooser for custom */
201         dialog->account_chooser = empathy_account_chooser_new ();
202         empathy_account_chooser_set_filter (EMPATHY_ACCOUNT_CHOOSER (dialog->account_chooser),
203                                             empathy_account_chooser_filter_is_connected,
204                                             NULL);
205         gtk_table_attach_defaults (GTK_TABLE (dialog->table_info),
206                                    dialog->account_chooser,
207                                    1, 2, 0, 1);
208         gtk_widget_show (dialog->account_chooser);
209
210         g_signal_connect (GTK_COMBO_BOX (dialog->account_chooser), "changed",
211                           G_CALLBACK (new_chatroom_dialog_account_changed_cb),
212                           dialog);
213         new_chatroom_dialog_account_changed_cb (GTK_COMBO_BOX (dialog->account_chooser),
214                                                 dialog);
215
216         if (parent) {
217                 gtk_window_set_transient_for (GTK_WINDOW (dialog->window),
218                                               GTK_WINDOW (parent));
219         }
220
221         gtk_widget_show (dialog->window);
222 }
223
224 static void
225 new_chatroom_dialog_response_cb (GtkWidget               *widget,
226                                  gint                     response,
227                                  EmpathyNewChatroomDialog *dialog)
228 {
229         if (response == GTK_RESPONSE_OK) {
230                 new_chatroom_dialog_join (dialog);
231         }
232
233         gtk_widget_destroy (widget);
234 }
235
236 static void
237 new_chatroom_dialog_destroy_cb (GtkWidget               *widget,
238                                 EmpathyNewChatroomDialog *dialog)
239 {
240         if (dialog->room_list) {
241                 g_object_unref (dialog->room_list);
242         }
243         g_object_unref (dialog->model);
244
245         g_free (dialog);
246 }
247
248 static void
249 new_chatroom_dialog_model_setup (EmpathyNewChatroomDialog *dialog)
250 {
251         GtkTreeView      *view;
252         GtkListStore     *store;
253         GtkTreeSelection *selection;
254
255         /* View */
256         view = GTK_TREE_VIEW (dialog->treeview);
257
258         g_signal_connect (view, "row-activated",
259                           G_CALLBACK (new_chatroom_dialog_model_row_activated_cb),
260                           dialog);
261
262         /* Store/Model */
263         store = gtk_list_store_new (COL_COUNT,
264                                     G_TYPE_STRING,       /* Invite */
265                                     G_TYPE_STRING,       /* Password */
266                                     G_TYPE_STRING,       /* Name */
267                                     G_TYPE_STRING,       /* Room */
268                                     G_TYPE_STRING,       /* Member count */
269                                     G_TYPE_INT,          /* Member count int */
270                                     G_TYPE_STRING);      /* Tool tip */
271
272         dialog->model = GTK_TREE_MODEL (store);
273         gtk_tree_view_set_model (view, dialog->model);
274         gtk_tree_view_set_tooltip_column (view, COL_TOOLTIP);
275         gtk_tree_view_set_search_column (view, COL_NAME);
276
277         /* Selection */
278         selection = gtk_tree_view_get_selection (view);
279         gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
280                                               COL_NAME, GTK_SORT_ASCENDING);
281
282         g_signal_connect (selection, "changed",
283                           G_CALLBACK (new_chatroom_dialog_model_selection_changed),
284                           dialog);
285
286         /* Columns */
287         new_chatroom_dialog_model_add_columns (dialog);
288 }
289
290 static void
291 new_chatroom_dialog_model_add_columns (EmpathyNewChatroomDialog *dialog)
292 {
293         GtkTreeView       *view;
294         GtkTreeViewColumn *column;
295         GtkCellRenderer   *cell;
296         gint               width, height;
297
298         gtk_icon_size_lookup (GTK_ICON_SIZE_MENU, &width, &height);
299
300         view = GTK_TREE_VIEW (dialog->treeview);
301
302         cell = gtk_cell_renderer_pixbuf_new ();
303         g_object_set (cell,
304                       "width", width,
305                       "height", height,
306                       "stock-size", GTK_ICON_SIZE_MENU,
307                       NULL);
308         column = gtk_tree_view_column_new_with_attributes (NULL,
309                                                            cell,
310                                                            "stock-id", COL_INVITE_ONLY,
311                                                            NULL);
312
313         gtk_tree_view_column_set_sort_column_id (column, COL_INVITE_ONLY);
314         gtk_tree_view_append_column (view, column);
315
316         column = gtk_tree_view_column_new_with_attributes (NULL,
317                                                            cell,
318                                                            "stock-id", COL_NEED_PASSWORD,
319                                                            NULL);
320
321         gtk_tree_view_column_set_sort_column_id (column, COL_NEED_PASSWORD);
322         gtk_tree_view_append_column (view, column);
323
324         cell = gtk_cell_renderer_text_new ();
325         g_object_set (cell,
326                       "xpad", (guint) 4,
327                       "ypad", (guint) 2,
328                       "ellipsize", PANGO_ELLIPSIZE_END,
329                       NULL);
330
331         column = gtk_tree_view_column_new_with_attributes (_("Chat Room"),
332                                                            cell,
333                                                            "text", COL_NAME,
334                                                            NULL);
335
336         gtk_tree_view_column_set_sort_column_id (column, COL_NAME);
337         gtk_tree_view_column_set_expand (column, TRUE);
338         gtk_tree_view_append_column (view, column);
339
340         cell = gtk_cell_renderer_text_new ();
341         g_object_set (cell,
342                       "xpad", (guint) 4,
343                       "ypad", (guint) 2,
344                       "ellipsize", PANGO_ELLIPSIZE_END,
345                       "alignment", PANGO_ALIGN_RIGHT,
346                       NULL);
347         column = gtk_tree_view_column_new_with_attributes (_("Members"),
348                                                            cell,
349                                                            "text", COL_MEMBERS,
350                                                            NULL);
351
352         gtk_tree_view_column_set_sort_column_id (column, COL_MEMBERS_INT);
353         gtk_tree_view_append_column (view, column);
354 }
355
356 static void
357 new_chatroom_dialog_update_widgets (EmpathyNewChatroomDialog *dialog)
358 {
359         EmpathyAccountChooser *account_chooser;
360         EmpathyAccount        *account;
361         McProfile             *profile;
362         const gchar           *protocol;
363         const gchar           *room;
364
365         account_chooser = EMPATHY_ACCOUNT_CHOOSER (dialog->account_chooser);
366         account = empathy_account_chooser_dup_account (account_chooser);
367         profile = empathy_account_get_profile (account);
368         protocol = mc_profile_get_protocol_name (profile);
369
370         gtk_entry_set_text (GTK_ENTRY (dialog->entry_server), "");
371
372         /* hardcode here known protocols */
373         if (strcmp (protocol, "jabber") == 0) {
374                 gtk_widget_set_sensitive (dialog->entry_server, TRUE);
375         }
376         else if (strcmp (protocol, "local-xmpp") == 0) {
377                 gtk_widget_set_sensitive (dialog->entry_server, FALSE);
378         }
379         else if (strcmp (protocol, "irc") == 0) {
380                 gtk_widget_set_sensitive (dialog->entry_server, FALSE);
381         }
382         else {
383                 gtk_widget_set_sensitive (dialog->entry_server, TRUE);
384         }
385
386         room = gtk_entry_get_text (GTK_ENTRY (dialog->entry_room));
387         gtk_widget_set_sensitive (dialog->button_join, !EMP_STR_EMPTY (room));
388
389         /* Final set up of the dialog */
390         gtk_widget_grab_focus (dialog->entry_room);
391
392         g_object_unref (account);
393         g_object_unref (profile);
394 }
395
396 static void
397 new_chatroom_dialog_account_changed_cb (GtkComboBox             *combobox,
398                                         EmpathyNewChatroomDialog *dialog)
399 {
400         EmpathyAccountChooser *account_chooser;
401         EmpathyAccount        *account;
402         gboolean               listing = FALSE;
403         gboolean               expanded = FALSE;
404
405         if (dialog->room_list) {
406                 g_object_unref (dialog->room_list);
407                 dialog->room_list = NULL;
408         }
409
410         ephy_spinner_stop (EPHY_SPINNER (dialog->throbber));
411         new_chatroom_dialog_model_clear (dialog);
412
413         account_chooser = EMPATHY_ACCOUNT_CHOOSER (dialog->account_chooser);
414         account = empathy_account_chooser_dup_account (account_chooser);
415         if (account == NULL)
416                 goto out;
417
418         dialog->room_list = empathy_tp_roomlist_new (account);
419
420         if (dialog->room_list) {
421                 g_signal_connect (dialog->room_list, "destroy",
422                                   G_CALLBACK (new_chatroom_dialog_roomlist_destroy_cb),
423                                   dialog);
424                 g_signal_connect (dialog->room_list, "new-room",
425                                   G_CALLBACK (new_chatroom_dialog_new_room_cb),
426                                   dialog);
427                 g_signal_connect (dialog->room_list, "notify::is-listing",
428                                   G_CALLBACK (new_chatroom_dialog_listing_cb),
429                                   dialog);
430                 g_signal_connect (dialog->room_list, "error::start",
431                                   G_CALLBACK (start_listing_error_cb),
432                                   dialog);
433                 g_signal_connect (dialog->room_list, "error::stop",
434                                   G_CALLBACK (stop_listing_error_cb),
435                                   dialog);
436
437                 expanded = gtk_expander_get_expanded (GTK_EXPANDER (dialog->expander_browse));
438                 if (expanded) {
439                         gtk_widget_hide (dialog->viewport_error);
440                         gtk_widget_set_sensitive (dialog->treeview, TRUE);
441                         new_chatroom_dialog_browse_start (dialog);
442                 }
443
444                 listing = empathy_tp_roomlist_is_listing (dialog->room_list);
445                 if (listing) {
446                         ephy_spinner_start (EPHY_SPINNER (dialog->throbber));
447                 }
448         }
449
450         g_object_unref (account);
451
452 out:
453         new_chatroom_dialog_update_widgets (dialog);
454 }
455
456 static void
457 new_chatroom_dialog_button_close_error_clicked_cb   (GtkButton                *button,
458                                                      EmpathyNewChatroomDialog *dialog)
459 {
460         gtk_widget_hide (dialog->viewport_error);
461 }
462
463 static void
464 new_chatroom_dialog_roomlist_destroy_cb (EmpathyTpRoomlist        *room_list,
465                                          EmpathyNewChatroomDialog *dialog)
466 {
467         g_object_unref (dialog->room_list);
468         dialog->room_list = NULL;
469 }
470
471 static void
472 new_chatroom_dialog_new_room_cb (EmpathyTpRoomlist        *room_list,
473                                  EmpathyChatroom          *chatroom,
474                                  EmpathyNewChatroomDialog *dialog)
475 {
476         GtkTreeView      *view;
477         GtkTreeSelection *selection;
478         GtkListStore     *store;
479         GtkTreeIter       iter;
480         gchar            *members;
481         gchar            *tooltip;
482         const gchar      *need_password;
483         const gchar      *invite_only;
484
485         DEBUG ("New chatroom listed: %s (%s)",
486                 empathy_chatroom_get_name (chatroom),
487                 empathy_chatroom_get_room (chatroom));
488
489         /* Add to model */
490         view = GTK_TREE_VIEW (dialog->treeview);
491         selection = gtk_tree_view_get_selection (view);
492         store = GTK_LIST_STORE (dialog->model);
493         members = g_strdup_printf ("%d", empathy_chatroom_get_members_count (chatroom));
494         tooltip = g_strdup_printf (C_("Room/Join's roomlist tooltip. Parameters"
495                 "are a channel name, yes/no, yes/no and a number.",
496                 "<b>%s</b>\nInvite required: %s\nPassword required: %s\nMembers: %s"),
497                 empathy_chatroom_get_name (chatroom),
498                 empathy_chatroom_get_invite_only (chatroom) ? _("Yes") : _("No"),
499                 empathy_chatroom_get_need_password (chatroom) ? _("Yes") : _("No"),
500                 members);
501         invite_only = (empathy_chatroom_get_invite_only (chatroom) ?
502                 GTK_STOCK_INDEX : NULL);
503         need_password = (empathy_chatroom_get_need_password (chatroom) ?
504                 GTK_STOCK_DIALOG_AUTHENTICATION : NULL);
505
506         gtk_list_store_append (store, &iter);
507         gtk_list_store_set (store, &iter,
508                             COL_NEED_PASSWORD, need_password,
509                             COL_INVITE_ONLY, invite_only,
510                             COL_NAME, empathy_chatroom_get_name (chatroom),
511                             COL_ROOM, empathy_chatroom_get_room (chatroom),
512                             COL_MEMBERS, members,
513                             COL_MEMBERS_INT, empathy_chatroom_get_members_count (chatroom),
514                             COL_TOOLTIP, tooltip,
515                             -1);
516
517         g_free (members);
518         g_free (tooltip);
519 }
520
521 static void
522 start_listing_error_cb (EmpathyTpRoomlist        *room_list,
523                         GError                   *error,
524                         EmpathyNewChatroomDialog *dialog)
525 {
526         gtk_label_set_text (GTK_LABEL (dialog->label_error_message), _("Could not start room listing"));
527         gtk_widget_show_all (dialog->viewport_error);
528         gtk_widget_set_sensitive (dialog->treeview, FALSE);
529 }
530
531 static void
532 stop_listing_error_cb (EmpathyTpRoomlist        *room_list,
533                        GError                   *error,
534                        EmpathyNewChatroomDialog *dialog)
535 {
536         gtk_label_set_text (GTK_LABEL (dialog->label_error_message), _("Could not stop room listing"));
537         gtk_widget_show_all (dialog->viewport_error);
538         gtk_widget_set_sensitive (dialog->treeview, FALSE);
539 }
540
541 static void
542 new_chatroom_dialog_listing_cb (EmpathyTpRoomlist        *room_list,
543                                 gpointer                  unused,
544                                 EmpathyNewChatroomDialog *dialog)
545 {
546         gboolean listing;
547
548         listing = empathy_tp_roomlist_is_listing (room_list);
549
550         /* Update the throbber */
551         if (listing) {
552                 ephy_spinner_start (EPHY_SPINNER (dialog->throbber));
553         } else {
554                 ephy_spinner_stop (EPHY_SPINNER (dialog->throbber));
555         }
556 }
557
558 static void
559 new_chatroom_dialog_model_clear (EmpathyNewChatroomDialog *dialog)
560 {
561         GtkListStore *store;
562
563         store = GTK_LIST_STORE (dialog->model);
564         gtk_list_store_clear (store);
565 }
566
567 static void
568 new_chatroom_dialog_model_row_activated_cb (GtkTreeView             *tree_view,
569                                             GtkTreePath             *path,
570                                             GtkTreeViewColumn       *column,
571                                             EmpathyNewChatroomDialog *dialog)
572 {
573         gtk_widget_activate (dialog->button_join);
574 }
575
576 static void
577 new_chatroom_dialog_model_selection_changed (GtkTreeSelection         *selection,
578                                              EmpathyNewChatroomDialog *dialog)
579 {
580         GtkTreeModel *model;
581         GtkTreeIter   iter;
582         gchar        *room = NULL;
583         gchar        *server = NULL;
584
585         if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
586                 return;
587         }
588
589         gtk_tree_model_get (model, &iter, COL_ROOM, &room, -1);
590         server = strstr (room, "@");
591         if (server) {
592                 *server = '\0';
593                 server++;
594         }
595
596         gtk_entry_set_text (GTK_ENTRY (dialog->entry_server), server ? server : "");
597         gtk_entry_set_text (GTK_ENTRY (dialog->entry_room), room ? room : "");
598
599         g_free (room);
600 }
601
602 static void
603 new_chatroom_dialog_join (EmpathyNewChatroomDialog *dialog)
604 {
605         EmpathyAccountChooser *account_chooser;
606         TpConnection          *connection;
607         const gchar           *room;
608         const gchar           *server = NULL;
609         gchar                 *room_name = NULL;
610
611         room = gtk_entry_get_text (GTK_ENTRY (dialog->entry_room));
612         server = gtk_entry_get_text (GTK_ENTRY (dialog->entry_server));
613
614         account_chooser = EMPATHY_ACCOUNT_CHOOSER (dialog->account_chooser);
615         connection = empathy_account_chooser_get_connection (account_chooser);
616
617         if (!EMP_STR_EMPTY (server)) {
618                 room_name = g_strconcat (room, "@", server, NULL);
619         } else {
620                 room_name = g_strdup (room);
621         }
622
623         DEBUG ("Requesting channel for '%s'", room_name);
624         empathy_dispatcher_join_muc (connection, room_name, NULL, NULL);
625
626         g_free (room_name);
627 }
628
629 static void
630 new_chatroom_dialog_entry_changed_cb (GtkWidget                *entry,
631                                       EmpathyNewChatroomDialog *dialog)
632 {
633         if (entry == dialog->entry_room) {
634                 const gchar *room;
635
636                 room = gtk_entry_get_text (GTK_ENTRY (dialog->entry_room));
637                 gtk_widget_set_sensitive (dialog->button_join, !EMP_STR_EMPTY (room));
638                 /* FIXME: Select the room in the list */
639         }
640 }
641
642 static void
643 new_chatroom_dialog_browse_start (EmpathyNewChatroomDialog *dialog)
644 {
645         new_chatroom_dialog_model_clear (dialog);
646         if (dialog->room_list) {
647                 empathy_tp_roomlist_start (dialog->room_list);
648         }
649 }
650
651 static void
652 new_chatroom_dialog_browse_stop (EmpathyNewChatroomDialog *dialog)
653 {
654         if (dialog->room_list) {
655                 empathy_tp_roomlist_stop (dialog->room_list);
656         }
657 }
658
659 static void
660 new_chatroom_dialog_entry_server_activate_cb (GtkWidget                *widget,
661                                               EmpathyNewChatroomDialog  *dialog)
662 {
663         new_chatroom_dialog_browse_start (dialog);
664 }
665
666 static void
667 new_chatroom_dialog_expander_browse_activate_cb (GtkWidget               *widget,
668                                                  EmpathyNewChatroomDialog *dialog)
669 {
670         gboolean expanded;
671
672         expanded = gtk_expander_get_expanded (GTK_EXPANDER (widget));
673         if (expanded) {
674                 new_chatroom_dialog_browse_stop (dialog);
675                 gtk_window_set_resizable (GTK_WINDOW (dialog->window), FALSE);
676         } else {
677                 gtk_widget_hide (dialog->viewport_error);
678                 gtk_widget_set_sensitive (dialog->treeview, TRUE);
679                 new_chatroom_dialog_browse_start (dialog);
680                 gtk_window_set_resizable (GTK_WINDOW (dialog->window), TRUE);
681         }
682 }
683
684 static gboolean
685 new_chatroom_dialog_entry_server_focus_out_cb (GtkWidget               *widget,
686                                                GdkEventFocus           *event,
687                                                EmpathyNewChatroomDialog *dialog)
688 {
689         gboolean expanded;
690
691         expanded = gtk_expander_get_expanded (GTK_EXPANDER (dialog->expander_browse));
692         if (expanded) {
693                 new_chatroom_dialog_browse_start (dialog);
694         }
695         return FALSE;
696 }