Skip to content

Commit

Permalink
Merge pull request #1220 from deXol/develop
Browse files Browse the repository at this point in the history
Fix SnoreToast notifications for Win10
  • Loading branch information
limpkin authored Apr 11, 2024
2 parents 48015ad + ecf23d8 commit 95399fb
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 28 deletions.
1 change: 1 addition & 0 deletions src/AppGui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

#ifdef Q_OS_WIN
#include "SystemNotifications/SystemNotificationWindows.h"
#include "windows.h"
#endif

#ifdef Q_OS_MAC
Expand Down
44 changes: 19 additions & 25 deletions src/SystemNotifications/SystemNotificationWindows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ typedef NTSTATUS (NTAPI *PNTQUERYWNFSTATEDATA)(
_Out_writes_bytes_to_opt_(*BufferSize, *BufferSize) PVOID Buffer,
_Inout_ PULONG BufferSize);

const QString SystemNotificationWindows::SNORETOAST_FORMAT= "SnoreToast.exe -t \"%1\" -m \"%2\" %3 %4 -p icon.png %5";
const QString SystemNotificationWindows::SNORETOAST_INSTALL= "SnoreToast.exe -install";
const QString SystemNotificationWindows::WINDOWS10_VERSION = "10";
const QString SystemNotificationWindows::SNORETOAST= "SnoreToast.exe";
const QString SystemNotificationWindows::SNORETOAST_INSTALL= "-install";
const QString SystemNotificationWindows::NOTIFICATIONS_SETTING_REGENTRY = "HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Notifications\\Settings";
const QString SystemNotificationWindows::DND_ENABLED_REGENTRY = "NOC_GLOBAL_SETTING_TOASTS_ENABLED";
const QString SystemNotificationWindows::TOAST_ENABLED_SETTING_REGPATH = "HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\PushNotifications";
const QString SystemNotificationWindows::TOAST_ENABLED_REGENTRY = "ToastEnabled";
const bool SystemNotificationWindows::IS_WIN10 = QSysInfo::productVersion() == WINDOWS10_VERSION;
const QString SystemNotificationWindows::ICON = "icon.png";
const bool SystemNotificationWindows::IS_WIN10_OR_ABOVE = QSysInfo::productVersion().toInt() >= WINDOWS10_VERSION;


SystemNotificationWindows::SystemNotificationWindows(QObject *parent)
Expand Down Expand Up @@ -79,15 +79,7 @@ SystemNotificationWindows::~SystemNotificationWindows()

void SystemNotificationWindows::createNotification(const QString &title, const QString text)
{
if (IS_WIN10)
{
QString notification = SNORETOAST_FORMAT.arg(title, text, "", "", "");
process->start(notification);
}
else
{
emit notifySystray(title, text);
}
emit notifySystray(title, text);
}

void SystemNotificationWindows::createButtonChoiceNotification(const QString &title, const QString text, const QStringList &buttons)
Expand All @@ -97,34 +89,33 @@ void SystemNotificationWindows::createButtonChoiceNotification(const QString &ti
qDebug() << "createButtonChoiceNotification called without button text";
return;
}
QString buttonString = "-b \"";
QString buttonString = "";
for (const auto &piece : buttons)
{
buttonString += piece + ";";
}

buttonString = buttonString.replace(buttonString.size()-1, 1, "\"");
buttonString = buttonString.replace(buttonString.size()-1, 1, "");

QProcess *proc = new QProcess();
notificationMap->insert(notificationId, proc);
QString notification = SNORETOAST_FORMAT.arg(title, text, buttonString, "-id " + QString::number(notificationId++), "-w");
connect(proc, static_cast<CallbackType>(&QProcess::finished), this, &SystemNotificationWindows::callbackFunction);
qDebug() << notification;
proc->start(notification);
QStringList choiceArgs = { "-t", title, "-m", text, "-b", buttonString, "-id", QString::number(notificationId++), "-p", ICON, "-w"};
proc->start(SNORETOAST, choiceArgs);
}

void SystemNotificationWindows::createTextBoxNotification(const QString &title, const QString text)
{
QProcess *proc = new QProcess();
notificationMap->insert(notificationId, proc);
QString notification = SNORETOAST_FORMAT.arg(title, text, "-tb", "-id " + QString::number(notificationId++), "-w");
QStringList textNotiArgs = { "-t", title, "-m", text, "-tb", "-id", QString::number(notificationId++), "-p", ICON, "-w"};
connect(proc, static_cast<CallbackType>(&QProcess::finished), this, &SystemNotificationWindows::callbackFunction);
proc->start(notification);
proc->start(SNORETOAST, textNotiArgs);
}

bool SystemNotificationWindows::displayLoginRequestNotification(const QString &service, QString &loginName, QString message)
{
if (IS_WIN10 && !isDoNotDisturbEnabled())
if (IS_WIN10_OR_ABOVE && !isDoNotDisturbEnabled())
{
// A text box notification is displayed on Win10
messageMap->insert(notificationId, message);
Expand All @@ -144,7 +135,7 @@ bool SystemNotificationWindows::displayLoginRequestNotification(const QString &s

bool SystemNotificationWindows::displayDomainSelectionNotification(const QString &domain, const QString &subdomain, QString &serviceName, QString message)
{
if (IS_WIN10 && !isDoNotDisturbEnabled())
if (IS_WIN10_OR_ABOVE && !isDoNotDisturbEnabled())
{
// A button choice notification is displayed on Win10
QStringList buttons;
Expand All @@ -166,9 +157,9 @@ bool SystemNotificationWindows::displayDomainSelectionNotification(const QString

void SystemNotificationWindows::installSnoreToast()
{
if (IS_WIN10)
if (IS_WIN10_OR_ABOVE)
{
process->start(SNORETOAST_INSTALL);
process->start(SNORETOAST, {SNORETOAST_INSTALL});
}
}

Expand Down Expand Up @@ -201,7 +192,10 @@ bool SystemNotificationWindows::isDoNotDisturbEnabled() const
QSettings settings(NOTIFICATIONS_SETTING_REGENTRY, QSettings::NativeFormat);
bool isDoNotDisturb = !settings.value(DND_ENABLED_REGENTRY).isNull();
QSettings toastSetting(TOAST_ENABLED_SETTING_REGPATH, QSettings::NativeFormat);
isDoNotDisturb |= !toastSetting.value(TOAST_ENABLED_REGENTRY).toBool();
if (toastSetting.contains(TOAST_ENABLED_REGENTRY))
{
isDoNotDisturb |= !toastSetting.value(TOAST_ENABLED_REGENTRY).toBool();
}

if (!isDoNotDisturb)
{
Expand Down
7 changes: 4 additions & 3 deletions src/SystemNotifications/SystemNotificationWindows.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,15 @@ class SystemNotificationWindows : public ISystemNotification
virtual bool displayLoginRequestNotification(const QString& service, QString &loginName, QString message) override;
virtual bool displayDomainSelectionNotification(const QString& domain, const QString& subdomain, QString &serviceName, QString message) override;

const static QString SNORETOAST_FORMAT;
const static QString SNORETOAST;
const static QString SNORETOAST_INSTALL;
const static QString WINDOWS10_VERSION;
const static QString NOTIFICATIONS_SETTING_REGENTRY;
const static QString DND_ENABLED_REGENTRY;
const static QString TOAST_ENABLED_SETTING_REGPATH;
const static QString TOAST_ENABLED_REGENTRY;
const static bool IS_WIN10;
const static QString ICON;
const static int WINDOWS10_VERSION = 10;
const static bool IS_WIN10_OR_ABOVE;

signals:
void notifySystray(QString title, QString text);
Expand Down

0 comments on commit 95399fb

Please sign in to comment.