File: rb-play-order-linear-loop.c

package info (click to toggle)
rhythmbox 3.4.9-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 27,376 kB
  • sloc: ansic: 114,861; python: 4,941; xml: 730; javascript: 350; perl: 307; sh: 84; makefile: 43
file content (133 lines) | stat: -rw-r--r-- 4,142 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
 *  Copyright (C) 2003 Jeffrey Yasskin <jyasskin@mail.utexas.edu>
 *
 *  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.
 *
 *  The Rhythmbox authors hereby grant permission for non-GPL compatible
 *  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.
 *
 *  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
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA.
 *
 */

#include "config.h"

#include "rb-play-order-linear-loop.h"

#include "rb-debug.h"
#include "rb-util.h"

static void rb_linear_play_order_loop_class_init (RBLinearPlayOrderLoopClass *klass);

static RhythmDBEntry* rb_linear_play_order_loop_get_next (RBPlayOrder* method);
static RhythmDBEntry* rb_linear_play_order_loop_get_previous (RBPlayOrder* method);

G_DEFINE_TYPE (RBLinearPlayOrderLoop, rb_linear_play_order_loop, RB_TYPE_PLAY_ORDER)

RBPlayOrder *
rb_linear_play_order_loop_new (RBShellPlayer *player)
{
	RBLinearPlayOrderLoop *lorder;

	lorder = g_object_new (RB_TYPE_LINEAR_PLAY_ORDER_LOOP,
			       "player", player,
			       NULL);

	return RB_PLAY_ORDER (lorder);
}

static void
rb_linear_play_order_loop_class_init (RBLinearPlayOrderLoopClass *klass)
{
	RBPlayOrderClass *porder = RB_PLAY_ORDER_CLASS (klass);
	porder->has_next = rb_play_order_model_not_empty;
	porder->has_previous = rb_play_order_model_not_empty;
	porder->get_next = rb_linear_play_order_loop_get_next;
	porder->get_previous = rb_linear_play_order_loop_get_previous;
}

static void
rb_linear_play_order_loop_init (RBLinearPlayOrderLoop *porder)
{
}

static RhythmDBEntry *
rb_linear_play_order_loop_get_next (RBPlayOrder *porder)
{
	RhythmDBQueryModel *model;
	RhythmDBEntry *entry;

	g_return_val_if_fail (porder != NULL, NULL);
	g_return_val_if_fail (RB_IS_LINEAR_PLAY_ORDER_LOOP (porder), NULL);

	model = rb_play_order_get_query_model (porder);
	if (model == NULL)
		return NULL;

	g_object_get (porder, "playing-entry", &entry, NULL);

	if (entry != NULL) {
		RhythmDBEntry *next;

		next = rhythmdb_query_model_get_next_from_entry (model, entry);
		rhythmdb_entry_unref (entry);
		entry = next;
	}

	if (entry == NULL) {
		/* loop back to (or start from) the first entry */
		GtkTreeIter iter;
		if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (model), &iter))
			return NULL;
		return rhythmdb_query_model_iter_to_entry (model, &iter);
	}

	return entry;
}

static RhythmDBEntry *
rb_linear_play_order_loop_get_previous (RBPlayOrder *porder)
{
	RhythmDBQueryModel *model;
	RhythmDBEntry *entry;
	RhythmDBEntry *prev = NULL;

	g_return_val_if_fail (porder != NULL, NULL);
	g_return_val_if_fail (RB_IS_LINEAR_PLAY_ORDER_LOOP (porder), NULL);

	model = rb_play_order_get_query_model (porder);
	if (model == NULL)
		return NULL;

	g_object_get (porder, "playing-entry", &entry, NULL);
	if (entry != NULL) {
		prev = rhythmdb_query_model_get_previous_from_entry (model, entry);
		rhythmdb_entry_unref (entry);
	}

	if (prev == NULL) {
		/* loop to last entry */
		GtkTreeIter iter;
		gint num_entries = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (model), NULL);
		if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (model), &iter, NULL, num_entries-1))
			return NULL;
		prev = rhythmdb_query_model_iter_to_entry (model, &iter);
	}

	return prev;
}