chiark / gitweb /
Don't allocate per-queue tabtype. In fact the allocate version broken
[disorder] / disobedience / queue.c
... / ...
CommitLineData
1/*
2 * This file is part of DisOrder
3 * Copyright (C) 2006-2008 Richard Kettlewell
4 *
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.
9 *
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.
14 *
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
18 * USA
19 */
20#include "disobedience.h"
21#include "queue-generic.h"
22
23/** @brief The actual queue */
24static struct queue_entry *actual_queue;
25static struct queue_entry *actual_playing_track;
26
27/** @brief The playing track */
28struct queue_entry *playing_track;
29
30/** @brief When we last got the playing track */
31time_t last_playing;
32
33static void queue_completed(void *v,
34 const char *error,
35 struct queue_entry *q);
36static void playing_completed(void *v,
37 const char *error,
38 struct queue_entry *q);
39
40/** @brief Called when either the actual queue or the playing track change */
41static void queue_playing_changed(void) {
42
43 /* Check that the playing track isn't in the queue. There's a race here due
44 * to the fact that we issue the two commands at slightly different times.
45 * If it goes wrong we re-issue and try again, so that we never offer up an
46 * inconsistent state. */
47 if(actual_playing_track) {
48 struct queue_entry *q;
49 for(q = actual_queue; q; q = q->next)
50 if(!strcmp(q->id, actual_playing_track->id))
51 break;
52 if(q) {
53 disorder_eclient_playing(client, playing_completed, 0);
54 disorder_eclient_queue(client, queue_completed, 0);
55 return;
56 }
57 }
58
59 struct queue_entry *q = xmalloc(sizeof *q);
60 if(actual_playing_track) {
61 *q = *actual_playing_track;
62 q->next = actual_queue;
63 playing_track = q;
64 } else {
65 playing_track = NULL;
66 q = actual_queue;
67 }
68 time(&last_playing); /* for column_length() */
69 ql_new_queue(&ql_queue, q);
70 /* Tell anyone who cares */
71 event_raise("queue-list-changed", q);
72 event_raise("playing-track-changed", q);
73}
74
75/** @brief Update the queue itself */
76static void queue_completed(void attribute((unused)) *v,
77 const char *error,
78 struct queue_entry *q) {
79 if(error) {
80 popup_protocol_error(0, error);
81 return;
82 }
83 actual_queue = q;
84 queue_playing_changed();
85}
86
87/** @brief Update the playing track */
88static void playing_completed(void attribute((unused)) *v,
89 const char *error,
90 struct queue_entry *q) {
91 if(error) {
92 popup_protocol_error(0, error);
93 return;
94 }
95 actual_playing_track = q;
96 queue_playing_changed();
97}
98
99/** @brief Schedule an update to the queue
100 *
101 * Called whenever a track is added to it or removed from it.
102 */
103static void queue_changed(const char attribute((unused)) *event,
104 void attribute((unused)) *eventdata,
105 void attribute((unused)) *callbackdata) {
106 D(("queue_changed"));
107 gtk_label_set_text(GTK_LABEL(report_label), "updating queue");
108 disorder_eclient_queue(client, queue_completed, 0);
109}
110
111/** @brief Schedule an update to the playing track
112 *
113 * Called whenever it changes
114 */
115static void playing_changed(const char attribute((unused)) *event,
116 void attribute((unused)) *eventdata,
117 void attribute((unused)) *callbackdata) {
118 D(("playing_changed"));
119 gtk_label_set_text(GTK_LABEL(report_label), "updating playing track");
120 disorder_eclient_playing(client, playing_completed, 0);
121}
122
123/** @brief Called regularly
124 *
125 * Updates the played-so-far field
126 */
127static gboolean playing_periodic(gpointer attribute((unused)) data) {
128 /* If there's a track playing, update its row */
129 if(playing_track)
130 ql_update_row(playing_track, 0);
131 return TRUE;
132}
133
134/** @brief Called at startup */
135static void queue_init(void) {
136 /* Arrange a callback whenever the playing state changes */
137 event_register("playing-changed", playing_changed, 0);
138 /* We reget both playing track and queue at pause/resume so that start times
139 * can be computed correctly */
140 event_register("pause-changed", playing_changed, 0);
141 event_register("pause-changed", queue_changed, 0);
142 /* Reget the queue whenever it changes */
143 event_register("queue-changed", queue_changed, 0);
144 /* ...and once a second anyway */
145 g_timeout_add(1000/*ms*/, playing_periodic, 0);
146}
147
148/** @brief Columns for the queue */
149static const struct queue_column queue_columns[] = {
150 { "When", column_when, 0, COL_RIGHT },
151 { "Who", column_who, 0, 0 },
152 { "Artist", column_namepart, "artist", COL_EXPAND|COL_ELLIPSIZE },
153 { "Album", column_namepart, "album", COL_EXPAND|COL_ELLIPSIZE },
154 { "Title", column_namepart, "title", COL_EXPAND|COL_ELLIPSIZE },
155 { "Length", column_length, 0, COL_RIGHT }
156};
157
158/** @brief Pop-up menu for queue */
159static struct queue_menuitem queue_menuitems[] = {
160 { "Track properties", ql_properties_activate, ql_properties_sensitive, 0, 0 },
161 { "Select all tracks", ql_selectall_activate, ql_selectall_sensitive, 0, 0 },
162 { "Deselect all tracks", ql_selectnone_activate, ql_selectnone_sensitive, 0, 0 },
163 { "Scratch playing track", ql_scratch_activate, ql_scratch_sensitive, 0, 0 },
164 { "Remove track from queue", ql_remove_activate, ql_remove_sensitive, 0, 0 },
165};
166
167struct queuelike ql_queue = {
168 .name = "queue",
169 .init = queue_init,
170 .columns = queue_columns,
171 .ncolumns = sizeof queue_columns / sizeof *queue_columns,
172 .menuitems = queue_menuitems,
173 .nmenuitems = sizeof queue_menuitems / sizeof *queue_menuitems
174};
175
176GtkWidget *queue_widget(void) {
177 return init_queuelike(&ql_queue);
178}
179
180/** @brief Return nonzero if @p track is in the queue */
181int queued(const char *track) {
182 struct queue_entry *q;
183
184 D(("queued %s", track));
185 for(q = ql_queue.q; q; q = q->next)
186 if(!strcmp(q->track, track))
187 return 1;
188 return 0;
189}
190
191/*
192Local Variables:
193c-basic-offset:2
194comment-column:40
195fill-column:79
196indent-tabs-mode:nil
197End:
198*/