mpris: some clang-tidy fix

This commit is contained in:
giskard 2024-05-14 15:00:05 +08:00
parent e8d91eb14b
commit 7721dcdae8
1 changed files with 34 additions and 38 deletions

View File

@ -96,9 +96,9 @@ Mpris::Mpris(const std::string& id, const Json::Value& config)
} }
if (config_["dynamic-order"].isArray()) { if (config_["dynamic-order"].isArray()) {
dynamic_order_.clear(); dynamic_order_.clear();
for (auto it = config_["dynamic-order"].begin(); it != config_["dynamic-order"].end(); ++it) { for (const auto& item : config_["dynamic-order"]) {
if (it->isString()) { if (item.isString()) {
dynamic_order_.push_back(it->asString()); dynamic_order_.push_back(item.asString());
} }
} }
} }
@ -110,10 +110,9 @@ Mpris::Mpris(const std::string& id, const Json::Value& config)
player_ = config_["player"].asString(); player_ = config_["player"].asString();
} }
if (config_["ignored-players"].isArray()) { if (config_["ignored-players"].isArray()) {
for (auto it = config_["ignored-players"].begin(); it != config_["ignored-players"].end(); for (const auto& item : config_["ignored-players"]) {
++it) { if (item.isString()) {
if (it->isString()) { ignored_players_.push_back(item.asString());
ignored_players_.push_back(it->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)); throw std::runtime_error(fmt::format("unable to list players: {}", error->message));
} }
for (auto p = players; p != NULL; p = p->next) { for (auto* p = players; p != nullptr; p = p->next) {
auto pn = static_cast<PlayerctlPlayerName*>(p->data); auto* pn = static_cast<PlayerctlPlayerName*>(p->data);
if (strcmp(pn->name, player_.c_str()) == 0) { if (strcmp(pn->name, player_.c_str()) == 0) {
player = playerctl_player_new_from_name(pn, &error); player = playerctl_player_new_from_name(pn, &error);
break; break;
@ -180,17 +179,14 @@ Mpris::Mpris(const std::string& id, const Json::Value& config)
} }
Mpris::~Mpris() { Mpris::~Mpris() {
if (manager != NULL) g_object_unref(manager); if (manager != nullptr) g_object_unref(manager);
if (player != NULL) g_object_unref(player); if (player != nullptr) g_object_unref(player);
} }
auto Mpris::getIconFromJson(const Json::Value& icons, const std::string& key) -> std::string { auto Mpris::getIconFromJson(const Json::Value& icons, const std::string& key) -> std::string {
if (icons.isObject()) { if (icons.isObject()) {
if (icons[key].isString()) { if (icons[key].isString()) return icons[key].asString();
return icons[key].asString(); if (icons["default"].isString()) return icons["default"].asString();
} else if (icons["default"].isString()) {
return icons["default"].asString();
}
} }
return ""; return "";
} }
@ -205,7 +201,7 @@ size_t utf8_truncate(std::string& str, size_t width = std::string::npos) {
size_t total_width = 0; 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); gunichar c = g_utf8_get_char_validated(data, end - data);
if (c == -1U || c == -2U) { if (c == -1U || c == -2U) {
// invalid unicode, treat string as ascii // 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(); auto length = info.length.value();
return (truncated && length.substr(0, 3) == "00:") ? length.substr(3) : length; 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 { 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(); auto position = info.position.value();
return (truncated && position.substr(0, 3) == "00:") ? position.substr(3) : position; 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 { 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; size_t totalLen = 0;
for (auto it = dynamic_prio_.begin(); it != dynamic_prio_.end(); ++it) { for (const auto& item : dynamic_prio_) {
if (*it == "artist") { if (item == "artist") {
if (totalLen + artistLen > dynamicLen) { if (totalLen + artistLen > dynamicLen) {
showArtist = false; showArtist = false;
} else if (showArtist) { } else if (showArtist) {
totalLen += artistLen; totalLen += artistLen;
} }
} else if (*it == "album") { } else if (item == "album") {
if (totalLen + albumLen > dynamicLen) { if (totalLen + albumLen > dynamicLen) {
showAlbum = false; showAlbum = false;
} else if (showAlbum) { } else if (showAlbum) {
totalLen += albumLen; totalLen += albumLen;
} }
} else if (*it == "title") { } else if (item == "title") {
if (totalLen + titleLen > dynamicLen) { if (totalLen + titleLen > dynamicLen) {
showTitle = false; showTitle = false;
} else if (showTitle) { } else if (showTitle) {
totalLen += titleLen; totalLen += titleLen;
} }
} else if (*it == "length") { } else if (item == "length") {
if (totalLen + lengthLen > dynamicLen) { if (totalLen + lengthLen > dynamicLen) {
showLength = false; showLength = false;
} else if (showLength) { } else if (showLength) {
totalLen += lengthLen; totalLen += lengthLen;
posLen = std::max((size_t)2, posLen) - 2; posLen = std::max((size_t)2, posLen) - 2;
} }
} else if (*it == "position") { } else if (item == "position") {
if (totalLen + posLen > dynamicLen) { if (totalLen + posLen > dynamicLen) {
showPos = false; showPos = false;
} else if (showPos) { } 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, auto Mpris::onPlayerNameAppeared(PlayerctlPlayerManager* manager, PlayerctlPlayerName* player_name,
gpointer data) -> void { gpointer data) -> void {
Mpris* mpris = static_cast<Mpris*>(data); auto* mpris = static_cast<Mpris*>(data);
if (!mpris) return; if (!mpris) return;
spdlog::debug("mpris: name-appeared callback: {}", player_name->name); spdlog::debug("mpris: name-appeared callback: {}", player_name->name);
@ -415,7 +411,7 @@ auto Mpris::onPlayerNameAppeared(PlayerctlPlayerManager* manager, PlayerctlPlaye
return; 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_object_connect(mpris->player, "signal::play", G_CALLBACK(onPlayerPlay), mpris, "signal::pause",
G_CALLBACK(onPlayerPause), mpris, "signal::stop", G_CALLBACK(onPlayerStop), G_CALLBACK(onPlayerPause), mpris, "signal::stop", G_CALLBACK(onPlayerStop),
mpris, "signal::stop", G_CALLBACK(onPlayerStop), mpris, "signal::metadata", 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, auto Mpris::onPlayerNameVanished(PlayerctlPlayerManager* manager, PlayerctlPlayerName* player_name,
gpointer data) -> void { gpointer data) -> void {
Mpris* mpris = static_cast<Mpris*>(data); auto* mpris = static_cast<Mpris*>(data);
if (!mpris) return; if (!mpris) return;
spdlog::debug("mpris: player-vanished callback: {}", player_name->name); 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 { auto Mpris::onPlayerPlay(PlayerctlPlayer* player, gpointer data) -> void {
Mpris* mpris = static_cast<Mpris*>(data); auto* mpris = static_cast<Mpris*>(data);
if (!mpris) return; if (!mpris) return;
spdlog::debug("mpris: player-play callback"); 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 { auto Mpris::onPlayerPause(PlayerctlPlayer* player, gpointer data) -> void {
Mpris* mpris = static_cast<Mpris*>(data); auto* mpris = static_cast<Mpris*>(data);
if (!mpris) return; if (!mpris) return;
spdlog::debug("mpris: player-pause callback"); 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 { auto Mpris::onPlayerStop(PlayerctlPlayer* player, gpointer data) -> void {
Mpris* mpris = static_cast<Mpris*>(data); auto* mpris = static_cast<Mpris*>(data);
if (!mpris) return; if (!mpris) return;
spdlog::debug("mpris: player-stop callback"); 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 { auto Mpris::onPlayerMetadata(PlayerctlPlayer* player, GVariant* metadata, gpointer data) -> void {
Mpris* mpris = static_cast<Mpris*>(data); auto* mpris = static_cast<Mpris*>(data);
if (!mpris) return; if (!mpris) return;
spdlog::debug("mpris: player-metadata callback"); spdlog::debug("mpris: player-metadata callback");
@ -524,30 +520,30 @@ auto Mpris::getPlayerInfo() -> std::optional<PlayerInfo> {
.length = std::nullopt, .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_); spdlog::debug("mpris[{}]: artist = {}", info.name, artist_);
info.artist = artist_; info.artist = artist_;
g_free(artist_); g_free(artist_);
} }
if (error) goto errorexit; 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_); spdlog::debug("mpris[{}]: album = {}", info.name, album_);
info.album = album_; info.album = album_;
g_free(album_); g_free(album_);
} }
if (error) goto errorexit; 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_); spdlog::debug("mpris[{}]: title = {}", info.name, title_);
info.title = title_; info.title = title_;
g_free(title_); g_free(title_);
} }
if (error) goto errorexit; 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_); 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<std::chrono::hours>(len); auto len_h = std::chrono::duration_cast<std::chrono::hours>(len);
auto len_m = std::chrono::duration_cast<std::chrono::minutes>(len - len_h); auto len_m = std::chrono::duration_cast<std::chrono::minutes>(len - len_h);
auto len_s = std::chrono::duration_cast<std::chrono::seconds>(len - len_h - len_m); auto len_s = std::chrono::duration_cast<std::chrono::seconds>(len - len_h - len_m);
@ -564,7 +560,7 @@ auto Mpris::getPlayerInfo() -> std::optional<PlayerInfo> {
error = nullptr; error = nullptr;
} else { } else {
spdlog::debug("mpris[{}]: position = {}", info.name, position_); 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<std::chrono::hours>(len); auto len_h = std::chrono::duration_cast<std::chrono::hours>(len);
auto len_m = std::chrono::duration_cast<std::chrono::minutes>(len - len_h); auto len_m = std::chrono::duration_cast<std::chrono::minutes>(len - len_h);
auto len_s = std::chrono::duration_cast<std::chrono::seconds>(len - len_h - len_m); auto len_s = std::chrono::duration_cast<std::chrono::seconds>(len - len_h - len_m);