Merge pull request #275 from cole-h/issue-273

Ensure no NULL tags are set
This commit is contained in:
Alex 2019-04-20 23:32:13 +02:00 committed by GitHub
commit 768bc29bc1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 6 deletions

View File

@ -15,6 +15,7 @@ class MPD : public ALabel {
private: private:
std::thread periodic_updater(); std::thread periodic_updater();
std::string getTag(mpd_tag_type type, unsigned idx = 0);
void setLabel(); void setLabel();
std::string getStateIcon(); std::string getStateIcon();
std::string getOptionIcon(std::string optionName, bool activated); std::string getOptionIcon(std::string optionName, bool activated);

View File

@ -30,6 +30,7 @@
"format": "{stateIcon} {consumeIcon}{randomIcon}{repeatIcon}{singleIcon}{artist} - {album} - {title} ({elapsedTime:%M:%S}/{totalTime:%M:%S}) ", "format": "{stateIcon} {consumeIcon}{randomIcon}{repeatIcon}{singleIcon}{artist} - {album} - {title} ({elapsedTime:%M:%S}/{totalTime:%M:%S}) ",
"format-disconnected": "Disconnected ", "format-disconnected": "Disconnected ",
"format-stopped": "{consumeIcon}{randomIcon}{repeatIcon}{singleIcon}Stopped ", "format-stopped": "{consumeIcon}{randomIcon}{repeatIcon}{singleIcon}Stopped ",
"unknown-tag": "N/A",
"interval": 2, "interval": 2,
"consume-icons": { "consume-icons": {
"on": " " "on": " "

View File

@ -75,6 +75,17 @@ std::thread waybar::modules::MPD::periodic_updater() {
}); });
} }
std::string waybar::modules::MPD::getTag(mpd_tag_type type, unsigned idx) {
std::string result = config_["unknown-tag"].isString() ? config_["unknown-tag"].asString() : "N/A";
const char* tag = mpd_song_get_tag(song_.get(), type, idx);
// mpd_song_get_tag can return NULL, so make sure it's valid before setting
if (tag)
result = tag;
return result;
}
void waybar::modules::MPD::setLabel() { void waybar::modules::MPD::setLabel() {
if (connection_ == nullptr) { if (connection_ == nullptr) {
label_.get_style_context()->add_class("disconnected"); label_.get_style_context()->add_class("disconnected");
@ -123,11 +134,11 @@ void waybar::modules::MPD::setLabel() {
stateIcon = getStateIcon(); stateIcon = getStateIcon();
artist = mpd_song_get_tag(song_.get(), MPD_TAG_ARTIST, 0); artist = getTag(MPD_TAG_ARTIST);
album_artist = mpd_song_get_tag(song_.get(), MPD_TAG_ALBUM_ARTIST, 0); album_artist = getTag(MPD_TAG_ALBUM_ARTIST);
album = mpd_song_get_tag(song_.get(), MPD_TAG_ALBUM, 0); album = getTag(MPD_TAG_ALBUM);
title = mpd_song_get_tag(song_.get(), MPD_TAG_TITLE, 0); title = getTag(MPD_TAG_TITLE);
date = mpd_song_get_tag(song_.get(), MPD_TAG_DATE, 0); date = getTag(MPD_TAG_DATE);
elapsedTime = std::chrono::seconds(mpd_status_get_elapsed_time(status_.get())); elapsedTime = std::chrono::seconds(mpd_status_get_elapsed_time(status_.get()));
totalTime = std::chrono::seconds(mpd_status_get_total_time(status_.get())); totalTime = std::chrono::seconds(mpd_status_get_total_time(status_.get()));
} }
@ -234,7 +245,7 @@ void waybar::modules::MPD::tryConnect() {
try { try {
checkErrors(connection_.get()); checkErrors(connection_.get());
} catch (std::runtime_error e) { } catch (std::runtime_error &e) {
std::cerr << module_name_ << ": Failed to connect to MPD: " << e.what() << std::endl; std::cerr << module_name_ << ": Failed to connect to MPD: " << e.what() << std::endl;
connection_.reset(); connection_.reset();
alternate_connection_.reset(); alternate_connection_.reset();