search.c 5.56 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 );
49
50
    if( i != -1 ) {
        if( !b_locked ) PL_UNLOCK;
zorglub's avatar
zorglub committed
51
        return ARRAY_VAL( p_playlist->all_items, i );
52
53
    }
    if( !b_locked ) PL_UNLOCK;
54
55
56
57
58
59
60
61
62
63
64
    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 ,
65
66
                                           input_item_t *p_item,
                                           vlc_bool_t b_locked )
67
68
{
    int i;
69
    if( !b_locked ) PL_LOCK;
70
71
    if( p_playlist->status.p_item &&
        p_playlist->status.p_item->p_input == p_item )
72
    {
73
        if( !b_locked ) PL_UNLOCK;
74
75
        return p_playlist->status.p_item;
    }
zorglub's avatar
zorglub committed
76
77
    /** \todo Check if this is always incremental and whether we can bsearch */
    for( i =  0 ; i < p_playlist->all_items.i_size; i++ )
78
    {
zorglub's avatar
zorglub committed
79
        if( ARRAY_VAL(p_playlist->all_items, i)->p_input->i_id == p_item->i_id )
80
81
        {
            if( !b_locked ) PL_UNLOCK;
zorglub's avatar
zorglub committed
82
            return ARRAY_VAL(p_playlist->all_items, i);
83
        }
84
    }
85
    if( !b_locked ) PL_UNLOCK;
86
87
88
    return NULL;
}

89
90
91
92
93
94
/** Find the playlist item matching the input id under the given node */
playlist_item_t * playlist_ItemGetByInputId( playlist_t *p_playlist,
                                             int i_input_id,
                                             playlist_item_t *p_root )
{
    int i;
95
    assert( p_root != NULL );
96
97
    for( i = 0 ; i< p_root->i_children ; i++ )
    {
98
        if( p_root->pp_children[i]->p_input &&
99
100
101
102
103
104
105
106
107
108
109
110
111
            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;
}

112
113
114
115
/***************************************************************************
 * Live search handling
 ***************************************************************************/

116
117
118
static vlc_bool_t playlist_LiveSearchUpdateInternal( playlist_t *p_playlist,
                                                     playlist_item_t *p_root,
                                                     const char *psz_string )
119
120
{
   int i;
121
122
   vlc_bool_t b_match = VLC_FALSE;
   for( i = 0 ; i < p_root->i_children ; i ++ )
123
   {
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
124

125
126
127
        playlist_item_t *p_item = p_root->pp_children[i];
        if( p_item->i_children > -1 )
        {
128
129
130
131
132
133
134
135
136
137
            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;
            }
138
139
        }
        else
140
        {
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
141
142
143
144
            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 ) )
            {
145
                p_item->i_flags &= ~PLAYLIST_DBL_FLAG;
146
                b_match = VLC_TRUE;
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
147
            }
148
            else
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
149
            {
150
                p_item->i_flags |= PLAYLIST_DBL_FLAG;
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
151
            }
152
        }
153
   }
154
155
156
157
158
159
160
161
162
163
   return b_match;
}

int playlist_LiveSearchUpdate( playlist_t *p_playlist, playlist_item_t *p_root,
                               const char *psz_string )
{
   p_playlist->b_reset_currently_playing = VLC_TRUE;
    playlist_LiveSearchUpdateInternal( p_playlist, p_root, psz_string );
    vlc_cond_signal( &p_playlist->object_wait );
    return VLC_SUCCESS;
164
}