Audacious  $Id:Doxyfile42802007-03-2104:39:00Znenolod$
ui_albumart.c
Go to the documentation of this file.
1 /*
2  * ui_albumart.c
3  * Copyright 2006 Michael Hanselmann and Yoshiki Yazawa
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions, and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions, and the following disclaimer in the documentation
13  * provided with the distribution.
14  *
15  * This software is provided "as is" and without any warranty, express or
16  * implied. In no event shall the authors be liable for any damages arising from
17  * the use of this software.
18  */
19 
20 #include <glib.h>
21 #include <string.h>
22 
23 #include <libaudcore/audstrings.h>
24 
25 #include "config.h"
26 #include "i18n.h"
27 #include "misc.h"
28 
29 static bool_t
31 {
32  char *ext;
33 
34  ext = strrchr(name, '.');
35  if (!ext) {
36  /* No file extension */
37  return FALSE;
38  }
39 
40  return g_ascii_strcasecmp(ext, ".jpg") == 0 ||
41  g_ascii_strcasecmp(ext, ".jpeg") == 0 ||
42  g_ascii_strcasecmp(ext, ".png") == 0;
43 }
44 
45 static bool_t
46 cover_name_filter(const char *name, const char *filter, const bool_t ret_on_empty)
47 {
48  bool_t result = FALSE;
49  char **splitted;
50  char *current;
51  char *lname;
52  int i;
53 
54  if (!filter || strlen(filter) == 0) {
55  return ret_on_empty;
56  }
57 
58  splitted = g_strsplit(filter, ",", 0);
59  lname = g_ascii_strdown (name, -1);
60 
61  for (i = 0; ! result && (current = splitted[i]); i ++)
62  {
63  char * stripped = g_strstrip (g_ascii_strdown (current, -1));
64  result = result || strstr(lname, stripped);
65  g_free(stripped);
66  }
67 
68  g_free(lname);
69  g_strfreev(splitted);
70 
71  return result;
72 }
73 
74 /* Check wether it's an image we want */
75 static bool_t is_front_cover_image (const char * file)
76 {
77  char * include = get_string (NULL, "cover_name_include");
78  char * exclude = get_string (NULL, "cover_name_exclude");
79  bool_t accept = cover_name_filter (file, include, TRUE) &&
80  ! cover_name_filter (file, exclude, FALSE);
81  g_free (include);
82  g_free (exclude);
83  return accept;
84 }
85 
86 static bool_t
87 is_file_image(const char *imgfile, const char *file_name)
88 {
89  char *imgfile_ext, *file_name_ext;
90  size_t imgfile_len, file_name_len;
91 
92  imgfile_ext = strrchr(imgfile, '.');
93  if (!imgfile_ext) {
94  /* No file extension */
95  return FALSE;
96  }
97 
98  file_name_ext = strrchr(file_name, '.');
99  if (!file_name_ext) {
100  /* No file extension */
101  return FALSE;
102  }
103 
104  imgfile_len = (imgfile_ext - imgfile);
105  file_name_len = (file_name_ext - file_name);
106 
107  if (imgfile_len == file_name_len) {
108  return (g_ascii_strncasecmp(imgfile, file_name, imgfile_len) == 0);
109  } else {
110  return FALSE;
111  }
112 }
113 
114 static char * fileinfo_recursive_get_image (const char * path, const char *
115  file_name, int depth)
116 {
117  GDir *d;
118 
119  if (get_bool (NULL, "recurse_for_cover") && depth > get_int (NULL, "recurse_for_cover_depth"))
120  return NULL;
121 
122  d = g_dir_open(path, 0, NULL);
123 
124  if (d) {
125  const char *f;
126 
127  if (get_bool (NULL, "use_file_cover") && file_name)
128  {
129  /* Look for images matching file name */
130  while((f = g_dir_read_name(d))) {
131  char *newpath = g_strconcat(path, "/", f, NULL);
132 
133  if (!g_file_test(newpath, G_FILE_TEST_IS_DIR) &&
135  is_file_image(f, file_name)) {
136  g_dir_close(d);
137  return newpath;
138  }
139 
140  g_free(newpath);
141  }
142  g_dir_rewind(d);
143  }
144 
145  /* Search for files using filter */
146  while ((f = g_dir_read_name(d))) {
147  char *newpath = g_strconcat(path, "/", f, NULL);
148 
149  if (!g_file_test(newpath, G_FILE_TEST_IS_DIR) &&
152  g_dir_close(d);
153  return newpath;
154  }
155 
156  g_free(newpath);
157  }
158  g_dir_rewind(d);
159 
160  /* checks whether recursive or not. */
161  if (! get_bool (NULL, "recurse_for_cover"))
162  {
163  g_dir_close(d);
164  return NULL;
165  }
166 
167  /* Descend into directories recursively. */
168  while ((f = g_dir_read_name(d))) {
169  char *newpath = g_strconcat(path, "/", f, NULL);
170 
171  if(g_file_test(newpath, G_FILE_TEST_IS_DIR)) {
172  char *tmp = fileinfo_recursive_get_image(newpath,
173  NULL, depth + 1);
174  if(tmp) {
175  g_free(newpath);
176  g_dir_close(d);
177  return tmp;
178  }
179  }
180 
181  g_free(newpath);
182  }
183 
184  g_dir_close(d);
185  }
186 
187  return NULL;
188 }
189 
190 char * get_associated_image_file (const char * filename)
191 {
192  if (strncmp (filename, "file://", 7))
193  return NULL;
194 
195  char * unesc = uri_to_filename (filename);
196  if (! unesc)
197  return NULL;
198 
199  char * path = g_path_get_dirname (unesc);
200  char * base = g_path_get_basename (unesc);
201  char * image_file = fileinfo_recursive_get_image (path, base, 0);
202 
203  g_free (unesc);
204  g_free (path);
205  g_free (base);
206  return image_file;
207 }