2006-12-14 Francisco Javier F. Serrador <serrador@openshine.com>
[rhythmbox.git] / shell / rb-play-order-linear-loop.c
blob33ab486f37d8d8e2785b153bc3258c023815cf51
1 /*
2 * arch-tag: Implementation of linear, looping navigation method
4 * Copyright (C) 2003 Jeffrey Yasskin <jyasskin@mail.utexas.edu>
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.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "rb-play-order-linear-loop.h"
24 #include "rb-debug.h"
25 #include "rb-util.h"
26 #include "eel-gconf-extensions.h"
28 static void rb_linear_play_order_loop_class_init (RBLinearPlayOrderLoopClass *klass);
30 static RhythmDBEntry* rb_linear_play_order_loop_get_next (RBPlayOrder* method);
31 static RhythmDBEntry* rb_linear_play_order_loop_get_previous (RBPlayOrder* method);
33 G_DEFINE_TYPE (RBLinearPlayOrderLoop, rb_linear_play_order_loop, RB_TYPE_PLAY_ORDER)
35 RBPlayOrder *
36 rb_linear_play_order_loop_new (RBShellPlayer *player)
38 RBLinearPlayOrderLoop *lorder;
40 lorder = g_object_new (RB_TYPE_LINEAR_PLAY_ORDER_LOOP,
41 "player", player,
42 NULL);
44 return RB_PLAY_ORDER (lorder);
47 static void
48 rb_linear_play_order_loop_class_init (RBLinearPlayOrderLoopClass *klass)
50 RBPlayOrderClass *porder = RB_PLAY_ORDER_CLASS (klass);
51 porder->has_next = rb_play_order_model_not_empty;
52 porder->has_previous = rb_play_order_model_not_empty;
53 porder->get_next = rb_linear_play_order_loop_get_next;
54 porder->get_previous = rb_linear_play_order_loop_get_previous;
57 static void
58 rb_linear_play_order_loop_init (RBLinearPlayOrderLoop *porder)
62 static RhythmDBEntry *
63 rb_linear_play_order_loop_get_next (RBPlayOrder *porder)
65 RhythmDBQueryModel *model;
66 RhythmDBEntry *entry;
68 g_return_val_if_fail (porder != NULL, NULL);
69 g_return_val_if_fail (RB_IS_LINEAR_PLAY_ORDER_LOOP (porder), NULL);
71 model = rb_play_order_get_query_model (porder);
72 if (model == NULL)
73 return NULL;
75 g_object_get (porder, "playing-entry", &entry, NULL);
77 if (entry != NULL) {
78 RhythmDBEntry *next;
80 next = rhythmdb_query_model_get_next_from_entry (model, entry);
81 rhythmdb_entry_unref (entry);
82 entry = next;
85 if (entry == NULL) {
86 /* loop back to (or start from) the first entry */
87 GtkTreeIter iter;
88 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (model), &iter))
89 return NULL;
90 return rhythmdb_query_model_iter_to_entry (model, &iter);
93 return entry;
96 static RhythmDBEntry *
97 rb_linear_play_order_loop_get_previous (RBPlayOrder *porder)
99 RhythmDBQueryModel *model;
100 RhythmDBEntry *entry;
101 RhythmDBEntry *prev = NULL;
103 g_return_val_if_fail (porder != NULL, NULL);
104 g_return_val_if_fail (RB_IS_LINEAR_PLAY_ORDER_LOOP (porder), NULL);
106 model = rb_play_order_get_query_model (porder);
107 if (model == NULL)
108 return NULL;
110 g_object_get (porder, "playing-entry", &entry, NULL);
111 if (entry != NULL) {
112 prev = rhythmdb_query_model_get_previous_from_entry (model, entry);
113 rhythmdb_entry_unref (entry);
116 if (prev == NULL) {
117 /* loop to last entry */
118 GtkTreeIter iter;
119 gint num_entries = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (model), NULL);
120 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (model), &iter, NULL, num_entries-1))
121 return NULL;
122 prev = rhythmdb_query_model_iter_to_entry (model, &iter);
125 return prev;