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)
73 static struct tagcache_search tcs
, tcs2
;
74 static bool sort_inverse
;
77 * "%3d. %s" autoscore title %sort = "inverse" %limit = "100"
80 * formatstr = "%-3d. %s"
81 * tags[0] = tag_autoscore
88 struct display_format
{
90 struct tagcache_search_clause
*clause
[TAGCACHE_MAX_CLAUSES
];
103 static struct display_format
*formats
[TAGMENU_MAX_FMTS
];
104 static int format_count
;
106 struct search_instruction
{
108 int tagorder
[MAX_TAGS
];
110 struct tagcache_search_clause
*clause
[MAX_TAGS
][TAGCACHE_MAX_CLAUSES
];
111 int format_id
[MAX_TAGS
];
112 int clause_count
[MAX_TAGS
];
113 int result_seek
[MAX_TAGS
];
119 struct search_instruction
*si
;
127 struct root_menu
*parent
;
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
, "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
);
236 static int get_clause(int *condition
)
241 /* Find the start. */
242 while (*strp
== ' ' && *strp
!= '\0')
248 for (i
= 0; i
< (int)sizeof(buf
)-1; i
++)
250 if (*strp
== '\0' || *strp
== ' ')
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
);
275 static bool read_clause(struct tagcache_search_clause
*clause
)
279 if (get_tag(&clause
->tag
) <= 0)
282 if (get_clause(&clause
->type
) <= 0)
285 if (get_token_str(buf
, sizeof buf
) < 0)
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;
296 clause
->input
= false;
298 if (tagcache_is_numeric_tag(clause
->tag
))
300 clause
->numeric
= true;
301 clause
->numeric_data
= atoi(clause
->str
);
304 clause
->numeric
= false;
309 static bool read_variable(char *buf
, int size
)
313 if (!get_clause(&condition
))
316 if (condition
!= clause_is
)
319 if (get_token_str(buf
, size
) < 0)
325 /* "%3d. %s" autoscore title %sort = "inverse" %limit = "100" */
326 static int get_format_str(struct display_format
*fmt
)
332 memset(fmt
, 0, sizeof(struct display_format
));
334 if (get_token_str(fmt
->name
, sizeof fmt
->name
) < 0)
337 /* Determine the group id */
339 for (i
= 0; i
< format_count
; i
++)
341 if (!strcasecmp(formats
[i
]->name
, fmt
->name
))
343 fmt
->group_id
= formats
[i
]->group_id
;
347 if (formats
[i
]->group_id
> fmt
->group_id
)
348 fmt
->group_id
= formats
[i
]->group_id
;
351 if (i
== format_count
)
354 logf("format: (%d) %s", fmt
->group_id
, fmt
->name
);
356 if (get_token_str(buf
, sizeof buf
) < 0)
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
]);
371 switch (fmt
->tags
[fmt
->tag_count
]) {
373 if (!read_variable(buf
, sizeof buf
))
375 if (!strcasecmp("inverse", buf
))
376 fmt
->sort_inverse
= true;
382 if (!read_variable(buf
, sizeof buf
))
384 fmt
->limit
= atoi(buf
);
388 if (!read_variable(buf
, sizeof buf
))
390 fmt
->strip
= atoi(buf
);
401 static int add_format(const char *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!");
415 while (*strp
!= '\0' && *strp
!= '?')
420 int clause_count
= 0;
425 if (clause_count
>= TAGCACHE_MAX_CLAUSES
)
427 logf("too many clauses");
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
]))
440 formats
[format_count
]->clause_count
= clause_count
;
448 static int get_condition(struct search_instruction
*inst
)
459 if (get_token_str(buf
, sizeof buf
) < 0)
462 for (i
= 0; i
< format_count
; i
++)
464 if (!strcasecmp(formats
[i
]->name
, buf
))
468 if (i
== format_count
)
470 logf("format not found: %s", buf
);
474 inst
->format_id
[inst
->tagorder_count
] = formats
[i
]->group_id
;
487 clause_count
= inst
->clause_count
[inst
->tagorder_count
];
488 if (clause_count
>= TAGCACHE_MAX_CLAUSES
)
490 logf("Too many clauses");
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
]))
500 inst
->clause_count
[inst
->tagorder_count
]++;
506 * "Best" artist ? year >= "2000" & title !^ "crap" & genre = "good genre" \
507 * : album ? year >= "2000" : songs
513 static bool parse_search(struct menu_entry
*entry
, const char *str
)
517 struct search_instruction
*inst
= entry
->si
;
523 /* Parse entry name */
524 if (get_token_str(entry
->name
, sizeof entry
->name
) < 0)
526 logf("No name found.");
530 /* Parse entry type */
531 if (get_tag(&entry
->type
) <= 0)
534 if (entry
->type
== menu_load
)
536 if (get_token_str(buf
, sizeof buf
) < 0)
539 /* Find the matching root menu or "create" it */
540 for (i
= 0; i
< menu_count
; i
++)
542 if (!strcasecmp(menus
[i
]->id
, buf
))
545 menus
[i
]->parent
= menu
;
553 if (entry
->type
!= menu_next
)
556 while (inst
->tagorder_count
< MAX_TAGS
)
558 ret
= get_tag(&inst
->tagorder
[inst
->tagorder_count
]);
561 logf("Parse error #1");
569 logf("tag: %d", inst
->tagorder
[inst
->tagorder_count
]);
571 while ( (ret
= get_condition(inst
)) > 0 ) ;
575 inst
->tagorder_count
++;
577 if (get_tag(&type
) <= 0 || type
!= menu_next
)
584 static int compare(const void *p1
, const void *p2
)
586 struct tagentry
*e1
= (struct tagentry
*)p1
;
587 struct tagentry
*e2
= (struct tagentry
*)p2
;
590 return strncasecmp(e2
->name
, e1
->name
, MAX_PATH
);
592 return strncasecmp(e1
->name
, e2
->name
, MAX_PATH
);
595 static void tagtree_buffer_event(struct mp3entry
*id3
, bool last_track
)
600 /* Do not gather data unless proper setting has been enabled. */
601 if (!global_settings
.runtimedb
)
604 logf("be:%d%s", last_track
, id3
->path
);
606 if (!tagcache_find_index(&tcs
, id3
->path
))
608 logf("tc stat: not found: %s", id3
->path
);
612 id3
->playcount
= tagcache_get_numeric(&tcs
, tag_playcount
);
613 if(!id3
->rating
) id3
->rating
= tagcache_get_numeric(&tcs
, tag_rating
);
614 id3
->lastplayed
= tagcache_get_numeric(&tcs
, tag_lastplayed
);
615 id3
->score
= tagcache_get_numeric(&tcs
, tag_virt_autoscore
) / 10;
617 tagcache_search_finish(&tcs
);
620 static void tagtree_unbuffer_event(struct mp3entry
*id3
, bool last_track
)
628 /* Do not gather data unless proper setting has been enabled. */
629 if (!global_settings
.runtimedb
)
631 logf("runtimedb gathering not enabled");
635 /* Don't process unplayed tracks. */
636 if (id3
->elapsed
== 0)
638 logf("not logging unplayed track");
642 if (!tagcache_find_index(&tcs
, id3
->path
))
644 logf("tc stat: not found: %s", id3
->path
);
648 playcount
= tagcache_get_numeric(&tcs
, tag_playcount
);
649 playtime
= tagcache_get_numeric(&tcs
, tag_playtime
);
650 lastplayed
= tagcache_get_numeric(&tcs
, tag_lastplayed
);
654 rating
= (long) id3
->rating
;
656 lastplayed
= tagcache_increase_serial();
659 logf("incorrect tc serial:%ld", lastplayed
);
660 tagcache_search_finish(&tcs
);
664 /* Ignore the last 15s (crossfade etc.) */
665 playtime
+= MIN(id3
->length
, id3
->elapsed
+ 15 * 1000);
667 logf("ube:%s", id3
->path
);
668 logf("-> %d/%ld/%ld/%ld", last_track
, playcount
, rating
, playtime
);
669 logf("-> %ld/%ld/%ld", id3
->elapsed
, id3
->length
, MIN(id3
->length
, id3
->elapsed
+ 15 * 1000));
671 /* lastplayed not yet supported. */
673 if (!tagcache_modify_numeric_entry(&tcs
, tag_playcount
, playcount
)
674 || !tagcache_modify_numeric_entry(&tcs
, tag_rating
, rating
)
675 || !tagcache_modify_numeric_entry(&tcs
, tag_playtime
, playtime
)
676 || !tagcache_modify_numeric_entry(&tcs
, tag_lastplayed
, lastplayed
))
678 logf("tc stat: modify failed!");
679 tagcache_search_finish(&tcs
);
683 tagcache_search_finish(&tcs
);
686 bool tagtree_export(void)
688 gui_syncsplash(0, str(LANG_CREATING
));
689 if (!tagcache_create_changelog(&tcs
))
691 gui_syncsplash(HZ
*2, str(LANG_FAILED
));
697 bool tagtree_import(void)
699 gui_syncsplash(0, str(LANG_WAIT
));
700 if (!tagcache_import_changelog())
702 gui_syncsplash(HZ
*2, str(LANG_FAILED
));
708 static bool parse_menu(const char *filename
);
710 static int parse_line(int n
, const char *buf
, void *parameters
)
714 static bool read_menu
;
719 logf("parse:%d/%s", n
, buf
);
721 /* First line, do initialisation. */
724 if (strcasecmp(TAGNAVI_VERSION
, buf
))
726 logf("Version mismatch");
750 if (get_tag(&variable
) <= 0)
756 if (add_format(strp
) < 0)
758 logf("Format add fail: %s", data
);
763 if (get_token_str(data
, sizeof(data
)) < 0)
765 logf("%%include empty");
769 if (!parse_menu(data
))
771 logf("Load menu fail: %s", data
);
776 if (menu_count
>= TAGMENU_MAX_MENUS
)
778 logf("max menucount reached");
782 menus
[menu_count
] = buffer_alloc(sizeof(struct root_menu
));
783 menu
= menus
[menu_count
];
784 memset(menu
, 0, sizeof(struct root_menu
));
785 if (get_token_str(menu
->id
, sizeof(menu
->id
)) < 0)
787 logf("%%menu_start id empty");
790 if (get_token_str(menu
->title
, sizeof(menu
->title
)) < 0)
792 logf("%%menu_start title empty");
795 logf("menu: %s", menu
->title
);
801 /* Only set root menu once. */
805 if (get_token_str(data
, sizeof(data
)) < 0)
807 logf("%%root_menu empty");
811 for (i
= 0; i
< menu_count
; i
++)
813 if (!strcasecmp(menus
[i
]->id
, data
))
824 if (menu
->itemcount
>= TAGMENU_MAX_ITEMS
)
826 logf("max itemcount reached");
831 if (menu
->items
[menu
->itemcount
] == NULL
)
833 menu
->items
[menu
->itemcount
] = buffer_alloc(sizeof(struct menu_entry
));
834 memset(menu
->items
[menu
->itemcount
], 0, sizeof(struct menu_entry
));
835 menu
->items
[menu
->itemcount
]->si
= buffer_alloc(sizeof(struct search_instruction
));
838 memset(menu
->items
[menu
->itemcount
]->si
, 0, sizeof(struct search_instruction
));
839 if (!parse_search(menu
->items
[menu
->itemcount
], buf
))
847 static bool parse_menu(const char *filename
)
852 if (menu_count
>= TAGMENU_MAX_MENUS
)
854 logf("max menucount reached");
858 fd
= open(filename
, O_RDONLY
);
861 logf("Search instruction file not found.");
865 /* Now read file for real, parsing into si */
866 fast_readline(fd
, buf
, sizeof buf
, NULL
, parse_line
);
872 void tagtree_init(void)
878 parse_menu(FILE_SEARCH_INSTRUCTIONS
);
880 /* If no root menu is set, assume it's the first single menu
881 * we have. That shouldn't normally happen. */
885 uniqbuf
= buffer_alloc(UNIQBUF_SIZE
);
886 audio_set_track_buffer_event(tagtree_buffer_event
);
887 audio_set_track_unbuffer_event(tagtree_unbuffer_event
);
890 static bool show_search_progress(bool init
, int count
)
892 static int last_tick
= 0;
896 last_tick
= current_tick
;
900 if (current_tick
- last_tick
> HZ
/4)
902 gui_syncsplash(0, str(LANG_PLAYLIST_SEARCH_MSG
), count
,
903 #if CONFIG_KEYPAD == PLAYER_PAD
909 if (action_userabort(TIMEOUT_NOBLOCK
))
911 last_tick
= current_tick
;
918 static int format_str(struct tagcache_search
*tcs
, struct display_format
*fmt
,
919 char *buf
, int buf_size
)
922 bool read_format
= false;
928 memset(buf
, 0, buf_size
);
929 for (i
= 0; fmt
->formatstr
[i
] != '\0'; i
++)
931 if (fmt
->formatstr
[i
] == '%')
935 if (parpos
>= fmt
->tag_count
)
937 logf("too many format tags");
944 fmtbuf
[fmtbuf_pos
++] = fmt
->formatstr
[i
];
945 if (fmtbuf_pos
>= buf_size
)
947 logf("format parse error");
951 if (fmt
->formatstr
[i
] == 's')
953 fmtbuf
[fmtbuf_pos
] = '\0';
955 if (fmt
->tags
[parpos
] == tcs
->type
)
957 snprintf(&buf
[buf_pos
], buf_size
- buf_pos
, fmtbuf
, tcs
->result
);
961 /* Need to fetch the tag data. */
962 if (!tagcache_retrieve(tcs
, tcs
->idx_id
, fmt
->tags
[parpos
],
963 &buf
[buf_pos
], buf_size
- buf_pos
))
965 logf("retrieve failed");
969 buf_pos
+= strlen(&buf
[buf_pos
]);
972 else if (fmt
->formatstr
[i
] == 'd')
974 fmtbuf
[fmtbuf_pos
] = '\0';
976 snprintf(&buf
[buf_pos
], buf_size
- buf_pos
, fmtbuf
,
977 tagcache_get_numeric(tcs
, fmt
->tags
[parpos
]));
978 buf_pos
+= strlen(&buf
[buf_pos
]);
984 buf
[buf_pos
++] = fmt
->formatstr
[i
];
986 if (buf_pos
- 1 >= buf_size
)
988 logf("buffer overflow");
993 buf
[buf_pos
++] = '\0';
998 static int retrieve_entries(struct tree_context
*c
, struct tagcache_search
*tcs
,
999 int offset
, bool init
)
1001 struct tagentry
*dptr
= (struct tagentry
*)c
->dircache
;
1002 struct display_format
*fmt
;
1004 int namebufused
= 0;
1005 int total_count
= 0;
1006 int special_entry_count
= 0;
1007 int level
= c
->currextra
;
1014 #ifdef HAVE_TC_RAMCACHE
1015 && !tagcache_is_ramcache()
1019 show_search_progress(true, 0);
1020 gui_syncsplash(0, str(LANG_PLAYLIST_SEARCH_MSG
),
1024 if (c
->currtable
== allsubentries
)
1030 tag
= csi
->tagorder
[level
];
1032 if (!tagcache_search(tcs
, tag
))
1035 /* Prevent duplicate entries in the search list. */
1036 tagcache_search_set_uniqbuf(tcs
, uniqbuf
, UNIQBUF_SIZE
);
1038 if (level
|| csi
->clause_count
[0] || tagcache_is_numeric_tag(tag
))
1041 for (i
= 0; i
< level
; i
++)
1043 if (tagcache_is_numeric_tag(csi
->tagorder
[i
]))
1045 static struct tagcache_search_clause cc
;
1047 memset(&cc
, 0, sizeof(struct tagcache_search_clause
));
1048 cc
.tag
= csi
->tagorder
[i
];
1049 cc
.type
= clause_is
;
1051 cc
.numeric_data
= csi
->result_seek
[i
];
1052 tagcache_search_add_clause(tcs
, &cc
);
1056 tagcache_search_add_filter(tcs
, csi
->tagorder
[i
],
1057 csi
->result_seek
[i
]);
1061 for (i
= 0; i
<= level
; i
++)
1065 for (j
= 0; j
< csi
->clause_count
[i
]; j
++)
1066 tagcache_search_add_clause(tcs
, csi
->clause
[i
][j
]);
1069 current_offset
= offset
;
1070 current_entry_count
= 0;
1074 for (i
= 0; i
< format_count
; i
++)
1076 if (formats
[i
]->group_id
== csi
->format_id
[level
])
1082 sort_inverse
= fmt
->sort_inverse
;
1083 sort_limit
= fmt
->limit
;
1086 /* Check if sorting is forced. */
1092 sort_inverse
= false;
1097 if (tag
!= tag_title
&& tag
!= tag_filename
)
1101 dptr
->newtable
= allsubentries
;
1102 dptr
->name
= str(LANG_TAGNAVI_ALL_TRACKS
);
1104 current_entry_count
++;
1106 special_entry_count
++;
1109 total_count
+= special_entry_count
;
1111 while (tagcache_get_next(tcs
))
1113 if (total_count
++ < offset
)
1116 dptr
->newtable
= navibrowse
;
1117 if (tag
== tag_title
|| tag
== tag_filename
)
1119 dptr
->newtable
= playtrack
;
1120 dptr
->extraseek
= tcs
->idx_id
;
1123 dptr
->extraseek
= tcs
->result_seek
;
1126 /* Check the format */
1127 for (i
= 0; i
< format_count
; i
++)
1129 if (formats
[i
]->group_id
!= csi
->format_id
[level
])
1132 if (tagcache_check_clauses(tcs
, formats
[i
]->clause
,
1133 formats
[i
]->clause_count
))
1140 if (!tcs
->ramresult
|| fmt
)
1146 if (format_str(tcs
, fmt
, buf
, sizeof buf
) < 0)
1148 logf("format_str() failed");
1153 dptr
->name
= &c
->name_buffer
[namebufused
];
1155 namebufused
+= strlen(buf
)+1;
1157 namebufused
+= tcs
->result_len
;
1159 if (namebufused
>= c
->name_buffer_size
)
1161 logf("chunk mode #2: %d", current_entry_count
);
1167 strcpy(dptr
->name
, buf
);
1169 strcpy(dptr
->name
, tcs
->result
);
1172 dptr
->name
= tcs
->result
;
1175 current_entry_count
++;
1177 if (current_entry_count
>= global_settings
.max_files_in_dir
)
1179 logf("chunk mode #3: %d", current_entry_count
);
1185 if (init
&& !tcs
->ramsearch
)
1187 if (!show_search_progress(false, i
))
1189 tagcache_search_finish(tcs
);
1190 return current_entry_count
;
1196 qsort(c
->dircache
+ special_entry_count
* c
->dentry_size
,
1197 current_entry_count
- special_entry_count
,
1198 c
->dentry_size
, compare
);
1202 tagcache_search_finish(tcs
);
1203 return current_entry_count
;
1206 while (tagcache_get_next(tcs
))
1208 if (!tcs
->ramsearch
)
1210 if (!show_search_progress(false, total_count
))
1216 tagcache_search_finish(tcs
);
1218 if (!sort
&& (sort_inverse
|| sort_limit
))
1220 gui_syncsplash(HZ
*4, str(LANG_SHOWDIR_BUFFER_FULL
), total_count
);
1221 logf("Too small dir buffer");
1226 total_count
= MIN(total_count
, sort_limit
);
1231 for (i
= 0; i
< total_count
; i
++, dptr
++)
1233 int len
= strlen(dptr
->name
);
1238 dptr
->name
= &dptr
->name
[strip
];
1245 static int load_root(struct tree_context
*c
)
1247 struct tagentry
*dptr
= (struct tagentry
*)c
->dircache
;
1251 c
->currtable
= root
;
1252 if (c
->dirlevel
== 0)
1253 c
->currextra
= root_menu
;
1255 menu
= menus
[c
->currextra
];
1259 for (i
= 0; i
< menu
->itemcount
; i
++)
1261 dptr
->name
= menu
->items
[i
]->name
;
1262 switch (menu
->items
[i
]->type
)
1265 dptr
->newtable
= navibrowse
;
1266 dptr
->extraseek
= i
;
1270 dptr
->newtable
= root
;
1271 dptr
->extraseek
= menu
->items
[i
]->link
;
1279 current_entry_count
= i
;
1284 int tagtree_load(struct tree_context
* c
)
1287 int table
= c
->currtable
;
1289 c
->dentry_size
= sizeof(struct tagentry
);
1296 c
->currtable
= table
;
1297 c
->currextra
= root_menu
;
1303 count
= load_root(c
);
1308 logf("navibrowse...");
1310 count
= retrieve_entries(c
, &tcs
, 0, true);
1315 logf("Unsupported table %d\n", table
);
1322 count
= load_root(c
);
1323 gui_syncsplash(HZ
, str(LANG_TAGCACHE_BUSY
));
1326 /* The _total_ numer of entries available. */
1327 c
->dirlength
= c
->filesindir
= count
;
1332 int tagtree_enter(struct tree_context
* c
)
1335 struct tagentry
*dptr
;
1339 dptr
= tagtree_get_entry(c
, c
->selected_item
);
1342 newextra
= dptr
->newtable
;
1343 seek
= dptr
->extraseek
;
1345 if (c
->dirlevel
>= MAX_DIR_LEVELS
)
1348 c
->selected_item_history
[c
->dirlevel
]=c
->selected_item
;
1349 c
->table_history
[c
->dirlevel
] = c
->currtable
;
1350 c
->extra_history
[c
->dirlevel
] = c
->currextra
;
1351 c
->pos_history
[c
->dirlevel
] = c
->firstpos
;
1354 switch (c
->currtable
) {
1356 c
->currextra
= newextra
;
1358 if (newextra
== root
)
1361 c
->currextra
= seek
;
1364 else if (newextra
== navibrowse
)
1368 csi
= menu
->items
[seek
]->si
;
1371 strncpy(current_title
[c
->currextra
], dptr
->name
,
1372 sizeof(current_title
[0]) - 1);
1374 /* Read input as necessary. */
1375 for (i
= 0; i
< csi
->tagorder_count
; i
++)
1377 for (j
= 0; j
< csi
->clause_count
[i
]; j
++)
1379 if (!csi
->clause
[i
][j
]->input
)
1382 rc
= kbd_input(searchstring
, sizeof(searchstring
));
1383 if (rc
== -1 || !searchstring
[0])
1389 if (csi
->clause
[i
][j
]->numeric
)
1390 csi
->clause
[i
][j
]->numeric_data
= atoi(searchstring
);
1392 csi
->clause
[i
][j
]->str
= searchstring
;
1396 c
->currtable
= newextra
;
1402 if (newextra
== playtrack
)
1405 /* about to create a new current playlist...
1406 allow user to cancel the operation */
1407 if (global_settings
.warnon_erase_dynplaylist
&&
1408 !global_settings
.party_mode
&&
1409 playlist_modified(NULL
))
1411 char *lines
[]={str(LANG_WARN_ERASEDYNPLAYLIST_PROMPT
)};
1412 struct text_message message
={lines
, 1};
1414 if (gui_syncyesno_run(&message
, NULL
, NULL
) != YESNO_YES
)
1418 if (tagtree_play_folder(c
) >= 0)
1423 c
->currtable
= newextra
;
1424 csi
->result_seek
[c
->currextra
] = seek
;
1425 if (c
->currextra
< csi
->tagorder_count
-1)
1430 /* Update the statusbar title */
1431 strncpy(current_title
[c
->currextra
], dptr
->name
,
1432 sizeof(current_title
[0]) - 1);
1441 gui_synclist_select_item(&tree_lists
, c
->selected_item
);
1446 void tagtree_exit(struct tree_context
* c
)
1449 if (c
->dirlevel
> 0)
1451 c
->selected_item
=c
->selected_item_history
[c
->dirlevel
];
1452 gui_synclist_select_item(&tree_lists
, c
->selected_item
);
1453 c
->currtable
= c
->table_history
[c
->dirlevel
];
1454 c
->currextra
= c
->extra_history
[c
->dirlevel
];
1455 c
->firstpos
= c
->pos_history
[c
->dirlevel
];
1458 int tagtree_get_filename(struct tree_context
* c
, char *buf
, int buflen
)
1460 struct tagentry
*entry
;
1462 entry
= tagtree_get_entry(c
, c
->selected_item
);
1464 if (!tagcache_search(&tcs
, tag_filename
))
1467 if (!tagcache_retrieve(&tcs
, entry
->extraseek
, tcs
.type
, buf
, buflen
))
1469 tagcache_search_finish(&tcs
);
1473 tagcache_search_finish(&tcs
);
1478 static bool insert_all_playlist(struct tree_context
*c
, int position
, bool queue
)
1482 int from
, to
, direction
;
1483 int files_left
= c
->filesindir
;
1486 if (!tagcache_search(&tcs
, tag_filename
))
1488 gui_syncsplash(HZ
, str(LANG_TAGCACHE_BUSY
));
1493 if (position
== PLAYLIST_REPLACE
)
1495 if (remove_all_tracks(NULL
) == 0)
1496 position
= PLAYLIST_INSERT_LAST
;
1499 if (position
== PLAYLIST_INSERT_FIRST
)
1501 from
= c
->filesindir
- 1;
1512 for (i
= from
; i
!= to
; i
+= direction
)
1514 /* Count back to zero */
1515 if (!show_search_progress(false, files_left
--))
1518 if (!tagcache_retrieve(&tcs
, tagtree_get_entry(c
, i
)->extraseek
,
1519 tcs
.type
, buf
, sizeof buf
))
1524 if (playlist_insert_track(NULL
, buf
, position
, queue
, false) < 0)
1526 logf("playlist_insert_track failed");
1531 playlist_sync(NULL
);
1532 tagcache_search_finish(&tcs
);
1538 bool tagtree_insert_selection_playlist(int position
, bool queue
)
1540 struct tagentry
*dptr
;
1542 int dirlevel
= tc
->dirlevel
;
1544 /* We need to set the table to allsubentries. */
1545 show_search_progress(true, 0);
1547 dptr
= tagtree_get_entry(tc
, tc
->selected_item
);
1549 /* Insert a single track? */
1550 if (dptr
->newtable
== playtrack
)
1552 if (tagtree_get_filename(tc
, buf
, sizeof buf
) < 0)
1554 logf("tagtree_get_filename failed");
1557 playlist_insert_track(NULL
, buf
, position
, queue
, true);
1562 if (dptr
->newtable
== navibrowse
)
1566 dptr
= tagtree_get_entry(tc
, tc
->selected_item
);
1568 else if (dptr
->newtable
!= allsubentries
)
1570 logf("unsupported table: %d", dptr
->newtable
);
1574 /* Now the current table should be allsubentries. */
1575 if (dptr
->newtable
!= playtrack
)
1579 dptr
= tagtree_get_entry(tc
, tc
->selected_item
);
1581 /* And now the newtable should be playtrack. */
1582 if (dptr
->newtable
!= playtrack
)
1584 logf("newtable: %d !!", dptr
->newtable
);
1585 tc
->dirlevel
= dirlevel
;
1590 if (tc
->filesindir
<= 0)
1591 gui_syncsplash(HZ
, str(LANG_END_PLAYLIST_PLAYER
));
1594 logf("insert_all_playlist");
1595 if (!insert_all_playlist(tc
, position
, queue
))
1596 gui_syncsplash(HZ
*2, str(LANG_FAILED
));
1599 /* Finally return the dirlevel to its original value. */
1600 while (tc
->dirlevel
> dirlevel
)
1607 static int tagtree_play_folder(struct tree_context
* c
)
1609 if (playlist_create(NULL
, NULL
) < 0)
1611 logf("Failed creating playlist\n");
1615 if (!insert_all_playlist(c
, PLAYLIST_INSERT_LAST
, false))
1618 if (global_settings
.playlist_shuffle
)
1619 c
->selected_item
= playlist_shuffle(current_tick
, c
->selected_item
);
1620 if (!global_settings
.play_selected
)
1621 c
->selected_item
= 0;
1622 gui_synclist_select_item(&tree_lists
, c
->selected_item
);
1624 playlist_start(c
->selected_item
,0);
1629 struct tagentry
* tagtree_get_entry(struct tree_context
*c
, int id
)
1631 struct tagentry
*entry
= (struct tagentry
*)c
->dircache
;
1632 int realid
= id
- current_offset
;
1634 /* Load the next chunk if necessary. */
1635 if (realid
>= current_entry_count
|| realid
< 0)
1638 if (retrieve_entries(c
, &tcs2
, MAX(0, id
- (current_entry_count
/ 2)),
1641 logf("retrieve failed");
1645 realid
= id
- current_offset
;
1649 return &entry
[realid
];
1652 char *tagtree_get_title(struct tree_context
* c
)
1654 switch (c
->currtable
)
1661 return current_title
[c
->currextra
];
1667 int tagtree_get_attr(struct tree_context
* c
)
1670 switch (c
->currtable
)
1673 if (csi
->tagorder
[c
->currextra
] == tag_title
)
1674 attr
= FILE_ATTR_AUDIO
;
1676 attr
= ATTR_DIRECTORY
;
1680 attr
= FILE_ATTR_AUDIO
;
1684 attr
= ATTR_DIRECTORY
;
1691 int tagtree_get_icon(struct tree_context
* c
)
1693 int icon
= Icon_Folder
;
1695 if (tagtree_get_attr(c
) == FILE_ATTR_AUDIO
)