c82ab59b67b7e7a5ee8c39175eb8f31cac385929
[disorder] / disobedience / choose-search.c
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 #include "disobedience.h"
21 #include "choose.h"
22
23 static GtkWidget *choose_search_entry;
24 static GtkWidget *choose_next;
25 static GtkWidget *choose_prev;
26 static GtkWidget *choose_clear;
27
28 /** @brief True if a search command is in flight */
29 static int choose_searching;
30
31 /** @brief True if in-flight search is now known to be obsolete */
32 static int choose_search_obsolete;
33
34 /** @brief Hash of all search result */
35 static hash *choose_search_hash;
36
37 /** @brief List of invisible search results
38 *
39 * This only lists search results not yet known to be visible, and is
40 * gradually depleted.
41 */
42 static char **choose_search_results;
43
44 /** @brief Length of @ref choose_search_results */
45 static int choose_n_search_results;
46
47 /** @brief Row references for search results */
48 static GtkTreeRowReference **choose_search_references;
49
50 /** @brief Length of @ref choose_search_references */
51 static int choose_n_search_references;
52
53 /** @brief Event handle for monitoring newly inserted tracks */
54 static event_handle choose_inserted_handle;
55
56 static void choose_search_entry_changed(GtkEditable *editable,
57 gpointer user_data);
58
59 int choose_is_search_result(const char *track) {
60 return choose_search_hash && hash_find(choose_search_hash, track);
61 }
62
63 /** @brief Called when the cancel search button is clicked */
64 static void choose_clear_clicked(GtkButton attribute((unused)) *button,
65 gpointer attribute((unused)) userdata) {
66 gtk_entry_set_text(GTK_ENTRY(choose_search_entry), "");
67 /* The changed signal will do the rest of the work for us */
68 }
69
70 static int is_prefix(const char *dir, const char *track) {
71 size_t nd = strlen(dir);
72
73 if(nd < strlen(track)
74 && track[nd] == '/'
75 && !strncmp(track, dir, nd))
76 return 1;
77 else
78 return 0;
79 }
80
81 /** @brief Do some work towards making @p track visible
82 * @return True if we made it visible or it was missing
83 */
84 static int choose_make_one_visible(const char *track) {
85 //fprintf(stderr, " choose_make_one_visible %s\n", track);
86 /* We walk through nodes at the top level looking for directories that are
87 * prefixes of the target track.
88 *
89 * - if we find one and it's expanded we walk through its children
90 * - if we find one and it's NOT expanded then we expand it, and arrange
91 * to be revisited
92 * - if we don't find one then we're probably out of date
93 */
94 GtkTreeIter it[1];
95 gboolean itv = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(choose_store),
96 it);
97 while(itv) {
98 const char *dir = choose_get_track(it);
99
100 //fprintf(stderr, " %s\n", dir);
101 if(!dir) {
102 /* Placeholder */
103 itv = gtk_tree_model_iter_next(GTK_TREE_MODEL(choose_store), it);
104 continue;
105 }
106 GtkTreePath *path = gtk_tree_model_get_path(GTK_TREE_MODEL(choose_store),
107 it);
108 if(!strcmp(dir, track)) {
109 /* We found the track. If everything above it was expanded, it will be
110 * too. So we can report it as visible. */
111 //fprintf(stderr, " found %s\n", track);
112 choose_search_references[choose_n_search_references++]
113 = gtk_tree_row_reference_new(GTK_TREE_MODEL(choose_store), path);
114 gtk_tree_path_free(path);
115 return 1;
116 }
117 if(is_prefix(dir, track)) {
118 /* We found a prefix of the target track. */
119 //fprintf(stderr, " is a prefix\n");
120 const gboolean expanded
121 = gtk_tree_view_row_expanded(GTK_TREE_VIEW(choose_view), path);
122 if(expanded) {
123 //fprintf(stderr, " is apparently expanded\n");
124 /* This directory is expanded, let's make like Augustus Gibbons and
125 * take it to the next level. */
126 GtkTreeIter child[1]; /* don't know if parent==iter allowed */
127 itv = gtk_tree_model_iter_children(GTK_TREE_MODEL(choose_store),
128 child,
129 it);
130 *it = *child;
131 if(choose_is_placeholder(it)) {
132 //fprintf(stderr, " %s is expanded, has a placeholder child\n", dir);
133 /* We assume that placeholder children of expanded rows are about to
134 * be replaced */
135 gtk_tree_path_free(path);
136 return 0;
137 }
138 } else {
139 //fprintf(stderr, " requesting expansion of %s\n", dir);
140 /* Track is below a non-expanded directory. So let's expand it.
141 * choose_make_visible() will arrange a revisit in due course. */
142 gtk_tree_view_expand_row(GTK_TREE_VIEW(choose_view),
143 path,
144 FALSE/*open_all*/);
145 gtk_tree_path_free(path);
146 /* TODO: the old version would remember which rows had been expanded
147 * just to show search results and collapse them again. We should
148 * probably do that. */
149 return 0;
150 }
151 } else
152 itv = gtk_tree_model_iter_next(GTK_TREE_MODEL(choose_store), it);
153 gtk_tree_path_free(path);
154 }
155 /* If we reach the end then we didn't find the track at all. */
156 fprintf(stderr, "choose_make_one_visible: could not find %s\n",
157 track);
158 return 1;
159 }
160
161 /** @brief Compare two GtkTreeRowReferences
162 *
163 * Not very efficient since it does multiple memory operations per
164 * comparison!
165 */
166 static int choose_compare_references(const void *av, const void *bv) {
167 GtkTreeRowReference *a = *(GtkTreeRowReference **)av;
168 GtkTreeRowReference *b = *(GtkTreeRowReference **)bv;
169 GtkTreePath *pa = gtk_tree_row_reference_get_path(a);
170 GtkTreePath *pb = gtk_tree_row_reference_get_path(b);
171 const int rc = gtk_tree_path_compare(pa, pb);
172 gtk_tree_path_free(pa);
173 gtk_tree_path_free(pb);
174 return rc;
175 }
176
177 /** @brief Move the cursor to @p ref
178 * @return 0 on success, nonzero if @p ref has gone stale
179 */
180 static int choose_set_cursor(GtkTreeRowReference *ref) {
181 GtkTreePath *path = gtk_tree_row_reference_get_path(ref);
182 if(!path)
183 return -1;
184 gtk_tree_view_set_cursor(GTK_TREE_VIEW(choose_view), path, NULL, FALSE);
185 gtk_tree_path_free(path);
186 return 0;
187 }
188
189 /** @brief Do some work towards ensuring that all search results are visible
190 *
191 * Assumes there's at least one results!
192 */
193 static void choose_make_visible(const char attribute((unused)) *event,
194 void attribute((unused)) *eventdata,
195 void attribute((unused)) *callbackdata) {
196 //fprintf(stderr, "choose_make_visible\n");
197 int remaining = 0;
198
199 for(int n = 0; n < choose_n_search_results; ++n) {
200 if(!choose_search_results[n])
201 continue;
202 if(choose_make_one_visible(choose_search_results[n]))
203 choose_search_results[n] = 0;
204 else
205 ++remaining;
206 }
207 //fprintf(stderr, "remaining=%d\n", remaining);
208 if(remaining) {
209 /* If there's work left to be done make sure we get a callback when
210 * something changes */
211 if(!choose_inserted_handle)
212 choose_inserted_handle = event_register("choose-inserted-tracks",
213 choose_make_visible, 0);
214 } else {
215 /* Suppress callbacks if there's nothing more to do */
216 event_cancel(choose_inserted_handle);
217 choose_inserted_handle = 0;
218 /* We've expanded everything, now we can mess with the cursor */
219 //fprintf(stderr, "sort %d references\n", choose_n_search_references);
220 qsort(choose_search_references,
221 choose_n_search_references,
222 sizeof (GtkTreeRowReference *),
223 choose_compare_references);
224 choose_set_cursor(choose_search_references[0]);
225 }
226 }
227
228 /** @brief Called with search results */
229 static void choose_search_completed(void attribute((unused)) *v,
230 const char *error,
231 int nvec, char **vec) {
232 //fprintf(stderr, "choose_search_completed\n");
233 if(error) {
234 popup_protocol_error(0, error);
235 return;
236 }
237 choose_searching = 0;
238 /* If the search was obsoleted initiate another one */
239 if(choose_search_obsolete) {
240 choose_search_obsolete = 0;
241 choose_search_entry_changed(0, 0);
242 return;
243 }
244 //fprintf(stderr, "*** %d search results\n", nvec);
245 choose_search_hash = hash_new(1);
246 if(nvec) {
247 for(int n = 0; n < nvec; ++n)
248 hash_add(choose_search_hash, vec[n], "", HASH_INSERT);
249 /* Stash results for choose_make_visible */
250 choose_n_search_results = nvec;
251 choose_search_results = vec;
252 /* Make a big-enough buffer for the results row reference list */
253 choose_n_search_references = 0;
254 choose_search_references = xcalloc(nvec, sizeof (GtkTreeRowReference *));
255 /* Start making rows visible */
256 choose_make_visible(0, 0, 0);
257 }
258 event_raise("search-results-changed", 0);
259 }
260
261 /** @brief Called when the search entry changes */
262 static void choose_search_entry_changed
263 (GtkEditable attribute((unused)) *editable,
264 gpointer attribute((unused)) user_data) {
265 //fprintf(stderr, "choose_search_entry_changed\n");
266 /* If a search is in flight don't initiate a new one until it comes back */
267 if(choose_searching) {
268 choose_search_obsolete = 1;
269 return;
270 }
271 char *terms = xstrdup(gtk_entry_get_text(GTK_ENTRY(choose_search_entry)));
272 /* Strip leading and trailing space */
273 while(*terms == ' ') ++terms;
274 char *e = terms + strlen(terms);
275 while(e > terms && e[-1] == ' ') --e;
276 *e = 0;
277 if(!*terms) {
278 /* Nothing to search for. Fake a completion call. */
279 choose_search_completed(0, 0, 0, 0);
280 return;
281 }
282 if(disorder_eclient_search(client, choose_search_completed, terms, 0)) {
283 /* Bad search terms. Fake a completion call. */
284 choose_search_completed(0, 0, 0, 0);
285 return;
286 }
287 choose_searching = 1;
288 }
289
290 static void choose_next_clicked(GtkButton attribute((unused)) *button,
291 gpointer attribute((unused)) userdata) {
292 fprintf(stderr, "next\n"); /* TODO */
293 }
294
295 static void choose_prev_clicked(GtkButton attribute((unused)) *button,
296 gpointer attribute((unused)) userdata) {
297 fprintf(stderr, "prev\n"); /* TODO */
298 }
299
300 /** @brief Create the search widget */
301 GtkWidget *choose_search_widget(void) {
302
303 /* Text entry box for search terms */
304 choose_search_entry = gtk_entry_new();
305 gtk_widget_set_style(choose_search_entry, tool_style);
306 g_signal_connect(choose_search_entry, "changed",
307 G_CALLBACK(choose_search_entry_changed), 0);
308 gtk_tooltips_set_tip(tips, choose_search_entry,
309 "Enter search terms here; search is automatic", "");
310
311 /* Cancel button to clear the search */
312 choose_clear = gtk_button_new_from_stock(GTK_STOCK_CANCEL);
313 gtk_widget_set_style(choose_clear, tool_style);
314 g_signal_connect(G_OBJECT(choose_clear), "clicked",
315 G_CALLBACK(choose_clear_clicked), 0);
316 gtk_tooltips_set_tip(tips, choose_clear, "Clear search terms", "");
317
318 /* Up and down buttons to find previous/next results; initially they are not
319 * usable as there are no search results. */
320 choose_prev = iconbutton("up.png", "Previous search result");
321 g_signal_connect(G_OBJECT(choose_prev), "clicked",
322 G_CALLBACK(choose_prev_clicked), 0);
323 gtk_widget_set_style(choose_prev, tool_style);
324 gtk_widget_set_sensitive(choose_prev, 0);
325 choose_next = iconbutton("down.png", "Next search result");
326 g_signal_connect(G_OBJECT(choose_next), "clicked",
327 G_CALLBACK(choose_next_clicked), 0);
328 gtk_widget_set_style(choose_next, tool_style);
329 gtk_widget_set_sensitive(choose_next, 0);
330
331 /* Pack the search tools button together on a line */
332 GtkWidget *hbox = gtk_hbox_new(FALSE/*homogeneous*/, 1/*spacing*/);
333 gtk_box_pack_start(GTK_BOX(hbox), choose_search_entry,
334 TRUE/*expand*/, TRUE/*fill*/, 0/*padding*/);
335 gtk_box_pack_start(GTK_BOX(hbox), choose_prev,
336 FALSE/*expand*/, FALSE/*fill*/, 0/*padding*/);
337 gtk_box_pack_start(GTK_BOX(hbox), choose_next,
338 FALSE/*expand*/, FALSE/*fill*/, 0/*padding*/);
339 gtk_box_pack_start(GTK_BOX(hbox), choose_clear,
340 FALSE/*expand*/, FALSE/*fill*/, 0/*padding*/);
341
342 return hbox;
343 }
344
345 /*
346 Local Variables:
347 c-basic-offset:2
348 comment-column:40
349 fill-column:79
350 indent-tabs-mode:nil
351 End:
352 */