1 /*****************************************************************************
2 * meta.c : Metadata handling
3 *****************************************************************************
4 * Copyright (C) 1998-2004 the VideoLAN team
7 * Authors: Antoine Cellerier <dionoea@videolan.org>
8 * Clément Stenac <zorglub@videolan.org
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.
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.
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 *****************************************************************************/
26 #include <vlc/input.h>
28 #include "vlc_playlist.h"
31 #ifdef HAVE_SYS_STAT_H
32 # include <sys/stat.h>
35 int input_FindArtInCache( playlist_t *p_playlist, input_item_t *p_item );
37 vlc_bool_t input_MetaSatisfied( playlist_t *p_playlist, input_item_t *p_item,
38 uint32_t *pi_mandatory, uint32_t *pi_optional,
39 vlc_bool_t b_check_cache )
41 // FIXME don't var_Stuff at each loop
42 int i_policy = var_CreateGetInteger( p_playlist, "album-art" );
44 input_FindArtInCache( p_playlist, p_item );
46 *pi_mandatory = VLC_META_ENGINE_TITLE | VLC_META_ENGINE_ARTIST |
47 (i_policy == ALBUM_ART_ALL ? VLC_META_ENGINE_ART_URL : 0 );
49 uint32_t i_meta = input_CurrentMetaFlags( p_item->p_meta );
50 *pi_mandatory &= ~i_meta;
51 *pi_optional = 0; /// Todo
52 return *pi_mandatory ? VLC_FALSE:VLC_TRUE;
55 int input_MetaFetch( playlist_t *p_playlist, input_item_t *p_item )
57 struct meta_engine_t *p_me;
58 uint32_t i_mandatory, i_optional;
60 if( !p_item->p_meta ) return VLC_EGENERIC;
62 input_MetaSatisfied( p_playlist, p_item,
63 &i_mandatory, &i_optional, VLC_FALSE );
65 // Meta shouldn't magically appear
66 assert( i_mandatory );
68 p_me = vlc_object_create( p_playlist, VLC_OBJECT_META_ENGINE );
69 p_me->i_flags |= OBJECT_FLAGS_NOINTERACT;
70 p_me->i_mandatory = i_mandatory;
71 p_me->i_optional = i_optional;
73 p_me->p_item = p_item;
74 p_me->p_module = module_Need( p_me, "meta fetcher", 0, VLC_FALSE );
75 vlc_object_attach( p_me, p_playlist );
78 msg_Dbg( p_playlist, "unable to fetch meta" );
79 vlc_object_detach( p_me );
80 vlc_object_destroy( p_me );
84 module_Unneed( p_me, p_me->p_module );
86 vlc_object_detach( p_me );
87 vlc_object_destroy( p_me );
92 int input_ArtFetch( playlist_t *p_playlist, input_item_t *p_item )
94 if( !p_item->p_meta ) return VLC_EGENERIC;
96 if( !p_item->p_meta->psz_arturl || !*p_item->p_meta->psz_arturl )
100 p_playlist->p_private = p_item;
101 p_module = module_Need( p_playlist, "art finder", 0, VLC_FALSE );
104 msg_Dbg( p_playlist, "unable to find art" );
108 module_Unneed( p_playlist, p_module );
109 p_playlist->p_private = NULL;
112 if( !p_item->p_meta->psz_arturl || !*p_item->p_meta->psz_arturl )
115 return input_DownloadAndCacheArt( p_playlist, p_item );
119 # define MAX_PATH 250
121 int input_FindArtInCache( playlist_t *p_playlist, input_item_t *p_item )
125 char psz_filename[MAX_PATH];
128 const char *ppsz_type[] = { ".jpg", ".png", ".gif", ".bmp", "" };
130 if( !p_item->p_meta ) return VLC_EGENERIC;
132 psz_artist = p_item->p_meta->psz_artist;
133 psz_album = p_item->p_meta->psz_album;
135 for( i = 0; i < 5; i++ )
137 snprintf( psz_filename, MAX_PATH,
138 "file://%s" DIR_SEP CONFIG_DIR DIR_SEP "art"
139 DIR_SEP "%s" DIR_SEP "%s" DIR_SEP "art%s",
140 p_playlist->p_libvlc->psz_homedir,
141 psz_artist, psz_album, ppsz_type[i] );
143 /* Check if file exists */
144 if( utf8_stat( psz_filename+7, &a ) == 0 )
146 vlc_meta_SetArtURL( p_item->p_meta, psz_filename );
155 * Download the art using the URL or an art downloaded
156 * This function should be called only if data is not already in cache
158 int input_DownloadAndCacheArt( playlist_t *p_playlist, input_item_t *p_item )
160 int i_status = VLC_EGENERIC;
162 char psz_filename[MAX_PATH], psz_dir[MAX_PATH];
166 psz_artist = p_item->p_meta->psz_artist;
167 psz_album = p_item->p_meta->psz_album;
169 /* You dummy ! How am I supposed to download NULL ? */
170 if( !p_item->p_meta || !p_item->p_meta->psz_arturl
171 || !*p_item->p_meta->psz_arturl )
174 psz_type = strrchr( p_item->p_meta->psz_arturl, '.' );
176 /* Todo: get a helper to do this */
177 snprintf( psz_filename, MAX_PATH,
178 "file://%s" DIR_SEP CONFIG_DIR DIR_SEP "art"
179 DIR_SEP "%s" DIR_SEP "%s" DIR_SEP "art%s",
180 p_playlist->p_libvlc->psz_homedir,
181 psz_artist, psz_album, psz_type );
183 snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR,
184 p_playlist->p_libvlc->psz_homedir );
185 utf8_mkdir( psz_dir );
186 snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP "art",
187 p_playlist->p_libvlc->psz_homedir );
188 utf8_mkdir( psz_dir );
189 snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP
191 p_playlist->p_libvlc->psz_homedir, psz_artist );
192 utf8_mkdir( psz_dir );
193 snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP
194 "art" DIR_SEP "%s" DIR_SEP "%s",
195 p_playlist->p_libvlc->psz_homedir,
196 psz_artist, psz_album );
197 utf8_mkdir( psz_dir );
199 /* Todo: check for stuff that needs a downloader module */
200 p_stream = stream_UrlNew( p_playlist, p_item->p_meta->psz_arturl );
204 void *p_buffer = malloc( 1<<16 );
206 FILE *p_file = utf8_fopen( psz_filename+7, "w" );
207 while( ( l_read = stream_Read( p_stream, p_buffer, 1<<16 ) ) )
209 fwrite( p_buffer, l_read, 1, p_file );
213 stream_Delete( p_stream );
214 msg_Dbg( p_playlist, "album art saved to %s\n", psz_filename );
215 free( p_item->p_meta->psz_arturl );
216 p_item->p_meta->psz_arturl = strdup( psz_filename );
217 i_status = VLC_SUCCESS;
222 uint32_t input_CurrentMetaFlags( vlc_meta_t *p_meta )
226 #define CHECK( a, b ) \
227 if( p_meta->psz_ ## a && *p_meta->psz_ ## a ) \
228 i_meta |= VLC_META_ENGINE_ ## b;
230 CHECK( title, TITLE )
231 CHECK( artist, ARTIST )
232 CHECK( genre, GENRE )
233 CHECK( copyright, COPYRIGHT )
234 CHECK( album, COLLECTION )
235 CHECK( tracknum, SEQ_NUM )
236 CHECK( description, DESCRIPTION )
237 CHECK( rating, RATING )
240 CHECK( language, LANGUAGE )
241 CHECK( arturl, ART_URL )