libsaria: Create functions for checking and setting flags

I also replace a few PLAYLIST_* notifications with a generic
PLAYLIST_CHANGED notification for when small changes occur.  I think
that using set_flag() and check_flag() will make code cleaner and easier
to maintain.

Signed-off-by: Bryan Schumaker <bjschuma@gmail.com>
This commit is contained in:
Bryan Schumaker 2012-11-03 22:34:03 -04:00
parent 0744aaad45
commit e1930b0c88
10 changed files with 42 additions and 90 deletions

View File

@ -15,9 +15,7 @@ enum notify_t {
PLAYLIST_NEW, // libsaria::Playlist *
PLAYLIST_DELETE, // libsaria::Playlist *
PLAYLIST_RENUMBER, // libsaria::Playlist *
PLAYLIST_DISABLE, // libsaria::Playlist *
PLAYLIST_RANDOM, // libsaria::Playlist *
PLAYLIST_SORTED, // libsaria::Playlist *
PLAYLIST_CHANGED, // libsaria::Playlist *
PLAYLIST_ADD, // libsaria::PlaylistNotification *
PLAYLIST_RM, // libsaria::PlaylistNotification *
PLAYLIST_UPDATE, // libsaria::PlaylistNotification *

View File

@ -34,7 +34,6 @@ namespace libsaria
unsigned int cur;
Index index;
void set_flag(bool, PlaylistFlags);
void schedule_save();
void rm_file();
void notify_ui(notify_t, Track *, unsigned int);
@ -59,21 +58,15 @@ namespace libsaria
void push_back(list<Track *> &);
Track *next();
void set_random(bool);
bool get_random();
void set_sorted(bool);
bool get_sorted();
void set_flag(PlaylistFlags, bool);
bool check_flag(PlaylistFlags);
void save(ofstream &);
bool is_static();
bool is_disabled();
void prepare_for_removal();
void reset_iterator();
void set_filter_text(string &);
bool is_visible(libsaria::Track *);
void set_disabled(bool);
void renumber(int);
unsigned int get_number();
unsigned int get_size();

View File

@ -172,7 +172,7 @@ namespace libsaria
void library::set_random(bool random)
{
prefs::set("libsaria.library.random", random);
lib_playlist.set_random(random);
lib_playlist.set_flag(PL_RANDOM, random);
}
Playlist *library::get_playlist()
@ -184,7 +184,7 @@ namespace libsaria
{
IdleCallback *task;
bool random = prefs::init("libsaria.library.random", true);
lib_playlist.set_random(random);
lib_playlist.set_flag(PL_RANDOM, random);
app::mkdir("library");
next_id = schedule_load();

View File

@ -53,7 +53,7 @@ namespace libsaria
{
list<unsigned int>::iterator it;
if (is_static())
if (check_flag(PL_STATIC))
return;
indices.sort();
@ -66,7 +66,7 @@ namespace libsaria
plist.insert(plist.begin(), track);
add_track(track, 0);
if (get_sorted())
if (check_flag(PL_SORTED))
do_sort();
}
@ -75,7 +75,7 @@ namespace libsaria
plist.push_back(track);
add_track(track, plist.size());
if (get_sorted())
if (check_flag(PL_SORTED))
do_sort();
}
@ -87,7 +87,7 @@ namespace libsaria
add_track(*it, plist.size());
}
if (get_sorted())
if (check_flag(PL_SORTED))
do_sort();
}

View File

@ -31,7 +31,7 @@ static libsaria::Playlist *get_active_playlist()
list<libsaria::Playlist *>::iterator it;
for (it = playlist_deck.begin(); it != playlist_deck.end(); it++) {
if (!(*it)->is_disabled())
if (!(*it)->check_flag(PL_DISABLED))
return *it;
}

View File

@ -80,10 +80,10 @@ namespace libsaria
vector<Track *>::iterator it;
stream << PLAYLIST_CURRENT_VERSION << "\n";
stream << plist.size() << " ";
stream << is_disabled() <<" ";
stream << get_random() << " ";
stream << get_sorted() << "\n";
stream << plist.size() << " ";
stream << check_flag(PL_DISABLED) <<" ";
stream << check_flag(PL_RANDOM) << " ";
stream << check_flag(PL_SORTED) << "\n";
for (it = plist.begin(); it != plist.end(); it++) {
stream << (**it)[TRACK_LIBID] << " ";

View File

@ -20,53 +20,21 @@ namespace libsaria
{
}
void Playlist::set_flag(bool enabled, PlaylistFlags flag)
void Playlist::set_flag(PlaylistFlags flag, bool enabled)
{
if (enabled)
if (enabled) {
flags |= flag;
else
if (flag == PL_SORTED)
do_sort();
} else
flags &= ~flag;
schedule_save();
notify(PLAYLIST_CHANGED, this);
}
bool Playlist::get_sorted()
bool Playlist::check_flag(PlaylistFlags flag)
{
return (flags & PL_SORTED) == PL_SORTED;
}
void Playlist::set_sorted(bool sorted)
{
set_flag(sorted, PL_SORTED);
do_sort();
notify_update_all();
notify(PLAYLIST_SORTED, this);
}
bool Playlist::get_random()
{
return (flags & PL_RANDOM) == PL_RANDOM;
}
void Playlist::set_random(bool random)
{
set_flag(random, PL_RANDOM);
notify(PLAYLIST_RANDOM, this);
}
bool Playlist::is_static()
{
return (flags & PL_STATIC) == PL_STATIC;
}
bool Playlist::is_disabled()
{
return (flags & PL_DISABLED) == PL_DISABLED;
}
void Playlist::set_disabled(bool disabled)
{
set_flag(disabled, PL_DISABLED);
notify(PLAYLIST_DISABLE, this);
return (flags & flag) == flag;
}
void Playlist::track_updated(Track *track)
@ -146,7 +114,7 @@ namespace libsaria
if (get_size() == 0)
return NULL;
if (get_random())
if (check_flag(PL_RANDOM))
cur += rand() % get_size();
else if (flags & PL_NO_DRAIN)
cur++;
@ -174,13 +142,8 @@ namespace libsaria
void Playlist::notify_update_all()
{
vector<Track *>::iterator it;
unsigned int index = 0;
for (it = plist.begin(); it != plist.end(); it++) {
notify_ui(PLAYLIST_UPDATE, *it, index);
index++;
}
for (unsigned int i = 0; i < plist.size(); i++)
notify_ui(PLAYLIST_UPDATE, plist[i], i);
}
}

View File

@ -38,9 +38,7 @@ void on_notify(notify_t event, void *data)
case PLAYLIST_NEW:
case PLAYLIST_DELETE:
case PLAYLIST_RENUMBER:
case PLAYLIST_DISABLE:
case PLAYLIST_RANDOM:
case PLAYLIST_SORTED:
case PLAYLIST_CHANGED:
update_tabs(event, (libsaria::Playlist *)data);
break;
case PLAYLIST_ADD:

View File

@ -387,7 +387,7 @@ static void playlist_close(GtkWidget *button, gpointer data)
static void playlist_disable(GtkToggleButton *button, gpointer data)
{
struct libsaria::Playlist *playlist = (libsaria::Playlist *)data;
playlist->set_disabled(gtk_toggle_button_get_active(button));
playlist->set_flag(PL_DISABLED, gtk_toggle_button_get_active(button));
}
static void playlist_random(GtkToggleButton *button, gpointer data)
@ -396,13 +396,13 @@ static void playlist_random(GtkToggleButton *button, gpointer data)
if (playlist == libsaria::library::get_playlist())
libsaria::library::set_random(gtk_toggle_button_get_active(button));
else
playlist->set_random(gtk_toggle_button_get_active(button));
playlist->set_flag(PL_RANDOM, gtk_toggle_button_get_active(button));
}
static void playlist_sort(GtkToggleButton *button, gpointer data)
{
struct libsaria::Playlist *playlist = (libsaria::Playlist *)data;
playlist->set_sorted(gtk_toggle_button_get_active(button));
playlist->set_flag(PL_SORTED, gtk_toggle_button_get_active(button));
}
void setup_widgets(struct PlaylistWidgets *widgets, libsaria::Playlist *playlist)
@ -469,7 +469,7 @@ static GtkWidget *setup_random_button(struct PlaylistWidgets *widgets)
gtk_button_set_image(GTK_BUTTON(widgets->random), image);
gtk_widget_set_name(GTK_WIDGET(widgets->random), "ocarina-small-button");
gtk_toggle_button_set_active(widgets->random, widgets->playlist->get_random());
gtk_toggle_button_set_active(widgets->random, widgets->playlist->check_flag(PL_RANDOM));
g_signal_connect(GTK_WIDGET(widgets->random), "toggled", G_CALLBACK(playlist_random), widgets->playlist);
return GTK_WIDGET(widgets->random);
@ -482,7 +482,7 @@ static GtkWidget *setup_sort_button(struct PlaylistWidgets *widgets)
gtk_button_set_image(GTK_BUTTON(widgets->sort), image);
gtk_widget_set_name(GTK_WIDGET(widgets->sort), "ocarina-small-button");
gtk_toggle_button_set_active(widgets->sort, widgets->playlist->get_sorted());
gtk_toggle_button_set_active(widgets->sort, widgets->playlist->check_flag(PL_SORTED));
g_signal_connect(GTK_WIDGET(widgets->sort), "toggled", G_CALLBACK(playlist_sort), widgets->playlist);
return GTK_WIDGET(widgets->sort);

View File

@ -244,26 +244,28 @@ static void renumber_playlist(libsaria::Playlist *playlist)
static void disable_playlist(libsaria::Playlist *playlist)
{
bool enabled = !playlist->is_disabled();
bool enabled = !playlist->check_flag(PL_DISABLED);
struct PlaylistWidgets *widgets = find_playlist_widgets(playlist);
gtk_toggle_button_set_active(widgets->disable, !enabled);
gtk_widget_set_sensitive(GTK_WIDGET(widgets->treeview), enabled);
gtk_widget_set_sensitive(GTK_WIDGET(widgets->tab_box), enabled);
if (widgets) {
gtk_toggle_button_set_active(widgets->disable, !enabled);
gtk_widget_set_sensitive(GTK_WIDGET(widgets->treeview), enabled);
gtk_widget_set_sensitive(GTK_WIDGET(widgets->tab_box), enabled);
}
}
static void random_playlist(libsaria::Playlist *playlist)
{
struct PlaylistWidgets *widgets = find_playlist_widgets(playlist);
if (widgets)
gtk_toggle_button_set_active(widgets->random, playlist->get_random());
gtk_toggle_button_set_active(widgets->random, playlist->check_flag(PL_RANDOM));
}
static void sort_playlist(libsaria::Playlist *playlist)
{
struct PlaylistWidgets *widgets = find_playlist_widgets(playlist);
if (widgets)
gtk_toggle_button_set_active(widgets->sort, playlist->get_sorted());
gtk_toggle_button_set_active(widgets->sort, playlist->check_flag(PL_SORTED));
}
void update_tabs(notify_t event, libsaria::Playlist *playlist)
@ -272,12 +274,10 @@ void update_tabs(notify_t event, libsaria::Playlist *playlist)
new_playlist(playlist);
else if (event == PLAYLIST_DELETE)
delete_playlist(playlist);
else if (event == PLAYLIST_RENUMBER)
renumber_playlist(playlist);
else if (event == PLAYLIST_DISABLE)
else if (event == PLAYLIST_CHANGED) {
disable_playlist(playlist);
else if (event == PLAYLIST_RANDOM)
random_playlist(playlist);
else if (event == PLAYLIST_SORTED)
sort_playlist(playlist);
} else if (event == PLAYLIST_RENUMBER)
renumber_playlist(playlist);
}