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_FindArt( 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_FindArt( 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 );
93 int input_FindArt( vlc_object_t *p_parent, input_item_t *p_item )
98 char psz_filename[MAX_PATH];
101 const char ppsz_type[] = { ".jpg", ".png", ".gif", ".bmp", "" };
103 if( !p_item->p_meta ) return VLC_EGENERIC;
105 psz_artist = p_item->p_meta->psz_artist;
106 psz_album = p_item->p_meta->psz_album;
108 for( i = 0; i < 5; i++ )
110 snprintf( psz_filename, MAX_PATH,
111 "file://%s" DIR_SEP CONFIG_DIR DIR_SEP "art"
112 DIR_SEP "%s" DIR_SEP "%s" DIR_SEP "art%s",
113 p_parent->p_libvlc->psz_homedir,
114 psz_artist, psz_album, psz_type );
116 /* Check if file exists */
117 if( utf8_stat( psz_filename+7, &a ) == 0 )
119 msg_Dbg( p_parent, "album art %s already exists in cache"
125 /* Use a art finder module to find the URL */
130 * Download the art using the URL or an art downloaded
131 * This function should be called only if data is not already in cache
133 int input_DownloadAndCacheArt( vlc_object_t *p_parent, input_item_t *p_item )
135 int i_status = VLC_EGENERIC;
137 char psz_filename[MAX_PATH], psz_dir[MAX_PATH];
141 psz_artist = p_item->p_meta->psz_artist;
142 psz_album = p_item->p_meta->psz_album;
144 /* You dummy ! How am I supposed to download NULL ? */
145 if( !p_item->p_meta || !p_item->p_meta->psz_arturl
146 || !*p_item->p_meta->psz_arturl )
149 psz_type = strrchr( p_item->p_meta->psz_arturl, '.' );
151 /* Todo: get a helper to do this */
152 snprintf( psz_filename, MAX_PATH,
153 "file://%s" DIR_SEP CONFIG_DIR DIR_SEP "art"
154 DIR_SEP "%s" DIR_SEP "%s" DIR_SEP "art%s",
155 p_parent->p_libvlc->psz_homedir,
156 psz_artist, psz_album, psz_type );
158 snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR,
159 p_parent->p_libvlc->psz_homedir );
160 utf8_mkdir( psz_dir );
161 snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP "art",
162 p_parent->p_libvlc->psz_homedir );
163 utf8_mkdir( psz_dir );
164 snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP
166 p_parent->p_libvlc->psz_homedir, psz_artist );
167 utf8_mkdir( psz_dir );
168 snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP
169 "art" DIR_SEP "%s" DIR_SEP "%s",
170 p_parent->p_libvlc->psz_homedir,
171 psz_artist, psz_album );
172 utf8_mkdir( psz_dir );
174 /* Todo: check for stuff that needs a downloader module */
175 p_stream = stream_UrlNew( p_parent, p_item->p_meta->psz_arturl );
179 void *p_buffer = malloc( 1<<16 );
181 FILE *p_file = utf8_fopen( psz_filename+7, "w" );
182 while( ( l_read = stream_Read( p_stream, p_buffer, 1<<16 ) ) )
184 fwrite( p_buffer, l_read, 1, p_file );
188 stream_Delete( p_stream );
189 msg_Dbg( p_parent, "Album art saved to %s\n", psz_filename );
190 free( p_item->p_meta->psz_arturl );
191 p_item->p_meta->psz_arturl = strdup( psz_filename );
192 i_status = VLC_SUCCESS;
197 uint32_t input_GetMetaEngineFlags( vlc_meta_t *p_meta )
201 #define CHECK( a, b ) \
202 if( p_meta->psz_ ## a && *p_meta->psz_ ## a ) \
203 i_meta |= VLC_META_ENGINE_ ## b;
205 CHECK( title, TITLE )
206 CHECK( author, AUTHOR )
207 CHECK( artist, ARTIST )
208 CHECK( genre, GENRE )
209 CHECK( copyright, COPYRIGHT )
210 CHECK( album, COLLECTION )
211 CHECK( tracknum, SEQ_NUM )
212 CHECK( description, DESCRIPTION )
213 CHECK( rating, RATING )
216 CHECK( language, LANGUAGE )
217 CHECK( arturl, ART_URL )