]> git.sesse.net Git - vlc/blob - src/playlist/art.c
Fix xspf reading/writing
[vlc] / src / playlist / art.c
1 /*****************************************************************************
2  * art.c : Art metadata handling
3  *****************************************************************************
4  * Copyright (C) 1998-2008 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 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
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 <limits.h>                                             /* PATH_MAX */
36
37 #ifdef HAVE_SYS_STAT_H
38 #   include <sys/stat.h>
39 #endif
40
41 #include "../libvlc.h"
42 #include "playlist_internal.h"
43
44 static void ArtCacheCreateDir( const char *psz_dir )
45 {
46     char newdir[strlen( psz_dir ) + 1];
47     strcpy( newdir, psz_dir );
48     char * psz_newdir = newdir;
49     char * psz = psz_newdir;
50
51     while( *psz )
52     {
53         while( *psz && *psz != DIR_SEP_CHAR) psz++;
54         if( !*psz ) break;
55         *psz = 0;
56         if( !EMPTY_STR( psz_newdir ) )
57             utf8_mkdir( psz_newdir, 0700 );
58         *psz = DIR_SEP_CHAR;
59         psz++;
60     }
61     utf8_mkdir( psz_dir, 0700 );
62 }
63
64 static void ArtCacheGetDirPath( char *psz_dir,
65                                 const char *psz_title,
66                                 const char *psz_artist, const char *psz_album )
67 {
68     char *psz_cachedir = config_GetCacheDir();
69
70     if( !EMPTY_STR(psz_artist) && !EMPTY_STR(psz_album) )
71     {
72         char *psz_album_sanitized = filename_sanitize( psz_album );
73         char *psz_artist_sanitized = filename_sanitize( psz_artist );
74         snprintf( psz_dir, PATH_MAX, "%s" DIR_SEP
75                   "art" DIR_SEP "artistalbum" DIR_SEP "%s" DIR_SEP "%s",
76                   psz_cachedir, psz_artist_sanitized, psz_album_sanitized );
77         free( psz_album_sanitized );
78         free( psz_artist_sanitized );
79     }
80     else
81     {
82         char * psz_title_sanitized = filename_sanitize( psz_title );
83         snprintf( psz_dir, PATH_MAX, "%s" DIR_SEP
84                   "art" DIR_SEP "title" DIR_SEP "%s",
85                   psz_cachedir, psz_title_sanitized );
86         free( psz_title_sanitized );
87     }
88     free( psz_cachedir );
89 }
90
91 static char *ArtCachePath( input_item_t *p_item )
92 {
93     char psz_path[PATH_MAX+1]; /* FIXME */
94
95     vlc_mutex_lock( &p_item->lock );
96
97     if( !p_item->p_meta )
98         p_item->p_meta = vlc_meta_New();
99     if( !p_item->p_meta )
100     {
101         vlc_mutex_unlock( &p_item->lock );
102         return NULL;
103     }
104
105     const char *psz_artist = vlc_meta_Get( p_item->p_meta, vlc_meta_Artist );
106     const char *psz_album = vlc_meta_Get( p_item->p_meta, vlc_meta_Album );
107     const char *psz_title = vlc_meta_Get( p_item->p_meta, vlc_meta_Title );
108
109     if( !psz_title )
110         psz_title = p_item->psz_name;
111
112     if( (!psz_artist || !psz_album ) && !psz_title )
113     {
114         vlc_mutex_unlock( &p_item->lock );
115         return NULL;
116     }
117
118     ArtCacheGetDirPath( psz_path, psz_title, psz_artist, psz_album );
119
120     vlc_mutex_unlock( &p_item->lock );
121
122     return strdup( psz_path );
123 }
124
125 static char *ArtCacheName( input_item_t *p_item, const char *psz_type )
126 {
127     char *psz_path = ArtCachePath( p_item );
128     if( !psz_path )
129         return NULL;
130
131     ArtCacheCreateDir( psz_path );
132
133     char *psz_ext = filename_sanitize( psz_type ? psz_type : "" );
134     char *psz_filename;
135     if( asprintf( &psz_filename, "%s" DIR_SEP "art%s", psz_path, psz_ext ) < 0 )
136         psz_filename = NULL;
137
138     free( psz_ext );
139     free( psz_path );
140
141     return psz_filename;
142 }
143
144 /* */
145 int playlist_FindArtInCache( input_item_t *p_item )
146 {
147     char *psz_path = ArtCachePath( p_item );
148
149     if( !psz_path )
150         return VLC_EGENERIC;
151
152     /* Check if file exists */
153     DIR *p_dir = utf8_opendir( psz_path );
154     if( !p_dir )
155     {
156         free( psz_path );
157         return VLC_EGENERIC;
158     }
159
160     bool b_found = false;
161     char *psz_filename;
162     while( !b_found && (psz_filename = utf8_readdir( p_dir )) )
163     {
164         if( !strncmp( psz_filename, "art", 3 ) )
165         {
166             char *psz_file;
167             if( asprintf( &psz_file, "%s" DIR_SEP "%s",
168                           psz_path, psz_filename ) < 0 )
169                 psz_file = NULL;
170             if( psz_file )
171             {
172                 char *psz_uri = make_URI( psz_file );
173                 if( psz_uri )
174                 {
175                     input_item_SetArtURL( p_item, psz_uri );
176                     free( psz_uri );
177                 }
178                 free( psz_file );
179             }
180
181             b_found = true;
182         }
183         free( psz_filename );
184     }
185
186     /* */
187     closedir( p_dir );
188     free( psz_path );
189     return b_found ? VLC_SUCCESS : VLC_EGENERIC;
190 }
191
192
193 /* */
194 int playlist_SaveArt( playlist_t *p_playlist, input_item_t *p_item,
195                       const uint8_t *p_buffer, int i_buffer, const char *psz_type )
196 {
197     char *psz_filename = ArtCacheName( p_item, psz_type );
198
199     if( !psz_filename )
200         return VLC_EGENERIC;
201
202     char *psz_uri = make_URI( psz_filename );
203     if( !psz_uri )
204     {
205         free( psz_filename );
206         return VLC_EGENERIC;
207     }
208
209     /* Check if we already dumped it */
210     struct stat s;
211     if( !utf8_stat( psz_filename, &s ) )
212     {
213         input_item_SetArtURL( p_item, psz_uri );
214         free( psz_filename );
215         free( psz_uri );
216         return VLC_SUCCESS;
217     }
218
219     /* Dump it otherwise */
220     FILE *f = utf8_fopen( psz_filename, "wb" );
221     if( f )
222     {
223         if( fwrite( p_buffer, i_buffer, 1, f ) != 1 )
224         {
225             msg_Err( p_playlist, "%s: %m", psz_filename );
226         }
227         else
228         {
229             msg_Dbg( p_playlist, "album art saved to %s", psz_filename );
230             input_item_SetArtURL( p_item, psz_uri );
231         }
232         fclose( f );
233     }
234     free( psz_filename );
235     free( psz_uri );
236     return VLC_SUCCESS;
237 }
238