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