mirror of
https://github.com/keepassxreboot/keepassxc.git
synced 2024-12-25 23:39:45 -05:00
Lock database on switching user in macOS
This commit is contained in:
parent
e4eee897f9
commit
ebe6649683
@ -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
|
||||
|
@ -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()
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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()
|
||||
|
@ -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)
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user