]> git.sesse.net Git - vlc/blob - src/playlist/search.c
Use var_Inherit* instead of var_CreateGet*.
[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  * 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
60  */
61 playlist_item_t* playlist_ItemGetByInput( playlist_t * p_playlist,
62                                           input_item_t *p_item )
63 {
64     int i;
65     PL_ASSERT_LOCKED;
66     if( get_current_status_item( p_playlist ) &&
67         get_current_status_item( p_playlist )->p_input == p_item )
68     {
69         return get_current_status_item( p_playlist );
70     }
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++ )
73     {
74         if( ARRAY_VAL(p_playlist->all_items, i)->p_input == p_item )
75         {
76             return ARRAY_VAL(p_playlist->all_items, i);
77         }
78     }
79     return NULL;
80 }
81
82
83 /***************************************************************************
84  * Live search handling
85  ***************************************************************************/
86
87 /**
88  * Enable all items in the playlist
89  * @param p_root: the current root item
90  */
91 static void playlist_LiveSearchClean( playlist_item_t *p_root )
92 {
93     for( int i = 0; i < p_root->i_children; i++ )
94     {
95         playlist_item_t *p_item = p_root->pp_children[i];
96         if( p_item->i_children >= 0 )
97             playlist_LiveSearchClean( p_item );
98         p_item->i_flags &= ~PLAYLIST_DBL_FLAG;
99     }
100 }
101
102
103 /**
104  * Enable/Disable items in the playlist according to the search argument
105  * @param p_root: the current root item
106  * @param psz_string: the string to search
107  * @return true if an item match
108  */
109 static bool playlist_LiveSearchUpdateInternal( playlist_item_t *p_root,
110                                                const char *psz_string, bool b_recursive )
111 {
112     int i;
113     bool b_match = false;
114     for( i = 0 ; i < p_root->i_children ; i ++ )
115     {
116         bool b_enable = false;
117         playlist_item_t *p_item = p_root->pp_children[i];
118         // Go recurssively if their is some children
119         if( b_recursive && p_item->i_children >= 0 &&
120             playlist_LiveSearchUpdateInternal( p_item, psz_string, true ) )
121         {
122             b_enable = true;
123         }
124
125         if( !b_enable )
126         {
127             vlc_mutex_lock( &p_item->p_input->lock );
128             // Do we have some meta ?
129             if( p_item->p_input->p_meta )
130             {
131                 // Use Title or fall back to psz_name
132                 const char *psz_title = vlc_meta_Get( p_item->p_input->p_meta, vlc_meta_Title );
133                 if( !psz_title )
134                     psz_title = p_item->p_input->psz_name;
135                 const char *psz_album = vlc_meta_Get( p_item->p_input->p_meta, vlc_meta_Album );
136                 const char *psz_artist = vlc_meta_Get( p_item->p_input->p_meta, vlc_meta_Artist );
137                 b_enable = ( psz_title && strcasestr( psz_title, psz_string ) ) ||
138                            ( psz_album && strcasestr( psz_album, psz_string ) ) ||
139                            ( psz_artist && strcasestr( psz_artist, psz_string ) );
140             }
141             else
142                 b_enable = p_item->p_input->psz_name && strcasestr( p_item->p_input->psz_name, psz_string );
143             vlc_mutex_unlock( &p_item->p_input->lock );
144         }
145
146         if( b_enable )
147             p_item->i_flags &= ~PLAYLIST_DBL_FLAG;
148         else
149             p_item->i_flags |= PLAYLIST_DBL_FLAG;
150
151         b_match |= b_enable;
152    }
153    return b_match;
154 }
155
156
157
158 /**
159  * Launch the recursive search in the playlist
160  * @param p_playlist: the playlist
161  * @param p_root: the current root item
162  * @param psz_string: the string to find
163  * @return VLC_SUCCESS
164  */
165 int playlist_LiveSearchUpdate( playlist_t *p_playlist, playlist_item_t *p_root,
166                                const char *psz_string, bool b_recursive )
167 {
168     PL_ASSERT_LOCKED;
169     pl_priv(p_playlist)->b_reset_currently_playing = true;
170     if( *psz_string )
171         playlist_LiveSearchUpdateInternal( p_root, psz_string, b_recursive );
172     else
173         playlist_LiveSearchClean( p_root );
174     vlc_cond_signal( &pl_priv(p_playlist)->signal );
175     return VLC_SUCCESS;
176 }
177