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_flags |= OBJECT_FLAGS_QUIET;
71 p_me->i_mandatory = i_mandatory;
72 p_me->i_optional = i_optional;
74 p_me->p_item = p_item;
75 p_me->p_module = module_Need( p_me, "meta fetcher", 0, VLC_FALSE );
78 vlc_object_destroy( p_me );
82 module_Unneed( p_me, p_me->p_module );
83 vlc_object_destroy( p_me );
88 int input_ArtFetch( playlist_t *p_playlist, input_item_t *p_item )
90 if( !p_item->p_meta ) return VLC_EGENERIC;
92 if( !p_item->p_meta->psz_arturl || !*p_item->p_meta->psz_arturl )
96 p_playlist->p_private = p_item;
97 p_module = module_Need( p_playlist, "art finder", 0, VLC_FALSE );
100 msg_Dbg( p_playlist, "unable to find art" );
104 module_Unneed( p_playlist, p_module );
105 p_playlist->p_private = NULL;
108 if( !p_item->p_meta->psz_arturl || !*p_item->p_meta->psz_arturl )
111 return input_DownloadAndCacheArt( p_playlist, p_item );
115 # define MAX_PATH 250
117 int input_FindArtInCache( playlist_t *p_playlist, input_item_t *p_item )
121 char psz_filename[MAX_PATH];
124 const char *ppsz_type[] = { ".jpg", ".png", ".gif", ".bmp", "" };
126 if( !p_item->p_meta ) return VLC_EGENERIC;
128 psz_artist = p_item->p_meta->psz_artist;
129 psz_album = p_item->p_meta->psz_album;
131 for( i = 0; i < 5; i++ )
133 snprintf( psz_filename, MAX_PATH,
134 "file://%s" DIR_SEP CONFIG_DIR DIR_SEP "art"
135 DIR_SEP "%s" DIR_SEP "%s" DIR_SEP "art%s",
136 p_playlist->p_libvlc->psz_homedir,
137 psz_artist, psz_album, ppsz_type[i] );
139 /* Check if file exists */
140 if( utf8_stat( psz_filename+7, &a ) == 0 )
142 vlc_meta_SetArtURL( p_item->p_meta, psz_filename );
151 * Download the art using the URL or an art downloaded
152 * This function should be called only if data is not already in cache
154 int input_DownloadAndCacheArt( playlist_t *p_playlist, input_item_t *p_item )
156 int i_status = VLC_EGENERIC;
158 char psz_filename[MAX_PATH], psz_dir[MAX_PATH];
162 psz_artist = p_item->p_meta->psz_artist;
163 psz_album = p_item->p_meta->psz_album;
165 /* You dummy ! How am I supposed to download NULL ? */
166 if( !p_item->p_meta || !p_item->p_meta->psz_arturl
167 || !*p_item->p_meta->psz_arturl )
170 /* FIXME: use an alternate saving filename scheme if we don't have
171 * the artist or album name */
172 if( !p_item->p_meta->psz_artist
173 || !p_item->p_meta->psz_album )
176 /* Check if file doesn't already exist */
177 if( input_FindArtInCache( p_playlist, p_item ) == VLC_SUCCESS )
180 psz_type = strrchr( p_item->p_meta->psz_arturl, '.' );
182 /* Todo: get a helper to do this */
183 snprintf( psz_filename, MAX_PATH,
184 "file://%s" DIR_SEP CONFIG_DIR DIR_SEP "art"
185 DIR_SEP "%s" DIR_SEP "%s" DIR_SEP "art%s",
186 p_playlist->p_libvlc->psz_homedir,
187 psz_artist, psz_album, psz_type );
189 snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR,
190 p_playlist->p_libvlc->psz_homedir );
191 utf8_mkdir( psz_dir );
192 snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP "art",
193 p_playlist->p_libvlc->psz_homedir );
194 utf8_mkdir( psz_dir );
195 snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP
197 p_playlist->p_libvlc->psz_homedir, psz_artist );
198 utf8_mkdir( psz_dir );
199 snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP
200 "art" DIR_SEP "%s" DIR_SEP "%s",
201 p_playlist->p_libvlc->psz_homedir,
202 psz_artist, psz_album );
203 utf8_mkdir( psz_dir );
205 /* Todo: check for stuff that needs a downloader module */
206 p_stream = stream_UrlNew( p_playlist, p_item->p_meta->psz_arturl );
210 void *p_buffer = malloc( 1<<16 );
212 FILE *p_file = utf8_fopen( psz_filename+7, "w" );
213 while( ( l_read = stream_Read( p_stream, p_buffer, 1<<16 ) ) )
215 fwrite( p_buffer, l_read, 1, p_file );
219 stream_Delete( p_stream );
220 msg_Dbg( p_playlist, "album art saved to %s\n", psz_filename );
221 free( p_item->p_meta->psz_arturl );
222 p_item->p_meta->psz_arturl = strdup( psz_filename );
223 i_status = VLC_SUCCESS;
228 uint32_t input_CurrentMetaFlags( vlc_meta_t *p_meta )
232 #define CHECK( a, b ) \
233 if( p_meta->psz_ ## a && *p_meta->psz_ ## a ) \
234 i_meta |= VLC_META_ENGINE_ ## b;
236 CHECK( title, TITLE )
237 CHECK( artist, ARTIST )
238 CHECK( genre, GENRE )
239 CHECK( copyright, COPYRIGHT )
240 CHECK( album, COLLECTION )
241 CHECK( tracknum, SEQ_NUM )
242 CHECK( description, DESCRIPTION )
243 CHECK( rating, RATING )
246 CHECK( language, LANGUAGE )
247 CHECK( arturl, ART_URL )