]> git.sesse.net Git - vlc/blob - modules/meta_engine/folder.c
5d1305333594644c0ae90c79357ce9b8acbb46f0
[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_art_finder.h>
35 #include <vlc_fs.h>
36 #include <vlc_url.h>
37 #include <vlc_input_item.h>
38
39 #ifdef HAVE_SYS_STAT_H
40 #   include <sys/stat.h>
41 #endif
42
43 #ifndef MAX_PATH
44 #   define MAX_PATH 250
45 #endif
46
47 static const char* cover_files[] = {
48     "Folder.jpg",           /* Windows */
49     "AlbumArtSmall.jpg",    /* Windows */
50     ".folder.png",          /* KDE?    */
51     "cover.jpg",            /* rockbox */
52 };
53
54 static const int i_covers = (sizeof(cover_files)/sizeof(cover_files[0]));
55
56 /*****************************************************************************
57  * Local prototypes
58  *****************************************************************************/
59 static int FindMeta( vlc_object_t * );
60
61 /*****************************************************************************
62  * Module descriptor
63  *****************************************************************************/
64
65 vlc_module_begin ()
66     set_shortname( N_( "Folder" ) )
67     set_description( N_("Folder meta data") )
68     add_file( "album-art-filename", NULL, NULL,
69         N_("Album art filename"), N_("Filename to look for album art in current directory"), false );
70     set_capability( "art finder", 90 )
71     set_callbacks( FindMeta, NULL )
72 vlc_module_end ()
73
74 /*****************************************************************************
75  *****************************************************************************/
76 static int FindMeta( vlc_object_t *p_this )
77 {
78     art_finder_t *p_finder = (art_finder_t *)p_this;
79     input_item_t *p_item = p_finder->p_item;
80     bool b_have_art = false;
81
82     int i;
83     struct stat a;
84     char psz_filename[MAX_PATH];
85
86     if( !p_item )
87         return VLC_EGENERIC;
88
89     char *psz_dir = input_item_GetURI( p_item );
90     if( !psz_dir )
91         return VLC_EGENERIC;
92
93     char *psz_path = make_path( psz_dir );
94     free( psz_dir );
95     if( psz_path == NULL )
96         return VLC_EGENERIC;
97
98     char *psz_buf = strrchr( psz_path, DIR_SEP_CHAR );
99     if( psz_buf )
100         *++psz_buf = '\0';
101     else
102         *psz_path = '\0'; /* relative path */
103
104     for( i = -1; !b_have_art && i < i_covers; i++ )
105     {
106         if( i == -1 ) /* higher priority : configured filename */
107         {
108             char *psz_userfile = var_InheritString( p_this, "album-art-filename" );
109             if( !psz_userfile )
110                 continue;
111             snprintf( psz_filename, MAX_PATH, "%s%s", psz_path, psz_userfile );
112             free( psz_userfile );
113         }
114         else
115             snprintf( psz_filename, MAX_PATH, "%s%s", psz_path, cover_files[i] );
116
117         if( vlc_stat( psz_filename, &a ) != -1 )
118         {
119             char *psz_uri = make_URI( psz_filename );
120             if( psz_uri )
121             {
122                 input_item_SetArtURL( p_item, psz_uri );
123                 free( psz_uri );
124                 b_have_art = true;
125             }
126         }
127     }
128
129     return b_have_art ? VLC_SUCCESS : VLC_EGENERIC;
130 }