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 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * 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, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 #include "disorder-server.h"
22 static int find_in_list(struct queue_entry
*needle
,
23 int nqs
, struct queue_entry
**qs
) {
26 for(n
= 0; n
< nqs
; ++n
)
32 static int id_in_use(const char *id
) {
33 struct queue_entry
*q
;
35 for(q
= qhead
.next
; q
!= &qhead
; q
= q
->next
)
36 if(!strcmp(id
, q
->id
))
41 static void queue_id(struct queue_entry
*q
) {
50 struct queue_entry
*queue_add(const char *track
, const char *submitter
,
52 struct queue_entry
*q
, *beforeme
;
54 q
= xmalloc(sizeof *q
);
55 q
->track
= xstrdup(track
);
56 q
->submitter
= submitter ?
xstrdup(submitter
) : 0;
57 q
->state
= playing_unplayed
;
62 queue_insert_entry(&qhead
, q
);
65 queue_insert_entry(qhead
.prev
, q
);
67 case WHERE_BEFORE_RANDOM
:
68 /* We want to find the point in the queue before the block of random tracks
71 while(beforeme
->prev
!= &qhead
72 && beforeme
->prev
->state
== playing_random
)
73 beforeme
= beforeme
->prev
;
74 queue_insert_entry(beforeme
->prev
, q
);
77 /* submitter will be a null pointer for a scratch */
79 notify_queue(track
, submitter
);
80 eventlog_raw("queue", queue_marshall(q
), (const char *)0);
84 int queue_move(struct queue_entry
*q
, int delta
, const char *who
) {
88 /* not the most efficient approach but hopefuly relatively comprehensible:
89 * the idea is that for each step we determine which nodes are affected, and
90 * fill in all the links starting at the 'prev' end and moving towards the
93 while(delta
> 0 && q
->prev
!= &qhead
) {
94 struct queue_entry
*n
, *p
, *pp
;
109 while(delta
< 0 && q
->next
!= &qhead
) {
110 struct queue_entry
*n
, *p
, *nn
;
126 info("user %s moved %s", who
, q
->id
);
127 notify_queue_move(q
->track
, who
);
128 sprintf(buffer
, "%d", moved
);
129 eventlog("moved", who
, (char *)0);
135 void queue_moveafter(struct queue_entry
*target
,
136 int nqs
, struct queue_entry
**qs
,
138 struct queue_entry
*q
;
145 while(find_in_list(target
, nqs
, qs
))
146 target
= target
->prev
;
148 for(n
= 0; n
< nqs
; ++n
) {
150 queue_delete_entry(q
);
151 queue_insert_entry(target
, q
);
153 /* Log the individual tracks */
154 info("user %s moved %s", who
, q
->id
);
155 notify_queue_move(q
->track
, who
);
157 /* Report that the queue changed to the event log */
158 eventlog("moved", who
, (char *)0);
161 void queue_remove(struct queue_entry
*which
, const char *who
) {
163 info("user %s removed %s", who
, which
->id
);
164 notify_queue_move(which
->track
, who
);
166 eventlog("removed", which
->id
, who
, (const char *)0);
167 queue_delete_entry(which
);
170 void queue_played(struct queue_entry
*q
) {
171 while(pcount
&& pcount
>= config
->history
) {
172 eventlog("recent_removed", phead
.next
->id
, (char *)0);
173 queue_delete_entry(phead
.next
);
176 if(config
->history
) {
177 eventlog_raw("recent_added", queue_marshall(q
), (char *)0);
178 queue_insert_entry(phead
.prev
, q
);