hythmbox-2.98/shell/rb-play-order-random-by-age.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-random-by-age.h"
31 
32 #include "rhythmdb.h"
33 #include <time.h>
34 #include <math.h>
35 
36 static void rb_random_play_order_by_age_class_init (RBRandomPlayOrderByAgeClass *klass);
37 
38 static double rb_random_by_age_get_entry_weight (RBRandomPlayOrder *rorder,
39 						 RhythmDB *db, RhythmDBEntry *entry);
40 
41 G_DEFINE_TYPE (RBRandomPlayOrderByAge, rb_random_play_order_by_age, RB_TYPE_RANDOM_PLAY_ORDER)
42 
43 static void
44 rb_random_play_order_by_age_class_init (RBRandomPlayOrderByAgeClass *klass)
45 {
46 	RBRandomPlayOrderClass *rorder;
47 
48 	rorder = RB_RANDOM_PLAY_ORDER_CLASS (klass);
49 	rorder->get_entry_weight = rb_random_by_age_get_entry_weight;
50 }
51 
52 RBPlayOrder *
53 rb_random_play_order_by_age_new (RBShellPlayer *player)
54 {
55 	RBRandomPlayOrderByAge *rorder;
56 
57 	rorder = g_object_new (RB_TYPE_RANDOM_PLAY_ORDER_BY_AGE,
58 			"player", player,
59 			NULL);
60 
61 	return RB_PLAY_ORDER (rorder);
62 }
63 
64 static void
65 rb_random_play_order_by_age_init (RBRandomPlayOrderByAge *porder)
66 {
67 }
68 
69 static double
70 rb_random_by_age_get_entry_weight (RBRandomPlayOrder *rorder, RhythmDB *db, RhythmDBEntry *entry)
71 {
72 	time_t now;
73 	gulong last_play;
74 	gulong seconds_since_last_play = 0;
75 	RhythmDBEntry *playing_entry;
76 
77 	/* This returns the log of the number of seconds since the last play.
78 	 * It handles never played automatically, since now-0 is a valid
79 	 * argument to log(). */
80 
81 	time (&now);
82 
83 	playing_entry = rb_play_order_get_playing_entry (RB_PLAY_ORDER (rorder));
84 	if (playing_entry != entry) {
85 		last_play = rhythmdb_entry_get_ulong (entry, RHYTHMDB_PROP_LAST_PLAYED);
86 		seconds_since_last_play = now - last_play;
87 	}
88 	if (playing_entry != NULL)
89 		rhythmdb_entry_unref (playing_entry);
90 
91 	/* The lowest weight should be 0. */
92 	if (seconds_since_last_play < 1)
93 		seconds_since_last_play = 1;
94 	return log (seconds_since_last_play);
95 }