Refactoring
This commit is contained in:
@@ -31,30 +31,26 @@
|
||||
#include "osdbase.h"
|
||||
#include "osdpretty.h"
|
||||
|
||||
#include "core/shared_ptr.h"
|
||||
#include "core/application.h"
|
||||
#include "includes/shared_ptr.h"
|
||||
#include "core/logging.h"
|
||||
#include "core/settings.h"
|
||||
#ifdef Q_OS_MACOS
|
||||
# include "core/macsystemtrayicon.h"
|
||||
# include "systemtrayicon/macsystemtrayicon.h"
|
||||
#else
|
||||
# include "core/qtsystemtrayicon.h"
|
||||
# include "systemtrayicon/qtsystemtrayicon.h"
|
||||
#endif
|
||||
#include "utilities/strutils.h"
|
||||
#include "covermanager/currentalbumcoverloader.h"
|
||||
#include "constants/notificationssettings.h"
|
||||
|
||||
using namespace Qt::Literals::StringLiterals;
|
||||
|
||||
const char *OSDBase::kSettingsGroup = "OSD";
|
||||
|
||||
OSDBase::OSDBase(SharedPtr<SystemTrayIcon> tray_icon, Application *app, QObject *parent)
|
||||
OSDBase::OSDBase(const SharedPtr<SystemTrayIcon> tray_icon, QObject *parent)
|
||||
: QObject(parent),
|
||||
app_(app),
|
||||
tray_icon_(tray_icon),
|
||||
pretty_popup_(new OSDPretty(OSDPretty::Mode::Popup)),
|
||||
app_name_(QCoreApplication::applicationName()),
|
||||
timeout_msec_(5000),
|
||||
behaviour_(Behaviour::Native),
|
||||
type_(OSDSettings::Type::Native),
|
||||
show_on_volume_change_(false),
|
||||
show_art_(true),
|
||||
show_on_play_mode_change_(true),
|
||||
@@ -65,8 +61,6 @@ OSDBase::OSDBase(SharedPtr<SystemTrayIcon> tray_icon, Application *app, QObject
|
||||
ignore_next_stopped_(false),
|
||||
playing_(false) {
|
||||
|
||||
QObject::connect(&*app_->current_albumcover_loader(), &CurrentAlbumCoverLoader::ThumbnailLoaded, this, &OSDBase::AlbumCoverLoaded);
|
||||
|
||||
app_name_[0] = app_name_[0].toUpper();
|
||||
|
||||
}
|
||||
@@ -78,9 +72,9 @@ OSDBase::~OSDBase() {
|
||||
void OSDBase::ReloadSettings() {
|
||||
|
||||
Settings s;
|
||||
s.beginGroup(kSettingsGroup);
|
||||
behaviour_ = static_cast<OSDBase::Behaviour>(s.value("Behaviour", static_cast<int>(Behaviour::Native)).toInt());
|
||||
timeout_msec_ = s.value("Timeout", 5000).toInt();
|
||||
s.beginGroup(OSDSettings::kSettingsGroup);
|
||||
type_ = static_cast<OSDSettings::Type>(s.value(OSDSettings::kType, static_cast<int>(OSDSettings::Type::Native)).toInt());
|
||||
timeout_msec_ = s.value(OSDSettings::kTimeout, 5000).toInt();
|
||||
show_on_volume_change_ = s.value("ShowOnVolumeChange", false).toBool();
|
||||
show_art_ = s.value("ShowArt", true).toBool();
|
||||
show_on_play_mode_change_ = s.value("ShowOnPlayModeChange", true).toBool();
|
||||
@@ -92,15 +86,15 @@ void OSDBase::ReloadSettings() {
|
||||
s.endGroup();
|
||||
|
||||
#ifdef Q_OS_WIN32
|
||||
if (!SupportsNativeNotifications() && !SupportsTrayPopups() && behaviour_ == Behaviour::Native) {
|
||||
if (!SupportsNativeNotifications() && !SupportsTrayPopups() && type_ == OSDSettings::Type::Native) {
|
||||
#else
|
||||
if (!SupportsNativeNotifications() && behaviour_ == Behaviour::Native) {
|
||||
if (!SupportsNativeNotifications() && type_ == OSDSettings::Type::Native) {
|
||||
#endif
|
||||
behaviour_ = Behaviour::Pretty;
|
||||
type_ = OSDSettings::Type::Pretty;
|
||||
}
|
||||
|
||||
if (!SupportsTrayPopups() && behaviour_ == Behaviour::TrayPopup) {
|
||||
behaviour_ = Behaviour::Disabled;
|
||||
if (!SupportsTrayPopups() && type_ == OSDSettings::Type::TrayPopup) {
|
||||
type_ = OSDSettings::Type::Disabled;
|
||||
}
|
||||
|
||||
ReloadPrettyOSDSettings();
|
||||
@@ -160,12 +154,12 @@ void OSDBase::ShowPlaying(const Song &song, const QUrl &cover_url, const QImage
|
||||
if (song.track() > 0) {
|
||||
message_parts << tr("track %1").arg(song.track());
|
||||
}
|
||||
if (behaviour_ == Behaviour::Pretty) {
|
||||
if (type_ == OSDSettings::Type::Pretty) {
|
||||
summary = summary.toHtmlEscaped();
|
||||
html_escaped = true;
|
||||
}
|
||||
#if defined(HAVE_DBUS) && !defined(Q_OS_MACOS)
|
||||
else if (behaviour_ == Behaviour::Native) {
|
||||
else if (type_ == OSDSettings::Type::Native) {
|
||||
html_escaped = true;
|
||||
}
|
||||
#endif
|
||||
@@ -206,7 +200,7 @@ void OSDBase::Paused() {
|
||||
summary.prepend(" - "_L1);
|
||||
summary.prepend(last_song_.artist());
|
||||
}
|
||||
if (behaviour_ == Behaviour::Pretty) {
|
||||
if (type_ == OSDSettings::Type::Pretty) {
|
||||
summary = summary.toHtmlEscaped();
|
||||
}
|
||||
}
|
||||
@@ -251,7 +245,7 @@ void OSDBase::Stopped() {
|
||||
summary.prepend(" - "_L1);
|
||||
summary.prepend(last_song_.artist());
|
||||
}
|
||||
if (behaviour_ == Behaviour::Pretty) {
|
||||
if (type_ == OSDSettings::Type::Pretty) {
|
||||
summary = summary.toHtmlEscaped();
|
||||
}
|
||||
}
|
||||
@@ -287,11 +281,11 @@ void OSDBase::VolumeChanged(const uint value) {
|
||||
if (!show_on_volume_change_) return;
|
||||
|
||||
QString message = tr("Volume %1%").arg(value);
|
||||
if (behaviour_ == Behaviour::Pretty) {
|
||||
if (type_ == OSDSettings::Type::Pretty) {
|
||||
message = message.toHtmlEscaped();
|
||||
}
|
||||
#if defined(HAVE_DBUS) && !defined(Q_OS_MACOS)
|
||||
else if (behaviour_ == Behaviour::Native) {
|
||||
else if (type_ == OSDSettings::Type::Native) {
|
||||
message = message.toHtmlEscaped();
|
||||
}
|
||||
#endif
|
||||
@@ -306,8 +300,8 @@ void OSDBase::ShowMessage(const QString &summary, const QString &message, const
|
||||
pretty_popup_->ShowMessage(summary, message, image);
|
||||
}
|
||||
else {
|
||||
switch (behaviour_) {
|
||||
case Behaviour::Native:
|
||||
switch (type_) {
|
||||
case OSDSettings::Type::Native:
|
||||
#ifdef Q_OS_WIN32
|
||||
Q_UNUSED(icon)
|
||||
[[fallthrough]];
|
||||
@@ -320,18 +314,18 @@ void OSDBase::ShowMessage(const QString &summary, const QString &message, const
|
||||
}
|
||||
break;
|
||||
#endif
|
||||
case Behaviour::TrayPopup:
|
||||
case OSDSettings::Type::TrayPopup:
|
||||
#ifdef Q_OS_MACOS
|
||||
[[fallthrough]];
|
||||
#else
|
||||
if (tray_icon_) tray_icon_->ShowPopup(summary, message, timeout_msec_);
|
||||
break;
|
||||
#endif
|
||||
case Behaviour::Disabled:
|
||||
case OSDSettings::Type::Disabled:
|
||||
if (!force_show_next_) break;
|
||||
force_show_next_ = false;
|
||||
[[fallthrough]];
|
||||
case Behaviour::Pretty:
|
||||
case OSDSettings::Type::Pretty:
|
||||
pretty_popup_->ShowMessage(summary, message, image);
|
||||
break;
|
||||
|
||||
@@ -384,8 +378,8 @@ QString OSDBase::ReplaceMessage(const MessageType type, const QString &message,
|
||||
QString newline = ""_L1;
|
||||
|
||||
// We need different strings depending on notification type
|
||||
switch (behaviour_) {
|
||||
case Behaviour::Native:
|
||||
switch (type_) {
|
||||
case OSDSettings::Type::Native:
|
||||
#if defined(Q_OS_MACOS)
|
||||
html_escaped = false;
|
||||
newline = QLatin1String("\n");
|
||||
@@ -411,13 +405,13 @@ QString OSDBase::ReplaceMessage(const MessageType type, const QString &message,
|
||||
qLog(Debug) << "Native notifications are not supported on this OS.";
|
||||
break;
|
||||
#endif
|
||||
case Behaviour::TrayPopup:
|
||||
case OSDSettings::Type::TrayPopup:
|
||||
qLog(Debug) << "New line not supported by this notification type.";
|
||||
html_escaped = false;
|
||||
newline = ""_L1;
|
||||
break;
|
||||
case Behaviour::Disabled: // When notifications are disabled, we force the PrettyOSD
|
||||
case Behaviour::Pretty:
|
||||
case OSDSettings::Type::Disabled: // When notifications are disabled, we force the PrettyOSD
|
||||
case OSDSettings::Type::Pretty:
|
||||
html_escaped = true;
|
||||
newline = "<br />"_L1;
|
||||
break;
|
||||
@@ -427,9 +421,9 @@ QString OSDBase::ReplaceMessage(const MessageType type, const QString &message,
|
||||
|
||||
}
|
||||
|
||||
void OSDBase::ShowPreview(const Behaviour type, const QString &line1, const QString &line2, const Song &song) {
|
||||
void OSDBase::ShowPreview(const OSDSettings::Type type, const QString &line1, const QString &line2, const Song &song) {
|
||||
|
||||
behaviour_ = type;
|
||||
type_ = type;
|
||||
custom_text1_ = line1;
|
||||
custom_text2_ = line2;
|
||||
if (!use_custom_text_) use_custom_text_ = true;
|
||||
|
||||
@@ -31,11 +31,11 @@
|
||||
#include <QDateTime>
|
||||
#include <QImage>
|
||||
|
||||
#include "core/shared_ptr.h"
|
||||
#include "includes/shared_ptr.h"
|
||||
#include "core/song.h"
|
||||
#include "playlist/playlistsequence.h"
|
||||
#include "constants/notificationssettings.h"
|
||||
|
||||
class Application;
|
||||
class OSDPretty;
|
||||
class SystemTrayIcon;
|
||||
|
||||
@@ -43,18 +43,9 @@ class OSDBase : public QObject {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit OSDBase(SharedPtr<SystemTrayIcon> tray_icon, Application *app, QObject *parent = nullptr);
|
||||
explicit OSDBase(const SharedPtr<SystemTrayIcon> tray_icon, QObject *parent = nullptr);
|
||||
~OSDBase() override;
|
||||
|
||||
static const char *kSettingsGroup;
|
||||
|
||||
enum class Behaviour {
|
||||
Disabled = 0,
|
||||
Native,
|
||||
TrayPopup,
|
||||
Pretty
|
||||
};
|
||||
|
||||
int timeout_msec() const { return timeout_msec_; }
|
||||
void ReloadPrettyOSDSettings();
|
||||
void SetPrettyOSDToggleMode(bool toggle);
|
||||
@@ -79,7 +70,9 @@ class OSDBase : public QObject {
|
||||
|
||||
void ReshowCurrentSong();
|
||||
|
||||
void ShowPreview(const OSDBase::Behaviour type, const QString &line1, const QString &line2, const Song &song);
|
||||
void ShowPreview(const OSDSettings::Type type, const QString &line1, const QString &line2, const Song &song);
|
||||
|
||||
void AlbumCoverLoaded(const Song &song, const QUrl &cover_url, const QImage &image);
|
||||
|
||||
private:
|
||||
enum class MessageType {
|
||||
@@ -91,17 +84,13 @@ class OSDBase : public QObject {
|
||||
QString ReplaceMessage(const MessageType type, const QString &message, const Song &song);
|
||||
virtual void ShowMessageNative(const QString &summary, const QString &message, const QString &icon = QString(), const QImage &image = QImage());
|
||||
|
||||
private Q_SLOTS:
|
||||
void AlbumCoverLoaded(const Song &song, const QUrl &cover_url, const QImage &image);
|
||||
|
||||
private:
|
||||
Application *app_;
|
||||
SharedPtr<SystemTrayIcon> tray_icon_;
|
||||
const SharedPtr<SystemTrayIcon> tray_icon_;
|
||||
OSDPretty *pretty_popup_;
|
||||
|
||||
QString app_name_;
|
||||
int timeout_msec_;
|
||||
Behaviour behaviour_;
|
||||
OSDSettings::Type type_;
|
||||
bool show_on_volume_change_;
|
||||
bool show_art_;
|
||||
bool show_on_play_mode_change_;
|
||||
|
||||
@@ -40,8 +40,8 @@
|
||||
#include <QDBusPendingReply>
|
||||
#include <QDBusPendingCallWatcher>
|
||||
|
||||
#include "includes/scoped_ptr.h"
|
||||
#include "core/logging.h"
|
||||
#include "core/scoped_ptr.h"
|
||||
#include "osddbus.h"
|
||||
|
||||
#include "notification.h"
|
||||
@@ -105,8 +105,8 @@ const QDBusArgument &operator>>(const QDBusArgument &arg, QImage &image) {
|
||||
|
||||
}
|
||||
|
||||
OSDDBus::OSDDBus(SharedPtr<SystemTrayIcon> tray_icon, Application *app, QObject *parent)
|
||||
: OSDBase(tray_icon, app, parent),
|
||||
OSDDBus::OSDDBus(const SharedPtr<SystemTrayIcon> tray_icon, QObject *parent)
|
||||
: OSDBase(tray_icon, parent),
|
||||
version_(1, 1),
|
||||
notification_id_(0) {
|
||||
|
||||
|
||||
@@ -33,20 +33,19 @@
|
||||
#include <QDBusArgument>
|
||||
#include <QVersionNumber>
|
||||
|
||||
#include "core/scoped_ptr.h"
|
||||
#include "includes/scoped_ptr.h"
|
||||
#include "osdbase.h"
|
||||
|
||||
class OrgFreedesktopNotificationsInterface;
|
||||
class QDBusPendingCallWatcher;
|
||||
|
||||
class Application;
|
||||
class SystemTrayIcon;
|
||||
|
||||
class OSDDBus : public OSDBase {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit OSDDBus(SharedPtr<SystemTrayIcon> tray_icon, Application *app, QObject *parent = nullptr);
|
||||
explicit OSDDBus(const SharedPtr<SystemTrayIcon> tray_icon, QObject *parent = nullptr);
|
||||
~OSDDBus() override;
|
||||
|
||||
static const char *kSettingsGroup;
|
||||
|
||||
@@ -37,7 +37,7 @@ class OSDMac : public OSDBase {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit OSDMac(SharedPtr<SystemTrayIcon> tray_icon, Application *app, QObject *parent = nullptr);
|
||||
explicit OSDMac(const SharedPtr<SystemTrayIcon> tray_icon, QObject *parent = nullptr);
|
||||
~OSDMac() override;
|
||||
|
||||
bool SupportsNativeNotifications() override;
|
||||
|
||||
@@ -27,7 +27,7 @@
|
||||
#include <QByteArray>
|
||||
#include <QFile>
|
||||
|
||||
#include "core/scoped_nsobject.h"
|
||||
#include "includes/scoped_nsobject.h"
|
||||
|
||||
#include "osdmac.h"
|
||||
|
||||
@@ -53,7 +53,7 @@ void SendNotificationCenterMessage(NSString *title, NSString *subtitle) {
|
||||
|
||||
} // namespace
|
||||
|
||||
OSDMac::OSDMac(SharedPtr<SystemTrayIcon> tray_icon, Application *app, QObject *parent) : OSDBase(tray_icon, app, parent) {}
|
||||
OSDMac::OSDMac(const SharedPtr<SystemTrayIcon> tray_icon, QObject *parent) : OSDBase(tray_icon, parent) {}
|
||||
|
||||
OSDMac::~OSDMac() = default;
|
||||
|
||||
|
||||
@@ -56,6 +56,7 @@
|
||||
#endif
|
||||
|
||||
#include "core/settings.h"
|
||||
#include "constants/notificationssettings.h"
|
||||
|
||||
#include "osdpretty.h"
|
||||
#include "ui_osdpretty.h"
|
||||
@@ -71,22 +72,20 @@
|
||||
using namespace std::chrono_literals;
|
||||
using namespace Qt::Literals::StringLiterals;
|
||||
|
||||
const char *OSDPretty::kSettingsGroup = "OSDPretty";
|
||||
namespace {
|
||||
|
||||
const int OSDPretty::kDropShadowSize = 13;
|
||||
const int OSDPretty::kBorderRadius = 10;
|
||||
const int OSDPretty::kMaxIconSize = 100;
|
||||
constexpr int kDropShadowSize = 13;
|
||||
constexpr int kBorderRadius = 10;
|
||||
constexpr int kMaxIconSize = 100;
|
||||
constexpr int kSnapProximity = 20;
|
||||
|
||||
const int OSDPretty::kSnapProximity = 20;
|
||||
} // namespace
|
||||
|
||||
const QRgb OSDPretty::kPresetBlue = qRgb(102, 150, 227);
|
||||
const QRgb OSDPretty::kPresetRed = qRgb(202, 22, 16);
|
||||
|
||||
OSDPretty::OSDPretty(Mode mode, QWidget *parent)
|
||||
OSDPretty::OSDPretty(const Mode mode, QWidget *parent)
|
||||
: QWidget(parent),
|
||||
ui_(new Ui_OSDPretty),
|
||||
mode_(mode),
|
||||
background_color_(kPresetBlue),
|
||||
background_color_(OSDPrettySettings::kPresetBlue),
|
||||
background_opacity_(0.85),
|
||||
popup_screen_(nullptr),
|
||||
disable_duration_(false),
|
||||
@@ -232,20 +231,20 @@ bool OSDPretty::IsTransparencyAvailable() {
|
||||
void OSDPretty::Load() {
|
||||
|
||||
Settings s;
|
||||
s.beginGroup(kSettingsGroup);
|
||||
foreground_color_ = QColor(s.value("foreground_color", 0).toInt());
|
||||
background_color_ = QColor(s.value("background_color", kPresetBlue).toInt());
|
||||
background_opacity_ = s.value("background_opacity", 0.85).toFloat();
|
||||
font_.fromString(s.value("font", u"Verdana,9,-1,5,50,0,0,0,0,0"_s).toString());
|
||||
disable_duration_ = s.value("disable_duration", false).toBool();
|
||||
s.beginGroup(OSDPrettySettings::kSettingsGroup);
|
||||
foreground_color_ = QColor(s.value(OSDPrettySettings::kForegroundColor, 0).toInt());
|
||||
background_color_ = QColor(s.value(OSDPrettySettings::kBackgroundColor, OSDPrettySettings::kPresetBlue).toInt());
|
||||
background_opacity_ = s.value(OSDPrettySettings::kBackgroundOpacity, 0.85).toFloat();
|
||||
font_.fromString(s.value(OSDPrettySettings::kFont, u"Verdana,9,-1,5,50,0,0,0,0,0"_s).toString());
|
||||
disable_duration_ = s.value(OSDPrettySettings::kDisableDuration, false).toBool();
|
||||
#ifdef Q_OS_WIN
|
||||
fading_enabled_ = s.value("fading", true).toBool();
|
||||
fading_enabled_ = s.value(OSDPrettySettings::kFading, true).toBool();
|
||||
#else
|
||||
fading_enabled_ = s.value("fading", false).toBool();
|
||||
fading_enabled_ = s.value(OSDPrettySettings::kFading, false).toBool();
|
||||
#endif
|
||||
|
||||
if (s.contains("popup_screen"_L1)) {
|
||||
popup_screen_name_ = s.value("popup_screen").toString();
|
||||
if (s.contains(OSDPrettySettings::kPopupScreen)) {
|
||||
popup_screen_name_ = s.value(OSDPrettySettings::kPopupScreen).toString();
|
||||
if (screens_.contains(popup_screen_name_)) {
|
||||
popup_screen_ = screens_.value(popup_screen_name_);
|
||||
}
|
||||
@@ -260,8 +259,8 @@ void OSDPretty::Load() {
|
||||
if (current_screen()) popup_screen_name_ = current_screen()->name();
|
||||
}
|
||||
|
||||
if (s.contains("popup_pos"_L1)) {
|
||||
popup_pos_ = s.value("popup_pos").toPoint();
|
||||
if (s.contains(OSDPrettySettings::kPopupPos)) {
|
||||
popup_pos_ = s.value(OSDPrettySettings::kPopupPos).toPoint();
|
||||
}
|
||||
else {
|
||||
if (popup_screen_) {
|
||||
|
||||
@@ -57,20 +57,9 @@ class OSDPretty : public QWidget {
|
||||
Draggable
|
||||
};
|
||||
|
||||
explicit OSDPretty(Mode mode, QWidget *parent = nullptr);
|
||||
explicit OSDPretty(const Mode mode, QWidget *parent = nullptr);
|
||||
~OSDPretty() override;
|
||||
|
||||
static const char *kSettingsGroup;
|
||||
|
||||
static const int kDropShadowSize;
|
||||
static const int kBorderRadius;
|
||||
static const int kMaxIconSize;
|
||||
|
||||
static const int kSnapProximity;
|
||||
|
||||
static const QRgb kPresetBlue;
|
||||
static const QRgb kPresetRed;
|
||||
|
||||
bool IsTransparencyAvailable();
|
||||
|
||||
void SetMessage(const QString &summary, const QString &message, const QImage &image);
|
||||
|
||||
Reference in New Issue
Block a user