Add platform file for Ipod 1G / 2G. Now only the front image is missing for building...
[Rockbox.git] / apps / tagtree.c
blob271f30ffa83a3bfb17b2e69f06d7b8d243cd61b0
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)
628 id3->rating = tagcache_get_numeric(&tcs, tag_rating);
629 id3->lastplayed = tagcache_get_numeric(&tcs, tag_lastplayed);
630 id3->score = tagcache_get_numeric(&tcs, tag_virt_autoscore) / 10;
631 id3->playtime = tagcache_get_numeric(&tcs, tag_playtime);
633 /* Store our tagcache index pointer. */
634 id3->tagcache_idx = tcs.idx_id;
636 tagcache_search_finish(&tcs);
639 static void tagtree_unbuffer_event(struct mp3entry *id3, bool last_track)
641 (void)last_track;
642 long playcount;
643 long playtime;
644 long lastplayed;
646 /* Do not gather data unless proper setting has been enabled. */
647 if (!global_settings.runtimedb)
649 logf("runtimedb gathering not enabled");
650 return;
653 if (!id3->tagcache_idx)
655 logf("No tagcache index pointer found");
656 return;
659 /* Don't process unplayed tracks. */
660 if (id3->elapsed == 0)
662 logf("not logging unplayed track");
663 return;
666 playcount = id3->playcount + 1;
667 lastplayed = tagcache_increase_serial();
668 if (lastplayed < 0)
670 logf("incorrect tc serial:%ld", lastplayed);
671 return;
674 /* Ignore the last 15s (crossfade etc.) */
675 playtime = id3->playtime + MIN(id3->length, id3->elapsed + 15 * 1000);
677 logf("ube:%s", id3->path);
678 logf("-> %d/%ld/%ld", last_track, playcount, playtime);
679 logf("-> %ld/%ld/%ld", id3->elapsed, id3->length, MIN(id3->length, id3->elapsed + 15 * 1000));
681 /* Queue the updates to the tagcache system. */
682 tagcache_update_numeric(id3->tagcache_idx, tag_playcount, playcount);
683 tagcache_update_numeric(id3->tagcache_idx, tag_playtime, playtime);
684 tagcache_update_numeric(id3->tagcache_idx, tag_lastplayed, lastplayed);
687 bool tagtree_export(void)
689 gui_syncsplash(0, str(LANG_CREATING));
690 if (!tagcache_create_changelog(&tcs))
692 gui_syncsplash(HZ*2, str(LANG_FAILED));
695 return false;
698 bool tagtree_import(void)
700 gui_syncsplash(0, str(LANG_WAIT));
701 if (!tagcache_import_changelog())
703 gui_syncsplash(HZ*2, str(LANG_FAILED));
706 return false;
709 static bool parse_menu(const char *filename);
711 static int parse_line(int n, const char *buf, void *parameters)
713 char data[256];
714 int variable;
715 static bool read_menu;
716 int i;
718 (void)parameters;
720 logf("parse:%d/%s", n, buf);
722 /* First line, do initialisation. */
723 if (n == 0)
725 if (strcasecmp(TAGNAVI_VERSION, buf))
727 logf("Version mismatch");
728 return -1;
731 read_menu = false;
734 if (buf[0] == '#')
735 return 0;
737 if (buf[0] == '\0')
739 if (read_menu)
741 /* End the menu */
742 read_menu = false;
744 return 0;
747 if (!read_menu)
749 strp = buf;
750 if (get_tag(&variable) <= 0)
751 return 0;
753 switch (variable)
755 case var_format:
756 if (add_format(strp) < 0)
758 logf("Format add fail: %s", data);
760 break;
762 case var_include:
763 if (get_token_str(data, sizeof(data)) < 0)
765 logf("%%include empty");
766 return 0;
769 if (!parse_menu(data))
771 logf("Load menu fail: %s", data);
773 break;
775 case var_menu_start:
776 if (menu_count >= TAGMENU_MAX_MENUS)
778 logf("max menucount reached");
779 return 0;
782 if (get_token_str(data, sizeof data) < 0)
784 logf("%%menu_start id empty");
785 return 0;
788 menu = NULL;
789 for (i = 0; i < menu_count; i++)
791 if (!strcasecmp(menus[i]->id, data))
793 menu = menus[i];
797 if (menu == NULL)
799 menus[menu_count] = buffer_alloc(sizeof(struct root_menu));
800 menu = menus[menu_count];
801 ++menu_count;
802 memset(menu, 0, sizeof(struct root_menu));
803 strncpy(menu->id, data, MAX_MENU_ID_SIZE-1);
806 if (get_token_str(menu->title, sizeof(menu->title)) < 0)
808 logf("%%menu_start title empty");
809 return 0;
811 logf("menu: %s", menu->title);
812 read_menu = true;
813 break;
815 case var_rootmenu:
816 /* Only set root menu once. */
817 if (root_menu >= 0)
818 break;
820 if (get_token_str(data, sizeof(data)) < 0)
822 logf("%%root_menu empty");
823 return 0;
826 for (i = 0; i < menu_count; i++)
828 if (!strcasecmp(menus[i]->id, data))
830 root_menu = i;
833 break;
836 return 0;
839 if (menu->itemcount >= TAGMENU_MAX_ITEMS)
841 logf("max itemcount reached");
842 return 0;
845 /* Allocate */
846 if (menu->items[menu->itemcount] == NULL)
848 menu->items[menu->itemcount] = buffer_alloc(sizeof(struct menu_entry));
849 memset(menu->items[menu->itemcount], 0, sizeof(struct menu_entry));
850 menu->items[menu->itemcount]->si = buffer_alloc(sizeof(struct search_instruction));
853 memset(menu->items[menu->itemcount]->si, 0, sizeof(struct search_instruction));
854 if (!parse_search(menu->items[menu->itemcount], buf))
855 return 0;
857 menu->itemcount++;
859 return 0;
862 static bool parse_menu(const char *filename)
864 int fd;
865 char buf[1024];
867 if (menu_count >= TAGMENU_MAX_MENUS)
869 logf("max menucount reached");
870 return false;
873 fd = open(filename, O_RDONLY);
874 if (fd < 0)
876 logf("Search instruction file not found.");
877 return false;
880 /* Now read file for real, parsing into si */
881 fast_readline(fd, buf, sizeof buf, NULL, parse_line);
882 close(fd);
884 return true;
887 void tagtree_init(void)
889 format_count = 0;
890 menu_count = 0;
891 menu = NULL;
892 root_menu = -1;
893 parse_menu(FILE_SEARCH_INSTRUCTIONS);
895 /* If no root menu is set, assume it's the first single menu
896 * we have. That shouldn't normally happen. */
897 if (root_menu < 0)
898 root_menu = 0;
900 uniqbuf = buffer_alloc(UNIQBUF_SIZE);
901 audio_set_track_buffer_event(tagtree_buffer_event);
902 audio_set_track_unbuffer_event(tagtree_unbuffer_event);
905 static bool show_search_progress(bool init, int count)
907 static int last_tick = 0;
909 if (init)
911 last_tick = current_tick;
912 return true;
915 if (current_tick - last_tick > HZ/4)
917 gui_syncsplash(0, str(LANG_PLAYLIST_SEARCH_MSG), count,
918 #if CONFIG_KEYPAD == PLAYER_PAD
919 str(LANG_STOP_ABORT)
920 #else
921 str(LANG_OFF_ABORT)
922 #endif
924 if (action_userabort(TIMEOUT_NOBLOCK))
925 return false;
926 last_tick = current_tick;
927 yield();
930 return true;
933 static int format_str(struct tagcache_search *tcs, struct display_format *fmt,
934 char *buf, int buf_size)
936 char fmtbuf[8];
937 bool read_format = false;
938 int fmtbuf_pos = 0;
939 int parpos = 0;
940 int buf_pos = 0;
941 int i;
943 memset(buf, 0, buf_size);
944 for (i = 0; fmt->formatstr[i] != '\0'; i++)
946 if (fmt->formatstr[i] == '%')
948 read_format = true;
949 fmtbuf_pos = 0;
950 if (parpos >= fmt->tag_count)
952 logf("too many format tags");
953 return -1;
957 if (read_format)
959 fmtbuf[fmtbuf_pos++] = fmt->formatstr[i];
960 if (fmtbuf_pos >= buf_size)
962 logf("format parse error");
963 return -2;
966 if (fmt->formatstr[i] == 's')
968 fmtbuf[fmtbuf_pos] = '\0';
969 read_format = false;
970 if (fmt->tags[parpos] == tcs->type)
972 snprintf(&buf[buf_pos], buf_size - buf_pos, fmtbuf, tcs->result);
974 else
976 /* Need to fetch the tag data. */
977 if (!tagcache_retrieve(tcs, tcs->idx_id, fmt->tags[parpos],
978 &buf[buf_pos], buf_size - buf_pos))
980 logf("retrieve failed");
981 return -3;
984 buf_pos += strlen(&buf[buf_pos]);
985 parpos++;
987 else if (fmt->formatstr[i] == 'd')
989 fmtbuf[fmtbuf_pos] = '\0';
990 read_format = false;
991 snprintf(&buf[buf_pos], buf_size - buf_pos, fmtbuf,
992 tagcache_get_numeric(tcs, fmt->tags[parpos]));
993 buf_pos += strlen(&buf[buf_pos]);
994 parpos++;
996 continue;
999 buf[buf_pos++] = fmt->formatstr[i];
1001 if (buf_pos - 1 >= buf_size)
1003 logf("buffer overflow");
1004 return -4;
1008 buf[buf_pos++] = '\0';
1010 return 0;
1013 static int retrieve_entries(struct tree_context *c, struct tagcache_search *tcs,
1014 int offset, bool init)
1016 struct tagentry *dptr = (struct tagentry *)c->dircache;
1017 struct display_format *fmt;
1018 int i;
1019 int namebufused = 0;
1020 int total_count = 0;
1021 int special_entry_count = 0;
1022 int level = c->currextra;
1023 int tag;
1024 bool sort = false;
1025 int sort_limit;
1026 int strip;
1028 if (init
1029 #ifdef HAVE_TC_RAMCACHE
1030 && !tagcache_is_ramcache()
1031 #endif
1034 show_search_progress(true, 0);
1035 gui_syncsplash(0, str(LANG_PLAYLIST_SEARCH_MSG),
1036 0, csi->name);
1039 if (c->currtable == allsubentries)
1041 tag = tag_title;
1042 level--;
1044 else
1045 tag = csi->tagorder[level];
1047 if (!tagcache_search(tcs, tag))
1048 return -1;
1050 /* Prevent duplicate entries in the search list. */
1051 tagcache_search_set_uniqbuf(tcs, uniqbuf, UNIQBUF_SIZE);
1053 if (level || csi->clause_count[0] || tagcache_is_numeric_tag(tag))
1054 sort = true;
1056 for (i = 0; i < level; i++)
1058 if (tagcache_is_numeric_tag(csi->tagorder[i]))
1060 static struct tagcache_search_clause cc;
1062 memset(&cc, 0, sizeof(struct tagcache_search_clause));
1063 cc.tag = csi->tagorder[i];
1064 cc.type = clause_is;
1065 cc.numeric = true;
1066 cc.numeric_data = csi->result_seek[i];
1067 tagcache_search_add_clause(tcs, &cc);
1069 else
1071 tagcache_search_add_filter(tcs, csi->tagorder[i],
1072 csi->result_seek[i]);
1076 for (i = 0; i <= level; i++)
1078 int j;
1080 for (j = 0; j < csi->clause_count[i]; j++)
1081 tagcache_search_add_clause(tcs, csi->clause[i][j]);
1084 current_offset = offset;
1085 current_entry_count = 0;
1086 c->dirfull = false;
1088 fmt = NULL;
1089 for (i = 0; i < format_count; i++)
1091 if (formats[i]->group_id == csi->format_id[level])
1092 fmt = formats[i];
1095 if (fmt)
1097 sort_inverse = fmt->sort_inverse;
1098 sort_limit = fmt->limit;
1099 strip = fmt->strip;
1101 /* Check if sorting is forced. */
1102 if (fmt->sort)
1103 sort = true;
1105 else
1107 sort_inverse = false;
1108 sort_limit = 0;
1109 strip = 0;
1112 if (tag != tag_title && tag != tag_filename)
1114 if (offset == 0)
1116 dptr->newtable = allsubentries;
1117 dptr->name = str(LANG_TAGNAVI_ALL_TRACKS);
1118 dptr++;
1119 current_entry_count++;
1121 special_entry_count++;
1124 total_count += special_entry_count;
1126 while (tagcache_get_next(tcs))
1128 if (total_count++ < offset)
1129 continue;
1131 dptr->newtable = navibrowse;
1132 if (tag == tag_title || tag == tag_filename)
1134 dptr->newtable = playtrack;
1135 dptr->extraseek = tcs->idx_id;
1137 else
1138 dptr->extraseek = tcs->result_seek;
1140 fmt = NULL;
1141 /* Check the format */
1142 for (i = 0; i < format_count; i++)
1144 if (formats[i]->group_id != csi->format_id[level])
1145 continue;
1147 if (tagcache_check_clauses(tcs, formats[i]->clause,
1148 formats[i]->clause_count))
1150 fmt = formats[i];
1151 break;
1155 if (!tcs->ramresult || fmt)
1157 char buf[MAX_PATH];
1159 if (fmt)
1161 if (format_str(tcs, fmt, buf, sizeof buf) < 0)
1163 logf("format_str() failed");
1164 return 0;
1168 dptr->name = &c->name_buffer[namebufused];
1169 if (fmt)
1170 namebufused += strlen(buf)+1;
1171 else
1172 namebufused += tcs->result_len;
1174 if (namebufused >= c->name_buffer_size)
1176 logf("chunk mode #2: %d", current_entry_count);
1177 c->dirfull = true;
1178 sort = false;
1179 break ;
1181 if (fmt)
1182 strcpy(dptr->name, buf);
1183 else
1184 strcpy(dptr->name, tcs->result);
1186 else
1187 dptr->name = tcs->result;
1189 dptr++;
1190 current_entry_count++;
1192 if (current_entry_count >= global_settings.max_files_in_dir)
1194 logf("chunk mode #3: %d", current_entry_count);
1195 c->dirfull = true;
1196 sort = false;
1197 break ;
1200 if (init && !tcs->ramsearch)
1202 if (!show_search_progress(false, i))
1204 tagcache_search_finish(tcs);
1205 return current_entry_count;
1210 if (sort)
1211 qsort(c->dircache + special_entry_count * c->dentry_size,
1212 current_entry_count - special_entry_count,
1213 c->dentry_size, compare);
1215 if (!init)
1217 tagcache_search_finish(tcs);
1218 return current_entry_count;
1221 while (tagcache_get_next(tcs))
1223 if (!tcs->ramsearch)
1225 if (!show_search_progress(false, total_count))
1226 break;
1228 total_count++;
1231 tagcache_search_finish(tcs);
1233 if (!sort && (sort_inverse || sort_limit))
1235 gui_syncsplash(HZ*4, str(LANG_SHOWDIR_BUFFER_FULL), total_count);
1236 logf("Too small dir buffer");
1237 return 0;
1240 if (sort_limit)
1241 total_count = MIN(total_count, sort_limit);
1243 if (strip)
1245 dptr = c->dircache;
1246 for (i = 0; i < total_count; i++, dptr++)
1248 int len = strlen(dptr->name);
1250 if (len < strip)
1251 continue;
1253 dptr->name = &dptr->name[strip];
1257 return total_count;
1260 static int load_root(struct tree_context *c)
1262 struct tagentry *dptr = (struct tagentry *)c->dircache;
1263 int i;
1265 tc = c;
1266 c->currtable = root;
1267 if (c->dirlevel == 0)
1268 c->currextra = root_menu;
1270 menu = menus[c->currextra];
1271 if (menu == NULL)
1272 return 0;
1274 for (i = 0; i < menu->itemcount; i++)
1276 dptr->name = menu->items[i]->name;
1277 switch (menu->items[i]->type)
1279 case menu_next:
1280 dptr->newtable = navibrowse;
1281 dptr->extraseek = i;
1282 break;
1284 case menu_load:
1285 dptr->newtable = root;
1286 dptr->extraseek = menu->items[i]->link;
1287 break;
1290 dptr++;
1293 current_offset = 0;
1294 current_entry_count = i;
1296 return i;
1299 int tagtree_load(struct tree_context* c)
1301 int count;
1302 int table = c->currtable;
1304 c->dentry_size = sizeof(struct tagentry);
1305 c->dirsindir = 0;
1307 if (!table)
1309 c->dirfull = false;
1310 table = root;
1311 c->currtable = table;
1312 c->currextra = root_menu;
1315 switch (table)
1317 case root:
1318 count = load_root(c);
1319 break;
1321 case allsubentries:
1322 case navibrowse:
1323 logf("navibrowse...");
1324 cpu_boost(true);
1325 count = retrieve_entries(c, &tcs, 0, true);
1326 cpu_boost(false);
1327 break;
1329 default:
1330 logf("Unsupported table %d\n", table);
1331 return -1;
1334 if (count < 0)
1336 c->dirlevel = 0;
1337 count = load_root(c);
1338 gui_syncsplash(HZ, str(LANG_TAGCACHE_BUSY));
1341 /* The _total_ numer of entries available. */
1342 c->dirlength = c->filesindir = count;
1344 return count;
1347 int tagtree_enter(struct tree_context* c)
1349 int rc = 0;
1350 struct tagentry *dptr;
1351 int newextra;
1352 int seek;
1354 dptr = tagtree_get_entry(c, c->selected_item);
1356 c->dirfull = false;
1357 newextra = dptr->newtable;
1358 seek = dptr->extraseek;
1360 if (c->dirlevel >= MAX_DIR_LEVELS)
1361 return 0;
1363 c->selected_item_history[c->dirlevel]=c->selected_item;
1364 c->table_history[c->dirlevel] = c->currtable;
1365 c->extra_history[c->dirlevel] = c->currextra;
1366 c->pos_history[c->dirlevel] = c->firstpos;
1367 c->dirlevel++;
1369 switch (c->currtable) {
1370 case root:
1371 c->currextra = newextra;
1373 if (newextra == root)
1375 menu = menus[seek];
1376 c->currextra = seek;
1379 else if (newextra == navibrowse)
1381 int i, j;
1383 csi = menu->items[seek]->si;
1384 c->currextra = 0;
1386 strncpy(current_title[c->currextra], dptr->name,
1387 sizeof(current_title[0]) - 1);
1389 /* Read input as necessary. */
1390 for (i = 0; i < csi->tagorder_count; i++)
1392 for (j = 0; j < csi->clause_count[i]; j++)
1394 if (!csi->clause[i][j]->input)
1395 continue;
1397 rc = kbd_input(searchstring, sizeof(searchstring));
1398 if (rc == -1 || !searchstring[0])
1400 tagtree_exit(c);
1401 return 0;
1404 if (csi->clause[i][j]->numeric)
1405 csi->clause[i][j]->numeric_data = atoi(searchstring);
1406 else
1407 csi->clause[i][j]->str = searchstring;
1411 c->currtable = newextra;
1413 break;
1415 case navibrowse:
1416 case allsubentries:
1417 if (newextra == playtrack)
1419 c->dirlevel--;
1420 /* about to create a new current playlist...
1421 allow user to cancel the operation */
1422 if (global_settings.warnon_erase_dynplaylist &&
1423 !global_settings.party_mode &&
1424 playlist_modified(NULL))
1426 char *lines[]={str(LANG_WARN_ERASEDYNPLAYLIST_PROMPT)};
1427 struct text_message message={lines, 1};
1429 if (gui_syncyesno_run(&message, NULL, NULL) != YESNO_YES)
1430 break;
1433 if (tagtree_play_folder(c) >= 0)
1434 rc = 2;
1435 break;
1438 c->currtable = newextra;
1439 csi->result_seek[c->currextra] = seek;
1440 if (c->currextra < csi->tagorder_count-1)
1441 c->currextra++;
1442 else
1443 c->dirlevel--;
1445 /* Update the statusbar title */
1446 strncpy(current_title[c->currextra], dptr->name,
1447 sizeof(current_title[0]) - 1);
1448 break;
1450 default:
1451 c->dirlevel--;
1452 break;
1455 c->selected_item=0;
1456 gui_synclist_select_item(&tree_lists, c->selected_item);
1458 return rc;
1461 void tagtree_exit(struct tree_context* c)
1463 c->dirfull = false;
1464 if (c->dirlevel > 0)
1465 c->dirlevel--;
1466 c->selected_item=c->selected_item_history[c->dirlevel];
1467 gui_synclist_select_item(&tree_lists, c->selected_item);
1468 c->currtable = c->table_history[c->dirlevel];
1469 c->currextra = c->extra_history[c->dirlevel];
1470 c->firstpos = c->pos_history[c->dirlevel];
1473 int tagtree_get_filename(struct tree_context* c, char *buf, int buflen)
1475 struct tagentry *entry;
1477 entry = tagtree_get_entry(c, c->selected_item);
1479 if (!tagcache_search(&tcs, tag_filename))
1480 return -1;
1482 if (!tagcache_retrieve(&tcs, entry->extraseek, tcs.type, buf, buflen))
1484 tagcache_search_finish(&tcs);
1485 return -2;
1488 tagcache_search_finish(&tcs);
1490 return 0;
1493 static bool insert_all_playlist(struct tree_context *c, int position, bool queue)
1495 int i;
1496 char buf[MAX_PATH];
1497 int from, to, direction;
1498 int files_left = c->filesindir;
1500 cpu_boost(true);
1501 if (!tagcache_search(&tcs, tag_filename))
1503 gui_syncsplash(HZ, str(LANG_TAGCACHE_BUSY));
1504 cpu_boost(false);
1505 return false;
1508 if (position == PLAYLIST_REPLACE)
1510 if (remove_all_tracks(NULL) == 0)
1511 position = PLAYLIST_INSERT_LAST;
1512 else return -1; }
1514 if (position == PLAYLIST_INSERT_FIRST)
1516 from = c->filesindir - 1;
1517 to = -1;
1518 direction = -1;
1520 else
1522 from = 0;
1523 to = c->filesindir;
1524 direction = 1;
1527 for (i = from; i != to; i += direction)
1529 /* Count back to zero */
1530 if (!show_search_progress(false, files_left--))
1531 break;
1533 if (!tagcache_retrieve(&tcs, tagtree_get_entry(c, i)->extraseek,
1534 tcs.type, buf, sizeof buf))
1536 continue;
1539 if (playlist_insert_track(NULL, buf, position, queue, false) < 0)
1541 logf("playlist_insert_track failed");
1542 break;
1544 yield();
1546 playlist_sync(NULL);
1547 tagcache_search_finish(&tcs);
1548 cpu_boost(false);
1550 return true;
1553 bool tagtree_insert_selection_playlist(int position, bool queue)
1555 struct tagentry *dptr;
1556 char buf[MAX_PATH];
1557 int dirlevel = tc->dirlevel;
1559 /* We need to set the table to allsubentries. */
1560 show_search_progress(true, 0);
1562 dptr = tagtree_get_entry(tc, tc->selected_item);
1564 /* Insert a single track? */
1565 if (dptr->newtable == playtrack)
1567 if (tagtree_get_filename(tc, buf, sizeof buf) < 0)
1569 logf("tagtree_get_filename failed");
1570 return false;
1572 playlist_insert_track(NULL, buf, position, queue, true);
1574 return true;
1577 if (dptr->newtable == navibrowse)
1579 tagtree_enter(tc);
1580 tagtree_load(tc);
1581 dptr = tagtree_get_entry(tc, tc->selected_item);
1583 else if (dptr->newtable != allsubentries)
1585 logf("unsupported table: %d", dptr->newtable);
1586 return false;
1589 /* Now the current table should be allsubentries. */
1590 if (dptr->newtable != playtrack)
1592 tagtree_enter(tc);
1593 tagtree_load(tc);
1594 dptr = tagtree_get_entry(tc, tc->selected_item);
1596 /* And now the newtable should be playtrack. */
1597 if (dptr->newtable != playtrack)
1599 logf("newtable: %d !!", dptr->newtable);
1600 tc->dirlevel = dirlevel;
1601 return false;
1605 if (tc->filesindir <= 0)
1606 gui_syncsplash(HZ, str(LANG_END_PLAYLIST_PLAYER));
1607 else
1609 logf("insert_all_playlist");
1610 if (!insert_all_playlist(tc, position, queue))
1611 gui_syncsplash(HZ*2, str(LANG_FAILED));
1614 /* Finally return the dirlevel to its original value. */
1615 while (tc->dirlevel > dirlevel)
1616 tagtree_exit(tc);
1617 tagtree_load(tc);
1619 return true;
1622 static int tagtree_play_folder(struct tree_context* c)
1624 if (playlist_create(NULL, NULL) < 0)
1626 logf("Failed creating playlist\n");
1627 return -1;
1630 if (!insert_all_playlist(c, PLAYLIST_INSERT_LAST, false))
1631 return -2;
1633 if (global_settings.playlist_shuffle)
1634 c->selected_item = playlist_shuffle(current_tick, c->selected_item);
1635 if (!global_settings.play_selected)
1636 c->selected_item = 0;
1637 gui_synclist_select_item(&tree_lists, c->selected_item);
1639 playlist_start(c->selected_item,0);
1641 return 0;
1644 struct tagentry* tagtree_get_entry(struct tree_context *c, int id)
1646 struct tagentry *entry = (struct tagentry *)c->dircache;
1647 int realid = id - current_offset;
1649 /* Load the next chunk if necessary. */
1650 if (realid >= current_entry_count || realid < 0)
1652 cpu_boost(true);
1653 if (retrieve_entries(c, &tcs2, MAX(0, id - (current_entry_count / 2)),
1654 false) < 0)
1656 logf("retrieve failed");
1657 cpu_boost(false);
1658 return NULL;
1660 realid = id - current_offset;
1661 cpu_boost(false);
1664 return &entry[realid];
1667 char *tagtree_get_title(struct tree_context* c)
1669 switch (c->currtable)
1671 case root:
1672 return menu->title;
1674 case navibrowse:
1675 case allsubentries:
1676 return current_title[c->currextra];
1679 return "?";
1682 int tagtree_get_attr(struct tree_context* c)
1684 int attr = -1;
1685 switch (c->currtable)
1687 case navibrowse:
1688 if (csi->tagorder[c->currextra] == tag_title)
1689 attr = FILE_ATTR_AUDIO;
1690 else
1691 attr = ATTR_DIRECTORY;
1692 break;
1694 case allsubentries:
1695 attr = FILE_ATTR_AUDIO;
1696 break;
1698 default:
1699 attr = ATTR_DIRECTORY;
1700 break;
1703 return attr;
1706 int tagtree_get_icon(struct tree_context* c)
1708 int icon = Icon_Folder;
1710 if (tagtree_get_attr(c) == FILE_ATTR_AUDIO)
1711 icon = Icon_Audio;
1713 return icon;