Anna Schumaker
ca5f0701e9
This is much more useful than a boolean status, since we can use the playlist pointer right away. Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
118 lines
2.8 KiB
C
118 lines
2.8 KiB
C
/*
|
|
* Copyright 2015 (c) Anna Schumaker.
|
|
*/
|
|
#include <core/idle.h>
|
|
#include <core/playlist.h>
|
|
#include <gui/idle.h>
|
|
#include <gui/playlists/library.h>
|
|
#include <gui/sidebar.h>
|
|
#include <gui/window.h>
|
|
#include <glib/gi18n.h>
|
|
|
|
void __gui_pl_library_choose(GtkButton *button, gpointer data)
|
|
{
|
|
GtkFileFilter *filter;
|
|
const gchar *music;
|
|
GtkWidget *dialog;
|
|
gchar *path;
|
|
gint res;
|
|
|
|
filter = gtk_file_filter_new();
|
|
gtk_file_filter_add_mime_type(filter, "inode/directory");
|
|
|
|
dialog = gtk_file_chooser_dialog_new("Add Music", gui_window(),
|
|
GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
|
|
_("_Cancel"), GTK_RESPONSE_CANCEL,
|
|
_("_Open"), GTK_RESPONSE_ACCEPT,
|
|
NULL);
|
|
gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT);
|
|
gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(dialog), filter);
|
|
|
|
music = g_get_user_special_dir(G_USER_DIRECTORY_MUSIC);
|
|
if (music)
|
|
gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog),
|
|
music);
|
|
|
|
res = gtk_dialog_run(GTK_DIALOG(dialog));
|
|
if (res == GTK_RESPONSE_ACCEPT) {
|
|
path = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
|
|
gui_pl_library_add(path);
|
|
g_free(path);
|
|
}
|
|
|
|
gtk_widget_destroy(dialog);
|
|
}
|
|
|
|
static bool __gui_pl_library_header(GtkTreeIter *iter)
|
|
{
|
|
if (gui_sidebar_iter_first(iter))
|
|
return gui_sidebar_iter_find(iter, "Library", PL_MAX_TYPE);
|
|
return false;
|
|
}
|
|
|
|
static bool __gui_pl_library_init_idle()
|
|
{
|
|
struct db_entry *library, *next;
|
|
struct playlist *playlist;
|
|
GtkTreeIter iter;
|
|
|
|
if (!__gui_pl_library_header(&iter))
|
|
return false;
|
|
|
|
db_for_each(library, next, library_db_get()) {
|
|
playlist = LIBRARY(library)->li_playlist;
|
|
gui_sidebar_iter_sort_child(&iter, playlist, "folder");
|
|
}
|
|
|
|
#ifndef CONFIG_TESTING
|
|
if (library_db_get()->db_size == 0)
|
|
__gui_pl_library_choose(NULL, NULL);
|
|
#endif /* CONFIG_TESTING */
|
|
|
|
return true;
|
|
}
|
|
|
|
void gui_pl_library_init()
|
|
{
|
|
idle_schedule(IDLE_SYNC, __gui_pl_library_init_idle, NULL);
|
|
}
|
|
|
|
struct playlist *gui_pl_library_add(const gchar *filename)
|
|
{
|
|
struct playlist *playlist;
|
|
GtkTreeIter iter;
|
|
|
|
if (!__gui_pl_library_header(&iter))
|
|
return false;
|
|
|
|
playlist = playlist_new(PL_LIBRARY, filename);
|
|
if (playlist) {
|
|
gui_sidebar_iter_sort_child(&iter, playlist, "folder");
|
|
gui_idle_enable();
|
|
}
|
|
return playlist;
|
|
}
|
|
|
|
void gui_pl_library_update(struct playlist *playlist)
|
|
{
|
|
GtkTreeIter iter, child;
|
|
|
|
if (!__gui_pl_library_header(&iter))
|
|
return;
|
|
if (!gui_sidebar_iter_down(&iter, &child))
|
|
return;
|
|
if (gui_sidebar_iter_find(&child, playlist->pl_name, playlist->pl_type))
|
|
gui_sidebar_iter_update(&child);
|
|
}
|
|
|
|
void gui_pl_library_select(struct playlist *playlist)
|
|
{
|
|
GtkTreeIter iter, child;
|
|
if (!__gui_pl_library_header(&iter))
|
|
return;
|
|
if (!gui_sidebar_iter_down(&iter, &child))
|
|
return;
|
|
if (gui_sidebar_iter_find(&child, playlist->pl_name, playlist->pl_type))
|
|
gui_sidebar_iter_select(&child);
|
|
}
|