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