From 0902741b856a89887db1a3d770826e3fc6871cba Mon Sep 17 00:00:00 2001 From: John Preston Date: Mon, 17 Oct 2016 13:55:17 +0300 Subject: [PATCH] Respecting Quite Hours in Windows. Also closing current chat on window close or passcode lock. --- Telegram/SourceFiles/app.cpp | 5 +- Telegram/SourceFiles/dialogswidget.cpp | 2 +- Telegram/SourceFiles/facades.cpp | 6 +- Telegram/SourceFiles/mainwindow.cpp | 6 +- Telegram/SourceFiles/overviewwidget.cpp | 2 +- Telegram/SourceFiles/passcodewidget.cpp | 2 + .../linux/notifications_manager_linux.h | 12 ++- .../platform/mac/notifications_manager_mac.h | 10 ++- .../win/notifications_manager_win.cpp | 83 +++++++++++++++++++ .../platform/win/notifications_manager_win.h | 8 +- Telegram/SourceFiles/pspecific_linux.cpp | 8 -- Telegram/SourceFiles/pspecific_linux.h | 3 - Telegram/SourceFiles/pspecific_mac.cpp | 8 -- Telegram/SourceFiles/pspecific_mac.h | 3 - Telegram/SourceFiles/pspecific_win.cpp | 16 ---- Telegram/SourceFiles/pspecific_win.h | 3 - 16 files changed, 121 insertions(+), 56 deletions(-) diff --git a/Telegram/SourceFiles/app.cpp b/Telegram/SourceFiles/app.cpp index 726f7c857..2c20fe32a 100644 --- a/Telegram/SourceFiles/app.cpp +++ b/Telegram/SourceFiles/app.cpp @@ -43,6 +43,7 @@ Copyright (c) 2014-2016 John Preston, https://desktop.telegram.org #include "observer_peer.h" #include "window/chat_background.h" #include "window/notifications_manager.h" +#include "platform/platform_notifications_manager.h" namespace { App::LaunchState _launchState = App::Launched; @@ -2399,7 +2400,9 @@ namespace { } void playSound() { - if (Global::SoundNotify() && !psSkipAudioNotify()) audioPlayNotify(); + if (Global::SoundNotify() && !Platform::Notifications::skipAudio()) { + audioPlayNotify(); + } } void checkImageCacheSize() { diff --git a/Telegram/SourceFiles/dialogswidget.cpp b/Telegram/SourceFiles/dialogswidget.cpp index c42a019ff..978109375 100644 --- a/Telegram/SourceFiles/dialogswidget.cpp +++ b/Telegram/SourceFiles/dialogswidget.cpp @@ -1886,7 +1886,7 @@ void DialogsWidget::step_show(float64 ms, bool timer) { _a_show.stop(); onFilterUpdate(); - activate(); + if (App::wnd()) App::wnd()->setInnerFocus(); if (App::app()) App::app()->mtpUnpause(); } else { diff --git a/Telegram/SourceFiles/facades.cpp b/Telegram/SourceFiles/facades.cpp index f3c09b82b..8457a0775 100644 --- a/Telegram/SourceFiles/facades.cpp +++ b/Telegram/SourceFiles/facades.cpp @@ -285,11 +285,15 @@ bool hideWindowNoQuit() { if (!App::quitting()) { if (auto w = App::wnd()) { if (cWorkMode() == dbiwmTrayOnly || cWorkMode() == dbiwmWindowAndTray) { - return w->minimizeToTray(); + if (w->minimizeToTray()) { + Ui::showChatsList(); + return true; + } } else if (cPlatform() == dbipMac || cPlatform() == dbipMacOld) { w->closeWithoutDestroy(); w->updateIsActive(Global::OfflineBlurTimeout()); w->updateGlobalMenu(); + Ui::showChatsList(); return true; } } diff --git a/Telegram/SourceFiles/mainwindow.cpp b/Telegram/SourceFiles/mainwindow.cpp index cbba2a63b..e9ecb1c39 100644 --- a/Telegram/SourceFiles/mainwindow.cpp +++ b/Telegram/SourceFiles/mainwindow.cpp @@ -44,6 +44,7 @@ Copyright (c) 2014-2016 John Preston, https://desktop.telegram.org #include "apiwrap.h" #include "settings/settings_widget.h" #include "window/notifications_manager.h" +#include "platform/platform_notifications_manager.h" ConnectingWidget::ConnectingWidget(QWidget *parent, const QString &text, const QString &reconnect) : QWidget(parent) , _shadow(st::boxShadow) @@ -1120,11 +1121,10 @@ void MainWindow::notifySchedule(History *history, HistoryItem *item) { } else if (cOtherOnline() >= t) { delay = Global::NotifyDefaultDelay(); } -// LOG(("Is online: %1, otherOnline: %2, currentTime: %3, otherNotOld: %4, otherLaterThanMe: %5").arg(Logs::b(isOnline)).arg(cOtherOnline()).arg(t).arg(Logs::b(otherNotOld)).arg(Logs::b(otherLaterThanMe))); uint64 when = ms + delay; _notifyWhenAlerts[history].insert(when, notifyByFrom); - if (Global::DesktopNotify() && !psSkipDesktopNotify()) { + if (Global::DesktopNotify() && !Platform::Notifications::skipToast()) { NotifyWhenMaps::iterator i = _notifyWhenMaps.find(history); if (i == _notifyWhenMaps.end()) { i = _notifyWhenMaps.insert(history, NotifyWhenMap()); @@ -1254,7 +1254,7 @@ void MainWindow::notifyShowNext() { App::playSound(); } - if (_notifyWaiters.isEmpty() || !Global::DesktopNotify() || psSkipDesktopNotify()) { + if (_notifyWaiters.isEmpty() || !Global::DesktopNotify() || Platform::Notifications::skipToast()) { if (nextAlert) { _notifyWaitTimer.start(nextAlert - ms); } diff --git a/Telegram/SourceFiles/overviewwidget.cpp b/Telegram/SourceFiles/overviewwidget.cpp index 2271e58b4..b58b9a468 100644 --- a/Telegram/SourceFiles/overviewwidget.cpp +++ b/Telegram/SourceFiles/overviewwidget.cpp @@ -531,7 +531,7 @@ void OverviewInner::dragActionFinish(const QPoint &screenPos, Qt::MouseButton bu auto sel = _selected.cbegin().value(); if (sel != FullSelection && sel.from == sel.to) { _selected.clear(); - App::main()->activate(); + App::wnd()->setInnerFocus(); } } } diff --git a/Telegram/SourceFiles/passcodewidget.cpp b/Telegram/SourceFiles/passcodewidget.cpp index 1a2688f00..b62404961 100644 --- a/Telegram/SourceFiles/passcodewidget.cpp +++ b/Telegram/SourceFiles/passcodewidget.cpp @@ -142,6 +142,8 @@ void PasscodeWidget::step_show(float64 ms, bool timer) { if (App::wnd()) App::wnd()->setInnerFocus(); if (App::app()) App::app()->mtpUnpause(); + + Ui::showChatsList(); } else { a_coordUnder.update(dt, st::slideFunction); a_coordOver.update(dt, st::slideFunction); diff --git a/Telegram/SourceFiles/platform/linux/notifications_manager_linux.h b/Telegram/SourceFiles/platform/linux/notifications_manager_linux.h index 79f9c0259..c1f28d40d 100644 --- a/Telegram/SourceFiles/platform/linux/notifications_manager_linux.h +++ b/Telegram/SourceFiles/platform/linux/notifications_manager_linux.h @@ -25,6 +25,15 @@ Copyright (c) 2014-2016 John Preston, https://desktop.telegram.org namespace Platform { namespace Notifications { +inline void defaultNotificationShown(QWidget *widget) { +} +inline bool skipAudio() { + return false; +} +inline bool skipToast() { + return false; +} + class Manager; void start(); @@ -32,9 +41,6 @@ Manager *manager(); bool supported(); void finish(); -inline void defaultNotificationShown(QWidget *widget) { -} - class Manager : public Window::Notifications::NativeManager { public: Manager(); diff --git a/Telegram/SourceFiles/platform/mac/notifications_manager_mac.h b/Telegram/SourceFiles/platform/mac/notifications_manager_mac.h index 17d033d7c..a7fd87922 100644 --- a/Telegram/SourceFiles/platform/mac/notifications_manager_mac.h +++ b/Telegram/SourceFiles/platform/mac/notifications_manager_mac.h @@ -25,14 +25,20 @@ Copyright (c) 2014-2016 John Preston, https://desktop.telegram.org namespace Platform { namespace Notifications { +void defaultNotificationShown(QWidget *widget); +inline bool skipAudio() { + return false; +} +inline bool skipToast() { + return false; +} + class Manager; void start(); Manager *manager(); void finish(); -void defaultNotificationShown(QWidget *widget); - class Manager : public Window::Notifications::NativeManager { public: Manager(); diff --git a/Telegram/SourceFiles/platform/win/notifications_manager_win.cpp b/Telegram/SourceFiles/platform/win/notifications_manager_win.cpp index aded7f1b4..3a87d6ec3 100644 --- a/Telegram/SourceFiles/platform/win/notifications_manager_win.cpp +++ b/Telegram/SourceFiles/platform/win/notifications_manager_win.cpp @@ -23,6 +23,7 @@ Copyright (c) 2014-2016 John Preston, https://desktop.telegram.org #include "window/notifications_utilities.h" #include "platform/win/windows_app_user_model_id.h" +#include "platform/win/windows_event_filter.h" #include "platform/win/windows_dlls.h" #include "mainwindow.h" @@ -534,5 +535,87 @@ void Manager::onAfterNotificationActivated(PeerId peerId, MsgId msgId) { _impl->afterNotificationActivated(peerId, msgId); } +namespace { + +bool QuiteHoursEnabled = false; +DWORD QuiteHoursValue = 0; + +void queryQuiteHours() { + LPTSTR lpKeyName = L"Software\\Microsoft\\Windows\\CurrentVersion\\Notifications\\Settings"; + LPTSTR lpValueName = L"NOC_GLOBAL_SETTING_TOASTS_ENABLED"; + HKEY key; + auto result = RegOpenKeyEx(HKEY_CURRENT_USER, lpKeyName, 0, KEY_READ, &key); + if (result != ERROR_SUCCESS) { + return; + } + + DWORD value = 0, type = 0, size = sizeof(value); + result = RegQueryValueEx(key, lpValueName, 0, &type, (LPBYTE)&value, &size); + RegCloseKey(key); + + auto quiteHoursEnabled = (result == ERROR_SUCCESS); + if (QuiteHoursEnabled != quiteHoursEnabled) { + QuiteHoursEnabled = quiteHoursEnabled; + QuiteHoursValue = value; + LOG(("Quite hours changed, entry value: %1").arg(value)); + } else if (QuiteHoursValue != value) { + QuiteHoursValue = value; + LOG(("Quite hours value changed, was value: %1, entry value: %2").arg(QuiteHoursValue).arg(value)); + } +} + +QUERY_USER_NOTIFICATION_STATE UserNotificationState = QUNS_ACCEPTS_NOTIFICATIONS; + +void queryUserNotificationState() { + if (Dlls::SHQueryUserNotificationState != nullptr) { + QUERY_USER_NOTIFICATION_STATE state; + if (SUCCEEDED(Dlls::SHQueryUserNotificationState(&state))) { + UserNotificationState = state; + } + } +} + +static constexpr int QuerySettingsEachMs = 1000; +uint64 LastSettingsQueryMs = 0; + +void querySystemNotificationSettings() { + auto ms = getms(true); + if (LastSettingsQueryMs > 0 && ms <= LastSettingsQueryMs + QuerySettingsEachMs) { + return; + } + LastSettingsQueryMs = ms; + queryQuiteHours(); + queryUserNotificationState(); +} + +} // namespace + +bool skipAudio() { + querySystemNotificationSettings(); + + if (UserNotificationState == QUNS_NOT_PRESENT || UserNotificationState == QUNS_PRESENTATION_MODE) { + return true; + } + if (QuiteHoursEnabled) { + return true; + } + if (EventFilter::getInstance()->sessionLoggedOff()) { + return true; + } + return false; +} + +bool skipToast() { + querySystemNotificationSettings(); + + if (UserNotificationState == QUNS_PRESENTATION_MODE || UserNotificationState == QUNS_RUNNING_D3D_FULL_SCREEN/* || UserNotificationState == QUNS_BUSY*/) { + return true; + } + if (QuiteHoursEnabled) { + return true; + } + return false; +} + } // namespace Notifications } // namespace Platform diff --git a/Telegram/SourceFiles/platform/win/notifications_manager_win.h b/Telegram/SourceFiles/platform/win/notifications_manager_win.h index ae5e396bb..403e4e00a 100644 --- a/Telegram/SourceFiles/platform/win/notifications_manager_win.h +++ b/Telegram/SourceFiles/platform/win/notifications_manager_win.h @@ -25,6 +25,11 @@ Copyright (c) 2014-2016 John Preston, https://desktop.telegram.org namespace Platform { namespace Notifications { +inline void defaultNotificationShown(QWidget *widget) { +} +bool skipAudio(); +bool skipToast(); + class Manager; void start(); @@ -32,9 +37,6 @@ Manager *manager(); bool supported(); void finish(); -inline void defaultNotificationShown(QWidget *widget) { -} - class Manager : public Window::Notifications::NativeManager { public: Manager(); diff --git a/Telegram/SourceFiles/pspecific_linux.cpp b/Telegram/SourceFiles/pspecific_linux.cpp index 6c4ad17a9..0662398b7 100644 --- a/Telegram/SourceFiles/pspecific_linux.cpp +++ b/Telegram/SourceFiles/pspecific_linux.cpp @@ -294,14 +294,6 @@ uint64 psIdleTime() { return getms(true) - _lastUserAction; } -bool psSkipAudioNotify() { - return false; -} - -bool psSkipDesktopNotify() { - return false; -} - void psActivateProcess(uint64 pid) { // objc_activateProgram(); } diff --git a/Telegram/SourceFiles/pspecific_linux.h b/Telegram/SourceFiles/pspecific_linux.h index 9e13d9ab9..b7eea56aa 100644 --- a/Telegram/SourceFiles/pspecific_linux.h +++ b/Telegram/SourceFiles/pspecific_linux.h @@ -39,9 +39,6 @@ void psUserActionDone(); bool psIdleSupported(); uint64 psIdleTime(); -bool psSkipAudioNotify(); -bool psSkipDesktopNotify(); - QStringList psInitLogs(); void psClearInitLogs(); diff --git a/Telegram/SourceFiles/pspecific_mac.cpp b/Telegram/SourceFiles/pspecific_mac.cpp index cf6a12e4a..d9029e508 100644 --- a/Telegram/SourceFiles/pspecific_mac.cpp +++ b/Telegram/SourceFiles/pspecific_mac.cpp @@ -302,14 +302,6 @@ uint64 psIdleTime() { return objc_idleTime(idleTime) ? idleTime : (getms(true) - _lastUserAction); } -bool psSkipAudioNotify() { - return false; -} - -bool psSkipDesktopNotify() { - return false; -} - QStringList psInitLogs() { return _initLogs; } diff --git a/Telegram/SourceFiles/pspecific_mac.h b/Telegram/SourceFiles/pspecific_mac.h index b77374387..45d8e6969 100644 --- a/Telegram/SourceFiles/pspecific_mac.h +++ b/Telegram/SourceFiles/pspecific_mac.h @@ -42,9 +42,6 @@ void psUserActionDone(); bool psIdleSupported(); uint64 psIdleTime(); -bool psSkipAudioNotify(); -bool psSkipDesktopNotify(); - QStringList psInitLogs(); void psClearInitLogs(); diff --git a/Telegram/SourceFiles/pspecific_win.cpp b/Telegram/SourceFiles/pspecific_win.cpp index 72ade2e76..7e79bfec7 100644 --- a/Telegram/SourceFiles/pspecific_win.cpp +++ b/Telegram/SourceFiles/pspecific_win.cpp @@ -176,22 +176,6 @@ uint64 psIdleTime() { return GetLastInputInfo(&lii) ? (GetTickCount() - lii.dwTime) : (getms(true) - _lastUserAction); } -bool psSkipAudioNotify() { - QUERY_USER_NOTIFICATION_STATE state; - if (useShellapi && SUCCEEDED(Dlls::SHQueryUserNotificationState(&state))) { - if (state == QUNS_NOT_PRESENT || state == QUNS_PRESENTATION_MODE) return true; - } - return EventFilter::getInstance()->sessionLoggedOff(); -} - -bool psSkipDesktopNotify() { - QUERY_USER_NOTIFICATION_STATE state; - if (useShellapi && SUCCEEDED(Dlls::SHQueryUserNotificationState(&state))) { - if (state == QUNS_PRESENTATION_MODE || state == QUNS_RUNNING_D3D_FULL_SCREEN/* || state == QUNS_BUSY*/) return true; - } - return false; -} - QStringList psInitLogs() { return _initLogs; } diff --git a/Telegram/SourceFiles/pspecific_win.h b/Telegram/SourceFiles/pspecific_win.h index a5f20bd7b..2d9280c21 100644 --- a/Telegram/SourceFiles/pspecific_win.h +++ b/Telegram/SourceFiles/pspecific_win.h @@ -38,9 +38,6 @@ void psUserActionDone(); bool psIdleSupported(); uint64 psIdleTime(); -bool psSkipAudioNotify(); -bool psSkipDesktopNotify(); - QStringList psInitLogs(); void psClearInitLogs();