Mercurial > audlegacy
annotate src/audacious/util.c @ 2365:cfc8431aceb5 trunk
[svn] cleanups in many regards
author | mf0102 |
---|---|
date | Fri, 19 Jan 2007 14:12:34 -0800 |
parents | f24ae4f40e29 |
children | ad1d7687814c |
rev | line source |
---|---|
2313 | 1 /* Audacious - Cross-platform multimedia player |
2 * Copyright (C) 2005-2007 Audacious development team | |
3 * | |
4 * Based on BMP: | |
5 * Copyright (C) 2003-2004 BMP development team. | |
6 * | |
7 * Based on XMMS: | |
8 * Copyright (C) 1998-2003 XMMS development team. | |
9 * | |
10 * This program is free software; you can redistribute it and/or modify | |
11 * it under the terms of the GNU General Public License as published by | |
12 * the Free Software Foundation; under version 2 of the License. | |
13 * | |
14 * This program is distributed in the hope that it will be useful, | |
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 * GNU General Public License for more details. | |
18 * | |
19 * You should have received a copy of the GNU General Public License | |
20 * along with this program; if not, write to the Free Software | |
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
22 */ | |
23 | |
24 #define WEIRD_UTF_16_PLAYLIST_ENCODING | |
25 | |
26 #ifdef HAVE_CONFIG_H | |
27 # include "config.h" | |
28 #endif | |
29 | |
30 #define NEED_GLADE | |
31 #include "util.h" | |
32 | |
33 #include <glib.h> | |
34 #include <glib/gi18n.h> | |
35 #include <glade/glade.h> | |
36 #include <gtk/gtk.h> | |
37 #include <stdio.h> | |
38 #include <stdlib.h> | |
39 #include <string.h> | |
40 #include <ctype.h> | |
41 | |
42 #include "platform/smartinclude.h" | |
43 #include <gdk/gdkkeysyms.h> | |
44 #include <X11/Xlib.h> | |
45 //#include <sys/ipc.h> | |
46 #include <unistd.h> | |
47 #include <errno.h> | |
48 | |
49 #ifdef HAVE_FTS_H | |
50 # include <fts.h> | |
51 #endif | |
52 | |
53 #include "glade.h" | |
54 #include "input.h" | |
55 #include "main.h" | |
56 #include "playback.h" | |
57 #include "playlist.h" | |
58 #include "ui_playlist.h" | |
59 | |
60 #ifdef USE_CHARDET | |
61 #include "../librcd/librcd.h" | |
62 #ifdef HAVE_UDET | |
63 #include <libudet_c.h> | |
64 #endif | |
65 #endif | |
66 | |
67 /* | |
68 * find <file> in directory <dirname> or subdirectories. return | |
69 * pointer to complete filename which has to be freed by calling | |
70 * "g_free()" after use. Returns NULL if file could not be found. | |
71 */ | |
72 | |
73 typedef struct { | |
74 const gchar *to_match; | |
75 gchar *match; | |
76 gboolean found; | |
77 } FindFileContext; | |
78 | |
79 static gboolean | |
80 find_file_func(const gchar * path, const gchar * basename, gpointer data) | |
81 { | |
82 FindFileContext *context = data; | |
83 | |
84 if (strlen(path) > FILENAME_MAX) { | |
85 g_warning("Ignoring path: name too long (%s)", path); | |
86 return TRUE; | |
87 } | |
88 | |
89 if (g_file_test(path, G_FILE_TEST_IS_REGULAR)) { | |
90 if (!strcasecmp(basename, context->to_match)) { | |
91 context->match = g_strdup(path); | |
92 context->found = TRUE; | |
93 return TRUE; | |
94 } | |
95 } | |
96 else if (g_file_test(path, G_FILE_TEST_IS_DIR)) { | |
97 dir_foreach(path, find_file_func, context, NULL); | |
98 if (context->found) | |
99 return TRUE; | |
100 } | |
101 | |
102 return FALSE; | |
103 } | |
104 | |
105 gchar * | |
106 find_file_recursively(const gchar * path, const gchar * filename) | |
107 { | |
108 FindFileContext context; | |
109 | |
110 context.to_match = filename; | |
111 context.match = NULL; | |
112 context.found = FALSE; | |
113 | |
114 dir_foreach(path, find_file_func, &context, NULL); | |
115 return context.match; | |
116 } | |
117 | |
118 | |
119 typedef enum { | |
120 ARCHIVE_UNKNOWN = 0, | |
121 ARCHIVE_DIR, | |
122 ARCHIVE_TAR, | |
123 ARCHIVE_TGZ, | |
124 ARCHIVE_ZIP, | |
125 ARCHIVE_TBZ2 | |
126 } ArchiveType; | |
127 | |
128 typedef gchar *(*ArchiveExtractFunc) (const gchar *, const gchar *); | |
129 | |
130 typedef struct { | |
131 ArchiveType type; | |
132 const gchar *ext; | |
133 } ArchiveExtensionType; | |
134 | |
135 static ArchiveExtensionType archive_extensions[] = { | |
136 {ARCHIVE_TAR, ".tar"}, | |
137 {ARCHIVE_ZIP, ".wsz"}, | |
138 {ARCHIVE_ZIP, ".zip"}, | |
139 {ARCHIVE_TGZ, ".tar.gz"}, | |
140 {ARCHIVE_TGZ, ".tgz"}, | |
141 {ARCHIVE_TBZ2, ".tar.bz2"}, | |
142 {ARCHIVE_TBZ2, ".bz2"}, | |
143 {ARCHIVE_UNKNOWN, NULL} | |
144 }; | |
145 | |
146 static gchar *archive_extract_tar(const gchar * archive, const gchar * dest); | |
147 static gchar *archive_extract_zip(const gchar * archive, const gchar * dest); | |
148 static gchar *archive_extract_tgz(const gchar * archive, const gchar * dest); | |
149 static gchar *archive_extract_tbz2(const gchar * archive, const gchar * dest); | |
150 | |
151 static ArchiveExtractFunc archive_extract_funcs[] = { | |
152 NULL, | |
153 NULL, | |
154 archive_extract_tar, | |
155 archive_extract_tgz, | |
156 archive_extract_zip, | |
157 archive_extract_tbz2 | |
158 }; | |
159 | |
160 | |
161 /* FIXME: these functions can be generalised into a function using a | |
162 * command lookup table */ | |
163 | |
164 static const gchar * | |
165 get_tar_command(void) | |
166 { | |
167 static const gchar *command = NULL; | |
168 | |
169 if (!command) { | |
170 if (!(command = getenv("TARCMD"))) | |
171 command = "tar"; | |
172 } | |
173 | |
174 return command; | |
175 } | |
176 | |
177 static const gchar * | |
178 get_unzip_command(void) | |
179 { | |
180 static const gchar *command = NULL; | |
181 | |
182 if (!command) { | |
183 if (!(command = getenv("UNZIPCMD"))) | |
184 command = "unzip"; | |
185 } | |
186 | |
187 return command; | |
188 } | |
189 | |
190 | |
191 static gchar * | |
192 archive_extract_tar(const gchar * archive, const gchar * dest) | |
193 { | |
194 return g_strdup_printf("%s >/dev/null xf \"%s\" -C %s", | |
195 get_tar_command(), archive, dest); | |
196 } | |
197 | |
198 static gchar * | |
199 archive_extract_zip(const gchar * archive, const gchar * dest) | |
200 { | |
201 return g_strdup_printf("%s >/dev/null -o -j \"%s\" -d %s", | |
202 get_unzip_command(), archive, dest); | |
203 } | |
204 | |
205 static gchar * | |
206 archive_extract_tgz(const gchar * archive, const gchar * dest) | |
207 { | |
208 return g_strdup_printf("%s >/dev/null xzf \"%s\" -C %s", | |
209 get_tar_command(), archive, dest); | |
210 } | |
211 | |
212 static gchar * | |
213 archive_extract_tbz2(const gchar * archive, const gchar * dest) | |
214 { | |
215 return g_strdup_printf("bzip2 -dc \"%s\" | %s >/dev/null xf - -C %s", | |
216 archive, get_tar_command(), dest); | |
217 } | |
218 | |
219 | |
220 ArchiveType | |
221 archive_get_type(const gchar * filename) | |
222 { | |
223 gint i = 0; | |
224 | |
225 if (g_file_test(filename, G_FILE_TEST_IS_DIR)) | |
226 return ARCHIVE_DIR; | |
227 | |
228 while (archive_extensions[i].ext) { | |
229 if (g_str_has_suffix(filename, archive_extensions[i].ext)) { | |
230 return archive_extensions[i].type; | |
231 } | |
232 i++; | |
233 } | |
234 | |
235 return ARCHIVE_UNKNOWN; | |
236 } | |
237 | |
238 gboolean | |
239 file_is_archive(const gchar * filename) | |
240 { | |
241 return (archive_get_type(filename) > ARCHIVE_DIR); | |
242 } | |
243 | |
244 gchar * | |
245 archive_basename(const gchar * str) | |
246 { | |
247 gint i = 0; | |
248 | |
249 while (archive_extensions[i].ext) { | |
250 if (str_has_suffix_nocase(str, archive_extensions[i].ext)) { | |
251 const gchar *end = g_strrstr(str, archive_extensions[i].ext); | |
252 if (end) { | |
253 return g_strndup(str, end - str); | |
254 } | |
255 break; | |
256 } | |
257 i++; | |
258 } | |
259 | |
260 return NULL; | |
261 } | |
262 | |
263 /* | |
264 decompress_archive | |
265 | |
266 Decompresses the archive "filename" to a temporary directory, | |
267 returns the path to the temp dir, or NULL if failed, | |
268 watch out tho, doesn't actually check if the system command succeeds :-| | |
269 */ | |
270 | |
271 gchar * | |
272 archive_decompress(const gchar * filename) | |
273 { | |
274 gchar *tmpdir, *cmd, *escaped_filename; | |
275 ArchiveType type; | |
2328 | 276 #ifndef HAVE_MKDTEMP |
2313 | 277 mode_t mode755 = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH; |
2328 | 278 #endif |
2313 | 279 |
280 if ((type = archive_get_type(filename)) <= ARCHIVE_DIR) | |
281 return NULL; | |
282 | |
283 #ifdef HAVE_MKDTEMP | |
284 tmpdir = g_build_filename(g_get_tmp_dir(), "audacious.XXXXXXXX", NULL); | |
285 if (!mkdtemp(tmpdir)) { | |
286 g_free(tmpdir); | |
287 g_message("Unable to load skin: Failed to create temporary " | |
288 "directory: %s", g_strerror(errno)); | |
289 return NULL; | |
290 } | |
291 #else | |
292 tmpdir = g_strdup_printf("%s/audacious.%ld", g_get_tmp_dir(), rand()); | |
293 make_directory(tmpdir, mode755); | |
294 #endif | |
295 | |
296 escaped_filename = escape_shell_chars(filename); | |
297 cmd = archive_extract_funcs[type] (escaped_filename, tmpdir); | |
298 g_free(escaped_filename); | |
299 | |
300 if (!cmd) { | |
301 g_message("extraction function is NULL!"); | |
302 g_free(tmpdir); | |
303 return NULL; | |
304 } | |
305 | |
2361
f24ae4f40e29
[svn] - security and warning fixes from ssommer@suse
nenolod
parents:
2332
diff
changeset
|
306 if(system(cmd) == -1) |
f24ae4f40e29
[svn] - security and warning fixes from ssommer@suse
nenolod
parents:
2332
diff
changeset
|
307 { |
f24ae4f40e29
[svn] - security and warning fixes from ssommer@suse
nenolod
parents:
2332
diff
changeset
|
308 g_message("could not execute cmd %s",cmd); |
f24ae4f40e29
[svn] - security and warning fixes from ssommer@suse
nenolod
parents:
2332
diff
changeset
|
309 g_free(cmd); |
f24ae4f40e29
[svn] - security and warning fixes from ssommer@suse
nenolod
parents:
2332
diff
changeset
|
310 return NULL; |
f24ae4f40e29
[svn] - security and warning fixes from ssommer@suse
nenolod
parents:
2332
diff
changeset
|
311 } |
2313 | 312 g_free(cmd); |
313 | |
314 return tmpdir; | |
315 } | |
316 | |
317 | |
318 #ifdef HAVE_FTS_H | |
319 | |
320 void | |
321 del_directory(const gchar * dirname) | |
322 { | |
323 gchar *const argv[2] = { (gchar *) dirname, NULL }; | |
324 FTS *fts; | |
325 FTSENT *p; | |
326 | |
327 fts = fts_open(argv, FTS_PHYSICAL, (gint(*)())NULL); | |
328 while ((p = fts_read(fts))) { | |
329 switch (p->fts_info) { | |
330 case FTS_D: | |
331 break; | |
332 case FTS_DNR: | |
333 case FTS_ERR: | |
334 break; | |
335 case FTS_DP: | |
336 rmdir(p->fts_accpath); | |
337 break; | |
338 default: | |
339 unlink(p->fts_accpath); | |
340 break; | |
341 } | |
342 } | |
343 fts_close(fts); | |
344 } | |
345 | |
346 #else /* !HAVE_FTS */ | |
347 | |
348 gboolean | |
349 del_directory_func(const gchar * path, const gchar * basename, | |
350 gpointer params) | |
351 { | |
352 if (!strcmp(basename, ".") || !strcmp(path, "..")) | |
353 return FALSE; | |
354 | |
355 if (g_file_test(path, G_FILE_TEST_IS_DIR)) { | |
356 dir_foreach(path, del_directory_func, NULL, NULL); | |
357 rmdir(path); | |
358 return FALSE; | |
359 } | |
360 | |
361 unlink(path); | |
362 | |
363 return FALSE; | |
364 } | |
365 | |
366 void | |
367 del_directory(const gchar * path) | |
368 { | |
369 dir_foreach(path, del_directory_func, NULL, NULL); | |
370 rmdir(path); | |
371 } | |
372 | |
373 #endif /* ifdef HAVE_FTS */ | |
374 | |
375 gchar * | |
376 read_ini_string(const gchar * filename, const gchar * section, | |
377 const gchar * key) | |
378 { | |
379 static gchar *buffer = NULL; | |
380 static gchar *open_buffer = NULL; | |
381 gchar *ret_buffer = NULL; | |
382 gint found_section = 0, len = 0; | |
383 static gsize filesize = 0; | |
384 gsize off = 0; | |
385 gchar *outbuf; | |
386 unsigned char x[] = { 0xff, 0xfe, 0x00 }; | |
387 guint counter; | |
388 | |
389 if (!filename) | |
390 return NULL; | |
391 | |
392 /* | |
393 * We optimise for the condition that we may be reading from the | |
394 * same ini-file multiple times. This is fairly common; it happens | |
395 * on .pls playlist loads. To do otherwise would take entirely too | |
396 * long, as fstat() can be very slow when done 21,000 times too many. | |
397 * | |
398 * Therefore, we optimise by keeping the last ini file in memory. | |
399 * Yes, this is a memory leak, but it is not too bad, hopefully. | |
400 * - nenolod | |
401 */ | |
402 if (open_buffer == NULL || strcasecmp(filename, open_buffer)) | |
403 { | |
404 if (buffer != NULL) | |
405 { | |
406 g_free(buffer); | |
407 buffer = NULL; | |
408 } | |
409 | |
410 if (open_buffer != NULL) | |
411 { | |
412 g_free(open_buffer); | |
413 open_buffer = NULL; | |
414 } | |
415 | |
416 if (!g_file_get_contents(filename, &buffer, &filesize, NULL)) | |
417 return NULL; | |
418 | |
419 open_buffer = g_strdup(filename); | |
420 } | |
421 | |
422 /* | |
423 * Convert UTF-16 into something useful. Original implementation | |
424 * by incomp@#audacious. Cleanups \nenolod | |
425 */ | |
426 if (!memcmp(&buffer[0],&x,2)) { | |
427 outbuf = g_malloc (filesize); /* it's safe to waste memory. */ | |
428 | |
429 for (counter = 2; counter < filesize; counter += 2) | |
430 if (!memcmp(&buffer[counter+1], &x[2], 1)) | |
431 outbuf[(counter-2)/2] = buffer[counter]; | |
432 else | |
433 return NULL; | |
434 | |
435 outbuf[(counter-2)/2] = '\0'; | |
436 | |
437 if ((filesize - 2) / 2 == (counter - 2) / 2) { | |
438 g_free(buffer); | |
439 buffer = outbuf; | |
440 } else { | |
441 g_free(outbuf); | |
442 return NULL; /* XXX wrong encoding */ | |
443 } | |
444 } | |
445 | |
446 while (!ret_buffer && off < filesize) { | |
447 while (off < filesize && | |
448 (buffer[off] == '\r' || buffer[off] == '\n' || | |
449 buffer[off] == ' ' || buffer[off] == '\t')) | |
450 off++; | |
451 if (off >= filesize) | |
452 break; | |
453 if (buffer[off] == '[') { | |
454 gint slen = strlen(section); | |
455 off++; | |
456 found_section = 0; | |
457 if (off + slen + 1 < filesize && | |
458 !strncasecmp(section, &buffer[off], slen)) { | |
459 off += slen; | |
460 if (buffer[off] == ']') { | |
461 off++; | |
462 found_section = 1; | |
463 } | |
464 } | |
465 } | |
466 else if (found_section && off + strlen(key) < filesize && | |
467 !strncasecmp(key, &buffer[off], strlen(key))) { | |
468 off += strlen(key); | |
469 while (off < filesize && | |
470 (buffer[off] == ' ' || buffer[off] == '\t')) | |
471 off++; | |
472 if (off >= filesize) | |
473 break; | |
474 if (buffer[off] == '=') { | |
475 off++; | |
476 while (off < filesize && | |
477 (buffer[off] == ' ' || buffer[off] == '\t')) | |
478 off++; | |
479 if (off >= filesize) | |
480 break; | |
481 len = 0; | |
482 while (off + len < filesize && | |
483 buffer[off + len] != '\r' && | |
484 buffer[off + len] != '\n' && buffer[off + len] != ';') | |
485 len++; | |
486 ret_buffer = g_strndup(&buffer[off], len); | |
487 off += len; | |
488 } | |
489 } | |
490 while (off < filesize && buffer[off] != '\r' && buffer[off] != '\n') | |
491 off++; | |
492 } | |
493 | |
494 return ret_buffer; | |
495 } | |
496 | |
497 GArray * | |
498 string_to_garray(const gchar * str) | |
499 { | |
500 GArray *array; | |
501 gint temp; | |
502 const gchar *ptr = str; | |
503 gchar *endptr; | |
504 | |
505 array = g_array_new(FALSE, TRUE, sizeof(gint)); | |
506 for (;;) { | |
507 temp = strtol(ptr, &endptr, 10); | |
508 if (ptr == endptr) | |
509 break; | |
510 g_array_append_val(array, temp); | |
511 ptr = endptr; | |
512 while (!isdigit((int) *ptr) && (*ptr) != '\0') | |
513 ptr++; | |
514 if (*ptr == '\0') | |
515 break; | |
516 } | |
517 return (array); | |
518 } | |
519 | |
520 GArray * | |
521 read_ini_array(const gchar * filename, const gchar * section, | |
522 const gchar * key) | |
523 { | |
524 gchar *temp; | |
525 GArray *a; | |
526 | |
527 if ((temp = read_ini_string(filename, section, key)) == NULL) { | |
528 g_free(temp); | |
529 return NULL; | |
530 } | |
531 a = string_to_garray(temp); | |
532 g_free(temp); | |
533 return a; | |
534 } | |
535 | |
536 void | |
537 glist_movedown(GList * list) | |
538 { | |
539 gpointer temp; | |
540 | |
541 if (g_list_next(list)) { | |
542 temp = list->data; | |
543 list->data = list->next->data; | |
544 list->next->data = temp; | |
545 } | |
546 } | |
547 | |
548 void | |
549 glist_moveup(GList * list) | |
550 { | |
551 gpointer temp; | |
552 | |
553 if (g_list_previous(list)) { | |
554 temp = list->data; | |
555 list->data = list->prev->data; | |
556 list->prev->data = temp; | |
557 } | |
558 } | |
559 | |
560 | |
561 void | |
562 util_menu_position(GtkMenu * menu, gint * x, gint * y, | |
563 gboolean * push_in, gpointer data) | |
564 { | |
565 GtkRequisition requisition; | |
566 gint screen_width; | |
567 gint screen_height; | |
568 MenuPos *pos = data; | |
569 | |
570 gtk_widget_size_request(GTK_WIDGET(menu), &requisition); | |
571 | |
572 screen_width = gdk_screen_width(); | |
573 screen_height = gdk_screen_height(); | |
574 | |
575 *x = CLAMP(pos->x - 2, 0, MAX(0, screen_width - requisition.width)); | |
576 *y = CLAMP(pos->y - 2, 0, MAX(0, screen_height - requisition.height)); | |
577 } | |
578 | |
579 #define URL_HISTORY_MAX_SIZE 30 | |
580 | |
581 static void | |
582 util_add_url_callback(GtkWidget * widget, | |
583 GtkEntry * entry) | |
584 { | |
585 const gchar *text; | |
586 | |
587 text = gtk_entry_get_text(entry); | |
588 if (g_list_find_custom(cfg.url_history, text, (GCompareFunc) strcasecmp)) | |
589 return; | |
590 | |
591 cfg.url_history = g_list_prepend(cfg.url_history, g_strdup(text)); | |
592 | |
593 while (g_list_length(cfg.url_history) > URL_HISTORY_MAX_SIZE) { | |
594 GList *node = g_list_last(cfg.url_history); | |
595 g_free(node->data); | |
596 cfg.url_history = g_list_delete_link(cfg.url_history, node); | |
597 } | |
598 } | |
599 | |
600 GtkWidget * | |
601 util_add_url_dialog_new(const gchar * caption, GCallback ok_func, | |
602 GCallback enqueue_func) | |
603 { | |
604 GtkWidget *win, *vbox, *bbox, *enqueue, *ok, *cancel, *combo, *entry, | |
605 *label; | |
606 GList *url; | |
607 | |
608 win = gtk_window_new(GTK_WINDOW_TOPLEVEL); | |
609 gtk_window_set_title(GTK_WINDOW(win), _("Add/Open URL Dialog")); | |
610 gtk_window_set_type_hint(GTK_WINDOW(win), GDK_WINDOW_TYPE_HINT_DIALOG); | |
611 gtk_window_set_position(GTK_WINDOW(win), GTK_WIN_POS_CENTER); | |
612 gtk_window_set_default_size(GTK_WINDOW(win), 400, -1); | |
613 gtk_container_set_border_width(GTK_CONTAINER(win), 12); | |
614 | |
615 vbox = gtk_vbox_new(FALSE, 10); | |
616 gtk_container_add(GTK_CONTAINER(win), vbox); | |
617 | |
618 label = gtk_label_new(caption); | |
619 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5); | |
620 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0); | |
621 | |
622 combo = gtk_combo_box_entry_new_text(); | |
623 gtk_box_pack_start(GTK_BOX(vbox), combo, FALSE, FALSE, 0); | |
624 | |
625 entry = gtk_bin_get_child(GTK_BIN(combo)); | |
626 gtk_window_set_focus(GTK_WINDOW(win), entry); | |
627 gtk_entry_set_text(GTK_ENTRY(entry), ""); | |
628 | |
629 for (url = cfg.url_history; url; url = g_list_next(url)) | |
630 gtk_combo_box_append_text(GTK_COMBO_BOX(combo), (const gchar *) url->data); | |
631 | |
632 g_signal_connect(entry, "activate", | |
633 G_CALLBACK(util_add_url_callback), | |
634 entry); | |
635 g_signal_connect(entry, "activate", | |
636 G_CALLBACK(ok_func), | |
637 entry); | |
638 g_signal_connect_swapped(entry, "activate", | |
639 G_CALLBACK(gtk_widget_destroy), | |
640 win); | |
641 | |
642 bbox = gtk_hbutton_box_new(); | |
643 gtk_button_box_set_layout(GTK_BUTTON_BOX(bbox), GTK_BUTTONBOX_END); | |
644 gtk_button_box_set_spacing(GTK_BUTTON_BOX(bbox), 5); | |
645 gtk_box_pack_start(GTK_BOX(vbox), bbox, FALSE, FALSE, 0); | |
646 | |
647 ok = gtk_button_new_from_stock(GTK_STOCK_OPEN); | |
648 g_signal_connect(ok, "clicked", | |
649 G_CALLBACK(util_add_url_callback), entry); | |
650 g_signal_connect(ok, "clicked", | |
651 G_CALLBACK(ok_func), entry); | |
652 g_signal_connect_swapped(ok, "clicked", | |
653 G_CALLBACK(gtk_widget_destroy), | |
654 win); | |
655 gtk_box_pack_start(GTK_BOX(bbox), ok, FALSE, FALSE, 0); | |
656 | |
657 enqueue = gtk_button_new_from_stock(GTK_STOCK_ADD); | |
658 gtk_box_pack_start(GTK_BOX(bbox), enqueue, FALSE, FALSE, 0); | |
659 | |
660 g_signal_connect(enqueue, "clicked", | |
661 G_CALLBACK(util_add_url_callback), | |
662 entry); | |
663 g_signal_connect(enqueue, "clicked", | |
664 G_CALLBACK(enqueue_func), | |
665 entry); | |
666 g_signal_connect_swapped(enqueue, "clicked", | |
667 G_CALLBACK(gtk_widget_destroy), | |
668 win); | |
669 | |
670 cancel = gtk_button_new_from_stock(GTK_STOCK_CLOSE); | |
671 gtk_box_pack_start(GTK_BOX(bbox), cancel, FALSE, FALSE, 0); | |
672 | |
673 g_signal_connect_swapped(cancel, "clicked", | |
674 G_CALLBACK(gtk_widget_destroy), | |
675 win); | |
676 | |
677 gtk_widget_show_all(vbox); | |
678 | |
679 return win; | |
680 } | |
681 | |
682 static void | |
683 filebrowser_add_files(GtkFileChooser * browser, | |
684 GSList * files) | |
685 { | |
686 GSList *cur; | |
687 gchar *ptr; | |
688 guint ctr = 0; | |
689 Playlist *playlist = playlist_get_active(); | |
690 | |
691 if (GTK_IS_WIDGET(mainwin_jtf)) | |
692 gtk_widget_set_sensitive(mainwin_jtf, FALSE); | |
693 | |
694 for (cur = files; cur; cur = g_slist_next(cur)) { | |
695 | |
696 if (g_file_test(cur->data,G_FILE_TEST_IS_DIR)) { | |
697 playlist_add_dir(playlist, (const gchar *) cur->data); | |
698 } else { | |
699 playlist_add(playlist, (const gchar *) cur->data); | |
700 } | |
701 | |
702 if (++ctr == 20) { | |
703 playlistwin_update_list(playlist); | |
704 ctr = 0; | |
705 while (gtk_events_pending() ) gtk_main_iteration(); | |
706 } | |
707 } | |
708 | |
709 playlistwin_update_list(playlist); | |
710 | |
711 if (GTK_IS_WIDGET(mainwin_jtf)) | |
712 gtk_widget_set_sensitive(mainwin_jtf, TRUE); | |
713 | |
714 ptr = gtk_file_chooser_get_current_folder(GTK_FILE_CHOOSER(browser)); | |
715 | |
716 g_free(cfg.filesel_path); | |
717 cfg.filesel_path = ptr; | |
718 } | |
719 | |
720 static void | |
721 filebrowser_add(GtkFileChooser *browser) | |
722 { | |
723 GSList *files; | |
724 | |
725 files = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(browser)); | |
726 | |
727 if (!files) { | |
728 return; | |
729 } | |
730 | |
731 filebrowser_add_files(browser, files); | |
732 g_slist_foreach(files, (GFunc) g_free, NULL); | |
733 g_slist_free(files); | |
734 } | |
735 | |
736 static void | |
737 filebrowser_play(GtkFileChooser * browser) | |
738 { | |
739 GSList *files; | |
740 | |
741 files = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(browser)); | |
742 | |
743 if (!files) return; | |
744 | |
745 playlist_clear(playlist_get_active()); | |
746 | |
747 filebrowser_add_files(browser, files); | |
748 g_slist_foreach(files, (GFunc) g_free, NULL); | |
749 g_slist_free(files); | |
750 | |
751 playback_initiate(); | |
752 } | |
753 | |
754 | |
755 static void | |
756 _filebrowser_add_gtk2(GtkWidget *widget, | |
757 gpointer data) | |
758 { | |
759 filebrowser_add(data); | |
760 gtk_file_chooser_unselect_all(GTK_FILE_CHOOSER(data)); | |
761 } | |
762 | |
763 static void | |
764 _filebrowser_play_gtk2(GtkWidget *widget, gpointer data) | |
765 { | |
766 filebrowser_play(data); | |
767 gtk_file_chooser_unselect_all(data); | |
768 } | |
769 | |
770 #if 0 | |
771 static void | |
772 filebrowser_on_response(GtkFileChooser * browser, | |
773 gint response, | |
774 gpointer data) | |
775 { | |
776 gtk_widget_hide(GTK_WIDGET(browser)); | |
777 switch (response) { | |
778 case GTK_RESPONSE_OK: | |
779 break; | |
780 case GTK_RESPONSE_ACCEPT: | |
781 break; | |
782 case GTK_RESPONSE_CLOSE: | |
783 break; | |
784 } | |
785 gtk_widget_destroy(GTK_WIDGET(browser)); | |
786 | |
787 } | |
788 | |
789 #endif | |
790 | |
791 static void | |
792 _filebrowser_check_hide_add(GtkWidget * widget, | |
793 gpointer data) | |
794 { | |
795 cfg.close_dialog_add = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)); | |
796 } | |
797 | |
798 static void | |
799 _filebrowser_check_hide_open(GtkWidget * widget, | |
800 gpointer data) | |
801 { | |
802 cfg.close_dialog_open = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)); | |
803 } | |
804 | |
805 | |
806 | |
807 static gboolean | |
808 filebrowser_on_keypress(GtkWidget * browser, | |
809 GdkEventKey * event, | |
810 gpointer data) | |
811 { | |
812 if (event->keyval == GDK_Escape) { | |
813 /* FIXME: this crashes BMP for some reason */ | |
814 /* g_signal_emit_by_name(browser, "delete-event"); */ | |
815 gtk_widget_hide(browser); | |
816 return TRUE; | |
817 } | |
818 | |
819 return FALSE; | |
820 } | |
821 | |
822 static void | |
823 _filebrowser_do_hide_add(GtkWidget *widget, | |
824 gpointer data) | |
825 { | |
826 if (cfg.close_dialog_add) | |
827 gtk_widget_hide(data); | |
828 } | |
829 | |
830 static void | |
831 _filebrowser_do_hide_open(GtkWidget *widget, | |
832 gpointer data) | |
833 { | |
834 if (cfg.close_dialog_open) | |
835 gtk_widget_hide(data); | |
836 } | |
837 | |
838 void | |
839 util_run_filebrowser_gtk2style(gboolean play_button) | |
840 { | |
841 static GladeXML *xml = NULL; | |
842 static GtkWidget *dialog = NULL; | |
843 static GtkWidget *chooser = NULL; | |
844 | |
845 static GtkWidget *button_add; | |
846 static GtkWidget *button_select_all, *button_deselect_all; | |
847 static GtkWidget *toggle; | |
848 | |
849 static gulong handlerid, handlerid_check, handlerid_do; | |
850 static gulong handlerid_activate, handlerid_do_activate; | |
851 | |
852 if (!xml) { | |
853 /* FIXME: Creating a file chooser dialog manually using | |
854 libglade because there's no way to stop | |
855 GtkFileChooserDialog from resizing the buttons to the same | |
856 size. The long toggle button title causes the buttons to | |
857 turn unnecessarily elongated and fugly. */ | |
858 | |
859 GtkWidget *alignment; | |
860 | |
861 xml = glade_xml_new_or_die(_("Add/Open Files dialog"), | |
862 DATA_DIR "/glade/addfiles.glade", | |
863 NULL, NULL); | |
864 glade_xml_signal_autoconnect(xml); | |
865 | |
866 dialog = glade_xml_get_widget(xml, "add_files_dialog"); | |
867 | |
868 /* FIXME: Creating file chooser widget here because libglade <= 2.4.0 does | |
869 not support GtkFileChooserWidget */ | |
870 | |
871 chooser = gtk_file_chooser_widget_new(GTK_FILE_CHOOSER_ACTION_OPEN); | |
872 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(chooser), TRUE); | |
873 | |
874 if (cfg.filesel_path) | |
875 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(chooser), | |
876 cfg.filesel_path); | |
877 | |
878 alignment = glade_xml_get_widget(xml, "alignment2"); | |
879 gtk_container_add(GTK_CONTAINER(alignment), chooser); | |
880 | |
881 toggle = glade_xml_get_widget(xml, "close_on_action"); | |
882 button_select_all = glade_xml_get_widget(xml, "select_all"); | |
883 button_deselect_all = glade_xml_get_widget(xml, "deselect_all"); | |
884 button_add = glade_xml_get_widget(xml, "action"); | |
885 | |
886 g_signal_connect_swapped(button_select_all, "clicked", | |
887 G_CALLBACK(gtk_file_chooser_select_all), | |
888 chooser); | |
889 g_signal_connect_swapped(button_deselect_all, "clicked", | |
890 G_CALLBACK(gtk_file_chooser_unselect_all), | |
891 chooser); | |
892 | |
893 g_signal_connect(dialog, "key_press_event", | |
894 G_CALLBACK(filebrowser_on_keypress), | |
895 NULL); | |
896 | |
897 gtk_widget_show_all(dialog); | |
898 } /* !xml */ | |
899 else { | |
900 g_signal_handler_disconnect(button_add, handlerid); | |
901 g_signal_handler_disconnect(toggle, handlerid_check); | |
902 g_signal_handler_disconnect(chooser, handlerid_activate); | |
903 g_signal_handler_disconnect(button_add, handlerid_do); | |
904 g_signal_handler_disconnect(chooser, handlerid_do_activate); | |
905 | |
906 if (cfg.refresh_file_list) | |
907 { | |
908 gchar *tmp = gtk_file_chooser_get_current_folder(GTK_FILE_CHOOSER(chooser)); | |
909 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(chooser), tmp); | |
910 | |
911 g_free(tmp); | |
912 } | |
913 } | |
914 | |
915 if (play_button) { | |
916 gtk_window_set_title(GTK_WINDOW(dialog), _("Open Files")); | |
917 | |
918 gtk_button_set_label(GTK_BUTTON(button_add), GTK_STOCK_OPEN); | |
919 | |
920 gtk_button_set_label(GTK_BUTTON(toggle), _("Close dialog on Open")); | |
921 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(toggle), cfg.close_dialog_open); | |
922 | |
923 handlerid = g_signal_connect(button_add, "clicked", G_CALLBACK(_filebrowser_play_gtk2), chooser); | |
924 handlerid_check = g_signal_connect(toggle, "toggled", G_CALLBACK(_filebrowser_check_hide_open), NULL); | |
925 handlerid_do = g_signal_connect_after(button_add, "clicked", G_CALLBACK(_filebrowser_do_hide_open), dialog); | |
926 handlerid_activate = g_signal_connect(chooser, "file-activated", G_CALLBACK(_filebrowser_play_gtk2), chooser); | |
927 handlerid_do_activate = g_signal_connect_after(chooser,"file_activated", G_CALLBACK(_filebrowser_do_hide_open), dialog); | |
928 } | |
929 else { | |
930 gtk_window_set_title(GTK_WINDOW(dialog), _("Add Files")); | |
931 | |
932 gtk_button_set_label(GTK_BUTTON(button_add), GTK_STOCK_ADD); | |
933 | |
934 gtk_button_set_label(GTK_BUTTON(toggle), _("Close dialog on Add")); | |
935 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(toggle), cfg.close_dialog_add); | |
936 | |
937 handlerid = g_signal_connect(button_add, "clicked", G_CALLBACK(_filebrowser_add_gtk2), chooser); | |
938 handlerid_check = g_signal_connect(toggle, "toggled", G_CALLBACK(_filebrowser_check_hide_add), NULL); | |
939 handlerid_do = g_signal_connect_after(button_add, "clicked", G_CALLBACK(_filebrowser_do_hide_add), dialog); | |
940 handlerid_activate = g_signal_connect(chooser, "file-activated", G_CALLBACK(_filebrowser_add_gtk2), chooser); | |
941 handlerid_do_activate = g_signal_connect_after(chooser,"file_activated", G_CALLBACK(_filebrowser_do_hide_add), dialog); | |
942 } | |
943 | |
944 gtk_window_present(GTK_WINDOW(dialog)); | |
945 } | |
946 | |
947 /* | |
948 * Derived from Beep Media Player 0.9.6.1. | |
949 * Which is (C) 2003 - 2006 Milosz Derezynski &c | |
950 * | |
951 * Although I changed it quite a bit. -nenolod | |
952 */ | |
953 static void filebrowser_changed_classic(GtkFileSelection * filesel) | |
954 { | |
955 GList *list; | |
956 GList *node; | |
957 char *filename = (char *) | |
958 gtk_file_selection_get_filename(GTK_FILE_SELECTION(filesel)); | |
959 GtkListStore *store; | |
960 GtkTreeIter iter; | |
961 | |
962 if ((list = input_scan_dir(filename)) != NULL) { | |
963 /* | |
964 * We enter a directory that has been "hijacked" by an | |
965 * input-plugin. This is used by the CDDA plugin | |
966 */ | |
967 store = | |
968 GTK_LIST_STORE(gtk_tree_view_get_model | |
969 (GTK_TREE_VIEW(filesel->file_list))); | |
970 gtk_list_store_clear(store); | |
971 | |
972 node = list; | |
973 while (node) { | |
974 | |
975 gtk_list_store_append(store, &iter); | |
976 gtk_list_store_set(store, &iter, 0, node->data, -1); | |
977 g_free(node->data); | |
978 node = g_list_next(node); | |
979 } | |
980 g_list_free(list); | |
981 } | |
982 } | |
983 | |
984 static void filebrowser_entry_changed_classic(GtkEditable * entry, gpointer data) | |
985 { | |
986 filebrowser_changed_classic(GTK_FILE_SELECTION(data)); | |
987 } | |
988 | |
989 gboolean util_filebrowser_is_dir_classic(GtkFileSelection * filesel) | |
990 { | |
991 char *text; | |
992 struct stat buf; | |
993 gboolean retv = FALSE; | |
994 | |
995 text = g_strdup(gtk_file_selection_get_filename(filesel)); | |
996 | |
997 if (stat(text, &buf) == 0 && S_ISDIR(buf.st_mode)) { | |
998 /* Selected directory */ | |
999 int len = strlen(text); | |
1000 if (len > 3 && !strcmp(text + len - 4, "/../")) { | |
1001 if (len == 4) | |
1002 /* At the root already */ | |
1003 *(text + len - 3) = '\0'; | |
1004 else { | |
1005 char *ptr; | |
1006 *(text + len - 4) = '\0'; | |
1007 ptr = strrchr(text, '/'); | |
1008 *(ptr + 1) = '\0'; | |
1009 } | |
1010 } else if (len > 2 && !strcmp(text + len - 3, "/./")) | |
1011 *(text + len - 2) = '\0'; | |
1012 gtk_file_selection_set_filename(filesel, text); | |
1013 retv = TRUE; | |
1014 } | |
1015 g_free(text); | |
1016 return retv; | |
1017 } | |
1018 | |
1019 static void filebrowser_add_files_classic(gchar ** files, | |
1020 GtkFileSelection * filesel) | |
1021 { | |
1022 int ctr = 0; | |
1023 char *ptr; | |
1024 Playlist *playlist = playlist_get_active(); | |
1025 | |
1026 if (GTK_IS_WIDGET(mainwin_jtf)) | |
1027 gtk_widget_set_sensitive(mainwin_jtf, FALSE); | |
1028 | |
1029 while (files[ctr] != NULL) { | |
1030 playlist_add(playlist, files[ctr++]); | |
1031 } | |
1032 playlistwin_update_list(playlist); | |
1033 | |
1034 if (GTK_IS_WIDGET(mainwin_jtf)) | |
1035 gtk_widget_set_sensitive(mainwin_jtf, TRUE); | |
1036 | |
1037 gtk_label_get(GTK_LABEL(GTK_BIN(filesel->history_pulldown)->child), | |
1038 &ptr); | |
1039 | |
1040 /* This will give an extra slash if the current dir is the root. */ | |
1041 cfg.filesel_path = g_strconcat(ptr, "/", NULL); | |
1042 } | |
1043 | |
1044 static void filebrowser_ok_classic(GtkWidget * w, GtkWidget * filesel) | |
1045 { | |
1046 gchar **files; | |
1047 | |
1048 if (util_filebrowser_is_dir_classic(GTK_FILE_SELECTION(filesel))) | |
1049 return; | |
1050 files = gtk_file_selection_get_selections(GTK_FILE_SELECTION(filesel)); | |
1051 filebrowser_add_files_classic(files, GTK_FILE_SELECTION(filesel)); | |
1052 gtk_widget_destroy(filesel); | |
1053 } | |
1054 | |
1055 static void filebrowser_play_classic(GtkWidget * w, GtkWidget * filesel) | |
1056 { | |
1057 gchar **files; | |
1058 | |
1059 if (util_filebrowser_is_dir_classic | |
1060 (GTK_FILE_SELECTION(GTK_FILE_SELECTION(filesel)))) | |
1061 return; | |
1062 playlist_clear(playlist_get_active()); | |
1063 files = gtk_file_selection_get_selections(GTK_FILE_SELECTION(filesel)); | |
1064 filebrowser_add_files_classic(files, GTK_FILE_SELECTION(filesel)); | |
1065 gtk_widget_destroy(filesel); | |
1066 playback_initiate(); | |
1067 } | |
1068 | |
1069 static void filebrowser_add_selected_files_classic(GtkWidget * w, gpointer data) | |
1070 { | |
1071 gchar **files; | |
1072 | |
1073 GtkFileSelection *filesel = GTK_FILE_SELECTION(data); | |
1074 files = gtk_file_selection_get_selections(filesel); | |
1075 | |
1076 filebrowser_add_files_classic(files, filesel); | |
1077 gtk_tree_selection_unselect_all(gtk_tree_view_get_selection | |
1078 (GTK_TREE_VIEW(filesel->file_list))); | |
1079 | |
1080 gtk_entry_set_text(GTK_ENTRY(filesel->selection_entry), ""); | |
1081 } | |
1082 | |
1083 static void filebrowser_add_all_files_classic(GtkWidget * w, gpointer data) | |
1084 { | |
1085 gchar **files; | |
1086 GtkFileSelection *filesel; | |
1087 | |
1088 filesel = data; | |
1089 gtk_tree_selection_select_all(gtk_tree_view_get_selection | |
1090 (GTK_TREE_VIEW(filesel->file_list))); | |
1091 files = gtk_file_selection_get_selections(filesel); | |
1092 filebrowser_add_files_classic(files, filesel); | |
1093 gtk_tree_selection_unselect_all(gtk_tree_view_get_selection | |
1094 (GTK_TREE_VIEW(filesel->file_list))); | |
1095 gtk_entry_set_text(GTK_ENTRY(filesel->selection_entry), ""); | |
1096 } | |
1097 | |
1098 void | |
1099 util_run_filebrowser_classic(gboolean play_button) | |
1100 { | |
1101 static GtkWidget *dialog; | |
1102 GtkWidget *button_add_selected, *button_add_all, *button_close, | |
1103 *button_add; | |
1104 char *title; | |
1105 | |
1106 if (dialog != NULL) { | |
1107 gtk_window_present(GTK_WINDOW(dialog)); | |
1108 return; | |
1109 } | |
1110 | |
1111 if (play_button) | |
1112 title = _("Play files"); | |
1113 else | |
1114 title = _("Load files"); | |
1115 | |
1116 dialog = gtk_file_selection_new(title); | |
1117 | |
1118 gtk_file_selection_set_select_multiple | |
1119 (GTK_FILE_SELECTION(dialog), TRUE); | |
1120 | |
1121 if (cfg.filesel_path) | |
1122 gtk_file_selection_set_filename(GTK_FILE_SELECTION(dialog), | |
1123 cfg.filesel_path); | |
1124 | |
1125 gtk_file_selection_hide_fileop_buttons(GTK_FILE_SELECTION(dialog)); | |
1126 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER); | |
1127 | |
1128 gtk_widget_hide(GTK_FILE_SELECTION(dialog)->ok_button); | |
1129 gtk_widget_destroy(GTK_FILE_SELECTION(dialog)->cancel_button); | |
1130 | |
1131 /* | |
1132 * The mnemonics are quite unorthodox, but that should guarantee they're unique in any locale | |
1133 * plus kinda easy to use | |
1134 */ | |
1135 button_add_selected = | |
1136 gtk_dialog_add_button(GTK_DIALOG(dialog), "Add selected", | |
1137 GTK_RESPONSE_NONE); | |
1138 gtk_button_set_use_underline(GTK_BUTTON(button_add_selected), TRUE); | |
1139 g_signal_connect(G_OBJECT(button_add_selected), "clicked", | |
1140 G_CALLBACK(filebrowser_add_selected_files_classic), dialog); | |
1141 | |
1142 button_add_all = | |
1143 gtk_dialog_add_button(GTK_DIALOG(dialog), "Add all", | |
1144 GTK_RESPONSE_NONE); | |
1145 gtk_button_set_use_underline(GTK_BUTTON(button_add_all), TRUE); | |
1146 g_signal_connect(G_OBJECT(button_add_all), "clicked", | |
1147 G_CALLBACK(filebrowser_add_all_files_classic), dialog); | |
1148 | |
1149 if (play_button) { | |
1150 button_add = | |
1151 gtk_dialog_add_button(GTK_DIALOG(dialog), GTK_STOCK_MEDIA_PLAY, | |
1152 GTK_RESPONSE_NONE); | |
1153 gtk_button_set_use_stock(GTK_BUTTON(button_add), TRUE); | |
1154 g_signal_connect(G_OBJECT(button_add), "clicked", | |
1155 G_CALLBACK(filebrowser_play_classic), dialog); | |
1156 g_signal_connect(G_OBJECT(GTK_FILE_SELECTION(dialog)->ok_button), | |
1157 "clicked", G_CALLBACK(filebrowser_play_classic), dialog); | |
1158 } else { | |
1159 button_add = | |
1160 gtk_dialog_add_button(GTK_DIALOG(dialog), GTK_STOCK_ADD, | |
1161 GTK_RESPONSE_NONE); | |
1162 gtk_button_set_use_stock(GTK_BUTTON(button_add), TRUE); | |
1163 g_signal_connect(G_OBJECT(button_add), "clicked", | |
1164 G_CALLBACK(filebrowser_ok_classic), dialog); | |
1165 g_signal_connect(G_OBJECT(GTK_FILE_SELECTION(dialog)->ok_button), | |
1166 "clicked", G_CALLBACK(filebrowser_ok_classic), dialog); | |
1167 } | |
1168 | |
1169 button_close = | |
1170 gtk_dialog_add_button(GTK_DIALOG(dialog), GTK_STOCK_CLOSE, | |
1171 GTK_RESPONSE_NONE); | |
1172 gtk_button_set_use_stock(GTK_BUTTON(button_close), TRUE); | |
1173 g_signal_connect_swapped(G_OBJECT(button_close), "clicked", | |
1174 G_CALLBACK(gtk_widget_destroy), | |
1175 G_OBJECT(dialog)); | |
1176 | |
1177 gtk_widget_set_size_request(dialog, 600, 450); | |
1178 gtk_widget_realize(dialog); | |
1179 | |
1180 g_signal_connect(G_OBJECT | |
1181 (GTK_FILE_SELECTION(dialog)->history_pulldown), | |
1182 "changed", G_CALLBACK(filebrowser_entry_changed_classic), | |
1183 dialog); | |
1184 | |
1185 g_signal_connect(G_OBJECT(dialog), "destroy", | |
1186 G_CALLBACK(gtk_widget_destroyed), &dialog); | |
1187 | |
1188 filebrowser_changed_classic(GTK_FILE_SELECTION(dialog)); | |
1189 | |
1190 gtk_widget_show(dialog); | |
1191 } | |
1192 | |
1193 /* | |
1194 * util_run_filebrowser(gboolean play_button) | |
1195 * | |
1196 * Inputs: | |
1197 * - whether or not a play button should be used | |
1198 * | |
1199 * Outputs: | |
1200 * - none | |
1201 * | |
1202 * Side Effects: | |
1203 * - either a GTK1 or a GTK2 fileselector is launched | |
1204 */ | |
1205 void | |
1206 util_run_filebrowser(gboolean play_button) | |
1207 { | |
1208 if (!cfg.use_xmms_style_fileselector) | |
1209 util_run_filebrowser_gtk2style(play_button); | |
1210 else | |
1211 util_run_filebrowser_classic(play_button); | |
1212 } | |
1213 | |
1214 GdkFont * | |
1215 util_font_load(const gchar * name) | |
1216 { | |
1217 GdkFont *font; | |
1218 PangoFontDescription *desc; | |
1219 | |
1220 desc = pango_font_description_from_string(name); | |
1221 font = gdk_font_from_description(desc); | |
1222 | |
1223 return font; | |
1224 } | |
1225 | |
1226 #ifdef ENABLE_NLS | |
1227 gchar * | |
1228 bmp_menu_translate(const gchar * path, gpointer func_data) | |
1229 { | |
1230 gchar *translation = gettext(path); | |
1231 | |
1232 if (!translation || *translation != '/') { | |
1233 g_warning("Bad translation for menupath: %s", path); | |
1234 translation = (gchar *) path; | |
1235 } | |
1236 | |
1237 return translation; | |
1238 } | |
1239 #endif | |
1240 | |
1241 void | |
1242 util_set_cursor(GtkWidget * window) | |
1243 { | |
1244 static GdkCursor *cursor = NULL; | |
1245 | |
1246 if (!window) { | |
1247 if (cursor) { | |
1248 gdk_cursor_unref(cursor); | |
1249 cursor = NULL; | |
1250 } | |
1251 | |
1252 return; | |
1253 } | |
1254 | |
1255 if (!cursor) | |
1256 cursor = gdk_cursor_new(GDK_LEFT_PTR); | |
1257 | |
1258 gdk_window_set_cursor(window->window, cursor); | |
1259 } | |
1260 | |
1261 /* text_get_extents() taken from The GIMP (C) Spencer Kimball, Peter | |
1262 * Mattis et al */ | |
1263 gboolean | |
1264 text_get_extents(const gchar * fontname, | |
1265 const gchar * text, | |
1266 gint * width, gint * height, gint * ascent, gint * descent) | |
1267 { | |
1268 PangoFontDescription *font_desc; | |
1269 PangoLayout *layout; | |
1270 PangoRectangle rect; | |
1271 | |
1272 g_return_val_if_fail(fontname != NULL, FALSE); | |
1273 g_return_val_if_fail(text != NULL, FALSE); | |
1274 | |
1275 /* FIXME: resolution */ | |
1276 layout = gtk_widget_create_pango_layout(GTK_WIDGET(mainwin), text); | |
1277 | |
1278 font_desc = pango_font_description_from_string(fontname); | |
1279 pango_layout_set_font_description(layout, font_desc); | |
1280 pango_font_description_free(font_desc); | |
1281 pango_layout_get_pixel_extents(layout, NULL, &rect); | |
1282 | |
1283 if (width) | |
1284 *width = rect.width; | |
1285 if (height) | |
1286 *height = rect.height; | |
1287 | |
1288 if (ascent || descent) { | |
1289 PangoLayoutIter *iter; | |
1290 PangoLayoutLine *line; | |
1291 | |
1292 iter = pango_layout_get_iter(layout); | |
1293 line = pango_layout_iter_get_line(iter); | |
1294 pango_layout_iter_free(iter); | |
1295 | |
1296 pango_layout_line_get_pixel_extents(line, NULL, &rect); | |
1297 | |
1298 if (ascent) | |
1299 *ascent = PANGO_ASCENT(rect); | |
1300 if (descent) | |
1301 *descent = -PANGO_DESCENT(rect); | |
1302 } | |
1303 | |
1304 g_object_unref(layout); | |
1305 | |
1306 return TRUE; | |
1307 } | |
1308 | |
1309 /* counts number of digits in a gint */ | |
1310 guint | |
1311 gint_count_digits(gint n) | |
1312 { | |
1313 guint count = 0; | |
1314 | |
1315 n = ABS(n); | |
1316 do { | |
1317 count++; | |
1318 n /= 10; | |
1319 } while (n > 0); | |
1320 | |
1321 return count; | |
1322 } | |
1323 | |
1324 gboolean | |
1325 dir_foreach(const gchar * path, DirForeachFunc function, | |
1326 gpointer user_data, GError ** error) | |
1327 { | |
1328 GError *error_out = NULL; | |
1329 GDir *dir; | |
1330 const gchar *entry; | |
1331 gchar *entry_fullpath; | |
1332 | |
1333 if (!(dir = g_dir_open(path, 0, &error_out))) { | |
1334 g_propagate_error(error, error_out); | |
1335 return FALSE; | |
1336 } | |
1337 | |
1338 while ((entry = g_dir_read_name(dir))) { | |
1339 entry_fullpath = g_build_filename(path, entry, NULL); | |
1340 | |
1341 if ((*function) (entry_fullpath, entry, user_data)) { | |
1342 g_free(entry_fullpath); | |
1343 break; | |
1344 } | |
1345 | |
1346 g_free(entry_fullpath); | |
1347 } | |
1348 | |
1349 g_dir_close(dir); | |
1350 | |
1351 return TRUE; | |
1352 } | |
1353 | |
1354 GtkWidget * | |
1355 make_filebrowser(const gchar * title, | |
1356 gboolean save) | |
1357 { | |
1358 GtkWidget *dialog; | |
1359 GtkWidget *button; | |
1360 GtkWidget *button_close; | |
1361 | |
1362 g_return_val_if_fail(title != NULL, NULL); | |
1363 | |
1364 dialog = gtk_file_chooser_dialog_new(title, GTK_WINDOW(mainwin), | |
1365 GTK_FILE_CHOOSER_ACTION_OPEN, NULL, NULL); | |
1366 if (save) | |
1367 gtk_file_chooser_set_action(GTK_FILE_CHOOSER(dialog), | |
1368 GTK_FILE_CHOOSER_ACTION_SAVE); | |
1369 | |
1370 if (!save) | |
1371 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), TRUE); | |
1372 | |
1373 g_signal_connect(dialog, "destroy", | |
1374 G_CALLBACK(gtk_widget_destroyed), &dialog); | |
1375 | |
1376 button_close = gtk_dialog_add_button(GTK_DIALOG(dialog), GTK_STOCK_CANCEL, | |
1377 GTK_RESPONSE_REJECT); | |
1378 gtk_button_set_use_stock(GTK_BUTTON(button_close), TRUE); | |
1379 GTK_WIDGET_SET_FLAGS(button_close, GTK_CAN_DEFAULT); | |
1380 g_signal_connect_swapped(button_close, "clicked", | |
1381 G_CALLBACK(gtk_widget_destroy), dialog); | |
1382 | |
1383 button = gtk_dialog_add_button(GTK_DIALOG(dialog), save ? | |
1384 GTK_STOCK_SAVE : GTK_STOCK_OPEN, | |
1385 GTK_RESPONSE_ACCEPT); | |
1386 gtk_button_set_use_stock(GTK_BUTTON(button), TRUE); | |
1387 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT); | |
1388 gtk_window_set_default(GTK_WINDOW(dialog), button); | |
1389 | |
1390 gtk_widget_show(dialog); | |
1391 | |
1392 return dialog; | |
1393 } | |
1394 | |
1395 /* | |
1396 * Resizes a GDK pixmap. | |
1397 */ | |
1398 GdkPixmap *audacious_pixmap_resize(GdkWindow *src, GdkGC *src_gc, GdkPixmap *in, gint width, gint height) | |
1399 { | |
1400 GdkPixmap *out; | |
1401 gint owidth, oheight; | |
1402 | |
1403 g_return_val_if_fail(src != NULL, NULL); | |
1404 g_return_val_if_fail(src_gc != NULL, NULL); | |
1405 g_return_val_if_fail(in != NULL, NULL); | |
1406 g_return_val_if_fail(width > 0 && height > 0, NULL); | |
1407 | |
1408 gdk_drawable_get_size(in, &owidth, &oheight); | |
1409 | |
1410 if (oheight == height && owidth == width) | |
1411 return NULL; | |
1412 | |
1413 out = gdk_pixmap_new(src, width, height, -1); | |
1414 | |
1415 gdk_draw_rectangle(out, src_gc, TRUE, 0, 0, width, height); | |
1416 | |
1417 gdk_window_copy_area(out, src_gc, 0, 0, in, 0, 0, owidth, oheight); | |
1418 g_object_unref(src); | |
1419 | |
1420 return out; | |
1421 } | |
1422 | |
1423 GdkImage *create_dblsize_image(GdkImage * img) | |
1424 { | |
1425 GdkImage *dblimg; | |
1426 register guint x, y; | |
1427 | |
1428 /* | |
1429 * This needs to be optimized | |
1430 */ | |
1431 | |
1432 dblimg = | |
1433 gdk_image_new(GDK_IMAGE_NORMAL, gdk_visual_get_system(), | |
1434 img->width << 1, img->height << 1); | |
1435 if (dblimg->bpp == 1) { | |
1436 register guint8 *srcptr, *ptr, *ptr2, pix; | |
1437 | |
2332 | 1438 srcptr = GDK_IMAGE(img)->mem; |
1439 ptr = GDK_IMAGE(dblimg)->mem; | |
2313 | 1440 ptr2 = ptr + dblimg->bpl; |
1441 | |
1442 for (y = 0; y < img->height; y++) { | |
1443 for (x = 0; x < img->width; x++) { | |
1444 pix = *srcptr++; | |
1445 *ptr++ = pix; | |
1446 *ptr++ = pix; | |
1447 *ptr2++ = pix; | |
1448 *ptr2++ = pix; | |
1449 } | |
1450 srcptr += img->bpl - img->width; | |
1451 ptr += (dblimg->bpl << 1) - dblimg->width; | |
1452 ptr2 += (dblimg->bpl << 1) - dblimg->width; | |
1453 } | |
1454 } | |
1455 if (dblimg->bpp == 2) { | |
1456 guint16 *srcptr, *ptr, *ptr2, pix; | |
1457 | |
1458 srcptr = (guint16 *) GDK_IMAGE_XIMAGE(img)->data; | |
1459 ptr = (guint16 *) GDK_IMAGE_XIMAGE(dblimg)->data; | |
1460 ptr2 = ptr + (dblimg->bpl >> 1); | |
1461 | |
1462 for (y = 0; y < img->height; y++) { | |
1463 for (x = 0; x < img->width; x++) { | |
1464 pix = *srcptr++; | |
1465 *ptr++ = pix; | |
1466 *ptr++ = pix; | |
1467 *ptr2++ = pix; | |
1468 *ptr2++ = pix; | |
1469 } | |
1470 srcptr += (img->bpl >> 1) - img->width; | |
1471 ptr += (dblimg->bpl) - dblimg->width; | |
1472 ptr2 += (dblimg->bpl) - dblimg->width; | |
1473 } | |
1474 } | |
1475 if (dblimg->bpp == 3) { | |
1476 register guint8 *srcptr, *ptr, *ptr2, pix1, pix2, pix3; | |
1477 | |
2332 | 1478 srcptr = GDK_IMAGE(img)->mem; |
1479 ptr = GDK_IMAGE(dblimg)->mem; | |
2313 | 1480 ptr2 = ptr + dblimg->bpl; |
1481 | |
1482 for (y = 0; y < img->height; y++) { | |
1483 for (x = 0; x < img->width; x++) { | |
1484 pix1 = *srcptr++; | |
1485 pix2 = *srcptr++; | |
1486 pix3 = *srcptr++; | |
1487 *ptr++ = pix1; | |
1488 *ptr++ = pix2; | |
1489 *ptr++ = pix3; | |
1490 *ptr++ = pix1; | |
1491 *ptr++ = pix2; | |
1492 *ptr++ = pix3; | |
1493 *ptr2++ = pix1; | |
1494 *ptr2++ = pix2; | |
1495 *ptr2++ = pix3; | |
1496 *ptr2++ = pix1; | |
1497 *ptr2++ = pix2; | |
1498 *ptr2++ = pix3; | |
1499 | |
1500 } | |
1501 srcptr += img->bpl - (img->width * 3); | |
1502 ptr += (dblimg->bpl << 1) - (dblimg->width * 3); | |
1503 ptr2 += (dblimg->bpl << 1) - (dblimg->width * 3); | |
1504 } | |
1505 } | |
1506 if (dblimg->bpp == 4) { | |
1507 register guint32 *srcptr, *ptr, *ptr2, pix; | |
1508 | |
2332 | 1509 srcptr = (guint32 *) GDK_IMAGE(img)->mem; |
1510 ptr = (guint32 *) GDK_IMAGE(dblimg)->mem; | |
2313 | 1511 ptr2 = ptr + (dblimg->bpl >> 2); |
1512 | |
1513 for (y = 0; y < img->height; y++) { | |
1514 for (x = 0; x < img->width; x++) { | |
1515 pix = *srcptr++; | |
1516 *ptr++ = pix; | |
1517 *ptr++ = pix; | |
1518 *ptr2++ = pix; | |
1519 *ptr2++ = pix; | |
1520 } | |
1521 srcptr += (img->bpl >> 2) - img->width; | |
1522 ptr += (dblimg->bpl >> 1) - dblimg->width; | |
1523 ptr2 += (dblimg->bpl >> 1) - dblimg->width; | |
1524 } | |
1525 } | |
1526 return dblimg; | |
1527 } | |
1528 | |
1529 /* URL-decode a file: URL path, return NULL if it's not what we expect */ | |
1530 gchar * | |
1531 xmms_urldecode_path(const gchar * encoded_path) | |
1532 { | |
1533 const gchar *cur, *ext; | |
1534 gchar *path, *tmp; | |
1535 gint realchar; | |
1536 | |
1537 if (!encoded_path) | |
1538 return NULL; | |
1539 | |
1540 if (!str_has_prefix_nocase(encoded_path, "file:")) | |
1541 return NULL; | |
1542 | |
1543 cur = encoded_path + 5; | |
1544 | |
1545 if (str_has_prefix_nocase(cur, "//localhost")) | |
1546 cur += 11; | |
1547 | |
1548 if (*cur == '/') | |
1549 while (cur[1] == '/') | |
1550 cur++; | |
1551 | |
1552 tmp = g_malloc0(strlen(cur) + 1); | |
1553 | |
1554 while ((ext = strchr(cur, '%')) != NULL) { | |
1555 strncat(tmp, cur, ext - cur); | |
1556 ext++; | |
1557 cur = ext + 2; | |
1558 if (!sscanf(ext, "%2x", &realchar)) { | |
1559 /* Assume it is a literal '%'. Several file | |
1560 * managers send unencoded file: urls on drag | |
1561 * and drop. */ | |
1562 realchar = '%'; | |
1563 cur -= 2; | |
1564 } | |
1565 tmp[strlen(tmp)] = realchar; | |
1566 } | |
1567 | |
1568 path = g_strconcat(tmp, cur, NULL); | |
1569 g_free(tmp); | |
1570 return path; | |
1571 } | |
1572 |