colmgr: Move some collection manager code into lib/

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2014-06-21 12:25:43 -04:00
parent 9ea0fd4f29
commit a5f47e46a8
10 changed files with 191 additions and 93 deletions

View File

@ -3,36 +3,16 @@
*/ */
#include <core/callback.h> #include <core/callback.h>
#include <core/library.h> #include <core/library.h>
#include <lib/colmgr.h>
#include <gui/ocarina.h> #include <gui/ocarina.h>
static void on_library_add(unsigned int id, Library *library);
static class CollectionColumns : public Gtk::TreeModelColumnRecord {
public:
CollectionColumns()
{ add(c_col_id); add(c_col_enabled); add(c_col_size); add(c_col_path); }
Gtk::TreeModelColumn<unsigned int> c_col_id;
Gtk::TreeModelColumn<bool> c_col_enabled;
Gtk::TreeModelColumn<unsigned int> c_col_size;
Gtk::TreeModelColumn<Glib::ustring> c_col_path;
} collection_cols;
static Glib::RefPtr<Gtk::ListStore> get_collection_list()
{
return lib :: get_object<Gtk::ListStore>("o_collection_dirs");
}
static void on_collection_ok() static void on_collection_ok()
{ {
std::string path;
Gtk::FileChooserWidget *chooser; Gtk::FileChooserWidget *chooser;
chooser = lib :: get_widget<Gtk::FileChooserWidget>("o_collection_chooser"); chooser = lib :: get_widget<Gtk::FileChooserWidget>("o_collection_chooser");
path = chooser->get_filename(); colmgr :: add_path(chooser->get_filename());
Library *library = library::add(path);
enable_idle(); enable_idle();
on_library_add(library->id, library);
} }
static void on_collection_update() static void on_collection_update()
@ -45,11 +25,7 @@ static void on_collection_row_activated(const Gtk::TreePath &path,
Gtk::TreeViewColumn *col) Gtk::TreeViewColumn *col)
{ {
Gtk::FileChooser *chooser = lib :: get_widget<Gtk::FileChooser>("o_collection_chooser"); Gtk::FileChooser *chooser = lib :: get_widget<Gtk::FileChooser>("o_collection_chooser");
Glib::RefPtr<Gtk::ListStore> list = get_collection_list(); chooser->set_current_folder(colmgr :: get_path(path));
Gtk::TreeModel::Row row = *(list->get_iter(path));
Glib::ustring dir = row[collection_cols.c_col_path];
chooser->set_current_folder(dir);
} }
#ifndef CONFIG_TEST #ifndef CONFIG_TEST
@ -62,12 +38,8 @@ void do_collection_delete()
Gtk::TreeView *treeview = lib :: get_widget<Gtk::TreeView>("o_collection_treeview"); Gtk::TreeView *treeview = lib :: get_widget<Gtk::TreeView>("o_collection_treeview");
treeview->get_cursor(path, col); treeview->get_cursor(path, col);
if (path) { if (path)
Glib::RefPtr<Gtk::ListStore> list = get_collection_list(); colmgr :: del_path(path);
Gtk::TreeModel::Row row = *(list->get_iter(path));
library :: remove(tagdb :: lookup_library(row[collection_cols.c_col_id]));
list->erase(row);
}
} }
static bool on_collection_key_pressed(GdkEventKey *event) static bool on_collection_key_pressed(GdkEventKey *event)
@ -85,43 +57,12 @@ static
#endif /* CONFIG_TEST */ #endif /* CONFIG_TEST */
void on_collection_toggled(const Glib::ustring &path) void on_collection_toggled(const Glib::ustring &path)
{ {
Glib::RefPtr<Gtk::ListStore> list = get_collection_list(); colmgr :: toggle_path_enabled(Gtk::TreePath(path));
Gtk::TreeModel::Row row = *(list->get_iter(path));
row[collection_cols.c_col_enabled] = !row[collection_cols.c_col_enabled];
library :: set_enabled(tagdb :: lookup_library(row[collection_cols.c_col_id]),
row[collection_cols.c_col_enabled]);
}
static void on_library_add(unsigned int id, Library *library)
{
Gtk::TreeModel::Row row;
Glib::RefPtr<Gtk::ListStore> list = get_collection_list();
row = *(list->append());
row[collection_cols.c_col_id] = id;
row[collection_cols.c_col_enabled] = library->enabled;
row[collection_cols.c_col_size] = library->count;
row[collection_cols.c_col_path] = library->root_path;
}
static void on_library_update(unsigned int id, Library *library)
{
Gtk::TreeModel::Row row;
Glib::RefPtr<Gtk::ListStore> list = get_collection_list();
Gtk::TreeModel::Children children = list->children();
for (Gtk::TreeModel::Children::iterator it = children.begin();
it != children.end(); it++) {
row = *it;
if (row[collection_cols.c_col_id] == id)
row[collection_cols.c_col_size] = library->count;
}
} }
void collection_mgr_init() void collection_mgr_init()
{ {
Gtk::TreeView *treeview = lib :: get_widget<Gtk::TreeView>("o_collection_treeview"); Gtk::TreeView *treeview = lib :: get_widget<Gtk::TreeView>("o_collection_treeview");
Glib::RefPtr<Gtk::ListStore> list = get_collection_list();
Glib::RefPtr<Gtk::CellRendererToggle> toggle; Glib::RefPtr<Gtk::CellRendererToggle> toggle;
toggle = lib :: get_object<Gtk::CellRendererToggle>("o_collection_toggle"); toggle = lib :: get_object<Gtk::CellRendererToggle>("o_collection_toggle");
@ -129,28 +70,9 @@ void collection_mgr_init()
connect_button("o_collection_ok", on_collection_ok); connect_button("o_collection_ok", on_collection_ok);
connect_button("o_collection_update", on_collection_update); connect_button("o_collection_update", on_collection_update);
list->set_sort_column(collection_cols.c_col_path, Gtk::SORT_ASCENDING);
treeview->signal_row_activated().connect(sigc::ptr_fun(on_collection_row_activated)); treeview->signal_row_activated().connect(sigc::ptr_fun(on_collection_row_activated));
treeview->signal_key_press_event().connect(sigc::ptr_fun(on_collection_key_pressed)); treeview->signal_key_press_event().connect(sigc::ptr_fun(on_collection_key_pressed));
toggle->signal_toggled().connect(sigc::ptr_fun(on_collection_toggled)); toggle->signal_toggled().connect(sigc::ptr_fun(on_collection_toggled));
}
colmgr :: init();
void collection_mgr_init2()
{
Database<Library> *db;
Database<Library>::iterator it;
db = &tagdb :: get_library_db();
for (it = db->begin(); it != db->end(); it = db->next(it))
on_library_add((*it)->id, *it);
}
void collection_mgr_update()
{
Database<Library> *db;
Database<Library>::iterator it;
db = &tagdb :: get_library_db();
for (it = db->begin(); it != db->end(); it = db->next(it))
on_library_update((*it)->id, *it);
} }

View File

@ -8,6 +8,7 @@
#include <core/idle.h> #include <core/idle.h>
#include <core/playlist.h> #include <core/playlist.h>
#include <core/print.h> #include <core/print.h>
#include <lib/colmgr.h>
#include <lib/lib.h> #include <lib/lib.h>
#include <gui/ocarina.h> #include <gui/ocarina.h>
@ -195,7 +196,7 @@ bool on_idle()
prog->set_fraction(idle::get_progress()); prog->set_fraction(idle::get_progress());
} }
collection_mgr_update(); colmgr :: update_paths();
return ret; return ret;
} }

View File

@ -14,7 +14,6 @@ Gtk::Window *ocarina_init(int *argc, char ***argv)
Gtk::Window *window = setup_gui(); Gtk::Window *window = setup_gui();
playlist :: select("Favorites"); playlist :: select("Favorites");
post_init_tabs(); post_init_tabs();
collection_mgr_init2();
return window; return window;
} }

View File

@ -10,8 +10,6 @@
/* collection_mgr.cpp */ /* collection_mgr.cpp */
void collection_mgr_init(); void collection_mgr_init();
void collection_mgr_init2();
void collection_mgr_update();
/* main.cpp */ /* main.cpp */

22
include/lib/colmgr.h Normal file
View File

@ -0,0 +1,22 @@
/*
* 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(Gtk::TreePath &);
Glib::ustring get_path(const Gtk::TreePath &);
void update_paths();
void toggle_path_enabled(const Gtk::TreePath &);
};
#endif /* OCARINA_LIB_COLMGR_H */

96
lib/colmgr.cpp Normal file
View File

@ -0,0 +1,96 @@
/*
* Copyright 2014 (c) Anna Schumaker.
*/
#include <core/library.h>
#include <core/tags.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 tagdb :: lookup_library(row[c_cols.c_id]);
}
static void list_path(Library *lib)
{
Gtk::TreeModel::Row row = *(c_list->append());
row[c_cols.c_id] = lib->id;
row[c_cols.c_enabled] = lib->enabled;
row[c_cols.c_size] = lib->count;
row[c_cols.c_path] = lib->root_path;
}
void colmgr :: init()
{
Database<Library>::iterator it;
Database<Library> *db = &tagdb :: get_library_db();
c_list = lib :: get_object<Gtk::ListStore>("o_collection_list");
c_list->set_sort_column(c_cols.c_path, Gtk::SORT_ASCENDING);
for (it = db->begin(); it != db->end(); it != db->next(it))
list_path(*it);
}
void colmgr :: add_path(const std::string &path)
{
list_path(library :: add(path));
}
void colmgr :: del_path(Gtk::TreePath &path)
{
Gtk::TreeModel::Row row = *(c_list->get_iter(path));
Library *lib = find_library(row);
library :: remove(lib);
c_list->erase(row);
}
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 :: 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->count;
}
}
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]);
}

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.16.1 --> <!-- Generated with glade 3.18.3 -->
<interface> <interface>
<requires lib="gtk+" version="3.10"/> <requires lib="gtk+" version="3.10"/>
<object class="GtkAdjustment" id="adjustment1"> <object class="GtkAdjustment" id="adjustment1">
@ -178,7 +178,7 @@
</object> </object>
</child> </child>
</object> </object>
<object class="GtkListStore" id="o_collection_dirs"> <object class="GtkListStore" id="o_collection_list">
<columns> <columns>
<!-- column-name id --> <!-- column-name id -->
<column type="guint"/> <column type="guint"/>
@ -1096,7 +1096,7 @@ Manager</property>
<property name="can_focus">True</property> <property name="can_focus">True</property>
<property name="hscroll_policy">natural</property> <property name="hscroll_policy">natural</property>
<property name="vscroll_policy">natural</property> <property name="vscroll_policy">natural</property>
<property name="model">o_collection_dirs</property> <property name="model">o_collection_list</property>
<property name="rules_hint">True</property> <property name="rules_hint">True</property>
<property name="enable_search">False</property> <property name="enable_search">False</property>
<child internal-child="selection"> <child internal-child="selection">

View File

@ -1 +1,2 @@
lib lib
colmgr

View File

@ -24,6 +24,7 @@ class LibTest(OTest):
res = TestList("lib", [ res = TestList("lib", [
LibTest("lib.cpp", "gtkmm-3.0"), LibTest("lib.cpp", "gtkmm-3.0"),
LibTest("colmgr.cpp"),
]).prepare() ]).prepare()

58
tests/lib/colmgr.cpp Normal file
View File

@ -0,0 +1,58 @@
/*
* Copyright 2014 (c) Anna Schumaker.
*/
#include <core/idle.h>
#include <core/tags.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>("o_collection_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();
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 = tagdb :: lookup_library(1);
test_equal(library->enabled, true);
colmgr :: toggle_path_enabled(path);
test_equal(library->enabled, false);
colmgr :: del_path(path);
test_equal((size_t)list->children().size(), (size_t)1);
}
int main(int argc, char **argv)
{
Gtk::Main ocarina(&argc, &argv);
test :: cp_data_dir();
lib :: init(&argc, &argv, "ocarina6.glade");
run_test("Collection Manager Test", test_colmgr);
return 0;
}