]> git.sesse.net Git - vlc/blob - src/input/meta.c
input_item_WriteMeta: use make_path()
[vlc] / src / input / meta.c
1 /*****************************************************************************
2  * meta.c : Metadata handling
3  *****************************************************************************
4  * Copyright (C) 1998-2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Antoine Cellerier <dionoea@videolan.org>
8  *          ClĂ©ment Stenac <zorglub@videolan.org
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <vlc_common.h>
30 #include <vlc_playlist.h>
31 #include <vlc_url.h>
32 #include <vlc_arrays.h>
33
34 #include "input_internal.h"
35 #include "../playlist/art.h"
36
37 struct vlc_meta_t
38 {
39     char * ppsz_meta[VLC_META_TYPE_COUNT];
40     
41     vlc_dictionary_t extra_tags;
42     
43     int i_status;
44 };
45
46 /* FIXME bad name convention */
47 const char * vlc_meta_TypeToLocalizedString( vlc_meta_type_t meta_type )
48 {
49     switch( meta_type )
50     {
51     case vlc_meta_Title:        return _("Title");
52     case vlc_meta_Artist:       return _("Artist");
53     case vlc_meta_Genre:        return _("Genre");
54     case vlc_meta_Copyright:    return _("Copyright");
55     case vlc_meta_Album:        return _("Album");
56     case vlc_meta_TrackNumber:  return _("Track number");
57     case vlc_meta_Description:  return _("Description");
58     case vlc_meta_Rating:       return _("Rating");
59     case vlc_meta_Date:         return _("Date");
60     case vlc_meta_Setting:      return _("Setting");
61     case vlc_meta_URL:          return _("URL");
62     case vlc_meta_Language:     return _("Language");
63     case vlc_meta_NowPlaying:   return _("Now Playing");
64     case vlc_meta_Publisher:    return _("Publisher");
65     case vlc_meta_EncodedBy:    return _("Encoded by");
66     case vlc_meta_ArtworkURL:   return _("Artwork URL");
67     case vlc_meta_TrackID:      return _("Track ID");
68
69     default: abort();
70     }
71 };
72
73
74 /**
75  * vlc_meta contructor.
76  * vlc_meta_Delete() will free the returned pointer.
77  */ 
78 vlc_meta_t *vlc_meta_New( void )
79 {
80     vlc_meta_t *m = (vlc_meta_t*)malloc( sizeof(*m) );
81     if( !m )
82         return NULL;
83     memset( m->ppsz_meta, 0, sizeof(m->ppsz_meta) );
84     m->i_status = 0;
85     vlc_dictionary_init( &m->extra_tags, 0 );
86     return m;
87 }
88
89 /* Free a dictonary key allocated by strdup() in vlc_meta_AddExtra() */
90 static void vlc_meta_FreeExtraKey( void *p_data, void *p_obj )
91 {
92     VLC_UNUSED( p_obj );
93     free( p_data );
94 }
95
96 void vlc_meta_Delete( vlc_meta_t *m )
97 {
98     int i;
99     for( i = 0; i < VLC_META_TYPE_COUNT ; i++ )
100         free( m->ppsz_meta[i] );
101     vlc_dictionary_clear( &m->extra_tags, vlc_meta_FreeExtraKey, NULL );
102     free( m );
103 }
104
105 /**
106  * vlc_meta has two kinds of meta, the one in a table, and the one in a
107  * dictionary.
108  * FIXME - Why don't we merge those two?
109  */ 
110
111 void vlc_meta_Set( vlc_meta_t *p_meta, vlc_meta_type_t meta_type, const char *psz_val )
112 {
113     free( p_meta->ppsz_meta[meta_type] );
114     p_meta->ppsz_meta[meta_type] = psz_val ? strdup( psz_val ) : NULL;
115 }
116
117 const char *vlc_meta_Get( const vlc_meta_t *p_meta, vlc_meta_type_t meta_type )
118 {
119     return p_meta->ppsz_meta[meta_type];
120 }
121
122 void vlc_meta_AddExtra( vlc_meta_t *m, const char *psz_name, const char *psz_value )
123 {
124     char *psz_oldvalue = (char *)vlc_dictionary_value_for_key( &m->extra_tags, psz_name );
125     if( psz_oldvalue != kVLCDictionaryNotFound )
126         vlc_dictionary_remove_value_for_key( &m->extra_tags, psz_name,
127                                             vlc_meta_FreeExtraKey, NULL );
128     vlc_dictionary_insert( &m->extra_tags, psz_name, strdup(psz_value) );
129 }
130
131 const char * vlc_meta_GetExtra( const vlc_meta_t *m, const char *psz_name )
132 {
133     return (char *)vlc_dictionary_value_for_key(&m->extra_tags, psz_name);
134 }
135
136 unsigned vlc_meta_GetExtraCount( const vlc_meta_t *m )
137 {
138     return vlc_dictionary_keys_count(&m->extra_tags);
139 }
140
141 char** vlc_meta_CopyExtraNames( const vlc_meta_t *m )
142 {
143     return vlc_dictionary_all_keys(&m->extra_tags);
144 }
145
146 /**
147  * vlc_meta status (see vlc_meta_status_e)
148  */
149 int vlc_meta_GetStatus( vlc_meta_t *m )
150 {
151     return m->i_status;
152 }
153
154 void vlc_meta_SetStatus( vlc_meta_t *m, int status )
155 {
156     m->i_status = status;
157 }
158
159
160 /**
161  * Merging meta
162  */
163 void vlc_meta_Merge( vlc_meta_t *dst, const vlc_meta_t *src )
164 {
165     char **ppsz_all_keys;
166     int i;
167     
168     if( !dst || !src )
169         return;
170     
171     for( i = 0; i < VLC_META_TYPE_COUNT; i++ )
172     {
173         if( src->ppsz_meta[i] )
174         {
175             free( dst->ppsz_meta[i] );
176             dst->ppsz_meta[i] = strdup( src->ppsz_meta[i] );
177         }
178     }
179     
180     /* XXX: If speed up are needed, it is possible */
181     ppsz_all_keys = vlc_dictionary_all_keys( &src->extra_tags );
182     for( i = 0; ppsz_all_keys && ppsz_all_keys[i]; i++ )
183     {
184         /* Always try to remove the previous value */
185         vlc_dictionary_remove_value_for_key( &dst->extra_tags, ppsz_all_keys[i], vlc_meta_FreeExtraKey, NULL );
186         
187         void *p_value = vlc_dictionary_value_for_key( &src->extra_tags, ppsz_all_keys[i] );
188         vlc_dictionary_insert( &dst->extra_tags, ppsz_all_keys[i], strdup( (const char*)p_value ) );
189         free( ppsz_all_keys[i] );
190     }
191     free( ppsz_all_keys );
192 }
193
194
195 void input_ExtractAttachmentAndCacheArt( input_thread_t *p_input )
196 {
197     input_item_t *p_item = p_input->p->p_item;
198
199     /* */
200     char *psz_arturl = input_item_GetArtURL( p_item );
201     if( !psz_arturl || strncmp( psz_arturl, "attachment://", strlen("attachment://") ) )
202     {
203         msg_Err( p_input, "internal input error with input_ExtractAttachmentAndCacheArt" );
204         free( psz_arturl );
205         return;
206     }
207
208     playlist_t *p_playlist = pl_Hold( p_input );
209     if( !p_playlist )
210     {
211         free( psz_arturl );
212         return;
213     }
214
215
216     if( input_item_IsArtFetched( p_item ) )
217     {
218         /* XXX Weird, we should not have end up with attachment:// art url unless there is a race
219          * condition */
220         msg_Warn( p_input, "internal input error with input_ExtractAttachmentAndCacheArt" );
221         playlist_FindArtInCache( p_item );
222         goto exit;
223     }
224
225     /* */
226     input_attachment_t *p_attachment = NULL;
227
228     vlc_mutex_lock( &p_item->lock );
229     for( int i_idx = 0; i_idx < p_input->p->i_attachment; i_idx++ )
230     {
231         if( !strcmp( p_input->p->attachment[i_idx]->psz_name,
232                      &psz_arturl[strlen("attachment://")] ) )
233         {
234             p_attachment = vlc_input_attachment_Duplicate( p_input->p->attachment[i_idx] );
235             break;
236         }
237     }
238     vlc_mutex_unlock( &p_item->lock );
239
240     if( !p_attachment || p_attachment->i_data <= 0 )
241     {
242         if( p_attachment )
243             vlc_input_attachment_Delete( p_attachment );
244         msg_Warn( p_input, "internal input error with input_ExtractAttachmentAndCacheArt" );
245         goto exit;
246     }
247
248     /* */
249     const char *psz_type = NULL;
250     if( !strcmp( p_attachment->psz_mime, "image/jpeg" ) )
251         psz_type = ".jpg";
252     else if( !strcmp( p_attachment->psz_mime, "image/png" ) )
253         psz_type = ".png";
254
255     /* */
256     playlist_SaveArt( p_playlist, p_item,
257                       p_attachment->p_data, p_attachment->i_data, psz_type );
258
259     vlc_input_attachment_Delete( p_attachment );
260
261 exit:
262     pl_Release( p_input );
263     free( psz_arturl );
264 }
265
266 int input_item_WriteMeta( vlc_object_t *obj, input_item_t *p_item )
267 {
268     meta_export_t *p_export =
269         vlc_custom_create( obj, sizeof( *p_export ), VLC_OBJECT_GENERIC,
270                            "meta writer" );
271     if( p_export == NULL )
272         return VLC_ENOMEM;
273     vlc_object_attach( p_export, obj );
274     p_export->p_item = p_item;
275
276     int type;
277     vlc_mutex_lock( &p_item->lock );
278     type = p_item->i_type;
279     vlc_mutex_unlock( &p_item->lock );
280     if( type != ITEM_TYPE_FILE )
281         goto error;
282
283     char *psz_uri = input_item_GetURI( p_item );
284     p_export->psz_file = make_path( psz_uri );
285     if( p_export->psz_file == NULL )
286         msg_Err( p_export, "cannot write meta to remote media %s", psz_uri );
287     free( psz_uri );
288     if( p_export->psz_file == NULL )
289         goto error;
290
291     module_t *p_mod = module_need( p_export, "meta writer", NULL, false );
292     if( p_mod )
293         module_unneed( p_export, p_mod );
294     vlc_object_release( p_export );
295     return VLC_SUCCESS;
296
297 error:
298     vlc_object_release( p_export );
299     return VLC_EGENERIC;
300 }