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-05-15 19:46:28 -04:00
|
|
|
P_SB_TYPE,
|
2016-01-01 19:58:30 -05:00
|
|
|
};
|
|
|
|
|
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-05-15 19:46:28 -04:00
|
|
|
|
|
|
|
static inline enum playlist_type_t __playlist_type(GtkTreeIter *iter)
|
|
|
|
{
|
|
|
|
enum playlist_type_t type;
|
|
|
|
|
|
|
|
gtk_tree_model_get(GTK_TREE_MODEL(p_store), iter, P_SB_TYPE, &type, -1);
|
|
|
|
return (type < PL_MAX_TYPE) ? type : PL_SYSTEM;
|
|
|
|
}
|
|
|
|
|
2016-04-29 14:58:40 -04:00
|
|
|
static void __playlist_set(GtkTreeIter *iter, const gchar *name,
|
2016-05-15 19:46:28 -04:00
|
|
|
const gchar *image, enum playlist_type_t type)
|
2016-01-04 07:55:10 -05:00
|
|
|
{
|
2016-05-15 19:46:28 -04:00
|
|
|
gtk_tree_store_set(p_store, iter, P_SB_NAME, name,
|
|
|
|
P_SB_IMAGE, image,
|
|
|
|
P_SB_TYPE, type, -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)
|
|
|
|
{
|
2016-05-03 11:20:31 -04:00
|
|
|
GtkTreePath *path = gtk_tree_model_get_path(GTK_TREE_MODEL(p_store), iter);
|
2016-05-11 08:09:27 -04:00
|
|
|
unsigned int size = playlist_size(__playlist_type(iter), name);
|
2016-04-28 11:06:50 -04:00
|
|
|
const gchar *fmt = "%s\n%d track%s";
|
|
|
|
gchar *text;
|
|
|
|
|
2016-05-03 11:20:31 -04:00
|
|
|
if (gtk_tree_path_get_depth(path) == 1)
|
|
|
|
fmt = "<b>%s</b>\n%d track%s";
|
|
|
|
|
2016-04-28 11:06:50 -04:00
|
|
|
text = g_strdup_printf(fmt, name, size, (size == 1) ? "" : "s");
|
|
|
|
gtk_tree_store_set(p_store, iter, P_SB_NAME, text, -1);
|
2016-05-03 11:20:31 -04:00
|
|
|
|
|
|
|
gtk_tree_path_free(path);
|
2016-04-28 11:06:50 -04:00
|
|
|
g_free(text);
|
|
|
|
}
|
|
|
|
|
2016-01-04 07:55:10 -05:00
|
|
|
static void __playlist_add(GtkTreeIter *parent, const gchar *name,
|
2016-05-15 19:46:28 -04:00
|
|
|
const gchar *image, enum playlist_type_t type)
|
2016-01-04 07:55:10 -05:00
|
|
|
{
|
|
|
|
GtkTreeIter iter;
|
|
|
|
gtk_tree_store_insert(p_store, &iter, parent, -1);
|
2016-05-15 19:46:28 -04:00
|
|
|
__playlist_set(&iter, name, image, type);
|
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);
|
2016-08-01 12:05:24 -04:00
|
|
|
gchar *text, *parsed, *name, **split;
|
2016-04-28 11:06:50 -04:00
|
|
|
|
2016-05-03 11:20:31 -04:00
|
|
|
gtk_tree_model_get(model, iter, P_SB_NAME, &text, -1);
|
|
|
|
if (!text)
|
|
|
|
return NULL;
|
2016-08-01 12:05:24 -04:00
|
|
|
pango_parse_markup(text, -1, 0, NULL, &parsed, NULL, NULL);
|
|
|
|
split = g_strsplit(parsed, "\n", 2);
|
|
|
|
name = g_strdup(split[0]);
|
2016-04-28 11:06:50 -04:00
|
|
|
|
|
|
|
g_strfreev(split);
|
2016-05-03 11:20:31 -04:00
|
|
|
g_free(text);
|
2016-08-01 12:05:24 -04:00
|
|
|
g_free(parsed);
|
2016-04-28 11:06:50 -04:00
|
|
|
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);
|
2016-05-11 08:09:27 -04:00
|
|
|
struct queue *queue;
|
2016-01-04 08:15:55 -05:00
|
|
|
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-05-11 08:09:27 -04:00
|
|
|
queue = playlist_get_queue(__playlist_type(&iter), p_name);
|
|
|
|
gui_sidebar_selected(SB_PLAYLIST, gui_queue(queue));
|
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-03 11:20:31 -04:00
|
|
|
gboolean __playlist_on_select(GtkTreeSelection *selection, GtkTreeModel *model,
|
|
|
|
GtkTreePath *path, gboolean selected, gpointer data)
|
|
|
|
{
|
|
|
|
struct queue *queue = NULL;
|
|
|
|
GtkTreeIter iter;
|
|
|
|
gchar *name;
|
|
|
|
|
|
|
|
gtk_tree_model_get_iter(model, &iter, path);
|
|
|
|
|
|
|
|
name = __playlist_name(&iter);
|
2016-05-11 08:09:27 -04:00
|
|
|
queue = playlist_get_queue(__playlist_type(&iter), name);
|
2016-05-03 11:20:31 -04:00
|
|
|
g_free(name);
|
|
|
|
|
|
|
|
return queue != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool __playlist_queue_set_size(struct queue *queue, GtkTreeIter *iter)
|
|
|
|
{
|
2016-05-15 19:46:28 -04:00
|
|
|
struct gui_queue *gq = gui_queue(queue);
|
|
|
|
bool match = (__playlist_type(iter) == gq->gq_playlist->pl_type);
|
2016-05-03 11:20:31 -04:00
|
|
|
gchar *name = __playlist_name(iter);
|
|
|
|
|
2016-05-15 19:46:28 -04:00
|
|
|
if (match)
|
|
|
|
match = string_match(name, gq->gq_text);
|
2016-05-03 11:20:31 -04:00
|
|
|
if (match)
|
|
|
|
__playlist_set_size(iter, name);
|
|
|
|
g_free(name);
|
|
|
|
|
|
|
|
return match;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (!gtk_tree_model_get_iter_first(model, &parent))
|
|
|
|
return;
|
|
|
|
|
|
|
|
do {
|
2016-05-03 11:20:31 -04:00
|
|
|
if (__playlist_queue_set_size(queue, &parent))
|
|
|
|
return;
|
|
|
|
if (gtk_tree_model_iter_children(model, &iter, &parent)) {
|
|
|
|
do {
|
|
|
|
if (__playlist_queue_set_size(queue, &iter))
|
|
|
|
return;
|
|
|
|
} while (gtk_tree_model_iter_next(model, &iter));
|
|
|
|
}
|
|
|
|
} while (gtk_tree_model_iter_next(model, &parent));
|
2016-04-28 11:06:50 -04:00
|
|
|
}
|
|
|
|
|
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;
|
2016-05-03 11:20:31 -04:00
|
|
|
unsigned int flags = 0;
|
|
|
|
|
|
|
|
if (string_match(playlist->pl_name, "Collection"))
|
|
|
|
flags = GQ_CAN_RANDOM;
|
2016-05-15 11:04:13 -04:00
|
|
|
return gui_queue_alloc(playlist, queue, playlist->pl_name, flags);
|
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)
|
|
|
|
{
|
2016-05-03 11:20:31 -04:00
|
|
|
if (string_match(gui_playlist_cur(), "Collection"))
|
2016-05-06 08:05:21 -04:00
|
|
|
playlist_add(PL_SYSTEM, "Hidden", track);
|
2016-05-03 11:20:31 -04:00
|
|
|
else if (string_match(gui_playlist_cur(), "Favorites") ||
|
|
|
|
string_match(gui_playlist_cur(), "Hidden"))
|
2016-05-06 08:05:21 -04:00
|
|
|
playlist_remove(PL_SYSTEM, gui_playlist_cur(), track);
|
2016-01-21 09:42:37 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-01-01 19:58:30 -05:00
|
|
|
void gui_playlist_init()
|
|
|
|
{
|
2016-07-25 15:03:36 -04:00
|
|
|
GtkTreeSelection *selection;
|
2016-01-01 19:58:30 -05:00
|
|
|
GtkTreeView *treeview;
|
2016-01-04 07:55:10 -05:00
|
|
|
GtkTreeIter parent;
|
2016-07-25 15:03:36 -04:00
|
|
|
GtkTreePath *path;
|
2016-01-01 19:58:30 -05:00
|
|
|
|
2016-07-25 15:03:36 -04:00
|
|
|
p_store = GTK_TREE_STORE(gui_builder_object("o_playlist_store"));
|
|
|
|
treeview = GTK_TREE_VIEW(gui_builder_widget("o_playlist_view"));
|
|
|
|
selection = gtk_tree_view_get_selection(treeview);
|
2016-01-01 19:58:30 -05:00
|
|
|
|
2016-05-03 11:20:31 -04:00
|
|
|
gtk_tree_store_insert(p_store, &parent, NULL, -1);
|
2016-05-15 19:46:28 -04:00
|
|
|
__playlist_set(&parent, "Collection", "media-optical", PL_SYSTEM);
|
2016-05-03 11:20:31 -04:00
|
|
|
__playlist_set_size(&parent, "Collection");
|
|
|
|
|
2016-07-25 15:03:36 -04:00
|
|
|
path = gtk_tree_model_get_path(GTK_TREE_MODEL(p_store), &parent);
|
|
|
|
gtk_tree_selection_select_path(selection, path);
|
|
|
|
gtk_tree_path_free(path);
|
|
|
|
|
2016-05-03 11:20:31 -04:00
|
|
|
gtk_tree_store_insert(p_store, &parent, NULL, -1);
|
2016-05-15 19:46:28 -04:00
|
|
|
__playlist_set(&parent, "History", "document-open-recent", PL_SYSTEM);
|
2016-05-03 11:20:31 -04:00
|
|
|
__playlist_set_size(&parent, "History");
|
|
|
|
|
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-05-15 19:46:28 -04:00
|
|
|
__playlist_set(&parent, "<big>Playlists</big>", "emblem-documents", 0);
|
2016-01-04 07:55:10 -05:00
|
|
|
|
|
|
|
/* Add playlists. */
|
2016-05-15 19:46:28 -04:00
|
|
|
__playlist_add(&parent, "Favorites", "emblem-favorite", PL_SYSTEM);
|
|
|
|
__playlist_add(&parent, "Hidden", "window-close", PL_SYSTEM);
|
|
|
|
__playlist_add(&parent, "Most Played", "go-up", PL_SYSTEM);
|
|
|
|
__playlist_add(&parent, "Least Played", "go-down", PL_SYSTEM);
|
|
|
|
__playlist_add(&parent, "Unplayed", "audio-x-generic", PL_SYSTEM);
|
2016-01-01 19:58:30 -05:00
|
|
|
|
2016-05-11 08:09:27 -04:00
|
|
|
/* Add "Library" header. */
|
|
|
|
gtk_tree_store_insert(p_store, &parent, NULL, -1);
|
|
|
|
gtk_tree_store_insert(p_store, &parent, NULL, -1);
|
|
|
|
__playlist_set(&parent, "<big>Library</big>", "system-file-manager", 0);
|
|
|
|
|
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-05-03 11:20:31 -04:00
|
|
|
__playlist_on_select, NULL, NULL);
|
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
|
|
|
|
2016-05-11 08:09:27 -04:00
|
|
|
void gui_playlist_add_library(struct library *library)
|
|
|
|
{
|
|
|
|
GtkTreeIter parent;
|
|
|
|
gchar *name;
|
|
|
|
|
|
|
|
gtk_tree_model_get_iter_first(GTK_TREE_MODEL(p_store), &parent);
|
|
|
|
while (gtk_tree_model_iter_next(GTK_TREE_MODEL(p_store), &parent)) {
|
|
|
|
name = __playlist_name(&parent);
|
|
|
|
if (string_match(name, "Library"))
|
|
|
|
__playlist_add(&parent, library->li_path, "folder", PL_LIBRARY);
|
|
|
|
g_free(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
gtk_tree_view_expand_all(
|
|
|
|
GTK_TREE_VIEW(gui_builder_widget("o_playlist_view")));
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
};
|