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