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 Nov 29, 2018
1 parent 294188d commit 2e95bc8
Show file tree
Hide file tree
Showing 8 changed files with 223 additions and 7 deletions.
13 changes: 12 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,14 @@ set(autotype_SOURCES
autotype/WildcardMatcher.cpp
autotype/WindowSelectComboBox.cpp)

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} ${CMAKE_SOURCE_DIR}/share/windows/icon.rc)
endif()
Expand All @@ -249,6 +257,9 @@ 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)
Expand All @@ -272,7 +283,7 @@ target_link_libraries(keepassx_core
${ZXCVBN_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 @@ -28,6 +28,7 @@
#include "BrowserEntrySaveDialog.h"
#include "BrowserService.h"
#include "BrowserSettings.h"
#include "BrowserUtils.h"
#include "core/Database.h"
#include "core/EntrySearcher.h"
#include "core/Group.h"
Expand Down Expand Up @@ -89,8 +90,8 @@ bool BrowserService::openDatabase(bool triggerUnlock)
}

if (triggerUnlock) {
getMainWindow()->bringToFront();
m_bringToFrontRequested = true;
m_dbTabWidget->unlockDatabaseInDialog(dbWidget, DatabaseOpenDialog::Intent::Browser);
}

return false;
Expand Down Expand Up @@ -909,7 +910,7 @@ void BrowserService::databaseUnlocked(DatabaseWidget* dbWidget)
{
if (dbWidget) {
if (m_bringToFrontRequested) {
getMainWindow()->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 @@ -27,11 +27,16 @@ 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()
9 changes: 5 additions & 4 deletions src/gui/DatabaseOpenDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
* 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 "core/Global.h"

Expand All @@ -37,7 +37,8 @@ class DatabaseOpenDialog : public QDialog
{
None,
AutoType,
Merge
Merge,
Browser
};

explicit DatabaseOpenDialog(QWidget* parent = nullptr);
Expand All @@ -61,4 +62,4 @@ public slots:
Intent m_intent = Intent::None;
};

#endif // KEEPASSX_AUTOTYPEUNLOCKDIALOG_H
#endif // KEEPASSX_UNLOCKDATABASEDIALOG_H
4 changes: 4 additions & 0 deletions src/gui/DatabaseTabWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <QTabWidget>

#include "autotype/AutoType.h"
#include "browser/BrowserUtils.h"
#include "core/AsyncTask.h"
#include "core/Config.h"
#include "core/Database.h"
Expand Down Expand Up @@ -547,6 +548,9 @@ void DatabaseTabWidget::unlockDatabaseInDialog(DatabaseWidget* dbWidget, Databas
if (intent == DatabaseOpenDialog::Intent::AutoType) {
autoType()->raiseWindow();
Tools::wait(500);
} else if (intent == DatabaseOpenDialog::Intent::Browser) {
browserUtils()->raiseWindow();
Tools::wait(500);
}
#endif

Expand Down

0 comments on commit 2e95bc8

Please sign in to comment.