From 2de5a9d2812c0e4f026453cd98a85cf2b4217eaf Mon Sep 17 00:00:00 2001 From: Claudio Bantaloukas Date: Sat, 24 Dec 2016 01:04:43 +0100 Subject: [PATCH 01/13] Lock database when OS is locked (Windows, DBus, macOS implementations) #134 --- src/CMakeLists.txt | 22 ++++++++ src/core/ScreenLockListener.cpp | 11 ++++ src/core/ScreenLockListener.h | 21 ++++++++ src/core/ScreenLockListenerDBus.cpp | 66 ++++++++++++++++++++++++ src/core/ScreenLockListenerDBus.h | 18 +++++++ src/core/ScreenLockListenerMac.cpp | 41 +++++++++++++++ src/core/ScreenLockListenerMac.h | 24 +++++++++ src/core/ScreenLockListenerPrivate.cpp | 26 ++++++++++ src/core/ScreenLockListenerPrivate.h | 19 +++++++ src/core/ScreenLockListenerWin.cpp | 69 ++++++++++++++++++++++++++ src/core/ScreenLockListenerWin.h | 21 ++++++++ src/gui/MainWindow.cpp | 10 ++++ src/gui/MainWindow.h | 3 ++ src/gui/SettingsWidget.cpp | 4 ++ src/gui/SettingsWidgetGeneral.ui | 7 +++ 15 files changed, 362 insertions(+) create mode 100644 src/core/ScreenLockListener.cpp create mode 100644 src/core/ScreenLockListener.h create mode 100644 src/core/ScreenLockListenerDBus.cpp create mode 100644 src/core/ScreenLockListenerDBus.h create mode 100644 src/core/ScreenLockListenerMac.cpp create mode 100644 src/core/ScreenLockListenerMac.h create mode 100644 src/core/ScreenLockListenerPrivate.cpp create mode 100644 src/core/ScreenLockListenerPrivate.h create mode 100644 src/core/ScreenLockListenerWin.cpp create mode 100644 src/core/ScreenLockListenerWin.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 42cbe36bb..dfdf96c8a 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -48,6 +48,10 @@ set(keepassx_SOURCES core/PasswordGenerator.cpp core/PassphraseGenerator.cpp core/SignalMultiplexer.cpp + core/ScreenLockListener.cpp + core/ScreenLockListener.h + core/ScreenLockListenerPrivate.h + core/ScreenLockListenerPrivate.cpp core/TimeDelta.cpp core/TimeInfo.cpp core/ToDbExporter.cpp @@ -136,6 +140,24 @@ set(keepassx_SOURCES totp/totp.h totp/totp.cpp ) +if(APPLE) + set(keepassx_SOURCES ${keepassx_SOURCES} + core/ScreenLockListenerMac.h + core/ScreenLockListenerMac.cpp + ) +endif() +if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux") + set(keepassx_SOURCES ${keepassx_SOURCES} + core/ScreenLockListenerDBus.h + core/ScreenLockListenerDBus.cpp + ) +endif() +if(MINGW) + set(keepassx_SOURCES ${keepassx_SOURCES} + core/ScreenLockListenerWin.h + core/ScreenLockListenerWin.cpp + ) +endif() set(keepassx_SOURCES_MAINEXE main.cpp diff --git a/src/core/ScreenLockListener.cpp b/src/core/ScreenLockListener.cpp new file mode 100644 index 000000000..2c17dfa14 --- /dev/null +++ b/src/core/ScreenLockListener.cpp @@ -0,0 +1,11 @@ +#include "ScreenLockListener.h" +#include "ScreenLockListenerPrivate.h" + +ScreenLockListener::ScreenLockListener(QWidget* parent): + QObject(parent){ + m_listener = ScreenLockListenerPrivate::instance(parent); + connect(m_listener,SIGNAL(screenLocked()), this,SIGNAL(screenLocked())); +} + +ScreenLockListener::~ScreenLockListener(){ +} diff --git a/src/core/ScreenLockListener.h b/src/core/ScreenLockListener.h new file mode 100644 index 000000000..7339054f9 --- /dev/null +++ b/src/core/ScreenLockListener.h @@ -0,0 +1,21 @@ +#ifndef SCREENLOCKLISTENER_H +#define SCREENLOCKLISTENER_H +#include + +class ScreenLockListenerPrivate; + +class ScreenLockListener : public QObject { + Q_OBJECT + +public: + ScreenLockListener(QWidget* parent=NULL); + ~ScreenLockListener(); + +Q_SIGNALS: + void screenLocked(); + +private: + ScreenLockListenerPrivate* m_listener; +}; + +#endif // SCREENLOCKLISTENER_H diff --git a/src/core/ScreenLockListenerDBus.cpp b/src/core/ScreenLockListenerDBus.cpp new file mode 100644 index 000000000..283b1e27e --- /dev/null +++ b/src/core/ScreenLockListenerDBus.cpp @@ -0,0 +1,66 @@ +#include "ScreenLockListenerDBus.h" + +#include +#include +#include + +ScreenLockListenerDBus::ScreenLockListenerDBus(QWidget *parent): + ScreenLockListenerMac(parent) +{ + QDBusConnection sessionBus = QDBusConnection::sessionBus(); + QDBusConnection systemBus = QDBusConnection::systemBus(); + sessionBus.connect( + "org.gnome.SessionManager", // service + "/org/gnome/SessionManager/Presence", // path + "org.gnome.SessionManager.Presence", // interface + "StatusChanged", // signal name + this, //receiver + SLOT(gnomeSessionStatusChanged(uint))); + + systemBus.connect( + "org.freedesktop.login1", // service + "/org/freedesktop/login1", // path + "org.freedesktop.login1.Manager", // interface + "PrepareForSleep", // signal name + this, //receiver + SLOT(logindPrepareForSleep(bool))); + + sessionBus.connect( + "com.canonical.Unity", // service + "/com/canonical/Unity/Session", // path + "com.canonical.Unity.Session", // interface + "Locked", // signal name + this, //receiver + SLOT(unityLocked())); + + /* Currently unable to get the current user session from login1.Manager + QDBusInterface login1_manager_iface("org.freedesktop.login1", "/org/freedesktop/login1", + "org.freedesktop.login1.Manager", systemBus); + if(login1_manager_iface.isValid()){ + qint64 my_pid = QCoreApplication::applicationPid(); + QDBusReply reply = login1_manager_iface.call("GetSessionByPID",static_cast(my_pid)); + if (reply.isValid()){ + QString current_session = reply.value(); + } + } + */ +} + +void ScreenLockListenerDBus::gnomeSessionStatusChanged(uint status) +{ + if(status != 0){ + Q_EMIT screenLocked(); + } +} + +void ScreenLockListenerDBus::logindPrepareForSleep(bool beforeSleep) +{ + if(beforeSleep){ + Q_EMIT screenLocked(); + } +} + +void ScreenLockListenerDBus::unityLocked() +{ + Q_EMIT screenLocked(); +} diff --git a/src/core/ScreenLockListenerDBus.h b/src/core/ScreenLockListenerDBus.h new file mode 100644 index 000000000..016932b8a --- /dev/null +++ b/src/core/ScreenLockListenerDBus.h @@ -0,0 +1,18 @@ +#ifndef SCREENLOCKLISTENERDBUS_H +#define SCREENLOCKLISTENERDBUS_H +#include +#include + +class ScreenLockListenerDBus : public ScreenLockListenerPrivate +{ + Q_OBJECT +public: + explicit ScreenLockListenerDBus(QWidget *parent = 0); + +private Q_SLOTS: + void gnomeSessionStatusChanged(uint status); + void logindPrepareForSleep(bool beforeSleep); + void unityLocked(); +}; + +#endif // SCREENLOCKLISTENERDBUS_H diff --git a/src/core/ScreenLockListenerMac.cpp b/src/core/ScreenLockListenerMac.cpp new file mode 100644 index 000000000..57dc2de3f --- /dev/null +++ b/src/core/ScreenLockListenerMac.cpp @@ -0,0 +1,41 @@ +#include "ScreenLockListenerMac.h" + +#include +#include + +ScreenLockListenerMac* ScreenLockListenerMac::instance(){ + static QMutex mutex; + QMutexLocker lock(&mutex); + + static ScreenLockListenerMac* m_ptr=NULL; + if (m_ptr==NULL){ + m_ptr = new ScreenLockListenerMac(); + } + return m_ptr; +} + +void ScreenLockListenerMac::notificationCenterCallBack(CFNotificationCenterRef /*center*/, void */*observer*/, + CFNotificationName /*name*/, const void */*object*/, CFDictionaryRef /*userInfo*/){ + instance()->onSignalReception(); +} + +ScreenLockListenerMac::ScreenLockListenerMac(QWidget* parent): + ScreenLockListenerPrivate(parent){ + CFNotificationCenterRef distCenter; + CFStringRef screenIsLockedSignal = CFSTR("com.apple.screenIsLocked"); + distCenter = CFNotificationCenterGetDistributedCenter(); + if (NULL == distCenter) + return; + + CFNotificationCenterAddObserver( + distCenter, + this, &ScreenLockListenerMac::notificationCenterCallBack, + screenIsLockedSignal, + NULL, + CFNotificationSuspensionBehaviorDeliverImmediately); +} + +void ScreenLockListenerMac::onSignalReception() +{ + Q_EMIT screenLocked(); +} diff --git a/src/core/ScreenLockListenerMac.h b/src/core/ScreenLockListenerMac.h new file mode 100644 index 000000000..5824339f1 --- /dev/null +++ b/src/core/ScreenLockListenerMac.h @@ -0,0 +1,24 @@ +#ifndef SCREENLOCKLISTENERMAC_H +#define SCREENLOCKLISTENERMAC_H +#include +#include + +#include + +#include "ScreenLockListenerPrivate.h" + +class ScreenLockListenerMac: public ScreenLockListenerPrivate { + Q_OBJECT + +public: + static ScreenLockListenerMac* instance(); + static void notificationCenterCallBack(CFNotificationCenterRef /*center*/, void */*observer*/, + CFNotificationName /*name*/, const void */*object*/, CFDictionaryRef /*userInfo*/); + +private: + ScreenLockListenerMac(QWidget* parent=NULL); + void onSignalReception(); + +}; + +#endif // SCREENLOCKLISTENERMAC_H diff --git a/src/core/ScreenLockListenerPrivate.cpp b/src/core/ScreenLockListenerPrivate.cpp new file mode 100644 index 000000000..e9355c7d0 --- /dev/null +++ b/src/core/ScreenLockListenerPrivate.cpp @@ -0,0 +1,26 @@ +#include "ScreenLockListenerPrivate.h" +#if defined(Q_OS_OSX) +#include "ScreenLockListenerMac.h" +#endif +#if defined(Q_OS_LINUX) +#include "ScreenLockListenerDBus.h" +#endif +#if defined(Q_OS_WIN) +#include "ScreenLockListenerWin.h" +#endif + +ScreenLockListenerPrivate::ScreenLockListenerPrivate(QWidget* parent): + QObject(parent){ +} + +ScreenLockListenerPrivate* ScreenLockListenerPrivate::instance(QWidget* parent){ +#if defined(Q_OS_OSX) + return ScreenLockListenerMac::instance(); +#endif +#if defined(Q_OS_LINUX) + return new ScreenLockListenerDBus(parent); +#endif +#if defined(Q_OS_WIN) + return new ScreenLockListenerWin(parent); +#endif +} diff --git a/src/core/ScreenLockListenerPrivate.h b/src/core/ScreenLockListenerPrivate.h new file mode 100644 index 000000000..aef20edcc --- /dev/null +++ b/src/core/ScreenLockListenerPrivate.h @@ -0,0 +1,19 @@ +#ifndef SCREENLOCKLISTENERPRIVATE_H +#define SCREENLOCKLISTENERPRIVATE_H +#include +#include + +class ScreenLockListenerPrivate : public QObject +{ + Q_OBJECT +public: + static ScreenLockListenerPrivate* instance(QWidget *parent = 0); + +protected: + ScreenLockListenerPrivate(QWidget *parent = 0); + +Q_SIGNALS: + void screenLocked(); +}; + +#endif // SCREENLOCKLISTENERPRIVATE_H diff --git a/src/core/ScreenLockListenerWin.cpp b/src/core/ScreenLockListenerWin.cpp new file mode 100644 index 000000000..349d5de58 --- /dev/null +++ b/src/core/ScreenLockListenerWin.cpp @@ -0,0 +1,69 @@ +#include "ScreenLockListenerWin.h" +#include +#include +#include + +/* + * See https://msdn.microsoft.com/en-us/library/windows/desktop/aa373196(v=vs.85).aspx + * See https://msdn.microsoft.com/en-us/library/aa383841(v=vs.85).aspx + * See https://blogs.msdn.microsoft.com/oldnewthing/20060104-50/?p=32783 + */ +ScreenLockListenerWin::ScreenLockListenerWin(QWidget *parent) : + ScreenLockListenerPrivate(parent), + QAbstractNativeEventFilter() +{ + Q_ASSERT(parent!=NULL); + // On windows, we need to register for platform specific messages and + // install a message handler for them + QCoreApplication::instance()->installNativeEventFilter(this); + + // This call requests a notification from windows when a laptop is closed + HPOWERNOTIFY hPnotify = RegisterPowerSettingNotification( + reinterpret_cast(parent->winId()), + &GUID_LIDSWITCH_STATE_CHANGE, DEVICE_NOTIFY_WINDOW_HANDLE); + m_powernotificationhandle = reinterpret_cast(hPnotify); + + // This call requests a notification for session changes + if (!WTSRegisterSessionNotification( + reinterpret_cast(parent->winId()), + NOTIFY_FOR_THIS_SESSION)){ + } +} + +ScreenLockListenerWin::~ScreenLockListenerWin() +{ + HWND h= reinterpret_cast(static_cast(parent())->winId()); + WTSUnRegisterSessionNotification(h); + + if(m_powernotificationhandle){ + UnregisterPowerSettingNotification(reinterpret_cast(m_powernotificationhandle)); + } +} + +bool ScreenLockListenerWin::nativeEventFilter(const QByteArray &eventType, void *message, long *) +{ + if(eventType == "windows_generic_MSG" || eventType == "windows_dispatcher_MSG"){ + MSG* m = static_cast(message); + if(m->message == WM_POWERBROADCAST){ + const POWERBROADCAST_SETTING* setting = reinterpret_cast(m->lParam); + if (setting->PowerSetting == GUID_LIDSWITCH_STATE_CHANGE){ + const DWORD* state = reinterpret_cast(&setting->Data); + if (*state == 0){ + Q_EMIT screenLocked(); + return true; + } + } + } + if(m->message == WM_WTSSESSION_CHANGE){ + if (m->wParam==WTS_CONSOLE_DISCONNECT){ + Q_EMIT screenLocked(); + return true; + } + if (m->wParam==WTS_SESSION_LOCK){ + Q_EMIT screenLocked(); + return true; + } + } + } + return false; +} diff --git a/src/core/ScreenLockListenerWin.h b/src/core/ScreenLockListenerWin.h new file mode 100644 index 000000000..ccf42ad70 --- /dev/null +++ b/src/core/ScreenLockListenerWin.h @@ -0,0 +1,21 @@ +#ifndef SCREENLOCKLISTENERWIN_H +#define SCREENLOCKLISTENERWIN_H +#include +#include +#include + +#include "ScreenLockListenerPrivate.h" + +class ScreenLockListenerWin : public ScreenLockListenerPrivate, public QAbstractNativeEventFilter +{ + Q_OBJECT +public: + explicit ScreenLockListenerWin(QWidget *parent = 0); + ~ScreenLockListenerWin(); + virtual bool nativeEventFilter(const QByteArray &eventType, void *message, long *) Q_DECL_OVERRIDE; + +private: + void * m_powernotificationhandle; +}; + +#endif // SCREENLOCKLISTENERWIN_H diff --git a/src/gui/MainWindow.cpp b/src/gui/MainWindow.cpp index 0415f2b4e..65062fb79 100644 --- a/src/gui/MainWindow.cpp +++ b/src/gui/MainWindow.cpp @@ -329,6 +329,9 @@ MainWindow::MainWindow() connect(m_ui->tabWidget, SIGNAL(messageTab(QString,MessageWidget::MessageType)), this, SLOT(displayTabMessage(QString, MessageWidget::MessageType))); connect(m_ui->tabWidget, SIGNAL(messageDismissTab()), this, SLOT(hideTabMessage())); + m_screenLockListener = new ScreenLockListener(this); + connect(m_screenLockListener, SIGNAL(screenLocked()), SLOT(handleScreenLock())); + updateTrayIcon(); if (config()->hasAccessError()) { @@ -959,3 +962,10 @@ void MainWindow::hideYubiKeyPopup() hideGlobalMessage(); setEnabled(true); } + +void MainWindow::handleScreenLock() +{ + if (config()->get("AutoCloseOnScreenLock").toBool()){ + lockDatabasesAfterInactivity(); + } +} diff --git a/src/gui/MainWindow.h b/src/gui/MainWindow.h index f35cd4658..72082e4ed 100644 --- a/src/gui/MainWindow.h +++ b/src/gui/MainWindow.h @@ -23,6 +23,7 @@ #include #include "core/SignalMultiplexer.h" +#include "core/ScreenLockListener.h" #include "gui/DatabaseWidget.h" #include "gui/Application.h" @@ -91,6 +92,7 @@ private slots: void lockDatabasesAfterInactivity(); void repairDatabase(); void hideTabMessage(); + void handleScreenLock(); private: static void setShortcut(QAction* action, QKeySequence::StandardKey standard, int fallback = 0); @@ -112,6 +114,7 @@ private: InactivityTimer* m_inactivityTimer; int m_countDefaultAttributes; QSystemTrayIcon* m_trayIcon; + ScreenLockListener* m_screenLockListener; Q_DISABLE_COPY(MainWindow) diff --git a/src/gui/SettingsWidget.cpp b/src/gui/SettingsWidget.cpp index 716eb14f1..15f812a0d 100644 --- a/src/gui/SettingsWidget.cpp +++ b/src/gui/SettingsWidget.cpp @@ -141,6 +141,8 @@ void SettingsWidget::loadSettings() } } + m_generalUi->autoCloseOnScreenLockCheckBox->setChecked(config()->get("AutoCloseOnScreenLock").toBool()); + m_secUi->clearClipboardCheckBox->setChecked(config()->get("security/clearclipboard").toBool()); m_secUi->clearClipboardSpinBox->setValue(config()->get("security/clearclipboardtimeout").toInt()); @@ -186,6 +188,8 @@ void SettingsWidget::saveSettings() config()->set("AutoTypeEntryTitleMatch", m_generalUi->autoTypeEntryTitleMatchCheckBox->isChecked()); int currentLangIndex = m_generalUi->languageComboBox->currentIndex(); + config()->set("AutoCloseOnScreenLock", m_generalUi->autoCloseOnScreenLockCheckBox->isChecked()); + config()->set("GUI/Language", m_generalUi->languageComboBox->itemData(currentLangIndex).toString()); config()->set("GUI/ShowTrayIcon", m_generalUi->systrayShowCheckBox->isChecked()); diff --git a/src/gui/SettingsWidgetGeneral.ui b/src/gui/SettingsWidgetGeneral.ui index 2fe0f4089..ef947d3f0 100644 --- a/src/gui/SettingsWidgetGeneral.ui +++ b/src/gui/SettingsWidgetGeneral.ui @@ -367,6 +367,13 @@ + + + + Close database when session is locked or lid is closed + + + From c6ecf48ccdb5d8bc56c96707f39117c43afd45e7 Mon Sep 17 00:00:00 2001 From: Claudio Bantaloukas Date: Sat, 24 Dec 2016 01:05:24 +0100 Subject: [PATCH 02/13] enable OS X build with Xcode 8.2 --- .travis.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index e24d1d178..f3aeccf72 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,7 +5,9 @@ services: [docker] os: - linux -# - osx + - osx + +osx_image: xcode8.2 # Define clang compiler without any frills compiler: @@ -26,6 +28,7 @@ before_install: - if [ "$TRAVIS_OS_NAME" = "osx" ]; then brew ls | grep -wq cmake || brew install cmake; fi - if [ "$TRAVIS_OS_NAME" = "osx" ]; then brew ls | grep -wq qt5 || brew install qt5; fi - if [ "$TRAVIS_OS_NAME" = "osx" ]; then brew ls | grep -wq libgcrypt || brew install libgcrypt; fi + - if [ "$TRAVIS_OS_NAME" = "osx" ]; then brew ls | grep -wq libmicrohttpd || brew install libmicrohttpd; fi before_script: - if [ "$TRAVIS_OS_NAME" = "osx" ]; then CMAKE_ARGS="-DCMAKE_PREFIX_PATH=/usr/local/opt/qt5"; fi From a3af8fc0ea0de6e3464dd8910d894bfbfade4025 Mon Sep 17 00:00:00 2001 From: Claudio Bantaloukas Date: Sat, 31 Dec 2016 01:23:25 +0100 Subject: [PATCH 03/13] Fix Linux ScreenLockListener implementation --- src/core/ScreenLockListenerDBus.cpp | 2 +- src/core/ScreenLockListenerDBus.h | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/core/ScreenLockListenerDBus.cpp b/src/core/ScreenLockListenerDBus.cpp index 283b1e27e..4ac54a839 100644 --- a/src/core/ScreenLockListenerDBus.cpp +++ b/src/core/ScreenLockListenerDBus.cpp @@ -5,7 +5,7 @@ #include ScreenLockListenerDBus::ScreenLockListenerDBus(QWidget *parent): - ScreenLockListenerMac(parent) + ScreenLockListenerPrivate(parent) { QDBusConnection sessionBus = QDBusConnection::sessionBus(); QDBusConnection systemBus = QDBusConnection::systemBus(); diff --git a/src/core/ScreenLockListenerDBus.h b/src/core/ScreenLockListenerDBus.h index 016932b8a..be6fcd5f0 100644 --- a/src/core/ScreenLockListenerDBus.h +++ b/src/core/ScreenLockListenerDBus.h @@ -2,6 +2,7 @@ #define SCREENLOCKLISTENERDBUS_H #include #include +#include "ScreenLockListenerPrivate.h" class ScreenLockListenerDBus : public ScreenLockListenerPrivate { From 44085df592a7de66d01eb6ea557964905598fc09 Mon Sep 17 00:00:00 2001 From: Claudio Bantaloukas Date: Sat, 31 Dec 2016 01:32:29 +0100 Subject: [PATCH 04/13] Avoid warning in MacOS implementation --- src/core/ScreenLockListenerPrivate.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/core/ScreenLockListenerPrivate.cpp b/src/core/ScreenLockListenerPrivate.cpp index e9355c7d0..6f3f7bbf8 100644 --- a/src/core/ScreenLockListenerPrivate.cpp +++ b/src/core/ScreenLockListenerPrivate.cpp @@ -15,6 +15,7 @@ ScreenLockListenerPrivate::ScreenLockListenerPrivate(QWidget* parent): ScreenLockListenerPrivate* ScreenLockListenerPrivate::instance(QWidget* parent){ #if defined(Q_OS_OSX) + Q_UNUSED(parent); return ScreenLockListenerMac::instance(); #endif #if defined(Q_OS_LINUX) From d1acd750684b808e447c17e4b0b5125ed4a4ba95 Mon Sep 17 00:00:00 2001 From: Claudio Bantaloukas Date: Sun, 1 Jan 2017 14:08:14 +0100 Subject: [PATCH 05/13] Moved "Lock databases on screen lock" setting to security settings widget. Changed wording and preference variable name for conformity with existing settings. --- src/gui/MainWindow.cpp | 2 +- src/gui/SettingsWidget.cpp | 4 ++-- src/gui/SettingsWidgetSecurity.ui | 7 +++++++ 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/gui/MainWindow.cpp b/src/gui/MainWindow.cpp index 65062fb79..3d5fad919 100644 --- a/src/gui/MainWindow.cpp +++ b/src/gui/MainWindow.cpp @@ -965,7 +965,7 @@ void MainWindow::hideYubiKeyPopup() void MainWindow::handleScreenLock() { - if (config()->get("AutoCloseOnScreenLock").toBool()){ + if (config()->get("security/lockdatabasescreenlock").toBool()){ lockDatabasesAfterInactivity(); } } diff --git a/src/gui/SettingsWidget.cpp b/src/gui/SettingsWidget.cpp index 15f812a0d..75ebf2601 100644 --- a/src/gui/SettingsWidget.cpp +++ b/src/gui/SettingsWidget.cpp @@ -141,7 +141,6 @@ void SettingsWidget::loadSettings() } } - m_generalUi->autoCloseOnScreenLockCheckBox->setChecked(config()->get("AutoCloseOnScreenLock").toBool()); m_secUi->clearClipboardCheckBox->setChecked(config()->get("security/clearclipboard").toBool()); m_secUi->clearClipboardSpinBox->setValue(config()->get("security/clearclipboardtimeout").toInt()); @@ -149,6 +148,7 @@ void SettingsWidget::loadSettings() m_secUi->lockDatabaseIdleCheckBox->setChecked(config()->get("security/lockdatabaseidle").toBool()); m_secUi->lockDatabaseIdleSpinBox->setValue(config()->get("security/lockdatabaseidlesec").toInt()); m_secUi->lockDatabaseMinimizeCheckBox->setChecked(config()->get("security/lockdatabaseminimize").toBool()); + m_secUi->lockDatabaseOnScreenLockCheckBox->setChecked(config()->get("security/lockdatabasescreenlock").toBool()); m_secUi->passwordCleartextCheckBox->setChecked(config()->get("security/passwordscleartext").toBool()); m_secUi->passwordRepeatCheckBox->setChecked(config()->get("security/passwordsrepeat").toBool()); @@ -188,7 +188,6 @@ void SettingsWidget::saveSettings() config()->set("AutoTypeEntryTitleMatch", m_generalUi->autoTypeEntryTitleMatchCheckBox->isChecked()); int currentLangIndex = m_generalUi->languageComboBox->currentIndex(); - config()->set("AutoCloseOnScreenLock", m_generalUi->autoCloseOnScreenLockCheckBox->isChecked()); config()->set("GUI/Language", m_generalUi->languageComboBox->itemData(currentLangIndex).toString()); @@ -210,6 +209,7 @@ void SettingsWidget::saveSettings() config()->set("security/lockdatabaseidle", m_secUi->lockDatabaseIdleCheckBox->isChecked()); config()->set("security/lockdatabaseidlesec", m_secUi->lockDatabaseIdleSpinBox->value()); config()->set("security/lockdatabaseminimize", m_secUi->lockDatabaseMinimizeCheckBox->isChecked()); + config()->set("security/lockdatabasescreenlock", m_secUi->lockDatabaseOnScreenLockCheckBox->isChecked()); config()->set("security/passwordscleartext", m_secUi->passwordCleartextCheckBox->isChecked()); config()->set("security/passwordsrepeat", m_secUi->passwordRepeatCheckBox->isChecked()); diff --git a/src/gui/SettingsWidgetSecurity.ui b/src/gui/SettingsWidgetSecurity.ui index 08fa6f4ea..93e3e9114 100644 --- a/src/gui/SettingsWidgetSecurity.ui +++ b/src/gui/SettingsWidgetSecurity.ui @@ -142,6 +142,13 @@ + + + + Lock databases when session is locked or lid is closed + + + From 289e98ed5b0e542aa3347f8ce2ef440ace6f3a0d Mon Sep 17 00:00:00 2001 From: Claudio Bantaloukas Date: Sun, 1 Jan 2017 14:11:31 +0100 Subject: [PATCH 06/13] remove commented code --- src/core/ScreenLockListenerDBus.cpp | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/src/core/ScreenLockListenerDBus.cpp b/src/core/ScreenLockListenerDBus.cpp index 4ac54a839..3edb59577 100644 --- a/src/core/ScreenLockListenerDBus.cpp +++ b/src/core/ScreenLockListenerDBus.cpp @@ -32,18 +32,6 @@ ScreenLockListenerDBus::ScreenLockListenerDBus(QWidget *parent): "Locked", // signal name this, //receiver SLOT(unityLocked())); - - /* Currently unable to get the current user session from login1.Manager - QDBusInterface login1_manager_iface("org.freedesktop.login1", "/org/freedesktop/login1", - "org.freedesktop.login1.Manager", systemBus); - if(login1_manager_iface.isValid()){ - qint64 my_pid = QCoreApplication::applicationPid(); - QDBusReply reply = login1_manager_iface.call("GetSessionByPID",static_cast(my_pid)); - if (reply.isValid()){ - QString current_session = reply.value(); - } - } - */ } void ScreenLockListenerDBus::gnomeSessionStatusChanged(uint status) From 1b7b2ff456dca5b17d6c25ccfc368170950bf43c Mon Sep 17 00:00:00 2001 From: thez3ro Date: Thu, 4 May 2017 22:52:10 +0200 Subject: [PATCH 07/13] Added freedesktop DBus, fixed codestyle --- src/CMakeLists.txt | 7 +++++++ src/core/Config.cpp | 1 + src/core/ScreenLockListenerDBus.cpp | 20 ++++++++++++++++++-- src/core/ScreenLockListenerDBus.h | 1 + src/core/ScreenLockListenerMac.cpp | 16 ++++++++++------ src/core/ScreenLockListenerPrivate.cpp | 8 +++++--- src/core/ScreenLockListenerWin.cpp | 26 +++++++++++++------------- src/gui/SettingsWidgetSecurity.ui | 14 +++++++------- 8 files changed, 62 insertions(+), 31 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index dfdf96c8a..9dfa52679 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -222,9 +222,16 @@ target_link_libraries(keepassx_core ${GCRYPT_LIBRARIES} ${GPGERROR_LIBRARIES} ${ZLIB_LIBRARIES}) + +if(APPLE) + target_link_libraries(keepassx_core "-framework Foundation") +endif() if (UNIX AND NOT APPLE) target_link_libraries(keepassx_core Qt5::DBus) endif() +if(MINGW) + target_link_libraries(keepassx_core Wtsapi32.lib) +endif() if(MINGW) include(GenerateProductVersion) diff --git a/src/core/Config.cpp b/src/core/Config.cpp index c0876daa5..f0a369c30 100644 --- a/src/core/Config.cpp +++ b/src/core/Config.cpp @@ -119,6 +119,7 @@ void Config::init(const QString& fileName) m_defaults.insert("security/lockdatabaseidle", false); m_defaults.insert("security/lockdatabaseidlesec", 240); m_defaults.insert("security/lockdatabaseminimize", false); + m_defaults.insert("security/lockdatabasescreenlock", true); m_defaults.insert("security/passwordsrepeat", false); m_defaults.insert("security/passwordscleartext", false); m_defaults.insert("security/autotypeask", true); diff --git a/src/core/ScreenLockListenerDBus.cpp b/src/core/ScreenLockListenerDBus.cpp index 3edb59577..2b4de9da3 100644 --- a/src/core/ScreenLockListenerDBus.cpp +++ b/src/core/ScreenLockListenerDBus.cpp @@ -9,6 +9,15 @@ ScreenLockListenerDBus::ScreenLockListenerDBus(QWidget *parent): { QDBusConnection sessionBus = QDBusConnection::sessionBus(); QDBusConnection systemBus = QDBusConnection::systemBus(); + + sessionBus.connect( + "org.freedesktop.ScreenSaver", // service + "/org/freedesktop/ScreenSaver", // path + "org.freedesktop.ScreenSaver", // interface + "ActiveChanged", // signal name + this, //receiver + SLOT(freedesktopScreenSaver(bool))); + sessionBus.connect( "org.gnome.SessionManager", // service "/org/gnome/SessionManager/Presence", // path @@ -36,14 +45,14 @@ ScreenLockListenerDBus::ScreenLockListenerDBus(QWidget *parent): void ScreenLockListenerDBus::gnomeSessionStatusChanged(uint status) { - if(status != 0){ + if (status != 0) { Q_EMIT screenLocked(); } } void ScreenLockListenerDBus::logindPrepareForSleep(bool beforeSleep) { - if(beforeSleep){ + if (beforeSleep) { Q_EMIT screenLocked(); } } @@ -52,3 +61,10 @@ void ScreenLockListenerDBus::unityLocked() { Q_EMIT screenLocked(); } + +void ScreenLockListenerDBus::freedesktopScreenSaver(bool status) +{ + if (status) { + Q_EMIT screenLocked(); + } +} \ No newline at end of file diff --git a/src/core/ScreenLockListenerDBus.h b/src/core/ScreenLockListenerDBus.h index be6fcd5f0..0c49323f7 100644 --- a/src/core/ScreenLockListenerDBus.h +++ b/src/core/ScreenLockListenerDBus.h @@ -14,6 +14,7 @@ private Q_SLOTS: void gnomeSessionStatusChanged(uint status); void logindPrepareForSleep(bool beforeSleep); void unityLocked(); + void freedesktopScreenSaver(bool status); }; #endif // SCREENLOCKLISTENERDBUS_H diff --git a/src/core/ScreenLockListenerMac.cpp b/src/core/ScreenLockListenerMac.cpp index 57dc2de3f..7be9dd2f2 100644 --- a/src/core/ScreenLockListenerMac.cpp +++ b/src/core/ScreenLockListenerMac.cpp @@ -3,29 +3,33 @@ #include #include -ScreenLockListenerMac* ScreenLockListenerMac::instance(){ +ScreenLockListenerMac* ScreenLockListenerMac::instance() +{ static QMutex mutex; QMutexLocker lock(&mutex); static ScreenLockListenerMac* m_ptr=NULL; - if (m_ptr==NULL){ + if (m_ptr == NULL) { m_ptr = new ScreenLockListenerMac(); } return m_ptr; } void ScreenLockListenerMac::notificationCenterCallBack(CFNotificationCenterRef /*center*/, void */*observer*/, - CFNotificationName /*name*/, const void */*object*/, CFDictionaryRef /*userInfo*/){ + CFNotificationName /*name*/, const void */*object*/, CFDictionaryRef /*userInfo*/) +{ instance()->onSignalReception(); } -ScreenLockListenerMac::ScreenLockListenerMac(QWidget* parent): - ScreenLockListenerPrivate(parent){ +ScreenLockListenerMac::ScreenLockListenerMac(QWidget* parent) + : ScreenLockListenerPrivate(parent) +{ CFNotificationCenterRef distCenter; CFStringRef screenIsLockedSignal = CFSTR("com.apple.screenIsLocked"); distCenter = CFNotificationCenterGetDistributedCenter(); - if (NULL == distCenter) + if (NULL == distCenter) { return; + } CFNotificationCenterAddObserver( distCenter, diff --git a/src/core/ScreenLockListenerPrivate.cpp b/src/core/ScreenLockListenerPrivate.cpp index 6f3f7bbf8..e0c6d234c 100644 --- a/src/core/ScreenLockListenerPrivate.cpp +++ b/src/core/ScreenLockListenerPrivate.cpp @@ -9,11 +9,13 @@ #include "ScreenLockListenerWin.h" #endif -ScreenLockListenerPrivate::ScreenLockListenerPrivate(QWidget* parent): - QObject(parent){ +ScreenLockListenerPrivate::ScreenLockListenerPrivate(QWidget* parent) + : QObject(parent) +{ } -ScreenLockListenerPrivate* ScreenLockListenerPrivate::instance(QWidget* parent){ +ScreenLockListenerPrivate* ScreenLockListenerPrivate::instance(QWidget* parent) +{ #if defined(Q_OS_OSX) Q_UNUSED(parent); return ScreenLockListenerMac::instance(); diff --git a/src/core/ScreenLockListenerWin.cpp b/src/core/ScreenLockListenerWin.cpp index 349d5de58..21c59c723 100644 --- a/src/core/ScreenLockListenerWin.cpp +++ b/src/core/ScreenLockListenerWin.cpp @@ -8,11 +8,11 @@ * See https://msdn.microsoft.com/en-us/library/aa383841(v=vs.85).aspx * See https://blogs.msdn.microsoft.com/oldnewthing/20060104-50/?p=32783 */ -ScreenLockListenerWin::ScreenLockListenerWin(QWidget *parent) : - ScreenLockListenerPrivate(parent), - QAbstractNativeEventFilter() +ScreenLockListenerWin::ScreenLockListenerWin(QWidget *parent) + : ScreenLockListenerPrivate(parent) + , QAbstractNativeEventFilter() { - Q_ASSERT(parent!=NULL); + Q_ASSERT(parent != NULL); // On windows, we need to register for platform specific messages and // install a message handler for them QCoreApplication::instance()->installNativeEventFilter(this); @@ -26,7 +26,7 @@ ScreenLockListenerWin::ScreenLockListenerWin(QWidget *parent) : // This call requests a notification for session changes if (!WTSRegisterSessionNotification( reinterpret_cast(parent->winId()), - NOTIFY_FOR_THIS_SESSION)){ + NOTIFY_FOR_THIS_SESSION)) { } } @@ -35,31 +35,31 @@ ScreenLockListenerWin::~ScreenLockListenerWin() HWND h= reinterpret_cast(static_cast(parent())->winId()); WTSUnRegisterSessionNotification(h); - if(m_powernotificationhandle){ + if (m_powernotificationhandle) { UnregisterPowerSettingNotification(reinterpret_cast(m_powernotificationhandle)); } } bool ScreenLockListenerWin::nativeEventFilter(const QByteArray &eventType, void *message, long *) { - if(eventType == "windows_generic_MSG" || eventType == "windows_dispatcher_MSG"){ + if (eventType == "windows_generic_MSG" || eventType == "windows_dispatcher_MSG") { MSG* m = static_cast(message); - if(m->message == WM_POWERBROADCAST){ + if (m->message == WM_POWERBROADCAST) { const POWERBROADCAST_SETTING* setting = reinterpret_cast(m->lParam); - if (setting->PowerSetting == GUID_LIDSWITCH_STATE_CHANGE){ + if (setting->PowerSetting == GUID_LIDSWITCH_STATE_CHANGE) { const DWORD* state = reinterpret_cast(&setting->Data); - if (*state == 0){ + if (*state == 0) { Q_EMIT screenLocked(); return true; } } } - if(m->message == WM_WTSSESSION_CHANGE){ - if (m->wParam==WTS_CONSOLE_DISCONNECT){ + if (m->message == WM_WTSSESSION_CHANGE) { + if (m->wParam == WTS_CONSOLE_DISCONNECT) { Q_EMIT screenLocked(); return true; } - if (m->wParam==WTS_SESSION_LOCK){ + if (m->wParam == WTS_SESSION_LOCK) { Q_EMIT screenLocked(); return true; } diff --git a/src/gui/SettingsWidgetSecurity.ui b/src/gui/SettingsWidgetSecurity.ui index 93e3e9114..30ab2ecfc 100644 --- a/src/gui/SettingsWidgetSecurity.ui +++ b/src/gui/SettingsWidgetSecurity.ui @@ -123,6 +123,13 @@ + + + + Lock databases when session is locked or lid is closed + + + @@ -142,13 +149,6 @@ - - - - Lock databases when session is locked or lid is closed - - - From 8ddd0b2f6f12e248aa042155bd298bf6c0060901 Mon Sep 17 00:00:00 2001 From: thez3ro Date: Thu, 4 May 2017 23:09:08 +0200 Subject: [PATCH 08/13] Revert travis settings --- .travis.yml | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index f3aeccf72..e24d1d178 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,9 +5,7 @@ services: [docker] os: - linux - - osx - -osx_image: xcode8.2 +# - osx # Define clang compiler without any frills compiler: @@ -28,7 +26,6 @@ before_install: - if [ "$TRAVIS_OS_NAME" = "osx" ]; then brew ls | grep -wq cmake || brew install cmake; fi - if [ "$TRAVIS_OS_NAME" = "osx" ]; then brew ls | grep -wq qt5 || brew install qt5; fi - if [ "$TRAVIS_OS_NAME" = "osx" ]; then brew ls | grep -wq libgcrypt || brew install libgcrypt; fi - - if [ "$TRAVIS_OS_NAME" = "osx" ]; then brew ls | grep -wq libmicrohttpd || brew install libmicrohttpd; fi before_script: - if [ "$TRAVIS_OS_NAME" = "osx" ]; then CMAKE_ARGS="-DCMAKE_PREFIX_PATH=/usr/local/opt/qt5"; fi From 147c000ef1478e305ed78e525a1184c2e08910a7 Mon Sep 17 00:00:00 2001 From: Jonathan White Date: Sat, 6 May 2017 22:14:53 -0400 Subject: [PATCH 09/13] Corrected nullptr crash on Windows when going to sleep --- src/core/ScreenLockListenerWin.cpp | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/core/ScreenLockListenerWin.cpp b/src/core/ScreenLockListenerWin.cpp index 21c59c723..4aae8a28d 100644 --- a/src/core/ScreenLockListenerWin.cpp +++ b/src/core/ScreenLockListenerWin.cpp @@ -45,13 +45,18 @@ bool ScreenLockListenerWin::nativeEventFilter(const QByteArray &eventType, void if (eventType == "windows_generic_MSG" || eventType == "windows_dispatcher_MSG") { MSG* m = static_cast(message); if (m->message == WM_POWERBROADCAST) { - const POWERBROADCAST_SETTING* setting = reinterpret_cast(m->lParam); - if (setting->PowerSetting == GUID_LIDSWITCH_STATE_CHANGE) { - const DWORD* state = reinterpret_cast(&setting->Data); - if (*state == 0) { - Q_EMIT screenLocked(); - return true; + if (m->wParam == PBT_POWERSETTINGCHANGE) { + const POWERBROADCAST_SETTING* setting = reinterpret_cast(m->lParam); + if (setting != nullptr && setting->PowerSetting == GUID_LIDSWITCH_STATE_CHANGE) { + const DWORD* state = reinterpret_cast(&setting->Data); + if (*state == 0) { + Q_EMIT screenLocked(); + return true; + } } + } else if (m->wParam == PBT_APMSUSPEND) { + Q_EMIT screenLocked(); + return true; } } if (m->message == WM_WTSSESSION_CHANGE) { From 3218cb9ace1e3fb32e8cc2d74b2b839a920276bc Mon Sep 17 00:00:00 2001 From: Jonathan White Date: Sat, 6 May 2017 22:34:12 -0400 Subject: [PATCH 10/13] Moved locking checkboxes into security settings --- src/gui/SettingsWidgetGeneral.ui | 7 ---- src/gui/SettingsWidgetSecurity.ui | 62 +++++++++++++++++-------------- 2 files changed, 34 insertions(+), 35 deletions(-) diff --git a/src/gui/SettingsWidgetGeneral.ui b/src/gui/SettingsWidgetGeneral.ui index ef947d3f0..2fe0f4089 100644 --- a/src/gui/SettingsWidgetGeneral.ui +++ b/src/gui/SettingsWidgetGeneral.ui @@ -367,13 +367,6 @@ - - - - Close database when session is locked or lid is closed - - - diff --git a/src/gui/SettingsWidgetSecurity.ui b/src/gui/SettingsWidgetSecurity.ui index 30ab2ecfc..679c470ad 100644 --- a/src/gui/SettingsWidgetSecurity.ui +++ b/src/gui/SettingsWidgetSecurity.ui @@ -28,7 +28,14 @@ Timeouts - + + + + + Clear clipboard after + + + @@ -54,7 +61,20 @@ - + + + + + 0 + 0 + + + + Lock databases after inactivity of + + + + false @@ -79,20 +99,6 @@ - - - - Clear clipboard after - - - - - - - Lock databases after inactivity of - - - @@ -101,18 +107,11 @@ Convenience - + - + - Don't require password repeat when it is visible - - - - - - - Show passwords in cleartext by default + Lock databases when session is locked or lid is closed @@ -124,9 +123,16 @@ - + - Lock databases when session is locked or lid is closed + Don't require password repeat when it is visible + + + + + + + Show passwords in cleartext by default From 533136fb0eb0391a294f742ddfb099adb69cec92 Mon Sep 17 00:00:00 2001 From: thez3ro Date: Mon, 8 May 2017 16:03:19 +0200 Subject: [PATCH 11/13] Add file header, use nullptr instead of NULL, fix code style --- src/core/ScreenLockListener.cpp | 17 ++++++++++++++++ src/core/ScreenLockListener.h | 19 +++++++++++++++++- src/core/ScreenLockListenerDBus.cpp | 17 ++++++++++++++++ src/core/ScreenLockListenerDBus.h | 17 ++++++++++++++++ src/core/ScreenLockListenerMac.cpp | 25 ++++++++++++++++++++---- src/core/ScreenLockListenerMac.h | 19 +++++++++++++++++- src/core/ScreenLockListenerPrivate.cpp | 17 ++++++++++++++++ src/core/ScreenLockListenerPrivate.h | 21 ++++++++++++++++++-- src/core/ScreenLockListenerWin.cpp | 27 +++++++++++++++++++++----- src/core/ScreenLockListenerWin.h | 23 +++++++++++++++++++--- 10 files changed, 186 insertions(+), 16 deletions(-) diff --git a/src/core/ScreenLockListener.cpp b/src/core/ScreenLockListener.cpp index 2c17dfa14..eb78cd608 100644 --- a/src/core/ScreenLockListener.cpp +++ b/src/core/ScreenLockListener.cpp @@ -1,3 +1,20 @@ +/* + * Copyright (C) 2017 KeePassXC Team + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 or (at your option) + * version 3 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + #include "ScreenLockListener.h" #include "ScreenLockListenerPrivate.h" diff --git a/src/core/ScreenLockListener.h b/src/core/ScreenLockListener.h index 7339054f9..bc115d19c 100644 --- a/src/core/ScreenLockListener.h +++ b/src/core/ScreenLockListener.h @@ -1,3 +1,20 @@ +/* + * Copyright (C) 2017 KeePassXC Team + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 or (at your option) + * version 3 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + #ifndef SCREENLOCKLISTENER_H #define SCREENLOCKLISTENER_H #include @@ -8,7 +25,7 @@ class ScreenLockListener : public QObject { Q_OBJECT public: - ScreenLockListener(QWidget* parent=NULL); + ScreenLockListener(QWidget* parent = nullptr); ~ScreenLockListener(); Q_SIGNALS: diff --git a/src/core/ScreenLockListenerDBus.cpp b/src/core/ScreenLockListenerDBus.cpp index 2b4de9da3..e24e388a3 100644 --- a/src/core/ScreenLockListenerDBus.cpp +++ b/src/core/ScreenLockListenerDBus.cpp @@ -1,3 +1,20 @@ +/* + * Copyright (C) 2017 KeePassXC Team + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 or (at your option) + * version 3 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + #include "ScreenLockListenerDBus.h" #include diff --git a/src/core/ScreenLockListenerDBus.h b/src/core/ScreenLockListenerDBus.h index 0c49323f7..a907f20cc 100644 --- a/src/core/ScreenLockListenerDBus.h +++ b/src/core/ScreenLockListenerDBus.h @@ -1,3 +1,20 @@ +/* + * Copyright (C) 2017 KeePassXC Team + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 or (at your option) + * version 3 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + #ifndef SCREENLOCKLISTENERDBUS_H #define SCREENLOCKLISTENERDBUS_H #include diff --git a/src/core/ScreenLockListenerMac.cpp b/src/core/ScreenLockListenerMac.cpp index 7be9dd2f2..3917a888c 100644 --- a/src/core/ScreenLockListenerMac.cpp +++ b/src/core/ScreenLockListenerMac.cpp @@ -1,3 +1,20 @@ +/* + * Copyright (C) 2017 KeePassXC Team + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 or (at your option) + * version 3 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + #include "ScreenLockListenerMac.h" #include @@ -8,8 +25,8 @@ ScreenLockListenerMac* ScreenLockListenerMac::instance() static QMutex mutex; QMutexLocker lock(&mutex); - static ScreenLockListenerMac* m_ptr=NULL; - if (m_ptr == NULL) { + static ScreenLockListenerMac* m_ptr = nullptr; + if (m_ptr == nullptr) { m_ptr = new ScreenLockListenerMac(); } return m_ptr; @@ -27,7 +44,7 @@ ScreenLockListenerMac::ScreenLockListenerMac(QWidget* parent) CFNotificationCenterRef distCenter; CFStringRef screenIsLockedSignal = CFSTR("com.apple.screenIsLocked"); distCenter = CFNotificationCenterGetDistributedCenter(); - if (NULL == distCenter) { + if (nullptr == distCenter) { return; } @@ -35,7 +52,7 @@ ScreenLockListenerMac::ScreenLockListenerMac(QWidget* parent) distCenter, this, &ScreenLockListenerMac::notificationCenterCallBack, screenIsLockedSignal, - NULL, + nullptr, CFNotificationSuspensionBehaviorDeliverImmediately); } diff --git a/src/core/ScreenLockListenerMac.h b/src/core/ScreenLockListenerMac.h index 5824339f1..733cb3756 100644 --- a/src/core/ScreenLockListenerMac.h +++ b/src/core/ScreenLockListenerMac.h @@ -1,3 +1,20 @@ +/* + * Copyright (C) 2017 KeePassXC Team + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 or (at your option) + * version 3 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + #ifndef SCREENLOCKLISTENERMAC_H #define SCREENLOCKLISTENERMAC_H #include @@ -16,7 +33,7 @@ public: CFNotificationName /*name*/, const void */*object*/, CFDictionaryRef /*userInfo*/); private: - ScreenLockListenerMac(QWidget* parent=NULL); + ScreenLockListenerMac(QWidget* parent = nullptr); void onSignalReception(); }; diff --git a/src/core/ScreenLockListenerPrivate.cpp b/src/core/ScreenLockListenerPrivate.cpp index e0c6d234c..9a0ebd69b 100644 --- a/src/core/ScreenLockListenerPrivate.cpp +++ b/src/core/ScreenLockListenerPrivate.cpp @@ -1,3 +1,20 @@ +/* + * Copyright (C) 2017 KeePassXC Team + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 or (at your option) + * version 3 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + #include "ScreenLockListenerPrivate.h" #if defined(Q_OS_OSX) #include "ScreenLockListenerMac.h" diff --git a/src/core/ScreenLockListenerPrivate.h b/src/core/ScreenLockListenerPrivate.h index aef20edcc..781d64527 100644 --- a/src/core/ScreenLockListenerPrivate.h +++ b/src/core/ScreenLockListenerPrivate.h @@ -1,3 +1,20 @@ +/* + * Copyright (C) 2017 KeePassXC Team + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 or (at your option) + * version 3 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + #ifndef SCREENLOCKLISTENERPRIVATE_H #define SCREENLOCKLISTENERPRIVATE_H #include @@ -7,10 +24,10 @@ class ScreenLockListenerPrivate : public QObject { Q_OBJECT public: - static ScreenLockListenerPrivate* instance(QWidget *parent = 0); + static ScreenLockListenerPrivate* instance(QWidget* parent = 0); protected: - ScreenLockListenerPrivate(QWidget *parent = 0); + ScreenLockListenerPrivate(QWidget* parent = 0); Q_SIGNALS: void screenLocked(); diff --git a/src/core/ScreenLockListenerWin.cpp b/src/core/ScreenLockListenerWin.cpp index 4aae8a28d..3e7950abc 100644 --- a/src/core/ScreenLockListenerWin.cpp +++ b/src/core/ScreenLockListenerWin.cpp @@ -1,3 +1,20 @@ +/* + * Copyright (C) 2017 KeePassXC Team + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 or (at your option) + * version 3 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + #include "ScreenLockListenerWin.h" #include #include @@ -8,11 +25,11 @@ * See https://msdn.microsoft.com/en-us/library/aa383841(v=vs.85).aspx * See https://blogs.msdn.microsoft.com/oldnewthing/20060104-50/?p=32783 */ -ScreenLockListenerWin::ScreenLockListenerWin(QWidget *parent) +ScreenLockListenerWin::ScreenLockListenerWin(QWidget* parent) : ScreenLockListenerPrivate(parent) , QAbstractNativeEventFilter() { - Q_ASSERT(parent != NULL); + Q_ASSERT(parent != nullptr); // On windows, we need to register for platform specific messages and // install a message handler for them QCoreApplication::instance()->installNativeEventFilter(this); @@ -21,7 +38,7 @@ ScreenLockListenerWin::ScreenLockListenerWin(QWidget *parent) HPOWERNOTIFY hPnotify = RegisterPowerSettingNotification( reinterpret_cast(parent->winId()), &GUID_LIDSWITCH_STATE_CHANGE, DEVICE_NOTIFY_WINDOW_HANDLE); - m_powernotificationhandle = reinterpret_cast(hPnotify); + m_powerNotificationHandle = reinterpret_cast(hPnotify); // This call requests a notification for session changes if (!WTSRegisterSessionNotification( @@ -40,10 +57,10 @@ ScreenLockListenerWin::~ScreenLockListenerWin() } } -bool ScreenLockListenerWin::nativeEventFilter(const QByteArray &eventType, void *message, long *) +bool ScreenLockListenerWin::nativeEventFilter(const QByteArray &eventType, void* message, long *) { if (eventType == "windows_generic_MSG" || eventType == "windows_dispatcher_MSG") { - MSG* m = static_cast(message); + MSG* m = static_cast(message); if (m->message == WM_POWERBROADCAST) { if (m->wParam == PBT_POWERSETTINGCHANGE) { const POWERBROADCAST_SETTING* setting = reinterpret_cast(m->lParam); diff --git a/src/core/ScreenLockListenerWin.h b/src/core/ScreenLockListenerWin.h index ccf42ad70..0a1c37f91 100644 --- a/src/core/ScreenLockListenerWin.h +++ b/src/core/ScreenLockListenerWin.h @@ -1,3 +1,20 @@ +/* + * Copyright (C) 2017 KeePassXC Team + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 or (at your option) + * version 3 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + #ifndef SCREENLOCKLISTENERWIN_H #define SCREENLOCKLISTENERWIN_H #include @@ -10,12 +27,12 @@ class ScreenLockListenerWin : public ScreenLockListenerPrivate, public QAbstract { Q_OBJECT public: - explicit ScreenLockListenerWin(QWidget *parent = 0); + explicit ScreenLockListenerWin(QWidget* parent = 0); ~ScreenLockListenerWin(); - virtual bool nativeEventFilter(const QByteArray &eventType, void *message, long *) Q_DECL_OVERRIDE; + virtual bool nativeEventFilter(const QByteArray &eventType, void* message, long *) override; private: - void * m_powernotificationhandle; + void* m_powerNotificationHandle ; }; #endif // SCREENLOCKLISTENERWIN_H From 970525cfd4c5714ce19516517254138153ca2b59 Mon Sep 17 00:00:00 2001 From: Louis-Bertrand Varin Date: Wed, 10 May 2017 11:05:17 -0400 Subject: [PATCH 12/13] Styling + CFNotificationName -> CFStringRef --- src/core/ScreenLockListenerMac.cpp | 17 +++++++++-------- src/core/ScreenLockListenerMac.h | 5 +++-- src/core/ScreenLockListenerWin.cpp | 2 +- src/core/ScreenLockListenerWin.h | 2 +- 4 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/core/ScreenLockListenerMac.cpp b/src/core/ScreenLockListenerMac.cpp index 3917a888c..d919adb6c 100644 --- a/src/core/ScreenLockListenerMac.cpp +++ b/src/core/ScreenLockListenerMac.cpp @@ -32,8 +32,9 @@ ScreenLockListenerMac* ScreenLockListenerMac::instance() return m_ptr; } -void ScreenLockListenerMac::notificationCenterCallBack(CFNotificationCenterRef /*center*/, void */*observer*/, - CFNotificationName /*name*/, const void */*object*/, CFDictionaryRef /*userInfo*/) +void ScreenLockListenerMac::notificationCenterCallBack(CFNotificationCenterRef, void*, + CFStringRef, const void*, + CFDictionaryRef) { instance()->onSignalReception(); } @@ -48,12 +49,12 @@ ScreenLockListenerMac::ScreenLockListenerMac(QWidget* parent) return; } - CFNotificationCenterAddObserver( - distCenter, - this, &ScreenLockListenerMac::notificationCenterCallBack, - screenIsLockedSignal, - nullptr, - CFNotificationSuspensionBehaviorDeliverImmediately); + CFNotificationCenterAddObserver(distCenter, + this, + &ScreenLockListenerMac::notificationCenterCallBack, + screenIsLockedSignal, + nullptr, + CFNotificationSuspensionBehaviorDeliverImmediately); } void ScreenLockListenerMac::onSignalReception() diff --git a/src/core/ScreenLockListenerMac.h b/src/core/ScreenLockListenerMac.h index 733cb3756..cd36ce9e6 100644 --- a/src/core/ScreenLockListenerMac.h +++ b/src/core/ScreenLockListenerMac.h @@ -29,8 +29,9 @@ class ScreenLockListenerMac: public ScreenLockListenerPrivate { public: static ScreenLockListenerMac* instance(); - static void notificationCenterCallBack(CFNotificationCenterRef /*center*/, void */*observer*/, - CFNotificationName /*name*/, const void */*object*/, CFDictionaryRef /*userInfo*/); + static void notificationCenterCallBack(CFNotificationCenterRef center, void* observer, + CFStringRef name, const void* object, + CFDictionaryRef userInfo); private: ScreenLockListenerMac(QWidget* parent = nullptr); diff --git a/src/core/ScreenLockListenerWin.cpp b/src/core/ScreenLockListenerWin.cpp index 3e7950abc..9429393fe 100644 --- a/src/core/ScreenLockListenerWin.cpp +++ b/src/core/ScreenLockListenerWin.cpp @@ -57,7 +57,7 @@ ScreenLockListenerWin::~ScreenLockListenerWin() } } -bool ScreenLockListenerWin::nativeEventFilter(const QByteArray &eventType, void* message, long *) +bool ScreenLockListenerWin::nativeEventFilter(const QByteArray& eventType, void* message, long*) { if (eventType == "windows_generic_MSG" || eventType == "windows_dispatcher_MSG") { MSG* m = static_cast(message); diff --git a/src/core/ScreenLockListenerWin.h b/src/core/ScreenLockListenerWin.h index 0a1c37f91..6a8380efe 100644 --- a/src/core/ScreenLockListenerWin.h +++ b/src/core/ScreenLockListenerWin.h @@ -29,7 +29,7 @@ class ScreenLockListenerWin : public ScreenLockListenerPrivate, public QAbstract public: explicit ScreenLockListenerWin(QWidget* parent = 0); ~ScreenLockListenerWin(); - virtual bool nativeEventFilter(const QByteArray &eventType, void* message, long *) override; + virtual bool nativeEventFilter(const QByteArray &eventType, void* message, long*) override; private: void* m_powerNotificationHandle ; From b69b50c6c6f2152cedf4528af24beed6179bcbbe Mon Sep 17 00:00:00 2001 From: thez3ro Date: Wed, 17 May 2017 13:03:51 +0200 Subject: [PATCH 13/13] fix codestyle and use C++11 keywords --- src/core/ScreenLockListener.h | 2 +- src/core/ScreenLockListenerDBus.cpp | 8 ++++---- src/core/ScreenLockListenerDBus.h | 2 +- src/core/ScreenLockListenerMac.cpp | 2 +- src/core/ScreenLockListenerPrivate.cpp | 6 ++---- src/core/ScreenLockListenerPrivate.h | 2 +- src/core/ScreenLockListenerWin.cpp | 8 ++++---- 7 files changed, 14 insertions(+), 16 deletions(-) diff --git a/src/core/ScreenLockListener.h b/src/core/ScreenLockListener.h index bc115d19c..b4eb81e04 100644 --- a/src/core/ScreenLockListener.h +++ b/src/core/ScreenLockListener.h @@ -28,7 +28,7 @@ public: ScreenLockListener(QWidget* parent = nullptr); ~ScreenLockListener(); -Q_SIGNALS: +signals: void screenLocked(); private: diff --git a/src/core/ScreenLockListenerDBus.cpp b/src/core/ScreenLockListenerDBus.cpp index e24e388a3..1976b47ea 100644 --- a/src/core/ScreenLockListenerDBus.cpp +++ b/src/core/ScreenLockListenerDBus.cpp @@ -63,25 +63,25 @@ ScreenLockListenerDBus::ScreenLockListenerDBus(QWidget *parent): void ScreenLockListenerDBus::gnomeSessionStatusChanged(uint status) { if (status != 0) { - Q_EMIT screenLocked(); + emit screenLocked(); } } void ScreenLockListenerDBus::logindPrepareForSleep(bool beforeSleep) { if (beforeSleep) { - Q_EMIT screenLocked(); + emit screenLocked(); } } void ScreenLockListenerDBus::unityLocked() { - Q_EMIT screenLocked(); + emit screenLocked(); } void ScreenLockListenerDBus::freedesktopScreenSaver(bool status) { if (status) { - Q_EMIT screenLocked(); + emit screenLocked(); } } \ No newline at end of file diff --git a/src/core/ScreenLockListenerDBus.h b/src/core/ScreenLockListenerDBus.h index a907f20cc..72f308f72 100644 --- a/src/core/ScreenLockListenerDBus.h +++ b/src/core/ScreenLockListenerDBus.h @@ -27,7 +27,7 @@ class ScreenLockListenerDBus : public ScreenLockListenerPrivate public: explicit ScreenLockListenerDBus(QWidget *parent = 0); -private Q_SLOTS: +private slots: void gnomeSessionStatusChanged(uint status); void logindPrepareForSleep(bool beforeSleep); void unityLocked(); diff --git a/src/core/ScreenLockListenerMac.cpp b/src/core/ScreenLockListenerMac.cpp index d919adb6c..dce05de3f 100644 --- a/src/core/ScreenLockListenerMac.cpp +++ b/src/core/ScreenLockListenerMac.cpp @@ -59,5 +59,5 @@ ScreenLockListenerMac::ScreenLockListenerMac(QWidget* parent) void ScreenLockListenerMac::onSignalReception() { - Q_EMIT screenLocked(); + emit screenLocked(); } diff --git a/src/core/ScreenLockListenerPrivate.cpp b/src/core/ScreenLockListenerPrivate.cpp index 9a0ebd69b..36ee301f2 100644 --- a/src/core/ScreenLockListenerPrivate.cpp +++ b/src/core/ScreenLockListenerPrivate.cpp @@ -36,11 +36,9 @@ ScreenLockListenerPrivate* ScreenLockListenerPrivate::instance(QWidget* parent) #if defined(Q_OS_OSX) Q_UNUSED(parent); return ScreenLockListenerMac::instance(); -#endif -#if defined(Q_OS_LINUX) +#elif defined(Q_OS_LINUX) return new ScreenLockListenerDBus(parent); -#endif -#if defined(Q_OS_WIN) +#elif defined(Q_OS_WIN) return new ScreenLockListenerWin(parent); #endif } diff --git a/src/core/ScreenLockListenerPrivate.h b/src/core/ScreenLockListenerPrivate.h index 781d64527..8ecad17d8 100644 --- a/src/core/ScreenLockListenerPrivate.h +++ b/src/core/ScreenLockListenerPrivate.h @@ -29,7 +29,7 @@ public: protected: ScreenLockListenerPrivate(QWidget* parent = 0); -Q_SIGNALS: +signals: void screenLocked(); }; diff --git a/src/core/ScreenLockListenerWin.cpp b/src/core/ScreenLockListenerWin.cpp index 9429393fe..a1bf13d4f 100644 --- a/src/core/ScreenLockListenerWin.cpp +++ b/src/core/ScreenLockListenerWin.cpp @@ -67,22 +67,22 @@ bool ScreenLockListenerWin::nativeEventFilter(const QByteArray& eventType, void* if (setting != nullptr && setting->PowerSetting == GUID_LIDSWITCH_STATE_CHANGE) { const DWORD* state = reinterpret_cast(&setting->Data); if (*state == 0) { - Q_EMIT screenLocked(); + emit screenLocked(); return true; } } } else if (m->wParam == PBT_APMSUSPEND) { - Q_EMIT screenLocked(); + emit screenLocked(); return true; } } if (m->message == WM_WTSSESSION_CHANGE) { if (m->wParam == WTS_CONSOLE_DISCONNECT) { - Q_EMIT screenLocked(); + emit screenLocked(); return true; } if (m->wParam == WTS_SESSION_LOCK) { - Q_EMIT screenLocked(); + emit screenLocked(); return true; } }