Skip to content

Commit

Permalink
can activate last played station from the previous program's invocation
Browse files Browse the repository at this point in the history
  • Loading branch information
thekvs committed Nov 16, 2016
1 parent 89c1915 commit bd9f35d
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 16 deletions.
6 changes: 6 additions & 0 deletions src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,10 @@ Config::load_config()
}
}

bool
Config::has_last_station()
{
return (last_station.empty() == false);
}

} // namespace radiotray
1 change: 1 addition & 0 deletions src/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Config

void set_config_file(const std::string& name);
void load_config();
bool has_last_station();

Glib::ustring last_station;
long url_timeout_ms = kDefaultHTTPRequestTimeout;
Expand Down
32 changes: 22 additions & 10 deletions src/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,7 @@ Player::init(int argc, char** argv)
void
Player::play(Glib::ustring data_url, Glib::ustring station)
{
bool ok;
std::tie(ok, streams) = playlist.get_streams(data_url);
if ((not ok) or streams.empty()) {
// TODO: D-Bus message
LOG(ERROR) << "Couldn't get audio streams!";
return;
}

current_station = station;

init_streams(data_url, station);
play();
}

Expand Down Expand Up @@ -192,4 +183,25 @@ Player::get_station()
return current_station;
}

bool
Player::has_station()
{
return (current_station.empty() == false);
}

void
Player::init_streams(Glib::ustring data_url, Glib::ustring station)
{
bool ok;

std::tie(ok, streams) = playlist.get_streams(data_url);
if ((not ok) or streams.empty()) {
// TODO: D-Bus message
LOG(ERROR) << "Couldn't get audio streams!";
return;
}

current_station = station;
}

} // namespace radiotray
4 changes: 4 additions & 0 deletions src/player.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,13 @@ class Player
void pause();
void stop();
void start();

Glib::ustring get_station();
bool has_station();
Glib::RefPtr<PlayBin> get_playbin();

void init_streams(Glib::ustring data_url, Glib::ustring station);

std::shared_ptr<EventManager> em;

private:
Expand Down
29 changes: 23 additions & 6 deletions src/tray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ RadioTrayLite::on_current_station_button()
{
if (em->state == StationState::PLAYING) {
player->pause();
} else if (em->state == StationState::IDLE) {
} else if (em->state == StationState::IDLE or em->state == StationState::UNKNOWN) {
player->play();
}
}
Expand Down Expand Up @@ -258,16 +258,17 @@ RadioTrayLite::search_for_bookmarks_file()
auto bookmarks_file_exists = std::bind(file_exists, std::placeholders::_1, kBookmarksFileName);
auto result = std::find_if(std::begin(paths), std::end(paths), bookmarks_file_exists);
if (result != std::end(paths)) {
bookmarks_file = *result + kBookmarksFileName;
auto dir = *result;
bookmarks_file = dir + kBookmarksFileName;

// This is the last search path i.e. bookmarks.xml which comes with distribution
if (*result == paths.back()) {
if (dir == paths.back()) {
copy_default_bookmarks(bookmarks_file);
}

config->set_config_file(*result + kConfigFileName);
config->set_config_file(dir + kConfigFileName);

if (file_exists(*result, kConfigFileName)) {
if (file_exists(dir, kConfigFileName)) {
config->load_config();
}
}
Expand All @@ -276,7 +277,23 @@ RadioTrayLite::search_for_bookmarks_file()
void
RadioTrayLite::set_current_station(bool turn_on)
{
if (not player->get_station().empty()) {
if ((not player->has_station()) and config->has_last_station()) {
try {
char xpath_query[1024];
memset(xpath_query, 0, sizeof(xpath_query));
snprintf(xpath_query, sizeof(xpath_query) - 1, "//bookmark[@name='%s']", config->last_station.c_str());

pugi::xpath_node node = bookmarks_doc.select_node(xpath_query);
if (not node.node().empty()) {
auto data_url = node.node().attribute("url").as_string();
player->init_streams(data_url, config->last_station);
}
} catch (pugi::xpath_exception& exc) {
LOG(WARNING) << "XPath error: " << exc.what();
}
}

if (player->has_station()) {
auto mk_menu_entry = [](Glib::ustring name, bool turn_on) {
std::stringstream ss;
if (turn_on) {
Expand Down

0 comments on commit bd9f35d

Please sign in to comment.