ocarina/lib/colmgr.cpp

107 lines
2.3 KiB
C++

/*
* 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;
if (!lib)
return;
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>("colmgr_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(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->count;
}
}
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]);
}