Skip to content

Commit

Permalink
WMC: Do not save covers if the current source isn't WMC
Browse files Browse the repository at this point in the history
  • Loading branch information
univrsal committed Jun 13, 2024
1 parent 431248d commit 4b7f688
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/query/mpris_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ DBusHandlerResult mpris_source::handle_dbus(DBusMessage* message)

static inline QString correct_art_url(const char* url)
{
return QUrl::toPercentEncoding(utf8_to_qt(url)).replace("%2F", "/").replace("file%3A", "file:").replace("%3A", ":"); // idk why it encodes slashes
return QUrl::toPercentEncoding(utf8_to_qt(url)).replace("%2F", "/").replace("file%3A", "file:").replace("%3A", ":"); // idk why it encodes slashes
}

void mpris_source::parse_metadata(DBusMessageIter* iter, QString const& player, int level)
Expand Down
40 changes: 27 additions & 13 deletions src/query/wmc_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#include "../util/constants.hpp"
#include "../util/utility.hpp"
#include <QFile>
#include <QImage>

/**
* Large chunks of this source were taken from
Expand Down Expand Up @@ -84,6 +83,23 @@ void wmc_source::update_players()
}
}

void wmc_source::save_cover(QImage& image)
{
auto tmp = config::cover_path + ".tmp";

if (image.save(tmp, "png")) {
QFile current(config::cover_path);
current.remove();
if (!QFile::rename(tmp, config::cover_path)) {
util::reset_cover();
berr("[WMC] Failed to move cover from temporary file to %s", qt_to_utf8(config::cover_path));
}

} else {
berr("[WMC] Failed to save cover to %s", qt_to_utf8(config::cover_path));
}
}

bool wmc_source::execute_capability(capability c)
{
/* We don't wait for the result of the async calls because
Expand Down Expand Up @@ -198,18 +214,14 @@ void wmc_source::handle_media_property_change(GlobalSystemMediaTransportControls
data = (uint8_t*)pixel_data_detached.data();

QImage image(data, width, height, QImage::Format_RGBA8888);
auto tmp = config::cover_path + ".tmp";

if (image.save(tmp, "png")) {
QFile current(config::cover_path);
current.remove();
if (!QFile::rename(tmp, config::cover_path)) {
util::reset_cover();
berr("[WMC] Failed to move cover from temporary file to %s", qt_to_utf8(config::cover_path));
}

} else {
berr("[WMC] Failed to save cover to %s", qt_to_utf8(config::cover_path));
auto current_source = music_source::selected_source();
m_covers[id] = image;

/* We receive cover updates regardless of whether tuna is
* configured to monitor WMC so if the current source isn't WMC, we
* just save the cover for when the user switches to WMC*/
if (curren_source.get() == this) {
save_cover(image);
}
}
}
Expand Down Expand Up @@ -259,4 +271,6 @@ void wmc_source::refresh()
std::lock_guard<std::mutex> lock(m_internal_mutex);
if (m_info.find(m_selected_player) != m_info.end())
m_current = m_info[m_selected_player];
if (m_covers.find(m_selected_player) != m_covers.end())
save_cover(m_covers[m_selected_player]);
}
8 changes: 6 additions & 2 deletions src/query/wmc_source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#pragma once
#include "music_source.hpp"
#include <QImage>
#include <string>
#include <vector>

Expand All @@ -44,7 +45,10 @@ class wmc_source : public music_source {
std::string m_selected_player {};
std::vector<std::string> m_registered_players {};
std::mutex m_internal_mutex;
std::map<std::string, song> m_info;
std::map<std::string, song> m_info {};
std::map<std::string, QImage> m_covers {};

void save_cover(QImage& image);

public:
wmc_source();
Expand All @@ -66,4 +70,4 @@ class wmc_source : public music_source {
// bool enabled() const override;
// void handle_cover() override;
// void reset_info() override;
};
};

0 comments on commit 4b7f688

Please sign in to comment.