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