/* * Copyright 2014 (c) Anna Schumaker. */ #include #include #include #include #include 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 c_list; static Glib::RefPtr c_toggle; static class CollectionColumns : public Gtk::TreeModelColumnRecord { public: Gtk::TreeModelColumn c_id; Gtk::TreeModelColumn c_enabled; Gtk::TreeModelColumn c_size; Gtk::TreeModelColumn c_path; CollectionColumns() { add(c_id); add(c_enabled); add(c_size); add(c_path); } } c_cols; static void list_path(Library *lib) { Gtk::TreeModel::Row row; if (lib) { 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 Library *get_library_and_row(const Gtk::TreePath &path, Gtk::TreeModel::Row &row) { row = *(c_list->get_iter(path)); return tags :: get_library(row[c_cols.c_id]); } static Library *get_library(const Gtk::TreePath &path) { Gtk::TreeModel::Row row; return get_library_and_row(path, row); } static 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 Library *current_library() { Gtk::TreeModel::Row row; return current_library_and_row(row); } void update_paths() { Library *lib; Gtk::TreeModel::Children::iterator it; for (it = c_list->children().begin(); it != c_list->children().end(); it++) { lib = tags :: get_library((*it)[c_cols.c_id]); if (lib) (*it)[c_cols.c_size] = lib->size(); } } static void remove_banned_tracks() { std::set::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)); } 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 idle_enable() { Glib :: signal_idle().connect(sigc::ptr_fun(on_idle)); } static void on_add() { list_path(library :: add(c_chooser->get_filename())); idle_enable(); } static void on_update() { library :: update_all(); idle_enable(); } static void on_row_activated(const Gtk::TreePath &path, Gtk::TreeViewColumn *col) { library :: update(get_library(path)); idle_enable(); } static void on_cursor_changed() { Library *lib = current_library(); if (lib && (c_chooser->get_current_folder() != lib->primary_key())) c_chooser->set_current_folder(lib->primary_key()); } static bool on_key_pressed(GdkEventKey *event) { 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); library :: remove(lib); c_list->erase(row); return true; } static void on_toggled(const Glib::ustring &str) { Gtk::TreeModel::Row row; Library *lib = get_library_and_row(Gtk::TreePath(str), row); library :: set_enabled(lib, !lib->enabled()); row[c_cols.c_enabled] = lib->enabled(); if (lib->enabled()) remove_banned_tracks(); } void manager :: init() { c_add = gui :: get_widget("colmgr_add"); c_update = gui :: get_widget("colmgr_update"); c_chooser = gui :: get_widget("colmgr_chooser"); c_progress = gui :: get_widget("o_idle_progress"); c_toggle = gui :: get_object("colmgr_toggle"); c_list = gui :: get_object("colmgr_list"); c_treeview = gui :: get_widget("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)); for (unsigned int i = 0; i < tags :: library_size(); i++) list_path(tags :: get_library(i)); }