ocarina/ocarina/settings/library.cpp

189 lines
4.5 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>
struct PathInfo
{
GtkWidget *widget;
GtkWidget *remove;
GtkWidget *update;
string path;
};
/* This is a gtk vbox */
static GtkWidget *library_settings = NULL;
static GtkWidget *path_panels = NULL;
static list<PathInfo> 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 void on_click_update_path(GtkWidget *b, GdkEvent *e, gpointer d)
{
list<PathInfo>::iterator it;
for (it = path_widgets.begin(); it != path_widgets.end(); it++) {
if ((*it).update == b) {
libsaria::library::update_path((*it).path);
break;
}
}
}
static void on_click_remove(GtkWidget *b, GdkEvent *e, gpointer d)
{
list<PathInfo>::iterator it;
for (it = path_widgets.begin(); it != path_widgets.end(); it++) {
if ((*it).remove == b) {
libsaria::library::remove_path((*it).path);
break;
}
}
}
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 *make_update_path_button()
{
return make_text_button(GTK_STOCK_REFRESH,
"Update Path",
on_click_update_path,
true);
}
static GtkWidget *make_remove_button()
{
return make_text_button(GTK_STOCK_REMOVE,
"Remove Path",
on_click_remove,
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 GtkWidget *add_buttons(GtkWidget *info_box, struct PathInfo &path)
{
GtkWidget *button_box = gtk_hbox_new(FALSE, 0);
GtkWidget *remove_btn = make_remove_button();
GtkWidget *update_btn = make_update_path_button();
path.remove = remove_btn;
path.update = update_btn;
box_pack_start(button_box, info_box, TRUE, TRUE, 0);
box_pack_start(button_box, update_btn, FALSE, FALSE, 0);
box_pack_start(button_box, remove_btn, FALSE, FALSE, 0);
return button_box;
}
static void add_library_panel(GtkWidget *content, struct PathInfo &path)
{
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.widget = panel_box;
path_widgets.push_back(path);
}
static void add_library_path(struct libsaria::library::PathInfo &info)
{
struct PathInfo path;
GtkWidget *main_box = gtk_hbox_new(FALSE, 0);
GtkWidget *info_box = make_path_info(info.path, info.size);
GtkWidget *buttons = add_buttons(info_box, path);
path.path = info.path;
box_pack_start(main_box, buttons, TRUE, TRUE, 0);
add_library_panel(main_box, path);
}
void library_settings_refresh()
{
while (path_widgets.size() != 0) {
gtk_widget_destroy(path_widgets.front().widget);
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);
}