folder.c 4.37 KB
Newer Older
1
2
3
/*****************************************************************************
 * folder.c
 *****************************************************************************
4
 * Copyright (C) 2006 VLC authors and VideoLAN
5
6
7
8
 * $Id$
 *
 * Authors: Antoine Cellerier <dionoea -at- videolan -dot- org>
 *
9
10
11
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
12
13
14
15
 * (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
16
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
18
 *
19
20
21
 * You should have received a copy of the GNU Lesser 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.
22
23
24
25
26
27
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/

28
29
30
31
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

32
#include <sys/stat.h>
33

34
#include <vlc_common.h>
35
#include <vlc_plugin.h>
36
#include <vlc_art_finder.h>
37
#include <vlc_fs.h>
ivoire's avatar
ivoire committed
38
#include <vlc_url.h>
39
#include <vlc_input_item.h>
40

41
42
43
static const char* cover_files[] = {
    "Folder.jpg",           /* Windows */
    "AlbumArtSmall.jpg",    /* Windows */
44
45
    "AlbumArt.jpg",         /* Windows */
    "Album.jpg",
46
47
    ".folder.png",          /* KDE?    */
    "cover.jpg",            /* rockbox */
48
    "thumb.jpg",
49
50
51
52
};

static const int i_covers = (sizeof(cover_files)/sizeof(cover_files[0]));

53
54
55
56
57
58
59
60
61
/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static int FindMeta( vlc_object_t * );

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/

62
63
64
vlc_module_begin ()
    set_shortname( N_( "Folder" ) )
    set_description( N_("Folder meta data") )
65
    add_loadfile( "album-art-filename", NULL,
66
        N_("Album art filename"), N_("Filename to look for album art in current directory"), false );
67
68
69
    set_capability( "art finder", 90 )
    set_callbacks( FindMeta, NULL )
vlc_module_end ()
70
71
72
73
74

/*****************************************************************************
 *****************************************************************************/
static int FindMeta( vlc_object_t *p_this )
{
75
76
    art_finder_t *p_finder = (art_finder_t *)p_this;
    input_item_t *p_item = p_finder->p_item;
77
    bool b_have_art = false;
78

79
80
81
    if( !p_item )
        return VLC_EGENERIC;

82
    char *psz_dir = input_item_GetURI( p_item );
83
84
    if( !psz_dir )
        return VLC_EGENERIC;
85

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
86
87
88
    char *psz_path = make_path( psz_dir );
    free( psz_dir );
    if( psz_path == NULL )
89
        return VLC_EGENERIC;
90

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
91
    char *psz_buf = strrchr( psz_path, DIR_SEP_CHAR );
92
93
94
95
    if( psz_buf )
        *++psz_buf = '\0';
    else
        *psz_path = '\0'; /* relative path */
96

97
    for( int i = -1; !b_have_art && i < i_covers; i++ )
98
    {
99
100
101
        const char *filename;
        char *filebuf, *filepath;

102
        if( i == -1 ) /* higher priority : configured filename */
103
        {
104
105
            filebuf = var_InheritString( p_this, "album-art-filename" );
            if( filebuf == NULL )
106
                continue;
107
            filename = filebuf;
108
        }
109
        else
110
111
112
113
114
115
116
117
        {
            filename = cover_files[i];
            filebuf = NULL;
        }

        if( asprintf( &filepath, "%s%s", psz_path, filename ) == -1 )
            filepath = NULL;
        free( filebuf );
Rafaël Carré's avatar
Rafaël Carré committed
118
        if( unlikely(filepath == NULL) )
119
            continue;
120

121
122
        struct stat dummy;
        if( vlc_stat( filepath, &dummy ) == 0 )
123
        {
124
            char *psz_uri = vlc_path2uri( filepath, "file" );
Rafaël Carré's avatar
Rafaël Carré committed
125
126
127
128
129
130
            if( psz_uri )
            {
                input_item_SetArtURL( p_item, psz_uri );
                free( psz_uri );
                b_have_art = true;
            }
131
        }
132
        free( filepath );
133
    }
ivoire's avatar
ivoire committed
134
    free( psz_path );
135

136
    return b_have_art ? VLC_SUCCESS : VLC_EGENERIC;
137
}