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