Audacious  $Id:Doxyfile42802007-03-2104:39:00Znenolod$
ui_albumart.c
Go to the documentation of this file.
1 /*
2  * Audacious: A cross-platform multimedia player
3  * Copyright (c) 2007 William Pitcock, Tony Vroon, George Averill,
4  * Giacomo Lozito, Derek Pomery and Yoshiki Yazawa.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; under version 3 of the License.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses>.
17  *
18  * The Audacious team does not consider modular code linking to
19  * Audacious or using our public API to be a derived work.
20  */
21 
22 #include <glib.h>
23 #include <string.h>
24 
25 #include <libaudcore/audstrings.h>
26 
27 #include "config.h"
28 #include "i18n.h"
29 #include "misc.h"
30 
31 static bool_t
33 {
34  char *ext;
35 
36  ext = strrchr(name, '.');
37  if (!ext) {
38  /* No file extension */
39  return FALSE;
40  }
41 
42  return g_strcasecmp(ext, ".jpg") == 0 ||
43  g_strcasecmp(ext, ".jpeg") == 0 ||
44  g_strcasecmp(ext, ".png") == 0;
45 }
46 
47 static bool_t
48 cover_name_filter(const char *name, const char *filter, const bool_t ret_on_empty)
49 {
50  bool_t result = FALSE;
51  char **splitted;
52  char *current;
53  char *lname;
54  int i;
55 
56  if (!filter || strlen(filter) == 0) {
57  return ret_on_empty;
58  }
59 
60  splitted = g_strsplit(filter, ",", 0);
61 
62  lname = g_strdup(name);
63  g_strdown(lname);
64 
65  for (i = 0; !result && (current = splitted[i]); i++) {
66  char *stripped = g_strstrip(g_strdup(current));
67  g_strdown(stripped);
68 
69  result = result || strstr(lname, stripped);
70 
71  g_free(stripped);
72  }
73 
74  g_free(lname);
75  g_strfreev(splitted);
76 
77  return result;
78 }
79 
80 /* Check wether it's an image we want */
81 static bool_t is_front_cover_image (const char * file)
82 {
83  char * include = get_string (NULL, "cover_name_include");
84  char * exclude = get_string (NULL, "cover_name_exclude");
85  bool_t accept = cover_name_filter (file, include, TRUE) &&
86  ! cover_name_filter (file, exclude, FALSE);
87  g_free (include);
88  g_free (exclude);
89  return accept;
90 }
91 
92 static bool_t
93 is_file_image(const char *imgfile, const char *file_name)
94 {
95  char *imgfile_ext, *file_name_ext;
96  size_t imgfile_len, file_name_len;
97 
98  imgfile_ext = strrchr(imgfile, '.');
99  if (!imgfile_ext) {
100  /* No file extension */
101  return FALSE;
102  }
103 
104  file_name_ext = strrchr(file_name, '.');
105  if (!file_name_ext) {
106  /* No file extension */
107  return FALSE;
108  }
109 
110  imgfile_len = (imgfile_ext - imgfile);
111  file_name_len = (file_name_ext - file_name);
112 
113  if (imgfile_len == file_name_len) {
114  return (g_ascii_strncasecmp(imgfile, file_name, imgfile_len) == 0);
115  } else {
116  return FALSE;
117  }
118 }
119 
120 static char * fileinfo_recursive_get_image (const char * path, const char *
121  file_name, int depth)
122 {
123  GDir *d;
124 
125  if (get_bool (NULL, "recurse_for_cover") && depth > get_int (NULL, "recurse_for_cover_depth"))
126  return NULL;
127 
128  d = g_dir_open(path, 0, NULL);
129 
130  if (d) {
131  const char *f;
132 
133  if (get_bool (NULL, "use_file_cover") && file_name)
134  {
135  /* Look for images matching file name */
136  while((f = g_dir_read_name(d))) {
137  char *newpath = g_strconcat(path, "/", f, NULL);
138 
139  if (!g_file_test(newpath, G_FILE_TEST_IS_DIR) &&
141  is_file_image(f, file_name)) {
142  g_dir_close(d);
143  return newpath;
144  }
145 
146  g_free(newpath);
147  }
148  g_dir_rewind(d);
149  }
150 
151  /* Search for files using filter */
152  while ((f = g_dir_read_name(d))) {
153  char *newpath = g_strconcat(path, "/", f, NULL);
154 
155  if (!g_file_test(newpath, G_FILE_TEST_IS_DIR) &&
158  g_dir_close(d);
159  return newpath;
160  }
161 
162  g_free(newpath);
163  }
164  g_dir_rewind(d);
165 
166  /* checks whether recursive or not. */
167  if (! get_bool (NULL, "recurse_for_cover"))
168  {
169  g_dir_close(d);
170  return NULL;
171  }
172 
173  /* Descend into directories recursively. */
174  while ((f = g_dir_read_name(d))) {
175  char *newpath = g_strconcat(path, "/", f, NULL);
176 
177  if(g_file_test(newpath, G_FILE_TEST_IS_DIR)) {
178  char *tmp = fileinfo_recursive_get_image(newpath,
179  NULL, depth + 1);
180  if(tmp) {
181  g_free(newpath);
182  g_dir_close(d);
183  return tmp;
184  }
185  }
186 
187  g_free(newpath);
188  }
189 
190  g_dir_close(d);
191  }
192 
193  return NULL;
194 }
195 
196 char * get_associated_image_file (const char * filename)
197 {
198  if (strncmp (filename, "file://", 7))
199  return NULL;
200 
201  char * unesc = uri_to_filename (filename);
202  if (! unesc)
203  return NULL;
204 
205  char * path = g_path_get_dirname (unesc);
206  char * base = g_path_get_basename (unesc);
207  char * image_file = fileinfo_recursive_get_image (path, base, 0);
208 
209  g_free (unesc);
210  g_free (path);
211  g_free (base);
212  return image_file;
213 }