Thu, 15 Jan 2004 22:20:29 +0000
[gaim-migrate @ 8817]
Tim 'marv' Ringenbach wrote us a wonderful core/ui split chat room list
thing-a-ma-jig. I've taken the liberty of adding jabber support to it
as well.
committer: Nathan Walp <nwalp@pidgin.im>
| 8113 | 1 | /** |
| 2 | * @file gtkroomlist.c Gtk Room List UI | |
| 3 | * @ingroup gtkui | |
| 4 | * | |
| 5 | * gaim | |
| 6 | * | |
| 7 | * Copyright (C) 2003, Timothy Ringenbach <omarvo@hotmail.com> | |
| 8 | * | |
| 9 | * This program is free software; you can redistribute it and/or modify | |
| 10 | * it under the terms of the GNU General Public License as published by | |
| 11 | * the Free Software Foundation; either version 2 of the License, or | |
| 12 | * (at your option) any later version. | |
| 13 | * | |
| 14 | * This program is distributed in the hope that it will be useful, | |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 17 | * GNU General Public License for more details. | |
| 18 | * | |
| 19 | * You should have received a copy of the GNU General Public License | |
| 20 | * along with this program; if not, write to the Free Software | |
| 21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 22 | */ | |
| 23 | ||
| 24 | #include "gtkinternal.h" | |
| 25 | #include "gtkutils.h" | |
| 26 | #include "stock.h" | |
| 27 | #include "debug.h" | |
| 28 | #include "account.h" | |
| 29 | #include "connection.h" | |
| 30 | #include "notify.h" | |
| 31 | ||
| 32 | #include "gtkroomlist.h" | |
| 33 | ||
| 34 | typedef struct _GaimGtkRoomlist { | |
| 35 | GaimGtkRoomlistDialog *dialog; | |
| 36 | GtkTreeStore *model; | |
| 37 | GtkWidget *tree; | |
| 38 | GHashTable *cats; /**< Meow. */ | |
| 39 | gint num_rooms, total_rooms; | |
| 40 | } GaimGtkRoomlist; | |
| 41 | ||
| 42 | struct _GaimGtkRoomlistDialog { | |
| 43 | GtkWidget *window; | |
| 44 | GtkWidget *account_widget; | |
| 45 | GtkWidget *progress; | |
| 46 | GtkWidget *sw; | |
| 47 | ||
| 48 | GtkWidget *list_button; | |
| 49 | GtkWidget *stop_button; | |
| 50 | GtkWidget *close_button; | |
| 51 | ||
| 52 | GaimAccount *account; | |
| 53 | GaimRoomlist *roomlist; | |
| 54 | ||
| 55 | }; | |
| 56 | ||
| 57 | enum { | |
| 58 | NAME_COLUMN = 0, | |
| 59 | ROOM_COLUMN, | |
| 60 | NUM_OF_COLUMNS, | |
| 61 | }; | |
| 62 | ||
| 63 | static GList *roomlists = NULL; | |
| 64 | ||
| 65 | ||
| 66 | ||
| 67 | static gint delete_win_cb(GtkWidget *w, GdkEventAny *e, gpointer d) | |
| 68 | { | |
| 69 | GaimGtkRoomlistDialog *dialog; | |
| 70 | ||
| 71 | dialog = (GaimGtkRoomlistDialog *) d; | |
| 72 | ||
| 73 | /* free stuff here */ | |
| 74 | if (dialog->roomlist) | |
| 75 | gaim_roomlist_unref(dialog->roomlist); | |
| 76 | g_free(dialog); | |
| 77 | ||
| 78 | return FALSE; | |
| 79 | } | |
| 80 | ||
| 81 | static void dialog_select_account_cb(GObject *w, GaimAccount *account, | |
| 82 | GaimGtkRoomlistDialog *dialog) | |
| 83 | { | |
| 84 | dialog->account = account; | |
| 85 | } | |
| 86 | ||
| 87 | static void list_button_cb(GtkButton *button, GaimGtkRoomlistDialog *dialog) | |
| 88 | { | |
| 89 | GaimConnection *gc; | |
| 90 | GaimGtkRoomlist *rl; | |
| 91 | ||
| 92 | gc = gaim_account_get_connection(dialog->account); | |
| 93 | if (!gc) | |
| 94 | return; | |
| 95 | ||
| 96 | dialog->roomlist = gaim_roomlist_get_list(gc); | |
| 97 | gaim_roomlist_ref(dialog->roomlist); | |
| 98 | rl = dialog->roomlist->ui_data; | |
| 99 | rl->dialog = dialog; | |
| 100 | if (dialog->account_widget) | |
| 101 | gtk_widget_set_sensitive(dialog->account_widget, FALSE); | |
| 102 | gtk_widget_set_sensitive(dialog->list_button, FALSE); | |
| 103 | gtk_container_add(GTK_CONTAINER(dialog->sw), rl->tree); /* XXX */ | |
| 104 | } | |
| 105 | ||
| 106 | static void stop_button_cb(GtkButton *button, GaimGtkRoomlistDialog *dialog) | |
| 107 | { | |
| 108 | gaim_roomlist_cancel_get_list(dialog->roomlist); | |
| 109 | gtk_widget_set_sensitive(GTK_WIDGET(button), FALSE); | |
| 110 | } | |
| 111 | ||
| 112 | static void close_button_cb(GtkButton *button, GaimGtkRoomlistDialog *dialog) | |
| 113 | { | |
| 114 | GtkWidget *window = dialog->window; | |
| 115 | ||
| 116 | delete_win_cb(NULL, NULL, dialog); | |
| 117 | gtk_widget_destroy(window); | |
| 118 | } | |
| 119 | ||
| 120 | struct _menu_cb_info { | |
| 121 | GaimRoomlist *list; | |
| 122 | GaimRoomlistRoom *room; | |
| 123 | }; | |
| 124 | ||
| 125 | static void do_join_cb(GtkWidget *w, struct _menu_cb_info *info) | |
| 126 | { | |
| 127 | GHashTable *components; | |
| 128 | GList *l, *j; | |
| 129 | GaimConnection *gc; | |
| 130 | ||
| 131 | gc = gaim_account_get_connection(info->list->account); | |
| 132 | if (!gc) | |
| 133 | return; | |
| 134 | ||
| 135 | components = g_hash_table_new(g_str_hash, g_str_equal); | |
| 136 | ||
| 137 | g_hash_table_replace(components, g_strdup("name"), g_strdup(info->room->name)); | |
| 138 | for (l = info->list->fields, j = info->room->fields; l && j; l = l->next, j = j->next) { | |
| 139 | GaimRoomlistField *f = l->data; | |
| 140 | ||
| 141 | g_hash_table_replace(components, f->name, j->data); | |
| 142 | } | |
| 143 | ||
| 144 | serv_join_chat(gc, components); | |
| 145 | ||
| 146 | g_hash_table_destroy(components); | |
| 147 | } | |
| 148 | ||
| 149 | static void row_activated_cb(GtkTreeView *tv, GtkTreePath *path, GtkTreeViewColumn *arg2, | |
| 150 | GaimRoomlist *list) | |
| 151 | { | |
| 152 | GaimGtkRoomlist *grl = list->ui_data; | |
| 153 | GtkTreeIter iter; | |
| 154 | GaimRoomlistRoom *room; | |
| 155 | GValue val = { 0, }; | |
| 156 | struct _menu_cb_info info; | |
| 157 | ||
| 158 | gtk_tree_model_get_iter(GTK_TREE_MODEL(grl->model), &iter, path); | |
| 159 | gtk_tree_model_get_value(GTK_TREE_MODEL(grl->model), &iter, ROOM_COLUMN, &val); | |
| 160 | room = g_value_get_pointer(&val); | |
| 161 | if (!room || !(room->type & GAIM_ROOMLIST_ROOMTYPE_ROOM)) | |
| 162 | return; | |
| 163 | ||
| 164 | info.list = list; | |
| 165 | info.room = room; | |
| 166 | ||
| 167 | do_join_cb(GTK_WIDGET(tv), &info); | |
| 168 | } | |
| 169 | ||
| 170 | static gboolean room_click_cb(GtkWidget *tv, GdkEventButton *event, GaimRoomlist *list) | |
| 171 | { | |
| 172 | GtkTreePath *path; | |
| 173 | GaimGtkRoomlist *grl = list->ui_data; | |
| 174 | GValue val = { 0, }; | |
| 175 | GaimRoomlistRoom *room; | |
| 176 | GtkTreeIter iter; | |
| 177 | GtkWidget *menu; | |
| 178 | static struct _menu_cb_info info; /* XXX? */ | |
| 179 | ||
| 180 | if (event->button != 3 || event->type != GDK_BUTTON_PRESS) | |
| 181 | return FALSE; | |
| 182 | ||
| 183 | /* Here we figure out which room was clicked */ | |
| 184 | if (!gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(tv), event->x, event->y, &path, NULL, NULL, NULL)) | |
| 185 | return FALSE; | |
| 186 | gtk_tree_model_get_iter(GTK_TREE_MODEL(grl->model), &iter, path); | |
| 187 | gtk_tree_path_free(path); | |
| 188 | gtk_tree_model_get_value (GTK_TREE_MODEL(grl->model), &iter, ROOM_COLUMN, &val); | |
| 189 | room = g_value_get_pointer(&val); | |
| 190 | ||
| 191 | if (!room || !(room->type & GAIM_ROOMLIST_ROOMTYPE_ROOM)) | |
| 192 | return FALSE; | |
| 193 | ||
| 194 | info.list = list; | |
| 195 | info.room = room; | |
| 196 | ||
| 197 | ||
| 198 | menu = gtk_menu_new(); | |
| 199 | gaim_new_item_from_stock(menu, _("_Join"), GAIM_STOCK_CHAT, | |
| 200 | G_CALLBACK(do_join_cb), &info, 0, 0, NULL); | |
| 201 | ||
| 202 | ||
| 203 | gtk_widget_show_all(menu); | |
| 204 | gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL, 3, event->time); | |
| 205 | ||
| 206 | return FALSE; | |
| 207 | } | |
| 208 | ||
| 209 | static void row_expanded_cb(GtkTreeView *treeview, GtkTreeIter *arg1, GtkTreePath *arg2, gpointer user_data) | |
| 210 | { | |
| 211 | GaimRoomlist *list = user_data; | |
| 212 | GaimRoomlistRoom *catagory; | |
| 213 | GValue val = { 0, }; | |
| 214 | ||
| 215 | gtk_tree_model_get_value(gtk_tree_view_get_model(treeview), arg1, ROOM_COLUMN, &val); | |
| 216 | catagory = g_value_get_pointer(&val); | |
| 217 | ||
| 218 | if (!catagory->expanded_once) { | |
| 219 | gaim_roomlist_expand_catagory(list, catagory); | |
| 220 | catagory->expanded_once = TRUE; | |
| 221 | } | |
| 222 | } | |
| 223 | ||
| 224 | static gboolean accounts_filter_func(GaimAccount *account) | |
| 225 | { | |
| 226 | GaimConnection *gc; | |
| 227 | ||
| 228 | gc = gaim_account_get_connection(account); | |
| 229 | if (!gc) | |
| 230 | return FALSE; | |
| 231 | return gaim_roomlist_is_possible(gc); | |
| 232 | } | |
| 233 | ||
| 234 | GaimGtkRoomlistDialog *gaim_gtk_roomlist_dialog_new_with_account(GaimAccount *account) | |
| 235 | { | |
| 236 | GaimGtkRoomlistDialog *dialog; | |
| 237 | GtkWidget *window; | |
| 238 | GtkWidget *vbox1, *vbox2; | |
| 239 | GtkWidget *account_hbox; | |
| 240 | GtkWidget *bbox; | |
| 241 | GtkWidget *label; | |
| 242 | GtkWidget *button; | |
| 243 | GaimAccount *first_account = NULL; | |
| 244 | ||
| 245 | if (!account) { | |
| 246 | GList *c; | |
| 247 | GaimConnection *gc; | |
| 248 | ||
| 249 | for (c = gaim_connections_get_all(); c != NULL; c = c->next) { | |
| 250 | gc = c->data; | |
| 251 | ||
| 252 | if (gaim_roomlist_is_possible(gc)) { | |
| 253 | first_account = gaim_connection_get_account(gc); | |
| 254 | break; | |
| 255 | } | |
| 256 | } | |
| 257 | ||
| 258 | if (first_account == NULL) { | |
| 259 | gaim_notify_error(NULL, NULL, | |
| 260 | _("You are not currently signed on with any " | |
| 261 | "protocols that have the ability to list rooms."), | |
| 262 | NULL); | |
| 263 | ||
| 264 | return NULL; | |
| 265 | } | |
| 266 | } | |
| 267 | ||
| 268 | dialog = g_new0(GaimGtkRoomlistDialog, 1); | |
| 269 | ||
| 270 | /* Create the window. */ | |
| 271 | dialog->window = window = gtk_window_new(GTK_WINDOW_TOPLEVEL); | |
| 272 | gtk_window_set_role(GTK_WINDOW(window), "room list"); | |
| 273 | gtk_window_set_title(GTK_WINDOW(window), _("Room List")); | |
| 274 | ||
| 275 | gtk_container_set_border_width(GTK_CONTAINER(window), 12); | |
| 276 | gtk_widget_realize(window); | |
| 277 | ||
| 278 | g_signal_connect(G_OBJECT(window), "delete_event", | |
| 279 | G_CALLBACK(delete_win_cb), dialog); | |
| 280 | ||
| 281 | /* Create the parent vbox for everything. */ | |
| 282 | vbox1 = gtk_vbox_new(FALSE, 12); | |
| 283 | gtk_container_add(GTK_CONTAINER(window), vbox1); | |
| 284 | gtk_widget_show(vbox1); | |
| 285 | ||
| 286 | /* Create the main vbox for top half of the window. */ | |
| 287 | vbox2 = gtk_vbox_new(FALSE, 6); | |
| 288 | gtk_box_pack_start(GTK_BOX(vbox1), vbox2, FALSE, FALSE, 0); | |
| 289 | gtk_widget_show(vbox2); | |
| 290 | ||
| 291 | account_hbox = gtk_hbox_new(FALSE, 0); | |
| 292 | gtk_box_pack_start(GTK_BOX(vbox2), account_hbox, TRUE, TRUE, 0); | |
| 293 | gtk_widget_show(account_hbox); | |
| 294 | ||
| 295 | /* accounts dropdown list */ | |
| 296 | if (!account) { | |
| 297 | dialog->account = first_account; | |
| 298 | label = gtk_label_new(NULL); | |
| 299 | gtk_box_pack_start(GTK_BOX(account_hbox), label, TRUE, TRUE, 0); | |
| 300 | gtk_label_set_markup_with_mnemonic(GTK_LABEL(label), _("_Account:")); | |
| 301 | gtk_misc_set_alignment(GTK_MISC(label), 0, 0); | |
| 302 | ||
| 303 | ||
| 304 | dialog->account_widget = gaim_gtk_account_option_menu_new(first_account, FALSE, | |
| 305 | G_CALLBACK(dialog_select_account_cb), accounts_filter_func, dialog); | |
| 306 | ||
| 307 | gtk_box_pack_start(GTK_BOX(account_hbox), dialog->account_widget, TRUE, TRUE, 0); | |
| 308 | gtk_label_set_mnemonic_widget(GTK_LABEL(label), GTK_WIDGET(dialog->account_widget)); | |
| 309 | gtk_widget_show(label); | |
| 310 | gtk_widget_show(dialog->account_widget); | |
| 311 | } else { | |
| 312 | dialog->account = account; | |
| 313 | } | |
| 314 | ||
| 315 | ||
| 316 | /* Now the button box for the buttons */ | |
| 317 | bbox = gtk_hbutton_box_new(); | |
| 318 | gtk_box_set_spacing(GTK_BOX(bbox), 6); | |
| 319 | gtk_button_box_set_layout(GTK_BUTTON_BOX(bbox), GTK_BUTTONBOX_EDGE); | |
| 320 | gtk_box_pack_start(GTK_BOX(vbox2), bbox, FALSE, TRUE, 0); | |
| 321 | gtk_widget_show(bbox); | |
| 322 | ||
| 323 | /* Get list button */ | |
| 324 | button = gtk_button_new_with_mnemonic(_("Get _list")); | |
| 325 | gtk_box_pack_start(GTK_BOX(bbox), button, FALSE, FALSE, 0); | |
| 326 | gtk_widget_show(button); | |
| 327 | dialog->list_button = button; | |
| 328 | ||
| 329 | g_signal_connect(G_OBJECT(button), "clicked", | |
| 330 | G_CALLBACK(list_button_cb), dialog); | |
| 331 | /* Stop button */ | |
| 332 | button = gtk_button_new_from_stock(GTK_STOCK_STOP); | |
| 333 | gtk_box_pack_start(GTK_BOX(bbox), button, FALSE, FALSE, 0); | |
| 334 | gtk_widget_show(button); | |
| 335 | gtk_widget_set_sensitive(button, FALSE); | |
| 336 | dialog->stop_button = button; | |
| 337 | ||
| 338 | g_signal_connect(G_OBJECT(button), "clicked", | |
| 339 | G_CALLBACK(stop_button_cb), dialog); | |
| 340 | /* Close button */ | |
| 341 | button = gtk_button_new_from_stock(GTK_STOCK_CLOSE); | |
| 342 | gtk_box_pack_start(GTK_BOX(bbox), button, FALSE, FALSE, 0); | |
| 343 | gtk_widget_show(button); | |
| 344 | dialog->close_button = button; | |
| 345 | ||
| 346 | g_signal_connect(G_OBJECT(button), "clicked", | |
| 347 | G_CALLBACK(close_button_cb), dialog); | |
| 348 | ||
| 349 | /* The pusling dilly */ | |
| 350 | dialog->progress = gtk_progress_bar_new(); | |
| 351 | gtk_progress_bar_set_pulse_step(GTK_PROGRESS_BAR(dialog->progress), 0.1); | |
| 352 | gtk_progress_bar_set_text(GTK_PROGRESS_BAR(dialog->progress), | |
| 353 | " "); | |
| 354 | gtk_box_pack_start(GTK_BOX(vbox2), dialog->progress, TRUE, TRUE, 0); | |
| 355 | gtk_widget_show(dialog->progress); | |
| 356 | ||
| 357 | ||
| 358 | gtk_widget_show(dialog->window); | |
| 359 | ||
| 360 | dialog->sw = gtk_scrolled_window_new(NULL, NULL); | |
| 361 | gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(dialog->sw), | |
| 362 | GTK_SHADOW_IN); | |
| 363 | gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(dialog->sw), | |
| 364 | GTK_POLICY_AUTOMATIC, | |
| 365 | GTK_POLICY_AUTOMATIC); | |
| 366 | gtk_box_pack_start(GTK_BOX(vbox1), dialog->sw, TRUE, TRUE, 0); | |
| 367 | gtk_widget_show(dialog->sw); | |
| 368 | ||
| 369 | return dialog; | |
| 370 | } | |
| 371 | ||
| 372 | GaimGtkRoomlistDialog *gaim_gtk_roomlist_dialog_new(void) | |
| 373 | { | |
| 374 | return gaim_gtk_roomlist_dialog_new_with_account(NULL); | |
| 375 | } | |
| 376 | ||
| 377 | void gaim_gtk_roomlist_dialog_show(void) | |
| 378 | { | |
| 379 | gaim_gtk_roomlist_dialog_new(); | |
| 380 | } | |
| 381 | ||
| 382 | static void gaim_gtk_roomlist_new(GaimRoomlist *list) | |
| 383 | { | |
| 384 | GaimGtkRoomlist *rl; | |
| 385 | ||
| 386 | rl = g_new0(GaimGtkRoomlist, 1); | |
| 387 | ||
| 388 | list->ui_data = rl; | |
| 389 | ||
| 390 | rl->cats = g_hash_table_new_full(NULL, NULL, NULL, (GDestroyNotify)gtk_tree_row_reference_free); | |
| 391 | ||
| 392 | roomlists = g_list_append(roomlists, list); | |
| 393 | } | |
| 394 | ||
| 395 | static void int_cell_data_func(GtkTreeViewColumn *col, GtkCellRenderer *renderer, | |
| 396 | GtkTreeModel *model, GtkTreeIter *iter, gpointer user_data) | |
| 397 | { | |
| 398 | gchar buf[16]; | |
| 399 | int myint; | |
| 400 | ||
| 401 | gtk_tree_model_get(model, iter, GPOINTER_TO_INT(user_data), &myint, -1); | |
| 402 | ||
| 403 | if (myint) | |
| 404 | g_snprintf(buf, sizeof(buf), "%d", myint); | |
| 405 | else | |
| 406 | buf[0] = '\0'; | |
| 407 | ||
| 408 | g_object_set(renderer, "text", buf, NULL); | |
| 409 | } | |
| 410 | ||
| 411 | /* this sorts backwards on purpose, so that clicking name sorts a-z, while clicking users sorts | |
| 412 | infinity-0. you can still click again to reverse it on any of them. */ | |
| 413 | static gint int_sort_func(GtkTreeModel *model, GtkTreeIter *a, GtkTreeIter *b, gpointer user_data) | |
| 414 | { | |
| 415 | int c, d; | |
| 416 | ||
| 417 | c = d = 0; | |
| 418 | ||
| 419 | gtk_tree_model_get(model, a, GPOINTER_TO_INT(user_data), &c, -1); | |
| 420 | gtk_tree_model_get(model, b, GPOINTER_TO_INT(user_data), &d, -1); | |
| 421 | ||
| 422 | if (c == d) | |
| 423 | return 0; | |
| 424 | else if (c > d) | |
| 425 | return -1; | |
| 426 | else | |
| 427 | return 1; | |
| 428 | } | |
| 429 | ||
| 430 | static void gaim_gtk_roomlist_set_fields(GaimRoomlist *list, GList *fields) | |
| 431 | { | |
| 432 | GaimGtkRoomlist *grl = list->ui_data; | |
| 433 | gint columns = NUM_OF_COLUMNS; | |
| 434 | int j; | |
| 435 | GtkTreeStore *model; | |
| 436 | GtkWidget *tree; | |
| 437 | GtkCellRenderer *renderer; | |
| 438 | GtkTreeViewColumn *column; | |
| 439 | GList *l; | |
| 440 | GType *types; | |
| 441 | ||
| 442 | g_return_if_fail(grl != NULL); | |
| 443 | ||
| 444 | columns += g_list_length(fields); | |
| 445 | types = g_new(GType, columns); | |
| 446 | ||
| 447 | types[NAME_COLUMN] = G_TYPE_STRING; | |
| 448 | types[ROOM_COLUMN] = G_TYPE_POINTER; | |
| 449 | ||
| 450 | for (j = NUM_OF_COLUMNS, l = fields; l; l = l->next, j++) { | |
| 451 | GaimRoomlistField *f = l->data; | |
| 452 | ||
| 453 | switch (f->type) { | |
| 454 | case GAIM_ROOMLIST_FIELD_BOOL: | |
| 455 | types[j] = G_TYPE_BOOLEAN; | |
| 456 | break; | |
| 457 | case GAIM_ROOMLIST_FIELD_INT: | |
| 458 | types[j] = G_TYPE_INT; | |
| 459 | break; | |
| 460 | case GAIM_ROOMLIST_FIELD_STRING: | |
| 461 | types[j] = G_TYPE_STRING; | |
| 462 | break; | |
| 463 | } | |
| 464 | } | |
| 465 | ||
| 466 | model = gtk_tree_store_newv(columns, types); | |
| 467 | g_free(types); | |
| 468 | ||
| 469 | tree = gtk_tree_view_new_with_model(GTK_TREE_MODEL(model)); | |
| 470 | gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(tree), TRUE); | |
| 471 | ||
| 472 | g_object_unref(model); | |
| 473 | ||
| 474 | grl->model = model; | |
| 475 | grl->tree = tree; | |
| 476 | gtk_widget_show(grl->tree); | |
| 477 | ||
| 478 | renderer = gtk_cell_renderer_text_new(); | |
| 479 | column = gtk_tree_view_column_new_with_attributes(_("Name"), renderer, | |
| 480 | "text", NAME_COLUMN, NULL); | |
| 481 | gtk_tree_view_column_set_sizing(GTK_TREE_VIEW_COLUMN(column), | |
| 482 | GTK_TREE_VIEW_COLUMN_GROW_ONLY); | |
| 483 | gtk_tree_view_column_set_resizable(GTK_TREE_VIEW_COLUMN(column), TRUE); | |
| 484 | gtk_tree_view_column_set_sort_column_id(GTK_TREE_VIEW_COLUMN(column), NAME_COLUMN); | |
| 485 | gtk_tree_view_column_set_reorderable(GTK_TREE_VIEW_COLUMN(column), TRUE); | |
| 486 | gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column); | |
| 487 | ||
| 488 | for (j = NUM_OF_COLUMNS, l = fields; l; l = l->next, j++) { | |
| 489 | GaimRoomlistField *f = l->data; | |
| 490 | ||
| 491 | if (f->hidden) | |
| 492 | continue; | |
| 493 | ||
| 494 | renderer = gtk_cell_renderer_text_new(); | |
| 495 | column = gtk_tree_view_column_new_with_attributes(f->label, renderer, | |
| 496 | "text", j, NULL); | |
| 497 | gtk_tree_view_column_set_sizing(GTK_TREE_VIEW_COLUMN(column), | |
| 498 | GTK_TREE_VIEW_COLUMN_GROW_ONLY); | |
| 499 | gtk_tree_view_column_set_resizable(GTK_TREE_VIEW_COLUMN(column), TRUE); | |
| 500 | gtk_tree_view_column_set_sort_column_id(GTK_TREE_VIEW_COLUMN(column), j); | |
| 501 | gtk_tree_view_column_set_reorderable(GTK_TREE_VIEW_COLUMN(column), TRUE); | |
| 502 | if (f->type == GAIM_ROOMLIST_FIELD_INT) { | |
| 503 | gtk_tree_view_column_set_cell_data_func(column, renderer, int_cell_data_func, | |
| 504 | GINT_TO_POINTER(j), NULL); | |
| 505 | gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(model), j, int_sort_func, | |
| 506 | GINT_TO_POINTER(j), NULL); | |
| 507 | } | |
| 508 | gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column); | |
| 509 | } | |
| 510 | ||
| 511 | g_signal_connect(G_OBJECT(tree), "button-press-event", G_CALLBACK(room_click_cb), list); | |
| 512 | g_signal_connect(G_OBJECT(tree), "row-expanded", G_CALLBACK(row_expanded_cb), list); | |
| 513 | g_signal_connect(G_OBJECT(tree), "row-activated", G_CALLBACK(row_activated_cb), list); | |
| 514 | /* gtk_container_add(GTK_CONTAINER(grl->sw), tree); */ | |
| 515 | } | |
| 516 | ||
| 517 | static void gaim_gtk_roomlist_add_room(GaimRoomlist *list, GaimRoomlistRoom *room) | |
| 518 | { | |
| 519 | GaimGtkRoomlist *rl= list->ui_data; | |
| 520 | GtkTreeRowReference *rr, *parentrr = NULL; | |
| 521 | GtkTreePath *path; | |
| 522 | GtkTreeIter iter, parent, child; | |
| 523 | GList *l, *k; | |
| 524 | int j; | |
| 525 | gboolean append = TRUE; | |
| 526 | ||
| 527 | rl->total_rooms++; | |
| 528 | if (room->type == GAIM_ROOMLIST_ROOMTYPE_ROOM) | |
| 529 | rl->num_rooms++; | |
| 530 | ||
| 531 | if (rl->dialog) { | |
| 532 | if (rl->total_rooms > 100) | |
| 533 | gtk_progress_bar_set_pulse_step(GTK_PROGRESS_BAR(rl->dialog->progress), | |
| 534 | 0.01); | |
| 535 | gtk_progress_bar_pulse(GTK_PROGRESS_BAR(rl->dialog->progress)); | |
| 536 | } | |
| 537 | if (room->parent) { | |
| 538 | parentrr = g_hash_table_lookup(rl->cats, room->parent); | |
| 539 | path = gtk_tree_row_reference_get_path(parentrr); | |
| 540 | if (path) { | |
| 541 | GaimRoomlistRoom *tmproom = NULL; | |
| 542 | ||
| 543 | gtk_tree_model_get_iter(GTK_TREE_MODEL(rl->model), &parent, path); | |
| 544 | gtk_tree_path_free(path); | |
| 545 | ||
| 546 | if (gtk_tree_model_iter_children(GTK_TREE_MODEL(rl->model), &child, &parent)) { | |
| 547 | gtk_tree_model_get(GTK_TREE_MODEL(rl->model), &child, ROOM_COLUMN, &tmproom, -1); | |
| 548 | if (!tmproom) | |
| 549 | append = FALSE; | |
| 550 | } | |
| 551 | } | |
| 552 | } | |
| 553 | ||
| 554 | if (append) | |
| 555 | gtk_tree_store_append(rl->model, &iter, (parentrr ? &parent : NULL)); | |
| 556 | else | |
| 557 | iter = child; | |
| 558 | ||
| 559 | if (room->type & GAIM_ROOMLIST_ROOMTYPE_CATAGORY) | |
| 560 | gtk_tree_store_append(rl->model, &child, &iter); | |
| 561 | ||
| 562 | path = gtk_tree_model_get_path(GTK_TREE_MODEL(rl->model), &iter); | |
| 563 | ||
| 564 | if (room->type & GAIM_ROOMLIST_ROOMTYPE_CATAGORY) { | |
| 565 | rr = gtk_tree_row_reference_new(GTK_TREE_MODEL(rl->model), path); | |
| 566 | g_hash_table_insert(rl->cats, room, rr); | |
| 567 | } | |
| 568 | ||
| 569 | gtk_tree_path_free(path); | |
| 570 | ||
| 571 | gtk_tree_store_set(rl->model, &iter, NAME_COLUMN, room->name, -1); | |
| 572 | gtk_tree_store_set(rl->model, &iter, ROOM_COLUMN, room, -1); | |
| 573 | ||
| 574 | for (j = NUM_OF_COLUMNS, l = room->fields, k = list->fields; l && k; j++, l = l->next, k = k->next) { | |
| 575 | GaimRoomlistField *f = k->data; | |
| 576 | if (f->hidden) | |
| 577 | continue; | |
| 578 | gtk_tree_store_set(rl->model, &iter, j, l->data, -1); | |
| 579 | } | |
| 580 | } | |
| 581 | ||
| 582 | static void gaim_gtk_roomlist_in_progress(GaimRoomlist *list, gboolean flag) | |
| 583 | { | |
| 584 | GaimGtkRoomlist *rl = list->ui_data; | |
| 585 | ||
| 586 | if (!rl || !rl->dialog) | |
| 587 | return; | |
| 588 | ||
| 589 | gtk_widget_set_sensitive(rl->dialog->stop_button, flag); | |
| 590 | if (flag) { | |
| 591 | gtk_progress_bar_set_text(GTK_PROGRESS_BAR(rl->dialog->progress), | |
| 592 | _("Downloading List...")); | |
| 593 | } else { | |
| 594 | gtk_progress_bar_set_text(GTK_PROGRESS_BAR(rl->dialog->progress), | |
| 595 | " "); | |
| 596 | gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(rl->dialog->progress), 0.0); | |
| 597 | } | |
| 598 | } | |
| 599 | ||
| 600 | static void gaim_gtk_roomlist_destroy(GaimRoomlist *list) | |
| 601 | { | |
| 602 | GaimGtkRoomlist *rl; | |
| 603 | ||
| 604 | roomlists = g_list_remove(roomlists, list); | |
| 605 | ||
| 606 | rl = list->ui_data; | |
| 607 | ||
| 608 | g_return_if_fail(rl != NULL); | |
| 609 | ||
| 610 | g_hash_table_destroy(rl->cats); | |
| 611 | g_free(rl); | |
| 612 | list->ui_data = NULL; | |
| 613 | } | |
| 614 | ||
| 615 | static GaimRoomlistUiOps ops = { | |
| 616 | gaim_gtk_roomlist_new, | |
| 617 | gaim_gtk_roomlist_set_fields, | |
| 618 | gaim_gtk_roomlist_add_room, | |
| 619 | gaim_gtk_roomlist_in_progress, | |
| 620 | gaim_gtk_roomlist_destroy | |
| 621 | }; | |
| 622 | ||
| 623 | ||
| 624 | void gaim_gtk_roomlist_init(void) | |
| 625 | { | |
| 626 | gaim_roomlist_set_ui_ops(&ops); | |
| 627 | } |