]> git.sesse.net Git - vlc/blob - src/playlist/search.c
f7708b582445dd5e4cafe7495a66def02a590310
[vlc] / src / playlist / search.c
1 /*****************************************************************************
2  * search.c : Search functions
3  *****************************************************************************
4  * Copyright (C) 1999-2009 the VideoLAN team
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #endif
26 #include <assert.h>
27
28 #include <vlc_common.h>
29 #include "vlc_playlist.h"
30 #include "playlist_internal.h"
31
32 /***************************************************************************
33  * Item search functions
34  ***************************************************************************/
35
36 /**
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
42  */
43 playlist_item_t* playlist_ItemGetById( playlist_t * p_playlist , int i_id )
44 {
45     int i;
46     PL_ASSERT_LOCKED;
47     ARRAY_BSEARCH( p_playlist->all_items,->i_id, int, i_id, i );
48     if( i != -1 )
49         return ARRAY_VAL( p_playlist->all_items, i );
50     else
51         return NULL;
52 }
53
54 /**
55  * Search an item by its input_item_t
56  *
57  * \param p_playlist the playlist
58  * \param p_item the input_item_t to find
59  * \return the item, or NULL on failure
60  */
61 playlist_item_t * playlist_ItemGetByInput( playlist_t * p_playlist ,
62                                            input_item_t *p_item,
63                                            bool b_locked )
64 {
65     int i;
66     PL_LOCK_IF( !b_locked );
67     if( get_current_status_item( p_playlist ) &&
68         get_current_status_item( p_playlist )->p_input == p_item )
69     {
70         /* FIXME: this is potentially dangerous, we could destroy
71          * p_ret any time soon */
72         playlist_item_t *p_ret = get_current_status_item( p_playlist );
73         PL_UNLOCK_IF( !b_locked );
74         return p_ret;
75     }
76     /** \todo Check if this is always incremental and whether we can bsearch */
77     for( i =  0 ; i < p_playlist->all_items.i_size; i++ )
78     {
79         if( ARRAY_VAL(p_playlist->all_items, i)->p_input->i_id == p_item->i_id )
80         {
81             PL_UNLOCK_IF( !b_locked );
82             return ARRAY_VAL(p_playlist->all_items, i);
83         }
84     }
85     PL_UNLOCK_IF( !b_locked );
86     return NULL;
87 }
88
89
90 /**
91  * Get input by item id
92  *
93  * Find the playlist item matching the input id under the given node
94  * \param p_playlist the playlist
95  * \param i_input_id the id of the input to find
96  * \param p_root the root node of the search
97  * \return the playlist item or NULL on failure
98  */
99 playlist_item_t * playlist_ItemGetByInputId( playlist_t *p_playlist,
100                                              int i_input_id,
101                                              playlist_item_t *p_root )
102 {
103     int i;
104     PL_ASSERT_LOCKED;
105     assert( p_root != NULL );
106     for( i = 0 ; i< p_root->i_children ; i++ )
107     {
108         if( p_root->pp_children[i]->p_input &&
109             p_root->pp_children[i]->p_input->i_id == i_input_id )
110         {
111             return p_root->pp_children[i];
112         }
113         else if( p_root->pp_children[i]->i_children >= 0 )
114         {
115             return playlist_ItemGetByInputId( p_playlist, i_input_id,
116                                               p_root->pp_children[i] );
117         }
118     }
119     return NULL;
120 }
121
122 /***************************************************************************
123  * Live search handling
124  ***************************************************************************/
125
126 /**
127  * Enable all items in the playlist
128  * @param p_root: the current root item
129  */
130 static void playlist_LiveSearchClean( playlist_item_t *p_root )
131 {
132     for( int i = 0; i < p_root->i_children; i++ )
133     {
134         playlist_item_t *p_item = p_root->pp_children[i];
135         if( p_item->i_children >= 0 )
136             playlist_LiveSearchClean( p_item );
137         p_item->i_flags &= ~PLAYLIST_DBL_FLAG;
138     }
139 }
140
141
142 /**
143  * Enable/Disable items in the playlist according to the search argument
144  * @param p_root: the current root item
145  * @param psz_string: the string to search
146  * @return true if an item match
147  */
148 static bool playlist_LiveSearchUpdateInternal( playlist_item_t *p_root,
149                                                const char *psz_string )
150 {
151     int i;
152     bool b_match = false;
153     for( i = 0 ; i < p_root->i_children ; i ++ )
154     {
155         bool b_enable = false;
156         playlist_item_t *p_item = p_root->pp_children[i];
157         // Go recurssively if their is some children
158         if( p_item->i_children >= 0 &&
159             playlist_LiveSearchUpdateInternal( p_item, psz_string ) )
160         {
161             b_enable = true;
162         }
163
164         if( !b_enable )
165         {
166             vlc_mutex_lock( &p_item->p_input->lock );
167             // Do we have some meta ?
168             if( p_item->p_input->p_meta )
169             {
170                 // Use Title or fall back to psz_name
171                 const char *psz_title = vlc_meta_Get( p_item->p_input->p_meta, vlc_meta_Title );
172                 if( !psz_title )
173                     psz_title = p_item->p_input->psz_name;
174                 const char *psz_album = vlc_meta_Get( p_item->p_input->p_meta, vlc_meta_Album );
175                 const char *psz_artist = vlc_meta_Get( p_item->p_input->p_meta, vlc_meta_Artist );
176                 b_enable = ( psz_title && strcasestr( psz_title, psz_string ) ) ||
177                            ( psz_album && strcasestr( psz_album, psz_string ) ) ||
178                            ( psz_artist && strcasestr( psz_artist, psz_string ) );
179             }
180             else
181                 b_enable = p_item->p_input->psz_name && strcasestr( p_item->p_input->psz_name, psz_string );
182             vlc_mutex_unlock( &p_item->p_input->lock );
183         }
184
185         if( b_enable )
186             p_item->i_flags &= ~PLAYLIST_DBL_FLAG;
187         else
188             p_item->i_flags |= PLAYLIST_DBL_FLAG;
189
190         b_match |= b_enable;
191    }
192    return b_match;
193 }
194
195
196
197 /**
198  * Launch the recursive search in the playlist
199  * @param p_playlist: the playlist
200  * @param p_root: the current root item
201  * @param psz_string: the string to find
202  * @return VLC_SUCCESS
203  */
204 int playlist_LiveSearchUpdate( playlist_t *p_playlist, playlist_item_t *p_root,
205                                const char *psz_string )
206 {
207     PL_ASSERT_LOCKED;
208     pl_priv(p_playlist)->b_reset_currently_playing = true;
209     if( *psz_string )
210         playlist_LiveSearchUpdateInternal( p_root, psz_string );
211     else
212         playlist_LiveSearchClean( p_root );
213     vlc_cond_signal( &pl_priv(p_playlist)->signal );
214     return VLC_SUCCESS;
215 }
216