tags: Add Track->played()

This function is called to increment play count and set "date last
played" variables.  Moving this into the Track class lets me remove an
internal callback and a few other unnecessary functions.

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2014-05-23 21:52:09 -04:00
parent ea369eb14e
commit 59220cc2ce
11 changed files with 41 additions and 25 deletions

7
DESIGN
View File

@ -773,6 +773,7 @@ Track Tag:
void tag();
const std::string path();
void played();
bool less_than(Track *, sort_t);
};
@ -818,6 +819,12 @@ Track Tag:
Combine library->path and filepath to find the full path to
the audio file.
void Track :: played();
Called when a track has been played. Increment the play count
and set last_day, last_month, and last_year to today's date.
Call tagdb :: commit() to save the track database.
int Track :: less_than(Track *rhs, sort_t field);
Compare the requested field for this Track instance to the same
field in the provided track.

View File

@ -24,7 +24,6 @@ struct Callbacks {
void (*on_library_update)(unsigned int, Library *);
void (*on_library_track_add)(unsigned int);
void (*on_library_track_del)(unsigned int);
void (*on_library_track_updated)(unsigned int);
/* Queue callbacks */
void (*on_queue_track_add)(Queue *, unsigned int);

View File

@ -8,6 +8,7 @@
namespace deck
{
void change_library_track(unsigned int);
void init();
void read();

View File

@ -25,7 +25,6 @@ namespace library
void update_path(unsigned int);
void update_all();
void set_enabled(unsigned int, bool);
void track_played(unsigned int);
#ifdef CONFIG_TEST
void print_db(DB_Type);
void reset();

View File

@ -105,6 +105,7 @@ public:
void tag();
const std::string path() const;
void played();
int less_than(Track *, sort_t);
};

View File

@ -68,6 +68,8 @@ static void handle_pause_count()
static gboolean on_message(GstBus *bus, GstMessage *message, gpointer data)
{
Track *track;
switch (GST_MESSAGE_TYPE(message)) {
case GST_MESSAGE_ERROR:
parse_error(message);
@ -76,7 +78,11 @@ static gboolean on_message(GstBus *bus, GstMessage *message, gpointer data)
break;
case GST_MESSAGE_EOS:
handle_pause_count();
library :: track_played(cur_trackid);
track = tagdb :: lookup(cur_trackid);
if (track) {
track->played();
deck :: change_library_track(track->id);
}
audio :: next();
audio :: seek_to(0);
default:

View File

@ -26,7 +26,6 @@ static struct Callbacks callbacks = {
.on_library_update = no_op,
.on_library_track_add = no_op,
.on_library_track_del = no_op,
.on_library_track_updated = no_op,
.on_queue_track_add = no_op,
.on_queue_track_del = no_op,

View File

@ -23,7 +23,7 @@ static void del_library_track(unsigned int id)
library_playqueue.del(tagdb :: lookup(id));
}
static void change_library_track(unsigned int id)
void deck :: change_library_track(unsigned int id)
{
library_playqueue.updated(tagdb :: lookup(id));
}
@ -39,7 +39,6 @@ void deck :: init()
get_callbacks()->on_library_track_add = add_library_track;
get_callbacks()->on_library_track_del = del_library_track;
get_callbacks()->on_library_track_updated = change_library_track;
get_callbacks()->on_queue_changed = write;
}

View File

@ -176,24 +176,6 @@ void library :: set_enabled(unsigned int id, bool enabled)
}
}
void library :: track_played(unsigned int id)
{
time_t the_time = time(NULL);
struct tm *now = localtime(&the_time);
Track *track = tagdb :: lookup(id);
if (!track)
return;
track->play_count++;
track->last_day = now->tm_mday;
track->last_month = now->tm_mon + 1;
track->last_year = now->tm_year + 1900;
tagdb :: commit();
get_callbacks()->on_library_track_updated(track->id);
}
#ifdef CONFIG_TEST
void library :: print_db(DB_Type type)
{

View File

@ -265,6 +265,19 @@ const std::string Track :: path() const
return library->root_path + "/" + filepath;
}
void Track :: played()
{
time_t the_time = time(NULL);
struct tm *now = localtime(&the_time);
play_count++;
last_day = now->tm_mday;
last_month = now->tm_mon + 1;
last_year = now->tm_year + 1900;
tagdb :: commit();
}
/*
* Returns:
* 0: lhs == rhs
@ -373,6 +386,7 @@ void tagdb :: remove_library(unsigned int library_id)
if ((*it)->library->id == library_id)
track_db.remove((*it)->id);
}
tagdb :: commit();
library_db.remove(library_id);
}

View File

@ -56,7 +56,7 @@ static void test_track(struct TagArgs *args)
Track *track = tagdb :: add_track(args->full_path, args->library);
test_not_equal(track, TRACK_NULL);
test_equal(track->path(), args->full_path);
test_equal(track->path(), args->full_path);
/*
* Check tags
@ -85,6 +85,15 @@ static void test_track(struct TagArgs *args)
test_equal(tagdb :: lookup(args->track - 1), TRACK_NULL);
test_equal(tagdb :: get_track_db().size(), (unsigned)0);
test_equal(args->library->count, (unsigned)0);
/*
* Mark track played, double check new values
*/
track->played();
test_equal(track->play_count, args->play_count + 1);
test_not_equal(track->last_year, args->last_year);
test_not_equal(track->last_month, args->last_month);
test_not_equal(track->last_day, args->last_day);
}
static void test_all_tracks()