misc: medialibrary: ctx does not need dynamic lifetime
[vlc.git] / modules / meta_engine / folder.c
blob7133f06ecb0ce79b4795f266a8da01a235951651
1 /*****************************************************************************
2 * folder.c
3 *****************************************************************************
4 * Copyright (C) 2006 VLC authors and VideoLAN
5 * $Id$
7 * Authors: Antoine Cellerier <dionoea -at- videolan -dot- org>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
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 Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
25 * Preamble
26 *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
32 #include <sys/stat.h>
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36 #include <vlc_meta_fetcher.h>
37 #include <vlc_fs.h>
38 #include <vlc_url.h>
39 #include <vlc_input_item.h>
41 static const char cover_files[][20] = {
42 "Folder.jpg", /* Windows */
43 "Folder.png",
44 "AlbumArtSmall.jpg", /* Windows */
45 "AlbumArt.jpg", /* Windows */
46 "Album.jpg",
47 ".folder.png", /* KDE? */
48 "cover.jpg", /* rockbox */
49 "cover.png",
50 "cover.gif",
51 "front.jpg",
52 "front.png",
53 "front.gif",
54 "front.bmp",
55 "thumb.jpg",
58 /*****************************************************************************
59 * Local prototypes
60 *****************************************************************************/
61 static int FindMeta( vlc_object_t * );
63 /*****************************************************************************
64 * Module descriptor
65 *****************************************************************************/
67 vlc_module_begin ()
68 set_shortname( N_( "Folder" ) )
69 set_description( N_("Folder meta data") )
70 add_loadfile("album-art-filename", NULL, N_("Album art filename"),
71 N_("Filename to look for album art in current directory"))
72 set_capability( "art finder", 90 )
73 set_callbacks( FindMeta, NULL )
74 vlc_module_end ()
76 static bool ProbeArtFile(input_item_t *item,
77 const char *dirpath, const char *filename)
79 char *filepath;
80 struct stat st;
81 bool found = false;
83 if (asprintf(&filepath, "%s"DIR_SEP"%s", dirpath, filename) == -1)
84 return false;
86 if (vlc_stat(filepath, &st) == 0 && S_ISREG(st.st_mode))
88 char *url = vlc_path2uri(filepath, "file");
89 if (likely(url != NULL))
91 input_item_SetArtURL(item, url);
92 free(url);
93 found = true;
97 free(filepath);
98 return found;
101 static int FindMeta( vlc_object_t *p_this )
103 meta_fetcher_t *p_finder = (meta_fetcher_t *)p_this;
104 input_item_t *p_item = p_finder->p_item;
106 if( !p_item )
107 return VLC_EGENERIC;
109 char *psz_uri = input_item_GetURI( p_item );
110 if( !psz_uri )
111 return VLC_EGENERIC;
113 char *psz_basedir = vlc_uri2path(psz_uri);
114 free(psz_uri);
115 if (psz_basedir == NULL)
116 return VLC_EGENERIC;
118 /* If the item is an accessible directory, look for art inside it.
119 * Otherwise, look for art in the same directory. */
120 struct stat st;
121 if (vlc_stat(psz_basedir, &st) == 0 && !S_ISDIR(st.st_mode))
123 char *psz_buf = strrchr( psz_basedir, DIR_SEP_CHAR );
124 if (psz_buf != NULL)
125 *psz_buf = '\0';
128 int ret = VLC_EGENERIC;
130 char *filename = var_InheritString(p_this, "album-art-filename");
131 if (filename != NULL && ProbeArtFile(p_item, psz_basedir, filename))
132 ret = VLC_SUCCESS;
133 else
135 for (size_t i = 0; i < ARRAY_SIZE(cover_files); i++)
136 if (ProbeArtFile(p_item, psz_basedir, cover_files[i]))
138 ret = VLC_SUCCESS;
139 break;
143 free(psz_basedir);
144 return ret;