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