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_MetaFetch( vlc_object_t *p_parent, input_item_t *p_item )
37 struct meta_engine_t *p_me;
39 /* FIXME: don't launch any module if we already have all the needed
40 * info. Easiest way to do this would be to add a dummy module.
41 * I'll do that later */
43 p_me = vlc_object_create( p_parent, VLC_OBJECT_META_ENGINE );
44 p_me->i_flags |= OBJECT_FLAGS_NOINTERACT;
45 p_me->i_mandatory = VLC_META_ENGINE_TITLE
46 | VLC_META_ENGINE_ARTIST;
49 if( var_CreateGetInteger( p_parent, "album-art" ) != ALBUM_ART_NEVER )
51 p_me->i_mandatory |= VLC_META_ENGINE_ART_URL;
55 p_me->i_optional |= VLC_META_ENGINE_ART_URL;
58 p_me->p_item = p_item;
59 p_me->p_module = module_Need( p_me, "meta fetcher", 0, VLC_FALSE );
60 vlc_object_attach( p_me, p_parent );
63 msg_Err( p_parent, "no suitable meta engine module" );
64 vlc_object_detach( p_me );
65 vlc_object_destroy( p_me );
69 module_Unneed( p_me, p_me->p_module );
71 vlc_object_destroy( p_me );
79 int input_FindArt( vlc_object_t *p_parent, input_item_t *p_item )
84 char psz_filename[MAX_PATH];
88 if( !p_item->p_meta ) return VLC_EGENERIC;
90 psz_artist = p_item->p_meta->psz_artist;
91 psz_album = p_item->p_meta->psz_album;
94 psz_type = strdup( "jpg" );
96 snprintf( psz_filename, MAX_PATH,
97 "file://%s" DIR_SEP CONFIG_DIR DIR_SEP "art"
98 DIR_SEP "%s" DIR_SEP "%s" DIR_SEP "art%s",
99 p_parent->p_libvlc->psz_homedir,
100 psz_artist, psz_album, psz_type );
102 /* Check if file exists */
103 i_ret = utf8_stat( psz_filename+7, &a );
106 msg_Dbg( p_parent, "album art %s already exists in cache"
112 /* Use a art finder module to find the URL */
118 * Download the art using the URL or an art downloaded
119 * This function should be called only if data is not already in cache
121 int input_DownloadAndCacheArt( vlc_object_t *p_parent, input_item_t *p_item )
123 int i_status = VLC_EGENERIC;
127 char psz_filename[MAX_PATH], psz_dir[MAX_PATH];
131 psz_artist = p_item->p_meta->psz_artist;
132 psz_album = p_item->p_meta->psz_album;
134 /* You dummy ! How am I supposed to download NULL ? */
135 if( !p_item->p_meta || !p_item->p_meta->psz_arturl
136 || !*p_item->p_meta->psz_arturl )
139 /* Todo: get a helper to do this */
140 snprintf( psz_filename, MAX_PATH,
141 "file://%s" DIR_SEP CONFIG_DIR DIR_SEP "art"
142 DIR_SEP "%s" DIR_SEP "%s" DIR_SEP "art%s",
143 p_parent->p_libvlc->psz_homedir,
144 psz_artist, psz_album, psz_type );
146 snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR,
147 p_parent->p_libvlc->psz_homedir );
148 utf8_mkdir( psz_dir );
149 snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP "art",
150 p_parent->p_libvlc->psz_homedir );
151 utf8_mkdir( psz_dir );
152 snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP
154 p_parent->p_libvlc->psz_homedir, psz_artist );
155 utf8_mkdir( psz_dir );
156 snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP
157 "art" DIR_SEP "%s" DIR_SEP "%s",
158 p_parent->p_libvlc->psz_homedir,
159 psz_artist, psz_album );
160 utf8_mkdir( psz_dir );
162 /* Todo: check for stuff that needs a downloader module */
163 p_stream = stream_UrlNew( p_parent, p_item->p_meta->psz_arturl );
167 void *p_buffer = malloc( 1<<16 );
169 FILE *p_file = utf8_fopen( psz_filename+7, "w" );
170 while( ( l_read = stream_Read( p_stream, p_buffer, 1<<16 ) ) )
172 fwrite( p_buffer, l_read, 1, p_file );
176 stream_Delete( p_stream );
177 msg_Dbg( p_parent, "Album art saved to %s\n", psz_filename );
178 free( p_item->p_meta->psz_arturl );
179 p_item->p_meta->psz_arturl = strdup( psz_filename );
180 i_status = VLC_SUCCESS;