]> git.sesse.net Git - vlc/blob - modules/meta_engine/folder.c
9ef715f44591e706dd2b9f8f850c5ceddb863b16
[vlc] / modules / meta_engine / folder.c
1 /*****************************************************************************
2  * folder.c
3  *****************************************************************************
4  * Copyright (C) 2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Antoine Cellerier <dionoea -at- videolan -dot- org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_playlist.h>
35 #include <vlc_charset.h>
36 #include <vlc_url.h>
37
38 #ifdef HAVE_SYS_STAT_H
39 #   include <sys/stat.h>
40 #endif
41
42 #ifndef MAX_PATH
43 #   define MAX_PATH 250
44 #endif
45
46 static const char* cover_files[] = {
47     "Folder.jpg",           /* Windows */
48     "AlbumArtSmall.jpg",    /* Windows */
49     ".folder.png",          /* KDE?    */
50     "cover.jpg",            /* rockbox */
51 };
52
53 static const int i_covers = (sizeof(cover_files)/sizeof(cover_files[0]));
54
55 /*****************************************************************************
56  * Local prototypes
57  *****************************************************************************/
58 static int FindMeta( vlc_object_t * );
59
60 /*****************************************************************************
61  * Module descriptor
62  *****************************************************************************/
63
64 vlc_module_begin ()
65     set_shortname( N_( "Folder" ) )
66     set_description( N_("Folder meta data") )
67     add_file( "album-art-filename", NULL, NULL,
68         N_("Album art filename"), N_("Filename to look for album art in current directory"), false );
69     set_capability( "art finder", 90 )
70     set_callbacks( FindMeta, NULL )
71 vlc_module_end ()
72
73 /*****************************************************************************
74  *****************************************************************************/
75 static int FindMeta( vlc_object_t *p_this )
76 {
77     input_item_t *p_item = (input_item_t *)p_this->p_private;
78     bool b_have_art = false;
79
80     int i;
81     struct stat a;
82     char psz_filename[MAX_PATH];
83
84     if( !p_item )
85         return VLC_EGENERIC;
86
87     char *psz_dir = input_item_GetURI( p_item );
88     if( !psz_dir )
89         return VLC_EGENERIC;
90
91     char *psz_path = psz_dir;
92     if( strncmp( psz_path, "file://", 7 ) || !decode_URI( psz_path + 7 ) )
93     {
94         free( psz_dir );
95         return VLC_EGENERIC;
96     }
97
98 #if defined(WIN32) && !defined(UNDER_CE)
99     psz_path += 8;
100 #else
101     psz_path += 7;
102 #endif
103
104     char *psz_buf = strrchr( psz_path, '/' );
105     if( psz_buf )
106         *++psz_buf = '\0';
107     else
108         *psz_path = '\0'; /* relative path */
109
110     for( i = -1; !b_have_art && i < i_covers; i++ )
111     {
112         if( i == -1 ) /* higher priority : configured filename */
113         {
114             char *psz_userfile = config_GetPsz( p_this, "album-art-filename" );
115             if( !psz_userfile )
116                 continue;
117             snprintf( psz_filename, MAX_PATH, "%s%s", psz_path, psz_userfile );
118             free( psz_userfile );
119         }
120         else
121             snprintf( psz_filename, MAX_PATH, "%s%s", psz_path, cover_files[i] );
122
123         if( utf8_stat( psz_filename, &a ) != -1 )
124         {
125             char *psz_uri = make_URI( psz_filename );
126             if( psz_uri )
127             {
128                 input_item_SetArtURL( p_item, psz_uri );
129                 free( psz_uri );
130                 b_have_art = true;
131             }
132         }
133     }
134
135     free( psz_dir );
136
137     return b_have_art ? VLC_SUCCESS : VLC_EGENERIC;
138 }