Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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
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
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
00099 return FALSE;
00100 }
00101
00102 file_name_ext = strrchr(file_name, '.');
00103 if (!file_name_ext) {
00104
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
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
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
00164 if (!cfg.recurse_for_cover) {
00165 g_dir_close(d);
00166 return NULL;
00167 }
00168
00169
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 }