1 /*****************************************************************************
2 * search.c : Search functions
3 *****************************************************************************
4 * Copyright (C) 1999-2009 VLC authors and VideoLAN
7 * Authors: Clément Stenac <zorglub@videolan.org>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
28 #include <vlc_common.h>
29 #include <vlc_playlist.h>
30 #include <vlc_charset.h>
31 #include "playlist_internal.h"
33 /***************************************************************************
34 * Item search functions
35 ***************************************************************************/
38 * Search a playlist item by its playlist_item id.
39 * The playlist have to be locked
40 * @param p_playlist: the playlist
41 * @param i_id: the id to find
42 * @return the item or NULL on failure
44 playlist_item_t
* playlist_ItemGetById( playlist_t
* p_playlist
, int i_id
)
48 ARRAY_BSEARCH( p_playlist
->all_items
,->i_id
, int, i_id
, i
);
50 return ARRAY_VAL( p_playlist
->all_items
, i
);
56 * Search an item by its input_item_t
57 * The playlist have to be locked
58 * @param p_playlist: the playlist
59 * @param p_item: the input_item_t to find
60 * @return the item, or NULL on failure
62 playlist_item_t
* playlist_ItemGetByInput( playlist_t
* p_playlist
,
63 input_item_t
*p_item
)
67 if( get_current_status_item( p_playlist
) &&
68 get_current_status_item( p_playlist
)->p_input
== p_item
)
70 return get_current_status_item( p_playlist
);
72 /** \todo Check if this is always incremental and whether we can bsearch */
73 for( i
= 0 ; i
< p_playlist
->all_items
.i_size
; i
++ )
75 if( ARRAY_VAL(p_playlist
->all_items
, i
)->p_input
== p_item
)
77 return ARRAY_VAL(p_playlist
->all_items
, i
);
84 /***************************************************************************
85 * Live search handling
86 ***************************************************************************/
89 * Enable all items in the playlist
90 * @param p_root: the current root item
92 static void playlist_LiveSearchClean( playlist_item_t
*p_root
)
94 for( int i
= 0; i
< p_root
->i_children
; i
++ )
96 playlist_item_t
*p_item
= p_root
->pp_children
[i
];
97 if( p_item
->i_children
>= 0 )
98 playlist_LiveSearchClean( p_item
);
99 p_item
->i_flags
&= ~PLAYLIST_DBL_FLAG
;
105 * Enable/Disable items in the playlist according to the search argument
106 * @param p_root: the current root item
107 * @param psz_string: the string to search
108 * @return true if an item match
110 static bool playlist_LiveSearchUpdateInternal( playlist_item_t
*p_root
,
111 const char *psz_string
, bool b_recursive
)
114 bool b_match
= false;
115 for( i
= 0 ; i
< p_root
->i_children
; i
++ )
117 bool b_enable
= false;
118 playlist_item_t
*p_item
= p_root
->pp_children
[i
];
119 // Go recurssively if their is some children
120 if( b_recursive
&& p_item
->i_children
>= 0 &&
121 playlist_LiveSearchUpdateInternal( p_item
, psz_string
, true ) )
128 vlc_mutex_lock( &p_item
->p_input
->lock
);
129 // Do we have some meta ?
130 if( p_item
->p_input
->p_meta
)
132 // Use Title or fall back to psz_name
133 const char *psz_title
= vlc_meta_Get( p_item
->p_input
->p_meta
, vlc_meta_Title
);
135 psz_title
= p_item
->p_input
->psz_name
;
136 const char *psz_album
= vlc_meta_Get( p_item
->p_input
->p_meta
, vlc_meta_Album
);
137 const char *psz_artist
= vlc_meta_Get( p_item
->p_input
->p_meta
, vlc_meta_Artist
);
138 b_enable
= ( psz_title
&& vlc_strcasestr( psz_title
, psz_string
) ) ||
139 ( psz_album
&& vlc_strcasestr( psz_album
, psz_string
) ) ||
140 ( psz_artist
&& vlc_strcasestr( psz_artist
, psz_string
) );
143 b_enable
= p_item
->p_input
->psz_name
&& vlc_strcasestr( p_item
->p_input
->psz_name
, psz_string
);
144 vlc_mutex_unlock( &p_item
->p_input
->lock
);
148 p_item
->i_flags
&= ~PLAYLIST_DBL_FLAG
;
150 p_item
->i_flags
|= PLAYLIST_DBL_FLAG
;
160 * Launch the recursive search in the playlist
161 * @param p_playlist: the playlist
162 * @param p_root: the current root item
163 * @param psz_string: the string to find
164 * @return VLC_SUCCESS
166 int playlist_LiveSearchUpdate( playlist_t
*p_playlist
, playlist_item_t
*p_root
,
167 const char *psz_string
, bool b_recursive
)
170 pl_priv(p_playlist
)->b_reset_currently_playing
= true;
172 playlist_LiveSearchUpdateInternal( p_root
, psz_string
, b_recursive
);
174 playlist_LiveSearchClean( p_root
);
175 vlc_cond_signal( &pl_priv(p_playlist
)->signal
);