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-and-rating.h"
31
32 #include "rhythmdb.h"
33 #include <time.h>
34 #include <math.h>
35
36 static void rb_random_play_order_by_age_and_rating_class_init (RBRandomPlayOrderByAgeAndRatingClass *klass);
37
38 static double rb_random_by_age_and_rating_get_entry_weight (RBRandomPlayOrder *rorder,
39 RhythmDB *db, RhythmDBEntry *entry);
40
41 G_DEFINE_TYPE (RBRandomPlayOrderByAgeAndRating,
42 rb_random_play_order_by_age_and_rating,
43 RB_TYPE_RANDOM_PLAY_ORDER)
44
45 static void
46 rb_random_play_order_by_age_and_rating_class_init (RBRandomPlayOrderByAgeAndRatingClass *klass)
47 {
48 RBRandomPlayOrderClass *rorder;
49
50 rorder = RB_RANDOM_PLAY_ORDER_CLASS (klass);
51 rorder->get_entry_weight = rb_random_by_age_and_rating_get_entry_weight;
52 }
53
54 RBPlayOrder *
55 rb_random_play_order_by_age_and_rating_new (RBShellPlayer *player)
56 {
57 RBRandomPlayOrderByAgeAndRating *rorder;
58
59 rorder = g_object_new (RB_TYPE_RANDOM_PLAY_ORDER_BY_AGE_AND_RATING,
60 "player", player,
61 NULL);
62
63 return RB_PLAY_ORDER (rorder);
64 }
65
66 static void
67 rb_random_play_order_by_age_and_rating_init (RBRandomPlayOrderByAgeAndRating *porder)
68 {
69 }
70
71 static double
72 rb_random_by_age_and_rating_get_entry_weight (RBRandomPlayOrder *rorder, RhythmDB *db, RhythmDBEntry *entry)
73 {
74 time_t now;
75 gulong last_play;
76 gulong seconds_since_last_play = 0;
77 gdouble rating;
78 RhythmDBEntry *playing_entry;
79
80 /* This finds the log of the number of seconds since the last play.
81 * It handles never played automatically, since now-0 is a valid
82 * argument to log(). */
83 time (&now);
84
85 playing_entry = rb_play_order_get_playing_entry (RB_PLAY_ORDER (rorder));
86 if (playing_entry != entry) {
87 last_play = rhythmdb_entry_get_ulong (entry, RHYTHMDB_PROP_LAST_PLAYED);
88 seconds_since_last_play = now - last_play;
89 }
90 if (playing_entry != NULL)
91 rhythmdb_entry_unref (playing_entry);
92
93 /* The lowest weight should be 0. */
94 if (seconds_since_last_play < 1)
95 seconds_since_last_play = 1;
96
97 rating = rhythmdb_entry_get_double (entry, RHYTHMDB_PROP_RATING);
98
99 /* treat unrated as 2.5 for the purposes of probabilities */
100 if (rating < 0.01)
101 rating = 2.5;
102
103 return log (seconds_since_last_play) * (rating + 1.0);
104 }