chiark / gitweb /
Columns are now resizable and wide columns are ellipsized. Columns
[disorder] / disobedience / queue.c
CommitLineData
460b9539 1/*
2 * This file is part of DisOrder
fb009628 3 * Copyright (C) 2006-2008 Richard Kettlewell
460b9539 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 */
717ba987 20#include "disobedience.h"
c133bd3c 21#include "queue-generic.h"
460b9539 22
c133bd3c
RK
23/** @brief The actual queue */
24static struct queue_entry *actual_queue;
25static struct queue_entry *actual_playing_track;
460b9539 26
c133bd3c
RK
27/** @brief The playing track */
28struct queue_entry *playing_track;
4eb1f430 29
c133bd3c
RK
30/** @brief When we last got the playing track */
31time_t last_playing;
460b9539 32
c133bd3c
RK
33/** @brief Called when either the actual queue or the playing track change */
34static void queue_playing_changed(void) {
35 struct queue_entry *q = xmalloc(sizeof *q);
460b9539 36
c133bd3c
RK
37 *q = *actual_playing_track;
38 q->next = actual_queue;
39 playing_track = q;
40 time(&last_playing); /* for column_length() */
41 ql_new_queue(&ql_queue, q);
42 /* Tell anyone who cares */
43 event_raise("queue-list-changed", q);
44 event_raise("playing-track-changed", q);
460b9539 45}
46
c133bd3c
RK
47/** @brief Update the queue itself */
48static void queue_completed(void attribute((unused)) *v,
49 const char *error,
50 struct queue_entry *q) {
06bfbba4 51 if(error) {
3035257f 52 popup_protocol_error(0, error);
c133bd3c 53 return;
3035257f 54 }
c133bd3c
RK
55 actual_queue = q;
56 queue_playing_changed();
460b9539 57}
58
c133bd3c 59/** @brief Update the playing track */
460b9539 60static void playing_completed(void attribute((unused)) *v,
3035257f 61 const char *error,
460b9539 62 struct queue_entry *q) {
c133bd3c 63 if(error) {
3035257f 64 popup_protocol_error(0, error);
c133bd3c 65 return;
460b9539 66 }
c133bd3c
RK
67 actual_playing_track = q;
68 queue_playing_changed();
460b9539 69}
70
c133bd3c 71/** @brief Schedule an update to the queue
717ba987 72 *
c133bd3c
RK
73 * Called whenever a track is added to it or removed from it.
74 */
75static void queue_changed(const char attribute((unused)) *event,
76 void attribute((unused)) *eventdata,
77 void attribute((unused)) *callbackdata) {
78 D(("queue_changed"));
79 gtk_label_set_text(GTK_LABEL(report_label), "updating queue");
80 disorder_eclient_queue(client, queue_completed, 0);
460b9539 81}
82
c133bd3c 83/** @brief Schedule an update to the playing track
717ba987 84 *
c133bd3c 85 * Called whenever it changes
3ffa2d15 86 */
a8cd6f84 87static void playing_changed(const char attribute((unused)) *event,
c133bd3c
RK
88 void attribute((unused)) *eventdata,
89 void attribute((unused)) *callbackdata) {
a8cd6f84 90 D(("playing_changed"));
3ffa2d15
RK
91 gtk_label_set_text(GTK_LABEL(report_label), "updating playing track");
92 disorder_eclient_playing(client, playing_completed, 0);
93}
94
c133bd3c 95/** @brief Called regularly
717ba987 96 *
c133bd3c 97 * Updates the played-so-far field
717ba987 98 */
c133bd3c
RK
99static gboolean playing_periodic(gpointer attribute((unused)) data) {
100 /* If there's a track playing, update its row */
101 if(playing_track)
102 ql_update_row(playing_track, 0);
103 return TRUE;
460b9539 104}
105
c133bd3c
RK
106/** @brief Called at startup */
107static void queue_init(void) {
108 /* Arrange a callback whenever the playing state changes */
109 event_register("playing-changed", playing_changed, 0);
110 event_register("pause-changed", playing_changed, 0);
111 /* ...and when the queue changes */
112 event_register("queue-changed", queue_changed, 0);
113 /* ...and once a second anyway */
114 g_timeout_add(1000/*ms*/, playing_periodic, 0);
460b9539 115}
116
c133bd3c
RK
117/** @brief Columns for the queue */
118static const struct queue_column queue_columns[] = {
b0b15b7c 119 { "When", column_when, 0, COL_RIGHT },
c133bd3c 120 { "Who", column_who, 0, 0 },
b0b15b7c
RK
121 { "Artist", column_namepart, "artist", COL_EXPAND|COL_ELLIPSIZE },
122 { "Album", column_namepart, "album", COL_EXPAND|COL_ELLIPSIZE },
123 { "Title", column_namepart, "title", COL_EXPAND|COL_ELLIPSIZE },
124 { "Length", column_length, 0, COL_RIGHT }
460b9539 125};
126
c133bd3c
RK
127/** @brief Pop-up menu for queue */
128static struct queue_menuitem queue_menuitems[] = {
129 { "Track properties", ql_properties_activate, ql_properties_sensitive, 0, 0 },
130 { "Select all tracks", ql_selectall_activate, ql_selectall_sensitive, 0, 0 },
131 { "Deselect all tracks", ql_selectnone_activate, ql_selectnone_sensitive, 0, 0 },
132 { "Scratch track", ql_scratch_activate, ql_scratch_sensitive, 0, 0 },
133 { "Remove track from queue", ql_remove_activate, ql_remove_sensitive, 0, 0 },
4eb1f430
RK
134};
135
c133bd3c
RK
136struct queuelike ql_queue = {
137 .init = queue_init,
138 .columns = queue_columns,
139 .ncolumns = sizeof queue_columns / sizeof *queue_columns,
140 .menuitems = queue_menuitems,
141 .nmenuitems = sizeof queue_menuitems / sizeof *queue_menuitems,
460b9539 142};
143
c133bd3c
RK
144GtkWidget *queue_widget(void) {
145 return init_queuelike(&ql_queue);
146}
460b9539 147
717ba987 148/** @brief Return nonzero if @p track is in the queue */
460b9539 149int queued(const char *track) {
150 struct queue_entry *q;
151
152 D(("queued %s", track));
153 for(q = ql_queue.q; q; q = q->next)
154 if(!strcmp(q->track, track))
155 return 1;
156 return 0;
157}
158
159/*
160Local Variables:
161c-basic-offset:2
162comment-column:40
163fill-column:79
164indent-tabs-mode:nil
165End:
166*/