]> git.sesse.net Git - vlc/blob - modules/meta_engine/folder.c
added start playback in paused mode (#2936)
[vlc] / modules / meta_engine / folder.c
1 /*****************************************************************************
2  * folder.c
3  *****************************************************************************
4  * Copyright (C) 2006 VLC authors and VideoLAN
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 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.
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 Lesser General Public License for more details.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <sys/stat.h>
33
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>
40
41 static const char* cover_files[] = {
42     "Folder.jpg",           /* Windows */
43     "AlbumArtSmall.jpg",    /* Windows */
44     "AlbumArt.jpg",         /* Windows */
45     "Album.jpg",
46     ".folder.png",          /* KDE?    */
47     "cover.jpg",            /* rockbox */
48     "thumb.jpg",
49 };
50
51 static const int i_covers = (sizeof(cover_files)/sizeof(cover_files[0]));
52
53 /*****************************************************************************
54  * Local prototypes
55  *****************************************************************************/
56 static int FindMeta( vlc_object_t * );
57
58 /*****************************************************************************
59  * Module descriptor
60  *****************************************************************************/
61
62 vlc_module_begin ()
63     set_shortname( N_( "Folder" ) )
64     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     set_capability( "art finder", 90 )
68     set_callbacks( FindMeta, NULL )
69 vlc_module_end ()
70
71 /*****************************************************************************
72  *****************************************************************************/
73 static int FindMeta( vlc_object_t *p_this )
74 {
75     meta_fetcher_t *p_finder = (meta_fetcher_t *)p_this;
76     input_item_t *p_item = p_finder->p_item;
77     bool b_have_art = false;
78     struct stat statinfo;
79     char *psz_path = NULL;
80
81     if( !p_item )
82         return VLC_EGENERIC;
83
84     char *psz_uri = input_item_GetURI( p_item );
85     if( !psz_uri )
86         return VLC_EGENERIC;
87
88     if ( *psz_uri && psz_uri[strlen( psz_uri ) - 1] != DIR_SEP_CHAR )
89     {
90         if ( asprintf( &psz_path, "%s"DIR_SEP, psz_uri ) == -1 )
91         {
92             free( psz_uri );
93             return VLC_EGENERIC;
94         }
95         char *psz_basedir = make_path( psz_path );
96         FREENULL( psz_path );
97         if( psz_basedir == NULL )
98         {
99             free( psz_uri );
100             return VLC_EGENERIC;
101         }
102         if( vlc_stat( psz_basedir, &statinfo ) == 0 && S_ISDIR(statinfo.st_mode) )
103             psz_path = psz_basedir;
104         else
105             free( psz_basedir );
106     }
107
108     if ( psz_path == NULL )
109     {
110         char *psz_basedir = make_path( psz_uri );
111         if( psz_basedir == NULL )
112         {
113             free( psz_uri );
114             return VLC_EGENERIC;
115         }
116
117         char *psz_buf = strrchr( psz_basedir, DIR_SEP_CHAR );
118         if( psz_buf )
119             *++psz_buf = '\0';
120         else
121             *psz_basedir = '\0'; /* relative path */
122         psz_path = psz_basedir;
123     }
124
125     free( psz_uri );
126
127     for( int i = -1; !b_have_art && i < i_covers; i++ )
128     {
129         const char *filename;
130         char *filebuf, *filepath;
131
132         if( i == -1 ) /* higher priority : configured filename */
133         {
134             filebuf = var_InheritString( p_this, "album-art-filename" );
135             if( filebuf == NULL )
136                 continue;
137             filename = filebuf;
138         }
139         else
140         {
141             filename = cover_files[i];
142             filebuf = NULL;
143         }
144
145         if( asprintf( &filepath, "%s%s", psz_path, filename ) == -1 )
146             filepath = NULL;
147         free( filebuf );
148         if( unlikely(filepath == NULL) )
149             continue;
150
151         if( vlc_stat( filepath, &statinfo ) == 0 && S_ISREG(statinfo.st_mode) )
152         {
153             char *psz_uri = vlc_path2uri( filepath, "file" );
154             if( psz_uri )
155             {
156                 input_item_SetArtURL( p_item, psz_uri );
157                 free( psz_uri );
158                 b_have_art = true;
159             }
160         }
161         free( filepath );
162     }
163     free( psz_path );
164
165     return b_have_art ? VLC_SUCCESS : VLC_EGENERIC;
166 }