Skip to content
Permalink
Browse files

Allow the use of UnlockDatabaseDialog without AutoType. Switch browse…

…r to use UnlockDatabaseDialog.
  • Loading branch information...
varjolintu committed Jan 25, 2018
1 parent 5c7aaf5 commit a5be1535f7667883d55db6979efb9758347a9db9
@@ -82,7 +82,7 @@ bool BrowserService::openDatabase(bool triggerUnlock)
}

if (triggerUnlock) {
KEEPASSXC_MAIN_WINDOW->bringToFront();
dbWidget->showUnlockDialog(false);
}

return false;
@@ -766,6 +766,7 @@ void BrowserService::databaseLocked(DatabaseWidget* dbWidget)
void BrowserService::databaseUnlocked(DatabaseWidget* dbWidget)
{
if (dbWidget) {
// TODO: browserUtils()->raiseLastActiveWindow() after 2.3.2 is merged to develop. See #1884
emit databaseUnlocked();
}
}
@@ -0,0 +1,63 @@
/*
* Copyright (C) 2012 Felix Geyer <debfx@fobos.de>
* Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
*
* 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 <http://www.gnu.org/licenses/>.
*/

#include "BrowserUtils.h"
#include <QApplication>

BrowserUtils* BrowserUtils::m_instance = nullptr;

#ifndef Q_OS_MAC
BrowserUtils::BrowserUtils(QObject* parent) : QObject(parent)
{

}

BrowserUtils::~BrowserUtils()
{

}
#endif

BrowserUtils* BrowserUtils::instance()
{
if (!m_instance) {
m_instance = new BrowserUtils(qApp);
}

return m_instance;
}

void BrowserUtils::raiseWindow()
{
#if defined(Q_OS_MAC)
raiseOwnWindow();
#endif
}

#ifndef Q_OS_MAC
bool BrowserUtils::raiseOwnWindow()
{
return false;
}

bool BrowserUtils::raiseLastActiveWindow()
{
return false;
}

#endif
@@ -0,0 +1,55 @@
/*
* Copyright (C) 2012 Felix Geyer <debfx@fobos.de>
* Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
*
* 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 <http://www.gnu.org/licenses/>.
*/

#ifndef KEEPASSXC_BROWSERUTILS_H
#define KEEPASSXC_BROWSERUTILS_H

#include <QObject>

class BrowserUtils : public QObject
{
Q_OBJECT

public:
static BrowserUtils* instance();
static void createTestInstance();

public slots:
virtual void raiseWindow();
virtual bool raiseLastActiveWindow();
virtual bool raiseOwnWindow();
#if defined(Q_OS_MAC)
virtual bool activateProcess(pid_t pid);
#endif

private:
explicit BrowserUtils(QObject* parent = nullptr);
~BrowserUtils();

static BrowserUtils* m_instance;
void* self;

Q_DISABLE_COPY(BrowserUtils)
};

inline BrowserUtils* browserUtils()
{
return BrowserUtils::instance();
}

#endif // KEEPASSXC_BROWSERUTILS_H
@@ -0,0 +1,76 @@
/*
* Copyright (C) 2016 Lennart Glauer <mail@lennart-glauer.de>
* Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
*
* 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 <http://www.gnu.org/licenses/>.
*/

#include "BrowserUtils.h"
#import <AppKit/AppKit.h>

@interface AppKitImpl : NSObject
@property (strong) NSRunningApplication *lastActiveApplication;
@end

@implementation AppKitImpl

BrowserUtils::BrowserUtils(QObject* parent)
{
self = [[AppKitImpl alloc] init];
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:static_cast<id>(self)
selector:@selector(didDeactivateApplicationObserver:)
name:NSWorkspaceDidDeactivateApplicationNotification
object:nil];
}

BrowserUtils::~BrowserUtils()
{
[[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:static_cast<id>(self)];
[static_cast<id>(self) dealloc];
}

- (pid_t) ownProcessId
{
return [NSProcessInfo processInfo].processIdentifier;
}

- (void) didDeactivateApplicationObserver:(NSNotification *) notification
{
NSDictionary *userInfo = notification.userInfo;
NSRunningApplication *app = userInfo[NSWorkspaceApplicationKey];

if (app.processIdentifier != [self ownProcessId]) {
self.lastActiveApplication = app;
}
}

bool BrowserUtils::raiseOwnWindow()
{
pid_t pid = [static_cast<id>(self) ownProcessId];
return activateProcess(pid);
}

bool BrowserUtils::raiseLastActiveWindow()
{
pid_t pid = [static_cast<id>(self) lastActiveApplication].processIdentifier;
return activateProcess(pid);
}

bool BrowserUtils::activateProcess(pid_t pid)
{
NSRunningApplication* app = [NSRunningApplication runningApplicationWithProcessIdentifier:pid];
return app && [app activateWithOptions:static_cast<NSApplicationActivationOptions>(NSApplicationActivateIgnoringOtherApps)];
}

@end
@@ -26,12 +26,20 @@ if(WITH_XC_BROWSER)
BrowserOptionDialog.cpp
BrowserService.cpp
BrowserSettings.cpp
BrowserUtils.cpp
HostInstaller.cpp
NativeMessagingBase.cpp
NativeMessagingHost.cpp
Variant.cpp
)

if(APPLE)
set(keepassxcbrowser_SOURCES ${keepassxcbrowser_SOURCES} BrowserUtils.mm)
endif()

add_library(keepassxcbrowser STATIC ${keepassxcbrowser_SOURCES})
target_link_libraries(keepassxcbrowser Qt5::Core Qt5::Concurrent Qt5::Widgets Qt5::Network sodium)
if(APPLE)
target_link_libraries(keepassxcbrowser "-framework AppKit")
endif()
endif()
@@ -33,6 +33,7 @@
#include <QSplitter>

#include "autotype/AutoType.h"
#include "browser/BrowserUtils.h"
#include "core/Config.h"
#include "core/EntrySearcher.h"
#include "core/FilePath.h"
@@ -916,7 +917,9 @@ void DatabaseWidget::unlockDatabase(bool accepted)
if (sender() == m_unlockDatabaseDialog) {
QList<Database*> dbList;
dbList.append(m_db);
autoType()->performGlobalAutoType(dbList);
if (m_unlockDatabaseDialog->isAutoTypeEnabled()) {
autoType()->performGlobalAutoType(dbList);
}
}
}

@@ -1448,13 +1451,25 @@ EntryView* DatabaseWidget::entryView()
return m_entryView;
}

void DatabaseWidget::showUnlockDialog()
void DatabaseWidget::showUnlockDialog(bool autotypeEnabled)
{
if (m_unlockDatabaseDialog->isVisible()) {
return;
}

m_unlockDatabaseDialog->clearForms();
m_unlockDatabaseDialog->setFilePath(m_filePath);

if (!autotypeEnabled) {
m_unlockDatabaseDialog->disableAutoTypeResponse();
}

#if defined(Q_OS_MAC)
autoType()->raiseWindow();
if (autotypeEnabled) {
autoType()->raiseWindow();
} else {
browserUtils()->raiseWindow();
}
Tools::wait(500);
#endif

@@ -108,7 +108,7 @@ class DatabaseWidget : public QStackedWidget
bool currentEntryHasTotp();
GroupView* groupView();
EntryView* entryView();
void showUnlockDialog();
void showUnlockDialog(bool autotypeEnabled = true);
void closeUnlockDialog();
void blockAutoReload(bool block = true);
void refreshSearch();
@@ -25,6 +25,7 @@
UnlockDatabaseDialog::UnlockDatabaseDialog(QWidget* parent)
: QDialog(parent)
, m_view(new UnlockDatabaseWidget(this))
, m_autotypeResponse(true)
{
setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);
connect(m_view, SIGNAL(editFinished(bool)), this, SLOT(complete(bool)));
@@ -35,6 +36,11 @@ void UnlockDatabaseDialog::setFilePath(const QString& filePath)
m_view->load(filePath);
}

void UnlockDatabaseDialog::disableAutoTypeResponse()
{
m_autotypeResponse = false;
}

void UnlockDatabaseDialog::clearForms()
{
m_view->clearForms();
@@ -45,6 +51,10 @@ Database* UnlockDatabaseDialog::database()
return m_view->database();
}

bool UnlockDatabaseDialog::isAutoTypeEnabled() {
return m_autotypeResponse;
}

void UnlockDatabaseDialog::complete(bool r)
{
if (r) {
@@ -15,13 +15,10 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef KEEPASSX_AUTOTYPEUNLOCKDIALOG_H
#define KEEPASSX_AUTOTYPEUNLOCKDIALOG_H
#ifndef KEEPASSX_UNLOCKDATABASEDIALOG_H
#define KEEPASSX_UNLOCKDATABASEDIALOG_H

#include <QDialog>

//#include <gui/DatabaseTabWidget.h>

#include "core/Global.h"

class UnlockDatabaseWidget;
@@ -33,8 +30,10 @@ class UnlockDatabaseDialog : public QDialog
public:
explicit UnlockDatabaseDialog(QWidget* parent = nullptr);
void setFilePath(const QString& filePath);
void disableAutoTypeResponse();
void clearForms();
Database* database();
bool isAutoTypeEnabled();

signals:
void unlockDone(bool);
@@ -44,6 +43,7 @@ public slots:

private:
UnlockDatabaseWidget* const m_view;
bool m_autotypeResponse;
};

#endif // KEEPASSX_AUTOTYPEUNLOCKDIALOG_H
#endif // KEEPASSX_UNLOCKDATABASEDIALOG_H

0 comments on commit a5be153

Please sign in to comment.
You can’t perform that action at this time.