ocarina/ocarina/status.cpp

204 lines
5.7 KiB
C++

// Copyright (c) 2012 Bryan Schumaker
#include <format.h>
#include <audio.h>
#include <track.h>
#include <prefs.h>
#include <deck.h>
#include <idle.h>
#include <ban.h>
#include "ocarina.h"
#include <sstream>
using namespace std;
static void set_label(const string &name, const string &text, const string &size)
{
GtkWidget *label = get_widget(name);
if (!label)
return;
if (size == "") {
gtk_label_set_text(GTK_LABEL(label), text.c_str());
} else {
char *escaped = g_markup_escape_text(text.c_str(), -1);
string markup = "<span size='" + size + "'>" + escaped + "</span>";
gtk_label_set_markup(GTK_LABEL(label), markup.c_str());
g_free(escaped);
}
}
void update_labels(libsaria::Track *track)
{
set_label("TitleLabel", track->title, "xx-large");
set_label("ArtistLabel", track->artist, "x-large");
set_label("AlbumLabel", track->album, "x-large");
set_label("DurLabel", track->lenstr, "");
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(get_widget("BanButton")),
track->banned);
}
void update_buttons(notify_t event)
{
GtkWidget *play, *pause;
play = get_widget("PlayButton");
pause = get_widget("PauseButton");
if (event == PLAYING) {
gtk_widget_show(pause);
gtk_widget_hide(play);
} else {
gtk_widget_show(play);
gtk_widget_hide(pause);
}
}
void update_progress()
{
GtkWidget *label = get_widget("PosLabel");
GtkWidget *progress = get_widget("TrackProgress");
gint64 pos = libsaria::audio::position();
gint64 dur = libsaria::audio::duration() + 1;
gtk_label_set_text(GTK_LABEL(label), libsaria::audio::posstr().c_str());
/*
* This happens when gstreamer's "about-to-finish" signal
* is emitted and we queue up a shorter song before the
* pipeline finishes.
*/
if (pos > dur)
pos = 0;
gtk_range_set_range(GTK_RANGE(progress), 0, dur);
gtk_range_set_value(GTK_RANGE(progress), pos);
}
static void slider_changed(GtkWidget *w, GtkScrollType s, gdouble v, gpointer d)
{
libsaria::audio::seek_to(v);
}
void update_autopause_type(AutoPauseType *type)
{
GtkWidget *button;
if (*type == PS_NONE)
button = get_widget("NoPause");
else
button = get_widget("PauseAfterN");
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), true);
}
void update_autopause_count(unsigned int *count)
{
gtk_spin_button_set_value(GTK_SPIN_BUTTON(get_widget("PauseCounter")), *count);
}
static void toggle_pause(GtkWidget *b, gpointer d)
{
if (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(b)))
return;
if (b == get_widget("NoPause"))
libsaria::deck::set_pause_type(PS_NONE, 0);
else {
unsigned short count = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(get_widget("PauseCounter")));
libsaria::deck::set_pause_type(PS_AFTER_N, count);
}
}
static void counter_changed(GtkWidget *b, GtkScrollType *s, gpointer d)
{
unsigned short count = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(get_widget("PauseCounter")));
libsaria::deck::set_pause_type(PS_AFTER_N, count);
}
static void toggle_ban(GtkWidget *b, gpointer d)
{
libsaria::Track *cur = libsaria::current_track();
GtkWidget *ban = get_widget("BanButton");
bool banned = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(ban));
if (banned)
gtk_button_set_image(GTK_BUTTON(ban), get_widget("NoImage"));
else
gtk_button_set_image(GTK_BUTTON(ban), get_widget("YesImage"));
if (cur->banned == banned)
return;
cur->set_banned(banned);
if (banned)
libsaria::deck::next();
}
static void toggle_playlist(GtkToggleButton *button, gpointer data)
{
libsaria::Playlist *plist = (libsaria::Playlist *)data;
struct PlaylistWidgets *widgets = find_playlist_widgets(plist);
bool show = gtk_toggle_button_get_active(button);
if (show)
gtk_widget_show(GTK_WIDGET(widgets->page_box));
else
gtk_widget_hide(GTK_WIDGET(widgets->page_box));
if (GTK_WIDGET(button) == get_widget("ShowRecent"))
libsaria::prefs::set("ocarina.showrecent", show);
else if (GTK_WIDGET(button) == get_widget("ShowBanned"))
libsaria::prefs::set("ocarina.showbanned", show);
}
bool update_idle_bar(int size)
{
GtkWidget *idle = get_widget("IdleProgress");
if (size == 0) {
gtk_widget_hide(idle);
return false;
}
gtk_widget_show(idle);
gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(idle),
libsaria::idle::progress());
return true;
}
void update_length_label(libsaria::Playlist *playlist)
{
stringstream stream;
unsigned int size = playlist->get_size();
GtkWidget *label = get_widget("LengthLabel");
if (!label)
return;
stream << size << " song";
if (size != 1)
stream << "s";
if (size != 0)
stream << ": " << libsaria::length_string(playlist->get_length());
gtk_label_set_text(GTK_LABEL(label), stream.str().c_str());
}
void init_plist_visible_button(const string &button_name, const string &pref,
libsaria::Playlist *plist)
{
bool show = libsaria::prefs::init(pref, true);
GtkToggleButton *button = GTK_TOGGLE_BUTTON(get_widget(button_name));
gtk_toggle_button_set_active(button, show);
connect_signal(button_name, "toggled", G_CALLBACK(toggle_playlist), plist);
toggle_playlist(button, (void *)plist);
}
void init_status()
{
connect_signal("TrackProgress", "change-value", G_CALLBACK(slider_changed), NULL);
connect_signal("NoPause", "toggled", G_CALLBACK(toggle_pause), NULL);
connect_signal("PauseAfterN", "toggled", G_CALLBACK(toggle_pause), NULL);
connect_signal("PauseCounter", "value-changed", G_CALLBACK(counter_changed), NULL);
connect_signal("BanButton", "toggled", G_CALLBACK(toggle_ban), NULL);
connect_signal("ShowBanned", "toggled", G_CALLBACK(toggle_playlist), libsaria::ban::get_banned_plist());
init_plist_visible_button("ShowRecent", "ocarina.showrecent", libsaria::deck::get_recent_plist());
init_plist_visible_button("ShowBanned", "ocarina.showbanned", libsaria::ban::get_banned_plist());
}