typo
[disorder] / disobedience / playlists.c
index 9184502..bf6d185 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * This file is part of DisOrder
- * Copyright (C) 2008 Richard Kettlewell
+ * Copyright (C) 2008, 2009 Richard Kettlewell
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * - a close button
  */
 #include "disobedience.h"
+#include "queue-generic.h"
+#include "popup.h"
+
+#if PLAYLISTS
 
 static void playlists_updated(void *v,
                               const char *err,
@@ -54,6 +58,37 @@ char **playlists;
 /** @brief Count of playlists */
 int nplaylists;
 
+/** @brief Columns for the playlist editor */
+static const struct queue_column playlist_columns[] = {
+  { "Artist", column_namepart, "artist", COL_EXPAND|COL_ELLIPSIZE },
+  { "Album",  column_namepart, "album",  COL_EXPAND|COL_ELLIPSIZE },
+  { "Title",  column_namepart, "title",  COL_EXPAND|COL_ELLIPSIZE },
+  { "Length", column_length,   0,        COL_RIGHT }
+};
+
+/** @brief Pop-up menu for playlist editor */
+// TODO some of these may not be generic enough yet - check!
+static struct menuitem playlist_menuitems[] = {
+  { "Track properties", ql_properties_activate, ql_properties_sensitive, 0, 0 },
+  { "Play track", ql_play_activate, ql_play_sensitive, 0, 0 },
+  //{ "Play playlist", ql_playall_activate, ql_playall_sensitive, 0, 0 },
+  { "Remove track from queue", ql_remove_activate, ql_remove_sensitive, 0, 0 },
+  { "Select all tracks", ql_selectall_activate, ql_selectall_sensitive, 0, 0 },
+  { "Deselect all tracks", ql_selectnone_activate, ql_selectnone_sensitive, 0, 0 },
+};
+
+/** @brief Queuelike for editing a playlist */
+static struct queuelike ql_playlist = {
+  .name = "playlist",
+  .columns = playlist_columns,
+  .ncolumns = sizeof playlist_columns / sizeof *playlist_columns,
+  .menuitems = playlist_menuitems,
+  .nmenuitems = sizeof playlist_menuitems / sizeof *playlist_menuitems,
+  //.drop = playlist_drop  //TODO
+};
+
+/* Maintaining the list of playlists ---------------------------------------- */
+
 /** @brief Schedule an update to the list of playlists */
 static void playlists_update(const char attribute((unused)) *event,
                              void attribute((unused)) *eventdata,
@@ -104,13 +139,33 @@ static void playlists_updated(void attribute((unused)) *v,
   event_raise("playlists-updated", 0);
 }
 
-/** @brief Called to activate a playlist */
+/* Playlists menu ----------------------------------------------------------- */
+
+/** @brief Play received playlist contents
+ *
+ * Passed as a completion callback by menu_activate_playlist().
+ */
+static void playlist_play_content(void attribute((unused)) *v,
+                                  const char *err,
+                                  int nvec, char **vec) {
+  if(err) {
+    popup_protocol_error(0, err);
+    return;
+  }
+  for(int n = 0; n < nvec; ++n)
+    disorder_eclient_play(client, vec[n], NULL, NULL);
+}
+
+/** @brief Called to activate a playlist
+ *
+ * Called when the menu item for a playlist is clicked.
+ */
 static void menu_activate_playlist(GtkMenuItem *menuitem,
                                    gpointer attribute((unused)) user_data) {
   GtkLabel *label = GTK_LABEL(GTK_BIN(menuitem)->child);
   const char *playlist = gtk_label_get_text(label);
 
-  fprintf(stderr, "activate playlist %s\n", playlist); /* TODO */
+  disorder_eclient_playlist_get(client, playlist_play_content, playlist, NULL);
 }
 
 /** @brief Called when the playlists change */
@@ -121,7 +176,9 @@ static void menu_playlists_changed(const char attribute((unused)) *event,
     return;                             /* OMG too soon */
   GtkMenuShell *menu = GTK_MENU_SHELL(playlists_menu);
   /* TODO: we could be more sophisticated and only insert/remove widgets as
-   * needed.  For now that's too much effort. */
+   * needed.  The current logic trashes the selection which is not acceptable
+   * and interacts badly with one playlist being currently locked and
+   * edited. */
   while(menu->children)
     gtk_container_remove(GTK_CONTAINER(menu), GTK_WIDGET(menu->children->data));
   /* NB nplaylists can be -1 as well as 0 */
@@ -131,14 +188,18 @@ static void menu_playlists_changed(const char attribute((unused)) *event,
     gtk_widget_show(w);
     gtk_menu_shell_append(menu, w);
   }
-  gtk_widget_set_sensitive(playlists_widget,
+  gtk_widget_set_sensitive(menu_playlists_widget,
                            nplaylists > 0);
-  gtk_widget_set_sensitive(editplaylists_widget,
+  gtk_widget_set_sensitive(menu_editplaylists_widget,
                            nplaylists >= 0);
 }
 
+/* Playlists window (list of playlists) ------------------------------------- */
+
 /** @brief (Re-)populate the playlist tree model */
-static void playlists_fill(void) {
+static void playlists_fill(const char attribute((unused)) *event,
+                           void attribute((unused)) *eventdata,
+                           void attribute((unused)) *callbackdata) {
   GtkTreeIter iter[1];
 
   if(!playlists_list)
@@ -186,11 +247,17 @@ static void playlists_add(GtkButton attribute((unused)) *button,
   /* Unselect whatever is selected */
   gtk_tree_selection_unselect_all(playlists_selection);
   fprintf(stderr, "playlists_add\n");/* TODO */
+  /* We need to pop up a window asking for:
+   * - the name for the playlist
+   * - whether it is to be a public, private or shared playlist
+   * Moreover we should keep track of the known playlists and grey out OK
+   * if the name is a clash (as well as if it's actually invalid).
+   */
 }
 
 /** @brief Called when the 'Delete' button is pressed */
 static void playlists_delete(GtkButton attribute((unused)) *button,
-                        gpointer attribute((unused)) userdata) {
+                             gpointer attribute((unused)) userdata) {
   GtkWidget *yesno;
   int res;
 
@@ -200,7 +267,7 @@ static void playlists_delete(GtkButton attribute((unused)) *button,
                                  GTK_DIALOG_MODAL,
                                  GTK_MESSAGE_QUESTION,
                                  GTK_BUTTONS_YES_NO,
-                                 "Do you really want to delete user %s?"
+                                 "Do you really want to delete playlist %s?"
                                  " This action cannot be undone.",
                                  playlists_selected);
   res = gtk_dialog_run(GTK_DIALOG(yesno));
@@ -230,6 +297,51 @@ static struct button playlists_buttons[] = {
 };
 #define NPLAYLISTS_BUTTONS (sizeof playlists_buttons / sizeof *playlists_buttons)
 
+/** @brief Create the list of playlists for the edit playlists window */
+static GtkWidget *playlists_window_list(void) {
+  /* Create the list of playlist and populate it */
+  playlists_fill(NULL, NULL, NULL);
+  /* Create the tree view */
+  GtkWidget *tree = gtk_tree_view_new_with_model(GTK_TREE_MODEL(playlists_list));
+  /* ...and the renderers for it */
+  GtkCellRenderer *cr = gtk_cell_renderer_text_new();
+  GtkTreeViewColumn *col = gtk_tree_view_column_new_with_attributes("Playlist",
+                                                                    cr,
+                                                                    "text", 0,
+                                                                    NULL);
+  gtk_tree_view_append_column(GTK_TREE_VIEW(tree), col);
+  /* Get the selection for the view; set its mode; arrange for a callback when
+   * it changes */
+  playlists_selected = NULL;
+  playlists_selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree));
+  gtk_tree_selection_set_mode(playlists_selection, GTK_SELECTION_BROWSE);
+  g_signal_connect(playlists_selection, "changed",
+                   G_CALLBACK(playlists_selection_changed), NULL);
+
+  /* Create the control buttons */
+  GtkWidget *buttons = create_buttons_box(playlists_buttons,
+                                          NPLAYLISTS_BUTTONS,
+                                          gtk_hbox_new(FALSE, 1));
+  playlists_delete_button = playlists_buttons[1].widget;
+
+  /* Buttons live below the list */
+  GtkWidget *vbox = gtk_vbox_new(FALSE, 0);
+  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);
+
+  return vbox;
+}
+
+/* Playlists window (edit current playlist) --------------------------------- */
+
+static GtkWidget *playlists_window_edit(void) {
+  assert(ql_playlist.view == NULL);     /* better not be set up already */
+  GtkWidget *w = init_queuelike(&ql_playlist);
+  return w;
+}
+
+/* Playlists window --------------------------------------------------------- */
+
 /** @brief Keypress handler */
 static gboolean playlists_keypress(GtkWidget attribute((unused)) *widget,
                                    GdkEventKey *event,
@@ -245,13 +357,21 @@ static gboolean playlists_keypress(GtkWidget attribute((unused)) *widget,
   }
 }
 
+/** @brief Called when the playlist window is destroyed */
+static void playlists_window_destroyed(GtkWidget attribute((unused)) *widget,
+                                       GtkWidget **widget_pointer) {
+  fprintf(stderr, "playlists_window_destroy\n");
+  destroy_queuelike(&ql_playlist);
+  *widget_pointer = NULL;
+}
+
+/** @brief Pop up the playlists window
+ *
+ * Called when the playlists menu item is selected
+ */
 void edit_playlists(gpointer attribute((unused)) callback_data,
                      guint attribute((unused)) callback_action,
                      GtkWidget attribute((unused)) *menu_item) {
-  GtkWidget *tree, *hbox, *vbox, *buttons;
-  GtkCellRenderer *cr;
-  GtkTreeViewColumn *col;
-
   /* If the window already exists, raise it */
   if(playlists_window) {
     gtk_window_present(GTK_WINDOW(playlists_window));
@@ -261,7 +381,7 @@ void edit_playlists(gpointer attribute((unused)) callback_data,
   playlists_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
   gtk_widget_set_style(playlists_window, tool_style);
   g_signal_connect(playlists_window, "destroy",
-                  G_CALLBACK(gtk_widget_destroyed), &playlists_window);
+                  G_CALLBACK(playlists_window_destroyed), &playlists_window);
   gtk_window_set_title(GTK_WINDOW(playlists_window), "Playlists Management");
   /* TODO loads of this is very similar to (copied from!) users.c - can we
    * de-dupe? */
@@ -270,41 +390,15 @@ void edit_playlists(gpointer attribute((unused)) callback_data,
                    G_CALLBACK(playlists_keypress), 0);
   /* default size is too small */
   gtk_window_set_default_size(GTK_WINDOW(playlists_window), 240, 240);
-  /* Create the list of playlist and populate it */
-  playlists_fill();
-  /* Create the tree view */
-  tree = gtk_tree_view_new_with_model(GTK_TREE_MODEL(playlists_list));
-  /* ...and the renderers for it */
-  cr = gtk_cell_renderer_text_new();
-  col = gtk_tree_view_column_new_with_attributes("Playlist",
-                                                cr,
-                                                "text", 0,
-                                                NULL);
-  gtk_tree_view_append_column(GTK_TREE_VIEW(tree), col);
-  /* Get the selection for the view; set its mode; arrange for a callback when
-   * it changes */
-  playlists_selected = NULL;
-  playlists_selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree));
-  gtk_tree_selection_set_mode(playlists_selection, GTK_SELECTION_BROWSE);
-  g_signal_connect(playlists_selection, "changed",
-                   G_CALLBACK(playlists_selection_changed), NULL);
-
-  /* Create the control buttons */
-  buttons = create_buttons_box(playlists_buttons,
-                              NPLAYLISTS_BUTTONS,
-                              gtk_hbox_new(FALSE, 1));
-  playlists_delete_button = playlists_buttons[1].widget;
 
-  /* Buttons live below the list */
-  vbox = gtk_vbox_new(FALSE, 0);
-  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);
+  GtkWidget *hbox = gtk_hbox_new(FALSE, 0);
+  gtk_box_pack_start(GTK_BOX(hbox), playlists_window_list(),
+                     FALSE/*expand*/, FALSE, 0);
+  gtk_box_pack_start(GTK_BOX(hbox), gtk_event_box_new(),
+                     FALSE/*expand*/, FALSE, 2);
+  gtk_box_pack_start(GTK_BOX(hbox), playlists_window_edit(),
+                     TRUE/*expand*/, TRUE/*fill*/, 0);
 
-  hbox = gtk_hbox_new(FALSE, 0);
-  gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE/*expand*/, FALSE, 0);
-  gtk_box_pack_start(GTK_BOX(hbox), gtk_event_box_new(), FALSE/*expand*/, FALSE, 2);
-  // TODO something to edit the playlist in
-  //gtk_box_pack_start(GTK_BOX(hbox), vbox2, TRUE/*expand*/, TRUE/*fill*/, 0);
   gtk_container_add(GTK_CONTAINER(playlists_window), frame_widget(hbox, NULL));
   gtk_widget_show_all(playlists_window);
 }
@@ -324,6 +418,8 @@ void playlists_init(void) {
   playlists_update(0, 0, 0);
 }
 
+#endif
+
 /*
 Local Variables:
 c-basic-offset:2