|
2313
|
1 /* Audacious
|
|
|
2 * Copyright (C) 2005-2007 Audacious development team.
|
|
|
3 *
|
|
|
4 * BMP - Cross-platform multimedia player
|
|
|
5 * Copyright (C) 2003-2004 BMP development team.
|
|
|
6 *
|
|
|
7 * Based on XMMS:
|
|
|
8 * Copyright (C) 1998-2003 XMMS development team.
|
|
|
9 *
|
|
|
10 * This program is free software; you can redistribute it and/or modify
|
|
|
11 * it under the terms of the GNU General Public License as published by
|
|
|
12 * the Free Software Foundation; under version 2 of the License.
|
|
|
13 *
|
|
|
14 * This program is distributed in the hope that it will be useful,
|
|
|
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
17 * GNU General Public License for more details.
|
|
|
18 *
|
|
|
19 * You should have received a copy of the GNU General Public License
|
|
|
20 * along with this program; if not, write to the Free Software
|
|
|
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|
|
22 * 02110-1301, USA.
|
|
|
23 */
|
|
|
24
|
|
|
25 #define WEIRD_UTF_16_PLAYLIST_ENCODING
|
|
|
26
|
|
|
27 #ifdef HAVE_CONFIG_H
|
|
|
28 # include "config.h"
|
|
|
29 #endif
|
|
|
30
|
|
|
31 #define NEED_GLADE
|
|
|
32 #include "util.h"
|
|
|
33
|
|
|
34 #include <glib.h>
|
|
|
35 #include <glib/gi18n.h>
|
|
|
36 #include <glade/glade.h>
|
|
|
37 #include <gtk/gtk.h>
|
|
|
38 #include <stdio.h>
|
|
|
39 #include <stdlib.h>
|
|
|
40 #include <string.h>
|
|
|
41 #include <ctype.h>
|
|
|
42
|
|
|
43 #include "platform/smartinclude.h"
|
|
|
44 #include <gdk/gdkkeysyms.h>
|
|
|
45 #include <X11/Xlib.h>
|
|
|
46 //#include <sys/ipc.h>
|
|
|
47 #include <unistd.h>
|
|
|
48 #include <errno.h>
|
|
|
49
|
|
|
50 #ifdef HAVE_FTS_H
|
|
|
51 # include <fts.h>
|
|
|
52 #endif
|
|
|
53
|
|
|
54 #include "glade.h"
|
|
|
55 #include "input.h"
|
|
|
56 #include "main.h"
|
|
|
57 #include "playback.h"
|
|
|
58 #include "playlist.h"
|
|
|
59 #include "ui_playlist.h"
|
|
|
60
|
|
|
61 #ifdef USE_CHARDET
|
|
|
62 #include "../libguess/libguess.h"
|
|
|
63 #include "../librcd/librcd.h"
|
|
|
64 #ifdef HAVE_UDET
|
|
|
65 #include <libudet_c.h>
|
|
|
66 #endif
|
|
|
67 #endif
|
|
|
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 static gchar *
|
|
|
103 str_twenty_to_space(gchar * str)
|
|
|
104 {
|
|
|
105 gchar *match, *match_end;
|
|
|
106
|
|
|
107 g_return_val_if_fail(str != NULL, NULL);
|
|
|
108
|
|
|
109 while ((match = strstr(str, "%20"))) {
|
|
|
110 match_end = match + 3;
|
|
|
111 *match++ = ' ';
|
|
|
112 while (*match_end)
|
|
|
113 *match++ = *match_end++;
|
|
|
114 *match = 0;
|
|
|
115 }
|
|
|
116
|
|
|
117 return str;
|
|
|
118 }
|
|
|
119
|
|
|
120 static gchar *
|
|
|
121 str_replace_char(gchar * str, gchar old, gchar new)
|
|
|
122 {
|
|
|
123 gchar *match;
|
|
|
124
|
|
|
125 g_return_val_if_fail(str != NULL, NULL);
|
|
|
126
|
|
|
127 match = str;
|
|
|
128 while ((match = strchr(match, old)))
|
|
|
129 *match = new;
|
|
|
130
|
|
|
131 return str;
|
|
|
132 }
|
|
|
133
|
|
|
134 gchar *
|
|
|
135 str_append(gchar * str, const gchar * add_str)
|
|
|
136 {
|
|
|
137 return str_replace(str, g_strconcat(str, add_str, NULL));
|
|
|
138 }
|
|
|
139
|
|
|
140 gchar *
|
|
|
141 str_replace(gchar * str, gchar * new_str)
|
|
|
142 {
|
|
|
143 g_free(str);
|
|
|
144 return new_str;
|
|
|
145 }
|
|
|
146
|
|
|
147 void
|
|
|
148 str_replace_in(gchar ** str, gchar * new_str)
|
|
|
149 {
|
|
|
150 *str = str_replace(*str, new_str);
|
|
|
151 }
|
|
|
152
|
|
|
153
|
|
|
154 gboolean
|
|
|
155 str_has_prefix_nocase(const gchar * str, const gchar * prefix)
|
|
|
156 {
|
|
|
157 return (strncasecmp(str, prefix, strlen(prefix)) == 0);
|
|
|
158 }
|
|
|
159
|
|
|
160 gboolean
|
|
|
161 str_has_suffix_nocase(const gchar * str, const gchar * suffix)
|
|
|
162 {
|
|
|
163 return (strcasecmp(str + strlen(str) - strlen(suffix), suffix) == 0);
|
|
|
164 }
|
|
|
165
|
|
|
166 gboolean
|
|
|
167 str_has_suffixes_nocase(const gchar * str, gchar * const *suffixes)
|
|
|
168 {
|
|
|
169 gchar *const *suffix;
|
|
|
170
|
|
|
171 g_return_val_if_fail(str != NULL, FALSE);
|
|
|
172 g_return_val_if_fail(suffixes != NULL, FALSE);
|
|
|
173
|
|
|
174 for (suffix = suffixes; *suffix; suffix++)
|
|
|
175 if (str_has_suffix_nocase(str, *suffix))
|
|
|
176 return TRUE;
|
|
|
177
|
|
|
178 return FALSE;
|
|
|
179 }
|
|
|
180
|
|
|
181 gchar *
|
|
|
182 str_to_utf8_fallback(const gchar * str)
|
|
|
183 {
|
|
|
184 gchar *out_str, *convert_str, *chr;
|
|
|
185
|
|
|
186 /* NULL in NULL out */
|
|
|
187 if (!str)
|
|
|
188 return NULL;
|
|
|
189
|
|
|
190 convert_str = g_strdup(str);
|
|
|
191 for (chr = convert_str; *chr; chr++) {
|
|
|
192 if (*chr & 0x80)
|
|
|
193 *chr = '?';
|
|
|
194 }
|
|
|
195
|
|
|
196 out_str = g_strconcat(convert_str, _(" (invalid UTF-8)"), NULL);
|
|
|
197 g_free(convert_str);
|
|
|
198
|
|
|
199 return out_str;
|
|
|
200 }
|
|
|
201
|
|
|
202 gchar *
|
|
|
203 filename_to_utf8(const gchar * filename)
|
|
|
204 {
|
|
|
205 gchar *out_str;
|
|
|
206
|
|
|
207 /* NULL in NULL out */
|
|
|
208 if (!filename)
|
|
|
209 return NULL;
|
|
|
210
|
|
|
211 if ((out_str = g_filename_to_utf8(filename, -1, NULL, NULL, NULL)))
|
|
|
212 return out_str;
|
|
|
213
|
|
|
214 return str_to_utf8_fallback(filename);
|
|
|
215 }
|
|
|
216
|
|
|
217 gchar *
|
|
|
218 str_to_utf8(const gchar * str)
|
|
|
219 {
|
|
|
220 gchar *out_str;
|
|
|
221
|
|
|
222 /* NULL in NULL out */
|
|
|
223 if (!str)
|
|
|
224 return NULL;
|
|
|
225
|
|
|
226 /* Note: Currently, playlist calls this function repeatedly, even
|
|
|
227 * if the string is already converted into utf-8.
|
|
|
228 * chardet_to_utf8() would convert a valid utf-8 string into a
|
|
|
229 * different utf-8 string, if fallback encodings were supplied and
|
|
|
230 * the given string could be treated as a string in one of fallback
|
|
|
231 * encodings. To avoid this, the order of evaluation has been
|
|
|
232 * changed. (It might cause a drawback?)
|
|
|
233 */
|
|
|
234 /* chardet encoding detector */
|
|
|
235 if ((out_str = chardet_to_utf8(str, strlen(str), NULL, NULL, NULL)))
|
|
|
236 return out_str;
|
|
|
237
|
|
|
238 /* already UTF-8? */
|
|
|
239 if (g_utf8_validate(str, -1, NULL))
|
|
|
240 return g_strdup(str);
|
|
|
241
|
|
|
242 /* assume encoding associated with locale */
|
|
|
243 if ((out_str = g_locale_to_utf8(str, -1, NULL, NULL, NULL)))
|
|
|
244 return out_str;
|
|
|
245
|
|
|
246 /* all else fails, we mask off character codes >= 128,
|
|
|
247 replace with '?' */
|
|
|
248 return str_to_utf8_fallback(str);
|
|
|
249 }
|
|
|
250
|
|
|
251
|
|
|
252 const gchar *
|
|
|
253 str_skip_chars(const gchar * str, const gchar * chars)
|
|
|
254 {
|
|
|
255 while (strchr(chars, *str))
|
|
|
256 str++;
|
|
|
257 return str;
|
|
|
258 }
|
|
|
259
|
|
|
260 gchar *
|
|
|
261 convert_title_text(gchar * title)
|
|
|
262 {
|
|
|
263 g_return_val_if_fail(title != NULL, NULL);
|
|
|
264
|
|
|
265 if (cfg.convert_slash)
|
|
|
266 str_replace_char(title, '\\', '/');
|
|
|
267
|
|
|
268 if (cfg.convert_underscore)
|
|
|
269 str_replace_char(title, '_', ' ');
|
|
|
270
|
|
|
271 if (cfg.convert_twenty)
|
|
|
272 str_twenty_to_space(title);
|
|
|
273
|
|
|
274 return title;
|
|
|
275 }
|
|
|
276
|
|
|
277 gchar *chardet_to_utf8(const gchar *str, gssize len,
|
|
|
278 gsize *arg_bytes_read, gsize *arg_bytes_write, GError **arg_error)
|
|
|
279 {
|
|
|
280 #ifdef USE_CHARDET
|
|
|
281 char *det = NULL, *encoding = NULL;
|
|
|
282 #endif
|
|
|
283 gchar *ret = NULL;
|
|
|
284 gsize *bytes_read, *bytes_write;
|
|
|
285 GError **error;
|
|
|
286 gsize my_bytes_read, my_bytes_write;
|
|
|
287
|
|
|
288 bytes_read = arg_bytes_read ? arg_bytes_read : &my_bytes_read;
|
|
|
289 bytes_write = arg_bytes_write ? arg_bytes_write : &my_bytes_write;
|
|
|
290 error = arg_error ? arg_error : NULL;
|
|
|
291
|
|
|
292 #ifdef USE_CHARDET
|
|
|
293 if(cfg.chardet_detector)
|
|
|
294 det = cfg.chardet_detector;
|
|
|
295
|
|
|
296 if(det){
|
|
|
297 if(!strncasecmp("japanese", det, sizeof("japanese"))) {
|
|
|
298 encoding = (char *)guess_jp(str, strlen(str));
|
|
|
299 if (!encoding)
|
|
|
300 goto fallback;
|
|
|
301 } else if(!strncasecmp("taiwanese", det, sizeof("taiwanese"))) {
|
|
|
302 encoding = (char *)guess_tw(str, strlen(str));
|
|
|
303 if (!encoding)
|
|
|
304 goto fallback;
|
|
|
305 } else if(!strncasecmp("chinese", det, sizeof("chinese"))) {
|
|
|
306 encoding = (char *)guess_cn(str, strlen(str));
|
|
|
307 if (!encoding)
|
|
|
308 goto fallback;
|
|
|
309 } else if(!strncasecmp("korean", det, sizeof("korean"))) {
|
|
|
310 encoding = (char *)guess_kr(str, strlen(str));
|
|
|
311 if (!encoding)
|
|
|
312 goto fallback;
|
|
|
313 } else if(!strncasecmp("russian", det, sizeof("russian"))) {
|
|
|
314 rcd_russian_charset res = rcdGetRussianCharset(str, strlen(str));
|
|
|
315 switch(res) {
|
|
|
316 case RUSSIAN_CHARSET_WIN:
|
|
|
317 encoding = "CP1251";
|
|
|
318 break;
|
|
|
319 case RUSSIAN_CHARSET_ALT:
|
|
|
320 encoding = "CP866";
|
|
|
321 break;
|
|
|
322 case RUSSIAN_CHARSET_KOI:
|
|
|
323 encoding = "KOI8-R";
|
|
|
324 break;
|
|
|
325 case RUSSIAN_CHARSET_UTF8:
|
|
|
326 encoding = "UTF-8";
|
|
|
327 break;
|
|
|
328 }
|
|
|
329 if (!encoding)
|
|
|
330 goto fallback;
|
|
|
331 #ifdef HAVE_UDET
|
|
|
332 } else if (!strncasecmp("universal", det, sizeof("universal"))) {
|
|
|
333 encoding = (char *)detectCharset((char *)str, strlen(str));
|
|
|
334 if (!encoding)
|
|
|
335 goto fallback;
|
|
|
336 #endif
|
|
|
337 } else /* none, invalid */
|
|
|
338 goto fallback;
|
|
|
339
|
|
|
340 ret = g_convert(str, len, "UTF-8", encoding, bytes_read, bytes_write, error);
|
|
|
341 }
|
|
|
342
|
|
|
343 fallback:
|
|
|
344 #endif
|
|
|
345 if(!ret && cfg.chardet_fallback){
|
|
|
346 gchar **encs=NULL, **enc=NULL;
|
|
|
347 encs = g_strsplit_set(cfg.chardet_fallback, " ,:;|/", 0);
|
|
|
348
|
|
|
349 if(encs){
|
|
|
350 enc = encs;
|
|
|
351 for(enc=encs; *enc ; enc++){
|
|
|
352 ret = g_convert(str, len, "UTF-8", *enc, bytes_read, bytes_write, error);
|
|
|
353 if(len == *bytes_read){
|
|
|
354 break;
|
|
|
355 }
|
|
|
356 }
|
|
|
357 g_strfreev(encs);
|
|
|
358 }
|
|
|
359 }
|
|
|
360
|
|
|
361 #ifdef USE_CHARDET
|
|
|
362 /* many tag libraries return 2byte latin1 utf8 character as
|
|
|
363 converted 8bit iso-8859-1 character, if they are asked to return
|
|
|
364 latin1 string.
|
|
|
365 */
|
|
|
366 if(!ret){
|
|
|
367 ret = g_convert(str, len, "UTF-8", "ISO-8859-1", bytes_read, bytes_write, error);
|
|
|
368 }
|
|
|
369 #endif
|
|
|
370
|
|
|
371 if(ret){
|
|
|
372 if(g_utf8_validate(ret, -1, NULL))
|
|
|
373 return ret;
|
|
|
374 else {
|
|
|
375 g_free(ret);
|
|
|
376 ret = NULL;
|
|
|
377 }
|
|
|
378 }
|
|
|
379
|
|
|
380 return NULL; /* if I have no idea, return NULL. */
|
|
|
381 }
|