Skip to content

Commit

Permalink
Allow the use of UnlockDatabaseDialog without AutoType. Switch browse…
Browse files Browse the repository at this point in the history
…r to use UnlockDatabaseDialog.
  • Loading branch information
varjolintu committed May 8, 2018
1 parent aae6d09 commit cd4a59e
Show file tree
Hide file tree
Showing 10 changed files with 250 additions and 13 deletions.
14 changes: 13 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,14 @@ set(autotype_SOURCES
autotype/test/AutoTypeTestInterface.h
)

set(browserUtil_SOURCES
browser/BrowserUtils.cpp
)

if(APPLE)
set(browserUtil_SOURCES ${browserUtil_SOURCES} browser/BrowserUtils.mm)
endif()

if(MINGW)
set(keepassx_SOURCES_MAINEXE
${keepassx_SOURCES_MAINEXE}
Expand All @@ -243,11 +251,15 @@ endif()
add_library(autotype STATIC ${autotype_SOURCES})
target_link_libraries(autotype Qt5::Core Qt5::Widgets)

add_library(browserUtils STATIC ${browserUtil_SOURCES})
target_link_libraries(browserUtils Qt5::Core Qt5::Widgets)

add_library(keepassx_core STATIC ${keepassx_SOURCES})

set_target_properties(keepassx_core PROPERTIES COMPILE_DEFINITIONS KEEPASSX_BUILDING_CORE)
target_link_libraries(keepassx_core
autotype
browserUtils
${keepassxcbrowser_LIB}
${sshagent_LIB}
Qt5::Core
Expand All @@ -263,7 +275,7 @@ target_link_libraries(keepassx_core
${ZLIB_LIBRARIES})

if(APPLE)
target_link_libraries(keepassx_core "-framework Foundation")
target_link_libraries(keepassx_core "-framework Foundation -framework AppKit")
if(Qt5MacExtras_FOUND)
target_link_libraries(keepassx_core Qt5::MacExtras)
endif()
Expand Down
5 changes: 3 additions & 2 deletions src/browser/BrowserService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "BrowserAccessControlDialog.h"
#include "BrowserEntryConfig.h"
#include "BrowserSettings.h"
#include "BrowserUtils.h"
#include "core/Database.h"
#include "core/EntrySearcher.h"
#include "core/Group.h"
Expand Down Expand Up @@ -83,8 +84,8 @@ bool BrowserService::openDatabase(bool triggerUnlock)
}

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

return false;
Expand Down Expand Up @@ -793,7 +794,7 @@ void BrowserService::databaseUnlocked(DatabaseWidget* dbWidget)
{
if (dbWidget) {
if (m_bringToFrontRequested) {
KEEPASSXC_MAIN_WINDOW->lower();
browserUtils()->raiseLastActiveWindow();
m_bringToFrontRequested = false;
}
emit databaseUnlocked();
Expand Down
63 changes: 63 additions & 0 deletions src/browser/BrowserUtils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright (C) 2012 Felix Geyer <[email protected]>
* Copyright (C) 2017 KeePassXC Team <[email protected]>
*
* 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
55 changes: 55 additions & 0 deletions src/browser/BrowserUtils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (C) 2012 Felix Geyer <[email protected]>
* Copyright (C) 2017 KeePassXC Team <[email protected]>
*
* 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
76 changes: 76 additions & 0 deletions src/browser/BrowserUtils.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright (C) 2016 Lennart Glauer <[email protected]>
* Copyright (C) 2017 KeePassXC Team <[email protected]>
*
* 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
5 changes: 5 additions & 0 deletions src/browser/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,17 @@ 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)
endif()
21 changes: 18 additions & 3 deletions src/gui/DatabaseWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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);
}
}
}

Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/gui/DatabaseWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
10 changes: 10 additions & 0 deletions src/gui/UnlockDatabaseDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
Expand All @@ -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();
Expand All @@ -45,6 +51,10 @@ Database* UnlockDatabaseDialog::database()
return m_view->database();
}

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

void UnlockDatabaseDialog::complete(bool r)
{
if (r) {
Expand Down
Loading

0 comments on commit cd4a59e

Please sign in to comment.