From 98ff0b79cddb25c666ba972c5d0579d7044e678f Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Sat, 17 Jan 2015 12:12:07 -0500 Subject: [PATCH] colmgr: Merge lib/ and gui/ code back together The code is still messy now, but I'll be cleaning it up soon! Signed-off-by: Anna Schumaker --- gui/collection_mgr.cpp | 105 ---------------------- gui/main.cpp | 2 +- gui/manager.cpp | 198 +++++++++++++++++++++++++++++++++++++++++ include/gui/ocarina.h | 11 ++- include/lib/colmgr.h | 23 ----- lib/colmgr.cpp | 124 -------------------------- tests/lib/Sconscript | 1 - tests/lib/colmgr.cpp | 85 ------------------ 8 files changed, 206 insertions(+), 343 deletions(-) delete mode 100644 gui/collection_mgr.cpp create mode 100644 gui/manager.cpp delete mode 100644 include/lib/colmgr.h delete mode 100644 lib/colmgr.cpp delete mode 100644 tests/lib/colmgr.cpp diff --git a/gui/collection_mgr.cpp b/gui/collection_mgr.cpp deleted file mode 100644 index 197a5ae0..00000000 --- a/gui/collection_mgr.cpp +++ /dev/null @@ -1,105 +0,0 @@ -/* - * Copyright 2014 (c) Anna Schumaker. - */ -#include -#include -#include -#include -#include -#include - - -static Gtk::FileChooserWidget *c_chooser; -static Gtk::ProgressBar *c_progress; -static Gtk::TreeView *c_treeview; - - -static void cd_chooser(const std::string &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(); - - colmgr :: update_paths(); - return ret; -} - - -static void on_ok() -{ - colmgr :: add_path(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) -{ - colmgr :: update_path(path); - lib :: idle(on_idle); -} - -static void on_cursor_changed() -{ - Gtk::TreePath path; - if (find_cur_path(path)) - cd_chooser(colmgr :: get_path(path)); -} - -static bool on_key_pressed(GdkEventKey *event) -{ - Gtk::TreePath path; - - if (lib :: key_name(event) != "Delete") - return false; - - if (find_cur_path(path)) - colmgr :: del_path(path); - return true; -} - -static void on_toggled(const Glib::ustring &path) -{ - colmgr :: toggle_path_enabled(Gtk::TreePath(path)); -} - -void collection_mgr_init() -{ - Glib::RefPtr toggle; - - c_treeview = lib :: get_widget("colmgr_treeview"); - c_progress = lib :: get_widget("o_idle_progress"); - c_chooser = lib :: get_widget("colmgr_chooser"); - toggle = lib :: get_object("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)); - - colmgr :: init(); -} diff --git a/gui/main.cpp b/gui/main.cpp index 2fe7e65c..16897754 100644 --- a/gui/main.cpp +++ b/gui/main.cpp @@ -23,7 +23,7 @@ Gtk::Window *ocarina_init(int *argc, char ***argv) core :: init(); - collection_mgr_init(); + manager :: init(); init_tabs(); window = window_init(); post_init_tabs(); diff --git a/gui/manager.cpp b/gui/manager.cpp new file mode 100644 index 00000000..190ca243 --- /dev/null +++ b/gui/manager.cpp @@ -0,0 +1,198 @@ +/* + * Copyright 2014 (c) Anna Schumaker. + */ +#include +#include +#include +#include +#include +#include + + +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 Gtk::FileChooserWidget *c_chooser; +static Gtk::ProgressBar *c_progress; +static Gtk::TreeView *c_treeview; +static Glib::RefPtr 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::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 toggle; + + c_treeview = lib :: get_widget("colmgr_treeview"); + c_progress = lib :: get_widget("o_idle_progress"); + c_chooser = lib :: get_widget("colmgr_chooser"); + toggle = lib :: get_object("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("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); + } +} diff --git a/include/gui/ocarina.h b/include/gui/ocarina.h index 0722d3a6..42671477 100644 --- a/include/gui/ocarina.h +++ b/include/gui/ocarina.h @@ -8,10 +8,6 @@ #include -/* collection_mgr.cpp */ -void collection_mgr_init(); - - /* main.cpp */ Gtk::Window *ocarina_init(int *, char ***); void connect_button(const std::string &, void (*func)()); @@ -31,6 +27,13 @@ namespace gst } +/* manager.cpp */ +namespace manager +{ + void init(); +} + + /* playlist.cpp */ namespace plist { diff --git a/include/lib/colmgr.h b/include/lib/colmgr.h deleted file mode 100644 index 7d4464dc..00000000 --- a/include/lib/colmgr.h +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Copyright 2014 (c) Anna Schumaker. - */ -#ifndef OCARINA_LIB_COLMGR_H -#define OCARINA_LIB_COLMGR_H - -#include -#include - -namespace colmgr -{ - - void init(); - void add_path(const std::string &); - void del_path(const Gtk::TreePath &); - void update_path(const Gtk::TreePath &); - void update_paths(); - - Glib::ustring get_path(const Gtk::TreePath &); - void toggle_path_enabled(const Gtk::TreePath &); -}; - -#endif /* OCARINA_LIB_COLMGR_H */ diff --git a/lib/colmgr.cpp b/lib/colmgr.cpp deleted file mode 100644 index 6cdd03dc..00000000 --- a/lib/colmgr.cpp +++ /dev/null @@ -1,124 +0,0 @@ -/* - * Copyright 2014 (c) Anna Schumaker. - */ -#include -#include -#include -#include -#include -#include - - -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); - } -}; - - -static CollectionColumns c_cols; -static Glib::RefPtr 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::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 colmgr :: init() -{ - Library *library; - - c_list = lib :: get_object("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); - } -} - -void colmgr :: add_path(const std::string &path) -{ - list_path(library :: add(path)); -} - -void colmgr :: 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 colmgr :: update_path(const Gtk::TreePath &path) -{ - Gtk::TreeModel::Row row = *(c_list->get_iter(path)); - library :: update(find_library(row)); -} - -void colmgr :: 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(); - } -} - -Glib::ustring colmgr :: get_path(const Gtk::TreePath &path) -{ - Gtk::TreeModel::Row row = *(c_list->get_iter(path)); - return row[c_cols.c_path]; -} - -void colmgr :: 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(); -} diff --git a/tests/lib/Sconscript b/tests/lib/Sconscript index 9744334e..bd48e220 100644 --- a/tests/lib/Sconscript +++ b/tests/lib/Sconscript @@ -20,5 +20,4 @@ def test(name): test( "lib" ) test_env.UsePackage("gtkmm-3.0") -test( "colmgr" ) test( "model" ) diff --git a/tests/lib/colmgr.cpp b/tests/lib/colmgr.cpp deleted file mode 100644 index 9f959468..00000000 --- a/tests/lib/colmgr.cpp +++ /dev/null @@ -1,85 +0,0 @@ -/* - * Copyright 2014 (c) Anna Schumaker. - */ -#include -#include -#include -#include -#include -#include -#include - -void test_colmgr() -{ - Library *library; - unsigned int count; - Gtk::TreePath path; - Gtk::TreeModel::Row row; - Glib::RefPtr list; - list = lib :: get_object("colmgr_list"); - - test_equal((size_t)list->children().size(), (size_t)0); - - colmgr :: init(); - test_equal((size_t)list->children().size(), (size_t)1); - - colmgr :: add_path("/tmp/ocarina/"); - test_equal((size_t)list->children().size(), (size_t)2); - - while (idle :: run_task()) - colmgr :: update_paths(); - - - /* - * Test using the newly added library path - */ - row = *list->children().end(); - row--; - - row->get_value(2, count); - test_equal(count, (unsigned)28); - - path = Gtk::TreePath(row); - test_equal(colmgr :: get_path(path), Glib::ustring("/tmp/ocarina/")); - - library = tags :: get_library(1); - test_equal(library->enabled(), true); - colmgr :: toggle_path_enabled(path); - test_equal(library->enabled(), false); - - colmgr :: update_path(path); - test_equal(idle :: run_task(), true); - - colmgr :: del_path(path); - test_equal((size_t)list->children().size(), (size_t)1); - - - /* - * Test using the original path - */ - row = *list->children().begin(); - path = Gtk::TreePath(row); - library = tags :: get_library(0); - - test_equal(library->enabled(), true); - test_equal(library :: get_queue()->size(), (unsigned)20); - - colmgr :: toggle_path_enabled(path); - test_equal(library->enabled(), false); - test_equal(library :: get_queue()->size(), (unsigned)0); - - colmgr :: toggle_path_enabled(path); - test_equal(library->enabled(), true); - test_equal(library :: get_queue()->size(), (unsigned)20); -} - -int main(int argc, char **argv) -{ - Gtk::Main ocarina(&argc, &argv); - test :: cp_data_dir(); - test :: gen_library(); - lib :: init(&argc, &argv, "ocarina6.glade"); - - run_test("Collection Manager Test", test_colmgr); - return 0; -}