Skip to content
Permalink
Browse files

Switch browser integration to use native raising of windows

  • Loading branch information...
varjolintu committed Jan 25, 2018
1 parent b6eeaba commit 05a9a8fcef56a6ceb03a704d18c85a12a5c02687
@@ -174,7 +174,9 @@ if(APPLE)
set(keepassx_SOURCES
${keepassx_SOURCES}
core/ScreenLockListenerMac.cpp
core/MacPasteboard.cpp)
core/MacPasteboard.cpp
gui/macutils/MacUtils.cpp
gui/macutils/AppKitImpl.mm)
endif()
if(UNIX AND NOT APPLE)
set(keepassx_SOURCES
@@ -272,14 +274,17 @@ 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()
if(WITH_XC_TOUCHID)
target_link_libraries(keepassx_core "-framework Security")
target_link_libraries(keepassx_core "-framework LocalAuthentication")
endif()
#if(WITH_XC_BROWSER OR WITH_XC_AUTOTYPE)
# target_link_libraries(keepassx_core "-framework AppKit")
#endif()
endif()
if(UNIX AND NOT APPLE)
target_link_libraries(keepassx_core Qt5::DBus)
@@ -17,6 +17,7 @@
*/

#include "AutoTypeMac.h"
#include "gui/macutils/MacUtils.h"

#include <ApplicationServices/ApplicationServices.h>

@@ -25,8 +26,7 @@
#define INVALID_KEYCODE 0xFFFF

AutoTypePlatformMac::AutoTypePlatformMac()
: m_appkit(new AppKit())
, m_hotkeyRef(nullptr)
: m_hotkeyRef(nullptr)
, m_hotkeyId({ 'kpx2', HOTKEY_ID })
{
EventTypeSpec eventSpec;
@@ -79,7 +79,7 @@ QStringList AutoTypePlatformMac::windowTitles()
//
WId AutoTypePlatformMac::activeWindow()
{
return m_appkit->activeProcessId();
return macUtils()->activeWindow();
}

//
@@ -159,23 +159,23 @@ AutoTypeExecutor* AutoTypePlatformMac::createExecutor()
//
bool AutoTypePlatformMac::raiseWindow(WId pid)
{
return m_appkit->activateProcess(pid);
return macUtils()->raiseWindow(pid);
}

//
// Activate last active window
//
bool AutoTypePlatformMac::raiseLastActiveWindow()
{
return m_appkit->activateProcess(m_appkit->lastActiveProcessId());
return macUtils()->raiseLastActiveWindow();
}

//
// Activate keepassx window
//
bool AutoTypePlatformMac::raiseOwnWindow()
{
return m_appkit->activateProcess(m_appkit->ownProcessId());
return macUtils()->raiseOwnWindow();
}

//
@@ -23,7 +23,6 @@
#include <QtPlugin>
#include <memory>

#include "AppKit.h"
#include "autotype/AutoTypePlatformPlugin.h"
#include "autotype/AutoTypeAction.h"

@@ -55,7 +54,6 @@ class AutoTypePlatformMac : public QObject, public AutoTypePlatformInterface
void globalShortcutTriggered();

private:
std::unique_ptr<AppKit> m_appkit;
EventHotKeyRef m_hotkeyRef;
EventHotKeyID m_hotkeyId;

@@ -1,6 +1,8 @@
set(autotype_mac_SOURCES AutoTypeMac.cpp)

set(autotype_mac_mm_SOURCES AppKitImpl.mm)
set(autotype_mac_mm_SOURCES
${CMAKE_SOURCE_DIR}/src/gui/macutils/AppKitImpl.mm
${CMAKE_SOURCE_DIR}/src/gui/macutils/MacUtils.cpp)

add_library(keepassx-autotype-cocoa MODULE ${autotype_mac_SOURCES} ${autotype_mac_mm_SOURCES})
set_target_properties(keepassx-autotype-cocoa PROPERTIES LINK_FLAGS "-framework Foundation -framework AppKit -framework Carbon")
@@ -33,7 +33,11 @@
#include "core/Group.h"
#include "core/Metadata.h"
#include "core/PasswordGenerator.h"
#include "core/Tools.h"
#include "gui/MainWindow.h"
#ifdef Q_OS_MACOS
#include "gui/macutils/MacUtils.h"
#endif

const char BrowserService::KEEPASSXCBROWSER_NAME[] = "KeePassXC-Browser Settings";
const char BrowserService::KEEPASSXCBROWSER_OLD_NAME[] = "keepassxc-browser Settings";
@@ -49,6 +53,7 @@ BrowserService::BrowserService(DatabaseTabWidget* parent)
: m_dbTabWidget(parent)
, m_dialogActive(false)
, m_bringToFrontRequested(false)
, m_wasMinimized(false)
, m_keepassBrowserUUID(QUuid::fromRfc4122(QByteArray::fromHex("de887cc3036343b8974b5911b8816224")))
{
// Don't connect the signals when used from DatabaseSettingsWidgetBrowser (parent is nullptr)
@@ -89,8 +94,9 @@ bool BrowserService::openDatabase(bool triggerUnlock)
}

if (triggerUnlock) {
getMainWindow()->bringToFront();
m_bringToFrontRequested = true;
m_wasMinimized = getMainWindow()->isMinimized();
raiseWindow(true);
}

return false;
@@ -167,6 +173,7 @@ QString BrowserService::storeKey(const QString& key)
"give it a unique name to identify and accept it."));
keyDialog.setOkButtonText(tr("Save and allow access"));
keyDialog.setWindowFlags(keyDialog.windowFlags() | Qt::WindowStaysOnTopHint);
raiseWindow();
keyDialog.show();
keyDialog.activateWindow();
keyDialog.raise();
@@ -175,6 +182,7 @@ QString BrowserService::storeKey(const QString& key)
id = keyDialog.textValue();

if (ok != QDialog::Accepted || id.isEmpty()) {
hideWindow();
return {};
}

@@ -189,6 +197,7 @@ QString BrowserService::storeKey(const QString& key)
}
} while (contains && dialogResult == QMessageBox::No);

hideWindow();
db->metadata()->customData()->set(QLatin1String(ASSOCIATE_KEY_PREFIX) + id, key);
return id;
}
@@ -367,6 +376,7 @@ void BrowserService::updateEntry(const QString& id,
msgBox.addButton(QMessageBox::No);
msgBox.setDefaultButton(QMessageBox::No);
msgBox.setWindowFlags(Qt::WindowStaysOnTopHint);
raiseWindow();
msgBox.activateWindow();
msgBox.raise();
dialogResult = msgBox.exec();
@@ -378,6 +388,8 @@ void BrowserService::updateEntry(const QString& id,
entry->setPassword(password);
entry->endUpdate();
}

hideWindow();
}
}

@@ -584,6 +596,11 @@ bool BrowserService::confirmEntries(QList<Entry*>& pwEntriesToConfirm,
accessControlDialog.setUrl(url);
accessControlDialog.setItems(pwEntriesToConfirm);

raiseWindow();
accessControlDialog.show();
accessControlDialog.activateWindow();
accessControlDialog.raise();

int res = accessControlDialog.exec();
if (accessControlDialog.remember()) {
for (Entry* entry : pwEntriesToConfirm) {
@@ -607,6 +624,7 @@ bool BrowserService::confirmEntries(QList<Entry*>& pwEntriesToConfirm,
}

m_dialogActive = false;
hideWindow();
if (res == QDialog::Accepted) {
return true;
}
@@ -898,6 +916,32 @@ bool BrowserService::checkLegacySettings()
return dialogResult == QMessageBox::Yes;
}

void BrowserService::hideWindow() const
{
if (m_wasMinimized) {
getMainWindow()->showMinimized();
} else {
#ifdef Q_OS_MACOS
macUtils()->raiseLastActiveWindow();
#else
getMainWindow()->lower();
#endif
}
}

void BrowserService::raiseWindow(const bool force)
{
m_wasMinimized = getMainWindow()->isMinimized();
#ifdef Q_OS_MACOS
macUtils()->raiseOwnWindow();
Tools::wait(500);
#else
if (force) {
getMainWindow()->bringToFront();
}
#endif
}

void BrowserService::databaseLocked(DatabaseWidget* dbWidget)
{
if (dbWidget) {
@@ -909,7 +953,7 @@ void BrowserService::databaseUnlocked(DatabaseWidget* dbWidget)
{
if (dbWidget) {
if (m_bringToFrontRequested) {
getMainWindow()->lower();
hideWindow();
m_bringToFrontRequested = false;
}
emit databaseUnlocked();
@@ -113,11 +113,14 @@ public slots:
bool moveSettingsToCustomData(Entry* entry, const QString& name) const;
int moveKeysToCustomData(Entry* entry, QSharedPointer<Database> db) const;
bool checkLegacySettings();
void hideWindow() const;
void raiseWindow(const bool force = false);

private:
DatabaseTabWidget* const m_dbTabWidget;
bool m_dialogActive;
bool m_bringToFrontRequested;
bool m_wasMinimized;
QUuid m_keepassBrowserUUID;
};

@@ -25,7 +25,11 @@ DatabaseOpenDialog::DatabaseOpenDialog(QWidget* parent)
, m_view(new DatabaseOpenWidget(this))
{
setWindowTitle(tr("Unlock Database - KeePassXC"));
#ifdef Q_OS_MACOS
setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);
#else
setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint | Qt::ForeignWindow);
#endif
connect(m_view, SIGNAL(dialogFinished(bool)), this, SLOT(complete(bool)));
}

@@ -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"

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

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

#endif // KEEPASSX_AUTOTYPEUNLOCKDIALOG_H
#endif // KEEPASSX_UNLOCKDATABASEDIALOG_H
@@ -39,6 +39,7 @@
#include "gui/DatabaseOpenDialog.h"
#include "gui/entry/EntryView.h"
#include "gui/group/GroupView.h"
#include "gui/macutils/MacUtils.h"
#include "gui/wizard/NewDatabaseWizard.h"

DatabaseTabWidget::DatabaseTabWidget(QWidget* parent)
@@ -544,8 +545,8 @@ void DatabaseTabWidget::unlockDatabaseInDialog(DatabaseWidget* dbWidget, Databas
m_databaseOpenDialog->setFilePath(filePath);

#ifdef Q_OS_MACOS
if (intent == DatabaseOpenDialog::Intent::AutoType) {
autoType()->raiseWindow();
if (intent == DatabaseOpenDialog::Intent::AutoType || intent == DatabaseOpenDialog::Intent::Browser) {
macUtils()->raiseOwnWindow();
Tools::wait(500);
}
#endif
File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -0,0 +1,63 @@
/*
* Copyright (C) 2012 Felix Geyer <debfx@fobos.de>
* Copyright (C) 2018 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 "AppKit.h"
#include "MacUtils.h"
#include <QApplication>

MacUtils* MacUtils::m_instance = nullptr;

MacUtils::MacUtils(QObject* parent) : QObject(parent)
, m_appkit(new AppKit())
{

}

MacUtils::~MacUtils()
{

}

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

return m_instance;
}

WId MacUtils::activeWindow()
{
return m_appkit->activeProcessId();
}

bool MacUtils::raiseWindow(WId pid)
{
return m_appkit->activateProcess(pid);
}

bool MacUtils::raiseOwnWindow()
{
return m_appkit->activateProcess(m_appkit->ownProcessId());
}

bool MacUtils::raiseLastActiveWindow()
{
return m_appkit->activateProcess(m_appkit->lastActiveProcessId());
}
Oops, something went wrong.

0 comments on commit 05a9a8f

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