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 static GQuark quark_popup_data;
|
|
70
|
|
71
|
|
72 /*
|
|
73 * escape_shell_chars()
|
|
74 *
|
|
75 * Escapes characters that are special to the shell inside double quotes.
|
|
76 */
|
|
77
|
|
78 gchar *
|
|
79 escape_shell_chars(const gchar * string)
|
|
80 {
|
|
81 const gchar *special = "$`\"\\"; /* Characters to escape */
|
|
82 const gchar *in = string;
|
|
83 gchar *out, *escaped;
|
|
84 gint num = 0;
|
|
85
|
|
86 while (*in != '\0')
|
|
87 if (strchr(special, *in++))
|
|
88 num++;
|
|
89
|
|
90 escaped = g_malloc(strlen(string) + num + 1);
|
|
91
|
|
92 in = string;
|
|
93 out = escaped;
|
|
94
|
|
95 while (*in != '\0') {
|
|
96 if (strchr(special, *in))
|
|
97 *out++ = '\\';
|
|
98 *out++ = *in++;
|
|
99 }
|
|
100 *out = '\0';
|
|
101
|
|
102 return escaped;
|
|
103 }
|
|
104
|
|
105 static gchar *
|
|
106 str_twenty_to_space(gchar * str)
|
|
107 {
|
|
108 gchar *match, *match_end;
|
|
109
|
|
110 g_return_val_if_fail(str != NULL, NULL);
|
|
111
|
|
112 while ((match = strstr(str, "%20"))) {
|
|
113 match_end = match + 3;
|
|
114 *match++ = ' ';
|
|
115 while (*match_end)
|
|
116 *match++ = *match_end++;
|
|
117 *match = 0;
|
|
118 }
|
|
119
|
|
120 return str;
|
|
121 }
|
|
122
|
|
123 static gchar *
|
|
124 str_replace_char(gchar * str, gchar old, gchar new)
|
|
125 {
|
|
126 gchar *match;
|
|
127
|
|
128 g_return_val_if_fail(str != NULL, NULL);
|
|
129
|
|
130 match = str;
|
|
131 while ((match = strchr(match, old)))
|
|
132 *match = new;
|
|
133
|
|
134 return str;
|
|
135 }
|
|
136
|
|
137 gchar *
|
|
138 str_append(gchar * str, const gchar * add_str)
|
|
139 {
|
|
140 return str_replace(str, g_strconcat(str, add_str, NULL));
|
|
141 }
|
|
142
|
|
143 gchar *
|
|
144 str_replace(gchar * str, gchar * new_str)
|
|
145 {
|
|
146 g_free(str);
|
|
147 return new_str;
|
|
148 }
|
|
149
|
|
150 void
|
|
151 str_replace_in(gchar ** str, gchar * new_str)
|
|
152 {
|
|
153 *str = str_replace(*str, new_str);
|
|
154 }
|
|
155
|
|
156
|
|
157 gboolean
|
|
158 str_has_prefix_nocase(const gchar * str, const gchar * prefix)
|
|
159 {
|
|
160 return (strncasecmp(str, prefix, strlen(prefix)) == 0);
|
|
161 }
|
|
162
|
|
163 gboolean
|
|
164 str_has_suffix_nocase(const gchar * str, const gchar * suffix)
|
|
165 {
|
|
166 return (strcasecmp(str + strlen(str) - strlen(suffix), suffix) == 0);
|
|
167 }
|
|
168
|
|
169 gboolean
|
|
170 str_has_suffixes_nocase(const gchar * str, gchar * const *suffixes)
|
|
171 {
|
|
172 gchar *const *suffix;
|
|
173
|
|
174 g_return_val_if_fail(str != NULL, FALSE);
|
|
175 g_return_val_if_fail(suffixes != NULL, FALSE);
|
|
176
|
|
177 for (suffix = suffixes; *suffix; suffix++)
|
|
178 if (str_has_suffix_nocase(str, *suffix))
|
|
179 return TRUE;
|
|
180
|
|
181 return FALSE;
|
|
182 }
|
|
183
|
|
184 gchar *
|
|
185 str_to_utf8_fallback(const gchar * str)
|
|
186 {
|
|
187 gchar *out_str, *convert_str, *chr;
|
|
188
|
|
189 /* NULL in NULL out */
|
|
190 if (!str)
|
|
191 return NULL;
|
|
192
|
|
193 convert_str = g_strdup(str);
|
|
194 for (chr = convert_str; *chr; chr++) {
|
|
195 if (*chr & 0x80)
|
|
196 *chr = '?';
|
|
197 }
|
|
198
|
|
199 out_str = g_strconcat(convert_str, _(" (invalid UTF-8)"), NULL);
|
|
200 g_free(convert_str);
|
|
201
|
|
202 return out_str;
|
|
203 }
|
|
204
|
|
205 gchar *
|
|
206 filename_to_utf8(const gchar * filename)
|
|
207 {
|
|
208 gchar *out_str;
|
|
209
|
|
210 /* NULL in NULL out */
|
|
211 if (!filename)
|
|
212 return NULL;
|
|
213
|
|
214 if ((out_str = g_filename_to_utf8(filename, -1, NULL, NULL, NULL)))
|
|
215 return out_str;
|
|
216
|
|
217 return str_to_utf8_fallback(filename);
|
|
218 }
|
|
219
|
|
220 gchar *
|
|
221 str_to_utf8(const gchar * str)
|
|
222 {
|
|
223 gchar *out_str;
|
|
224
|
|
225 /* NULL in NULL out */
|
|
226 if (!str)
|
|
227 return NULL;
|
|
228
|
|
229 /* Note: Currently, playlist calls this function repeatedly, even
|
|
230 * if the string is already converted into utf-8.
|
|
231 * chardet_to_utf8() would convert a valid utf-8 string into a
|
|
232 * different utf-8 string, if fallback encodings were supplied and
|
|
233 * the given string could be treated as a string in one of fallback
|
|
234 * encodings. To avoid this, the order of evaluation has been
|
|
235 * changed. (It might cause a drawback?)
|
|
236 */
|
|
237 /* chardet encoding detector */
|
|
238 if ((out_str = chardet_to_utf8(str, strlen(str), NULL, NULL, NULL)))
|
|
239 return out_str;
|
|
240
|
|
241 /* already UTF-8? */
|
|
242 if (g_utf8_validate(str, -1, NULL))
|
|
243 return g_strdup(str);
|
|
244
|
|
245 /* assume encoding associated with locale */
|
|
246 if ((out_str = g_locale_to_utf8(str, -1, NULL, NULL, NULL)))
|
|
247 return out_str;
|
|
248
|
|
249 /* all else fails, we mask off character codes >= 128,
|
|
250 replace with '?' */
|
|
251 return str_to_utf8_fallback(str);
|
|
252 }
|
|
253
|
|
254
|
|
255 const gchar *
|
|
256 str_skip_chars(const gchar * str, const gchar * chars)
|
|
257 {
|
|
258 while (strchr(chars, *str))
|
|
259 str++;
|
|
260 return str;
|
|
261 }
|
|
262
|
|
263 gchar *
|
|
264 convert_title_text(gchar * title)
|
|
265 {
|
|
266 g_return_val_if_fail(title != NULL, NULL);
|
|
267
|
|
268 if (cfg.convert_slash)
|
|
269 str_replace_char(title, '\\', '/');
|
|
270
|
|
271 if (cfg.convert_underscore)
|
|
272 str_replace_char(title, '_', ' ');
|
|
273
|
|
274 if (cfg.convert_twenty)
|
|
275 str_twenty_to_space(title);
|
|
276
|
|
277 return title;
|
|
278 }
|
|
279
|
|
280 gchar *chardet_to_utf8(const gchar *str, gssize len,
|
|
281 gsize *arg_bytes_read, gsize *arg_bytes_write, GError **arg_error)
|
|
282 {
|
|
283 #ifdef USE_CHARDET
|
|
284 char *det = NULL, *encoding = NULL;
|
|
285 #endif
|
|
286 gchar *ret = NULL;
|
|
287 gsize *bytes_read, *bytes_write;
|
|
288 GError **error;
|
|
289 gsize my_bytes_read, my_bytes_write;
|
|
290
|
|
291 bytes_read = arg_bytes_read ? arg_bytes_read : &my_bytes_read;
|
|
292 bytes_write = arg_bytes_write ? arg_bytes_write : &my_bytes_write;
|
|
293 error = arg_error ? arg_error : NULL;
|
|
294
|
|
295 #ifdef USE_CHARDET
|
|
296 if(cfg.chardet_detector)
|
|
297 det = cfg.chardet_detector;
|
|
298
|
|
299 if(det){
|
|
300 if(!strncasecmp("japanese", det, sizeof("japanese"))) {
|
|
301 encoding = (char *)guess_jp(str, strlen(str));
|
|
302 if (!encoding)
|
|
303 goto fallback;
|
|
304 } else if(!strncasecmp("taiwanese", det, sizeof("taiwanese"))) {
|
|
305 encoding = (char *)guess_tw(str, strlen(str));
|
|
306 if (!encoding)
|
|
307 goto fallback;
|
|
308 } else if(!strncasecmp("chinese", det, sizeof("chinese"))) {
|
|
309 encoding = (char *)guess_cn(str, strlen(str));
|
|
310 if (!encoding)
|
|
311 goto fallback;
|
|
312 } else if(!strncasecmp("korean", det, sizeof("korean"))) {
|
|
313 encoding = (char *)guess_kr(str, strlen(str));
|
|
314 if (!encoding)
|
|
315 goto fallback;
|
|
316 } else if(!strncasecmp("russian", det, sizeof("russian"))) {
|
|
317 rcd_russian_charset res = rcdGetRussianCharset(str, strlen(str));
|
|
318 switch(res) {
|
|
319 case RUSSIAN_CHARSET_WIN:
|
|
320 encoding = "CP1251";
|
|
321 break;
|
|
322 case RUSSIAN_CHARSET_ALT:
|
|
323 encoding = "CP866";
|
|
324 break;
|
|
325 case RUSSIAN_CHARSET_KOI:
|
|
326 encoding = "KOI8-R";
|
|
327 break;
|
|
328 case RUSSIAN_CHARSET_UTF8:
|
|
329 encoding = "UTF-8";
|
|
330 break;
|
|
331 }
|
|
332 if (!encoding)
|
|
333 goto fallback;
|
|
334 #ifdef HAVE_UDET
|
|
335 } else if (!strncasecmp("universal", det, sizeof("universal"))) {
|
|
336 encoding = (char *)detectCharset((char *)str, strlen(str));
|
|
337 if (!encoding)
|
|
338 goto fallback;
|
|
339 #endif
|
|
340 } else /* none, invalid */
|
|
341 goto fallback;
|
|
342
|
|
343 ret = g_convert(str, len, "UTF-8", encoding, bytes_read, bytes_write, error);
|
|
344 }
|
|
345
|
|
346 fallback:
|
|
347 #endif
|
|
348 if(!ret && cfg.chardet_fallback){
|
|
349 gchar **encs=NULL, **enc=NULL;
|
|
350 encs = g_strsplit_set(cfg.chardet_fallback, " ,:;|/", 0);
|
|
351
|
|
352 if(encs){
|
|
353 enc = encs;
|
|
354 for(enc=encs; *enc ; enc++){
|
|
355 ret = g_convert(str, len, "UTF-8", *enc, bytes_read, bytes_write, error);
|
|
356 if(len == *bytes_read){
|
|
357 break;
|
|
358 }
|
|
359 }
|
|
360 g_strfreev(encs);
|
|
361 }
|
|
362 }
|
|
363
|
|
364 #ifdef USE_CHARDET
|
|
365 /* many tag libraries return 2byte latin1 utf8 character as
|
|
366 converted 8bit iso-8859-1 character, if they are asked to return
|
|
367 latin1 string.
|
|
368 */
|
|
369 if(!ret){
|
|
370 ret = g_convert(str, len, "UTF-8", "ISO-8859-1", bytes_read, bytes_write, error);
|
|
371 }
|
|
372 #endif
|
|
373
|
|
374 if(ret){
|
|
375 if(g_utf8_validate(ret, -1, NULL))
|
|
376 return ret;
|
|
377 else {
|
|
378 g_free(ret);
|
|
379 ret = NULL;
|
|
380 }
|
|
381 }
|
|
382
|
|
383 return NULL; /* if I have no idea, return NULL. */
|
|
384 }
|