]> git.sesse.net Git - vlc/blob - src/playlist/search.c
demux: ts: remove pid array
[vlc] / src / playlist / search.c
1 /*****************************************************************************
2  * search.c : Search functions
3  *****************************************************************************
4  * Copyright (C) 1999-2009 VLC authors and VideoLAN
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 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.
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 Lesser General Public License for more details.
18  *
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  *****************************************************************************/
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 <vlc_charset.h>
31 #include "playlist_internal.h"
32
33 /***************************************************************************
34  * Item search functions
35  ***************************************************************************/
36
37 /**
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
43  */
44 playlist_item_t* playlist_ItemGetById( playlist_t * p_playlist , int i_id )
45 {
46     int i;
47     PL_ASSERT_LOCKED;
48     ARRAY_BSEARCH( p_playlist->all_items,->i_id, int, i_id, i );
49     if( i != -1 )
50         return ARRAY_VAL( p_playlist->all_items, i );
51     else
52         return NULL;
53 }
54
55 /**
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
61  */
62 playlist_item_t* playlist_ItemGetByInput( playlist_t * p_playlist,
63                                           input_item_t *p_item )
64 {
65     int i;
66     PL_ASSERT_LOCKED;
67     if( get_current_status_item( p_playlist ) &&
68         get_current_status_item( p_playlist )->p_input == p_item )
69     {
70         return get_current_status_item( p_playlist );
71     }
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++ )
74     {
75         if( ARRAY_VAL(p_playlist->all_items, i)->p_input == p_item )
76         {
77             return ARRAY_VAL(p_playlist->all_items, i);
78         }
79     }
80     return NULL;
81 }
82
83
84 /***************************************************************************
85  * Live search handling
86  ***************************************************************************/
87
88 /**
89  * Enable all items in the playlist
90  * @param p_root: the current root item
91  */
92 static void playlist_LiveSearchClean( playlist_item_t *p_root )
93 {
94     for( int i = 0; i < p_root->i_children; i++ )
95     {
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;
100     }
101 }
102
103
104 /**
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
109  */
110 static bool playlist_LiveSearchUpdateInternal( playlist_item_t *p_root,
111                                                const char *psz_string, bool b_recursive )
112 {
113     int i;
114     bool b_match = false;
115     for( i = 0 ; i < p_root->i_children ; i ++ )
116     {
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 ) )
122         {
123             b_enable = true;
124         }
125
126         if( !b_enable )
127         {
128             vlc_mutex_lock( &p_item->p_input->lock );
129             // Do we have some meta ?
130             if( p_item->p_input->p_meta )
131             {
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 );
134                 if( !psz_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 ) );
141             }
142             else
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 );
145         }
146
147         if( b_enable )
148             p_item->i_flags &= ~PLAYLIST_DBL_FLAG;
149         else
150             p_item->i_flags |= PLAYLIST_DBL_FLAG;
151
152         b_match |= b_enable;
153    }
154    return b_match;
155 }
156
157
158
159 /**
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
165  */
166 int playlist_LiveSearchUpdate( playlist_t *p_playlist, playlist_item_t *p_root,
167                                const char *psz_string, bool b_recursive )
168 {
169     PL_ASSERT_LOCKED;
170     pl_priv(p_playlist)->b_reset_currently_playing = true;
171     if( *psz_string )
172         playlist_LiveSearchUpdateInternal( p_root, psz_string, b_recursive );
173     else
174         playlist_LiveSearchClean( p_root );
175     vlc_cond_signal( &pl_priv(p_playlist)->signal );
176     return VLC_SUCCESS;
177 }
178