RichPresence: Only initialize discord when enabled
This commit is contained in:
@@ -46,9 +46,7 @@ RichPresence::RichPresence(const SharedPtr<Player> player,
|
|||||||
: QObject(parent),
|
: QObject(parent),
|
||||||
player_(player),
|
player_(player),
|
||||||
playlist_manager_(playlist_manager),
|
playlist_manager_(playlist_manager),
|
||||||
enabled_(false) {
|
initialized_(false) {
|
||||||
|
|
||||||
Discord_Initialize(kDiscordApplicationId, nullptr, 1);
|
|
||||||
|
|
||||||
QObject::connect(&*player_->engine(), &EngineBase::StateChanged, this, &RichPresence::EngineStateChanged);
|
QObject::connect(&*player_->engine(), &EngineBase::StateChanged, this, &RichPresence::EngineStateChanged);
|
||||||
QObject::connect(&*playlist_manager_, &PlaylistManager::CurrentSongChanged, this, &RichPresence::CurrentSongChanged);
|
QObject::connect(&*playlist_manager_, &PlaylistManager::CurrentSongChanged, this, &RichPresence::CurrentSongChanged);
|
||||||
@@ -59,7 +57,11 @@ RichPresence::RichPresence(const SharedPtr<Player> player,
|
|||||||
}
|
}
|
||||||
|
|
||||||
RichPresence::~RichPresence() {
|
RichPresence::~RichPresence() {
|
||||||
Discord_Shutdown();
|
|
||||||
|
if (initialized_) {
|
||||||
|
Discord_Shutdown();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void RichPresence::ReloadSettings() {
|
void RichPresence::ReloadSettings() {
|
||||||
@@ -69,16 +71,22 @@ void RichPresence::ReloadSettings() {
|
|||||||
const bool enabled = s.value(DiscordRPCSettings::kEnabled, false).toBool();
|
const bool enabled = s.value(DiscordRPCSettings::kEnabled, false).toBool();
|
||||||
s.endGroup();
|
s.endGroup();
|
||||||
|
|
||||||
if (enabled_ && !enabled) {
|
if (enabled && !initialized_) {
|
||||||
Discord_ClearPresence();
|
Discord_Initialize(kDiscordApplicationId, nullptr, 1);
|
||||||
|
initialized_ = true;
|
||||||
|
}
|
||||||
|
else if (!enabled && initialized_) {
|
||||||
|
Discord_ClearPresence();
|
||||||
|
Discord_Shutdown();
|
||||||
|
initialized_ = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
enabled_ = enabled;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void RichPresence::EngineStateChanged(const EngineBase::State state) {
|
void RichPresence::EngineStateChanged(const EngineBase::State state) {
|
||||||
|
|
||||||
|
if (!initialized_) return;
|
||||||
|
|
||||||
if (state == EngineBase::State::Playing) {
|
if (state == EngineBase::State::Playing) {
|
||||||
SetTimestamp(player_->engine()->position_nanosec() / kNsecPerSec);
|
SetTimestamp(player_->engine()->position_nanosec() / kNsecPerSec);
|
||||||
SendPresenceUpdate();
|
SendPresenceUpdate();
|
||||||
@@ -91,6 +99,8 @@ void RichPresence::EngineStateChanged(const EngineBase::State state) {
|
|||||||
|
|
||||||
void RichPresence::CurrentSongChanged(const Song &song) {
|
void RichPresence::CurrentSongChanged(const Song &song) {
|
||||||
|
|
||||||
|
if (!initialized_) return;
|
||||||
|
|
||||||
SetTimestamp(0LL);
|
SetTimestamp(0LL);
|
||||||
activity_.length_secs = song.length_nanosec() / kNsecPerSec;
|
activity_.length_secs = song.length_nanosec() / kNsecPerSec;
|
||||||
activity_.title = song.title();
|
activity_.title = song.title();
|
||||||
@@ -103,9 +113,7 @@ void RichPresence::CurrentSongChanged(const Song &song) {
|
|||||||
|
|
||||||
void RichPresence::SendPresenceUpdate() {
|
void RichPresence::SendPresenceUpdate() {
|
||||||
|
|
||||||
if (!enabled_) {
|
if (!initialized_) return;
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
::DiscordRichPresence presence_data{};
|
::DiscordRichPresence presence_data{};
|
||||||
memset(&presence_data, 0, sizeof(presence_data));
|
memset(&presence_data, 0, sizeof(presence_data));
|
||||||
@@ -141,13 +149,19 @@ void RichPresence::SendPresenceUpdate() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void RichPresence::SetTimestamp(const qint64 seconds) {
|
void RichPresence::SetTimestamp(const qint64 seconds) {
|
||||||
|
|
||||||
activity_.start_timestamp = QDateTime::currentSecsSinceEpoch();
|
activity_.start_timestamp = QDateTime::currentSecsSinceEpoch();
|
||||||
activity_.seek_secs = seconds;
|
activity_.seek_secs = seconds;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void RichPresence::Seeked(const qint64 seek_microseconds) {
|
void RichPresence::Seeked(const qint64 seek_microseconds) {
|
||||||
|
|
||||||
|
if (!initialized_) return;
|
||||||
|
|
||||||
SetTimestamp(seek_microseconds / 1000LL);
|
SetTimestamp(seek_microseconds / 1000LL);
|
||||||
SendPresenceUpdate();
|
SendPresenceUpdate();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace discord
|
} // namespace discord
|
||||||
|
|||||||
@@ -69,7 +69,7 @@ class RichPresence : public QObject {
|
|||||||
qint64 seek_secs;
|
qint64 seek_secs;
|
||||||
};
|
};
|
||||||
Activity activity_;
|
Activity activity_;
|
||||||
bool enabled_;
|
bool initialized_;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace discord
|
} // namespace discord
|
||||||
|
|||||||
Reference in New Issue
Block a user