Lock database on switching user in macOS

This commit is contained in:
varjolintu 2019-04-28 11:08:20 +03:00 committed by Jonathan White
parent e4eee897f9
commit ebe6649683
7 changed files with 60 additions and 21 deletions

View File

@ -168,7 +168,8 @@ if(APPLE)
core/ScreenLockListenerMac.cpp core/ScreenLockListenerMac.cpp
core/MacPasteboard.cpp core/MacPasteboard.cpp
gui/macutils/MacUtils.cpp gui/macutils/MacUtils.cpp
gui/macutils/AppKitImpl.mm) gui/macutils/AppKitImpl.mm
gui/macutils/AppKit.h)
endif() endif()
if(UNIX AND NOT APPLE) if(UNIX AND NOT APPLE)
set(keepassx_SOURCES set(keepassx_SOURCES

View File

@ -63,6 +63,10 @@ DatabaseTabWidget::DatabaseTabWidget(QWidget* parent)
connect(autoType(), SIGNAL(autotypePerformed()), SLOT(relockPendingDatabase())); connect(autoType(), SIGNAL(autotypePerformed()), SLOT(relockPendingDatabase()));
connect(autoType(), SIGNAL(autotypeRejected()), SLOT(relockPendingDatabase())); connect(autoType(), SIGNAL(autotypeRejected()), SLOT(relockPendingDatabase()));
// clang-format on // clang-format on
#ifdef Q_OS_MACOS
connect(macUtils(), SIGNAL(lockDatabases()), SLOT(lockDatabases()));
#endif
} }
DatabaseTabWidget::~DatabaseTabWidget() DatabaseTabWidget::~DatabaseTabWidget()

View File

@ -19,14 +19,15 @@
#ifndef KEEPASSX_APPKIT_H #ifndef KEEPASSX_APPKIT_H
#define KEEPASSX_APPKIT_H #define KEEPASSX_APPKIT_H
#include <QObject>
#include <unistd.h> #include <unistd.h>
extern "C" { class AppKit : public QObject
class AppKit
{ {
Q_OBJECT
public: public:
AppKit(); AppKit(QObject* parent = nullptr);
~AppKit(); ~AppKit();
pid_t lastActiveProcessId(); pid_t lastActiveProcessId();
@ -37,10 +38,11 @@ public:
bool isHidden(pid_t pid); bool isHidden(pid_t pid);
bool isDarkMode(); bool isDarkMode();
signals:
void lockDatabases();
private: private:
void *self; void *self;
}; };
} // extern "C"
#endif // KEEPASSX_APPKIT_H #endif // KEEPASSX_APPKIT_H

View File

@ -22,6 +22,10 @@
#import <AppKit/NSRunningApplication.h> #import <AppKit/NSRunningApplication.h>
@interface AppKitImpl : NSObject @interface AppKitImpl : NSObject
{
AppKit *m_appkit;
}
- (id) initWithObject:(AppKit *)appkit;
@property (strong) NSRunningApplication *lastActiveApplication; @property (strong) NSRunningApplication *lastActiveApplication;
@ -31,5 +35,6 @@
- (bool) hideProcess:(pid_t) pid; - (bool) hideProcess:(pid_t) pid;
- (bool) isHidden:(pid_t) pid; - (bool) isHidden:(pid_t) pid;
- (bool) isDarkMode; - (bool) isDarkMode;
- (void) userSwitchHandler:(NSNotification*) notification;
@end @end

View File

@ -22,19 +22,22 @@
@implementation AppKitImpl @implementation AppKitImpl
AppKit::AppKit() - (id) initWithObject:(AppKit *)appkit
{ {
self = [[AppKitImpl alloc] init]; self = [super init];
if (self) {
m_appkit = appkit;
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:static_cast<id>(self) [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:static_cast<id>(self)
selector:@selector(didDeactivateApplicationObserver:) selector:@selector(didDeactivateApplicationObserver:)
name:NSWorkspaceDidDeactivateApplicationNotification name:NSWorkspaceDidDeactivateApplicationNotification
object:nil]; object:nil];
}
AppKit::~AppKit() [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:static_cast<id>(self)
{ selector:@selector(userSwitchHandler:)
[[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:static_cast<id>(self)]; name:NSWorkspaceSessionDidResignActiveNotification
[static_cast<id>(self) dealloc]; object:nil];
}
return self;
} }
// //
@ -104,10 +107,34 @@ AppKit::~AppKit()
&& NSOrderedSame == [style caseInsensitiveCompare:@"dark"] ); && NSOrderedSame == [style caseInsensitiveCompare:@"dark"] );
} }
//
// Notification for user switch
//
- (void) userSwitchHandler:(NSNotification*) notification
{
if ([[notification name] isEqualToString:NSWorkspaceSessionDidResignActiveNotification] && m_appkit)
{
emit m_appkit->lockDatabases();
}
}
@end
// //
// ------------------------- C++ Trampolines ------------------------- // ------------------------- C++ Trampolines -------------------------
// //
AppKit::AppKit(QObject* parent) : QObject(parent)
{
self = [[AppKitImpl alloc] initWithObject:this];
}
AppKit::~AppKit()
{
[[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:static_cast<id>(self)];
[static_cast<id>(self) dealloc];
}
pid_t AppKit::lastActiveProcessId() pid_t AppKit::lastActiveProcessId()
{ {
return [static_cast<id>(self) lastActiveApplication].processIdentifier; return [static_cast<id>(self) lastActiveApplication].processIdentifier;
@ -142,5 +169,3 @@ bool AppKit::isDarkMode()
{ {
return [static_cast<id>(self) isDarkMode]; return [static_cast<id>(self) isDarkMode];
} }
@end

View File

@ -24,7 +24,7 @@ MacUtils* MacUtils::m_instance = nullptr;
MacUtils::MacUtils(QObject* parent) : QObject(parent) MacUtils::MacUtils(QObject* parent) : QObject(parent)
, m_appkit(new AppKit()) , m_appkit(new AppKit())
{ {
connect(m_appkit.data(), SIGNAL(lockDatabases()), SIGNAL(lockDatabases()));
} }
MacUtils::~MacUtils() MacUtils::~MacUtils()

View File

@ -39,14 +39,16 @@ public:
bool isHidden(); bool isHidden();
bool isDarkMode(); bool isDarkMode();
signals:
void lockDatabases();
private: private:
explicit MacUtils(QObject* parent = nullptr); explicit MacUtils(QObject* parent = nullptr);
~MacUtils(); ~MacUtils();
private: private:
std::unique_ptr<AppKit> m_appkit; QScopedPointer<AppKit> m_appkit;
static MacUtils* m_instance; static MacUtils* m_instance;
void* self;
Q_DISABLE_COPY(MacUtils) Q_DISABLE_COPY(MacUtils)
}; };