Mercurial > audlegacy
annotate audacious/util.c @ 1068:de6e77a1ef79 trunk
[svn] - fix dock.c
author | nenolod |
---|---|
date | Tue, 16 May 2006 20:38:22 -0700 |
parents | dc9373f0186b |
children | 4be4d74db123 |
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 gtk_window_set_title(GTK_WINDOW(dialog), _("Open Files")); | |
983 | |
984 gtk_button_set_label(GTK_BUTTON(button_add), GTK_STOCK_OPEN); | |
985 | |
986 gtk_button_set_label(GTK_BUTTON(toggle), _("Close dialog on Open")); | |
987 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(toggle), cfg.close_dialog_open); | |
988 | |
989 handlerid = g_signal_connect(button_add, "clicked", G_CALLBACK(_filebrowser_play), chooser); | |
990 handlerid_check = g_signal_connect(toggle, "toggled", G_CALLBACK(_filebrowser_check_hide_open), NULL); | |
991 handlerid_do = g_signal_connect_after(button_add, "clicked", G_CALLBACK(_filebrowser_do_hide_open), dialog); | |
992 handlerid_activate = g_signal_connect(chooser, "file-activated", G_CALLBACK(_filebrowser_play), chooser); | |
993 handlerid_do_activate = g_signal_connect_after(chooser,"file_activated", G_CALLBACK(_filebrowser_do_hide_open), dialog); | |
994 } | |
995 else { | |
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) { | |
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
|
1047 gdk_cursor_unref(cursor); |
0 | 1048 cursor = NULL; |
1049 } | |
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
|
1050 |
0 | 1051 return; |
1052 } | |
1053 | |
1054 if (!cursor) | |
1055 cursor = gdk_cursor_new(GDK_LEFT_PTR); | |
1056 | |
1057 gdk_window_set_cursor(window->window, cursor); | |
1058 } | |
1059 | |
1060 /* text_get_extents() taken from The GIMP (C) Spencer Kimball, Peter | |
1061 * Mattis et al */ | |
1062 gboolean | |
1063 text_get_extents(const gchar * fontname, | |
1064 const gchar * text, | |
1065 gint * width, gint * height, gint * ascent, gint * descent) | |
1066 { | |
1067 PangoFontDescription *font_desc; | |
1068 PangoLayout *layout; | |
1069 PangoRectangle rect; | |
1070 | |
1071 g_return_val_if_fail(fontname != NULL, FALSE); | |
1072 g_return_val_if_fail(text != NULL, FALSE); | |
1073 | |
1074 /* FIXME: resolution */ | |
1075 layout = gtk_widget_create_pango_layout(GTK_WIDGET(mainwin), text); | |
1076 | |
1077 font_desc = pango_font_description_from_string(fontname); | |
1078 pango_layout_set_font_description(layout, font_desc); | |
1079 pango_font_description_free(font_desc); | |
1080 pango_layout_get_pixel_extents(layout, NULL, &rect); | |
1081 | |
1082 if (width) | |
1083 *width = rect.width; | |
1084 if (height) | |
1085 *height = rect.height; | |
1086 | |
1087 if (ascent || descent) { | |
1088 PangoLayoutIter *iter; | |
1089 PangoLayoutLine *line; | |
1090 | |
1091 iter = pango_layout_get_iter(layout); | |
1092 line = pango_layout_iter_get_line(iter); | |
1093 pango_layout_iter_free(iter); | |
1094 | |
1095 pango_layout_line_get_pixel_extents(line, NULL, &rect); | |
1096 | |
1097 if (ascent) | |
1098 *ascent = PANGO_ASCENT(rect); | |
1099 if (descent) | |
1100 *descent = -PANGO_DESCENT(rect); | |
1101 } | |
1102 | |
1103 g_object_unref(layout); | |
1104 | |
1105 return TRUE; | |
1106 } | |
1107 | |
1108 /* counts number of digits in a gint */ | |
1109 guint | |
1110 gint_count_digits(gint n) | |
1111 { | |
1112 guint count = 0; | |
1113 | |
1114 n = ABS(n); | |
1115 do { | |
1116 count++; | |
1117 n /= 10; | |
1118 } while (n > 0); | |
1119 | |
1120 return count; | |
1121 } | |
1122 | |
1123 static gchar * | |
1124 str_twenty_to_space(gchar * str) | |
1125 { | |
1126 gchar *match, *match_end; | |
1127 | |
1128 g_return_val_if_fail(str != NULL, NULL); | |
1129 | |
1130 while ((match = strstr(str, "%20"))) { | |
1131 match_end = match + 3; | |
1132 *match++ = ' '; | |
1133 while (*match_end) | |
1134 *match++ = *match_end++; | |
1135 *match = 0; | |
1136 } | |
1137 | |
1138 return str; | |
1139 } | |
1140 | |
1141 static gchar * | |
1142 str_replace_char(gchar * str, gchar old, gchar new) | |
1143 { | |
1144 gchar *match; | |
1145 | |
1146 g_return_val_if_fail(str != NULL, NULL); | |
1147 | |
1148 match = str; | |
1149 while ((match = strchr(match, old))) | |
1150 *match = new; | |
1151 | |
1152 return str; | |
1153 } | |
1154 | |
1155 gchar * | |
1156 str_append(gchar * str, const gchar * add_str) | |
1157 { | |
1158 return str_replace(str, g_strconcat(str, add_str, NULL)); | |
1159 } | |
1160 | |
1161 gchar * | |
1162 str_replace(gchar * str, gchar * new_str) | |
1163 { | |
1164 g_free(str); | |
1165 return new_str; | |
1166 } | |
1167 | |
1168 void | |
1169 str_replace_in(gchar ** str, gchar * new_str) | |
1170 { | |
1171 *str = str_replace(*str, new_str); | |
1172 } | |
1173 | |
1174 | |
1175 gboolean | |
1176 str_has_prefix_nocase(const gchar * str, const gchar * prefix) | |
1177 { | |
1178 return (strncasecmp(str, prefix, strlen(prefix)) == 0); | |
1179 } | |
1180 | |
1181 gboolean | |
1182 str_has_suffix_nocase(const gchar * str, const gchar * suffix) | |
1183 { | |
1184 return (strcasecmp(str + strlen(str) - strlen(suffix), suffix) == 0); | |
1185 } | |
1186 | |
1187 gboolean | |
1188 str_has_suffixes_nocase(const gchar * str, gchar * const *suffixes) | |
1189 { | |
1190 gchar *const *suffix; | |
1191 | |
1192 g_return_val_if_fail(str != NULL, FALSE); | |
1193 g_return_val_if_fail(suffixes != NULL, FALSE); | |
1194 | |
1195 for (suffix = suffixes; *suffix; suffix++) | |
1196 if (str_has_suffix_nocase(str, *suffix)) | |
1197 return TRUE; | |
1198 | |
1199 return FALSE; | |
1200 } | |
1201 | |
1202 gchar * | |
1203 str_to_utf8_fallback(const gchar * str) | |
1204 { | |
1205 gchar *out_str, *convert_str, *chr; | |
1206 | |
1207 /* NULL in NULL out */ | |
1208 if (!str) | |
1209 return NULL; | |
1210 | |
1211 convert_str = g_strdup(str); | |
1212 for (chr = convert_str; *chr; chr++) { | |
1213 if (*chr & 0x80) | |
1214 *chr = '?'; | |
1215 } | |
1216 | |
1217 out_str = g_strconcat(convert_str, _(" (invalid UTF-8)"), NULL); | |
1218 g_free(convert_str); | |
1219 | |
1220 return out_str; | |
1221 } | |
1222 | |
1223 gchar * | |
1224 filename_to_utf8(const gchar * filename) | |
1225 { | |
1226 gchar *out_str; | |
1227 | |
1228 /* NULL in NULL out */ | |
1229 if (!filename) | |
1230 return NULL; | |
1231 | |
1232 if ((out_str = g_filename_to_utf8(filename, -1, NULL, NULL, NULL))) | |
1233 return out_str; | |
1234 | |
1235 return str_to_utf8_fallback(filename); | |
1236 } | |
1237 | |
1238 gchar * | |
1239 str_to_utf8(const gchar * str) | |
1240 { | |
1241 gchar *out_str; | |
1242 | |
1243 /* NULL in NULL out */ | |
1244 if (!str) | |
1245 return NULL; | |
1246 | |
1247 /* already UTF-8? */ | |
1248 if (g_utf8_validate(str, -1, NULL)) | |
1249 return g_strdup(str); | |
1250 | |
1251 /* assume encoding associated with locale */ | |
1252 if ((out_str = g_locale_to_utf8(str, -1, NULL, NULL, NULL))) | |
1253 return out_str; | |
1254 | |
1255 /* all else fails, we mask off character codes >= 128, | |
1256 replace with '?' */ | |
1257 return str_to_utf8_fallback(str); | |
1258 } | |
1259 | |
1260 | |
1261 const gchar * | |
1262 str_skip_chars(const gchar * str, const gchar * chars) | |
1263 { | |
1264 while (strchr(chars, *str)) | |
1265 str++; | |
1266 return str; | |
1267 } | |
1268 | |
1269 gchar * | |
1270 convert_title_text(gchar * title) | |
1271 { | |
1272 g_return_val_if_fail(title != NULL, NULL); | |
1273 | |
1274 if (cfg.convert_underscore) | |
1275 str_replace_char(title, '_', ' '); | |
1276 | |
1277 if (cfg.convert_twenty) | |
1278 str_twenty_to_space(title); | |
1279 | |
1280 return title; | |
1281 } | |
1282 | |
1283 | |
1284 gboolean | |
1285 dir_foreach(const gchar * path, DirForeachFunc function, | |
1286 gpointer user_data, GError ** error) | |
1287 { | |
1288 GError *error_out = NULL; | |
1289 GDir *dir; | |
1290 const gchar *entry; | |
1291 gchar *entry_fullpath; | |
1292 | |
1293 if (!(dir = g_dir_open(path, 0, &error_out))) { | |
1294 g_propagate_error(error, error_out); | |
1295 return FALSE; | |
1296 } | |
1297 | |
1298 while ((entry = g_dir_read_name(dir))) { | |
1299 entry_fullpath = g_build_filename(path, entry, NULL); | |
1300 | |
1301 if ((*function) (entry_fullpath, entry, user_data)) { | |
1302 g_free(entry_fullpath); | |
1303 break; | |
1304 } | |
1305 | |
1306 g_free(entry_fullpath); | |
1307 } | |
1308 | |
1309 g_dir_close(dir); | |
1310 | |
1311 return TRUE; | |
1312 } | |
1313 | |
1314 GtkWidget * | |
1315 make_filebrowser(const gchar * title, | |
1316 gboolean save) | |
1317 { | |
1318 GtkWidget *dialog; | |
1319 GtkWidget *button; | |
1320 GtkWidget *button_close; | |
1321 | |
1322 g_return_val_if_fail(title != NULL, NULL); | |
1323 | |
1324 dialog = gtk_file_chooser_dialog_new(title, GTK_WINDOW(mainwin), | |
376 | 1325 GTK_FILE_CHOOSER_ACTION_OPEN, NULL, NULL); |
0 | 1326 if (save) |
1327 gtk_file_chooser_set_action(GTK_FILE_CHOOSER(dialog), | |
1328 GTK_FILE_CHOOSER_ACTION_SAVE); | |
1329 | |
1330 if (!save) | |
1331 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), TRUE); | |
1332 | |
1333 g_signal_connect(dialog, "destroy", | |
1334 G_CALLBACK(gtk_widget_destroyed), &dialog); | |
1335 | |
1336 #ifdef HAVE_GNOME_VFS | |
1337 gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(dialog), FALSE); | |
1338 #endif | |
1339 | |
1340 button_close = gtk_dialog_add_button(GTK_DIALOG(dialog), GTK_STOCK_CANCEL, | |
1341 GTK_RESPONSE_REJECT); | |
1342 gtk_button_set_use_stock(GTK_BUTTON(button_close), TRUE); | |
1343 GTK_WIDGET_SET_FLAGS(button_close, GTK_CAN_DEFAULT); | |
1344 g_signal_connect_swapped(button_close, "clicked", | |
1345 G_CALLBACK(gtk_widget_destroy), dialog); | |
1346 | |
1347 button = gtk_dialog_add_button(GTK_DIALOG(dialog), save ? | |
1348 GTK_STOCK_SAVE : GTK_STOCK_OPEN, | |
1349 GTK_RESPONSE_ACCEPT); | |
1350 gtk_button_set_use_stock(GTK_BUTTON(button), TRUE); | |
1351 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT); | |
1352 gtk_window_set_default(GTK_WINDOW(dialog), button); | |
1353 | |
1354 gtk_widget_show(dialog); | |
1355 | |
1356 return dialog; | |
1357 } | |
1358 | |
1359 | |
1360 GtkItemFactory * | |
1361 create_menu(GtkItemFactoryEntry *entries, | |
1362 guint n_entries, | |
1363 GtkAccelGroup *accel) | |
1364 { | |
1365 GtkItemFactory *menu; | |
1366 | |
1367 menu = gtk_item_factory_new(GTK_TYPE_MENU, "<Main>", accel); | |
1368 gtk_item_factory_set_translate_func(menu, bmp_menu_translate, NULL, | |
1369 NULL); | |
1370 gtk_item_factory_create_items(menu, n_entries, entries, NULL); | |
1371 | |
1372 return menu; | |
1373 } | |
1374 | |
1375 | |
1376 void | |
1377 make_submenu(GtkItemFactory *menu, | |
1378 const gchar *item_path, | |
1379 GtkItemFactory *submenu) | |
1380 { | |
1381 GtkWidget *item, *menu_; | |
1382 | |
1383 item = gtk_item_factory_get_widget(menu, item_path); | |
1384 menu_ = gtk_item_factory_get_widget(submenu, ""); | |
1385 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), menu_); | |
1386 } | |
1387 | |
1388 | |
1389 | |
1390 | |
1391 | |
1392 | |
1393 |