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