Mercurial > audlegacy
annotate audacious/util.c @ 897:eeed0ddebdfc trunk
[svn] - icons woohoo
author | nhjm449 |
---|---|
date | Wed, 29 Mar 2006 22:17:26 -0800 |
parents | 12feaeb5ad5b |
children | dc9373f0186b |
rev | line source |
---|---|
0 | 1 /* BMP - Cross-platform multimedia player |
2 * Copyright (C) 2003-2004 BMP development team. | |
3 * | |
4 * Based on XMMS: | |
5 * Copyright (C) 1998-2003 XMMS development team. | |
6 * | |
7 * This program is free software; you can redistribute it and/or modify | |
8 * it under the terms of the GNU General Public License as published by | |
9 * the Free Software Foundation; either version 2 of the License, or | |
10 * (at your option) any later version. | |
11 * | |
12 * This program is distributed in the hope that it will be useful, | |
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 * GNU General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU General Public License | |
18 * along with this program; if not, write to the Free Software | |
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
20 */ | |
21 | |
245
1d8012046612
[svn] Convert UTF-16 (windows-1252) into something useful. Original patch by incomp@#audacious,
nenolod
parents:
86
diff
changeset
|
22 #define WEIRD_UTF_16_PLAYLIST_ENCODING |
1d8012046612
[svn] Convert UTF-16 (windows-1252) into something useful. Original patch by incomp@#audacious,
nenolod
parents:
86
diff
changeset
|
23 |
0 | 24 #ifdef HAVE_CONFIG_H |
25 # include "config.h" | |
26 #endif | |
27 | |
28 #define NEED_GLADE | |
29 #include "util.h" | |
30 | |
31 #include <glib.h> | |
32 #include <glib/gi18n.h> | |
33 #include <glade/glade.h> | |
34 #include <gtk/gtk.h> | |
35 #include <gdk/gdk.h> | |
36 #include <stdio.h> | |
37 #include <stdlib.h> | |
38 #include <string.h> | |
39 #include <ctype.h> | |
40 | |
41 #include <gdk/gdkx.h> | |
42 #include <gdk/gdkkeysyms.h> | |
43 #include <X11/Xlib.h> | |
287
2d919c756941
[svn] - attach -Iaudacious to libaudcore build crap.
nenolod
parents:
284
diff
changeset
|
44 //#include <sys/ipc.h> |
0 | 45 #include <unistd.h> |
46 #include <errno.h> | |
47 | |
48 #ifdef HAVE_FTS_H | |
49 # include <fts.h> | |
50 #endif | |
51 | |
52 #include "glade.h" | |
53 #include "input.h" | |
54 #include "main.h" | |
538
e4e897d20791
[svn] remove libaudcore, we never did anything with it
nenolod
parents:
383
diff
changeset
|
55 #include "playback.h" |
0 | 56 #include "playlist.h" |
383 | 57 #include "ui_playlist.h" |
0 | 58 |
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; | |
625
0a73d1faeb4e
[svn] GCC 4.1 warning fixes by Diego 'Flameeyes' Petteno from Gentoo.
chainsaw
parents:
538
diff
changeset
|
397 gint found_section = 0, len = 0; |
0a73d1faeb4e
[svn] GCC 4.1 warning fixes by Diego 'Flameeyes' Petteno from Gentoo.
chainsaw
parents:
538
diff
changeset
|
398 gsize filesize, off = 0; |
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 }; |
625
0a73d1faeb4e
[svn] GCC 4.1 warning fixes by Diego 'Flameeyes' Petteno from Gentoo.
chainsaw
parents:
538
diff
changeset
|
401 guint 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); | |
893
6afdd0d7e1e1
[svn] Make refreshing optional, default to disabled if Gnome VFS available.
nemo
parents:
879
diff
changeset
|
975 if (cfg.refresh_file_list) { |
6afdd0d7e1e1
[svn] Make refreshing optional, default to disabled if Gnome VFS available.
nemo
parents:
879
diff
changeset
|
976 // *sigh* force a refresh |
6afdd0d7e1e1
[svn] Make refreshing optional, default to disabled if Gnome VFS available.
nemo
parents:
879
diff
changeset
|
977 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(chooser),gtk_file_chooser_get_current_folder(GTK_FILE_CHOOSER(chooser))); |
6afdd0d7e1e1
[svn] Make refreshing optional, default to disabled if Gnome VFS available.
nemo
parents:
879
diff
changeset
|
978 } |
0 | 979 } |
980 | |
981 if (play_button) { | |
982 cfg.close_dialog_open = TRUE; | |
983 | |
984 gtk_window_set_title(GTK_WINDOW(dialog), _("Open Files")); | |
985 | |
986 gtk_button_set_label(GTK_BUTTON(button_add), GTK_STOCK_OPEN); | |
987 | |
988 gtk_button_set_label(GTK_BUTTON(toggle), _("Close dialog on Open")); | |
989 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(toggle), cfg.close_dialog_open); | |
990 | |
991 handlerid = g_signal_connect(button_add, "clicked", G_CALLBACK(_filebrowser_play), chooser); | |
992 handlerid_check = g_signal_connect(toggle, "toggled", G_CALLBACK(_filebrowser_check_hide_open), NULL); | |
993 handlerid_do = g_signal_connect_after(button_add, "clicked", G_CALLBACK(_filebrowser_do_hide_open), dialog); | |
994 handlerid_activate = g_signal_connect(chooser, "file-activated", G_CALLBACK(_filebrowser_play), chooser); | |
995 handlerid_do_activate = g_signal_connect_after(chooser,"file_activated", G_CALLBACK(_filebrowser_do_hide_open), dialog); | |
996 } | |
997 else { | |
998 cfg.close_dialog_add = TRUE; | |
999 | |
1000 gtk_window_set_title(GTK_WINDOW(dialog), _("Add Files")); | |
1001 | |
1002 gtk_button_set_label(GTK_BUTTON(button_add), GTK_STOCK_ADD); | |
1003 | |
1004 gtk_button_set_label(GTK_BUTTON(toggle), _("Close dialog on Add")); | |
1005 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(toggle), cfg.close_dialog_add); | |
1006 | |
1007 handlerid = g_signal_connect(button_add, "clicked", G_CALLBACK(_filebrowser_add), chooser); | |
1008 handlerid_check = g_signal_connect(toggle, "toggled", G_CALLBACK(_filebrowser_check_hide_add), NULL); | |
1009 handlerid_do = g_signal_connect_after(button_add, "clicked", G_CALLBACK(_filebrowser_do_hide_add), dialog); | |
1010 handlerid_activate = g_signal_connect(chooser, "file-activated", G_CALLBACK(_filebrowser_add), chooser); | |
1011 handlerid_do_activate = g_signal_connect_after(chooser,"file_activated", G_CALLBACK(_filebrowser_do_hide_add), dialog); | |
1012 } | |
1013 | |
1014 gtk_window_present(GTK_WINDOW(dialog)); | |
1015 } | |
1016 | |
1017 GdkFont * | |
1018 util_font_load(const gchar * name) | |
1019 { | |
1020 GdkFont *font; | |
1021 PangoFontDescription *desc; | |
1022 | |
1023 desc = pango_font_description_from_string(name); | |
1024 font = gdk_font_from_description(desc); | |
1025 | |
1026 return font; | |
1027 } | |
1028 | |
1029 #ifdef ENABLE_NLS | |
1030 gchar * | |
1031 bmp_menu_translate(const gchar * path, gpointer func_data) | |
1032 { | |
1033 gchar *translation = gettext(path); | |
1034 | |
1035 if (!translation || *translation != '/') { | |
1036 g_warning("Bad translation for menupath: %s", path); | |
1037 translation = (gchar *) path; | |
1038 } | |
1039 | |
1040 return translation; | |
1041 } | |
1042 #endif | |
1043 | |
1044 void | |
1045 util_set_cursor(GtkWidget * window) | |
1046 { | |
1047 static GdkCursor *cursor = NULL; | |
1048 | |
1049 if (!window) { | |
1050 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
|
1051 gdk_cursor_unref(cursor); |
0 | 1052 cursor = NULL; |
1053 } | |
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
|
1054 |
0 | 1055 return; |
1056 } | |
1057 | |
1058 if (!cursor) | |
1059 cursor = gdk_cursor_new(GDK_LEFT_PTR); | |
1060 | |
1061 gdk_window_set_cursor(window->window, cursor); | |
1062 } | |
1063 | |
1064 /* text_get_extents() taken from The GIMP (C) Spencer Kimball, Peter | |
1065 * Mattis et al */ | |
1066 gboolean | |
1067 text_get_extents(const gchar * fontname, | |
1068 const gchar * text, | |
1069 gint * width, gint * height, gint * ascent, gint * descent) | |
1070 { | |
1071 PangoFontDescription *font_desc; | |
1072 PangoLayout *layout; | |
1073 PangoRectangle rect; | |
1074 | |
1075 g_return_val_if_fail(fontname != NULL, FALSE); | |
1076 g_return_val_if_fail(text != NULL, FALSE); | |
1077 | |
1078 /* FIXME: resolution */ | |
1079 layout = gtk_widget_create_pango_layout(GTK_WIDGET(mainwin), text); | |
1080 | |
1081 font_desc = pango_font_description_from_string(fontname); | |
1082 pango_layout_set_font_description(layout, font_desc); | |
1083 pango_font_description_free(font_desc); | |
1084 pango_layout_get_pixel_extents(layout, NULL, &rect); | |
1085 | |
1086 if (width) | |
1087 *width = rect.width; | |
1088 if (height) | |
1089 *height = rect.height; | |
1090 | |
1091 if (ascent || descent) { | |
1092 PangoLayoutIter *iter; | |
1093 PangoLayoutLine *line; | |
1094 | |
1095 iter = pango_layout_get_iter(layout); | |
1096 line = pango_layout_iter_get_line(iter); | |
1097 pango_layout_iter_free(iter); | |
1098 | |
1099 pango_layout_line_get_pixel_extents(line, NULL, &rect); | |
1100 | |
1101 if (ascent) | |
1102 *ascent = PANGO_ASCENT(rect); | |
1103 if (descent) | |
1104 *descent = -PANGO_DESCENT(rect); | |
1105 } | |
1106 | |
1107 g_object_unref(layout); | |
1108 | |
1109 return TRUE; | |
1110 } | |
1111 | |
1112 /* counts number of digits in a gint */ | |
1113 guint | |
1114 gint_count_digits(gint n) | |
1115 { | |
1116 guint count = 0; | |
1117 | |
1118 n = ABS(n); | |
1119 do { | |
1120 count++; | |
1121 n /= 10; | |
1122 } while (n > 0); | |
1123 | |
1124 return count; | |
1125 } | |
1126 | |
1127 static gchar * | |
1128 str_twenty_to_space(gchar * str) | |
1129 { | |
1130 gchar *match, *match_end; | |
1131 | |
1132 g_return_val_if_fail(str != NULL, NULL); | |
1133 | |
1134 while ((match = strstr(str, "%20"))) { | |
1135 match_end = match + 3; | |
1136 *match++ = ' '; | |
1137 while (*match_end) | |
1138 *match++ = *match_end++; | |
1139 *match = 0; | |
1140 } | |
1141 | |
1142 return str; | |
1143 } | |
1144 | |
1145 static gchar * | |
1146 str_replace_char(gchar * str, gchar old, gchar new) | |
1147 { | |
1148 gchar *match; | |
1149 | |
1150 g_return_val_if_fail(str != NULL, NULL); | |
1151 | |
1152 match = str; | |
1153 while ((match = strchr(match, old))) | |
1154 *match = new; | |
1155 | |
1156 return str; | |
1157 } | |
1158 | |
1159 gchar * | |
1160 str_append(gchar * str, const gchar * add_str) | |
1161 { | |
1162 return str_replace(str, g_strconcat(str, add_str, NULL)); | |
1163 } | |
1164 | |
1165 gchar * | |
1166 str_replace(gchar * str, gchar * new_str) | |
1167 { | |
1168 g_free(str); | |
1169 return new_str; | |
1170 } | |
1171 | |
1172 void | |
1173 str_replace_in(gchar ** str, gchar * new_str) | |
1174 { | |
1175 *str = str_replace(*str, new_str); | |
1176 } | |
1177 | |
1178 | |
1179 gboolean | |
1180 str_has_prefix_nocase(const gchar * str, const gchar * prefix) | |
1181 { | |
1182 return (strncasecmp(str, prefix, strlen(prefix)) == 0); | |
1183 } | |
1184 | |
1185 gboolean | |
1186 str_has_suffix_nocase(const gchar * str, const gchar * suffix) | |
1187 { | |
1188 return (strcasecmp(str + strlen(str) - strlen(suffix), suffix) == 0); | |
1189 } | |
1190 | |
1191 gboolean | |
1192 str_has_suffixes_nocase(const gchar * str, gchar * const *suffixes) | |
1193 { | |
1194 gchar *const *suffix; | |
1195 | |
1196 g_return_val_if_fail(str != NULL, FALSE); | |
1197 g_return_val_if_fail(suffixes != NULL, FALSE); | |
1198 | |
1199 for (suffix = suffixes; *suffix; suffix++) | |
1200 if (str_has_suffix_nocase(str, *suffix)) | |
1201 return TRUE; | |
1202 | |
1203 return FALSE; | |
1204 } | |
1205 | |
1206 gchar * | |
1207 str_to_utf8_fallback(const gchar * str) | |
1208 { | |
1209 gchar *out_str, *convert_str, *chr; | |
1210 | |
1211 /* NULL in NULL out */ | |
1212 if (!str) | |
1213 return NULL; | |
1214 | |
1215 convert_str = g_strdup(str); | |
1216 for (chr = convert_str; *chr; chr++) { | |
1217 if (*chr & 0x80) | |
1218 *chr = '?'; | |
1219 } | |
1220 | |
1221 out_str = g_strconcat(convert_str, _(" (invalid UTF-8)"), NULL); | |
1222 g_free(convert_str); | |
1223 | |
1224 return out_str; | |
1225 } | |
1226 | |
1227 gchar * | |
1228 filename_to_utf8(const gchar * filename) | |
1229 { | |
1230 gchar *out_str; | |
1231 | |
1232 /* NULL in NULL out */ | |
1233 if (!filename) | |
1234 return NULL; | |
1235 | |
1236 if ((out_str = g_filename_to_utf8(filename, -1, NULL, NULL, NULL))) | |
1237 return out_str; | |
1238 | |
1239 return str_to_utf8_fallback(filename); | |
1240 } | |
1241 | |
1242 gchar * | |
1243 str_to_utf8(const gchar * str) | |
1244 { | |
1245 gchar *out_str; | |
1246 | |
1247 /* NULL in NULL out */ | |
1248 if (!str) | |
1249 return NULL; | |
1250 | |
1251 /* already UTF-8? */ | |
1252 if (g_utf8_validate(str, -1, NULL)) | |
1253 return g_strdup(str); | |
1254 | |
1255 /* assume encoding associated with locale */ | |
1256 if ((out_str = g_locale_to_utf8(str, -1, NULL, NULL, NULL))) | |
1257 return out_str; | |
1258 | |
1259 /* all else fails, we mask off character codes >= 128, | |
1260 replace with '?' */ | |
1261 return str_to_utf8_fallback(str); | |
1262 } | |
1263 | |
1264 | |
1265 const gchar * | |
1266 str_skip_chars(const gchar * str, const gchar * chars) | |
1267 { | |
1268 while (strchr(chars, *str)) | |
1269 str++; | |
1270 return str; | |
1271 } | |
1272 | |
1273 gchar * | |
1274 convert_title_text(gchar * title) | |
1275 { | |
1276 g_return_val_if_fail(title != NULL, NULL); | |
1277 | |
1278 if (cfg.convert_underscore) | |
1279 str_replace_char(title, '_', ' '); | |
1280 | |
1281 if (cfg.convert_twenty) | |
1282 str_twenty_to_space(title); | |
1283 | |
1284 return title; | |
1285 } | |
1286 | |
1287 | |
1288 gboolean | |
1289 dir_foreach(const gchar * path, DirForeachFunc function, | |
1290 gpointer user_data, GError ** error) | |
1291 { | |
1292 GError *error_out = NULL; | |
1293 GDir *dir; | |
1294 const gchar *entry; | |
1295 gchar *entry_fullpath; | |
1296 | |
1297 if (!(dir = g_dir_open(path, 0, &error_out))) { | |
1298 g_propagate_error(error, error_out); | |
1299 return FALSE; | |
1300 } | |
1301 | |
1302 while ((entry = g_dir_read_name(dir))) { | |
1303 entry_fullpath = g_build_filename(path, entry, NULL); | |
1304 | |
1305 if ((*function) (entry_fullpath, entry, user_data)) { | |
1306 g_free(entry_fullpath); | |
1307 break; | |
1308 } | |
1309 | |
1310 g_free(entry_fullpath); | |
1311 } | |
1312 | |
1313 g_dir_close(dir); | |
1314 | |
1315 return TRUE; | |
1316 } | |
1317 | |
1318 GtkWidget * | |
1319 make_filebrowser(const gchar * title, | |
1320 gboolean save) | |
1321 { | |
1322 GtkWidget *dialog; | |
1323 GtkWidget *button; | |
1324 GtkWidget *button_close; | |
1325 | |
1326 g_return_val_if_fail(title != NULL, NULL); | |
1327 | |
1328 dialog = gtk_file_chooser_dialog_new(title, GTK_WINDOW(mainwin), | |
376 | 1329 GTK_FILE_CHOOSER_ACTION_OPEN, NULL, NULL); |
0 | 1330 if (save) |
1331 gtk_file_chooser_set_action(GTK_FILE_CHOOSER(dialog), | |
1332 GTK_FILE_CHOOSER_ACTION_SAVE); | |
1333 | |
1334 if (!save) | |
1335 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), TRUE); | |
1336 | |
1337 g_signal_connect(dialog, "destroy", | |
1338 G_CALLBACK(gtk_widget_destroyed), &dialog); | |
1339 | |
1340 #ifdef HAVE_GNOME_VFS | |
1341 gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(dialog), FALSE); | |
1342 #endif | |
1343 | |
1344 button_close = gtk_dialog_add_button(GTK_DIALOG(dialog), GTK_STOCK_CANCEL, | |
1345 GTK_RESPONSE_REJECT); | |
1346 gtk_button_set_use_stock(GTK_BUTTON(button_close), TRUE); | |
1347 GTK_WIDGET_SET_FLAGS(button_close, GTK_CAN_DEFAULT); | |
1348 g_signal_connect_swapped(button_close, "clicked", | |
1349 G_CALLBACK(gtk_widget_destroy), dialog); | |
1350 | |
1351 button = gtk_dialog_add_button(GTK_DIALOG(dialog), save ? | |
1352 GTK_STOCK_SAVE : GTK_STOCK_OPEN, | |
1353 GTK_RESPONSE_ACCEPT); | |
1354 gtk_button_set_use_stock(GTK_BUTTON(button), TRUE); | |
1355 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT); | |
1356 gtk_window_set_default(GTK_WINDOW(dialog), button); | |
1357 | |
1358 gtk_widget_show(dialog); | |
1359 | |
1360 return dialog; | |
1361 } | |
1362 | |
1363 | |
1364 GtkItemFactory * | |
1365 create_menu(GtkItemFactoryEntry *entries, | |
1366 guint n_entries, | |
1367 GtkAccelGroup *accel) | |
1368 { | |
1369 GtkItemFactory *menu; | |
1370 | |
1371 menu = gtk_item_factory_new(GTK_TYPE_MENU, "<Main>", accel); | |
1372 gtk_item_factory_set_translate_func(menu, bmp_menu_translate, NULL, | |
1373 NULL); | |
1374 gtk_item_factory_create_items(menu, n_entries, entries, NULL); | |
1375 | |
1376 return menu; | |
1377 } | |
1378 | |
1379 | |
1380 void | |
1381 make_submenu(GtkItemFactory *menu, | |
1382 const gchar *item_path, | |
1383 GtkItemFactory *submenu) | |
1384 { | |
1385 GtkWidget *item, *menu_; | |
1386 | |
1387 item = gtk_item_factory_get_widget(menu, item_path); | |
1388 menu_ = gtk_item_factory_get_widget(submenu, ""); | |
1389 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), menu_); | |
1390 } | |
1391 | |
1392 | |
1393 | |
1394 | |
1395 | |
1396 | |
1397 |