Add icon to process dialog button.
[Rockbox.git] / apps / tagtree.c
blob06150c1f26ff841481560bc0059ed3f0093ec3ae
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 by Miika Pekkarinen
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
20 /**
21 * Basic structure on this file was copied from dbtree.c and modified to
22 * support the tag cache interface.
24 #include <stdio.h>
25 #include <string.h>
26 #include <stdlib.h>
27 #include "config.h"
28 #include "system.h"
29 #include "kernel.h"
30 #include "splash.h"
31 #include "icons.h"
32 #include "tree.h"
33 #include "action.h"
34 #include "settings.h"
35 #include "tagcache.h"
36 #include "tagtree.h"
37 #include "lang.h"
38 #include "logf.h"
39 #include "playlist.h"
40 #include "keyboard.h"
41 #include "gui/list.h"
42 #include "buffer.h"
43 #include "atoi.h"
44 #include "playback.h"
45 #include "yesno.h"
46 #include "misc.h"
47 #include "filetypes.h"
49 #define FILE_SEARCH_INSTRUCTIONS ROCKBOX_DIR "/tagnavi.config"
51 static int tagtree_play_folder(struct tree_context* c);
53 static char searchstring[128];
55 enum variables {
56 var_sorttype = 100,
57 var_limit,
58 var_strip,
59 var_menu_start,
60 var_include,
61 var_rootmenu,
62 var_format,
63 menu_next,
64 menu_load,
67 /* Capacity 10 000 entries (for example 10k different artists) */
68 #define UNIQBUF_SIZE (64*1024)
69 static long *uniqbuf;
71 #define MAX_TAGS 5
72 #define MAX_MENU_ID_SIZE 32
74 static struct tagcache_search tcs, tcs2;
75 static bool sort_inverse;
78 * "%3d. %s" autoscore title %sort = "inverse" %limit = "100"
80 * valid = true
81 * formatstr = "%-3d. %s"
82 * tags[0] = tag_autoscore
83 * tags[1] = tag_title
84 * tag_count = 2
86 * limit = 100
87 * sort_inverse = true
89 struct display_format {
90 char name[32];
91 struct tagcache_search_clause *clause[TAGCACHE_MAX_CLAUSES];
92 int clause_count;
93 char *formatstr;
94 int group_id;
95 int tags[MAX_TAGS];
96 int tag_count;
98 int limit;
99 int strip;
100 bool sort_inverse;
101 bool sort;
104 static struct display_format *formats[TAGMENU_MAX_FMTS];
105 static int format_count;
107 struct search_instruction {
108 char name[64];
109 int tagorder[MAX_TAGS];
110 int tagorder_count;
111 struct tagcache_search_clause *clause[MAX_TAGS][TAGCACHE_MAX_CLAUSES];
112 int format_id[MAX_TAGS];
113 int clause_count[MAX_TAGS];
114 int result_seek[MAX_TAGS];
117 struct menu_entry {
118 char name[64];
119 int type;
120 struct search_instruction *si;
121 int link;
124 struct root_menu {
125 char title[64];
126 char id[MAX_MENU_ID_SIZE];
127 int itemcount;
128 struct menu_entry *items[TAGMENU_MAX_ITEMS];
131 /* Statusbar text of the current view. */
132 static char current_title[MAX_TAGS][128];
134 static struct root_menu *menus[TAGMENU_MAX_MENUS];
135 static struct root_menu *menu;
136 static struct search_instruction *csi;
137 static const char *strp;
138 static int menu_count;
139 static int root_menu;
141 static int current_offset;
142 static int current_entry_count;
144 static int format_count;
145 static struct tree_context *tc;
147 static int get_token_str(char *buf, int size)
149 /* Find the start. */
150 while (*strp != '"' && *strp != '\0')
151 strp++;
153 if (*strp == '\0' || *(++strp) == '\0')
154 return -1;
156 /* Read the data. */
157 while (*strp != '"' && *strp != '\0' && --size > 0)
158 *(buf++) = *(strp++);
160 *buf = '\0';
161 if (*strp != '"')
162 return -2;
164 strp++;
166 return 0;
169 #define MATCH(tag,str1,str2,settag) \
170 if (!strcasecmp(str1, str2)) { \
171 *tag = settag; \
172 return 1; \
175 static int get_tag(int *tag)
177 char buf[128];
178 int i;
180 /* Find the start. */
181 while ((*strp == ' ' || *strp == '>') && *strp != '\0')
182 strp++;
184 if (*strp == '\0' || *strp == '?')
185 return 0;
187 for (i = 0; i < (int)sizeof(buf)-1; i++)
189 if (*strp == '\0' || *strp == ' ')
190 break ;
191 buf[i] = *strp;
192 strp++;
194 buf[i] = '\0';
196 MATCH(tag, buf, "album", tag_album);
197 MATCH(tag, buf, "artist", tag_artist);
198 MATCH(tag, buf, "bitrate", tag_bitrate);
199 MATCH(tag, buf, "composer", tag_composer);
200 MATCH(tag, buf, "comment", tag_comment);
201 MATCH(tag, buf, "albumartist", tag_albumartist);
202 MATCH(tag, buf, "ensemble", tag_albumartist);
203 MATCH(tag, buf, "genre", tag_genre);
204 MATCH(tag, buf, "length", tag_length);
205 MATCH(tag, buf, "Lm", tag_virt_length_min);
206 MATCH(tag, buf, "Ls", tag_virt_length_sec);
207 MATCH(tag, buf, "Pm", tag_virt_playtime_min);
208 MATCH(tag, buf, "Ps", tag_virt_playtime_sec);
209 MATCH(tag, buf, "title", tag_title);
210 MATCH(tag, buf, "filename", tag_filename);
211 MATCH(tag, buf, "tracknum", tag_tracknumber);
212 MATCH(tag, buf, "discnum", tag_discnumber);
213 MATCH(tag, buf, "year", tag_year);
214 MATCH(tag, buf, "playcount", tag_playcount);
215 MATCH(tag, buf, "rating", tag_rating);
216 MATCH(tag, buf, "lastplayed", tag_lastplayed);
217 MATCH(tag, buf, "commitid", tag_commitid);
218 MATCH(tag, buf, "entryage", tag_virt_entryage);
219 MATCH(tag, buf, "autoscore", tag_virt_autoscore);
220 MATCH(tag, buf, "%sort", var_sorttype);
221 MATCH(tag, buf, "%limit", var_limit);
222 MATCH(tag, buf, "%strip", var_strip);
223 MATCH(tag, buf, "%menu_start", var_menu_start);
224 MATCH(tag, buf, "%include", var_include);
225 MATCH(tag, buf, "%root_menu", var_rootmenu);
226 MATCH(tag, buf, "%format", var_format);
227 MATCH(tag, buf, "->", menu_next);
228 MATCH(tag, buf, "==>", menu_load);
230 logf("NO MATCH: %s\n", buf);
231 if (buf[0] == '?')
232 return 0;
234 return -1;
237 static int get_clause(int *condition)
239 char buf[4];
240 int i;
242 /* Find the start. */
243 while (*strp == ' ' && *strp != '\0')
244 strp++;
246 if (*strp == '\0')
247 return 0;
249 for (i = 0; i < (int)sizeof(buf)-1; i++)
251 if (*strp == '\0' || *strp == ' ')
252 break ;
253 buf[i] = *strp;
254 strp++;
256 buf[i] = '\0';
258 MATCH(condition, buf, "=", clause_is);
259 MATCH(condition, buf, "==", clause_is);
260 MATCH(condition, buf, "!=", clause_is_not);
261 MATCH(condition, buf, ">", clause_gt);
262 MATCH(condition, buf, ">=", clause_gteq);
263 MATCH(condition, buf, "<", clause_lt);
264 MATCH(condition, buf, "<=", clause_lteq);
265 MATCH(condition, buf, "~", clause_contains);
266 MATCH(condition, buf, "!~", clause_not_contains);
267 MATCH(condition, buf, "^", clause_begins_with);
268 MATCH(condition, buf, "!^", clause_not_begins_with);
269 MATCH(condition, buf, "$", clause_ends_with);
270 MATCH(condition, buf, "!$", clause_not_ends_with);
271 MATCH(condition, buf, "@", clause_oneof);
273 return 0;
276 static bool read_clause(struct tagcache_search_clause *clause)
278 char buf[256];
280 if (get_tag(&clause->tag) <= 0)
281 return false;
283 if (get_clause(&clause->type) <= 0)
284 return false;
286 if (get_token_str(buf, sizeof buf) < 0)
287 return false;
289 clause->str = buffer_alloc(strlen(buf)+1);
290 strcpy(clause->str, buf);
292 logf("got clause: %d/%d [%s]", clause->tag, clause->type, clause->str);
294 if (*(clause->str) == '\0')
295 clause->input = true;
296 else
297 clause->input = false;
299 if (tagcache_is_numeric_tag(clause->tag))
301 clause->numeric = true;
302 clause->numeric_data = atoi(clause->str);
304 else
305 clause->numeric = false;
307 return true;
310 static bool read_variable(char *buf, int size)
312 int condition;
314 if (!get_clause(&condition))
315 return false;
317 if (condition != clause_is)
318 return false;
320 if (get_token_str(buf, size) < 0)
321 return false;
323 return true;
326 /* "%3d. %s" autoscore title %sort = "inverse" %limit = "100" */
327 static int get_format_str(struct display_format *fmt)
329 int ret;
330 char buf[128];
331 int i;
333 memset(fmt, 0, sizeof(struct display_format));
335 if (get_token_str(fmt->name, sizeof fmt->name) < 0)
336 return -10;
338 /* Determine the group id */
339 fmt->group_id = 0;
340 for (i = 0; i < format_count; i++)
342 if (!strcasecmp(formats[i]->name, fmt->name))
344 fmt->group_id = formats[i]->group_id;
345 break;
348 if (formats[i]->group_id > fmt->group_id)
349 fmt->group_id = formats[i]->group_id;
352 if (i == format_count)
353 fmt->group_id++;
355 logf("format: (%d) %s", fmt->group_id, fmt->name);
357 if (get_token_str(buf, sizeof buf) < 0)
358 return -10;
360 fmt->formatstr = buffer_alloc(strlen(buf) + 1);
361 strcpy(fmt->formatstr, buf);
363 while (fmt->tag_count < MAX_TAGS)
365 ret = get_tag(&fmt->tags[fmt->tag_count]);
366 if (ret < 0)
367 return -11;
369 if (ret == 0)
370 break;
372 switch (fmt->tags[fmt->tag_count]) {
373 case var_sorttype:
374 if (!read_variable(buf, sizeof buf))
375 return -12;
376 if (!strcasecmp("inverse", buf))
377 fmt->sort_inverse = true;
379 fmt->sort = true;
380 break;
382 case var_limit:
383 if (!read_variable(buf, sizeof buf))
384 return -13;
385 fmt->limit = atoi(buf);
386 break;
388 case var_strip:
389 if (!read_variable(buf, sizeof buf))
390 return -14;
391 fmt->strip = atoi(buf);
392 break;
394 default:
395 fmt->tag_count++;
399 return 1;
402 static int add_format(const char *buf)
404 strp = buf;
406 if (formats[format_count] == NULL)
407 formats[format_count] = buffer_alloc(sizeof(struct display_format));
409 memset(formats[format_count], 0, sizeof(struct display_format));
410 if (get_format_str(formats[format_count]) < 0)
412 logf("get_format_str() parser failed!");
413 return -4;
416 while (*strp != '\0' && *strp != '?')
417 strp++;
419 if (*strp == '?')
421 int clause_count = 0;
422 strp++;
424 while (1)
426 if (clause_count >= TAGCACHE_MAX_CLAUSES)
428 logf("too many clauses");
429 break;
432 formats[format_count]->clause[clause_count] =
433 buffer_alloc(sizeof(struct tagcache_search_clause));
435 if (!read_clause(formats[format_count]->clause[clause_count]))
436 break;
438 clause_count++;
441 formats[format_count]->clause_count = clause_count;
444 format_count++;
446 return 1;
449 static int get_condition(struct search_instruction *inst)
451 int clause_count;
452 char buf[128];
454 switch (*strp)
456 case '=':
458 int i;
460 if (get_token_str(buf, sizeof buf) < 0)
461 return -1;
463 for (i = 0; i < format_count; i++)
465 if (!strcasecmp(formats[i]->name, buf))
466 break;
469 if (i == format_count)
471 logf("format not found: %s", buf);
472 return -2;
475 inst->format_id[inst->tagorder_count] = formats[i]->group_id;
476 return 1;
478 case '?':
479 case ' ':
480 case '&':
481 strp++;
482 return 1;
483 case '-':
484 case '\0':
485 return 0;
488 clause_count = inst->clause_count[inst->tagorder_count];
489 if (clause_count >= TAGCACHE_MAX_CLAUSES)
491 logf("Too many clauses");
492 return false;
495 inst->clause[inst->tagorder_count][clause_count] =
496 buffer_alloc(sizeof(struct tagcache_search_clause));
498 if (!read_clause(inst->clause[inst->tagorder_count][clause_count]))
499 return -1;
501 inst->clause_count[inst->tagorder_count]++;
503 return 1;
506 /* example search:
507 * "Best" artist ? year >= "2000" & title !^ "crap" & genre = "good genre" \
508 * : album ? year >= "2000" : songs
509 * ^ begins with
510 * * contains
511 * $ ends with
514 static bool parse_search(struct menu_entry *entry, const char *str)
516 int ret;
517 int type;
518 struct search_instruction *inst = entry->si;
519 char buf[MAX_PATH];
520 int i;
521 struct root_menu *new_menu;
523 strp = str;
525 /* Parse entry name */
526 if (get_token_str(entry->name, sizeof entry->name) < 0)
528 logf("No name found.");
529 return false;
532 /* Parse entry type */
533 if (get_tag(&entry->type) <= 0)
534 return false;
536 if (entry->type == menu_load)
538 if (get_token_str(buf, sizeof buf) < 0)
539 return false;
541 /* Find the matching root menu or "create" it */
542 for (i = 0; i < menu_count; i++)
544 if (!strcasecmp(menus[i]->id, buf))
546 entry->link = i;
547 return true;
551 if (menu_count >= TAGMENU_MAX_MENUS)
553 logf("max menucount reached");
554 return false;
557 /* Allocate a new menu unless link is found. */
558 menus[menu_count] = buffer_alloc(sizeof(struct root_menu));
559 new_menu = menus[menu_count];
560 memset(new_menu, 0, sizeof(struct root_menu));
561 strncpy(new_menu->id, buf, MAX_MENU_ID_SIZE-1);
562 entry->link = menu_count;
563 ++menu_count;
565 return true;
568 if (entry->type != menu_next)
569 return false;
571 while (inst->tagorder_count < MAX_TAGS)
573 ret = get_tag(&inst->tagorder[inst->tagorder_count]);
574 if (ret < 0)
576 logf("Parse error #1");
577 logf("%s", strp);
578 return false;
581 if (ret == 0)
582 break ;
584 logf("tag: %d", inst->tagorder[inst->tagorder_count]);
586 while ( (ret = get_condition(inst)) > 0 ) ;
587 if (ret < 0)
588 return false;
590 inst->tagorder_count++;
592 if (get_tag(&type) <= 0 || type != menu_next)
593 break;
596 return true;
599 static int compare(const void *p1, const void *p2)
601 struct tagentry *e1 = (struct tagentry *)p1;
602 struct tagentry *e2 = (struct tagentry *)p2;
604 if (sort_inverse)
605 return strncasecmp(e2->name, e1->name, MAX_PATH);
607 return strncasecmp(e1->name, e2->name, MAX_PATH);
610 static void tagtree_buffer_event(struct mp3entry *id3, bool last_track)
612 (void)id3;
613 (void)last_track;
615 /* Do not gather data unless proper setting has been enabled. */
616 if (!global_settings.runtimedb)
617 return;
619 logf("be:%d%s", last_track, id3->path);
621 if (!tagcache_find_index(&tcs, id3->path))
623 logf("tc stat: not found: %s", id3->path);
624 return;
627 id3->playcount = tagcache_get_numeric(&tcs, tag_playcount);
628 if (!id3->rating)
629 id3->rating = tagcache_get_numeric(&tcs, tag_rating);
630 id3->lastplayed = tagcache_get_numeric(&tcs, tag_lastplayed);
631 id3->score = tagcache_get_numeric(&tcs, tag_virt_autoscore) / 10;
632 id3->playtime = tagcache_get_numeric(&tcs, tag_playtime);
634 /* Store our tagcache index pointer. */
635 id3->tagcache_idx = tcs.idx_id;
637 tagcache_search_finish(&tcs);
640 static void tagtree_unbuffer_event(struct mp3entry *id3, bool last_track)
642 (void)last_track;
643 long playcount;
644 long playtime;
645 long lastplayed;
647 /* Do not gather data unless proper setting has been enabled. */
648 if (!global_settings.runtimedb)
650 logf("runtimedb gathering not enabled");
651 return;
654 if (!id3->tagcache_idx)
656 logf("No tagcache index pointer found");
657 return;
660 /* Don't process unplayed tracks. */
661 if (id3->elapsed == 0)
663 logf("not logging unplayed track");
664 return;
667 playcount = id3->playcount + 1;
668 lastplayed = tagcache_increase_serial();
669 if (lastplayed < 0)
671 logf("incorrect tc serial:%ld", lastplayed);
672 return;
675 /* Ignore the last 15s (crossfade etc.) */
676 playtime = id3->playtime + MIN(id3->length, id3->elapsed + 15 * 1000);
678 logf("ube:%s", id3->path);
679 logf("-> %d/%ld/%ld", last_track, playcount, playtime);
680 logf("-> %ld/%ld/%ld", id3->elapsed, id3->length, MIN(id3->length, id3->elapsed + 15 * 1000));
682 /* Queue the updates to the tagcache system. */
683 tagcache_update_numeric(id3->tagcache_idx, tag_playcount, playcount);
684 tagcache_update_numeric(id3->tagcache_idx, tag_playtime, playtime);
685 tagcache_update_numeric(id3->tagcache_idx, tag_lastplayed, lastplayed);
688 bool tagtree_export(void)
690 gui_syncsplash(0, str(LANG_CREATING));
691 if (!tagcache_create_changelog(&tcs))
693 gui_syncsplash(HZ*2, str(LANG_FAILED));
696 return false;
699 bool tagtree_import(void)
701 gui_syncsplash(0, str(LANG_WAIT));
702 if (!tagcache_import_changelog())
704 gui_syncsplash(HZ*2, str(LANG_FAILED));
707 return false;
710 static bool parse_menu(const char *filename);
712 static int parse_line(int n, const char *buf, void *parameters)
714 char data[256];
715 int variable;
716 static bool read_menu;
717 int i;
719 (void)parameters;
721 logf("parse:%d/%s", n, buf);
723 /* First line, do initialisation. */
724 if (n == 0)
726 if (strcasecmp(TAGNAVI_VERSION, buf))
728 logf("Version mismatch");
729 return -1;
732 read_menu = false;
735 if (buf[0] == '#')
736 return 0;
738 if (buf[0] == '\0')
740 if (read_menu)
742 /* End the menu */
743 read_menu = false;
745 return 0;
748 if (!read_menu)
750 strp = buf;
751 if (get_tag(&variable) <= 0)
752 return 0;
754 switch (variable)
756 case var_format:
757 if (add_format(strp) < 0)
759 logf("Format add fail: %s", data);
761 break;
763 case var_include:
764 if (get_token_str(data, sizeof(data)) < 0)
766 logf("%%include empty");
767 return 0;
770 if (!parse_menu(data))
772 logf("Load menu fail: %s", data);
774 break;
776 case var_menu_start:
777 if (menu_count >= TAGMENU_MAX_MENUS)
779 logf("max menucount reached");
780 return 0;
783 if (get_token_str(data, sizeof data) < 0)
785 logf("%%menu_start id empty");
786 return 0;
789 menu = NULL;
790 for (i = 0; i < menu_count; i++)
792 if (!strcasecmp(menus[i]->id, data))
794 menu = menus[i];
798 if (menu == NULL)
800 menus[menu_count] = buffer_alloc(sizeof(struct root_menu));
801 menu = menus[menu_count];
802 ++menu_count;
803 memset(menu, 0, sizeof(struct root_menu));
804 strncpy(menu->id, data, MAX_MENU_ID_SIZE-1);
807 if (get_token_str(menu->title, sizeof(menu->title)) < 0)
809 logf("%%menu_start title empty");
810 return 0;
812 logf("menu: %s", menu->title);
813 read_menu = true;
814 break;
816 case var_rootmenu:
817 /* Only set root menu once. */
818 if (root_menu >= 0)
819 break;
821 if (get_token_str(data, sizeof(data)) < 0)
823 logf("%%root_menu empty");
824 return 0;
827 for (i = 0; i < menu_count; i++)
829 if (!strcasecmp(menus[i]->id, data))
831 root_menu = i;
834 break;
837 return 0;
840 if (menu->itemcount >= TAGMENU_MAX_ITEMS)
842 logf("max itemcount reached");
843 return 0;
846 /* Allocate */
847 if (menu->items[menu->itemcount] == NULL)
849 menu->items[menu->itemcount] = buffer_alloc(sizeof(struct menu_entry));
850 memset(menu->items[menu->itemcount], 0, sizeof(struct menu_entry));
851 menu->items[menu->itemcount]->si = buffer_alloc(sizeof(struct search_instruction));
854 memset(menu->items[menu->itemcount]->si, 0, sizeof(struct search_instruction));
855 if (!parse_search(menu->items[menu->itemcount], buf))
856 return 0;
858 menu->itemcount++;
860 return 0;
863 static bool parse_menu(const char *filename)
865 int fd;
866 char buf[1024];
868 if (menu_count >= TAGMENU_MAX_MENUS)
870 logf("max menucount reached");
871 return false;
874 fd = open(filename, O_RDONLY);
875 if (fd < 0)
877 logf("Search instruction file not found.");
878 return false;
881 /* Now read file for real, parsing into si */
882 fast_readline(fd, buf, sizeof buf, NULL, parse_line);
883 close(fd);
885 return true;
888 void tagtree_init(void)
890 format_count = 0;
891 menu_count = 0;
892 menu = NULL;
893 root_menu = -1;
894 parse_menu(FILE_SEARCH_INSTRUCTIONS);
896 /* If no root menu is set, assume it's the first single menu
897 * we have. That shouldn't normally happen. */
898 if (root_menu < 0)
899 root_menu = 0;
901 uniqbuf = buffer_alloc(UNIQBUF_SIZE);
902 audio_set_track_buffer_event(tagtree_buffer_event);
903 audio_set_track_unbuffer_event(tagtree_unbuffer_event);
906 static bool show_search_progress(bool init, int count)
908 static int last_tick = 0;
910 if (init)
912 last_tick = current_tick;
913 return true;
916 if (current_tick - last_tick > HZ/4)
918 gui_syncsplash(0, str(LANG_PLAYLIST_SEARCH_MSG), count,
919 #if CONFIG_KEYPAD == PLAYER_PAD
920 str(LANG_STOP_ABORT)
921 #else
922 str(LANG_OFF_ABORT)
923 #endif
925 if (action_userabort(TIMEOUT_NOBLOCK))
926 return false;
927 last_tick = current_tick;
928 yield();
931 return true;
934 static int format_str(struct tagcache_search *tcs, struct display_format *fmt,
935 char *buf, int buf_size)
937 char fmtbuf[8];
938 bool read_format = false;
939 int fmtbuf_pos = 0;
940 int parpos = 0;
941 int buf_pos = 0;
942 int i;
944 memset(buf, 0, buf_size);
945 for (i = 0; fmt->formatstr[i] != '\0'; i++)
947 if (fmt->formatstr[i] == '%')
949 read_format = true;
950 fmtbuf_pos = 0;
951 if (parpos >= fmt->tag_count)
953 logf("too many format tags");
954 return -1;
958 if (read_format)
960 fmtbuf[fmtbuf_pos++] = fmt->formatstr[i];
961 if (fmtbuf_pos >= buf_size)
963 logf("format parse error");
964 return -2;
967 if (fmt->formatstr[i] == 's')
969 fmtbuf[fmtbuf_pos] = '\0';
970 read_format = false;
971 if (fmt->tags[parpos] == tcs->type)
973 snprintf(&buf[buf_pos], buf_size - buf_pos, fmtbuf, tcs->result);
975 else
977 /* Need to fetch the tag data. */
978 if (!tagcache_retrieve(tcs, tcs->idx_id, fmt->tags[parpos],
979 &buf[buf_pos], buf_size - buf_pos))
981 logf("retrieve failed");
982 return -3;
985 buf_pos += strlen(&buf[buf_pos]);
986 parpos++;
988 else if (fmt->formatstr[i] == 'd')
990 fmtbuf[fmtbuf_pos] = '\0';
991 read_format = false;
992 snprintf(&buf[buf_pos], buf_size - buf_pos, fmtbuf,
993 tagcache_get_numeric(tcs, fmt->tags[parpos]));
994 buf_pos += strlen(&buf[buf_pos]);
995 parpos++;
997 continue;
1000 buf[buf_pos++] = fmt->formatstr[i];
1002 if (buf_pos - 1 >= buf_size)
1004 logf("buffer overflow");
1005 return -4;
1009 buf[buf_pos++] = '\0';
1011 return 0;
1014 static int retrieve_entries(struct tree_context *c, struct tagcache_search *tcs,
1015 int offset, bool init)
1017 struct tagentry *dptr = (struct tagentry *)c->dircache;
1018 struct display_format *fmt;
1019 int i;
1020 int namebufused = 0;
1021 int total_count = 0;
1022 int special_entry_count = 0;
1023 int level = c->currextra;
1024 int tag;
1025 bool sort = false;
1026 int sort_limit;
1027 int strip;
1029 if (init
1030 #ifdef HAVE_TC_RAMCACHE
1031 && !tagcache_is_ramcache()
1032 #endif
1035 show_search_progress(true, 0);
1036 gui_syncsplash(0, str(LANG_PLAYLIST_SEARCH_MSG),
1037 0, csi->name);
1040 if (c->currtable == allsubentries)
1042 tag = tag_title;
1043 level--;
1045 else
1046 tag = csi->tagorder[level];
1048 if (!tagcache_search(tcs, tag))
1049 return -1;
1051 /* Prevent duplicate entries in the search list. */
1052 tagcache_search_set_uniqbuf(tcs, uniqbuf, UNIQBUF_SIZE);
1054 if (level || csi->clause_count[0] || tagcache_is_numeric_tag(tag))
1055 sort = true;
1057 for (i = 0; i < level; i++)
1059 if (tagcache_is_numeric_tag(csi->tagorder[i]))
1061 static struct tagcache_search_clause cc;
1063 memset(&cc, 0, sizeof(struct tagcache_search_clause));
1064 cc.tag = csi->tagorder[i];
1065 cc.type = clause_is;
1066 cc.numeric = true;
1067 cc.numeric_data = csi->result_seek[i];
1068 tagcache_search_add_clause(tcs, &cc);
1070 else
1072 tagcache_search_add_filter(tcs, csi->tagorder[i],
1073 csi->result_seek[i]);
1077 for (i = 0; i <= level; i++)
1079 int j;
1081 for (j = 0; j < csi->clause_count[i]; j++)
1082 tagcache_search_add_clause(tcs, csi->clause[i][j]);
1085 current_offset = offset;
1086 current_entry_count = 0;
1087 c->dirfull = false;
1089 fmt = NULL;
1090 for (i = 0; i < format_count; i++)
1092 if (formats[i]->group_id == csi->format_id[level])
1093 fmt = formats[i];
1096 if (fmt)
1098 sort_inverse = fmt->sort_inverse;
1099 sort_limit = fmt->limit;
1100 strip = fmt->strip;
1102 /* Check if sorting is forced. */
1103 if (fmt->sort)
1104 sort = true;
1106 else
1108 sort_inverse = false;
1109 sort_limit = 0;
1110 strip = 0;
1113 if (tag != tag_title && tag != tag_filename)
1115 if (offset == 0)
1117 dptr->newtable = allsubentries;
1118 dptr->name = str(LANG_TAGNAVI_ALL_TRACKS);
1119 dptr++;
1120 current_entry_count++;
1122 special_entry_count++;
1125 total_count += special_entry_count;
1127 while (tagcache_get_next(tcs))
1129 if (total_count++ < offset)
1130 continue;
1132 dptr->newtable = navibrowse;
1133 if (tag == tag_title || tag == tag_filename)
1135 dptr->newtable = playtrack;
1136 dptr->extraseek = tcs->idx_id;
1138 else
1139 dptr->extraseek = tcs->result_seek;
1141 fmt = NULL;
1142 /* Check the format */
1143 for (i = 0; i < format_count; i++)
1145 if (formats[i]->group_id != csi->format_id[level])
1146 continue;
1148 if (tagcache_check_clauses(tcs, formats[i]->clause,
1149 formats[i]->clause_count))
1151 fmt = formats[i];
1152 break;
1156 if (!tcs->ramresult || fmt)
1158 char buf[MAX_PATH];
1160 if (fmt)
1162 if (format_str(tcs, fmt, buf, sizeof buf) < 0)
1164 logf("format_str() failed");
1165 return 0;
1169 dptr->name = &c->name_buffer[namebufused];
1170 if (fmt)
1171 namebufused += strlen(buf)+1;
1172 else
1173 namebufused += tcs->result_len;
1175 if (namebufused >= c->name_buffer_size)
1177 logf("chunk mode #2: %d", current_entry_count);
1178 c->dirfull = true;
1179 sort = false;
1180 break ;
1182 if (fmt)
1183 strcpy(dptr->name, buf);
1184 else
1185 strcpy(dptr->name, tcs->result);
1187 else
1188 dptr->name = tcs->result;
1190 dptr++;
1191 current_entry_count++;
1193 if (current_entry_count >= global_settings.max_files_in_dir)
1195 logf("chunk mode #3: %d", current_entry_count);
1196 c->dirfull = true;
1197 sort = false;
1198 break ;
1201 if (init && !tcs->ramsearch)
1203 if (!show_search_progress(false, i))
1205 tagcache_search_finish(tcs);
1206 return current_entry_count;
1211 if (sort)
1212 qsort(c->dircache + special_entry_count * c->dentry_size,
1213 current_entry_count - special_entry_count,
1214 c->dentry_size, compare);
1216 if (!init)
1218 tagcache_search_finish(tcs);
1219 return current_entry_count;
1222 while (tagcache_get_next(tcs))
1224 if (!tcs->ramsearch)
1226 if (!show_search_progress(false, total_count))
1227 break;
1229 total_count++;
1232 tagcache_search_finish(tcs);
1234 if (!sort && (sort_inverse || sort_limit))
1236 gui_syncsplash(HZ*4, str(LANG_SHOWDIR_BUFFER_FULL), total_count);
1237 logf("Too small dir buffer");
1238 return 0;
1241 if (sort_limit)
1242 total_count = MIN(total_count, sort_limit);
1244 if (strip)
1246 dptr = c->dircache;
1247 for (i = 0; i < total_count; i++, dptr++)
1249 int len = strlen(dptr->name);
1251 if (len < strip)
1252 continue;
1254 dptr->name = &dptr->name[strip];
1258 return total_count;
1261 static int load_root(struct tree_context *c)
1263 struct tagentry *dptr = (struct tagentry *)c->dircache;
1264 int i;
1266 tc = c;
1267 c->currtable = root;
1268 if (c->dirlevel == 0)
1269 c->currextra = root_menu;
1271 menu = menus[c->currextra];
1272 if (menu == NULL)
1273 return 0;
1275 for (i = 0; i < menu->itemcount; i++)
1277 dptr->name = menu->items[i]->name;
1278 switch (menu->items[i]->type)
1280 case menu_next:
1281 dptr->newtable = navibrowse;
1282 dptr->extraseek = i;
1283 break;
1285 case menu_load:
1286 dptr->newtable = root;
1287 dptr->extraseek = menu->items[i]->link;
1288 break;
1291 dptr++;
1294 current_offset = 0;
1295 current_entry_count = i;
1297 return i;
1300 int tagtree_load(struct tree_context* c)
1302 int count;
1303 int table = c->currtable;
1305 c->dentry_size = sizeof(struct tagentry);
1306 c->dirsindir = 0;
1308 if (!table)
1310 c->dirfull = false;
1311 table = root;
1312 c->currtable = table;
1313 c->currextra = root_menu;
1316 switch (table)
1318 case root:
1319 count = load_root(c);
1320 break;
1322 case allsubentries:
1323 case navibrowse:
1324 logf("navibrowse...");
1325 cpu_boost(true);
1326 count = retrieve_entries(c, &tcs, 0, true);
1327 cpu_boost(false);
1328 break;
1330 default:
1331 logf("Unsupported table %d\n", table);
1332 return -1;
1335 if (count < 0)
1337 c->dirlevel = 0;
1338 count = load_root(c);
1339 gui_syncsplash(HZ, str(LANG_TAGCACHE_BUSY));
1342 /* The _total_ numer of entries available. */
1343 c->dirlength = c->filesindir = count;
1345 return count;
1348 int tagtree_enter(struct tree_context* c)
1350 int rc = 0;
1351 struct tagentry *dptr;
1352 int newextra;
1353 int seek;
1355 dptr = tagtree_get_entry(c, c->selected_item);
1357 c->dirfull = false;
1358 newextra = dptr->newtable;
1359 seek = dptr->extraseek;
1361 if (c->dirlevel >= MAX_DIR_LEVELS)
1362 return 0;
1364 c->selected_item_history[c->dirlevel]=c->selected_item;
1365 c->table_history[c->dirlevel] = c->currtable;
1366 c->extra_history[c->dirlevel] = c->currextra;
1367 c->pos_history[c->dirlevel] = c->firstpos;
1368 c->dirlevel++;
1370 switch (c->currtable) {
1371 case root:
1372 c->currextra = newextra;
1374 if (newextra == root)
1376 menu = menus[seek];
1377 c->currextra = seek;
1380 else if (newextra == navibrowse)
1382 int i, j;
1384 csi = menu->items[seek]->si;
1385 c->currextra = 0;
1387 strncpy(current_title[c->currextra], dptr->name,
1388 sizeof(current_title[0]) - 1);
1390 /* Read input as necessary. */
1391 for (i = 0; i < csi->tagorder_count; i++)
1393 for (j = 0; j < csi->clause_count[i]; j++)
1395 if (!csi->clause[i][j]->input)
1396 continue;
1398 rc = kbd_input(searchstring, sizeof(searchstring));
1399 if (rc == -1 || !searchstring[0])
1401 tagtree_exit(c);
1402 return 0;
1405 if (csi->clause[i][j]->numeric)
1406 csi->clause[i][j]->numeric_data = atoi(searchstring);
1407 else
1408 csi->clause[i][j]->str = searchstring;
1412 c->currtable = newextra;
1414 break;
1416 case navibrowse:
1417 case allsubentries:
1418 if (newextra == playtrack)
1420 c->dirlevel--;
1421 /* about to create a new current playlist...
1422 allow user to cancel the operation */
1423 if (global_settings.warnon_erase_dynplaylist &&
1424 !global_settings.party_mode &&
1425 playlist_modified(NULL))
1427 char *lines[]={str(LANG_WARN_ERASEDYNPLAYLIST_PROMPT)};
1428 struct text_message message={lines, 1};
1430 if (gui_syncyesno_run(&message, NULL, NULL) != YESNO_YES)
1431 break;
1434 if (tagtree_play_folder(c) >= 0)
1435 rc = 2;
1436 break;
1439 c->currtable = newextra;
1440 csi->result_seek[c->currextra] = seek;
1441 if (c->currextra < csi->tagorder_count-1)
1442 c->currextra++;
1443 else
1444 c->dirlevel--;
1446 /* Update the statusbar title */
1447 strncpy(current_title[c->currextra], dptr->name,
1448 sizeof(current_title[0]) - 1);
1449 break;
1451 default:
1452 c->dirlevel--;
1453 break;
1456 c->selected_item=0;
1457 gui_synclist_select_item(&tree_lists, c->selected_item);
1459 return rc;
1462 void tagtree_exit(struct tree_context* c)
1464 c->dirfull = false;
1465 if (c->dirlevel > 0)
1466 c->dirlevel--;
1467 c->selected_item=c->selected_item_history[c->dirlevel];
1468 gui_synclist_select_item(&tree_lists, c->selected_item);
1469 c->currtable = c->table_history[c->dirlevel];
1470 c->currextra = c->extra_history[c->dirlevel];
1471 c->firstpos = c->pos_history[c->dirlevel];
1474 int tagtree_get_filename(struct tree_context* c, char *buf, int buflen)
1476 struct tagentry *entry;
1478 entry = tagtree_get_entry(c, c->selected_item);
1480 if (!tagcache_search(&tcs, tag_filename))
1481 return -1;
1483 if (!tagcache_retrieve(&tcs, entry->extraseek, tcs.type, buf, buflen))
1485 tagcache_search_finish(&tcs);
1486 return -2;
1489 tagcache_search_finish(&tcs);
1491 return 0;
1494 static bool insert_all_playlist(struct tree_context *c, int position, bool queue)
1496 int i;
1497 char buf[MAX_PATH];
1498 int from, to, direction;
1499 int files_left = c->filesindir;
1501 cpu_boost(true);
1502 if (!tagcache_search(&tcs, tag_filename))
1504 gui_syncsplash(HZ, str(LANG_TAGCACHE_BUSY));
1505 cpu_boost(false);
1506 return false;
1509 if (position == PLAYLIST_REPLACE)
1511 if (remove_all_tracks(NULL) == 0)
1512 position = PLAYLIST_INSERT_LAST;
1513 else return -1; }
1515 if (position == PLAYLIST_INSERT_FIRST)
1517 from = c->filesindir - 1;
1518 to = -1;
1519 direction = -1;
1521 else
1523 from = 0;
1524 to = c->filesindir;
1525 direction = 1;
1528 for (i = from; i != to; i += direction)
1530 /* Count back to zero */
1531 if (!show_search_progress(false, files_left--))
1532 break;
1534 if (!tagcache_retrieve(&tcs, tagtree_get_entry(c, i)->extraseek,
1535 tcs.type, buf, sizeof buf))
1537 continue;
1540 if (playlist_insert_track(NULL, buf, position, queue, false) < 0)
1542 logf("playlist_insert_track failed");
1543 break;
1545 yield();
1547 playlist_sync(NULL);
1548 tagcache_search_finish(&tcs);
1549 cpu_boost(false);
1551 return true;
1554 bool tagtree_insert_selection_playlist(int position, bool queue)
1556 struct tagentry *dptr;
1557 char buf[MAX_PATH];
1558 int dirlevel = tc->dirlevel;
1560 /* We need to set the table to allsubentries. */
1561 show_search_progress(true, 0);
1563 dptr = tagtree_get_entry(tc, tc->selected_item);
1565 /* Insert a single track? */
1566 if (dptr->newtable == playtrack)
1568 if (tagtree_get_filename(tc, buf, sizeof buf) < 0)
1570 logf("tagtree_get_filename failed");
1571 return false;
1573 playlist_insert_track(NULL, buf, position, queue, true);
1575 return true;
1578 if (dptr->newtable == navibrowse)
1580 tagtree_enter(tc);
1581 tagtree_load(tc);
1582 dptr = tagtree_get_entry(tc, tc->selected_item);
1584 else if (dptr->newtable != allsubentries)
1586 logf("unsupported table: %d", dptr->newtable);
1587 return false;
1590 /* Now the current table should be allsubentries. */
1591 if (dptr->newtable != playtrack)
1593 tagtree_enter(tc);
1594 tagtree_load(tc);
1595 dptr = tagtree_get_entry(tc, tc->selected_item);
1597 /* And now the newtable should be playtrack. */
1598 if (dptr->newtable != playtrack)
1600 logf("newtable: %d !!", dptr->newtable);
1601 tc->dirlevel = dirlevel;
1602 return false;
1606 if (tc->filesindir <= 0)
1607 gui_syncsplash(HZ, str(LANG_END_PLAYLIST_PLAYER));
1608 else
1610 logf("insert_all_playlist");
1611 if (!insert_all_playlist(tc, position, queue))
1612 gui_syncsplash(HZ*2, str(LANG_FAILED));
1615 /* Finally return the dirlevel to its original value. */
1616 while (tc->dirlevel > dirlevel)
1617 tagtree_exit(tc);
1618 tagtree_load(tc);
1620 return true;
1623 static int tagtree_play_folder(struct tree_context* c)
1625 if (playlist_create(NULL, NULL) < 0)
1627 logf("Failed creating playlist\n");
1628 return -1;
1631 if (!insert_all_playlist(c, PLAYLIST_INSERT_LAST, false))
1632 return -2;
1634 if (global_settings.playlist_shuffle)
1635 c->selected_item = playlist_shuffle(current_tick, c->selected_item);
1636 if (!global_settings.play_selected)
1637 c->selected_item = 0;
1638 gui_synclist_select_item(&tree_lists, c->selected_item);
1640 playlist_start(c->selected_item,0);
1642 return 0;
1645 struct tagentry* tagtree_get_entry(struct tree_context *c, int id)
1647 struct tagentry *entry = (struct tagentry *)c->dircache;
1648 int realid = id - current_offset;
1650 /* Load the next chunk if necessary. */
1651 if (realid >= current_entry_count || realid < 0)
1653 cpu_boost(true);
1654 if (retrieve_entries(c, &tcs2, MAX(0, id - (current_entry_count / 2)),
1655 false) < 0)
1657 logf("retrieve failed");
1658 cpu_boost(false);
1659 return NULL;
1661 realid = id - current_offset;
1662 cpu_boost(false);
1665 return &entry[realid];
1668 char *tagtree_get_title(struct tree_context* c)
1670 switch (c->currtable)
1672 case root:
1673 return menu->title;
1675 case navibrowse:
1676 case allsubentries:
1677 return current_title[c->currextra];
1680 return "?";
1683 int tagtree_get_attr(struct tree_context* c)
1685 int attr = -1;
1686 switch (c->currtable)
1688 case navibrowse:
1689 if (csi->tagorder[c->currextra] == tag_title)
1690 attr = FILE_ATTR_AUDIO;
1691 else
1692 attr = ATTR_DIRECTORY;
1693 break;
1695 case allsubentries:
1696 attr = FILE_ATTR_AUDIO;
1697 break;
1699 default:
1700 attr = ATTR_DIRECTORY;
1701 break;
1704 return attr;
1707 int tagtree_get_icon(struct tree_context* c)
1709 int icon = Icon_Folder;
1711 if (tagtree_get_attr(c) == FILE_ATTR_AUDIO)
1712 icon = Icon_Audio;
1714 return icon;