@@ -41,9 +41,10 @@ char *lib_live_filter = NULL;
4141
4242struct rb_root lib_shuffle_root ;
4343struct rb_root lib_album_shuffle_root ;
44+ LIST_HEAD (lib_all_tracks );
4445static struct expr * filter = NULL ;
4546static struct expr * add_filter = NULL ;
46- static int remove_from_hash = 1 ;
47+ int lib_filter_action = 0 ;
4748
4849static struct expr * live_filter_expr = NULL ;
4950static struct track_info * cur_track_ti = NULL ;
@@ -74,34 +75,21 @@ static void all_wins_changed(void)
7475 lib_editable .shared -> win -> changed = 1 ;
7576}
7677
77- static void shuffle_add (struct tree_track * track )
78- {
79- shuffle_list_add (& track -> simple_track .shuffle_info , & lib_shuffle_root , track -> album );
80- }
81-
82- static void album_shuffle_list_add (struct album * album )
83- {
84- shuffle_list_add (& album -> shuffle_info , & lib_album_shuffle_root , album );
85- }
86-
87- static void album_shuffle_list_remove (struct album * album )
88- {
89- rb_erase (& album -> shuffle_info .tree_node , & lib_album_shuffle_root );
90- }
91-
9278static void views_add_track (struct track_info * ti )
9379{
9480 struct tree_track * track = xnew (struct tree_track , 1 );
81+ track -> in_tree = false;
9582
9683 /* NOTE: does not ref ti */
9784 simple_track_init ((struct simple_track * )track , ti );
9885
9986 /* both the hash table and views have refs */
10087 track_info_ref (ti );
10188
102- tree_add_track (track , album_shuffle_list_add );
103- shuffle_add (track );
104- editable_add (& lib_editable , (struct simple_track * )track );
89+ tree_add_track (track );
90+
91+ if (!lib_is_filtered (ti ))
92+ editable_add (& lib_editable , (struct simple_track * )track );
10593}
10694
10795struct fh_entry {
@@ -160,7 +148,7 @@ static void hash_remove(struct track_info *ti)
160148 }
161149}
162150
163- static int is_filtered (struct track_info * ti )
151+ int lib_is_filtered (struct track_info * ti )
164152{
165153 if (live_filter_expr && !expr_eval (live_filter_expr , ti ))
166154 return 1 ;
@@ -182,7 +170,7 @@ static bool track_exists(struct track_info *ti)
182170 return false;
183171
184172 char * artist_collkey_name = u_strcasecoll_key (tree_artist_name (ti ));
185- rb_for_each_entry (artist , node , & lib_artist_root , tree_node ) {
173+ rb_for_each_entry (artist , node , & lib_all_artists_root , all_artists_node ) {
186174 if (strcmp (artist -> collkey_name , artist_collkey_name ) == 0 )
187175 break ;
188176 }
@@ -192,16 +180,16 @@ static bool track_exists(struct track_info *ti)
192180 return false;
193181
194182 char * album_collkey_name = u_strcasecoll_key (tree_album_name (ti ));
195- rb_for_each_entry (album , node , & artist -> album_root , tree_node ) {
183+ list_for_each_entry (album , & artist -> all_albums , all_albums_node ) {
196184 if (strcmp (album -> collkey_name , album_collkey_name ) == 0 )
197185 break ;
198186 }
199187 free (album_collkey_name );
200188
201- if (! album )
189+ if (& album -> all_albums_node == & artist -> all_albums ) // looped back to list head
202190 return false;
203191
204- rb_for_each_entry (track , node , & album -> track_root , tree_node ) {
192+ list_for_each_entry (track , & album -> all_tracks , all_tracks_node ) {
205193 struct track_info * iter_ti = tree_track_info (track );
206194 if (iter_ti -> tracknumber == ti -> tracknumber
207195 && iter_ti -> discnumber == ti -> discnumber
@@ -226,8 +214,7 @@ void lib_add_track(struct track_info *ti, void *opaque)
226214 return ;
227215 }
228216
229- if (!is_filtered (ti ))
230- views_add_track (ti );
217+ views_add_track (ti );
231218}
232219
233220static struct tree_track * album_first_track (const struct album * album )
@@ -442,7 +429,7 @@ void lib_reshuffle(void)
442429}
443430
444431void lib_sort_artists (void ) {
445- tree_sort_artists (album_shuffle_list_add , album_shuffle_list_remove );
432+ tree_sort_artists ();
446433}
447434
448435static void free_lib_track (struct editable * e , struct list_head * item )
@@ -453,14 +440,13 @@ static void free_lib_track(struct editable *e, struct list_head *item)
453440 if (track == lib_cur_track )
454441 lib_cur_track = NULL ;
455442
456- if (remove_from_hash )
457- hash_remove (ti );
458-
459- rb_erase (& track -> simple_track .shuffle_info .tree_node , & lib_shuffle_root );
460- tree_remove (track , album_shuffle_list_remove );
443+ tree_remove (track );
461444
462- track_info_unref (ti );
463- free (track );
445+ if (!lib_filter_action ) {
446+ hash_remove (ti );
447+ track_info_unref (ti );
448+ free (track );
449+ }
464450}
465451
466452void lib_init (void )
@@ -621,23 +607,6 @@ struct track_info *sorted_activate_selected(void)
621607 return lib_set_track (sorted_get_selected ());
622608}
623609
624- static void hash_add_to_views (void )
625- {
626- int i ;
627- for (i = 0 ; i < FH_SIZE ; i ++ ) {
628- struct fh_entry * e ;
629-
630- e = ti_hash [i ];
631- while (e ) {
632- struct track_info * ti = e -> ti ;
633-
634- if (!is_filtered (ti ))
635- views_add_track (ti );
636- e = e -> next ;
637- }
638- }
639- }
640-
641610struct tree_track * lib_find_track (struct track_info * ti )
642611{
643612 struct simple_track * track ;
@@ -675,25 +644,37 @@ static void restore_cur_track(struct track_info *ti)
675644
676645static int is_filtered_cb (void * data , struct track_info * ti )
677646{
678- return is_filtered (ti );
647+ return lib_is_filtered (ti );
679648}
680649
681- static void do_lib_filter (int clear_before )
650+ static void do_lib_filter (int may_grow )
682651{
652+ struct tree_track * track ;
653+
683654 /* try to save cur_track */
684655 if (lib_cur_track )
685656 lib_store_cur_track (tree_track_info (lib_cur_track ));
686657
687- if (clear_before )
688- d_print ("filter results could grow, clear tracks and re-add (slow)\n" );
658+ lib_filter_action = 1 ;
659+
660+ editable_remove_matching_tracks (& lib_editable , is_filtered_cb , NULL );
661+
662+ if (may_grow ) {
663+ /* ensure an album isn't selected when we collapse the artists */
664+ window_goto_top (lib_tree_win );
689665
690- remove_from_hash = 0 ;
691- if (clear_before ) {
692- editable_clear (& lib_editable );
693- hash_add_to_views ();
694- } else
695- editable_remove_matching_tracks (& lib_editable , is_filtered_cb , NULL );
696- remove_from_hash = 1 ;
666+ list_for_each_entry (track , & lib_all_tracks , lib_all_tracks_node ) {
667+ /* collapse all artists for now, live filter matches will be
668+ * expanded again later */
669+ track -> album -> artist -> expanded = false;
670+
671+ if (!track -> in_tree && !lib_is_filtered (tree_track_info (track ))) {
672+ tree_readd_track (track );
673+ editable_add (& lib_editable , (struct simple_track * )track );
674+ }
675+ }
676+ }
677+ lib_filter_action = 0 ;
697678
698679 window_changed (lib_editable .shared -> win );
699680 window_goto_top (lib_editable .shared -> win );
@@ -715,12 +696,12 @@ static void unset_live_filter(void)
715696
716697void lib_set_filter (struct expr * expr )
717698{
718- int clear_before = lib_live_filter || filter ;
699+ int may_grow = lib_live_filter || filter ;
719700 unset_live_filter ();
720701 if (filter )
721702 expr_free (filter );
722703 filter = expr ;
723- do_lib_filter (clear_before );
704+ do_lib_filter (may_grow );
724705}
725706
726707void lib_set_add_filter (struct expr * expr )
@@ -777,8 +758,8 @@ static void restore_sel_track(void)
777758 }
778759}
779760
780- /* determine if filter results could grow, in which case all tracks must be cleared and re-added */
781- static int do_clear_before (const char * str , struct expr * expr )
761+ /* determine if filter results could grow */
762+ static int expr_may_grow (const char * str , struct expr * expr )
782763{
783764 if (!lib_live_filter )
784765 return 0 ;
@@ -793,7 +774,7 @@ static int do_clear_before(const char *str, struct expr *expr)
793774
794775void lib_set_live_filter (const char * str )
795776{
796- int clear_before ;
777+ int may_grow ;
797778 struct expr * expr = NULL ;
798779
799780 if (strcmp0 (str , lib_live_filter ) == 0 )
@@ -805,15 +786,15 @@ void lib_set_live_filter(const char *str)
805786 return ;
806787 }
807788
808- clear_before = do_clear_before (str , expr );
789+ may_grow = expr_may_grow (str , expr );
809790
810791 if (!str )
811792 store_sel_track ();
812793
813794 unset_live_filter ();
814795 lib_live_filter = str ? xstrdup (str ) : NULL ;
815796 live_filter_expr = expr ;
816- do_lib_filter (clear_before );
797+ do_lib_filter (may_grow );
817798
818799 if (expr ) {
819800 unsigned int match_type = expr_get_match_type (expr );
0 commit comments