2014-10-17 09:42:45 -04:00
|
|
|
/**
|
2013-12-31 15:44:44 -05:00
|
|
|
* Copyright 2013 (c) Anna Schumaker.
|
|
|
|
*/
|
2014-06-21 09:37:47 -04:00
|
|
|
#ifndef OCARINA_CORE_QUEUE_H
|
|
|
|
#define OCARINA_CORE_QUEUE_H
|
2013-12-31 15:44:44 -05:00
|
|
|
|
2015-10-09 13:55:34 -04:00
|
|
|
extern "C" {
|
2014-06-05 10:19:22 -04:00
|
|
|
#include <core/file.h>
|
2015-10-09 13:55:34 -04:00
|
|
|
}
|
2014-12-02 08:24:36 -05:00
|
|
|
#include <core/tags/track.h>
|
2014-05-11 19:52:08 -04:00
|
|
|
|
2013-12-31 15:44:44 -05:00
|
|
|
#include <vector>
|
2014-01-26 12:12:01 -05:00
|
|
|
#include <list>
|
2013-12-31 15:44:44 -05:00
|
|
|
|
2014-12-07 09:57:43 -05:00
|
|
|
|
2014-10-17 09:42:45 -04:00
|
|
|
/**
|
2014-12-07 09:57:43 -05:00
|
|
|
* Enum defining flags that effect a Queue's behavior.
|
2014-10-17 09:42:45 -04:00
|
|
|
*/
|
2014-04-01 20:28:19 -04:00
|
|
|
enum queue_flags {
|
2015-04-09 09:19:32 -04:00
|
|
|
Q_ENABLED = (1 << 0), /**< Queue is enabled. */
|
|
|
|
Q_RANDOM = (1 << 1), /**< Queue will pick songs randomly. */
|
|
|
|
Q_REPEAT = (1 << 2), /**< Queue will not remove songs when picked. */
|
|
|
|
Q_NO_SORT = (1 << 3), /**< Queue will not be sorted. */
|
2013-12-31 15:44:44 -05:00
|
|
|
};
|
|
|
|
|
2014-10-17 09:42:45 -04:00
|
|
|
|
2014-11-28 10:28:15 -05:00
|
|
|
/**
|
|
|
|
* Track fields that can be compared.
|
|
|
|
*/
|
|
|
|
enum sort_t {
|
2014-12-07 10:07:47 -05:00
|
|
|
SORT_ARTIST, /**< Sort tracks by Artist name. */
|
|
|
|
SORT_ALBUM, /**< Sort tracks by Album name. */
|
|
|
|
SORT_COUNT, /**< Sort tracks by Play count. */
|
|
|
|
SORT_GENRE, /**< Sort tracks by Genre. */
|
|
|
|
SORT_LENGTH, /**< Sort tracks by Track length. */
|
|
|
|
SORT_PLAYED, /**< Sort tracks by last played date. */
|
|
|
|
SORT_TITLE, /**< Sort tracks by Track title. */
|
|
|
|
SORT_TRACK, /**< Sort tracks by Track number. */
|
|
|
|
SORT_YEAR, /**< Sort tracks by Track year. */
|
2014-11-28 10:28:15 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-04-03 10:30:46 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class to assist in notifying the GUI of queue changes.
|
|
|
|
*/
|
|
|
|
class QNotifier {
|
|
|
|
public:
|
|
|
|
QNotifier() {}; /**< Notifier constructor. */
|
2015-04-03 11:35:23 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when a track is added to a queue.
|
|
|
|
*
|
|
|
|
* @param pos Position in the queue that the track was added.
|
|
|
|
*/
|
|
|
|
virtual void on_track_added(unsigned int) = 0;
|
2015-04-03 12:24:26 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when a track is removed from a queue.
|
|
|
|
*
|
|
|
|
* @param pos Position in the queue that the track was removed from.
|
|
|
|
*/
|
|
|
|
virtual void on_track_removed(unsigned int) = 0;
|
2015-04-03 20:54:45 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when a track has been updated.
|
|
|
|
*
|
|
|
|
* @param pos Position in the queue of the updated track.
|
|
|
|
*/
|
|
|
|
virtual void on_track_updated(unsigned int) = 0;
|
2015-04-03 10:30:46 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-10-17 09:42:45 -04:00
|
|
|
/**
|
|
|
|
* Struct for passing sort parameters.
|
|
|
|
*/
|
2014-01-26 14:44:56 -05:00
|
|
|
struct sort_info {
|
|
|
|
sort_t field;
|
|
|
|
bool ascending;
|
|
|
|
};
|
|
|
|
|
2014-10-17 09:42:45 -04:00
|
|
|
|
|
|
|
/**
|
2014-12-09 08:39:04 -05:00
|
|
|
* Queues are lists of songs that the user has requested to play next,
|
|
|
|
* although not necessarily in a specific order.
|
|
|
|
*
|
|
|
|
* When writing a Queue to disk: write out the _flags and size values
|
|
|
|
* first, followed by the list of track indexes.
|
|
|
|
*
|
|
|
|
* ... << _flags << _tracks.size() << tracks[N]->index() << ...;
|
2014-10-17 09:42:45 -04:00
|
|
|
*/
|
2014-04-01 20:28:19 -04:00
|
|
|
class Queue {
|
2014-04-13 12:26:32 -04:00
|
|
|
protected:
|
2014-12-09 08:39:04 -05:00
|
|
|
std :: vector <Track *> _tracks; /**< List of tracks in this queue. */
|
2015-04-03 10:26:53 -04:00
|
|
|
std :: vector <struct sort_info> _sort_order; /**< Current sort settings. */
|
2014-12-09 08:39:04 -05:00
|
|
|
unsigned int _cur; /**< The index of the last track played. */
|
|
|
|
unsigned int _flags; /**< Mask of queue_flags. */
|
|
|
|
unsigned int _length; /**< Total runtime of the queue. */
|
2015-04-03 10:30:46 -04:00
|
|
|
QNotifier *_notify; /**< Notification object associated with this queue. */
|
2013-12-31 15:44:44 -05:00
|
|
|
|
2014-03-29 11:44:39 -04:00
|
|
|
unsigned int find_sorted_id(Track *);
|
2014-05-11 10:53:48 -04:00
|
|
|
unsigned int _add_at(Track *, unsigned int);
|
2014-01-25 14:43:51 -05:00
|
|
|
|
2013-12-31 15:44:44 -05:00
|
|
|
public:
|
2014-10-17 09:42:45 -04:00
|
|
|
/**
|
2014-12-09 08:39:04 -05:00
|
|
|
* Alternate queue constructor.
|
|
|
|
*
|
|
|
|
* @param flags Create a queue with _flags set to flags.
|
2014-10-17 09:42:45 -04:00
|
|
|
*/
|
2014-04-13 18:38:49 -04:00
|
|
|
Queue(unsigned int);
|
2014-10-17 09:42:45 -04:00
|
|
|
|
2015-04-04 09:37:03 -04:00
|
|
|
Queue(); /**< Default queue constructor. */
|
|
|
|
virtual ~Queue(); /**< Queue destructor. */
|
2014-10-17 09:42:45 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Write a queue to disk.
|
2014-12-09 08:39:04 -05:00
|
|
|
*
|
|
|
|
* @param file File that Queue data will be written to.
|
2014-10-17 09:42:45 -04:00
|
|
|
*/
|
2015-09-10 08:00:42 -04:00
|
|
|
void write(file &);
|
2014-10-17 09:42:45 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Read a queue from disk.
|
2014-12-09 08:39:04 -05:00
|
|
|
*
|
|
|
|
* @param file File to read Queue data from.
|
2014-10-17 09:42:45 -04:00
|
|
|
*/
|
2015-09-10 08:00:42 -04:00
|
|
|
void read(file &);
|
2013-12-31 15:44:44 -05:00
|
|
|
|
2014-10-17 09:42:45 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a queue flag.
|
2014-12-09 08:39:04 -05:00
|
|
|
*
|
|
|
|
* @param flag queue_flag to set.
|
2014-10-17 09:42:45 -04:00
|
|
|
*/
|
2014-05-24 12:47:33 -04:00
|
|
|
virtual void set_flag(queue_flags);
|
2014-10-17 09:42:45 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear a queue flag.
|
2014-12-09 08:39:04 -05:00
|
|
|
*
|
|
|
|
* @param flag queue_flag to clear.
|
2014-10-17 09:42:45 -04:00
|
|
|
*/
|
2014-05-24 12:47:33 -04:00
|
|
|
virtual void unset_flag(queue_flags);
|
2014-10-17 09:42:45 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if a queue has a specific flag set.
|
2014-12-09 08:39:04 -05:00
|
|
|
*
|
|
|
|
* @param flag queue_flag to check.
|
|
|
|
* @return true if queue_flag is set and false otherwise.
|
2014-10-17 09:42:45 -04:00
|
|
|
*/
|
2014-04-13 12:26:32 -04:00
|
|
|
bool has_flag(queue_flags);
|
|
|
|
|
2014-10-17 09:42:45 -04:00
|
|
|
|
2015-04-03 10:30:46 -04:00
|
|
|
/**
|
|
|
|
* Set a notifier for this queue.
|
|
|
|
*
|
|
|
|
* @param notify notifier instance to associate with this queue.
|
|
|
|
*/
|
|
|
|
void set_notifier(QNotifier *);
|
|
|
|
|
|
|
|
|
2014-10-17 09:42:45 -04:00
|
|
|
/**
|
2014-12-09 08:39:04 -05:00
|
|
|
* Add a track to the queue, possibly matching the
|
|
|
|
* current sort order.
|
|
|
|
*
|
2014-10-17 09:42:45 -04:00
|
|
|
* @param track Track to add to the queue.
|
|
|
|
* @return The index of the track in the queue.
|
|
|
|
*/
|
2014-05-11 10:53:48 -04:00
|
|
|
virtual unsigned int add(Track *);
|
2014-10-17 09:42:45 -04:00
|
|
|
|
|
|
|
/**
|
2014-12-09 08:48:27 -05:00
|
|
|
* Remove a track based on its queue index.
|
2014-12-09 08:39:04 -05:00
|
|
|
*
|
2014-12-09 08:48:27 -05:00
|
|
|
* @param index Track index to remove from the queue.
|
2014-10-17 09:42:45 -04:00
|
|
|
*/
|
2014-12-09 08:48:27 -05:00
|
|
|
virtual void del(unsigned int);
|
2014-10-17 09:42:45 -04:00
|
|
|
|
|
|
|
/**
|
2014-12-09 08:48:27 -05:00
|
|
|
* Remove all instances of a track from the queue.
|
2014-12-09 08:39:04 -05:00
|
|
|
*
|
2014-12-09 08:48:27 -05:00
|
|
|
* @param track Track to remove from the queue.
|
2014-10-17 09:42:45 -04:00
|
|
|
*/
|
2014-12-09 08:48:27 -05:00
|
|
|
virtual void del(Track *);
|
2014-10-17 09:42:45 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Signal to the queue that a track has been updated.
|
2014-12-09 08:39:04 -05:00
|
|
|
*
|
2014-10-17 09:42:45 -04:00
|
|
|
* @param track The track that has been modified.
|
|
|
|
*/
|
2014-05-11 18:32:53 -04:00
|
|
|
void updated(Track *);
|
2014-10-17 09:42:45 -04:00
|
|
|
|
|
|
|
/**
|
2014-12-09 08:39:04 -05:00
|
|
|
* Pick the next track from the queue. If Q_RANDOM is set then a
|
|
|
|
* random Track will be returned, otherwise _cur will be incremented
|
|
|
|
* and used to pick the next Track. If Q_REPEAT is not set then
|
|
|
|
* the chosen track will be removed from the queue.
|
|
|
|
*
|
|
|
|
* @return The next Track on the queue, or NULL if the Queue is empty.
|
2014-10-17 09:42:45 -04:00
|
|
|
*/
|
2014-05-18 11:08:58 -04:00
|
|
|
Track *next();
|
2014-05-11 19:52:08 -04:00
|
|
|
|
2014-10-17 09:42:45 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Find the size of the queue.
|
2014-12-09 08:39:04 -05:00
|
|
|
*
|
2014-10-17 09:42:45 -04:00
|
|
|
* @return The number of tracks on the queue.
|
|
|
|
*/
|
2013-12-31 15:44:44 -05:00
|
|
|
unsigned int size();
|
2014-10-17 09:42:45 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Find the runtime of the queue.
|
2014-12-09 08:39:04 -05:00
|
|
|
*
|
2015-01-29 08:27:25 -05:00
|
|
|
* @return The runtime of the queue.
|
2014-10-17 09:42:45 -04:00
|
|
|
*/
|
2015-01-29 08:27:25 -05:00
|
|
|
unsigned int length();
|
2013-12-31 15:44:44 -05:00
|
|
|
|
2014-10-17 09:42:45 -04:00
|
|
|
|
|
|
|
/**
|
2014-12-09 08:39:04 -05:00
|
|
|
* Add a new sort field to the queue. If the field is already in the
|
|
|
|
* _sort_order then it's ascending or descending value will be toggled.
|
|
|
|
*
|
|
|
|
* If reset is set to True then the _sort_order will be cleared before
|
|
|
|
* adding the new value.
|
|
|
|
*
|
2014-10-17 09:42:45 -04:00
|
|
|
* @param field Field to sort by.
|
|
|
|
* @param reset Set to true if current sort data should be discarded.
|
|
|
|
*/
|
2014-05-24 12:47:33 -04:00
|
|
|
virtual void sort(sort_t, bool);
|
2014-10-17 09:42:45 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Access a track by index.
|
2014-12-09 08:39:04 -05:00
|
|
|
*
|
2014-10-17 09:42:45 -04:00
|
|
|
* @param index The index to look up.
|
|
|
|
* @return The track found at the requested index.
|
|
|
|
*/
|
2014-04-13 12:26:32 -04:00
|
|
|
Track *operator[](unsigned int);
|
2014-10-17 09:42:45 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Tell the queue that a track has been selected for playback without
|
2014-12-09 08:39:04 -05:00
|
|
|
* calling Queue :: next(). If Q_REPEAT is not set then the Track
|
|
|
|
* will be removed from the queue.
|
|
|
|
*
|
2014-10-17 09:42:45 -04:00
|
|
|
* @param index The index that is now playing.
|
|
|
|
*/
|
2014-05-18 14:24:46 -04:00
|
|
|
void track_selected(unsigned int);
|
2013-12-31 15:44:44 -05:00
|
|
|
};
|
|
|
|
|
2014-06-21 09:37:47 -04:00
|
|
|
#endif /* OCARINA_CORE_QUEUE_H */
|