fix a warning.
[Rockbox.git] / apps / tagtree.c
blob03673fc6539ed5ce06f9df2608feb7b3b9182228
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, "year", tag_year);
213 MATCH(tag, buf, "playcount", tag_playcount);
214 MATCH(tag, buf, "rating", tag_rating);
215 MATCH(tag, buf, "lastplayed", tag_lastplayed);
216 MATCH(tag, buf, "commitid", tag_commitid);
217 MATCH(tag, buf, "entryage", tag_virt_entryage);
218 MATCH(tag, buf, "autoscore", tag_virt_autoscore);
219 MATCH(tag, buf, "%sort", var_sorttype);
220 MATCH(tag, buf, "%limit", var_limit);
221 MATCH(tag, buf, "%strip", var_strip);
222 MATCH(tag, buf, "%menu_start", var_menu_start);
223 MATCH(tag, buf, "%include", var_include);
224 MATCH(tag, buf, "%root_menu", var_rootmenu);
225 MATCH(tag, buf, "%format", var_format);
226 MATCH(tag, buf, "->", menu_next);
227 MATCH(tag, buf, "==>", menu_load);
229 logf("NO MATCH: %s\n", buf);
230 if (buf[0] == '?')
231 return 0;
233 return -1;
236 static int get_clause(int *condition)
238 char buf[4];
239 int i;
241 /* Find the start. */
242 while (*strp == ' ' && *strp != '\0')
243 strp++;
245 if (*strp == '\0')
246 return 0;
248 for (i = 0; i < (int)sizeof(buf)-1; i++)
250 if (*strp == '\0' || *strp == ' ')
251 break ;
252 buf[i] = *strp;
253 strp++;
255 buf[i] = '\0';
257 MATCH(condition, buf, "=", clause_is);
258 MATCH(condition, buf, "==", clause_is);
259 MATCH(condition, buf, "!=", clause_is_not);
260 MATCH(condition, buf, ">", clause_gt);
261 MATCH(condition, buf, ">=", clause_gteq);
262 MATCH(condition, buf, "<", clause_lt);
263 MATCH(condition, buf, "<=", clause_lteq);
264 MATCH(condition, buf, "~", clause_contains);
265 MATCH(condition, buf, "!~", clause_not_contains);
266 MATCH(condition, buf, "^", clause_begins_with);
267 MATCH(condition, buf, "!^", clause_not_begins_with);
268 MATCH(condition, buf, "$", clause_ends_with);
269 MATCH(condition, buf, "!$", clause_not_ends_with);
270 MATCH(condition, buf, "@", clause_oneof);
272 return 0;
275 static bool read_clause(struct tagcache_search_clause *clause)
277 char buf[256];
279 if (get_tag(&clause->tag) <= 0)
280 return false;
282 if (get_clause(&clause->type) <= 0)
283 return false;
285 if (get_token_str(buf, sizeof buf) < 0)
286 return false;
288 clause->str = buffer_alloc(strlen(buf)+1);
289 strcpy(clause->str, buf);
291 logf("got clause: %d/%d [%s]", clause->tag, clause->type, clause->str);
293 if (*(clause->str) == '\0')
294 clause->input = true;
295 else
296 clause->input = false;
298 if (tagcache_is_numeric_tag(clause->tag))
300 clause->numeric = true;
301 clause->numeric_data = atoi(clause->str);
303 else
304 clause->numeric = false;
306 return true;
309 static bool read_variable(char *buf, int size)
311 int condition;
313 if (!get_clause(&condition))
314 return false;
316 if (condition != clause_is)
317 return false;
319 if (get_token_str(buf, size) < 0)
320 return false;
322 return true;
325 /* "%3d. %s" autoscore title %sort = "inverse" %limit = "100" */
326 static int get_format_str(struct display_format *fmt)
328 int ret;
329 char buf[128];
330 int i;
332 memset(fmt, 0, sizeof(struct display_format));
334 if (get_token_str(fmt->name, sizeof fmt->name) < 0)
335 return -10;
337 /* Determine the group id */
338 fmt->group_id = 0;
339 for (i = 0; i < format_count; i++)
341 if (!strcasecmp(formats[i]->name, fmt->name))
343 fmt->group_id = formats[i]->group_id;
344 break;
347 if (formats[i]->group_id > fmt->group_id)
348 fmt->group_id = formats[i]->group_id;
351 if (i == format_count)
352 fmt->group_id++;
354 logf("format: (%d) %s", fmt->group_id, fmt->name);
356 if (get_token_str(buf, sizeof buf) < 0)
357 return -10;
359 fmt->formatstr = buffer_alloc(strlen(buf) + 1);
360 strcpy(fmt->formatstr, buf);
362 while (fmt->tag_count < MAX_TAGS)
364 ret = get_tag(&fmt->tags[fmt->tag_count]);
365 if (ret < 0)
366 return -11;
368 if (ret == 0)
369 break;
371 switch (fmt->tags[fmt->tag_count]) {
372 case var_sorttype:
373 if (!read_variable(buf, sizeof buf))
374 return -12;
375 if (!strcasecmp("inverse", buf))
376 fmt->sort_inverse = true;
378 fmt->sort = true;
379 break;
381 case var_limit:
382 if (!read_variable(buf, sizeof buf))
383 return -13;
384 fmt->limit = atoi(buf);
385 break;
387 case var_strip:
388 if (!read_variable(buf, sizeof buf))
389 return -14;
390 fmt->strip = atoi(buf);
391 break;
393 default:
394 fmt->tag_count++;
398 return 1;
401 static int add_format(const char *buf)
403 strp = buf;
405 if (formats[format_count] == NULL)
406 formats[format_count] = buffer_alloc(sizeof(struct display_format));
408 memset(formats[format_count], 0, sizeof(struct display_format));
409 if (get_format_str(formats[format_count]) < 0)
411 logf("get_format_str() parser failed!");
412 return -4;
415 while (*strp != '\0' && *strp != '?')
416 strp++;
418 if (*strp == '?')
420 int clause_count = 0;
421 strp++;
423 while (1)
425 if (clause_count >= TAGCACHE_MAX_CLAUSES)
427 logf("too many clauses");
428 break;
431 formats[format_count]->clause[clause_count] =
432 buffer_alloc(sizeof(struct tagcache_search_clause));
434 if (!read_clause(formats[format_count]->clause[clause_count]))
435 break;
437 clause_count++;
440 formats[format_count]->clause_count = clause_count;
443 format_count++;
445 return 1;
448 static int get_condition(struct search_instruction *inst)
450 int clause_count;
451 char buf[128];
453 switch (*strp)
455 case '=':
457 int i;
459 if (get_token_str(buf, sizeof buf) < 0)
460 return -1;
462 for (i = 0; i < format_count; i++)
464 if (!strcasecmp(formats[i]->name, buf))
465 break;
468 if (i == format_count)
470 logf("format not found: %s", buf);
471 return -2;
474 inst->format_id[inst->tagorder_count] = formats[i]->group_id;
475 return 1;
477 case '?':
478 case ' ':
479 case '&':
480 strp++;
481 return 1;
482 case '-':
483 case '\0':
484 return 0;
487 clause_count = inst->clause_count[inst->tagorder_count];
488 if (clause_count >= TAGCACHE_MAX_CLAUSES)
490 logf("Too many clauses");
491 return false;
494 inst->clause[inst->tagorder_count][clause_count] =
495 buffer_alloc(sizeof(struct tagcache_search_clause));
497 if (!read_clause(inst->clause[inst->tagorder_count][clause_count]))
498 return -1;
500 inst->clause_count[inst->tagorder_count]++;
502 return 1;
505 /* example search:
506 * "Best" artist ? year >= "2000" & title !^ "crap" & genre = "good genre" \
507 * : album ? year >= "2000" : songs
508 * ^ begins with
509 * * contains
510 * $ ends with
513 static bool parse_search(struct menu_entry *entry, const char *str)
515 int ret;
516 int type;
517 struct search_instruction *inst = entry->si;
518 char buf[MAX_PATH];
519 int i;
520 struct root_menu *new_menu;
522 strp = str;
524 /* Parse entry name */
525 if (get_token_str(entry->name, sizeof entry->name) < 0)
527 logf("No name found.");
528 return false;
531 /* Parse entry type */
532 if (get_tag(&entry->type) <= 0)
533 return false;
535 if (entry->type == menu_load)
537 if (get_token_str(buf, sizeof buf) < 0)
538 return false;
540 /* Find the matching root menu or "create" it */
541 for (i = 0; i < menu_count; i++)
543 if (!strcasecmp(menus[i]->id, buf))
545 entry->link = i;
546 return true;
550 if (menu_count >= TAGMENU_MAX_MENUS)
552 logf("max menucount reached");
553 return false;
556 /* Allocate a new menu unless link is found. */
557 menus[menu_count] = buffer_alloc(sizeof(struct root_menu));
558 new_menu = menus[menu_count];
559 memset(new_menu, 0, sizeof(struct root_menu));
560 strncpy(new_menu->id, buf, MAX_MENU_ID_SIZE-1);
561 entry->link = menu_count;
562 ++menu_count;
564 return true;
567 if (entry->type != menu_next)
568 return false;
570 while (inst->tagorder_count < MAX_TAGS)
572 ret = get_tag(&inst->tagorder[inst->tagorder_count]);
573 if (ret < 0)
575 logf("Parse error #1");
576 logf("%s", strp);
577 return false;
580 if (ret == 0)
581 break ;
583 logf("tag: %d", inst->tagorder[inst->tagorder_count]);
585 while ( (ret = get_condition(inst)) > 0 ) ;
586 if (ret < 0)
587 return false;
589 inst->tagorder_count++;
591 if (get_tag(&type) <= 0 || type != menu_next)
592 break;
595 return true;
598 static int compare(const void *p1, const void *p2)
600 struct tagentry *e1 = (struct tagentry *)p1;
601 struct tagentry *e2 = (struct tagentry *)p2;
603 if (sort_inverse)
604 return strncasecmp(e2->name, e1->name, MAX_PATH);
606 return strncasecmp(e1->name, e2->name, MAX_PATH);
609 static void tagtree_buffer_event(struct mp3entry *id3, bool last_track)
611 (void)id3;
612 (void)last_track;
614 /* Do not gather data unless proper setting has been enabled. */
615 if (!global_settings.runtimedb)
616 return;
618 logf("be:%d%s", last_track, id3->path);
620 if (!tagcache_find_index(&tcs, id3->path))
622 logf("tc stat: not found: %s", id3->path);
623 return;
626 id3->playcount = tagcache_get_numeric(&tcs, tag_playcount);
627 if(!id3->rating) id3->rating = tagcache_get_numeric(&tcs, tag_rating);
628 id3->lastplayed = tagcache_get_numeric(&tcs, tag_lastplayed);
629 id3->score = tagcache_get_numeric(&tcs, tag_virt_autoscore) / 10;
631 tagcache_search_finish(&tcs);
634 static void tagtree_unbuffer_event(struct mp3entry *id3, bool last_track)
636 (void)last_track;
637 long playcount;
638 long rating;
639 long playtime;
640 long lastplayed;
642 /* Do not gather data unless proper setting has been enabled. */
643 if (!global_settings.runtimedb)
645 logf("runtimedb gathering not enabled");
646 return;
649 /* Don't process unplayed tracks. */
650 if (id3->elapsed == 0)
652 logf("not logging unplayed track");
653 return;
656 if (!tagcache_find_index(&tcs, id3->path))
658 logf("tc stat: not found: %s", id3->path);
659 return;
662 playcount = tagcache_get_numeric(&tcs, tag_playcount);
663 playtime = tagcache_get_numeric(&tcs, tag_playtime);
664 lastplayed = tagcache_get_numeric(&tcs, tag_lastplayed);
666 playcount++;
668 rating = (long) id3->rating;
670 lastplayed = tagcache_increase_serial();
671 if (lastplayed < 0)
673 logf("incorrect tc serial:%ld", lastplayed);
674 tagcache_search_finish(&tcs);
675 return;
678 /* Ignore the last 15s (crossfade etc.) */
679 playtime += MIN(id3->length, id3->elapsed + 15 * 1000);
681 logf("ube:%s", id3->path);
682 logf("-> %d/%ld/%ld/%ld", last_track, playcount, rating, playtime);
683 logf("-> %ld/%ld/%ld", id3->elapsed, id3->length, MIN(id3->length, id3->elapsed + 15 * 1000));
685 /* lastplayed not yet supported. */
687 if (!tagcache_modify_numeric_entry(&tcs, tag_playcount, playcount)
688 || !tagcache_modify_numeric_entry(&tcs, tag_rating, rating)
689 || !tagcache_modify_numeric_entry(&tcs, tag_playtime, playtime)
690 || !tagcache_modify_numeric_entry(&tcs, tag_lastplayed, lastplayed))
692 logf("tc stat: modify failed!");
693 tagcache_search_finish(&tcs);
694 return;
697 tagcache_search_finish(&tcs);
700 bool tagtree_export(void)
702 gui_syncsplash(0, str(LANG_CREATING));
703 if (!tagcache_create_changelog(&tcs))
705 gui_syncsplash(HZ*2, str(LANG_FAILED));
708 return false;
711 bool tagtree_import(void)
713 gui_syncsplash(0, str(LANG_WAIT));
714 if (!tagcache_import_changelog())
716 gui_syncsplash(HZ*2, str(LANG_FAILED));
719 return false;
722 static bool parse_menu(const char *filename);
724 static int parse_line(int n, const char *buf, void *parameters)
726 char data[256];
727 int variable;
728 static bool read_menu;
729 int i;
731 (void)parameters;
733 logf("parse:%d/%s", n, buf);
735 /* First line, do initialisation. */
736 if (n == 0)
738 if (strcasecmp(TAGNAVI_VERSION, buf))
740 logf("Version mismatch");
741 return -1;
744 read_menu = false;
747 if (buf[0] == '#')
748 return 0;
750 if (buf[0] == '\0')
752 if (read_menu)
754 /* End the menu */
755 read_menu = false;
757 return 0;
760 if (!read_menu)
762 strp = buf;
763 if (get_tag(&variable) <= 0)
764 return 0;
766 switch (variable)
768 case var_format:
769 if (add_format(strp) < 0)
771 logf("Format add fail: %s", data);
773 break;
775 case var_include:
776 if (get_token_str(data, sizeof(data)) < 0)
778 logf("%%include empty");
779 return 0;
782 if (!parse_menu(data))
784 logf("Load menu fail: %s", data);
786 break;
788 case var_menu_start:
789 if (menu_count >= TAGMENU_MAX_MENUS)
791 logf("max menucount reached");
792 return 0;
795 if (get_token_str(data, sizeof data) < 0)
797 logf("%%menu_start id empty");
798 return 0;
801 menu = NULL;
802 for (i = 0; i < menu_count; i++)
804 if (!strcasecmp(menus[i]->id, data))
806 menu = menus[i];
810 if (menu == NULL)
812 menus[menu_count] = buffer_alloc(sizeof(struct root_menu));
813 menu = menus[menu_count];
814 ++menu_count;
815 memset(menu, 0, sizeof(struct root_menu));
816 strncpy(menu->id, data, MAX_MENU_ID_SIZE-1);
819 if (get_token_str(menu->title, sizeof(menu->title)) < 0)
821 logf("%%menu_start title empty");
822 return 0;
824 logf("menu: %s", menu->title);
825 read_menu = true;
826 break;
828 case var_rootmenu:
829 /* Only set root menu once. */
830 if (root_menu >= 0)
831 break;
833 if (get_token_str(data, sizeof(data)) < 0)
835 logf("%%root_menu empty");
836 return 0;
839 for (i = 0; i < menu_count; i++)
841 if (!strcasecmp(menus[i]->id, data))
843 root_menu = i;
846 break;
849 return 0;
852 if (menu->itemcount >= TAGMENU_MAX_ITEMS)
854 logf("max itemcount reached");
855 return 0;
858 /* Allocate */
859 if (menu->items[menu->itemcount] == NULL)
861 menu->items[menu->itemcount] = buffer_alloc(sizeof(struct menu_entry));
862 memset(menu->items[menu->itemcount], 0, sizeof(struct menu_entry));
863 menu->items[menu->itemcount]->si = buffer_alloc(sizeof(struct search_instruction));
866 memset(menu->items[menu->itemcount]->si, 0, sizeof(struct search_instruction));
867 if (!parse_search(menu->items[menu->itemcount], buf))
868 return 0;
870 menu->itemcount++;
872 return 0;
875 static bool parse_menu(const char *filename)
877 int fd;
878 char buf[1024];
880 if (menu_count >= TAGMENU_MAX_MENUS)
882 logf("max menucount reached");
883 return false;
886 fd = open(filename, O_RDONLY);
887 if (fd < 0)
889 logf("Search instruction file not found.");
890 return false;
893 /* Now read file for real, parsing into si */
894 fast_readline(fd, buf, sizeof buf, NULL, parse_line);
895 close(fd);
897 return true;
900 void tagtree_init(void)
902 format_count = 0;
903 menu_count = 0;
904 menu = NULL;
905 root_menu = -1;
906 parse_menu(FILE_SEARCH_INSTRUCTIONS);
908 /* If no root menu is set, assume it's the first single menu
909 * we have. That shouldn't normally happen. */
910 if (root_menu < 0)
911 root_menu = 0;
913 uniqbuf = buffer_alloc(UNIQBUF_SIZE);
914 audio_set_track_buffer_event(tagtree_buffer_event);
915 audio_set_track_unbuffer_event(tagtree_unbuffer_event);
918 static bool show_search_progress(bool init, int count)
920 static int last_tick = 0;
922 if (init)
924 last_tick = current_tick;
925 return true;
928 if (current_tick - last_tick > HZ/4)
930 gui_syncsplash(0, str(LANG_PLAYLIST_SEARCH_MSG), count,
931 #if CONFIG_KEYPAD == PLAYER_PAD
932 str(LANG_STOP_ABORT)
933 #else
934 str(LANG_OFF_ABORT)
935 #endif
937 if (action_userabort(TIMEOUT_NOBLOCK))
938 return false;
939 last_tick = current_tick;
940 yield();
943 return true;
946 static int format_str(struct tagcache_search *tcs, struct display_format *fmt,
947 char *buf, int buf_size)
949 char fmtbuf[8];
950 bool read_format = false;
951 int fmtbuf_pos = 0;
952 int parpos = 0;
953 int buf_pos = 0;
954 int i;
956 memset(buf, 0, buf_size);
957 for (i = 0; fmt->formatstr[i] != '\0'; i++)
959 if (fmt->formatstr[i] == '%')
961 read_format = true;
962 fmtbuf_pos = 0;
963 if (parpos >= fmt->tag_count)
965 logf("too many format tags");
966 return -1;
970 if (read_format)
972 fmtbuf[fmtbuf_pos++] = fmt->formatstr[i];
973 if (fmtbuf_pos >= buf_size)
975 logf("format parse error");
976 return -2;
979 if (fmt->formatstr[i] == 's')
981 fmtbuf[fmtbuf_pos] = '\0';
982 read_format = false;
983 if (fmt->tags[parpos] == tcs->type)
985 snprintf(&buf[buf_pos], buf_size - buf_pos, fmtbuf, tcs->result);
987 else
989 /* Need to fetch the tag data. */
990 if (!tagcache_retrieve(tcs, tcs->idx_id, fmt->tags[parpos],
991 &buf[buf_pos], buf_size - buf_pos))
993 logf("retrieve failed");
994 return -3;
997 buf_pos += strlen(&buf[buf_pos]);
998 parpos++;
1000 else if (fmt->formatstr[i] == 'd')
1002 fmtbuf[fmtbuf_pos] = '\0';
1003 read_format = false;
1004 snprintf(&buf[buf_pos], buf_size - buf_pos, fmtbuf,
1005 tagcache_get_numeric(tcs, fmt->tags[parpos]));
1006 buf_pos += strlen(&buf[buf_pos]);
1007 parpos++;
1009 continue;
1012 buf[buf_pos++] = fmt->formatstr[i];
1014 if (buf_pos - 1 >= buf_size)
1016 logf("buffer overflow");
1017 return -4;
1021 buf[buf_pos++] = '\0';
1023 return 0;
1026 static int retrieve_entries(struct tree_context *c, struct tagcache_search *tcs,
1027 int offset, bool init)
1029 struct tagentry *dptr = (struct tagentry *)c->dircache;
1030 struct display_format *fmt;
1031 int i;
1032 int namebufused = 0;
1033 int total_count = 0;
1034 int special_entry_count = 0;
1035 int level = c->currextra;
1036 int tag;
1037 bool sort = false;
1038 int sort_limit;
1039 int strip;
1041 if (init
1042 #ifdef HAVE_TC_RAMCACHE
1043 && !tagcache_is_ramcache()
1044 #endif
1047 show_search_progress(true, 0);
1048 gui_syncsplash(0, str(LANG_PLAYLIST_SEARCH_MSG),
1049 0, csi->name);
1052 if (c->currtable == allsubentries)
1054 tag = tag_title;
1055 level--;
1057 else
1058 tag = csi->tagorder[level];
1060 if (!tagcache_search(tcs, tag))
1061 return -1;
1063 /* Prevent duplicate entries in the search list. */
1064 tagcache_search_set_uniqbuf(tcs, uniqbuf, UNIQBUF_SIZE);
1066 if (level || csi->clause_count[0] || tagcache_is_numeric_tag(tag))
1067 sort = true;
1069 for (i = 0; i < level; i++)
1071 if (tagcache_is_numeric_tag(csi->tagorder[i]))
1073 static struct tagcache_search_clause cc;
1075 memset(&cc, 0, sizeof(struct tagcache_search_clause));
1076 cc.tag = csi->tagorder[i];
1077 cc.type = clause_is;
1078 cc.numeric = true;
1079 cc.numeric_data = csi->result_seek[i];
1080 tagcache_search_add_clause(tcs, &cc);
1082 else
1084 tagcache_search_add_filter(tcs, csi->tagorder[i],
1085 csi->result_seek[i]);
1089 for (i = 0; i <= level; i++)
1091 int j;
1093 for (j = 0; j < csi->clause_count[i]; j++)
1094 tagcache_search_add_clause(tcs, csi->clause[i][j]);
1097 current_offset = offset;
1098 current_entry_count = 0;
1099 c->dirfull = false;
1101 fmt = NULL;
1102 for (i = 0; i < format_count; i++)
1104 if (formats[i]->group_id == csi->format_id[level])
1105 fmt = formats[i];
1108 if (fmt)
1110 sort_inverse = fmt->sort_inverse;
1111 sort_limit = fmt->limit;
1112 strip = fmt->strip;
1114 /* Check if sorting is forced. */
1115 if (fmt->sort)
1116 sort = true;
1118 else
1120 sort_inverse = false;
1121 sort_limit = 0;
1122 strip = 0;
1125 if (tag != tag_title && tag != tag_filename)
1127 if (offset == 0)
1129 dptr->newtable = allsubentries;
1130 dptr->name = str(LANG_TAGNAVI_ALL_TRACKS);
1131 dptr++;
1132 current_entry_count++;
1134 special_entry_count++;
1137 total_count += special_entry_count;
1139 while (tagcache_get_next(tcs))
1141 if (total_count++ < offset)
1142 continue;
1144 dptr->newtable = navibrowse;
1145 if (tag == tag_title || tag == tag_filename)
1147 dptr->newtable = playtrack;
1148 dptr->extraseek = tcs->idx_id;
1150 else
1151 dptr->extraseek = tcs->result_seek;
1153 fmt = NULL;
1154 /* Check the format */
1155 for (i = 0; i < format_count; i++)
1157 if (formats[i]->group_id != csi->format_id[level])
1158 continue;
1160 if (tagcache_check_clauses(tcs, formats[i]->clause,
1161 formats[i]->clause_count))
1163 fmt = formats[i];
1164 break;
1168 if (!tcs->ramresult || fmt)
1170 char buf[MAX_PATH];
1172 if (fmt)
1174 if (format_str(tcs, fmt, buf, sizeof buf) < 0)
1176 logf("format_str() failed");
1177 return 0;
1181 dptr->name = &c->name_buffer[namebufused];
1182 if (fmt)
1183 namebufused += strlen(buf)+1;
1184 else
1185 namebufused += tcs->result_len;
1187 if (namebufused >= c->name_buffer_size)
1189 logf("chunk mode #2: %d", current_entry_count);
1190 c->dirfull = true;
1191 sort = false;
1192 break ;
1194 if (fmt)
1195 strcpy(dptr->name, buf);
1196 else
1197 strcpy(dptr->name, tcs->result);
1199 else
1200 dptr->name = tcs->result;
1202 dptr++;
1203 current_entry_count++;
1205 if (current_entry_count >= global_settings.max_files_in_dir)
1207 logf("chunk mode #3: %d", current_entry_count);
1208 c->dirfull = true;
1209 sort = false;
1210 break ;
1213 if (init && !tcs->ramsearch)
1215 if (!show_search_progress(false, i))
1217 tagcache_search_finish(tcs);
1218 return current_entry_count;
1223 if (sort)
1224 qsort(c->dircache + special_entry_count * c->dentry_size,
1225 current_entry_count - special_entry_count,
1226 c->dentry_size, compare);
1228 if (!init)
1230 tagcache_search_finish(tcs);
1231 return current_entry_count;
1234 while (tagcache_get_next(tcs))
1236 if (!tcs->ramsearch)
1238 if (!show_search_progress(false, total_count))
1239 break;
1241 total_count++;
1244 tagcache_search_finish(tcs);
1246 if (!sort && (sort_inverse || sort_limit))
1248 gui_syncsplash(HZ*4, str(LANG_SHOWDIR_BUFFER_FULL), total_count);
1249 logf("Too small dir buffer");
1250 return 0;
1253 if (sort_limit)
1254 total_count = MIN(total_count, sort_limit);
1256 if (strip)
1258 dptr = c->dircache;
1259 for (i = 0; i < total_count; i++, dptr++)
1261 int len = strlen(dptr->name);
1263 if (len < strip)
1264 continue;
1266 dptr->name = &dptr->name[strip];
1270 return total_count;
1273 static int load_root(struct tree_context *c)
1275 struct tagentry *dptr = (struct tagentry *)c->dircache;
1276 int i;
1278 tc = c;
1279 c->currtable = root;
1280 if (c->dirlevel == 0)
1281 c->currextra = root_menu;
1283 menu = menus[c->currextra];
1284 if (menu == NULL)
1285 return 0;
1287 for (i = 0; i < menu->itemcount; i++)
1289 dptr->name = menu->items[i]->name;
1290 switch (menu->items[i]->type)
1292 case menu_next:
1293 dptr->newtable = navibrowse;
1294 dptr->extraseek = i;
1295 break;
1297 case menu_load:
1298 dptr->newtable = root;
1299 dptr->extraseek = menu->items[i]->link;
1300 break;
1303 dptr++;
1306 current_offset = 0;
1307 current_entry_count = i;
1309 return i;
1312 int tagtree_load(struct tree_context* c)
1314 int count;
1315 int table = c->currtable;
1317 c->dentry_size = sizeof(struct tagentry);
1318 c->dirsindir = 0;
1320 if (!table)
1322 c->dirfull = false;
1323 table = root;
1324 c->currtable = table;
1325 c->currextra = root_menu;
1328 switch (table)
1330 case root:
1331 count = load_root(c);
1332 break;
1334 case allsubentries:
1335 case navibrowse:
1336 logf("navibrowse...");
1337 cpu_boost(true);
1338 count = retrieve_entries(c, &tcs, 0, true);
1339 cpu_boost(false);
1340 break;
1342 default:
1343 logf("Unsupported table %d\n", table);
1344 return -1;
1347 if (count < 0)
1349 c->dirlevel = 0;
1350 count = load_root(c);
1351 gui_syncsplash(HZ, str(LANG_TAGCACHE_BUSY));
1354 /* The _total_ numer of entries available. */
1355 c->dirlength = c->filesindir = count;
1357 return count;
1360 int tagtree_enter(struct tree_context* c)
1362 int rc = 0;
1363 struct tagentry *dptr;
1364 int newextra;
1365 int seek;
1367 dptr = tagtree_get_entry(c, c->selected_item);
1369 c->dirfull = false;
1370 newextra = dptr->newtable;
1371 seek = dptr->extraseek;
1373 if (c->dirlevel >= MAX_DIR_LEVELS)
1374 return 0;
1376 c->selected_item_history[c->dirlevel]=c->selected_item;
1377 c->table_history[c->dirlevel] = c->currtable;
1378 c->extra_history[c->dirlevel] = c->currextra;
1379 c->pos_history[c->dirlevel] = c->firstpos;
1380 c->dirlevel++;
1382 switch (c->currtable) {
1383 case root:
1384 c->currextra = newextra;
1386 if (newextra == root)
1388 menu = menus[seek];
1389 c->currextra = seek;
1392 else if (newextra == navibrowse)
1394 int i, j;
1396 csi = menu->items[seek]->si;
1397 c->currextra = 0;
1399 strncpy(current_title[c->currextra], dptr->name,
1400 sizeof(current_title[0]) - 1);
1402 /* Read input as necessary. */
1403 for (i = 0; i < csi->tagorder_count; i++)
1405 for (j = 0; j < csi->clause_count[i]; j++)
1407 if (!csi->clause[i][j]->input)
1408 continue;
1410 rc = kbd_input(searchstring, sizeof(searchstring));
1411 if (rc == -1 || !searchstring[0])
1413 tagtree_exit(c);
1414 return 0;
1417 if (csi->clause[i][j]->numeric)
1418 csi->clause[i][j]->numeric_data = atoi(searchstring);
1419 else
1420 csi->clause[i][j]->str = searchstring;
1424 c->currtable = newextra;
1426 break;
1428 case navibrowse:
1429 case allsubentries:
1430 if (newextra == playtrack)
1432 c->dirlevel--;
1433 /* about to create a new current playlist...
1434 allow user to cancel the operation */
1435 if (global_settings.warnon_erase_dynplaylist &&
1436 !global_settings.party_mode &&
1437 playlist_modified(NULL))
1439 char *lines[]={str(LANG_WARN_ERASEDYNPLAYLIST_PROMPT)};
1440 struct text_message message={lines, 1};
1442 if (gui_syncyesno_run(&message, NULL, NULL) != YESNO_YES)
1443 break;
1446 if (tagtree_play_folder(c) >= 0)
1447 rc = 2;
1448 break;
1451 c->currtable = newextra;
1452 csi->result_seek[c->currextra] = seek;
1453 if (c->currextra < csi->tagorder_count-1)
1454 c->currextra++;
1455 else
1456 c->dirlevel--;
1458 /* Update the statusbar title */
1459 strncpy(current_title[c->currextra], dptr->name,
1460 sizeof(current_title[0]) - 1);
1461 break;
1463 default:
1464 c->dirlevel--;
1465 break;
1468 c->selected_item=0;
1469 gui_synclist_select_item(&tree_lists, c->selected_item);
1471 return rc;
1474 void tagtree_exit(struct tree_context* c)
1476 c->dirfull = false;
1477 if (c->dirlevel > 0)
1478 c->dirlevel--;
1479 c->selected_item=c->selected_item_history[c->dirlevel];
1480 gui_synclist_select_item(&tree_lists, c->selected_item);
1481 c->currtable = c->table_history[c->dirlevel];
1482 c->currextra = c->extra_history[c->dirlevel];
1483 c->firstpos = c->pos_history[c->dirlevel];
1486 int tagtree_get_filename(struct tree_context* c, char *buf, int buflen)
1488 struct tagentry *entry;
1490 entry = tagtree_get_entry(c, c->selected_item);
1492 if (!tagcache_search(&tcs, tag_filename))
1493 return -1;
1495 if (!tagcache_retrieve(&tcs, entry->extraseek, tcs.type, buf, buflen))
1497 tagcache_search_finish(&tcs);
1498 return -2;
1501 tagcache_search_finish(&tcs);
1503 return 0;
1506 static bool insert_all_playlist(struct tree_context *c, int position, bool queue)
1508 int i;
1509 char buf[MAX_PATH];
1510 int from, to, direction;
1511 int files_left = c->filesindir;
1513 cpu_boost(true);
1514 if (!tagcache_search(&tcs, tag_filename))
1516 gui_syncsplash(HZ, str(LANG_TAGCACHE_BUSY));
1517 cpu_boost(false);
1518 return false;
1521 if (position == PLAYLIST_REPLACE)
1523 if (remove_all_tracks(NULL) == 0)
1524 position = PLAYLIST_INSERT_LAST;
1525 else return -1; }
1527 if (position == PLAYLIST_INSERT_FIRST)
1529 from = c->filesindir - 1;
1530 to = -1;
1531 direction = -1;
1533 else
1535 from = 0;
1536 to = c->filesindir;
1537 direction = 1;
1540 for (i = from; i != to; i += direction)
1542 /* Count back to zero */
1543 if (!show_search_progress(false, files_left--))
1544 break;
1546 if (!tagcache_retrieve(&tcs, tagtree_get_entry(c, i)->extraseek,
1547 tcs.type, buf, sizeof buf))
1549 continue;
1552 if (playlist_insert_track(NULL, buf, position, queue, false) < 0)
1554 logf("playlist_insert_track failed");
1555 break;
1557 yield();
1559 playlist_sync(NULL);
1560 tagcache_search_finish(&tcs);
1561 cpu_boost(false);
1563 return true;
1566 bool tagtree_insert_selection_playlist(int position, bool queue)
1568 struct tagentry *dptr;
1569 char buf[MAX_PATH];
1570 int dirlevel = tc->dirlevel;
1572 /* We need to set the table to allsubentries. */
1573 show_search_progress(true, 0);
1575 dptr = tagtree_get_entry(tc, tc->selected_item);
1577 /* Insert a single track? */
1578 if (dptr->newtable == playtrack)
1580 if (tagtree_get_filename(tc, buf, sizeof buf) < 0)
1582 logf("tagtree_get_filename failed");
1583 return false;
1585 playlist_insert_track(NULL, buf, position, queue, true);
1587 return true;
1590 if (dptr->newtable == navibrowse)
1592 tagtree_enter(tc);
1593 tagtree_load(tc);
1594 dptr = tagtree_get_entry(tc, tc->selected_item);
1596 else if (dptr->newtable != allsubentries)
1598 logf("unsupported table: %d", dptr->newtable);
1599 return false;
1602 /* Now the current table should be allsubentries. */
1603 if (dptr->newtable != playtrack)
1605 tagtree_enter(tc);
1606 tagtree_load(tc);
1607 dptr = tagtree_get_entry(tc, tc->selected_item);
1609 /* And now the newtable should be playtrack. */
1610 if (dptr->newtable != playtrack)
1612 logf("newtable: %d !!", dptr->newtable);
1613 tc->dirlevel = dirlevel;
1614 return false;
1618 if (tc->filesindir <= 0)
1619 gui_syncsplash(HZ, str(LANG_END_PLAYLIST_PLAYER));
1620 else
1622 logf("insert_all_playlist");
1623 if (!insert_all_playlist(tc, position, queue))
1624 gui_syncsplash(HZ*2, str(LANG_FAILED));
1627 /* Finally return the dirlevel to its original value. */
1628 while (tc->dirlevel > dirlevel)
1629 tagtree_exit(tc);
1630 tagtree_load(tc);
1632 return true;
1635 static int tagtree_play_folder(struct tree_context* c)
1637 if (playlist_create(NULL, NULL) < 0)
1639 logf("Failed creating playlist\n");
1640 return -1;
1643 if (!insert_all_playlist(c, PLAYLIST_INSERT_LAST, false))
1644 return -2;
1646 if (global_settings.playlist_shuffle)
1647 c->selected_item = playlist_shuffle(current_tick, c->selected_item);
1648 if (!global_settings.play_selected)
1649 c->selected_item = 0;
1650 gui_synclist_select_item(&tree_lists, c->selected_item);
1652 playlist_start(c->selected_item,0);
1654 return 0;
1657 struct tagentry* tagtree_get_entry(struct tree_context *c, int id)
1659 struct tagentry *entry = (struct tagentry *)c->dircache;
1660 int realid = id - current_offset;
1662 /* Load the next chunk if necessary. */
1663 if (realid >= current_entry_count || realid < 0)
1665 cpu_boost(true);
1666 if (retrieve_entries(c, &tcs2, MAX(0, id - (current_entry_count / 2)),
1667 false) < 0)
1669 logf("retrieve failed");
1670 cpu_boost(false);
1671 return NULL;
1673 realid = id - current_offset;
1674 cpu_boost(false);
1677 return &entry[realid];
1680 char *tagtree_get_title(struct tree_context* c)
1682 switch (c->currtable)
1684 case root:
1685 return menu->title;
1687 case navibrowse:
1688 case allsubentries:
1689 return current_title[c->currextra];
1692 return "?";
1695 int tagtree_get_attr(struct tree_context* c)
1697 int attr = -1;
1698 switch (c->currtable)
1700 case navibrowse:
1701 if (csi->tagorder[c->currextra] == tag_title)
1702 attr = FILE_ATTR_AUDIO;
1703 else
1704 attr = ATTR_DIRECTORY;
1705 break;
1707 case allsubentries:
1708 attr = FILE_ATTR_AUDIO;
1709 break;
1711 default:
1712 attr = ATTR_DIRECTORY;
1713 break;
1716 return attr;
1719 int tagtree_get_icon(struct tree_context* c)
1721 int icon = Icon_Folder;
1723 if (tagtree_get_attr(c) == FILE_ATTR_AUDIO)
1724 icon = Icon_Audio;
1726 return icon;