rb-header.c 42.6 KB
Newer Older
1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
2
3
 *
 *  Copyright (C) 2002, 2003 Jorn Baayen <jorn@nl.linux.org>
4
 *  Copyright (C) 2003 Colin Walters <walters@gnome.org>
5
6
7
8
9
10
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
11
 *  The Rhythmbox authors hereby grant permission for non-GPL compatible
James Livingston's avatar
James Livingston committed
12
13
14
15
16
17
18
 *  GStreamer plugins to be used and distributed together with GStreamer
 *  and Rhythmbox. This permission is above and beyond the permissions granted
 *  by the GPL license by which Rhythmbox is covered. If you modify this code
 *  you may extend this exception to your version of the code, but you are not
 *  obligated to do so. If you do not wish to do so, delete this exception
 *  statement from your version.
 *
19
20
21
22
23
24
25
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
Ryan P. Skadberg's avatar
Ryan P. Skadberg committed
26
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA.
27
28
29
30
 *
 */

#include <config.h>
31
32

#include <math.h>
33
34
#include <string.h>

35
36
37
#include <glib/gi18n.h>
#include <gtk/gtk.h>

38
#include "rb-stock-icons.h"
39
#include "rb-header.h"
40
#include "rb-debug.h"
41
42
#include "rb-shell-player.h"
#include "rb-util.h"
43
#include "rhythmdb.h"
44
#include "rb-player.h"
45
#include "rb-text-helpers.h"
46
47
48
#include "rb-fading-image.h"
#include "rb-file-helpers.h"
#include "rb-ext-db.h"
49

50
51
#define LABEL_SELECT_PADDING	20

52
53
54
55
56
57
58
59
60
61
62
63
64
65
/**
 * SECTION:rb-header
 * @short_description: playback area widgetry
 *
 * The RBHeader widget displays information about the current playing track
 * (title, album, artist), the elapsed or remaining playback time, and a
 * position slider indicating the playback position.  It translates slider
 * move and drag events into seek requests for the player backend.
 *
 * For shoutcast-style streams, the title/artist/album display is supplemented
 * by metadata extracted from the stream.  See #RBStreamingSource for more information
 * on how the metadata is reported.
 */

66
static void rb_header_class_init (RBHeaderClass *klass);
67
static void rb_header_init (RBHeader *header);
68
static void rb_header_constructed (GObject *object);
69
static void rb_header_dispose (GObject *object);
70
71
static void rb_header_finalize (GObject *object);
static void rb_header_set_property (GObject *object,
72
73
74
				    guint prop_id,
				    const GValue *value,
				    GParamSpec *pspec);
75
static void rb_header_get_property (GObject *object,
76
77
78
				    guint prop_id,
				    GValue *value,
				    GParamSpec *pspec);
79
80
81
82
static GtkSizeRequestMode rb_header_get_request_mode (GtkWidget *widget);
static void rb_header_get_preferred_width (GtkWidget *widget,
					   int *minimum_size,
					   int *natural_size);
83
static void rb_header_size_allocate (GtkWidget *widget, GtkAllocation *allocation);
84
static void rb_header_update_elapsed (RBHeader *header);
85
static void apply_slider_position (RBHeader *header);
86
87
88
89
static gboolean slider_press_callback (GtkWidget *widget, GdkEventButton *event, RBHeader *header);
static gboolean slider_moved_callback (GtkWidget *widget, GdkEventMotion *event, RBHeader *header);
static gboolean slider_release_callback (GtkWidget *widget, GdkEventButton *event, RBHeader *header);
static void slider_changed_callback (GtkWidget *widget, RBHeader *header);
90
static gboolean slider_scroll_callback (GtkWidget *widget, GdkEventScroll *event, RBHeader *header);
91
static gboolean slider_focus_out_callback (GtkWidget *widget, GdkEvent *event, RBHeader *header);
92
static void time_button_clicked_cb (GtkWidget *button, RBHeader *header);
93

94
static void rb_header_elapsed_changed_cb (RBShellPlayer *player, gint64 elapsed, RBHeader *header);
95
static void rb_header_extra_metadata_cb (RhythmDB *db, RhythmDBEntry *entry, const char *property_name, const GValue *metadata, RBHeader *header);
96
97
98
99
100
101
static void rb_header_sync (RBHeader *header);
static void rb_header_sync_time (RBHeader *header);

static void uri_dropped_cb (RBFadingImage *image, const char *uri, RBHeader *header);
static void pixbuf_dropped_cb (RBFadingImage *image, GdkPixbuf *pixbuf, RBHeader *header);
static void image_button_press_cb (GtkWidget *widget, GdkEvent *event, RBHeader *header);
102
static gboolean label_button_press_cb (GtkWidget *widget, GdkEventButton *event, RBHeader *header);
103
static void art_added_cb (RBExtDB *db, RBExtDBKey *key, const char *filename, GValue *data, RBHeader *header);
104
105
static void volume_widget_changed_cb (GtkScaleButton *widget, gdouble volume, RBHeader *header);
static void player_volume_changed_cb (RBShellPlayer *player, GParamSpec *pspec, RBHeader *header);
106

107
struct RBHeaderPrivate
108
109
110
{
	RhythmDB *db;
	RhythmDBEntry *entry;
111
	RBExtDB *art_store;
112

113
	RBShellPlayer *shell_player;
114
115
116
	RBSource *playing_source;
	gulong status_changed_id;
	gboolean showing_playback_status;
117

118
	GtkWidget *songbox;
119
	GtkWidget *song;
120
	GtkWidget *details;
121
	GtkWidget *not_playing;
122
	GtkWidget *image;
123
	GtkWidget *volume_button;
124

125
126
127
128
	GtkWidget *scale;
	GtkAdjustment *adjustment;
	gboolean slider_dragging;
	gboolean slider_locked;
129
	gboolean slider_drag_moved;
130
131
	guint slider_moved_timeout;
	long latest_set_time;
132
133
134

	GtkWidget *timebutton;
	GtkWidget *timelabel;
135

136
	gint64 elapsed_time;		/* nanoseconds */
137
	gboolean show_remaining;
138
	long duration;
139
	gboolean seekable;
140
	char *image_path;
141
	RBExtDBKey *art_key;
142
143
	gboolean show_album_art;
	gboolean show_slider;
144
145
	
	gboolean syncing_volume;
146
147
148
149
150
151
};

enum
{
	PROP_0,
	PROP_DB,
152
	PROP_SHELL_PLAYER,
153
	PROP_SEEKABLE,
154
155
	PROP_SLIDER_DRAGGING,
	PROP_SHOW_REMAINING,
156
157
	PROP_SHOW_POSITION_SLIDER,
	PROP_SHOW_ALBUM_ART
158
159
};

160
161
162
#define TITLE_FORMAT  "<big><b>%s</b></big>"
#define ALBUM_FORMAT  "<i>%s</i>"
#define ARTIST_FORMAT "<i>%s</i>"
163
164
165
#define STREAM_FORMAT "%s"

/* unicode graphic characters, encoded in UTF-8 */
166
static const char *const UNICODE_MIDDLE_DOT = "\xC2\xB7";
167

168
169
170
#define SCROLL_UP_SEEK_OFFSET	5
#define SCROLL_DOWN_SEEK_OFFSET -5

171
G_DEFINE_TYPE (RBHeader, rb_header, GTK_TYPE_GRID)
172
173

static void
174
rb_header_class_init (RBHeaderClass *klass)
175
176
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);
177
	GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
178

179
	object_class->constructed = rb_header_constructed;
180
	object_class->dispose = rb_header_dispose;
181
	object_class->finalize = rb_header_finalize;
182

183
184
	object_class->set_property = rb_header_set_property;
	object_class->get_property = rb_header_get_property;
185

186
187
	widget_class->get_request_mode = rb_header_get_request_mode;
	widget_class->get_preferred_width = rb_header_get_preferred_width;
188
	widget_class->size_allocate = rb_header_size_allocate;
189
	/* GtkGrid's get_preferred_height_for_width does all we need here */
190

191
192
193
194
195
	/**
	 * RBHeader:db:
	 *
	 * #RhythmDB instance
	 */
196
197
198
199
200
201
	g_object_class_install_property (object_class,
					 PROP_DB,
					 g_param_spec_object ("db",
							      "RhythmDB",
							      "RhythmDB object",
							      RHYTHMDB_TYPE,
202
							      G_PARAM_READWRITE));
203

204
205
206
207
208
	/**
	 * RBHeader:shell-player:
	 *
	 * The #RBShellPlayer instance
	 */
209
	g_object_class_install_property (object_class,
210
211
212
213
214
215
					 PROP_SHELL_PLAYER,
					 g_param_spec_object ("shell-player",
							      "shell player",
							      "RBShellPlayer object",
							      RB_TYPE_SHELL_PLAYER,
							      G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
216
217
218
219
220
	/**
	 * RBHeader:seekable:
	 *
	 * If TRUE, the header should allow seeking by dragging the playback position slider
	 */
221
222
223
224
225
226
227
228
	g_object_class_install_property (object_class,
					 PROP_SEEKABLE,
					 g_param_spec_boolean ("seekable",
						 	       "seekable",
							       "seekable",
							       TRUE,
							       G_PARAM_READWRITE));

229
230
231
232
233
234
235
236
237
238
239
240
	/**
	 * RBHeader:slider-dragging:
	 *
	 * Whether the song position slider is currently being dragged.
	 */
	g_object_class_install_property (object_class,
					 PROP_SLIDER_DRAGGING,
					 g_param_spec_boolean ("slider-dragging",
						 	       "slider dragging",
							       "slider dragging",
							       FALSE,
							       G_PARAM_READABLE));
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
	/**
	 * RBHeader:show-remaining:
	 *
	 * Whether to show remaining time (as opposed to elapsed time) in the numeric
	 * time display.
	 */
	g_object_class_install_property (object_class,
					 PROP_SHOW_REMAINING,
					 g_param_spec_boolean ("show-remaining",
							       "show remaining",
							       "whether to show remaining or elapsed time",
							       FALSE,
							       G_PARAM_READWRITE));

	/**
	 * RBHeader:show-position-slider:
	 *
	 * Whether to show the playback position slider.
	 */
	g_object_class_install_property (object_class,
					 PROP_SHOW_POSITION_SLIDER,
					 g_param_spec_boolean ("show-position-slider",
							       "show position slider",
							       "whether to show the playback position slider",
							       TRUE,
							       G_PARAM_READWRITE));
267
268
269
270
271
272
273
274
275
276
277
278
	/**
	 * RBHeader:show-album-art:
	 *
	 * Whether to show the album art display widget.
	 */
	g_object_class_install_property (object_class,
					 PROP_SHOW_ALBUM_ART,
					 g_param_spec_boolean ("show-album-art",
							       "show album art",
							       "whether to show album art",
							       TRUE,
							       G_PARAM_READWRITE));
279

280
	g_type_class_add_private (klass, sizeof (RBHeaderPrivate));
281
282
283
}

static void
284
rb_header_init (RBHeader *header)
285
{
286
	header->priv = G_TYPE_INSTANCE_GET_PRIVATE (header, RB_TYPE_HEADER, RBHeaderPrivate);
287
288
289
290
291
292
}

static void
rb_header_constructed (GObject *object)
{
	RBHeader *header = RB_HEADER (object);
293
	char *label;
294
295

	RB_CHAIN_GOBJECT_METHOD (rb_header_parent_class, constructed, object);
296

297
298
299
	gtk_grid_set_column_spacing (GTK_GRID (header), 6);
	gtk_grid_set_column_homogeneous (GTK_GRID (header), TRUE);
	gtk_container_set_border_width (GTK_CONTAINER (header), 3);
300

301
	/* set up position slider */
302
	header->priv->adjustment = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 10.0, 1.0, 10.0, 0.0));
303
	header->priv->scale = gtk_scale_new (GTK_ORIENTATION_HORIZONTAL, header->priv->adjustment);
304
	gtk_widget_set_no_show_all (header->priv->scale, TRUE);
305
	gtk_range_set_fill_level (GTK_RANGE (header->priv->scale), 0.0);
306
	gtk_range_set_show_fill_level (GTK_RANGE (header->priv->scale), FALSE);
307
	gtk_range_set_restrict_to_fill_level (GTK_RANGE (header->priv->scale), FALSE);
308
	gtk_widget_set_hexpand (header->priv->scale, TRUE);
309
	g_signal_connect_object (G_OBJECT (header->priv->scale),
310
311
				 "button_press_event",
				 G_CALLBACK (slider_press_callback),
312
313
				 header, 0);
	g_signal_connect_object (G_OBJECT (header->priv->scale),
314
315
				 "button_release_event",
				 G_CALLBACK (slider_release_callback),
316
317
				 header, 0);
	g_signal_connect_object (G_OBJECT (header->priv->scale),
318
319
				 "motion_notify_event",
				 G_CALLBACK (slider_moved_callback),
320
321
				 header, 0);
	g_signal_connect_object (G_OBJECT (header->priv->scale),
322
323
				 "value_changed",
				 G_CALLBACK (slider_changed_callback),
324
				 header, 0);
325
326
327
328
	g_signal_connect_object (G_OBJECT (header->priv->scale),
				 "scroll_event",
				 G_CALLBACK (slider_scroll_callback),
				 header, 0);
329
330
331
332
	g_signal_connect_object (G_OBJECT (header->priv->scale),
				 "focus-out-event",
				 G_CALLBACK (slider_focus_out_callback),
				 header, 0);
333
334
	gtk_scale_set_draw_value (GTK_SCALE (header->priv->scale), FALSE);
	gtk_widget_set_size_request (header->priv->scale, 150, -1);
335
336
337
338
339

	/* set up song information labels */
	header->priv->songbox = gtk_grid_new ();
	gtk_widget_set_hexpand (header->priv->songbox, TRUE);
	gtk_widget_set_valign (header->priv->songbox, GTK_ALIGN_CENTER);
340
	gtk_orientable_set_orientation (GTK_ORIENTABLE (header->priv->songbox), GTK_ORIENTATION_VERTICAL);
341

342
343
	header->priv->song = g_object_ref (gtk_label_new (" "));
	gtk_widget_show (header->priv->song);
344
345
346
	gtk_label_set_use_markup (GTK_LABEL (header->priv->song), TRUE);
	gtk_label_set_selectable (GTK_LABEL (header->priv->song), TRUE);
	gtk_label_set_ellipsize (GTK_LABEL (header->priv->song), PANGO_ELLIPSIZE_END);
347
348
	gtk_widget_set_halign (header->priv->song, GTK_ALIGN_START);
	gtk_widget_set_valign (header->priv->song, GTK_ALIGN_CENTER);
349
350
351
352
	g_signal_connect_object (header->priv->song,
				 "button-press-event",
				 G_CALLBACK (label_button_press_cb),
				 header, 0);
353

354
355
	header->priv->details = g_object_ref (gtk_label_new (""));
	gtk_widget_show (header->priv->details);
356
357
358
359
	gtk_label_set_use_markup (GTK_LABEL (header->priv->details), TRUE);
	gtk_label_set_selectable (GTK_LABEL (header->priv->details), TRUE);
	gtk_label_set_ellipsize (GTK_LABEL (header->priv->details), PANGO_ELLIPSIZE_END);
	gtk_widget_set_hexpand (header->priv->details, TRUE);
360
361
	gtk_widget_set_halign (header->priv->details, GTK_ALIGN_START);
	gtk_widget_set_valign (header->priv->details, GTK_ALIGN_CENTER);
362
363
364
365
	g_signal_connect_object (header->priv->details,
				 "button-press-event",
				 G_CALLBACK (label_button_press_cb),
				 header, 0);
366

367
368
369
370
371
372
373
374
	label = g_markup_printf_escaped (TITLE_FORMAT, _("Not Playing"));
	header->priv->not_playing = g_object_ref (gtk_label_new (label));
	g_free (label);
	gtk_widget_show (header->priv->not_playing);
	gtk_label_set_use_markup (GTK_LABEL (header->priv->not_playing), TRUE);
	gtk_label_set_selectable (GTK_LABEL (header->priv->not_playing), TRUE);
	gtk_label_set_ellipsize (GTK_LABEL (header->priv->not_playing), PANGO_ELLIPSIZE_END);
	gtk_widget_set_hexpand (header->priv->not_playing, TRUE);
375
376
	gtk_widget_set_halign (header->priv->not_playing, GTK_ALIGN_START);
	gtk_widget_set_valign (header->priv->not_playing, GTK_ALIGN_CENTER);
377
378
379
380
381
382
	gtk_container_add (GTK_CONTAINER (header->priv->songbox), header->priv->not_playing);
	g_signal_connect_object (header->priv->not_playing,
				 "button-press-event",
				 G_CALLBACK (label_button_press_cb),
				 header, 0);

383
384
385
	/* elapsed time / duration display */
	header->priv->timelabel = gtk_label_new ("");
	gtk_widget_set_halign (header->priv->timelabel, GTK_ALIGN_END);
386
	gtk_widget_set_no_show_all (header->priv->timelabel, TRUE);
387
388

	header->priv->timebutton = gtk_button_new ();
389
	gtk_button_set_relief (GTK_BUTTON (header->priv->timebutton), GTK_RELIEF_NONE);
390
391
392
393
394
395
396
397
	gtk_container_add (GTK_CONTAINER (header->priv->timebutton), header->priv->timelabel);
	g_signal_connect_object (header->priv->timebutton,
				 "clicked",
				 G_CALLBACK (time_button_clicked_cb),
				 header, 0);

	/* image display */
	header->priv->art_store = rb_ext_db_new ("album-art");
398
399
400
401
	g_signal_connect (header->priv->art_store,
			  "added",
			  G_CALLBACK (art_added_cb),
			  header);
402
403
404
	header->priv->image = GTK_WIDGET (g_object_new (RB_TYPE_FADING_IMAGE,
							"fallback", RB_STOCK_MISSING_ARTWORK,
							NULL));
405
	gtk_widget_set_no_show_all (header->priv->image, TRUE);
406
407
408
409
410
411
412
413
414
415
416
417
418
	g_signal_connect (header->priv->image,
			  "pixbuf-dropped",
			  G_CALLBACK (pixbuf_dropped_cb),
			  header);
	g_signal_connect (header->priv->image,
			  "uri-dropped",
			  G_CALLBACK (uri_dropped_cb),
			  header);
	g_signal_connect (header->priv->image,
			  "button-press-event",
			  G_CALLBACK (image_button_press_cb),
			  header);

419
420
421
422
423
424
425
426
427
	/* volume button */
	header->priv->volume_button = gtk_volume_button_new ();
	g_signal_connect (header->priv->volume_button, "value-changed",
			  G_CALLBACK (volume_widget_changed_cb),
			  header);
	g_signal_connect (header->priv->shell_player, "notify::volume",
			  G_CALLBACK (player_volume_changed_cb),
			  header);

428
429
430
431
	gtk_grid_attach (GTK_GRID (header), header->priv->image, 0, 0, 1, 1);
	gtk_grid_attach (GTK_GRID (header), header->priv->songbox, 2, 0, 1, 1);
	gtk_grid_attach (GTK_GRID (header), header->priv->timebutton, 3, 0, 1, 1);
	gtk_grid_attach (GTK_GRID (header), header->priv->scale, 4, 0, 1, 1);
432
	gtk_grid_attach (GTK_GRID (header), header->priv->volume_button, 5, 0, 1, 1);
433
434
435
436
437

	/* currently, nothing sets this.  it should be set on track changes. */
	header->priv->seekable = TRUE;

	rb_header_sync (header);
438
439
}

440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
static void
rb_header_dispose (GObject *object)
{
	RBHeader *header = RB_HEADER (object);

	if (header->priv->db != NULL) {
		g_object_unref (header->priv->db);
		header->priv->db = NULL;
	}

	if (header->priv->shell_player != NULL) {
		g_object_unref (header->priv->shell_player);
		header->priv->shell_player = NULL;
	}

	if (header->priv->art_store != NULL) {
		g_object_unref (header->priv->art_store);
		header->priv->art_store = NULL;
	}

460
461
462
463
	g_clear_object (&header->priv->song);
	g_clear_object (&header->priv->details);
	g_clear_object (&header->priv->not_playing);

464
465
466
	G_OBJECT_CLASS (rb_header_parent_class)->dispose (object);
}

467
static void
468
rb_header_finalize (GObject *object)
469
{
470
	RBHeader *header;
471
472

	g_return_if_fail (object != NULL);
473
	g_return_if_fail (RB_IS_HEADER (object));
474

475
476
	header = RB_HEADER (object);
	g_return_if_fail (header->priv != NULL);
477

478
	g_free (header->priv->image_path);
479
480
	if (header->priv->art_key)
		rb_ext_db_key_free (header->priv->art_key);
481

482
	G_OBJECT_CLASS (rb_header_parent_class)->finalize (object);
483
484
}

485
static void
486
art_cb (RBExtDBKey *key, RBExtDBKey *store_key, const char *filename, GValue *data, RBHeader *header)
487
488
489
490
491
492
493
494
{
	RhythmDBEntry *entry;

	entry = rb_shell_player_get_playing_entry (header->priv->shell_player);
	if (entry == NULL) {
		return;
	}

495
	if (rhythmdb_entry_matches_ext_db_key (header->priv->db, entry, store_key)) {
496
497
498
499
500
501
502
503
504
505
		GdkPixbuf *pixbuf = NULL;

		if (data != NULL && G_VALUE_HOLDS (data, GDK_TYPE_PIXBUF)) {
			pixbuf = GDK_PIXBUF (g_value_get_object (data));
		}

		rb_fading_image_set_pixbuf (RB_FADING_IMAGE (header->priv->image), pixbuf);

		g_free (header->priv->image_path);
		header->priv->image_path = g_strdup (filename);
506
507
508
509

		if (header->priv->art_key)
			rb_ext_db_key_free (header->priv->art_key);
		header->priv->art_key = rb_ext_db_key_copy (store_key);
510
511
512
513
514
	}

	rhythmdb_entry_unref (entry);
}

515
516
517
static void
art_added_cb (RBExtDB *db, RBExtDBKey *key, const char *filename, GValue *data, RBHeader *header)
{
518
	art_cb (key, key, filename, data, header);
519
520
}

521
522
523
524
525
static void
playback_status_changed_cb (RBSource *source, RBHeader *header)
{
	rb_header_sync (header);
}
526

527
static void
528
rb_header_playing_song_changed_cb (RBShellPlayer *player, RhythmDBEntry *entry, RBHeader *header)
529
530
531
532
{
	if (header->priv->entry == entry)
		return;

533
534
535
536
	if (header->priv->entry != NULL) {
		g_signal_handler_disconnect (header->priv->playing_source, header->priv->status_changed_id);
	}

537

538
	header->priv->entry = entry;
539
	header->priv->elapsed_time = 0;
540
	if (header->priv->entry) {
541
542
		RBExtDBKey *key;

543
544
		header->priv->duration = rhythmdb_entry_get_ulong (header->priv->entry,
								   RHYTHMDB_PROP_DURATION);
545

546
547
548
549
550
551
552
553
554
555
556
557
558
		if (header->priv->art_key == NULL ||
		    rhythmdb_entry_matches_ext_db_key (header->priv->db, entry, header->priv->art_key) == FALSE) {
			rb_fading_image_start (RB_FADING_IMAGE (header->priv->image), 2000);
			key = rhythmdb_entry_create_ext_db_key (entry, RHYTHMDB_PROP_ALBUM);
			rb_ext_db_request (header->priv->art_store,
					   key,
					   (RBExtDBRequestCallback) art_cb,
					   g_object_ref (header),
					   g_object_unref);
			rb_ext_db_key_free (key);
		} else {
			rb_debug ("existing art matches new entry");
		}
559
560
561
562
563
564
565

		header->priv->playing_source = rb_shell_player_get_playing_source (player);
		header->priv->status_changed_id =
			g_signal_connect (header->priv->playing_source,
					  "playback-status-changed",
					  G_CALLBACK (playback_status_changed_cb),
					  header);
566
	} else {
567
		rb_fading_image_start (RB_FADING_IMAGE (header->priv->image), 2000);
568
569
570
		header->priv->duration = 0;
	}

571
	rb_header_sync (header);
572
573
574
575
576

	g_free (header->priv->image_path);
	header->priv->image_path = NULL;
}

577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
static GtkSizeRequestMode
rb_header_get_request_mode (GtkWidget *widget)
{
	return GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH;
}

static void
rb_header_get_preferred_width (GtkWidget *widget,
			       int *minimum_width,
			       int *natural_width)
{
	*minimum_width = 0;
	*natural_width = 0;
}

592
593
594
595
596
597
598
599
static void
rb_header_size_allocate (GtkWidget *widget, GtkAllocation *allocation)
{
	int spacing;
	int scale_width;
	int info_width;
	int time_width;
	int image_width;
600
	int volume_width;
601
602
603
	GtkAllocation child_alloc;
	gboolean rtl;

604
605
	GTK_WIDGET_CLASS (rb_header_parent_class)->size_allocate (widget, allocation);

606
607
608
609
610
	gtk_widget_set_allocation (widget, allocation);
	spacing = gtk_grid_get_column_spacing (GTK_GRID (widget));
	rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);

	/* take some leading space for the image, which we always make square */
611
	if (RB_HEADER (widget)->priv->show_album_art) {
612
613
614
615
616
617
618
619
620
621
622
623
		image_width = allocation->height;
		if (rtl) {
			child_alloc.x = allocation->x + allocation->width - image_width;
		} else {
			child_alloc.x = allocation->x;
			allocation->x += image_width + spacing;
		}
		allocation->width -= image_width + spacing;
		child_alloc.y = allocation->y;
		child_alloc.width = image_width;
		child_alloc.height = allocation->height;
		gtk_widget_size_allocate (RB_HEADER (widget)->priv->image, &child_alloc);
624
625
	} else {
		image_width = 0;
626
627
	}

628
629
	/* allocate space for the volume button at the end */
	gtk_widget_get_preferred_width (RB_HEADER (widget)->priv->volume_button, &volume_width, NULL);
630
631
632
633
634
635
	if (rtl) {
		child_alloc.x = allocation->x;
		allocation->x += volume_width + spacing;
	} else {
		child_alloc.x = (allocation->x + allocation->width) - volume_width;
	}
636
637
638
639
640
641
	child_alloc.y = allocation->y;
	child_alloc.width = volume_width;
	child_alloc.height = allocation->height;
	allocation->width -= volume_width + spacing;
	gtk_widget_size_allocate (RB_HEADER (widget)->priv->volume_button, &child_alloc);

642
643
644
645
	/* figure out how much space to allocate to the scale.
	 * it gets at least its minimum size, at most 1/3 of the
	 * space we have.
	 */
646
	if (RB_HEADER (widget)->priv->show_slider) {
647
648
649
650
		gtk_widget_get_preferred_width (RB_HEADER (widget)->priv->scale, &scale_width, NULL);
		if (scale_width < allocation->width / 3)
			scale_width = allocation->width / 3;

651
652
653
654
655
656
657
658
659
660
661
662
663
664
		if (scale_width + image_width > allocation->width)
			scale_width = allocation->width - image_width;

		if (scale_width > 0) {
			if (rtl) {
				child_alloc.x = allocation->x;
			} else {
				child_alloc.x = allocation->x + (allocation->width - scale_width) + spacing;
			}
			child_alloc.y = allocation->y;
			child_alloc.width = scale_width - spacing;
			child_alloc.height = allocation->height;
			gtk_widget_show (RB_HEADER (widget)->priv->scale);
			gtk_widget_size_allocate (RB_HEADER (widget)->priv->scale, &child_alloc);
665
		} else {
666
			gtk_widget_hide (RB_HEADER (widget)->priv->scale);
667
668
669
670
671
672
673
		}
	} else {
		scale_width = 0;
	}

	/* time button gets its minimum size */
	gtk_widget_get_preferred_width (RB_HEADER (widget)->priv->songbox, NULL, &info_width);
674
675
676
677
678
	if (gtk_widget_get_visible (RB_HEADER (widget)->priv->timelabel)) {
		gtk_widget_get_preferred_width (RB_HEADER (widget)->priv->timebutton, &time_width, NULL);
	} else {
		time_width = 0;
	}
679
680
681
682
683
684
685
686
687

	info_width = allocation->width - (scale_width + time_width) - (2 * spacing);

	if (rtl) {
		child_alloc.x = allocation->x + allocation->width - info_width;
	} else {
		child_alloc.x = allocation->x;
	}

688
689
690
691
692
693
694
695
696
697
698
	if (info_width > 0) {
		child_alloc.y = allocation->y;
		child_alloc.width = info_width;
		child_alloc.height = allocation->height;
		gtk_widget_show (RB_HEADER (widget)->priv->songbox);
		gtk_widget_size_allocate (RB_HEADER (widget)->priv->songbox, &child_alloc);
	} else {
		gtk_widget_hide (RB_HEADER (widget)->priv->songbox);
		info_width = 0;
	}

699
700
701
	if (time_width == 0) {
		gtk_widget_hide (RB_HEADER (widget)->priv->timebutton);
	} else if (info_width + scale_width + (2 * spacing) + time_width > allocation->width) {
702
		gtk_widget_hide (RB_HEADER (widget)->priv->timebutton);
703
	} else {
704
705
706
707
708
709
710
711
712
713
		if (rtl) {
			child_alloc.x = allocation->x + scale_width + spacing;
		} else {
			child_alloc.x = allocation->x + info_width + spacing;
		}
		child_alloc.y = allocation->y;
		child_alloc.width = time_width;
		child_alloc.height = allocation->height;
		gtk_widget_show (RB_HEADER (widget)->priv->timebutton);
		gtk_widget_size_allocate (RB_HEADER (widget)->priv->timebutton, &child_alloc);
714
	}
715
716
}

717
static void
718
rb_header_set_property (GObject *object,
719
720
721
722
			guint prop_id,
			const GValue *value,
			GParamSpec *pspec)
{
723
	RBHeader *header = RB_HEADER (object);
724
725
726

	switch (prop_id) {
	case PROP_DB:
727
		header->priv->db = g_value_get_object (value);
728
729
730
731
		g_signal_connect_object (header->priv->db,
					 "entry-extra-metadata-notify",
					 G_CALLBACK (rb_header_extra_metadata_cb),
					 header, 0);
732
		break;
733
734
	case PROP_SHELL_PLAYER:
		header->priv->shell_player = g_value_get_object (value);
735
736
737
738
739
740
741
742
		g_signal_connect_object (header->priv->shell_player,
					 "elapsed-nano-changed",
					 G_CALLBACK (rb_header_elapsed_changed_cb),
					 header, 0);
		g_signal_connect_object (header->priv->shell_player,
					 "playing-song-changed",
					 G_CALLBACK (rb_header_playing_song_changed_cb),
					 header, 0);
743
		break;
744
745
746
	case PROP_SEEKABLE:
		header->priv->seekable = g_value_get_boolean (value);
		break;
747
748
749
750
751
	case PROP_SHOW_REMAINING:
		header->priv->show_remaining = g_value_get_boolean (value);
		rb_header_update_elapsed (header);
		break;
	case PROP_SHOW_POSITION_SLIDER:
752
753
		header->priv->show_slider = g_value_get_boolean (value);
		gtk_widget_set_visible (header->priv->scale, header->priv->show_slider);
754
755
		break;
	case PROP_SHOW_ALBUM_ART:
756
757
		header->priv->show_album_art = g_value_get_boolean (value);
		gtk_widget_set_visible (header->priv->image, header->priv->show_album_art);
758
		break;
759
760
761
762
763
764
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

765
static void
766
rb_header_get_property (GObject *object,
767
768
769
770
			guint prop_id,
			GValue *value,
			GParamSpec *pspec)
{
771
	RBHeader *header = RB_HEADER (object);
772
773
774

	switch (prop_id) {
	case PROP_DB:
775
		g_value_set_object (value, header->priv->db);
776
		break;
777
778
	case PROP_SHELL_PLAYER:
		g_value_set_object (value, header->priv->shell_player);
779
		break;
780
781
782
	case PROP_SEEKABLE:
		g_value_set_boolean (value, header->priv->seekable);
		break;
783
784
785
	case PROP_SLIDER_DRAGGING:
		g_value_set_boolean (value, header->priv->slider_dragging);
		break;
786
787
788
789
	case PROP_SHOW_REMAINING:
		g_value_set_boolean (value, header->priv->show_remaining);
		break;
	case PROP_SHOW_POSITION_SLIDER:
790
		g_value_set_boolean (value, header->priv->show_slider);
791
792
		break;
	case PROP_SHOW_ALBUM_ART:
793
		g_value_set_boolean (value, header->priv->show_album_art);
794
		break;
795
796
797
798
799
800
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

801
802
803
804
805
/**
 * rb_header_new:
 * @shell_player: the #RBShellPlayer instance
 * @db: the #RhythmDB instance
 *
Jonathan Matthew's avatar
Jonathan Matthew committed
806
807
 * Creates a new header widget.
 *
808
809
 * Return value: the header widget
 */
810
RBHeader *
811
rb_header_new (RBShellPlayer *shell_player, RhythmDB *db)
812
{
813
	RBHeader *header;
814

815
816
	header = RB_HEADER (g_object_new (RB_TYPE_HEADER,
					  "shell-player", shell_player,
817
					  "db", db,
818
					  NULL));
819

820
	g_return_val_if_fail (header->priv != NULL, NULL);
821

822
	return header;
823
824
}

825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
static void
get_extra_metadata (RhythmDB *db, RhythmDBEntry *entry, const char *field, char **value)
{
	GValue *v;

	v = rhythmdb_entry_request_extra_metadata (db, entry, field);
	if (v != NULL) {
		*value = g_value_dup_string (v);
		g_value_unset (v);
		g_free (v);
	} else {
		*value = NULL;
	}
}

840
static void
841
rb_header_sync (RBHeader *header)
842
{
843
	if (header->priv->entry != NULL) {
844
		const char *title;
845
		const char *album;
846
		const char *artist;
847
		const char *stream_name = NULL;
848
849
850
		char *streaming_title;
		char *streaming_artist;
		char *streaming_album;
851
		char *t;
852
		PangoDirection widget_dir;
853

854
855
		rb_debug ("syncing with %s",
			  rhythmdb_entry_get_string (header->priv->entry, RHYTHMDB_PROP_LOCATION));
856
		gboolean have_duration = (header->priv->duration > 0);
857

858
859
860
861
862
863
		if (gtk_widget_get_parent (header->priv->song) == NULL) {
			gtk_container_remove (GTK_CONTAINER (header->priv->songbox), header->priv->not_playing);
			gtk_container_add (GTK_CONTAINER (header->priv->songbox), header->priv->song);
			gtk_container_add (GTK_CONTAINER (header->priv->songbox), header->priv->details);
		}

864
		title = rhythmdb_entry_get_string (header->priv->entry, RHYTHMDB_PROP_TITLE);
865
866
		album = rhythmdb_entry_get_string (header->priv->entry, RHYTHMDB_PROP_ALBUM);
		artist = rhythmdb_entry_get_string (header->priv->entry, RHYTHMDB_PROP_ARTIST);
867

868
869
870
871
872
		get_extra_metadata (header->priv->db,
				    header->priv->entry,
				    RHYTHMDB_PROP_STREAM_SONG_TITLE,
				    &streaming_title);
		if (streaming_title) {
873
874
875
			/* use entry title as stream name */
			stream_name = title;
			title = streaming_title;
876
877
		}

878
879
880
881
882
		get_extra_metadata (header->priv->db,
				    header->priv->entry,
				    RHYTHMDB_PROP_STREAM_SONG_ARTIST,
				    &streaming_artist);
		if (streaming_artist) {
883
884
885
886
			/* override artist from entry */
			artist = streaming_artist;
		}

887
888
889
890
891
892
893
894
895
		get_extra_metadata (header->priv->db,
				    header->priv->entry,
				    RHYTHMDB_PROP_STREAM_SONG_ALBUM,
				    &streaming_album);
		if (streaming_album) {
			/* override album from entry */
			album = streaming_album;
		}

896
897
		widget_dir = (gtk_widget_get_direction (GTK_WIDGET (header->priv->song)) == GTK_TEXT_DIR_LTR) ?
			     PANGO_DIRECTION_LTR : PANGO_DIRECTION_RTL;
898

899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
		t = rb_text_cat (widget_dir, title, TITLE_FORMAT, NULL);
		gtk_label_set_markup (GTK_LABEL (header->priv->song), t);
		g_free (t);

		if (artist == NULL || artist[0] == '\0') {	/* this is crap; should be up to the entry type */
			if (stream_name != NULL) {
				t = rb_text_cat (widget_dir, stream_name, STREAM_FORMAT, NULL);
				gtk_label_set_markup (GTK_LABEL (header->priv->details), t);
				g_free (t);
			} else {
				gtk_label_set_markup (GTK_LABEL (header->priv->details), "");
			}
		} else {
			const char *by;
			const char *from;
			PangoDirection dir;
			PangoDirection native;

			native = PANGO_DIRECTION_LTR;
			if (gtk_widget_get_direction (GTK_WIDGET (header->priv->details)) != GTK_TEXT_DIR_LTR) {
				native = PANGO_DIRECTION_RTL;
			}

			dir = rb_text_common_direction (artist, album, NULL);
			if (!rb_text_direction_conflict (dir, native)) {
				dir = native;
				by = _("by");
				from = _("from");
			} else {
				by = UNICODE_MIDDLE_DOT;
				from = UNICODE_MIDDLE_DOT;
			}

932
933
934
935
936
937
938
939
940
941
942
943
944
			if (album != NULL && album[0] != '\0') {
				t = rb_text_cat (dir,
						 by, "%s",
						 artist, ARTIST_FORMAT,
						 from, "%s",
						 album, ALBUM_FORMAT,
						 NULL);
			} else {
				t = rb_text_cat (dir,
						 by, "%s",
						 artist, ARTIST_FORMAT,
						 NULL);
			}
945
946
947
			gtk_label_set_markup (GTK_LABEL (header->priv->details), t);
			g_free (t);
		}
948

949
950
951
952
953
954
955
956
957
958
959
960
961
962
		if (header->priv->playing_source) {
			char *text = NULL;
			float progress = 0.0;

			rb_source_get_playback_status (header->priv->playing_source, &text, &progress);
			if (text) {
				header->priv->showing_playback_status = TRUE;
				gtk_widget_show (header->priv->timelabel);
				gtk_widget_show (header->priv->timebutton);
				gtk_label_set_text (GTK_LABEL (header->priv->timelabel), text);
				g_free (text);
			} else {
				header->priv->showing_playback_status = FALSE;
			}
963
			gtk_range_set_show_fill_level (GTK_RANGE (header->priv->scale), (text != NULL));
964
965
966
967
968
969

			progress = progress * gtk_adjustment_get_upper (header->priv->adjustment);
			gtk_range_set_fill_level (GTK_RANGE (header->priv->scale), progress);
		}


970
971
		gtk_widget_set_sensitive (header->priv->scale, have_duration && header->priv->seekable);
		rb_header_sync_time (header);
972
973
974
975

		g_free (streaming_artist);
		g_free (streaming_album);
		g_free (streaming_title);
976
977
	} else {
		rb_debug ("not playing");
978
979
980
981
982
		if (gtk_widget_get_parent (header->priv->not_playing) == NULL) {
			gtk_container_remove (GTK_CONTAINER (header->priv->songbox), header->priv->song);
			gtk_container_remove (GTK_CONTAINER (header->priv->songbox), header->priv->details);
			gtk_container_add (GTK_CONTAINER (header->priv->songbox), header->priv->not_playing);
		}
983

984
		rb_header_sync_time (header);
985
		gtk_widget_set_sensitive (header->priv->scale, FALSE);
986
	}
987
988
}

989
static void
990
rb_header_sync_time (RBHeader *header)
991
{
992
	if (header->priv->shell_player == NULL)
993
		return;
994

995
	if (header->priv->slider_dragging == TRUE) {
996
		rb_debug ("slider is dragging, not syncing");
997
		return;
998
	}
999

1000
	if (header->priv->duration > 0) {
1001
		double progress = ((double) header->priv->elapsed_time) / RB_PLAYER_SECOND;
1002

1003
		header->priv->slider_locked = TRUE;
1004
1005

		g_object_freeze_notify (G_OBJECT (header->priv->adjustment));
1006
		gtk_adjustment_set_value (header->priv->adjustment, progress);
1007
1008
1009
		gtk_adjustment_set_upper (header->priv->adjustment, header->priv->duration);
		g_object_thaw_notify (G_OBJECT (header->priv->adjustment));

1010
		header->priv->slider_locked = FALSE;
1011
		gtk_widget_set_sensitive (header->priv->scale, header->priv->seekable);
1012
	} else {
1013
		header->priv->slider_locked = TRUE;
1014
1015

		g_object_freeze_notify (G_OBJECT (header->priv->adjustment));
1016
		gtk_adjustment_set_value (header->priv->adjustment, 0.0);
1017
		gtk_adjustment_set_upper (header->priv->adjustment, 1.0);
1018
1019
		g_object_thaw_notify (G_OBJECT (header->priv->adjustment));

1020
1021
		header->priv->slider_locked = FALSE;
		gtk_widget_set_sensitive (header->priv->scale, FALSE);
1022
1023
	}

1024
	rb_header_update_elapsed (header);
1025
1026
1027
1028
1029
}

static gboolean
slider_press_callback (GtkWidget *widget,
		       GdkEventButton *event,
1030
		       RBHeader *header)
1031
{
1032
1033
	int height;

1034
	header->priv->slider_dragging = TRUE;
1035
	header->priv->slider_drag_moved = FALSE;
1036
	header->priv->latest_set_time = -1;
1037
1038
	g_object_notify (G_OBJECT (header), "slider-dragging");

1039
	/* hack: pretend the trough is at least 20 pixels high */
1040
1041
1042
	height = gtk_widget_get_allocated_height (widget);
	if (abs (event->y - (height / 2)) < 10)
		event->y = height / 2;
1043

1044
1045
1046
1047
	return FALSE;
}

static gboolean
1048
slider_moved_timeout (RBHeader *header)
1049
{
1050
	apply_slider_position (header);
1051
	header->priv->slider_moved_timeout = 0;
1052
	header->priv->slider_drag_moved = FALSE;
1053
1054
1055
1056
1057
1058
	return FALSE;
}

static gboolean
slider_moved_callback (GtkWidget *widget,
		       GdkEventMotion *event,
1059
		       RBHeader *header)
1060
1061
1062
{
	double progress;

1063
	if (header->priv->slider_dragging == FALSE) {
1064
		rb_debug ("slider is not dragging");
1065
		return FALSE;
1066
	}
1067
	header->priv->slider_drag_moved = TRUE;
1068

1069
	progress = gtk_adjustment_get_value (header->priv->adjustment);
1070
	header->priv->elapsed_time = (gint64) ((progress+0.5) * RB_PLAYER_SECOND);
1071

1072
	rb_header_update_elapsed (header);
1073

1074
	if (header->priv->slider_moved_timeout != 0) {
1075
		rb_debug ("removing old timer");
1076
1077
		g_source_remove (header->priv->slider_moved_timeout);
		header->priv->slider_moved_timeout = 0;
1078
	}
1079
1080
	header->priv->slider_moved_timeout =
		g_timeout_add (40, (GSourceFunc) slider_moved_timeout, header);
1081

1082
1083
1084
	return FALSE;
}

1085
1086
static void