[gaim-migrate @ 5229]
[pidgin-git.git] / src / buddy_chat.c
blobe7b194c966fdaa7a64634c6d0a4ab5bff4435ae2
1 /*
2 * gaim
4 * Copyright (C) 1998-1999, Mark Spencer <markster@marko.net>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (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
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25 #include <string.h>
26 #include <sys/time.h>
27 #include <unistd.h>
28 #include <ctype.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <gtk/gtk.h>
32 #include "gtkimhtml.h"
33 #ifdef USE_GTKSPELL
34 #include <gtkspell/gtkspell.h>
35 #endif
36 #include <gdk/gdkkeysyms.h>
38 #include "prpl.h"
40 #ifdef _WIN32
41 #include "wspell.h"
42 #endif
44 static GList *chatentries = NULL;
45 static GtkWidget *joinchat = NULL;
46 static GtkWidget *jc_vbox = NULL;
47 static struct gaim_connection *joinchatgc;
49 static void
50 do_join_chat()
52 if (joinchat) {
53 GList *data = NULL;
54 GList *tmp;
55 int *ival;
56 char *sval;
58 for (tmp = chatentries; tmp != NULL; tmp = tmp->next) {
59 if (g_object_get_data(tmp->data, "is_spin")) {
60 ival = g_new0(int, 1);
61 *ival = gtk_spin_button_get_value_as_int(tmp->data);
62 data = g_list_append(data, ival);
64 else {
65 sval = g_strdup(gtk_entry_get_text(tmp->data));
66 data = g_list_append(data, sval);
70 serv_join_chat(joinchatgc, data);
72 for (tmp = data; tmp != NULL; tmp = tmp->next)
73 g_free(tmp->data);
75 g_list_free(data);
77 gtk_widget_destroy(joinchat);
79 if (chatentries)
80 g_list_free(chatentries);
82 chatentries = NULL;
83 joinchat = NULL;
87 static void
88 rebuild_jc()
90 GList *list, *tmp;
91 struct proto_chat_entry *pce;
92 gboolean focus = TRUE;
94 if (!joinchatgc)
95 return;
97 while (GTK_BOX(jc_vbox)->children)
98 gtk_container_remove(GTK_CONTAINER(jc_vbox),
99 ((GtkBoxChild *)GTK_BOX(jc_vbox)->children->data)->widget);
101 if (chatentries)
102 g_list_free(chatentries);
104 chatentries = NULL;
106 list = joinchatgc->prpl->chat_info(joinchatgc);
108 for (tmp = list; tmp != NULL; tmp = tmp->next) {
109 GtkWidget *label;
110 GtkWidget *rowbox;
112 pce = tmp->data;
114 rowbox = gtk_hbox_new(FALSE, 5);
115 gtk_box_pack_start(GTK_BOX(jc_vbox), rowbox, TRUE, TRUE, 0);
116 gtk_widget_show(rowbox);
118 label = gtk_label_new(pce->label);
119 gtk_box_pack_start(GTK_BOX(rowbox), label, FALSE, FALSE, 0);
120 gtk_widget_show(label);
122 if (pce->is_int) {
123 GtkObject *adjust;
124 GtkWidget *spin;
125 adjust = gtk_adjustment_new(pce->min, pce->min,
126 pce->max, 1, 10, 10);
127 spin = gtk_spin_button_new(GTK_ADJUSTMENT(adjust), 1, 0);
128 g_object_set_data(G_OBJECT(spin), "is_spin", GINT_TO_POINTER(TRUE));
129 chatentries = g_list_append(chatentries, spin);
130 gtk_widget_set_size_request(spin, 50, -1);
131 gtk_box_pack_end(GTK_BOX(rowbox), spin, FALSE, FALSE, 0);
132 gtk_widget_show(spin);
134 else {
135 GtkWidget *entry;
137 entry = gtk_entry_new();
138 gtk_box_pack_end(GTK_BOX(rowbox), entry, FALSE, FALSE, 0);
140 chatentries = g_list_append(chatentries, entry);
142 if (pce->def)
143 gtk_entry_set_text(GTK_ENTRY(entry), pce->def);
145 if (focus) {
146 gtk_widget_grab_focus(entry);
147 focus = FALSE;
150 g_signal_connect(G_OBJECT(entry), "activate",
151 G_CALLBACK(do_join_chat), NULL);
153 gtk_widget_show(entry);
156 g_free(pce);
159 g_list_free(list);
162 static void
163 joinchat_choose(GtkWidget *w, struct gaim_connection *g)
165 if (joinchatgc == g)
166 return;
168 joinchatgc = g;
170 rebuild_jc();
173 static void
174 create_joinchat_menu(GtkWidget *box)
176 GtkWidget *optmenu;
177 GtkWidget *menu;
178 GtkWidget *opt;
179 GSList *c;
180 struct gaim_connection *g;
181 char buf[2048];
183 optmenu = gtk_option_menu_new();
184 gtk_box_pack_start(GTK_BOX(box), optmenu, FALSE, FALSE, 0);
186 menu = gtk_menu_new();
187 joinchatgc = NULL;
189 for (c = connections; c != NULL; c = c->next) {
190 g = (struct gaim_connection *)c->data;
192 if (!g->prpl->join_chat)
193 continue;
195 if (!joinchatgc)
196 joinchatgc = g;
198 g_snprintf(buf, sizeof(buf), "%s (%s)", g->username, g->prpl->name);
199 opt = gtk_menu_item_new_with_label(buf);
201 g_object_set_data(G_OBJECT(opt), "gaim_connection", g);
203 g_signal_connect(G_OBJECT(opt), "activate",
204 G_CALLBACK(joinchat_choose), g);
206 gtk_menu_shell_append(GTK_MENU_SHELL(menu), opt);
207 gtk_widget_show(opt);
210 gtk_option_menu_set_menu(GTK_OPTION_MENU(optmenu), menu);
211 gtk_option_menu_set_history(GTK_OPTION_MENU(optmenu), 0);
214 static void
215 destroy_join_chat()
217 if (joinchat)
218 gtk_widget_destroy(joinchat);
220 joinchat = NULL;
223 void
224 join_chat()
226 GtkWidget *mainbox;
227 GtkWidget *frame;
228 GtkWidget *fbox;
229 GtkWidget *rowbox;
230 GtkWidget *bbox;
231 GtkWidget *join;
232 GtkWidget *cancel;
233 GtkWidget *label;
234 GtkWidget *sep;
235 GSList *c;
236 struct gaim_connection *gc = NULL;
238 for (c = connections; c != NULL; c = c->next) {
239 gc = c->data;
241 if (gc->prpl->join_chat)
242 break;
244 gc = NULL;
247 if (gc == NULL) {
248 do_error_dialog(
249 _("You are not currently signed on with any protocols that have "
250 "the ability to chat."), NULL, GAIM_ERROR);
252 return;
255 if (!joinchat) {
256 GAIM_DIALOG(joinchat);
257 gtk_window_set_role(GTK_WINDOW(joinchat), "joinchat");
258 gtk_window_set_resizable(GTK_WINDOW(joinchat), TRUE);
259 gtk_widget_realize(joinchat);
260 g_signal_connect(G_OBJECT(joinchat), "delete_event",
261 G_CALLBACK(destroy_join_chat), joinchat);
262 gtk_window_set_title(GTK_WINDOW(joinchat), _("Join Chat"));
264 mainbox = gtk_vbox_new(FALSE, 5);
265 gtk_container_set_border_width(GTK_CONTAINER(mainbox), 5);
266 gtk_container_add(GTK_CONTAINER(joinchat), mainbox);
268 frame = make_frame(mainbox, _("Buddy Chat"));
270 fbox = gtk_vbox_new(FALSE, 5);
271 gtk_container_set_border_width(GTK_CONTAINER(fbox), 5);
272 gtk_container_add(GTK_CONTAINER(frame), fbox);
274 #ifndef NO_MULTI
275 rowbox = gtk_hbox_new(FALSE, 5);
276 gtk_box_pack_start(GTK_BOX(fbox), rowbox, TRUE, TRUE, 0);
278 label = gtk_label_new(_("Join Chat As:"));
279 gtk_box_pack_start(GTK_BOX(rowbox), label, FALSE, FALSE, 0);
281 create_joinchat_menu(rowbox);
283 jc_vbox = gtk_vbox_new(FALSE, 5);
284 gtk_container_add(GTK_CONTAINER(fbox), jc_vbox);
285 gtk_container_set_border_width(GTK_CONTAINER(jc_vbox), 0);
287 #else
288 joinchatgc = connections->data;
289 #endif
290 rebuild_jc();
291 /* buttons */
293 sep = gtk_hseparator_new();
294 gtk_widget_show(sep);
295 gtk_box_pack_start(GTK_BOX(mainbox), sep, FALSE, FALSE, 0);
297 bbox = gtk_hbox_new(FALSE, 5);
298 gtk_box_pack_start(GTK_BOX(mainbox), bbox, FALSE, FALSE, 0);
300 /* Join button. */
301 join = gaim_pixbuf_button_from_stock(_("Join"), GTK_STOCK_JUMP_TO,
302 GAIM_BUTTON_HORIZONTAL);
303 gtk_box_pack_end(GTK_BOX(bbox), join, FALSE, FALSE, 0);
304 g_signal_connect(G_OBJECT(join), "clicked",
305 G_CALLBACK(do_join_chat), NULL);
306 /* Cancel button. */
307 cancel = gaim_pixbuf_button_from_stock(_("Cancel"), GTK_STOCK_CANCEL,
308 GAIM_BUTTON_HORIZONTAL);
309 gtk_box_pack_end(GTK_BOX(bbox), cancel, FALSE, FALSE, 0);
310 g_signal_connect(G_OBJECT(cancel), "clicked",
311 G_CALLBACK(destroy_join_chat), joinchat);
315 gtk_widget_show_all(joinchat);