Anna Schumaker
ca5f0701e9
This is much more useful than a boolean status, since we can use the playlist pointer right away. Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
124 lines
3.0 KiB
C
124 lines
3.0 KiB
C
/*
|
|
* Copyright 2016 (c) Anna Schumaker.
|
|
*/
|
|
#include <core/idle.h>
|
|
#include <core/playlist.h>
|
|
#include <gui/sidebar.h>
|
|
#include <gui/window.h>
|
|
#include <glib/gi18n.h>
|
|
|
|
static bool __gui_pl_user_header(GtkTreeIter *iter)
|
|
{
|
|
if (gui_sidebar_iter_first(iter))
|
|
return gui_sidebar_iter_find(iter, "Playlists", PL_MAX_TYPE);
|
|
return false;
|
|
}
|
|
|
|
static gint __gui_pl_user_compare(gconstpointer a, gconstpointer b)
|
|
{
|
|
struct playlist *pl_a = (struct playlist *)a;
|
|
struct playlist *pl_b = (struct playlist *)b;
|
|
return g_utf8_collate(pl_a->pl_name, pl_b->pl_name);
|
|
}
|
|
|
|
static bool __gui_pl_user_init_idle()
|
|
{
|
|
struct db_entry *user, *next;
|
|
struct playlist *playlist;
|
|
GtkTreeIter iter;
|
|
|
|
if (!__gui_pl_user_header(&iter))
|
|
return false;
|
|
|
|
db_for_each(user, next, pl_user_db_get()) {
|
|
playlist = &USER_PLAYLIST(user)->pl_playlist;
|
|
gui_sidebar_iter_sort_child(&iter, playlist, "text-x-generic");
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
void gui_pl_user_init()
|
|
{
|
|
idle_schedule(IDLE_SYNC, __gui_pl_user_init_idle, NULL);
|
|
}
|
|
|
|
struct playlist *gui_pl_user_add(const gchar *name)
|
|
{
|
|
struct playlist *playlist;
|
|
GtkTreeIter iter;
|
|
|
|
if (!__gui_pl_user_header(&iter))
|
|
return NULL;
|
|
|
|
playlist = playlist_new(PL_USER, name);
|
|
if (playlist)
|
|
gui_sidebar_iter_sort_child(&iter, playlist, "text-x-generic");
|
|
return playlist;
|
|
}
|
|
|
|
struct playlist *gui_pl_user_add_dialog(void)
|
|
{
|
|
unsigned int flags = GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_MODAL;
|
|
GtkWidget *entry, *dialog, *content;
|
|
struct playlist *playlist = NULL;
|
|
|
|
entry = gtk_entry_new();
|
|
dialog = gtk_dialog_new_with_buttons("New Playlist Name?",
|
|
gui_window(), flags,
|
|
_("_Cancel"), GTK_RESPONSE_CANCEL,
|
|
_("_OK"), GTK_RESPONSE_ACCEPT,
|
|
NULL);
|
|
content = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
|
|
|
|
gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT);
|
|
gtk_entry_set_activates_default(GTK_ENTRY(entry), true);
|
|
gtk_container_add(GTK_CONTAINER(content), entry);
|
|
gtk_widget_show_all(dialog);
|
|
|
|
if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT)
|
|
playlist = gui_pl_user_add(gtk_entry_get_text(GTK_ENTRY(entry)));
|
|
gtk_widget_destroy(dialog);
|
|
|
|
return playlist;
|
|
}
|
|
|
|
void gui_pl_user_update(struct playlist *playlist)
|
|
{
|
|
GtkTreeIter iter, child;
|
|
|
|
if (!__gui_pl_user_header(&iter))
|
|
return;
|
|
if (!gui_sidebar_iter_down(&iter, &child))
|
|
return;
|
|
if (gui_sidebar_iter_find(&child, playlist->pl_name, playlist->pl_type))
|
|
gui_sidebar_iter_update(&child);
|
|
}
|
|
|
|
void gui_pl_user_select(struct playlist *playlist)
|
|
{
|
|
GtkTreeIter iter, child;
|
|
|
|
if (!__gui_pl_user_header(&iter))
|
|
return;
|
|
if (!gui_sidebar_iter_down(&iter, &child))
|
|
return;
|
|
if (gui_sidebar_iter_find(&child, playlist->pl_name, playlist->pl_type))
|
|
gui_sidebar_iter_select(&child);
|
|
}
|
|
|
|
GList *gui_pl_user_list(void)
|
|
{
|
|
struct db_entry *user, *next;
|
|
struct playlist *playlist;
|
|
GList *list = NULL;
|
|
|
|
db_for_each(user, next, pl_user_db_get()) {
|
|
playlist = &USER_PLAYLIST(user)->pl_playlist;
|
|
list = g_list_insert_sorted(list, playlist,
|
|
__gui_pl_user_compare);
|
|
}
|
|
|
|
return list;
|
|
}
|