chiark / gitweb /
spot changes to playing track via monitor list
[disorder] / disobedience / queue.c
index f9b9ec8af98d5a8398eb3960a71bb060a55c92ee..65c09d30a0d37f178d3e24469418035a1efedc30 100644 (file)
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  * USA
  */
+/** @file disobedience/queue.c
+ * @brief Queue widgets
+ *
+ * This file provides both the queue widget and the recently-played widget.
+ */
 
 #include "disobedience.h"
 
@@ -849,7 +854,7 @@ static void remove_drag_targets(struct queuelike *ql) {
 static void redisplay_queue(struct queuelike *ql) {
   struct queue_entry *q;
   int row, col;
-  GList *c;
+  GList *c, *children;
   const char *name;
   GtkRequisition req;  
   GtkWidget *w;
@@ -858,7 +863,7 @@ static void redisplay_queue(struct queuelike *ql) {
 
   D(("redisplay_queue"));
   /* Eliminate all the existing widgets and start from scratch */
-  for(c = gtk_container_get_children(GTK_CONTAINER(ql->mainlayout));
+  for(c = children = gtk_container_get_children(GTK_CONTAINER(ql->mainlayout));
       c;
       c = c->next) {
     /* Destroy both the label and the eventbox */
@@ -869,6 +874,7 @@ static void redisplay_queue(struct queuelike *ql) {
     DW(event_box);
     gtk_widget_destroy(GTK_WIDGET(c->data));
   }
+  g_list_free(children);
   /* Adjust the row count */
   for(q = ql->q, ql->nrows = 0; q; q = q->next)
     ++ql->nrows;
@@ -1095,7 +1101,9 @@ static int scratch_sensitive(struct queuelike attribute((unused)) *ql,
                              struct menuitem attribute((unused)) *m,
                              struct queue_entry attribute((unused)) *q) {
   /* We can scratch if the playing track is selected */
-  return playing_track && selection_selected(ql->selection, playing_track->id);
+  return (playing_track
+          && (disorder_eclient_state(client) & DISORDER_CONNECTED)
+          && selection_selected(ql->selection, playing_track->id));
 }
 
 static void scratch_activate(GtkMenuItem attribute((unused)) *menuitem,
@@ -1109,7 +1117,10 @@ static int remove_sensitive(struct queuelike *ql,
                             struct queue_entry *q) {
   /* We can remove if we're hovering over a particular track or any non-playing
    * tracks are selected */
-  return (q && q != playing_track) || count_selected_nonplaying(ql);
+  return ((disorder_eclient_state(client) & DISORDER_CONNECTED)
+          && ((q
+               && q != playing_track)
+              || count_selected_nonplaying(ql)));
 }
 
 static void remove_activate(GtkMenuItem attribute((unused)) *menuitem,
@@ -1132,7 +1143,8 @@ static int properties_sensitive(struct queuelike *ql,
                                 struct menuitem attribute((unused)) *m,
                                 struct queue_entry attribute((unused)) *q) {
   /* "Properties" is sensitive if at least something is selected */
-  return hash_count(ql->selection) > 0;
+  return (hash_count(ql->selection) > 0
+          && (disorder_eclient_state(client) & DISORDER_CONNECTED));
 }
 
 static void properties_activate(GtkMenuItem attribute((unused)) *menuitem,
@@ -1183,17 +1195,31 @@ static gboolean adjust_sofar(gpointer attribute((unused)) data) {
 /* Popup menu for the queue.  Put the properties first so that finger trouble
  * is less dangerous. */
 static struct menuitem queue_menu[] = {
-  { "Properties", properties_activate, properties_sensitive, 0, 0 },
-  { "Select all", selectall_activate, selectall_sensitive, 0, 0 },
-  { "Scratch", scratch_activate, scratch_sensitive, 0, 0 },
-  { "Remove", remove_activate, remove_sensitive, 0, 0 },
+  { "Track properties", properties_activate, properties_sensitive, 0, 0 },
+  { "Select all tracks", selectall_activate, selectall_sensitive, 0, 0 },
+  { "Scratch track", scratch_activate, scratch_sensitive, 0, 0 },
+  { "Remove track from queue", remove_activate, remove_sensitive, 0, 0 },
   { 0, 0, 0, 0, 0 }
 };
 
+/** @brief Called whenever @ref DISORDER_PLAYING or @ref DISORDER_TRACK_PAUSED changes
+ *
+ * We monitor pause/resume as well as whether the track is playing in order to
+ * keep the time played so far up to date correctly.  See playing_completed().
+ */
+static void playing_update(void attribute((unused)) *v) {
+  D(("playing_update"));
+  gtk_label_set_text(GTK_LABEL(report_label), "updating playing track");
+  disorder_eclient_playing(client, playing_completed, 0);
+}
+
+/** @brief Create the queue widget */
 GtkWidget *queue_widget(void) {
   D(("queue_widget"));
   /* Arrange periodic update of the so-far played field */
   g_timeout_add(1000/*ms*/, adjust_sofar, 0);
+  /* Arrange a callback whenever the playing state changes */ 
+  register_monitor(playing_update, 0,  DISORDER_PLAYING|DISORDER_TRACK_PAUSED);
   /* We pass choose_update() as our notify function since the choose screen
    * marks tracks that are playing/in the queue. */
   return queuelike(&ql_queue, fixup_queue, choose_update, queue_menu,
@@ -1211,12 +1237,6 @@ void queue_update(void) {
   disorder_eclient_queue(client, queuelike_completed, cbd);
 }
 
-void playing_update(void) {
-  D(("playing_update"));
-  gtk_label_set_text(GTK_LABEL(report_label), "updating playing track");
-  disorder_eclient_playing(client, playing_completed, 0);
-}
-
 /* Recently played tracks -------------------------------------------------- */
 
 static struct queue_entry *fixup_recent(struct queue_entry *q) {
@@ -1238,8 +1258,8 @@ static struct queue_entry *fixup_recent(struct queue_entry *q) {
 }
 
 static struct menuitem recent_menu[] = {
-  { "Properties", properties_activate, properties_sensitive,0, 0 },
-  { "Select all", selectall_activate, selectall_sensitive, 0, 0 },
+  { "Track properties", properties_activate, properties_sensitive,0, 0 },
+  { "Select all tracks", selectall_activate, selectall_sensitive, 0, 0 },
   { 0, 0, 0, 0, 0 }
 };
 
@@ -1262,7 +1282,8 @@ void recent_update(void) {
 /* Main menu plumbing ------------------------------------------------------ */
 
 static int queue_properties_sensitive(GtkWidget *w) {
-  return !!queue_count_selected(g_object_get_data(G_OBJECT(w), "queue"));
+  return (!!queue_count_selected(g_object_get_data(G_OBJECT(w), "queue"))
+          && (disorder_eclient_state(client) & DISORDER_CONNECTED));
 }
 
 static int queue_selectall_sensitive(GtkWidget *w) {