+/** @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);
+
+ /* We need to:
+ * - lock the playlist
+ * - check it doesn't exist
+ * - set sharing (which will create it empty
+ * - unlock it
+ *
+ * TODO we should freeze the window while this is going on to stop a second
+ * click.
+ */
+ disorder_eclient_playlist_lock(client, playlist_new_locked, fullname,
+ fullname);
+}
+
+/** @brief Called when the proposed new playlist has been locked */
+static void playlist_new_locked(void *v, const char *err) {
+ char *fullname = v;
+ if(err) {
+ popup_submsg(playlist_window, GTK_MESSAGE_ERROR, err);
+ return;
+ }
+ disorder_eclient_playlist_get(client, playlist_new_retrieved,
+ fullname, fullname);
+}
+
+/** @brief Called when the proposed new playlist's contents have been retrieved
+ *
+ * ...or rather, normally, when it's been reported that it does not exist.
+ */
+static void playlist_new_retrieved(void *v, const char *err,
+ int nvec,
+ char attribute((unused)) **vec) {
+ char *fullname = v;
+ if(!err && nvec != -1)
+ /* A rare case but not in principle impossible */
+ err = "A playlist with that name already exists.";
+ if(err) {
+ popup_submsg(playlist_window, GTK_MESSAGE_ERROR, err);
+ disorder_eclient_playlist_unlock(client, playlist_new_unlocked, fullname);
+ return;
+ }
+ gboolean shared, public, private;
+ playlist_new_details(0, 0, &shared, &public, &private);
+ disorder_eclient_playlist_set_share(client, playlist_new_created, fullname,
+ public ? "public"
+ : private ? "private"
+ : "shared",
+ fullname);
+}
+
+/** @brief Called when the new playlist has been created */
+static void playlist_new_created(void attribute((unused)) *v, const char *err) {
+ if(err) {
+ popup_submsg(playlist_window, GTK_MESSAGE_ERROR, err);
+ return;
+ }
+ disorder_eclient_playlist_unlock(client, playlist_new_unlocked, NULL);
+ // TODO arrange for the new playlist to be selected
+}
+
+/** @brief Called when the newly created playlist has unlocked */
+static void playlist_new_unlocked(void attribute((unused)) *v, const char *err) {
+ if(err)
+ popup_submsg(playlist_window, GTK_MESSAGE_ERROR, err);
+ /* Pop down the creation window */
+ gtk_widget_destroy(playlist_new_window);
+}
+
+/** @brief Called when 'cancel' is clicked in new-playlist popup */
+static void playlist_new_cancel(GtkButton attribute((unused)) *button,
+ gpointer attribute((unused)) userdata) {
+ gtk_widget_destroy(playlist_new_window);
+}
+
+/** @brief Called when some radio button in the new-playlist popup changes */
+static void playlist_new_button_toggled(GtkToggleButton attribute((unused)) *tb,
+ gpointer attribute((unused)) userdata) {
+ playlist_new_changed(0,0,0);
+}
+
+/** @brief Called when the text entry field in the new-playlist popup changes */
+static void playlist_new_entry_edited(GtkEditable attribute((unused)) *editable,
+ gpointer attribute((unused)) user_data) {
+ playlist_new_changed(0,0,0);
+}
+
+/** @brief Called to update new playlist window state
+ *
+ * This is called whenever one the text entry or radio buttons changed, and
+ * also when the set of known playlists changes. It determines whether the new
+ * playlist would be creatable and sets the sensitivity of the OK button
+ * and info display accordingly.
+ */
+static void playlist_new_changed(const char attribute((unused)) *event,
+ void attribute((unused)) *eventdata,
+ void attribute((unused)) *callbackdata) {
+ if(!playlist_new_window)
+ return;
+ const char *reason = playlist_new_valid();
+ gtk_widget_set_sensitive(playlist_new_buttons[0].widget,
+ !reason);
+ gtk_label_set_text(GTK_LABEL(playlist_new_info), reason);
+}
+
+/** @brief Test whether the new-playlist window settings are valid
+ * @return NULL on success or an error string if not
+ */
+static const char *playlist_new_valid(void) {
+ gboolean shared, public, private;
+ char *name, *fullname;
+ playlist_new_details(&name, &fullname, &shared, &public, &private);
+ if(!(shared || public || private))
+ return "No type set.";
+ if(!*name)
+ return "";
+ /* See if the result is valid */
+ if(!valid_username(name)
+ || playlist_parse_name(fullname, NULL, NULL))
+ return "Not a valid playlist name.";
+ /* See if the result clashes with an existing name. This is not a perfect
+ * check, the playlist might be created after this point but before we get a
+ * chance to disable the "OK" button. However when we try to create the
+ * playlist we will first try to retrieve it, with a lock held, so we
+ * shouldn't end up overwriting anything. */
+ for(int n = 0; n < nplaylists; ++n)
+ if(!strcmp(playlists[n], fullname)) {
+ if(shared)
+ return "A shared playlist with that name already exists.";
+ else
+ return "You already have a playlist with that name.";
+ }
+ /* As far as we can tell creation would work */
+ return NULL;
+}
+
+/** @brief Get entered new-playlist details
+ * @param namep Where to store entered name (or NULL)
+ * @param fullnamep Where to store computed full name (or NULL)
+ * @param sharep Where to store 'shared' flag (or NULL)
+ * @param publicp Where to store 'public' flag (or NULL)
+ * @param privatep Where to store 'private' flag (or NULL)
+ */
+static void playlist_new_details(char **namep,
+ char **fullnamep,
+ gboolean *sharedp,
+ gboolean *publicp,
+ gboolean *privatep) {
+ gboolean shared, public, private;
+ g_object_get(playlist_new_shared, "active", &shared, (char *)NULL);
+ g_object_get(playlist_new_public, "active", &public, (char *)NULL);
+ g_object_get(playlist_new_private, "active", &private, (char *)NULL);
+ char *gname = gtk_editable_get_chars(GTK_EDITABLE(playlist_new_entry),
+ 0, -1); /* name owned by calle */
+ char *name = xstrdup(gname);
+ g_free(gname);
+ if(sharedp) *sharedp = shared;
+ if(publicp) *publicp = public;
+ if(privatep) *privatep = private;
+ if(namep) *namep = name;
+ if(fullnamep) {
+ if(*sharedp) *fullnamep = *namep;
+ else byte_xasprintf(fullnamep, "%s.%s", config->username, name);
+ }
+}
+
+/* Playlist picker ---------------------------------------------------------- */
+
+/** @brief Delete button */
+static GtkWidget *playlist_picker_delete_button;
+
+/** @brief Tree model for list of playlists
+ *
+ * This has two columns:
+ * - column 0 will be the display name
+ * - column 1 will be the sort key/playlist name (and will not be displayed)
+ */
+static GtkTreeStore *playlist_picker_list;
+
+/** @brief Selection for list of playlists */
+static GtkTreeSelection *playlist_picker_selection;
+
+/** @brief Currently selected playlist */
+static const char *playlist_picker_selected;
+
+/** @brief (Re-)populate the playlist picker tree model */
+static void playlist_picker_fill(const char attribute((unused)) *event,
+ void attribute((unused)) *eventdata,
+ void attribute((unused)) *callbackdata) {
+ if(!playlist_window)
+ return;
+ if(!playlist_picker_list)
+ playlist_picker_list = gtk_tree_store_new(2, G_TYPE_STRING, G_TYPE_STRING);
+ /* We will accumulate a list of all the sections that exist */
+ char **sections = xcalloc(nplaylists, sizeof (char *));
+ int nsections = 0;
+ /* Make sure shared playlists are there */
+ int start = 0, end;
+ for(end = start; end < nplaylists && !strchr(playlists[end], '.'); ++end)
+ ;
+ if(start != end) {
+ playlist_picker_update_section("Shared playlists", "",
+ start, end);
+ sections[nsections++] = (char *)"";
+ }
+ /* Make sure owned playlists are there */
+ while((start = end) < nplaylists) {
+ const int nl = strchr(playlists[start], '.') - playlists[start];
+ char *name = xstrndup(playlists[start], nl);
+ for(end = start;
+ end < nplaylists
+ && playlists[end][nl] == '.'
+ && !strncmp(playlists[start], playlists[end], nl);
+ ++end)
+ ;
+ playlist_picker_update_section(name, name, start, end);
+ sections[nsections++] = name;
+ }
+ /* Delete obsolete sections */
+ playlist_picker_delete_obsolete(NULL, sections, nsections);
+}
+
+/** @brief Update a section in the picker tree model
+ * @param section Section name
+ * @param start First entry in @ref playlists
+ * @param end Past last entry in @ref playlists
+ */
+static void playlist_picker_update_section(const char *title, const char *key,
+ int start, int end) {
+ /* Find the section, creating it if necessary */
+ GtkTreeIter section_iter[1];
+ playlist_picker_find(NULL, title, key, section_iter, TRUE);
+ /* Add missing rows */
+ for(int n = start; n < end; ++n) {
+ GtkTreeIter child[1];
+ char *name;
+ if((name = strchr(playlists[n], '.')))
+ ++name;
+ else
+ name = playlists[n];
+ playlist_picker_find(section_iter,
+ name, playlists[n],
+ child,
+ TRUE);
+ }
+ /* Delete anything that shouldn't exist. */
+ playlist_picker_delete_obsolete(section_iter, playlists + start, end - start);
+}
+
+/** @brief Find and maybe create a row in the picker tree model
+ * @param parent Parent iterator (or NULL for top level)
+ * @param title Display name of section
+ * @param key Key to search for
+ * @param iter Iterator to point at key
+ * @param create If TRUE, key will be created if it doesn't exist
+ * @param compare Row comparison function
+ * @return TRUE if key exists else FALSE
+ *
+ * If the @p key exists then @p iter will point to it and TRUE will be
+ * returned.
+ *
+ * If the @p key does not exist and @p create is TRUE then it will be created.
+ * @p iter wil point to it and TRUE will be returned.
+ *
+ * If the @p key does not exist and @p create is FALSE then FALSE will be
+ * returned.
+ */
+static gboolean playlist_picker_find(GtkTreeIter *parent,
+ const char *title,
+ const char *key,
+ GtkTreeIter iter[1],
+ gboolean create) {
+ gchar *candidate;
+ GtkTreeIter next[1];
+ gboolean it;
+ int row = 0;
+
+ it = gtk_tree_model_iter_children(GTK_TREE_MODEL(playlist_picker_list),
+ next,
+ parent);
+ while(it) {
+ /* Find the value at row 'next' */
+ gtk_tree_model_get(GTK_TREE_MODEL(playlist_picker_list),
+ next,
+ 1, &candidate,
+ -1);
+ /* See how it compares with @p key */
+ int c = strcmp(key, candidate);
+ g_free(candidate);
+ if(!c) {
+ *iter = *next;
+ return TRUE; /* we found our key */
+ }
+ if(c < 0) {
+ /* @p key belongs before row 'next' */
+ if(create) {
+ gtk_tree_store_insert_with_values(playlist_picker_list,
+ iter,
+ parent,
+ row, /* insert here */
+ 0, title, 1, key, -1);
+ return TRUE;
+ } else
+ return FALSE;
+ ++row;
+ }
+ it = gtk_tree_model_iter_next(GTK_TREE_MODEL(playlist_picker_list), next);
+ }
+ /* We have reached the end and not found a row that should be later than @p
+ * key. */
+ if(create) {
+ gtk_tree_store_insert_with_values(playlist_picker_list,
+ iter,
+ parent,
+ INT_MAX, /* insert at end */
+ 0, title, 1, key, -1);
+ return TRUE;
+ } else
+ return FALSE;
+}
+
+/** @brief Delete obsolete rows
+ * @param parent Parent or NULL
+ * @param exists List of rows that should exist (by key)
+ * @param nexists Length of @p exists
+ */
+static void playlist_picker_delete_obsolete(GtkTreeIter parent[1],
+ char **exists,
+ int nexists) {
+ /* Delete anything that shouldn't exist. */
+ GtkTreeIter iter[1];
+ gboolean it = gtk_tree_model_iter_children(GTK_TREE_MODEL(playlist_picker_list),
+ iter,
+ parent);
+ while(it) {
+ /* Find the value at row 'next' */
+ gchar *candidate;
+ gtk_tree_model_get(GTK_TREE_MODEL(playlist_picker_list),
+ iter,
+ 1, &candidate,
+ -1);
+ gboolean found = FALSE;
+ for(int n = 0; n < nexists; ++n)
+ if((found = !strcmp(candidate, exists[n])))
+ break;
+ if(!found)
+ it = gtk_tree_store_remove(playlist_picker_list, iter);
+ else
+ it = gtk_tree_model_iter_next(GTK_TREE_MODEL(playlist_picker_list),
+ iter);
+ g_free(candidate);
+ }
+}
+
+/** @brief Called when the selection might have changed */
+static void playlist_picker_selection_changed(GtkTreeSelection attribute((unused)) *treeselection,
+ gpointer attribute((unused)) user_data) {
+ GtkTreeIter iter;
+ char *gselected, *selected;
+
+ /* Identify the current selection */
+ if(gtk_tree_selection_get_selected(playlist_picker_selection, 0, &iter)
+ && gtk_tree_store_iter_depth(playlist_picker_list, &iter) > 0) {
+ gtk_tree_model_get(GTK_TREE_MODEL(playlist_picker_list), &iter,
+ 1, &gselected, -1);
+ selected = xstrdup(gselected);
+ g_free(gselected);
+ } else
+ selected = 0;
+ /* Set button sensitivity according to the new state */
+ 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);