search.c
上传用户:kjfoods
上传日期:2020-07-06
资源大小:29949k
文件大小:7k
源码类别:

midi

开发平台:

Unix_Linux

  1. /*****************************************************************************
  2.  * search.c : Search functions
  3.  *****************************************************************************
  4.  * Copyright (C) 1999-2009 the VideoLAN team
  5.  * $Id: 66bd3ad0929344f5861b4769eb48271362dc9b68 $
  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. #include <vlc_common.h>
  28. #include "vlc_playlist.h"
  29. #include "playlist_internal.h"
  30. /***************************************************************************
  31.  * Item search functions
  32.  ***************************************************************************/
  33. /**
  34.  * Search a playlist item by its playlist_item id.
  35.  * The playlist have to be locked
  36.  * @param p_playlist: the playlist
  37.  * @param i_id: the id to find
  38.  * @return the item or NULL on failure
  39.  */
  40. playlist_item_t* playlist_ItemGetById( playlist_t * p_playlist , int i_id )
  41. {
  42.     int i;
  43.     PL_ASSERT_LOCKED;
  44.     ARRAY_BSEARCH( p_playlist->all_items,->i_id, int, i_id, i );
  45.     if( i != -1 )
  46.         return ARRAY_VAL( p_playlist->all_items, i );
  47.     else
  48.         return NULL;
  49. }
  50. /**
  51.  * Search an item by its input_item_t
  52.  * The playlist have to be locked
  53.  * @param p_playlist: the playlist
  54.  * @param p_item: the input_item_t to find
  55.  * @return the item, or NULL on failure
  56.  */
  57. playlist_item_t* playlist_ItemGetByInput( playlist_t * p_playlist,
  58.                                           input_item_t *p_item )
  59. {
  60.     int i;
  61.     PL_ASSERT_LOCKED;
  62.     if( get_current_status_item( p_playlist ) &&
  63.         get_current_status_item( p_playlist )->p_input == p_item )
  64.     {
  65.         return get_current_status_item( p_playlist );
  66.     }
  67.     /** todo Check if this is always incremental and whether we can bsearch */
  68.     for( i =  0 ; i < p_playlist->all_items.i_size; i++ )
  69.     {
  70.         if( ARRAY_VAL(p_playlist->all_items, i)->p_input->i_id == p_item->i_id )
  71.         {
  72.             return ARRAY_VAL(p_playlist->all_items, i);
  73.         }
  74.     }
  75.     return NULL;
  76. }
  77. /**
  78.  * Get input by item id
  79.  *
  80.  * Find the playlist item matching the input id under the given node
  81.  * param p_playlist the playlist
  82.  * param i_input_id the id of the input to find
  83.  * param p_root the root node of the search
  84.  * return the playlist item or NULL on failure
  85.  */
  86. playlist_item_t * playlist_ItemGetByInputId( playlist_t *p_playlist,
  87.                                              int i_input_id,
  88.                                              playlist_item_t *p_root )
  89. {
  90.     int i;
  91.     PL_ASSERT_LOCKED;
  92.     assert( p_root != NULL );
  93.     for( i = 0 ; i< p_root->i_children ; i++ )
  94.     {
  95.         if( p_root->pp_children[i]->p_input &&
  96.             p_root->pp_children[i]->p_input->i_id == i_input_id )
  97.         {
  98.             return p_root->pp_children[i];
  99.         }
  100.         else if( p_root->pp_children[i]->i_children >= 0 )
  101.         {
  102.             return playlist_ItemGetByInputId( p_playlist, i_input_id,
  103.                                               p_root->pp_children[i] );
  104.         }
  105.     }
  106.     return NULL;
  107. }
  108. /***************************************************************************
  109.  * Live search handling
  110.  ***************************************************************************/
  111. /**
  112.  * Enable all items in the playlist
  113.  * @param p_root: the current root item
  114.  */
  115. static void playlist_LiveSearchClean( playlist_item_t *p_root )
  116. {
  117.     for( int i = 0; i < p_root->i_children; i++ )
  118.     {
  119.         playlist_item_t *p_item = p_root->pp_children[i];
  120.         if( p_item->i_children >= 0 )
  121.             playlist_LiveSearchClean( p_item );
  122.         p_item->i_flags &= ~PLAYLIST_DBL_FLAG;
  123.     }
  124. }
  125. /**
  126.  * Enable/Disable items in the playlist according to the search argument
  127.  * @param p_root: the current root item
  128.  * @param psz_string: the string to search
  129.  * @return true if an item match
  130.  */
  131. static bool playlist_LiveSearchUpdateInternal( playlist_item_t *p_root,
  132.                                                const char *psz_string )
  133. {
  134.     int i;
  135.     bool b_match = false;
  136.     for( i = 0 ; i < p_root->i_children ; i ++ )
  137.     {
  138.         bool b_enable = false;
  139.         playlist_item_t *p_item = p_root->pp_children[i];
  140.         // Go recurssively if their is some children
  141.         if( p_item->i_children >= 0 &&
  142.             playlist_LiveSearchUpdateInternal( p_item, psz_string ) )
  143.         {
  144.             b_enable = true;
  145.         }
  146.         if( !b_enable )
  147.         {
  148.             vlc_mutex_lock( &p_item->p_input->lock );
  149.             // Do we have some meta ?
  150.             if( p_item->p_input->p_meta )
  151.             {
  152.                 // Use Title or fall back to psz_name
  153.                 const char *psz_title = vlc_meta_Get( p_item->p_input->p_meta, vlc_meta_Title );
  154.                 if( !psz_title )
  155.                     psz_title = p_item->p_input->psz_name;
  156.                 const char *psz_album = vlc_meta_Get( p_item->p_input->p_meta, vlc_meta_Album );
  157.                 const char *psz_artist = vlc_meta_Get( p_item->p_input->p_meta, vlc_meta_Artist );
  158.                 b_enable = ( psz_title && strcasestr( psz_title, psz_string ) ) ||
  159.                            ( psz_album && strcasestr( psz_album, psz_string ) ) ||
  160.                            ( psz_artist && strcasestr( psz_artist, psz_string ) );
  161.             }
  162.             else
  163.                 b_enable = p_item->p_input->psz_name && strcasestr( p_item->p_input->psz_name, psz_string );
  164.             vlc_mutex_unlock( &p_item->p_input->lock );
  165.         }
  166.         if( b_enable )
  167.             p_item->i_flags &= ~PLAYLIST_DBL_FLAG;
  168.         else
  169.             p_item->i_flags |= PLAYLIST_DBL_FLAG;
  170.         b_match |= b_enable;
  171.    }
  172.    return b_match;
  173. }
  174. /**
  175.  * Launch the recursive search in the playlist
  176.  * @param p_playlist: the playlist
  177.  * @param p_root: the current root item
  178.  * @param psz_string: the string to find
  179.  * @return VLC_SUCCESS
  180.  */
  181. int playlist_LiveSearchUpdate( playlist_t *p_playlist, playlist_item_t *p_root,
  182.                                const char *psz_string )
  183. {
  184.     PL_ASSERT_LOCKED;
  185.     pl_priv(p_playlist)->b_reset_currently_playing = true;
  186.     if( *psz_string )
  187.         playlist_LiveSearchUpdateInternal( p_root, psz_string );
  188.     else
  189.         playlist_LiveSearchClean( p_root );
  190.     vlc_cond_signal( &pl_priv(p_playlist)->signal );
  191.     return VLC_SUCCESS;
  192. }