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