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 #include "disorder-server.h"
20 static int find_in_list(struct queue_entry *needle,
21 int nqs, struct queue_entry **qs) {
24 for(n = 0; n < nqs; ++n)
30 static int id_in_use(const char *id) {
31 struct queue_entry *q;
33 for(q = qhead.next; q != &qhead; q = q->next)
34 if(!strcmp(id, q->id))
39 static void queue_id(struct queue_entry *q) {
48 struct queue_entry *queue_add(const char *track, const char *submitter,
50 struct queue_entry *q, *beforeme;
52 q = xmalloc(sizeof *q);
53 q->track = xstrdup(track);
54 q->submitter = submitter ? xstrdup(submitter) : 0;
55 q->state = playing_unplayed;
60 queue_insert_entry(&qhead, q);
63 queue_insert_entry(qhead.prev, q);
65 case WHERE_BEFORE_RANDOM:
66 /* We want to find the point in the queue before the block of random tracks
69 while(beforeme->prev != &qhead
70 && beforeme->prev->state == playing_random)
71 beforeme = beforeme->prev;
72 queue_insert_entry(beforeme->prev, q);
75 /* submitter will be a null pointer for a scratch */
77 notify_queue(track, submitter);
78 eventlog_raw("queue", queue_marshall(q), (const char *)0);
82 int queue_move(struct queue_entry *q, int delta, const char *who) {
86 /* not the most efficient approach but hopefuly relatively comprehensible:
87 * the idea is that for each step we determine which nodes are affected, and
88 * fill in all the links starting at the 'prev' end and moving towards the
91 while(delta > 0 && q->prev != &qhead) {
92 struct queue_entry *n, *p, *pp;
107 while(delta < 0 && q->next != &qhead) {
108 struct queue_entry *n, *p, *nn;
124 info("user %s moved %s", who, q->id);
125 notify_queue_move(q->track, who);
126 sprintf(buffer, "%d", moved);
127 eventlog("moved", who, (char *)0);
133 void queue_moveafter(struct queue_entry *target,
134 int nqs, struct queue_entry **qs,
136 struct queue_entry *q;
143 while(find_in_list(target, nqs, qs))
144 target = target->prev;
146 for(n = 0; n < nqs; ++n) {
148 queue_delete_entry(q);
149 queue_insert_entry(target, q);
151 /* Log the individual tracks */
152 info("user %s moved %s", who, q->id);
153 notify_queue_move(q->track, who);
155 /* Report that the queue changed to the event log */
156 eventlog("moved", who, (char *)0);
159 void queue_remove(struct queue_entry *which, const char *who) {
161 info("user %s removed %s", who, which->id);
162 notify_queue_move(which->track, who);
164 eventlog("removed", which->id, who, (const char *)0);
165 queue_delete_entry(which);
168 void queue_played(struct queue_entry *q) {
169 while(pcount && pcount >= config->history) {
170 eventlog("recent_removed", phead.next->id, (char *)0);
171 queue_delete_entry(phead.next);
174 if(config->history) {
175 eventlog_raw("recent_added", queue_marshall(q), (char *)0);
176 queue_insert_entry(phead.prev, q);