comparison src/audacious/ui_fileinfo.c @ 2313:3149d4b1a9a9 trunk

[svn] - objective-make autodepend fixes - move all sourcecode into src/ and adjust Makefiles accordingly
author nenolod
date Fri, 12 Jan 2007 11:43:40 -0800
parents
children d88558b0de0a
comparison
equal deleted inserted replaced
2312:e1a5a66fb9cc 2313:3149d4b1a9a9
1 /*
2 * Audacious: A cross-platform multimedia player
3 * Copyright (c) 2006 William Pitcock, Tony Vroon, George Averill,
4 * Giacomo Lozito, Derek Pomery and Yoshiki Yazawa.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; under version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 * 02110-1301, USA.
19 */
20
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
24
25 #include <glib.h>
26 #include <glib/gi18n.h>
27 #include <gtk/gtk.h>
28 #include <glade/glade.h>
29 #include <string.h>
30 #include <stddef.h>
31 #include <stdio.h>
32 #include <sys/types.h>
33 #include <dirent.h>
34 #include <unistd.h>
35 #include <errno.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38
39 #include "glade.h"
40
41 #include "plugin.h"
42 #include "pluginenum.h"
43 #include "input.h"
44 #include "effect.h"
45 #include "general.h"
46 #include "output.h"
47 #include "visualization.h"
48
49 #include "main.h"
50 #include "libaudacious/urldecode.h"
51 #include "util.h"
52 #include "dnd.h"
53 #include "titlestring.h"
54
55 #include "libaudacious/configdb.h"
56
57 #include "playlist.h"
58
59 #include "ui_main.h"
60 #include "ui_playlist.h"
61 #include "build_stamp.h"
62 #include "ui_fileinfo.h"
63 #include "ui_playlist.h"
64
65 GtkWidget *fileinfo_win;
66 GtkWidget *filepopup_win;
67
68 static void
69 fileinfo_entry_set_text(const char *entry, const char *text)
70 {
71 GladeXML *xml = g_object_get_data(G_OBJECT(fileinfo_win), "glade-xml");
72 GtkWidget *widget = glade_xml_get_widget(xml, entry);
73
74 if (xml == NULL || widget == NULL)
75 return;
76
77 gtk_entry_set_text(GTK_ENTRY(widget), text);
78 }
79
80 static void
81 fileinfo_entry_set_text_free(const char *entry, char *text)
82 {
83 GladeXML *xml = g_object_get_data(G_OBJECT(fileinfo_win), "glade-xml");
84 GtkWidget *widget = glade_xml_get_widget(xml, entry);
85
86 if (xml == NULL || widget == NULL)
87 return;
88
89 gtk_entry_set_text(GTK_ENTRY(widget), text);
90
91 g_free(text);
92 }
93
94 static void
95 fileinfo_entry_set_image(const char *entry, const char *text)
96 {
97 GladeXML *xml = g_object_get_data(G_OBJECT(fileinfo_win), "glade-xml");
98 GtkWidget *widget = glade_xml_get_widget(xml, entry);
99 GdkPixbuf *pixbuf;
100 int width, height;
101 double aspect;
102 GdkPixbuf *pixbuf2;
103
104 if (xml == NULL || widget == NULL)
105 return;
106
107 pixbuf = gdk_pixbuf_new_from_file(text, NULL);
108
109 if (pixbuf == NULL)
110 return;
111
112 width = gdk_pixbuf_get_width(GDK_PIXBUF(pixbuf));
113 height = gdk_pixbuf_get_height(GDK_PIXBUF(pixbuf));
114
115 if(strcmp(DATA_DIR "/images/audio.png", text))
116 {
117 if(width == 0)
118 width = 1;
119 aspect = (double)height / (double)width;
120 if(aspect > 1.0) {
121 height = (int)(cfg.filepopup_pixelsize * aspect);
122 width = cfg.filepopup_pixelsize;
123 } else {
124 height = cfg.filepopup_pixelsize;
125 width = (int)(cfg.filepopup_pixelsize / aspect);
126 }
127 pixbuf2 = gdk_pixbuf_scale_simple(GDK_PIXBUF(pixbuf), width, height, GDK_INTERP_BILINEAR);
128 g_object_unref(G_OBJECT(pixbuf));
129 pixbuf = pixbuf2;
130 }
131
132 gtk_image_set_from_pixbuf(GTK_IMAGE(widget), GDK_PIXBUF(pixbuf));
133 g_object_unref(G_OBJECT(pixbuf));
134 }
135
136 static void
137 filepopup_entry_set_text(const char *entry, const char *text)
138 {
139 GladeXML *xml = g_object_get_data(G_OBJECT(filepopup_win), "glade-xml");
140 GtkWidget *widget = glade_xml_get_widget(xml, entry);
141
142 if (xml == NULL || widget == NULL)
143 return;
144
145 gtk_label_set_text(GTK_LABEL(widget), text);
146 }
147
148 static void
149 filepopup_entry_set_image(const char *entry, const char *text)
150 {
151 GladeXML *xml = g_object_get_data(G_OBJECT(filepopup_win), "glade-xml");
152 GtkWidget *widget = glade_xml_get_widget(xml, entry);
153 GdkPixbuf *pixbuf;
154 int width, height;
155 double aspect;
156 GdkPixbuf *pixbuf2;
157
158 if (xml == NULL || widget == NULL)
159 return;
160
161 pixbuf = gdk_pixbuf_new_from_file(text, NULL);
162
163 if (pixbuf == NULL)
164 return;
165
166 width = gdk_pixbuf_get_width(GDK_PIXBUF(pixbuf));
167 height = gdk_pixbuf_get_height(GDK_PIXBUF(pixbuf));
168
169 if(strcmp(DATA_DIR "/images/audio.png", text))
170 {
171 if(width == 0)
172 width = 1;
173 aspect = (double)height / (double)width;
174 if(aspect > 1.0) {
175 height = (int)(cfg.filepopup_pixelsize * aspect);
176 width = cfg.filepopup_pixelsize;
177 } else {
178 height = cfg.filepopup_pixelsize;
179 width = (int)(cfg.filepopup_pixelsize / aspect);
180 }
181 pixbuf2 = gdk_pixbuf_scale_simple(GDK_PIXBUF(pixbuf), width, height, GDK_INTERP_BILINEAR);
182 g_object_unref(G_OBJECT(pixbuf));
183 pixbuf = pixbuf2;
184 }
185
186 gtk_image_set_from_pixbuf(GTK_IMAGE(widget), GDK_PIXBUF(pixbuf));
187 g_object_unref(G_OBJECT(pixbuf));
188 }
189
190 static void
191 filepopup_entry_set_text_free(const char *entry, char *text)
192 {
193 GladeXML *xml = g_object_get_data(G_OBJECT(filepopup_win), "glade-xml");
194 GtkWidget *widget = glade_xml_get_widget(xml, entry);
195
196 if (xml == NULL || widget == NULL)
197 return;
198
199 gtk_label_set_text(GTK_LABEL(widget), text);
200
201 g_free(text);
202 }
203
204 static gboolean
205 filepopup_pointer_check_iter(gpointer unused)
206 {
207 gint x, y, pos;
208 TitleInput *tuple;
209 static gint prev_x = 0, prev_y = 0, ctr = 0, prev_pos = -1;
210 static gint shaded_pos = -1, shaded_prev_pos = -1;
211 gboolean skip = FALSE;
212 GdkWindow *win;
213 Playlist *playlist = playlist_get_active();
214
215 win = gdk_window_at_pointer(NULL, NULL);
216 gdk_window_get_pointer(GDK_WINDOW(playlistwin->window), &x, &y, NULL);
217 pos = playlist_list_get_playlist_position(playlistwin_list, x, y);
218
219 if (win == NULL
220 || cfg.show_filepopup_for_tuple == FALSE
221 || playlistwin_list->pl_tooltips == FALSE
222 || pos != prev_pos
223 || win != GDK_WINDOW(playlistwin->window))
224 {
225 prev_pos = pos;
226 ctr = 0;
227 if ( filepopup_win->window != NULL &&
228 gdk_window_is_viewable(GDK_WINDOW(filepopup_win->window)) )
229 filepopup_hide(NULL);
230 return TRUE;
231 }
232
233 if (prev_x == x && prev_y == y)
234 ctr++;
235 else
236 {
237 ctr = 0;
238 prev_x = x;
239 prev_y = y;
240 filepopup_hide(NULL);
241 return TRUE;
242 }
243
244 if (filepopup_win->window == NULL)
245 skip = TRUE;
246
247 if (playlistwin_is_shaded()) {
248 shaded_pos = playlist_get_position(playlist);
249 if (shaded_prev_pos != shaded_pos)
250 skip = TRUE;
251 }
252
253 if (ctr >= cfg.filepopup_delay && (skip == TRUE || gdk_window_is_viewable(GDK_WINDOW(filepopup_win->window)) != TRUE)) {
254 if (pos == -1 && !playlistwin_is_shaded()) {
255 filepopup_hide(NULL);
256 return TRUE;
257 }
258 else { /* shaded mode */
259 tuple = playlist_get_tuple(playlist, shaded_pos);
260 filepopup_hide(NULL);
261 filepopup_show_for_tuple(tuple);
262 shaded_prev_pos = shaded_pos;
263 }
264
265 prev_pos = pos;
266
267 tuple = playlist_get_tuple(playlist, pos);
268 filepopup_show_for_tuple(tuple);
269 }
270
271 return TRUE;
272 }
273
274 void fileinfo_hide(gpointer unused)
275 {
276 gtk_widget_hide(fileinfo_win);
277
278 /* Clear it out. */
279 fileinfo_entry_set_text("entry_title", "");
280 fileinfo_entry_set_text("entry_artist", "");
281 fileinfo_entry_set_text("entry_album", "");
282 fileinfo_entry_set_text("entry_comment", "");
283 fileinfo_entry_set_text("entry_genre", "");
284 fileinfo_entry_set_text("entry_year", "");
285 fileinfo_entry_set_text("entry_track", "");
286 fileinfo_entry_set_text("entry_location", "");
287
288 fileinfo_entry_set_image("image_artwork", DATA_DIR "/images/audio.png");
289 }
290
291 void filepopup_hide(gpointer unused)
292 {
293 gtk_widget_hide(filepopup_win);
294
295 filepopup_entry_set_text("label_title", "");
296 filepopup_entry_set_text("label_artist", "");
297 filepopup_entry_set_text("label_album", "");
298 filepopup_entry_set_text("label_genre", "");
299 filepopup_entry_set_text("label_track", "");
300 filepopup_entry_set_text("label_year", "");
301 filepopup_entry_set_text("label_length", "");
302
303
304 gtk_window_resize(GTK_WINDOW(filepopup_win), 1, 1);
305 }
306
307 void
308 create_fileinfo_window(void)
309 {
310 const gchar *glade_file = DATA_DIR "/glade/fileinfo.glade";
311 GladeXML *xml;
312 GtkWidget *widget;
313
314 xml = glade_xml_new_or_die(_("Track Information Window"), glade_file, NULL, NULL);
315
316 glade_xml_signal_autoconnect(xml);
317
318 fileinfo_win = glade_xml_get_widget(xml, "fileinfo_win");
319 g_object_set_data(G_OBJECT(fileinfo_win), "glade-xml", xml);
320 gtk_window_set_transient_for(GTK_WINDOW(fileinfo_win), GTK_WINDOW(mainwin));
321
322 widget = glade_xml_get_widget(xml, "image_artwork");
323 gtk_image_set_from_file(GTK_IMAGE(widget), DATA_DIR "/images/audio.png");
324
325 widget = glade_xml_get_widget(xml, "btn_close");
326 g_signal_connect(G_OBJECT(widget), "clicked", (GCallback) fileinfo_hide, NULL);
327 }
328
329 void
330 create_filepopup_window(void)
331 {
332 const gchar *glade_file = DATA_DIR "/glade/fileinfo_popup.glade";
333 GladeXML *xml;
334 GtkWidget *widget;
335
336 xml = glade_xml_new_or_die(_("Track Information Popup"), glade_file, NULL, NULL);
337
338 glade_xml_signal_autoconnect(xml);
339
340 filepopup_win = glade_xml_get_widget(xml, "win_pl_popup");
341 g_object_set_data(G_OBJECT(filepopup_win), "glade-xml", xml);
342 gtk_window_set_transient_for(GTK_WINDOW(filepopup_win), GTK_WINDOW(mainwin));
343
344 widget = glade_xml_get_widget(xml, "image_artwork");
345 gtk_image_set_from_file(GTK_IMAGE(widget), DATA_DIR "/images/audio.png");
346
347 g_timeout_add(50, filepopup_pointer_check_iter, NULL);
348 }
349
350 void
351 fileinfo_show_for_tuple(TitleInput *tuple)
352 {
353 gchar *tmp = NULL;
354
355 if (tuple == NULL)
356 return;
357
358 gtk_widget_realize(fileinfo_win);
359
360 if (tuple->track_name)
361 fileinfo_entry_set_text("entry_title", tuple->track_name);
362 if (tuple->performer)
363 fileinfo_entry_set_text("entry_artist", tuple->performer);
364 if (tuple->album_name)
365 fileinfo_entry_set_text("entry_album", tuple->album_name);
366 if (tuple->comment)
367 fileinfo_entry_set_text("entry_comment", tuple->comment);
368 if (tuple->genre)
369 fileinfo_entry_set_text("entry_genre", tuple->genre);
370
371 tmp = g_strdup_printf("%s/%s", tuple->file_path, tuple->file_name);
372 if(tmp){
373 fileinfo_entry_set_text_free("entry_location", str_to_utf8(tmp));
374 g_free(tmp);
375 tmp = NULL;
376 }
377
378 if (tuple->year != 0)
379 fileinfo_entry_set_text_free("entry_year", g_strdup_printf("%d", tuple->year));
380
381 if (tuple->track_number != 0)
382 fileinfo_entry_set_text_free("entry_track", g_strdup_printf("%d", tuple->track_number));
383
384 tmp = fileinfo_recursive_get_image(tuple->file_path, tuple->file_name, 0);
385
386 if(tmp)
387 {
388 fileinfo_entry_set_image("image_artwork", tmp);
389 g_free(tmp);
390 }
391
392 gtk_widget_show(fileinfo_win);
393 }
394
395 static gboolean
396 has_front_cover_extension(const gchar *name)
397 {
398 char *ext;
399
400 ext = strrchr(name, '.');
401 if (!ext) {
402 /* No file extension */
403 return FALSE;
404 }
405
406 return g_strcasecmp(ext, ".jpg") == 0 ||
407 g_strcasecmp(ext, ".jpeg") == 0 ||
408 g_strcasecmp(ext, ".png") == 0;
409 }
410
411 static gboolean
412 cover_name_filter(const gchar *name, const gchar *filter, const gboolean ret_on_empty)
413 {
414 gboolean result = FALSE;
415 gchar **splitted;
416 gchar *current;
417 gchar *lname;
418 gint i;
419
420 if (!filter || strlen(filter) == 0) {
421 return ret_on_empty;
422 }
423
424 splitted = g_strsplit(filter, ",", 0);
425
426 lname = g_strdup(name);
427 g_strdown(lname);
428
429 for (i = 0; !result && (current = splitted[i]); i++) {
430 gchar *stripped = g_strstrip(g_strdup(current));
431 g_strdown(stripped);
432
433 result = result || strstr(lname, stripped);
434
435 g_free(stripped);
436 }
437
438 g_free(lname);
439 g_strfreev(splitted);
440
441 return result;
442 }
443
444 /* Check wether it's an image we want */
445 static gboolean
446 is_front_cover_image(const gchar *imgfile)
447 {
448 return cover_name_filter(imgfile, cfg.cover_name_include, TRUE) &&
449 !cover_name_filter(imgfile, cfg.cover_name_exclude, FALSE);
450 }
451
452 static gboolean
453 is_file_image(const gchar *imgfile, const gchar *file_name)
454 {
455 char *imgfile_ext, *file_name_ext;
456 size_t imgfile_len, file_name_len;
457 gboolean matches;
458
459 imgfile_ext = strrchr(imgfile, '.');
460 if (!imgfile_ext) {
461 /* No file extension */
462 return FALSE;
463 }
464
465 file_name_ext = strrchr(file_name, '.');
466 if (!file_name_ext) {
467 /* No file extension */
468 return FALSE;
469 }
470
471 imgfile_len = (imgfile_ext - imgfile);
472 file_name_len = (file_name_ext - file_name);
473
474 if (imgfile_len == file_name_len) {
475 return (g_ascii_strncasecmp(imgfile, file_name, imgfile_len) == 0);
476 } else {
477 return FALSE;
478 }
479 }
480
481 gchar*
482 fileinfo_recursive_get_image(const gchar* path,
483 const gchar* file_name, gint depth)
484 {
485 GDir *d;
486
487 if (cfg.recurse_for_cover && depth > cfg.recurse_for_cover_depth)
488 return NULL;
489
490 d = g_dir_open(path, 0, NULL);
491
492 if (d) {
493 const gchar *f;
494
495 if (cfg.use_file_cover && file_name) {
496 /* Look for images matching file name */
497 while(f = g_dir_read_name(d)) {
498 gchar *newpath = g_strconcat(path, "/", f, NULL);
499
500 if (!g_file_test(newpath, G_FILE_TEST_IS_DIR) &&
501 has_front_cover_extension(f) &&
502 is_file_image(f, file_name)) {
503 g_dir_close(d);
504 return newpath;
505 }
506
507 g_free(newpath);
508 }
509 g_dir_rewind(d);
510 }
511
512 /* Search for files using filter */
513 while (f = g_dir_read_name(d)) {
514 gchar *newpath = g_strconcat(path, "/", f, NULL);
515
516 if (!g_file_test(newpath, G_FILE_TEST_IS_DIR) &&
517 has_front_cover_extension(f) &&
518 is_front_cover_image(f)) {
519 g_dir_close(d);
520 return newpath;
521 }
522
523 g_free(newpath);
524 }
525 g_dir_rewind(d);
526
527 /* checks whether recursive or not. */
528 if (!cfg.recurse_for_cover) {
529 g_dir_close(d);
530 return NULL;
531 }
532
533 /* Descend into directories recursively. */
534 while (f = g_dir_read_name(d)) {
535 gchar *newpath = g_strconcat(path, "/", f, NULL);
536
537 if(g_file_test(newpath, G_FILE_TEST_IS_DIR)) {
538 gchar *tmp = fileinfo_recursive_get_image(newpath,
539 NULL, depth + 1);
540 if(tmp) {
541 g_free(newpath);
542 g_dir_close(d);
543 return tmp;
544 }
545 }
546
547 g_free(newpath);
548 }
549
550 g_dir_close(d);
551 }
552
553 return NULL;
554 }
555
556 void
557 filepopup_show_for_tuple(TitleInput *tuple)
558 {
559 gchar *tmp = NULL;
560 gint x, y, x_off = 3, y_off = 3, h, w;
561
562 static gchar *last_artwork = NULL;
563 const static char default_artwork[] = DATA_DIR "/images/audio.png";
564
565 if (tuple == NULL)
566 return;
567
568 gtk_widget_realize(filepopup_win);
569
570 filepopup_entry_set_text("label_title", tuple->track_name);
571 filepopup_entry_set_text("label_artist", tuple->performer);
572 filepopup_entry_set_text("label_album", tuple->album_name);
573 filepopup_entry_set_text("label_genre", tuple->genre);
574
575 if (tuple->length != -1)
576 filepopup_entry_set_text_free("label_length", g_strdup_printf("%d:%02d", tuple->length / 60000, (tuple->length / 1000) % 60));
577
578 if (tuple->year != 0)
579 filepopup_entry_set_text_free("label_year", g_strdup_printf("%d", tuple->year));
580
581 if (tuple->track_number != 0)
582 filepopup_entry_set_text_free("label_track", g_strdup_printf("%d", tuple->track_number));
583
584 tmp = fileinfo_recursive_get_image(tuple->file_path, tuple->file_name, 0);
585 if (tmp) { // picture found
586 if (!last_artwork || strcmp(last_artwork, tmp)) { // new picture
587 filepopup_entry_set_image("image_artwork", tmp);
588 g_free(last_artwork);
589 last_artwork = tmp;
590 }
591 else { // same picture
592 }
593 }
594 else { // no picture found
595 if (!last_artwork || strcmp(last_artwork, default_artwork)) {
596 filepopup_entry_set_image("image_artwork", default_artwork);
597 g_free(last_artwork);
598 last_artwork = g_strdup(default_artwork);
599 }
600 else {
601 }
602 }
603
604 gdk_window_get_pointer(NULL, &x, &y, NULL);
605 gtk_window_get_size(GTK_WINDOW(filepopup_win), &w, &h);
606 if (gdk_screen_width()-(w+3) < x) x_off = (w*-1)-3;
607 if (gdk_screen_height()-(h+3) < y) y_off = (h*-1)-3;
608 gtk_window_move(GTK_WINDOW(filepopup_win), x + x_off, y + y_off);
609
610 gtk_widget_show(filepopup_win);
611 }
612
613 void
614 fileinfo_show_for_path(gchar *path)
615 {
616 TitleInput *tuple = input_get_song_tuple(path);
617
618 if (tuple == NULL)
619 return input_file_info_box(path);
620
621 fileinfo_show_for_tuple(tuple);
622
623 bmp_title_input_free(tuple);
624 tuple = NULL;
625 }