Mercurial > pidgin
annotate plugins/chatlist.c @ 2859:82cf2358c771
[gaim-migrate @ 2872]
heh. one of these days i'll start thinking before acting.
committer: Tailor Script <tailor@pidgin.im>
author | Eric Warmenhoven <eric@warmenhoven.org> |
---|---|
date | Sat, 08 Dec 2001 10:15:09 +0000 |
parents | c74482cb3a8b |
children | 30828b83143d |
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 } | |
2692
c74482cb3a8b
[gaim-migrate @ 2705]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2664
diff
changeset
|
107 g_free(x); |
2241 | 108 fclose(f); |
109 } | |
110 | |
111 static void restore_chat_prefs() | |
112 { | |
113 FILE *f; | |
114 char path[1024]; | |
115 char *x = gaim_user_dir(); | |
116 char buf[1024]; | |
117 | |
118 g_snprintf(path, sizeof(path), "%s/%s", x, "chats"); | |
119 f = fopen(path, "r"); | |
120 if (!f) { | |
121 g_free(x); | |
122 return; | |
123 } | |
124 while (fgets(buf, 1024, f)) { | |
125 struct chat_room *cr = g_new0(struct chat_room, 1); | |
2356
ddf404cd9757
[gaim-migrate @ 2369]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2272
diff
changeset
|
126 g_snprintf(cr->name, sizeof(cr->name), "%s", g_strchomp(buf)); |
2241 | 127 if (!fgets(buf, 1024, f)) { |
128 g_free(cr); | |
129 break; | |
130 } | |
131 cr->exchange = atoi(buf); | |
132 chat_rooms = g_list_append(chat_rooms, cr); | |
133 } | |
2692
c74482cb3a8b
[gaim-migrate @ 2705]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2664
diff
changeset
|
134 g_free(x); |
2241 | 135 fclose(f); |
136 setup_buddy_chats(); | |
137 } | |
138 | |
139 static void ref_list_callback(gpointer data, char *text) | |
140 { | |
141 char *c; | |
142 int len; | |
143 GtkWidget *item; | |
144 GList *items = GTK_LIST(cp->list1)->children; | |
145 struct chat_room *cr; | |
146 c = text; | |
147 | |
148 if (!text) | |
149 return; | |
150 | |
151 len = strlen(text); | |
152 | |
153 while (items) { | |
154 g_free(gtk_object_get_user_data(GTK_OBJECT(items->data))); | |
155 items = items->next; | |
156 } | |
157 | |
158 items = NULL; | |
159 | |
160 gtk_list_clear_items(GTK_LIST(cp->list1), 0, -1); | |
161 | |
162 item = gtk_list_item_new_with_label(_("Gaim Chat")); | |
163 cr = g_new0(struct chat_room, 1); | |
164 strcpy(cr->name, _("Gaim Chat")); | |
165 cr->exchange = 4; | |
166 gtk_object_set_user_data(GTK_OBJECT(item), cr); | |
167 gtk_widget_show(item); | |
168 | |
169 items = g_list_append(NULL, item); | |
170 | |
171 while (c) { | |
172 if (c - text > len - 30) | |
173 break; /* assume no chat rooms 30 from end, padding */ | |
174 if (!g_strncasecmp(AOL_SRCHSTR, c, strlen(AOL_SRCHSTR))) { | |
175 char *t; | |
176 int len = 0; | |
177 int exchange; | |
178 char *name = NULL; | |
179 | |
180 c += strlen(AOL_SRCHSTR); | |
181 t = c; | |
182 while (t) { | |
183 len++; | |
184 name = g_realloc(name, len); | |
185 if (*t == '+') | |
186 name[len - 1] = ' '; | |
187 else if (*t == '&') { | |
188 name[len - 1] = 0; | |
189 sscanf(t, "&Exchange=%d", &exchange); | |
190 c = t + strlen("&Exchange=x"); | |
191 break; | |
192 } else | |
193 name[len - 1] = *t; | |
194 t++; | |
195 } | |
196 cr = g_new0(struct chat_room, 1); | |
197 strcpy(cr->name, name); | |
198 cr->exchange = exchange; | |
199 item = gtk_list_item_new_with_label(name); | |
200 gtk_widget_show(item); | |
201 items = g_list_append(items, item); | |
202 gtk_object_set_user_data(GTK_OBJECT(item), cr); | |
203 g_free(name); | |
204 } | |
205 c++; | |
206 } | |
207 gtk_list_append_items(GTK_LIST(cp->list1), items); | |
208 } | |
209 | |
210 static void refresh_list(GtkWidget *w, gpointer *m) | |
211 { | |
2584
34812d648f72
[gaim-migrate @ 2597]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2417
diff
changeset
|
212 grab_url("http://www.aol.com/community/chat/allchats.html", FALSE, ref_list_callback, NULL); |
2241 | 213 } |
214 | |
215 static void add_chat(GtkWidget *w, gpointer *m) | |
216 { | |
217 GList *sel = GTK_LIST(cp->list1)->selection; | |
218 struct chat_room *cr, *cr2; | |
219 GList *crs = chat_rooms; | |
220 GtkWidget *item; | |
221 | |
222 if (sel) { | |
223 cr = (struct chat_room *)gtk_object_get_user_data(GTK_OBJECT(sel->data)); | |
224 } else | |
225 return; | |
226 | |
227 while (crs) { | |
228 cr2 = (struct chat_room *)crs->data; | |
229 if (!g_strcasecmp(cr->name, cr2->name)) | |
230 return; | |
231 crs = crs->next; | |
232 } | |
233 item = gtk_list_item_new_with_label(cr->name); | |
234 cr2 = g_new0(struct chat_room, 1); | |
235 strcpy(cr2->name, cr->name); | |
236 cr2->exchange = cr->exchange; | |
237 gtk_object_set_user_data(GTK_OBJECT(item), cr2); | |
238 gtk_widget_show(item); | |
239 sel = g_list_append(NULL, item); | |
240 gtk_list_append_items(GTK_LIST(cp->list2), sel); | |
241 chat_rooms = g_list_append(chat_rooms, cr2); | |
242 | |
243 setup_buddy_chats(); | |
244 save_chat_prefs(); | |
245 } | |
246 | |
247 static void remove_chat(GtkWidget *w, gpointer *m) | |
248 { | |
249 GList *sel = GTK_LIST(cp->list2)->selection; | |
250 struct chat_room *cr; | |
251 GList *crs; | |
252 GtkWidget *item; | |
253 | |
254 if (sel) { | |
255 item = (GtkWidget *)sel->data; | |
256 cr = (struct chat_room *)gtk_object_get_user_data(GTK_OBJECT(item)); | |
257 } else | |
258 return; | |
259 | |
260 chat_rooms = g_list_remove(chat_rooms, cr); | |
261 | |
262 | |
263 gtk_list_clear_items(GTK_LIST(cp->list2), 0, -1); | |
264 | |
265 if (g_list_length(chat_rooms) == 0) | |
266 chat_rooms = NULL; | |
267 | |
268 crs = chat_rooms; | |
269 | |
270 while (crs) { | |
271 cr = (struct chat_room *)crs->data; | |
272 item = gtk_list_item_new_with_label(cr->name); | |
273 gtk_object_set_user_data(GTK_OBJECT(item), cr); | |
274 gtk_widget_show(item); | |
275 gtk_list_append_items(GTK_LIST(cp->list2), g_list_append(NULL, item)); | |
276 | |
277 | |
278 crs = crs->next; | |
279 } | |
280 | |
281 setup_buddy_chats(); | |
282 save_chat_prefs(); | |
283 } | |
284 | |
285 static void parent_destroy() | |
286 { | |
287 if (parent) | |
288 gtk_widget_destroy(parent); | |
289 parent = NULL; | |
290 } | |
291 | |
292 void gaim_plugin_config() | |
293 { | |
294 GtkWidget *vbox; | |
295 GtkWidget *frame; | |
296 GtkWidget *box; | |
297 GtkWidget *table; | |
298 GtkWidget *rem_button, *add_button, *ref_button; | |
299 GtkWidget *list1, *list2; | |
300 GtkWidget *label; | |
301 GtkWidget *sw1, *sw2; | |
302 GtkWidget *item; | |
303 GtkWidget *hbox; | |
304 GtkWidget *button; | |
305 GList *crs = chat_rooms; | |
306 GList *items = NULL; | |
307 struct chat_room *cr; | |
308 | |
309 if (parent) { | |
310 gtk_widget_show(parent); | |
311 return; | |
312 } | |
313 | |
314 if (cp) | |
315 g_free(cp); | |
316 cp = g_new0(struct chat_page, 1); | |
317 | |
318 parent = gtk_window_new(GTK_WINDOW_TOPLEVEL); | |
319 gtk_widget_set_usize(parent, 300, 400); | |
320 gtk_window_set_title(GTK_WINDOW(parent), "Chat Rooms"); | |
321 gtk_window_set_wmclass(GTK_WINDOW(parent), "chatlist", "Gaim"); | |
322 gtk_widget_realize(parent); | |
323 aol_icon(parent->window); | |
324 gtk_signal_connect(GTK_OBJECT(parent), "destroy", | |
325 GTK_SIGNAL_FUNC(parent_destroy), NULL); | |
326 | |
327 vbox = gtk_vbox_new(FALSE, 0); | |
328 gtk_container_add(GTK_CONTAINER(parent), vbox); | |
329 gtk_widget_show(vbox); | |
330 | |
331 hbox = gtk_hbox_new(FALSE, 0); | |
332 gtk_box_pack_end(GTK_BOX(vbox), hbox, FALSE, FALSE, 5); | |
333 gtk_widget_show(hbox); | |
334 | |
335 button = picture_button(parent, _("Close"), cancel_xpm); | |
336 gtk_box_pack_end(GTK_BOX(hbox), button, FALSE, FALSE, 5); | |
337 gtk_signal_connect(GTK_OBJECT(button), "clicked", | |
338 GTK_SIGNAL_FUNC(parent_destroy), NULL); | |
339 | |
340 frame = gtk_frame_new(_("Chat Rooms")); | |
341 gtk_box_pack_start(GTK_BOX(vbox), frame, TRUE, TRUE, 5); | |
342 gtk_widget_show(frame); | |
343 | |
344 box = gtk_vbox_new(FALSE, 5); | |
345 gtk_container_set_border_width(GTK_CONTAINER(box), 5); | |
346 gtk_container_add(GTK_CONTAINER(frame), box); | |
347 gtk_widget_show(box); | |
348 | |
349 table = gtk_table_new(4, 2, FALSE); | |
350 gtk_widget_show(table); | |
351 | |
352 gtk_box_pack_start(GTK_BOX(box), table, TRUE, TRUE, 0); | |
353 | |
354 list1 = gtk_list_new(); | |
355 list2 = gtk_list_new(); | |
356 sw1 = gtk_scrolled_window_new(NULL, NULL); | |
357 sw2 = gtk_scrolled_window_new(NULL, NULL); | |
358 | |
359 ref_button = picture_button(parent, _("Refresh"), refresh_xpm); | |
360 add_button = picture_button(parent, _("Add"), gnome_add_xpm); | |
361 rem_button = picture_button(parent, _("Remove"), gnome_remove_xpm); | |
362 gtk_widget_show(list1); | |
363 gtk_widget_show(sw1); | |
364 gtk_widget_show(list2); | |
365 gtk_widget_show(sw2); | |
366 | |
367 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(sw1), list1); | |
368 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(sw2), list2); | |
369 | |
370 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw1), | |
371 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS); | |
372 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw2), | |
373 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS); | |
374 | |
375 cp->list1 = list1; | |
376 cp->list2 = list2; | |
377 | |
378 gtk_signal_connect(GTK_OBJECT(ref_button), "clicked", GTK_SIGNAL_FUNC(refresh_list), cp); | |
379 gtk_signal_connect(GTK_OBJECT(rem_button), "clicked", GTK_SIGNAL_FUNC(remove_chat), cp); | |
380 gtk_signal_connect(GTK_OBJECT(add_button), "clicked", GTK_SIGNAL_FUNC(add_chat), cp); | |
381 | |
382 | |
383 | |
384 label = gtk_label_new(_("List of available chats")); | |
385 gtk_widget_show(label); | |
386 | |
387 gtk_table_attach(GTK_TABLE(table), label, 0, 1, 0, 1, GTK_SHRINK, GTK_SHRINK, 0, 0); | |
388 gtk_table_attach(GTK_TABLE(table), ref_button, 0, 1, 1, 2, GTK_SHRINK, GTK_SHRINK, 0, 0); | |
389 gtk_table_attach(GTK_TABLE(table), sw1, 0, 1, 2, 3, | |
390 GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 5, 5); | |
391 gtk_table_attach(GTK_TABLE(table), add_button, 0, 1, 3, 4, GTK_SHRINK, GTK_SHRINK, 0, 0); | |
392 | |
393 | |
394 label = gtk_label_new(_("List of subscribed chats")); | |
395 gtk_widget_show(label); | |
396 | |
397 gtk_table_attach(GTK_TABLE(table), label, 1, 2, 0, 1, GTK_SHRINK, GTK_SHRINK, 0, 0); | |
398 gtk_table_attach(GTK_TABLE(table), sw2, 1, 2, 2, 3, | |
399 GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 5, 5); | |
400 gtk_table_attach(GTK_TABLE(table), rem_button, 1, 2, 3, 4, GTK_SHRINK, GTK_SHRINK, 0, 0); | |
401 | |
402 | |
403 item = gtk_list_item_new_with_label(_("Gaim Chat")); | |
404 cr = g_new0(struct chat_room, 1); | |
405 strcpy(cr->name, _("Gaim Chat")); | |
406 cr->exchange = 4; | |
407 gtk_object_set_user_data(GTK_OBJECT(item), cr); | |
408 gtk_widget_show(item); | |
409 gtk_list_append_items(GTK_LIST(list1), g_list_append(NULL, item)); | |
410 | |
411 | |
412 while (crs) { | |
413 cr = (struct chat_room *)crs->data; | |
414 item = gtk_list_item_new_with_label(cr->name); | |
415 gtk_object_set_user_data(GTK_OBJECT(item), cr); | |
416 gtk_widget_show(item); | |
417 items = g_list_append(items, item); | |
418 | |
419 crs = crs->next; | |
420 } | |
421 | |
422 gtk_list_append_items(GTK_LIST(list2), items); | |
423 | |
424 gtk_widget_show(parent); | |
425 } | |
426 | |
427 static void handle_signon(struct gaim_connection *gc) | |
428 { | |
429 setup_buddy_chats(); | |
430 } | |
431 | |
432 char *gaim_plugin_init(GModule *m) | |
433 { | |
434 restore_chat_prefs(); | |
435 gaim_signal_connect(m, event_signon, handle_signon, NULL); | |
436 return NULL; | |
437 } | |
438 | |
439 void gaim_plugin_remove() | |
440 { | |
441 if (parent) | |
442 gtk_widget_destroy(parent); | |
443 parent = NULL; | |
444 | |
445 if (item) | |
446 gtk_tree_remove_item(GTK_TREE(buddies), item); | |
447 item = NULL; | |
448 | |
449 while (chat_rooms) { | |
450 g_free(chat_rooms->data); | |
451 chat_rooms = g_list_remove(chat_rooms, chat_rooms->data); | |
452 } | |
453 | |
454 if (cp) | |
455 g_free(cp); | |
456 cp = NULL; | |
457 } | |
458 | |
459 char *name() | |
460 { | |
461 return "Chat List"; | |
462 } | |
463 | |
464 char *description() | |
465 { | |
466 return "Allows you to add chat rooms to your buddy list. Click the configure button to choose" | |
467 " which rooms."; | |
468 } |