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