2006-12-14 Francisco Javier F. Serrador <serrador@openshine.com>
[rhythmbox.git] / shell / rb-play-order-random-by-age-and-rating.c
blob669ee028089c176a93e34ea0ad3addbfb24cd1be
1 /*
2 * arch-tag: Implementation of random play order weighted by the time since last play and the rating
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-random-by-age-and-rating.h"
24 #include "rhythmdb.h"
25 #include <time.h>
26 #include <math.h>
28 static void rb_random_play_order_by_age_and_rating_class_init (RBRandomPlayOrderByAgeAndRatingClass *klass);
30 static double rb_random_by_age_and_rating_get_entry_weight (RBRandomPlayOrder *rorder,
31 RhythmDB *db, RhythmDBEntry *entry);
33 G_DEFINE_TYPE (RBRandomPlayOrderByAgeAndRating,
34 rb_random_play_order_by_age_and_rating,
35 RB_TYPE_RANDOM_PLAY_ORDER)
37 static void
38 rb_random_play_order_by_age_and_rating_class_init (RBRandomPlayOrderByAgeAndRatingClass *klass)
40 RBRandomPlayOrderClass *rorder;
42 rorder = RB_RANDOM_PLAY_ORDER_CLASS (klass);
43 rorder->get_entry_weight = rb_random_by_age_and_rating_get_entry_weight;
46 RBPlayOrder *
47 rb_random_play_order_by_age_and_rating_new (RBShellPlayer *player)
49 RBRandomPlayOrderByAgeAndRating *rorder;
51 rorder = g_object_new (RB_TYPE_RANDOM_PLAY_ORDER_BY_AGE_AND_RATING,
52 "player", player,
53 NULL);
55 return RB_PLAY_ORDER (rorder);
58 static void
59 rb_random_play_order_by_age_and_rating_init (RBRandomPlayOrderByAgeAndRating *porder)
63 static double
64 rb_random_by_age_and_rating_get_entry_weight (RBRandomPlayOrder *rorder, RhythmDB *db, RhythmDBEntry *entry)
66 time_t now;
67 gulong last_play;
68 gulong seconds_since_last_play;
69 gdouble rating;
71 /* This finds the log of the number of seconds since the last play.
72 * It handles never played automatically, since now-0 is a valid
73 * argument to log(). */
74 time (&now);
75 last_play = rhythmdb_entry_get_ulong (entry, RHYTHMDB_PROP_LAST_PLAYED);
76 seconds_since_last_play = now - last_play;
77 /* The lowest weight should be 0. */
78 if (seconds_since_last_play < 1)
79 seconds_since_last_play = 1;
81 rating = rhythmdb_entry_get_double (entry, RHYTHMDB_PROP_RATING);
83 /* treat unrated as 2.5 for the purposes of probabilities */
84 if (rating < 0.01)
85 rating = 2.5;
87 return log (seconds_since_last_play) * (rating + 1.0);