]> git.sesse.net Git - vlc/blob - src/input/meta.c
Use VLC object for meta writer and factor code
[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
33 #include "input_internal.h"
34 #include "../playlist/art.h"
35
36 /* FIXME bad name convention */
37 const char * input_MetaTypeToLocalizedString( vlc_meta_type_t meta_type )
38 {
39     switch( meta_type )
40     {
41     case vlc_meta_Title:        return _("Title");
42     case vlc_meta_Artist:       return _("Artist");
43     case vlc_meta_Genre:        return _("Genre");
44     case vlc_meta_Copyright:    return _("Copyright");
45     case vlc_meta_Album:        return _("Album");
46     case vlc_meta_TrackNumber:  return _("Track number");
47     case vlc_meta_Description:  return _("Description");
48     case vlc_meta_Rating:       return _("Rating");
49     case vlc_meta_Date:         return _("Date");
50     case vlc_meta_Setting:      return _("Setting");
51     case vlc_meta_URL:          return _("URL");
52     case vlc_meta_Language:     return _("Language");
53     case vlc_meta_NowPlaying:   return _("Now Playing");
54     case vlc_meta_Publisher:    return _("Publisher");
55     case vlc_meta_EncodedBy:    return _("Encoded by");
56     case vlc_meta_ArtworkURL:   return _("Artwork URL");
57     case vlc_meta_TrackID:      return _("Track ID");
58
59     default: abort();
60     }
61 };
62
63 void input_ExtractAttachmentAndCacheArt( input_thread_t *p_input )
64 {
65     input_item_t *p_item = p_input->p->p_item;
66
67     /* */
68     char *psz_arturl = input_item_GetArtURL( p_item );
69     if( !psz_arturl || strncmp( psz_arturl, "attachment://", strlen("attachment://") ) )
70     {
71         msg_Err( p_input, "internal input error with input_ExtractAttachmentAndCacheArt" );
72         free( psz_arturl );
73         return;
74     }
75
76     playlist_t *p_playlist = pl_Hold( p_input );
77     if( !p_playlist )
78     {
79         free( psz_arturl );
80         return;
81     }
82
83
84     if( input_item_IsArtFetched( p_item ) )
85     {
86         /* XXX Weird, we should not have end up with attachment:// art url unless there is a race
87          * condition */
88         msg_Warn( p_input, "internal input error with input_ExtractAttachmentAndCacheArt" );
89         playlist_FindArtInCache( p_item );
90         goto exit;
91     }
92
93     /* */
94     input_attachment_t *p_attachment = NULL;
95
96     vlc_mutex_lock( &p_item->lock );
97     for( int i_idx = 0; i_idx < p_input->p->i_attachment; i_idx++ )
98     {
99         if( !strcmp( p_input->p->attachment[i_idx]->psz_name,
100                      &psz_arturl[strlen("attachment://")] ) )
101         {
102             p_attachment = vlc_input_attachment_Duplicate( p_input->p->attachment[i_idx] );
103             break;
104         }
105     }
106     vlc_mutex_unlock( &p_item->lock );
107
108     if( !p_attachment || p_attachment->i_data <= 0 )
109     {
110         if( p_attachment )
111             vlc_input_attachment_Delete( p_attachment );
112         msg_Warn( p_input, "internal input error with input_ExtractAttachmentAndCacheArt" );
113         goto exit;
114     }
115
116     /* */
117     const char *psz_type = NULL;
118     if( !strcmp( p_attachment->psz_mime, "image/jpeg" ) )
119         psz_type = ".jpg";
120     else if( !strcmp( p_attachment->psz_mime, "image/png" ) )
121         psz_type = ".png";
122
123     /* */
124     playlist_SaveArt( p_playlist, p_item,
125                       p_attachment->p_data, p_attachment->i_data, psz_type );
126
127     vlc_input_attachment_Delete( p_attachment );
128
129 exit:
130     pl_Release( p_input );
131     free( psz_arturl );
132 }
133
134 int input_item_WriteMeta( vlc_object_t *obj, input_item_t *p_item )
135 {
136     meta_export_t *p_export =
137         vlc_custom_create( obj, sizeof( *p_export ), VLC_OBJECT_GENERIC,
138                            "meta writer" );
139     if( p_export == NULL )
140         return VLC_ENOMEM;
141     vlc_object_attach( p_export, obj );
142     p_export->p_item = p_item;
143
144     int type;
145     vlc_mutex_lock( &p_item->lock );
146     type = p_item->i_type;
147     vlc_mutex_unlock( &p_item->lock );
148     if( type != ITEM_TYPE_FILE )
149     {
150         char *psz_uri = input_item_GetURI( p_item );
151
152 #warning FIXME: function for URI->path conversion!
153         decode_URI( psz_uri );
154         if( !strncmp( psz_uri, "file://", 7 ) )
155         {
156             p_export->psz_file = strdup( psz_uri + 7 );
157             free( psz_uri );
158         }
159         else
160 #warning This should not happen!
161             p_export->psz_file = psz_uri;
162     }
163     else
164     {
165         vlc_object_release( p_export );
166         return VLC_EGENERIC;
167     }
168
169     module_t *p_mod = module_need( p_export, "meta writer", NULL, false );
170     if( p_mod )
171         module_unneed( p_export, p_mod );
172     vlc_object_release( p_export );
173     return VLC_SUCCESS;
174 }