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