ocarina/ocarina/settings/library.cpp

120 lines
3.0 KiB
C++

#include <sstream>
#include <list>
using namespace std;
#include <ocarina/settings.h>
#include <ocarina/chooser.h>
#include <ocarina/button.h>
#include <ocarina/gtk.h>
#include <libsaria/library.h>
#include <libsaria/print.h>
/* This is a gtk vbox */
static GtkWidget *library_settings = NULL;
static GtkWidget *path_panels = NULL;
static list<GtkWidget *> path_widgets;
static void on_click_add(GtkWidget *b, GdkEvent *e, gpointer d)
{
string dir = ocarina_choose_dir();
if (dir != "" ) {
println("Scanning dir: " + dir);
libsaria::library::add_path(dir);
}
}
static void on_click_update(GtkWidget *b, GdkEvent *e, gpointer d)
{
libsaria::library::update();
}
static GtkWidget *make_add_button()
{
return make_text_button(GTK_STOCK_ADD,
"Add to Library",
on_click_add,
true);
}
static GtkWidget *make_update_button()
{
return make_text_button(GTK_STOCK_REFRESH,
"Update Library",
on_click_update,
true);
}
static GtkWidget *markup_label(string markup)
{
GtkWidget *label = gtk_label_new("");
gtk_label_set_markup(GTK_LABEL(label), markup.c_str());
return label;
}
static GtkWidget *make_path_info(string path, unsigned int size)
{
stringstream stream;
GtkWidget *path_label, *size_label;
GtkWidget *box = gtk_vbox_new(FALSE, 0);
stream << "<span size='xx-large' weight='bold'>" << path << "</span>";
path_label = markup_label(stream.str());
stream.str("");
stream << "<span size='large'>" << size << " files</span>";
size_label = markup_label(stream.str());
box_pack_start(box, path_label, TRUE, TRUE, 0);
box_pack_start(box, size_label, TRUE, TRUE, 0);
return box;
}
static void add_library_panel(GtkWidget *content)
{
GtkWidget *panel_box = gtk_vbox_new(FALSE, 0);
GtkWidget *sep = gtk_hseparator_new();
box_pack_start(panel_box, content, TRUE, TRUE, 0);
box_pack_start(panel_box, sep, TRUE, TRUE, 0);
box_pack_start(path_panels, panel_box, FALSE, FALSE, 0);
path_widgets.push_back(panel_box);
}
static void add_library_path(struct libsaria::library::PathInfo &info)
{
GtkWidget *main_box = gtk_hbox_new(FALSE, 0);
GtkWidget *info_box = make_path_info(info.path, info.size);
box_pack_start(main_box, info_box, TRUE, TRUE, 0);
add_library_panel(main_box);
}
void library_settings_refresh()
{
while (path_widgets.size() != 0) {
gtk_widget_destroy(path_widgets.front());
path_widgets.pop_front();
}
libsaria::library::for_each_path(add_library_path);
gtk_widget_show_all(library_settings);
}
void library_settings_init()
{
GtkWidget *button_row = gtk_hbox_new(FALSE, 0);
library_settings = gtk_vbox_new(FALSE, 0);
path_panels = gtk_vbox_new(FALSE, 0);
libsaria::library::for_each_path(add_library_path);
box_pack_start(library_settings, path_panels, FALSE, FALSE, 0);
box_pack_start(library_settings, button_row, FALSE, FALSE, 0);
box_pack_end(button_row, make_add_button(), FALSE, FALSE, 0);
box_pack_end(button_row, make_update_button(), FALSE, FALSE, 0);
gtk_widget_show_all(library_settings);
add_settings_page("Library", library_settings);
}