]> git.sesse.net Git - vlc/blob - src/input/meta.c
* src/input/meta.c: stop the spamming of the meta engine.
[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
31 #ifdef HAVE_SYS_STAT_H
32 #   include <sys/stat.h>
33 #endif
34
35 int input_FindArtInCache( playlist_t *p_playlist, input_item_t *p_item );
36
37 vlc_bool_t input_MetaSatisfied( playlist_t *p_playlist, input_item_t *p_item,
38                                 uint32_t *pi_mandatory, uint32_t *pi_optional,
39                                 vlc_bool_t b_check_cache )
40 {
41     // FIXME don't var_Stuff at each loop
42     int i_policy = var_CreateGetInteger( p_playlist,     "album-art" );
43     if( b_check_cache )
44         input_FindArtInCache( p_playlist, p_item );
45
46     *pi_mandatory = VLC_META_ENGINE_TITLE | VLC_META_ENGINE_ARTIST |
47                     (i_policy == ALBUM_ART_ALL ? VLC_META_ENGINE_ART_URL : 0 );
48
49     uint32_t i_meta = input_CurrentMetaFlags( p_item->p_meta );
50     *pi_mandatory &= ~i_meta;
51     *pi_optional = 0; /// Todo
52     return *pi_mandatory ? VLC_FALSE:VLC_TRUE;
53 }
54
55 int input_MetaFetch( playlist_t *p_playlist, input_item_t *p_item )
56 {
57     struct meta_engine_t *p_me;
58     uint32_t i_mandatory, i_optional;
59
60     if( !p_item->p_meta ) return VLC_EGENERIC;
61
62     input_MetaSatisfied( p_playlist, p_item,
63                          &i_mandatory, &i_optional, VLC_FALSE );
64
65     // Meta shouldn't magically appear
66     assert( i_mandatory );
67
68     p_me = vlc_object_create( p_playlist, VLC_OBJECT_META_ENGINE );
69     p_me->i_flags |= OBJECT_FLAGS_NOINTERACT;
70     p_me->i_flags |= OBJECT_FLAGS_QUIET;
71     p_me->i_mandatory = i_mandatory;
72     p_me->i_optional = i_optional;
73
74     p_me->p_item = p_item;
75     p_me->p_module = module_Need( p_me, "meta fetcher", 0, VLC_FALSE );
76     if( !p_me->p_module )
77     {
78         vlc_object_destroy( p_me );
79         return VLC_EGENERIC;
80     }
81
82     module_Unneed( p_me, p_me->p_module );
83     vlc_object_destroy( p_me );
84
85     return VLC_SUCCESS;
86 }
87
88 int input_ArtFetch( playlist_t *p_playlist, input_item_t *p_item )
89 {
90     if( !p_item->p_meta ) return VLC_EGENERIC;
91
92     if( !p_item->p_meta->psz_arturl || !*p_item->p_meta->psz_arturl )
93     {
94         module_t *p_module;
95         PL_LOCK;
96         p_playlist->p_private = p_item;
97         p_module = module_Need( p_playlist, "art finder", 0, VLC_FALSE );
98         if( !p_module )
99         {
100             msg_Dbg( p_playlist, "unable to find art" );
101             PL_UNLOCK;
102             return VLC_EGENERIC;
103         }
104         module_Unneed( p_playlist, p_module );
105         p_playlist->p_private = NULL;
106         PL_UNLOCK;
107
108         if( !p_item->p_meta->psz_arturl || !*p_item->p_meta->psz_arturl )
109             return VLC_EGENERIC;
110     }
111     return input_DownloadAndCacheArt( p_playlist, p_item );
112 }
113
114 #ifndef MAX_PATH
115 #   define MAX_PATH 250
116 #endif
117 int input_FindArtInCache( playlist_t *p_playlist, input_item_t *p_item )
118 {
119     char *psz_artist;
120     char *psz_album;
121     char psz_filename[MAX_PATH];
122     int i;
123     struct stat a;
124     const char *ppsz_type[] = { ".jpg", ".png", ".gif", ".bmp", "" };
125
126     if( !p_item->p_meta ) return VLC_EGENERIC;
127
128     psz_artist = p_item->p_meta->psz_artist;
129     psz_album = p_item->p_meta->psz_album;
130
131     for( i = 0; i < 5; i++ )
132     {
133         snprintf( psz_filename, MAX_PATH,
134                   "file://%s" DIR_SEP CONFIG_DIR DIR_SEP "art"
135                   DIR_SEP "%s" DIR_SEP "%s" DIR_SEP "art%s",
136                   p_playlist->p_libvlc->psz_homedir,
137                   psz_artist, psz_album, ppsz_type[i] );
138
139         /* Check if file exists */
140         if( utf8_stat( psz_filename+7, &a ) == 0 )
141         {
142             vlc_meta_SetArtURL( p_item->p_meta, psz_filename );
143             return VLC_SUCCESS;
144         }
145     }
146
147     return VLC_EGENERIC;
148 }
149
150 /**
151  * Download the art using the URL or an art downloaded
152  * This function should be called only if data is not already in cache
153  */
154 int input_DownloadAndCacheArt( playlist_t *p_playlist, input_item_t *p_item )
155 {
156     int i_status = VLC_EGENERIC;
157     stream_t *p_stream;
158     char psz_filename[MAX_PATH], psz_dir[MAX_PATH];
159     char *psz_artist;
160     char *psz_album;
161     char *psz_type;
162     psz_artist = p_item->p_meta->psz_artist;
163     psz_album = p_item->p_meta->psz_album;
164
165     /* You dummy ! How am I supposed to download NULL ? */
166     if( !p_item->p_meta || !p_item->p_meta->psz_arturl
167                         || !*p_item->p_meta->psz_arturl )
168         return VLC_EGENERIC;
169
170     /* FIXME: use an alternate saving filename scheme if we don't have
171      * the artist or album name */
172     if(    !p_item->p_meta->psz_artist
173         || !p_item->p_meta->psz_album )
174         return VLC_EGENERIC;
175
176     /* Check if file doesn't already exist */
177     if( input_FindArtInCache( p_playlist, p_item ) == VLC_SUCCESS )
178         return VLC_SUCCESS;
179
180     psz_type = strrchr( p_item->p_meta->psz_arturl, '.' );
181
182     /* Todo: get a helper to do this */
183     snprintf( psz_filename, MAX_PATH,
184               "file://%s" DIR_SEP CONFIG_DIR DIR_SEP "art"
185               DIR_SEP "%s" DIR_SEP "%s" DIR_SEP "art%s",
186               p_playlist->p_libvlc->psz_homedir,
187               psz_artist, psz_album, psz_type );
188
189     snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR,
190               p_playlist->p_libvlc->psz_homedir );
191     utf8_mkdir( psz_dir );
192     snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP "art",
193               p_playlist->p_libvlc->psz_homedir );
194     utf8_mkdir( psz_dir );
195     snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP
196               "art" DIR_SEP "%s",
197                  p_playlist->p_libvlc->psz_homedir, psz_artist );
198     utf8_mkdir( psz_dir );
199     snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP
200               "art" DIR_SEP "%s" DIR_SEP "%s",
201                       p_playlist->p_libvlc->psz_homedir,
202                       psz_artist, psz_album );
203     utf8_mkdir( psz_dir );
204
205     /* Todo: check for stuff that needs a downloader module */
206     p_stream = stream_UrlNew( p_playlist, p_item->p_meta->psz_arturl );
207
208     if( p_stream )
209     {
210         void *p_buffer = malloc( 1<<16 );
211         long int l_read;
212         FILE *p_file = utf8_fopen( psz_filename+7, "w" );
213         while( ( l_read = stream_Read( p_stream, p_buffer, 1<<16 ) ) )
214         {
215             fwrite( p_buffer, l_read, 1, p_file );
216         }
217         free( p_buffer );
218         fclose( p_file );
219         stream_Delete( p_stream );
220         msg_Dbg( p_playlist, "album art saved to %s\n", psz_filename );
221         free( p_item->p_meta->psz_arturl );
222         p_item->p_meta->psz_arturl = strdup( psz_filename );
223         i_status = VLC_SUCCESS;
224     }
225     return i_status;
226 }
227
228 uint32_t input_CurrentMetaFlags( vlc_meta_t *p_meta )
229 {
230     uint32_t i_meta = 0;
231
232 #define CHECK( a, b ) \
233     if( p_meta->psz_ ## a && *p_meta->psz_ ## a ) \
234         i_meta |= VLC_META_ENGINE_ ## b;
235
236     CHECK( title, TITLE )
237     CHECK( artist, ARTIST )
238     CHECK( genre, GENRE )
239     CHECK( copyright, COPYRIGHT )
240     CHECK( album, COLLECTION )
241     CHECK( tracknum, SEQ_NUM )
242     CHECK( description, DESCRIPTION )
243     CHECK( rating, RATING )
244     CHECK( date, DATE )
245     CHECK( url, URL )
246     CHECK( language, LANGUAGE )
247     CHECK( arturl, ART_URL )
248
249     return i_meta;
250 }