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