]> git.sesse.net Git - vlc/blobdiff - modules/meta_engine/folder.c
Revert "MKV: Always set an i_pts in a lace otherwise it disturbs seeking performance"
[vlc] / modules / meta_engine / folder.c
index 34c41eec144235c0432945f3c9b48b2792635624..38c1e075f75b5fc96b065c7cd4c97381fe7b6ed8 100644 (file)
@@ -1,47 +1,55 @@
 /*****************************************************************************
  * folder.c
  *****************************************************************************
- * Copyright (C) 2006 the VideoLAN team
+ * Copyright (C) 2006 VLC authors and VideoLAN
  * $Id$
  *
  * Authors: Antoine Cellerier <dionoea -at- videolan -dot- org>
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
  *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
 /*****************************************************************************
  * Preamble
  *****************************************************************************/
-#define _GNU_SOURCE
-#include <stdlib.h>                                      /* malloc(), free() */
-
-#include <vlc/vlc.h>
-#include <vlc_interface.h>
-#include <vlc_meta.h>
-#include <vlc_playlist.h>
-#include <vlc_input.h>
-#include <vlc_charset.h>
-
-#ifdef HAVE_SYS_STAT_H
-#   include <sys/stat.h>
-#endif
 
-#ifndef MAX_PATH
-#   define MAX_PATH 250
+#ifdef HAVE_CONFIG_H
+# include "config.h"
 #endif
 
+#include <sys/stat.h>
+
+#include <vlc_common.h>
+#include <vlc_plugin.h>
+#include <vlc_meta_fetcher.h>
+#include <vlc_fs.h>
+#include <vlc_url.h>
+#include <vlc_input_item.h>
+
+static const char* cover_files[] = {
+    "Folder.jpg",           /* Windows */
+    "AlbumArtSmall.jpg",    /* Windows */
+    "AlbumArt.jpg",         /* Windows */
+    "Album.jpg",
+    ".folder.png",          /* KDE?    */
+    "cover.jpg",            /* rockbox */
+    "thumb.jpg",
+};
+
+static const int i_covers = (sizeof(cover_files)/sizeof(cover_files[0]));
+
 /*****************************************************************************
  * Local prototypes
  *****************************************************************************/
@@ -51,73 +59,108 @@ static int FindMeta( vlc_object_t * );
  * Module descriptor
  *****************************************************************************/
 
-vlc_module_begin();
-    set_shortname( N_( "Folder" ) );
-    set_description( _("Folder meta data") );
-
-    set_capability( "art finder", 10 );
-    set_callbacks( FindMeta, NULL );
-vlc_module_end();
+vlc_module_begin ()
+    set_shortname( N_( "Folder" ) )
+    set_description( N_("Folder meta data") )
+    add_loadfile( "album-art-filename", NULL,
+        N_("Album art filename"), N_("Filename to look for album art in current directory"), false );
+    set_capability( "art finder", 90 )
+    set_callbacks( FindMeta, NULL )
+vlc_module_end ()
 
 /*****************************************************************************
  *****************************************************************************/
 static int FindMeta( vlc_object_t *p_this )
 {
-    playlist_t *p_playlist = (playlist_t *)p_this;
-    input_item_t *p_item = (input_item_t *)(p_playlist->p_private);
-    vlc_bool_t b_have_art = VLC_FALSE;
-
-    int i = 0;
-    struct stat a;
-    char psz_filename[MAX_PATH];
-    char *psz_dir = strdup( p_item->psz_uri );
-    char *psz_buf = strrchr( psz_dir, '/' );
-
-    if( !p_item->p_meta ) return VLC_EGENERIC;
-    if( psz_buf )
+    meta_fetcher_t *p_finder = (meta_fetcher_t *)p_this;
+    input_item_t *p_item = p_finder->p_item;
+    bool b_have_art = false;
+    struct stat statinfo;
+    char *psz_path = NULL;
+
+    if( !p_item )
+        return VLC_EGENERIC;
+
+    char *psz_uri = input_item_GetURI( p_item );
+    if( !psz_uri )
+        return VLC_EGENERIC;
+
+    if ( *psz_uri && psz_uri[strlen( psz_uri ) - 1] != DIR_SEP_CHAR )
     {
-        psz_buf++;
-        *psz_buf = '\0';
+        if ( asprintf( &psz_path, "%s"DIR_SEP, psz_uri ) == -1 )
+        {
+            free( psz_uri );
+            return VLC_EGENERIC;
+        }
+        char *psz_basedir = make_path( psz_path );
+        FREENULL( psz_path );
+        if( psz_basedir == NULL )
+        {
+            free( psz_uri );
+            return VLC_EGENERIC;
+        }
+        if( vlc_stat( psz_basedir, &statinfo ) == 0 && S_ISDIR(statinfo.st_mode) )
+            psz_path = psz_basedir;
+        else
+            free( psz_basedir );
     }
-    else
+
+    if ( psz_path == NULL )
     {
-        *psz_dir = '\0';
+        char *psz_basedir = make_path( psz_uri );
+        if( psz_basedir == NULL )
+        {
+            free( psz_uri );
+            return VLC_EGENERIC;
+        }
+
+        char *psz_buf = strrchr( psz_basedir, DIR_SEP_CHAR );
+        if( psz_buf )
+            *++psz_buf = '\0';
+        else
+            *psz_basedir = '\0'; /* relative path */
+        psz_path = psz_basedir;
     }
 
-    if( !strncmp( psz_dir, "file://", 7 ) )
-        psz_dir += 7;
+    free( psz_uri );
 
-    for( i = 0; b_have_art == VLC_FALSE && i < 3; i++ )
+    for( int i = -1; !b_have_art && i < i_covers; i++ )
     {
-        switch( i )
+        const char *filename;
+        char *filebuf, *filepath;
+
+        if( i == -1 ) /* higher priority : configured filename */
+        {
+            filebuf = var_InheritString( p_this, "album-art-filename" );
+            if( filebuf == NULL )
+                continue;
+            filename = filebuf;
+        }
+        else
         {
-            case 0:
-            /* Windows Folder.jpg */
-            snprintf( psz_filename, MAX_PATH,
-                      "file://%sFolder.jpg", psz_dir );
-            break;
-
-            case 1:
-            /* Windows AlbumArtSmall.jpg == small version of Folder.jpg */
-            snprintf( psz_filename, MAX_PATH,
-                  "file://%sAlbumArtSmall.jpg", psz_dir );
-            break;
-
-            case 2:
-            /* KDE (?) .folder.png */
-            snprintf( psz_filename, MAX_PATH,
-                  "file://%s.folder.png", psz_dir );
-            break;
+            filename = cover_files[i];
+            filebuf = NULL;
         }
 
-        if( utf8_stat( psz_filename+7, &a ) != -1 )
+        if( asprintf( &filepath, "%s%s", psz_path, filename ) == -1 )
+            filepath = NULL;
+        free( filebuf );
+        if( unlikely(filepath == NULL) )
+            continue;
+
+        if( vlc_stat( filepath, &statinfo ) == 0 && S_ISREG(statinfo.st_mode) )
         {
-            vlc_meta_SetArtURL( p_item->p_meta, psz_filename );
-            b_have_art = VLC_TRUE;
+            char *psz_uri = vlc_path2uri( filepath, "file" );
+            if( psz_uri )
+            {
+                input_item_SetArtURL( p_item, psz_uri );
+                free( psz_uri );
+                b_have_art = true;
+            }
         }
+        free( filepath );
     }
-
-    free( psz_dir );
+    free( psz_path );
 
     return b_have_art ? VLC_SUCCESS : VLC_EGENERIC;
 }