Mercurial > audlegacy
annotate audacious/util.c @ 1424:7288721a34df trunk
[svn] - datarootdir stuff
author | nenolod |
---|---|
date | Tue, 18 Jul 2006 15:28:07 -0700 |
parents | 99454d030722 |
children | f12d7e208b43 |
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; |
1193
03414b9d2507
[svn] filesize must be cached, otherwise lookup loop will crash.
yaz
parents:
1166
diff
changeset
|
406 static gsize filesize = 0; |
03414b9d2507
[svn] filesize must be cached, otherwise lookup loop will crash.
yaz
parents:
1166
diff
changeset
|
407 gsize off = 0; |
245
1d8012046612
[svn] Convert UTF-16 (windows-1252) into something useful. Original patch by incomp@#audacious,
nenolod
parents:
86
diff
changeset
|
408 gchar *outbuf; |
1d8012046612
[svn] Convert UTF-16 (windows-1252) into something useful. Original patch by incomp@#audacious,
nenolod
parents:
86
diff
changeset
|
409 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
|
410 guint counter; |
0 | 411 |
412 if (!filename) | |
413 return NULL; | |
414 | |
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
|
415 /* |
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 * 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
|
417 * 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
|
418 * 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
|
419 * 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
|
420 * |
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 * 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
|
422 * 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
|
423 * - 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
|
424 */ |
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 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
|
426 { |
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 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
|
428 { |
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 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
|
430 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
|
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 |
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 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
|
434 { |
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 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
|
436 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
|
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 |
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 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
|
440 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
|
441 |
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 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
|
443 } |
0 | 444 |
245
1d8012046612
[svn] Convert UTF-16 (windows-1252) into something useful. Original patch by incomp@#audacious,
nenolod
parents:
86
diff
changeset
|
445 /* |
1d8012046612
[svn] Convert UTF-16 (windows-1252) into something useful. Original patch by incomp@#audacious,
nenolod
parents:
86
diff
changeset
|
446 * 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
|
447 * 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
|
448 */ |
1d8012046612
[svn] Convert UTF-16 (windows-1252) into something useful. Original patch by incomp@#audacious,
nenolod
parents:
86
diff
changeset
|
449 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
|
450 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
|
451 |
1d8012046612
[svn] Convert UTF-16 (windows-1252) into something useful. Original patch by incomp@#audacious,
nenolod
parents:
86
diff
changeset
|
452 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
|
453 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
|
454 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
|
455 else |
1d8012046612
[svn] Convert UTF-16 (windows-1252) into something useful. Original patch by incomp@#audacious,
nenolod
parents:
86
diff
changeset
|
456 return NULL; |
1d8012046612
[svn] Convert UTF-16 (windows-1252) into something useful. Original patch by incomp@#audacious,
nenolod
parents:
86
diff
changeset
|
457 |
1d8012046612
[svn] Convert UTF-16 (windows-1252) into something useful. Original patch by incomp@#audacious,
nenolod
parents:
86
diff
changeset
|
458 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
|
459 |
1d8012046612
[svn] Convert UTF-16 (windows-1252) into something useful. Original patch by incomp@#audacious,
nenolod
parents:
86
diff
changeset
|
460 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
|
461 g_free(buffer); |
1d8012046612
[svn] Convert UTF-16 (windows-1252) into something useful. Original patch by incomp@#audacious,
nenolod
parents:
86
diff
changeset
|
462 buffer = outbuf; |
1d8012046612
[svn] Convert UTF-16 (windows-1252) into something useful. Original patch by incomp@#audacious,
nenolod
parents:
86
diff
changeset
|
463 } else { |
1d8012046612
[svn] Convert UTF-16 (windows-1252) into something useful. Original patch by incomp@#audacious,
nenolod
parents:
86
diff
changeset
|
464 g_free(outbuf); |
1d8012046612
[svn] Convert UTF-16 (windows-1252) into something useful. Original patch by incomp@#audacious,
nenolod
parents:
86
diff
changeset
|
465 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
|
466 } |
1d8012046612
[svn] Convert UTF-16 (windows-1252) into something useful. Original patch by incomp@#audacious,
nenolod
parents:
86
diff
changeset
|
467 } |
1d8012046612
[svn] Convert UTF-16 (windows-1252) into something useful. Original patch by incomp@#audacious,
nenolod
parents:
86
diff
changeset
|
468 |
0 | 469 while (!ret_buffer && off < filesize) { |
470 while (off < filesize && | |
471 (buffer[off] == '\r' || buffer[off] == '\n' || | |
472 buffer[off] == ' ' || buffer[off] == '\t')) | |
473 off++; | |
474 if (off >= filesize) | |
475 break; | |
476 if (buffer[off] == '[') { | |
477 gint slen = strlen(section); | |
478 off++; | |
479 found_section = 0; | |
480 if (off + slen + 1 < filesize && | |
481 !strncasecmp(section, &buffer[off], slen)) { | |
482 off += slen; | |
483 if (buffer[off] == ']') { | |
484 off++; | |
485 found_section = 1; | |
486 } | |
487 } | |
488 } | |
489 else if (found_section && off + strlen(key) < filesize && | |
490 !strncasecmp(key, &buffer[off], strlen(key))) { | |
491 off += strlen(key); | |
492 while (off < filesize && | |
493 (buffer[off] == ' ' || buffer[off] == '\t')) | |
494 off++; | |
495 if (off >= filesize) | |
496 break; | |
497 if (buffer[off] == '=') { | |
498 off++; | |
499 while (off < filesize && | |
500 (buffer[off] == ' ' || buffer[off] == '\t')) | |
501 off++; | |
502 if (off >= filesize) | |
503 break; | |
504 len = 0; | |
505 while (off + len < filesize && | |
506 buffer[off + len] != '\r' && | |
507 buffer[off + len] != '\n' && buffer[off + len] != ';') | |
508 len++; | |
509 ret_buffer = g_strndup(&buffer[off], len); | |
510 off += len; | |
511 } | |
512 } | |
513 while (off < filesize && buffer[off] != '\r' && buffer[off] != '\n') | |
514 off++; | |
515 } | |
516 | |
517 return ret_buffer; | |
518 } | |
519 | |
520 GArray * | |
521 string_to_garray(const gchar * str) | |
522 { | |
523 GArray *array; | |
524 gint temp; | |
525 const gchar *ptr = str; | |
526 gchar *endptr; | |
527 | |
528 array = g_array_new(FALSE, TRUE, sizeof(gint)); | |
529 for (;;) { | |
530 temp = strtol(ptr, &endptr, 10); | |
531 if (ptr == endptr) | |
532 break; | |
533 g_array_append_val(array, temp); | |
534 ptr = endptr; | |
535 while (!isdigit(*ptr) && (*ptr) != '\0') | |
536 ptr++; | |
537 if (*ptr == '\0') | |
538 break; | |
539 } | |
540 return (array); | |
541 } | |
542 | |
543 GArray * | |
544 read_ini_array(const gchar * filename, const gchar * section, | |
545 const gchar * key) | |
546 { | |
547 gchar *temp; | |
548 GArray *a; | |
549 | |
550 if ((temp = read_ini_string(filename, section, key)) == NULL) { | |
551 g_free(temp); | |
552 return NULL; | |
553 } | |
554 a = string_to_garray(temp); | |
555 g_free(temp); | |
556 return a; | |
557 } | |
558 | |
559 void | |
560 glist_movedown(GList * list) | |
561 { | |
562 gpointer temp; | |
563 | |
564 if (g_list_next(list)) { | |
565 temp = list->data; | |
566 list->data = list->next->data; | |
567 list->next->data = temp; | |
568 } | |
569 } | |
570 | |
571 void | |
572 glist_moveup(GList * list) | |
573 { | |
574 gpointer temp; | |
575 | |
576 if (g_list_previous(list)) { | |
577 temp = list->data; | |
578 list->data = list->prev->data; | |
579 list->prev->data = temp; | |
580 } | |
581 } | |
582 | |
583 | |
584 void | |
585 util_menu_position(GtkMenu * menu, gint * x, gint * y, | |
586 gboolean * push_in, gpointer data) | |
587 { | |
588 GtkRequisition requisition; | |
589 gint screen_width; | |
590 gint screen_height; | |
591 MenuPos *pos = data; | |
592 | |
593 gtk_widget_size_request(GTK_WIDGET(menu), &requisition); | |
594 | |
595 screen_width = gdk_screen_width(); | |
596 screen_height = gdk_screen_height(); | |
597 | |
598 *x = CLAMP(pos->x - 2, 0, MAX(0, screen_width - requisition.width)); | |
599 *y = CLAMP(pos->y - 2, 0, MAX(0, screen_height - requisition.height)); | |
600 } | |
601 | |
602 static void | |
603 util_menu_delete_popup_data(GtkObject * object, GtkItemFactory * ifactory) | |
604 { | |
605 gtk_signal_disconnect_by_func(object, | |
606 GTK_SIGNAL_FUNC | |
607 (util_menu_delete_popup_data), ifactory); | |
608 gtk_object_remove_data_by_id(GTK_OBJECT(ifactory), quark_popup_data); | |
609 } | |
610 | |
611 | |
612 /* | |
613 * util_item_factory_popup[_with_data]() is a replacement for | |
614 * gtk_item_factory_popup[_with_data](). | |
615 * | |
616 * The difference is that the menu is always popped up whithin the | |
617 * screen. This means it does not neccesarily pop up at (x,y). | |
618 */ | |
619 | |
620 void | |
621 util_item_factory_popup_with_data(GtkItemFactory * ifactory, | |
622 gpointer data, | |
623 GtkDestroyNotify destroy, guint x, | |
624 guint y, guint mouse_button, guint32 time) | |
625 { | |
626 static GQuark quark_user_menu_pos = 0; | |
627 MenuPos *pos; | |
628 | |
629 if (!quark_user_menu_pos) | |
630 quark_user_menu_pos = g_quark_from_static_string("user_menu_pos"); | |
631 | |
632 if (!quark_popup_data) | |
633 quark_popup_data = | |
634 g_quark_from_static_string("GtkItemFactory-popup-data"); | |
635 | |
636 pos = g_object_get_qdata(G_OBJECT(ifactory), quark_user_menu_pos); | |
637 if (!pos) { | |
638 pos = g_new0(MenuPos, 1); | |
639 | |
640 g_object_set_qdata_full(G_OBJECT(ifactory->widget), | |
641 quark_user_menu_pos, pos, g_free); | |
642 } | |
643 pos->x = x; | |
644 pos->y = y; | |
645 | |
646 | |
647 if (data != NULL) { | |
648 g_object_set_qdata_full(G_OBJECT(ifactory), | |
649 quark_popup_data, data, destroy); | |
650 g_signal_connect(G_OBJECT(ifactory->widget), | |
651 "selection-done", | |
652 G_CALLBACK(util_menu_delete_popup_data), ifactory); | |
653 } | |
654 | |
655 gtk_menu_popup(GTK_MENU(ifactory->widget), NULL, NULL, | |
656 (GtkMenuPositionFunc) util_menu_position, | |
657 pos, mouse_button, time); | |
658 } | |
659 | |
660 void | |
661 util_item_factory_popup(GtkItemFactory * ifactory, guint x, guint y, | |
662 guint mouse_button, guint32 time) | |
663 { | |
664 util_item_factory_popup_with_data(ifactory, NULL, NULL, x, y, | |
665 mouse_button, time); | |
666 } | |
667 | |
668 | |
669 #define URL_HISTORY_MAX_SIZE 30 | |
670 | |
671 static void | |
672 util_add_url_callback(GtkWidget * widget, | |
673 GtkEntry * entry) | |
674 { | |
675 const gchar *text; | |
676 | |
677 text = gtk_entry_get_text(entry); | |
678 if (g_list_find_custom(cfg.url_history, text, (GCompareFunc) strcasecmp)) | |
679 return; | |
680 | |
681 cfg.url_history = g_list_prepend(cfg.url_history, g_strdup(text)); | |
682 | |
683 while (g_list_length(cfg.url_history) > URL_HISTORY_MAX_SIZE) { | |
684 GList *node = g_list_last(cfg.url_history); | |
685 g_free(node->data); | |
686 cfg.url_history = g_list_delete_link(cfg.url_history, node); | |
687 } | |
688 } | |
689 | |
690 GtkWidget * | |
86 | 691 util_add_url_dialog_new(const gchar * caption, GCallback ok_func, |
692 GCallback enqueue_func) | |
0 | 693 { |
86 | 694 GtkWidget *win, *vbox, *bbox, *enqueue, *ok, *cancel, *combo, *entry; |
0 | 695 GList *url; |
696 | |
697 win = gtk_window_new(GTK_WINDOW_TOPLEVEL); | |
698 gtk_window_set_title(GTK_WINDOW(win), caption); | |
699 gtk_window_set_type_hint(GTK_WINDOW(win), GDK_WINDOW_TYPE_HINT_DIALOG); | |
700 gtk_window_set_position(GTK_WINDOW(win), GTK_WIN_POS_CENTER); | |
701 gtk_window_set_default_size(GTK_WINDOW(win), 400, -1); | |
702 gtk_container_set_border_width(GTK_CONTAINER(win), 12); | |
703 | |
704 vbox = gtk_vbox_new(FALSE, 10); | |
705 gtk_container_add(GTK_CONTAINER(win), vbox); | |
706 | |
707 combo = gtk_combo_box_entry_new_text(); | |
708 gtk_box_pack_start(GTK_BOX(vbox), combo, FALSE, FALSE, 0); | |
709 | |
710 entry = gtk_bin_get_child(GTK_BIN(combo)); | |
711 gtk_window_set_focus(GTK_WINDOW(win), entry); | |
712 gtk_entry_set_text(GTK_ENTRY(entry), ""); | |
713 | |
714 for (url = cfg.url_history; url; url = g_list_next(url)) | |
715 gtk_combo_box_append_text(GTK_COMBO_BOX(combo), (const gchar *) url->data); | |
716 | |
717 g_signal_connect(entry, "activate", | |
718 G_CALLBACK(util_add_url_callback), | |
719 entry); | |
720 g_signal_connect(entry, "activate", | |
86 | 721 G_CALLBACK(ok_func), |
0 | 722 entry); |
723 g_signal_connect_swapped(entry, "activate", | |
724 G_CALLBACK(gtk_widget_destroy), | |
725 win); | |
726 | |
727 bbox = gtk_hbutton_box_new(); | |
728 gtk_button_box_set_layout(GTK_BUTTON_BOX(bbox), GTK_BUTTONBOX_END); | |
729 gtk_button_box_set_spacing(GTK_BUTTON_BOX(bbox), 5); | |
730 gtk_box_pack_start(GTK_BOX(vbox), bbox, FALSE, FALSE, 0); | |
731 | |
86 | 732 ok = gtk_button_new_from_stock(GTK_STOCK_OPEN); |
733 g_signal_connect(ok, "clicked", | |
734 G_CALLBACK(util_add_url_callback), entry); | |
735 g_signal_connect(ok, "clicked", | |
736 G_CALLBACK(ok_func), entry); | |
737 g_signal_connect_swapped(ok, "clicked", | |
738 G_CALLBACK(gtk_widget_destroy), | |
739 win); | |
740 gtk_box_pack_start(GTK_BOX(bbox), ok, FALSE, FALSE, 0); | |
741 | |
0 | 742 enqueue = gtk_button_new_from_stock(GTK_STOCK_ADD); |
743 gtk_box_pack_start(GTK_BOX(bbox), enqueue, FALSE, FALSE, 0); | |
744 | |
745 g_signal_connect(enqueue, "clicked", | |
746 G_CALLBACK(util_add_url_callback), | |
747 entry); | |
748 g_signal_connect(enqueue, "clicked", | |
749 G_CALLBACK(enqueue_func), | |
750 entry); | |
751 g_signal_connect_swapped(enqueue, "clicked", | |
752 G_CALLBACK(gtk_widget_destroy), | |
753 win); | |
754 | |
755 cancel = gtk_button_new_from_stock(GTK_STOCK_CLOSE); | |
756 gtk_box_pack_start(GTK_BOX(bbox), cancel, FALSE, FALSE, 0); | |
757 | |
758 g_signal_connect_swapped(cancel, "clicked", | |
759 G_CALLBACK(gtk_widget_destroy), | |
760 win); | |
761 | |
762 gtk_widget_show_all(vbox); | |
763 | |
764 return win; | |
765 } | |
766 | |
767 static void | |
768 filebrowser_add_files(GtkFileChooser * browser, | |
769 GSList * files) | |
770 { | |
771 GSList *cur; | |
772 gchar *ptr; | |
773 guint ctr = 0; | |
774 | |
775 if (GTK_IS_WIDGET(mainwin_jtf)) | |
776 gtk_widget_set_sensitive(mainwin_jtf, FALSE); | |
777 | |
778 for (cur = files; cur; cur = g_slist_next(cur)) { | |
779 | |
780 if (g_file_test(cur->data,G_FILE_TEST_IS_DIR)) { | |
781 playlist_add_dir((const gchar *) cur->data); | |
782 } else { | |
783 playlist_add((const gchar *) cur->data); | |
784 } | |
785 | |
786 if (++ctr == 20) { | |
787 playlistwin_update_list(); | |
788 ctr = 0; | |
789 while (gtk_events_pending() ) gtk_main_iteration(); | |
790 } | |
791 } | |
792 | |
793 playlistwin_update_list(); | |
794 | |
795 if (GTK_IS_WIDGET(mainwin_jtf)) | |
796 gtk_widget_set_sensitive(mainwin_jtf, TRUE); | |
797 | |
798 #ifdef HAVE_GNOME_VFS | |
799 ptr = gtk_file_chooser_get_current_folder_uri(GTK_FILE_CHOOSER(browser)); | |
800 #else | |
801 ptr = gtk_file_chooser_get_current_folder(GTK_FILE_CHOOSER(browser)); | |
802 #endif | |
803 | |
804 g_free(cfg.filesel_path); | |
805 cfg.filesel_path = ptr; | |
806 } | |
807 | |
808 static void | |
809 filebrowser_add(GtkFileChooser * browser) | |
810 { | |
811 GSList *files; | |
812 | |
813 #ifdef HAVE_GNOME_VFS | |
814 files = gtk_file_chooser_get_uris(GTK_FILE_CHOOSER(browser)); | |
815 #else | |
816 files = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(browser)); | |
817 #endif | |
818 | |
819 if (!files) { | |
820 return; | |
821 } | |
822 | |
823 filebrowser_add_files(browser, files); | |
824 g_slist_foreach(files, (GFunc) g_free, NULL); | |
825 g_slist_free(files); | |
826 } | |
827 | |
828 static void | |
829 filebrowser_play(GtkFileChooser * browser) | |
830 { | |
831 GSList *files; | |
832 | |
833 #ifdef HAVE_GNOME_VFS | |
834 files = gtk_file_chooser_get_uris(GTK_FILE_CHOOSER(browser)); | |
835 #else | |
836 files = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(browser)); | |
837 #endif | |
838 | |
839 if (!files) return; | |
840 | |
841 playlist_clear(); | |
842 | |
843 filebrowser_add_files(browser, files); | |
844 g_slist_foreach(files, (GFunc) g_free, NULL); | |
845 g_slist_free(files); | |
846 | |
847 bmp_playback_initiate(); | |
848 } | |
849 | |
850 | |
851 static void | |
852 _filebrowser_add(GtkWidget *widget, | |
853 gpointer data) | |
854 { | |
855 filebrowser_add(data); | |
856 gtk_file_chooser_unselect_all(GTK_FILE_CHOOSER(data)); | |
857 } | |
858 | |
859 static void | |
860 _filebrowser_play(GtkWidget *widget, gpointer data) | |
861 { | |
862 filebrowser_play(data); | |
863 gtk_file_chooser_unselect_all(data); | |
864 } | |
865 | |
866 #if 0 | |
867 static void | |
868 filebrowser_on_response(GtkFileChooser * browser, | |
869 gint response, | |
870 gpointer data) | |
871 { | |
872 gtk_widget_hide(GTK_WIDGET(browser)); | |
873 switch (response) { | |
874 case GTK_RESPONSE_OK: | |
875 break; | |
876 case GTK_RESPONSE_ACCEPT: | |
877 break; | |
878 case GTK_RESPONSE_CLOSE: | |
879 break; | |
880 } | |
881 gtk_widget_destroy(GTK_WIDGET(browser)); | |
882 | |
883 } | |
884 | |
885 #endif | |
886 | |
887 static void | |
888 _filebrowser_check_hide_add(GtkWidget * widget, | |
889 gpointer data) | |
890 { | |
891 cfg.close_dialog_add = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)); | |
892 } | |
893 | |
894 static void | |
895 _filebrowser_check_hide_open(GtkWidget * widget, | |
896 gpointer data) | |
897 { | |
898 cfg.close_dialog_open = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)); | |
899 } | |
900 | |
901 | |
902 | |
903 static gboolean | |
904 filebrowser_on_keypress(GtkWidget * browser, | |
905 GdkEventKey * event, | |
906 gpointer data) | |
907 { | |
908 if (event->keyval == GDK_Escape) { | |
909 /* FIXME: this crashes BMP for some reason */ | |
910 /* g_signal_emit_by_name(browser, "delete-event"); */ | |
911 gtk_widget_hide(browser); | |
912 return TRUE; | |
913 } | |
914 | |
915 return FALSE; | |
916 } | |
917 | |
918 static void | |
919 _filebrowser_do_hide_add(GtkWidget *widget, | |
920 gpointer data) | |
921 { | |
922 if (cfg.close_dialog_add) | |
923 gtk_widget_hide(data); | |
924 } | |
925 | |
926 static void | |
927 _filebrowser_do_hide_open(GtkWidget *widget, | |
928 gpointer data) | |
929 { | |
930 if (cfg.close_dialog_open) | |
931 gtk_widget_hide(data); | |
932 } | |
933 | |
934 void | |
935 util_run_filebrowser(gboolean play_button) | |
936 { | |
937 static GladeXML *xml = NULL; | |
938 static GtkWidget *dialog = NULL; | |
939 static GtkWidget *chooser = NULL; | |
940 | |
941 static GtkWidget *button_add; | |
942 static GtkWidget *button_select_all, *button_deselect_all; | |
943 static GtkWidget *toggle; | |
944 | |
945 static gulong handlerid, handlerid_check, handlerid_do; | |
946 static gulong handlerid_activate, handlerid_do_activate; | |
947 | |
948 if (!xml) { | |
949 /* FIXME: Creating a file chooser dialog manually using | |
950 libglade because there's no way to stop | |
951 GtkFileChooserDialog from resizing the buttons to the same | |
952 size. The long toggle button title causes the buttons to | |
953 turn unnecessarily elongated and fugly. */ | |
954 | |
955 GtkWidget *alignment; | |
956 | |
957 xml = glade_xml_new_or_die(_("Add/Open Files dialog"), | |
958 DATA_DIR "/glade/addfiles.glade", | |
959 NULL, NULL); | |
960 glade_xml_signal_autoconnect(xml); | |
961 | |
962 dialog = glade_xml_get_widget(xml, "add_files_dialog"); | |
963 | |
964 /* FIXME: Creating file chooser widget here because libglade <= 2.4.0 does | |
965 not support GtkFileChooserWidget */ | |
966 | |
967 chooser = gtk_file_chooser_widget_new(GTK_FILE_CHOOSER_ACTION_OPEN); | |
968 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(chooser), TRUE); | |
969 | |
970 #ifdef HAVE_GNOME_VFS | |
971 gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(chooser), FALSE); | |
972 #endif | |
973 | |
974 if (cfg.filesel_path) | |
975 #ifdef HAVE_GNOME_VFS | |
976 gtk_file_chooser_set_current_folder_uri(GTK_FILE_CHOOSER(chooser), | |
977 cfg.filesel_path); | |
978 #else | |
979 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(chooser), | |
980 cfg.filesel_path); | |
981 #endif | |
982 | |
983 alignment = glade_xml_get_widget(xml, "alignment2"); | |
984 gtk_container_add(GTK_CONTAINER(alignment), chooser); | |
985 | |
986 toggle = glade_xml_get_widget(xml, "close_on_action"); | |
987 button_select_all = glade_xml_get_widget(xml, "select_all"); | |
988 button_deselect_all = glade_xml_get_widget(xml, "deselect_all"); | |
989 button_add = glade_xml_get_widget(xml, "action"); | |
990 | |
991 g_signal_connect_swapped(button_select_all, "clicked", | |
992 G_CALLBACK(gtk_file_chooser_select_all), | |
993 chooser); | |
994 g_signal_connect_swapped(button_deselect_all, "clicked", | |
995 G_CALLBACK(gtk_file_chooser_unselect_all), | |
996 chooser); | |
997 | |
998 g_signal_connect(dialog, "key_press_event", | |
999 G_CALLBACK(filebrowser_on_keypress), | |
1000 NULL); | |
1001 | |
1002 gtk_widget_show_all(dialog); | |
1003 } /* !xml */ | |
1004 else { | |
1005 g_signal_handler_disconnect(button_add, handlerid); | |
1006 g_signal_handler_disconnect(toggle, handlerid_check); | |
1007 g_signal_handler_disconnect(chooser, handlerid_activate); | |
1008 g_signal_handler_disconnect(button_add, handlerid_do); | |
1009 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
|
1010 if (cfg.refresh_file_list) { |
6afdd0d7e1e1
[svn] Make refreshing optional, default to disabled if Gnome VFS available.
nemo
parents:
879
diff
changeset
|
1011 // *sigh* force a refresh |
6afdd0d7e1e1
[svn] Make refreshing optional, default to disabled if Gnome VFS available.
nemo
parents:
879
diff
changeset
|
1012 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
|
1013 } |
0 | 1014 } |
1015 | |
1016 if (play_button) { | |
1017 gtk_window_set_title(GTK_WINDOW(dialog), _("Open Files")); | |
1018 | |
1019 gtk_button_set_label(GTK_BUTTON(button_add), GTK_STOCK_OPEN); | |
1020 | |
1021 gtk_button_set_label(GTK_BUTTON(toggle), _("Close dialog on Open")); | |
1022 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(toggle), cfg.close_dialog_open); | |
1023 | |
1024 handlerid = g_signal_connect(button_add, "clicked", G_CALLBACK(_filebrowser_play), chooser); | |
1025 handlerid_check = g_signal_connect(toggle, "toggled", G_CALLBACK(_filebrowser_check_hide_open), NULL); | |
1026 handlerid_do = g_signal_connect_after(button_add, "clicked", G_CALLBACK(_filebrowser_do_hide_open), dialog); | |
1027 handlerid_activate = g_signal_connect(chooser, "file-activated", G_CALLBACK(_filebrowser_play), chooser); | |
1028 handlerid_do_activate = g_signal_connect_after(chooser,"file_activated", G_CALLBACK(_filebrowser_do_hide_open), dialog); | |
1029 } | |
1030 else { | |
1031 gtk_window_set_title(GTK_WINDOW(dialog), _("Add Files")); | |
1032 | |
1033 gtk_button_set_label(GTK_BUTTON(button_add), GTK_STOCK_ADD); | |
1034 | |
1035 gtk_button_set_label(GTK_BUTTON(toggle), _("Close dialog on Add")); | |
1036 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(toggle), cfg.close_dialog_add); | |
1037 | |
1038 handlerid = g_signal_connect(button_add, "clicked", G_CALLBACK(_filebrowser_add), chooser); | |
1039 handlerid_check = g_signal_connect(toggle, "toggled", G_CALLBACK(_filebrowser_check_hide_add), NULL); | |
1040 handlerid_do = g_signal_connect_after(button_add, "clicked", G_CALLBACK(_filebrowser_do_hide_add), dialog); | |
1041 handlerid_activate = g_signal_connect(chooser, "file-activated", G_CALLBACK(_filebrowser_add), chooser); | |
1042 handlerid_do_activate = g_signal_connect_after(chooser,"file_activated", G_CALLBACK(_filebrowser_do_hide_add), dialog); | |
1043 } | |
1044 | |
1045 gtk_window_present(GTK_WINDOW(dialog)); | |
1046 } | |
1047 | |
1048 GdkFont * | |
1049 util_font_load(const gchar * name) | |
1050 { | |
1051 GdkFont *font; | |
1052 PangoFontDescription *desc; | |
1053 | |
1054 desc = pango_font_description_from_string(name); | |
1055 font = gdk_font_from_description(desc); | |
1056 | |
1057 return font; | |
1058 } | |
1059 | |
1060 #ifdef ENABLE_NLS | |
1061 gchar * | |
1062 bmp_menu_translate(const gchar * path, gpointer func_data) | |
1063 { | |
1064 gchar *translation = gettext(path); | |
1065 | |
1066 if (!translation || *translation != '/') { | |
1067 g_warning("Bad translation for menupath: %s", path); | |
1068 translation = (gchar *) path; | |
1069 } | |
1070 | |
1071 return translation; | |
1072 } | |
1073 #endif | |
1074 | |
1075 void | |
1076 util_set_cursor(GtkWidget * window) | |
1077 { | |
1078 static GdkCursor *cursor = NULL; | |
1079 | |
1080 if (!window) { | |
1081 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
|
1082 gdk_cursor_unref(cursor); |
0 | 1083 cursor = NULL; |
1084 } | |
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
|
1085 |
0 | 1086 return; |
1087 } | |
1088 | |
1089 if (!cursor) | |
1090 cursor = gdk_cursor_new(GDK_LEFT_PTR); | |
1091 | |
1092 gdk_window_set_cursor(window->window, cursor); | |
1093 } | |
1094 | |
1095 /* text_get_extents() taken from The GIMP (C) Spencer Kimball, Peter | |
1096 * Mattis et al */ | |
1097 gboolean | |
1098 text_get_extents(const gchar * fontname, | |
1099 const gchar * text, | |
1100 gint * width, gint * height, gint * ascent, gint * descent) | |
1101 { | |
1102 PangoFontDescription *font_desc; | |
1103 PangoLayout *layout; | |
1104 PangoRectangle rect; | |
1105 | |
1106 g_return_val_if_fail(fontname != NULL, FALSE); | |
1107 g_return_val_if_fail(text != NULL, FALSE); | |
1108 | |
1109 /* FIXME: resolution */ | |
1110 layout = gtk_widget_create_pango_layout(GTK_WIDGET(mainwin), text); | |
1111 | |
1112 font_desc = pango_font_description_from_string(fontname); | |
1113 pango_layout_set_font_description(layout, font_desc); | |
1114 pango_font_description_free(font_desc); | |
1115 pango_layout_get_pixel_extents(layout, NULL, &rect); | |
1116 | |
1117 if (width) | |
1118 *width = rect.width; | |
1119 if (height) | |
1120 *height = rect.height; | |
1121 | |
1122 if (ascent || descent) { | |
1123 PangoLayoutIter *iter; | |
1124 PangoLayoutLine *line; | |
1125 | |
1126 iter = pango_layout_get_iter(layout); | |
1127 line = pango_layout_iter_get_line(iter); | |
1128 pango_layout_iter_free(iter); | |
1129 | |
1130 pango_layout_line_get_pixel_extents(line, NULL, &rect); | |
1131 | |
1132 if (ascent) | |
1133 *ascent = PANGO_ASCENT(rect); | |
1134 if (descent) | |
1135 *descent = -PANGO_DESCENT(rect); | |
1136 } | |
1137 | |
1138 g_object_unref(layout); | |
1139 | |
1140 return TRUE; | |
1141 } | |
1142 | |
1143 /* counts number of digits in a gint */ | |
1144 guint | |
1145 gint_count_digits(gint n) | |
1146 { | |
1147 guint count = 0; | |
1148 | |
1149 n = ABS(n); | |
1150 do { | |
1151 count++; | |
1152 n /= 10; | |
1153 } while (n > 0); | |
1154 | |
1155 return count; | |
1156 } | |
1157 | |
1158 static gchar * | |
1159 str_twenty_to_space(gchar * str) | |
1160 { | |
1161 gchar *match, *match_end; | |
1162 | |
1163 g_return_val_if_fail(str != NULL, NULL); | |
1164 | |
1165 while ((match = strstr(str, "%20"))) { | |
1166 match_end = match + 3; | |
1167 *match++ = ' '; | |
1168 while (*match_end) | |
1169 *match++ = *match_end++; | |
1170 *match = 0; | |
1171 } | |
1172 | |
1173 return str; | |
1174 } | |
1175 | |
1176 static gchar * | |
1177 str_replace_char(gchar * str, gchar old, gchar new) | |
1178 { | |
1179 gchar *match; | |
1180 | |
1181 g_return_val_if_fail(str != NULL, NULL); | |
1182 | |
1183 match = str; | |
1184 while ((match = strchr(match, old))) | |
1185 *match = new; | |
1186 | |
1187 return str; | |
1188 } | |
1189 | |
1190 gchar * | |
1191 str_append(gchar * str, const gchar * add_str) | |
1192 { | |
1193 return str_replace(str, g_strconcat(str, add_str, NULL)); | |
1194 } | |
1195 | |
1196 gchar * | |
1197 str_replace(gchar * str, gchar * new_str) | |
1198 { | |
1199 g_free(str); | |
1200 return new_str; | |
1201 } | |
1202 | |
1203 void | |
1204 str_replace_in(gchar ** str, gchar * new_str) | |
1205 { | |
1206 *str = str_replace(*str, new_str); | |
1207 } | |
1208 | |
1209 | |
1210 gboolean | |
1211 str_has_prefix_nocase(const gchar * str, const gchar * prefix) | |
1212 { | |
1213 return (strncasecmp(str, prefix, strlen(prefix)) == 0); | |
1214 } | |
1215 | |
1216 gboolean | |
1217 str_has_suffix_nocase(const gchar * str, const gchar * suffix) | |
1218 { | |
1219 return (strcasecmp(str + strlen(str) - strlen(suffix), suffix) == 0); | |
1220 } | |
1221 | |
1222 gboolean | |
1223 str_has_suffixes_nocase(const gchar * str, gchar * const *suffixes) | |
1224 { | |
1225 gchar *const *suffix; | |
1226 | |
1227 g_return_val_if_fail(str != NULL, FALSE); | |
1228 g_return_val_if_fail(suffixes != NULL, FALSE); | |
1229 | |
1230 for (suffix = suffixes; *suffix; suffix++) | |
1231 if (str_has_suffix_nocase(str, *suffix)) | |
1232 return TRUE; | |
1233 | |
1234 return FALSE; | |
1235 } | |
1236 | |
1237 gchar * | |
1238 str_to_utf8_fallback(const gchar * str) | |
1239 { | |
1240 gchar *out_str, *convert_str, *chr; | |
1241 | |
1242 /* NULL in NULL out */ | |
1243 if (!str) | |
1244 return NULL; | |
1245 | |
1246 convert_str = g_strdup(str); | |
1247 for (chr = convert_str; *chr; chr++) { | |
1248 if (*chr & 0x80) | |
1249 *chr = '?'; | |
1250 } | |
1251 | |
1252 out_str = g_strconcat(convert_str, _(" (invalid UTF-8)"), NULL); | |
1253 g_free(convert_str); | |
1254 | |
1255 return out_str; | |
1256 } | |
1257 | |
1258 gchar * | |
1259 filename_to_utf8(const gchar * filename) | |
1260 { | |
1261 gchar *out_str; | |
1262 | |
1263 /* NULL in NULL out */ | |
1264 if (!filename) | |
1265 return NULL; | |
1266 | |
1267 if ((out_str = g_filename_to_utf8(filename, -1, NULL, NULL, NULL))) | |
1268 return out_str; | |
1269 | |
1270 return str_to_utf8_fallback(filename); | |
1271 } | |
1272 | |
1273 gchar * | |
1274 str_to_utf8(const gchar * str) | |
1275 { | |
1276 gchar *out_str; | |
1277 | |
1278 /* NULL in NULL out */ | |
1279 if (!str) | |
1280 return NULL; | |
1281 | |
1324
99454d030722
[svn] try to avoid malconversion of latin1 character when chardet has been enabled.
yaz
parents:
1193
diff
changeset
|
1282 /* Note: Currently, playlist calls this function repeatedly, even |
99454d030722
[svn] try to avoid malconversion of latin1 character when chardet has been enabled.
yaz
parents:
1193
diff
changeset
|
1283 * if the string is already converted into utf-8. |
99454d030722
[svn] try to avoid malconversion of latin1 character when chardet has been enabled.
yaz
parents:
1193
diff
changeset
|
1284 * chardet_to_utf8() would convert a valid utf-8 string into a |
99454d030722
[svn] try to avoid malconversion of latin1 character when chardet has been enabled.
yaz
parents:
1193
diff
changeset
|
1285 * different utf-8 string, if fallback encodings were supplied and |
99454d030722
[svn] try to avoid malconversion of latin1 character when chardet has been enabled.
yaz
parents:
1193
diff
changeset
|
1286 * the given string could be treated as a string in one of fallback |
99454d030722
[svn] try to avoid malconversion of latin1 character when chardet has been enabled.
yaz
parents:
1193
diff
changeset
|
1287 * encodings. To avoid this, the order of evaluation has been |
99454d030722
[svn] try to avoid malconversion of latin1 character when chardet has been enabled.
yaz
parents:
1193
diff
changeset
|
1288 * changed. (It might cause a drawback?) |
99454d030722
[svn] try to avoid malconversion of latin1 character when chardet has been enabled.
yaz
parents:
1193
diff
changeset
|
1289 */ |
99454d030722
[svn] try to avoid malconversion of latin1 character when chardet has been enabled.
yaz
parents:
1193
diff
changeset
|
1290 /* already UTF-8? */ |
99454d030722
[svn] try to avoid malconversion of latin1 character when chardet has been enabled.
yaz
parents:
1193
diff
changeset
|
1291 if (g_utf8_validate(str, -1, NULL)) |
99454d030722
[svn] try to avoid malconversion of latin1 character when chardet has been enabled.
yaz
parents:
1193
diff
changeset
|
1292 return g_strdup(str); |
99454d030722
[svn] try to avoid malconversion of latin1 character when chardet has been enabled.
yaz
parents:
1193
diff
changeset
|
1293 |
1105
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1294 /* chardet encoding detector */ |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1295 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
|
1296 return out_str; |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1297 |
0 | 1298 /* assume encoding associated with locale */ |
1299 if ((out_str = g_locale_to_utf8(str, -1, NULL, NULL, NULL))) | |
1300 return out_str; | |
1301 | |
1302 /* all else fails, we mask off character codes >= 128, | |
1303 replace with '?' */ | |
1304 return str_to_utf8_fallback(str); | |
1305 } | |
1306 | |
1307 | |
1308 const gchar * | |
1309 str_skip_chars(const gchar * str, const gchar * chars) | |
1310 { | |
1311 while (strchr(chars, *str)) | |
1312 str++; | |
1313 return str; | |
1314 } | |
1315 | |
1316 gchar * | |
1317 convert_title_text(gchar * title) | |
1318 { | |
1319 g_return_val_if_fail(title != NULL, NULL); | |
1320 | |
1321 if (cfg.convert_underscore) | |
1322 str_replace_char(title, '_', ' '); | |
1323 | |
1324 if (cfg.convert_twenty) | |
1325 str_twenty_to_space(title); | |
1326 | |
1327 return title; | |
1328 } | |
1329 | |
1330 | |
1331 gboolean | |
1332 dir_foreach(const gchar * path, DirForeachFunc function, | |
1333 gpointer user_data, GError ** error) | |
1334 { | |
1335 GError *error_out = NULL; | |
1336 GDir *dir; | |
1337 const gchar *entry; | |
1338 gchar *entry_fullpath; | |
1339 | |
1340 if (!(dir = g_dir_open(path, 0, &error_out))) { | |
1341 g_propagate_error(error, error_out); | |
1342 return FALSE; | |
1343 } | |
1344 | |
1345 while ((entry = g_dir_read_name(dir))) { | |
1346 entry_fullpath = g_build_filename(path, entry, NULL); | |
1347 | |
1348 if ((*function) (entry_fullpath, entry, user_data)) { | |
1349 g_free(entry_fullpath); | |
1350 break; | |
1351 } | |
1352 | |
1353 g_free(entry_fullpath); | |
1354 } | |
1355 | |
1356 g_dir_close(dir); | |
1357 | |
1358 return TRUE; | |
1359 } | |
1360 | |
1361 GtkWidget * | |
1362 make_filebrowser(const gchar * title, | |
1363 gboolean save) | |
1364 { | |
1365 GtkWidget *dialog; | |
1366 GtkWidget *button; | |
1367 GtkWidget *button_close; | |
1368 | |
1369 g_return_val_if_fail(title != NULL, NULL); | |
1370 | |
1371 dialog = gtk_file_chooser_dialog_new(title, GTK_WINDOW(mainwin), | |
376 | 1372 GTK_FILE_CHOOSER_ACTION_OPEN, NULL, NULL); |
0 | 1373 if (save) |
1374 gtk_file_chooser_set_action(GTK_FILE_CHOOSER(dialog), | |
1375 GTK_FILE_CHOOSER_ACTION_SAVE); | |
1376 | |
1377 if (!save) | |
1378 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), TRUE); | |
1379 | |
1380 g_signal_connect(dialog, "destroy", | |
1381 G_CALLBACK(gtk_widget_destroyed), &dialog); | |
1382 | |
1383 #ifdef HAVE_GNOME_VFS | |
1384 gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(dialog), FALSE); | |
1385 #endif | |
1386 | |
1387 button_close = gtk_dialog_add_button(GTK_DIALOG(dialog), GTK_STOCK_CANCEL, | |
1388 GTK_RESPONSE_REJECT); | |
1389 gtk_button_set_use_stock(GTK_BUTTON(button_close), TRUE); | |
1390 GTK_WIDGET_SET_FLAGS(button_close, GTK_CAN_DEFAULT); | |
1391 g_signal_connect_swapped(button_close, "clicked", | |
1392 G_CALLBACK(gtk_widget_destroy), dialog); | |
1393 | |
1394 button = gtk_dialog_add_button(GTK_DIALOG(dialog), save ? | |
1395 GTK_STOCK_SAVE : GTK_STOCK_OPEN, | |
1396 GTK_RESPONSE_ACCEPT); | |
1397 gtk_button_set_use_stock(GTK_BUTTON(button), TRUE); | |
1398 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT); | |
1399 gtk_window_set_default(GTK_WINDOW(dialog), button); | |
1400 | |
1401 gtk_widget_show(dialog); | |
1402 | |
1403 return dialog; | |
1404 } | |
1405 | |
1406 | |
1407 GtkItemFactory * | |
1408 create_menu(GtkItemFactoryEntry *entries, | |
1409 guint n_entries, | |
1410 GtkAccelGroup *accel) | |
1411 { | |
1412 GtkItemFactory *menu; | |
1413 | |
1414 menu = gtk_item_factory_new(GTK_TYPE_MENU, "<Main>", accel); | |
1415 gtk_item_factory_set_translate_func(menu, bmp_menu_translate, NULL, | |
1416 NULL); | |
1417 gtk_item_factory_create_items(menu, n_entries, entries, NULL); | |
1418 | |
1419 return menu; | |
1420 } | |
1421 | |
1422 | |
1423 void | |
1424 make_submenu(GtkItemFactory *menu, | |
1425 const gchar *item_path, | |
1426 GtkItemFactory *submenu) | |
1427 { | |
1428 GtkWidget *item, *menu_; | |
1429 | |
1430 item = gtk_item_factory_get_widget(menu, item_path); | |
1431 menu_ = gtk_item_factory_get_widget(submenu, ""); | |
1432 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), menu_); | |
1433 } | |
1434 | |
1435 | |
1105
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1436 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
|
1437 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
|
1438 { |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1439 #ifdef USE_CHARDET |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1440 char *det = NULL, *encoding = NULL; |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1441 #endif |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1442 gchar *ret = NULL; |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1443 gsize *bytes_read, *bytes_write; |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1444 GError **error; |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1445 gsize my_bytes_read, my_bytes_write; |
0 | 1446 |
1105
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1447 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
|
1448 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
|
1449 error = arg_error ? arg_error : NULL; |
0 | 1450 |
1105
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1451 #ifdef USE_CHARDET |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1452 if(cfg.chardet_detector) |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1453 det = cfg.chardet_detector; |
0 | 1454 |
1105
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1455 if(det){ |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1456 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
|
1457 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
|
1458 if (!encoding) |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1459 goto fallback; |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1460 } 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
|
1461 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
|
1462 if (!encoding) |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1463 goto fallback; |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1464 } 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
|
1465 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
|
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 } 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
|
1469 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
|
1470 if (!encoding) |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1471 goto fallback; |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1472 #ifdef HAVE_UDET |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1473 } 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
|
1474 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
|
1475 if (!encoding) |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1476 goto fallback; |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1477 #endif |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1478 } else /* none, invalid */ |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1479 goto fallback; |
0 | 1480 |
1105
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1481 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
|
1482 } |
0 | 1483 |
1105
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1484 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
|
1485 #endif |
1105
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1486 if(!ret && cfg.chardet_fallback){ |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1487 gchar **encs=NULL, **enc=NULL; |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1488 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
|
1489 |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1490 if(encs){ |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1491 enc = encs; |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1492 for(enc=encs; *enc ; enc++){ |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1493 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
|
1494 if(len == *bytes_read){ |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1495 break; |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1496 } |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1497 } |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1498 g_strfreev(encs); |
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 |
1324
99454d030722
[svn] try to avoid malconversion of latin1 character when chardet has been enabled.
yaz
parents:
1193
diff
changeset
|
1502 #ifdef USE_CHARDET |
99454d030722
[svn] try to avoid malconversion of latin1 character when chardet has been enabled.
yaz
parents:
1193
diff
changeset
|
1503 /* many tag libraries return 2byte latin1 utf8 character as |
99454d030722
[svn] try to avoid malconversion of latin1 character when chardet has been enabled.
yaz
parents:
1193
diff
changeset
|
1504 converted 8bit iso-8859-1 character, if they are asked to return |
99454d030722
[svn] try to avoid malconversion of latin1 character when chardet has been enabled.
yaz
parents:
1193
diff
changeset
|
1505 latin1 string. |
99454d030722
[svn] try to avoid malconversion of latin1 character when chardet has been enabled.
yaz
parents:
1193
diff
changeset
|
1506 */ |
99454d030722
[svn] try to avoid malconversion of latin1 character when chardet has been enabled.
yaz
parents:
1193
diff
changeset
|
1507 if(!ret){ |
99454d030722
[svn] try to avoid malconversion of latin1 character when chardet has been enabled.
yaz
parents:
1193
diff
changeset
|
1508 ret = g_convert(str, len, "UTF-8", "ISO-8859-1", bytes_read, bytes_write, error); |
99454d030722
[svn] try to avoid malconversion of latin1 character when chardet has been enabled.
yaz
parents:
1193
diff
changeset
|
1509 } |
99454d030722
[svn] try to avoid malconversion of latin1 character when chardet has been enabled.
yaz
parents:
1193
diff
changeset
|
1510 #endif |
99454d030722
[svn] try to avoid malconversion of latin1 character when chardet has been enabled.
yaz
parents:
1193
diff
changeset
|
1511 |
1105
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1512 if(ret){ |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1513 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
|
1514 return ret; |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1515 else { |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1516 g_free(ret); |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1517 ret = NULL; |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1518 } |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1519 } |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1520 |
1324
99454d030722
[svn] try to avoid malconversion of latin1 character when chardet has been enabled.
yaz
parents:
1193
diff
changeset
|
1521 return NULL; /* if I have no idea, return NULL. */ |
1105
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1522 } |