| 1 | /* |
| 2 | * This file is part of DisOrder |
| 3 | * Copyright (C) 2008 Richard Kettlewell |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
| 18 | * USA |
| 19 | */ |
| 20 | /** @file disobedience/choose.c |
| 21 | * @brief Hierarchical track selection and search |
| 22 | * |
| 23 | * We now use an ordinary GtkTreeStore/GtkTreeView. |
| 24 | * |
| 25 | * We don't want to pull the entire tree in memory, but we want directories to |
| 26 | * show up as having children. Therefore we give directories a placeholder |
| 27 | * child and replace their children when they are opened. Placeholders have |
| 28 | * TRACK_COLUMN="" and ISFILE_COLUMN=FALSE (so that they don't get check boxes, |
| 29 | * lengths, etc). |
| 30 | * |
| 31 | * TODO We do a period sweep which kills contracted nodes, putting back |
| 32 | * placeholders, and updating expanded nodes to keep up with server-side |
| 33 | * changes. (We could trigger the latter off rescan complete notifications?) |
| 34 | * |
| 35 | * TODO: |
| 36 | * - sweep up contracted nodes |
| 37 | * - update when content may have changed (e.g. after a rescan) |
| 38 | * - searching! |
| 39 | * - proper sorting |
| 40 | */ |
| 41 | |
| 42 | #include "disobedience.h" |
| 43 | #include "choose.h" |
| 44 | |
| 45 | /** @brief The current selection tree */ |
| 46 | GtkTreeStore *choose_store; |
| 47 | |
| 48 | /** @brief The view onto the selection tree */ |
| 49 | GtkWidget *choose_view; |
| 50 | |
| 51 | /** @brief The selection tree's selection */ |
| 52 | GtkTreeSelection *choose_selection; |
| 53 | |
| 54 | static char *choose_get_string(GtkTreeIter *iter, int column) { |
| 55 | gchar *gs; |
| 56 | gtk_tree_model_get(GTK_TREE_MODEL(choose_store), iter, |
| 57 | column, &gs, |
| 58 | -1); |
| 59 | char *s = xstrdup(gs); |
| 60 | g_free(gs); |
| 61 | return s; |
| 62 | } |
| 63 | |
| 64 | char *choose_get_track(GtkTreeIter *iter) { |
| 65 | char *s = choose_get_string(iter, TRACK_COLUMN); |
| 66 | return *s ? s : 0; /* Placeholder -> NULL */ |
| 67 | } |
| 68 | |
| 69 | char *choose_get_sort(GtkTreeIter *iter) { |
| 70 | return choose_get_string(iter, SORT_COLUMN); |
| 71 | } |
| 72 | |
| 73 | int choose_is_file(GtkTreeIter *iter) { |
| 74 | gboolean isfile; |
| 75 | gtk_tree_model_get(GTK_TREE_MODEL(choose_store), iter, |
| 76 | ISFILE_COLUMN, &isfile, |
| 77 | -1); |
| 78 | return isfile; |
| 79 | } |
| 80 | |
| 81 | int choose_is_dir(GtkTreeIter *iter) { |
| 82 | gboolean isfile; |
| 83 | gtk_tree_model_get(GTK_TREE_MODEL(choose_store), iter, |
| 84 | ISFILE_COLUMN, &isfile, |
| 85 | -1); |
| 86 | if(isfile) |
| 87 | return FALSE; |
| 88 | return !choose_is_placeholder(iter); |
| 89 | } |
| 90 | |
| 91 | int choose_is_placeholder(GtkTreeIter *iter) { |
| 92 | return choose_get_string(iter, TRACK_COLUMN)[0] == 0; |
| 93 | } |
| 94 | |
| 95 | /** @brief Remove node @p it and all its children |
| 96 | * @param Iterator, updated to point to next |
| 97 | * @return True if iterator remains valid |
| 98 | */ |
| 99 | static gboolean choose_remove_node(GtkTreeIter *it) { |
| 100 | GtkTreeIter child[1]; |
| 101 | gboolean childv = gtk_tree_model_iter_children(GTK_TREE_MODEL(choose_store), |
| 102 | child, |
| 103 | it); |
| 104 | while(childv) |
| 105 | childv = choose_remove_node(child); |
| 106 | return gtk_tree_store_remove(choose_store, it); |
| 107 | } |
| 108 | |
| 109 | /** @brief Update length and state fields */ |
| 110 | static gboolean choose_set_state_callback(GtkTreeModel attribute((unused)) *model, |
| 111 | GtkTreePath attribute((unused)) *path, |
| 112 | GtkTreeIter *it, |
| 113 | gpointer attribute((unused)) data) { |
| 114 | if(choose_is_file(it)) { |
| 115 | const char *track = choose_get_track(it); |
| 116 | const long l = namepart_length(track); |
| 117 | char length[64]; |
| 118 | if(l > 0) |
| 119 | byte_snprintf(length, sizeof length, "%ld:%02ld", l / 60, l % 60); |
| 120 | else |
| 121 | length[0] = 0; |
| 122 | gtk_tree_store_set(choose_store, it, |
| 123 | LENGTH_COLUMN, length, |
| 124 | STATE_COLUMN, queued(track), |
| 125 | -1); |
| 126 | } |
| 127 | return FALSE; /* continue walking */ |
| 128 | } |
| 129 | |
| 130 | /** @brief Called when the queue or playing track change */ |
| 131 | static void choose_set_state(const char attribute((unused)) *event, |
| 132 | void attribute((unused)) *eventdata, |
| 133 | void attribute((unused)) *callbackdata) { |
| 134 | gtk_tree_model_foreach(GTK_TREE_MODEL(choose_store), |
| 135 | choose_set_state_callback, |
| 136 | NULL); |
| 137 | } |
| 138 | |
| 139 | /** @brief (Re-)populate a node |
| 140 | * @param parent_ref Node to populate or NULL to fill root |
| 141 | * @param nvec Number of children to add |
| 142 | * @param vec Children |
| 143 | * @param files 1 if children are files, 0 if directories |
| 144 | * |
| 145 | * Adjusts the set of files (or directories) below @p parent_ref to match those |
| 146 | * listed in @p nvec and @p vec. |
| 147 | * |
| 148 | * @parent_ref will be destroyed. |
| 149 | */ |
| 150 | static void choose_populate(GtkTreeRowReference *parent_ref, |
| 151 | int nvec, char **vec, |
| 152 | int isfile) { |
| 153 | /* Compute parent_* */ |
| 154 | GtkTreeIter pit[1], *parent_it; |
| 155 | GtkTreePath *parent_path; |
| 156 | if(parent_ref) { |
| 157 | parent_path = gtk_tree_row_reference_get_path(parent_ref); |
| 158 | parent_it = pit; |
| 159 | gboolean pitv = gtk_tree_model_get_iter(GTK_TREE_MODEL(choose_store), |
| 160 | pit, parent_path); |
| 161 | assert(pitv); |
| 162 | /*fprintf(stderr, "choose_populate %s: parent path is [%s]\n", |
| 163 | choose_type_map[type], |
| 164 | gtk_tree_path_to_string(parent_path));*/ |
| 165 | } else { |
| 166 | parent_path = 0; |
| 167 | parent_it = 0; |
| 168 | /*fprintf(stderr, "choose_populate %s: populating the root\n", |
| 169 | choose_type_map[type]);*/ |
| 170 | } |
| 171 | /* Remove unwanted nodes and find out which we must add */ |
| 172 | //fprintf(stderr, " trimming unwanted %s nodes\n", choose_type_map[type]); |
| 173 | char *found = xmalloc(nvec); |
| 174 | GtkTreeIter it[1]; |
| 175 | gboolean itv = gtk_tree_model_iter_children(GTK_TREE_MODEL(choose_store), |
| 176 | it, |
| 177 | parent_it); |
| 178 | while(itv) { |
| 179 | const char *track = choose_get_track(it); |
| 180 | int keep; |
| 181 | |
| 182 | if(!track) { |
| 183 | /* Always kill placeholders */ |
| 184 | //fprintf(stderr, " kill a placeholder\n"); |
| 185 | keep = 0; |
| 186 | } else if(choose_is_file(it) == isfile) { |
| 187 | /* This is the type we care about */ |
| 188 | //fprintf(stderr, " %s is a %s\n", track, isfile ? "file" : "dir"); |
| 189 | int n; |
| 190 | for(n = 0; n < nvec && strcmp(vec[n], track); ++n) |
| 191 | ; |
| 192 | if(n < nvec) { |
| 193 | //fprintf(stderr, " ... and survives\n"); |
| 194 | found[n] = 1; |
| 195 | keep = 1; |
| 196 | } else { |
| 197 | //fprintf(stderr, " ... and is to be removed\n"); |
| 198 | keep = 0; |
| 199 | } |
| 200 | } else { |
| 201 | /* Keep wrong-type entries */ |
| 202 | //fprintf(stderr, " %s has wrong type\n", track); |
| 203 | keep = 1; |
| 204 | } |
| 205 | if(keep) |
| 206 | itv = gtk_tree_model_iter_next(GTK_TREE_MODEL(choose_store), it); |
| 207 | else |
| 208 | itv = choose_remove_node(it); |
| 209 | } |
| 210 | /* Add nodes we don't have */ |
| 211 | int inserted = 0; |
| 212 | //fprintf(stderr, " inserting new %s nodes\n", choose_type_map[type]); |
| 213 | const char *typename = isfile ? "track" : "dir"; |
| 214 | for(int n = 0; n < nvec; ++n) { |
| 215 | if(!found[n]) { |
| 216 | //fprintf(stderr, " %s was not found\n", vec[n]); |
| 217 | gtk_tree_store_append(choose_store, it, parent_it); |
| 218 | gtk_tree_store_set(choose_store, it, |
| 219 | NAME_COLUMN, trackname_transform(typename, |
| 220 | vec[n], |
| 221 | "display"), |
| 222 | ISFILE_COLUMN, isfile, |
| 223 | TRACK_COLUMN, vec[n], |
| 224 | SORT_COLUMN, trackname_transform(typename, |
| 225 | vec[n], |
| 226 | "sort"), |
| 227 | -1); |
| 228 | /* Update length and state; we expect this to kick off length lookups |
| 229 | * rather than necessarily get the right value the first time round. */ |
| 230 | choose_set_state_callback(0, 0, it, 0); |
| 231 | ++inserted; |
| 232 | /* If we inserted a directory, insert a placeholder too, so it appears to |
| 233 | * have children; it will be deleted when we expand the directory. */ |
| 234 | if(!isfile) { |
| 235 | //fprintf(stderr, " inserting a placeholder\n"); |
| 236 | GtkTreeIter placeholder[1]; |
| 237 | |
| 238 | gtk_tree_store_append(choose_store, placeholder, it); |
| 239 | gtk_tree_store_set(choose_store, placeholder, |
| 240 | NAME_COLUMN, "Waddling...", |
| 241 | TRACK_COLUMN, "", |
| 242 | ISFILE_COLUMN, FALSE, |
| 243 | -1); |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | //fprintf(stderr, " %d nodes inserted\n", inserted); |
| 248 | if(inserted) { |
| 249 | /* TODO sort the children */ |
| 250 | } |
| 251 | if(parent_ref) { |
| 252 | /* If we deleted a placeholder then we must re-expand the row */ |
| 253 | gtk_tree_view_expand_row(GTK_TREE_VIEW(choose_view), parent_path, FALSE); |
| 254 | gtk_tree_row_reference_free(parent_ref); |
| 255 | gtk_tree_path_free(parent_path); |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | static void choose_dirs_completed(void *v, |
| 260 | const char *error, |
| 261 | int nvec, char **vec) { |
| 262 | if(error) { |
| 263 | popup_protocol_error(0, error); |
| 264 | return; |
| 265 | } |
| 266 | choose_populate(v, nvec, vec, 0/*!isfile*/); |
| 267 | } |
| 268 | |
| 269 | static void choose_files_completed(void *v, |
| 270 | const char *error, |
| 271 | int nvec, char **vec) { |
| 272 | if(error) { |
| 273 | popup_protocol_error(0, error); |
| 274 | return; |
| 275 | } |
| 276 | choose_populate(v, nvec, vec, 1/*isfile*/); |
| 277 | } |
| 278 | |
| 279 | void choose_play_completed(void attribute((unused)) *v, |
| 280 | const char *error) { |
| 281 | if(error) |
| 282 | popup_protocol_error(0, error); |
| 283 | } |
| 284 | |
| 285 | static void choose_state_toggled |
| 286 | (GtkCellRendererToggle attribute((unused)) *cell_renderer, |
| 287 | gchar *path_str, |
| 288 | gpointer attribute((unused)) user_data) { |
| 289 | GtkTreeIter it[1]; |
| 290 | /* Identify the track */ |
| 291 | gboolean itv = |
| 292 | gtk_tree_model_get_iter_from_string(GTK_TREE_MODEL(choose_store), |
| 293 | it, |
| 294 | path_str); |
| 295 | if(!itv) |
| 296 | return; |
| 297 | if(!choose_is_file(it)) |
| 298 | return; |
| 299 | const char *track = choose_get_track(it); |
| 300 | if(queued(track)) |
| 301 | return; |
| 302 | disorder_eclient_play(client, track, choose_play_completed, 0); |
| 303 | |
| 304 | } |
| 305 | |
| 306 | static void choose_row_expanded(GtkTreeView attribute((unused)) *treeview, |
| 307 | GtkTreeIter *iter, |
| 308 | GtkTreePath *path, |
| 309 | gpointer attribute((unused)) user_data) { |
| 310 | /*fprintf(stderr, "row-expanded path=[%s]\n", |
| 311 | gtk_tree_path_to_string(path));*/ |
| 312 | /* We update a node's contents whenever it is expanded, even if it was |
| 313 | * already populated; the effect is that contracting and expanding a node |
| 314 | * suffices to update it to the latest state on the server. */ |
| 315 | const char *track = choose_get_track(iter); |
| 316 | disorder_eclient_files(client, choose_files_completed, |
| 317 | track, |
| 318 | NULL, |
| 319 | gtk_tree_row_reference_new(GTK_TREE_MODEL(choose_store), |
| 320 | path)); |
| 321 | disorder_eclient_dirs(client, choose_dirs_completed, |
| 322 | track, |
| 323 | NULL, |
| 324 | gtk_tree_row_reference_new(GTK_TREE_MODEL(choose_store), |
| 325 | path)); |
| 326 | /* The row references are destroyed in the _completed handlers. */ |
| 327 | } |
| 328 | |
| 329 | /** @brief Create the choose tab */ |
| 330 | GtkWidget *choose_widget(void) { |
| 331 | /* Create the tree store. */ |
| 332 | choose_store = gtk_tree_store_new(CHOOSE_COLUMNS, |
| 333 | G_TYPE_BOOLEAN, |
| 334 | G_TYPE_STRING, |
| 335 | G_TYPE_STRING, |
| 336 | G_TYPE_BOOLEAN, |
| 337 | G_TYPE_STRING, |
| 338 | G_TYPE_STRING); |
| 339 | |
| 340 | /* Create the view */ |
| 341 | choose_view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(choose_store)); |
| 342 | gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(choose_view), TRUE); |
| 343 | |
| 344 | /* Create cell renderers and columns */ |
| 345 | /* TODO use a table */ |
| 346 | { |
| 347 | GtkCellRenderer *r = gtk_cell_renderer_text_new(); |
| 348 | GtkTreeViewColumn *c = gtk_tree_view_column_new_with_attributes |
| 349 | ("Track", |
| 350 | r, |
| 351 | "text", NAME_COLUMN, |
| 352 | (char *)0); |
| 353 | gtk_tree_view_column_set_resizable(c, TRUE); |
| 354 | gtk_tree_view_column_set_reorderable(c, TRUE); |
| 355 | g_object_set(c, "expand", TRUE, (char *)0); |
| 356 | gtk_tree_view_append_column(GTK_TREE_VIEW(choose_view), c); |
| 357 | } |
| 358 | { |
| 359 | GtkCellRenderer *r = gtk_cell_renderer_text_new(); |
| 360 | GtkTreeViewColumn *c = gtk_tree_view_column_new_with_attributes |
| 361 | ("Length", |
| 362 | r, |
| 363 | "text", LENGTH_COLUMN, |
| 364 | (char *)0); |
| 365 | gtk_tree_view_column_set_resizable(c, TRUE); |
| 366 | gtk_tree_view_column_set_reorderable(c, TRUE); |
| 367 | g_object_set(r, "xalign", (gfloat)1.0, (char *)0); |
| 368 | gtk_tree_view_append_column(GTK_TREE_VIEW(choose_view), c); |
| 369 | } |
| 370 | { |
| 371 | GtkCellRenderer *r = gtk_cell_renderer_toggle_new(); |
| 372 | GtkTreeViewColumn *c = gtk_tree_view_column_new_with_attributes |
| 373 | ("Queued", |
| 374 | r, |
| 375 | "active", STATE_COLUMN, |
| 376 | "visible", ISFILE_COLUMN, |
| 377 | (char *)0); |
| 378 | gtk_tree_view_column_set_resizable(c, TRUE); |
| 379 | gtk_tree_view_column_set_reorderable(c, TRUE); |
| 380 | gtk_tree_view_append_column(GTK_TREE_VIEW(choose_view), c); |
| 381 | g_signal_connect(r, "toggled", |
| 382 | G_CALLBACK(choose_state_toggled), 0); |
| 383 | } |
| 384 | |
| 385 | /* The selection should support multiple things being selected */ |
| 386 | choose_selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(choose_view)); |
| 387 | gtk_tree_selection_set_mode(choose_selection, GTK_SELECTION_MULTIPLE); |
| 388 | |
| 389 | /* Catch button presses */ |
| 390 | g_signal_connect(choose_view, "button-press-event", |
| 391 | G_CALLBACK(choose_button_event), 0); |
| 392 | g_signal_connect(choose_view, "button-release-event", |
| 393 | G_CALLBACK(choose_button_event), 0); |
| 394 | /* Catch row expansions so we can fill in placeholders */ |
| 395 | g_signal_connect(choose_view, "row-expanded", |
| 396 | G_CALLBACK(choose_row_expanded), 0); |
| 397 | |
| 398 | event_register("queue-list-changed", choose_set_state, 0); |
| 399 | event_register("playing-track-changed", choose_set_state, 0); |
| 400 | |
| 401 | /* Fill the root */ |
| 402 | disorder_eclient_files(client, choose_files_completed, "", NULL, NULL); |
| 403 | disorder_eclient_dirs(client, choose_dirs_completed, "", NULL, NULL); |
| 404 | |
| 405 | /* Make the widget scrollable */ |
| 406 | GtkWidget *scrolled = scroll_widget(choose_view); |
| 407 | g_object_set_data(G_OBJECT(scrolled), "type", (void *)&choose_tabtype); |
| 408 | return scrolled; |
| 409 | } |
| 410 | |
| 411 | /* |
| 412 | Local Variables: |
| 413 | c-basic-offset:2 |
| 414 | comment-column:40 |
| 415 | fill-column:79 |
| 416 | indent-tabs-mode:nil |
| 417 | End: |
| 418 | */ |