Mercurial > audlegacy
annotate audacious/util.c @ 1847:a46b6465c7e5 trunk
[svn] - update NEWS
author | nenolod |
---|---|
date | Sun, 08 Oct 2006 18:17:58 -0700 |
parents | be1c5774f675 |
children | aceb472cce6c |
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 | |
1459 | 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
0 | 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 <stdio.h> | |
36 #include <stdlib.h> | |
37 #include <string.h> | |
38 #include <ctype.h> | |
39 | |
1640 | 40 #include "platform/smartinclude.h" |
0 | 41 #include <gdk/gdkkeysyms.h> |
42 #include <X11/Xlib.h> | |
287
2d919c756941
[svn] - attach -Iaudacious to libaudcore build crap.
nenolod
parents:
284
diff
changeset
|
43 //#include <sys/ipc.h> |
0 | 44 #include <unistd.h> |
45 #include <errno.h> | |
46 | |
47 #ifdef HAVE_FTS_H | |
48 # include <fts.h> | |
49 #endif | |
50 | |
51 #include "glade.h" | |
52 #include "input.h" | |
53 #include "main.h" | |
538
e4e897d20791
[svn] remove libaudcore, we never did anything with it
nenolod
parents:
383
diff
changeset
|
54 #include "playback.h" |
0 | 55 #include "playlist.h" |
1653 | 56 #include "ui_playlist.h" |
0 | 57 |
1105
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
58 #ifdef USE_CHARDET |
1106 | 59 #include "../libguess/libguess.h" |
1613
9becbe564217
[svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
1459
diff
changeset
|
60 #include "../librcd/librcd.h" |
1106 | 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; | |
1717
837983bac90f
[svn] Fixed a lot of warnings that only showed up on *BSD.
js
parents:
1653
diff
changeset
|
535 while (!isdigit((int) *ptr) && (*ptr) != '\0') |
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); | |
1752
be1c5774f675
[svn] - fix a memory leak in util_run_filebrowser()
nenolod
parents:
1717
diff
changeset
|
1010 |
be1c5774f675
[svn] - fix a memory leak in util_run_filebrowser()
nenolod
parents:
1717
diff
changeset
|
1011 if (cfg.refresh_file_list) |
be1c5774f675
[svn] - fix a memory leak in util_run_filebrowser()
nenolod
parents:
1717
diff
changeset
|
1012 { |
be1c5774f675
[svn] - fix a memory leak in util_run_filebrowser()
nenolod
parents:
1717
diff
changeset
|
1013 gchar *tmp = gtk_file_chooser_get_current_folder(GTK_FILE_CHOOSER(chooser)); |
be1c5774f675
[svn] - fix a memory leak in util_run_filebrowser()
nenolod
parents:
1717
diff
changeset
|
1014 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(chooser), tmp); |
be1c5774f675
[svn] - fix a memory leak in util_run_filebrowser()
nenolod
parents:
1717
diff
changeset
|
1015 |
be1c5774f675
[svn] - fix a memory leak in util_run_filebrowser()
nenolod
parents:
1717
diff
changeset
|
1016 g_free(tmp); |
be1c5774f675
[svn] - fix a memory leak in util_run_filebrowser()
nenolod
parents:
1717
diff
changeset
|
1017 } |
0 | 1018 } |
1019 | |
1020 if (play_button) { | |
1021 gtk_window_set_title(GTK_WINDOW(dialog), _("Open Files")); | |
1022 | |
1023 gtk_button_set_label(GTK_BUTTON(button_add), GTK_STOCK_OPEN); | |
1024 | |
1025 gtk_button_set_label(GTK_BUTTON(toggle), _("Close dialog on Open")); | |
1026 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(toggle), cfg.close_dialog_open); | |
1027 | |
1028 handlerid = g_signal_connect(button_add, "clicked", G_CALLBACK(_filebrowser_play), chooser); | |
1029 handlerid_check = g_signal_connect(toggle, "toggled", G_CALLBACK(_filebrowser_check_hide_open), NULL); | |
1030 handlerid_do = g_signal_connect_after(button_add, "clicked", G_CALLBACK(_filebrowser_do_hide_open), dialog); | |
1031 handlerid_activate = g_signal_connect(chooser, "file-activated", G_CALLBACK(_filebrowser_play), chooser); | |
1032 handlerid_do_activate = g_signal_connect_after(chooser,"file_activated", G_CALLBACK(_filebrowser_do_hide_open), dialog); | |
1033 } | |
1034 else { | |
1035 gtk_window_set_title(GTK_WINDOW(dialog), _("Add Files")); | |
1036 | |
1037 gtk_button_set_label(GTK_BUTTON(button_add), GTK_STOCK_ADD); | |
1038 | |
1039 gtk_button_set_label(GTK_BUTTON(toggle), _("Close dialog on Add")); | |
1040 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(toggle), cfg.close_dialog_add); | |
1041 | |
1042 handlerid = g_signal_connect(button_add, "clicked", G_CALLBACK(_filebrowser_add), chooser); | |
1043 handlerid_check = g_signal_connect(toggle, "toggled", G_CALLBACK(_filebrowser_check_hide_add), NULL); | |
1044 handlerid_do = g_signal_connect_after(button_add, "clicked", G_CALLBACK(_filebrowser_do_hide_add), dialog); | |
1045 handlerid_activate = g_signal_connect(chooser, "file-activated", G_CALLBACK(_filebrowser_add), chooser); | |
1046 handlerid_do_activate = g_signal_connect_after(chooser,"file_activated", G_CALLBACK(_filebrowser_do_hide_add), dialog); | |
1047 } | |
1048 | |
1049 gtk_window_present(GTK_WINDOW(dialog)); | |
1050 } | |
1051 | |
1052 GdkFont * | |
1053 util_font_load(const gchar * name) | |
1054 { | |
1055 GdkFont *font; | |
1056 PangoFontDescription *desc; | |
1057 | |
1058 desc = pango_font_description_from_string(name); | |
1059 font = gdk_font_from_description(desc); | |
1060 | |
1061 return font; | |
1062 } | |
1063 | |
1064 #ifdef ENABLE_NLS | |
1065 gchar * | |
1066 bmp_menu_translate(const gchar * path, gpointer func_data) | |
1067 { | |
1068 gchar *translation = gettext(path); | |
1069 | |
1070 if (!translation || *translation != '/') { | |
1071 g_warning("Bad translation for menupath: %s", path); | |
1072 translation = (gchar *) path; | |
1073 } | |
1074 | |
1075 return translation; | |
1076 } | |
1077 #endif | |
1078 | |
1079 void | |
1080 util_set_cursor(GtkWidget * window) | |
1081 { | |
1082 static GdkCursor *cursor = NULL; | |
1083 | |
1084 if (!window) { | |
1085 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
|
1086 gdk_cursor_unref(cursor); |
0 | 1087 cursor = NULL; |
1088 } | |
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
|
1089 |
0 | 1090 return; |
1091 } | |
1092 | |
1093 if (!cursor) | |
1094 cursor = gdk_cursor_new(GDK_LEFT_PTR); | |
1095 | |
1096 gdk_window_set_cursor(window->window, cursor); | |
1097 } | |
1098 | |
1653 | 1099 /* text_get_extents() taken from The GIMP (C) Spencer Kimball, Peter |
1100 * Mattis et al */ | |
1101 gboolean | |
1102 text_get_extents(const gchar * fontname, | |
1103 const gchar * text, | |
1104 gint * width, gint * height, gint * ascent, gint * descent) | |
1105 { | |
1106 PangoFontDescription *font_desc; | |
1107 PangoLayout *layout; | |
1108 PangoRectangle rect; | |
1109 | |
1110 g_return_val_if_fail(fontname != NULL, FALSE); | |
1111 g_return_val_if_fail(text != NULL, FALSE); | |
1112 | |
1113 /* FIXME: resolution */ | |
1114 layout = gtk_widget_create_pango_layout(GTK_WIDGET(mainwin), text); | |
1115 | |
1116 font_desc = pango_font_description_from_string(fontname); | |
1117 pango_layout_set_font_description(layout, font_desc); | |
1118 pango_font_description_free(font_desc); | |
1119 pango_layout_get_pixel_extents(layout, NULL, &rect); | |
1120 | |
1121 if (width) | |
1122 *width = rect.width; | |
1123 if (height) | |
1124 *height = rect.height; | |
1125 | |
1126 if (ascent || descent) { | |
1127 PangoLayoutIter *iter; | |
1128 PangoLayoutLine *line; | |
1129 | |
1130 iter = pango_layout_get_iter(layout); | |
1131 line = pango_layout_iter_get_line(iter); | |
1132 pango_layout_iter_free(iter); | |
1133 | |
1134 pango_layout_line_get_pixel_extents(line, NULL, &rect); | |
1135 | |
1136 if (ascent) | |
1137 *ascent = PANGO_ASCENT(rect); | |
1138 if (descent) | |
1139 *descent = -PANGO_DESCENT(rect); | |
1140 } | |
1141 | |
1142 g_object_unref(layout); | |
1143 | |
1144 return TRUE; | |
1145 } | |
1146 | |
0 | 1147 /* counts number of digits in a gint */ |
1148 guint | |
1149 gint_count_digits(gint n) | |
1150 { | |
1151 guint count = 0; | |
1152 | |
1153 n = ABS(n); | |
1154 do { | |
1155 count++; | |
1156 n /= 10; | |
1157 } while (n > 0); | |
1158 | |
1159 return count; | |
1160 } | |
1161 | |
1162 static gchar * | |
1163 str_twenty_to_space(gchar * str) | |
1164 { | |
1165 gchar *match, *match_end; | |
1166 | |
1167 g_return_val_if_fail(str != NULL, NULL); | |
1168 | |
1169 while ((match = strstr(str, "%20"))) { | |
1170 match_end = match + 3; | |
1171 *match++ = ' '; | |
1172 while (*match_end) | |
1173 *match++ = *match_end++; | |
1174 *match = 0; | |
1175 } | |
1176 | |
1177 return str; | |
1178 } | |
1179 | |
1180 static gchar * | |
1181 str_replace_char(gchar * str, gchar old, gchar new) | |
1182 { | |
1183 gchar *match; | |
1184 | |
1185 g_return_val_if_fail(str != NULL, NULL); | |
1186 | |
1187 match = str; | |
1188 while ((match = strchr(match, old))) | |
1189 *match = new; | |
1190 | |
1191 return str; | |
1192 } | |
1193 | |
1194 gchar * | |
1195 str_append(gchar * str, const gchar * add_str) | |
1196 { | |
1197 return str_replace(str, g_strconcat(str, add_str, NULL)); | |
1198 } | |
1199 | |
1200 gchar * | |
1201 str_replace(gchar * str, gchar * new_str) | |
1202 { | |
1203 g_free(str); | |
1204 return new_str; | |
1205 } | |
1206 | |
1207 void | |
1208 str_replace_in(gchar ** str, gchar * new_str) | |
1209 { | |
1210 *str = str_replace(*str, new_str); | |
1211 } | |
1212 | |
1213 | |
1214 gboolean | |
1215 str_has_prefix_nocase(const gchar * str, const gchar * prefix) | |
1216 { | |
1217 return (strncasecmp(str, prefix, strlen(prefix)) == 0); | |
1218 } | |
1219 | |
1220 gboolean | |
1221 str_has_suffix_nocase(const gchar * str, const gchar * suffix) | |
1222 { | |
1223 return (strcasecmp(str + strlen(str) - strlen(suffix), suffix) == 0); | |
1224 } | |
1225 | |
1226 gboolean | |
1227 str_has_suffixes_nocase(const gchar * str, gchar * const *suffixes) | |
1228 { | |
1229 gchar *const *suffix; | |
1230 | |
1231 g_return_val_if_fail(str != NULL, FALSE); | |
1232 g_return_val_if_fail(suffixes != NULL, FALSE); | |
1233 | |
1234 for (suffix = suffixes; *suffix; suffix++) | |
1235 if (str_has_suffix_nocase(str, *suffix)) | |
1236 return TRUE; | |
1237 | |
1238 return FALSE; | |
1239 } | |
1240 | |
1241 gchar * | |
1242 str_to_utf8_fallback(const gchar * str) | |
1243 { | |
1244 gchar *out_str, *convert_str, *chr; | |
1245 | |
1246 /* NULL in NULL out */ | |
1247 if (!str) | |
1248 return NULL; | |
1249 | |
1250 convert_str = g_strdup(str); | |
1251 for (chr = convert_str; *chr; chr++) { | |
1252 if (*chr & 0x80) | |
1253 *chr = '?'; | |
1254 } | |
1255 | |
1256 out_str = g_strconcat(convert_str, _(" (invalid UTF-8)"), NULL); | |
1257 g_free(convert_str); | |
1258 | |
1259 return out_str; | |
1260 } | |
1261 | |
1262 gchar * | |
1263 filename_to_utf8(const gchar * filename) | |
1264 { | |
1265 gchar *out_str; | |
1266 | |
1267 /* NULL in NULL out */ | |
1268 if (!filename) | |
1269 return NULL; | |
1270 | |
1271 if ((out_str = g_filename_to_utf8(filename, -1, NULL, NULL, NULL))) | |
1272 return out_str; | |
1273 | |
1274 return str_to_utf8_fallback(filename); | |
1275 } | |
1276 | |
1277 gchar * | |
1278 str_to_utf8(const gchar * str) | |
1279 { | |
1280 gchar *out_str; | |
1281 | |
1282 /* NULL in NULL out */ | |
1283 if (!str) | |
1284 return NULL; | |
1285 | |
1324
99454d030722
[svn] try to avoid malconversion of latin1 character when chardet has been enabled.
yaz
parents:
1193
diff
changeset
|
1286 /* 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
|
1287 * 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
|
1288 * 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
|
1289 * 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
|
1290 * 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
|
1291 * 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
|
1292 * 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
|
1293 */ |
99454d030722
[svn] try to avoid malconversion of latin1 character when chardet has been enabled.
yaz
parents:
1193
diff
changeset
|
1294 /* already UTF-8? */ |
99454d030722
[svn] try to avoid malconversion of latin1 character when chardet has been enabled.
yaz
parents:
1193
diff
changeset
|
1295 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
|
1296 return g_strdup(str); |
99454d030722
[svn] try to avoid malconversion of latin1 character when chardet has been enabled.
yaz
parents:
1193
diff
changeset
|
1297 |
1105
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1298 /* chardet encoding detector */ |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1299 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
|
1300 return out_str; |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1301 |
0 | 1302 /* assume encoding associated with locale */ |
1303 if ((out_str = g_locale_to_utf8(str, -1, NULL, NULL, NULL))) | |
1304 return out_str; | |
1305 | |
1306 /* all else fails, we mask off character codes >= 128, | |
1307 replace with '?' */ | |
1308 return str_to_utf8_fallback(str); | |
1309 } | |
1310 | |
1311 | |
1312 const gchar * | |
1313 str_skip_chars(const gchar * str, const gchar * chars) | |
1314 { | |
1315 while (strchr(chars, *str)) | |
1316 str++; | |
1317 return str; | |
1318 } | |
1319 | |
1320 gchar * | |
1321 convert_title_text(gchar * title) | |
1322 { | |
1323 g_return_val_if_fail(title != NULL, NULL); | |
1324 | |
1325 if (cfg.convert_underscore) | |
1326 str_replace_char(title, '_', ' '); | |
1327 | |
1328 if (cfg.convert_twenty) | |
1329 str_twenty_to_space(title); | |
1330 | |
1331 return title; | |
1332 } | |
1333 | |
1334 | |
1335 gboolean | |
1336 dir_foreach(const gchar * path, DirForeachFunc function, | |
1337 gpointer user_data, GError ** error) | |
1338 { | |
1339 GError *error_out = NULL; | |
1340 GDir *dir; | |
1341 const gchar *entry; | |
1342 gchar *entry_fullpath; | |
1343 | |
1344 if (!(dir = g_dir_open(path, 0, &error_out))) { | |
1345 g_propagate_error(error, error_out); | |
1346 return FALSE; | |
1347 } | |
1348 | |
1349 while ((entry = g_dir_read_name(dir))) { | |
1350 entry_fullpath = g_build_filename(path, entry, NULL); | |
1351 | |
1352 if ((*function) (entry_fullpath, entry, user_data)) { | |
1353 g_free(entry_fullpath); | |
1354 break; | |
1355 } | |
1356 | |
1357 g_free(entry_fullpath); | |
1358 } | |
1359 | |
1360 g_dir_close(dir); | |
1361 | |
1362 return TRUE; | |
1363 } | |
1364 | |
1653 | 1365 GtkWidget * |
1366 make_filebrowser(const gchar * title, | |
1367 gboolean save) | |
1368 { | |
1369 GtkWidget *dialog; | |
1370 GtkWidget *button; | |
1371 GtkWidget *button_close; | |
1372 | |
1373 g_return_val_if_fail(title != NULL, NULL); | |
1374 | |
1375 dialog = gtk_file_chooser_dialog_new(title, GTK_WINDOW(mainwin), | |
1376 GTK_FILE_CHOOSER_ACTION_OPEN, NULL, NULL); | |
1377 if (save) | |
1378 gtk_file_chooser_set_action(GTK_FILE_CHOOSER(dialog), | |
1379 GTK_FILE_CHOOSER_ACTION_SAVE); | |
1380 | |
1381 if (!save) | |
1382 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), TRUE); | |
1383 | |
1384 g_signal_connect(dialog, "destroy", | |
1385 G_CALLBACK(gtk_widget_destroyed), &dialog); | |
1386 | |
1387 #ifdef HAVE_GNOME_VFS | |
1388 gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(dialog), FALSE); | |
1389 #endif | |
1390 | |
1391 button_close = gtk_dialog_add_button(GTK_DIALOG(dialog), GTK_STOCK_CANCEL, | |
1392 GTK_RESPONSE_REJECT); | |
1393 gtk_button_set_use_stock(GTK_BUTTON(button_close), TRUE); | |
1394 GTK_WIDGET_SET_FLAGS(button_close, GTK_CAN_DEFAULT); | |
1395 g_signal_connect_swapped(button_close, "clicked", | |
1396 G_CALLBACK(gtk_widget_destroy), dialog); | |
1397 | |
1398 button = gtk_dialog_add_button(GTK_DIALOG(dialog), save ? | |
1399 GTK_STOCK_SAVE : GTK_STOCK_OPEN, | |
1400 GTK_RESPONSE_ACCEPT); | |
1401 gtk_button_set_use_stock(GTK_BUTTON(button), TRUE); | |
1402 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT); | |
1403 gtk_window_set_default(GTK_WINDOW(dialog), button); | |
1404 | |
1405 gtk_widget_show(dialog); | |
1406 | |
1407 return dialog; | |
1408 } | |
1409 | |
1410 | |
0 | 1411 GtkItemFactory * |
1412 create_menu(GtkItemFactoryEntry *entries, | |
1413 guint n_entries, | |
1414 GtkAccelGroup *accel) | |
1415 { | |
1416 GtkItemFactory *menu; | |
1417 | |
1418 menu = gtk_item_factory_new(GTK_TYPE_MENU, "<Main>", accel); | |
1419 gtk_item_factory_set_translate_func(menu, bmp_menu_translate, NULL, | |
1420 NULL); | |
1421 gtk_item_factory_create_items(menu, n_entries, entries, NULL); | |
1422 | |
1423 return menu; | |
1424 } | |
1425 | |
1426 | |
1427 void | |
1428 make_submenu(GtkItemFactory *menu, | |
1429 const gchar *item_path, | |
1430 GtkItemFactory *submenu) | |
1431 { | |
1432 GtkWidget *item, *menu_; | |
1433 | |
1434 item = gtk_item_factory_get_widget(menu, item_path); | |
1435 menu_ = gtk_item_factory_get_widget(submenu, ""); | |
1436 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), menu_); | |
1437 } | |
1438 | |
1439 | |
1105
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1440 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
|
1441 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
|
1442 { |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1443 #ifdef USE_CHARDET |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1444 char *det = NULL, *encoding = NULL; |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1445 #endif |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1446 gchar *ret = NULL; |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1447 gsize *bytes_read, *bytes_write; |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1448 GError **error; |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1449 gsize my_bytes_read, my_bytes_write; |
0 | 1450 |
1105
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1451 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
|
1452 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
|
1453 error = arg_error ? arg_error : NULL; |
0 | 1454 |
1105
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1455 #ifdef USE_CHARDET |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1456 if(cfg.chardet_detector) |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1457 det = cfg.chardet_detector; |
0 | 1458 |
1105
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1459 if(det){ |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1460 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
|
1461 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
|
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("taiwanese", det, sizeof("taiwanese"))) { |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1465 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
|
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("chinese", det, sizeof("chinese"))) { |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1469 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
|
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 } 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
|
1473 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
|
1474 if (!encoding) |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1475 goto fallback; |
1613
9becbe564217
[svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
1459
diff
changeset
|
1476 } else if(!strncasecmp("russian", det, sizeof("russian"))) { |
9becbe564217
[svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
1459
diff
changeset
|
1477 rcd_russian_charset res = rcdGetRussianCharset(str, strlen(str)); |
9becbe564217
[svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
1459
diff
changeset
|
1478 switch(res) { |
9becbe564217
[svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
1459
diff
changeset
|
1479 case RUSSIAN_CHARSET_WIN: |
9becbe564217
[svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
1459
diff
changeset
|
1480 encoding = "CP1251"; |
9becbe564217
[svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
1459
diff
changeset
|
1481 break; |
9becbe564217
[svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
1459
diff
changeset
|
1482 case RUSSIAN_CHARSET_ALT: |
9becbe564217
[svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
1459
diff
changeset
|
1483 encoding = "CP866"; |
9becbe564217
[svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
1459
diff
changeset
|
1484 break; |
9becbe564217
[svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
1459
diff
changeset
|
1485 case RUSSIAN_CHARSET_KOI: |
9becbe564217
[svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
1459
diff
changeset
|
1486 encoding = "KOI8-R"; |
9becbe564217
[svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
1459
diff
changeset
|
1487 break; |
9becbe564217
[svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
1459
diff
changeset
|
1488 case RUSSIAN_CHARSET_UTF8: |
9becbe564217
[svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
1459
diff
changeset
|
1489 encoding = "UTF-8"; |
9becbe564217
[svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
1459
diff
changeset
|
1490 break; |
9becbe564217
[svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
1459
diff
changeset
|
1491 } |
9becbe564217
[svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
1459
diff
changeset
|
1492 if (!encoding) |
9becbe564217
[svn] - chardet patch for Russian language witten by Valentine Sinitsyn.
yaz
parents:
1459
diff
changeset
|
1493 goto fallback; |
1105
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1494 #ifdef HAVE_UDET |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1495 } 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
|
1496 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
|
1497 if (!encoding) |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1498 goto fallback; |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1499 #endif |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1500 } else /* none, invalid */ |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1501 goto fallback; |
0 | 1502 |
1105
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1503 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
|
1504 } |
0 | 1505 |
1105
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1506 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
|
1507 #endif |
1105
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1508 if(!ret && cfg.chardet_fallback){ |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1509 gchar **encs=NULL, **enc=NULL; |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1510 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
|
1511 |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1512 if(encs){ |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1513 enc = encs; |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1514 for(enc=encs; *enc ; enc++){ |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1515 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
|
1516 if(len == *bytes_read){ |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1517 break; |
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 g_strfreev(encs); |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1521 } |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1522 } |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1523 |
1324
99454d030722
[svn] try to avoid malconversion of latin1 character when chardet has been enabled.
yaz
parents:
1193
diff
changeset
|
1524 #ifdef USE_CHARDET |
99454d030722
[svn] try to avoid malconversion of latin1 character when chardet has been enabled.
yaz
parents:
1193
diff
changeset
|
1525 /* 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
|
1526 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
|
1527 latin1 string. |
99454d030722
[svn] try to avoid malconversion of latin1 character when chardet has been enabled.
yaz
parents:
1193
diff
changeset
|
1528 */ |
99454d030722
[svn] try to avoid malconversion of latin1 character when chardet has been enabled.
yaz
parents:
1193
diff
changeset
|
1529 if(!ret){ |
99454d030722
[svn] try to avoid malconversion of latin1 character when chardet has been enabled.
yaz
parents:
1193
diff
changeset
|
1530 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
|
1531 } |
99454d030722
[svn] try to avoid malconversion of latin1 character when chardet has been enabled.
yaz
parents:
1193
diff
changeset
|
1532 #endif |
99454d030722
[svn] try to avoid malconversion of latin1 character when chardet has been enabled.
yaz
parents:
1193
diff
changeset
|
1533 |
1105
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1534 if(ret){ |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1535 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
|
1536 return ret; |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1537 else { |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1538 g_free(ret); |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1539 ret = NULL; |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1540 } |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1541 } |
4be4d74db123
[svn] automatic character encoding detector for id3 metadata. --enable-chardet enables this feature.
yaz
parents:
1067
diff
changeset
|
1542 |
1324
99454d030722
[svn] try to avoid malconversion of latin1 character when chardet has been enabled.
yaz
parents:
1193
diff
changeset
|
1543 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
|
1544 } |