chiark / gitweb /
typo fix
[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
22 *
23 * TODO:
24 * - cleverer focus to implement typeahead find
bb9bcc90 25 */
cfa78eaa
RK
26#include "disobedience.h"
27#include "choose.h"
28
bd802f22
RK
29int choose_auto_expanding;
30
a59663d4 31GtkWidget *choose_search_entry;
cfa78eaa
RK
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
9eeb9f12
RK
42/** @brief Current search terms */
43static char *choose_search_terms;
44
cfa78eaa
RK
45/** @brief Hash of all search result */
46static hash *choose_search_hash;
47
2a9a65e4
RK
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
9eeb9f12
RK
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
cfa78eaa
RK
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
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).
e9a72659
RK
196 *
197 * TODO: if the row is already visible do nothing.
b96f65cf
RK
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).
2a9a65e4 218 */
b96f65cf
RK
219static int choose_make_ref_visible(GtkTreeRowReference *ref,
220 gfloat row_align) {
2a9a65e4
RK
221 GtkTreePath *path = gtk_tree_row_reference_get_path(ref);
222 if(!path)
223 return -1;
b96f65cf 224 choose_make_path_visible(path, row_align);
2a9a65e4
RK
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)
cab9a17c 252 choose_inserted_handle = event_register("choose-more-tracks",
2a9a65e4
RK
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);
b96f65cf 264 choose_make_ref_visible(choose_search_references[0], 0.5);
2a9a65e4
RK
265 }
266}
267
cfa78eaa
RK
268/** @brief Called with search results */
269static void choose_search_completed(void attribute((unused)) *v,
270 const char *error,
271 int nvec, char **vec) {
2a9a65e4 272 //fprintf(stderr, "choose_search_completed\n");
cfa78eaa
RK
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) {
2a9a65e4 280 choose_search_obsolete = 0;
cfa78eaa
RK
281 choose_search_entry_changed(0, 0);
282 return;
283 }
2a9a65e4 284 //fprintf(stderr, "*** %d search results\n", nvec);
bd802f22
RK
285 /* We're actually going to use these search results. Autocollapse anything
286 * left over from the old search. */
287 choose_auto_collapse();
cfa78eaa 288 choose_search_hash = hash_new(1);
2a9a65e4
RK
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);
b96f65cf
RK
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);
a59663d4
RK
305 choose_n_search_results = 0;
306 choose_search_results = 0;
307 choose_n_search_references = 0;
308 choose_search_references = 0;
2a9a65e4 309 }
cfa78eaa
RK
310 event_raise("search-results-changed", 0);
311}
312
9eeb9f12
RK
313/** @brief Actually initiate a search */
314static void initiate_search(void) {
315 //fprintf(stderr, "initiate_search\n");
cfa78eaa
RK
316 /* If a search is in flight don't initiate a new one until it comes back */
317 if(choose_searching) {
318 choose_search_obsolete = 1;
319 return;
320 }
321 char *terms = xstrdup(gtk_entry_get_text(GTK_ENTRY(choose_search_entry)));
322 /* Strip leading and trailing space */
9eeb9f12
RK
323 while(*terms == ' ')
324 ++terms;
cfa78eaa 325 char *e = terms + strlen(terms);
9eeb9f12
RK
326 while(e > terms && e[-1] == ' ')
327 --e;
cfa78eaa 328 *e = 0;
9eeb9f12
RK
329 if(choose_search_terms && !strcmp(terms, choose_search_terms)) {
330 /* Search terms have not actually changed in any way that matters */
331 return;
332 }
333 /* Remember the current terms */
334 choose_search_terms = terms;
cfa78eaa
RK
335 if(!*terms) {
336 /* Nothing to search for. Fake a completion call. */
337 choose_search_completed(0, 0, 0, 0);
338 return;
339 }
340 if(disorder_eclient_search(client, choose_search_completed, terms, 0)) {
341 /* Bad search terms. Fake a completion call. */
342 choose_search_completed(0, 0, 0, 0);
343 return;
344 }
345 choose_searching = 1;
346}
347
9eeb9f12
RK
348static gboolean choose_search_timeout(gpointer attribute((unused)) data) {
349 struct timeval now;
350 xgettimeofday(&now, NULL);
351 /*fprintf(stderr, "%ld.%06d choose_search_timeout\n",
352 now.tv_sec, now.tv_usec);*/
353 if(tvdouble(now) - tvdouble(choose_search_last_keypress)
354 < SEARCH_DELAY_MS / 1000.0) {
355 //fprintf(stderr, " ... too soon\n");
356 return TRUE; /* Call me again later */
357 }
358 //fprintf(stderr, " ... let's go\n");
359 choose_search_last_keypress.tv_sec = 0;
360 choose_search_last_keypress.tv_usec = 0;
361 choose_search_timeout_id = 0;
362 initiate_search();
363 return FALSE;
364}
365
366/** @brief Called when the search entry changes */
367static void choose_search_entry_changed
368 (GtkEditable attribute((unused)) *editable,
369 gpointer attribute((unused)) user_data) {
370 xgettimeofday(&choose_search_last_keypress, NULL);
371 /*fprintf(stderr, "%ld.%06d choose_search_entry_changed\n",
372 choose_search_last_keypress.tv_sec,
373 choose_search_last_keypress.tv_usec);*/
374 /* If there's already a timeout, remove it */
375 if(choose_search_timeout_id) {
376 g_source_remove(choose_search_timeout_id);
377 choose_search_timeout_id = 0;
378 }
379 /* Add a new timeout */
380 choose_search_timeout_id = g_timeout_add(SEARCH_DELAY_MS / 10,
381 choose_search_timeout,
382 0);
383 /* We really wanted to tell Glib what time we wanted the callback at rather
384 * than asking for calls at given intervals. But there's no interface for
385 * that, and defining a new source for it seems like overkill if we can
386 * reasonably avoid it. */
387}
388
b96f65cf
RK
389/** @brief Identify first and last visible paths
390 *
391 * We'd like to use gtk_tree_view_get_visible_range() for this, but that was
392 * introduced in GTK+ 2.8, and Fink only has 2.6 (which is around three years
393 * out of date at time of writing), and I'm not yet prepared to rule out Fink
394 * support.
395 */
396static gboolean choose_get_visible_range(GtkTreeView *tree_view,
397 GtkTreePath **startpathp,
398 GtkTreePath **endpathp) {
399 GdkRectangle visible_tc[1];
400
401 /* Get the visible rectangle in tree coordinates */
402 gtk_tree_view_get_visible_rect(tree_view, visible_tc);
403 /*fprintf(stderr, "visible: %dx%x at %dx%d\n",
404 visible_tc->width, visible_tc->height,
405 visible_tc->x, visible_tc->y);*/
406 if(startpathp) {
407 /* Convert top-left visible point to widget coordinates */
408 int x_wc, y_wc;
409 gtk_tree_view_tree_to_widget_coords(tree_view,
410 visible_tc->x, visible_tc->y,
411 &x_wc, &y_wc);
412 //fprintf(stderr, " start widget coords: %dx%d\n", x_wc, y_wc);
413 gtk_tree_view_get_path_at_pos(tree_view,
414 x_wc, y_wc,
415 startpathp,
416 NULL,
417 NULL, NULL);
418 }
419 if(endpathp) {
420 /* Convert bottom-left visible point to widget coordinates */
421 /* Convert top-left visible point to widget coordinates */
422 int x_wc, y_wc;
423 gtk_tree_view_tree_to_widget_coords(tree_view,
424 visible_tc->x,
425 visible_tc->y + visible_tc->height - 1,
426 &x_wc, &y_wc);
427 //fprintf(stderr, " end widget coords: %dx%d\n", x_wc, y_wc);
428 gtk_tree_view_get_path_at_pos(tree_view,
429 x_wc, y_wc,
430 endpathp,
431 NULL,
432 NULL, NULL);
433 }
434 return TRUE;
435}
436
a59663d4
RK
437void choose_next_clicked(GtkButton attribute((unused)) *button,
438 gpointer attribute((unused)) userdata) {
439 if(!choose_n_search_results)
440 return;
b96f65cf
RK
441 /* Find the last visible row */
442 GtkTreePath *endpath;
443 gboolean endvalid = choose_get_visible_range(GTK_TREE_VIEW(choose_view),
444 NULL,
445 &endpath);
446 if(!endvalid)
447 return;
448 /* Find a the first search result later than it. They're sorted so we could
449 * actually do much better than this if necessary. */
450 for(int n = 0; n < choose_n_search_references; ++n) {
451 GtkTreePath *path
452 = gtk_tree_row_reference_get_path(choose_search_references[n]);
453 if(!path)
454 continue;
455 if(gtk_tree_path_compare(endpath, path) < 0) {
456 choose_make_path_visible(path, 0.5);
457 gtk_tree_path_free(path);
458 return;
459 }
460 gtk_tree_path_free(path);
461 }
a59663d4
RK
462 /* We didn't find one. Loop back to the first. */
463 for(int n = 0; n < choose_n_search_references; ++n) {
464 GtkTreePath *path
465 = gtk_tree_row_reference_get_path(choose_search_references[n]);
466 if(!path)
467 continue;
468 choose_make_path_visible(path, 0.5);
469 gtk_tree_path_free(path);
470 return;
471 }
cfa78eaa
RK
472}
473
a59663d4
RK
474void choose_prev_clicked(GtkButton attribute((unused)) *button,
475 gpointer attribute((unused)) userdata) {
b96f65cf 476 /* TODO can we de-dupe with choose_next_clicked? Probably yes. */
a59663d4
RK
477 if(!choose_n_search_results)
478 return;
b96f65cf
RK
479 /* Find the first visible row */
480 GtkTreePath *startpath;
481 gboolean startvalid = choose_get_visible_range(GTK_TREE_VIEW(choose_view),
482 &startpath,
483 NULL);
484 if(!startvalid)
485 return;
486 /* Find a the last search result earlier than it. They're sorted so we could
487 * actually do much better than this if necessary. */
488 for(int n = choose_n_search_references - 1; n >= 0; --n) {
489 GtkTreePath *path
490 = gtk_tree_row_reference_get_path(choose_search_references[n]);
491 if(!path)
492 continue;
493 if(gtk_tree_path_compare(startpath, path) > 0) {
494 choose_make_path_visible(path, 0.5);
495 gtk_tree_path_free(path);
496 return;
497 }
498 gtk_tree_path_free(path);
499 }
a59663d4
RK
500 /* We didn't find one. Loop down to the last. */
501 for(int n = choose_n_search_references - 1; n >= 0; --n) {
502 GtkTreePath *path
503 = gtk_tree_row_reference_get_path(choose_search_references[n]);
504 if(!path)
505 continue;
506 choose_make_path_visible(path, 0.5);
507 gtk_tree_path_free(path);
508 return;
509 }
cfa78eaa
RK
510}
511
9eeb9f12
RK
512/** @brief Called when the cancel search button is clicked */
513static void choose_clear_clicked(GtkButton attribute((unused)) *button,
514 gpointer attribute((unused)) userdata) {
515 gtk_entry_set_text(GTK_ENTRY(choose_search_entry), "");
516 /* We start things off straight away in this case */
517 initiate_search();
518}
519
4d6a5c4e
RK
520/** @brief Called when the user hits ^F to start a new search */
521void choose_search_new(void) {
522 gtk_editable_select_region(GTK_EDITABLE(choose_search_entry), 0, -1);
523}
524
cfa78eaa
RK
525/** @brief Create the search widget */
526GtkWidget *choose_search_widget(void) {
527
528 /* Text entry box for search terms */
529 choose_search_entry = gtk_entry_new();
530 gtk_widget_set_style(choose_search_entry, tool_style);
531 g_signal_connect(choose_search_entry, "changed",
532 G_CALLBACK(choose_search_entry_changed), 0);
533 gtk_tooltips_set_tip(tips, choose_search_entry,
534 "Enter search terms here; search is automatic", "");
535
536 /* Cancel button to clear the search */
537 choose_clear = gtk_button_new_from_stock(GTK_STOCK_CANCEL);
538 gtk_widget_set_style(choose_clear, tool_style);
539 g_signal_connect(G_OBJECT(choose_clear), "clicked",
540 G_CALLBACK(choose_clear_clicked), 0);
541 gtk_tooltips_set_tip(tips, choose_clear, "Clear search terms", "");
542
543 /* Up and down buttons to find previous/next results; initially they are not
544 * usable as there are no search results. */
545 choose_prev = iconbutton("up.png", "Previous search result");
546 g_signal_connect(G_OBJECT(choose_prev), "clicked",
547 G_CALLBACK(choose_prev_clicked), 0);
548 gtk_widget_set_style(choose_prev, tool_style);
549 gtk_widget_set_sensitive(choose_prev, 0);
550 choose_next = iconbutton("down.png", "Next search result");
551 g_signal_connect(G_OBJECT(choose_next), "clicked",
552 G_CALLBACK(choose_next_clicked), 0);
553 gtk_widget_set_style(choose_next, tool_style);
554 gtk_widget_set_sensitive(choose_next, 0);
555
556 /* Pack the search tools button together on a line */
557 GtkWidget *hbox = gtk_hbox_new(FALSE/*homogeneous*/, 1/*spacing*/);
558 gtk_box_pack_start(GTK_BOX(hbox), choose_search_entry,
559 TRUE/*expand*/, TRUE/*fill*/, 0/*padding*/);
560 gtk_box_pack_start(GTK_BOX(hbox), choose_prev,
561 FALSE/*expand*/, FALSE/*fill*/, 0/*padding*/);
562 gtk_box_pack_start(GTK_BOX(hbox), choose_next,
563 FALSE/*expand*/, FALSE/*fill*/, 0/*padding*/);
564 gtk_box_pack_start(GTK_BOX(hbox), choose_clear,
565 FALSE/*expand*/, FALSE/*fill*/, 0/*padding*/);
566
567 return hbox;
568}
569
570/*
571Local Variables:
572c-basic-offset:2
573comment-column:40
574fill-column:79
575indent-tabs-mode:nil
576End:
577*/