2016-01-01 19:58:30 -05:00
|
|
|
/*
|
|
|
|
* Copyright 2016 (c) Anna Schumaker.
|
|
|
|
*/
|
2016-01-21 09:42:37 -05:00
|
|
|
#include <core/collection.h>
|
2016-04-27 09:25:37 -04:00
|
|
|
#include <core/string.h>
|
2016-01-01 19:58:30 -05:00
|
|
|
#include <gui/builder.h>
|
|
|
|
#include <gui/playlist.h>
|
2016-01-04 08:27:30 -05:00
|
|
|
#include <gui/sidebar.h>
|
2016-01-01 19:58:30 -05:00
|
|
|
|
|
|
|
enum playlist_sidebar_columns {
|
|
|
|
P_SB_IMAGE,
|
|
|
|
P_SB_NAME,
|
|
|
|
};
|
|
|
|
|
2016-04-27 09:25:37 -04:00
|
|
|
static GtkTreeStore *p_store;
|
2016-04-28 11:06:50 -04:00
|
|
|
static gchar *p_name = NULL;
|
2016-01-04 07:55:10 -05:00
|
|
|
|
2016-04-29 14:58:40 -04:00
|
|
|
static void __playlist_set(GtkTreeIter *iter, const gchar *name,
|
2016-04-27 09:41:17 -04:00
|
|
|
const gchar *image)
|
2016-01-04 07:55:10 -05:00
|
|
|
{
|
2016-01-04 08:15:55 -05:00
|
|
|
gtk_tree_store_set(p_store, iter, P_SB_NAME, name,
|
2016-04-27 09:41:17 -04:00
|
|
|
P_SB_IMAGE, image, -1);
|
2016-01-04 07:55:10 -05:00
|
|
|
}
|
|
|
|
|
2016-04-28 11:06:50 -04:00
|
|
|
static void __playlist_set_size(GtkTreeIter *iter, const gchar *name)
|
|
|
|
{
|
|
|
|
unsigned int size = playlist_size(name);
|
|
|
|
const gchar *fmt = "%s\n%d track%s";
|
|
|
|
gchar *text;
|
|
|
|
|
|
|
|
text = g_strdup_printf(fmt, name, size, (size == 1) ? "" : "s");
|
|
|
|
gtk_tree_store_set(p_store, iter, P_SB_NAME, text, -1);
|
|
|
|
g_free(text);
|
|
|
|
}
|
|
|
|
|
2016-01-04 07:55:10 -05:00
|
|
|
static void __playlist_add(GtkTreeIter *parent, const gchar *name,
|
2016-04-27 09:41:17 -04:00
|
|
|
const gchar *image)
|
2016-01-04 07:55:10 -05:00
|
|
|
{
|
|
|
|
GtkTreeIter iter;
|
|
|
|
gtk_tree_store_insert(p_store, &iter, parent, -1);
|
2016-04-27 09:41:17 -04:00
|
|
|
__playlist_set(&iter, name, image);
|
2016-04-28 11:06:50 -04:00
|
|
|
__playlist_set_size(&iter, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gchar *__playlist_name(GtkTreeIter *iter)
|
|
|
|
{
|
|
|
|
GtkTreeModel *model = GTK_TREE_MODEL(p_store);
|
|
|
|
gchar **split, *name;
|
|
|
|
|
|
|
|
gtk_tree_model_get(model, iter, P_SB_NAME, &name, -1);
|
|
|
|
split = g_strsplit(name, "\n", 2);
|
|
|
|
name = g_strdup(split[0]);
|
|
|
|
|
|
|
|
g_strfreev(split);
|
|
|
|
return name;
|
2016-01-04 08:15:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void __playlist_selection_changed(GtkTreeSelection *selection, gpointer data)
|
|
|
|
{
|
2016-01-15 14:45:23 -05:00
|
|
|
GtkStack *stack = GTK_STACK(gui_builder_widget("o_stack"));
|
2016-01-04 08:15:55 -05:00
|
|
|
GtkTreeModel *model = GTK_TREE_MODEL(p_store);
|
|
|
|
GtkTreeIter iter;
|
|
|
|
|
|
|
|
if (gtk_tree_selection_get_selected(selection, &model, &iter)) {
|
2016-04-28 11:06:50 -04:00
|
|
|
if (p_name)
|
|
|
|
g_free(p_name);
|
|
|
|
p_name = __playlist_name(&iter);
|
2016-01-04 08:15:55 -05:00
|
|
|
|
2016-01-15 14:45:23 -05:00
|
|
|
gtk_stack_set_visible_child_name(stack, "queues");
|
2016-01-14 08:59:26 -05:00
|
|
|
gui_sidebar_selected(SB_PLAYLIST,
|
2016-04-27 09:25:37 -04:00
|
|
|
gui_queue(playlist_get_queue(p_name)));
|
2016-01-04 08:15:55 -05:00
|
|
|
}
|
2016-01-04 07:55:10 -05:00
|
|
|
}
|
2016-01-01 19:58:30 -05:00
|
|
|
|
2016-05-02 08:42:28 -04:00
|
|
|
static void __playlist_update_sizes(struct queue *queue)
|
2016-04-28 11:06:50 -04:00
|
|
|
{
|
|
|
|
GtkTreeModel *model = GTK_TREE_MODEL(p_store);
|
|
|
|
GtkTreeIter parent, iter;
|
|
|
|
gchar *name;
|
|
|
|
|
|
|
|
if (!gtk_tree_model_get_iter_first(model, &parent))
|
|
|
|
return;
|
|
|
|
gtk_tree_model_iter_next(model, &parent);
|
|
|
|
if (!gtk_tree_model_iter_children(model, &iter, &parent))
|
|
|
|
return;
|
|
|
|
|
|
|
|
do {
|
|
|
|
name = __playlist_name(&iter);
|
2016-05-02 08:42:28 -04:00
|
|
|
if (string_match(name, gui_queue(queue)->gq_text))
|
|
|
|
__playlist_set_size(&iter, name);
|
2016-04-28 11:06:50 -04:00
|
|
|
g_free(name);
|
|
|
|
} while (gtk_tree_model_iter_next(model, &iter));
|
|
|
|
}
|
|
|
|
|
2016-05-02 07:52:18 -04:00
|
|
|
static void *__playlist_init(struct queue *queue, void *data)
|
2016-01-21 09:42:37 -05:00
|
|
|
{
|
2016-05-02 08:42:28 -04:00
|
|
|
struct playlist *playlist = (struct playlist *)data;
|
|
|
|
return gui_queue_alloc(queue, playlist->pl_name, 0);
|
2016-01-21 09:42:37 -05:00
|
|
|
}
|
|
|
|
|
2016-04-28 11:06:50 -04:00
|
|
|
static void __playlist_added(struct queue *queue, unsigned int row)
|
|
|
|
{
|
|
|
|
gui_queue_added(queue, row);
|
2016-05-02 08:42:28 -04:00
|
|
|
__playlist_update_sizes(queue);
|
2016-04-28 11:06:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void __playlist_removed(struct queue *queue, unsigned int row)
|
|
|
|
{
|
|
|
|
gui_queue_removed(queue, row);
|
2016-05-02 08:42:28 -04:00
|
|
|
__playlist_update_sizes(queue);
|
2016-04-28 11:06:50 -04:00
|
|
|
}
|
|
|
|
|
2016-01-21 09:42:37 -05:00
|
|
|
static bool __playlist_erase(struct queue *queue, struct track *track)
|
|
|
|
{
|
|
|
|
/* collection_unban() and playlist_remove() handle queue changes */
|
2016-04-27 09:25:37 -04:00
|
|
|
if (string_match(gui_playlist_cur(), "Hidden"))
|
2016-01-21 09:42:37 -05:00
|
|
|
collection_unban(track);
|
|
|
|
else
|
|
|
|
playlist_remove(gui_playlist_cur(), track);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-01-01 19:58:30 -05:00
|
|
|
void gui_playlist_init()
|
|
|
|
{
|
|
|
|
GtkTreeView *treeview;
|
2016-01-04 07:55:10 -05:00
|
|
|
GtkTreeIter parent;
|
2016-01-01 19:58:30 -05:00
|
|
|
|
2016-01-04 07:55:10 -05:00
|
|
|
p_store = GTK_TREE_STORE(gui_builder_object("o_playlist_store"));
|
2016-01-01 19:58:30 -05:00
|
|
|
|
|
|
|
/* Add "Playlist" header. */
|
2016-01-04 07:55:10 -05:00
|
|
|
gtk_tree_store_insert(p_store, &parent, NULL, -1);
|
2016-01-05 08:51:59 -05:00
|
|
|
gtk_tree_store_insert(p_store, &parent, NULL, -1);
|
2016-04-27 09:41:17 -04:00
|
|
|
__playlist_set(&parent, "<big>Playlists</big>", "emblem-documents");
|
2016-01-04 07:55:10 -05:00
|
|
|
|
|
|
|
/* Add playlists. */
|
2016-04-27 09:41:17 -04:00
|
|
|
__playlist_add(&parent, "Favorites", "emblem-favorite");
|
2016-04-28 11:06:50 -04:00
|
|
|
__playlist_add(&parent, "Hidden", "window-close");
|
2016-04-27 09:41:17 -04:00
|
|
|
__playlist_add(&parent, "Most Played", "go-up");
|
|
|
|
__playlist_add(&parent, "Least Played", "go-down");
|
|
|
|
__playlist_add(&parent, "Unplayed", "audio-x-generic");
|
2016-01-01 19:58:30 -05:00
|
|
|
|
|
|
|
treeview = GTK_TREE_VIEW(gui_builder_widget("o_playlist_view"));
|
2016-01-04 07:55:10 -05:00
|
|
|
gtk_tree_view_expand_all(treeview);
|
2016-01-01 19:58:30 -05:00
|
|
|
gtk_tree_selection_set_select_function(
|
|
|
|
gtk_tree_view_get_selection(treeview),
|
2016-01-05 08:51:59 -05:00
|
|
|
gui_sidebar_on_select, NULL, NULL);
|
2016-01-04 07:55:10 -05:00
|
|
|
|
|
|
|
gtk_tree_store_insert(p_store, &parent, NULL, -1);
|
2016-01-01 19:58:30 -05:00
|
|
|
}
|
2016-01-04 08:15:55 -05:00
|
|
|
|
2016-04-27 09:25:37 -04:00
|
|
|
gchar *gui_playlist_cur()
|
2016-01-04 08:15:55 -05:00
|
|
|
{
|
2016-04-27 09:25:37 -04:00
|
|
|
return p_name;
|
2016-01-04 08:15:55 -05:00
|
|
|
}
|
2016-01-21 09:42:37 -05:00
|
|
|
|
|
|
|
struct queue_ops playlist_ops = {
|
|
|
|
.qop_init = __playlist_init,
|
|
|
|
.qop_deinit = gui_queue_free,
|
2016-04-28 11:06:50 -04:00
|
|
|
.qop_added = __playlist_added,
|
2016-01-21 09:42:37 -05:00
|
|
|
.qop_erase = __playlist_erase,
|
2016-04-28 11:06:50 -04:00
|
|
|
.qop_removed = __playlist_removed,
|
2016-01-21 09:42:37 -05:00
|
|
|
.qop_cleared = gui_queue_cleared,
|
|
|
|
.qop_updated = gui_queue_updated,
|
|
|
|
};
|