ocarina/ocarina/library.cpp

148 lines
3.6 KiB
C++

// Copyright (c) 2012 Bryan Schumaker
#include <library.h>
#include "ocarina.h"
static inline GtkListStore *get_list()
{
return GTK_LIST_STORE(get_object("LibraryList"));
}
static bool find_iter(libsaria::library::Path *path, GtkTreeIter *iter)
{
libsaria::library::Path *list_path;
/* get_iter_first() return FALSE if the liststore is empty */
if (!gtk_tree_model_get_iter_first(GTK_TREE_MODEL(get_list()), iter))
return false;
do {
gtk_tree_model_get(GTK_TREE_MODEL(get_list()), iter, 0, &list_path, -1);
if (list_path == path)
return true;
} while (gtk_tree_model_iter_next(GTK_TREE_MODEL(get_list()), iter));
return false;
}
static libsaria::library::Path *find_path(gchar *path)
{
GtkTreeIter iter;
libsaria::library::Path *lib_path = NULL;
if (!gtk_tree_model_get_iter_from_string(GTK_TREE_MODEL(get_list()), &iter, path))
return NULL;
gtk_tree_model_get(GTK_TREE_MODEL(get_list()), &iter, 0, &lib_path, -1);
return lib_path;
}
static libsaria::library::Path *get_selected_path(GtkWidget *treeview)
{
GtkTreeIter iter;
GtkTreeModel *model = GTK_TREE_MODEL(get_list());
GtkTreeSelection *sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview));
libsaria::library::Path *path;
if (!gtk_tree_selection_get_selected(sel, &model, &iter))
return NULL;
gtk_tree_model_get(model, &iter, 0, &path, -1);
return path;
}
static void path_updated(libsaria::library::Path *path)
{
GtkTreeIter iter;
if (!find_iter(path, &iter))
return;
gtk_list_store_set(get_list(), &iter,
1, path->visible,
2, path->path.c_str(),
3, path->tracks.size(),
-1);
}
static void path_added(libsaria::library::Path *path)
{
GtkTreeIter iter;
println("Path added: " + path->path);
gtk_list_store_append(get_list(), &iter);
gtk_list_store_set(get_list(), &iter,
0, path,
1, path->visible,
2, path->path.c_str(),
3, path->tracks.size(),
-1);
}
static void path_removed(libsaria::library::Path *path)
{
GtkTreeIter iter;
if (!find_iter(path, &iter))
return;
gtk_list_store_remove(get_list(), &iter);
}
void notify_library(notify_t event, libsaria::library::Path *path)
{
switch (event) {
case PATH_ADDED:
path_added(path);
break;
case PATH_DELETED:
path_removed(path);
break;
case PATH_UPDATED:
path_updated(path);
default:
break;
}
}
static void add_library(GtkWidget *b, gpointer d)
{
string dir = run_chooser("DirectoryChooser");
if (dir != "") {
println("Scanning dir: " + dir);
libsaria::library::add_path(dir);
}
}
static void path_toggled(GtkCellRendererToggle *toggle, gchar *path, gpointer d)
{
libsaria::library::Path *lib_path = find_path(path);
if (!path)
return;
if (gtk_cell_renderer_toggle_get_active(toggle))
libsaria::library::show_path(lib_path);
else
libsaria::library::hide_path(lib_path);
}
static gboolean key_pressed(GtkWidget *treeview, GdkEvent *event, gpointer data)
{
libsaria::library::Path *path;
string key = gdk_keyval_name(event->key.keyval);
println("Library settings handling key press: " + key);
path = get_selected_path(treeview);
if (!path)
return FALSE;
if (key == "Delete")
libsaria::library::delete_path(path);
else if (key == "plus" || key == "KP_Add")
add_library(NULL, NULL);
else
return FALSE;
return TRUE;
}
void init_library()
{
connect_signal("AddLibrary", "clicked", G_CALLBACK(add_library), NULL);
connect_signal("UpdateLibraries", "clicked", G_CALLBACK(libsaria::library::update_all), NULL);
connect_signal("LibraryToggle", "toggled", G_CALLBACK(path_toggled), NULL);
connect_signal("LibraryView", "key-press-event", G_CALLBACK(key_pressed), NULL);
}