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/MacPasteboard.cpp
gui/macutils/MacUtils.cpp
gui/macutils/AppKitImpl.mm)
gui/macutils/AppKitImpl.mm
gui/macutils/AppKit.h)
endif()
if(UNIX AND NOT APPLE)
set(keepassx_SOURCES

View File

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

View File

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

View File

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

View File

@ -22,19 +22,22 @@
@implementation AppKitImpl
AppKit::AppKit()
- (id) initWithObject:(AppKit *)appkit
{
self = [[AppKitImpl alloc] init];
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:static_cast<id>(self)
self = [super init];
if (self) {
m_appkit = appkit;
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:static_cast<id>(self)
selector:@selector(didDeactivateApplicationObserver:)
name:NSWorkspaceDidDeactivateApplicationNotification
object:nil];
}
AppKit::~AppKit()
{
[[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:static_cast<id>(self)];
[static_cast<id>(self) dealloc];
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:static_cast<id>(self)
selector:@selector(userSwitchHandler:)
name:NSWorkspaceSessionDidResignActiveNotification
object:nil];
}
return self;
}
//
@ -104,10 +107,34 @@ AppKit::~AppKit()
&& 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 -------------------------
//
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()
{
return [static_cast<id>(self) lastActiveApplication].processIdentifier;
@ -142,5 +169,3 @@ bool AppKit::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)
, m_appkit(new AppKit())
{
connect(m_appkit.data(), SIGNAL(lockDatabases()), SIGNAL(lockDatabases()));
}
MacUtils::~MacUtils()

View File

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