hythmbox-2.98/shell/rb-play-order-linear-loop.c

No issues found

  1 /*
  2  *  Copyright (C) 2003 Jeffrey Yasskin <jyasskin@mail.utexas.edu>
  3  *
  4  *  This program is free software; you can redistribute it and/or modify
  5  *  it under the terms of the GNU General Public License as published by
  6  *  the Free Software Foundation; either version 2 of the License, or
  7  *  (at your option) any later version.
  8  *
  9  *  The Rhythmbox authors hereby grant permission for non-GPL compatible
 10  *  GStreamer plugins to be used and distributed together with GStreamer
 11  *  and Rhythmbox. This permission is above and beyond the permissions granted
 12  *  by the GPL license by which Rhythmbox is covered. If you modify this code
 13  *  you may extend this exception to your version of the code, but you are not
 14  *  obligated to do so. If you do not wish to do so, delete this exception
 15  *  statement from your version.
 16  *
 17  *  This program is distributed in the hope that it will be useful,
 18  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 19  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 20  *  GNU General Public License for more details.
 21  *
 22  *  You should have received a copy of the GNU General Public License
 23  *  along with this program; if not, write to the Free Software
 24  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA.
 25  *
 26  */
 27 
 28 #include "config.h"
 29 
 30 #include "rb-play-order-linear-loop.h"
 31 
 32 #include "rb-debug.h"
 33 #include "rb-util.h"
 34 
 35 static void rb_linear_play_order_loop_class_init (RBLinearPlayOrderLoopClass *klass);
 36 
 37 static RhythmDBEntry* rb_linear_play_order_loop_get_next (RBPlayOrder* method);
 38 static RhythmDBEntry* rb_linear_play_order_loop_get_previous (RBPlayOrder* method);
 39 
 40 G_DEFINE_TYPE (RBLinearPlayOrderLoop, rb_linear_play_order_loop, RB_TYPE_PLAY_ORDER)
 41 
 42 RBPlayOrder *
 43 rb_linear_play_order_loop_new (RBShellPlayer *player)
 44 {
 45 	RBLinearPlayOrderLoop *lorder;
 46 
 47 	lorder = g_object_new (RB_TYPE_LINEAR_PLAY_ORDER_LOOP,
 48 			       "player", player,
 49 			       NULL);
 50 
 51 	return RB_PLAY_ORDER (lorder);
 52 }
 53 
 54 static void
 55 rb_linear_play_order_loop_class_init (RBLinearPlayOrderLoopClass *klass)
 56 {
 57 	RBPlayOrderClass *porder = RB_PLAY_ORDER_CLASS (klass);
 58 	porder->has_next = rb_play_order_model_not_empty;
 59 	porder->has_previous = rb_play_order_model_not_empty;
 60 	porder->get_next = rb_linear_play_order_loop_get_next;
 61 	porder->get_previous = rb_linear_play_order_loop_get_previous;
 62 }
 63 
 64 static void
 65 rb_linear_play_order_loop_init (RBLinearPlayOrderLoop *porder)
 66 {
 67 }
 68 
 69 static RhythmDBEntry *
 70 rb_linear_play_order_loop_get_next (RBPlayOrder *porder)
 71 {
 72 	RhythmDBQueryModel *model;
 73 	RhythmDBEntry *entry;
 74 
 75 	g_return_val_if_fail (porder != NULL, NULL);
 76 	g_return_val_if_fail (RB_IS_LINEAR_PLAY_ORDER_LOOP (porder), NULL);
 77 
 78 	model = rb_play_order_get_query_model (porder);
 79 	if (model == NULL)
 80 		return NULL;
 81 
 82 	g_object_get (porder, "playing-entry", &entry, NULL);
 83 
 84 	if (entry != NULL) {
 85 		RhythmDBEntry *next;
 86 
 87 		next = rhythmdb_query_model_get_next_from_entry (model, entry);
 88 		rhythmdb_entry_unref (entry);
 89 		entry = next;
 90 	}
 91 
 92 	if (entry == NULL) {
 93 		/* loop back to (or start from) the first entry */
 94 		GtkTreeIter iter;
 95 		if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (model), &iter))
 96 			return NULL;
 97 		return rhythmdb_query_model_iter_to_entry (model, &iter);
 98 	}
 99 
100 	return entry;
101 }
102 
103 static RhythmDBEntry *
104 rb_linear_play_order_loop_get_previous (RBPlayOrder *porder)
105 {
106 	RhythmDBQueryModel *model;
107 	RhythmDBEntry *entry;
108 	RhythmDBEntry *prev = NULL;
109 
110 	g_return_val_if_fail (porder != NULL, NULL);
111 	g_return_val_if_fail (RB_IS_LINEAR_PLAY_ORDER_LOOP (porder), NULL);
112 
113 	model = rb_play_order_get_query_model (porder);
114 	if (model == NULL)
115 		return NULL;
116 
117 	g_object_get (porder, "playing-entry", &entry, NULL);
118 	if (entry != NULL) {
119 		prev = rhythmdb_query_model_get_previous_from_entry (model, entry);
120 		rhythmdb_entry_unref (entry);
121 	}
122 
123 	if (prev == NULL) {
124 		/* loop to last entry */
125 		GtkTreeIter iter;
126 		gint num_entries = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (model), NULL);
127 		if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (model), &iter, NULL, num_entries-1))
128 			return NULL;
129 		prev = rhythmdb_query_model_iter_to_entry (model, &iter);
130 	}
131 
132 	return prev;
133 }