Audacious  $Id:Doxyfile42802007-03-2104:39:00Znenolod$
ui_albumart.c
Go to the documentation of this file.
00001 /*
00002  * Audacious: A cross-platform multimedia player
00003  * Copyright (c) 2007 William Pitcock, Tony Vroon, George Averill,
00004  *                    Giacomo Lozito, Derek Pomery and Yoshiki Yazawa.
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; under version 3 of the License.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program.  If not, see <http://www.gnu.org/licenses>.
00017  *
00018  * The Audacious team does not consider modular code linking to
00019  * Audacious or using our public API to be a derived work.
00020  */
00021 
00022 #include <glib.h>
00023 #include <gtk/gtk.h>
00024 #include <string.h>
00025 
00026 #include <libaudcore/audstrings.h>
00027 
00028 #include "audconfig.h"
00029 #include "config.h"
00030 #include "i18n.h"
00031 #include "misc.h"
00032 
00033 static gboolean
00034 has_front_cover_extension(const gchar *name)
00035 {
00036     char *ext;
00037 
00038     ext = strrchr(name, '.');
00039     if (!ext) {
00040         /* No file extension */
00041         return FALSE;
00042     }
00043 
00044     return g_strcasecmp(ext, ".jpg") == 0 ||
00045            g_strcasecmp(ext, ".jpeg") == 0 ||
00046            g_strcasecmp(ext, ".png") == 0;
00047 }
00048 
00049 static gboolean
00050 cover_name_filter(const gchar *name, const gchar *filter, const gboolean ret_on_empty)
00051 {
00052     gboolean result = FALSE;
00053     gchar **splitted;
00054     gchar *current;
00055     gchar *lname;
00056     gint i;
00057 
00058     if (!filter || strlen(filter) == 0) {
00059         return ret_on_empty;
00060     }
00061 
00062     splitted = g_strsplit(filter, ",", 0);
00063 
00064     lname = g_strdup(name);
00065     g_strdown(lname);
00066 
00067     for (i = 0; !result && (current = splitted[i]); i++) {
00068         gchar *stripped = g_strstrip(g_strdup(current));
00069         g_strdown(stripped);
00070 
00071         result = result || strstr(lname, stripped);
00072 
00073         g_free(stripped);
00074     }
00075 
00076     g_free(lname);
00077     g_strfreev(splitted);
00078 
00079     return result;
00080 }
00081 
00082 /* Check wether it's an image we want */
00083 static gboolean
00084 is_front_cover_image(const gchar *imgfile)
00085 {
00086     return cover_name_filter(imgfile, cfg.cover_name_include, TRUE) &&
00087            !cover_name_filter(imgfile, cfg.cover_name_exclude, FALSE);
00088 }
00089 
00090 static gboolean
00091 is_file_image(const gchar *imgfile, const gchar *file_name)
00092 {
00093     char *imgfile_ext, *file_name_ext;
00094     size_t imgfile_len, file_name_len;
00095 
00096     imgfile_ext = strrchr(imgfile, '.');
00097     if (!imgfile_ext) {
00098         /* No file extension */
00099         return FALSE;
00100     }
00101 
00102     file_name_ext = strrchr(file_name, '.');
00103     if (!file_name_ext) {
00104         /* No file extension */
00105         return FALSE;
00106     }
00107 
00108     imgfile_len = (imgfile_ext - imgfile);
00109     file_name_len = (file_name_ext - file_name);
00110 
00111     if (imgfile_len == file_name_len) {
00112         return (g_ascii_strncasecmp(imgfile, file_name, imgfile_len) == 0);
00113     } else {
00114         return FALSE;
00115     }
00116 }
00117 
00118 static gchar * fileinfo_recursive_get_image (const gchar * path, const gchar *
00119  file_name, gint depth)
00120 {
00121     GDir *d;
00122 
00123     if (cfg.recurse_for_cover && depth > cfg.recurse_for_cover_depth)
00124         return NULL;
00125 
00126     d = g_dir_open(path, 0, NULL);
00127 
00128     if (d) {
00129         const gchar *f;
00130 
00131         if (cfg.use_file_cover && file_name) {
00132             /* Look for images matching file name */
00133             while((f = g_dir_read_name(d))) {
00134                 gchar *newpath = g_strconcat(path, "/", f, NULL);
00135 
00136                 if (!g_file_test(newpath, G_FILE_TEST_IS_DIR) &&
00137                     has_front_cover_extension(f) &&
00138                     is_file_image(f, file_name)) {
00139                     g_dir_close(d);
00140                     return newpath;
00141                 }
00142 
00143                 g_free(newpath);
00144             }
00145             g_dir_rewind(d);
00146         }
00147 
00148         /* Search for files using filter */
00149         while ((f = g_dir_read_name(d))) {
00150             gchar *newpath = g_strconcat(path, "/", f, NULL);
00151 
00152             if (!g_file_test(newpath, G_FILE_TEST_IS_DIR) &&
00153                 has_front_cover_extension(f) &&
00154                 is_front_cover_image(f)) {
00155                 g_dir_close(d);
00156                 return newpath;
00157             }
00158 
00159             g_free(newpath);
00160         }
00161         g_dir_rewind(d);
00162 
00163         /* checks whether recursive or not. */
00164         if (!cfg.recurse_for_cover) {
00165             g_dir_close(d);
00166             return NULL;
00167         }
00168 
00169         /* Descend into directories recursively. */
00170         while ((f = g_dir_read_name(d))) {
00171             gchar *newpath = g_strconcat(path, "/", f, NULL);
00172 
00173             if(g_file_test(newpath, G_FILE_TEST_IS_DIR)) {
00174                 gchar *tmp = fileinfo_recursive_get_image(newpath,
00175                     NULL, depth + 1);
00176                 if(tmp) {
00177                     g_free(newpath);
00178                     g_dir_close(d);
00179                     return tmp;
00180                 }
00181             }
00182 
00183             g_free(newpath);
00184         }
00185 
00186         g_dir_close(d);
00187     }
00188 
00189     return NULL;
00190 }
00191 
00192 gchar * get_associated_image_file (const gchar * filename)
00193 {
00194     if (strncmp (filename, "file://", 7))
00195         return NULL;
00196 
00197     gchar * unesc = uri_to_filename (filename);
00198     if (! unesc)
00199         return NULL;
00200 
00201     gchar * path = g_path_get_dirname (unesc);
00202     gchar * base = g_path_get_basename (unesc);
00203     gchar * image_file = fileinfo_recursive_get_image (path, base, 0);
00204 
00205     g_free (unesc);
00206     g_free (path);
00207     g_free (base);
00208     return image_file;
00209 }