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