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