core/tempq: Move tempq_init() out of the deck namespace

I also take this opportunity to drop support for upgrading from file
format 0.  I've had several releases since this format was supported, so
this should be safe to do.

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2015-12-13 09:27:52 -05:00
parent 51379c7e8c
commit 8c7550d5c3
6 changed files with 41 additions and 100 deletions

View File

@ -20,7 +20,7 @@ void core :: init(struct core_init_data *init)
playlist_init(init->playlist_ops);
collection_init(init->collection_ops);
history_init(init->history_ops);
deck :: init(init->tempq_ops);
tempq_init(init->tempq_ops);
audio :: init();
}

View File

@ -12,11 +12,6 @@ static std::list<TempQueue> queue_deck;
static struct file deck_file;
TempQueue :: TempQueue()
{
queue_init(this, 0, NULL);
}
TempQueue :: TempQueue(bool random, struct queue_ops *ops)
{
unsigned int flags = Q_ENABLED | Q_SAVE_FLAGS | Q_SAVE_SORT;
@ -61,51 +56,23 @@ void TempQueue :: del(unsigned int id)
}
static void upgrade_v0()
void tempq_init(struct queue_ops *ops)
{
int random, ascending;
unsigned int num, field;
queue *library = collection_get_queue();
file_readf(&deck_file, "%d %u", &random, &num);
if (random)
queue_set_flag(library, Q_RANDOM);
for (unsigned int i = 0; i < num; i++) {
file_readf(&deck_file, "%u %d", &field, &ascending);
queue_sort(library, (compare_t)field, (i == 0) ? true : false);
if (!ascending)
queue_sort(library, (compare_t)field, false);
}
}
void deck :: init(struct queue_ops *temp_ops)
{
unsigned int num;
bool upgraded = false;
std::list<TempQueue>::iterator it;
unsigned int num, i;
file_init(&deck_file, "deck", 1);
if (!file_open(&deck_file, OPEN_READ))
return;
if (file_version(&deck_file) == 0) {
upgrade_v0();
upgraded = true;
}
if (file_version(&deck_file) < 1)
return;
file_readf(&deck_file, "%u", &num);
queue_deck.resize(num);
for (it = queue_deck.begin(); it != queue_deck.end(); it++) {
it->read(deck_file);
it->q_ops = temp_ops;
for (i = 0; i < num; i++) {
queue_deck.push_back(TempQueue(false, NULL));
queue_deck.back().read(deck_file);
queue_deck.back().q_ops = ops;
}
file_close(&deck_file);
if (upgraded)
deck :: write();
}
void deck :: write()

View File

@ -17,7 +17,6 @@ extern "C" {
class TempQueue : public queue
{
public:
TempQueue();
TempQueue(bool, struct queue_ops *);
void read(file &);
@ -43,11 +42,6 @@ public:
namespace deck
{
/**
* Read the deck file from disk and restore the queues.
*/
void init(struct queue_ops *);
/**
* Save the current queues to a file on disk.
*/
@ -107,4 +101,8 @@ namespace deck
};
/* Called to initialize the temporary queue manager. */
void tempq_init(struct queue_ops *);
#endif /* OCARINA_CORE_DECK_H */

View File

@ -1,4 +1,4 @@
0
2 3 0 1 8 1 7 1 2
1
2
1 4 0 1 2 3
1 5 4 5 6 7 8

View File

@ -93,7 +93,7 @@ void test_init()
playlist_init(NULL);
collection_init(NULL);
history_init(NULL);
deck :: init(NULL);
tempq_init(NULL);
audio :: init();
track = audio :: current_track();

View File

@ -6,21 +6,39 @@ extern "C" {
#include <core/collection.h>
#include <core/filter.h>
#include <core/history.h>
#include <core/idle.h>
#include <core/playlist.h>
#include <core/tags/tags.h>
}
#include "test.h"
static queue *Q_NULL = NULL;
static struct track *TRACK_NULL = NULL;
static void test_init()
{
unsigned int val;
file f;
std::list<TempQueue>::iterator it;
filter_init();
tags_init();
playlist_init(NULL);
collection_init(NULL);
history_init(NULL);
test_equal(deck :: next(), TRACK_NULL);
test_equal(deck :: next(), NULL);
tempq_init(NULL);
test_equal(deck :: next(), NULL);
test_equal(deck :: get(0), NULL);
test_equal(deck :: index(NULL), 0);
test_equal(deck :: get_queues().size(), (size_t)0);
history_deinit();
collection_deinit();
playlist_deinit();
tags_deinit();
filter_deinit();
}
static void test_create_mv_destroy()
{
queue *q1, *q2;
test_cp_data_dir();
filter_init();
@ -28,49 +46,7 @@ static void test_init()
playlist_init(NULL);
collection_init(NULL);
history_init(NULL);
deck :: init(NULL);
test_equal(queue_has_flag(collection_get_queue(), Q_RANDOM), true);
test_equal(deck :: get_queues().size(), (size_t)2);
it = deck :: get_queues().begin();
test_equal(queue_size(&(*it)), (unsigned)4);
for (unsigned int i = 0; i < 4; i++)
test_equal(queue_at(&(*it), i)->tr_dbe.dbe_index, i);
it++;
test_equal(queue_size(&(*it)), (unsigned)5);
for (unsigned int i = 0; i < 5; i++)
test_equal(queue_at(&(*it), i)->tr_dbe.dbe_index, i + 4);
/*
* Test that we saved the deck in the new format
*/
file_init(&f, "deck", 0);
file_open(&f, OPEN_READ);
test_equal(file_version(&f), (unsigned)1);
file_readf(&f, "%u", &val);
test_equal(val, (unsigned)2);
for (unsigned int i = 0; i < 2; i++) {
file_readf(&f, "%u", &val); /* queues[i].flags */
test_equal(val, (unsigned)1);
file_readf(&f, "%u", &val); /* queues[i].size */
test_equal(val, 4 + i);
for (unsigned int j = 0; j < 4 + i; j++) {
file_readf(&f, "%u", &val);
test_equal(val, (4 * i) + j);
}
}
file_close(&f);
}
static void test_create_mv_destroy()
{
queue *q1, *q2;
tempq_init(NULL);
q1 = deck :: create(true, NULL);
test_not_equal(q1, Q_NULL);
@ -141,7 +117,7 @@ static void test_next_prev()
}
DECLARE_UNIT_TESTS(
UNIT_TEST("Deck Init", test_init),
UNIT_TEST("Temporary Queue Initialization", test_init),
UNIT_TEST("Deck Create, Move, and Destroy", test_create_mv_destroy),
UNIT_TEST("Deck Next and Prev", test_next_prev),
);