chiark / gitweb /
Assume initial digits in a track name are a sort key even without the
[disorder] / disobedience / choose-search.c
CommitLineData
cfa78eaa
RK
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 */
bb9bcc90
RK
20/** @file disobedience/search.c
21 * @brief Search support
bb9bcc90 22 */
cfa78eaa
RK
23#include "disobedience.h"
24#include "choose.h"
25
bd802f22
RK
26int choose_auto_expanding;
27
a59663d4 28GtkWidget *choose_search_entry;
cfa78eaa
RK
29static GtkWidget *choose_next;
30static GtkWidget *choose_prev;
31static GtkWidget *choose_clear;
32
33/** @brief True if a search command is in flight */
34static int choose_searching;
35
36/** @brief True if in-flight search is now known to be obsolete */
37static int choose_search_obsolete;
38
9eeb9f12
RK
39/** @brief Current search terms */
40static char *choose_search_terms;
41
cfa78eaa
RK
42/** @brief Hash of all search result */
43static hash *choose_search_hash;
44
2a9a65e4
RK
45/** @brief List of invisible search results
46 *
47 * This only lists search results not yet known to be visible, and is
48 * gradually depleted.
49 */
50static char **choose_search_results;
51
52/** @brief Length of @ref choose_search_results */
53static int choose_n_search_results;
54
55/** @brief Row references for search results */
56static GtkTreeRowReference **choose_search_references;
57
58/** @brief Length of @ref choose_search_references */
59static int choose_n_search_references;
60
61/** @brief Event handle for monitoring newly inserted tracks */
62static event_handle choose_inserted_handle;
63
9eeb9f12
RK
64/** @brief Time of last search entry keypress (or 0.0) */
65static struct timeval choose_search_last_keypress;
66
67/** @brief Timeout ID for search delay */
68static guint choose_search_timeout_id;
69
cfa78eaa
RK
70static void choose_search_entry_changed(GtkEditable *editable,
71 gpointer user_data);
30fc14d8
RK
72static gboolean choose_get_visible_range(GtkTreeView *tree_view,
73 GtkTreePath **startpathp,
74 GtkTreePath **endpathp);
cfa78eaa
RK
75
76int choose_is_search_result(const char *track) {
77 return choose_search_hash && hash_find(choose_search_hash, track);
78}
79
2a9a65e4
RK
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. */
cab9a17c 129 //fprintf(stderr, " %s is a prefix\n", dir);
2a9a65e4
RK
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.
bd802f22
RK
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;
2a9a65e4
RK
156 gtk_tree_view_expand_row(GTK_TREE_VIEW(choose_view),
157 path,
158 FALSE/*open_all*/);
159 gtk_tree_path_free(path);
bd802f22 160 --choose_auto_expanding;
2a9a65e4
RK
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
b96f65cf
RK
189/** @brief Make @p path visible
190 * @param path Row reference to make visible
191 * @param row_align Row alignment (or -ve)
2a9a65e4 192 * @return 0 on success, nonzero if @p ref has gone stale
b96f65cf
RK
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 */
197static int choose_make_path_visible(GtkTreePath *path,
198 gfloat row_align) {
199 /* Make sure that the target's parents are all expanded */
200 gtk_tree_view_expand_to_path(GTK_TREE_VIEW(choose_view), path);
30fc14d8
RK
201 /* Find out what's currently visible */
202 GtkTreePath *startpath, *endpath;
203 choose_get_visible_range(GTK_TREE_VIEW(choose_view), &startpath, &endpath);
b96f65cf 204 /* Make sure the target is visible */
30fc14d8
RK
205 if(gtk_tree_path_compare(path, startpath) < 0
206 || gtk_tree_path_compare(path, endpath) > 0)
207 gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(choose_view), path, NULL,
208 row_align >= 0.0,
209 row_align,
210 0);
211 gtk_tree_path_free(startpath);
212 gtk_tree_path_free(endpath);
b96f65cf
RK
213 return 0;
214}
215
216/** @brief Make @p ref visible
217 * @param ref Row reference to make visible
218 * @param row_align Row alignment (or -ve)
219 * @return 0 on success, nonzero if @p ref has gone stale
220 *
221 * If @p row_align is negative no row alignemt is performed. Otherwise
222 * it must be between 0 (the top) and 1 (the bottom).
2a9a65e4 223 */
b96f65cf
RK
224static int choose_make_ref_visible(GtkTreeRowReference *ref,
225 gfloat row_align) {
2a9a65e4
RK
226 GtkTreePath *path = gtk_tree_row_reference_get_path(ref);
227 if(!path)
228 return -1;
b96f65cf 229 choose_make_path_visible(path, row_align);
2a9a65e4
RK
230 gtk_tree_path_free(path);
231 return 0;
232}
233
234/** @brief Do some work towards ensuring that all search results are visible
235 *
236 * Assumes there's at least one results!
237 */
238static void choose_make_visible(const char attribute((unused)) *event,
239 void attribute((unused)) *eventdata,
240 void attribute((unused)) *callbackdata) {
241 //fprintf(stderr, "choose_make_visible\n");
242 int remaining = 0;
243
244 for(int n = 0; n < choose_n_search_results; ++n) {
245 if(!choose_search_results[n])
246 continue;
247 if(choose_make_one_visible(choose_search_results[n]))
248 choose_search_results[n] = 0;
249 else
250 ++remaining;
251 }
252 //fprintf(stderr, "remaining=%d\n", remaining);
253 if(remaining) {
254 /* If there's work left to be done make sure we get a callback when
255 * something changes */
256 if(!choose_inserted_handle)
cab9a17c 257 choose_inserted_handle = event_register("choose-more-tracks",
2a9a65e4
RK
258 choose_make_visible, 0);
259 } else {
260 /* Suppress callbacks if there's nothing more to do */
261 event_cancel(choose_inserted_handle);
262 choose_inserted_handle = 0;
263 /* We've expanded everything, now we can mess with the cursor */
264 //fprintf(stderr, "sort %d references\n", choose_n_search_references);
265 qsort(choose_search_references,
266 choose_n_search_references,
267 sizeof (GtkTreeRowReference *),
268 choose_compare_references);
b96f65cf 269 choose_make_ref_visible(choose_search_references[0], 0.5);
2a9a65e4
RK
270 }
271}
272
cfa78eaa
RK
273/** @brief Called with search results */
274static void choose_search_completed(void attribute((unused)) *v,
abf99697 275 const char *err,
cfa78eaa 276 int nvec, char **vec) {
2a9a65e4 277 //fprintf(stderr, "choose_search_completed\n");
abf99697
RK
278 if(err) {
279 popup_protocol_error(0, err);
cfa78eaa
RK
280 return;
281 }
282 choose_searching = 0;
283 /* If the search was obsoleted initiate another one */
284 if(choose_search_obsolete) {
2a9a65e4 285 choose_search_obsolete = 0;
cfa78eaa
RK
286 choose_search_entry_changed(0, 0);
287 return;
288 }
2a9a65e4 289 //fprintf(stderr, "*** %d search results\n", nvec);
bd802f22
RK
290 /* We're actually going to use these search results. Autocollapse anything
291 * left over from the old search. */
292 choose_auto_collapse();
cfa78eaa 293 choose_search_hash = hash_new(1);
2a9a65e4
RK
294 if(nvec) {
295 for(int n = 0; n < nvec; ++n)
296 hash_add(choose_search_hash, vec[n], "", HASH_INSERT);
297 /* Stash results for choose_make_visible */
298 choose_n_search_results = nvec;
299 choose_search_results = vec;
300 /* Make a big-enough buffer for the results row reference list */
301 choose_n_search_references = 0;
302 choose_search_references = xcalloc(nvec, sizeof (GtkTreeRowReference *));
303 /* Start making rows visible */
304 choose_make_visible(0, 0, 0);
b96f65cf
RK
305 gtk_widget_set_sensitive(choose_next, TRUE);
306 gtk_widget_set_sensitive(choose_prev, TRUE);
307 } else {
308 gtk_widget_set_sensitive(choose_next, FALSE);
309 gtk_widget_set_sensitive(choose_prev, FALSE);
a59663d4
RK
310 choose_n_search_results = 0;
311 choose_search_results = 0;
312 choose_n_search_references = 0;
313 choose_search_references = 0;
2a9a65e4 314 }
cfa78eaa
RK
315 event_raise("search-results-changed", 0);
316}
317
9eeb9f12
RK
318/** @brief Actually initiate a search */
319static void initiate_search(void) {
320 //fprintf(stderr, "initiate_search\n");
cfa78eaa
RK
321 /* If a search is in flight don't initiate a new one until it comes back */
322 if(choose_searching) {
323 choose_search_obsolete = 1;
324 return;
325 }
326 char *terms = xstrdup(gtk_entry_get_text(GTK_ENTRY(choose_search_entry)));
327 /* Strip leading and trailing space */
9eeb9f12
RK
328 while(*terms == ' ')
329 ++terms;
cfa78eaa 330 char *e = terms + strlen(terms);
9eeb9f12
RK
331 while(e > terms && e[-1] == ' ')
332 --e;
cfa78eaa 333 *e = 0;
9eeb9f12
RK
334 if(choose_search_terms && !strcmp(terms, choose_search_terms)) {
335 /* Search terms have not actually changed in any way that matters */
336 return;
337 }
338 /* Remember the current terms */
339 choose_search_terms = terms;
cfa78eaa
RK
340 if(!*terms) {
341 /* Nothing to search for. Fake a completion call. */
342 choose_search_completed(0, 0, 0, 0);
343 return;
344 }
345 if(disorder_eclient_search(client, choose_search_completed, terms, 0)) {
346 /* Bad search terms. Fake a completion call. */
347 choose_search_completed(0, 0, 0, 0);
348 return;
349 }
350 choose_searching = 1;
351}
352
9eeb9f12
RK
353static gboolean choose_search_timeout(gpointer attribute((unused)) data) {
354 struct timeval now;
355 xgettimeofday(&now, NULL);
356 /*fprintf(stderr, "%ld.%06d choose_search_timeout\n",
357 now.tv_sec, now.tv_usec);*/
358 if(tvdouble(now) - tvdouble(choose_search_last_keypress)
359 < SEARCH_DELAY_MS / 1000.0) {
360 //fprintf(stderr, " ... too soon\n");
361 return TRUE; /* Call me again later */
362 }
363 //fprintf(stderr, " ... let's go\n");
364 choose_search_last_keypress.tv_sec = 0;
365 choose_search_last_keypress.tv_usec = 0;
366 choose_search_timeout_id = 0;
367 initiate_search();
368 return FALSE;
369}
370
371/** @brief Called when the search entry changes */
372static void choose_search_entry_changed
373 (GtkEditable attribute((unused)) *editable,
374 gpointer attribute((unused)) user_data) {
375 xgettimeofday(&choose_search_last_keypress, NULL);
376 /*fprintf(stderr, "%ld.%06d choose_search_entry_changed\n",
377 choose_search_last_keypress.tv_sec,
378 choose_search_last_keypress.tv_usec);*/
379 /* If there's already a timeout, remove it */
380 if(choose_search_timeout_id) {
381 g_source_remove(choose_search_timeout_id);
382 choose_search_timeout_id = 0;
383 }
384 /* Add a new timeout */
385 choose_search_timeout_id = g_timeout_add(SEARCH_DELAY_MS / 10,
386 choose_search_timeout,
387 0);
388 /* We really wanted to tell Glib what time we wanted the callback at rather
389 * than asking for calls at given intervals. But there's no interface for
390 * that, and defining a new source for it seems like overkill if we can
391 * reasonably avoid it. */
392}
393
b96f65cf
RK
394/** @brief Identify first and last visible paths
395 *
396 * We'd like to use gtk_tree_view_get_visible_range() for this, but that was
397 * introduced in GTK+ 2.8, and Fink only has 2.6 (which is around three years
398 * out of date at time of writing), and I'm not yet prepared to rule out Fink
399 * support.
400 */
401static gboolean choose_get_visible_range(GtkTreeView *tree_view,
402 GtkTreePath **startpathp,
403 GtkTreePath **endpathp) {
404 GdkRectangle visible_tc[1];
405
406 /* Get the visible rectangle in tree coordinates */
407 gtk_tree_view_get_visible_rect(tree_view, visible_tc);
408 /*fprintf(stderr, "visible: %dx%x at %dx%d\n",
409 visible_tc->width, visible_tc->height,
410 visible_tc->x, visible_tc->y);*/
411 if(startpathp) {
412 /* Convert top-left visible point to widget coordinates */
413 int x_wc, y_wc;
414 gtk_tree_view_tree_to_widget_coords(tree_view,
415 visible_tc->x, visible_tc->y,
416 &x_wc, &y_wc);
417 //fprintf(stderr, " start widget coords: %dx%d\n", x_wc, y_wc);
418 gtk_tree_view_get_path_at_pos(tree_view,
419 x_wc, y_wc,
420 startpathp,
421 NULL,
422 NULL, NULL);
423 }
424 if(endpathp) {
425 /* Convert bottom-left visible point to widget coordinates */
426 /* Convert top-left visible point to widget coordinates */
427 int x_wc, y_wc;
428 gtk_tree_view_tree_to_widget_coords(tree_view,
429 visible_tc->x,
430 visible_tc->y + visible_tc->height - 1,
431 &x_wc, &y_wc);
432 //fprintf(stderr, " end widget coords: %dx%d\n", x_wc, y_wc);
433 gtk_tree_view_get_path_at_pos(tree_view,
434 x_wc, y_wc,
435 endpathp,
436 NULL,
437 NULL, NULL);
438 }
439 return TRUE;
440}
441
fd9faf16
RK
442/** @brief Move to the next/prev match
443 * @param direction -1 for prev, +1 for next
444 */
445static void choose_move(int direction) {
446 /* Refocus the main view so typahead find continues to work */
62dcb54f 447 gtk_widget_grab_focus(choose_view);
fd9faf16 448 /* If there's no results we have nothing to do */
a59663d4
RK
449 if(!choose_n_search_results)
450 return;
fd9faf16
RK
451 /* Compute bounds for searching over the array in the right direction */
452 const int first = direction > 0 ? 0 : choose_n_search_references - 1;
453 const int limit = direction > 0 ? choose_n_search_references : -1;
454 /* Find the first/last currently visible row */
455 GtkTreePath *limitpath;
456 if(!choose_get_visible_range(GTK_TREE_VIEW(choose_view),
457 direction < 0 ? &limitpath : 0,
458 direction > 0 ? &limitpath : 0))
b96f65cf 459 return;
fd9faf16
RK
460 /* Find a the first search result later/earlier than it. They're sorted so
461 * we could actually do much better than this if necessary. */
462 for(int n = first; n != limit; n += direction) {
b96f65cf
RK
463 GtkTreePath *path
464 = gtk_tree_row_reference_get_path(choose_search_references[n]);
465 if(!path)
466 continue;
fd9faf16
RK
467 /* gtk_tree_path_compare returns -1, 0 or 1 so we compare naively with
468 * direction */
469 if(gtk_tree_path_compare(limitpath, path) + direction == 0) {
b96f65cf
RK
470 choose_make_path_visible(path, 0.5);
471 gtk_tree_path_free(path);
472 return;
473 }
474 gtk_tree_path_free(path);
475 }
fd9faf16
RK
476 /* We didn't find one. Loop back to the first/las. */
477 for(int n = first; n != limit; n += direction) {
a59663d4
RK
478 GtkTreePath *path
479 = gtk_tree_row_reference_get_path(choose_search_references[n]);
480 if(!path)
481 continue;
482 choose_make_path_visible(path, 0.5);
483 gtk_tree_path_free(path);
484 return;
485 }
cfa78eaa
RK
486}
487
fd9faf16
RK
488void choose_next_clicked(GtkButton attribute((unused)) *button,
489 gpointer attribute((unused)) userdata) {
490 choose_move(1);
491}
492
a59663d4
RK
493void choose_prev_clicked(GtkButton attribute((unused)) *button,
494 gpointer attribute((unused)) userdata) {
fd9faf16 495 choose_move(-1);
cfa78eaa
RK
496}
497
9eeb9f12
RK
498/** @brief Called when the cancel search button is clicked */
499static void choose_clear_clicked(GtkButton attribute((unused)) *button,
500 gpointer attribute((unused)) userdata) {
501 gtk_entry_set_text(GTK_ENTRY(choose_search_entry), "");
62dcb54f 502 gtk_widget_grab_focus(choose_view);
9eeb9f12
RK
503 /* We start things off straight away in this case */
504 initiate_search();
505}
506
4d6a5c4e
RK
507/** @brief Called when the user hits ^F to start a new search */
508void choose_search_new(void) {
509 gtk_editable_select_region(GTK_EDITABLE(choose_search_entry), 0, -1);
510}
511
cfa78eaa
RK
512/** @brief Create the search widget */
513GtkWidget *choose_search_widget(void) {
514
515 /* Text entry box for search terms */
516 choose_search_entry = gtk_entry_new();
517 gtk_widget_set_style(choose_search_entry, tool_style);
518 g_signal_connect(choose_search_entry, "changed",
519 G_CALLBACK(choose_search_entry_changed), 0);
520 gtk_tooltips_set_tip(tips, choose_search_entry,
521 "Enter search terms here; search is automatic", "");
522
523 /* Cancel button to clear the search */
524 choose_clear = gtk_button_new_from_stock(GTK_STOCK_CANCEL);
525 gtk_widget_set_style(choose_clear, tool_style);
526 g_signal_connect(G_OBJECT(choose_clear), "clicked",
527 G_CALLBACK(choose_clear_clicked), 0);
528 gtk_tooltips_set_tip(tips, choose_clear, "Clear search terms", "");
529
530 /* Up and down buttons to find previous/next results; initially they are not
531 * usable as there are no search results. */
532 choose_prev = iconbutton("up.png", "Previous search result");
533 g_signal_connect(G_OBJECT(choose_prev), "clicked",
534 G_CALLBACK(choose_prev_clicked), 0);
535 gtk_widget_set_style(choose_prev, tool_style);
536 gtk_widget_set_sensitive(choose_prev, 0);
537 choose_next = iconbutton("down.png", "Next search result");
538 g_signal_connect(G_OBJECT(choose_next), "clicked",
539 G_CALLBACK(choose_next_clicked), 0);
540 gtk_widget_set_style(choose_next, tool_style);
541 gtk_widget_set_sensitive(choose_next, 0);
542
543 /* Pack the search tools button together on a line */
544 GtkWidget *hbox = gtk_hbox_new(FALSE/*homogeneous*/, 1/*spacing*/);
545 gtk_box_pack_start(GTK_BOX(hbox), choose_search_entry,
546 TRUE/*expand*/, TRUE/*fill*/, 0/*padding*/);
547 gtk_box_pack_start(GTK_BOX(hbox), choose_prev,
548 FALSE/*expand*/, FALSE/*fill*/, 0/*padding*/);
549 gtk_box_pack_start(GTK_BOX(hbox), choose_next,
550 FALSE/*expand*/, FALSE/*fill*/, 0/*padding*/);
551 gtk_box_pack_start(GTK_BOX(hbox), choose_clear,
552 FALSE/*expand*/, FALSE/*fill*/, 0/*padding*/);
553
554 return hbox;
555}
556
557/*
558Local Variables:
559c-basic-offset:2
560comment-column:40
561fill-column:79
562indent-tabs-mode:nil
563End:
564*/