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