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
, "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
);
237 static int get_clause(int *condition
)
242 /* Find the start. */
243 while (*strp
== ' ' && *strp
!= '\0')
249 for (i
= 0; i
< (int)sizeof(buf
)-1; i
++)
251 if (*strp
== '\0' || *strp
== ' ')
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
);
276 static bool read_clause(struct tagcache_search_clause
*clause
)
280 if (get_tag(&clause
->tag
) <= 0)
283 if (get_clause(&clause
->type
) <= 0)
286 if (get_token_str(buf
, sizeof buf
) < 0)
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;
297 clause
->input
= false;
299 if (tagcache_is_numeric_tag(clause
->tag
))
301 clause
->numeric
= true;
302 clause
->numeric_data
= atoi(clause
->str
);
305 clause
->numeric
= false;
310 static bool read_variable(char *buf
, int size
)
314 if (!get_clause(&condition
))
317 if (condition
!= clause_is
)
320 if (get_token_str(buf
, size
) < 0)
326 /* "%3d. %s" autoscore title %sort = "inverse" %limit = "100" */
327 static int get_format_str(struct display_format
*fmt
)
333 memset(fmt
, 0, sizeof(struct display_format
));
335 if (get_token_str(fmt
->name
, sizeof fmt
->name
) < 0)
338 /* Determine the group id */
340 for (i
= 0; i
< format_count
; i
++)
342 if (!strcasecmp(formats
[i
]->name
, fmt
->name
))
344 fmt
->group_id
= formats
[i
]->group_id
;
348 if (formats
[i
]->group_id
> fmt
->group_id
)
349 fmt
->group_id
= formats
[i
]->group_id
;
352 if (i
== format_count
)
355 logf("format: (%d) %s", fmt
->group_id
, fmt
->name
);
357 if (get_token_str(buf
, sizeof buf
) < 0)
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
]);
372 switch (fmt
->tags
[fmt
->tag_count
]) {
374 if (!read_variable(buf
, sizeof buf
))
376 if (!strcasecmp("inverse", buf
))
377 fmt
->sort_inverse
= true;
383 if (!read_variable(buf
, sizeof buf
))
385 fmt
->limit
= atoi(buf
);
389 if (!read_variable(buf
, sizeof buf
))
391 fmt
->strip
= atoi(buf
);
402 static int add_format(const char *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!");
416 while (*strp
!= '\0' && *strp
!= '?')
421 int clause_count
= 0;
426 if (clause_count
>= TAGCACHE_MAX_CLAUSES
)
428 logf("too many clauses");
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
]))
441 formats
[format_count
]->clause_count
= clause_count
;
449 static int get_condition(struct search_instruction
*inst
)
460 if (get_token_str(buf
, sizeof buf
) < 0)
463 for (i
= 0; i
< format_count
; i
++)
465 if (!strcasecmp(formats
[i
]->name
, buf
))
469 if (i
== format_count
)
471 logf("format not found: %s", buf
);
475 inst
->format_id
[inst
->tagorder_count
] = formats
[i
]->group_id
;
488 clause_count
= inst
->clause_count
[inst
->tagorder_count
];
489 if (clause_count
>= TAGCACHE_MAX_CLAUSES
)
491 logf("Too many clauses");
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
]))
501 inst
->clause_count
[inst
->tagorder_count
]++;
507 * "Best" artist ? year >= "2000" & title !^ "crap" & genre = "good genre" \
508 * : album ? year >= "2000" : songs
514 static bool parse_search(struct menu_entry
*entry
, const char *str
)
518 struct search_instruction
*inst
= entry
->si
;
521 struct root_menu
*new_menu
;
525 /* Parse entry name */
526 if (get_token_str(entry
->name
, sizeof entry
->name
) < 0)
528 logf("No name found.");
532 /* Parse entry type */
533 if (get_tag(&entry
->type
) <= 0)
536 if (entry
->type
== menu_load
)
538 if (get_token_str(buf
, sizeof buf
) < 0)
541 /* Find the matching root menu or "create" it */
542 for (i
= 0; i
< menu_count
; i
++)
544 if (!strcasecmp(menus
[i
]->id
, buf
))
551 if (menu_count
>= TAGMENU_MAX_MENUS
)
553 logf("max menucount reached");
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
;
568 if (entry
->type
!= menu_next
)
571 while (inst
->tagorder_count
< MAX_TAGS
)
573 ret
= get_tag(&inst
->tagorder
[inst
->tagorder_count
]);
576 logf("Parse error #1");
584 logf("tag: %d", inst
->tagorder
[inst
->tagorder_count
]);
586 while ( (ret
= get_condition(inst
)) > 0 ) ;
590 inst
->tagorder_count
++;
592 if (get_tag(&type
) <= 0 || type
!= menu_next
)
599 static int compare(const void *p1
, const void *p2
)
601 struct tagentry
*e1
= (struct tagentry
*)p1
;
602 struct tagentry
*e2
= (struct tagentry
*)p2
;
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
)
615 /* Do not gather data unless proper setting has been enabled. */
616 if (!global_settings
.runtimedb
)
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
);
627 id3
->playcount
= tagcache_get_numeric(&tcs
, tag_playcount
);
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
)
647 /* Do not gather data unless proper setting has been enabled. */
648 if (!global_settings
.runtimedb
)
650 logf("runtimedb gathering not enabled");
654 if (!id3
->tagcache_idx
)
656 logf("No tagcache index pointer found");
660 /* Don't process unplayed tracks. */
661 if (id3
->elapsed
== 0)
663 logf("not logging unplayed track");
667 playcount
= id3
->playcount
+ 1;
668 lastplayed
= tagcache_increase_serial();
671 logf("incorrect tc serial:%ld", lastplayed
);
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
));
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
));
710 static bool parse_menu(const char *filename
);
712 static int parse_line(int n
, const char *buf
, void *parameters
)
716 static bool read_menu
;
721 logf("parse:%d/%s", n
, buf
);
723 /* First line, do initialisation. */
726 if (strcasecmp(TAGNAVI_VERSION
, buf
))
728 logf("Version mismatch");
751 if (get_tag(&variable
) <= 0)
757 if (add_format(strp
) < 0)
759 logf("Format add fail: %s", data
);
764 if (get_token_str(data
, sizeof(data
)) < 0)
766 logf("%%include empty");
770 if (!parse_menu(data
))
772 logf("Load menu fail: %s", data
);
777 if (menu_count
>= TAGMENU_MAX_MENUS
)
779 logf("max menucount reached");
783 if (get_token_str(data
, sizeof data
) < 0)
785 logf("%%menu_start id empty");
790 for (i
= 0; i
< menu_count
; i
++)
792 if (!strcasecmp(menus
[i
]->id
, data
))
800 menus
[menu_count
] = buffer_alloc(sizeof(struct root_menu
));
801 menu
= menus
[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");
812 logf("menu: %s", menu
->title
);
817 /* Only set root menu once. */
821 if (get_token_str(data
, sizeof(data
)) < 0)
823 logf("%%root_menu empty");
827 for (i
= 0; i
< menu_count
; i
++)
829 if (!strcasecmp(menus
[i
]->id
, data
))
840 if (menu
->itemcount
>= TAGMENU_MAX_ITEMS
)
842 logf("max itemcount reached");
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
))
863 static bool parse_menu(const char *filename
)
868 if (menu_count
>= TAGMENU_MAX_MENUS
)
870 logf("max menucount reached");
874 fd
= open(filename
, O_RDONLY
);
877 logf("Search instruction file not found.");
881 /* Now read file for real, parsing into si */
882 fast_readline(fd
, buf
, sizeof buf
, NULL
, parse_line
);
888 void tagtree_init(void)
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. */
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;
912 last_tick
= current_tick
;
916 if (current_tick
- last_tick
> HZ
/4)
918 gui_syncsplash(0, str(LANG_PLAYLIST_SEARCH_MSG
), count
,
919 #if CONFIG_KEYPAD == PLAYER_PAD
925 if (action_userabort(TIMEOUT_NOBLOCK
))
927 last_tick
= current_tick
;
934 static int format_str(struct tagcache_search
*tcs
, struct display_format
*fmt
,
935 char *buf
, int buf_size
)
938 bool read_format
= false;
944 memset(buf
, 0, buf_size
);
945 for (i
= 0; fmt
->formatstr
[i
] != '\0'; i
++)
947 if (fmt
->formatstr
[i
] == '%')
951 if (parpos
>= fmt
->tag_count
)
953 logf("too many format tags");
960 fmtbuf
[fmtbuf_pos
++] = fmt
->formatstr
[i
];
961 if (fmtbuf_pos
>= buf_size
)
963 logf("format parse error");
967 if (fmt
->formatstr
[i
] == 's')
969 fmtbuf
[fmtbuf_pos
] = '\0';
971 if (fmt
->tags
[parpos
] == tcs
->type
)
973 snprintf(&buf
[buf_pos
], buf_size
- buf_pos
, fmtbuf
, tcs
->result
);
977 /* Need to fetch the tag data. */
978 if (!tagcache_retrieve(tcs
, tcs
->idx_id
, fmt
->tags
[parpos
],
979 &buf
[buf_pos
], buf_size
- buf_pos
))
981 logf("retrieve failed");
985 buf_pos
+= strlen(&buf
[buf_pos
]);
988 else if (fmt
->formatstr
[i
] == 'd')
990 fmtbuf
[fmtbuf_pos
] = '\0';
992 snprintf(&buf
[buf_pos
], buf_size
- buf_pos
, fmtbuf
,
993 tagcache_get_numeric(tcs
, fmt
->tags
[parpos
]));
994 buf_pos
+= strlen(&buf
[buf_pos
]);
1000 buf
[buf_pos
++] = fmt
->formatstr
[i
];
1002 if (buf_pos
- 1 >= buf_size
)
1004 logf("buffer overflow");
1009 buf
[buf_pos
++] = '\0';
1014 static int retrieve_entries(struct tree_context
*c
, struct tagcache_search
*tcs
,
1015 int offset
, bool init
)
1017 struct tagentry
*dptr
= (struct tagentry
*)c
->dircache
;
1018 struct display_format
*fmt
;
1020 int namebufused
= 0;
1021 int total_count
= 0;
1022 int special_entry_count
= 0;
1023 int level
= c
->currextra
;
1030 #ifdef HAVE_TC_RAMCACHE
1031 && !tagcache_is_ramcache()
1035 show_search_progress(true, 0);
1036 gui_syncsplash(0, str(LANG_PLAYLIST_SEARCH_MSG
),
1040 if (c
->currtable
== allsubentries
)
1046 tag
= csi
->tagorder
[level
];
1048 if (!tagcache_search(tcs
, tag
))
1051 /* Prevent duplicate entries in the search list. */
1052 tagcache_search_set_uniqbuf(tcs
, uniqbuf
, UNIQBUF_SIZE
);
1054 if (level
|| csi
->clause_count
[0] || tagcache_is_numeric_tag(tag
))
1057 for (i
= 0; i
< level
; i
++)
1059 if (tagcache_is_numeric_tag(csi
->tagorder
[i
]))
1061 static struct tagcache_search_clause cc
;
1063 memset(&cc
, 0, sizeof(struct tagcache_search_clause
));
1064 cc
.tag
= csi
->tagorder
[i
];
1065 cc
.type
= clause_is
;
1067 cc
.numeric_data
= csi
->result_seek
[i
];
1068 tagcache_search_add_clause(tcs
, &cc
);
1072 tagcache_search_add_filter(tcs
, csi
->tagorder
[i
],
1073 csi
->result_seek
[i
]);
1077 for (i
= 0; i
<= level
; i
++)
1081 for (j
= 0; j
< csi
->clause_count
[i
]; j
++)
1082 tagcache_search_add_clause(tcs
, csi
->clause
[i
][j
]);
1085 current_offset
= offset
;
1086 current_entry_count
= 0;
1090 for (i
= 0; i
< format_count
; i
++)
1092 if (formats
[i
]->group_id
== csi
->format_id
[level
])
1098 sort_inverse
= fmt
->sort_inverse
;
1099 sort_limit
= fmt
->limit
;
1102 /* Check if sorting is forced. */
1108 sort_inverse
= false;
1113 if (tag
!= tag_title
&& tag
!= tag_filename
)
1117 dptr
->newtable
= allsubentries
;
1118 dptr
->name
= str(LANG_TAGNAVI_ALL_TRACKS
);
1120 current_entry_count
++;
1122 special_entry_count
++;
1125 total_count
+= special_entry_count
;
1127 while (tagcache_get_next(tcs
))
1129 if (total_count
++ < offset
)
1132 dptr
->newtable
= navibrowse
;
1133 if (tag
== tag_title
|| tag
== tag_filename
)
1135 dptr
->newtable
= playtrack
;
1136 dptr
->extraseek
= tcs
->idx_id
;
1139 dptr
->extraseek
= tcs
->result_seek
;
1142 /* Check the format */
1143 for (i
= 0; i
< format_count
; i
++)
1145 if (formats
[i
]->group_id
!= csi
->format_id
[level
])
1148 if (tagcache_check_clauses(tcs
, formats
[i
]->clause
,
1149 formats
[i
]->clause_count
))
1156 if (!tcs
->ramresult
|| fmt
)
1162 if (format_str(tcs
, fmt
, buf
, sizeof buf
) < 0)
1164 logf("format_str() failed");
1169 dptr
->name
= &c
->name_buffer
[namebufused
];
1171 namebufused
+= strlen(buf
)+1;
1173 namebufused
+= tcs
->result_len
;
1175 if (namebufused
>= c
->name_buffer_size
)
1177 logf("chunk mode #2: %d", current_entry_count
);
1183 strcpy(dptr
->name
, buf
);
1185 strcpy(dptr
->name
, tcs
->result
);
1188 dptr
->name
= tcs
->result
;
1191 current_entry_count
++;
1193 if (current_entry_count
>= global_settings
.max_files_in_dir
)
1195 logf("chunk mode #3: %d", current_entry_count
);
1201 if (init
&& !tcs
->ramsearch
)
1203 if (!show_search_progress(false, i
))
1205 tagcache_search_finish(tcs
);
1206 return current_entry_count
;
1212 qsort(c
->dircache
+ special_entry_count
* c
->dentry_size
,
1213 current_entry_count
- special_entry_count
,
1214 c
->dentry_size
, compare
);
1218 tagcache_search_finish(tcs
);
1219 return current_entry_count
;
1222 while (tagcache_get_next(tcs
))
1224 if (!tcs
->ramsearch
)
1226 if (!show_search_progress(false, total_count
))
1232 tagcache_search_finish(tcs
);
1234 if (!sort
&& (sort_inverse
|| sort_limit
))
1236 gui_syncsplash(HZ
*4, str(LANG_SHOWDIR_BUFFER_FULL
), total_count
);
1237 logf("Too small dir buffer");
1242 total_count
= MIN(total_count
, sort_limit
);
1247 for (i
= 0; i
< total_count
; i
++, dptr
++)
1249 int len
= strlen(dptr
->name
);
1254 dptr
->name
= &dptr
->name
[strip
];
1261 static int load_root(struct tree_context
*c
)
1263 struct tagentry
*dptr
= (struct tagentry
*)c
->dircache
;
1267 c
->currtable
= root
;
1268 if (c
->dirlevel
== 0)
1269 c
->currextra
= root_menu
;
1271 menu
= menus
[c
->currextra
];
1275 for (i
= 0; i
< menu
->itemcount
; i
++)
1277 dptr
->name
= menu
->items
[i
]->name
;
1278 switch (menu
->items
[i
]->type
)
1281 dptr
->newtable
= navibrowse
;
1282 dptr
->extraseek
= i
;
1286 dptr
->newtable
= root
;
1287 dptr
->extraseek
= menu
->items
[i
]->link
;
1295 current_entry_count
= i
;
1300 int tagtree_load(struct tree_context
* c
)
1303 int table
= c
->currtable
;
1305 c
->dentry_size
= sizeof(struct tagentry
);
1312 c
->currtable
= table
;
1313 c
->currextra
= root_menu
;
1319 count
= load_root(c
);
1324 logf("navibrowse...");
1326 count
= retrieve_entries(c
, &tcs
, 0, true);
1331 logf("Unsupported table %d\n", table
);
1338 count
= load_root(c
);
1339 gui_syncsplash(HZ
, str(LANG_TAGCACHE_BUSY
));
1342 /* The _total_ numer of entries available. */
1343 c
->dirlength
= c
->filesindir
= count
;
1348 int tagtree_enter(struct tree_context
* c
)
1351 struct tagentry
*dptr
;
1355 dptr
= tagtree_get_entry(c
, c
->selected_item
);
1358 newextra
= dptr
->newtable
;
1359 seek
= dptr
->extraseek
;
1361 if (c
->dirlevel
>= MAX_DIR_LEVELS
)
1364 c
->selected_item_history
[c
->dirlevel
]=c
->selected_item
;
1365 c
->table_history
[c
->dirlevel
] = c
->currtable
;
1366 c
->extra_history
[c
->dirlevel
] = c
->currextra
;
1367 c
->pos_history
[c
->dirlevel
] = c
->firstpos
;
1370 switch (c
->currtable
) {
1372 c
->currextra
= newextra
;
1374 if (newextra
== root
)
1377 c
->currextra
= seek
;
1380 else if (newextra
== navibrowse
)
1384 csi
= menu
->items
[seek
]->si
;
1387 strncpy(current_title
[c
->currextra
], dptr
->name
,
1388 sizeof(current_title
[0]) - 1);
1390 /* Read input as necessary. */
1391 for (i
= 0; i
< csi
->tagorder_count
; i
++)
1393 for (j
= 0; j
< csi
->clause_count
[i
]; j
++)
1395 if (!csi
->clause
[i
][j
]->input
)
1398 rc
= kbd_input(searchstring
, sizeof(searchstring
));
1399 if (rc
== -1 || !searchstring
[0])
1405 if (csi
->clause
[i
][j
]->numeric
)
1406 csi
->clause
[i
][j
]->numeric_data
= atoi(searchstring
);
1408 csi
->clause
[i
][j
]->str
= searchstring
;
1412 c
->currtable
= newextra
;
1418 if (newextra
== playtrack
)
1421 /* about to create a new current playlist...
1422 allow user to cancel the operation */
1423 if (global_settings
.warnon_erase_dynplaylist
&&
1424 !global_settings
.party_mode
&&
1425 playlist_modified(NULL
))
1427 char *lines
[]={str(LANG_WARN_ERASEDYNPLAYLIST_PROMPT
)};
1428 struct text_message message
={lines
, 1};
1430 if (gui_syncyesno_run(&message
, NULL
, NULL
) != YESNO_YES
)
1434 if (tagtree_play_folder(c
) >= 0)
1439 c
->currtable
= newextra
;
1440 csi
->result_seek
[c
->currextra
] = seek
;
1441 if (c
->currextra
< csi
->tagorder_count
-1)
1446 /* Update the statusbar title */
1447 strncpy(current_title
[c
->currextra
], dptr
->name
,
1448 sizeof(current_title
[0]) - 1);
1457 gui_synclist_select_item(&tree_lists
, c
->selected_item
);
1462 void tagtree_exit(struct tree_context
* c
)
1465 if (c
->dirlevel
> 0)
1467 c
->selected_item
=c
->selected_item_history
[c
->dirlevel
];
1468 gui_synclist_select_item(&tree_lists
, c
->selected_item
);
1469 c
->currtable
= c
->table_history
[c
->dirlevel
];
1470 c
->currextra
= c
->extra_history
[c
->dirlevel
];
1471 c
->firstpos
= c
->pos_history
[c
->dirlevel
];
1474 int tagtree_get_filename(struct tree_context
* c
, char *buf
, int buflen
)
1476 struct tagentry
*entry
;
1478 entry
= tagtree_get_entry(c
, c
->selected_item
);
1480 if (!tagcache_search(&tcs
, tag_filename
))
1483 if (!tagcache_retrieve(&tcs
, entry
->extraseek
, tcs
.type
, buf
, buflen
))
1485 tagcache_search_finish(&tcs
);
1489 tagcache_search_finish(&tcs
);
1494 static bool insert_all_playlist(struct tree_context
*c
, int position
, bool queue
)
1498 int from
, to
, direction
;
1499 int files_left
= c
->filesindir
;
1502 if (!tagcache_search(&tcs
, tag_filename
))
1504 gui_syncsplash(HZ
, str(LANG_TAGCACHE_BUSY
));
1509 if (position
== PLAYLIST_REPLACE
)
1511 if (remove_all_tracks(NULL
) == 0)
1512 position
= PLAYLIST_INSERT_LAST
;
1515 if (position
== PLAYLIST_INSERT_FIRST
)
1517 from
= c
->filesindir
- 1;
1528 for (i
= from
; i
!= to
; i
+= direction
)
1530 /* Count back to zero */
1531 if (!show_search_progress(false, files_left
--))
1534 if (!tagcache_retrieve(&tcs
, tagtree_get_entry(c
, i
)->extraseek
,
1535 tcs
.type
, buf
, sizeof buf
))
1540 if (playlist_insert_track(NULL
, buf
, position
, queue
, false) < 0)
1542 logf("playlist_insert_track failed");
1547 playlist_sync(NULL
);
1548 tagcache_search_finish(&tcs
);
1554 bool tagtree_insert_selection_playlist(int position
, bool queue
)
1556 struct tagentry
*dptr
;
1558 int dirlevel
= tc
->dirlevel
;
1560 /* We need to set the table to allsubentries. */
1561 show_search_progress(true, 0);
1563 dptr
= tagtree_get_entry(tc
, tc
->selected_item
);
1565 /* Insert a single track? */
1566 if (dptr
->newtable
== playtrack
)
1568 if (tagtree_get_filename(tc
, buf
, sizeof buf
) < 0)
1570 logf("tagtree_get_filename failed");
1573 playlist_insert_track(NULL
, buf
, position
, queue
, true);
1578 if (dptr
->newtable
== navibrowse
)
1582 dptr
= tagtree_get_entry(tc
, tc
->selected_item
);
1584 else if (dptr
->newtable
!= allsubentries
)
1586 logf("unsupported table: %d", dptr
->newtable
);
1590 /* Now the current table should be allsubentries. */
1591 if (dptr
->newtable
!= playtrack
)
1595 dptr
= tagtree_get_entry(tc
, tc
->selected_item
);
1597 /* And now the newtable should be playtrack. */
1598 if (dptr
->newtable
!= playtrack
)
1600 logf("newtable: %d !!", dptr
->newtable
);
1601 tc
->dirlevel
= dirlevel
;
1606 if (tc
->filesindir
<= 0)
1607 gui_syncsplash(HZ
, str(LANG_END_PLAYLIST_PLAYER
));
1610 logf("insert_all_playlist");
1611 if (!insert_all_playlist(tc
, position
, queue
))
1612 gui_syncsplash(HZ
*2, str(LANG_FAILED
));
1615 /* Finally return the dirlevel to its original value. */
1616 while (tc
->dirlevel
> dirlevel
)
1623 static int tagtree_play_folder(struct tree_context
* c
)
1625 if (playlist_create(NULL
, NULL
) < 0)
1627 logf("Failed creating playlist\n");
1631 if (!insert_all_playlist(c
, PLAYLIST_INSERT_LAST
, false))
1634 if (global_settings
.playlist_shuffle
)
1635 c
->selected_item
= playlist_shuffle(current_tick
, c
->selected_item
);
1636 if (!global_settings
.play_selected
)
1637 c
->selected_item
= 0;
1638 gui_synclist_select_item(&tree_lists
, c
->selected_item
);
1640 playlist_start(c
->selected_item
,0);
1645 struct tagentry
* tagtree_get_entry(struct tree_context
*c
, int id
)
1647 struct tagentry
*entry
= (struct tagentry
*)c
->dircache
;
1648 int realid
= id
- current_offset
;
1650 /* Load the next chunk if necessary. */
1651 if (realid
>= current_entry_count
|| realid
< 0)
1654 if (retrieve_entries(c
, &tcs2
, MAX(0, id
- (current_entry_count
/ 2)),
1657 logf("retrieve failed");
1661 realid
= id
- current_offset
;
1665 return &entry
[realid
];
1668 char *tagtree_get_title(struct tree_context
* c
)
1670 switch (c
->currtable
)
1677 return current_title
[c
->currextra
];
1683 int tagtree_get_attr(struct tree_context
* c
)
1686 switch (c
->currtable
)
1689 if (csi
->tagorder
[c
->currextra
] == tag_title
)
1690 attr
= FILE_ATTR_AUDIO
;
1692 attr
= ATTR_DIRECTORY
;
1696 attr
= FILE_ATTR_AUDIO
;
1700 attr
= ATTR_DIRECTORY
;
1707 int tagtree_get_icon(struct tree_context
* c
)
1709 int icon
= Icon_Folder
;
1711 if (tagtree_get_attr(c
) == FILE_ATTR_AUDIO
)