From 0c826cbe7bcc075020e5d2be53b7603429137c7c Mon Sep 17 00:00:00 2001 From: Bob van Loosen Date: Tue, 2 Apr 2013 20:17:02 +0200 Subject: [PATCH] added: when there's no artist or title set, show the filename without directories and extension instead --- src/bitvis/mpdclient.cpp | 37 ++++++++++++++++++++++++++----------- src/bitvis/mpdclient.h | 1 + 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/src/bitvis/mpdclient.cpp b/src/bitvis/mpdclient.cpp index 2e79722..aea2d22 100644 --- a/src/bitvis/mpdclient.cpp +++ b/src/bitvis/mpdclient.cpp @@ -85,6 +85,7 @@ bool CMpdClient::GetCurrentSong() string artist; string title; + string file; data.Clear(); while(1) @@ -112,24 +113,17 @@ bool CMpdClient::GetCurrentSong() artist = tmpline.substr(1); else if (word == "Title:") title = tmpline.substr(1); + else if (word == "file:") + file = StripFilename(tmpline.substr(1)); } if (line == "OK") { string songtext; - if (artist.empty() && title.empty()) - { - songtext = "Wat? No title?"; - } + if (artist.empty() || title.empty()) + songtext = file; else - { - if (artist.empty()) - artist = "Unknown artist"; - if (title.empty()) - title = "Unknown title"; - songtext = artist + " - " + title; - } SetCurrentSong(songtext); return true; @@ -262,3 +256,24 @@ bool CMpdClient::GetVolume(int& volume) m_volumechanged = false; return changed; } + +std::string CMpdClient::StripFilename(const std::string& filename) +{ + //strip any preceding directories + size_t start = filename.rfind('/'); + if (start == string::npos) + start = 0; + else if (start < filename.length() - 1) + start++; + + //strip the extension + size_t end = filename.rfind('.'); + size_t nchars; + if (end == string::npos || end == 0 || end <= start) + nchars = string::npos; + else + nchars = end - start; + + return filename.substr(start, nchars); +} + diff --git a/src/bitvis/mpdclient.h b/src/bitvis/mpdclient.h index 53c7ce1..cc28426 100644 --- a/src/bitvis/mpdclient.h +++ b/src/bitvis/mpdclient.h @@ -25,6 +25,7 @@ class CMpdClient : public CThread bool GetPlayStatus(); void SetCurrentSong(const std::string& song); void SetSockError(); + std::string StripFilename(const std::string& filename); int m_port; std::string m_address;