search.c 6 KB
Newer Older
1 2 3
/*****************************************************************************
 * search.c : Search functions
 *****************************************************************************
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
4
 * Copyright (C) 1999-2009 VLC authors and VideoLAN
5 6 7 8
 * $Id$
 *
 * Authors: Clément Stenac <zorglub@videolan.org>
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
9 10 11
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
12 13 14 15
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
16 17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
18
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
19 20 21
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22
 *****************************************************************************/
23 24 25
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
26
#include <assert.h>
27

28
#include <vlc_common.h>
29 30
#include <vlc_playlist.h>
#include <vlc_charset.h>
31
#include "playlist_internal.h"
32 33 34 35 36 37

/***************************************************************************
 * Item search functions
 ***************************************************************************/

/**
38 39 40 41 42
 * Search a playlist item by its playlist_item id.
 * The playlist have to be locked
 * @param p_playlist: the playlist
 * @param i_id: the id to find
 * @return the item or NULL on failure
43
 */
44
playlist_item_t* playlist_ItemGetById( playlist_t * p_playlist , int i_id )
45
{
Clément Stenac's avatar
Clément Stenac committed
46
    int i;
47
    PL_ASSERT_LOCKED;
Clément Stenac's avatar
Clément Stenac committed
48
    ARRAY_BSEARCH( p_playlist->all_items,->i_id, int, i_id, i );
Rémi Duraffort's avatar
Rémi Duraffort committed
49
    if( i != -1 )
Clément Stenac's avatar
Clément Stenac committed
50
        return ARRAY_VAL( p_playlist->all_items, i );
51 52
    else
        return NULL;
53 54 55 56
}

/**
 * Search an item by its input_item_t
57 58 59 60
 * The playlist have to be locked
 * @param p_playlist: the playlist
 * @param p_item: the input_item_t to find
 * @return the item, or NULL on failure
61
 */
62 63
playlist_item_t* playlist_ItemGetByInput( playlist_t * p_playlist,
                                          input_item_t *p_item )
64 65
{
    int i;
66
    PL_ASSERT_LOCKED;
67 68
    if( get_current_status_item( p_playlist ) &&
        get_current_status_item( p_playlist )->p_input == p_item )
69
    {
70
        return get_current_status_item( p_playlist );
71
    }
Clément Stenac's avatar
Clément Stenac committed
72 73
    /** \todo Check if this is always incremental and whether we can bsearch */
    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
        {
Clément Stenac's avatar
Clément Stenac committed
77
            return ARRAY_VAL(p_playlist->all_items, i);
78
        }
79 80 81 82
    }
    return NULL;
}

83

84 85 86 87
/***************************************************************************
 * Live search handling
 ***************************************************************************/

88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
/**
 * Enable all items in the playlist
 * @param p_root: the current root item
 */
static void playlist_LiveSearchClean( playlist_item_t *p_root )
{
    for( int i = 0; i < p_root->i_children; i++ )
    {
        playlist_item_t *p_item = p_root->pp_children[i];
        if( p_item->i_children >= 0 )
            playlist_LiveSearchClean( p_item );
        p_item->i_flags &= ~PLAYLIST_DBL_FLAG;
    }
}


/**
 * Enable/Disable items in the playlist according to the search argument
 * @param p_root: the current root item
 * @param psz_string: the string to search
 * @return true if an item match
 */
static bool playlist_LiveSearchUpdateInternal( playlist_item_t *p_root,
111
                                               const char *psz_string, bool b_recursive )
112
{
Rémi Duraffort's avatar
Rémi Duraffort committed
113 114 115 116 117
    int i;
    bool b_match = false;
    for( i = 0 ; i < p_root->i_children ; i ++ )
    {
        bool b_enable = false;
118
        playlist_item_t *p_item = p_root->pp_children[i];
Rémi Duraffort's avatar
Rémi Duraffort committed
119
        // Go recurssively if their is some children
120 121
        if( b_recursive && p_item->i_children >= 0 &&
            playlist_LiveSearchUpdateInternal( p_item, psz_string, true ) )
122
        {
Rémi Duraffort's avatar
Rémi Duraffort committed
123
            b_enable = true;
124
        }
Rémi Duraffort's avatar
Rémi Duraffort committed
125 126

        if( !b_enable )
127
        {
Rémi Duraffort's avatar
Rémi Duraffort committed
128 129 130
            vlc_mutex_lock( &p_item->p_input->lock );
            // Do we have some meta ?
            if( p_item->p_input->p_meta )
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
131
            {
Rémi Duraffort's avatar
Rémi Duraffort committed
132 133 134 135 136 137
                // Use Title or fall back to psz_name
                const char *psz_title = vlc_meta_Get( p_item->p_input->p_meta, vlc_meta_Title );
                if( !psz_title )
                    psz_title = p_item->p_input->psz_name;
                const char *psz_album = vlc_meta_Get( p_item->p_input->p_meta, vlc_meta_Album );
                const char *psz_artist = vlc_meta_Get( p_item->p_input->p_meta, vlc_meta_Artist );
138 139 140
                b_enable = ( psz_title && vlc_strcasestr( psz_title, psz_string ) ) ||
                           ( psz_album && vlc_strcasestr( psz_album, psz_string ) ) ||
                           ( psz_artist && vlc_strcasestr( psz_artist, psz_string ) );
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
141
            }
142
            else
143
                b_enable = p_item->p_input->psz_name && vlc_strcasestr( p_item->p_input->psz_name, psz_string );
Rémi Duraffort's avatar
Rémi Duraffort committed
144
            vlc_mutex_unlock( &p_item->p_input->lock );
145
        }
Rémi Duraffort's avatar
Rémi Duraffort committed
146 147 148 149 150 151 152

        if( b_enable )
            p_item->i_flags &= ~PLAYLIST_DBL_FLAG;
        else
            p_item->i_flags |= PLAYLIST_DBL_FLAG;

        b_match |= b_enable;
153
   }
154 155 156
   return b_match;
}

Rémi Duraffort's avatar
Rémi Duraffort committed
157 158 159 160 161 162 163 164 165


/**
 * Launch the recursive search in the playlist
 * @param p_playlist: the playlist
 * @param p_root: the current root item
 * @param psz_string: the string to find
 * @return VLC_SUCCESS
 */
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 174
    else
        playlist_LiveSearchClean( p_root );
175
    vlc_cond_signal( &pl_priv(p_playlist)->signal );
176
    return VLC_SUCCESS;
177
}
178