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>
27 #include <vlc_stream.h>
29 #include <vlc_playlist.h>
30 #include <vlc_charset.h>
31 #include "../playlist/playlist_internal.h"
33 #ifdef HAVE_SYS_STAT_H
34 # include <sys/stat.h>
37 int input_FindArtInCache( playlist_t *p_playlist, input_item_t *p_item );
39 vlc_bool_t input_MetaSatisfied( playlist_t *p_playlist, input_item_t *p_item,
40 uint32_t *pi_mandatory, uint32_t *pi_optional )
43 *pi_mandatory = VLC_META_ENGINE_TITLE | VLC_META_ENGINE_ARTIST;
44 assert( p_item->p_meta );
46 uint32_t i_meta = input_CurrentMetaFlags( p_item->p_meta );
47 *pi_mandatory &= ~i_meta;
48 *pi_optional = 0; /// Todo
49 return *pi_mandatory ? VLC_FALSE:VLC_TRUE;
52 int input_MetaFetch( playlist_t *p_playlist, input_item_t *p_item )
54 struct meta_engine_t *p_me;
55 uint32_t i_mandatory, i_optional;
57 assert( p_item->p_meta );
59 input_MetaSatisfied( p_playlist, p_item, &i_mandatory, &i_optional );
60 // Meta shouldn't magically appear
61 assert( i_mandatory );
63 /* FIXME: object creation is overkill, use p_private */
64 p_me = vlc_object_create( p_playlist, VLC_OBJECT_META_ENGINE );
65 p_me->i_flags |= OBJECT_FLAGS_NOINTERACT;
66 p_me->i_flags |= OBJECT_FLAGS_QUIET;
67 p_me->i_mandatory = i_mandatory;
68 p_me->i_optional = i_optional;
70 p_me->p_item = p_item;
71 p_me->p_module = module_Need( p_me, "meta fetcher", 0, VLC_FALSE );
74 vlc_object_destroy( p_me );
77 module_Unneed( p_me, p_me->p_module );
78 vlc_object_destroy( p_me );
84 * 1 : Art found, need to download
85 * -X : Error/not found
87 int input_ArtFind( playlist_t *p_playlist, input_item_t *p_item )
89 int i_ret = VLC_EGENERIC;
91 if( !p_item->p_meta || !p_item->p_meta->psz_album ||
92 !p_item->p_meta->psz_artist )
95 /* If we already checked this album in this session, skip */
96 FOREACH_ARRAY( playlist_album_t album, p_playlist->p_fetcher->albums )
97 if( !strcmp( album.psz_artist, p_item->p_meta->psz_artist ) &&
98 !strcmp( album.psz_album, p_item->p_meta->psz_album ) )
100 msg_Dbg( p_playlist, " %s - %s has already been searched",
101 p_item->p_meta->psz_artist, p_item->p_meta->psz_album );
104 /* Actually get URL from cache */
105 input_FindArtInCache( p_playlist, p_item );
113 input_FindArtInCache( p_playlist, p_item );
114 if( !EMPTY_STR( p_item->p_meta->psz_arturl ) )
118 p_playlist->p_private = p_item;
119 msg_Dbg( p_playlist, "searching art for %s - %s",
120 p_item->p_meta->psz_artist, p_item->p_meta->psz_album );
121 p_module = module_Need( p_playlist, "art finder", 0, VLC_FALSE );
126 msg_Dbg( p_playlist, "unable to find art" );
128 /* Record this album */
130 a.psz_artist = strdup( p_item->p_meta->psz_artist );
131 a.psz_album = strdup( p_item->p_meta->psz_album );
132 a.b_found = (i_ret == VLC_EGENERIC ? VLC_FALSE : VLC_TRUE );
133 ARRAY_APPEND( p_playlist->p_fetcher->albums, a );
136 module_Unneed( p_playlist, p_module );
137 p_playlist->p_private = NULL;
144 # define MAX_PATH 250
146 int input_FindArtInCache( playlist_t *p_playlist, input_item_t *p_item )
150 char psz_filename[MAX_PATH+1];
153 const char *ppsz_type[] = { ".jpg", ".png", ".gif", ".bmp", "" };
155 if( !p_item->p_meta ) return VLC_EGENERIC;
157 psz_artist = p_item->p_meta->psz_artist;
158 psz_album = p_item->p_meta->psz_album;
160 for( i = 0; i < 5; i++ )
162 snprintf( psz_filename, MAX_PATH,
163 "file://%s" DIR_SEP CONFIG_DIR DIR_SEP "art"
164 DIR_SEP "%s" DIR_SEP "%s" DIR_SEP "art%s",
165 p_playlist->p_libvlc->psz_homedir,
166 psz_artist, psz_album, ppsz_type[i] );
168 /* Check if file exists */
169 if( utf8_stat( psz_filename+7, &a ) == 0 )
171 vlc_meta_SetArtURL( p_item->p_meta, psz_filename );
179 * Download the art using the URL or an art downloaded
180 * This function should be called only if data is not already in cache
182 int input_DownloadAndCacheArt( playlist_t *p_playlist, input_item_t *p_item )
184 int i_status = VLC_EGENERIC;
186 char psz_filename[MAX_PATH+1], psz_dir[MAX_PATH+1];
187 char *psz_artist = NULL;
188 char *psz_album = NULL;
191 if( p_item->p_meta->psz_artist )
192 psz_artist = strdup( p_item->p_meta->psz_artist );
193 if( p_item->p_meta->psz_album )
194 psz_album = strdup( p_item->p_meta->psz_album );
196 assert( p_item->p_meta && !EMPTY_STR(p_item->p_meta->psz_arturl) );
198 /* FIXME: use an alternate saving filename scheme if we don't have
199 * the artist or album name */
200 if( !p_item->p_meta->psz_artist || !p_item->p_meta->psz_album )
207 /* Doesn't create a filename with invalid characters
208 * TODO: several filesystems forbid several characters: list them all
210 for( i = 0 ; i < strlen( psz_artist ) ; i++ )
211 if( psz_artist[i] == '/' )
214 for( i = 0 ; i < strlen( psz_album ) ; i++ )
215 if( psz_album[i] == '/' )
218 psz_type = strrchr( p_item->p_meta->psz_arturl, '.' );
220 /* Todo: get a helper to do this */
221 snprintf( psz_filename, MAX_PATH,
222 "file://%s" DIR_SEP CONFIG_DIR DIR_SEP "art"
223 DIR_SEP "%s" DIR_SEP "%s" DIR_SEP "art%s",
224 p_playlist->p_libvlc->psz_homedir,
225 psz_artist, psz_album, psz_type );
227 snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR,
228 p_playlist->p_libvlc->psz_homedir );
229 utf8_mkdir( psz_dir );
230 snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP "art",
231 p_playlist->p_libvlc->psz_homedir );
232 utf8_mkdir( psz_dir );
233 snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP
235 p_playlist->p_libvlc->psz_homedir, psz_artist );
236 utf8_mkdir( psz_dir );
237 snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP
238 "art" DIR_SEP "%s" DIR_SEP "%s",
239 p_playlist->p_libvlc->psz_homedir,
240 psz_artist, psz_album );
241 utf8_mkdir( psz_dir );
243 if( !strncmp( p_item->p_meta->psz_arturl , "APIC", 4 ) )
245 msg_Warn( p_playlist, "APIC fetch not supported yet" );
251 p_stream = stream_UrlNew( p_playlist, p_item->p_meta->psz_arturl );
254 uint8_t p_buffer[65536];
256 FILE *p_file = utf8_fopen( psz_filename+7, "w" );
258 while( ( l_read = stream_Read( p_stream, p_buffer, sizeof (p_buffer) ) ) )
260 if( fwrite( p_buffer, l_read, 1, p_file ) != 1 )
266 if( fclose( p_file ) && !err )
268 stream_Delete( p_stream );
269 free( p_item->p_meta->psz_arturl );
272 msg_Err( p_playlist, "%s: %s", psz_filename, strerror( err ) );
274 msg_Dbg( p_playlist, "album art saved to %s\n", psz_filename );
276 p_item->p_meta->psz_arturl = strdup( psz_filename );
277 i_status = VLC_SUCCESS;
284 uint32_t input_CurrentMetaFlags( vlc_meta_t *p_meta )
288 #define CHECK( a, b ) \
289 if( p_meta->psz_ ## a && *p_meta->psz_ ## a ) \
290 i_meta |= VLC_META_ENGINE_ ## b;
292 CHECK( title, TITLE )
293 CHECK( artist, ARTIST )
294 CHECK( album, COLLECTION )
296 /* As this is not used at the moment, don't uselessly check for it.
297 * Re-enable this when it is used */
298 CHECK( genre, GENRE )
299 CHECK( copyright, COPYRIGHT )
300 CHECK( tracknum, SEQ_NUM )
301 CHECK( description, DESCRIPTION )
302 CHECK( rating, RATING )
305 CHECK( language, LANGUAGE )
307 CHECK( arturl, ART_URL )