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 <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2015-01-17 12:12:07 -05:00
parent 1935cf9c5e
commit 98ff0b79cd
8 changed files with 206 additions and 343 deletions

View File

@ -1,105 +0,0 @@
/*
* Copyright 2014 (c) Anna Schumaker.
*/
#include <core/callback.h>
#include <core/idle.h>
#include <core/index.h>
#include <core/library.h>
#include <lib/colmgr.h>
#include <gui/ocarina.h>
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<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));
colmgr :: init();
}

View File

@ -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();

198
gui/manager.cpp Normal file
View File

@ -0,0 +1,198 @@
/*
* 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);
}
}

View File

@ -8,10 +8,6 @@
#include <lib/lib.h>
/* 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
{

View File

@ -1,23 +0,0 @@
/*
* Copyright 2014 (c) Anna Schumaker.
*/
#ifndef OCARINA_LIB_COLMGR_H
#define OCARINA_LIB_COLMGR_H
#include <gtkmm.h>
#include <string>
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 */

View File

@ -1,124 +0,0 @@
/*
* Copyright 2014 (c) Anna Schumaker.
*/
#include <core/library.h>
#include <core/playlist.h>
#include <core/tags/track.h>
#include <lib/colmgr.h>
#include <lib/lib.h>
#include <gtkmm.h>
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);
}
};
static CollectionColumns c_cols;
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 colmgr :: init()
{
Library *library;
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);
}
}
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();
}

View File

@ -20,5 +20,4 @@ def test(name):
test( "lib" )
test_env.UsePackage("gtkmm-3.0")
test( "colmgr" )
test( "model" )

View File

@ -1,85 +0,0 @@
/*
* Copyright 2014 (c) Anna Schumaker.
*/
#include <core/idle.h>
#include <core/library.h>
#include <lib/colmgr.h>
#include <lib/lib.h>
#include <tests/test.h>
#include <gtkmm.h>
#include <string>
void test_colmgr()
{
Library *library;
unsigned int count;
Gtk::TreePath path;
Gtk::TreeModel::Row row;
Glib::RefPtr<Gtk::ListStore> list;
list = lib :: get_object<Gtk::ListStore>("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;
}