]> git.sesse.net Git - vlc/blob - src/input/meta.c
Clean up and improve core handling for album art. Still only "always fetch" implemented
[vlc] / src / input / meta.c
1 /*****************************************************************************
2  * meta.c : Metadata handling
3  *****************************************************************************
4  * Copyright (C) 1998-2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Antoine Cellerier <dionoea@videolan.org>
8  *          ClĂ©ment Stenac <zorglub@videolan.org
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 #include <vlc/vlc.h>
26 #include <vlc/input.h>
27 #include <vlc_meta.h>
28 #include "vlc_playlist.h"
29 #include "charset.h"
30 #include "../playlist/playlist_internal.h"
31
32 #ifdef HAVE_SYS_STAT_H
33 #   include <sys/stat.h>
34 #endif
35
36 int input_FindArtInCache( playlist_t *p_playlist, input_item_t *p_item );
37
38 vlc_bool_t input_MetaSatisfied( playlist_t *p_playlist, input_item_t *p_item,
39                                 uint32_t *pi_mandatory, uint32_t *pi_optional )
40 {
41     *pi_mandatory = VLC_META_ENGINE_TITLE | VLC_META_ENGINE_ARTIST;
42
43     uint32_t i_meta = input_CurrentMetaFlags( p_item->p_meta );
44     *pi_mandatory &= ~i_meta;
45     *pi_optional = 0; /// Todo
46     return *pi_mandatory ? VLC_FALSE:VLC_TRUE;
47 }
48
49 int input_MetaFetch( playlist_t *p_playlist, input_item_t *p_item )
50 {
51     struct meta_engine_t *p_me;
52     uint32_t i_mandatory, i_optional;
53
54     if( !p_item->p_meta ) return VLC_EGENERIC;
55
56     input_MetaSatisfied( p_playlist, p_item, &i_mandatory, &i_optional );
57     // Meta shouldn't magically appear
58     assert( i_mandatory );
59
60     /* FIXME: object creation is overkill, use p_private */
61     p_me = vlc_object_create( p_playlist, VLC_OBJECT_META_ENGINE );
62     p_me->i_flags |= OBJECT_FLAGS_NOINTERACT;
63     p_me->i_flags |= OBJECT_FLAGS_QUIET;
64     p_me->i_mandatory = i_mandatory;
65     p_me->i_optional = i_optional;
66
67     p_me->p_item = p_item;
68     p_me->p_module = module_Need( p_me, "meta fetcher", 0, VLC_FALSE );
69     if( !p_me->p_module )
70     {
71         vlc_object_destroy( p_me );
72         return VLC_EGENERIC;
73     }
74     module_Unneed( p_me, p_me->p_module );
75     vlc_object_destroy( p_me );
76     return VLC_SUCCESS;
77 }
78
79 /* Return codes:
80  *   0 : Art is in cache
81  *   1 : Art found, need to download
82  *  -X : Error/not found
83  */
84 int input_ArtFind( playlist_t *p_playlist, input_item_t *p_item )
85 {
86     int i_ret = VLC_EGENERIC;
87     module_t *p_module;
88     if( !p_item->p_meta || !p_item->p_meta->psz_album ||
89                            !p_item->p_meta->psz_artist )
90         return VLC_EGENERIC;
91
92     /* If we already checked this album in this session, skip */
93     FOREACH_ARRAY( playlist_album_t album, p_playlist->p_fetcher->albums )
94         if( !strcmp( album.psz_artist, p_item->p_meta->psz_artist ) &&
95             !strcmp( album.psz_album, p_item->p_meta->psz_album ) )
96         {
97             msg_Dbg( p_playlist, " %s - %s has already been searched",
98                      p_item->p_meta->psz_artist,  p_item->p_meta->psz_album );
99             if( album.b_found )
100             {
101                 /* Actually get URL from cache */
102                 input_FindArtInCache( p_playlist, p_item );
103                 return 0;
104             }
105             else
106                 return VLC_EGENERIC;
107         }
108     FOREACH_END();
109
110     input_FindArtInCache( p_playlist, p_item );
111     if( !EMPTY_STR( p_item->p_meta->psz_arturl ) )
112         return 0;
113
114     PL_LOCK;
115     p_playlist->p_private = p_item;
116     msg_Dbg( p_playlist, "searching art for %s - %s",
117              p_item->p_meta->psz_artist,  p_item->p_meta->psz_album );
118     p_module = module_Need( p_playlist, "art finder", 0, VLC_FALSE );
119
120     if( p_module )
121         i_ret = 1;
122     else
123         msg_Dbg( p_playlist, "unable to find art" );
124
125     /* Record this album */
126     playlist_album_t a;
127     a.psz_artist = strdup( p_item->p_meta->psz_artist );
128     a.psz_album = strdup( p_item->p_meta->psz_album );
129     a.b_found = (i_ret == VLC_EGENERIC ? VLC_FALSE : VLC_TRUE );
130     ARRAY_APPEND( p_playlist->p_fetcher->albums, a );
131
132     if( p_module )
133         module_Unneed( p_playlist, p_module );
134     p_playlist->p_private = NULL;
135     PL_UNLOCK;
136
137     return i_ret;
138 }
139
140 #ifndef MAX_PATH
141 #   define MAX_PATH 250
142 #endif
143 int input_FindArtInCache( playlist_t *p_playlist, input_item_t *p_item )
144 {
145     char *psz_artist;
146     char *psz_album;
147     char psz_filename[MAX_PATH+1];
148     int i;
149     struct stat a;
150     const char *ppsz_type[] = { ".jpg", ".png", ".gif", ".bmp", "" };
151
152     if( !p_item->p_meta ) return VLC_EGENERIC;
153
154     psz_artist = p_item->p_meta->psz_artist;
155     psz_album = p_item->p_meta->psz_album;
156
157     for( i = 0; i < 5; i++ )
158     {
159         snprintf( psz_filename, MAX_PATH,
160                   "file://%s" DIR_SEP CONFIG_DIR DIR_SEP "art"
161                   DIR_SEP "%s" DIR_SEP "%s" DIR_SEP "art%s",
162                   p_playlist->p_libvlc->psz_homedir,
163                   psz_artist, psz_album, ppsz_type[i] );
164
165         /* Check if file exists */
166         if( utf8_stat( psz_filename+7, &a ) == 0 )
167         {
168             vlc_meta_SetArtURL( p_item->p_meta, psz_filename );
169             return VLC_SUCCESS;
170         }
171     }
172     return VLC_EGENERIC;
173 }
174
175 /**
176  * Download the art using the URL or an art downloaded
177  * This function should be called only if data is not already in cache
178  */
179 int input_DownloadAndCacheArt( playlist_t *p_playlist, input_item_t *p_item )
180 {
181     int i_status = VLC_EGENERIC;
182     stream_t *p_stream;
183     char psz_filename[MAX_PATH+1], psz_dir[MAX_PATH+1];
184     char *psz_artist;
185     char *psz_album;
186     char *psz_type;
187     psz_artist = p_item->p_meta->psz_artist;
188     psz_album = p_item->p_meta->psz_album;
189
190     assert( p_item->p_meta && !EMPTY_STR(p_item->p_meta->psz_arturl) );
191
192     /* FIXME: use an alternate saving filename scheme if we don't have
193      * the artist or album name */
194     if( !p_item->p_meta->psz_artist || !p_item->p_meta->psz_album )
195         return VLC_EGENERIC;
196
197     psz_type = strrchr( p_item->p_meta->psz_arturl, '.' );
198
199     /* Todo: get a helper to do this */
200     snprintf( psz_filename, MAX_PATH,
201               "file://%s" DIR_SEP CONFIG_DIR DIR_SEP "art"
202               DIR_SEP "%s" DIR_SEP "%s" DIR_SEP "art%s",
203               p_playlist->p_libvlc->psz_homedir,
204               psz_artist, psz_album, psz_type );
205
206     snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR,
207               p_playlist->p_libvlc->psz_homedir );
208     utf8_mkdir( psz_dir );
209     snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP "art",
210               p_playlist->p_libvlc->psz_homedir );
211     utf8_mkdir( psz_dir );
212     snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP
213               "art" DIR_SEP "%s",
214                  p_playlist->p_libvlc->psz_homedir, psz_artist );
215     utf8_mkdir( psz_dir );
216     snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP
217               "art" DIR_SEP "%s" DIR_SEP "%s",
218                       p_playlist->p_libvlc->psz_homedir,
219                       psz_artist, psz_album );
220     utf8_mkdir( psz_dir );
221
222     if( !strncmp( p_item->p_meta->psz_arturl , "APIC", 4 ) )
223     {
224         msg_Warn( p_playlist, "APIC fetch not supported yet" );
225         return VLC_EGENERIC;
226     }
227
228     p_stream = stream_UrlNew( p_playlist, p_item->p_meta->psz_arturl );
229     if( p_stream )
230     {
231         void *p_buffer = malloc( 1<<16 );
232         long int l_read;
233         FILE *p_file = utf8_fopen( psz_filename+7, "w" );
234         while( ( l_read = stream_Read( p_stream, p_buffer, 1<<16 ) ) )
235         {
236             fwrite( p_buffer, l_read, 1, p_file );
237         }
238         free( p_buffer );
239         fclose( p_file );
240         stream_Delete( p_stream );
241         msg_Dbg( p_playlist, "album art saved to %s\n", psz_filename );
242         free( p_item->p_meta->psz_arturl );
243         p_item->p_meta->psz_arturl = strdup( psz_filename );
244         i_status = VLC_SUCCESS;
245     }
246     return i_status;
247 }
248
249 uint32_t input_CurrentMetaFlags( vlc_meta_t *p_meta )
250 {
251     uint32_t i_meta = 0;
252
253 #define CHECK( a, b ) \
254     if( p_meta->psz_ ## a && *p_meta->psz_ ## a ) \
255         i_meta |= VLC_META_ENGINE_ ## b;
256
257     CHECK( title, TITLE )
258     CHECK( artist, ARTIST )
259     CHECK( album, COLLECTION )
260 #if 0
261     /* As this is not used at the moment, don't uselessly check for it.
262      * Re-enable this when it is used */
263     CHECK( genre, GENRE )
264     CHECK( copyright, COPYRIGHT )
265     CHECK( tracknum, SEQ_NUM )
266     CHECK( description, DESCRIPTION )
267     CHECK( rating, RATING )
268     CHECK( date, DATE )
269     CHECK( url, URL )
270     CHECK( language, LANGUAGE )
271 #endif
272     CHECK( arturl, ART_URL )
273
274     return i_meta;
275 }