2 * This file is part of DisOrder
3 * Copyright (C) 2008 Richard Kettlewell
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.
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.
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
20 #include "disobedience.h"
23 static GtkWidget
*choose_search_entry
;
24 static GtkWidget
*choose_next
;
25 static GtkWidget
*choose_prev
;
26 static GtkWidget
*choose_clear
;
28 /** @brief True if a search command is in flight */
29 static int choose_searching
;
31 /** @brief True if in-flight search is now known to be obsolete */
32 static int choose_search_obsolete
;
34 /** @brief Hash of all search result */
35 static hash
*choose_search_hash
;
37 /** @brief List of invisible search results
39 * This only lists search results not yet known to be visible, and is
42 static char **choose_search_results
;
44 /** @brief Length of @ref choose_search_results */
45 static int choose_n_search_results
;
47 /** @brief Row references for search results */
48 static GtkTreeRowReference
**choose_search_references
;
50 /** @brief Length of @ref choose_search_references */
51 static int choose_n_search_references
;
53 /** @brief Event handle for monitoring newly inserted tracks */
54 static event_handle choose_inserted_handle
;
56 static void choose_search_entry_changed(GtkEditable
*editable
,
59 int choose_is_search_result(const char *track
) {
60 return choose_search_hash
&& hash_find(choose_search_hash
, track
);
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 */
70 static int is_prefix(const char *dir
, const char *track
) {
71 size_t nd
= strlen(dir
);
75 && !strncmp(track
, dir
, nd
))
81 /** @brief Do some work towards making @p track visible
82 * @return True if we made it visible or it was missing
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.
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
92 * - if we don't find one then we're probably out of date
95 gboolean itv
= gtk_tree_model_get_iter_first(GTK_TREE_MODEL(choose_store
),
98 const char *dir
= choose_get_track(it
);
100 //fprintf(stderr, " %s\n", dir);
103 itv
= gtk_tree_model_iter_next(GTK_TREE_MODEL(choose_store
), it
);
106 GtkTreePath
*path
= gtk_tree_model_get_path(GTK_TREE_MODEL(choose_store
),
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
);
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
);
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
),
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
135 gtk_tree_path_free(path
);
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
),
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. */
152 itv
= gtk_tree_model_iter_next(GTK_TREE_MODEL(choose_store
), it
);
153 gtk_tree_path_free(path
);
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",
161 /** @brief Compare two GtkTreeRowReferences
163 * Not very efficient since it does multiple memory operations per
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
);
177 /** @brief Move the cursor to @p ref
178 * @return 0 on success, nonzero if @p ref has gone stale
180 static int choose_set_cursor(GtkTreeRowReference
*ref
) {
181 GtkTreePath
*path
= gtk_tree_row_reference_get_path(ref
);
184 gtk_tree_view_set_cursor(GTK_TREE_VIEW(choose_view
), path
, NULL
, FALSE
);
185 gtk_tree_path_free(path
);
189 /** @brief Do some work towards ensuring that all search results are visible
191 * Assumes there's at least one results!
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");
199 for(int n
= 0; n
< choose_n_search_results
; ++n
) {
200 if(!choose_search_results
[n
])
202 if(choose_make_one_visible(choose_search_results
[n
]))
203 choose_search_results
[n
] = 0;
207 //fprintf(stderr, "remaining=%d\n", 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);
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]);
228 /** @brief Called with search results */
229 static void choose_search_completed(void attribute((unused
)) *v
,
231 int nvec
, char **vec
) {
232 //fprintf(stderr, "choose_search_completed\n");
234 popup_protocol_error(0, error
);
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);
244 //fprintf(stderr, "*** %d search results\n", nvec);
245 choose_search_hash
= hash_new(1);
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);
258 event_raise("search-results-changed", 0);
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;
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
;
278 /* Nothing to search for. Fake a completion call. */
279 choose_search_completed(0, 0, 0, 0);
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);
287 choose_searching
= 1;
290 static void choose_next_clicked(GtkButton
attribute((unused
)) *button
,
291 gpointer
attribute((unused
)) userdata
) {
292 fprintf(stderr
, "next\n"); /* TODO */
295 static void choose_prev_clicked(GtkButton
attribute((unused
)) *button
,
296 gpointer
attribute((unused
)) userdata
) {
297 fprintf(stderr
, "prev\n"); /* TODO */
300 /** @brief Create the search widget */
301 GtkWidget
*choose_search_widget(void) {
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", "");
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", "");
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);
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*/);