1 /*****************************************************************************
2 * search.c : Search functions
3 *****************************************************************************
4 * Copyright (C) 1999-2009 the VideoLAN team
7 * Authors: Clément Stenac <zorglub@videolan.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 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 General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
28 #include <vlc_common.h>
29 #include "vlc_playlist.h"
30 #include "playlist_internal.h"
32 /***************************************************************************
33 * Item search functions
34 ***************************************************************************/
37 * Search a playlist item by its playlist_item id.
38 * The playlist have to be locked
39 * @param p_playlist: the playlist
40 * @param i_id: the id to find
41 * @return the item or NULL on failure
43 playlist_item_t* playlist_ItemGetById( playlist_t * p_playlist , int i_id )
47 ARRAY_BSEARCH( p_playlist->all_items,->i_id, int, i_id, i );
49 return ARRAY_VAL( p_playlist->all_items, i );
55 * Search an item by its input_item_t
56 * The playlist have to be locked
57 * @param p_playlist: the playlist
58 * @param p_item: the input_item_t to find
59 * @return the item, or NULL on failure
61 playlist_item_t* playlist_ItemGetByInput( playlist_t * p_playlist,
62 input_item_t *p_item )
66 if( get_current_status_item( p_playlist ) &&
67 get_current_status_item( p_playlist )->p_input == p_item )
69 return get_current_status_item( p_playlist );
71 /** \todo Check if this is always incremental and whether we can bsearch */
72 for( i = 0 ; i < p_playlist->all_items.i_size; i++ )
74 if( ARRAY_VAL(p_playlist->all_items, i)->p_input->i_id == p_item->i_id )
76 return ARRAY_VAL(p_playlist->all_items, i);
84 * Get input by item id
86 * Find the playlist item matching the input id under the given node
87 * \param p_playlist the playlist
88 * \param i_input_id the id of the input to find
89 * \param p_root the root node of the search
90 * \return the playlist item or NULL on failure
92 playlist_item_t * playlist_ItemGetByInputId( playlist_t *p_playlist,
94 playlist_item_t *p_root )
98 assert( p_root != NULL );
99 for( i = 0 ; i< p_root->i_children ; i++ )
101 if( p_root->pp_children[i]->p_input &&
102 p_root->pp_children[i]->p_input->i_id == i_input_id )
104 return p_root->pp_children[i];
106 else if( p_root->pp_children[i]->i_children >= 0 )
108 return playlist_ItemGetByInputId( p_playlist, i_input_id,
109 p_root->pp_children[i] );
115 /***************************************************************************
116 * Live search handling
117 ***************************************************************************/
120 * Enable all items in the playlist
121 * @param p_root: the current root item
123 static void playlist_LiveSearchClean( playlist_item_t *p_root )
125 for( int i = 0; i < p_root->i_children; i++ )
127 playlist_item_t *p_item = p_root->pp_children[i];
128 if( p_item->i_children >= 0 )
129 playlist_LiveSearchClean( p_item );
130 p_item->i_flags &= ~PLAYLIST_DBL_FLAG;
136 * Enable/Disable items in the playlist according to the search argument
137 * @param p_root: the current root item
138 * @param psz_string: the string to search
139 * @return true if an item match
141 static bool playlist_LiveSearchUpdateInternal( playlist_item_t *p_root,
142 const char *psz_string )
145 bool b_match = false;
146 for( i = 0 ; i < p_root->i_children ; i ++ )
148 bool b_enable = false;
149 playlist_item_t *p_item = p_root->pp_children[i];
150 // Go recurssively if their is some children
151 if( p_item->i_children >= 0 &&
152 playlist_LiveSearchUpdateInternal( p_item, psz_string ) )
159 vlc_mutex_lock( &p_item->p_input->lock );
160 // Do we have some meta ?
161 if( p_item->p_input->p_meta )
163 // Use Title or fall back to psz_name
164 const char *psz_title = vlc_meta_Get( p_item->p_input->p_meta, vlc_meta_Title );
166 psz_title = p_item->p_input->psz_name;
167 const char *psz_album = vlc_meta_Get( p_item->p_input->p_meta, vlc_meta_Album );
168 const char *psz_artist = vlc_meta_Get( p_item->p_input->p_meta, vlc_meta_Artist );
169 b_enable = ( psz_title && strcasestr( psz_title, psz_string ) ) ||
170 ( psz_album && strcasestr( psz_album, psz_string ) ) ||
171 ( psz_artist && strcasestr( psz_artist, psz_string ) );
174 b_enable = p_item->p_input->psz_name && strcasestr( p_item->p_input->psz_name, psz_string );
175 vlc_mutex_unlock( &p_item->p_input->lock );
179 p_item->i_flags &= ~PLAYLIST_DBL_FLAG;
181 p_item->i_flags |= PLAYLIST_DBL_FLAG;
191 * Launch the recursive search in the playlist
192 * @param p_playlist: the playlist
193 * @param p_root: the current root item
194 * @param psz_string: the string to find
195 * @return VLC_SUCCESS
197 int playlist_LiveSearchUpdate( playlist_t *p_playlist, playlist_item_t *p_root,
198 const char *psz_string )
201 pl_priv(p_playlist)->b_reset_currently_playing = true;
203 playlist_LiveSearchUpdateInternal( p_root, psz_string );
205 playlist_LiveSearchClean( p_root );
206 vlc_cond_signal( &pl_priv(p_playlist)->signal );