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( vlc_object_t *p_parent, input_item_t *p_item );
37 int __input_MetaFetch( vlc_object_t *p_parent, input_item_t *p_item )
39 struct meta_engine_t *p_me;
40 uint32_t i_mandatory = 0, i_optional = 0, i_meta;
43 if( !p_item->p_meta ) return VLC_EGENERIC;
45 i_policy = var_CreateGetInteger( p_parent, "album-art" );
47 i_mandatory = VLC_META_ENGINE_TITLE
48 | VLC_META_ENGINE_ARTIST;
49 if( i_policy == ALBUM_ART_ALL )
51 i_mandatory |= VLC_META_ENGINE_ART_URL;
55 i_optional |= VLC_META_ENGINE_ART_URL;
58 input_FindArtInCache( p_parent, p_item );
60 i_meta = input_GetMetaEngineFlags( p_item->p_meta );
61 i_mandatory &= ~i_meta;
62 i_optional &= ~i_meta;
64 if( !i_mandatory ) return VLC_SUCCESS;
66 p_me = vlc_object_create( p_parent, VLC_OBJECT_META_ENGINE );
67 p_me->i_flags |= OBJECT_FLAGS_NOINTERACT;
68 p_me->i_mandatory = i_mandatory;
69 p_me->i_optional = i_optional;
71 p_me->p_item = p_item;
72 p_me->p_module = module_Need( p_me, "meta fetcher", 0, VLC_FALSE );
73 vlc_object_attach( p_me, p_parent );
76 msg_Err( p_parent, "no suitable meta fetcher module" );
77 vlc_object_detach( p_me );
78 vlc_object_destroy( p_me );
82 module_Unneed( p_me, p_me->p_module );
84 vlc_object_detach( p_me );
85 vlc_object_destroy( p_me );
90 int __input_ArtFetch( vlc_object_t *p_parent, input_item_t *p_item )
95 /* TODO: call art fetcher modules */
97 if( !p_item->p_meta->psz_arturl || !*p_item->p_meta->psz_arturl )
100 if( strncmp( "file://", p_item->p_meta->psz_arturl, 7 ) )
102 return input_DownloadAndCacheArt( p_parent, p_item );
108 # define MAX_PATH 250
110 int input_FindArtInCache( vlc_object_t *p_parent, input_item_t *p_item )
114 char psz_filename[MAX_PATH];
117 const char *ppsz_type[] = { ".jpg", ".png", ".gif", ".bmp", "" };
119 if( !p_item->p_meta ) return VLC_EGENERIC;
121 psz_artist = p_item->p_meta->psz_artist;
122 psz_album = p_item->p_meta->psz_album;
124 for( i = 0; i < 5; i++ )
126 snprintf( psz_filename, MAX_PATH,
127 "file://%s" DIR_SEP CONFIG_DIR DIR_SEP "art"
128 DIR_SEP "%s" DIR_SEP "%s" DIR_SEP "art%s",
129 p_parent->p_libvlc->psz_homedir,
130 psz_artist, psz_album, ppsz_type[i] );
132 /* Check if file exists */
133 if( utf8_stat( psz_filename+7, &a ) == 0 )
135 msg_Dbg( p_parent, "album art %s already exists in cache"
137 vlc_meta_SetArtURL( p_item->p_meta, psz_filename );
142 /* Use a art finder module to find the URL */
147 * Download the art using the URL or an art downloaded
148 * This function should be called only if data is not already in cache
150 int input_DownloadAndCacheArt( vlc_object_t *p_parent, input_item_t *p_item )
152 int i_status = VLC_EGENERIC;
154 char psz_filename[MAX_PATH], psz_dir[MAX_PATH];
158 psz_artist = p_item->p_meta->psz_artist;
159 psz_album = p_item->p_meta->psz_album;
161 /* You dummy ! How am I supposed to download NULL ? */
162 if( !p_item->p_meta || !p_item->p_meta->psz_arturl
163 || !*p_item->p_meta->psz_arturl )
166 psz_type = strrchr( p_item->p_meta->psz_arturl, '.' );
168 /* Todo: get a helper to do this */
169 snprintf( psz_filename, MAX_PATH,
170 "file://%s" DIR_SEP CONFIG_DIR DIR_SEP "art"
171 DIR_SEP "%s" DIR_SEP "%s" DIR_SEP "art%s",
172 p_parent->p_libvlc->psz_homedir,
173 psz_artist, psz_album, psz_type );
175 snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR,
176 p_parent->p_libvlc->psz_homedir );
177 utf8_mkdir( psz_dir );
178 snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP "art",
179 p_parent->p_libvlc->psz_homedir );
180 utf8_mkdir( psz_dir );
181 snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP
183 p_parent->p_libvlc->psz_homedir, psz_artist );
184 utf8_mkdir( psz_dir );
185 snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP
186 "art" DIR_SEP "%s" DIR_SEP "%s",
187 p_parent->p_libvlc->psz_homedir,
188 psz_artist, psz_album );
189 utf8_mkdir( psz_dir );
191 /* Todo: check for stuff that needs a downloader module */
192 p_stream = stream_UrlNew( p_parent, p_item->p_meta->psz_arturl );
196 void *p_buffer = malloc( 1<<16 );
198 FILE *p_file = utf8_fopen( psz_filename+7, "w" );
199 while( ( l_read = stream_Read( p_stream, p_buffer, 1<<16 ) ) )
201 fwrite( p_buffer, l_read, 1, p_file );
205 stream_Delete( p_stream );
206 msg_Dbg( p_parent, "Album art saved to %s\n", psz_filename );
207 free( p_item->p_meta->psz_arturl );
208 p_item->p_meta->psz_arturl = strdup( psz_filename );
209 i_status = VLC_SUCCESS;
214 uint32_t input_GetMetaEngineFlags( vlc_meta_t *p_meta )
218 #define CHECK( a, b ) \
219 if( p_meta->psz_ ## a && *p_meta->psz_ ## a ) \
220 i_meta |= VLC_META_ENGINE_ ## b;
222 CHECK( title, TITLE )
223 CHECK( author, AUTHOR )
224 CHECK( artist, ARTIST )
225 CHECK( genre, GENRE )
226 CHECK( copyright, COPYRIGHT )
227 CHECK( album, COLLECTION )
228 CHECK( tracknum, SEQ_NUM )
229 CHECK( description, DESCRIPTION )
230 CHECK( rating, RATING )
233 CHECK( language, LANGUAGE )
234 CHECK( arturl, ART_URL )