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