From 1b7b2ff456dca5b17d6c25ccfc368170950bf43c Mon Sep 17 00:00:00 2001 From: thez3ro Date: Thu, 4 May 2017 22:52:10 +0200 Subject: [PATCH] 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 - - -