chiark / gitweb /
queue() now uses the resolved name if available. This is looked up in
[disorder] / disobedience / choose-search.c
... / ...
CommitLineData
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/search.c
21 * @brief Search support
22 *
23 * TODO:
24 * - cleverer focus to implement typeahead find
25 */
26#include "disobedience.h"
27#include "choose.h"
28
29int choose_auto_expanding;
30
31static GtkWidget *choose_search_entry;
32static GtkWidget *choose_next;
33static GtkWidget *choose_prev;
34static GtkWidget *choose_clear;
35
36/** @brief True if a search command is in flight */
37static int choose_searching;
38
39/** @brief True if in-flight search is now known to be obsolete */
40static int choose_search_obsolete;
41
42/** @brief Current search terms */
43static char *choose_search_terms;
44
45/** @brief Hash of all search result */
46static hash *choose_search_hash;
47
48/** @brief List of invisible search results
49 *
50 * This only lists search results not yet known to be visible, and is
51 * gradually depleted.
52 */
53static char **choose_search_results;
54
55/** @brief Length of @ref choose_search_results */
56static int choose_n_search_results;
57
58/** @brief Row references for search results */
59static GtkTreeRowReference **choose_search_references;
60
61/** @brief Length of @ref choose_search_references */
62static int choose_n_search_references;
63
64/** @brief Event handle for monitoring newly inserted tracks */
65static event_handle choose_inserted_handle;
66
67/** @brief Time of last search entry keypress (or 0.0) */
68static struct timeval choose_search_last_keypress;
69
70/** @brief Timeout ID for search delay */
71static guint choose_search_timeout_id;
72
73static void choose_search_entry_changed(GtkEditable *editable,
74 gpointer user_data);
75
76int choose_is_search_result(const char *track) {
77 return choose_search_hash && hash_find(choose_search_hash, track);
78}
79
80static int is_prefix(const char *dir, const char *track) {
81 size_t nd = strlen(dir);
82
83 if(nd < strlen(track)
84 && track[nd] == '/'
85 && !strncmp(track, dir, nd))
86 return 1;
87 else
88 return 0;
89}
90
91/** @brief Do some work towards making @p track visible
92 * @return True if we made it visible or it was missing
93 */
94static int choose_make_one_visible(const char *track) {
95 //fprintf(stderr, " choose_make_one_visible %s\n", track);
96 /* We walk through nodes at the top level looking for directories that are
97 * prefixes of the target track.
98 *
99 * - if we find one and it's expanded we walk through its children
100 * - if we find one and it's NOT expanded then we expand it, and arrange
101 * to be revisited
102 * - if we don't find one then we're probably out of date
103 */
104 GtkTreeIter it[1];
105 gboolean itv = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(choose_store),
106 it);
107 while(itv) {
108 const char *dir = choose_get_track(it);
109
110 //fprintf(stderr, " %s\n", dir);
111 if(!dir) {
112 /* Placeholder */
113 itv = gtk_tree_model_iter_next(GTK_TREE_MODEL(choose_store), it);
114 continue;
115 }
116 GtkTreePath *path = gtk_tree_model_get_path(GTK_TREE_MODEL(choose_store),
117 it);
118 if(!strcmp(dir, track)) {
119 /* We found the track. If everything above it was expanded, it will be
120 * too. So we can report it as visible. */
121 //fprintf(stderr, " found %s\n", track);
122 choose_search_references[choose_n_search_references++]
123 = gtk_tree_row_reference_new(GTK_TREE_MODEL(choose_store), path);
124 gtk_tree_path_free(path);
125 return 1;
126 }
127 if(is_prefix(dir, track)) {
128 /* We found a prefix of the target track. */
129 //fprintf(stderr, " %s is a prefix\n", dir);
130 const gboolean expanded
131 = gtk_tree_view_row_expanded(GTK_TREE_VIEW(choose_view), path);
132 if(expanded) {
133 //fprintf(stderr, " is apparently expanded\n");
134 /* This directory is expanded, let's make like Augustus Gibbons and
135 * take it to the next level. */
136 GtkTreeIter child[1]; /* don't know if parent==iter allowed */
137 itv = gtk_tree_model_iter_children(GTK_TREE_MODEL(choose_store),
138 child,
139 it);
140 *it = *child;
141 if(choose_is_placeholder(it)) {
142 //fprintf(stderr, " %s is expanded, has a placeholder child\n", dir);
143 /* We assume that placeholder children of expanded rows are about to
144 * be replaced */
145 gtk_tree_path_free(path);
146 return 0;
147 }
148 } else {
149 //fprintf(stderr, " requesting expansion of %s\n", dir);
150 /* Track is below a non-expanded directory. So let's expand it.
151 * choose_make_visible() will arrange a revisit in due course.
152 *
153 * We mark the row as auto-expanded.
154 */
155 ++choose_auto_expanding;
156 gtk_tree_view_expand_row(GTK_TREE_VIEW(choose_view),
157 path,
158 FALSE/*open_all*/);
159 gtk_tree_path_free(path);
160 --choose_auto_expanding;
161 return 0;
162 }
163 } else
164 itv = gtk_tree_model_iter_next(GTK_TREE_MODEL(choose_store), it);
165 gtk_tree_path_free(path);
166 }
167 /* If we reach the end then we didn't find the track at all. */
168 fprintf(stderr, "choose_make_one_visible: could not find %s\n",
169 track);
170 return 1;
171}
172
173/** @brief Compare two GtkTreeRowReferences
174 *
175 * Not very efficient since it does multiple memory operations per
176 * comparison!
177 */
178static int choose_compare_references(const void *av, const void *bv) {
179 GtkTreeRowReference *a = *(GtkTreeRowReference **)av;
180 GtkTreeRowReference *b = *(GtkTreeRowReference **)bv;
181 GtkTreePath *pa = gtk_tree_row_reference_get_path(a);
182 GtkTreePath *pb = gtk_tree_row_reference_get_path(b);
183 const int rc = gtk_tree_path_compare(pa, pb);
184 gtk_tree_path_free(pa);
185 gtk_tree_path_free(pb);
186 return rc;
187}
188
189/** @brief Make @p path visible
190 * @param path Row reference to make visible
191 * @param row_align Row alignment (or -ve)
192 * @return 0 on success, nonzero if @p ref has gone stale
193 *
194 * If @p row_align is negative no row alignemt is performed. Otherwise
195 * it must be between 0 (the top) and 1 (the bottom).
196 *
197 * TODO: if the row is already visible do nothing.
198 */
199static int choose_make_path_visible(GtkTreePath *path,
200 gfloat row_align) {
201 /* Make sure that the target's parents are all expanded */
202 gtk_tree_view_expand_to_path(GTK_TREE_VIEW(choose_view), path);
203 /* Make sure the target is visible */
204 gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(choose_view), path, NULL,
205 row_align >= 0.0,
206 row_align,
207 0);
208 return 0;
209}
210
211/** @brief Make @p ref visible
212 * @param ref Row reference to make visible
213 * @param row_align Row alignment (or -ve)
214 * @return 0 on success, nonzero if @p ref has gone stale
215 *
216 * If @p row_align is negative no row alignemt is performed. Otherwise
217 * it must be between 0 (the top) and 1 (the bottom).
218 */
219static int choose_make_ref_visible(GtkTreeRowReference *ref,
220 gfloat row_align) {
221 GtkTreePath *path = gtk_tree_row_reference_get_path(ref);
222 if(!path)
223 return -1;
224 choose_make_path_visible(path, row_align);
225 gtk_tree_path_free(path);
226 return 0;
227}
228
229/** @brief Do some work towards ensuring that all search results are visible
230 *
231 * Assumes there's at least one results!
232 */
233static void choose_make_visible(const char attribute((unused)) *event,
234 void attribute((unused)) *eventdata,
235 void attribute((unused)) *callbackdata) {
236 //fprintf(stderr, "choose_make_visible\n");
237 int remaining = 0;
238
239 for(int n = 0; n < choose_n_search_results; ++n) {
240 if(!choose_search_results[n])
241 continue;
242 if(choose_make_one_visible(choose_search_results[n]))
243 choose_search_results[n] = 0;
244 else
245 ++remaining;
246 }
247 //fprintf(stderr, "remaining=%d\n", remaining);
248 if(remaining) {
249 /* If there's work left to be done make sure we get a callback when
250 * something changes */
251 if(!choose_inserted_handle)
252 choose_inserted_handle = event_register("choose-more-tracks",
253 choose_make_visible, 0);
254 } else {
255 /* Suppress callbacks if there's nothing more to do */
256 event_cancel(choose_inserted_handle);
257 choose_inserted_handle = 0;
258 /* We've expanded everything, now we can mess with the cursor */
259 //fprintf(stderr, "sort %d references\n", choose_n_search_references);
260 qsort(choose_search_references,
261 choose_n_search_references,
262 sizeof (GtkTreeRowReference *),
263 choose_compare_references);
264 choose_make_ref_visible(choose_search_references[0], 0.5);
265 }
266}
267
268/** @brief Called with search results */
269static void choose_search_completed(void attribute((unused)) *v,
270 const char *error,
271 int nvec, char **vec) {
272 //fprintf(stderr, "choose_search_completed\n");
273 if(error) {
274 popup_protocol_error(0, error);
275 return;
276 }
277 choose_searching = 0;
278 /* If the search was obsoleted initiate another one */
279 if(choose_search_obsolete) {
280 choose_search_obsolete = 0;
281 choose_search_entry_changed(0, 0);
282 return;
283 }
284 //fprintf(stderr, "*** %d search results\n", nvec);
285 /* We're actually going to use these search results. Autocollapse anything
286 * left over from the old search. */
287 choose_auto_collapse();
288 choose_search_hash = hash_new(1);
289 if(nvec) {
290 for(int n = 0; n < nvec; ++n)
291 hash_add(choose_search_hash, vec[n], "", HASH_INSERT);
292 /* Stash results for choose_make_visible */
293 choose_n_search_results = nvec;
294 choose_search_results = vec;
295 /* Make a big-enough buffer for the results row reference list */
296 choose_n_search_references = 0;
297 choose_search_references = xcalloc(nvec, sizeof (GtkTreeRowReference *));
298 /* Start making rows visible */
299 choose_make_visible(0, 0, 0);
300 gtk_widget_set_sensitive(choose_next, TRUE);
301 gtk_widget_set_sensitive(choose_prev, TRUE);
302 } else {
303 gtk_widget_set_sensitive(choose_next, FALSE);
304 gtk_widget_set_sensitive(choose_prev, FALSE);
305 }
306 event_raise("search-results-changed", 0);
307}
308
309/** @brief Actually initiate a search */
310static void initiate_search(void) {
311 //fprintf(stderr, "initiate_search\n");
312 /* If a search is in flight don't initiate a new one until it comes back */
313 if(choose_searching) {
314 choose_search_obsolete = 1;
315 return;
316 }
317 char *terms = xstrdup(gtk_entry_get_text(GTK_ENTRY(choose_search_entry)));
318 /* Strip leading and trailing space */
319 while(*terms == ' ')
320 ++terms;
321 char *e = terms + strlen(terms);
322 while(e > terms && e[-1] == ' ')
323 --e;
324 *e = 0;
325 if(choose_search_terms && !strcmp(terms, choose_search_terms)) {
326 /* Search terms have not actually changed in any way that matters */
327 return;
328 }
329 /* Remember the current terms */
330 choose_search_terms = terms;
331 if(!*terms) {
332 /* Nothing to search for. Fake a completion call. */
333 choose_search_completed(0, 0, 0, 0);
334 return;
335 }
336 if(disorder_eclient_search(client, choose_search_completed, terms, 0)) {
337 /* Bad search terms. Fake a completion call. */
338 choose_search_completed(0, 0, 0, 0);
339 return;
340 }
341 choose_searching = 1;
342}
343
344static gboolean choose_search_timeout(gpointer attribute((unused)) data) {
345 struct timeval now;
346 xgettimeofday(&now, NULL);
347 /*fprintf(stderr, "%ld.%06d choose_search_timeout\n",
348 now.tv_sec, now.tv_usec);*/
349 if(tvdouble(now) - tvdouble(choose_search_last_keypress)
350 < SEARCH_DELAY_MS / 1000.0) {
351 //fprintf(stderr, " ... too soon\n");
352 return TRUE; /* Call me again later */
353 }
354 //fprintf(stderr, " ... let's go\n");
355 choose_search_last_keypress.tv_sec = 0;
356 choose_search_last_keypress.tv_usec = 0;
357 choose_search_timeout_id = 0;
358 initiate_search();
359 return FALSE;
360}
361
362/** @brief Called when the search entry changes */
363static void choose_search_entry_changed
364 (GtkEditable attribute((unused)) *editable,
365 gpointer attribute((unused)) user_data) {
366 xgettimeofday(&choose_search_last_keypress, NULL);
367 /*fprintf(stderr, "%ld.%06d choose_search_entry_changed\n",
368 choose_search_last_keypress.tv_sec,
369 choose_search_last_keypress.tv_usec);*/
370 /* If there's already a timeout, remove it */
371 if(choose_search_timeout_id) {
372 g_source_remove(choose_search_timeout_id);
373 choose_search_timeout_id = 0;
374 }
375 /* Add a new timeout */
376 choose_search_timeout_id = g_timeout_add(SEARCH_DELAY_MS / 10,
377 choose_search_timeout,
378 0);
379 /* We really wanted to tell Glib what time we wanted the callback at rather
380 * than asking for calls at given intervals. But there's no interface for
381 * that, and defining a new source for it seems like overkill if we can
382 * reasonably avoid it. */
383}
384
385/** @brief Identify first and last visible paths
386 *
387 * We'd like to use gtk_tree_view_get_visible_range() for this, but that was
388 * introduced in GTK+ 2.8, and Fink only has 2.6 (which is around three years
389 * out of date at time of writing), and I'm not yet prepared to rule out Fink
390 * support.
391 */
392static gboolean choose_get_visible_range(GtkTreeView *tree_view,
393 GtkTreePath **startpathp,
394 GtkTreePath **endpathp) {
395 GdkRectangle visible_tc[1];
396
397 /* Get the visible rectangle in tree coordinates */
398 gtk_tree_view_get_visible_rect(tree_view, visible_tc);
399 /*fprintf(stderr, "visible: %dx%x at %dx%d\n",
400 visible_tc->width, visible_tc->height,
401 visible_tc->x, visible_tc->y);*/
402 if(startpathp) {
403 /* Convert top-left visible point to widget coordinates */
404 int x_wc, y_wc;
405 gtk_tree_view_tree_to_widget_coords(tree_view,
406 visible_tc->x, visible_tc->y,
407 &x_wc, &y_wc);
408 //fprintf(stderr, " start widget coords: %dx%d\n", x_wc, y_wc);
409 gtk_tree_view_get_path_at_pos(tree_view,
410 x_wc, y_wc,
411 startpathp,
412 NULL,
413 NULL, NULL);
414 }
415 if(endpathp) {
416 /* Convert bottom-left visible point to widget coordinates */
417 /* Convert top-left visible point to widget coordinates */
418 int x_wc, y_wc;
419 gtk_tree_view_tree_to_widget_coords(tree_view,
420 visible_tc->x,
421 visible_tc->y + visible_tc->height - 1,
422 &x_wc, &y_wc);
423 //fprintf(stderr, " end widget coords: %dx%d\n", x_wc, y_wc);
424 gtk_tree_view_get_path_at_pos(tree_view,
425 x_wc, y_wc,
426 endpathp,
427 NULL,
428 NULL, NULL);
429 }
430 return TRUE;
431}
432
433static void choose_next_clicked(GtkButton attribute((unused)) *button,
434 gpointer attribute((unused)) userdata) {
435 /* Find the last visible row */
436 GtkTreePath *endpath;
437 gboolean endvalid = choose_get_visible_range(GTK_TREE_VIEW(choose_view),
438 NULL,
439 &endpath);
440 if(!endvalid)
441 return;
442 /* Find a the first search result later than it. They're sorted so we could
443 * actually do much better than this if necessary. */
444 for(int n = 0; n < choose_n_search_references; ++n) {
445 GtkTreePath *path
446 = gtk_tree_row_reference_get_path(choose_search_references[n]);
447 if(!path)
448 continue;
449 if(gtk_tree_path_compare(endpath, path) < 0) {
450 choose_make_path_visible(path, 0.5);
451 gtk_tree_path_free(path);
452 return;
453 }
454 gtk_tree_path_free(path);
455 }
456}
457
458static void choose_prev_clicked(GtkButton attribute((unused)) *button,
459 gpointer attribute((unused)) userdata) {
460 /* TODO can we de-dupe with choose_next_clicked? Probably yes. */
461 /* Find the first visible row */
462 GtkTreePath *startpath;
463 gboolean startvalid = choose_get_visible_range(GTK_TREE_VIEW(choose_view),
464 &startpath,
465 NULL);
466 if(!startvalid)
467 return;
468 /* Find a the last search result earlier than it. They're sorted so we could
469 * actually do much better than this if necessary. */
470 for(int n = choose_n_search_references - 1; n >= 0; --n) {
471 GtkTreePath *path
472 = gtk_tree_row_reference_get_path(choose_search_references[n]);
473 if(!path)
474 continue;
475 if(gtk_tree_path_compare(startpath, path) > 0) {
476 choose_make_path_visible(path, 0.5);
477 gtk_tree_path_free(path);
478 return;
479 }
480 gtk_tree_path_free(path);
481 }
482}
483
484/** @brief Called when the cancel search button is clicked */
485static void choose_clear_clicked(GtkButton attribute((unused)) *button,
486 gpointer attribute((unused)) userdata) {
487 gtk_entry_set_text(GTK_ENTRY(choose_search_entry), "");
488 /* We start things off straight away in this case */
489 initiate_search();
490}
491
492/** @brief Create the search widget */
493GtkWidget *choose_search_widget(void) {
494
495 /* Text entry box for search terms */
496 choose_search_entry = gtk_entry_new();
497 gtk_widget_set_style(choose_search_entry, tool_style);
498 g_signal_connect(choose_search_entry, "changed",
499 G_CALLBACK(choose_search_entry_changed), 0);
500 gtk_tooltips_set_tip(tips, choose_search_entry,
501 "Enter search terms here; search is automatic", "");
502
503 /* Cancel button to clear the search */
504 choose_clear = gtk_button_new_from_stock(GTK_STOCK_CANCEL);
505 gtk_widget_set_style(choose_clear, tool_style);
506 g_signal_connect(G_OBJECT(choose_clear), "clicked",
507 G_CALLBACK(choose_clear_clicked), 0);
508 gtk_tooltips_set_tip(tips, choose_clear, "Clear search terms", "");
509
510 /* Up and down buttons to find previous/next results; initially they are not
511 * usable as there are no search results. */
512 choose_prev = iconbutton("up.png", "Previous search result");
513 g_signal_connect(G_OBJECT(choose_prev), "clicked",
514 G_CALLBACK(choose_prev_clicked), 0);
515 gtk_widget_set_style(choose_prev, tool_style);
516 gtk_widget_set_sensitive(choose_prev, 0);
517 choose_next = iconbutton("down.png", "Next search result");
518 g_signal_connect(G_OBJECT(choose_next), "clicked",
519 G_CALLBACK(choose_next_clicked), 0);
520 gtk_widget_set_style(choose_next, tool_style);
521 gtk_widget_set_sensitive(choose_next, 0);
522
523 /* Pack the search tools button together on a line */
524 GtkWidget *hbox = gtk_hbox_new(FALSE/*homogeneous*/, 1/*spacing*/);
525 gtk_box_pack_start(GTK_BOX(hbox), choose_search_entry,
526 TRUE/*expand*/, TRUE/*fill*/, 0/*padding*/);
527 gtk_box_pack_start(GTK_BOX(hbox), choose_prev,
528 FALSE/*expand*/, FALSE/*fill*/, 0/*padding*/);
529 gtk_box_pack_start(GTK_BOX(hbox), choose_next,
530 FALSE/*expand*/, FALSE/*fill*/, 0/*padding*/);
531 gtk_box_pack_start(GTK_BOX(hbox), choose_clear,
532 FALSE/*expand*/, FALSE/*fill*/, 0/*padding*/);
533
534 return hbox;
535}
536
537/*
538Local Variables:
539c-basic-offset:2
540comment-column:40
541fill-column:79
542indent-tabs-mode:nil
543End:
544*/