Mercurial > audlegacy
annotate audacious/util.c @ 1179:1eef4fd1aeb5 trunk
[svn] - make the scrobbler preferences appear in the main window; this does not work yet because I need to debug apparently!
author | nenolod |
---|---|
date | Sun, 11 Jun 2006 20:33:45 -0700 |
parents | 36e8431d8e19 |
children | 03414b9d2507 |
rev | line source |
---|---|
0 | 1 /* BMP - Cross-platform multimedia player |
2 * Copyright (C) 2003-2004 BMP development team. | |
3 * | |
4 * Based on XMMS: | |
5 * Copyright (C) 1998-2003 XMMS development team. | |
6 * | |
7 * This program is free software; you can redistribute it and/or modify | |
8 * it under the terms of the GNU General Public License as published by | |
9 * the Free Software Foundation; either version 2 of the License, or | |
10 * (at your option) any later version. | |
11 * | |
12 * This program is distributed in the hope that it will be useful, | |
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 * GNU General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU General Public License | |
18 * along with this program; if not, write to the Free Software | |
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
20 */ | |
21 | |
245
1d8012046612
[svn] Convert UTF-16 (windows-1252) into something useful. Original patch by incomp@#audacious,
nenolod
parents:
86
diff
changeset
|
22 #define WEIRD_UTF_16_PLAYLIST_ENCODING |
1d8012046612
[svn] Convert UTF-16 (windows-1252) into something useful. Original patch by incomp@#audacious,
nenolod
parents:
86
diff
changeset
|
23 |
0 | 24 #ifdef HAVE_CONFIG_H |
25 # include "config.h" | |
26 #endif | |
27 | |
28 #define NEED_GLADE | |
29 #include "util.h" | |
30 | |
31 #include <glib.h> | |
32 #include <glib/gi18n.h> | |
33 #include <glade/glade.h> | |
34 #include <gtk/gtk.h> | |
35 #include <gdk/gdk.h> | |
36 #include <stdio.h> | |
37 #include <stdlib.h> | |
38 #include <string.h> | |
39 #include <ctype.h> | |
40 | |
41 #include <gdk/gdkx.h> | |
42 #include <gdk/gdkkeysyms.h> | |
43 #include <X11/Xlib.h> | |
287
2d919c756941
[svn] - attach -Iaudacious to libaudcore build crap.
nenolod
parents:
284
diff
changeset
|
44 //#include <sys/ipc.h> |
0 | 45 #include <unistd.h> |
46 #include <errno.h> | |
47 | |
48 #ifdef HAVE_FTS_H | |
49 # include <fts.h> | |
50 #endif | |
51 | |
52 #include "glade.h" | |
53 #include "input.h" | |
54 #include "main.h" | |
538
e4e897d20791
[svn] remove libaudcore, we never did anything with it
nenolod
parents:
383
diff
changeset
|
55 #include "playback.h" |
0 | 56 #include "playlist.h" |
383 | 57 #include "ui_playlist.h" |
0 | 58 |
1105
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
59 #ifdef USE_CHARDET |
1106 | 60 #include "../libguess/libguess.h" |
61 #ifdef HAVE_UDET | |
1105
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
62 #include <libudet_c.h> |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
63 #endif |
1106 | 64 #endif |
0 | 65 |
66 static GQuark quark_popup_data; | |
67 | |
68 | |
69 /* | |
70 * escape_shell_chars() | |
71 * | |
72 * Escapes characters that are special to the shell inside double quotes. | |
73 */ | |
74 | |
75 gchar * | |
76 escape_shell_chars(const gchar * string) | |
77 { | |
78 const gchar *special = "$`\"\\"; /* Characters to escape */ | |
79 const gchar *in = string; | |
80 gchar *out, *escaped; | |
81 gint num = 0; | |
82 | |
83 while (*in != '\0') | |
84 if (strchr(special, *in++)) | |
85 num++; | |
86 | |
87 escaped = g_malloc(strlen(string) + num + 1); | |
88 | |
89 in = string; | |
90 out = escaped; | |
91 | |
92 while (*in != '\0') { | |
93 if (strchr(special, *in)) | |
94 *out++ = '\\'; | |
95 *out++ = *in++; | |
96 } | |
97 *out = '\0'; | |
98 | |
99 return escaped; | |
100 } | |
101 | |
102 | |
103 /* | |
104 * find <file> in directory <dirname> or subdirectories. return | |
105 * pointer to complete filename which has to be freed by calling | |
106 * "g_free()" after use. Returns NULL if file could not be found. | |
107 */ | |
108 | |
109 typedef struct { | |
110 const gchar *to_match; | |
111 gchar *match; | |
112 gboolean found; | |
113 } FindFileContext; | |
114 | |
115 static gboolean | |
116 find_file_func(const gchar * path, const gchar * basename, gpointer data) | |
117 { | |
118 FindFileContext *context = data; | |
119 | |
120 if (strlen(path) > FILENAME_MAX) { | |
121 g_warning("Ignoring path: name too long (%s)", path); | |
122 return TRUE; | |
123 } | |
124 | |
125 if (g_file_test(path, G_FILE_TEST_IS_REGULAR)) { | |
126 if (!strcasecmp(basename, context->to_match)) { | |
127 context->match = g_strdup(path); | |
128 context->found = TRUE; | |
129 return TRUE; | |
130 } | |
131 } | |
132 else if (g_file_test(path, G_FILE_TEST_IS_DIR)) { | |
133 dir_foreach(path, find_file_func, context, NULL); | |
134 if (context->found) | |
135 return TRUE; | |
136 } | |
137 | |
138 return FALSE; | |
139 } | |
140 | |
141 gchar * | |
142 find_file_recursively(const gchar * path, const gchar * filename) | |
143 { | |
144 FindFileContext context; | |
145 | |
146 context.to_match = filename; | |
147 context.match = NULL; | |
148 context.found = FALSE; | |
149 | |
150 dir_foreach(path, find_file_func, &context, NULL); | |
151 return context.match; | |
152 } | |
153 | |
154 | |
155 typedef enum { | |
156 ARCHIVE_UNKNOWN = 0, | |
157 ARCHIVE_DIR, | |
158 ARCHIVE_TAR, | |
159 ARCHIVE_TGZ, | |
160 ARCHIVE_ZIP, | |
161 ARCHIVE_TBZ2 | |
162 } ArchiveType; | |
163 | |
164 typedef gchar *(*ArchiveExtractFunc) (const gchar *, const gchar *); | |
165 | |
166 typedef struct { | |
167 ArchiveType type; | |
168 const gchar *ext; | |
169 } ArchiveExtensionType; | |
170 | |
171 static ArchiveExtensionType archive_extensions[] = { | |
172 {ARCHIVE_TAR, ".tar"}, | |
173 {ARCHIVE_ZIP, ".wsz"}, | |
174 {ARCHIVE_ZIP, ".zip"}, | |
175 {ARCHIVE_TGZ, ".tar.gz"}, | |
176 {ARCHIVE_TGZ, ".tgz"}, | |
177 {ARCHIVE_TBZ2, ".tar.bz2"}, | |
178 {ARCHIVE_TBZ2, ".bz2"}, | |
179 {ARCHIVE_UNKNOWN, NULL} | |
180 }; | |
181 | |
182 static gchar *archive_extract_tar(const gchar * archive, const gchar * dest); | |
183 static gchar *archive_extract_zip(const gchar * archive, const gchar * dest); | |
184 static gchar *archive_extract_tgz(const gchar * archive, const gchar * dest); | |
185 static gchar *archive_extract_tbz2(const gchar * archive, const gchar * dest); | |
186 | |
187 static ArchiveExtractFunc archive_extract_funcs[] = { | |
188 NULL, | |
189 NULL, | |
190 archive_extract_tar, | |
191 archive_extract_tgz, | |
192 archive_extract_zip, | |
193 archive_extract_tbz2 | |
194 }; | |
195 | |
196 | |
197 /* FIXME: these functions can be generalised into a function using a | |
198 * command lookup table */ | |
199 | |
200 static const gchar * | |
201 get_tar_command(void) | |
202 { | |
203 static const gchar *command = NULL; | |
204 | |
205 if (!command) { | |
206 if (!(command = getenv("TARCMD"))) | |
207 command = "tar"; | |
208 } | |
209 | |
210 return command; | |
211 } | |
212 | |
213 static const gchar * | |
214 get_unzip_command(void) | |
215 { | |
216 static const gchar *command = NULL; | |
217 | |
218 if (!command) { | |
219 if (!(command = getenv("UNZIPCMD"))) | |
220 command = "unzip"; | |
221 } | |
222 | |
223 return command; | |
224 } | |
225 | |
226 | |
227 static gchar * | |
228 archive_extract_tar(const gchar * archive, const gchar * dest) | |
229 { | |
230 return g_strdup_printf("%s >/dev/null xf \"%s\" -C %s", | |
231 get_tar_command(), archive, dest); | |
232 } | |
233 | |
234 static gchar * | |
235 archive_extract_zip(const gchar * archive, const gchar * dest) | |
236 { | |
237 return g_strdup_printf("%s >/dev/null -o -j \"%s\" -d %s", | |
238 get_unzip_command(), archive, dest); | |
239 } | |
240 | |
241 static gchar * | |
242 archive_extract_tgz(const gchar * archive, const gchar * dest) | |
243 { | |
244 return g_strdup_printf("%s >/dev/null xzf \"%s\" -C %s", | |
245 get_tar_command(), archive, dest); | |
246 } | |
247 | |
248 static gchar * | |
249 archive_extract_tbz2(const gchar * archive, const gchar * dest) | |
250 { | |
251 return g_strdup_printf("bzip2 -dc \"%s\" | %s >/dev/null xf - -C %s", | |
252 archive, get_tar_command(), dest); | |
253 } | |
254 | |
255 | |
256 ArchiveType | |
257 archive_get_type(const gchar * filename) | |
258 { | |
259 gint i = 0; | |
260 | |
261 if (g_file_test(filename, G_FILE_TEST_IS_DIR)) | |
262 return ARCHIVE_DIR; | |
263 | |
264 while (archive_extensions[i].ext) { | |
265 if (g_str_has_suffix(filename, archive_extensions[i].ext)) { | |
266 return archive_extensions[i].type; | |
267 } | |
268 i++; | |
269 } | |
270 | |
271 return ARCHIVE_UNKNOWN; | |
272 } | |
273 | |
274 gboolean | |
275 file_is_archive(const gchar * filename) | |
276 { | |
277 return (archive_get_type(filename) > ARCHIVE_DIR); | |
278 } | |
279 | |
280 gchar * | |
281 archive_basename(const gchar * str) | |
282 { | |
283 gint i = 0; | |
284 | |
285 while (archive_extensions[i].ext) { | |
286 if (str_has_suffix_nocase(str, archive_extensions[i].ext)) { | |
287 const gchar *end = g_strrstr(str, archive_extensions[i].ext); | |
288 if (end) { | |
289 return g_strndup(str, end - str); | |
290 } | |
291 break; | |
292 } | |
293 i++; | |
294 } | |
295 | |
296 return NULL; | |
297 } | |
298 | |
299 /* | |
300 decompress_archive | |
301 | |
302 Decompresses the archive "filename" to a temporary directory, | |
303 returns the path to the temp dir, or NULL if failed, | |
304 watch out tho, doesn't actually check if the system command succeeds :-| | |
305 */ | |
306 | |
307 gchar * | |
308 archive_decompress(const gchar * filename) | |
309 { | |
310 gchar *tmpdir, *cmd, *escaped_filename; | |
311 ArchiveType type; | |
312 | |
313 if ((type = archive_get_type(filename)) <= ARCHIVE_DIR) | |
314 return NULL; | |
315 | |
316 tmpdir = g_build_filename(g_get_tmp_dir(), "bmp.XXXXXXXX", NULL); | |
317 if (!mkdtemp(tmpdir)) { | |
318 g_free(tmpdir); | |
319 g_message("Unable to load skin: Failed to create temporary " | |
320 "directory: %s", g_strerror(errno)); | |
321 return NULL; | |
322 } | |
323 | |
324 escaped_filename = escape_shell_chars(filename); | |
325 cmd = archive_extract_funcs[type] (escaped_filename, tmpdir); | |
326 g_free(escaped_filename); | |
327 | |
328 if (!cmd) { | |
329 g_message("extraction function is NULL!"); | |
330 g_free(tmpdir); | |
331 return NULL; | |
332 } | |
333 | |
334 system(cmd); | |
335 g_free(cmd); | |
336 | |
337 return tmpdir; | |
338 } | |
339 | |
340 | |
341 #ifdef HAVE_FTS_H | |
342 | |
343 void | |
344 del_directory(const gchar * dirname) | |
345 { | |
346 gchar *const argv[2] = { (gchar *) dirname, NULL }; | |
347 FTS *fts; | |
348 FTSENT *p; | |
349 | |
350 fts = fts_open(argv, FTS_PHYSICAL, (gint(*)())NULL); | |
351 while ((p = fts_read(fts))) { | |
352 switch (p->fts_info) { | |
353 case FTS_D: | |
354 break; | |
355 case FTS_DNR: | |
356 case FTS_ERR: | |
357 break; | |
358 case FTS_DP: | |
359 rmdir(p->fts_accpath); | |
360 break; | |
361 default: | |
362 unlink(p->fts_accpath); | |
363 break; | |
364 } | |
365 } | |
366 fts_close(fts); | |
367 } | |
368 | |
369 #else /* !HAVE_FTS */ | |
370 | |
371 gboolean | |
372 del_directory_func(const gchar * path, const gchar * basename, | |
373 gpointer params) | |
374 { | |
375 if (!strcmp(basename, ".") || !strcmp(path, "..")) | |
376 return FALSE; | |
377 | |
378 if (g_file_test(path, G_FILE_TEST_IS_DIR)) { | |
379 dir_foreach(path, del_directory_func, NULL, NULL); | |
380 rmdir(path); | |
381 return FALSE; | |
382 } | |
383 | |
384 unlink(path); | |
385 | |
386 return FALSE; | |
387 } | |
388 | |
389 void | |
390 del_directory(const gchar * path) | |
391 { | |
392 dir_foreach(path, del_directory_func, NULL, NULL); | |
393 rmdir(path); | |
394 } | |
395 | |
396 #endif /* ifdef HAVE_FTS */ | |
397 | |
398 gchar * | |
399 read_ini_string(const gchar * filename, const gchar * section, | |
400 const gchar * key) | |
401 { | |
1166
36e8431d8e19
[svn] - optimise for multiple lookups of a single INI file, by caching the contents of the INI file.
nenolod
parents:
1123
diff
changeset
|
402 static gchar *buffer = NULL; |
36e8431d8e19
[svn] - optimise for multiple lookups of a single INI file, by caching the contents of the INI file.
nenolod
parents:
1123
diff
changeset
|
403 static gchar *open_buffer = NULL; |
36e8431d8e19
[svn] - optimise for multiple lookups of a single INI file, by caching the contents of the INI file.
nenolod
parents:
1123
diff
changeset
|
404 gchar *ret_buffer = NULL; |
625
0a73d1faeb4e
[svn] GCC 4.1 warning fixes by Diego 'Flameeyes' Petteno from Gentoo.
chainsaw
parents:
538
diff
changeset
|
405 gint found_section = 0, len = 0; |
0a73d1faeb4e
[svn] GCC 4.1 warning fixes by Diego 'Flameeyes' Petteno from Gentoo.
chainsaw
parents:
538
diff
changeset
|
406 gsize filesize, off = 0; |
245
1d8012046612
[svn] Convert UTF-16 (windows-1252) into something useful. Original patch by incomp@#audacious,
nenolod
parents:
86
diff
changeset
|
407 gchar *outbuf; |
1d8012046612
[svn] Convert UTF-16 (windows-1252) into something useful. Original patch by incomp@#audacious,
nenolod
parents:
86
diff
changeset
|
408 unsigned char x[] = { 0xff, 0xfe, 0x00 }; |
625
0a73d1faeb4e
[svn] GCC 4.1 warning fixes by Diego 'Flameeyes' Petteno from Gentoo.
chainsaw
parents:
538
diff
changeset
|
409 guint counter; |
0 | 410 |
411 if (!filename) | |
412 return NULL; | |
413 | |
1166
36e8431d8e19
[svn] - optimise for multiple lookups of a single INI file, by caching the contents of the INI file.
nenolod
parents:
1123
diff
changeset
|
414 /* |
36e8431d8e19
[svn] - optimise for multiple lookups of a single INI file, by caching the contents of the INI file.
nenolod
parents:
1123
diff
changeset
|
415 * We optimise for the condition that we may be reading from the |
36e8431d8e19
[svn] - optimise for multiple lookups of a single INI file, by caching the contents of the INI file.
nenolod
parents:
1123
diff
changeset
|
416 * same ini-file multiple times. This is fairly common; it happens |
36e8431d8e19
[svn] - optimise for multiple lookups of a single INI file, by caching the contents of the INI file.
nenolod
parents:
1123
diff
changeset
|
417 * on .pls playlist loads. To do otherwise would take entirely too |
36e8431d8e19
[svn] - optimise for multiple lookups of a single INI file, by caching the contents of the INI file.
nenolod
parents:
1123
diff
changeset
|
418 * long, as fstat() can be very slow when done 21,000 times too many. |
36e8431d8e19
[svn] - optimise for multiple lookups of a single INI file, by caching the contents of the INI file.
nenolod
parents:
1123
diff
changeset
|
419 * |
36e8431d8e19
[svn] - optimise for multiple lookups of a single INI file, by caching the contents of the INI file.
nenolod
parents:
1123
diff
changeset
|
420 * Therefore, we optimise by keeping the last ini file in memory. |
36e8431d8e19
[svn] - optimise for multiple lookups of a single INI file, by caching the contents of the INI file.
nenolod
parents:
1123
diff
changeset
|
421 * Yes, this is a memory leak, but it is not too bad, hopefully. |
36e8431d8e19
[svn] - optimise for multiple lookups of a single INI file, by caching the contents of the INI file.
nenolod
parents:
1123
diff
changeset
|
422 * - nenolod |
36e8431d8e19
[svn] - optimise for multiple lookups of a single INI file, by caching the contents of the INI file.
nenolod
parents:
1123
diff
changeset
|
423 */ |
36e8431d8e19
[svn] - optimise for multiple lookups of a single INI file, by caching the contents of the INI file.
nenolod
parents:
1123
diff
changeset
|
424 if (open_buffer == NULL || strcasecmp(filename, open_buffer)) |
36e8431d8e19
[svn] - optimise for multiple lookups of a single INI file, by caching the contents of the INI file.
nenolod
parents:
1123
diff
changeset
|
425 { |
36e8431d8e19
[svn] - optimise for multiple lookups of a single INI file, by caching the contents of the INI file.
nenolod
parents:
1123
diff
changeset
|
426 if (buffer != NULL) |
36e8431d8e19
[svn] - optimise for multiple lookups of a single INI file, by caching the contents of the INI file.
nenolod
parents:
1123
diff
changeset
|
427 { |
36e8431d8e19
[svn] - optimise for multiple lookups of a single INI file, by caching the contents of the INI file.
nenolod
parents:
1123
diff
changeset
|
428 g_free(buffer); |
36e8431d8e19
[svn] - optimise for multiple lookups of a single INI file, by caching the contents of the INI file.
nenolod
parents:
1123
diff
changeset
|
429 buffer = NULL; |
36e8431d8e19
[svn] - optimise for multiple lookups of a single INI file, by caching the contents of the INI file.
nenolod
parents:
1123
diff
changeset
|
430 } |
36e8431d8e19
[svn] - optimise for multiple lookups of a single INI file, by caching the contents of the INI file.
nenolod
parents:
1123
diff
changeset
|
431 |
36e8431d8e19
[svn] - optimise for multiple lookups of a single INI file, by caching the contents of the INI file.
nenolod
parents:
1123
diff
changeset
|
432 if (open_buffer != NULL) |
36e8431d8e19
[svn] - optimise for multiple lookups of a single INI file, by caching the contents of the INI file.
nenolod
parents:
1123
diff
changeset
|
433 { |
36e8431d8e19
[svn] - optimise for multiple lookups of a single INI file, by caching the contents of the INI file.
nenolod
parents:
1123
diff
changeset
|
434 g_free(open_buffer); |
36e8431d8e19
[svn] - optimise for multiple lookups of a single INI file, by caching the contents of the INI file.
nenolod
parents:
1123
diff
changeset
|
435 open_buffer = NULL; |
36e8431d8e19
[svn] - optimise for multiple lookups of a single INI file, by caching the contents of the INI file.
nenolod
parents:
1123
diff
changeset
|
436 } |
36e8431d8e19
[svn] - optimise for multiple lookups of a single INI file, by caching the contents of the INI file.
nenolod
parents:
1123
diff
changeset
|
437 |
36e8431d8e19
[svn] - optimise for multiple lookups of a single INI file, by caching the contents of the INI file.
nenolod
parents:
1123
diff
changeset
|
438 if (!g_file_get_contents(filename, &buffer, &filesize, NULL)) |
36e8431d8e19
[svn] - optimise for multiple lookups of a single INI file, by caching the contents of the INI file.
nenolod
parents:
1123
diff
changeset
|
439 return NULL; |
36e8431d8e19
[svn] - optimise for multiple lookups of a single INI file, by caching the contents of the INI file.
nenolod
parents:
1123
diff
changeset
|
440 |
36e8431d8e19
[svn] - optimise for multiple lookups of a single INI file, by caching the contents of the INI file.
nenolod
parents:
1123
diff
changeset
|
441 open_buffer = g_strdup(filename); |
36e8431d8e19
[svn] - optimise for multiple lookups of a single INI file, by caching the contents of the INI file.
nenolod
parents:
1123
diff
changeset
|
442 } |
0 | 443 |
245
1d8012046612
[svn] Convert UTF-16 (windows-1252) into something useful. Original patch by incomp@#audacious,
nenolod
parents:
86
diff
changeset
|
444 /* |
1d8012046612
[svn] Convert UTF-16 (windows-1252) into something useful. Original patch by incomp@#audacious,
nenolod
parents:
86
diff
changeset
|
445 * Convert UTF-16 into something useful. Original implementation |
1d8012046612
[svn] Convert UTF-16 (windows-1252) into something useful. Original patch by incomp@#audacious,
nenolod
parents:
86
diff
changeset
|
446 * by incomp@#audacious. Cleanups \nenolod |
1d8012046612
[svn] Convert UTF-16 (windows-1252) into something useful. Original patch by incomp@#audacious,
nenolod
parents:
86
diff
changeset
|
447 */ |
1d8012046612
[svn] Convert UTF-16 (windows-1252) into something useful. Original patch by incomp@#audacious,
nenolod
parents:
86
diff
changeset
|
448 if (!memcmp(&buffer[0],&x,2)) { |
1d8012046612
[svn] Convert UTF-16 (windows-1252) into something useful. Original patch by incomp@#audacious,
nenolod
parents:
86
diff
changeset
|
449 outbuf = g_malloc (filesize); /* it's safe to waste memory. */ |
1d8012046612
[svn] Convert UTF-16 (windows-1252) into something useful. Original patch by incomp@#audacious,
nenolod
parents:
86
diff
changeset
|
450 |
1d8012046612
[svn] Convert UTF-16 (windows-1252) into something useful. Original patch by incomp@#audacious,
nenolod
parents:
86
diff
changeset
|
451 for (counter = 2; counter < filesize; counter += 2) |
1d8012046612
[svn] Convert UTF-16 (windows-1252) into something useful. Original patch by incomp@#audacious,
nenolod
parents:
86
diff
changeset
|
452 if (!memcmp(&buffer[counter+1], &x[2], 1)) |
1d8012046612
[svn] Convert UTF-16 (windows-1252) into something useful. Original patch by incomp@#audacious,
nenolod
parents:
86
diff
changeset
|
453 outbuf[(counter-2)/2] = buffer[counter]; |
1d8012046612
[svn] Convert UTF-16 (windows-1252) into something useful. Original patch by incomp@#audacious,
nenolod
parents:
86
diff
changeset
|
454 else |
1d8012046612
[svn] Convert UTF-16 (windows-1252) into something useful. Original patch by incomp@#audacious,
nenolod
parents:
86
diff
changeset
|
455 return NULL; |
1d8012046612
[svn] Convert UTF-16 (windows-1252) into something useful. Original patch by incomp@#audacious,
nenolod
parents:
86
diff
changeset
|
456 |
1d8012046612
[svn] Convert UTF-16 (windows-1252) into something useful. Original patch by incomp@#audacious,
nenolod
parents:
86
diff
changeset
|
457 outbuf[(counter-2)/2] = '\0'; |
1d8012046612
[svn] Convert UTF-16 (windows-1252) into something useful. Original patch by incomp@#audacious,
nenolod
parents:
86
diff
changeset
|
458 |
1d8012046612
[svn] Convert UTF-16 (windows-1252) into something useful. Original patch by incomp@#audacious,
nenolod
parents:
86
diff
changeset
|
459 if ((filesize - 2) / 2 == (counter - 2) / 2) { |
1d8012046612
[svn] Convert UTF-16 (windows-1252) into something useful. Original patch by incomp@#audacious,
nenolod
parents:
86
diff
changeset
|
460 g_free(buffer); |
1d8012046612
[svn] Convert UTF-16 (windows-1252) into something useful. Original patch by incomp@#audacious,
nenolod
parents:
86
diff
changeset
|
461 buffer = outbuf; |
1d8012046612
[svn] Convert UTF-16 (windows-1252) into something useful. Original patch by incomp@#audacious,
nenolod
parents:
86
diff
changeset
|
462 } else { |
1d8012046612
[svn] Convert UTF-16 (windows-1252) into something useful. Original patch by incomp@#audacious,
nenolod
parents:
86
diff
changeset
|
463 g_free(outbuf); |
1d8012046612
[svn] Convert UTF-16 (windows-1252) into something useful. Original patch by incomp@#audacious,
nenolod
parents:
86
diff
changeset
|
464 return NULL; /* XXX wrong encoding */ |
1d8012046612
[svn] Convert UTF-16 (windows-1252) into something useful. Original patch by incomp@#audacious,
nenolod
parents:
86
diff
changeset
|
465 } |
1d8012046612
[svn] Convert UTF-16 (windows-1252) into something useful. Original patch by incomp@#audacious,
nenolod
parents:
86
diff
changeset
|
466 } |
1d8012046612
[svn] Convert UTF-16 (windows-1252) into something useful. Original patch by incomp@#audacious,
nenolod
parents:
86
diff
changeset
|
467 |
0 | 468 while (!ret_buffer && off < filesize) { |
469 while (off < filesize && | |
470 (buffer[off] == '\r' || buffer[off] == '\n' || | |
471 buffer[off] == ' ' || buffer[off] == '\t')) | |
472 off++; | |
473 if (off >= filesize) | |
474 break; | |
475 if (buffer[off] == '[') { | |
476 gint slen = strlen(section); | |
477 off++; | |
478 found_section = 0; | |
479 if (off + slen + 1 < filesize && | |
480 !strncasecmp(section, &buffer[off], slen)) { | |
481 off += slen; | |
482 if (buffer[off] == ']') { | |
483 off++; | |
484 found_section = 1; | |
485 } | |
486 } | |
487 } | |
488 else if (found_section && off + strlen(key) < filesize && | |
489 !strncasecmp(key, &buffer[off], strlen(key))) { | |
490 off += strlen(key); | |
491 while (off < filesize && | |
492 (buffer[off] == ' ' || buffer[off] == '\t')) | |
493 off++; | |
494 if (off >= filesize) | |
495 break; | |
496 if (buffer[off] == '=') { | |
497 off++; | |
498 while (off < filesize && | |
499 (buffer[off] == ' ' || buffer[off] == '\t')) | |
500 off++; | |
501 if (off >= filesize) | |
502 break; | |
503 len = 0; | |
504 while (off + len < filesize && | |
505 buffer[off + len] != '\r' && | |
506 buffer[off + len] != '\n' && buffer[off + len] != ';') | |
507 len++; | |
508 ret_buffer = g_strndup(&buffer[off], len); | |
509 off += len; | |
510 } | |
511 } | |
512 while (off < filesize && buffer[off] != '\r' && buffer[off] != '\n') | |
513 off++; | |
514 } | |
515 | |
516 return ret_buffer; | |
517 } | |
518 | |
519 GArray * | |
520 string_to_garray(const gchar * str) | |
521 { | |
522 GArray *array; | |
523 gint temp; | |
524 const gchar *ptr = str; | |
525 gchar *endptr; | |
526 | |
527 array = g_array_new(FALSE, TRUE, sizeof(gint)); | |
528 for (;;) { | |
529 temp = strtol(ptr, &endptr, 10); | |
530 if (ptr == endptr) | |
531 break; | |
532 g_array_append_val(array, temp); | |
533 ptr = endptr; | |
534 while (!isdigit(*ptr) && (*ptr) != '\0') | |
535 ptr++; | |
536 if (*ptr == '\0') | |
537 break; | |
538 } | |
539 return (array); | |
540 } | |
541 | |
542 GArray * | |
543 read_ini_array(const gchar * filename, const gchar * section, | |
544 const gchar * key) | |
545 { | |
546 gchar *temp; | |
547 GArray *a; | |
548 | |
549 if ((temp = read_ini_string(filename, section, key)) == NULL) { | |
550 g_free(temp); | |
551 return NULL; | |
552 } | |
553 a = string_to_garray(temp); | |
554 g_free(temp); | |
555 return a; | |
556 } | |
557 | |
558 void | |
559 glist_movedown(GList * list) | |
560 { | |
561 gpointer temp; | |
562 | |
563 if (g_list_next(list)) { | |
564 temp = list->data; | |
565 list->data = list->next->data; | |
566 list->next->data = temp; | |
567 } | |
568 } | |
569 | |
570 void | |
571 glist_moveup(GList * list) | |
572 { | |
573 gpointer temp; | |
574 | |
575 if (g_list_previous(list)) { | |
576 temp = list->data; | |
577 list->data = list->prev->data; | |
578 list->prev->data = temp; | |
579 } | |
580 } | |
581 | |
582 | |
583 void | |
584 util_menu_position(GtkMenu * menu, gint * x, gint * y, | |
585 gboolean * push_in, gpointer data) | |
586 { | |
587 GtkRequisition requisition; | |
588 gint screen_width; | |
589 gint screen_height; | |
590 MenuPos *pos = data; | |
591 | |
592 gtk_widget_size_request(GTK_WIDGET(menu), &requisition); | |
593 | |
594 screen_width = gdk_screen_width(); | |
595 screen_height = gdk_screen_height(); | |
596 | |
597 *x = CLAMP(pos->x - 2, 0, MAX(0, screen_width - requisition.width)); | |
598 *y = CLAMP(pos->y - 2, 0, MAX(0, screen_height - requisition.height)); | |
599 } | |
600 | |
601 static void | |
602 util_menu_delete_popup_data(GtkObject * object, GtkItemFactory * ifactory) | |
603 { | |
604 gtk_signal_disconnect_by_func(object, | |
605 GTK_SIGNAL_FUNC | |
606 (util_menu_delete_popup_data), ifactory); | |
607 gtk_object_remove_data_by_id(GTK_OBJECT(ifactory), quark_popup_data); | |
608 } | |
609 | |
610 | |
611 /* | |
612 * util_item_factory_popup[_with_data]() is a replacement for | |
613 * gtk_item_factory_popup[_with_data](). | |
614 * | |
615 * The difference is that the menu is always popped up whithin the | |
616 * screen. This means it does not neccesarily pop up at (x,y). | |
617 */ | |
618 | |
619 void | |
620 util_item_factory_popup_with_data(GtkItemFactory * ifactory, | |
621 gpointer data, | |
622 GtkDestroyNotify destroy, guint x, | |
623 guint y, guint mouse_button, guint32 time) | |
624 { | |
625 static GQuark quark_user_menu_pos = 0; | |
626 MenuPos *pos; | |
627 | |
628 if (!quark_user_menu_pos) | |
629 quark_user_menu_pos = g_quark_from_static_string("user_menu_pos"); | |
630 | |
631 if (!quark_popup_data) | |
632 quark_popup_data = | |
633 g_quark_from_static_string("GtkItemFactory-popup-data"); | |
634 | |
635 pos = g_object_get_qdata(G_OBJECT(ifactory), quark_user_menu_pos); | |
636 if (!pos) { | |
637 pos = g_new0(MenuPos, 1); | |
638 | |
639 g_object_set_qdata_full(G_OBJECT(ifactory->widget), | |
640 quark_user_menu_pos, pos, g_free); | |
641 } | |
642 pos->x = x; | |
643 pos->y = y; | |
644 | |
645 | |
646 if (data != NULL) { | |
647 g_object_set_qdata_full(G_OBJECT(ifactory), | |
648 quark_popup_data, data, destroy); | |
649 g_signal_connect(G_OBJECT(ifactory->widget), | |
650 "selection-done", | |
651 G_CALLBACK(util_menu_delete_popup_data), ifactory); | |
652 } | |
653 | |
654 gtk_menu_popup(GTK_MENU(ifactory->widget), NULL, NULL, | |
655 (GtkMenuPositionFunc) util_menu_position, | |
656 pos, mouse_button, time); | |
657 } | |
658 | |
659 void | |
660 util_item_factory_popup(GtkItemFactory * ifactory, guint x, guint y, | |
661 guint mouse_button, guint32 time) | |
662 { | |
663 util_item_factory_popup_with_data(ifactory, NULL, NULL, x, y, | |
664 mouse_button, time); | |
665 } | |
666 | |
667 | |
668 #define URL_HISTORY_MAX_SIZE 30 | |
669 | |
670 static void | |
671 util_add_url_callback(GtkWidget * widget, | |
672 GtkEntry * entry) | |
673 { | |
674 const gchar *text; | |
675 | |
676 text = gtk_entry_get_text(entry); | |
677 if (g_list_find_custom(cfg.url_history, text, (GCompareFunc) strcasecmp)) | |
678 return; | |
679 | |
680 cfg.url_history = g_list_prepend(cfg.url_history, g_strdup(text)); | |
681 | |
682 while (g_list_length(cfg.url_history) > URL_HISTORY_MAX_SIZE) { | |
683 GList *node = g_list_last(cfg.url_history); | |
684 g_free(node->data); | |
685 cfg.url_history = g_list_delete_link(cfg.url_history, node); | |
686 } | |
687 } | |
688 | |
689 GtkWidget * | |
86 | 690 util_add_url_dialog_new(const gchar * caption, GCallback ok_func, |
691 GCallback enqueue_func) | |
0 | 692 { |
86 | 693 GtkWidget *win, *vbox, *bbox, *enqueue, *ok, *cancel, *combo, *entry; |
0 | 694 GList *url; |
695 | |
696 win = gtk_window_new(GTK_WINDOW_TOPLEVEL); | |
697 gtk_window_set_title(GTK_WINDOW(win), caption); | |
698 gtk_window_set_type_hint(GTK_WINDOW(win), GDK_WINDOW_TYPE_HINT_DIALOG); | |
699 gtk_window_set_position(GTK_WINDOW(win), GTK_WIN_POS_CENTER); | |
700 gtk_window_set_default_size(GTK_WINDOW(win), 400, -1); | |
701 gtk_container_set_border_width(GTK_CONTAINER(win), 12); | |
702 | |
703 vbox = gtk_vbox_new(FALSE, 10); | |
704 gtk_container_add(GTK_CONTAINER(win), vbox); | |
705 | |
706 combo = gtk_combo_box_entry_new_text(); | |
707 gtk_box_pack_start(GTK_BOX(vbox), combo, FALSE, FALSE, 0); | |
708 | |
709 entry = gtk_bin_get_child(GTK_BIN(combo)); | |
710 gtk_window_set_focus(GTK_WINDOW(win), entry); | |
711 gtk_entry_set_text(GTK_ENTRY(entry), ""); | |
712 | |
713 for (url = cfg.url_history; url; url = g_list_next(url)) | |
714 gtk_combo_box_append_text(GTK_COMBO_BOX(combo), (const gchar *) url->data); | |
715 | |
716 g_signal_connect(entry, "activate", | |
717 G_CALLBACK(util_add_url_callback), | |
718 entry); | |
719 g_signal_connect(entry, "activate", | |
86 | 720 G_CALLBACK(ok_func), |
0 | 721 entry); |
722 g_signal_connect_swapped(entry, "activate", | |
723 G_CALLBACK(gtk_widget_destroy), | |
724 win); | |
725 | |
726 bbox = gtk_hbutton_box_new(); | |
727 gtk_button_box_set_layout(GTK_BUTTON_BOX(bbox), GTK_BUTTONBOX_END); | |
728 gtk_button_box_set_spacing(GTK_BUTTON_BOX(bbox), 5); | |
729 gtk_box_pack_start(GTK_BOX(vbox), bbox, FALSE, FALSE, 0); | |
730 | |
86 | 731 ok = gtk_button_new_from_stock(GTK_STOCK_OPEN); |
732 g_signal_connect(ok, "clicked", | |
733 G_CALLBACK(util_add_url_callback), entry); | |
734 g_signal_connect(ok, "clicked", | |
735 G_CALLBACK(ok_func), entry); | |
736 g_signal_connect_swapped(ok, "clicked", | |
737 G_CALLBACK(gtk_widget_destroy), | |
738 win); | |
739 gtk_box_pack_start(GTK_BOX(bbox), ok, FALSE, FALSE, 0); | |
740 | |
0 | 741 enqueue = gtk_button_new_from_stock(GTK_STOCK_ADD); |
742 gtk_box_pack_start(GTK_BOX(bbox), enqueue, FALSE, FALSE, 0); | |
743 | |
744 g_signal_connect(enqueue, "clicked", | |
745 G_CALLBACK(util_add_url_callback), | |
746 entry); | |
747 g_signal_connect(enqueue, "clicked", | |
748 G_CALLBACK(enqueue_func), | |
749 entry); | |
750 g_signal_connect_swapped(enqueue, "clicked", | |
751 G_CALLBACK(gtk_widget_destroy), | |
752 win); | |
753 | |
754 cancel = gtk_button_new_from_stock(GTK_STOCK_CLOSE); | |
755 gtk_box_pack_start(GTK_BOX(bbox), cancel, FALSE, FALSE, 0); | |
756 | |
757 g_signal_connect_swapped(cancel, "clicked", | |
758 G_CALLBACK(gtk_widget_destroy), | |
759 win); | |
760 | |
761 gtk_widget_show_all(vbox); | |
762 | |
763 return win; | |
764 } | |
765 | |
766 static void | |
767 filebrowser_add_files(GtkFileChooser * browser, | |
768 GSList * files) | |
769 { | |
770 GSList *cur; | |
771 gchar *ptr; | |
772 guint ctr = 0; | |
773 | |
774 if (GTK_IS_WIDGET(mainwin_jtf)) | |
775 gtk_widget_set_sensitive(mainwin_jtf, FALSE); | |
776 | |
777 for (cur = files; cur; cur = g_slist_next(cur)) { | |
778 | |
779 if (g_file_test(cur->data,G_FILE_TEST_IS_DIR)) { | |
780 playlist_add_dir((const gchar *) cur->data); | |
781 } else { | |
782 playlist_add((const gchar *) cur->data); | |
783 } | |
784 | |
785 if (++ctr == 20) { | |
786 playlistwin_update_list(); | |
787 ctr = 0; | |
788 while (gtk_events_pending() ) gtk_main_iteration(); | |
789 } | |
790 } | |
791 | |
792 playlistwin_update_list(); | |
793 | |
794 if (GTK_IS_WIDGET(mainwin_jtf)) | |
795 gtk_widget_set_sensitive(mainwin_jtf, TRUE); | |
796 | |
797 #ifdef HAVE_GNOME_VFS | |
798 ptr = gtk_file_chooser_get_current_folder_uri(GTK_FILE_CHOOSER(browser)); | |
799 #else | |
800 ptr = gtk_file_chooser_get_current_folder(GTK_FILE_CHOOSER(browser)); | |
801 #endif | |
802 | |
803 g_free(cfg.filesel_path); | |
804 cfg.filesel_path = ptr; | |
805 } | |
806 | |
807 static void | |
808 filebrowser_add(GtkFileChooser * browser) | |
809 { | |
810 GSList *files; | |
811 | |
812 #ifdef HAVE_GNOME_VFS | |
813 files = gtk_file_chooser_get_uris(GTK_FILE_CHOOSER(browser)); | |
814 #else | |
815 files = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(browser)); | |
816 #endif | |
817 | |
818 if (!files) { | |
819 return; | |
820 } | |
821 | |
822 filebrowser_add_files(browser, files); | |
823 g_slist_foreach(files, (GFunc) g_free, NULL); | |
824 g_slist_free(files); | |
825 } | |
826 | |
827 static void | |
828 filebrowser_play(GtkFileChooser * browser) | |
829 { | |
830 GSList *files; | |
831 | |
832 #ifdef HAVE_GNOME_VFS | |
833 files = gtk_file_chooser_get_uris(GTK_FILE_CHOOSER(browser)); | |
834 #else | |
835 files = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(browser)); | |
836 #endif | |
837 | |
838 if (!files) return; | |
839 | |
840 playlist_clear(); | |
841 | |
842 filebrowser_add_files(browser, files); | |
843 g_slist_foreach(files, (GFunc) g_free, NULL); | |
844 g_slist_free(files); | |
845 | |
846 bmp_playback_initiate(); | |
847 } | |
848 | |
849 | |
850 static void | |
851 _filebrowser_add(GtkWidget *widget, | |
852 gpointer data) | |
853 { | |
854 filebrowser_add(data); | |
855 gtk_file_chooser_unselect_all(GTK_FILE_CHOOSER(data)); | |
856 } | |
857 | |
858 static void | |
859 _filebrowser_play(GtkWidget *widget, gpointer data) | |
860 { | |
861 filebrowser_play(data); | |
862 gtk_file_chooser_unselect_all(data); | |
863 } | |
864 | |
865 #if 0 | |
866 static void | |
867 filebrowser_on_response(GtkFileChooser * browser, | |
868 gint response, | |
869 gpointer data) | |
870 { | |
871 gtk_widget_hide(GTK_WIDGET(browser)); | |
872 switch (response) { | |
873 case GTK_RESPONSE_OK: | |
874 break; | |
875 case GTK_RESPONSE_ACCEPT: | |
876 break; | |
877 case GTK_RESPONSE_CLOSE: | |
878 break; | |
879 } | |
880 gtk_widget_destroy(GTK_WIDGET(browser)); | |
881 | |
882 } | |
883 | |
884 #endif | |
885 | |
886 static void | |
887 _filebrowser_check_hide_add(GtkWidget * widget, | |
888 gpointer data) | |
889 { | |
890 cfg.close_dialog_add = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)); | |
891 } | |
892 | |
893 static void | |
894 _filebrowser_check_hide_open(GtkWidget * widget, | |
895 gpointer data) | |
896 { | |
897 cfg.close_dialog_open = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)); | |
898 } | |
899 | |
900 | |
901 | |
902 static gboolean | |
903 filebrowser_on_keypress(GtkWidget * browser, | |
904 GdkEventKey * event, | |
905 gpointer data) | |
906 { | |
907 if (event->keyval == GDK_Escape) { | |
908 /* FIXME: this crashes BMP for some reason */ | |
909 /* g_signal_emit_by_name(browser, "delete-event"); */ | |
910 gtk_widget_hide(browser); | |
911 return TRUE; | |
912 } | |
913 | |
914 return FALSE; | |
915 } | |
916 | |
917 static void | |
918 _filebrowser_do_hide_add(GtkWidget *widget, | |
919 gpointer data) | |
920 { | |
921 if (cfg.close_dialog_add) | |
922 gtk_widget_hide(data); | |
923 } | |
924 | |
925 static void | |
926 _filebrowser_do_hide_open(GtkWidget *widget, | |
927 gpointer data) | |
928 { | |
929 if (cfg.close_dialog_open) | |
930 gtk_widget_hide(data); | |
931 } | |
932 | |
933 void | |
934 util_run_filebrowser(gboolean play_button) | |
935 { | |
936 static GladeXML *xml = NULL; | |
937 static GtkWidget *dialog = NULL; | |
938 static GtkWidget *chooser = NULL; | |
939 | |
940 static GtkWidget *button_add; | |
941 static GtkWidget *button_select_all, *button_deselect_all; | |
942 static GtkWidget *toggle; | |
943 | |
944 static gulong handlerid, handlerid_check, handlerid_do; | |
945 static gulong handlerid_activate, handlerid_do_activate; | |
946 | |
947 if (!xml) { | |
948 /* FIXME: Creating a file chooser dialog manually using | |
949 libglade because there's no way to stop | |
950 GtkFileChooserDialog from resizing the buttons to the same | |
951 size. The long toggle button title causes the buttons to | |
952 turn unnecessarily elongated and fugly. */ | |
953 | |
954 GtkWidget *alignment; | |
955 | |
956 xml = glade_xml_new_or_die(_("Add/Open Files dialog"), | |
957 DATA_DIR "/glade/addfiles.glade", | |
958 NULL, NULL); | |
959 glade_xml_signal_autoconnect(xml); | |
960 | |
961 dialog = glade_xml_get_widget(xml, "add_files_dialog"); | |
962 | |
963 /* FIXME: Creating file chooser widget here because libglade <= 2.4.0 does | |
964 not support GtkFileChooserWidget */ | |
965 | |
966 chooser = gtk_file_chooser_widget_new(GTK_FILE_CHOOSER_ACTION_OPEN); | |
967 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(chooser), TRUE); | |
968 | |
969 #ifdef HAVE_GNOME_VFS | |
970 gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(chooser), FALSE); | |
971 #endif | |
972 | |
973 if (cfg.filesel_path) | |
974 #ifdef HAVE_GNOME_VFS | |
975 gtk_file_chooser_set_current_folder_uri(GTK_FILE_CHOOSER(chooser), | |
976 cfg.filesel_path); | |
977 #else | |
978 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(chooser), | |
979 cfg.filesel_path); | |
980 #endif | |
981 | |
982 alignment = glade_xml_get_widget(xml, "alignment2"); | |
983 gtk_container_add(GTK_CONTAINER(alignment), chooser); | |
984 | |
985 toggle = glade_xml_get_widget(xml, "close_on_action"); | |
986 button_select_all = glade_xml_get_widget(xml, "select_all"); | |
987 button_deselect_all = glade_xml_get_widget(xml, "deselect_all"); | |
988 button_add = glade_xml_get_widget(xml, "action"); | |
989 | |
990 g_signal_connect_swapped(button_select_all, "clicked", | |
991 G_CALLBACK(gtk_file_chooser_select_all), | |
992 chooser); | |
993 g_signal_connect_swapped(button_deselect_all, "clicked", | |
994 G_CALLBACK(gtk_file_chooser_unselect_all), | |
995 chooser); | |
996 | |
997 g_signal_connect(dialog, "key_press_event", | |
998 G_CALLBACK(filebrowser_on_keypress), | |
999 NULL); | |
1000 | |
1001 gtk_widget_show_all(dialog); | |
1002 } /* !xml */ | |
1003 else { | |
1004 g_signal_handler_disconnect(button_add, handlerid); | |
1005 g_signal_handler_disconnect(toggle, handlerid_check); | |
1006 g_signal_handler_disconnect(chooser, handlerid_activate); | |
1007 g_signal_handler_disconnect(button_add, handlerid_do); | |
1008 g_signal_handler_disconnect(chooser, handlerid_do_activate); | |
893
6afdd0d7e1e1
[svn] Make refreshing optional, default to disabled if Gnome VFS available.
nemo
parents:
879
diff
changeset
|
1009 if (cfg.refresh_file_list) { |
6afdd0d7e1e1
[svn] Make refreshing optional, default to disabled if Gnome VFS available.
nemo
parents:
879
diff
changeset
|
1010 // *sigh* force a refresh |
6afdd0d7e1e1
[svn] Make refreshing optional, default to disabled if Gnome VFS available.
nemo
parents:
879
diff
changeset
|
1011 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(chooser),gtk_file_chooser_get_current_folder(GTK_FILE_CHOOSER(chooser))); |
6afdd0d7e1e1
[svn] Make refreshing optional, default to disabled if Gnome VFS available.
nemo
parents:
879
diff
changeset
|
1012 } |
0 | 1013 } |
1014 | |
1015 if (play_button) { | |
1016 gtk_window_set_title(GTK_WINDOW(dialog), _("Open Files")); | |
1017 | |
1018 gtk_button_set_label(GTK_BUTTON(button_add), GTK_STOCK_OPEN); | |
1019 | |
1020 gtk_button_set_label(GTK_BUTTON(toggle), _("Close dialog on Open")); | |
1021 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(toggle), cfg.close_dialog_open); | |
1022 | |
1023 handlerid = g_signal_connect(button_add, "clicked", G_CALLBACK(_filebrowser_play), chooser); | |
1024 handlerid_check = g_signal_connect(toggle, "toggled", G_CALLBACK(_filebrowser_check_hide_open), NULL); | |
1025 handlerid_do = g_signal_connect_after(button_add, "clicked", G_CALLBACK(_filebrowser_do_hide_open), dialog); | |
1026 handlerid_activate = g_signal_connect(chooser, "file-activated", G_CALLBACK(_filebrowser_play), chooser); | |
1027 handlerid_do_activate = g_signal_connect_after(chooser,"file_activated", G_CALLBACK(_filebrowser_do_hide_open), dialog); | |
1028 } | |
1029 else { | |
1030 gtk_window_set_title(GTK_WINDOW(dialog), _("Add Files")); | |
1031 | |
1032 gtk_button_set_label(GTK_BUTTON(button_add), GTK_STOCK_ADD); | |
1033 | |
1034 gtk_button_set_label(GTK_BUTTON(toggle), _("Close dialog on Add")); | |
1035 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(toggle), cfg.close_dialog_add); | |
1036 | |
1037 handlerid = g_signal_connect(button_add, "clicked", G_CALLBACK(_filebrowser_add), chooser); | |
1038 handlerid_check = g_signal_connect(toggle, "toggled", G_CALLBACK(_filebrowser_check_hide_add), NULL); | |
1039 handlerid_do = g_signal_connect_after(button_add, "clicked", G_CALLBACK(_filebrowser_do_hide_add), dialog); | |
1040 handlerid_activate = g_signal_connect(chooser, "file-activated", G_CALLBACK(_filebrowser_add), chooser); | |
1041 handlerid_do_activate = g_signal_connect_after(chooser,"file_activated", G_CALLBACK(_filebrowser_do_hide_add), dialog); | |
1042 } | |
1043 | |
1044 gtk_window_present(GTK_WINDOW(dialog)); | |
1045 } | |
1046 | |
1047 GdkFont * | |
1048 util_font_load(const gchar * name) | |
1049 { | |
1050 GdkFont *font; | |
1051 PangoFontDescription *desc; | |
1052 | |
1053 desc = pango_font_description_from_string(name); | |
1054 font = gdk_font_from_description(desc); | |
1055 | |
1056 return font; | |
1057 } | |
1058 | |
1059 #ifdef ENABLE_NLS | |
1060 gchar * | |
1061 bmp_menu_translate(const gchar * path, gpointer func_data) | |
1062 { | |
1063 gchar *translation = gettext(path); | |
1064 | |
1065 if (!translation || *translation != '/') { | |
1066 g_warning("Bad translation for menupath: %s", path); | |
1067 translation = (gchar *) path; | |
1068 } | |
1069 | |
1070 return translation; | |
1071 } | |
1072 #endif | |
1073 | |
1074 void | |
1075 util_set_cursor(GtkWidget * window) | |
1076 { | |
1077 static GdkCursor *cursor = NULL; | |
1078 | |
1079 if (!window) { | |
1080 if (cursor) { | |
813
c8cf439179b8
[svn] - Fix a ton and a half of memory leaks, via the wonderful Leonardo Boshell <leonardop -at- gentoo.org>.
nenolod
parents:
625
diff
changeset
|
1081 gdk_cursor_unref(cursor); |
0 | 1082 cursor = NULL; |
1083 } | |
813
c8cf439179b8
[svn] - Fix a ton and a half of memory leaks, via the wonderful Leonardo Boshell <leonardop -at- gentoo.org>.
nenolod
parents:
625
diff
changeset
|
1084 |
0 | 1085 return; |
1086 } | |
1087 | |
1088 if (!cursor) | |
1089 cursor = gdk_cursor_new(GDK_LEFT_PTR); | |
1090 | |
1091 gdk_window_set_cursor(window->window, cursor); | |
1092 } | |
1093 | |
1094 /* text_get_extents() taken from The GIMP (C) Spencer Kimball, Peter | |
1095 * Mattis et al */ | |
1096 gboolean | |
1097 text_get_extents(const gchar * fontname, | |
1098 const gchar * text, | |
1099 gint * width, gint * height, gint * ascent, gint * descent) | |
1100 { | |
1101 PangoFontDescription *font_desc; | |
1102 PangoLayout *layout; | |
1103 PangoRectangle rect; | |
1104 | |
1105 g_return_val_if_fail(fontname != NULL, FALSE); | |
1106 g_return_val_if_fail(text != NULL, FALSE); | |
1107 | |
1108 /* FIXME: resolution */ | |
1109 layout = gtk_widget_create_pango_layout(GTK_WIDGET(mainwin), text); | |
1110 | |
1111 font_desc = pango_font_description_from_string(fontname); | |
1112 pango_layout_set_font_description(layout, font_desc); | |
1113 pango_font_description_free(font_desc); | |
1114 pango_layout_get_pixel_extents(layout, NULL, &rect); | |
1115 | |
1116 if (width) | |
1117 *width = rect.width; | |
1118 if (height) | |
1119 *height = rect.height; | |
1120 | |
1121 if (ascent || descent) { | |
1122 PangoLayoutIter *iter; | |
1123 PangoLayoutLine *line; | |
1124 | |
1125 iter = pango_layout_get_iter(layout); | |
1126 line = pango_layout_iter_get_line(iter); | |
1127 pango_layout_iter_free(iter); | |
1128 | |
1129 pango_layout_line_get_pixel_extents(line, NULL, &rect); | |
1130 | |
1131 if (ascent) | |
1132 *ascent = PANGO_ASCENT(rect); | |
1133 if (descent) | |
1134 *descent = -PANGO_DESCENT(rect); | |
1135 } | |
1136 | |
1137 g_object_unref(layout); | |
1138 | |
1139 return TRUE; | |
1140 } | |
1141 | |
1142 /* counts number of digits in a gint */ | |
1143 guint | |
1144 gint_count_digits(gint n) | |
1145 { | |
1146 guint count = 0; | |
1147 | |
1148 n = ABS(n); | |
1149 do { | |
1150 count++; | |
1151 n /= 10; | |
1152 } while (n > 0); | |
1153 | |
1154 return count; | |
1155 } | |
1156 | |
1157 static gchar * | |
1158 str_twenty_to_space(gchar * str) | |
1159 { | |
1160 gchar *match, *match_end; | |
1161 | |
1162 g_return_val_if_fail(str != NULL, NULL); | |
1163 | |
1164 while ((match = strstr(str, "%20"))) { | |
1165 match_end = match + 3; | |
1166 *match++ = ' '; | |
1167 while (*match_end) | |
1168 *match++ = *match_end++; | |
1169 *match = 0; | |
1170 } | |
1171 | |
1172 return str; | |
1173 } | |
1174 | |
1175 static gchar * | |
1176 str_replace_char(gchar * str, gchar old, gchar new) | |
1177 { | |
1178 gchar *match; | |
1179 | |
1180 g_return_val_if_fail(str != NULL, NULL); | |
1181 | |
1182 match = str; | |
1183 while ((match = strchr(match, old))) | |
1184 *match = new; | |
1185 | |
1186 return str; | |
1187 } | |
1188 | |
1189 gchar * | |
1190 str_append(gchar * str, const gchar * add_str) | |
1191 { | |
1192 return str_replace(str, g_strconcat(str, add_str, NULL)); | |
1193 } | |
1194 | |
1195 gchar * | |
1196 str_replace(gchar * str, gchar * new_str) | |
1197 { | |
1198 g_free(str); | |
1199 return new_str; | |
1200 } | |
1201 | |
1202 void | |
1203 str_replace_in(gchar ** str, gchar * new_str) | |
1204 { | |
1205 *str = str_replace(*str, new_str); | |
1206 } | |
1207 | |
1208 | |
1209 gboolean | |
1210 str_has_prefix_nocase(const gchar * str, const gchar * prefix) | |
1211 { | |
1212 return (strncasecmp(str, prefix, strlen(prefix)) == 0); | |
1213 } | |
1214 | |
1215 gboolean | |
1216 str_has_suffix_nocase(const gchar * str, const gchar * suffix) | |
1217 { | |
1218 return (strcasecmp(str + strlen(str) - strlen(suffix), suffix) == 0); | |
1219 } | |
1220 | |
1221 gboolean | |
1222 str_has_suffixes_nocase(const gchar * str, gchar * const *suffixes) | |
1223 { | |
1224 gchar *const *suffix; | |
1225 | |
1226 g_return_val_if_fail(str != NULL, FALSE); | |
1227 g_return_val_if_fail(suffixes != NULL, FALSE); | |
1228 | |
1229 for (suffix = suffixes; *suffix; suffix++) | |
1230 if (str_has_suffix_nocase(str, *suffix)) | |
1231 return TRUE; | |
1232 | |
1233 return FALSE; | |
1234 } | |
1235 | |
1236 gchar * | |
1237 str_to_utf8_fallback(const gchar * str) | |
1238 { | |
1239 gchar *out_str, *convert_str, *chr; | |
1240 | |
1241 /* NULL in NULL out */ | |
1242 if (!str) | |
1243 return NULL; | |
1244 | |
1245 convert_str = g_strdup(str); | |
1246 for (chr = convert_str; *chr; chr++) { | |
1247 if (*chr & 0x80) | |
1248 *chr = '?'; | |
1249 } | |
1250 | |
1251 out_str = g_strconcat(convert_str, _(" (invalid UTF-8)"), NULL); | |
1252 g_free(convert_str); | |
1253 | |
1254 return out_str; | |
1255 } | |
1256 | |
1257 gchar * | |
1258 filename_to_utf8(const gchar * filename) | |
1259 { | |
1260 gchar *out_str; | |
1261 | |
1262 /* NULL in NULL out */ | |
1263 if (!filename) | |
1264 return NULL; | |
1265 | |
1266 if ((out_str = g_filename_to_utf8(filename, -1, NULL, NULL, NULL))) | |
1267 return out_str; | |
1268 | |
1269 return str_to_utf8_fallback(filename); | |
1270 } | |
1271 | |
1272 gchar * | |
1273 str_to_utf8(const gchar * str) | |
1274 { | |
1275 gchar *out_str; | |
1276 | |
1277 /* NULL in NULL out */ | |
1278 if (!str) | |
1279 return NULL; | |
1280 | |
1105
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1281 /* chardet encoding detector */ |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1282 if ((out_str = chardet_to_utf8(str, strlen(str), NULL, NULL, NULL))) |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1283 return out_str; |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1284 |
0 | 1285 /* already UTF-8? */ |
1286 if (g_utf8_validate(str, -1, NULL)) | |
1287 return g_strdup(str); | |
1288 | |
1289 /* assume encoding associated with locale */ | |
1290 if ((out_str = g_locale_to_utf8(str, -1, NULL, NULL, NULL))) | |
1291 return out_str; | |
1292 | |
1293 /* all else fails, we mask off character codes >= 128, | |
1294 replace with '?' */ | |
1295 return str_to_utf8_fallback(str); | |
1296 } | |
1297 | |
1298 | |
1299 const gchar * | |
1300 str_skip_chars(const gchar * str, const gchar * chars) | |
1301 { | |
1302 while (strchr(chars, *str)) | |
1303 str++; | |
1304 return str; | |
1305 } | |
1306 | |
1307 gchar * | |
1308 convert_title_text(gchar * title) | |
1309 { | |
1310 g_return_val_if_fail(title != NULL, NULL); | |
1311 | |
1312 if (cfg.convert_underscore) | |
1313 str_replace_char(title, '_', ' '); | |
1314 | |
1315 if (cfg.convert_twenty) | |
1316 str_twenty_to_space(title); | |
1317 | |
1318 return title; | |
1319 } | |
1320 | |
1321 | |
1322 gboolean | |
1323 dir_foreach(const gchar * path, DirForeachFunc function, | |
1324 gpointer user_data, GError ** error) | |
1325 { | |
1326 GError *error_out = NULL; | |
1327 GDir *dir; | |
1328 const gchar *entry; | |
1329 gchar *entry_fullpath; | |
1330 | |
1331 if (!(dir = g_dir_open(path, 0, &error_out))) { | |
1332 g_propagate_error(error, error_out); | |
1333 return FALSE; | |
1334 } | |
1335 | |
1336 while ((entry = g_dir_read_name(dir))) { | |
1337 entry_fullpath = g_build_filename(path, entry, NULL); | |
1338 | |
1339 if ((*function) (entry_fullpath, entry, user_data)) { | |
1340 g_free(entry_fullpath); | |
1341 break; | |
1342 } | |
1343 | |
1344 g_free(entry_fullpath); | |
1345 } | |
1346 | |
1347 g_dir_close(dir); | |
1348 | |
1349 return TRUE; | |
1350 } | |
1351 | |
1352 GtkWidget * | |
1353 make_filebrowser(const gchar * title, | |
1354 gboolean save) | |
1355 { | |
1356 GtkWidget *dialog; | |
1357 GtkWidget *button; | |
1358 GtkWidget *button_close; | |
1359 | |
1360 g_return_val_if_fail(title != NULL, NULL); | |
1361 | |
1362 dialog = gtk_file_chooser_dialog_new(title, GTK_WINDOW(mainwin), | |
376 | 1363 GTK_FILE_CHOOSER_ACTION_OPEN, NULL, NULL); |
0 | 1364 if (save) |
1365 gtk_file_chooser_set_action(GTK_FILE_CHOOSER(dialog), | |
1366 GTK_FILE_CHOOSER_ACTION_SAVE); | |
1367 | |
1368 if (!save) | |
1369 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), TRUE); | |
1370 | |
1371 g_signal_connect(dialog, "destroy", | |
1372 G_CALLBACK(gtk_widget_destroyed), &dialog); | |
1373 | |
1374 #ifdef HAVE_GNOME_VFS | |
1375 gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(dialog), FALSE); | |
1376 #endif | |
1377 | |
1378 button_close = gtk_dialog_add_button(GTK_DIALOG(dialog), GTK_STOCK_CANCEL, | |
1379 GTK_RESPONSE_REJECT); | |
1380 gtk_button_set_use_stock(GTK_BUTTON(button_close), TRUE); | |
1381 GTK_WIDGET_SET_FLAGS(button_close, GTK_CAN_DEFAULT); | |
1382 g_signal_connect_swapped(button_close, "clicked", | |
1383 G_CALLBACK(gtk_widget_destroy), dialog); | |
1384 | |
1385 button = gtk_dialog_add_button(GTK_DIALOG(dialog), save ? | |
1386 GTK_STOCK_SAVE : GTK_STOCK_OPEN, | |
1387 GTK_RESPONSE_ACCEPT); | |
1388 gtk_button_set_use_stock(GTK_BUTTON(button), TRUE); | |
1389 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT); | |
1390 gtk_window_set_default(GTK_WINDOW(dialog), button); | |
1391 | |
1392 gtk_widget_show(dialog); | |
1393 | |
1394 return dialog; | |
1395 } | |
1396 | |
1397 | |
1398 GtkItemFactory * | |
1399 create_menu(GtkItemFactoryEntry *entries, | |
1400 guint n_entries, | |
1401 GtkAccelGroup *accel) | |
1402 { | |
1403 GtkItemFactory *menu; | |
1404 | |
1405 menu = gtk_item_factory_new(GTK_TYPE_MENU, "<Main>", accel); | |
1406 gtk_item_factory_set_translate_func(menu, bmp_menu_translate, NULL, | |
1407 NULL); | |
1408 gtk_item_factory_create_items(menu, n_entries, entries, NULL); | |
1409 | |
1410 return menu; | |
1411 } | |
1412 | |
1413 | |
1414 void | |
1415 make_submenu(GtkItemFactory *menu, | |
1416 const gchar *item_path, | |
1417 GtkItemFactory *submenu) | |
1418 { | |
1419 GtkWidget *item, *menu_; | |
1420 | |
1421 item = gtk_item_factory_get_widget(menu, item_path); | |
1422 menu_ = gtk_item_factory_get_widget(submenu, ""); | |
1423 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), menu_); | |
1424 } | |
1425 | |
1426 | |
1105
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1427 gchar *chardet_to_utf8(const gchar *str, gssize len, |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1428 gsize *arg_bytes_read, gsize *arg_bytes_write, GError **arg_error) |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1429 { |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1430 #ifdef USE_CHARDET |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1431 char *det = NULL, *encoding = NULL; |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1432 #endif |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1433 gchar *ret = NULL; |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1434 gsize *bytes_read, *bytes_write; |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1435 GError **error; |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1436 gsize my_bytes_read, my_bytes_write; |
0 | 1437 |
1105
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1438 bytes_read = arg_bytes_read ? arg_bytes_read : &my_bytes_read; |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1439 bytes_write = arg_bytes_write ? arg_bytes_write : &my_bytes_write; |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1440 error = arg_error ? arg_error : NULL; |
0 | 1441 |
1105
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1442 #ifdef USE_CHARDET |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1443 if(cfg.chardet_detector) |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1444 det = cfg.chardet_detector; |
0 | 1445 |
1105
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1446 if(det){ |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1447 if(!strncasecmp("japanese", det, sizeof("japanese"))) { |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1448 encoding = (char *)guess_jp(str, strlen(str)); |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1449 if (!encoding) |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1450 goto fallback; |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1451 } else if(!strncasecmp("taiwanese", det, sizeof("taiwanese"))) { |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1452 encoding = (char *)guess_tw(str, strlen(str)); |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1453 if (!encoding) |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1454 goto fallback; |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1455 } else if(!strncasecmp("chinese", det, sizeof("chinese"))) { |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1456 encoding = (char *)guess_cn(str, strlen(str)); |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1457 if (!encoding) |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1458 goto fallback; |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1459 } else if(!strncasecmp("korean", det, sizeof("korean"))) { |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1460 encoding = (char *)guess_kr(str, strlen(str)); |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1461 if (!encoding) |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1462 goto fallback; |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1463 #ifdef HAVE_UDET |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1464 } else if (!strncasecmp("universal", det, sizeof("universal"))) { |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1465 encoding = (char *)detectCharset((char *)str, strlen(str)); |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1466 if (!encoding) |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1467 goto fallback; |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1468 #endif |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1469 } else /* none, invalid */ |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1470 goto fallback; |
0 | 1471 |
1105
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1472 ret = g_convert(str, len, "UTF-8", encoding, bytes_read, bytes_write, error); |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1473 } |
0 | 1474 |
1105
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1475 fallback: |
1123
170f9b68dd03
[svn] The fallback label is not used unless detection is defined. Move endif down to reflect that.
chainsaw
parents:
1106
diff
changeset
|
1476 #endif |
1105
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1477 if(!ret && cfg.chardet_fallback){ |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1478 gchar **encs=NULL, **enc=NULL; |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1479 encs = g_strsplit_set(cfg.chardet_fallback, " ,:;|/", 0); |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1480 |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1481 if(encs){ |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1482 enc = encs; |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1483 for(enc=encs; *enc ; enc++){ |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1484 ret = g_convert(str, len, "UTF-8", *enc, bytes_read, bytes_write, error); |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1485 if(len == *bytes_read){ |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1486 break; |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1487 } |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1488 } |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1489 g_strfreev(encs); |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1490 } |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1491 } |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1492 |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1493 if(ret){ |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1494 if(g_utf8_validate(ret, -1, NULL)) |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1495 return ret; |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1496 else { |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1497 g_free(ret); |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1498 ret = NULL; |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1499 } |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1500 } |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1501 |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1502 return NULL; // if I have no idea, return NULL. |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1503 } |