search.c 5.76 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*****************************************************************************
 * search.c : Search functions
 *****************************************************************************
 * Copyright (C) 1999-2004 the VideoLAN team
 * $Id$
 *
 * Authors: Clément Stenac <zorglub@videolan.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (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
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU 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.
 *****************************************************************************/
23
24
25
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
26
#include <assert.h>
27

28
29
#include <vlc/vlc.h>
#include "vlc_playlist.h"
30
#include "playlist_internal.h"
31
32
33
34
35
36
37
38
39
40
41
42

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

/**
 * Search a playlist item by its playlist_item id
 *
 * \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,
                                        vlc_bool_t b_locked )
45
{
zorglub's avatar
zorglub committed
46
    int i;
47
    if( !b_locked ) PL_LOCK;
zorglub's avatar
zorglub committed
48
    ARRAY_BSEARCH( p_playlist->all_items,->i_id, int, i_id, i );
ivoire's avatar
ivoire committed
49
50
    if( i != -1 )
    {
51
        if( !b_locked ) PL_UNLOCK;
zorglub's avatar
zorglub committed
52
        return ARRAY_VAL( p_playlist->all_items, i );
53
54
    }
    if( !b_locked ) PL_UNLOCK;
55
56
57
58
59
60
61
62
63
64
65
    return NULL;
}

/**
 * Search an item by its input_item_t
 *
 * \param p_playlist the playlist
 * \param p_item the input_item_t to find
 * \return the item, or NULL on failure
 */
playlist_item_t * playlist_ItemGetByInput( playlist_t * p_playlist ,
66
67
                                           input_item_t *p_item,
                                           vlc_bool_t b_locked )
68
69
{
    int i;
70
    if( !b_locked ) PL_LOCK;
71
72
    if( p_playlist->status.p_item &&
        p_playlist->status.p_item->p_input == p_item )
73
    {
74
        if( !b_locked ) PL_UNLOCK;
75
76
        return p_playlist->status.p_item;
    }
zorglub's avatar
zorglub committed
77
78
    /** \todo Check if this is always incremental and whether we can bsearch */
    for( i =  0 ; i < p_playlist->all_items.i_size; i++ )
79
    {
zorglub's avatar
zorglub committed
80
        if( ARRAY_VAL(p_playlist->all_items, i)->p_input->i_id == p_item->i_id )
81
82
        {
            if( !b_locked ) PL_UNLOCK;
zorglub's avatar
zorglub committed
83
            return ARRAY_VAL(p_playlist->all_items, i);
84
        }
85
    }
86
    if( !b_locked ) PL_UNLOCK;
87
88
89
    return NULL;
}

ivoire's avatar
ivoire committed
90
91
92
93
94
95
96
97
98
/**
 * Get input by item id
 *
 * Find the playlist item matching the input id under the given node
 * \param p_playlist the playlist
 * \param i_input_id the id of the input to find
 * \param p_root the root node of the search
 * \return the playlist item or NULL on failure
 */
99
100
101
102
103
playlist_item_t * playlist_ItemGetByInputId( playlist_t *p_playlist,
                                             int i_input_id,
                                             playlist_item_t *p_root )
{
    int i;
104
    assert( p_root != NULL );
105
106
    for( i = 0 ; i< p_root->i_children ; i++ )
    {
107
        if( p_root->pp_children[i]->p_input &&
108
109
110
111
112
113
114
115
116
117
118
119
120
            p_root->pp_children[i]->p_input->i_id == i_input_id )
        {
            return p_root->pp_children[i];
        }
        else if( p_root->pp_children[i]->i_children >= 0 )
        {
            return playlist_ItemGetByInputId( p_playlist, i_input_id,
                                              p_root->pp_children[i] );
        }
    }
    return NULL;
}

121
122
123
124
/***************************************************************************
 * Live search handling
 ***************************************************************************/

125
126
127
static vlc_bool_t playlist_LiveSearchUpdateInternal( playlist_t *p_playlist,
                                                     playlist_item_t *p_root,
                                                     const char *psz_string )
128
129
{
   int i;
130
131
   vlc_bool_t b_match = VLC_FALSE;
   for( i = 0 ; i < p_root->i_children ; i ++ )
132
133
134
135
   {
        playlist_item_t *p_item = p_root->pp_children[i];
        if( p_item->i_children > -1 )
        {
136
137
138
139
140
141
142
143
144
145
            if( playlist_LiveSearchUpdateInternal( p_playlist, p_item, psz_string ) ||
                strcasestr( p_item->p_input->psz_name, psz_string ) )
            {
                p_item->i_flags &= ~PLAYLIST_DBL_FLAG;
                b_match = VLC_TRUE;
            }
            else
            {
                p_item->i_flags |= PLAYLIST_DBL_FLAG;
            }
146
147
        }
        else
148
        {
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
149
150
151
152
            if( strcasestr( p_item->p_input->psz_name, psz_string ) || /* Soon to be replaced by vlc_meta_Title */
                input_item_MetaMatch( p_item->p_input, vlc_meta_Album, psz_string ) ||
                input_item_MetaMatch( p_item->p_input, vlc_meta_Artist, psz_string ) )
            {
153
                p_item->i_flags &= ~PLAYLIST_DBL_FLAG;
154
                b_match = VLC_TRUE;
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
155
            }
156
            else
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
157
            {
158
                p_item->i_flags |= PLAYLIST_DBL_FLAG;
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
159
            }
160
        }
161
   }
162
163
164
165
166
167
   return b_match;
}

int playlist_LiveSearchUpdate( playlist_t *p_playlist, playlist_item_t *p_root,
                               const char *psz_string )
{
ivoire's avatar
ivoire committed
168
    p_playlist->b_reset_currently_playing = VLC_TRUE;
169
170
171
    playlist_LiveSearchUpdateInternal( p_playlist, p_root, psz_string );
    vlc_cond_signal( &p_playlist->object_wait );
    return VLC_SUCCESS;
172
}