queue: Add an on_remove() notification to the QNotifier class

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2015-04-04 09:37:03 -04:00
parent c068cbb736
commit e8b68b84eb
3 changed files with 42 additions and 7 deletions

View File

@ -14,6 +14,7 @@
static class DefaultNotifier : public QNotifier { static class DefaultNotifier : public QNotifier {
public: public:
DefaultNotifier() {}; DefaultNotifier() {};
void on_remove() {};
void on_track_added(unsigned int pos) {}; void on_track_added(unsigned int pos) {};
void on_track_removed(unsigned int pos) {}; void on_track_removed(unsigned int pos) {};
void on_track_updated(unsigned int pos) {}; void on_track_updated(unsigned int pos) {};
@ -29,7 +30,10 @@ Queue :: Queue()
{} {}
Queue :: ~Queue() Queue :: ~Queue()
{} {
if (has_flag(Q_NOTIFY_REMOVE))
_notify->on_remove();
}
void Queue :: write(File &file) void Queue :: write(File &file)
{ {

View File

@ -16,10 +16,11 @@
* Enum defining flags that effect a Queue's behavior. * Enum defining flags that effect a Queue's behavior.
*/ */
enum queue_flags { enum queue_flags {
Q_ENABLED = (1 << 0), /**< Queue is enabled. */ Q_ENABLED = (1 << 0), /**< Queue is enabled. */
Q_RANDOM = (1 << 1), /**< Queue will pick songs randomly. */ Q_RANDOM = (1 << 1), /**< Queue will pick songs randomly. */
Q_REPEAT = (1 << 2), /**< Queue will not remove songs when picked. */ Q_REPEAT = (1 << 2), /**< Queue will not remove songs when picked. */
Q_NO_SORT = (1 << 3), /**< Queue will not be sorted. */ Q_NO_SORT = (1 << 3), /**< Queue will not be sorted. */
Q_NOTIFY_REMOVE = (1 << 4), /**< Queue will send a notification when removed. */
}; };
@ -47,6 +48,11 @@ class QNotifier {
public: public:
QNotifier() {}; /**< Notifier constructor. */ QNotifier() {}; /**< Notifier constructor. */
/**
* Called when a queue is removed and destroyed.
*/
virtual void on_remove() = 0;
/** /**
* Called when a track is added to a queue. * Called when a track is added to a queue.
* *
@ -108,8 +114,8 @@ public:
*/ */
Queue(unsigned int); Queue(unsigned int);
Queue(); /**< Default queue constructor. */ Queue(); /**< Default queue constructor. */
~Queue(); /**< Queue destructor. */ virtual ~Queue(); /**< Queue destructor. */
/** /**
* Write a queue to disk. * Write a queue to disk.

View File

@ -8,6 +8,7 @@
#include <tests/test.h> #include <tests/test.h>
unsigned int count_destroyed = 0;
unsigned int count_add = 0; unsigned int count_add = 0;
unsigned int count_del = 0; unsigned int count_del = 0;
unsigned int count_updated = 0; unsigned int count_updated = 0;
@ -19,6 +20,7 @@ Track *TRACK_NULL = NULL;
static class TestNotifier : public QNotifier { static class TestNotifier : public QNotifier {
public: public:
TestNotifier() : QNotifier() {} TestNotifier() : QNotifier() {}
void on_remove() { count_destroyed++; }
void on_track_added(unsigned int i) { count_add++; } void on_track_added(unsigned int i) { count_add++; }
void on_track_removed(unsigned int i) { count_del++; } void on_track_removed(unsigned int i) { count_del++; }
void on_track_updated(unsigned int i) { count_updated++; last_update = i; } void on_track_updated(unsigned int i) { count_updated++; last_update = i; }
@ -81,6 +83,28 @@ void test_flags()
test_equal(q.has_flag(Q_RANDOM), true); test_equal(q.has_flag(Q_RANDOM), true);
test_equal(q.has_flag(Q_REPEAT), true); test_equal(q.has_flag(Q_REPEAT), true);
test_equal(q.has_flag(Q_NO_SORT), false); test_equal(q.has_flag(Q_NO_SORT), false);
test_equal(q.has_flag(Q_NOTIFY_REMOVE), false);
}
unsigned int _test_destructor_null(unsigned int i)
{
TestQueue *q = new TestQueue(0);
delete q;
return (count_destroyed == 0) ? LOOP_PASSED : LOOP_FAILED;
}
unsigned int _test_destructor_flag(unsigned int i)
{
TestQueue *q = new TestQueue(Q_NOTIFY_REMOVE);
delete q;
return (count_destroyed == (i + 1)) ? LOOP_PASSED : LOOP_FAILED;
}
void test_destructor()
{
test_for_each(0, 10, 1, _test_destructor_null);
test_for_each(0, 10, 1, _test_destructor_flag);
} }
@ -362,6 +386,7 @@ int main(int argc, char **argv)
test :: run("Queue Default Constructor Test", test_default); test :: run("Queue Default Constructor Test", test_default);
test :: run("Queue Constructor Test", test_constructor); test :: run("Queue Constructor Test", test_constructor);
test :: run("Queue Flag Test", test_flags); test :: run("Queue Flag Test", test_flags);
test :: run("Queue Destructor Test", test_destructor);
test :: run("Queue Add and Remove Test", test_add_remove); test :: run("Queue Add and Remove Test", test_add_remove);
test :: run("Queue Track Updated Test", test_updated); test :: run("Queue Track Updated Test", test_updated);
test :: run("Queue Pick Next Test", test_next); test :: run("Queue Pick Next Test", test_next);