meta.c 8.17 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/*****************************************************************************
 * meta.c : Metadata handling
 *****************************************************************************
 * Copyright (C) 1998-2004 the VideoLAN team
 * $Id$
 *
 * Authors: Antoine Cellerier <dionoea@videolan.org>
 *          Clément Stenac <zorglub@videolan.org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

#include <vlc/vlc.h>
#include <vlc/input.h>
#include <vlc_meta.h>
#include "vlc_playlist.h"
#include "charset.h"

#ifdef HAVE_SYS_STAT_H
#   include <sys/stat.h>
#endif

zorglub's avatar
zorglub committed
35
int input_FindArtInCache( playlist_t *p_playlist, input_item_t *p_item );
36

zorglub's avatar
zorglub committed
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
vlc_bool_t input_MetaSatisfied( playlist_t *p_playlist, input_item_t *p_item,
                                uint32_t *pi_mandatory, uint32_t *pi_optional,
                                vlc_bool_t b_check_cache )
{
    // FIXME don't var_Stuff at each loop
    int i_policy = var_CreateGetInteger( p_playlist,     "album-art" );
    if( b_check_cache )
        input_FindArtInCache( p_playlist, p_item );

    *pi_mandatory = VLC_META_ENGINE_TITLE | VLC_META_ENGINE_ARTIST |
                    (i_policy == ALBUM_ART_ALL ? VLC_META_ENGINE_ART_URL : 0 );

    uint32_t i_meta = input_CurrentMetaFlags( p_item->p_meta );
    *pi_mandatory &= ~i_meta;
    *pi_optional = 0; /// Todo
    return *pi_mandatory ? VLC_FALSE:VLC_TRUE;
}

int input_MetaFetch( playlist_t *p_playlist, input_item_t *p_item )
56
57
{
    struct meta_engine_t *p_me;
zorglub's avatar
zorglub committed
58
    uint32_t i_mandatory, i_optional;
59

60
    if( !p_item->p_meta ) return VLC_EGENERIC;
61

zorglub's avatar
zorglub committed
62
63
    input_MetaSatisfied( p_playlist, p_item,
                         &i_mandatory, &i_optional, VLC_FALSE );
64

zorglub's avatar
zorglub committed
65
66
    // Meta shouldn't magically appear
    assert( i_mandatory );
67

zorglub's avatar
zorglub committed
68
    p_me = vlc_object_create( p_playlist, VLC_OBJECT_META_ENGINE );
69
70
71
72
    p_me->i_flags |= OBJECT_FLAGS_NOINTERACT;
    p_me->i_mandatory = i_mandatory;
    p_me->i_optional = i_optional;

73
74
    p_me->p_item = p_item;
    p_me->p_module = module_Need( p_me, "meta fetcher", 0, VLC_FALSE );
zorglub's avatar
zorglub committed
75
    vlc_object_attach( p_me, p_playlist );
76
77
    if( !p_me->p_module )
    {
zorglub's avatar
zorglub committed
78
        msg_Dbg( p_playlist, "unable to fetch meta" );
79
80
81
82
83
84
85
        vlc_object_detach( p_me );
        vlc_object_destroy( p_me );
        return VLC_EGENERIC;
    }

    module_Unneed( p_me, p_me->p_module );

86
    vlc_object_detach( p_me );
87
88
89
90
91
    vlc_object_destroy( p_me );

    return VLC_SUCCESS;
}

zorglub's avatar
zorglub committed
92
int input_ArtFetch( playlist_t *p_playlist, input_item_t *p_item )
dionoea's avatar
dionoea committed
93
{
zorglub's avatar
zorglub committed
94
    if( !p_item->p_meta ) return VLC_EGENERIC;
dionoea's avatar
dionoea committed
95
96
97

    if( !p_item->p_meta->psz_arturl || !*p_item->p_meta->psz_arturl )
    {
zorglub's avatar
zorglub committed
98
99
100
101
102
103
104
        module_t *p_module;
        PL_LOCK;
        p_playlist->p_private = p_item;
        p_module = module_Need( p_playlist, "art finder", 0, VLC_FALSE );
        if( !p_module )
        {
            msg_Dbg( p_playlist, "unable to find art" );
105
            PL_UNLOCK;
zorglub's avatar
zorglub committed
106
107
108
109
            return VLC_EGENERIC;
        }
        module_Unneed( p_playlist, p_module );
        p_playlist->p_private = NULL;
110
        PL_UNLOCK;
zorglub's avatar
zorglub committed
111
112
113

        if( !p_item->p_meta->psz_arturl || !*p_item->p_meta->psz_arturl )
            return VLC_EGENERIC;
dionoea's avatar
dionoea committed
114
    }
zorglub's avatar
zorglub committed
115
    return input_DownloadAndCacheArt( p_playlist, p_item );
dionoea's avatar
dionoea committed
116
117
}

118
119
120
#ifndef MAX_PATH
#   define MAX_PATH 250
#endif
zorglub's avatar
zorglub committed
121
int input_FindArtInCache( playlist_t *p_playlist, input_item_t *p_item )
122
123
124
125
{
    char *psz_artist;
    char *psz_album;
    char psz_filename[MAX_PATH];
126
    int i;
127
    struct stat a;
dionoea's avatar
dionoea committed
128
    const char *ppsz_type[] = { ".jpg", ".png", ".gif", ".bmp", "" };
129
130
131
132
133
134

    if( !p_item->p_meta ) return VLC_EGENERIC;

    psz_artist = p_item->p_meta->psz_artist;
    psz_album = p_item->p_meta->psz_album;

135
    for( i = 0; i < 5; i++ )
136
    {
137
138
139
        snprintf( psz_filename, MAX_PATH,
                  "file://%s" DIR_SEP CONFIG_DIR DIR_SEP "art"
                  DIR_SEP "%s" DIR_SEP "%s" DIR_SEP "art%s",
zorglub's avatar
zorglub committed
140
                  p_playlist->p_libvlc->psz_homedir,
dionoea's avatar
dionoea committed
141
                  psz_artist, psz_album, ppsz_type[i] );
142
143
144
145

        /* Check if file exists */
        if( utf8_stat( psz_filename+7, &a ) == 0 )
        {
dionoea's avatar
dionoea committed
146
            vlc_meta_SetArtURL( p_item->p_meta, psz_filename );
147
148
            return VLC_SUCCESS;
        }
149
    }
150
151

    return VLC_EGENERIC;
152
153
154
155
156
157
}

/**
 * Download the art using the URL or an art downloaded
 * This function should be called only if data is not already in cache
 */
zorglub's avatar
zorglub committed
158
int input_DownloadAndCacheArt( playlist_t *p_playlist, input_item_t *p_item )
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
{
    int i_status = VLC_EGENERIC;
    stream_t *p_stream;
    char psz_filename[MAX_PATH], psz_dir[MAX_PATH];
    char *psz_artist;
    char *psz_album;
    char *psz_type;
    psz_artist = p_item->p_meta->psz_artist;
    psz_album = p_item->p_meta->psz_album;

    /* You dummy ! How am I supposed to download NULL ? */
    if( !p_item->p_meta || !p_item->p_meta->psz_arturl
                        || !*p_item->p_meta->psz_arturl )
        return VLC_EGENERIC;

174
175
176
177
178
179
180
181
182
183
    /* FIXME: use an alternate saving filename scheme if we don't have
     * the artist or album name */
    if(    !p_item->p_meta->psz_artist
        || !p_item->p_meta->psz_album )
        return VLC_EGENERIC;

    /* Check if file doesn't already exist */
    if( input_FindArtInCache( p_playlist, p_item ) == VLC_SUCCESS )
        return VLC_SUCCESS;

184
185
    psz_type = strrchr( p_item->p_meta->psz_arturl, '.' );

186
187
188
189
    /* Todo: get a helper to do this */
    snprintf( psz_filename, MAX_PATH,
              "file://%s" DIR_SEP CONFIG_DIR DIR_SEP "art"
              DIR_SEP "%s" DIR_SEP "%s" DIR_SEP "art%s",
zorglub's avatar
zorglub committed
190
              p_playlist->p_libvlc->psz_homedir,
191
192
193
              psz_artist, psz_album, psz_type );

    snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR,
zorglub's avatar
zorglub committed
194
              p_playlist->p_libvlc->psz_homedir );
195
196
    utf8_mkdir( psz_dir );
    snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP "art",
zorglub's avatar
zorglub committed
197
              p_playlist->p_libvlc->psz_homedir );
198
199
200
    utf8_mkdir( psz_dir );
    snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP
              "art" DIR_SEP "%s",
zorglub's avatar
zorglub committed
201
                 p_playlist->p_libvlc->psz_homedir, psz_artist );
202
203
204
    utf8_mkdir( psz_dir );
    snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP
              "art" DIR_SEP "%s" DIR_SEP "%s",
zorglub's avatar
zorglub committed
205
                      p_playlist->p_libvlc->psz_homedir,
206
207
208
209
                      psz_artist, psz_album );
    utf8_mkdir( psz_dir );

    /* Todo: check for stuff that needs a downloader module */
zorglub's avatar
zorglub committed
210
    p_stream = stream_UrlNew( p_playlist, p_item->p_meta->psz_arturl );
211
212
213
214
215
216
217
218
219
220
221
222
223

    if( p_stream )
    {
        void *p_buffer = malloc( 1<<16 );
        long int l_read;
        FILE *p_file = utf8_fopen( psz_filename+7, "w" );
        while( ( l_read = stream_Read( p_stream, p_buffer, 1<<16 ) ) )
        {
            fwrite( p_buffer, l_read, 1, p_file );
        }
        free( p_buffer );
        fclose( p_file );
        stream_Delete( p_stream );
224
        msg_Dbg( p_playlist, "album art saved to %s\n", psz_filename );
225
226
227
228
229
230
        free( p_item->p_meta->psz_arturl );
        p_item->p_meta->psz_arturl = strdup( psz_filename );
        i_status = VLC_SUCCESS;
    }
    return i_status;
}
231

zorglub's avatar
zorglub committed
232
uint32_t input_CurrentMetaFlags( vlc_meta_t *p_meta )
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
{
    uint32_t i_meta = 0;

#define CHECK( a, b ) \
    if( p_meta->psz_ ## a && *p_meta->psz_ ## a ) \
        i_meta |= VLC_META_ENGINE_ ## b;

    CHECK( title, TITLE )
    CHECK( artist, ARTIST )
    CHECK( genre, GENRE )
    CHECK( copyright, COPYRIGHT )
    CHECK( album, COLLECTION )
    CHECK( tracknum, SEQ_NUM )
    CHECK( description, DESCRIPTION )
    CHECK( rating, RATING )
    CHECK( date, DATE )
    CHECK( url, URL )
    CHECK( language, LANGUAGE )
    CHECK( arturl, ART_URL )

    return i_meta;
}