Mercurial > pidgin
annotate plugins/chatlist.c @ 3608:7703b2689791
[gaim-migrate @ 3721]
Thanks, Chip--this is just temporary--but you get nice Pangonated
anti-aliased fonts. Whee! Such fun.
committer: Tailor Script <tailor@pidgin.im>
author | Sean Egan <seanegan@gmail.com> |
---|---|
date | Tue, 08 Oct 2002 23:45:20 +0000 |
parents | 154c4a9d9b6d |
children | 9682c0e022c6 |
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 | |
3565 | 296 GtkWidget *gaim_plugin_config_gtk() |
2241 | 297 { |
3565 | 298 GtkWidget *ret, *vbox; |
2241 | 299 GtkWidget *list1, *list2; |
300 GtkWidget *sw1, *sw2; | |
3565 | 301 GtkWidget *ref_button, *add_button, *rem_button; |
302 GtkWidget *table, *label; | |
303 struct chat_room *cr = NULL; | |
2241 | 304 GList *items = NULL; |
3565 | 305 GList *crs = chat_rooms; |
306 | |
2241 | 307 if (cp) |
3565 | 308 g_free(cp); |
309 cp = g_new0(struct chat_page, 1); | |
2241 | 310 |
311 | |
3565 | 312 ret = gtk_vbox_new(FALSE, 18); |
313 gtk_container_set_border_width (GTK_CONTAINER (ret), 12); | |
2241 | 314 |
3565 | 315 vbox = make_frame(ret, _("Chat Rooms")); |
2241 | 316 |
317 table = gtk_table_new(4, 2, FALSE); | |
318 gtk_widget_show(table); | |
319 | |
3565 | 320 gtk_box_pack_start(GTK_BOX(vbox), table, TRUE, TRUE, 0); |
2241 | 321 |
322 list1 = gtk_list_new(); | |
323 list2 = gtk_list_new(); | |
324 sw1 = gtk_scrolled_window_new(NULL, NULL); | |
325 sw2 = gtk_scrolled_window_new(NULL, NULL); | |
326 | |
3565 | 327 ref_button = picture_button(prefs, _("Refresh"), refresh_xpm); |
328 add_button = picture_button(prefs, _("Add"), gnome_add_xpm); | |
329 rem_button = picture_button(prefs, _("Remove"), gnome_remove_xpm); | |
2241 | 330 gtk_widget_show(list1); |
331 gtk_widget_show(sw1); | |
332 gtk_widget_show(list2); | |
333 gtk_widget_show(sw2); | |
334 | |
335 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(sw1), list1); | |
336 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(sw2), list2); | |
337 | |
338 cp->list1 = list1; | |
339 cp->list2 = list2; | |
340 | |
341 gtk_signal_connect(GTK_OBJECT(ref_button), "clicked", GTK_SIGNAL_FUNC(refresh_list), cp); | |
342 gtk_signal_connect(GTK_OBJECT(rem_button), "clicked", GTK_SIGNAL_FUNC(remove_chat), cp); | |
343 gtk_signal_connect(GTK_OBJECT(add_button), "clicked", GTK_SIGNAL_FUNC(add_chat), cp); | |
344 | |
345 label = gtk_label_new(_("List of available chats")); | |
346 gtk_widget_show(label); | |
347 | |
348 gtk_table_attach(GTK_TABLE(table), label, 0, 1, 0, 1, GTK_SHRINK, GTK_SHRINK, 0, 0); | |
349 gtk_table_attach(GTK_TABLE(table), ref_button, 0, 1, 1, 2, GTK_SHRINK, GTK_SHRINK, 0, 0); | |
350 gtk_table_attach(GTK_TABLE(table), sw1, 0, 1, 2, 3, | |
351 GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 5, 5); | |
352 gtk_table_attach(GTK_TABLE(table), add_button, 0, 1, 3, 4, GTK_SHRINK, GTK_SHRINK, 0, 0); | |
353 | |
354 | |
355 label = gtk_label_new(_("List of subscribed chats")); | |
356 gtk_widget_show(label); | |
357 | |
358 gtk_table_attach(GTK_TABLE(table), label, 1, 2, 0, 1, GTK_SHRINK, GTK_SHRINK, 0, 0); | |
359 gtk_table_attach(GTK_TABLE(table), sw2, 1, 2, 2, 3, | |
360 GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 5, 5); | |
361 gtk_table_attach(GTK_TABLE(table), rem_button, 1, 2, 3, 4, GTK_SHRINK, GTK_SHRINK, 0, 0); | |
362 | |
363 | |
364 item = gtk_list_item_new_with_label(_("Gaim Chat")); | |
365 cr = g_new0(struct chat_room, 1); | |
366 strcpy(cr->name, _("Gaim Chat")); | |
367 cr->exchange = 4; | |
368 gtk_object_set_user_data(GTK_OBJECT(item), cr); | |
369 gtk_widget_show(item); | |
370 gtk_list_append_items(GTK_LIST(list1), g_list_append(NULL, item)); | |
371 | |
372 | |
373 while (crs) { | |
374 cr = (struct chat_room *)crs->data; | |
375 item = gtk_list_item_new_with_label(cr->name); | |
376 gtk_object_set_user_data(GTK_OBJECT(item), cr); | |
377 gtk_widget_show(item); | |
378 items = g_list_append(items, item); | |
379 | |
380 crs = crs->next; | |
381 } | |
382 | |
383 gtk_list_append_items(GTK_LIST(list2), items); | |
3565 | 384 gtk_widget_show_all(ret); |
385 return ret; | |
2241 | 386 } |
387 | |
388 static void handle_signon(struct gaim_connection *gc) | |
389 { | |
390 setup_buddy_chats(); | |
391 } | |
392 | |
393 char *gaim_plugin_init(GModule *m) | |
394 { | |
395 restore_chat_prefs(); | |
396 gaim_signal_connect(m, event_signon, handle_signon, NULL); | |
397 return NULL; | |
398 } | |
399 | |
400 void gaim_plugin_remove() | |
401 { | |
402 if (parent) | |
403 gtk_widget_destroy(parent); | |
404 parent = NULL; | |
405 | |
406 if (item) | |
407 gtk_tree_remove_item(GTK_TREE(buddies), item); | |
408 item = NULL; | |
409 | |
410 while (chat_rooms) { | |
411 g_free(chat_rooms->data); | |
412 chat_rooms = g_list_remove(chat_rooms, chat_rooms->data); | |
413 } | |
414 | |
415 if (cp) | |
416 g_free(cp); | |
417 cp = NULL; | |
418 } | |
419 | |
3551 | 420 struct gaim_plugin_description desc; |
421 struct gaim_plugin_description *gaim_plugin_desc() { | |
422 desc.api_version = PLUGIN_API_VERSION; | |
423 desc.name = g_strdup("Chat List"); | |
424 desc.version = g_strdup(VERSION); | |
425 desc.description = g_strdup("Allows you to add chat rooms to your buddy list."); | |
426 desc.authors = g_strdup("Eric Warmenhoven <eric@warmenhoven.org>"); | |
427 desc.url = g_strdup(WEBSITE); | |
428 return &desc; | |
429 } | |
430 | |
2241 | 431 char *name() |
432 { | |
433 return "Chat List"; | |
434 } | |
435 | |
436 char *description() | |
437 { | |
438 return "Allows you to add chat rooms to your buddy list. Click the configure button to choose" | |
439 " which rooms."; | |
440 } |