2 * This file is part of DisOrder.
3 * Copyright (C) 2004-2008 Richard Kettlewell
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 /** @file server/queue-ops.c
19 * @brief Track queues (server-specific code)
21 #include "disorder-server.h"
23 static int find_in_list(struct queue_entry *needle,
24 int nqs, struct queue_entry **qs) {
27 for(n = 0; n < nqs; ++n)
33 static int id_in_use(const char *id) {
34 struct queue_entry *q;
36 for(q = qhead.next; q != &qhead; q = q->next)
37 if(!strcmp(id, q->id))
42 static void queue_id(struct queue_entry *q) {
51 /** @brief Add a track to the queue
52 * @param track Track to add
53 * @param submitter Who added it, or NULL
54 * @param where Where to add it
55 * @param target ID to add after for @ref WHERE_AFTER
56 * @param origin Track origin
57 * @return New queue entry or NULL
59 * The queue is NOT saved to disk.
61 * NULL can only be returned if @ref WHERE_AFTER is used with an invalid
64 struct queue_entry *queue_add(const char *track, const char *submitter,
65 int where, const char *target,
66 enum track_origin origin) {
67 struct queue_entry *q, *beforeme, *afterme;
69 q = xmalloc(sizeof *q);
70 q->track = xstrdup(track);
71 q->submitter = submitter ? xstrdup(submitter) : 0;
72 q->state = playing_unplayed;
79 queue_insert_entry(&qhead, q);
82 queue_insert_entry(qhead.prev, q);
84 case WHERE_BEFORE_RANDOM:
85 /* We want to find the point in the queue before the block of random tracks
88 while(beforeme->prev != &qhead
89 && beforeme->prev->origin == origin_random)
90 beforeme = beforeme->prev;
91 queue_insert_entry(beforeme->prev, q);
95 /* Insert at start of queue */
98 /* Insert after a specific track */
100 while(afterme != &qhead && strcmp(afterme->id, target))
101 afterme = afterme->next;
102 if(afterme == &qhead)
105 queue_insert_entry(afterme, q);
108 /* submitter will be a null pointer for a scratch */
110 notify_queue(track, submitter);
111 eventlog_raw("queue", queue_marshall(q), (const char *)0);
115 int queue_move(struct queue_entry *q, int delta, const char *who) {
119 /* not the most efficient approach but hopefuly relatively comprehensible:
120 * the idea is that for each step we determine which nodes are affected, and
121 * fill in all the links starting at the 'prev' end and moving towards the
124 while(delta > 0 && q->prev != &qhead) {
125 struct queue_entry *n, *p, *pp;
140 while(delta < 0 && q->next != &qhead) {
141 struct queue_entry *n, *p, *nn;
157 disorder_info("user %s moved %s", who, q->id);
158 notify_queue_move(q->track, who);
159 sprintf(buffer, "%d", moved);
160 eventlog("moved", who, (char *)0);
166 void queue_moveafter(struct queue_entry *target,
167 int nqs, struct queue_entry **qs,
169 struct queue_entry *q;
176 while(find_in_list(target, nqs, qs))
177 target = target->prev;
179 for(n = 0; n < nqs; ++n) {
181 queue_delete_entry(q);
182 queue_insert_entry(target, q);
184 /* Log the individual tracks */
185 disorder_info("user %s moved %s", who, q->id);
186 notify_queue_move(q->track, who);
188 /* Report that the queue changed to the event log */
189 eventlog("moved", who, (char *)0);
192 void queue_remove(struct queue_entry *which, const char *who) {
194 disorder_info("user %s removed %s", who, which->id);
195 notify_queue_move(which->track, who);
197 eventlog("removed", which->id, who, (const char *)0);
198 queue_delete_entry(which);
201 void queue_played(struct queue_entry *q) {
202 while(pcount && pcount >= config->history) {
203 eventlog("recent_removed", phead.next->id, (char *)0);
204 queue_delete_entry(phead.next);
207 if(config->history) {
208 eventlog_raw("recent_added", queue_marshall(q), (char *)0);
209 queue_insert_entry(phead.prev, q);