ocarina/gui/manager.cpp

203 lines
4.7 KiB
C++

/*
* Copyright 2014 (c) Anna Schumaker.
*/
extern "C" {
#include <core/containers/database.h>
#include <core/idle.h>
}
#include <core/containers/index.h>
#include <core/library.h>
#include <core/playlist.h>
#include <gui/ocarina.h>
static Gtk::Button *c_add;
static Gtk::Button *c_update;
static Gtk::FileChooserWidget *c_chooser;
static Gtk::ProgressBar *c_progress;
static Gtk::TreeView *c_treeview;
static Glib::RefPtr<Gtk::ListStore> c_list;
static Glib::RefPtr<Gtk::CellRendererToggle> c_toggle;
static class CollectionColumns : public Gtk::TreeModelColumnRecord {
public:
Gtk::TreeModelColumn<unsigned int> c_id;
Gtk::TreeModelColumn<bool> c_enabled;
Gtk::TreeModelColumn<unsigned int> c_size;
Gtk::TreeModelColumn<Glib::ustring> c_path;
CollectionColumns()
{ add(c_id); add(c_enabled); add(c_size); add(c_path); }
} c_cols;
static void list_path(struct library *lib)
{
Gtk::TreeModel::Row row;
if (lib) {
row = *(c_list->append());
row[c_cols.c_id] = lib->li_dbe.dbe_index;
row[c_cols.c_enabled] = lib->li_enabled;
row[c_cols.c_size] = lib->li_size;
row[c_cols.c_path] = lib->li_path;
}
}
static struct library *get_library_and_row(const Gtk::TreePath &path,
Gtk::TreeModel::Row &row)
{
row = *(c_list->get_iter(path));
return library_get(row[c_cols.c_id]);
}
static struct library *get_library(const Gtk::TreePath &path)
{
Gtk::TreeModel::Row row;
return get_library_and_row(path, row);
}
static struct library *current_library_and_row(Gtk::TreeModel::Row &row)
{
Gtk::TreePath path;
Gtk::TreeViewColumn *col;
c_treeview->get_cursor(path, col);
if (path.empty())
return NULL;
return get_library_and_row(path, row);
}
static struct library *current_library()
{
Gtk::TreeModel::Row row;
return current_library_and_row(row);
}
void update_paths()
{
struct library *lib;
Gtk::TreeModel::Children::iterator it;
for (it = c_list->children().begin(); it != c_list->children().end(); it++) {
lib = library_get((*it)[c_cols.c_id]);
if (lib)
(*it)[c_cols.c_size] = lib->li_size;
}
}
static void remove_banned_tracks()
{
index_entry *ent = playlist :: get_tracks("Banned");
struct set_iter it;
if (!ent)
return;
set_for_each(&ent->ie_set, &it)
queue_remove_all(collection :: get_queue(), track_get(it.it_val));
}
static bool on_idle()
{
bool ret = idle_run_task();
if (ret) {
c_progress->show();
c_progress->set_fraction(idle_progress());
} else
c_progress->hide();
update_paths();
return ret;
}
static void idle_enable()
{
Glib :: signal_idle().connect(sigc::ptr_fun(on_idle));
}
static void on_add()
{
list_path(collection_add(c_chooser->get_filename().c_str()));
idle_enable();
}
static void on_update()
{
collection :: update_all();
idle_enable();
}
static void on_row_activated(const Gtk::TreePath &path, Gtk::TreeViewColumn *col)
{
collection :: update(get_library(path));
idle_enable();
}
static void on_cursor_changed()
{
struct library *lib = current_library();
if (lib && (c_chooser->get_current_folder() != lib->li_path))
c_chooser->set_current_folder(lib->li_path);
}
static bool on_key_pressed(GdkEventKey *event)
{
struct library *lib;
Gtk::TreeModel::Row row;
std::string key = gdk_keyval_name(event->keyval);
if (key != "Delete")
return false;
lib = current_library_and_row(row);
collection :: remove(lib);
c_list->erase(row);
return true;
}
static void on_toggled(const Glib::ustring &str)
{
Gtk::TreeModel::Row row;
struct library *lib = get_library_and_row(Gtk::TreePath(str), row);
collection :: set_enabled(lib, !lib->li_enabled);
row[c_cols.c_enabled] = lib->li_enabled;
if (lib->li_enabled)
remove_banned_tracks();
}
void manager :: init()
{
struct db_entry *library, *next;
c_add = gui :: get_widget<Gtk::Button>("colmgr_add");
c_update = gui :: get_widget<Gtk::Button>("colmgr_update");
c_chooser = gui :: get_widget<Gtk::FileChooserWidget>("colmgr_chooser");
c_progress = gui :: get_widget<Gtk::ProgressBar>("o_idle_progress");
c_toggle = gui :: get_object<Gtk::CellRendererToggle>("colmgr_toggle");
c_list = gui :: get_object<Gtk::ListStore>("colmgr_list");
c_treeview = gui :: get_widget<Gtk::TreeView>("colmgr_treeview");
c_add->signal_clicked().connect(sigc::ptr_fun(on_add));
c_update->signal_clicked().connect(sigc::ptr_fun(on_update));
c_toggle->signal_toggled().connect(sigc::ptr_fun(on_toggled));
c_list->set_sort_column(c_cols.c_path, Gtk::SORT_ASCENDING);
c_treeview->signal_row_activated().connect(sigc::ptr_fun(on_row_activated));
c_treeview->signal_cursor_changed().connect(sigc::ptr_fun(on_cursor_changed));
c_treeview->signal_key_press_event().connect(sigc::ptr_fun(on_key_pressed));
db_for_each(library, next, library_db_get())
list_path(LIBRARY(library));
}