]> git.sesse.net Git - vlc/blob - src/input/meta.c
33cca2e7b4b44cfeb35b8a9840ee8eca2e05c16d
[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_MetaFetch( vlc_object_t *p_parent, input_item_t *p_item )
36 {
37     struct meta_engine_t *p_me;
38
39     /* FIXME: don't launch any module if we already have all the needed
40      * info. Easiest way to do this would be to add a dummy module.
41      * I'll do that later */
42
43     p_me = vlc_object_create( p_parent, VLC_OBJECT_META_ENGINE );
44     p_me->i_flags |= OBJECT_FLAGS_NOINTERACT;
45     p_me->i_mandatory =   VLC_META_ENGINE_TITLE
46                         | VLC_META_ENGINE_ARTIST;
47     p_me->i_optional = 0;
48 /*
49     if( var_CreateGetInteger( p_parent, "album-art" ) != ALBUM_ART_NEVER )
50     {
51         p_me->i_mandatory |= VLC_META_ENGINE_ART_URL;
52     }
53     else
54     {
55         p_me->i_optional |= VLC_META_ENGINE_ART_URL;
56     }
57 */
58     p_me->p_item = p_item;
59     p_me->p_module = module_Need( p_me, "meta fetcher", 0, VLC_FALSE );
60     vlc_object_attach( p_me, p_parent );
61     if( !p_me->p_module )
62     {
63         msg_Err( p_parent, "no suitable meta engine module" );
64         vlc_object_detach( p_me );
65         vlc_object_destroy( p_me );
66         return VLC_EGENERIC;
67     }
68
69     module_Unneed( p_me, p_me->p_module );
70
71     vlc_object_destroy( p_me );
72
73     return VLC_SUCCESS;
74 }
75
76 #ifndef MAX_PATH
77 #   define MAX_PATH 250
78 #endif
79 int input_FindArt( vlc_object_t *p_parent, input_item_t *p_item )
80 {
81     char *psz_artist;
82     char *psz_album;
83     char *psz_type;
84     char psz_filename[MAX_PATH];
85     int i_ret;
86     struct stat a;
87
88     if( !p_item->p_meta ) return VLC_EGENERIC;
89
90     psz_artist = p_item->p_meta->psz_artist;
91     psz_album = p_item->p_meta->psz_album;
92
93     //FIXME !!!!!
94     psz_type = strdup( "jpg" );
95
96     snprintf( psz_filename, MAX_PATH,
97               "file://%s" DIR_SEP CONFIG_DIR DIR_SEP "art"
98               DIR_SEP "%s" DIR_SEP "%s" DIR_SEP "art%s",
99               p_parent->p_libvlc->psz_homedir,
100               psz_artist, psz_album, psz_type );
101
102     /* Check if file exists */
103     i_ret = utf8_stat( psz_filename+7, &a );
104     if( i_ret == 0 )
105     {
106         msg_Dbg( p_parent, "album art %s already exists in cache"
107                          , psz_filename );
108         return VLC_SUCCESS;
109     }
110     else
111     {
112         /* Use a art finder module to find the URL */
113         return VLC_EGENERIC;
114     }
115 }
116
117 /**
118  * Download the art using the URL or an art downloaded
119  * This function should be called only if data is not already in cache
120  */
121 int input_DownloadAndCacheArt( vlc_object_t *p_parent, input_item_t *p_item )
122 {
123     int i_status = VLC_EGENERIC;
124     int i_ret;
125     struct stat a;
126     stream_t *p_stream;
127     char psz_filename[MAX_PATH], psz_dir[MAX_PATH];
128     char *psz_artist;
129     char *psz_album;
130     char *psz_type;
131     psz_artist = p_item->p_meta->psz_artist;
132     psz_album = p_item->p_meta->psz_album;
133
134     /* You dummy ! How am I supposed to download NULL ? */
135     if( !p_item->p_meta || !p_item->p_meta->psz_arturl
136                         || !*p_item->p_meta->psz_arturl )
137         return VLC_EGENERIC;
138
139     /* Todo: get a helper to do this */
140     snprintf( psz_filename, MAX_PATH,
141               "file://%s" DIR_SEP CONFIG_DIR DIR_SEP "art"
142               DIR_SEP "%s" DIR_SEP "%s" DIR_SEP "art%s",
143               p_parent->p_libvlc->psz_homedir,
144               psz_artist, psz_album, psz_type );
145
146     snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR,
147               p_parent->p_libvlc->psz_homedir );
148     utf8_mkdir( psz_dir );
149     snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP "art",
150               p_parent->p_libvlc->psz_homedir );
151     utf8_mkdir( psz_dir );
152     snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP
153               "art" DIR_SEP "%s",
154                  p_parent->p_libvlc->psz_homedir, psz_artist );
155     utf8_mkdir( psz_dir );
156     snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP
157               "art" DIR_SEP "%s" DIR_SEP "%s",
158                       p_parent->p_libvlc->psz_homedir,
159                       psz_artist, psz_album );
160     utf8_mkdir( psz_dir );
161
162     /* Todo: check for stuff that needs a downloader module */
163     p_stream = stream_UrlNew( p_parent, p_item->p_meta->psz_arturl );
164
165     if( p_stream )
166     {
167         void *p_buffer = malloc( 1<<16 );
168         long int l_read;
169         FILE *p_file = utf8_fopen( psz_filename+7, "w" );
170         while( ( l_read = stream_Read( p_stream, p_buffer, 1<<16 ) ) )
171         {
172             fwrite( p_buffer, l_read, 1, p_file );
173         }
174         free( p_buffer );
175         fclose( p_file );
176         stream_Delete( p_stream );
177         msg_Dbg( p_parent, "Album art saved to %s\n", psz_filename );
178         free( p_item->p_meta->psz_arturl );
179         p_item->p_meta->psz_arturl = strdup( psz_filename );
180         i_status = VLC_SUCCESS;
181     }
182     return i_status;
183 }