If http-use-IE-proxy is enable, use Internet Explorer entered HTTP proxy server confi...
[vlc.git] / src / playlist / art.c
blob89883a0e268a15b0a685eb00240461fb69d31e00
1 /*****************************************************************************
2 * art.c : Art metadata handling
3 *****************************************************************************
4 * Copyright (C) 1998-2008 the VideoLAN team
5 * $Id$
7 * Authors: Antoine Cellerier <dionoea@videolan.org>
8 * Clément Stenac <zorglub@videolan.org
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.
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.
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 *****************************************************************************/
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
29 #include <assert.h>
30 #include <vlc_common.h>
31 #include <vlc_playlist.h>
32 #include <vlc_charset.h>
33 #include <vlc_strings.h>
34 #include <vlc_stream.h>
35 #include <vlc_url.h>
37 #include <limits.h> /* PATH_MAX */
39 #ifdef HAVE_SYS_STAT_H
40 # include <sys/stat.h>
41 #endif
43 #include "../libvlc.h"
44 #include "playlist_internal.h"
46 static void ArtCacheCreateDir( const char *psz_dir )
48 char newdir[strlen( psz_dir ) + 1];
49 strcpy( newdir, psz_dir );
50 char * psz_newdir = newdir;
51 char * psz = psz_newdir;
53 while( *psz )
55 while( *psz && *psz != DIR_SEP_CHAR) psz++;
56 if( !*psz ) break;
57 *psz = 0;
58 if( !EMPTY_STR( psz_newdir ) )
59 utf8_mkdir( psz_newdir, 0700 );
60 *psz = DIR_SEP_CHAR;
61 psz++;
63 utf8_mkdir( psz_dir, 0700 );
66 static char* ArtCacheGetDirPath( const char *psz_title, const char *psz_artist,
67 const char *psz_album )
69 char *psz_dir;
70 char *psz_cachedir = config_GetUserDir(VLC_CACHE_DIR);
72 if( !EMPTY_STR(psz_artist) && !EMPTY_STR(psz_album) )
74 char *psz_album_sanitized = filename_sanitize( psz_album );
75 char *psz_artist_sanitized = filename_sanitize( psz_artist );
76 if( asprintf( &psz_dir, "%s" DIR_SEP "art" DIR_SEP "artistalbum"
77 DIR_SEP "%s" DIR_SEP "%s", psz_cachedir,
78 psz_artist_sanitized, psz_album_sanitized ) == -1 )
79 psz_dir = NULL;
80 free( psz_album_sanitized );
81 free( psz_artist_sanitized );
83 else
85 char * psz_title_sanitized = filename_sanitize( psz_title );
86 if( asprintf( &psz_dir, "%s" DIR_SEP "art" DIR_SEP "title" DIR_SEP
87 "%s", psz_cachedir, psz_title_sanitized ) == -1 )
88 psz_dir = NULL;
89 free( psz_title_sanitized );
91 free( psz_cachedir );
92 return psz_dir;
95 static char *ArtCachePath( input_item_t *p_item )
97 char* psz_path = NULL;
98 const char *psz_artist;
99 const char *psz_album;
100 const char *psz_title;
102 vlc_mutex_lock( &p_item->lock );
104 if( !p_item->p_meta )
105 p_item->p_meta = vlc_meta_New();
106 if( !p_item->p_meta )
107 goto end;
109 psz_artist = vlc_meta_Get( p_item->p_meta, vlc_meta_Artist );
110 psz_album = vlc_meta_Get( p_item->p_meta, vlc_meta_Album );
111 psz_title = vlc_meta_Get( p_item->p_meta, vlc_meta_Title );
113 if( !psz_title )
114 psz_title = p_item->psz_name;
116 if( (!psz_artist || !psz_album ) && !psz_title )
117 goto end;
119 psz_path = ArtCacheGetDirPath( psz_title, psz_artist, psz_album );
121 end:
122 vlc_mutex_unlock( &p_item->lock );
123 return psz_path;
126 static char *ArtCacheName( input_item_t *p_item, const char *psz_type )
128 char *psz_path = ArtCachePath( p_item );
129 if( !psz_path )
130 return NULL;
132 ArtCacheCreateDir( psz_path );
134 char *psz_ext = filename_sanitize( psz_type ? psz_type : "" );
135 char *psz_filename;
136 if( asprintf( &psz_filename, "%s" DIR_SEP "art%s", psz_path, psz_ext ) < 0 )
137 psz_filename = NULL;
139 free( psz_ext );
140 free( psz_path );
142 return psz_filename;
145 /* */
146 int playlist_FindArtInCache( input_item_t *p_item )
148 char *psz_path = ArtCachePath( p_item );
150 if( !psz_path )
151 return VLC_EGENERIC;
153 /* Check if file exists */
154 DIR *p_dir = utf8_opendir( psz_path );
155 if( !p_dir )
157 free( psz_path );
158 return VLC_EGENERIC;
161 bool b_found = false;
162 char *psz_filename;
163 while( !b_found && (psz_filename = utf8_readdir( p_dir )) )
165 if( !strncmp( psz_filename, "art", 3 ) )
167 char *psz_file;
168 if( asprintf( &psz_file, "%s" DIR_SEP "%s",
169 psz_path, psz_filename ) != -1 )
171 char *psz_uri = make_URI( psz_file );
172 if( psz_uri )
174 input_item_SetArtURL( p_item, psz_uri );
175 free( psz_uri );
177 free( psz_file );
180 b_found = true;
182 free( psz_filename );
185 /* */
186 closedir( p_dir );
187 free( psz_path );
188 return b_found ? VLC_SUCCESS : VLC_EGENERIC;
192 /* */
193 int playlist_SaveArt( playlist_t *p_playlist, input_item_t *p_item,
194 const uint8_t *p_buffer, int i_buffer, const char *psz_type )
196 char *psz_filename = ArtCacheName( p_item, psz_type );
198 if( !psz_filename )
199 return VLC_EGENERIC;
201 char *psz_uri = make_URI( psz_filename );
202 if( !psz_uri )
204 free( psz_filename );
205 return VLC_EGENERIC;
208 /* Check if we already dumped it */
209 struct stat s;
210 if( !utf8_stat( psz_filename, &s ) )
212 input_item_SetArtURL( p_item, psz_uri );
213 free( psz_filename );
214 free( psz_uri );
215 return VLC_SUCCESS;
218 /* Dump it otherwise */
219 FILE *f = utf8_fopen( psz_filename, "wb" );
220 if( f )
222 if( fwrite( p_buffer, i_buffer, 1, f ) != 1 )
224 msg_Err( p_playlist, "%s: %m", psz_filename );
226 else
228 msg_Dbg( p_playlist, "album art saved to %s", psz_filename );
229 input_item_SetArtURL( p_item, psz_uri );
231 fclose( f );
233 free( psz_filename );
234 free( psz_uri );
235 return VLC_SUCCESS;