]> git.sesse.net Git - vlc/blob - src/input/meta.c
Improve meta/art logic
[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_mandatory = i_mandatory;
71     p_me->i_optional = i_optional;
72
73     p_me->p_item = p_item;
74     p_me->p_module = module_Need( p_me, "meta fetcher", 0, VLC_FALSE );
75     vlc_object_attach( p_me, p_playlist );
76     if( !p_me->p_module )
77     {
78         msg_Dbg( p_playlist, "unable to fetch meta" );
79         vlc_object_detach( p_me );
80         vlc_object_destroy( p_me );
81         return VLC_EGENERIC;
82     }
83
84     module_Unneed( p_me, p_me->p_module );
85
86     vlc_object_detach( p_me );
87     vlc_object_destroy( p_me );
88
89     return VLC_SUCCESS;
90 }
91
92 int input_ArtFetch( playlist_t *p_playlist, input_item_t *p_item )
93 {
94     if( !p_item->p_meta ) return VLC_EGENERIC;
95
96     if( !p_item->p_meta->psz_arturl || !*p_item->p_meta->psz_arturl )
97     {
98         module_t *p_module;
99         PL_LOCK;
100         p_playlist->p_private = p_item;
101         p_module = module_Need( p_playlist, "art finder", 0, VLC_FALSE );
102         if( !p_module )
103         {
104             msg_Dbg( p_playlist, "unable to find art" );
105             return VLC_EGENERIC;
106         }
107         module_Unneed( p_playlist, p_module );
108         p_playlist->p_private = NULL;
109
110         if( !p_item->p_meta->psz_arturl || !*p_item->p_meta->psz_arturl )
111             return VLC_EGENERIC;
112     }
113     return input_DownloadAndCacheArt( p_playlist, p_item );
114 }
115
116 #ifndef MAX_PATH
117 #   define MAX_PATH 250
118 #endif
119 int input_FindArtInCache( playlist_t *p_playlist, input_item_t *p_item )
120 {
121     char *psz_artist;
122     char *psz_album;
123     char psz_filename[MAX_PATH];
124     int i;
125     struct stat a;
126     const char *ppsz_type[] = { ".jpg", ".png", ".gif", ".bmp", "" };
127
128     if( !p_item->p_meta ) return VLC_EGENERIC;
129
130     psz_artist = p_item->p_meta->psz_artist;
131     psz_album = p_item->p_meta->psz_album;
132
133     for( i = 0; i < 5; i++ )
134     {
135         snprintf( psz_filename, MAX_PATH,
136                   "file://%s" DIR_SEP CONFIG_DIR DIR_SEP "art"
137                   DIR_SEP "%s" DIR_SEP "%s" DIR_SEP "art%s",
138                   p_playlist->p_libvlc->psz_homedir,
139                   psz_artist, psz_album, ppsz_type[i] );
140
141         /* Check if file exists */
142         if( utf8_stat( psz_filename+7, &a ) == 0 )
143         {
144             msg_Dbg( p_playlist, "album art %s already exists in cache"
145                              , psz_filename );
146             vlc_meta_SetArtURL( p_item->p_meta, psz_filename );
147             return VLC_SUCCESS;
148         }
149     }
150
151     return VLC_EGENERIC;
152 }
153
154 /**
155  * Download the art using the URL or an art downloaded
156  * This function should be called only if data is not already in cache
157  */
158 int input_DownloadAndCacheArt( playlist_t *p_playlist, input_item_t *p_item )
159 {
160     int i_status = VLC_EGENERIC;
161     stream_t *p_stream;
162     char psz_filename[MAX_PATH], psz_dir[MAX_PATH];
163     char *psz_artist;
164     char *psz_album;
165     char *psz_type;
166     psz_artist = p_item->p_meta->psz_artist;
167     psz_album = p_item->p_meta->psz_album;
168
169     /* You dummy ! How am I supposed to download NULL ? */
170     if( !p_item->p_meta || !p_item->p_meta->psz_arturl
171                         || !*p_item->p_meta->psz_arturl )
172         return VLC_EGENERIC;
173
174     psz_type = strrchr( p_item->p_meta->psz_arturl, '.' );
175
176     /* Todo: get a helper to do this */
177     snprintf( psz_filename, MAX_PATH,
178               "file://%s" DIR_SEP CONFIG_DIR DIR_SEP "art"
179               DIR_SEP "%s" DIR_SEP "%s" DIR_SEP "art%s",
180               p_playlist->p_libvlc->psz_homedir,
181               psz_artist, psz_album, psz_type );
182
183     snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR,
184               p_playlist->p_libvlc->psz_homedir );
185     utf8_mkdir( psz_dir );
186     snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP "art",
187               p_playlist->p_libvlc->psz_homedir );
188     utf8_mkdir( psz_dir );
189     snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP
190               "art" DIR_SEP "%s",
191                  p_playlist->p_libvlc->psz_homedir, psz_artist );
192     utf8_mkdir( psz_dir );
193     snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP
194               "art" DIR_SEP "%s" DIR_SEP "%s",
195                       p_playlist->p_libvlc->psz_homedir,
196                       psz_artist, psz_album );
197     utf8_mkdir( psz_dir );
198
199     /* Todo: check for stuff that needs a downloader module */
200     p_stream = stream_UrlNew( p_playlist, p_item->p_meta->psz_arturl );
201
202     if( p_stream )
203     {
204         void *p_buffer = malloc( 1<<16 );
205         long int l_read;
206         FILE *p_file = utf8_fopen( psz_filename+7, "w" );
207         while( ( l_read = stream_Read( p_stream, p_buffer, 1<<16 ) ) )
208         {
209             fwrite( p_buffer, l_read, 1, p_file );
210         }
211         free( p_buffer );
212         fclose( p_file );
213         stream_Delete( p_stream );
214         msg_Dbg( p_playlist, "Album art saved to %s\n", psz_filename );
215         free( p_item->p_meta->psz_arturl );
216         p_item->p_meta->psz_arturl = strdup( psz_filename );
217         i_status = VLC_SUCCESS;
218     }
219     return i_status;
220 }
221
222 uint32_t input_CurrentMetaFlags( vlc_meta_t *p_meta )
223 {
224     uint32_t i_meta = 0;
225
226 #define CHECK( a, b ) \
227     if( p_meta->psz_ ## a && *p_meta->psz_ ## a ) \
228         i_meta |= VLC_META_ENGINE_ ## b;
229
230     CHECK( title, TITLE )
231     CHECK( author, AUTHOR )
232     CHECK( artist, ARTIST )
233     CHECK( genre, GENRE )
234     CHECK( copyright, COPYRIGHT )
235     CHECK( album, COLLECTION )
236     CHECK( tracknum, SEQ_NUM )
237     CHECK( description, DESCRIPTION )
238     CHECK( rating, RATING )
239     CHECK( date, DATE )
240     CHECK( url, URL )
241     CHECK( language, LANGUAGE )
242     CHECK( arturl, ART_URL )
243
244     return i_meta;
245 }