]> git.sesse.net Git - vlc/blob - src/control/tag_query.c
f804e6abca61b872434d301d60d461d44dae4907
[vlc] / src / control / tag_query.c
1 /*****************************************************************************
2  * tag_query.c: libvlc new API media tag query functions
3  *****************************************************************************
4  * Copyright (C) 2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Pierre d'Herbemont <pdherbemont # 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 #include "libvlc_internal.h"
24 #include <vlc/libvlc.h>
25 #include "vlc_arrays.h"
26
27 /* XXX This API is in construction
28  * 
29  * It's goal is to represent a meta tag query
30  * It should be also able to say if a query can be matched in a media
31  * descriptor through libvlc_query_match.
32  */
33  
34 /*
35  * Public libvlc functions
36  */
37
38 /**************************************************************************
39  *       new (Public)
40  *
41  * Init an object.
42  **************************************************************************/
43 libvlc_tag_query_t *
44 libvlc_tag_query_new( libvlc_instance_t * p_inst,
45                       libvlc_exception_t * p_e )
46 {
47         (void)p_e;
48     libvlc_tag_query_t * p_q;
49
50         p_q = malloc(sizeof(libvlc_tag_query_t));
51
52         if( !p_q )
53                 return NULL;
54         
55         p_q->p_libvlc_instance = p_inst;
56     p_q->i_refcount = 1;
57     p_q->tag = NULL;
58     p_q->psz_tag_key = NULL;
59
60         return p_q;
61 }
62
63 /**************************************************************************
64  *       release (Public)
65  *
66  * Release an object.
67  **************************************************************************/
68 void libvlc_tag_query_release( libvlc_tag_query_t * p_q )
69 {
70     p_q->i_refcount--;
71
72     if( p_q->i_refcount > 0 )
73         return;
74
75     free( p_q->tag );
76     free( p_q->psz_tag_key );
77         
78         free( p_q );
79 }
80
81 /**************************************************************************
82  *       retain (Public)
83  *
84  * Release an object.
85  **************************************************************************/
86 void libvlc_tag_query_retain( libvlc_tag_query_t * p_q )
87 {
88         p_q->i_refcount++;
89 }
90
91 /**************************************************************************
92  *       set_match_tag_and_key (Public)
93  **************************************************************************/
94 void libvlc_tag_query_set_match_tag_and_key( libvlc_tag_query_t * p_q,
95                                              libvlc_tag_t tag,
96                                              char * psz_tag_key,
97                                              libvlc_exception_t * p_e )
98 {
99     (void)p_e;
100
101     p_q->tag = strdup( tag );
102     p_q->psz_tag_key = strdup( psz_tag_key );
103 }
104
105 /**************************************************************************
106  *       match (Public)
107  *
108  * Return true if the query p_q is matched in p_md
109  **************************************************************************/
110 vlc_bool_t
111 libvlc_tag_query_match( libvlc_tag_query_t * p_q,
112                         libvlc_media_descriptor_t * p_md,
113                         libvlc_exception_t * p_e )
114 {
115     int i;
116     struct libvlc_tags_storage_t * p_ts;
117     (void)p_e;
118     
119     if( !p_q->psz_tag_key )
120         return 1;
121
122     p_ts = vlc_dictionary_value_for_key( &p_md->tags, p_q->psz_tag_key );
123     if( !p_q->tag )
124         return p_ts->i_count > 0;
125
126     for( i = 0; i < p_ts->i_count; i++ )
127     {
128         if( !strcmp( p_ts->ppsz_tags[i], p_q->tag ) )
129             return 1;
130     }
131
132     /* In construction... */
133     return 0;
134 }