ocarina/gui/manager.cpp

199 lines
4.3 KiB
C++

/*
* Copyright 2014 (c) Anna Schumaker.
*/
#include <core/callback.h>
#include <core/idle.h>
#include <core/index.h>
#include <core/library.h>
#include <core/playlist.h>
#include <gui/ocarina.h>
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 Gtk::FileChooserWidget *c_chooser;
static Gtk::ProgressBar *c_progress;
static Gtk::TreeView *c_treeview;
static Glib::RefPtr<Gtk::ListStore> c_list;
static Library *find_library(const Gtk::TreeModel::Row &row)
{
return tags :: get_library(row[c_cols.c_id]);
}
static void list_path(Library *lib)
{
Gtk::TreeModel::Row row;
if (!lib)
return;
row = *(c_list->append());
row[c_cols.c_id] = lib->index();
row[c_cols.c_enabled] = lib->enabled();
row[c_cols.c_size] = lib->size();
row[c_cols.c_path] = lib->primary_key();
}
static void remove_banned_tracks()
{
std::set<unsigned int>::iterator it;
IndexEntry *ent = playlist :: get_tracks("Banned");
if (!ent)
return;
for (it = ent->begin(); it != ent->end(); it++)
library :: get_queue()->del(tags :: get_track(*it));
}
void del_path(const Gtk::TreePath &path)
{
Gtk::TreeModel::Row row = *(c_list->get_iter(path));
Library *lib = find_library(row);
library :: remove(lib);
c_list->erase(row);
}
void update_paths()
{
Library *lib;
Gtk::TreeModel::Children::iterator it;
for (it = c_list->children().begin(); it != c_list->children().end(); it++) {
lib = find_library(*it);
if (lib)
(*it)[c_cols.c_size] = lib->size();
}
}
void toggle_path_enabled(const Gtk::TreePath &path)
{
Gtk::TreeModel::Row row = *(c_list->get_iter(path));
Library *lib = find_library(row);
row[c_cols.c_enabled] = !row[c_cols.c_enabled];
library :: set_enabled(lib, row[c_cols.c_enabled]);
if (row[c_cols.c_enabled])
remove_banned_tracks();
}
static void cd_chooser(const Glib::ustring &dir)
{
if (c_chooser->get_current_folder() != dir)
c_chooser->set_current_folder(dir);
}
static bool find_cur_path(Gtk::TreePath &path)
{
Gtk::TreeViewColumn *col;
c_treeview->get_cursor(path, col);
return !path.empty();
}
static bool on_idle()
{
bool ret = idle :: run_task();
if (ret) {
c_progress->show();
c_progress->set_fraction(idle :: get_progress());
} else
c_progress->hide();
update_paths();
return ret;
}
static void on_ok()
{
list_path(library :: add(c_chooser->get_filename()));
lib :: idle(on_idle);
}
static void on_update()
{
library :: update_all();
lib :: idle(on_idle);
}
static void on_row_activated(const Gtk::TreePath &path, Gtk::TreeViewColumn *col)
{
Gtk::TreeModel::Row row = *(c_list->get_iter(path));
library :: update(find_library(row));
lib :: idle(on_idle);
}
static void on_cursor_changed()
{
Gtk::TreePath path;
if (find_cur_path(path)) {
Gtk::TreeModel::Row row = *(c_list->get_iter(path));
cd_chooser(row[c_cols.c_path]);
}
}
static bool on_key_pressed(GdkEventKey *event)
{
Gtk::TreePath path;
if (lib :: key_name(event) != "Delete")
return false;
if (find_cur_path(path))
del_path(path);
return true;
}
static void on_toggled(const Glib::ustring &path)
{
toggle_path_enabled(Gtk::TreePath(path));
}
void manager :: init()
{
Library *library;
Glib::RefPtr<Gtk::CellRendererToggle> toggle;
c_treeview = lib :: get_widget<Gtk::TreeView>("colmgr_treeview");
c_progress = lib :: get_widget<Gtk::ProgressBar>("o_idle_progress");
c_chooser = lib :: get_widget<Gtk::FileChooserWidget>("colmgr_chooser");
toggle = lib :: get_object<Gtk::CellRendererToggle>("colmgr_toggle");
connect_button("colmgr_add", on_ok);
connect_button("colmgr_update", on_update);
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));
toggle->signal_toggled().connect(sigc::ptr_fun(on_toggled));
c_list = lib :: get_object<Gtk::ListStore>("colmgr_list");
c_list->set_sort_column(c_cols.c_path, Gtk::SORT_ASCENDING);
for (unsigned int i = 0; i < tags :: library_size(); i++) {
library = tags :: get_library(i);
if (library)
list_path(library);
}
}