diff --git a/src/mpdpp.cpp b/src/mpdpp.cpp index c71a3aac..0f79317d 100644 --- a/src/mpdpp.cpp +++ b/src/mpdpp.cpp @@ -30,6 +30,53 @@ MPD::Connection Mpd; namespace MPD {// +bool Statistics::empty() const +{ + return m_stats.get() == 0; +} + +unsigned Statistics::artists() const +{ + assert(!empty()); + return mpd_stats_get_number_of_artists(m_stats.get()); +} + +unsigned Statistics::albums() const +{ + assert(!empty()); + return mpd_stats_get_number_of_albums(m_stats.get()); +} + +unsigned Statistics::songs() const +{ + assert(!empty()); + return mpd_stats_get_number_of_songs(m_stats.get()); +} + +unsigned long Statistics::playTime() const +{ + assert(!empty()); + return mpd_stats_get_play_time(m_stats.get()); +} + +unsigned long Statistics::uptime() const +{ + assert(!empty()); + return mpd_stats_get_uptime(m_stats.get()); +} + +unsigned long Statistics::dbUpdateTime() const +{ + assert(!empty()); + return mpd_stats_get_db_update_time(m_stats.get()); +} + +unsigned long Statistics::dbPlayTime() const +{ + assert(!empty()); + return mpd_stats_get_db_play_time(m_stats.get()); +} + Connection::Connection() : itsConnection(0), isCommandsListEnabled(0), isIdle(0), @@ -39,7 +86,6 @@ Connection::Connection() : itsConnection(0), itsTimeout(15), itsCurrentStatus(0), itsOldStatus(0), - itsStats(0), itsUpdater(0), itsErrorHandler(0) { @@ -49,8 +95,6 @@ Connection::~Connection() { if (itsConnection) mpd_connection_free(itsConnection); - if (itsStats) - mpd_stats_free(itsStats); if (itsOldStatus) mpd_status_free(itsOldStatus); if (itsCurrentStatus) @@ -84,8 +128,6 @@ void Connection::Disconnect() { if (itsConnection) mpd_connection_free(itsConnection); - if (itsStats) - mpd_stats_free(itsStats); if (itsOldStatus) mpd_status_free(itsOldStatus); if (itsCurrentStatus) @@ -94,7 +136,6 @@ void Connection::Disconnect() isIdle = 0; itsCurrentStatus = 0; itsOldStatus = 0; - itsStats = 0; isCommandsListEnabled = 0; } @@ -155,6 +196,14 @@ int Connection::GoBusy() return flags; } +Statistics Connection::getStatistics() +{ + assert(itsConnection); + GoBusy(); + mpd_stats *stats = mpd_run_stats(itsConnection); + return Statistics(stats); +} + void Connection::UpdateStatus() { if (!itsConnection) @@ -309,18 +358,6 @@ void Connection::UpdateStatus() } } -void Connection::UpdateStats() -{ - if (!itsConnection) - return; - assert(!isCommandsListEnabled); - GoBusy(); - if (itsStats) - mpd_stats_free(itsStats); - itsStats = mpd_run_stats(itsConnection); - GoIdle(); -} - bool Connection::UpdateDirectory(const std::string &path) { if (!itsConnection) diff --git a/src/mpdpp.h b/src/mpdpp.h index d636949e..c6af960b 100644 --- a/src/mpdpp.h +++ b/src/mpdpp.h @@ -21,6 +21,7 @@ #ifndef _MPDPP_H #define _MPDPP_H +#include #include #include @@ -33,6 +34,26 @@ enum ItemType { itDirectory, itSong, itPlaylist }; enum PlayerState { psUnknown, psStop, psPlay, psPause }; enum ReplayGainMode { rgmOff, rgmTrack, rgmAlbum }; +struct Statistics +{ + friend class Connection; + + bool empty() const; + + unsigned artists() const; + unsigned albums() const; + unsigned songs() const; + unsigned long playTime() const; + unsigned long uptime() const; + unsigned long dbUpdateTime() const; + unsigned long dbPlayTime() const; + +private: + Statistics(mpd_stats *stats) : m_stats(stats, mpd_stats_free) { } + + std::shared_ptr m_stats; +}; + struct Item { std::shared_ptr song; @@ -78,6 +99,8 @@ typedef std::vector OutputList; class Connection { + friend struct Statistics; + typedef void (*StatusUpdater) (Connection *, StatusChanges, void *); typedef void (*ErrorHandler) (Connection *, int, const char *, void *); @@ -106,10 +129,11 @@ public: void SetPassword(const std::string &password) { itsPassword = password; } bool SendPassword(); + Statistics getStatistics(); + void SetStatusUpdater(StatusUpdater, void *); void SetErrorHandler(ErrorHandler, void *); void UpdateStatus(); - void UpdateStats(); bool UpdateDirectory(const std::string &); void Play(); @@ -143,14 +167,6 @@ public: int GetTotalTime() const { return itsCurrentStatus ? mpd_status_get_total_time(itsCurrentStatus) : 0; } unsigned GetBitrate() const { return itsCurrentStatus ? mpd_status_get_kbit_rate(itsCurrentStatus) : 0; } - unsigned NumberOfArtists() const { return itsStats ? mpd_stats_get_number_of_artists(itsStats) : 0; } - unsigned NumberOfAlbums() const { return itsStats ? mpd_stats_get_number_of_albums(itsStats) : 0; } - unsigned NumberOfSongs() const { return itsStats ? mpd_stats_get_number_of_songs(itsStats) : 0; } - unsigned long Uptime() const { return itsStats ? mpd_stats_get_uptime(itsStats) : 0; } - unsigned long DBUpdateTime() const { return itsStats ? mpd_stats_get_db_update_time(itsStats) : 0; } - unsigned long PlayTime() const { return itsStats ? mpd_stats_get_play_time(itsStats) : 0; } - unsigned long DBPlayTime() const { return itsStats ? mpd_stats_get_db_play_time(itsStats) : 0; } - size_t GetPlaylistLength() const { return itsCurrentStatus ? mpd_status_get_queue_length(itsCurrentStatus) : 0; } SongList GetPlaylistChanges(unsigned); @@ -219,6 +235,8 @@ public: StringList GetTagTypes(); private: + //void check + void GoIdle(); int GoBusy(); @@ -243,7 +261,6 @@ private: mpd_status *itsCurrentStatus; mpd_status *itsOldStatus; - mpd_stats *itsStats; unsigned itsElapsed; time_t itsElapsedTimer[2]; diff --git a/src/server_info.cpp b/src/server_info.cpp index df93ad11..0455007f 100644 --- a/src/server_info.cpp +++ b/src/server_info.cpp @@ -97,23 +97,26 @@ void ServerInfo::Update() return; gettimeofday(&past, 0); - Mpd.UpdateStats(); + MPD::Statistics stats = Mpd.getStatistics(); + if (stats.empty()) + return; + w->clear(); *w << NC::fmtBold << U("Version: ") << NC::fmtBoldEnd << U("0.") << Mpd.Version() << U(".*\n"); *w << NC::fmtBold << U("Uptime: ") << NC::fmtBoldEnd; - ShowTime(*w, Mpd.Uptime(), 1); + ShowTime(*w, stats.uptime(), 1); *w << '\n'; - *w << NC::fmtBold << U("Time playing: ") << NC::fmtBoldEnd << MPD::Song::ShowTime(Mpd.PlayTime()) << '\n'; + *w << NC::fmtBold << U("Time playing: ") << NC::fmtBoldEnd << MPD::Song::ShowTime(stats.playTime()) << '\n'; *w << '\n'; *w << NC::fmtBold << U("Total playtime: ") << NC::fmtBoldEnd; - ShowTime(*w, Mpd.DBPlayTime(), 1); + ShowTime(*w, stats.dbPlayTime(), 1); *w << '\n'; - *w << NC::fmtBold << U("Artist names: ") << NC::fmtBoldEnd << Mpd.NumberOfArtists() << '\n'; - *w << NC::fmtBold << U("Album names: ") << NC::fmtBoldEnd << Mpd.NumberOfAlbums() << '\n'; - *w << NC::fmtBold << U("Songs in database: ") << NC::fmtBoldEnd << Mpd.NumberOfSongs() << '\n'; + *w << NC::fmtBold << U("Artist names: ") << NC::fmtBoldEnd << stats.artists() << '\n'; + *w << NC::fmtBold << U("Album names: ") << NC::fmtBoldEnd << stats.albums() << '\n'; + *w << NC::fmtBold << U("Songs in database: ") << NC::fmtBoldEnd << stats.songs() << '\n'; *w << '\n'; - *w << NC::fmtBold << U("Last DB update: ") << NC::fmtBoldEnd << Timestamp(Mpd.DBUpdateTime()) << '\n'; + *w << NC::fmtBold << U("Last DB update: ") << NC::fmtBoldEnd << Timestamp(stats.dbUpdateTime()) << '\n'; *w << '\n'; *w << NC::fmtBold << U("URL Handlers:") << NC::fmtBoldEnd; for (auto it = itsURLHandlers.begin(); it != itsURLHandlers.end(); ++it)