diff --git a/src/config.cpp b/src/config.cpp index 834dee6..271be95 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -55,4 +55,10 @@ Config::load_config() } } +bool +Config::has_last_station() +{ + return (last_station.empty() == false); +} + } // namespace radiotray diff --git a/src/config.hpp b/src/config.hpp index a0f41c3..5400c88 100644 --- a/src/config.hpp +++ b/src/config.hpp @@ -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; diff --git a/src/player.cpp b/src/player.cpp index 9c7e606..2c4a5ec 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -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(); } @@ -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 diff --git a/src/player.hpp b/src/player.hpp index 7032908..450aa1a 100644 --- a/src/player.hpp +++ b/src/player.hpp @@ -37,9 +37,13 @@ class Player void pause(); void stop(); void start(); + Glib::ustring get_station(); + bool has_station(); Glib::RefPtr get_playbin(); + void init_streams(Glib::ustring data_url, Glib::ustring station); + std::shared_ptr em; private: diff --git a/src/tray.cpp b/src/tray.cpp index 1a37df7..75643b3 100644 --- a/src/tray.cpp +++ b/src/tray.cpp @@ -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(); } } @@ -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(); } } @@ -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) {