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