chiark / gitweb /
Disobedience drag+drop code is now part of queue-generic.* (and still
[disorder] / disobedience / queue.c
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 "popup.h"
22 #include "queue-generic.h"
23
24 /** @brief The actual queue */
25 static struct queue_entry *actual_queue;
26 static struct queue_entry *actual_playing_track;
27
28 /** @brief The playing track */
29 struct queue_entry *playing_track;
30
31 /** @brief When we last got the playing track */
32 time_t last_playing;
33
34 static void queue_completed(void *v,
35                             const char *err,
36                             struct queue_entry *q);
37 static void playing_completed(void *v,
38                               const char *err,
39                               struct queue_entry *q);
40
41 /** @brief Called when either the actual queue or the playing track change */
42 static void queue_playing_changed(void) {
43
44   /* Check that the playing track isn't in the queue.  There's a race here due
45    * to the fact that we issue the two commands at slightly different times.
46    * If it goes wrong we re-issue and try again, so that we never offer up an
47    * inconsistent state. */
48   if(actual_playing_track) {
49     struct queue_entry *q;
50     for(q = actual_queue; q; q = q->next)
51       if(!strcmp(q->id, actual_playing_track->id))
52         break;
53     if(q) {
54       disorder_eclient_playing(client, playing_completed, 0);
55       disorder_eclient_queue(client, queue_completed, 0);
56       return;
57     }
58   }
59   
60   struct queue_entry *q = xmalloc(sizeof *q);
61   if(actual_playing_track) {
62     *q = *actual_playing_track;
63     q->next = actual_queue;
64     playing_track = q;
65   } else {
66     playing_track = NULL;
67     q = actual_queue;
68   }
69   time(&last_playing);          /* for column_length() */
70   ql_new_queue(&ql_queue, q);
71   /* Tell anyone who cares */
72   event_raise("queue-list-changed", q);
73   event_raise("playing-track-changed", q);
74 }
75
76 /** @brief Update the queue itself */
77 static void queue_completed(void attribute((unused)) *v,
78                             const char *err,
79                             struct queue_entry *q) {
80   if(err) {
81     popup_protocol_error(0, err);
82     return;
83   }
84   actual_queue = q;
85   queue_playing_changed();
86 }
87
88 /** @brief Update the playing track */
89 static void playing_completed(void attribute((unused)) *v,
90                               const char *err,
91                               struct queue_entry *q) {
92   if(err) {
93     popup_protocol_error(0, err);
94     return;
95   }
96   actual_playing_track = q;
97   queue_playing_changed();
98 }
99
100 /** @brief Schedule an update to the queue
101  *
102  * Called whenever a track is added to it or removed from it.
103  */
104 static void queue_changed(const char attribute((unused)) *event,
105                            void  attribute((unused)) *eventdata,
106                            void  attribute((unused)) *callbackdata) {
107   D(("queue_changed"));
108   gtk_label_set_text(GTK_LABEL(report_label), "updating queue");
109   disorder_eclient_queue(client, queue_completed, 0);
110 }
111
112 /** @brief Schedule an update to the playing track
113  *
114  * Called whenever it changes
115  */
116 static void playing_changed(const char attribute((unused)) *event,
117                             void  attribute((unused)) *eventdata,
118                             void  attribute((unused)) *callbackdata) {
119   D(("playing_changed"));
120   gtk_label_set_text(GTK_LABEL(report_label), "updating playing track");
121   disorder_eclient_playing(client, playing_completed, 0);
122 }
123
124 /** @brief Called regularly
125  *
126  * Updates the played-so-far field
127  */
128 static gboolean playing_periodic(gpointer attribute((unused)) data) {
129   /* If there's a track playing, update its row */
130   if(playing_track)
131     ql_update_row(playing_track, 0);
132   return TRUE;
133 }
134
135 /** @brief Called at startup */
136 static void queue_init(void) {
137   /* Arrange a callback whenever the playing state changes */ 
138   event_register("playing-changed", playing_changed, 0);
139   /* We reget both playing track and queue at pause/resume so that start times
140    * can be computed correctly */
141   event_register("pause-changed", playing_changed, 0);
142   event_register("pause-changed", queue_changed, 0);
143   /* Reget the queue whenever it changes */
144   event_register("queue-changed", queue_changed, 0);
145   /* ...and once a second anyway */
146   g_timeout_add(1000/*ms*/, playing_periodic, 0);
147 }
148
149 static void queue_move_completed(void attribute((unused)) *v,
150                                  const char *err) {
151   if(err) {
152     popup_protocol_error(0, err);
153     return;
154   }
155   /* The log should tell us the queue changed so we do no more here */
156 }
157
158 /** @brief Called when drag+drop completes */
159 static void queue_drop(int src, int dst) {
160   struct queue_entry *sq, *dq;
161   int n;
162
163   //fprintf(stderr, "queue_drop %d -> %d\n", src, dst);
164   if(playing_track) {
165     /* If there's a playing track then you can't drag it anywhere  */
166     if(src == 0) {
167       //fprintf(stderr, "cannot drag playing track\n");
168       queue_playing_changed();
169       return;
170     }
171     /* If you try to drop before the playing track we assume you missed and
172      * mean after instead */
173     if(!dst)
174       dst = 1;
175     //fprintf(stderr, "...adjusted to %d -> %d\n\n", src, dst);
176   }
177   /* Find the entry to move */
178   for(n = 0, sq = ql_queue.q; n < src; ++n)
179     sq = sq->next;
180   /*fprintf(stderr, "source=%s (%s)\n",
181           sq->id, sq->track);*/
182   const int after = dst - 1;
183   if(after == -1)
184     dq = 0;
185   else
186     /* Find the entry to insert after */
187     for(n = 0, dq = ql_queue.q; n < after; ++n)
188       dq = dq->next;
189   if(dq == playing_track)
190     dq = 0;
191 #if 0
192   if(dq)
193     fprintf(stderr, "after=%s (%s)\n",
194             dq->id, dq->track);
195   else
196     fprintf(stderr, "after=NULL\n");
197 #endif
198   disorder_eclient_moveafter(client,
199                              dq ? dq->id : "",
200                              1, &sq->id,
201                              queue_move_completed, NULL);
202 }
203
204 /** @brief Columns for the queue */
205 static const struct queue_column queue_columns[] = {
206   { "When",   column_when,     0,        COL_RIGHT },
207   { "Who",    column_who,      0,        0 },
208   { "Artist", column_namepart, "artist", COL_EXPAND|COL_ELLIPSIZE },
209   { "Album",  column_namepart, "album",  COL_EXPAND|COL_ELLIPSIZE },
210   { "Title",  column_namepart, "title",  COL_EXPAND|COL_ELLIPSIZE },
211   { "Length", column_length,   0,        COL_RIGHT }
212 };
213
214 /** @brief Pop-up menu for queue */
215 static struct menuitem queue_menuitems[] = {
216   { "Track properties", ql_properties_activate, ql_properties_sensitive, 0, 0 },
217   { "Select all tracks", ql_selectall_activate, ql_selectall_sensitive, 0, 0 },
218   { "Deselect all tracks", ql_selectnone_activate, ql_selectnone_sensitive, 0, 0 },
219   { "Scratch playing track", ql_scratch_activate, ql_scratch_sensitive, 0, 0 },
220   { "Remove track from queue", ql_remove_activate, ql_remove_sensitive, 0, 0 },
221 };
222
223 struct queuelike ql_queue = {
224   .name = "queue",
225   .init = queue_init,
226   .columns = queue_columns,
227   .ncolumns = sizeof queue_columns / sizeof *queue_columns,
228   .menuitems = queue_menuitems,
229   .nmenuitems = sizeof queue_menuitems / sizeof *queue_menuitems,
230   .drop = queue_drop
231 };
232
233 /** @brief Called when a key is pressed in the queue tree view */
234 static gboolean queue_key_press(GtkWidget attribute((unused)) *widget,
235                                 GdkEventKey *event,
236                                 gpointer user_data) {
237   /*fprintf(stderr, "queue_key_press type=%d state=%#x keyval=%#x\n",
238           event->type, event->state, event->keyval);*/
239   switch(event->keyval) {
240   case GDK_BackSpace:
241   case GDK_Delete:
242     if(event->state)
243       break;                            /* Only take unmodified DEL/<-- */
244     ql_remove_activate(0, user_data);
245     return TRUE;                        /* Do not propagate */
246   }
247   return FALSE;                         /* Propagate */
248 }
249
250 GtkWidget *queue_widget(void) {
251   GtkWidget *const w = init_queuelike(&ql_queue);
252
253   /* Catch keypresses */
254   g_signal_connect(ql_queue.view, "key-press-event",
255                    G_CALLBACK(queue_key_press), &ql_queue);
256   return w;
257 }
258
259 /** @brief Return nonzero if @p track is in the queue */
260 int queued(const char *track) {
261   struct queue_entry *q;
262
263   D(("queued %s", track));
264   /* Queue will contain resolved name */
265   track = namepart_resolve(track);
266   for(q = ql_queue.q; q; q = q->next)
267     if(!strcmp(q->track, track))
268       return 1;
269   return 0;
270 }
271
272 /*
273 Local Variables:
274 c-basic-offset:2
275 comment-column:40
276 fill-column:79
277 indent-tabs-mode:nil
278 End:
279 */