Mercurial > pidgin.yaz
annotate plugins/chatlist.c @ 2670:f6f2871d77c2
[gaim-migrate @ 2683]
"just come a little closer"
committer: Tailor Script <tailor@pidgin.im>
author | Eric Warmenhoven <eric@warmenhoven.org> |
---|---|
date | Sun, 04 Nov 2001 10:46:39 +0000 |
parents | 0b4ef57035af |
children | c74482cb3a8b |
rev | line source |
---|---|
2241 | 1 #define GAIM_PLUGINS |
2 | |
3 #include "pixmaps/cancel.xpm" | |
4 #include "pixmaps/refresh.xpm" | |
5 #include "pixmaps/gnome_add.xpm" | |
6 #include "pixmaps/gnome_remove.xpm" | |
7 | |
8 #include "proxy.h" | |
9 #include "gaim.h" | |
10 | |
11 #include <stdlib.h> | |
2272
4ecc9a9a75d8
[gaim-migrate @ 2282]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2241
diff
changeset
|
12 #include <string.h> |
2241 | 13 |
2417
5473c8c5378d
[gaim-migrate @ 2430]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2356
diff
changeset
|
14 #define AOL_SRCHSTR "/community/aimcheck.adp/url=" |
5473c8c5378d
[gaim-migrate @ 2430]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2356
diff
changeset
|
15 |
2241 | 16 struct chat_page { |
17 GtkWidget *list1; | |
18 GtkWidget *list2; | |
19 }; | |
20 | |
21 struct chat_room { | |
22 char name[80]; | |
23 int exchange; | |
24 }; | |
25 | |
26 static GtkWidget *item = NULL; /* this is the parent tree */ | |
27 static GList *chat_rooms = NULL; | |
28 | |
29 static GtkWidget *parent = NULL; /* this is the config thing where you can see the list */ | |
30 static struct chat_page *cp = NULL; | |
31 | |
32 static void des_item() | |
33 { | |
34 if (item) | |
35 gtk_widget_destroy(item); | |
36 item = NULL; | |
37 } | |
38 | |
39 static void handle_click_chat(GtkWidget *widget, GdkEventButton * event, struct chat_room *cr) | |
40 { | |
41 if (event->type == GDK_2BUTTON_PRESS && event->button == 1) { | |
42 GList *m = g_list_append(NULL, cr->name); | |
43 int *x = g_new0(int, 1); | |
44 *x = cr->exchange; | |
45 m = g_list_append(m, x); | |
46 serv_join_chat(connections->data, m); | |
47 g_free(x); | |
48 g_list_free(m); | |
49 } | |
50 } | |
51 | |
52 static void setup_buddy_chats() | |
53 { | |
54 GList *crs = chat_rooms; | |
55 GtkWidget *tree; | |
56 | |
57 if (!blist) | |
58 return; | |
59 | |
60 if (item) | |
61 gtk_tree_remove_item(GTK_TREE(buddies), item); | |
62 item = NULL; | |
63 | |
64 if (!chat_rooms) | |
65 return; | |
66 | |
67 item = gtk_tree_item_new_with_label(_("Buddy Chat")); | |
68 gtk_signal_connect(GTK_OBJECT(item), "destroy", GTK_SIGNAL_FUNC(des_item), NULL); | |
69 gtk_tree_append(GTK_TREE(buddies), item); | |
70 gtk_tree_item_expand(GTK_TREE_ITEM(item)); | |
71 gtk_widget_show(item); | |
72 | |
73 tree = gtk_tree_new(); | |
74 gtk_tree_item_set_subtree(GTK_TREE_ITEM(item), tree); | |
75 gtk_widget_show(tree); | |
76 | |
77 while (crs) { | |
78 struct chat_room *cr = (struct chat_room *)crs->data; | |
79 GtkWidget *titem = gtk_tree_item_new_with_label(cr->name); | |
80 gtk_object_set_user_data(GTK_OBJECT(titem), cr); | |
81 gtk_tree_append(GTK_TREE(tree), titem); | |
82 gtk_widget_show(titem); | |
83 gtk_signal_connect(GTK_OBJECT(titem), "button_press_event", | |
84 GTK_SIGNAL_FUNC(handle_click_chat), cr); | |
85 crs = crs->next; | |
86 } | |
87 } | |
88 | |
89 static void save_chat_prefs() | |
90 { | |
91 FILE *f; | |
92 char path[1024]; | |
93 char *x = gaim_user_dir(); | |
94 GList *crs = chat_rooms; | |
95 | |
96 g_snprintf(path, sizeof(path), "%s/%s", x, "chats"); | |
97 f = fopen(path, "w"); | |
98 if (!f) { | |
99 g_free(x); | |
100 return; | |
101 } | |
102 while (crs) { | |
103 struct chat_room *cr = crs->data; | |
104 crs = crs->next; | |
105 fprintf(f, "%s\n%d\n", cr->name, cr->exchange); | |
106 } | |
107 fclose(f); | |
108 } | |
109 | |
110 static void restore_chat_prefs() | |
111 { | |
112 FILE *f; | |
113 char path[1024]; | |
114 char *x = gaim_user_dir(); | |
115 char buf[1024]; | |
116 | |
117 g_snprintf(path, sizeof(path), "%s/%s", x, "chats"); | |
118 f = fopen(path, "r"); | |
119 if (!f) { | |
120 g_free(x); | |
121 return; | |
122 } | |
123 while (fgets(buf, 1024, f)) { | |
124 struct chat_room *cr = g_new0(struct chat_room, 1); | |
2356
ddf404cd9757
[gaim-migrate @ 2369]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2272
diff
changeset
|
125 g_snprintf(cr->name, sizeof(cr->name), "%s", g_strchomp(buf)); |
2241 | 126 if (!fgets(buf, 1024, f)) { |
127 g_free(cr); | |
128 break; | |
129 } | |
130 cr->exchange = atoi(buf); | |
131 chat_rooms = g_list_append(chat_rooms, cr); | |
132 } | |
133 fclose(f); | |
134 setup_buddy_chats(); | |
135 } | |
136 | |
137 static void ref_list_callback(gpointer data, char *text) | |
138 { | |
139 char *c; | |
140 int len; | |
141 GtkWidget *item; | |
142 GList *items = GTK_LIST(cp->list1)->children; | |
143 struct chat_room *cr; | |
144 c = text; | |
145 | |
146 if (!text) | |
147 return; | |
148 | |
149 len = strlen(text); | |
150 | |
151 while (items) { | |
152 g_free(gtk_object_get_user_data(GTK_OBJECT(items->data))); | |
153 items = items->next; | |
154 } | |
155 | |
156 items = NULL; | |
157 | |
158 gtk_list_clear_items(GTK_LIST(cp->list1), 0, -1); | |
159 | |
160 item = gtk_list_item_new_with_label(_("Gaim Chat")); | |
161 cr = g_new0(struct chat_room, 1); | |
162 strcpy(cr->name, _("Gaim Chat")); | |
163 cr->exchange = 4; | |
164 gtk_object_set_user_data(GTK_OBJECT(item), cr); | |
165 gtk_widget_show(item); | |
166 | |
167 items = g_list_append(NULL, item); | |
168 | |
169 while (c) { | |
170 if (c - text > len - 30) | |
171 break; /* assume no chat rooms 30 from end, padding */ | |
172 if (!g_strncasecmp(AOL_SRCHSTR, c, strlen(AOL_SRCHSTR))) { | |
173 char *t; | |
174 int len = 0; | |
175 int exchange; | |
176 char *name = NULL; | |
177 | |
178 c += strlen(AOL_SRCHSTR); | |
179 t = c; | |
180 while (t) { | |
181 len++; | |
182 name = g_realloc(name, len); | |
183 if (*t == '+') | |
184 name[len - 1] = ' '; | |
185 else if (*t == '&') { | |
186 name[len - 1] = 0; | |
187 sscanf(t, "&Exchange=%d", &exchange); | |
188 c = t + strlen("&Exchange=x"); | |
189 break; | |
190 } else | |
191 name[len - 1] = *t; | |
192 t++; | |
193 } | |
194 cr = g_new0(struct chat_room, 1); | |
195 strcpy(cr->name, name); | |
196 cr->exchange = exchange; | |
197 item = gtk_list_item_new_with_label(name); | |
198 gtk_widget_show(item); | |
199 items = g_list_append(items, item); | |
200 gtk_object_set_user_data(GTK_OBJECT(item), cr); | |
201 g_free(name); | |
202 } | |
203 c++; | |
204 } | |
205 gtk_list_append_items(GTK_LIST(cp->list1), items); | |
206 } | |
207 | |
208 static void refresh_list(GtkWidget *w, gpointer *m) | |
209 { | |
2584
34812d648f72
[gaim-migrate @ 2597]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2417
diff
changeset
|
210 grab_url("http://www.aol.com/community/chat/allchats.html", FALSE, ref_list_callback, NULL); |
2241 | 211 } |
212 | |
213 static void add_chat(GtkWidget *w, gpointer *m) | |
214 { | |
215 GList *sel = GTK_LIST(cp->list1)->selection; | |
216 struct chat_room *cr, *cr2; | |
217 GList *crs = chat_rooms; | |
218 GtkWidget *item; | |
219 | |
220 if (sel) { | |
221 cr = (struct chat_room *)gtk_object_get_user_data(GTK_OBJECT(sel->data)); | |
222 } else | |
223 return; | |
224 | |
225 while (crs) { | |
226 cr2 = (struct chat_room *)crs->data; | |
227 if (!g_strcasecmp(cr->name, cr2->name)) | |
228 return; | |
229 crs = crs->next; | |
230 } | |
231 item = gtk_list_item_new_with_label(cr->name); | |
232 cr2 = g_new0(struct chat_room, 1); | |
233 strcpy(cr2->name, cr->name); | |
234 cr2->exchange = cr->exchange; | |
235 gtk_object_set_user_data(GTK_OBJECT(item), cr2); | |
236 gtk_widget_show(item); | |
237 sel = g_list_append(NULL, item); | |
238 gtk_list_append_items(GTK_LIST(cp->list2), sel); | |
239 chat_rooms = g_list_append(chat_rooms, cr2); | |
240 | |
241 setup_buddy_chats(); | |
242 save_chat_prefs(); | |
243 } | |
244 | |
245 static void remove_chat(GtkWidget *w, gpointer *m) | |
246 { | |
247 GList *sel = GTK_LIST(cp->list2)->selection; | |
248 struct chat_room *cr; | |
249 GList *crs; | |
250 GtkWidget *item; | |
251 | |
252 if (sel) { | |
253 item = (GtkWidget *)sel->data; | |
254 cr = (struct chat_room *)gtk_object_get_user_data(GTK_OBJECT(item)); | |
255 } else | |
256 return; | |
257 | |
258 chat_rooms = g_list_remove(chat_rooms, cr); | |
259 | |
260 | |
261 gtk_list_clear_items(GTK_LIST(cp->list2), 0, -1); | |
262 | |
263 if (g_list_length(chat_rooms) == 0) | |
264 chat_rooms = NULL; | |
265 | |
266 crs = chat_rooms; | |
267 | |
268 while (crs) { | |
269 cr = (struct chat_room *)crs->data; | |
270 item = gtk_list_item_new_with_label(cr->name); | |
271 gtk_object_set_user_data(GTK_OBJECT(item), cr); | |
272 gtk_widget_show(item); | |
273 gtk_list_append_items(GTK_LIST(cp->list2), g_list_append(NULL, item)); | |
274 | |
275 | |
276 crs = crs->next; | |
277 } | |
278 | |
279 setup_buddy_chats(); | |
280 save_chat_prefs(); | |
281 } | |
282 | |
283 static void parent_destroy() | |
284 { | |
285 if (parent) | |
286 gtk_widget_destroy(parent); | |
287 parent = NULL; | |
288 } | |
289 | |
290 void gaim_plugin_config() | |
291 { | |
292 GtkWidget *vbox; | |
293 GtkWidget *frame; | |
294 GtkWidget *box; | |
295 GtkWidget *table; | |
296 GtkWidget *rem_button, *add_button, *ref_button; | |
297 GtkWidget *list1, *list2; | |
298 GtkWidget *label; | |
299 GtkWidget *sw1, *sw2; | |
300 GtkWidget *item; | |
301 GtkWidget *hbox; | |
302 GtkWidget *button; | |
303 GList *crs = chat_rooms; | |
304 GList *items = NULL; | |
305 struct chat_room *cr; | |
306 | |
307 if (parent) { | |
308 gtk_widget_show(parent); | |
309 return; | |
310 } | |
311 | |
312 if (cp) | |
313 g_free(cp); | |
314 cp = g_new0(struct chat_page, 1); | |
315 | |
316 parent = gtk_window_new(GTK_WINDOW_TOPLEVEL); | |
317 gtk_widget_set_usize(parent, 300, 400); | |
318 gtk_window_set_title(GTK_WINDOW(parent), "Chat Rooms"); | |
319 gtk_window_set_wmclass(GTK_WINDOW(parent), "chatlist", "Gaim"); | |
320 gtk_widget_realize(parent); | |
321 aol_icon(parent->window); | |
322 gtk_signal_connect(GTK_OBJECT(parent), "destroy", | |
323 GTK_SIGNAL_FUNC(parent_destroy), NULL); | |
324 | |
325 vbox = gtk_vbox_new(FALSE, 0); | |
326 gtk_container_add(GTK_CONTAINER(parent), vbox); | |
327 gtk_widget_show(vbox); | |
328 | |
329 hbox = gtk_hbox_new(FALSE, 0); | |
330 gtk_box_pack_end(GTK_BOX(vbox), hbox, FALSE, FALSE, 5); | |
331 gtk_widget_show(hbox); | |
332 | |
333 button = picture_button(parent, _("Close"), cancel_xpm); | |
334 gtk_box_pack_end(GTK_BOX(hbox), button, FALSE, FALSE, 5); | |
335 gtk_signal_connect(GTK_OBJECT(button), "clicked", | |
336 GTK_SIGNAL_FUNC(parent_destroy), NULL); | |
337 | |
338 frame = gtk_frame_new(_("Chat Rooms")); | |
339 gtk_box_pack_start(GTK_BOX(vbox), frame, TRUE, TRUE, 5); | |
340 gtk_widget_show(frame); | |
341 | |
342 box = gtk_vbox_new(FALSE, 5); | |
343 gtk_container_set_border_width(GTK_CONTAINER(box), 5); | |
344 gtk_container_add(GTK_CONTAINER(frame), box); | |
345 gtk_widget_show(box); | |
346 | |
347 table = gtk_table_new(4, 2, FALSE); | |
348 gtk_widget_show(table); | |
349 | |
350 gtk_box_pack_start(GTK_BOX(box), table, TRUE, TRUE, 0); | |
351 | |
352 list1 = gtk_list_new(); | |
353 list2 = gtk_list_new(); | |
354 sw1 = gtk_scrolled_window_new(NULL, NULL); | |
355 sw2 = gtk_scrolled_window_new(NULL, NULL); | |
356 | |
357 ref_button = picture_button(parent, _("Refresh"), refresh_xpm); | |
358 add_button = picture_button(parent, _("Add"), gnome_add_xpm); | |
359 rem_button = picture_button(parent, _("Remove"), gnome_remove_xpm); | |
360 gtk_widget_show(list1); | |
361 gtk_widget_show(sw1); | |
362 gtk_widget_show(list2); | |
363 gtk_widget_show(sw2); | |
364 | |
365 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(sw1), list1); | |
366 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(sw2), list2); | |
367 | |
368 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw1), | |
369 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS); | |
370 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw2), | |
371 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS); | |
372 | |
373 cp->list1 = list1; | |
374 cp->list2 = list2; | |
375 | |
376 gtk_signal_connect(GTK_OBJECT(ref_button), "clicked", GTK_SIGNAL_FUNC(refresh_list), cp); | |
377 gtk_signal_connect(GTK_OBJECT(rem_button), "clicked", GTK_SIGNAL_FUNC(remove_chat), cp); | |
378 gtk_signal_connect(GTK_OBJECT(add_button), "clicked", GTK_SIGNAL_FUNC(add_chat), cp); | |
379 | |
380 | |
381 | |
382 label = gtk_label_new(_("List of available chats")); | |
383 gtk_widget_show(label); | |
384 | |
385 gtk_table_attach(GTK_TABLE(table), label, 0, 1, 0, 1, GTK_SHRINK, GTK_SHRINK, 0, 0); | |
386 gtk_table_attach(GTK_TABLE(table), ref_button, 0, 1, 1, 2, GTK_SHRINK, GTK_SHRINK, 0, 0); | |
387 gtk_table_attach(GTK_TABLE(table), sw1, 0, 1, 2, 3, | |
388 GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 5, 5); | |
389 gtk_table_attach(GTK_TABLE(table), add_button, 0, 1, 3, 4, GTK_SHRINK, GTK_SHRINK, 0, 0); | |
390 | |
391 | |
392 label = gtk_label_new(_("List of subscribed chats")); | |
393 gtk_widget_show(label); | |
394 | |
395 gtk_table_attach(GTK_TABLE(table), label, 1, 2, 0, 1, GTK_SHRINK, GTK_SHRINK, 0, 0); | |
396 gtk_table_attach(GTK_TABLE(table), sw2, 1, 2, 2, 3, | |
397 GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 5, 5); | |
398 gtk_table_attach(GTK_TABLE(table), rem_button, 1, 2, 3, 4, GTK_SHRINK, GTK_SHRINK, 0, 0); | |
399 | |
400 | |
401 item = gtk_list_item_new_with_label(_("Gaim Chat")); | |
402 cr = g_new0(struct chat_room, 1); | |
403 strcpy(cr->name, _("Gaim Chat")); | |
404 cr->exchange = 4; | |
405 gtk_object_set_user_data(GTK_OBJECT(item), cr); | |
406 gtk_widget_show(item); | |
407 gtk_list_append_items(GTK_LIST(list1), g_list_append(NULL, item)); | |
408 | |
409 | |
410 while (crs) { | |
411 cr = (struct chat_room *)crs->data; | |
412 item = gtk_list_item_new_with_label(cr->name); | |
413 gtk_object_set_user_data(GTK_OBJECT(item), cr); | |
414 gtk_widget_show(item); | |
415 items = g_list_append(items, item); | |
416 | |
417 crs = crs->next; | |
418 } | |
419 | |
420 gtk_list_append_items(GTK_LIST(list2), items); | |
421 | |
422 gtk_widget_show(parent); | |
423 } | |
424 | |
425 static void handle_signon(struct gaim_connection *gc) | |
426 { | |
427 setup_buddy_chats(); | |
428 } | |
429 | |
430 char *gaim_plugin_init(GModule *m) | |
431 { | |
432 restore_chat_prefs(); | |
433 gaim_signal_connect(m, event_signon, handle_signon, NULL); | |
434 return NULL; | |
435 } | |
436 | |
437 void gaim_plugin_remove() | |
438 { | |
439 if (parent) | |
440 gtk_widget_destroy(parent); | |
441 parent = NULL; | |
442 | |
443 if (item) | |
444 gtk_tree_remove_item(GTK_TREE(buddies), item); | |
445 item = NULL; | |
446 | |
447 while (chat_rooms) { | |
448 g_free(chat_rooms->data); | |
449 chat_rooms = g_list_remove(chat_rooms, chat_rooms->data); | |
450 } | |
451 | |
452 if (cp) | |
453 g_free(cp); | |
454 cp = NULL; | |
455 } | |
456 | |
457 char *name() | |
458 { | |
459 return "Chat List"; | |
460 } | |
461 | |
462 char *description() | |
463 { | |
464 return "Allows you to add chat rooms to your buddy list. Click the configure button to choose" | |
465 " which rooms."; | |
466 } |