1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
2  *
3  *  Copyright (C) 2002 Jorn Baayen <jorn@nl.linux.org>
4  *  Copyright (C) 2003 Colin Walters <walters@gnome.org>
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; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  The Rhythmbox authors hereby grant permission for non-GPL compatible
12  *  GStreamer plugins to be used and distributed together with GStreamer
13  *  and Rhythmbox. This permission is above and beyond the permissions granted
14  *  by the GPL license by which Rhythmbox is covered. If you modify this code
15  *  you may extend this exception to your version of the code, but you are not
16  *  obligated to do so. If you do not wish to do so, delete this exception
17  *  statement from your version.
18  *
19  *  This program is distributed in the hope that it will be useful,
20  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
21  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  *  GNU General Public License for more details.
23  *
24  *  You should have received a copy of the GNU General Public License
25  *  along with this program; if not, write to the Free Software
26  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA.
27  *
28  */
29 
30 #include "config.h"
31 
32 #include <time.h>
33 #include <string.h>
34 
35 #include <glib/gi18n.h>
36 #include <gtk/gtk.h>
37 
38 #include "rb-source.h"
39 #include "rb-cut-and-paste-code.h"
40 #include "rb-debug.h"
41 #include "rb-dialog.h"
42 #include "rb-shell.h"
43 #include "rb-source.h"
44 #include "rb-util.h"
45 #include "rb-static-playlist-source.h"
46 #include "rb-play-order.h"
47 
48 static void rb_source_class_init (RBSourceClass *klass);
49 static void rb_source_init (RBSource *source);
50 static void rb_source_dispose (GObject *object);
51 static void rb_source_finalize (GObject *object);
52 static void rb_source_set_property (GObject *object,
53 					guint prop_id,
54 					const GValue *value,
55 					GParamSpec *pspec);
56 static void rb_source_get_property (GObject *object,
57 					guint prop_id,
58 					GValue *value,
59 					GParamSpec *pspec);
60 
61 static void default_get_status (RBDisplayPage *page, char **text, gboolean *busy);
62 static void default_activate (RBDisplayPage *page);
63 static GList *default_get_property_views (RBSource *source);
64 static gboolean default_can_rename (RBSource *source);
65 static GList *default_copy (RBSource *source);
66 static void default_reset_filters (RBSource *source);
67 static gboolean default_try_playlist (RBSource *source);
68 static RBSourceEOFType default_handle_eos (RBSource *source);
69 static RBEntryView *default_get_entry_view (RBSource *source);
70 static void default_add_to_queue (RBSource *source, RBSource *queue);
71 static void default_move_to_trash (RBSource *source);
72 static char *default_get_delete_label (RBSource *source);
73 
74 static void rb_source_status_changed_cb (RBDisplayPage *page);
75 static void rb_source_post_entry_deleted_cb (GtkTreeModel *model,
76 					     RhythmDBEntry *entry,
77 					     RBSource *source);
78 static void rb_source_row_inserted_cb (GtkTreeModel *model,
79 				       GtkTreePath *path,
80 				       GtkTreeIter *iter,
81 				       RBSource *source);
82 
83 G_DEFINE_ABSTRACT_TYPE (RBSource, rb_source, RB_TYPE_DISPLAY_PAGE)
84 
85 /**
86  * SECTION:rb-source
87  * @short_description: base class for sources
88  *
89  * This class provides methods for requesting information
90  * about the UI capabilities of the source, and defines the
91  * expectations that apply to all sources - that they will
92  * provide #RBEntryView and #RhythmDBQueryModel objects, mostly.
93  *
94  * Many of the methods on this class come in can_do_x and do_x
95  * pairs.  When can_do_x always returns FALSE, the class does not
96  * need to implement the do_x method.
97  *
98  * Useful subclasses include #RBBrowserSource, which includes a #RBLibraryBrowser
99  * and takes care of constructing an #RBEntryView too; #RBRemovableMediaSource,
100  * which takes care of many aspects of implementing a source that represents a
101  * removable device; and #RBPlaylistSource, which provides functionality for
102  * playlist-like sources.
103  */
104 
105 struct _RBSourcePrivate
106 {
107 	RhythmDBQueryModel *query_model;
108 	guint hidden_when_empty : 1;
109 	guint update_visibility_id;
110 	guint update_status_id;
111 	guint status_changed_idle_id;
112 	RhythmDBEntryType *entry_type;
113 	RBSourceLoadStatus load_status;
114 
115 	GSettings *settings;
116 
117 	GMenu *toolbar_menu;
118 	GMenuModel *playlist_menu;
119 };
120 
121 enum
122 {
123 	PROP_0,
124 	PROP_QUERY_MODEL,
125 	PROP_HIDDEN_WHEN_EMPTY,
126 	PROP_ENTRY_TYPE,
127 	PROP_BASE_QUERY_MODEL,
128 	PROP_PLAY_ORDER,
129 	PROP_SETTINGS,
130 	PROP_SHOW_BROWSER,
131 	PROP_LOAD_STATUS,
132 	PROP_TOOLBAR_MENU,
133 	PROP_PLAYLIST_MENU
134 };
135 
136 enum
137 {
138 	FILTER_CHANGED,
139 	RESET_FILTERS,
140 	PLAYBACK_STATUS_CHANGED,
141 	LAST_SIGNAL
142 };
143 
144 static guint rb_source_signals[LAST_SIGNAL] = { 0 };
145 
146 static void
rb_source_class_init(RBSourceClass * klass)147 rb_source_class_init (RBSourceClass *klass)
148 {
149 	GObjectClass *object_class = G_OBJECT_CLASS (klass);
150 	RBDisplayPageClass *page_class = RB_DISPLAY_PAGE_CLASS (klass);
151 
152 	object_class->dispose = rb_source_dispose;
153 	object_class->finalize = rb_source_finalize;
154 	object_class->set_property = rb_source_set_property;
155 	object_class->get_property = rb_source_get_property;
156 
157 	page_class->activate = default_activate;
158 	page_class->get_status = default_get_status;
159 	page_class->status_changed = rb_source_status_changed_cb;
160 
161 	klass->reset_filters = default_reset_filters;
162 	klass->get_property_views = default_get_property_views;
163 	klass->can_rename = default_can_rename;
164 	klass->can_cut = (RBSourceFeatureFunc) rb_false_function;
165 	klass->can_paste = (RBSourceFeatureFunc) rb_false_function;
166 	klass->can_delete = (RBSourceFeatureFunc) rb_false_function;
167 	klass->can_copy = (RBSourceFeatureFunc) rb_false_function;
168 	klass->can_add_to_queue = (RBSourceFeatureFunc) rb_false_function;
169 	klass->can_move_to_trash = (RBSourceFeatureFunc) rb_false_function;
170 	klass->can_pause = (RBSourceFeatureFunc) rb_true_function;
171 	klass->get_entry_view = default_get_entry_view;
172 	klass->copy = default_copy;
173 	klass->handle_eos = default_handle_eos;
174 	klass->try_playlist = default_try_playlist;
175 	klass->add_to_queue = default_add_to_queue;
176 	klass->get_delete_label = default_get_delete_label;
177 	klass->move_to_trash = default_move_to_trash;
178 
179 	/**
180 	 * RBSource:hidden-when-empty:
181 	 *
182 	 * If TRUE, the source will not be displayed in the source list
183 	 * when it contains no entries.
184 	 */
185 	g_object_class_install_property (object_class,
186 					 PROP_HIDDEN_WHEN_EMPTY,
187 					 g_param_spec_boolean ("hidden-when-empty",
188 							       "hidden-when-empty",
189 							       "Whether the source should be displayed in the source list when it is empty",
190 							       FALSE,
191 							       G_PARAM_READWRITE));
192 
193 	/**
194 	 * RBSource:query-model:
195 	 *
196 	 * The current query model for the source.  This is used in
197 	 * various places, including the play order, to find the
198 	 * set of entries within the source.
199 	 */
200 	g_object_class_install_property (object_class,
201 					 PROP_QUERY_MODEL,
202 					 g_param_spec_object ("query-model",
203 							      "RhythmDBQueryModel",
204 							      "RhythmDBQueryModel object",
205 							      RHYTHMDB_TYPE_QUERY_MODEL,
206 							      G_PARAM_READWRITE));
207 	/**
208 	 * RBSource:entry-type:
209 	 *
210 	 * Entry type for entries in this source.
211 	 */
212 	g_object_class_install_property (object_class,
213 					 PROP_ENTRY_TYPE,
214 					 g_param_spec_object ("entry-type",
215 							      "Entry type",
216 							      "Type of the entries which should be displayed by this source",
217 							      RHYTHMDB_TYPE_ENTRY_TYPE,
218 							      G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
219 	/**
220 	 * RBSource:base-query-model:
221 	 *
222 	 * The unfiltered query model for the source, containing all entries in the source.
223 	 * Source classes should override this if they perform filtering based on the search
224 	 * box or a browser.
225 	 */
226 	g_object_class_install_property (object_class,
227 					 PROP_BASE_QUERY_MODEL,
228 					 g_param_spec_object ("base-query-model",
229 							      "RhythmDBQueryModel",
230 							      "RhythmDBQueryModel object (unfiltered)",
231 							      RHYTHMDB_TYPE_QUERY_MODEL,
232 							      G_PARAM_READABLE));
233 	/**
234 	 * RBSource:play-order:
235 	 *
236 	 * If the source provides its own play order, it can override this property.
237 	 */
238 	g_object_class_install_property (object_class,
239 					 PROP_PLAY_ORDER,
240 					 g_param_spec_object ("play-order",
241 							      "play order",
242 							      "optional play order specific to the source",
243 							      RB_TYPE_PLAY_ORDER,
244 							      G_PARAM_READABLE));
245 
246 	/**
247 	 * RBSource:load-status:
248 	 *
249 	 * Indicates whether the source is not loaded, is currently loading data, or is
250 	 * fully loaded.
251 	 */
252 	g_object_class_install_property (object_class,
253 					 PROP_LOAD_STATUS,
254 					 g_param_spec_enum ("load-status",
255 							    "load-status",
256 							    "load status",
257 							    RB_TYPE_SOURCE_LOAD_STATUS,
258 							    RB_SOURCE_LOAD_STATUS_LOADED,
259 							    G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
260 
261 	/**
262 	 * RBSource:settings:
263 	 *
264 	 * The #GSettings instance storing settings for the source.  The instance must
265 	 * have a schema of org.gnome.Rhythmbox.Source.
266 	 */
267 	g_object_class_install_property (object_class,
268 					 PROP_SETTINGS,
269 					 g_param_spec_object ("settings",
270 							      "settings",
271 							      "GSettings instance",
272 							      G_TYPE_SETTINGS,
273 							      G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
274 	/**
275 	 * RBSource:show-browser:
276 	 *
277 	 * Whether the browser widget for the source (if any) should be displayed.
278 	 * This should be overridden in sources that include a browser widget.
279 	 */
280 	g_object_class_install_property (object_class,
281 					 PROP_SHOW_BROWSER,
282 					 g_param_spec_boolean ("show-browser",
283 							       "show browser",
284 							       "whether the browser widget should be shown",
285 							       TRUE,
286 							       G_PARAM_READWRITE));
287 	/**
288 	 * RBSource:toolbar-menu:
289 	 *
290 	 * A GMenu instance describing the contents of a toolbar to display at
291 	 * the top of the source.  The #RBSource class doesn't actually display
292 	 * the toolbar anywhere.  Adding the toolbar to a container is the
293 	 * responsibility of a subclass such as #RBBrowserSource.
294 	 */
295 	g_object_class_install_property (object_class,
296 					 PROP_TOOLBAR_MENU,
297 					 g_param_spec_object ("toolbar-menu",
298 							      "toolbar menu",
299 							      "toolbar menu",
300 							      G_TYPE_MENU_MODEL,
301 							      G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
302 
303 	/**
304 	 * RBSource:playlist-menu:
305 	 *
306 	 * A GMenu instance to attach to the 'add to playlist' item in the edit menu.
307 	 * If NULL, the item will be disabled.
308 	 */
309 	g_object_class_install_property (object_class,
310 					 PROP_PLAYLIST_MENU,
311 					 g_param_spec_object ("playlist-menu",
312 							      "playlist menu",
313 							      "playlist menu",
314 							      G_TYPE_MENU_MODEL,
315 							      G_PARAM_READWRITE));
316 
317 	/**
318 	 * RBSource::filter-changed:
319 	 * @source: the #RBSource
320 	 *
321 	 * Fires when the user changes the filter, either by changing the
322 	 * contents of the search box or by selecting a different browser
323 	 * entry.
324 	 */
325 	rb_source_signals[FILTER_CHANGED] =
326 		g_signal_new ("filter_changed",
327 			      RB_TYPE_SOURCE,
328 			      G_SIGNAL_RUN_LAST,
329 			      G_STRUCT_OFFSET (RBSourceClass, filter_changed),
330 			      NULL, NULL,
331 			      NULL,
332 			      G_TYPE_NONE,
333 			      0);
334 	/**
335 	 * RBSource::reset-filters:
336 	 * @source: the #RBSource
337 	 *
338 	 * Action signal used to reset the source's filters.
339 	 */
340 	rb_source_signals[RESET_FILTERS] =
341 		g_signal_new ("reset-filters",
342 			      RB_TYPE_SOURCE,
343 			      G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
344 			      G_STRUCT_OFFSET (RBSourceClass, reset_filters),
345 			      NULL, NULL,
346 			      NULL,
347 			      G_TYPE_NONE,
348 			      0);
349 	/**
350 	 * RBSource::playback-status-changed:
351 	 * @source: the #RBSource
352 	 *
353 	 * Emitted to indicate playback status (buffering etc.) has changed
354 	 */
355 	rb_source_signals[PLAYBACK_STATUS_CHANGED] =
356 		g_signal_new ("playback-status-changed",
357 			      RB_TYPE_SOURCE,
358 			      G_SIGNAL_RUN_LAST,
359 			      0,
360 			      NULL, NULL,
361 			      NULL,
362 			      G_TYPE_NONE,
363 			      0);
364 
365 	g_type_class_add_private (object_class, sizeof (RBSourcePrivate));
366 }
367 
368 static void
rb_source_init(RBSource * source)369 rb_source_init (RBSource *source)
370 {
371 	source->priv = G_TYPE_INSTANCE_GET_PRIVATE (source, RB_TYPE_SOURCE, RBSourcePrivate);
372 }
373 
374 static void
rb_source_dispose(GObject * object)375 rb_source_dispose (GObject *object)
376 {
377 	RBSource *source;
378 
379 	g_return_if_fail (object != NULL);
380 	g_return_if_fail (RB_IS_SOURCE (object));
381 
382 	source = RB_SOURCE (object);
383 
384 	if (source->priv->update_visibility_id != 0) {
385 		g_source_remove (source->priv->update_visibility_id);
386 		source->priv->update_visibility_id = 0;
387 	}
388 	if (source->priv->update_status_id != 0) {
389 		g_source_remove (source->priv->update_status_id);
390 		source->priv->update_status_id = 0;
391 	}
392 	if (source->priv->status_changed_idle_id != 0) {
393 		g_source_remove (source->priv->status_changed_idle_id);
394 		source->priv->status_changed_idle_id = 0;
395 	}
396 
397 	g_clear_object (&source->priv->settings);
398 	g_clear_object (&source->priv->toolbar_menu);
399 	g_clear_object (&source->priv->playlist_menu);
400 
401 	G_OBJECT_CLASS (rb_source_parent_class)->dispose (object);
402 }
403 
404 static void
rb_source_finalize(GObject * object)405 rb_source_finalize (GObject *object)
406 {
407 	RBSource *source;
408 
409 	g_return_if_fail (object != NULL);
410 	g_return_if_fail (RB_IS_SOURCE (object));
411 
412 	source = RB_SOURCE (object);
413 
414 	if (source->priv->query_model != NULL) {
415 		rb_debug ("Unreffing model %p count: %d",
416 			  source->priv->query_model,
417 			  G_OBJECT (source->priv->query_model)->ref_count);
418 		g_object_unref (source->priv->query_model);
419 	}
420 
421 	G_OBJECT_CLASS (rb_source_parent_class)->finalize (object);
422 }
423 
424 static gboolean
update_visibility_idle(RBSource * source)425 update_visibility_idle (RBSource *source)
426 {
427 	gint count;
428 
429 	count = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (source->priv->query_model), NULL);
430 	g_object_set (source, "visibility", (count > 0), NULL);
431 
432 	source->priv->update_visibility_id = 0;
433 	return FALSE;
434 }
435 
436 static void
queue_update_visibility(RBSource * source)437 queue_update_visibility (RBSource *source)
438 {
439 	if (source->priv->update_visibility_id != 0) {
440 		g_source_remove (source->priv->update_visibility_id);
441 	}
442 	source->priv->update_visibility_id = g_idle_add ((GSourceFunc) update_visibility_idle, source);
443 }
444 
445 /**
446  * rb_source_set_hidden_when_empty:
447  * @source: a #RBSource
448  * @hidden: if TRUE, automatically hide the source
449  *
450  * Enables or disables automatic hiding of the source when
451  * there are no entries in it.
452  */
453 void
rb_source_set_hidden_when_empty(RBSource * source,gboolean hidden)454 rb_source_set_hidden_when_empty (RBSource *source,
455 				 gboolean  hidden)
456 {
457 	g_return_if_fail (RB_IS_SOURCE (source));
458 
459 	if (source->priv->hidden_when_empty != hidden) {
460 		source->priv->hidden_when_empty = hidden;
461 		queue_update_visibility (source);
462 	}
463 }
464 
465 static void
rb_source_set_query_model_internal(RBSource * source,RhythmDBQueryModel * model)466 rb_source_set_query_model_internal (RBSource *source,
467 				    RhythmDBQueryModel *model)
468 {
469 	if (source->priv->query_model == model) {
470 		return;
471 	}
472 
473 	if (source->priv->query_model != NULL) {
474 		g_signal_handlers_disconnect_by_func (source->priv->query_model,
475 						      G_CALLBACK (rb_source_post_entry_deleted_cb),
476 						      source);
477 		g_signal_handlers_disconnect_by_func (source->priv->query_model,
478 						      G_CALLBACK (rb_source_row_inserted_cb),
479 						      source);
480 		g_object_unref (source->priv->query_model);
481 	}
482 
483 	source->priv->query_model = model;
484 	if (source->priv->query_model != NULL) {
485 		g_object_ref (source->priv->query_model);
486 		g_signal_connect_object (model, "post-entry-delete",
487 					 G_CALLBACK (rb_source_post_entry_deleted_cb),
488 					 source, 0);
489 		g_signal_connect_object (model, "row_inserted",
490 					 G_CALLBACK (rb_source_row_inserted_cb),
491 					 source, 0);
492 	}
493 
494 	rb_display_page_notify_status_changed (RB_DISPLAY_PAGE (source));
495 }
496 
497 static void
rb_source_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)498 rb_source_set_property (GObject *object,
499 			guint prop_id,
500 			const GValue *value,
501 			GParamSpec *pspec)
502 {
503 	RBSource *source = RB_SOURCE (object);
504 
505 	switch (prop_id) {
506 	case PROP_HIDDEN_WHEN_EMPTY:
507 		rb_source_set_hidden_when_empty (source, g_value_get_boolean (value));
508 		break;
509 	case PROP_QUERY_MODEL:
510 		rb_source_set_query_model_internal (source, g_value_get_object (value));
511 		break;
512 	case PROP_ENTRY_TYPE:
513 		source->priv->entry_type = g_value_get_object (value);
514 		break;
515 	case PROP_SETTINGS:
516 		source->priv->settings = g_value_dup_object (value);
517 		break;
518 	case PROP_SHOW_BROWSER:
519 		/* not connected to anything here */
520 		break;
521 	case PROP_LOAD_STATUS:
522 		source->priv->load_status = g_value_get_enum (value);
523 		rb_display_page_notify_status_changed (RB_DISPLAY_PAGE (source));
524 		break;
525 	case PROP_TOOLBAR_MENU:
526 		source->priv->toolbar_menu = g_value_dup_object (value);
527 		break;
528 	case PROP_PLAYLIST_MENU:
529 		source->priv->playlist_menu = g_value_dup_object (value);
530 		break;
531 	default:
532 		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
533 		break;
534 	}
535 }
536 
537 static void
rb_source_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)538 rb_source_get_property (GObject *object,
539 			guint prop_id,
540 			GValue *value,
541 			GParamSpec *pspec)
542 {
543 	RBSource *source = RB_SOURCE (object);
544 
545 	switch (prop_id) {
546 	case PROP_QUERY_MODEL:
547 		g_value_set_object (value, source->priv->query_model);
548 		break;
549 	case PROP_ENTRY_TYPE:
550 		g_value_set_object (value, source->priv->entry_type);
551 		break;
552 	case PROP_BASE_QUERY_MODEL:
553 		/* unless the subclass overrides it, just assume the
554 		 * current query model is the base model.
555 		 */
556 		g_value_set_object (value, source->priv->query_model);
557 		break;
558 	case PROP_PLAY_ORDER:
559 		g_value_set_object (value, NULL);		/* ? */
560 		break;
561 	case PROP_SETTINGS:
562 		g_value_set_object (value, source->priv->settings);
563 		break;
564 	case PROP_SHOW_BROWSER:
565 		g_value_set_boolean (value, FALSE);
566 		break;
567 	case PROP_LOAD_STATUS:
568 		g_value_set_enum (value, source->priv->load_status);
569 		break;
570 	case PROP_TOOLBAR_MENU:
571 		g_value_set_object (value, source->priv->toolbar_menu);
572 		break;
573 	case PROP_PLAYLIST_MENU:
574 		g_value_set_object (value, source->priv->playlist_menu);
575 		break;
576 	default:
577 		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
578 		break;
579 	}
580 }
581 
582 static void
default_activate(RBDisplayPage * page)583 default_activate (RBDisplayPage *page)
584 {
585 	RBShell *shell;
586 
587 	g_object_get (page, "shell", &shell, NULL);
588 	rb_shell_activate_source (shell,
589 				  RB_SOURCE (page),
590 				  RB_SHELL_ACTIVATION_ALWAYS_PLAY,
591 				  NULL);
592 }
593 
594 static void
default_get_status(RBDisplayPage * page,char ** text,gboolean * busy)595 default_get_status (RBDisplayPage *page,
596 		    char **text,
597 		    gboolean *busy)
598 {
599 	RBSource *source = RB_SOURCE (page);
600 	RBSourceLoadStatus status;
601 
602 	/* hack to get these strings marked for translation */
603 	if (0) {
604 		ngettext ("%d song", "%d songs", 0);
605 	}
606 
607 	if (source->priv->query_model) {
608 		*text = rhythmdb_query_model_compute_status_normal (source->priv->query_model,
609 								    "%d song",
610 								    "%d songs");
611 	}
612 
613 	g_object_get (source, "load-status", &status, NULL);
614 	switch (status) {
615 	case RB_SOURCE_LOAD_STATUS_WAITING:
616 	case RB_SOURCE_LOAD_STATUS_LOADING:
617 		*busy = TRUE;
618 		break;
619 	default:
620 		break;
621 	}
622 }
623 
624 /**
625  * rb_source_notify_filter_changed:
626  * @source: a #RBSource
627  *
628  * Source implementations call this when their filter state changes
629  */
630 void
rb_source_notify_filter_changed(RBSource * source)631 rb_source_notify_filter_changed (RBSource *source)
632 {
633 	g_signal_emit (G_OBJECT (source), rb_source_signals[FILTER_CHANGED], 0);
634 }
635 
636 /**
637  * rb_source_update_play_statistics:
638  * @source: a #RBSource
639  * @db: the #RhythmDB instance
640  * @entry: the #RhythmDBEntry to update
641  *
642  * Updates play count and play time statistics for a database entry.
643  * Sources containing entries that do not normally reach EOS should
644  * call this for an entry when it is no longer being played.
645  */
646 void
rb_source_update_play_statistics(RBSource * source,RhythmDB * db,RhythmDBEntry * entry)647 rb_source_update_play_statistics (RBSource *source,
648 				  RhythmDB *db,
649 				  RhythmDBEntry *entry)
650 {
651 	time_t now;
652 	gulong current_count;
653 	GValue value = { 0, };
654 
655 	g_value_init (&value, G_TYPE_ULONG);
656 
657 	current_count = rhythmdb_entry_get_ulong (entry, RHYTHMDB_PROP_PLAY_COUNT);
658 
659 	g_value_set_ulong (&value, current_count + 1);
660 
661 	/* Increment current play count */
662 	rhythmdb_entry_set (db, entry, RHYTHMDB_PROP_PLAY_COUNT, &value);
663 	g_value_unset (&value);
664 
665 	/* Reset the last played time */
666 	time (&now);
667 
668 	g_value_init (&value, G_TYPE_ULONG);
669 	g_value_set_ulong (&value, now);
670 	rhythmdb_entry_set (db, entry, RHYTHMDB_PROP_LAST_PLAYED, &value);
671 	g_value_unset (&value);
672 
673 	rhythmdb_commit (db);
674 }
675 
676 /**
677  * rb_source_get_entry_view:
678  * @source: a #RBSource
679  *
680  * Returns the entry view widget for the source.
681  *
682  * Return value: (transfer none): the #RBEntryView instance for the source
683  */
684 RBEntryView *
rb_source_get_entry_view(RBSource * source)685 rb_source_get_entry_view (RBSource *source)
686 {
687 	RBSourceClass *klass = RB_SOURCE_GET_CLASS (source);
688 
689 	return klass->get_entry_view (source);
690 }
691 
692 static GList *
default_get_property_views(RBSource * source)693 default_get_property_views (RBSource *source)
694 {
695 	return NULL;
696 }
697 
698 /**
699  * rb_source_get_property_views:
700  * @source: a #RBSource
701  *
702  * Returns a list containing the #RBPropertyView instances for the
703  * source, if any.
704  *
705  * Return value: (element-type RB.PropertyView) (transfer container): list of property views
706  */
707 GList *
rb_source_get_property_views(RBSource * source)708 rb_source_get_property_views (RBSource *source)
709 {
710 	RBSourceClass *klass = RB_SOURCE_GET_CLASS (source);
711 
712 	return klass->get_property_views (source);
713 }
714 
715 static gboolean
default_can_rename(RBSource * source)716 default_can_rename (RBSource *source)
717 {
718 	return FALSE;
719 }
720 
721 static gboolean
is_party_mode(RBSource * source)722 is_party_mode (RBSource *source)
723 {
724 	gboolean result = FALSE;
725 	RBShell *shell;
726 
727 	g_object_get (source, "shell", &shell, NULL);
728 	result = rb_shell_get_party_mode (shell);
729 	g_object_unref (shell);
730 
731 	return result;
732 }
733 
734 /**
735  * rb_source_can_rename:
736  * @source: a #RBSource.
737  *
738  * Determines whether the source can be renamed.
739  *
740  * Return value: TRUE if this source can be renamed
741  */
742 gboolean
rb_source_can_rename(RBSource * source)743 rb_source_can_rename (RBSource *source)
744 {
745 	RBSourceClass *klass = RB_SOURCE_GET_CLASS (source);
746 
747 	if (is_party_mode (source)) {
748 		return FALSE;
749 	} else {
750 		return klass->can_rename (source);
751 	}
752 }
753 
754 /**
755  * rb_source_search:
756  * @source: a #RBSource
757  * @search: (allow-none): the active #RBSourceSearch instance
758  * @cur_text: (allow-none): the current search text
759  * @new_text: the new search text
760  *
761  * Updates the source with new search text.  The source
762  * should recreate the database query that feeds into the
763  * browser (if any).
764  */
765 void
rb_source_search(RBSource * source,RBSourceSearch * search,const char * cur_text,const char * new_text)766 rb_source_search (RBSource *source,
767 		  RBSourceSearch *search,
768 		  const char *cur_text,
769 		  const char *new_text)
770 {
771 	RBSourceClass *klass = RB_SOURCE_GET_CLASS (source);
772 	g_assert (new_text != NULL);
773 
774 	if (klass->search != NULL)
775 		klass->search (source, search, cur_text, new_text);
776 }
777 
778 
779 /**
780  * rb_source_can_cut:
781  * @source: a #RBSource
782  *
783  * Determines whether the source supporst the typical cut
784  * (as in cut-and-paste) operation.
785  *
786  * Return value: TRUE if cutting is supported
787  */
788 gboolean
rb_source_can_cut(RBSource * source)789 rb_source_can_cut (RBSource *source)
790 {
791 	RBSourceClass *klass = RB_SOURCE_GET_CLASS (source);
792 
793 	return klass->can_cut (source);
794 }
795 
796 /**
797  * rb_source_can_paste:
798  * @source: a #RBSource
799  *
800  * Determines whether the source supports paste operations.
801  *
802  * Return value: TRUE if the pasting is supported
803  */
804 gboolean
rb_source_can_paste(RBSource * source)805 rb_source_can_paste (RBSource *source)
806 {
807 	RBSourceClass *klass = RB_SOURCE_GET_CLASS (source);
808 
809 	return klass->can_paste (source);
810 }
811 
812 /**
813  * rb_source_can_delete:
814  * @source: a #RBSource
815  *
816  * Determines whether the source allows the user to delete
817  * a selected set of entries.
818  *
819  * Return value: TRUE if deletion is supported
820  */
821 gboolean
rb_source_can_delete(RBSource * source)822 rb_source_can_delete (RBSource *source)
823 {
824 	RBSourceClass *klass = RB_SOURCE_GET_CLASS (source);
825 	if (is_party_mode (source)) {
826 		return FALSE;
827 	} else {
828 		return klass->can_delete (source);
829 	}
830 }
831 
832 /**
833  * rb_source_can_move_to_trash:
834  * @source: a #RBSource
835  *
836  * Determines whether the source allows the user to trash
837  * the files backing a selected set of entries.
838  *
839  * Return value: TRUE if trashing is supported
840  */
841 gboolean
rb_source_can_move_to_trash(RBSource * source)842 rb_source_can_move_to_trash (RBSource *source)
843 {
844 	RBSourceClass *klass = RB_SOURCE_GET_CLASS (source);
845 	if (is_party_mode (source)) {
846 		return FALSE;
847 	} else {
848 		return klass->can_move_to_trash (source);
849 	}
850 }
851 
852 /**
853  * rb_source_can_copy:
854  * @source: a #RBSource
855  *
856  * Determines whether the source supports the copy part
857  * of a copy-and-paste operation.
858  *
859  * Return value: TRUE if copying is supported
860  */
861 gboolean
rb_source_can_copy(RBSource * source)862 rb_source_can_copy (RBSource *source)
863 {
864 	RBSourceClass *klass = RB_SOURCE_GET_CLASS (source);
865 
866 	return klass->can_copy (source);
867 }
868 
869 /**
870  * rb_source_cut:
871  * @source: a #RBSource
872  *
873  * Removes the currently selected entries from the source and
874  * returns them so they can be pasted into another source.
875  *
876  * Return value: (element-type RB.RhythmDBEntry) (transfer full): entries cut
877  * from the source.
878  */
879 GList *
rb_source_cut(RBSource * source)880 rb_source_cut (RBSource *source)
881 {
882 	RBSourceClass *klass = RB_SOURCE_GET_CLASS (source);
883 
884 	return klass->cut (source);
885 }
886 
887 static GList *
default_copy(RBSource * source)888 default_copy (RBSource *source)
889 {
890 	RBEntryView *entry_view;
891 	entry_view = rb_source_get_entry_view (source);
892 	if (entry_view == NULL)
893 		return NULL;
894 
895 	return rb_entry_view_get_selected_entries (entry_view);
896 }
897 
898 /**
899  * rb_source_copy:
900  * @source: a #RBSource
901  *
902  * Copies the selected entries to the clipboard.
903  *
904  * Return value: (element-type RB.RhythmDBEntry) (transfer full): a list containing
905  * the currently selected entries from the source.
906  */
907 GList *
rb_source_copy(RBSource * source)908 rb_source_copy (RBSource *source)
909 {
910 	RBSourceClass *klass = RB_SOURCE_GET_CLASS (source);
911 
912 	return klass->copy (source);
913 }
914 
915 /**
916  * rb_source_paste:
917  * @source: a #RBSource
918  * @entries: (element-type RB.RhythmDBEntry): a list of #RhythmDBEntry objects to paste in
919  *
920  * Adds a list of entries previously cut or copied from another
921  * source.  If the entries are not of the type used by the source,
922  * the entries will be copied and possibly converted into an acceptable format.
923  * This can be used for transfers to and from devices and network shares.
924  *
925  * If the transfer is performed using an #RBTrackTransferBatch, the batch object
926  * is returned so the caller can monitor the transfer progress.  The caller does not
927  * own a reference on the batch object.
928  *
929  * Return value: (transfer none): the #RBTrackTransferBatch used to perform the transfer (if any)
930  */
931 RBTrackTransferBatch *
rb_source_paste(RBSource * source,GList * entries)932 rb_source_paste (RBSource *source, GList *entries)
933 {
934 	RBSourceClass *klass = RB_SOURCE_GET_CLASS (source);
935 
936 	return klass->paste (source, entries);
937 }
938 
939 /**
940  * rb_source_can_add_to_queue:
941  * @source: a #RBSource
942  *
943  * Determines whether the source can add the selected entries to
944  * the play queue.
945  *
946  * Return value: TRUE if adding to the play queue is supported
947  */
948 gboolean
rb_source_can_add_to_queue(RBSource * source)949 rb_source_can_add_to_queue (RBSource *source)
950 {
951 	RBSourceClass *klass = RB_SOURCE_GET_CLASS (source);
952 	return klass->can_add_to_queue (source);
953 }
954 
955 static void
default_add_to_queue(RBSource * source,RBSource * queue)956 default_add_to_queue (RBSource *source,
957 		      RBSource *queue)
958 {
959 	RBEntryView *songs;
960 	GList *selection;
961 	GList *iter;
962 
963 	songs = rb_source_get_entry_view (source);
964 	if (songs == NULL)
965 		return;
966 
967 	selection = rb_entry_view_get_selected_entries (songs);
968 	if (selection == NULL)
969 		return;
970 
971 	for (iter = selection; iter; iter = iter->next) {
972 		rb_static_playlist_source_add_entry (RB_STATIC_PLAYLIST_SOURCE (queue),
973 						     (RhythmDBEntry *)iter->data, -1);
974 	}
975 
976 	g_list_foreach (selection, (GFunc)rhythmdb_entry_unref, NULL);
977 	g_list_free (selection);
978 }
979 
980 /**
981  * rb_source_add_to_queue:
982  * @source: a #RBSource
983  * @queue: the #RBSource for the play queue
984  *
985  * Adds the currently selected entries to the end of the
986  * play queue.
987  */
988 void
rb_source_add_to_queue(RBSource * source,RBSource * queue)989 rb_source_add_to_queue (RBSource *source,
990 			RBSource *queue)
991 {
992 	RBSourceClass *klass = RB_SOURCE_GET_CLASS (source);
993 	klass->add_to_queue (source, queue);
994 }
995 
996 /**
997  * rb_source_delete_selected:
998  * @source: a #RBSource
999  *
1000  * Deletes the currently selected entries from the source.
1001  */
1002 void
rb_source_delete_selected(RBSource * source)1003 rb_source_delete_selected (RBSource *source)
1004 {
1005 	RBSourceClass *klass = RB_SOURCE_GET_CLASS (source);
1006 
1007 	klass->delete_selected (source);
1008 }
1009 
1010 static void
default_move_to_trash(RBSource * source)1011 default_move_to_trash (RBSource *source)
1012 {
1013 	GList *sel, *tem;
1014 	RBEntryView *entry_view;
1015 	RhythmDB *db;
1016 
1017 	g_object_get (source->priv->query_model, "db", &db, NULL);
1018 
1019 	sel = NULL;
1020 	entry_view = rb_source_get_entry_view (source);
1021 	if (entry_view != NULL) {
1022 		sel = rb_entry_view_get_selected_entries (entry_view);
1023 	}
1024 
1025 	for (tem = sel; tem != NULL; tem = tem->next) {
1026 		rhythmdb_entry_move_to_trash (db, (RhythmDBEntry *)tem->data);
1027 		rhythmdb_commit (db);
1028 	}
1029 
1030 	g_list_foreach (sel, (GFunc)rhythmdb_entry_unref, NULL);
1031 	g_list_free (sel);
1032 	g_object_unref (db);
1033 }
1034 
1035 /**
1036  * rb_source_move_to_trash:
1037  * @source: a #RBSource
1038  *
1039  * Trashes the files backing the currently selected set of entries.
1040  * In general, this should use #rhythmdb_entry_move_to_trash to
1041  * perform the actual trash operation.
1042  */
1043 void
rb_source_move_to_trash(RBSource * source)1044 rb_source_move_to_trash (RBSource *source)
1045 {
1046 	RBSourceClass *klass = RB_SOURCE_GET_CLASS (source);
1047 
1048 	klass->move_to_trash (source);
1049 }
1050 
1051 static void
default_reset_filters(RBSource * source)1052 default_reset_filters (RBSource *source)
1053 {
1054 	rb_debug ("no implementation of reset_filters for this source");
1055 }
1056 
1057 /**
1058  * rb_source_can_show_properties:
1059  * @source: a #RBSource
1060  *
1061  * Determines whether the source can display a properties
1062  * window for the currently selected entry (or set of entries)
1063  *
1064  * Return value: TRUE if showing properties is supported
1065  */
1066 gboolean
rb_source_can_show_properties(RBSource * source)1067 rb_source_can_show_properties (RBSource *source)
1068 {
1069 	RBSourceClass *klass = RB_SOURCE_GET_CLASS (source);
1070 
1071 	return (klass->song_properties != NULL);
1072 }
1073 
1074 /**
1075  * rb_source_song_properties:
1076  * @source: a #RBSource
1077  *
1078  * Displays a properties window for the currently selected entries.
1079  */
1080 void
rb_source_song_properties(RBSource * source)1081 rb_source_song_properties (RBSource *source)
1082 {
1083 	RBSourceClass *klass = RB_SOURCE_GET_CLASS (source);
1084 
1085 	g_assert (klass->song_properties);
1086 	klass->song_properties (source);
1087 }
1088 
1089 /**
1090  * rb_source_try_playlist:
1091  * @source: a #RBSource
1092  *
1093  * Determines whether playback URIs for entries in the source should
1094  * be parsed as playlists rather than just played.
1095  *
1096  * Return value: TRUE to attempt playlist parsing
1097  */
1098 gboolean
rb_source_try_playlist(RBSource * source)1099 rb_source_try_playlist (RBSource *source)
1100 {
1101 	RBSourceClass *klass = RB_SOURCE_GET_CLASS (source);
1102 
1103 	return klass->try_playlist (source);
1104 }
1105 
1106 /**
1107  * rb_source_want_uri:
1108  * @source: a #RBSource
1109  * @uri: a URI for the source to consider
1110  *
1111  * Returns an indication of how much the source wants to handle
1112  * the specified URI.  100 is the highest usual value, and should
1113  * only be used when the URI can only be associated with this source.
1114  * 0 should be used when the URI does not match the source at all.
1115  *
1116  * Return value: value from 0 to 100 indicating how much the
1117  *  source wants this URI.
1118  */
1119 guint
rb_source_want_uri(RBSource * source,const char * uri)1120 rb_source_want_uri (RBSource *source, const char *uri)
1121 {
1122 	RBSourceClass *klass = RB_SOURCE_GET_CLASS (source);
1123 	if (klass->want_uri)
1124 		return klass->want_uri (source, uri);
1125 	return 0;
1126 }
1127 
1128 /**
1129  * rb_source_uri_is_source:
1130  * @source: a #RBSource
1131  * @uri: a URI for the source to consider
1132  *
1133  * Checks if the URI matches the source itself.  A source
1134  * should return TRUE here if the URI points to the device that
1135  * the source represents, for example.
1136  *
1137  * Return value: TRUE if the URI identifies the source itself.
1138  */
1139 gboolean
rb_source_uri_is_source(RBSource * source,const char * uri)1140 rb_source_uri_is_source (RBSource *source, const char *uri)
1141 {
1142 	RBSourceClass *klass = RB_SOURCE_GET_CLASS (source);
1143 	if (klass->uri_is_source)
1144 		return klass->uri_is_source (source, uri);
1145 	return FALSE;
1146 }
1147 
1148 /**
1149  * rb_source_add_uri:
1150  * @source: a #RBSource
1151  * @uri: a URI to add
1152  * @title: theoretically, the title of the entity the URI points to
1153  * @genre: theoretically, the genre of the entity the URI points to
1154  * @callback: a callback function to call when complete
1155  * @data: data to pass to the callback
1156  * @destroy_data: function to call to destroy the callback data
1157  *
1158  * Adds an entry corresponding to the URI to the source.  The
1159  * @title and @genre parameters are not really used.
1160  */
1161 void
rb_source_add_uri(RBSource * source,const char * uri,const char * title,const char * genre,RBSourceAddCallback callback,gpointer data,GDestroyNotify destroy_data)1162 rb_source_add_uri (RBSource *source,
1163 		   const char *uri,
1164 		   const char *title,
1165 		   const char *genre,
1166 		   RBSourceAddCallback callback,
1167 		   gpointer data,
1168 		   GDestroyNotify destroy_data)
1169 {
1170 	RBSourceClass *klass = RB_SOURCE_GET_CLASS (source);
1171 	if (klass->add_uri)
1172 		klass->add_uri (source, uri, title, genre, callback, data, destroy_data);
1173 }
1174 
1175 /**
1176  * rb_source_can_pause:
1177  * @source: a #RBSource
1178  *
1179  * Determines whether playback of entries from the source can
1180  * be paused.
1181  *
1182  * Return value: TRUE if pausing is supported
1183  */
1184 gboolean
rb_source_can_pause(RBSource * source)1185 rb_source_can_pause (RBSource *source)
1186 {
1187 	RBSourceClass *klass = RB_SOURCE_GET_CLASS (source);
1188 
1189 	return klass->can_pause (source);
1190 }
1191 
1192 static gboolean
default_try_playlist(RBSource * source)1193 default_try_playlist (RBSource *source)
1194 {
1195 	return FALSE;
1196 }
1197 
1198 static RBSourceEOFType
default_handle_eos(RBSource * source)1199 default_handle_eos (RBSource *source)
1200 {
1201 	return RB_SOURCE_EOF_NEXT;
1202 }
1203 
1204 /**
1205  * rb_source_handle_eos:
1206  * @source: a #RBSource
1207  *
1208  * Determines how EOS events should be handled when playing entries
1209  * from the source.
1210  *
1211  * Return value: EOS event handling type
1212  */
1213 RBSourceEOFType
rb_source_handle_eos(RBSource * source)1214 rb_source_handle_eos (RBSource *source)
1215 {
1216 	RBSourceClass *klass = RB_SOURCE_GET_CLASS (source);
1217 
1218 	return klass->handle_eos (source);
1219 }
1220 
1221 static RBEntryView*
default_get_entry_view(RBSource * source)1222 default_get_entry_view (RBSource *source)
1223 {
1224 	return NULL;
1225 }
1226 
1227 static char *
default_get_delete_label(RBSource * source)1228 default_get_delete_label (RBSource *source)
1229 {
1230 	return g_strdup (_("Remove"));
1231 }
1232 
1233 /**
1234  * rb_source_get_delete_label:
1235  * @source: a #RBSource
1236  *
1237  * Returns a translated label for the 'delete' menu item, allowing
1238  * sources to better describe what happens to deleted entries.
1239  * Playlists, for example, return "Remove from Playlist" here.
1240  *
1241  * Return value: allocated string holding the label string
1242  */
1243 char *
rb_source_get_delete_label(RBSource * source)1244 rb_source_get_delete_label (RBSource *source)
1245 {
1246 	RBSourceClass *klass = RB_SOURCE_GET_CLASS (source);
1247 	return klass->get_delete_label (source);
1248 }
1249 
1250 static gboolean
_update_status_idle(RBSource * source)1251 _update_status_idle (RBSource *source)
1252 {
1253 	rb_display_page_notify_status_changed (RB_DISPLAY_PAGE (source));
1254 
1255 	if (source->priv->hidden_when_empty)
1256 		update_visibility_idle (source);
1257 
1258 	source->priv->update_status_id = 0;
1259 	return FALSE;
1260 }
1261 
1262 static void
rb_source_row_inserted_cb(GtkTreeModel * model,GtkTreePath * path,GtkTreeIter * iter,RBSource * source)1263 rb_source_row_inserted_cb (GtkTreeModel *model,
1264 			   GtkTreePath *path,
1265 			   GtkTreeIter *iter,
1266 			   RBSource *source)
1267 {
1268 	if (source->priv->update_status_id == 0)
1269 		source->priv->update_status_id = g_idle_add ((GSourceFunc)_update_status_idle, source);
1270 }
1271 
1272 static void
rb_source_post_entry_deleted_cb(GtkTreeModel * model,RhythmDBEntry * entry,RBSource * source)1273 rb_source_post_entry_deleted_cb (GtkTreeModel *model,
1274 				 RhythmDBEntry *entry,
1275 				 RBSource *source)
1276 {
1277 	if (source->priv->update_status_id == 0)
1278 		source->priv->update_status_id = g_idle_add ((GSourceFunc)_update_status_idle, source);
1279 }
1280 
1281 static void
rb_source_gather_hash_keys(char * key,gpointer unused,GList ** data)1282 rb_source_gather_hash_keys (char *key,
1283 			    gpointer unused,
1284 			    GList **data)
1285 {
1286 	*data = g_list_prepend (*data, key);
1287 }
1288 
1289 /**
1290  * rb_source_gather_selected_properties:
1291  * @source: a #RBSource
1292  * @prop: property for which to gather selection
1293  *
1294  * Returns a list containing the values of the specified
1295  * property from the selected entries in the source.
1296  * This is used to implement the 'browse this artist' (etc.)
1297  * actions.
1298  *
1299  * Return value: (element-type utf8) (transfer full): list of property values
1300  */
1301 GList *
rb_source_gather_selected_properties(RBSource * source,RhythmDBPropType prop)1302 rb_source_gather_selected_properties (RBSource *source,
1303 				      RhythmDBPropType prop)
1304 {
1305 	RBEntryView *entryview;
1306 	GList *selected, *tem;
1307 	GHashTable *selected_set;
1308 
1309 	entryview = rb_source_get_entry_view (source);
1310 	if (entryview == NULL)
1311 		return NULL;
1312 
1313 	selected_set = g_hash_table_new (g_str_hash, g_str_equal);
1314 	selected = rb_entry_view_get_selected_entries (entryview);
1315 
1316 	for (tem = selected; tem; tem = tem->next) {
1317 		RhythmDBEntry *entry = tem->data;
1318 		char *val = g_strdup (rhythmdb_entry_get_string (entry, prop));
1319 		g_hash_table_insert (selected_set, val, NULL);
1320 	}
1321 
1322 	g_list_foreach (selected, (GFunc)rhythmdb_entry_unref, NULL);
1323 	g_list_free (selected);
1324 
1325 	tem = NULL;
1326 	g_hash_table_foreach (selected_set, (GHFunc) rb_source_gather_hash_keys,
1327 			      &tem);
1328 	g_hash_table_destroy (selected_set);
1329 	return tem;
1330 }
1331 
1332 /**
1333  * _rb_source_check_entry_type:
1334  * @source: a #RBSource
1335  * @entry: a #RhythmDBEntry
1336  *
1337  * Checks if a database entry matches the entry type for the source.
1338  *
1339  * Return value: %TRUE if the entry matches the source's entry type.
1340  */
1341 gboolean
_rb_source_check_entry_type(RBSource * source,RhythmDBEntry * entry)1342 _rb_source_check_entry_type (RBSource *source, RhythmDBEntry *entry)
1343 {
1344 	RhythmDBEntryType *entry_type;
1345 	gboolean ret = TRUE;
1346 
1347 	g_object_get (source, "entry-type", &entry_type, NULL);
1348 	if (entry_type != NULL) {
1349 		if (rhythmdb_entry_get_entry_type (entry) != entry_type) {
1350 			ret = FALSE;
1351 		}
1352 		g_object_unref (entry_type);
1353 	}
1354 	return ret;
1355 }
1356 
1357 static gboolean
sort_order_get_mapping(GValue * value,GVariant * variant,gpointer data)1358 sort_order_get_mapping (GValue *value, GVariant *variant, gpointer data)
1359 {
1360 	const char *column;
1361 	gboolean sort_type;
1362 	char *str;
1363 
1364 	g_variant_get (variant, "(&sb)", &column, &sort_type);
1365 	str = g_strdup_printf ("%s,%s", column, sort_type ? "ascending" : "descending");
1366 	g_value_take_string (value, str);
1367 	return TRUE;
1368 }
1369 
1370 static GVariant *
sort_order_set_mapping(const GValue * value,const GVariantType * expected_type,gpointer data)1371 sort_order_set_mapping (const GValue *value, const GVariantType *expected_type, gpointer data)
1372 {
1373 	gboolean sort_type;
1374 	GVariant *var;
1375 	char **strs;
1376 
1377 	strs = g_strsplit (g_value_get_string (value), ",", 0);
1378 	if (!strcmp ("ascending", strs[1])) {
1379 		sort_type = TRUE;
1380 	} else if (!strcmp ("descending", strs[1])) {
1381 		sort_type = FALSE;
1382 	} else {
1383 		g_warning ("atttempting to sort in unknown direction");
1384 		sort_type = TRUE;
1385 	}
1386 
1387 	var = g_variant_new ("(sb)", strs[0], sort_type);
1388 	g_strfreev (strs);
1389 	return var;
1390 }
1391 
1392 static void
sync_paned_position(GSettings * settings,GObject * paned)1393 sync_paned_position (GSettings *settings, GObject *paned)
1394 {
1395 	int pos;
1396 	g_object_get (paned, "position", &pos, NULL);
1397 
1398 	if (pos != g_settings_get_int (settings, "paned-position")) {
1399 		g_settings_set_int (settings, "paned-position", pos);
1400 	}
1401 }
1402 
1403 static void
paned_position_changed_cb(GObject * paned,GParamSpec * pspec,GSettings * settings)1404 paned_position_changed_cb (GObject *paned, GParamSpec *pspec, GSettings *settings)
1405 {
1406 	rb_settings_delayed_sync (settings,
1407 				  (RBDelayedSyncFunc) sync_paned_position,
1408 				  g_object_ref (paned),
1409 				  g_object_unref);
1410 }
1411 
1412 /**
1413  * rb_source_bind_settings:
1414  * @source: the #RBSource
1415  * @entry_view: (allow-none): the #RBEntryView for the source
1416  * @paned: (allow-none): the #GtkPaned containing the entry view and the browser
1417  * @browser: (allow-none):  the browser (typically a #RBLibraryBrowser) for the source
1418  * @sort_order: whether to bind the entry view sort order
1419  *
1420  * Binds the source's #GSettings instance to the given widgets.  Should be called
1421  * from the source's constructed method.
1422  *
1423  * If the browser widget has a browser-views property, it will be bound to the
1424  * browser-views settings key.
1425  */
1426 void
rb_source_bind_settings(RBSource * source,GtkWidget * entry_view,GtkWidget * paned,GtkWidget * browser,gboolean sort_order)1427 rb_source_bind_settings (RBSource *source, GtkWidget *entry_view, GtkWidget *paned, GtkWidget *browser, gboolean sort_order)
1428 {
1429 	char *name;
1430 	GSettings *common_settings;
1431 
1432 	common_settings = g_settings_new ("org.gnome.rhythmbox.sources");
1433 	g_object_get (source, "name", &name, NULL);
1434 
1435 	if (entry_view != NULL) {
1436 		if (sort_order && source->priv->settings) {
1437 			rb_debug ("binding entry view sort order for %s", name);
1438 			g_settings_bind_with_mapping (source->priv->settings, "sorting", entry_view, "sort-order",
1439 						      G_SETTINGS_BIND_GET | G_SETTINGS_BIND_SET | G_SETTINGS_BIND_NO_SENSITIVITY,
1440 						      (GSettingsBindGetMapping) sort_order_get_mapping,
1441 						      (GSettingsBindSetMapping) sort_order_set_mapping,
1442 						      NULL, NULL);
1443 		}
1444 
1445 		g_settings_bind (common_settings, "visible-columns",
1446 				 entry_view, "visible-columns",
1447 				 G_SETTINGS_BIND_DEFAULT);
1448 	}
1449 
1450 	if (paned != NULL && source->priv->settings != NULL) {
1451 		rb_debug ("binding paned position for %s", name);
1452 		/* can't use a normal binding here, as we want to delay writing to the
1453 		 * setting while the separator is being dragged.
1454 		 */
1455 		g_settings_bind (source->priv->settings, "paned-position", paned, "position", G_SETTINGS_BIND_GET);
1456 		g_signal_connect_object (paned, "notify::position", G_CALLBACK (paned_position_changed_cb), source->priv->settings, 0);
1457 	}
1458 
1459 	if (browser) {
1460 		rb_debug ("binding show-browser for %s", name);
1461 		if (source->priv->settings) {
1462 			g_settings_bind (source->priv->settings, "show-browser", source, "show-browser", G_SETTINGS_BIND_DEFAULT);
1463 		}
1464 
1465 		if (g_object_class_find_property (G_OBJECT_GET_CLASS (browser), "browser-views")) {
1466 			g_settings_bind (common_settings, "browser-views", browser, "browser-views", G_SETTINGS_BIND_DEFAULT);
1467 		}
1468 	}
1469 
1470 	g_free (name);
1471 }
1472 
1473 /**
1474  * rb_source_notify_playback_status_changed:
1475  * @source: a #RBSource
1476  *
1477  * Source implementations call this when their playback status
1478  * changes.
1479  */
1480 void
rb_source_notify_playback_status_changed(RBSource * source)1481 rb_source_notify_playback_status_changed (RBSource *source)
1482 {
1483 	g_signal_emit (G_OBJECT (source), rb_source_signals[PLAYBACK_STATUS_CHANGED], 0);
1484 }
1485 
1486 /**
1487  * rb_source_get_playback_status:
1488  * @source: a #RBSource
1489  * @text: (inout) (allow-none) (transfer full): holds returned playback status text
1490  * @progress: (inout) (allow-none): holds returned playback status progress value
1491  *
1492  * Retrieves playback status details, such as buffering progress.
1493  */
1494 void
rb_source_get_playback_status(RBSource * source,char ** text,float * progress)1495 rb_source_get_playback_status (RBSource *source, char **text, float *progress)
1496 {
1497 	RBSourceClass *klass = RB_SOURCE_GET_CLASS (source);
1498 
1499 	if (klass->get_playback_status)
1500 		klass->get_playback_status (source, text, progress);
1501 }
1502 
1503 static gboolean
status_changed_idle_cb(RBSource * source)1504 status_changed_idle_cb (RBSource *source)
1505 {
1506 	RBEntryView *entry_view;
1507 	char *status = NULL;
1508 	gboolean busy = FALSE;
1509 
1510 	rb_display_page_get_status (RB_DISPLAY_PAGE (source), &status, &busy);
1511 
1512 	entry_view = rb_source_get_entry_view (source);
1513 	if (entry_view != NULL)
1514 		rb_entry_view_set_status (entry_view, status, busy);
1515 
1516 	g_free (status);
1517 	source->priv->status_changed_idle_id = 0;
1518 	return FALSE;
1519 }
1520 
1521 static void
rb_source_status_changed_cb(RBDisplayPage * page)1522 rb_source_status_changed_cb (RBDisplayPage *page)
1523 {
1524 	RBSource *source = RB_SOURCE (page);
1525 	if (source->priv->status_changed_idle_id == 0) {
1526 		source->priv->status_changed_idle_id =
1527 			g_idle_add ((GSourceFunc) status_changed_idle_cb, source);
1528 	}
1529 }
1530 
1531 /* This should really be standard. */
1532 #define ENUM_ENTRY(NAME, DESC) { NAME, "" #NAME "", DESC }
1533 
1534 GType
rb_source_eof_type_get_type(void)1535 rb_source_eof_type_get_type (void)
1536 {
1537 	static GType etype = 0;
1538 
1539 	if (etype == 0)	{
1540 		static const GEnumValue values[] = {
1541 			ENUM_ENTRY (RB_SOURCE_EOF_ERROR, "error"),
1542 			ENUM_ENTRY (RB_SOURCE_EOF_STOP, "stop"),
1543 			ENUM_ENTRY (RB_SOURCE_EOF_RETRY, "retry"),
1544 			ENUM_ENTRY (RB_SOURCE_EOF_NEXT, "next"),
1545 			{ 0, 0, 0 }
1546 		};
1547 
1548 		etype = g_enum_register_static ("RBSourceEOFType", values);
1549 	}
1550 
1551 	return etype;
1552 }
1553 
1554 GType
rb_source_load_status_get_type(void)1555 rb_source_load_status_get_type (void)
1556 {
1557 	static GType etype = 0;
1558 
1559 	if (etype == 0) {
1560 		static const GEnumValue values[] = {
1561 			ENUM_ENTRY (RB_SOURCE_LOAD_STATUS_NOT_LOADED, "not-loaded"),
1562 			ENUM_ENTRY (RB_SOURCE_LOAD_STATUS_WAITING, "waiting"),
1563 			ENUM_ENTRY (RB_SOURCE_LOAD_STATUS_LOADING, "loading"),
1564 			ENUM_ENTRY (RB_SOURCE_LOAD_STATUS_LOADED, "loaded"),
1565 			{ 0, 0, 0 }
1566 		};
1567 
1568 		etype = g_enum_register_static ("RBSourceLoadStatus", values);
1569 	}
1570 
1571 	return etype;
1572 }
1573 
1574