From 7721dcdae80553584cfaf53f857335dfd2b07bc1 Mon Sep 17 00:00:00 2001 From: giskard Date: Tue, 14 May 2024 15:00:05 +0800 Subject: [PATCH] mpris: some clang-tidy fix --- src/modules/mpris/mpris.cpp | 72 ++++++++++++++++++------------------- 1 file changed, 34 insertions(+), 38 deletions(-) diff --git a/src/modules/mpris/mpris.cpp b/src/modules/mpris/mpris.cpp index 1e8741f8..ed383b0c 100644 --- a/src/modules/mpris/mpris.cpp +++ b/src/modules/mpris/mpris.cpp @@ -96,9 +96,9 @@ Mpris::Mpris(const std::string& id, const Json::Value& config) } if (config_["dynamic-order"].isArray()) { dynamic_order_.clear(); - for (auto it = config_["dynamic-order"].begin(); it != config_["dynamic-order"].end(); ++it) { - if (it->isString()) { - dynamic_order_.push_back(it->asString()); + for (const auto& item : config_["dynamic-order"]) { + if (item.isString()) { + dynamic_order_.push_back(item.asString()); } } } @@ -110,10 +110,9 @@ Mpris::Mpris(const std::string& id, const Json::Value& config) player_ = config_["player"].asString(); } if (config_["ignored-players"].isArray()) { - for (auto it = config_["ignored-players"].begin(); it != config_["ignored-players"].end(); - ++it) { - if (it->isString()) { - ignored_players_.push_back(it->asString()); + for (const auto& item : config_["ignored-players"]) { + if (item.isString()) { + ignored_players_.push_back(item.asString()); } } } @@ -146,8 +145,8 @@ Mpris::Mpris(const std::string& id, const Json::Value& config) throw std::runtime_error(fmt::format("unable to list players: {}", error->message)); } - for (auto p = players; p != NULL; p = p->next) { - auto pn = static_cast(p->data); + for (auto* p = players; p != nullptr; p = p->next) { + auto* pn = static_cast(p->data); if (strcmp(pn->name, player_.c_str()) == 0) { player = playerctl_player_new_from_name(pn, &error); break; @@ -180,17 +179,14 @@ Mpris::Mpris(const std::string& id, const Json::Value& config) } Mpris::~Mpris() { - if (manager != NULL) g_object_unref(manager); - if (player != NULL) g_object_unref(player); + if (manager != nullptr) g_object_unref(manager); + if (player != nullptr) g_object_unref(player); } auto Mpris::getIconFromJson(const Json::Value& icons, const std::string& key) -> std::string { if (icons.isObject()) { - if (icons[key].isString()) { - return icons[key].asString(); - } else if (icons["default"].isString()) { - return icons["default"].asString(); - } + if (icons[key].isString()) return icons[key].asString(); + if (icons["default"].isString()) return icons["default"].asString(); } return ""; } @@ -205,7 +201,7 @@ size_t utf8_truncate(std::string& str, size_t width = std::string::npos) { size_t total_width = 0; - for (gchar *data = str.data(), *end = data + str.size(); data;) { + for (gchar *data = str.data(), *end = data + str.size(); data != nullptr;) { gunichar c = g_utf8_get_char_validated(data, end - data); if (c == -1U || c == -2U) { // invalid unicode, treat string as ascii @@ -269,7 +265,7 @@ auto Mpris::getLengthStr(const PlayerInfo& info, bool truncated) -> std::string auto length = info.length.value(); return (truncated && length.substr(0, 3) == "00:") ? length.substr(3) : length; } - return std::string(); + return {}; } auto Mpris::getPositionStr(const PlayerInfo& info, bool truncated) -> std::string { @@ -277,7 +273,7 @@ auto Mpris::getPositionStr(const PlayerInfo& info, bool truncated) -> std::strin auto position = info.position.value(); return (truncated && position.substr(0, 3) == "00:") ? position.substr(3) : position; } - return std::string(); + return {}; } auto Mpris::getDynamicStr(const PlayerInfo& info, bool truncated, bool html) -> std::string { @@ -319,33 +315,33 @@ auto Mpris::getDynamicStr(const PlayerInfo& info, bool truncated, bool html) -> size_t totalLen = 0; - for (auto it = dynamic_prio_.begin(); it != dynamic_prio_.end(); ++it) { - if (*it == "artist") { + for (const auto& item : dynamic_prio_) { + if (item == "artist") { if (totalLen + artistLen > dynamicLen) { showArtist = false; } else if (showArtist) { totalLen += artistLen; } - } else if (*it == "album") { + } else if (item == "album") { if (totalLen + albumLen > dynamicLen) { showAlbum = false; } else if (showAlbum) { totalLen += albumLen; } - } else if (*it == "title") { + } else if (item == "title") { if (totalLen + titleLen > dynamicLen) { showTitle = false; } else if (showTitle) { totalLen += titleLen; } - } else if (*it == "length") { + } else if (item == "length") { if (totalLen + lengthLen > dynamicLen) { showLength = false; } else if (showLength) { totalLen += lengthLen; posLen = std::max((size_t)2, posLen) - 2; } - } else if (*it == "position") { + } else if (item == "position") { if (totalLen + posLen > dynamicLen) { showPos = false; } else if (showPos) { @@ -406,7 +402,7 @@ auto Mpris::getDynamicStr(const PlayerInfo& info, bool truncated, bool html) -> auto Mpris::onPlayerNameAppeared(PlayerctlPlayerManager* manager, PlayerctlPlayerName* player_name, gpointer data) -> void { - Mpris* mpris = static_cast(data); + auto* mpris = static_cast(data); if (!mpris) return; spdlog::debug("mpris: name-appeared callback: {}", player_name->name); @@ -415,7 +411,7 @@ auto Mpris::onPlayerNameAppeared(PlayerctlPlayerManager* manager, PlayerctlPlaye return; } - mpris->player = playerctl_player_new_from_name(player_name, NULL); + mpris->player = playerctl_player_new_from_name(player_name, nullptr); g_object_connect(mpris->player, "signal::play", G_CALLBACK(onPlayerPlay), mpris, "signal::pause", G_CALLBACK(onPlayerPause), mpris, "signal::stop", G_CALLBACK(onPlayerStop), mpris, "signal::stop", G_CALLBACK(onPlayerStop), mpris, "signal::metadata", @@ -426,7 +422,7 @@ auto Mpris::onPlayerNameAppeared(PlayerctlPlayerManager* manager, PlayerctlPlaye auto Mpris::onPlayerNameVanished(PlayerctlPlayerManager* manager, PlayerctlPlayerName* player_name, gpointer data) -> void { - Mpris* mpris = static_cast(data); + auto* mpris = static_cast(data); if (!mpris) return; spdlog::debug("mpris: player-vanished callback: {}", player_name->name); @@ -439,7 +435,7 @@ auto Mpris::onPlayerNameVanished(PlayerctlPlayerManager* manager, PlayerctlPlaye } auto Mpris::onPlayerPlay(PlayerctlPlayer* player, gpointer data) -> void { - Mpris* mpris = static_cast(data); + auto* mpris = static_cast(data); if (!mpris) return; spdlog::debug("mpris: player-play callback"); @@ -448,7 +444,7 @@ auto Mpris::onPlayerPlay(PlayerctlPlayer* player, gpointer data) -> void { } auto Mpris::onPlayerPause(PlayerctlPlayer* player, gpointer data) -> void { - Mpris* mpris = static_cast(data); + auto* mpris = static_cast(data); if (!mpris) return; spdlog::debug("mpris: player-pause callback"); @@ -457,7 +453,7 @@ auto Mpris::onPlayerPause(PlayerctlPlayer* player, gpointer data) -> void { } auto Mpris::onPlayerStop(PlayerctlPlayer* player, gpointer data) -> void { - Mpris* mpris = static_cast(data); + auto* mpris = static_cast(data); if (!mpris) return; spdlog::debug("mpris: player-stop callback"); @@ -469,7 +465,7 @@ auto Mpris::onPlayerStop(PlayerctlPlayer* player, gpointer data) -> void { } auto Mpris::onPlayerMetadata(PlayerctlPlayer* player, GVariant* metadata, gpointer data) -> void { - Mpris* mpris = static_cast(data); + auto* mpris = static_cast(data); if (!mpris) return; spdlog::debug("mpris: player-metadata callback"); @@ -524,30 +520,30 @@ auto Mpris::getPlayerInfo() -> std::optional { .length = std::nullopt, }; - if (auto artist_ = playerctl_player_get_artist(player, &error)) { + if (auto* artist_ = playerctl_player_get_artist(player, &error)) { spdlog::debug("mpris[{}]: artist = {}", info.name, artist_); info.artist = artist_; g_free(artist_); } if (error) goto errorexit; - if (auto album_ = playerctl_player_get_album(player, &error)) { + if (auto* album_ = playerctl_player_get_album(player, &error)) { spdlog::debug("mpris[{}]: album = {}", info.name, album_); info.album = album_; g_free(album_); } if (error) goto errorexit; - if (auto title_ = playerctl_player_get_title(player, &error)) { + if (auto* title_ = playerctl_player_get_title(player, &error)) { spdlog::debug("mpris[{}]: title = {}", info.name, title_); info.title = title_; g_free(title_); } if (error) goto errorexit; - if (auto length_ = playerctl_player_print_metadata_prop(player, "mpris:length", &error)) { + if (auto* length_ = playerctl_player_print_metadata_prop(player, "mpris:length", &error)) { spdlog::debug("mpris[{}]: mpris:length = {}", info.name, length_); - std::chrono::microseconds len = std::chrono::microseconds(std::strtol(length_, nullptr, 10)); + auto len = std::chrono::microseconds(std::strtol(length_, nullptr, 10)); auto len_h = std::chrono::duration_cast(len); auto len_m = std::chrono::duration_cast(len - len_h); auto len_s = std::chrono::duration_cast(len - len_h - len_m); @@ -564,7 +560,7 @@ auto Mpris::getPlayerInfo() -> std::optional { error = nullptr; } else { spdlog::debug("mpris[{}]: position = {}", info.name, position_); - std::chrono::microseconds len = std::chrono::microseconds(position_); + auto len = std::chrono::microseconds(position_); auto len_h = std::chrono::duration_cast(len); auto len_m = std::chrono::duration_cast(len - len_h); auto len_s = std::chrono::duration_cast(len - len_h - len_m);