]> git.sesse.net Git - vlc/blob - src/playlist/search.c
macosx: fixed menubar appearance in fullscreen mode by partially reverting [46c93c9cc...
[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->i_id == p_item->i_id )
75         {
76             return ARRAY_VAL(p_playlist->all_items, i);
77         }
78     }
79     return NULL;
80 }
81
82
83 /**
84  * Get input by item id
85  *
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
91  */
92 playlist_item_t * playlist_ItemGetByInputId( playlist_t *p_playlist,
93                                              int i_input_id,
94                                              playlist_item_t *p_root )
95 {
96     int i;
97     PL_ASSERT_LOCKED;
98     assert( p_root != NULL );
99     for( i = 0 ; i< p_root->i_children ; i++ )
100     {
101         if( p_root->pp_children[i]->p_input &&
102             p_root->pp_children[i]->p_input->i_id == i_input_id )
103         {
104             return p_root->pp_children[i];
105         }
106         else if( p_root->pp_children[i]->i_children >= 0 )
107         {
108             return playlist_ItemGetByInputId( p_playlist, i_input_id,
109                                               p_root->pp_children[i] );
110         }
111     }
112     return NULL;
113 }
114
115 /***************************************************************************
116  * Live search handling
117  ***************************************************************************/
118
119 /**
120  * Enable all items in the playlist
121  * @param p_root: the current root item
122  */
123 static void playlist_LiveSearchClean( playlist_item_t *p_root )
124 {
125     for( int i = 0; i < p_root->i_children; i++ )
126     {
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;
131     }
132 }
133
134
135 /**
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
140  */
141 static bool playlist_LiveSearchUpdateInternal( playlist_item_t *p_root,
142                                                const char *psz_string )
143 {
144     int i;
145     bool b_match = false;
146     for( i = 0 ; i < p_root->i_children ; i ++ )
147     {
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 ) )
153         {
154             b_enable = true;
155         }
156
157         if( !b_enable )
158         {
159             vlc_mutex_lock( &p_item->p_input->lock );
160             // Do we have some meta ?
161             if( p_item->p_input->p_meta )
162             {
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 );
165                 if( !psz_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 ) );
172             }
173             else
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 );
176         }
177
178         if( b_enable )
179             p_item->i_flags &= ~PLAYLIST_DBL_FLAG;
180         else
181             p_item->i_flags |= PLAYLIST_DBL_FLAG;
182
183         b_match |= b_enable;
184    }
185    return b_match;
186 }
187
188
189
190 /**
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
196  */
197 int playlist_LiveSearchUpdate( playlist_t *p_playlist, playlist_item_t *p_root,
198                                const char *psz_string )
199 {
200     PL_ASSERT_LOCKED;
201     pl_priv(p_playlist)->b_reset_currently_playing = true;
202     if( *psz_string )
203         playlist_LiveSearchUpdateInternal( p_root, psz_string );
204     else
205         playlist_LiveSearchClean( p_root );
206     vlc_cond_signal( &pl_priv(p_playlist)->signal );
207     return VLC_SUCCESS;
208 }
209