1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
21 * Basic structure on this file was copied from dbtree.c and modified to
22 * support the tag cache interface.
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];
67 /* Capacity 10 000 entries (for example 10k different artists) */
68 #define UNIQBUF_SIZE (64*1024)
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"
81 * formatstr = "%-3d. %s"
82 * tags[0] = tag_autoscore
89 struct display_format
{
91 struct tagcache_search_clause
*clause
[TAGCACHE_MAX_CLAUSES
];
104 static struct display_format
*formats
[TAGMENU_MAX_FMTS
];
105 static int format_count
;
107 struct search_instruction
{
109 int tagorder
[MAX_TAGS
];
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
];
120 struct search_instruction
*si
;
126 char id
[MAX_MENU_ID_SIZE
];
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')
153 if (*strp
== '\0' || *(++strp
) == '\0')
157 while (*strp
!= '"' && *strp
!= '\0' && --size
> 0)
158 *(buf
++) = *(strp
++);
169 #define MATCH(tag,str1,str2,settag) \
170 if (!strcasecmp(str1, str2)) { \
175 static int get_tag(int *tag
)
180 /* Find the start. */
181 while ((*strp
== ' ' || *strp
== '>') && *strp
!= '\0')
184 if (*strp
== '\0' || *strp
== '?')
187 for (i
= 0; i
< (int)sizeof(buf
)-1; i
++)
189 if (*strp
== '\0' || *strp
== ' ')
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
, "grouping", tag_grouping
);
204 MATCH(tag
, buf
, "genre", tag_genre
);
205 MATCH(tag
, buf
, "length", tag_length
);
206 MATCH(tag
, buf
, "Lm", tag_virt_length_min
);
207 MATCH(tag
, buf
, "Ls", tag_virt_length_sec
);
208 MATCH(tag
, buf
, "Pm", tag_virt_playtime_min
);
209 MATCH(tag
, buf
, "Ps", tag_virt_playtime_sec
);
210 MATCH(tag
, buf
, "title", tag_title
);
211 MATCH(tag
, buf
, "filename", tag_filename
);
212 MATCH(tag
, buf
, "tracknum", tag_tracknumber
);
213 MATCH(tag
, buf
, "discnum", tag_discnumber
);
214 MATCH(tag
, buf
, "year", tag_year
);
215 MATCH(tag
, buf
, "playcount", tag_playcount
);
216 MATCH(tag
, buf
, "rating", tag_rating
);
217 MATCH(tag
, buf
, "lastplayed", tag_lastplayed
);
218 MATCH(tag
, buf
, "commitid", tag_commitid
);
219 MATCH(tag
, buf
, "entryage", tag_virt_entryage
);
220 MATCH(tag
, buf
, "autoscore", tag_virt_autoscore
);
221 MATCH(tag
, buf
, "%sort", var_sorttype
);
222 MATCH(tag
, buf
, "%limit", var_limit
);
223 MATCH(tag
, buf
, "%strip", var_strip
);
224 MATCH(tag
, buf
, "%menu_start", var_menu_start
);
225 MATCH(tag
, buf
, "%include", var_include
);
226 MATCH(tag
, buf
, "%root_menu", var_rootmenu
);
227 MATCH(tag
, buf
, "%format", var_format
);
228 MATCH(tag
, buf
, "->", menu_next
);
229 MATCH(tag
, buf
, "==>", menu_load
);
231 logf("NO MATCH: %s\n", buf
);
238 static int get_clause(int *condition
)
243 /* Find the start. */
244 while (*strp
== ' ' && *strp
!= '\0')
250 for (i
= 0; i
< (int)sizeof(buf
)-1; i
++)
252 if (*strp
== '\0' || *strp
== ' ')
259 MATCH(condition
, buf
, "=", clause_is
);
260 MATCH(condition
, buf
, "==", clause_is
);
261 MATCH(condition
, buf
, "!=", clause_is_not
);
262 MATCH(condition
, buf
, ">", clause_gt
);
263 MATCH(condition
, buf
, ">=", clause_gteq
);
264 MATCH(condition
, buf
, "<", clause_lt
);
265 MATCH(condition
, buf
, "<=", clause_lteq
);
266 MATCH(condition
, buf
, "~", clause_contains
);
267 MATCH(condition
, buf
, "!~", clause_not_contains
);
268 MATCH(condition
, buf
, "^", clause_begins_with
);
269 MATCH(condition
, buf
, "!^", clause_not_begins_with
);
270 MATCH(condition
, buf
, "$", clause_ends_with
);
271 MATCH(condition
, buf
, "!$", clause_not_ends_with
);
272 MATCH(condition
, buf
, "@", clause_oneof
);
277 static bool read_clause(struct tagcache_search_clause
*clause
)
281 if (get_tag(&clause
->tag
) <= 0)
284 if (get_clause(&clause
->type
) <= 0)
287 if (get_token_str(buf
, sizeof buf
) < 0)
290 clause
->str
= buffer_alloc(strlen(buf
)+1);
291 strcpy(clause
->str
, buf
);
293 logf("got clause: %d/%d [%s]", clause
->tag
, clause
->type
, clause
->str
);
295 if (*(clause
->str
) == '\0')
296 clause
->input
= true;
298 clause
->input
= false;
300 if (tagcache_is_numeric_tag(clause
->tag
))
302 clause
->numeric
= true;
303 clause
->numeric_data
= atoi(clause
->str
);
306 clause
->numeric
= false;
311 static bool read_variable(char *buf
, int size
)
315 if (!get_clause(&condition
))
318 if (condition
!= clause_is
)
321 if (get_token_str(buf
, size
) < 0)
327 /* "%3d. %s" autoscore title %sort = "inverse" %limit = "100" */
328 static int get_format_str(struct display_format
*fmt
)
334 memset(fmt
, 0, sizeof(struct display_format
));
336 if (get_token_str(fmt
->name
, sizeof fmt
->name
) < 0)
339 /* Determine the group id */
341 for (i
= 0; i
< format_count
; i
++)
343 if (!strcasecmp(formats
[i
]->name
, fmt
->name
))
345 fmt
->group_id
= formats
[i
]->group_id
;
349 if (formats
[i
]->group_id
> fmt
->group_id
)
350 fmt
->group_id
= formats
[i
]->group_id
;
353 if (i
== format_count
)
356 logf("format: (%d) %s", fmt
->group_id
, fmt
->name
);
358 if (get_token_str(buf
, sizeof buf
) < 0)
361 fmt
->formatstr
= buffer_alloc(strlen(buf
) + 1);
362 strcpy(fmt
->formatstr
, buf
);
364 while (fmt
->tag_count
< MAX_TAGS
)
366 ret
= get_tag(&fmt
->tags
[fmt
->tag_count
]);
373 switch (fmt
->tags
[fmt
->tag_count
]) {
375 if (!read_variable(buf
, sizeof buf
))
377 if (!strcasecmp("inverse", buf
))
378 fmt
->sort_inverse
= true;
384 if (!read_variable(buf
, sizeof buf
))
386 fmt
->limit
= atoi(buf
);
390 if (!read_variable(buf
, sizeof buf
))
392 fmt
->strip
= atoi(buf
);
403 static int add_format(const char *buf
)
407 if (formats
[format_count
] == NULL
)
408 formats
[format_count
] = buffer_alloc(sizeof(struct display_format
));
410 memset(formats
[format_count
], 0, sizeof(struct display_format
));
411 if (get_format_str(formats
[format_count
]) < 0)
413 logf("get_format_str() parser failed!");
417 while (*strp
!= '\0' && *strp
!= '?')
422 int clause_count
= 0;
427 if (clause_count
>= TAGCACHE_MAX_CLAUSES
)
429 logf("too many clauses");
433 formats
[format_count
]->clause
[clause_count
] =
434 buffer_alloc(sizeof(struct tagcache_search_clause
));
436 if (!read_clause(formats
[format_count
]->clause
[clause_count
]))
442 formats
[format_count
]->clause_count
= clause_count
;
450 static int get_condition(struct search_instruction
*inst
)
461 if (get_token_str(buf
, sizeof buf
) < 0)
464 for (i
= 0; i
< format_count
; i
++)
466 if (!strcasecmp(formats
[i
]->name
, buf
))
470 if (i
== format_count
)
472 logf("format not found: %s", buf
);
476 inst
->format_id
[inst
->tagorder_count
] = formats
[i
]->group_id
;
489 clause_count
= inst
->clause_count
[inst
->tagorder_count
];
490 if (clause_count
>= TAGCACHE_MAX_CLAUSES
)
492 logf("Too many clauses");
496 inst
->clause
[inst
->tagorder_count
][clause_count
] =
497 buffer_alloc(sizeof(struct tagcache_search_clause
));
499 if (!read_clause(inst
->clause
[inst
->tagorder_count
][clause_count
]))
502 inst
->clause_count
[inst
->tagorder_count
]++;
508 * "Best" artist ? year >= "2000" & title !^ "crap" & genre = "good genre" \
509 * : album ? year >= "2000" : songs
515 static bool parse_search(struct menu_entry
*entry
, const char *str
)
519 struct search_instruction
*inst
= entry
->si
;
522 struct root_menu
*new_menu
;
526 /* Parse entry name */
527 if (get_token_str(entry
->name
, sizeof entry
->name
) < 0)
529 logf("No name found.");
533 /* Parse entry type */
534 if (get_tag(&entry
->type
) <= 0)
537 if (entry
->type
== menu_load
)
539 if (get_token_str(buf
, sizeof buf
) < 0)
542 /* Find the matching root menu or "create" it */
543 for (i
= 0; i
< menu_count
; i
++)
545 if (!strcasecmp(menus
[i
]->id
, buf
))
552 if (menu_count
>= TAGMENU_MAX_MENUS
)
554 logf("max menucount reached");
558 /* Allocate a new menu unless link is found. */
559 menus
[menu_count
] = buffer_alloc(sizeof(struct root_menu
));
560 new_menu
= menus
[menu_count
];
561 memset(new_menu
, 0, sizeof(struct root_menu
));
562 strncpy(new_menu
->id
, buf
, MAX_MENU_ID_SIZE
-1);
563 entry
->link
= menu_count
;
569 if (entry
->type
!= menu_next
)
572 while (inst
->tagorder_count
< MAX_TAGS
)
574 ret
= get_tag(&inst
->tagorder
[inst
->tagorder_count
]);
577 logf("Parse error #1");
585 logf("tag: %d", inst
->tagorder
[inst
->tagorder_count
]);
587 while ( (ret
= get_condition(inst
)) > 0 ) ;
591 inst
->tagorder_count
++;
593 if (get_tag(&type
) <= 0 || type
!= menu_next
)
600 static int compare(const void *p1
, const void *p2
)
602 struct tagentry
*e1
= (struct tagentry
*)p1
;
603 struct tagentry
*e2
= (struct tagentry
*)p2
;
606 return strncasecmp(e2
->name
, e1
->name
, MAX_PATH
);
608 return strncasecmp(e1
->name
, e2
->name
, MAX_PATH
);
611 static void tagtree_buffer_event(struct mp3entry
*id3
, bool last_track
)
616 /* Do not gather data unless proper setting has been enabled. */
617 if (!global_settings
.runtimedb
)
620 logf("be:%d%s", last_track
, id3
->path
);
622 if (!tagcache_find_index(&tcs
, id3
->path
))
624 logf("tc stat: not found: %s", id3
->path
);
628 id3
->playcount
= tagcache_get_numeric(&tcs
, tag_playcount
);
630 id3
->rating
= tagcache_get_numeric(&tcs
, tag_rating
);
631 id3
->lastplayed
= tagcache_get_numeric(&tcs
, tag_lastplayed
);
632 id3
->score
= tagcache_get_numeric(&tcs
, tag_virt_autoscore
) / 10;
633 id3
->playtime
= tagcache_get_numeric(&tcs
, tag_playtime
);
635 /* Store our tagcache index pointer. */
636 id3
->tagcache_idx
= tcs
.idx_id
;
638 tagcache_search_finish(&tcs
);
641 static void tagtree_unbuffer_event(struct mp3entry
*id3
, bool last_track
)
648 /* Do not gather data unless proper setting has been enabled. */
649 if (!global_settings
.runtimedb
)
651 logf("runtimedb gathering not enabled");
655 if (!id3
->tagcache_idx
)
657 logf("No tagcache index pointer found");
661 /* Don't process unplayed tracks. */
662 if (id3
->elapsed
== 0)
664 logf("not logging unplayed track");
668 playcount
= id3
->playcount
+ 1;
669 lastplayed
= tagcache_increase_serial();
672 logf("incorrect tc serial:%ld", lastplayed
);
676 /* Ignore the last 15s (crossfade etc.) */
677 playtime
= id3
->playtime
+ MIN(id3
->length
, id3
->elapsed
+ 15 * 1000);
679 logf("ube:%s", id3
->path
);
680 logf("-> %d/%ld/%ld", last_track
, playcount
, playtime
);
681 logf("-> %ld/%ld/%ld", id3
->elapsed
, id3
->length
, MIN(id3
->length
, id3
->elapsed
+ 15 * 1000));
683 /* Queue the updates to the tagcache system. */
684 tagcache_update_numeric(id3
->tagcache_idx
, tag_playcount
, playcount
);
685 tagcache_update_numeric(id3
->tagcache_idx
, tag_playtime
, playtime
);
686 tagcache_update_numeric(id3
->tagcache_idx
, tag_lastplayed
, lastplayed
);
689 bool tagtree_export(void)
691 gui_syncsplash(0, str(LANG_CREATING
));
692 if (!tagcache_create_changelog(&tcs
))
694 gui_syncsplash(HZ
*2, ID2P(LANG_FAILED
));
700 bool tagtree_import(void)
702 gui_syncsplash(0, ID2P(LANG_WAIT
));
703 if (!tagcache_import_changelog())
705 gui_syncsplash(HZ
*2, ID2P(LANG_FAILED
));
711 static bool parse_menu(const char *filename
);
713 static int parse_line(int n
, const char *buf
, void *parameters
)
717 static bool read_menu
;
722 logf("parse:%d/%s", n
, buf
);
724 /* First line, do initialisation. */
727 if (strcasecmp(TAGNAVI_VERSION
, buf
))
729 logf("Version mismatch");
752 if (get_tag(&variable
) <= 0)
758 if (add_format(strp
) < 0)
760 logf("Format add fail: %s", data
);
765 if (get_token_str(data
, sizeof(data
)) < 0)
767 logf("%%include empty");
771 if (!parse_menu(data
))
773 logf("Load menu fail: %s", data
);
778 if (menu_count
>= TAGMENU_MAX_MENUS
)
780 logf("max menucount reached");
784 if (get_token_str(data
, sizeof data
) < 0)
786 logf("%%menu_start id empty");
791 for (i
= 0; i
< menu_count
; i
++)
793 if (!strcasecmp(menus
[i
]->id
, data
))
801 menus
[menu_count
] = buffer_alloc(sizeof(struct root_menu
));
802 menu
= menus
[menu_count
];
804 memset(menu
, 0, sizeof(struct root_menu
));
805 strncpy(menu
->id
, data
, MAX_MENU_ID_SIZE
-1);
808 if (get_token_str(menu
->title
, sizeof(menu
->title
)) < 0)
810 logf("%%menu_start title empty");
813 logf("menu: %s", menu
->title
);
818 /* Only set root menu once. */
822 if (get_token_str(data
, sizeof(data
)) < 0)
824 logf("%%root_menu empty");
828 for (i
= 0; i
< menu_count
; i
++)
830 if (!strcasecmp(menus
[i
]->id
, data
))
841 if (menu
->itemcount
>= TAGMENU_MAX_ITEMS
)
843 logf("max itemcount reached");
848 if (menu
->items
[menu
->itemcount
] == NULL
)
850 menu
->items
[menu
->itemcount
] = buffer_alloc(sizeof(struct menu_entry
));
851 memset(menu
->items
[menu
->itemcount
], 0, sizeof(struct menu_entry
));
852 menu
->items
[menu
->itemcount
]->si
= buffer_alloc(sizeof(struct search_instruction
));
855 memset(menu
->items
[menu
->itemcount
]->si
, 0, sizeof(struct search_instruction
));
856 if (!parse_search(menu
->items
[menu
->itemcount
], buf
))
864 static bool parse_menu(const char *filename
)
869 if (menu_count
>= TAGMENU_MAX_MENUS
)
871 logf("max menucount reached");
875 fd
= open(filename
, O_RDONLY
);
878 logf("Search instruction file not found.");
882 /* Now read file for real, parsing into si */
883 fast_readline(fd
, buf
, sizeof buf
, NULL
, parse_line
);
889 void tagtree_init(void)
895 parse_menu(FILE_SEARCH_INSTRUCTIONS
);
897 /* If no root menu is set, assume it's the first single menu
898 * we have. That shouldn't normally happen. */
902 uniqbuf
= buffer_alloc(UNIQBUF_SIZE
);
903 audio_set_track_buffer_event(tagtree_buffer_event
);
904 audio_set_track_unbuffer_event(tagtree_unbuffer_event
);
907 static bool show_search_progress(bool init
, int count
)
909 static int last_tick
= 0;
913 last_tick
= current_tick
;
917 if (current_tick
- last_tick
> HZ
/4)
919 gui_syncsplash(0, str(LANG_PLAYLIST_SEARCH_MSG
), count
,
920 str(LANG_OFF_ABORT
));
921 if (action_userabort(TIMEOUT_NOBLOCK
))
923 last_tick
= current_tick
;
930 static int format_str(struct tagcache_search
*tcs
, struct display_format
*fmt
,
931 char *buf
, int buf_size
)
934 bool read_format
= false;
940 memset(buf
, 0, buf_size
);
941 for (i
= 0; fmt
->formatstr
[i
] != '\0'; i
++)
943 if (fmt
->formatstr
[i
] == '%')
947 if (parpos
>= fmt
->tag_count
)
949 logf("too many format tags");
956 fmtbuf
[fmtbuf_pos
++] = fmt
->formatstr
[i
];
957 if (fmtbuf_pos
>= buf_size
)
959 logf("format parse error");
963 if (fmt
->formatstr
[i
] == 's')
965 fmtbuf
[fmtbuf_pos
] = '\0';
967 if (fmt
->tags
[parpos
] == tcs
->type
)
969 snprintf(&buf
[buf_pos
], buf_size
- buf_pos
, fmtbuf
, tcs
->result
);
973 /* Need to fetch the tag data. */
974 if (!tagcache_retrieve(tcs
, tcs
->idx_id
, fmt
->tags
[parpos
],
975 &buf
[buf_pos
], buf_size
- buf_pos
))
977 logf("retrieve failed");
981 buf_pos
+= strlen(&buf
[buf_pos
]);
984 else if (fmt
->formatstr
[i
] == 'd')
986 fmtbuf
[fmtbuf_pos
] = '\0';
988 snprintf(&buf
[buf_pos
], buf_size
- buf_pos
, fmtbuf
,
989 tagcache_get_numeric(tcs
, fmt
->tags
[parpos
]));
990 buf_pos
+= strlen(&buf
[buf_pos
]);
996 buf
[buf_pos
++] = fmt
->formatstr
[i
];
998 if (buf_pos
- 1 >= buf_size
)
1000 logf("buffer overflow");
1005 buf
[buf_pos
++] = '\0';
1010 static int retrieve_entries(struct tree_context
*c
, struct tagcache_search
*tcs
,
1011 int offset
, bool init
)
1013 struct tagentry
*dptr
= (struct tagentry
*)c
->dircache
;
1014 struct display_format
*fmt
;
1016 int namebufused
= 0;
1017 int total_count
= 0;
1018 int special_entry_count
= 0;
1019 int level
= c
->currextra
;
1026 #ifdef HAVE_TC_RAMCACHE
1027 && !tagcache_is_ramcache()
1031 show_search_progress(true, 0);
1032 gui_syncsplash(0, str(LANG_PLAYLIST_SEARCH_MSG
),
1036 if (c
->currtable
== allsubentries
)
1042 tag
= csi
->tagorder
[level
];
1044 if (!tagcache_search(tcs
, tag
))
1047 /* Prevent duplicate entries in the search list. */
1048 tagcache_search_set_uniqbuf(tcs
, uniqbuf
, UNIQBUF_SIZE
);
1050 if (level
|| csi
->clause_count
[0] || tagcache_is_numeric_tag(tag
))
1053 for (i
= 0; i
< level
; i
++)
1055 if (tagcache_is_numeric_tag(csi
->tagorder
[i
]))
1057 static struct tagcache_search_clause cc
;
1059 memset(&cc
, 0, sizeof(struct tagcache_search_clause
));
1060 cc
.tag
= csi
->tagorder
[i
];
1061 cc
.type
= clause_is
;
1063 cc
.numeric_data
= csi
->result_seek
[i
];
1064 tagcache_search_add_clause(tcs
, &cc
);
1068 tagcache_search_add_filter(tcs
, csi
->tagorder
[i
],
1069 csi
->result_seek
[i
]);
1073 for (i
= 0; i
<= level
; i
++)
1077 for (j
= 0; j
< csi
->clause_count
[i
]; j
++)
1078 tagcache_search_add_clause(tcs
, csi
->clause
[i
][j
]);
1081 current_offset
= offset
;
1082 current_entry_count
= 0;
1086 for (i
= 0; i
< format_count
; i
++)
1088 if (formats
[i
]->group_id
== csi
->format_id
[level
])
1094 sort_inverse
= fmt
->sort_inverse
;
1095 sort_limit
= fmt
->limit
;
1098 /* Check if sorting is forced. */
1104 sort_inverse
= false;
1109 if (tag
!= tag_title
&& tag
!= tag_filename
)
1113 dptr
->newtable
= allsubentries
;
1114 dptr
->name
= str(LANG_TAGNAVI_ALL_TRACKS
);
1116 current_entry_count
++;
1118 special_entry_count
++;
1121 total_count
+= special_entry_count
;
1123 while (tagcache_get_next(tcs
))
1125 if (total_count
++ < offset
)
1128 dptr
->newtable
= navibrowse
;
1129 if (tag
== tag_title
|| tag
== tag_filename
)
1131 dptr
->newtable
= playtrack
;
1132 dptr
->extraseek
= tcs
->idx_id
;
1135 dptr
->extraseek
= tcs
->result_seek
;
1138 /* Check the format */
1139 for (i
= 0; i
< format_count
; i
++)
1141 if (formats
[i
]->group_id
!= csi
->format_id
[level
])
1144 if (tagcache_check_clauses(tcs
, formats
[i
]->clause
,
1145 formats
[i
]->clause_count
))
1152 if (!tcs
->ramresult
|| fmt
)
1158 if (format_str(tcs
, fmt
, buf
, sizeof buf
) < 0)
1160 logf("format_str() failed");
1165 dptr
->name
= &c
->name_buffer
[namebufused
];
1167 namebufused
+= strlen(buf
)+1;
1169 namebufused
+= tcs
->result_len
;
1171 if (namebufused
>= c
->name_buffer_size
)
1173 logf("chunk mode #2: %d", current_entry_count
);
1179 strcpy(dptr
->name
, buf
);
1181 strcpy(dptr
->name
, tcs
->result
);
1184 dptr
->name
= tcs
->result
;
1187 current_entry_count
++;
1189 if (current_entry_count
>= global_settings
.max_files_in_dir
)
1191 logf("chunk mode #3: %d", current_entry_count
);
1197 if (init
&& !tcs
->ramsearch
)
1199 if (!show_search_progress(false, i
))
1201 tagcache_search_finish(tcs
);
1202 return current_entry_count
;
1208 qsort(c
->dircache
+ special_entry_count
* c
->dentry_size
,
1209 current_entry_count
- special_entry_count
,
1210 c
->dentry_size
, compare
);
1214 tagcache_search_finish(tcs
);
1215 return current_entry_count
;
1218 while (tagcache_get_next(tcs
))
1220 if (!tcs
->ramsearch
)
1222 if (!show_search_progress(false, total_count
))
1228 tagcache_search_finish(tcs
);
1230 if (!sort
&& (sort_inverse
|| sort_limit
))
1232 gui_syncsplash(HZ
*4, ID2P(LANG_SHOWDIR_BUFFER_FULL
), total_count
);
1233 logf("Too small dir buffer");
1238 total_count
= MIN(total_count
, sort_limit
);
1243 for (i
= 0; i
< total_count
; i
++, dptr
++)
1245 int len
= strlen(dptr
->name
);
1250 dptr
->name
= &dptr
->name
[strip
];
1257 static int load_root(struct tree_context
*c
)
1259 struct tagentry
*dptr
= (struct tagentry
*)c
->dircache
;
1263 c
->currtable
= root
;
1264 if (c
->dirlevel
== 0)
1265 c
->currextra
= root_menu
;
1267 menu
= menus
[c
->currextra
];
1271 for (i
= 0; i
< menu
->itemcount
; i
++)
1273 dptr
->name
= menu
->items
[i
]->name
;
1274 switch (menu
->items
[i
]->type
)
1277 dptr
->newtable
= navibrowse
;
1278 dptr
->extraseek
= i
;
1282 dptr
->newtable
= root
;
1283 dptr
->extraseek
= menu
->items
[i
]->link
;
1291 current_entry_count
= i
;
1296 int tagtree_load(struct tree_context
* c
)
1299 int table
= c
->currtable
;
1301 c
->dentry_size
= sizeof(struct tagentry
);
1308 c
->currtable
= table
;
1309 c
->currextra
= root_menu
;
1315 count
= load_root(c
);
1320 logf("navibrowse...");
1322 count
= retrieve_entries(c
, &tcs
, 0, true);
1327 logf("Unsupported table %d\n", table
);
1334 count
= load_root(c
);
1335 gui_syncsplash(HZ
, str(LANG_TAGCACHE_BUSY
));
1338 /* The _total_ numer of entries available. */
1339 c
->dirlength
= c
->filesindir
= count
;
1344 int tagtree_enter(struct tree_context
* c
)
1347 struct tagentry
*dptr
;
1351 dptr
= tagtree_get_entry(c
, c
->selected_item
);
1354 newextra
= dptr
->newtable
;
1355 seek
= dptr
->extraseek
;
1357 if (c
->dirlevel
>= MAX_DIR_LEVELS
)
1360 c
->selected_item_history
[c
->dirlevel
]=c
->selected_item
;
1361 c
->table_history
[c
->dirlevel
] = c
->currtable
;
1362 c
->extra_history
[c
->dirlevel
] = c
->currextra
;
1363 c
->pos_history
[c
->dirlevel
] = c
->firstpos
;
1366 switch (c
->currtable
) {
1368 c
->currextra
= newextra
;
1370 if (newextra
== root
)
1373 c
->currextra
= seek
;
1376 else if (newextra
== navibrowse
)
1380 csi
= menu
->items
[seek
]->si
;
1383 strncpy(current_title
[c
->currextra
], dptr
->name
,
1384 sizeof(current_title
[0]) - 1);
1386 /* Read input as necessary. */
1387 for (i
= 0; i
< csi
->tagorder_count
; i
++)
1389 for (j
= 0; j
< csi
->clause_count
[i
]; j
++)
1391 if (!csi
->clause
[i
][j
]->input
)
1394 rc
= kbd_input(searchstring
, sizeof(searchstring
));
1395 if (rc
== -1 || !searchstring
[0])
1401 if (csi
->clause
[i
][j
]->numeric
)
1402 csi
->clause
[i
][j
]->numeric_data
= atoi(searchstring
);
1404 csi
->clause
[i
][j
]->str
= searchstring
;
1408 c
->currtable
= newextra
;
1414 if (newextra
== playtrack
)
1417 /* about to create a new current playlist...
1418 allow user to cancel the operation */
1419 if (global_settings
.warnon_erase_dynplaylist
&&
1420 !global_settings
.party_mode
&&
1421 playlist_modified(NULL
))
1423 char *lines
[]={ID2P(LANG_WARN_ERASEDYNPLAYLIST_PROMPT
)};
1424 struct text_message message
={lines
, 1};
1426 if (gui_syncyesno_run(&message
, NULL
, NULL
) != YESNO_YES
)
1430 if (tagtree_play_folder(c
) >= 0)
1435 c
->currtable
= newextra
;
1436 csi
->result_seek
[c
->currextra
] = seek
;
1437 if (c
->currextra
< csi
->tagorder_count
-1)
1442 /* Update the statusbar title */
1443 strncpy(current_title
[c
->currextra
], dptr
->name
,
1444 sizeof(current_title
[0]) - 1);
1453 gui_synclist_select_item(&tree_lists
, c
->selected_item
);
1458 void tagtree_exit(struct tree_context
* c
)
1461 if (c
->dirlevel
> 0)
1463 c
->selected_item
=c
->selected_item_history
[c
->dirlevel
];
1464 gui_synclist_select_item(&tree_lists
, c
->selected_item
);
1465 c
->currtable
= c
->table_history
[c
->dirlevel
];
1466 c
->currextra
= c
->extra_history
[c
->dirlevel
];
1467 c
->firstpos
= c
->pos_history
[c
->dirlevel
];
1470 int tagtree_get_filename(struct tree_context
* c
, char *buf
, int buflen
)
1472 struct tagentry
*entry
;
1474 entry
= tagtree_get_entry(c
, c
->selected_item
);
1476 if (!tagcache_search(&tcs
, tag_filename
))
1479 if (!tagcache_retrieve(&tcs
, entry
->extraseek
, tcs
.type
, buf
, buflen
))
1481 tagcache_search_finish(&tcs
);
1485 tagcache_search_finish(&tcs
);
1490 static bool insert_all_playlist(struct tree_context
*c
, int position
, bool queue
)
1494 int from
, to
, direction
;
1495 int files_left
= c
->filesindir
;
1498 if (!tagcache_search(&tcs
, tag_filename
))
1500 gui_syncsplash(HZ
, ID2P(LANG_TAGCACHE_BUSY
));
1505 if (position
== PLAYLIST_REPLACE
)
1507 if (remove_all_tracks(NULL
) == 0)
1508 position
= PLAYLIST_INSERT_LAST
;
1511 if (position
== PLAYLIST_INSERT_FIRST
)
1513 from
= c
->filesindir
- 1;
1524 for (i
= from
; i
!= to
; i
+= direction
)
1526 /* Count back to zero */
1527 if (!show_search_progress(false, files_left
--))
1530 if (!tagcache_retrieve(&tcs
, tagtree_get_entry(c
, i
)->extraseek
,
1531 tcs
.type
, buf
, sizeof buf
))
1536 if (playlist_insert_track(NULL
, buf
, position
, queue
, false) < 0)
1538 logf("playlist_insert_track failed");
1543 playlist_sync(NULL
);
1544 tagcache_search_finish(&tcs
);
1550 bool tagtree_insert_selection_playlist(int position
, bool queue
)
1552 struct tagentry
*dptr
;
1554 int dirlevel
= tc
->dirlevel
;
1556 /* We need to set the table to allsubentries. */
1557 show_search_progress(true, 0);
1559 dptr
= tagtree_get_entry(tc
, tc
->selected_item
);
1561 /* Insert a single track? */
1562 if (dptr
->newtable
== playtrack
)
1564 if (tagtree_get_filename(tc
, buf
, sizeof buf
) < 0)
1566 logf("tagtree_get_filename failed");
1569 playlist_insert_track(NULL
, buf
, position
, queue
, true);
1574 if (dptr
->newtable
== navibrowse
)
1578 dptr
= tagtree_get_entry(tc
, tc
->selected_item
);
1580 else if (dptr
->newtable
!= allsubentries
)
1582 logf("unsupported table: %d", dptr
->newtable
);
1586 /* Now the current table should be allsubentries. */
1587 if (dptr
->newtable
!= playtrack
)
1591 dptr
= tagtree_get_entry(tc
, tc
->selected_item
);
1593 /* And now the newtable should be playtrack. */
1594 if (dptr
->newtable
!= playtrack
)
1596 logf("newtable: %d !!", dptr
->newtable
);
1597 tc
->dirlevel
= dirlevel
;
1602 if (tc
->filesindir
<= 0)
1603 gui_syncsplash(HZ
, ID2P(LANG_END_PLAYLIST
));
1606 logf("insert_all_playlist");
1607 if (!insert_all_playlist(tc
, position
, queue
))
1608 gui_syncsplash(HZ
*2, ID2P(LANG_FAILED
));
1611 /* Finally return the dirlevel to its original value. */
1612 while (tc
->dirlevel
> dirlevel
)
1619 static int tagtree_play_folder(struct tree_context
* c
)
1621 if (playlist_create(NULL
, NULL
) < 0)
1623 logf("Failed creating playlist\n");
1627 if (!insert_all_playlist(c
, PLAYLIST_INSERT_LAST
, false))
1630 if (global_settings
.playlist_shuffle
)
1631 c
->selected_item
= playlist_shuffle(current_tick
, c
->selected_item
);
1632 if (!global_settings
.play_selected
)
1633 c
->selected_item
= 0;
1634 gui_synclist_select_item(&tree_lists
, c
->selected_item
);
1636 playlist_start(c
->selected_item
,0);
1641 struct tagentry
* tagtree_get_entry(struct tree_context
*c
, int id
)
1643 struct tagentry
*entry
= (struct tagentry
*)c
->dircache
;
1644 int realid
= id
- current_offset
;
1646 /* Load the next chunk if necessary. */
1647 if (realid
>= current_entry_count
|| realid
< 0)
1650 if (retrieve_entries(c
, &tcs2
, MAX(0, id
- (current_entry_count
/ 2)),
1653 logf("retrieve failed");
1657 realid
= id
- current_offset
;
1661 return &entry
[realid
];
1664 char *tagtree_get_title(struct tree_context
* c
)
1666 switch (c
->currtable
)
1673 return current_title
[c
->currextra
];
1679 int tagtree_get_attr(struct tree_context
* c
)
1682 switch (c
->currtable
)
1685 if (csi
->tagorder
[c
->currextra
] == tag_title
)
1686 attr
= FILE_ATTR_AUDIO
;
1688 attr
= ATTR_DIRECTORY
;
1692 attr
= FILE_ATTR_AUDIO
;
1696 attr
= ATTR_DIRECTORY
;
1703 int tagtree_get_icon(struct tree_context
* c
)
1705 int icon
= Icon_Folder
;
1707 if (tagtree_get_attr(c
) == FILE_ATTR_AUDIO
)