1 /* X-Chat
2  * Copyright (C) 2005 Peter Zelezny.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18 
19 /* joind.c - The Join Dialog.
20 
21    Popups up when you connect without any autojoin channels and helps you
22    to find or join a channel.
23 */
24 
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <string.h>
28 #include <stdio.h>
29 
30 #ifndef WIN32
31 #include <unistd.h>
32 #endif
33 
34 #include "../common/hexchat.h"
35 #include "../common/hexchatc.h"
36 #include "../common/server.h"
37 #include "../common/servlist.h"
38 #include "../common/fe.h"
39 #include "fe-gtk.h"
40 #include "chanlist.h"
41 
42 
43 static void
joind_radio2_cb(GtkWidget * radio,server * serv)44 joind_radio2_cb (GtkWidget *radio, server *serv)
45 {
46 	if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (radio)))
47 	{
48 		gtk_widget_grab_focus (serv->gui->joind_entry);
49 		gtk_editable_set_position (GTK_EDITABLE (serv->gui->joind_entry), 999);
50 	}
51 }
52 
53 static void
joind_entryenter_cb(GtkWidget * entry,GtkWidget * ok)54 joind_entryenter_cb (GtkWidget *entry, GtkWidget *ok)
55 {
56 	gtk_widget_grab_focus (ok);
57 }
58 
59 static void
joind_entryfocus_cb(GtkWidget * entry,GdkEventFocus * event,server * serv)60 joind_entryfocus_cb (GtkWidget *entry, GdkEventFocus *event, server *serv)
61 {
62 	gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (serv->gui->joind_radio2), TRUE);
63 }
64 
65 static void
joind_destroy_cb(GtkWidget * win,server * serv)66 joind_destroy_cb (GtkWidget *win, server *serv)
67 {
68 	if (is_server (serv))
69 		serv->gui->joind_win = NULL;
70 }
71 
72 static void
joind_ok_cb(GtkWidget * ok,server * serv)73 joind_ok_cb (GtkWidget *ok, server *serv)
74 {
75 	if (!is_server (serv))
76 	{
77 		gtk_widget_destroy (gtk_widget_get_toplevel (ok));
78 		return;
79 	}
80 
81 	/* do nothing */
82 	if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (serv->gui->joind_radio1)))
83 		goto xit;
84 
85 	/* join specific channel */
86 	if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (serv->gui->joind_radio2)))
87 	{
88 		char *text = (char *)gtk_entry_get_text (GTK_ENTRY (serv->gui->joind_entry));
89 		if (strlen (text) < 1)
90 		{
91 			fe_message (_("Channel name too short, try again."), FE_MSG_ERROR);
92 			return;
93 		}
94 		serv->p_join (serv, text, "");
95 		goto xit;
96 	}
97 
98 	/* channel list */
99 	chanlist_opengui (serv, TRUE);
100 
101 xit:
102 	prefs.hex_gui_join_dialog = 0;
103 	if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (serv->gui->joind_check)))
104 		prefs.hex_gui_join_dialog = 1;
105 
106 	gtk_widget_destroy (serv->gui->joind_win);
107 	serv->gui->joind_win = NULL;
108 }
109 
110 static void
joind_show_dialog(server * serv)111 joind_show_dialog (server *serv)
112 {
113 	GtkWidget *dialog1;
114 	GtkWidget *dialog_vbox1;
115 	GtkWidget *vbox1;
116 	GtkWidget *hbox1;
117 	GtkWidget *image1;
118 	GtkWidget *vbox2;
119 	GtkWidget *label;
120 	GtkWidget *radiobutton1;
121 	GtkWidget *radiobutton2;
122 	GtkWidget *radiobutton3;
123 	GSList *radiobutton1_group;
124 	GtkWidget *hbox2;
125 	GtkWidget *entry1;
126 	GtkWidget *checkbutton1;
127 	GtkWidget *dialog_action_area1;
128 	GtkWidget *okbutton1;
129 	char buf[256];
130 	char buf2[256];
131 
132 	serv->gui->joind_win = dialog1 = gtk_dialog_new ();
133 	g_snprintf(buf, sizeof(buf), _("Connection Complete - %s"), _(DISPLAY_NAME));
134 	gtk_window_set_title (GTK_WINDOW (dialog1), buf);
135 	gtk_window_set_type_hint (GTK_WINDOW (dialog1), GDK_WINDOW_TYPE_HINT_DIALOG);
136 	gtk_window_set_position (GTK_WINDOW (dialog1), GTK_WIN_POS_CENTER_ON_PARENT);
137 	gtk_window_set_transient_for (GTK_WINDOW(dialog1), GTK_WINDOW(serv->front_session->gui->window));
138 	gtk_window_set_modal (GTK_WINDOW (dialog1), TRUE);
139 	gtk_window_set_resizable (GTK_WINDOW (dialog1), FALSE);
140 
141 	dialog_vbox1 = gtk_dialog_get_content_area (GTK_DIALOG (dialog1));
142 	gtk_widget_show (dialog_vbox1);
143 
144 	vbox1 = gtk_vbox_new (FALSE, 0);
145 	gtk_widget_show (vbox1);
146 	gtk_box_pack_start (GTK_BOX (dialog_vbox1), vbox1, TRUE, TRUE, 0);
147 
148 	hbox1 = gtk_hbox_new (FALSE, 0);
149 	gtk_widget_show (hbox1);
150 	gtk_box_pack_start (GTK_BOX (vbox1), hbox1, TRUE, TRUE, 0);
151 
152 	image1 = gtk_image_new_from_stock (GTK_STOCK_NETWORK, GTK_ICON_SIZE_LARGE_TOOLBAR);
153 	gtk_widget_show (image1);
154 	gtk_box_pack_start (GTK_BOX (hbox1), image1, FALSE, TRUE, 24);
155 	gtk_misc_set_alignment (GTK_MISC (image1), 0.5f, 0.06f);
156 
157 	vbox2 = gtk_vbox_new (FALSE, 10);
158 	gtk_container_set_border_width (GTK_CONTAINER (vbox2), 6);
159 	gtk_widget_show (vbox2);
160 	gtk_box_pack_start (GTK_BOX (hbox1), vbox2, TRUE, TRUE, 0);
161 
162 	g_snprintf (buf2, sizeof (buf2), _("Connection to %s complete."),
163 				 server_get_network (serv, TRUE));
164 	g_snprintf (buf, sizeof (buf), "\n<b>%s</b>", buf2);
165 	label = gtk_label_new (buf);
166 	gtk_widget_show (label);
167 	gtk_box_pack_start (GTK_BOX (vbox2), label, FALSE, FALSE, 0);
168 	gtk_label_set_use_markup (GTK_LABEL (label), TRUE);
169 	gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
170 
171 	label = gtk_label_new (_("In the server list window, no channel (chat room) has been entered to be automatically joined for this network."));
172 	gtk_widget_show (label);
173 	gtk_box_pack_start (GTK_BOX (vbox2), label, FALSE, FALSE, 0);
174 	gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
175 	gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
176 
177 	label = gtk_label_new (_("What would you like to do next?"));
178 	gtk_widget_show (label);
179 	gtk_box_pack_start (GTK_BOX (vbox2), label, FALSE, FALSE, 0);
180 	gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
181 
182 	serv->gui->joind_radio1 = radiobutton1 = gtk_radio_button_new_with_mnemonic (NULL, _("_Nothing, I'll join a channel later."));
183 	gtk_widget_show (radiobutton1);
184 	gtk_box_pack_start (GTK_BOX (vbox2), radiobutton1, FALSE, FALSE, 0);
185 	radiobutton1_group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (radiobutton1));
186 
187 	hbox2 = gtk_hbox_new (FALSE, 0);
188 	gtk_widget_show (hbox2);
189 	gtk_box_pack_start (GTK_BOX (vbox2), hbox2, FALSE, FALSE, 0);
190 
191 	serv->gui->joind_radio2 = radiobutton2 = gtk_radio_button_new_with_mnemonic (NULL, _("_Join this channel:"));
192 	gtk_widget_show (radiobutton2);
193 	gtk_box_pack_start (GTK_BOX (hbox2), radiobutton2, FALSE, FALSE, 0);
194 	gtk_radio_button_set_group (GTK_RADIO_BUTTON (radiobutton2), radiobutton1_group);
195 	radiobutton1_group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (radiobutton2));
196 
197 	serv->gui->joind_entry = entry1 = gtk_entry_new ();
198 	gtk_entry_set_text (GTK_ENTRY (entry1), "#");
199 	gtk_widget_show (entry1);
200 	gtk_box_pack_start (GTK_BOX (hbox2), entry1, TRUE, TRUE, 8);
201 
202 	g_snprintf (buf, sizeof (buf), "<small>     %s</small>",
203 				 _("If you know the name of the channel you want to join, enter it here."));
204 	label = gtk_label_new (buf);
205 	gtk_widget_show (label);
206 	gtk_box_pack_start (GTK_BOX (vbox2), label, FALSE, FALSE, 0);
207 	gtk_label_set_use_markup (GTK_LABEL (label), TRUE);
208 	gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
209 
210 	radiobutton3 = gtk_radio_button_new_with_mnemonic (NULL, _("O_pen the channel list."));
211 	gtk_widget_show (radiobutton3);
212 	gtk_box_pack_start (GTK_BOX (vbox2), radiobutton3, FALSE, FALSE, 0);
213 	gtk_radio_button_set_group (GTK_RADIO_BUTTON (radiobutton3), radiobutton1_group);
214 
215 	g_snprintf (buf, sizeof (buf), "<small>     %s</small>",
216 				 _("Retrieving the channel list may take a minute or two."));
217 	label = gtk_label_new (buf);
218 	gtk_widget_show (label);
219 	gtk_box_pack_start (GTK_BOX (vbox2), label, FALSE, FALSE, 0);
220 	gtk_label_set_use_markup (GTK_LABEL (label), TRUE);
221 	gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
222 
223 	serv->gui->joind_check = checkbutton1 = gtk_check_button_new_with_mnemonic (_("_Always show this dialog after connecting."));
224 	if (prefs.hex_gui_join_dialog)
225 		gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (checkbutton1), TRUE);
226 	gtk_widget_show (checkbutton1);
227 	gtk_box_pack_start (GTK_BOX (vbox1), checkbutton1, FALSE, FALSE, 0);
228 
229 	dialog_action_area1 = gtk_dialog_get_action_area (GTK_DIALOG (dialog1));
230 	gtk_widget_show (dialog_action_area1);
231 	gtk_button_box_set_layout (GTK_BUTTON_BOX (dialog_action_area1), GTK_BUTTONBOX_END);
232 
233 	okbutton1 = gtk_button_new_from_stock ("gtk-ok");
234 	gtk_widget_show (okbutton1);
235 	gtk_box_pack_end (GTK_BOX (gtk_dialog_get_action_area (GTK_DIALOG (dialog1))), okbutton1, FALSE, TRUE, 0);
236 	gtk_widget_set_can_default (okbutton1, TRUE);
237 
238 	g_signal_connect (G_OBJECT (dialog1), "destroy",
239 							G_CALLBACK (joind_destroy_cb), serv);
240 	g_signal_connect (G_OBJECT (entry1), "focus_in_event",
241 							G_CALLBACK (joind_entryfocus_cb), serv);
242 	g_signal_connect (G_OBJECT (entry1), "activate",
243 							G_CALLBACK (joind_entryenter_cb), okbutton1);
244 	g_signal_connect (G_OBJECT (radiobutton2), "toggled",
245 							G_CALLBACK (joind_radio2_cb), serv);
246 	g_signal_connect (G_OBJECT (okbutton1), "clicked",
247 							G_CALLBACK (joind_ok_cb), serv);
248 
249 	if (serv->network)
250 		if (g_ascii_strcasecmp(((ircnet*)serv->network)->name, "Libera.Chat") == 0)
251 		{
252 			gtk_entry_set_text (GTK_ENTRY (entry1), "#hexchat");
253 		}
254 
255 	gtk_widget_grab_focus (okbutton1);
256 	gtk_widget_show_all (dialog1);
257 }
258 
259 void
joind_open(server * serv)260 joind_open (server *serv)
261 {
262 	if (prefs.hex_gui_join_dialog)
263 		joind_show_dialog (serv);
264 }
265 
266 void
joind_close(server * serv)267 joind_close (server *serv)
268 {
269 	if (serv->gui->joind_win)
270 	{
271 		gtk_widget_destroy (serv->gui->joind_win);
272 		serv->gui->joind_win = NULL;
273 	}
274 }
275