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