chiark / gitweb /
Comment
[disorder] / disobedience / playlists.c
index d04fd9d005dea3d5999ac68c42cc2da142e4ac54..bd4cf409458ec6cbe2de0869c8f073ac103ef628 100644 (file)
@@ -40,8 +40,6 @@
 #include "popup.h"
 #include "validity.h"
 
-#if PLAYLISTS
-
 static void playlist_list_received_playlists(void *v,
                                              const char *err,
                                              int nvec, char **vec);
@@ -101,6 +99,21 @@ static void playlist_drop_modify(struct playlist_modify_data *mod,
                                  int nvec, char **vec);
 static void playlist_remove_modify(struct playlist_modify_data *mod,
                                  int nvec, char **vec);
+static gboolean playlist_new_keypress(GtkWidget *widget,
+                                      GdkEventKey *event,
+                                      gpointer user_data);
+static gboolean playlist_picker_keypress(GtkWidget *widget,
+                                         GdkEventKey *event,
+                                         gpointer user_data);
+static void playlist_editor_button_toggled(GtkToggleButton *tb,
+                                           gpointer userdata);
+static void playlist_editor_set_buttons(const char *event,
+                                        void *eventdata,
+                                        void *callbackdata);
+static void playlist_editor_got_share(void *v,
+                                      const char *err,
+                                      const char *value);
+static void playlist_editor_share_set(void *v, const char *err);
 
 /** @brief Playlist editing window */
 static GtkWidget *playlist_window;
@@ -225,7 +238,7 @@ static int playlistcmp(const void *ap, const void *bp) {
 static void playlist_menu_playing(void attribute((unused)) *v,
                                   const char *err) {
   if(err)
-    popup_protocol_error(0, err);
+    popup_submsg(playlist_window, GTK_MESSAGE_ERROR, err);
 }
 
 /** @brief Play received playlist contents
@@ -236,7 +249,7 @@ static void playlist_menu_received_content(void attribute((unused)) *v,
                                            const char *err,
                                            int nvec, char **vec) {
   if(err) {
-    popup_protocol_error(0, err);
+    popup_submsg(playlist_window, GTK_MESSAGE_ERROR, err);
     return;
   }
   for(int n = 0; n < nvec; ++n)
@@ -378,15 +391,36 @@ static void playlist_new_playlist(void) {
   /* Set initial state of OK button */
   playlist_new_changed(0,0,0);
 
-  /* TODO: return should = OK, escape should = cancel */
+  g_signal_connect(playlist_new_window, "key-press-event",
+                   G_CALLBACK(playlist_new_keypress), 0);
   
   /* Display the window */
   gtk_widget_show_all(playlist_new_window);
 }
 
+/** @brief Keypress handler */
+static gboolean playlist_new_keypress(GtkWidget attribute((unused)) *widget,
+                                      GdkEventKey *event,
+                                      gpointer attribute((unused)) user_data) {
+  if(event->state)
+    return FALSE;
+  switch(event->keyval) {
+  case GDK_Return:
+    playlist_new_ok(NULL, NULL);
+    return TRUE;
+  case GDK_Escape:
+    gtk_widget_destroy(playlist_new_window);
+    return TRUE;
+  default:
+    return FALSE;
+  }
+}
+
 /** @brief Called when 'ok' is clicked in new-playlist popup */
 static void playlist_new_ok(GtkButton attribute((unused)) *button,
                             gpointer attribute((unused)) userdata) {
+  if(playlist_new_valid())
+    return;
   gboolean shared, public, private;
   char *name, *fullname;
   playlist_new_details(&name, &fullname, &shared, &public, &private);
@@ -408,7 +442,7 @@ static void playlist_new_ok(GtkButton attribute((unused)) *button,
 static void playlist_new_locked(void *v, const char *err) {
   char *fullname = v;
   if(err) {
-    popup_protocol_error(0, err);
+    popup_submsg(playlist_window, GTK_MESSAGE_ERROR, err);
     return;
   }
   disorder_eclient_playlist_get(client, playlist_new_retrieved,
@@ -427,7 +461,7 @@ static void playlist_new_retrieved(void *v, const char *err,
     /* A rare case but not in principle impossible */
     err = "A playlist with that name already exists.";
   if(err) {
-    popup_protocol_error(0, err);
+    popup_submsg(playlist_window, GTK_MESSAGE_ERROR, err);
     disorder_eclient_playlist_unlock(client, playlist_new_unlocked, fullname);
     return;
   }
@@ -443,7 +477,7 @@ static void playlist_new_retrieved(void *v, const char *err,
 /** @brief Called when the new playlist has been created */
 static void playlist_new_created(void attribute((unused)) *v, const char *err) {
   if(err) {
-    popup_protocol_error(0, err);
+    popup_submsg(playlist_window, GTK_MESSAGE_ERROR, err);
     return;
   }
   disorder_eclient_playlist_unlock(client, playlist_new_unlocked, NULL);
@@ -453,7 +487,7 @@ static void playlist_new_created(void attribute((unused)) *v, const char *err) {
 /** @brief Called when the newly created playlist has unlocked */
 static void playlist_new_unlocked(void attribute((unused)) *v, const char *err) {
   if(err)
-    popup_protocol_error(0, err);
+    popup_submsg(playlist_window, GTK_MESSAGE_ERROR, err);
   /* Pop down the creation window */
   gtk_widget_destroy(playlist_new_window);
 }
@@ -609,10 +643,15 @@ static void playlist_picker_selection_changed(GtkTreeSelection attribute((unused
   } else
     selected = 0;
   /* Set button sensitivity according to the new state */
-  if(selected)
-    gtk_widget_set_sensitive(playlist_picker_delete_button, 1);
-  else
-    gtk_widget_set_sensitive(playlist_picker_delete_button, 0);
+  int deletable = FALSE;
+  if(selected) {
+    if(strchr(selected, '.')) {
+      if(!strncmp(selected, config->username, strlen(config->username)))
+        deletable = TRUE;
+    } else
+      deletable = TRUE;
+  }
+  gtk_widget_set_sensitive(playlist_picker_delete_button, deletable);
   /* Eliminate no-change cases */
   if(!selected && !playlist_picker_selected)
     return;
@@ -624,7 +663,8 @@ static void playlist_picker_selection_changed(GtkTreeSelection attribute((unused
   playlist_picker_selected = selected;
   /* Re-initalize the queue */
   ql_new_queue(&ql_playlist, NULL);
-  playlist_editor_fill(NULL, (void *)playlist_picker_selected, NULL);
+  /* Synthesize a playlist-modified to re-initialize the editor etc */
+  event_raise("playlist-modified", (void *)playlist_picker_selected);
 }
 
 /** @brief Called when the 'add' button is pressed */
@@ -639,7 +679,7 @@ static void playlist_picker_add(GtkButton attribute((unused)) *button,
 static void playlists_picker_delete_completed(void attribute((unused)) *v,
                                               const char *err) {
   if(err)
-    popup_protocol_error(0, err);
+    popup_submsg(playlist_window, GTK_MESSAGE_ERROR, err);
 }
 
 /** @brief Called when the 'Delete' button is pressed */
@@ -649,7 +689,7 @@ static void playlist_picker_delete(GtkButton attribute((unused)) *button,
   int res;
 
   if(!playlist_picker_selected)
-    return;                             /* shouldn't happen */
+    return;
   yesno = gtk_message_dialog_new(GTK_WINDOW(playlist_window),
                                  GTK_DIALOG_MODAL,
                                  GTK_MESSAGE_QUESTION,
@@ -718,9 +758,27 @@ static GtkWidget *playlist_picker_create(void) {
   gtk_box_pack_start(GTK_BOX(vbox), scroll_widget(tree), TRUE/*expand*/, TRUE/*fill*/, 0);
   gtk_box_pack_start(GTK_BOX(vbox), buttons, FALSE/*expand*/, FALSE, 0);
 
+  g_signal_connect(tree, "key-press-event",
+                   G_CALLBACK(playlist_picker_keypress), 0);
+
   return vbox;
 }
 
+static gboolean playlist_picker_keypress(GtkWidget attribute((unused)) *widget,
+                                         GdkEventKey *event,
+                                         gpointer attribute((unused)) user_data) {
+  if(event->state)
+    return FALSE;
+  switch(event->keyval) {
+  case GDK_BackSpace:
+  case GDK_Delete:
+    playlist_picker_delete(NULL, NULL);
+    return TRUE;
+  default:
+    return FALSE;
+  }
+}
+
 static void playlist_picker_destroy(void) {
   playlist_picker_delete_button = NULL;
   g_object_unref(playlist_picker_list);
@@ -731,11 +789,110 @@ static void playlist_picker_destroy(void) {
 
 /* Playlist editor ---------------------------------------------------------- */
 
+static GtkWidget *playlist_editor_shared;
+static GtkWidget *playlist_editor_public;
+static GtkWidget *playlist_editor_private;
+static int playlist_editor_setting_buttons;
+
 static GtkWidget *playlists_editor_create(void) {
   assert(ql_playlist.view == NULL);     /* better not be set up already */
-  GtkWidget *w = init_queuelike(&ql_playlist);
-  /* Initially empty */
-  return w;
+
+  GtkWidget *hbox = gtk_hbox_new(FALSE, 0);
+  playlist_editor_shared = gtk_radio_button_new_with_label(NULL, "shared");
+  playlist_editor_public
+    = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(playlist_editor_shared),
+                                                  "public");
+  playlist_editor_private
+    = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(playlist_editor_shared),
+                                                  "private");
+  g_signal_connect(playlist_editor_public, "toggled",
+                   G_CALLBACK(playlist_editor_button_toggled),
+                   (void *)"public");
+  g_signal_connect(playlist_editor_private, "toggled",
+                   G_CALLBACK(playlist_editor_button_toggled),
+                   (void *)"private");
+  gtk_box_pack_start(GTK_BOX(hbox), playlist_editor_shared,
+                     FALSE/*expand*/, FALSE/*fill*/, 0);
+  gtk_box_pack_start(GTK_BOX(hbox), playlist_editor_public,
+                     FALSE/*expand*/, FALSE/*fill*/, 0);
+  gtk_box_pack_start(GTK_BOX(hbox), playlist_editor_private,
+                     FALSE/*expand*/, FALSE/*fill*/, 0);
+  playlist_editor_set_buttons(0,0,0);
+
+  GtkWidget *vbox = gtk_vbox_new(FALSE, 0);
+  gtk_box_pack_start(GTK_BOX(vbox), init_queuelike(&ql_playlist),
+                     TRUE/*expand*/, TRUE/*fill*/, 0);
+  gtk_box_pack_start(GTK_BOX(vbox), hbox,
+                     FALSE/*expand*/, FALSE/*fill*/, 0);
+  return vbox;
+}
+
+/** @brief Called when the public/private buttons are set */
+static void playlist_editor_button_toggled(GtkToggleButton *tb,
+                                           gpointer userdata) {
+  const char *state = userdata;
+  if(!gtk_toggle_button_get_active(tb)
+     || !playlist_picker_selected
+     || playlist_editor_setting_buttons)
+    return;
+  disorder_eclient_playlist_set_share(client, playlist_editor_share_set,
+                                      playlist_picker_selected, state, NULL);
+}
+
+static void playlist_editor_share_set(void attribute((unused)) *v,
+                                      const attribute((unused)) char *err) {
+  if(err)
+    popup_submsg(playlist_window, GTK_MESSAGE_ERROR, err);
+}
+  
+/** @brief Set the editor button state and sensitivity */
+static void playlist_editor_set_buttons(const char attribute((unused)) *event,
+                                        void *eventdata,
+                                        void attribute((unused)) *callbackdata) {
+  /* If this event is for a non-selected playlist do nothing */
+  if(eventdata
+     && playlist_picker_selected
+     && strcmp(eventdata, playlist_picker_selected))
+    return;
+  if(playlist_picker_selected) {
+    if(strchr(playlist_picker_selected, '.'))
+      disorder_eclient_playlist_get_share(client,
+                                          playlist_editor_got_share,
+                                          playlist_picker_selected,
+                                          (void *)playlist_picker_selected);
+    else
+      playlist_editor_got_share((void *)playlist_picker_selected, NULL,
+                                "shared");
+  } else
+    playlist_editor_got_share(NULL, NULL, NULL);
+}
+
+/** @brief Called with playlist sharing details */
+static void playlist_editor_got_share(void *v,
+                                      const char *err,
+                                      const char *value) {
+  const char *playlist = v;
+  if(err) {
+    popup_submsg(playlist_window, GTK_MESSAGE_ERROR, err);
+    value = NULL;
+  }
+  /* Set the currently active button */
+  ++playlist_editor_setting_buttons;
+  gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(playlist_editor_shared),
+                               value && !strcmp(value, "shared"));
+  gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(playlist_editor_public),
+                               value && !strcmp(value, "public"));
+  gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(playlist_editor_private),
+                               value && !strcmp(value, "private"));
+  /* Set button sensitivity */
+  gtk_widget_set_sensitive(playlist_editor_shared, FALSE);
+  int sensitive = (playlist
+                   && strchr(playlist, '.')
+                   && !strncmp(playlist, config->username,
+                               strlen(config->username)));
+  gtk_widget_set_sensitive(playlist_editor_public, sensitive);
+  gtk_widget_set_sensitive(playlist_editor_private, sensitive);
+  --playlist_editor_setting_buttons;
 }
 
 /** @brief (Re-)populate the playlist tree model */
@@ -759,7 +916,7 @@ static void playlists_editor_received_tracks(void *v,
                                              int nvec, char **vec) {
   const char *playlist = v;
   if(err) {
-    popup_protocol_error(0, err);
+    popup_submsg(playlist_window, GTK_MESSAGE_ERROR, err);
     return;
   }
   if(!playlist_picker_selected
@@ -841,7 +998,7 @@ struct playlist_modify_data {
 static void playlist_modify_locked(void *v, const char *err) {
   struct playlist_modify_data *mod = v;
   if(err) {
-    popup_protocol_error(0, err);
+    popup_submsg(playlist_window, GTK_MESSAGE_ERROR, err);
     return;
   }
   disorder_eclient_playlist_get(client, playlist_modify_retrieved,
@@ -856,7 +1013,7 @@ void playlist_modify_retrieved(void *v, const char *err,
                                char **vec) {
   struct playlist_modify_data *mod = v;
   if(err) {
-    popup_protocol_error(0, err);
+    popup_submsg(playlist_window, GTK_MESSAGE_ERROR, err);
     disorder_eclient_playlist_unlock(client, playlist_modify_unlocked, NULL);
     return;
   }
@@ -884,7 +1041,7 @@ void playlist_modify_retrieved(void *v, const char *err,
 static void playlist_modify_updated(void attribute((unused)) *v,
                                     const char *err) {
   if(err) 
-    popup_protocol_error(0, err);
+    popup_submsg(playlist_window, GTK_MESSAGE_ERROR, err);
   disorder_eclient_playlist_unlock(client, playlist_modify_unlocked, NULL);
 }
 
@@ -892,7 +1049,7 @@ static void playlist_modify_updated(void attribute((unused)) *v,
 static void playlist_modify_unlocked(void attribute((unused)) *v,
                                      const char *err) {
   if(err) 
-    popup_protocol_error(0, err);
+    popup_submsg(playlist_window, GTK_MESSAGE_ERROR, err);
 }
 
 /* Drop tracks into a playlist ---------------------------------------------- */
@@ -1039,10 +1196,9 @@ static int playlist_remove_sensitive(void attribute((unused)) *extra) {
   return TRUE;
 }
 
-/** @brief Called to play the selected playlist */
+/** @brief Called to remove the selected playlist */
 static void playlist_remove_activate(GtkMenuItem attribute((unused)) *menuitem,
                                      gpointer attribute((unused)) user_data) {
-  /* TODO backspace should work too */
   if(!playlist_picker_selected)
     return;
   struct playlist_modify_data *mod = xmalloc(sizeof *mod);
@@ -1153,10 +1309,10 @@ void playlists_init(void) {
   event_register("playlists-updated", playlist_picker_fill, 0);
   /* Update the displayed playlist when it is modified */
   event_register("playlist-modified", playlist_editor_fill, 0);
+  /* Update the shared/public/etc buttons when a playlist is modified */
+  event_register("playlist-modified", playlist_editor_set_buttons, 0);
 }
 
-#endif
-
 /*
 Local Variables:
 c-basic-offset:2