Correct spelling and add a couple translations
[kugel-rb/myfork.git] / apps / tagtree.c
blob0a1cce024761e60ea6bef20ca718fd351f13ff47
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 by Miika Pekkarinen
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 /**
23 * Basic structure on this file was copied from dbtree.c and modified to
24 * support the tag cache interface.
26 #include <stdio.h>
27 #include <string.h>
28 #include <stdlib.h>
29 #include "config.h"
30 #include "system.h"
31 #include "kernel.h"
32 #include "splash.h"
33 #include "icons.h"
34 #include "tree.h"
35 #include "action.h"
36 #include "settings.h"
37 #include "tagcache.h"
38 #include "tagtree.h"
39 #include "lang.h"
40 #include "logf.h"
41 #include "playlist.h"
42 #include "keyboard.h"
43 #include "gui/list.h"
44 #include "buffer.h"
45 #include "playback.h"
46 #include "yesno.h"
47 #include "misc.h"
48 #include "filetypes.h"
49 #include "audio.h"
50 #include "appevents.h"
51 #include "storage.h"
52 #include "dir_uncached.h"
54 #define FILE_SEARCH_INSTRUCTIONS ROCKBOX_DIR "/tagnavi.config"
56 static int tagtree_play_folder(struct tree_context* c);
58 #define SEARCHSTR_SIZE 256
60 static const struct id3_to_search_mapping {
61 char *string;
62 size_t id3_offset;
63 } id3_to_search_mapping[] = {
64 { "", 0 }, /* offset n/a */
65 { "#directory#", 0 }, /* offset n/a */
66 { "#title#", offsetof(struct mp3entry, title) },
67 { "#artist#", offsetof(struct mp3entry, artist) },
68 { "#album#", offsetof(struct mp3entry, album) },
69 { "#genre#", offsetof(struct mp3entry, genre_string) },
70 { "#composer#", offsetof(struct mp3entry, composer) },
71 { "#albumartist#", offsetof(struct mp3entry, albumartist) },
73 enum variables {
74 var_sorttype = 100,
75 var_limit,
76 var_strip,
77 var_menu_start,
78 var_include,
79 var_rootmenu,
80 var_format,
81 menu_next,
82 menu_load,
85 /* Capacity 10 000 entries (for example 10k different artists) */
86 #define UNIQBUF_SIZE (64*1024)
87 static long *uniqbuf;
89 #define MAX_TAGS 5
90 #define MAX_MENU_ID_SIZE 32
92 static struct tagcache_search tcs, tcs2;
93 static bool sort_inverse;
96 * "%3d. %s" autoscore title %sort = "inverse" %limit = "100"
98 * valid = true
99 * formatstr = "%-3d. %s"
100 * tags[0] = tag_autoscore
101 * tags[1] = tag_title
102 * tag_count = 2
104 * limit = 100
105 * sort_inverse = true
107 struct display_format {
108 char name[32];
109 struct tagcache_search_clause *clause[TAGCACHE_MAX_CLAUSES];
110 int clause_count;
111 char *formatstr;
112 int group_id;
113 int tags[MAX_TAGS];
114 int tag_count;
116 int limit;
117 int strip;
118 bool sort_inverse;
121 static struct display_format *formats[TAGMENU_MAX_FMTS];
122 static int format_count;
124 struct search_instruction {
125 char name[64];
126 int tagorder[MAX_TAGS];
127 int tagorder_count;
128 struct tagcache_search_clause *clause[MAX_TAGS][TAGCACHE_MAX_CLAUSES];
129 int format_id[MAX_TAGS];
130 int clause_count[MAX_TAGS];
131 int result_seek[MAX_TAGS];
134 struct menu_entry {
135 char name[64];
136 int type;
137 struct search_instruction *si;
138 int link;
141 struct root_menu {
142 char title[64];
143 char id[MAX_MENU_ID_SIZE];
144 int itemcount;
145 struct menu_entry *items[TAGMENU_MAX_ITEMS];
148 /* Statusbar text of the current view. */
149 static char current_title[MAX_TAGS][128];
151 static struct root_menu *menus[TAGMENU_MAX_MENUS];
152 static struct root_menu *menu;
153 static struct search_instruction *csi;
154 static const char *strp;
155 static int menu_count;
156 static int root_menu;
158 static int current_offset;
159 static int current_entry_count;
161 static int format_count;
162 static struct tree_context *tc;
164 static int get_token_str(char *buf, int size)
166 /* Find the start. */
167 while (*strp != '"' && *strp != '\0')
168 strp++;
170 if (*strp == '\0' || *(++strp) == '\0')
171 return -1;
173 /* Read the data. */
174 while (*strp != '"' && *strp != '\0' && --size > 0)
175 *(buf++) = *(strp++);
177 *buf = '\0';
178 if (*strp != '"')
179 return -2;
181 strp++;
183 return 0;
186 #define MATCH(tag,str1,str2,settag) \
187 if (!strcasecmp(str1, str2)) { \
188 *tag = settag; \
189 return 1; \
192 static int get_tag(int *tag)
194 char buf[128];
195 int i;
197 /* Find the start. */
198 while ((*strp == ' ' || *strp == '>') && *strp != '\0')
199 strp++;
201 if (*strp == '\0' || *strp == '?')
202 return 0;
204 for (i = 0; i < (int)sizeof(buf)-1; i++)
206 if (*strp == '\0' || *strp == ' ')
207 break ;
208 buf[i] = *strp;
209 strp++;
211 buf[i] = '\0';
213 MATCH(tag, buf, "album", tag_album);
214 MATCH(tag, buf, "artist", tag_artist);
215 MATCH(tag, buf, "bitrate", tag_bitrate);
216 MATCH(tag, buf, "composer", tag_composer);
217 MATCH(tag, buf, "comment", tag_comment);
218 MATCH(tag, buf, "albumartist", tag_albumartist);
219 MATCH(tag, buf, "ensemble", tag_albumartist);
220 MATCH(tag, buf, "grouping", tag_grouping);
221 MATCH(tag, buf, "genre", tag_genre);
222 MATCH(tag, buf, "length", tag_length);
223 MATCH(tag, buf, "Lm", tag_virt_length_min);
224 MATCH(tag, buf, "Ls", tag_virt_length_sec);
225 MATCH(tag, buf, "Pm", tag_virt_playtime_min);
226 MATCH(tag, buf, "Ps", tag_virt_playtime_sec);
227 MATCH(tag, buf, "title", tag_title);
228 MATCH(tag, buf, "filename", tag_filename);
229 MATCH(tag, buf, "tracknum", tag_tracknumber);
230 MATCH(tag, buf, "discnum", tag_discnumber);
231 MATCH(tag, buf, "year", tag_year);
232 MATCH(tag, buf, "playcount", tag_playcount);
233 MATCH(tag, buf, "rating", tag_rating);
234 MATCH(tag, buf, "lastplayed", tag_lastplayed);
235 MATCH(tag, buf, "commitid", tag_commitid);
236 MATCH(tag, buf, "entryage", tag_virt_entryage);
237 MATCH(tag, buf, "autoscore", tag_virt_autoscore);
238 MATCH(tag, buf, "%sort", var_sorttype);
239 MATCH(tag, buf, "%limit", var_limit);
240 MATCH(tag, buf, "%strip", var_strip);
241 MATCH(tag, buf, "%menu_start", var_menu_start);
242 MATCH(tag, buf, "%include", var_include);
243 MATCH(tag, buf, "%root_menu", var_rootmenu);
244 MATCH(tag, buf, "%format", var_format);
245 MATCH(tag, buf, "->", menu_next);
246 MATCH(tag, buf, "==>", menu_load);
248 logf("NO MATCH: %s\n", buf);
249 if (buf[0] == '?')
250 return 0;
252 return -1;
255 static int get_clause(int *condition)
257 char buf[4];
258 int i;
260 /* Find the start. */
261 while (*strp == ' ' && *strp != '\0')
262 strp++;
264 if (*strp == '\0')
265 return 0;
267 for (i = 0; i < (int)sizeof(buf)-1; i++)
269 if (*strp == '\0' || *strp == ' ')
270 break ;
271 buf[i] = *strp;
272 strp++;
274 buf[i] = '\0';
276 MATCH(condition, buf, "=", clause_is);
277 MATCH(condition, buf, "==", clause_is);
278 MATCH(condition, buf, "!=", clause_is_not);
279 MATCH(condition, buf, ">", clause_gt);
280 MATCH(condition, buf, ">=", clause_gteq);
281 MATCH(condition, buf, "<", clause_lt);
282 MATCH(condition, buf, "<=", clause_lteq);
283 MATCH(condition, buf, "~", clause_contains);
284 MATCH(condition, buf, "!~", clause_not_contains);
285 MATCH(condition, buf, "^", clause_begins_with);
286 MATCH(condition, buf, "!^", clause_not_begins_with);
287 MATCH(condition, buf, "$", clause_ends_with);
288 MATCH(condition, buf, "!$", clause_not_ends_with);
289 MATCH(condition, buf, "@", clause_oneof);
291 return 0;
294 static bool read_clause(struct tagcache_search_clause *clause)
296 char buf[SEARCHSTR_SIZE];
297 unsigned int i;
299 if (get_tag(&clause->tag) <= 0)
300 return false;
302 if (get_clause(&clause->type) <= 0)
303 return false;
305 if (get_token_str(buf, sizeof buf) < 0)
306 return false;
308 for (i=0; i<ARRAYLEN(id3_to_search_mapping); i++)
310 if (!strcasecmp(buf, id3_to_search_mapping[i].string))
311 break;
314 if (i<ARRAYLEN(id3_to_search_mapping)) /* runtime search operand found */
316 clause->source = source_runtime+i;
317 clause->str = buffer_alloc(SEARCHSTR_SIZE);
319 else
321 clause->source = source_constant;
322 clause->str = buffer_alloc(strlen(buf)+1);
323 strcpy(clause->str, buf);
326 if (tagcache_is_numeric_tag(clause->tag))
328 clause->numeric = true;
329 clause->numeric_data = atoi(clause->str);
331 else
332 clause->numeric = false;
334 logf("got clause: %d/%d [%s]", clause->tag, clause->type, clause->str);
336 return true;
339 static bool read_variable(char *buf, int size)
341 int condition;
343 if (!get_clause(&condition))
344 return false;
346 if (condition != clause_is)
347 return false;
349 if (get_token_str(buf, size) < 0)
350 return false;
352 return true;
355 /* "%3d. %s" autoscore title %sort = "inverse" %limit = "100" */
356 static int get_format_str(struct display_format *fmt)
358 int ret;
359 char buf[128];
360 int i;
362 memset(fmt, 0, sizeof(struct display_format));
364 if (get_token_str(fmt->name, sizeof fmt->name) < 0)
365 return -10;
367 /* Determine the group id */
368 fmt->group_id = 0;
369 for (i = 0; i < format_count; i++)
371 if (!strcasecmp(formats[i]->name, fmt->name))
373 fmt->group_id = formats[i]->group_id;
374 break;
377 if (formats[i]->group_id > fmt->group_id)
378 fmt->group_id = formats[i]->group_id;
381 if (i == format_count)
382 fmt->group_id++;
384 logf("format: (%d) %s", fmt->group_id, fmt->name);
386 if (get_token_str(buf, sizeof buf) < 0)
387 return -10;
389 fmt->formatstr = buffer_alloc(strlen(buf) + 1);
390 strcpy(fmt->formatstr, buf);
392 while (fmt->tag_count < MAX_TAGS)
394 ret = get_tag(&fmt->tags[fmt->tag_count]);
395 if (ret < 0)
396 return -11;
398 if (ret == 0)
399 break;
401 switch (fmt->tags[fmt->tag_count]) {
402 case var_sorttype:
403 if (!read_variable(buf, sizeof buf))
404 return -12;
405 if (!strcasecmp("inverse", buf))
406 fmt->sort_inverse = true;
407 break;
409 case var_limit:
410 if (!read_variable(buf, sizeof buf))
411 return -13;
412 fmt->limit = atoi(buf);
413 break;
415 case var_strip:
416 if (!read_variable(buf, sizeof buf))
417 return -14;
418 fmt->strip = atoi(buf);
419 break;
421 default:
422 fmt->tag_count++;
426 return 1;
429 static int add_format(const char *buf)
431 strp = buf;
433 if (formats[format_count] == NULL)
434 formats[format_count] = buffer_alloc(sizeof(struct display_format));
436 memset(formats[format_count], 0, sizeof(struct display_format));
437 if (get_format_str(formats[format_count]) < 0)
439 logf("get_format_str() parser failed!");
440 return -4;
443 while (*strp != '\0' && *strp != '?')
444 strp++;
446 if (*strp == '?')
448 int clause_count = 0;
449 strp++;
451 while (1)
453 if (clause_count >= TAGCACHE_MAX_CLAUSES)
455 logf("too many clauses");
456 break;
459 formats[format_count]->clause[clause_count] =
460 buffer_alloc(sizeof(struct tagcache_search_clause));
462 if (!read_clause(formats[format_count]->clause[clause_count]))
463 break;
465 clause_count++;
468 formats[format_count]->clause_count = clause_count;
471 format_count++;
473 return 1;
476 static int get_condition(struct search_instruction *inst)
478 int clause_count;
479 char buf[128];
481 switch (*strp)
483 case '=':
485 int i;
487 if (get_token_str(buf, sizeof buf) < 0)
488 return -1;
490 for (i = 0; i < format_count; i++)
492 if (!strcasecmp(formats[i]->name, buf))
493 break;
496 if (i == format_count)
498 logf("format not found: %s", buf);
499 return -2;
502 inst->format_id[inst->tagorder_count] = formats[i]->group_id;
503 return 1;
505 case '?':
506 case ' ':
507 case '&':
508 strp++;
509 return 1;
510 case '-':
511 case '\0':
512 return 0;
515 clause_count = inst->clause_count[inst->tagorder_count];
516 if (clause_count >= TAGCACHE_MAX_CLAUSES)
518 logf("Too many clauses");
519 return false;
522 inst->clause[inst->tagorder_count][clause_count] =
523 buffer_alloc(sizeof(struct tagcache_search_clause));
525 if (!read_clause(inst->clause[inst->tagorder_count][clause_count]))
526 return -1;
528 inst->clause_count[inst->tagorder_count]++;
530 return 1;
533 /* example search:
534 * "Best" artist ? year >= "2000" & title !^ "crap" & genre = "good genre" \
535 * : album ? year >= "2000" : songs
536 * ^ begins with
537 * * contains
538 * $ ends with
541 static bool parse_search(struct menu_entry *entry, const char *str)
543 int ret;
544 int type;
545 struct search_instruction *inst = entry->si;
546 char buf[MAX_PATH];
547 int i;
548 struct root_menu *new_menu;
550 strp = str;
552 /* Parse entry name */
553 if (get_token_str(entry->name, sizeof entry->name) < 0)
555 logf("No name found.");
556 return false;
559 /* Parse entry type */
560 if (get_tag(&entry->type) <= 0)
561 return false;
563 if (entry->type == menu_load)
565 if (get_token_str(buf, sizeof buf) < 0)
566 return false;
568 /* Find the matching root menu or "create" it */
569 for (i = 0; i < menu_count; i++)
571 if (!strcasecmp(menus[i]->id, buf))
573 entry->link = i;
574 return true;
578 if (menu_count >= TAGMENU_MAX_MENUS)
580 logf("max menucount reached");
581 return false;
584 /* Allocate a new menu unless link is found. */
585 menus[menu_count] = buffer_alloc(sizeof(struct root_menu));
586 new_menu = menus[menu_count];
587 memset(new_menu, 0, sizeof(struct root_menu));
588 strncpy(new_menu->id, buf, MAX_MENU_ID_SIZE-1);
589 entry->link = menu_count;
590 ++menu_count;
592 return true;
595 if (entry->type != menu_next)
596 return false;
598 while (inst->tagorder_count < MAX_TAGS)
600 ret = get_tag(&inst->tagorder[inst->tagorder_count]);
601 if (ret < 0)
603 logf("Parse error #1");
604 logf("%s", strp);
605 return false;
608 if (ret == 0)
609 break ;
611 logf("tag: %d", inst->tagorder[inst->tagorder_count]);
613 while ( (ret = get_condition(inst)) > 0 ) ;
614 if (ret < 0)
615 return false;
617 inst->tagorder_count++;
619 if (get_tag(&type) <= 0 || type != menu_next)
620 break;
623 return true;
626 static int compare(const void *p1, const void *p2)
628 struct tagentry *e1 = (struct tagentry *)p1;
629 struct tagentry *e2 = (struct tagentry *)p2;
631 if (sort_inverse)
632 return strncasecmp(e2->name, e1->name, MAX_PATH);
634 return strncasecmp(e1->name, e2->name, MAX_PATH);
637 static void tagtree_buffer_event(struct mp3entry *id3)
639 /* Do not gather data unless proper setting has been enabled. */
640 if (!global_settings.runtimedb)
641 return;
643 logf("be:%s", id3->path);
645 if (!tagcache_find_index(&tcs, id3->path))
647 logf("tc stat: not found: %s", id3->path);
648 return;
651 id3->playcount = tagcache_get_numeric(&tcs, tag_playcount);
652 if (!id3->rating)
653 id3->rating = tagcache_get_numeric(&tcs, tag_rating);
654 id3->lastplayed = tagcache_get_numeric(&tcs, tag_lastplayed);
655 id3->score = tagcache_get_numeric(&tcs, tag_virt_autoscore) / 10;
656 id3->playtime = tagcache_get_numeric(&tcs, tag_playtime);
658 /* Store our tagcache index pointer. */
659 id3->tagcache_idx = tcs.idx_id+1;
661 tagcache_search_finish(&tcs);
664 static void tagtree_track_finish_event(struct mp3entry *id3)
666 long playcount;
667 long playtime;
668 long lastplayed;
669 long tagcache_idx;
671 /* Do not gather data unless proper setting has been enabled. */
672 if (!global_settings.runtimedb)
674 logf("runtimedb gathering not enabled");
675 return;
678 tagcache_idx=id3->tagcache_idx;
679 if (!tagcache_idx)
681 logf("No tagcache index pointer found");
682 return;
684 tagcache_idx--;
686 /* Don't process unplayed tracks. */
687 if (id3->elapsed == 0)
689 logf("not logging unplayed track");
690 return;
693 playcount = id3->playcount + 1;
694 lastplayed = tagcache_increase_serial();
695 if (lastplayed < 0)
697 logf("incorrect tc serial:%ld", lastplayed);
698 return;
701 /* Ignore the last 15s (crossfade etc.) */
702 playtime = id3->playtime + MIN(id3->length, id3->elapsed + 15 * 1000);
704 logf("ube:%s", id3->path);
705 logf("-> %ld/%ld", playcount, playtime);
706 logf("-> %ld/%ld/%ld", id3->elapsed, id3->length, MIN(id3->length, id3->elapsed + 15 * 1000));
708 /* Queue the updates to the tagcache system. */
709 tagcache_update_numeric(tagcache_idx, tag_playcount, playcount);
710 tagcache_update_numeric(tagcache_idx, tag_playtime, playtime);
711 tagcache_update_numeric(tagcache_idx, tag_lastplayed, lastplayed);
714 bool tagtree_export(void)
716 splash(0, str(LANG_CREATING));
717 if (!tagcache_create_changelog(&tcs))
719 splash(HZ*2, ID2P(LANG_FAILED));
722 return false;
725 bool tagtree_import(void)
727 splash(0, ID2P(LANG_WAIT));
728 if (!tagcache_import_changelog())
730 splash(HZ*2, ID2P(LANG_FAILED));
733 return false;
736 static bool parse_menu(const char *filename);
738 static int parse_line(int n, const char *buf, void *parameters)
740 char data[256];
741 int variable;
742 static bool read_menu;
743 int i;
745 (void)parameters;
747 logf("parse:%d/%s", n, buf);
749 /* First line, do initialisation. */
750 if (n == 0)
752 if (strcasecmp(TAGNAVI_VERSION, buf))
754 logf("Version mismatch");
755 return -1;
758 read_menu = false;
761 if (buf[0] == '#')
762 return 0;
764 if (buf[0] == '\0')
766 if (read_menu)
768 /* End the menu */
769 read_menu = false;
771 return 0;
774 if (!read_menu)
776 strp = buf;
777 if (get_tag(&variable) <= 0)
778 return 0;
780 switch (variable)
782 case var_format:
783 if (add_format(strp) < 0)
785 logf("Format add fail: %s", data);
787 break;
789 case var_include:
790 if (get_token_str(data, sizeof(data)) < 0)
792 logf("%%include empty");
793 return 0;
796 if (!parse_menu(data))
798 logf("Load menu fail: %s", data);
800 break;
802 case var_menu_start:
803 if (menu_count >= TAGMENU_MAX_MENUS)
805 logf("max menucount reached");
806 return 0;
809 if (get_token_str(data, sizeof data) < 0)
811 logf("%%menu_start id empty");
812 return 0;
815 menu = NULL;
816 for (i = 0; i < menu_count; i++)
818 if (!strcasecmp(menus[i]->id, data))
820 menu = menus[i];
824 if (menu == NULL)
826 menus[menu_count] = buffer_alloc(sizeof(struct root_menu));
827 menu = menus[menu_count];
828 ++menu_count;
829 memset(menu, 0, sizeof(struct root_menu));
830 strncpy(menu->id, data, MAX_MENU_ID_SIZE-1);
833 if (get_token_str(menu->title, sizeof(menu->title)) < 0)
835 logf("%%menu_start title empty");
836 return 0;
838 logf("menu: %s", menu->title);
839 read_menu = true;
840 break;
842 case var_rootmenu:
843 /* Only set root menu once. */
844 if (root_menu >= 0)
845 break;
847 if (get_token_str(data, sizeof(data)) < 0)
849 logf("%%root_menu empty");
850 return 0;
853 for (i = 0; i < menu_count; i++)
855 if (!strcasecmp(menus[i]->id, data))
857 root_menu = i;
860 break;
863 return 0;
866 if (menu->itemcount >= TAGMENU_MAX_ITEMS)
868 logf("max itemcount reached");
869 return 0;
872 /* Allocate */
873 if (menu->items[menu->itemcount] == NULL)
875 menu->items[menu->itemcount] = buffer_alloc(sizeof(struct menu_entry));
876 memset(menu->items[menu->itemcount], 0, sizeof(struct menu_entry));
877 menu->items[menu->itemcount]->si = buffer_alloc(sizeof(struct search_instruction));
880 memset(menu->items[menu->itemcount]->si, 0, sizeof(struct search_instruction));
881 if (!parse_search(menu->items[menu->itemcount], buf))
882 return 0;
884 menu->itemcount++;
886 return 0;
889 static bool parse_menu(const char *filename)
891 int fd;
892 char buf[1024];
894 if (menu_count >= TAGMENU_MAX_MENUS)
896 logf("max menucount reached");
897 return false;
900 fd = open(filename, O_RDONLY);
901 if (fd < 0)
903 logf("Search instruction file not found.");
904 return false;
907 /* Now read file for real, parsing into si */
908 fast_readline(fd, buf, sizeof buf, NULL, parse_line);
909 close(fd);
911 return true;
914 void tagtree_init(void)
916 format_count = 0;
917 menu_count = 0;
918 menu = NULL;
919 root_menu = -1;
920 parse_menu(FILE_SEARCH_INSTRUCTIONS);
922 /* If no root menu is set, assume it's the first single menu
923 * we have. That shouldn't normally happen. */
924 if (root_menu < 0)
925 root_menu = 0;
927 uniqbuf = buffer_alloc(UNIQBUF_SIZE);
929 add_event(PLAYBACK_EVENT_TRACK_BUFFER, false, tagtree_buffer_event);
930 add_event(PLAYBACK_EVENT_TRACK_FINISH, false, tagtree_track_finish_event);
933 static bool show_search_progress(bool init, int count)
935 static int last_tick = 0;
937 /* Don't show splashes for 1/2 second after starting search */
938 if (init)
940 last_tick = current_tick + HZ/2;
941 return true;
944 /* Update progress every 1/10 of a second */
945 if (current_tick - last_tick > HZ/10)
947 splashf(0, str(LANG_PLAYLIST_SEARCH_MSG), count, str(LANG_OFF_ABORT));
948 if (action_userabort(TIMEOUT_NOBLOCK))
949 return false;
950 last_tick = current_tick;
951 yield();
954 return true;
957 static int format_str(struct tagcache_search *tcs, struct display_format *fmt,
958 char *buf, int buf_size)
960 char fmtbuf[8];
961 bool read_format = false;
962 int fmtbuf_pos = 0;
963 int parpos = 0;
964 int buf_pos = 0;
965 int i;
967 memset(buf, 0, buf_size);
968 for (i = 0; fmt->formatstr[i] != '\0'; i++)
970 if (fmt->formatstr[i] == '%')
972 read_format = true;
973 fmtbuf_pos = 0;
974 if (parpos >= fmt->tag_count)
976 logf("too many format tags");
977 return -1;
981 if (read_format)
983 fmtbuf[fmtbuf_pos++] = fmt->formatstr[i];
984 if (fmtbuf_pos >= buf_size)
986 logf("format parse error");
987 return -2;
990 if (fmt->formatstr[i] == 's')
992 fmtbuf[fmtbuf_pos] = '\0';
993 read_format = false;
994 if (fmt->tags[parpos] == tcs->type)
996 snprintf(&buf[buf_pos], buf_size - buf_pos, fmtbuf, tcs->result);
998 else
1000 /* Need to fetch the tag data. */
1001 if (!tagcache_retrieve(tcs, tcs->idx_id, fmt->tags[parpos],
1002 &buf[buf_pos], buf_size - buf_pos))
1004 logf("retrieve failed");
1005 return -3;
1008 buf_pos += strlen(&buf[buf_pos]);
1009 parpos++;
1011 else if (fmt->formatstr[i] == 'd')
1013 fmtbuf[fmtbuf_pos] = '\0';
1014 read_format = false;
1015 snprintf(&buf[buf_pos], buf_size - buf_pos, fmtbuf,
1016 tagcache_get_numeric(tcs, fmt->tags[parpos]));
1017 buf_pos += strlen(&buf[buf_pos]);
1018 parpos++;
1020 continue;
1023 buf[buf_pos++] = fmt->formatstr[i];
1025 if (buf_pos - 1 >= buf_size)
1027 logf("buffer overflow");
1028 return -4;
1032 buf[buf_pos++] = '\0';
1034 return 0;
1037 static int retrieve_entries(struct tree_context *c, struct tagcache_search *tcs,
1038 int offset, bool init)
1040 struct tagentry *dptr = (struct tagentry *)c->dircache;
1041 struct display_format *fmt;
1042 int i;
1043 int namebufused = 0;
1044 int total_count = 0;
1045 int special_entry_count = 0;
1046 int level = c->currextra;
1047 int tag;
1048 bool sort = false;
1049 int sort_limit;
1050 int strip;
1052 if (init
1053 #ifdef HAVE_TC_RAMCACHE
1054 && !tagcache_is_ramcache()
1055 #endif
1058 /* Show search progress straight away if the disk needs to spin up,
1059 otherwise show it after the normal 1/2 second delay */
1060 show_search_progress(
1061 #ifdef HAVE_DISK_STORAGE
1062 storage_disk_is_active()
1063 #else
1064 true
1065 #endif
1066 , 0);
1069 if (c->currtable == allsubentries)
1071 tag = tag_title;
1072 level--;
1074 else
1075 tag = csi->tagorder[level];
1077 if (!tagcache_search(tcs, tag))
1078 return -1;
1080 /* Prevent duplicate entries in the search list. */
1081 tagcache_search_set_uniqbuf(tcs, uniqbuf, UNIQBUF_SIZE);
1083 if (level || csi->clause_count[0] || tagcache_is_numeric_tag(tag))
1084 sort = true;
1086 for (i = 0; i < level; i++)
1088 if (tagcache_is_numeric_tag(csi->tagorder[i]))
1090 static struct tagcache_search_clause cc;
1092 memset(&cc, 0, sizeof(struct tagcache_search_clause));
1093 cc.tag = csi->tagorder[i];
1094 cc.type = clause_is;
1095 cc.numeric = true;
1096 cc.numeric_data = csi->result_seek[i];
1097 tagcache_search_add_clause(tcs, &cc);
1099 else
1101 tagcache_search_add_filter(tcs, csi->tagorder[i],
1102 csi->result_seek[i]);
1106 for (i = 0; i <= level; i++)
1108 int j;
1110 for (j = 0; j < csi->clause_count[i]; j++)
1111 tagcache_search_add_clause(tcs, csi->clause[i][j]);
1114 current_offset = offset;
1115 current_entry_count = 0;
1116 c->dirfull = false;
1118 fmt = NULL;
1119 for (i = 0; i < format_count; i++)
1121 if (formats[i]->group_id == csi->format_id[level])
1122 fmt = formats[i];
1125 if (fmt)
1127 sort_inverse = fmt->sort_inverse;
1128 sort_limit = fmt->limit;
1129 strip = fmt->strip;
1130 sort = true;
1132 else
1134 sort_inverse = false;
1135 sort_limit = 0;
1136 strip = 0;
1139 if (tag != tag_title && tag != tag_filename)
1141 if (offset == 0)
1143 dptr->newtable = allsubentries;
1144 dptr->name = str(LANG_TAGNAVI_ALL_TRACKS);
1145 dptr++;
1146 current_entry_count++;
1148 if (offset <= 1)
1150 dptr->newtable = navibrowse;
1151 dptr->name = str(LANG_TAGNAVI_RANDOM);
1152 dptr->extraseek = -1;
1153 dptr++;
1154 current_entry_count++;
1156 special_entry_count+=2;
1159 total_count += special_entry_count;
1161 while (tagcache_get_next(tcs))
1163 if (total_count++ < offset)
1164 continue;
1166 dptr->newtable = navibrowse;
1167 if (tag == tag_title || tag == tag_filename)
1169 dptr->newtable = playtrack;
1170 dptr->extraseek = tcs->idx_id;
1172 else
1173 dptr->extraseek = tcs->result_seek;
1175 fmt = NULL;
1176 /* Check the format */
1177 for (i = 0; i < format_count; i++)
1179 if (formats[i]->group_id != csi->format_id[level])
1180 continue;
1182 if (tagcache_check_clauses(tcs, formats[i]->clause,
1183 formats[i]->clause_count))
1185 fmt = formats[i];
1186 break;
1190 if (!tcs->ramresult || fmt)
1192 char buf[MAX_PATH];
1194 if (fmt)
1196 if (format_str(tcs, fmt, buf, sizeof buf) < 0)
1198 logf("format_str() failed");
1199 return 0;
1203 dptr->name = &c->name_buffer[namebufused];
1204 if (fmt)
1205 namebufused += strlen(buf)+1;
1206 else
1207 namebufused += tcs->result_len;
1209 if (namebufused >= c->name_buffer_size)
1211 logf("chunk mode #2: %d", current_entry_count);
1212 c->dirfull = true;
1213 sort = false;
1214 break ;
1216 if (fmt)
1217 strcpy(dptr->name, buf);
1218 else
1219 strcpy(dptr->name, tcs->result);
1221 else
1222 dptr->name = tcs->result;
1224 dptr++;
1225 current_entry_count++;
1227 if (current_entry_count >= global_settings.max_files_in_dir)
1229 logf("chunk mode #3: %d", current_entry_count);
1230 c->dirfull = true;
1231 sort = false;
1232 break ;
1235 if (init && !tcs->ramsearch)
1237 if (!show_search_progress(false, total_count))
1239 tagcache_search_finish(tcs);
1240 return current_entry_count;
1245 if (sort)
1246 qsort(c->dircache + special_entry_count * c->dentry_size,
1247 current_entry_count - special_entry_count,
1248 c->dentry_size, compare);
1250 if (!init)
1252 tagcache_search_finish(tcs);
1253 return current_entry_count;
1256 while (tagcache_get_next(tcs))
1258 if (!tcs->ramsearch)
1260 if (!show_search_progress(false, total_count))
1261 break;
1263 total_count++;
1266 tagcache_search_finish(tcs);
1268 if (!sort && (sort_inverse || sort_limit))
1270 splashf(HZ*4, ID2P(LANG_SHOWDIR_BUFFER_FULL), total_count);
1271 logf("Too small dir buffer");
1272 return 0;
1275 if (sort_limit)
1276 total_count = MIN(total_count, sort_limit);
1278 if (strip)
1280 dptr = c->dircache;
1281 for (i = 0; i < total_count; i++, dptr++)
1283 int len = strlen(dptr->name);
1285 if (len < strip)
1286 continue;
1288 dptr->name = &dptr->name[strip];
1292 return total_count;
1295 static int load_root(struct tree_context *c)
1297 struct tagentry *dptr = (struct tagentry *)c->dircache;
1298 int i;
1300 tc = c;
1301 c->currtable = root;
1302 if (c->dirlevel == 0)
1303 c->currextra = root_menu;
1305 menu = menus[c->currextra];
1306 if (menu == NULL)
1307 return 0;
1309 for (i = 0; i < menu->itemcount; i++)
1311 dptr->name = menu->items[i]->name;
1312 switch (menu->items[i]->type)
1314 case menu_next:
1315 dptr->newtable = navibrowse;
1316 dptr->extraseek = i;
1317 break;
1319 case menu_load:
1320 dptr->newtable = root;
1321 dptr->extraseek = menu->items[i]->link;
1322 break;
1325 dptr++;
1328 current_offset = 0;
1329 current_entry_count = i;
1331 return i;
1334 int tagtree_load(struct tree_context* c)
1336 int count;
1337 int table = c->currtable;
1339 c->dentry_size = sizeof(struct tagentry);
1340 c->dirsindir = 0;
1342 if (!table)
1344 c->dirfull = false;
1345 table = root;
1346 c->currtable = table;
1347 c->currextra = root_menu;
1350 switch (table)
1352 case root:
1353 count = load_root(c);
1354 break;
1356 case allsubentries:
1357 case navibrowse:
1358 logf("navibrowse...");
1359 cpu_boost(true);
1360 count = retrieve_entries(c, &tcs, 0, true);
1361 cpu_boost(false);
1362 break;
1364 default:
1365 logf("Unsupported table %d\n", table);
1366 return -1;
1369 if (count < 0)
1371 c->dirlevel = 0;
1372 count = load_root(c);
1373 splash(HZ, str(LANG_TAGCACHE_BUSY));
1376 /* The _total_ numer of entries available. */
1377 c->dirlength = c->filesindir = count;
1379 return count;
1382 int tagtree_enter(struct tree_context* c)
1384 int rc = 0;
1385 struct tagentry *dptr;
1386 struct mp3entry *id3;
1387 int newextra;
1388 int seek;
1389 int source;
1391 dptr = tagtree_get_entry(c, c->selected_item);
1393 c->dirfull = false;
1394 seek = dptr->extraseek;
1395 if (seek == -1)
1397 if(c->filesindir<=2)
1398 return 0;
1399 srand(current_tick);
1400 dptr = (tagtree_get_entry(c, 2+(rand() % (c->filesindir-2))));
1401 seek = dptr->extraseek;
1403 newextra = dptr->newtable;
1405 if (c->dirlevel >= MAX_DIR_LEVELS)
1406 return 0;
1408 c->selected_item_history[c->dirlevel]=c->selected_item;
1409 c->table_history[c->dirlevel] = c->currtable;
1410 c->extra_history[c->dirlevel] = c->currextra;
1411 c->pos_history[c->dirlevel] = c->firstpos;
1412 c->dirlevel++;
1414 switch (c->currtable) {
1415 case root:
1416 c->currextra = newextra;
1418 if (newextra == root)
1420 menu = menus[seek];
1421 c->currextra = seek;
1424 else if (newextra == navibrowse)
1426 int i, j;
1428 csi = menu->items[seek]->si;
1429 c->currextra = 0;
1431 strncpy(current_title[c->currextra], dptr->name,
1432 sizeof(current_title[0]) - 1);
1434 /* Read input as necessary. */
1435 for (i = 0; i < csi->tagorder_count; i++)
1437 for (j = 0; j < csi->clause_count[i]; j++)
1439 char* searchstring;
1440 source = csi->clause[i][j]->source;
1442 if (source == source_constant)
1443 continue;
1445 searchstring=csi->clause[i][j]->str;
1446 *searchstring = '\0';
1448 id3 = audio_current_track();
1450 if (source == source_current_path && id3)
1452 char *e;
1453 strncpy(searchstring, id3->path, SEARCHSTR_SIZE);
1454 e = strrchr(searchstring, '/');
1455 if (e)
1456 *e = '\0';
1458 else if (source > source_runtime && id3)
1461 int k = source-source_runtime;
1462 int offset = id3_to_search_mapping[k].id3_offset;
1463 char **src = (char**)((char*)id3 + offset);
1464 if (*src)
1466 strncpy(searchstring, *src, SEARCHSTR_SIZE);
1467 searchstring[SEARCHSTR_SIZE-1] = '\0';
1470 else
1472 rc = kbd_input(searchstring, SEARCHSTR_SIZE);
1473 if (rc == -1 || !searchstring[0])
1475 tagtree_exit(c);
1476 return 0;
1478 if (csi->clause[i][j]->numeric)
1479 csi->clause[i][j]->numeric_data = atoi(searchstring);
1486 c->currtable = newextra;
1488 break;
1490 case navibrowse:
1491 case allsubentries:
1492 if (newextra == playtrack)
1494 if (global_settings.party_mode && audio_status()) {
1495 splash(HZ, ID2P(LANG_PARTY_MODE));
1496 break;
1498 c->dirlevel--;
1499 /* about to create a new current playlist...
1500 allow user to cancel the operation */
1501 if (!warn_on_pl_erase())
1502 break;
1504 if (tagtree_play_folder(c) >= 0)
1505 rc = 2;
1506 break;
1509 c->currtable = newextra;
1510 csi->result_seek[c->currextra] = seek;
1511 if (c->currextra < csi->tagorder_count-1)
1512 c->currextra++;
1513 else
1514 c->dirlevel--;
1516 /* Update the statusbar title */
1517 strncpy(current_title[c->currextra], dptr->name,
1518 sizeof(current_title[0]) - 1);
1519 break;
1521 default:
1522 c->dirlevel--;
1523 break;
1526 c->selected_item=0;
1527 gui_synclist_select_item(&tree_lists, c->selected_item);
1529 return rc;
1532 void tagtree_exit(struct tree_context* c)
1534 c->dirfull = false;
1535 if (c->dirlevel > 0)
1536 c->dirlevel--;
1537 c->selected_item=c->selected_item_history[c->dirlevel];
1538 gui_synclist_select_item(&tree_lists, c->selected_item);
1539 c->currtable = c->table_history[c->dirlevel];
1540 c->currextra = c->extra_history[c->dirlevel];
1541 c->firstpos = c->pos_history[c->dirlevel];
1544 int tagtree_get_filename(struct tree_context* c, char *buf, int buflen)
1546 struct tagentry *entry;
1548 entry = tagtree_get_entry(c, c->selected_item);
1550 if (!tagcache_search(&tcs, tag_filename))
1551 return -1;
1553 if (!tagcache_retrieve(&tcs, entry->extraseek, tcs.type, buf, buflen))
1555 tagcache_search_finish(&tcs);
1556 return -2;
1559 tagcache_search_finish(&tcs);
1561 return 0;
1564 static bool insert_all_playlist(struct tree_context *c, int position, bool queue)
1566 int i;
1567 char buf[MAX_PATH];
1568 int from, to, direction;
1569 int files_left = c->filesindir;
1571 cpu_boost(true);
1572 if (!tagcache_search(&tcs, tag_filename))
1574 splash(HZ, ID2P(LANG_TAGCACHE_BUSY));
1575 cpu_boost(false);
1576 return false;
1579 if (position == PLAYLIST_REPLACE)
1581 if (playlist_remove_all_tracks(NULL) == 0)
1582 position = PLAYLIST_INSERT_LAST;
1583 else
1585 cpu_boost(false);
1586 return false;
1590 if (position == PLAYLIST_INSERT_FIRST)
1592 from = c->filesindir - 1;
1593 to = -1;
1594 direction = -1;
1596 else
1598 from = 0;
1599 to = c->filesindir;
1600 direction = 1;
1603 for (i = from; i != to; i += direction)
1605 /* Count back to zero */
1606 if (!show_search_progress(false, files_left--))
1607 break;
1609 if (!tagcache_retrieve(&tcs, tagtree_get_entry(c, i)->extraseek,
1610 tcs.type, buf, sizeof buf))
1612 continue;
1615 if (playlist_insert_track(NULL, buf, position, queue, false) < 0)
1617 logf("playlist_insert_track failed");
1618 break;
1620 yield();
1622 playlist_sync(NULL);
1623 tagcache_search_finish(&tcs);
1624 cpu_boost(false);
1626 return true;
1629 bool tagtree_insert_selection_playlist(int position, bool queue)
1631 struct tagentry *dptr;
1632 char buf[MAX_PATH];
1633 int dirlevel = tc->dirlevel;
1635 /* We need to set the table to allsubentries. */
1636 show_search_progress(true, 0);
1638 dptr = tagtree_get_entry(tc, tc->selected_item);
1640 /* Insert a single track? */
1641 if (dptr->newtable == playtrack)
1643 if (tagtree_get_filename(tc, buf, sizeof buf) < 0)
1645 logf("tagtree_get_filename failed");
1646 return false;
1648 playlist_insert_track(NULL, buf, position, queue, true);
1650 return true;
1653 if (dptr->newtable == navibrowse)
1655 tagtree_enter(tc);
1656 tagtree_load(tc);
1657 dptr = tagtree_get_entry(tc, tc->selected_item);
1659 else if (dptr->newtable != allsubentries)
1661 logf("unsupported table: %d", dptr->newtable);
1662 return false;
1665 /* Now the current table should be allsubentries. */
1666 if (dptr->newtable != playtrack)
1668 tagtree_enter(tc);
1669 tagtree_load(tc);
1670 dptr = tagtree_get_entry(tc, tc->selected_item);
1672 /* And now the newtable should be playtrack. */
1673 if (dptr->newtable != playtrack)
1675 logf("newtable: %d !!", dptr->newtable);
1676 tc->dirlevel = dirlevel;
1677 return false;
1681 if (tc->filesindir <= 0)
1682 splash(HZ, ID2P(LANG_END_PLAYLIST));
1683 else
1685 logf("insert_all_playlist");
1686 if (!insert_all_playlist(tc, position, queue))
1687 splash(HZ*2, ID2P(LANG_FAILED));
1690 /* Finally return the dirlevel to its original value. */
1691 while (tc->dirlevel > dirlevel)
1692 tagtree_exit(tc);
1693 tagtree_load(tc);
1695 return true;
1698 static int tagtree_play_folder(struct tree_context* c)
1700 if (playlist_create(NULL, NULL) < 0)
1702 logf("Failed creating playlist\n");
1703 return -1;
1706 if (!insert_all_playlist(c, PLAYLIST_INSERT_LAST, false))
1707 return -2;
1709 if (global_settings.playlist_shuffle)
1710 c->selected_item = playlist_shuffle(current_tick, c->selected_item);
1711 if (!global_settings.play_selected)
1712 c->selected_item = 0;
1713 gui_synclist_select_item(&tree_lists, c->selected_item);
1715 playlist_start(c->selected_item,0);
1717 return 0;
1720 struct tagentry* tagtree_get_entry(struct tree_context *c, int id)
1722 struct tagentry *entry = (struct tagentry *)c->dircache;
1723 int realid = id - current_offset;
1725 /* Load the next chunk if necessary. */
1726 if (realid >= current_entry_count || realid < 0)
1728 cpu_boost(true);
1729 if (retrieve_entries(c, &tcs2, MAX(0, id - (current_entry_count / 2)),
1730 false) < 0)
1732 logf("retrieve failed");
1733 cpu_boost(false);
1734 return NULL;
1736 realid = id - current_offset;
1737 cpu_boost(false);
1740 return &entry[realid];
1743 char *tagtree_get_title(struct tree_context* c)
1745 switch (c->currtable)
1747 case root:
1748 return menu->title;
1750 case navibrowse:
1751 case allsubentries:
1752 return current_title[c->currextra];
1755 return "?";
1758 int tagtree_get_attr(struct tree_context* c)
1760 int attr = -1;
1761 switch (c->currtable)
1763 case navibrowse:
1764 if (csi->tagorder[c->currextra] == tag_title)
1765 attr = FILE_ATTR_AUDIO;
1766 else
1767 attr = ATTR_DIRECTORY;
1768 break;
1770 case allsubentries:
1771 attr = FILE_ATTR_AUDIO;
1772 break;
1774 default:
1775 attr = ATTR_DIRECTORY;
1776 break;
1779 return attr;
1782 int tagtree_get_icon(struct tree_context* c)
1784 int icon = Icon_Folder;
1786 if (tagtree_get_attr(c) == FILE_ATTR_AUDIO)
1787 icon = Icon_Audio;
1789 return icon;