249 lines
6.8 KiB
C
249 lines
6.8 KiB
C
/*
|
|
* Copyright 2015 (c) Anna Schumaker.
|
|
*/
|
|
#include <core/collection.h>
|
|
#include <core/idle.h>
|
|
#include <core/playlist.h>
|
|
#include <core/tempq.h>
|
|
#include <gui/builder.h>
|
|
#include <gui/collection.h>
|
|
#include <gui/idle.h>
|
|
#include <gui/sidebar.h>
|
|
|
|
enum collection_sidebar_columns {
|
|
C_SB_IMAGE,
|
|
C_SB_PATH,
|
|
C_SB_LIBRARY,
|
|
};
|
|
|
|
static GtkTreeModel *c_model;
|
|
|
|
static void __collection_set_header(GtkTreeIter *iter, const gchar *image,
|
|
const gchar *text)
|
|
{
|
|
gtk_tree_store_set(GTK_TREE_STORE(c_model), iter, C_SB_IMAGE, image,
|
|
C_SB_PATH, text, -1);
|
|
}
|
|
|
|
static void __collection_set_library(GtkTreeIter *iter, struct library *library)
|
|
{
|
|
gtk_tree_store_set(GTK_TREE_STORE(c_model), iter,
|
|
C_SB_IMAGE, "folder",
|
|
C_SB_PATH, library->li_path,
|
|
C_SB_LIBRARY, library, -1);
|
|
}
|
|
|
|
static struct library *__collection_get_library(GtkTreeIter *iter)
|
|
{
|
|
struct library *library;
|
|
|
|
gtk_tree_model_get(c_model, iter, C_SB_LIBRARY, &library, -1);
|
|
return library;
|
|
}
|
|
|
|
void __collection_activated(GtkTreeView *treeview, GtkTreePath *path,
|
|
GtkTreeViewColumn *column, gpointer data)
|
|
{
|
|
struct library *library = NULL;
|
|
GtkTreeIter iter;
|
|
|
|
if (gtk_tree_model_get_iter(c_model, &iter, path))
|
|
library = __collection_get_library(&iter);
|
|
if (!library)
|
|
return;
|
|
|
|
collection_update(library);
|
|
gui_idle_enable();
|
|
}
|
|
|
|
void __collection_toggled(GtkCheckMenuItem *check, gpointer data)
|
|
{
|
|
GtkTreeView *treeview = GTK_TREE_VIEW(gui_builder_widget("o_collection_view"));
|
|
GtkTreeSelection *selection = gtk_tree_view_get_selection(treeview);
|
|
struct library *library = NULL;
|
|
GtkTreePath *path;
|
|
GtkTreeIter iter;
|
|
GList *rows;
|
|
|
|
rows = gtk_tree_selection_get_selected_rows(selection, &c_model);
|
|
if (!rows)
|
|
return;
|
|
path = rows->data;
|
|
if (gtk_tree_model_get_iter(c_model, &iter, path))
|
|
library = __collection_get_library(&iter);
|
|
if (!library)
|
|
return;
|
|
|
|
collection_set_enabled(library, gtk_check_menu_item_get_active(check));
|
|
__collection_set_library(&iter, library);
|
|
}
|
|
|
|
bool __collection_keypress(GtkTreeView *treeview, GdkEventKey *event,
|
|
gpointer data)
|
|
{
|
|
GtkTreeSelection *selection = gtk_tree_view_get_selection(treeview);
|
|
struct library *library = NULL;
|
|
GtkTreePath *path;
|
|
GtkTreeIter iter;
|
|
GList *rows;
|
|
|
|
if (event->keyval != GDK_KEY_Delete)
|
|
return false;
|
|
|
|
rows = gtk_tree_selection_get_selected_rows(selection, &c_model);
|
|
path = rows->data;
|
|
|
|
if (gtk_tree_model_get_iter(c_model, &iter, path))
|
|
library = __collection_get_library(&iter);
|
|
if (!library)
|
|
goto out;
|
|
|
|
collection_remove(library);
|
|
gtk_tree_store_remove(GTK_TREE_STORE(c_model), &iter);
|
|
out:
|
|
g_list_free_full(rows, (GDestroyNotify)gtk_tree_path_free);
|
|
return true;
|
|
}
|
|
|
|
bool __collection_buttonpress(GtkWidget *widget, GdkEventButton *event, gpointer data)
|
|
{
|
|
GtkCheckMenuItem *check = GTK_CHECK_MENU_ITEM(gui_builder_widget("o_collection_enabled"));
|
|
GtkTreeView *treeview = GTK_TREE_VIEW(gui_builder_widget("o_collection_view"));
|
|
GtkTreeSelection *selection = gtk_tree_view_get_selection(treeview);
|
|
GtkMenu *menu = GTK_MENU(gui_builder_widget("o_collection_rc"));
|
|
struct library *library = NULL;
|
|
GtkTreePath *path;
|
|
GtkTreeIter iter;
|
|
|
|
if (event->button != 3)
|
|
return false;
|
|
|
|
if (gtk_tree_view_get_path_at_pos(treeview, event->x, event->y,
|
|
&path, NULL, NULL, NULL))
|
|
gtk_tree_selection_select_path(selection, path);
|
|
if (gtk_tree_model_get_iter(c_model, &iter, path))
|
|
library = __collection_get_library(&iter);
|
|
|
|
if (library) {
|
|
gtk_check_menu_item_set_active(check, library->li_enabled);
|
|
gtk_menu_popup(menu, NULL, NULL, NULL, NULL,
|
|
event->button, event->time);
|
|
}
|
|
|
|
gtk_tree_path_free(path);
|
|
return true;
|
|
}
|
|
|
|
void __collection_add(GtkButton *button, GtkFileChooser *chooser)
|
|
{
|
|
gchar *filename = gtk_file_chooser_get_filename(chooser);
|
|
GtkTreeIter iter, last;
|
|
|
|
gtk_tree_model_get_iter_first(c_model, &iter);
|
|
gtk_tree_model_iter_nth_child(c_model, &last, &iter, 0);
|
|
while (__collection_get_library(&last) != NULL)
|
|
gtk_tree_model_iter_next(c_model, &last);
|
|
|
|
gtk_tree_store_insert_before(GTK_TREE_STORE(c_model), &iter, NULL, &last);
|
|
__collection_set_library(&iter, collection_add(filename));
|
|
|
|
gui_idle_enable();
|
|
g_free(filename);
|
|
}
|
|
|
|
void __collection_selection_changed(GtkTreeSelection *selection,
|
|
GtkFileChooser *chooser)
|
|
{
|
|
GtkStack *stack = GTK_STACK(gui_builder_widget("o_stack"));
|
|
struct library *library;
|
|
GtkTreeIter iter;
|
|
|
|
if (gtk_tree_selection_get_selected(selection, &c_model, &iter)) {
|
|
gtk_stack_set_visible_child_name(stack, "chooser");
|
|
library = __collection_get_library(&iter);
|
|
|
|
gui_sidebar_selected(SB_COLLECTION, NULL);
|
|
if (library)
|
|
gtk_file_chooser_set_current_folder(chooser,
|
|
library->li_path);
|
|
}
|
|
}
|
|
|
|
static void *__collection_init(struct queue *queue)
|
|
{
|
|
return gui_queue_alloc(queue, "Collection", GQ_CAN_RANDOM);
|
|
}
|
|
|
|
static void __collection_added(struct queue *queue, unsigned int pos)
|
|
{
|
|
gui_queue_added(queue, pos);
|
|
gui_sidebar_set_size(gui_queue(queue));
|
|
}
|
|
|
|
static bool __collection_erase(struct queue *queue, struct track *track)
|
|
{
|
|
return playlist_add(PL_HIDDEN, track);
|
|
}
|
|
|
|
static void __collection_removed(struct queue *queue, unsigned int pos)
|
|
{
|
|
gui_queue_removed(queue, pos);
|
|
gui_sidebar_set_size(gui_queue(queue));
|
|
}
|
|
|
|
void __gui_collection_init_idle()
|
|
{
|
|
struct db_entry *library, *next;
|
|
GtkTreeIter parent, iter, last;
|
|
|
|
gtk_tree_model_get_iter_first(c_model, &parent);
|
|
while (!gtk_tree_model_iter_has_child(c_model, &parent))
|
|
gtk_tree_model_iter_next(c_model, &parent);
|
|
gtk_tree_model_iter_nth_child(c_model, &last, &parent, 0);
|
|
|
|
/* Add library paths. */
|
|
db_for_each(library, next, library_db_get()) {
|
|
gtk_tree_store_insert_before(GTK_TREE_STORE(c_model), &iter, &parent, &last);
|
|
__collection_set_library(&iter, LIBRARY(library));
|
|
}
|
|
}
|
|
|
|
|
|
void gui_collection_init()
|
|
{
|
|
GtkTreeIter parent, iter;
|
|
GtkTreeView *treeview;
|
|
|
|
c_model = GTK_TREE_MODEL(gui_builder_object("o_collection_store"));
|
|
|
|
/* Add "Collection" header. */
|
|
gtk_tree_store_insert(GTK_TREE_STORE(c_model), &parent, NULL, -1);
|
|
__collection_set_header(&parent, "system-file-manager", "<big>Collection</big>");
|
|
|
|
/* Add "Add new Library" entry. */
|
|
gtk_tree_store_insert(GTK_TREE_STORE(c_model), &iter, &parent, -1);
|
|
__collection_set_header(&iter, "folder-new",
|
|
"<span style='italic'><Add new path></span>");
|
|
|
|
treeview = GTK_TREE_VIEW(gui_builder_widget("o_collection_view"));
|
|
gtk_tree_view_expand_all(treeview);
|
|
gtk_tree_selection_set_select_function(
|
|
gtk_tree_view_get_selection(treeview),
|
|
gui_sidebar_on_select, NULL, NULL);
|
|
|
|
idle_schedule(__gui_collection_init_idle, NULL);
|
|
gui_sidebar_set_size(gui_queue(collection_get_queue()));
|
|
gui_idle_enable();
|
|
}
|
|
|
|
struct queue_ops collection_ops = {
|
|
.qop_init = __collection_init,
|
|
.qop_deinit = gui_queue_free,
|
|
.qop_added = __collection_added,
|
|
.qop_erase = __collection_erase,
|
|
.qop_removed = __collection_removed,
|
|
.qop_cleared = gui_queue_cleared,
|
|
.qop_save = collection_save,
|
|
.qop_updated = gui_queue_updated,
|
|
};
|