Merge pull request #1390 from keepassxreboot/feature/multiple-autotype-matches

Add support for multiple autotype sequences for a single entry
This commit is contained in:
TheZ3ro 2018-02-05 11:09:43 +01:00 committed by GitHub
commit 4206fd94e3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
22 changed files with 876 additions and 266 deletions

View File

@ -39,6 +39,7 @@ configure_file(version.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/version.h @ONLY)
set(keepassx_SOURCES set(keepassx_SOURCES
core/AutoTypeAssociations.cpp core/AutoTypeAssociations.cpp
core/AsyncTask.h core/AsyncTask.h
core/AutoTypeMatch.cpp
core/Config.cpp core/Config.cpp
core/CsvParser.cpp core/CsvParser.cpp
core/Database.cpp core/Database.cpp
@ -137,6 +138,8 @@ set(keepassx_SOURCES
gui/csvImport/CsvImportWizard.cpp gui/csvImport/CsvImportWizard.cpp
gui/csvImport/CsvParserModel.cpp gui/csvImport/CsvParserModel.cpp
gui/entry/AutoTypeAssociationsModel.cpp gui/entry/AutoTypeAssociationsModel.cpp
gui/entry/AutoTypeMatchModel.cpp
gui/entry/AutoTypeMatchView.cpp
gui/entry/EditEntryWidget.cpp gui/entry/EditEntryWidget.cpp
gui/entry/EditEntryWidget_p.h gui/entry/EditEntryWidget_p.h
gui/entry/EntryAttachmentsModel.cpp gui/entry/EntryAttachmentsModel.cpp

View File

@ -27,6 +27,7 @@
#include "autotype/AutoTypePlatformPlugin.h" #include "autotype/AutoTypePlatformPlugin.h"
#include "autotype/AutoTypeSelectDialog.h" #include "autotype/AutoTypeSelectDialog.h"
#include "autotype/WildcardMatcher.h" #include "autotype/WildcardMatcher.h"
#include "core/AutoTypeMatch.h"
#include "core/Config.h" #include "core/Config.h"
#include "core/Database.h" #include "core/Database.h"
#include "core/Entry.h" #include "core/Entry.h"
@ -40,7 +41,6 @@ AutoType* AutoType::m_instance = nullptr;
AutoType::AutoType(QObject* parent, bool test) AutoType::AutoType(QObject* parent, bool test)
: QObject(parent) : QObject(parent)
, m_inAutoType(false)
, m_autoTypeDelay(0) , m_autoTypeDelay(0)
, m_currentGlobalKey(static_cast<Qt::Key>(0)) , m_currentGlobalKey(static_cast<Qt::Key>(0))
, m_currentGlobalModifiers(0) , m_currentGlobalModifiers(0)
@ -102,6 +102,19 @@ void AutoType::loadPlugin(const QString& pluginPath)
} }
} }
void AutoType::unloadPlugin()
{
if (m_executor) {
delete m_executor;
m_executor = nullptr;
}
if (m_plugin) {
m_plugin->unload();
m_plugin = nullptr;
}
}
AutoType* AutoType::instance() AutoType* AutoType::instance()
{ {
if (!m_instance) { if (!m_instance) {
@ -127,32 +140,72 @@ QStringList AutoType::windowTitles()
return m_plugin->windowTitles(); return m_plugin->windowTitles();
} }
void AutoType::executeAutoTypeActions(const Entry* entry, QWidget* hideWindow, const QString& customSequence, WId window) void AutoType::resetInAutoType()
{ {
if (m_inAutoType || !m_plugin) { m_inAutoType.unlock();
}
void AutoType::raiseWindow()
{
#if defined(Q_OS_MAC)
m_plugin->raiseOwnWindow();
#endif
}
bool AutoType::registerGlobalShortcut(Qt::Key key, Qt::KeyboardModifiers modifiers)
{
Q_ASSERT(key);
Q_ASSERT(modifiers);
if (!m_plugin) {
return false;
}
if (key != m_currentGlobalKey || modifiers != m_currentGlobalModifiers) {
if (m_currentGlobalKey && m_currentGlobalModifiers) {
m_plugin->unregisterGlobalShortcut(m_currentGlobalKey, m_currentGlobalModifiers);
}
if (m_plugin->registerGlobalShortcut(key, modifiers)) {
m_currentGlobalKey = key;
m_currentGlobalModifiers = modifiers;
return true;
}
return false;
}
return true;
}
void AutoType::unregisterGlobalShortcut()
{
if (m_plugin && m_currentGlobalKey && m_currentGlobalModifiers) {
m_plugin->unregisterGlobalShortcut(m_currentGlobalKey, m_currentGlobalModifiers);
}
}
int AutoType::callEventFilter(void* event)
{
if (!m_plugin) {
return -1;
}
return m_plugin->platformEventFilter(event);
}
/**
* Core Autotype function that will execute actions
*/
void AutoType::executeAutoTypeActions(const Entry* entry, QWidget* hideWindow, const QString& sequence, WId window)
{
// no edit to the sequence beyond this point
if (!verifyAutoTypeSyntax(sequence)) {
return; return;
} }
m_inAutoType = true;
QString sequence;
if (customSequence.isEmpty()) {
sequence = autoTypeSequence(entry);
} else {
sequence = customSequence;
}
if (!checkSyntax(sequence)) {
return;
}
sequence.replace("{{}", "{LEFTBRACE}");
sequence.replace("{}}", "{RIGHTBRACE}");
QList<AutoTypeAction*> actions; QList<AutoTypeAction*> actions;
ListDeleter<AutoTypeAction*> actionsDeleter(&actions); ListDeleter<AutoTypeAction*> actionsDeleter(&actions);
if (!parseActions(sequence, entry, actions)) { if (!parseActions(sequence, entry, actions)) {
m_inAutoType = false; // TODO: make this automatic
return; return;
} }
@ -181,13 +234,39 @@ void AutoType::executeAutoTypeActions(const Entry* entry, QWidget* hideWindow, c
action->accept(m_executor); action->accept(m_executor);
QCoreApplication::processEvents(QEventLoop::AllEvents, 10); QCoreApplication::processEvents(QEventLoop::AllEvents, 10);
} }
m_inAutoType = false;
} }
/**
* Single Autotype entry-point function
* Perfom autotype sequence in the active window
*/
void AutoType::performAutoType(const Entry* entry, QWidget* hideWindow)
{
if (!m_plugin) {
return;
}
QList<QString> sequences = autoTypeSequences(entry);
if (sequences.isEmpty()) {
return;
}
if (!m_inAutoType.tryLock()) {
return;
}
executeAutoTypeActions(entry, hideWindow, sequences.first());
m_inAutoType.unlock();
}
/**
* Global Autotype entry-point funcion
* Perform global autotype on the active window
*/
void AutoType::performGlobalAutoType(const QList<Database*>& dbList) void AutoType::performGlobalAutoType(const QList<Database*>& dbList)
{ {
if (m_inAutoType || !m_plugin) { if (!m_plugin) {
return; return;
} }
@ -197,38 +276,40 @@ void AutoType::performGlobalAutoType(const QList<Database*>& dbList)
return; return;
} }
m_inAutoType = true; if (!m_inAutoType.tryLock()) {
return;
}
QList<Entry*> entryList; QList<AutoTypeMatch> matchList;
QHash<Entry*, QString> sequenceHash;
for (Database* db : dbList) { for (Database* db : dbList) {
const QList<Entry*> dbEntries = db->rootGroup()->entriesRecursive(); const QList<Entry*> dbEntries = db->rootGroup()->entriesRecursive();
for (Entry* entry : dbEntries) { for (Entry* entry : dbEntries) {
QString sequence = autoTypeSequence(entry, windowTitle); const QSet<QString> sequences = autoTypeSequences(entry, windowTitle).toSet();
if (!sequence.isEmpty()) { for (const QString& sequence : sequences) {
entryList << entry; if (!sequence.isEmpty()) {
sequenceHash.insert(entry, sequence); matchList << AutoTypeMatch(entry, sequence);
}
} }
} }
} }
if (entryList.isEmpty()) { if (matchList.isEmpty()) {
m_inAutoType = false; m_inAutoType.unlock();
QString message = tr("Couldn't find an entry that matches the window title:"); QString message = tr("Couldn't find an entry that matches the window title:");
message.append("\n\n"); message.append("\n\n");
message.append(windowTitle); message.append(windowTitle);
MessageBox::information(nullptr, tr("Auto-Type - KeePassXC"), message); MessageBox::information(nullptr, tr("Auto-Type - KeePassXC"), message);
} else if ((entryList.size() == 1) && !config()->get("security/autotypeask").toBool()) { } else if ((matchList.size() == 1) && !config()->get("security/autotypeask").toBool()) {
m_inAutoType = false; executeAutoTypeActions(matchList.first().entry, nullptr, matchList.first().sequence);
performAutoType(entryList.first(), nullptr, sequenceHash[entryList.first()]); m_inAutoType.unlock();
} else { } else {
m_windowFromGlobal = m_plugin->activeWindow(); m_windowFromGlobal = m_plugin->activeWindow();
AutoTypeSelectDialog* selectDialog = new AutoTypeSelectDialog(); AutoTypeSelectDialog* selectDialog = new AutoTypeSelectDialog();
connect( connect(selectDialog, SIGNAL(matchActivated(AutoTypeMatch)),
selectDialog, SIGNAL(entryActivated(Entry*, QString)), SLOT(performAutoTypeFromGlobal(Entry*, QString))); SLOT(performAutoTypeFromGlobal(AutoTypeMatch)));
connect(selectDialog, SIGNAL(rejected()), SLOT(resetInAutoType())); connect(selectDialog, SIGNAL(rejected()), SLOT(resetInAutoType()));
selectDialog->setEntries(entryList, sequenceHash); selectDialog->setMatchList(matchList);
#if defined(Q_OS_MAC) #if defined(Q_OS_MAC)
m_plugin->raiseOwnWindow(); m_plugin->raiseOwnWindow();
Tools::wait(500); Tools::wait(500);
@ -239,92 +320,31 @@ void AutoType::performGlobalAutoType(const QList<Database*>& dbList)
} }
} }
void AutoType::performAutoTypeFromGlobal(Entry* entry, const QString& sequence) void AutoType::performAutoTypeFromGlobal(AutoTypeMatch match)
{ {
Q_ASSERT(m_inAutoType); // We don't care about the result here, the mutex should already be locked. Now it's locked for sure
m_inAutoType.tryLock();
m_plugin->raiseWindow(m_windowFromGlobal); m_plugin->raiseWindow(m_windowFromGlobal);
m_inAutoType = false; executeAutoTypeActions(match.entry, nullptr, match.sequence, m_windowFromGlobal);
performAutoType(entry, nullptr, sequence, m_windowFromGlobal); m_inAutoType.unlock();
} }
void AutoType::resetInAutoType() /**
{ * Parse an autotype sequence and resolve its Template/command inside as AutoTypeActions
Q_ASSERT(m_inAutoType); */
bool AutoType::parseActions(const QString& actionSequence, const Entry* entry, QList<AutoTypeAction*>& actions)
m_inAutoType = false;
}
void AutoType::raiseWindow()
{
#if defined(Q_OS_MAC)
m_plugin->raiseOwnWindow();
#endif
}
void AutoType::unloadPlugin()
{
if (m_executor) {
delete m_executor;
m_executor = nullptr;
}
if (m_plugin) {
m_plugin->unload();
m_plugin = nullptr;
}
}
bool AutoType::registerGlobalShortcut(Qt::Key key, Qt::KeyboardModifiers modifiers)
{
Q_ASSERT(key);
Q_ASSERT(modifiers);
if (!m_plugin) {
return false;
}
if (key != m_currentGlobalKey || modifiers != m_currentGlobalModifiers) {
if (m_currentGlobalKey && m_currentGlobalModifiers) {
m_plugin->unregisterGlobalShortcut(m_currentGlobalKey, m_currentGlobalModifiers);
}
if (m_plugin->registerGlobalShortcut(key, modifiers)) {
m_currentGlobalKey = key;
m_currentGlobalModifiers = modifiers;
return true;
} else {
return false;
}
} else {
return true;
}
}
void AutoType::unregisterGlobalShortcut()
{
if (m_plugin && m_currentGlobalKey && m_currentGlobalModifiers) {
m_plugin->unregisterGlobalShortcut(m_currentGlobalKey, m_currentGlobalModifiers);
}
}
int AutoType::callEventFilter(void* event)
{
if (!m_plugin) {
return -1;
}
return m_plugin->platformEventFilter(event);
}
bool AutoType::parseActions(const QString& sequence, const Entry* entry, QList<AutoTypeAction*>& actions)
{ {
QString tmpl; QString tmpl;
bool inTmpl = false; bool inTmpl = false;
m_autoTypeDelay = config()->get("AutoTypeDelay").toInt(); m_autoTypeDelay = config()->get("AutoTypeDelay").toInt();
QString sequence = actionSequence;
sequence.replace("{{}", "{LEFTBRACE}");
sequence.replace("{}}", "{RIGHTBRACE}");
for (const QChar& ch : sequence) { for (const QChar& ch : sequence) {
if (inTmpl) { if (inTmpl) {
if (ch == '{') { if (ch == '{') {
@ -363,6 +383,9 @@ bool AutoType::parseActions(const QString& sequence, const Entry* entry, QList<A
return true; return true;
} }
/**
* Convert an autotype Template/command to its AutoTypeAction that will be executed by the plugin executor
*/
QList<AutoTypeAction*> AutoType::createActionFromTemplate(const QString& tmpl, const Entry* entry) QList<AutoTypeAction*> AutoType::createActionFromTemplate(const QString& tmpl, const Entry* entry)
{ {
QString tmplName = tmpl; QString tmplName = tmpl;
@ -506,80 +529,65 @@ QList<AutoTypeAction*> AutoType::createActionFromTemplate(const QString& tmpl, c
return list; return list;
} }
QString AutoType::autoTypeSequence(const Entry* entry, const QString& windowTitle) /**
* Retrive the autotype sequences matches for a given windowTitle
* This returns a list with priority ordering. If you don't want duplicates call .toSet() on it.
*/
QList<QString> AutoType::autoTypeSequences(const Entry* entry, const QString& windowTitle)
{ {
QList<QString> sequenceList;
if (!entry->autoTypeEnabled()) { if (!entry->autoTypeEnabled()) {
return QString(); return sequenceList;
} }
bool enableSet = false; const Group* group = entry->group();
QString sequence; do {
if (group->autoTypeEnabled() == Group::Disable) {
return sequenceList;
} else if (group->autoTypeEnabled() == Group::Enable) {
break;
}
group = group->parentGroup();
} while (group);
if (!windowTitle.isEmpty()) { if (!windowTitle.isEmpty()) {
bool match = false;
const QList<AutoTypeAssociations::Association> assocList = entry->autoTypeAssociations()->getAll(); const QList<AutoTypeAssociations::Association> assocList = entry->autoTypeAssociations()->getAll();
for (const AutoTypeAssociations::Association& assoc : assocList) { for (const AutoTypeAssociations::Association& assoc : assocList) {
const QString window = entry->resolveMultiplePlaceholders(assoc.window); const QString window = entry->resolveMultiplePlaceholders(assoc.window);
if (windowMatches(windowTitle, window)) { if (windowMatches(windowTitle, window)) {
if (!assoc.sequence.isEmpty()) { if (!assoc.sequence.isEmpty()) {
sequence = assoc.sequence; sequenceList.append(assoc.sequence);
} else { } else {
sequence = entry->defaultAutoTypeSequence(); sequenceList.append(entry->effectiveAutoTypeSequence());
} }
match = true;
break;
} }
} }
if (!match && config()->get("AutoTypeEntryTitleMatch").toBool() && if (config()->get("AutoTypeEntryTitleMatch").toBool() &&
windowMatchesTitle(windowTitle, entry->resolvePlaceholder(entry->title()))) { windowMatchesTitle(windowTitle, entry->resolvePlaceholder(entry->title()))) {
sequence = entry->defaultAutoTypeSequence(); sequenceList.append(entry->effectiveAutoTypeSequence());
match = true;
} }
if (!match && config()->get("AutoTypeEntryURLMatch").toBool() && if (config()->get("AutoTypeEntryURLMatch").toBool() &&
windowMatchesUrl(windowTitle, entry->resolvePlaceholder(entry->url()))) { windowMatchesUrl(windowTitle, entry->resolvePlaceholder(entry->url()))) {
sequence = entry->defaultAutoTypeSequence(); sequenceList.append(entry->effectiveAutoTypeSequence());
match = true;
} }
if (!match) { if (sequenceList.isEmpty()) {
return QString(); return sequenceList;
} }
} else { } else {
sequence = entry->defaultAutoTypeSequence(); sequenceList.append(entry->effectiveAutoTypeSequence());
} }
const Group* group = entry->group(); return sequenceList;
do {
if (!enableSet) {
if (group->autoTypeEnabled() == Group::Disable) {
return QString();
} else if (group->autoTypeEnabled() == Group::Enable) {
enableSet = true;
}
}
if (sequence.isEmpty()) {
sequence = group->defaultAutoTypeSequence();
}
group = group->parentGroup();
} while (group && (!enableSet || sequence.isEmpty()));
if (sequence.isEmpty() && (!entry->resolvePlaceholder(entry->username()).isEmpty() ||
!entry->resolvePlaceholder(entry->password()).isEmpty())) {
if (entry->resolvePlaceholder(entry->username()).isEmpty()) {
sequence = "{PASSWORD}{ENTER}";
} else if (entry->resolvePlaceholder(entry->password()).isEmpty()) {
sequence = "{USERNAME}{ENTER}";
} else {
sequence = "{USERNAME}{TAB}{PASSWORD}{ENTER}";
}
}
return sequence;
} }
/**
* Checks if a window title matches a pattern
*/
bool AutoType::windowMatches(const QString& windowTitle, const QString& windowPattern) bool AutoType::windowMatches(const QString& windowTitle, const QString& windowPattern)
{ {
if (windowPattern.startsWith("//") && windowPattern.endsWith("//") && windowPattern.size() >= 4) { if (windowPattern.startsWith("//") && windowPattern.endsWith("//") && windowPattern.size() >= 4) {
@ -590,11 +598,19 @@ bool AutoType::windowMatches(const QString& windowTitle, const QString& windowPa
} }
} }
/**
* Checks if a window title matches an entry Title
* The entry title should be Spr-compiled by the caller
*/
bool AutoType::windowMatchesTitle(const QString& windowTitle, const QString& resolvedTitle) bool AutoType::windowMatchesTitle(const QString& windowTitle, const QString& resolvedTitle)
{ {
return !resolvedTitle.isEmpty() && windowTitle.contains(resolvedTitle, Qt::CaseInsensitive); return !resolvedTitle.isEmpty() && windowTitle.contains(resolvedTitle, Qt::CaseInsensitive);
} }
/**
* Checks if a window title matches an entry URL
* The entry URL should be Spr-compiled by the caller
*/
bool AutoType::windowMatchesUrl(const QString& windowTitle, const QString& resolvedUrl) bool AutoType::windowMatchesUrl(const QString& windowTitle, const QString& resolvedUrl)
{ {
if (!resolvedUrl.isEmpty() && windowTitle.contains(resolvedUrl, Qt::CaseInsensitive)) { if (!resolvedUrl.isEmpty() && windowTitle.contains(resolvedUrl, Qt::CaseInsensitive)) {
@ -609,6 +625,9 @@ bool AutoType::windowMatchesUrl(const QString& windowTitle, const QString& resol
return false; return false;
} }
/**
* Checks if the overall syntax of an autotype sequence is fine
*/
bool AutoType::checkSyntax(const QString& string) bool AutoType::checkSyntax(const QString& string)
{ {
QString allowRepetition = "(?:\\s\\d+)?"; QString allowRepetition = "(?:\\s\\d+)?";
@ -634,6 +653,9 @@ bool AutoType::checkSyntax(const QString& string)
return match.hasMatch(); return match.hasMatch();
} }
/**
* Checks an autotype sequence for high delay
*/
bool AutoType::checkHighDelay(const QString& string) bool AutoType::checkHighDelay(const QString& string)
{ {
// 5 digit numbers(10 seconds) are too much // 5 digit numbers(10 seconds) are too much
@ -642,6 +664,9 @@ bool AutoType::checkHighDelay(const QString& string)
return match.hasMatch(); return match.hasMatch();
} }
/**
* Checks an autotype sequence for slow keypress
*/
bool AutoType::checkSlowKeypress(const QString& string) bool AutoType::checkSlowKeypress(const QString& string)
{ {
// 3 digit numbers(100 milliseconds) are too much // 3 digit numbers(100 milliseconds) are too much
@ -650,6 +675,9 @@ bool AutoType::checkSlowKeypress(const QString& string)
return match.hasMatch(); return match.hasMatch();
} }
/**
* Checks an autotype sequence for high repetition command
*/
bool AutoType::checkHighRepetition(const QString& string) bool AutoType::checkHighRepetition(const QString& string)
{ {
// 3 digit numbers are too much // 3 digit numbers are too much
@ -658,6 +686,9 @@ bool AutoType::checkHighRepetition(const QString& string)
return match.hasMatch(); return match.hasMatch();
} }
/**
* Verify if the syntax of an autotype sequence is correct and doesn't have silly parameters
*/
bool AutoType::verifyAutoTypeSyntax(const QString& sequence) bool AutoType::verifyAutoTypeSyntax(const QString& sequence)
{ {
if (!AutoType::checkSyntax(sequence)) { if (!AutoType::checkSyntax(sequence)) {
@ -691,12 +722,3 @@ bool AutoType::verifyAutoTypeSyntax(const QString& sequence)
} }
return true; return true;
} }
void AutoType::performAutoType(const Entry* entry, QWidget* hideWindow, const QString& customSequence, WId window)
{
auto sequence = entry->effectiveAutoTypeSequence();
if (verifyAutoTypeSyntax(sequence)) {
executeAutoTypeActions(entry, hideWindow, customSequence, window);
}
}

View File

@ -1,4 +1,4 @@
/* /*
* Copyright (C) 2012 Felix Geyer <debfx@fobos.de> * Copyright (C) 2012 Felix Geyer <debfx@fobos.de>
* Copyright (C) 2017 KeePassXC Team <team@keepassxc.org> * Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
* *
@ -22,6 +22,9 @@
#include <QObject> #include <QObject>
#include <QStringList> #include <QStringList>
#include <QWidget> #include <QWidget>
#include <QMutex>
#include "core/AutoTypeMatch.h"
class AutoTypeAction; class AutoTypeAction;
class AutoTypeExecutor; class AutoTypeExecutor;
@ -36,10 +39,6 @@ class AutoType : public QObject
public: public:
QStringList windowTitles(); QStringList windowTitles();
void executeAutoTypeActions(const Entry* entry,
QWidget* hideWindow = nullptr,
const QString& customSequence = QString(),
WId window = 0);
bool registerGlobalShortcut(Qt::Key key, Qt::KeyboardModifiers modifiers); bool registerGlobalShortcut(Qt::Key key, Qt::KeyboardModifiers modifiers);
void unregisterGlobalShortcut(); void unregisterGlobalShortcut();
int callEventFilter(void* event); int callEventFilter(void* event);
@ -49,9 +48,7 @@ public:
static bool checkHighDelay(const QString& string); static bool checkHighDelay(const QString& string);
static bool verifyAutoTypeSyntax(const QString& sequence); static bool verifyAutoTypeSyntax(const QString& sequence);
void performAutoType(const Entry* entry, void performAutoType(const Entry* entry,
QWidget* hideWindow = nullptr, QWidget* hideWindow = nullptr);
const QString& customSequence = QString(),
WId window = 0);
inline bool isAvailable() inline bool isAvailable()
{ {
@ -69,7 +66,7 @@ signals:
void globalShortcutTriggered(); void globalShortcutTriggered();
private slots: private slots:
void performAutoTypeFromGlobal(Entry* entry, const QString& sequence); void performAutoTypeFromGlobal(AutoTypeMatch match);
void resetInAutoType(); void resetInAutoType();
void unloadPlugin(); void unloadPlugin();
@ -77,14 +74,18 @@ private:
explicit AutoType(QObject* parent = nullptr, bool test = false); explicit AutoType(QObject* parent = nullptr, bool test = false);
~AutoType(); ~AutoType();
void loadPlugin(const QString& pluginPath); void loadPlugin(const QString& pluginPath);
void executeAutoTypeActions(const Entry* entry,
QWidget* hideWindow = nullptr,
const QString& customSequence = QString(),
WId window = 0);
bool parseActions(const QString& sequence, const Entry* entry, QList<AutoTypeAction*>& actions); bool parseActions(const QString& sequence, const Entry* entry, QList<AutoTypeAction*>& actions);
QList<AutoTypeAction*> createActionFromTemplate(const QString& tmpl, const Entry* entry); QList<AutoTypeAction*> createActionFromTemplate(const QString& tmpl, const Entry* entry);
QString autoTypeSequence(const Entry* entry, const QString& windowTitle = QString()); QList<QString> autoTypeSequences(const Entry* entry, const QString& windowTitle = QString());
bool windowMatchesTitle(const QString& windowTitle, const QString& resolvedTitle); bool windowMatchesTitle(const QString& windowTitle, const QString& resolvedTitle);
bool windowMatchesUrl(const QString& windowTitle, const QString& resolvedUrl); bool windowMatchesUrl(const QString& windowTitle, const QString& resolvedUrl);
bool windowMatches(const QString& windowTitle, const QString& windowPattern); bool windowMatches(const QString& windowTitle, const QString& windowPattern);
bool m_inAutoType; QMutex m_inAutoType;
int m_autoTypeDelay; int m_autoTypeDelay;
Qt::Key m_currentGlobalKey; Qt::Key m_currentGlobalKey;
Qt::KeyboardModifiers m_currentGlobalModifiers; Qt::KeyboardModifiers m_currentGlobalModifiers;

View File

@ -20,6 +20,7 @@
#include <QChar> #include <QChar>
#include <Qt> #include <Qt>
#include <QObject>
#include "core/Global.h" #include "core/Global.h"

View File

@ -1,5 +1,6 @@
/* /*
* Copyright (C) 2012 Felix Geyer <debfx@fobos.de> * 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 * 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 * it under the terms of the GNU General Public License as published by
@ -25,14 +26,15 @@
#include <QVBoxLayout> #include <QVBoxLayout>
#include "autotype/AutoTypeSelectView.h" #include "autotype/AutoTypeSelectView.h"
#include "core/AutoTypeMatch.h"
#include "core/Config.h" #include "core/Config.h"
#include "core/FilePath.h" #include "core/FilePath.h"
#include "gui/entry/EntryModel.h" #include "gui/entry/AutoTypeMatchModel.h"
AutoTypeSelectDialog::AutoTypeSelectDialog(QWidget* parent) AutoTypeSelectDialog::AutoTypeSelectDialog(QWidget* parent)
: QDialog(parent) : QDialog(parent)
, m_view(new AutoTypeSelectView(this)) , m_view(new AutoTypeSelectView(this))
, m_entryActivatedEmitted(false) , m_matchActivatedEmitted(false)
{ {
setAttribute(Qt::WA_DeleteOnClose); setAttribute(Qt::WA_DeleteOnClose);
// Places the window on the active (virtual) desktop instead of where the main window is. // Places the window on the active (virtual) desktop instead of where the main window is.
@ -42,7 +44,7 @@ AutoTypeSelectDialog::AutoTypeSelectDialog(QWidget* parent)
setWindowIcon(filePath()->applicationIcon()); setWindowIcon(filePath()->applicationIcon());
QRect screenGeometry = QApplication::desktop()->availableGeometry(QCursor::pos()); QRect screenGeometry = QApplication::desktop()->availableGeometry(QCursor::pos());
QSize size = config()->get("GUI/AutoTypeSelectDialogSize", QSize(400, 250)).toSize(); QSize size = config()->get("GUI/AutoTypeSelectDialogSize", QSize(600, 250)).toSize();
size.setWidth(qMin(size.width(), screenGeometry.width())); size.setWidth(qMin(size.width(), screenGeometry.width()));
size.setHeight(qMin(size.height(), screenGeometry.height())); size.setHeight(qMin(size.height(), screenGeometry.height()));
resize(size); resize(size);
@ -56,10 +58,10 @@ AutoTypeSelectDialog::AutoTypeSelectDialog(QWidget* parent)
QLabel* descriptionLabel = new QLabel(tr("Select entry to Auto-Type:"), this); QLabel* descriptionLabel = new QLabel(tr("Select entry to Auto-Type:"), this);
layout->addWidget(descriptionLabel); layout->addWidget(descriptionLabel);
connect(m_view, SIGNAL(activated(QModelIndex)), SLOT(emitEntryActivated(QModelIndex))); connect(m_view, SIGNAL(activated(QModelIndex)), SLOT(emitMatchActivated(QModelIndex)));
connect(m_view, SIGNAL(clicked(QModelIndex)), SLOT(emitEntryActivated(QModelIndex))); connect(m_view, SIGNAL(clicked(QModelIndex)), SLOT(emitMatchActivated(QModelIndex)));
connect(m_view->model(), SIGNAL(rowsRemoved(QModelIndex,int,int)), SLOT(matchRemoved()));
connect(m_view, SIGNAL(rejected()), SLOT(reject())); connect(m_view, SIGNAL(rejected()), SLOT(reject()));
connect(m_view->model(), SIGNAL(rowsRemoved(QModelIndex,int,int)), SLOT(entryRemoved()));
layout->addWidget(m_view); layout->addWidget(m_view);
QDialogButtonBox* buttonBox = new QDialogButtonBox(QDialogButtonBox::Cancel, Qt::Horizontal, this); QDialogButtonBox* buttonBox = new QDialogButtonBox(QDialogButtonBox::Cancel, Qt::Horizontal, this);
@ -67,10 +69,9 @@ AutoTypeSelectDialog::AutoTypeSelectDialog(QWidget* parent)
layout->addWidget(buttonBox); layout->addWidget(buttonBox);
} }
void AutoTypeSelectDialog::setEntries(const QList<Entry*>& entries, const QHash<Entry*, QString>& sequences) void AutoTypeSelectDialog::setMatchList(const QList<AutoTypeMatch>& matchList)
{ {
m_sequences = sequences; m_view->setMatchList(matchList);
m_view->setEntryList(entries);
m_view->header()->resizeSections(QHeaderView::ResizeToContents); m_view->header()->resizeSections(QHeaderView::ResizeToContents);
} }
@ -82,20 +83,20 @@ void AutoTypeSelectDialog::done(int r)
QDialog::done(r); QDialog::done(r);
} }
void AutoTypeSelectDialog::emitEntryActivated(const QModelIndex& index) void AutoTypeSelectDialog::emitMatchActivated(const QModelIndex& index)
{ {
// make sure we don't emit the signal twice when both activated() and clicked() are triggered // make sure we don't emit the signal twice when both activated() and clicked() are triggered
if (m_entryActivatedEmitted) { if (m_matchActivatedEmitted) {
return; return;
} }
m_entryActivatedEmitted = true; m_matchActivatedEmitted = true;
Entry* entry = m_view->entryFromIndex(index); AutoTypeMatch match = m_view->matchFromIndex(index);
accept(); accept();
emit entryActivated(entry, m_sequences[entry]); emit matchActivated(match);
} }
void AutoTypeSelectDialog::entryRemoved() void AutoTypeSelectDialog::matchRemoved()
{ {
if (m_view->model()->rowCount() == 0) { if (m_view->model()->rowCount() == 0) {
reject(); reject();

View File

@ -22,8 +22,9 @@
#include <QDialog> #include <QDialog>
#include <QHash> #include <QHash>
#include "core/AutoTypeMatch.h"
class AutoTypeSelectView; class AutoTypeSelectView;
class Entry;
class AutoTypeSelectDialog : public QDialog class AutoTypeSelectDialog : public QDialog
{ {
@ -31,22 +32,21 @@ class AutoTypeSelectDialog : public QDialog
public: public:
explicit AutoTypeSelectDialog(QWidget* parent = nullptr); explicit AutoTypeSelectDialog(QWidget* parent = nullptr);
void setEntries(const QList<Entry*>& entries, const QHash<Entry*, QString>& sequences); void setMatchList(const QList<AutoTypeMatch>& matchList);
signals: signals:
void entryActivated(Entry* entry, const QString& sequence); void matchActivated(AutoTypeMatch match);
public slots: public slots:
void done(int r) override; void done(int r) override;
private slots: private slots:
void emitEntryActivated(const QModelIndex& index); void emitMatchActivated(const QModelIndex& index);
void entryRemoved(); void matchRemoved();
private: private:
AutoTypeSelectView* const m_view; AutoTypeSelectView* const m_view;
QHash<Entry*, QString> m_sequences; bool m_matchActivatedEmitted;
bool m_entryActivatedEmitted;
}; };
#endif // KEEPASSX_AUTOTYPESELECTDIALOG_H #endif // KEEPASSX_AUTOTYPESELECTDIALOG_H

View File

@ -21,15 +21,12 @@
#include <QMouseEvent> #include <QMouseEvent>
AutoTypeSelectView::AutoTypeSelectView(QWidget* parent) AutoTypeSelectView::AutoTypeSelectView(QWidget* parent)
: EntryView(parent) : AutoTypeMatchView(parent)
{ {
hideColumn(3);
setMouseTracking(true); setMouseTracking(true);
setAllColumnsShowFocus(true); setAllColumnsShowFocus(true);
setDragEnabled(false);
setSelectionMode(QAbstractItemView::SingleSelection);
connect(model(), SIGNAL(modelReset()), SLOT(selectFirstEntry())); connect(model(), SIGNAL(modelReset()), SLOT(selectFirstMatch()));
} }
void AutoTypeSelectView::mouseMoveEvent(QMouseEvent* event) void AutoTypeSelectView::mouseMoveEvent(QMouseEvent* event)
@ -44,10 +41,10 @@ void AutoTypeSelectView::mouseMoveEvent(QMouseEvent* event)
unsetCursor(); unsetCursor();
} }
EntryView::mouseMoveEvent(event); AutoTypeMatchView::mouseMoveEvent(event);
} }
void AutoTypeSelectView::selectFirstEntry() void AutoTypeSelectView::selectFirstMatch()
{ {
QModelIndex index = model()->index(0, 0); QModelIndex index = model()->index(0, 0);

View File

@ -18,11 +18,9 @@
#ifndef KEEPASSX_AUTOTYPESELECTVIEW_H #ifndef KEEPASSX_AUTOTYPESELECTVIEW_H
#define KEEPASSX_AUTOTYPESELECTVIEW_H #define KEEPASSX_AUTOTYPESELECTVIEW_H
#include "gui/entry/EntryView.h" #include "gui/entry/AutoTypeMatchView.h"
class Entry; class AutoTypeSelectView : public AutoTypeMatchView
class AutoTypeSelectView : public EntryView
{ {
Q_OBJECT Q_OBJECT
@ -34,7 +32,7 @@ protected:
void keyReleaseEvent(QKeyEvent* e) override; void keyReleaseEvent(QKeyEvent* e) override;
private slots: private slots:
void selectFirstEntry(); void selectFirstMatch();
signals: signals:
void rejected(); void rejected();

View File

@ -0,0 +1,39 @@
/*
* Copyright (C) 2015 David Wu <lightvector@gmail.com>
* 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 "AutoTypeMatch.h"
AutoTypeMatch::AutoTypeMatch()
: entry(nullptr),
sequence()
{}
AutoTypeMatch::AutoTypeMatch(Entry* entry, QString sequence)
: entry(entry),
sequence(sequence)
{}
bool AutoTypeMatch::operator==(const AutoTypeMatch& other) const
{
return entry == other.entry && sequence == other.sequence;
}
bool AutoTypeMatch::operator!=(const AutoTypeMatch& other) const
{
return entry != other.entry || sequence != other.sequence;
}

41
src/core/AutoTypeMatch.h Normal file
View File

@ -0,0 +1,41 @@
/*
* Copyright (C) 2015 David Wu <lightvector@gmail.com>
* 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 KEEPASSX_AUTOTYPEMATCH_H
#define KEEPASSX_AUTOTYPEMATCH_H
#include <QObject>
#include <QString>
class Entry;
struct AutoTypeMatch
{
Entry* entry;
QString sequence;
AutoTypeMatch();
AutoTypeMatch(Entry* entry, QString sequence);
bool operator==(const AutoTypeMatch& other) const;
bool operator!=(const AutoTypeMatch& other) const;
};
Q_DECLARE_TYPEINFO(AutoTypeMatch, Q_MOVABLE_TYPE);
#endif // KEEPASSX_AUTOTYPEMATCH_H

View File

@ -29,6 +29,8 @@
const int Entry::DefaultIconNumber = 0; const int Entry::DefaultIconNumber = 0;
const int Entry::ResolveMaximumDepth = 10; const int Entry::ResolveMaximumDepth = 10;
const QString Entry::AutoTypeSequenceUsername = "{USERNAME}{ENTER}";
const QString Entry::AutoTypeSequencePassword = "{PASSWORD}{ENTER}";
Entry::Entry() Entry::Entry()
@ -218,30 +220,37 @@ QString Entry::defaultAutoTypeSequence() const
return m_data.defaultAutoTypeSequence; return m_data.defaultAutoTypeSequence;
} }
/**
* Determine the effective sequence that will be injected
* This function return an empty string if a parent group has autotype disabled or if the entry has no parent
*/
QString Entry::effectiveAutoTypeSequence() const QString Entry::effectiveAutoTypeSequence() const
{ {
if (!autoTypeEnabled()) {
return {};
}
const Group* parent = group();
if (!parent) {
return {};
}
QString sequence = parent->effectiveAutoTypeSequence();
if (sequence.isEmpty()) {
return {};
}
if (!m_data.defaultAutoTypeSequence.isEmpty()) { if (!m_data.defaultAutoTypeSequence.isEmpty()) {
return m_data.defaultAutoTypeSequence; return m_data.defaultAutoTypeSequence;
} }
QString sequence;
const Group* grp = group(); if (sequence == Group::RootAutoTypeSequence && (!username().isEmpty() || !password().isEmpty())) {
if(grp) {
sequence = grp->effectiveAutoTypeSequence();
} else {
return QString();
}
if (sequence.isEmpty() && (!username().isEmpty() || !password().isEmpty())) {
if (username().isEmpty()) { if (username().isEmpty()) {
sequence = "{PASSWORD}{ENTER}"; return AutoTypeSequencePassword;
} } else if (password().isEmpty()) {
else if (password().isEmpty()) { return AutoTypeSequenceUsername;
sequence = "{USERNAME}{ENTER}";
}
else {
sequence = "{USERNAME}{TAB}{PASSWORD}{ENTER}";
} }
return Group::RootAutoTypeSequence;
} }
return sequence; return sequence;

View File

@ -85,6 +85,7 @@ public:
int autoTypeObfuscation() const; int autoTypeObfuscation() const;
QString defaultAutoTypeSequence() const; QString defaultAutoTypeSequence() const;
QString effectiveAutoTypeSequence() const; QString effectiveAutoTypeSequence() const;
QString effectiveNewAutoTypeSequence() const;
AutoTypeAssociations* autoTypeAssociations(); AutoTypeAssociations* autoTypeAssociations();
const AutoTypeAssociations* autoTypeAssociations() const; const AutoTypeAssociations* autoTypeAssociations() const;
QString title() const; QString title() const;
@ -109,6 +110,8 @@ public:
static const int DefaultIconNumber; static const int DefaultIconNumber;
static const int ResolveMaximumDepth; static const int ResolveMaximumDepth;
static const QString AutoTypeSequenceUsername;
static const QString AutoTypeSequencePassword;
void setUuid(const Uuid& uuid); void setUuid(const Uuid& uuid);
void setIcon(int iconNumber); void setIcon(int iconNumber);

View File

@ -25,6 +25,7 @@
const int Group::DefaultIconNumber = 48; const int Group::DefaultIconNumber = 48;
const int Group::RecycleBinIconNumber = 43; const int Group::RecycleBinIconNumber = 43;
const QString Group::RootAutoTypeSequence = "{USERNAME}{TAB}{PASSWORD}{ENTER}";
Group::CloneFlags Group::DefaultCloneFlags = static_cast<Group::CloneFlags>( Group::CloneFlags Group::DefaultCloneFlags = static_cast<Group::CloneFlags>(
Group::CloneNewUuid | Group::CloneResetTimeInfo | Group::CloneIncludeEntries); Group::CloneNewUuid | Group::CloneResetTimeInfo | Group::CloneIncludeEntries);
@ -192,6 +193,10 @@ QString Group::defaultAutoTypeSequence() const
return m_data.defaultAutoTypeSequence; return m_data.defaultAutoTypeSequence;
} }
/**
* Determine the effective sequence that will be injected
* This function return an empty string if the current group or any parent has autotype disabled
*/
QString Group::effectiveAutoTypeSequence() const QString Group::effectiveAutoTypeSequence() const
{ {
QString sequence; QString sequence;
@ -207,7 +212,7 @@ QString Group::effectiveAutoTypeSequence() const
} while (group && sequence.isEmpty()); } while (group && sequence.isEmpty());
if (sequence.isEmpty()) { if (sequence.isEmpty()) {
sequence = "{USERNAME}{TAB}{PASSWORD}{ENTER}"; sequence = RootAutoTypeSequence;
} }
return sequence; return sequence;

View File

@ -88,6 +88,7 @@ public:
static const int RecycleBinIconNumber; static const int RecycleBinIconNumber;
static CloneFlags DefaultCloneFlags; static CloneFlags DefaultCloneFlags;
static Entry::CloneFlags DefaultEntryCloneFlags; static Entry::CloneFlags DefaultEntryCloneFlags;
static const QString RootAutoTypeSequence;
Group* findChildByName(const QString& name); Group* findChildByName(const QString& name);
Group* findChildByUuid(const Uuid& uuid); Group* findChildByUuid(const Uuid& uuid);

View File

@ -0,0 +1,201 @@
/*
* Copyright (C) 2015 David Wu <lightvector@gmail.com>
* 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 "AutoTypeMatchModel.h"
#include <QFont>
#include "core/DatabaseIcons.h"
#include "core/Entry.h"
#include "core/Global.h"
#include "core/Group.h"
#include "core/Metadata.h"
AutoTypeMatchModel::AutoTypeMatchModel(QObject* parent)
: QAbstractTableModel(parent)
{
}
AutoTypeMatch AutoTypeMatchModel::matchFromIndex(const QModelIndex& index) const
{
Q_ASSERT(index.isValid() && index.row() < m_matches.size());
return m_matches.at(index.row());
}
QModelIndex AutoTypeMatchModel::indexFromMatch(AutoTypeMatch match) const
{
int row = m_matches.indexOf(match);
Q_ASSERT(row != -1);
return index(row, 1);
}
void AutoTypeMatchModel::setMatchList(const QList<AutoTypeMatch>& matches)
{
beginResetModel();
severConnections();
m_allGroups.clear();
m_matches = matches;
QSet<Database*> databases;
for (AutoTypeMatch& match : m_matches) {
databases.insert(match.entry->group()->database());
}
for (Database* db : asConst(databases)) {
Q_ASSERT(db);
for (const Group* group : db->rootGroup()->groupsRecursive(true)) {
m_allGroups.append(group);
}
if (db->metadata()->recycleBin()) {
m_allGroups.removeOne(db->metadata()->recycleBin());
}
}
for (const Group* group : asConst(m_allGroups)) {
makeConnections(group);
}
endResetModel();
}
int AutoTypeMatchModel::rowCount(const QModelIndex& parent) const
{
if (parent.isValid()) {
return 0;
}
return m_matches.size();
}
int AutoTypeMatchModel::columnCount(const QModelIndex& parent) const
{
Q_UNUSED(parent);
return 4;
}
QVariant AutoTypeMatchModel::data(const QModelIndex& index, int role) const
{
if (!index.isValid()) {
return {};
}
AutoTypeMatch match = matchFromIndex(index);
if (role == Qt::DisplayRole) {
QString result;
switch (index.column()) {
case ParentGroup:
if (match.entry->group()) {
return match.entry->group()->name();
}
break;
case Title:
return match.entry->resolveMultiplePlaceholders(match.entry->title());
case Username:
return match.entry->resolveMultiplePlaceholders(match.entry->username());
case Sequence:
return match.sequence;
}
} else if (role == Qt::DecorationRole) {
switch (index.column()) {
case ParentGroup:
if (match.entry->group()) {
return match.entry->group()->iconScaledPixmap();
}
break;
case Title:
if (match.entry->isExpired()) {
return databaseIcons()->iconPixmap(DatabaseIcons::ExpiredIconIndex);
} else {
return match.entry->iconScaledPixmap();
}
}
} else if (role == Qt::FontRole) {
QFont font;
if (match.entry->isExpired()) {
font.setStrikeOut(true);
}
return font;
}
return {};
}
QVariant AutoTypeMatchModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
switch (section) {
case ParentGroup:
return tr("Group");
case Title:
return tr("Title");
case Username:
return tr("Username");
case Sequence:
return tr("Sequence");
}
}
return {};
}
void AutoTypeMatchModel::entryDataChanged(Entry* entry)
{
for (int row = 0; row < m_matches.size(); ++row) {
AutoTypeMatch match = m_matches[row];
if (match.entry == entry) {
emit dataChanged(index(row, 0), index(row, columnCount()-1));
}
}
}
void AutoTypeMatchModel::entryAboutToRemove(Entry* entry)
{
for (int row = 0; row < m_matches.size(); ++row) {
AutoTypeMatch match = m_matches[row];
if (match.entry == entry) {
beginRemoveRows(QModelIndex(), row, row);
m_matches.removeAt(row);
endRemoveRows();
--row;
}
}
}
void AutoTypeMatchModel::entryRemoved()
{
}
void AutoTypeMatchModel::severConnections()
{
for (const Group* group : asConst(m_allGroups)) {
disconnect(group, nullptr, this, nullptr);
}
}
void AutoTypeMatchModel::makeConnections(const Group* group)
{
connect(group, SIGNAL(entryAboutToRemove(Entry*)), SLOT(entryAboutToRemove(Entry*)));
connect(group, SIGNAL(entryRemoved(Entry*)), SLOT(entryRemoved()));
connect(group, SIGNAL(entryDataChanged(Entry*)), SLOT(entryDataChanged(Entry*)));
}

View File

@ -0,0 +1,66 @@
/*
* Copyright (C) 2015 David Wu <lightvector@gmail.com>
* 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 KEEPASSX_AUTOTYPEMATCHMODEL_H
#define KEEPASSX_AUTOTYPEMATCHMODEL_H
#include <QAbstractTableModel>
#include "core/AutoTypeMatch.h"
class Entry;
class Group;
class AutoTypeMatchModel : public QAbstractTableModel
{
Q_OBJECT
public:
enum ModelColumn
{
ParentGroup = 0,
Title = 1,
Username = 2,
Sequence = 3
};
explicit AutoTypeMatchModel(QObject* parent = nullptr);
AutoTypeMatch matchFromIndex(const QModelIndex& index) const;
QModelIndex indexFromMatch(AutoTypeMatch match) const;
int rowCount(const QModelIndex& parent = QModelIndex()) const override;
int columnCount(const QModelIndex& parent = QModelIndex()) const override;
QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
void setMatchList(const QList<AutoTypeMatch>& matches);
private slots:
void entryAboutToRemove(Entry* entry);
void entryRemoved();
void entryDataChanged(Entry* entry);
private:
void severConnections();
void makeConnections(const Group* group);
QList<AutoTypeMatch> m_matches;
QList<const Group*> m_allGroups;
};
#endif // KEEPASSX_AUTOTYPEMATCHMODEL_H

View File

@ -0,0 +1,112 @@
/*
* Copyright (C) 2015 David Wu <lightvector@gmail.com>
* 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 "AutoTypeMatchView.h"
#include <QHeaderView>
#include <QKeyEvent>
#include "gui/SortFilterHideProxyModel.h"
AutoTypeMatchView::AutoTypeMatchView(QWidget* parent)
: QTreeView(parent)
, m_model(new AutoTypeMatchModel(this))
, m_sortModel(new SortFilterHideProxyModel(this))
{
m_sortModel->setSourceModel(m_model);
m_sortModel->setDynamicSortFilter(true);
m_sortModel->setSortLocaleAware(true);
m_sortModel->setSortCaseSensitivity(Qt::CaseInsensitive);
QTreeView::setModel(m_sortModel);
setUniformRowHeights(true);
setRootIsDecorated(false);
setAlternatingRowColors(true);
setDragEnabled(false);
setSortingEnabled(true);
setSelectionMode(QAbstractItemView::SingleSelection);
header()->setDefaultSectionSize(150);
connect(this, SIGNAL(doubleClicked(QModelIndex)), SLOT(emitMatchActivated(QModelIndex)));
connect(selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)), SIGNAL(matchSelectionChanged()));
}
void AutoTypeMatchView::keyPressEvent(QKeyEvent* event)
{
if ((event->key() == Qt::Key_Enter || event->key() == Qt::Key_Return) && currentIndex().isValid()) {
emitMatchActivated(currentIndex());
#ifdef Q_OS_MAC
// Pressing return does not emit the QTreeView::activated signal on mac os
emit activated(currentIndex());
#endif
}
QTreeView::keyPressEvent(event);
}
void AutoTypeMatchView::setMatchList(const QList<AutoTypeMatch>& matches)
{
m_model->setMatchList(matches);
for (int i = 0; i < m_model->columnCount(); ++i) {
resizeColumnToContents(i);
if (columnWidth(i) > 250) {
setColumnWidth(i, 250);
}
}
setFirstMatchActive();
}
void AutoTypeMatchView::setFirstMatchActive()
{
if (m_model->rowCount() > 0) {
QModelIndex index = m_sortModel->mapToSource(m_sortModel->index(0, 0));
setCurrentMatch(m_model->matchFromIndex(index));
} else {
emit matchSelectionChanged();
}
}
void AutoTypeMatchView::emitMatchActivated(const QModelIndex& index)
{
AutoTypeMatch match = matchFromIndex(index);
emit matchActivated(match);
}
AutoTypeMatch AutoTypeMatchView::currentMatch()
{
QModelIndexList list = selectionModel()->selectedRows();
if (list.size() == 1) {
return m_model->matchFromIndex(m_sortModel->mapToSource(list.first()));
}
return AutoTypeMatch();
}
void AutoTypeMatchView::setCurrentMatch(AutoTypeMatch match)
{
selectionModel()->setCurrentIndex(m_sortModel->mapFromSource(m_model->indexFromMatch(match)),
QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
}
AutoTypeMatch AutoTypeMatchView::matchFromIndex(const QModelIndex& index)
{
if (index.isValid()) {
return m_model->matchFromIndex(m_sortModel->mapToSource(index));
}
return AutoTypeMatch();
}

View File

@ -0,0 +1,57 @@
/*
* Copyright (C) 2015 David Wu <lightvector@gmail.com>
* 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 KEEPASSX_AUTOTYPEMATCHVIEW_H
#define KEEPASSX_AUTOTYPEMATCHVIEW_H
#include <QTreeView>
#include "core/AutoTypeMatch.h"
#include "gui/entry/AutoTypeMatchModel.h"
class SortFilterHideProxyModel;
class AutoTypeMatchView : public QTreeView
{
Q_OBJECT
public:
explicit AutoTypeMatchView(QWidget* parent = nullptr);
AutoTypeMatch currentMatch();
void setCurrentMatch(AutoTypeMatch match);
AutoTypeMatch matchFromIndex(const QModelIndex& index);
void setMatchList(const QList<AutoTypeMatch>& matches);
void setFirstMatchActive();
signals:
void matchActivated(AutoTypeMatch match);
void matchSelectionChanged();
protected:
void keyPressEvent(QKeyEvent* event) override;
private slots:
void emitMatchActivated(const QModelIndex& index);
private:
AutoTypeMatchModel* const m_model;
SortFilterHideProxyModel* const m_sortModel;
};
#endif // KEEPASSX_AUTOTYPEMATCHVIEW_H

View File

@ -170,8 +170,6 @@ void EditEntryWidget::setupAutoType()
m_autoTypeDefaultSequenceGroup->addButton(m_autoTypeUi->inheritSequenceButton); m_autoTypeDefaultSequenceGroup->addButton(m_autoTypeUi->inheritSequenceButton);
m_autoTypeDefaultSequenceGroup->addButton(m_autoTypeUi->customSequenceButton); m_autoTypeDefaultSequenceGroup->addButton(m_autoTypeUi->customSequenceButton);
m_autoTypeWindowSequenceGroup->addButton(m_autoTypeUi->defaultWindowSequenceButton);
m_autoTypeWindowSequenceGroup->addButton(m_autoTypeUi->customWindowSequenceButton);
m_autoTypeAssocModel->setAutoTypeAssociations(m_autoTypeAssoc); m_autoTypeAssocModel->setAutoTypeAssociations(m_autoTypeAssoc);
m_autoTypeUi->assocView->setModel(m_autoTypeAssocModel); m_autoTypeUi->assocView->setModel(m_autoTypeAssocModel);
m_autoTypeUi->assocView->setColumnHidden(1, true); m_autoTypeUi->assocView->setColumnHidden(1, true);
@ -190,8 +188,6 @@ void EditEntryWidget::setupAutoType()
connect(m_autoTypeAssocModel, SIGNAL(modelReset()), SLOT(clearCurrentAssoc())); connect(m_autoTypeAssocModel, SIGNAL(modelReset()), SLOT(clearCurrentAssoc()));
connect(m_autoTypeUi->windowTitleCombo, SIGNAL(editTextChanged(QString)), connect(m_autoTypeUi->windowTitleCombo, SIGNAL(editTextChanged(QString)),
SLOT(applyCurrentAssoc())); SLOT(applyCurrentAssoc()));
connect(m_autoTypeUi->defaultWindowSequenceButton, SIGNAL(toggled(bool)),
SLOT(applyCurrentAssoc()));
connect(m_autoTypeUi->windowSequenceEdit, SIGNAL(textChanged(QString)), connect(m_autoTypeUi->windowSequenceEdit, SIGNAL(textChanged(QString)),
SLOT(applyCurrentAssoc())); SLOT(applyCurrentAssoc()));
} }
@ -644,7 +640,7 @@ void EditEntryWidget::setForms(const Entry* entry, bool restore)
} }
m_autoTypeUi->sequenceEdit->setText(entry->effectiveAutoTypeSequence()); m_autoTypeUi->sequenceEdit->setText(entry->effectiveAutoTypeSequence());
m_autoTypeUi->windowTitleCombo->lineEdit()->clear(); m_autoTypeUi->windowTitleCombo->lineEdit()->clear();
m_autoTypeUi->defaultWindowSequenceButton->setChecked(true); m_autoTypeUi->customWindowSequenceButton->setChecked(false);
m_autoTypeUi->windowSequenceEdit->setText(""); m_autoTypeUi->windowSequenceEdit->setText("");
m_autoTypeAssoc->copyDataFrom(entry->autoTypeAssociations()); m_autoTypeAssoc->copyDataFrom(entry->autoTypeAssociations());
m_autoTypeAssocModel->setEntry(entry); m_autoTypeAssocModel->setEntry(entry);
@ -998,7 +994,6 @@ void EditEntryWidget::updateAutoTypeEnabled()
m_autoTypeUi->windowTitleLabel->setEnabled(autoTypeEnabled && validIndex); m_autoTypeUi->windowTitleLabel->setEnabled(autoTypeEnabled && validIndex);
m_autoTypeUi->windowTitleCombo->setEnabled(autoTypeEnabled && validIndex); m_autoTypeUi->windowTitleCombo->setEnabled(autoTypeEnabled && validIndex);
m_autoTypeUi->defaultWindowSequenceButton->setEnabled(!m_history && autoTypeEnabled && validIndex);
m_autoTypeUi->customWindowSequenceButton->setEnabled(!m_history && autoTypeEnabled && validIndex); m_autoTypeUi->customWindowSequenceButton->setEnabled(!m_history && autoTypeEnabled && validIndex);
m_autoTypeUi->windowSequenceEdit->setEnabled(autoTypeEnabled && validIndex m_autoTypeUi->windowSequenceEdit->setEnabled(autoTypeEnabled && validIndex
&& m_autoTypeUi->customWindowSequenceButton->isChecked()); && m_autoTypeUi->customWindowSequenceButton->isChecked());
@ -1029,16 +1024,15 @@ void EditEntryWidget::loadCurrentAssoc(const QModelIndex& current)
AutoTypeAssociations::Association assoc = m_autoTypeAssoc->get(current.row()); AutoTypeAssociations::Association assoc = m_autoTypeAssoc->get(current.row());
m_autoTypeUi->windowTitleCombo->setEditText(assoc.window); m_autoTypeUi->windowTitleCombo->setEditText(assoc.window);
if (assoc.sequence.isEmpty()) { if (assoc.sequence.isEmpty()) {
m_autoTypeUi->defaultWindowSequenceButton->setChecked(true); m_autoTypeUi->customWindowSequenceButton->setChecked(false);
} m_autoTypeUi->windowSequenceEdit->setText(m_entry->effectiveAutoTypeSequence());
else { } else {
m_autoTypeUi->customWindowSequenceButton->setChecked(true); m_autoTypeUi->customWindowSequenceButton->setChecked(true);
m_autoTypeUi->windowSequenceEdit->setText(assoc.sequence);
} }
m_autoTypeUi->windowSequenceEdit->setText(assoc.sequence);
updateAutoTypeEnabled(); updateAutoTypeEnabled();
} } else {
else {
clearCurrentAssoc(); clearCurrentAssoc();
} }
} }
@ -1047,7 +1041,7 @@ void EditEntryWidget::clearCurrentAssoc()
{ {
m_autoTypeUi->windowTitleCombo->setEditText(""); m_autoTypeUi->windowTitleCombo->setEditText("");
m_autoTypeUi->defaultWindowSequenceButton->setChecked(true); m_autoTypeUi->customWindowSequenceButton->setChecked(false);
m_autoTypeUi->windowSequenceEdit->setText(""); m_autoTypeUi->windowSequenceEdit->setText("");
updateAutoTypeEnabled(); updateAutoTypeEnabled();

View File

@ -203,16 +203,9 @@
</spacer> </spacer>
</item> </item>
<item> <item>
<widget class="QRadioButton" name="defaultWindowSequenceButton"> <widget class="QCheckBox" name="customWindowSequenceButton">
<property name="text"> <property name="text">
<string>Use default se&amp;quence</string> <string>Use a specific sequence for this association:</string>
</property>
</widget>
</item>
<item>
<widget class="QRadioButton" name="customWindowSequenceButton">
<property name="text">
<string>Set custo&amp;m sequence:</string>
</property> </property>
</widget> </widget>
</item> </item>
@ -277,7 +270,6 @@
<tabstop>sequenceEdit</tabstop> <tabstop>sequenceEdit</tabstop>
<tabstop>assocView</tabstop> <tabstop>assocView</tabstop>
<tabstop>windowTitleCombo</tabstop> <tabstop>windowTitleCombo</tabstop>
<tabstop>defaultWindowSequenceButton</tabstop>
<tabstop>customWindowSequenceButton</tabstop> <tabstop>customWindowSequenceButton</tabstop>
<tabstop>windowSequenceEdit</tabstop> <tabstop>windowSequenceEdit</tabstop>
<tabstop>assocAddButton</tabstop> <tabstop>assocAddButton</tabstop>

View File

@ -136,7 +136,7 @@ void TestAutoType::testInternal()
QCOMPARE(m_platform->activeWindowTitle(), QString("Test")); QCOMPARE(m_platform->activeWindowTitle(), QString("Test"));
} }
void TestAutoType::testAutoTypeWithoutSequence() void TestAutoType::testSingleAutoType()
{ {
m_autoType->performAutoType(m_entry1, nullptr); m_autoType->performAutoType(m_entry1, nullptr);
@ -147,17 +147,6 @@ void TestAutoType::testAutoTypeWithoutSequence()
.arg(m_test->keyToString(Qt::Key_Enter))); .arg(m_test->keyToString(Qt::Key_Enter)));
} }
void TestAutoType::testAutoTypeWithSequence()
{
m_autoType->performAutoType(m_entry1, nullptr, "{Username}abc{PaSsWoRd}");
QCOMPARE(m_test->actionCount(), 15);
QCOMPARE(m_test->actionChars(),
QString("%1abc%2")
.arg(m_entry1->username())
.arg(m_entry1->password()));
}
void TestAutoType::testGlobalAutoTypeWithNoMatch() void TestAutoType::testGlobalAutoTypeWithNoMatch()
{ {
m_test->setActiveWindowTitle("nomatch"); m_test->setActiveWindowTitle("nomatch");
@ -310,4 +299,82 @@ void TestAutoType::testAutoTypeSyntaxChecks()
QCOMPARE(true, AutoType::checkHighRepetition("{LEFT 50000000}")); QCOMPARE(true, AutoType::checkHighRepetition("{LEFT 50000000}"));
QCOMPARE(false, AutoType::checkHighRepetition("{SPACE 10}{TAB 3}{RIGHT 50}")); QCOMPARE(false, AutoType::checkHighRepetition("{SPACE 10}{TAB 3}{RIGHT 50}"));
QCOMPARE(false, AutoType::checkHighRepetition("{delay 5000000000}")); QCOMPARE(false, AutoType::checkHighRepetition("{delay 5000000000}"));
}
void TestAutoType::testAutoTypeEffectiveSequences()
{
QString defaultSequence("{USERNAME}{TAB}{PASSWORD}{ENTER}");
QString sequenceG1("{TEST_GROUP1}");
QString sequenceG3("{TEST_GROUP3}");
QString sequenceE2("{TEST_ENTRY2}");
QString sequenceDisabled("{TEST_DISABLED}");
QString sequenceOrphan("{TEST_ORPHAN}");
QScopedPointer<Database> db(new Database());
QPointer<Group> rootGroup = db->rootGroup();
// Group with autotype enabled and custom default sequence
QPointer<Group> group1 = new Group();
group1->setParent(rootGroup);
group1->setDefaultAutoTypeSequence(sequenceG1);
// Child group with inherit
QPointer<Group> group2 = new Group();
group2->setParent(group1);
// Group with autotype disabled and custom default sequence
QPointer<Group> group3 = new Group();
group3->setParent(group1);
group3->setAutoTypeEnabled(Group::Disable);
group3->setDefaultAutoTypeSequence(sequenceG3);
QCOMPARE(rootGroup->defaultAutoTypeSequence(), QString());
QCOMPARE(rootGroup->effectiveAutoTypeSequence(), defaultSequence);
QCOMPARE(group1->defaultAutoTypeSequence(), sequenceG1);
QCOMPARE(group1->effectiveAutoTypeSequence(), sequenceG1);
QCOMPARE(group2->defaultAutoTypeSequence(), QString());
QCOMPARE(group2->effectiveAutoTypeSequence(), sequenceG1);
QCOMPARE(group3->defaultAutoTypeSequence(), sequenceG3);
QCOMPARE(group3->effectiveAutoTypeSequence(), QString());
// Entry from root group
QPointer<Entry> entry1 = new Entry();
entry1->setGroup(rootGroup);
// Entry with custom default sequence
QPointer<Entry> entry2 = new Entry();
entry2->setDefaultAutoTypeSequence(sequenceE2);
entry2->setGroup(rootGroup);
// Entry from enabled child group
QPointer<Entry> entry3 = new Entry();
entry3->setGroup(group2);
// Entry from disabled group
QPointer<Entry> entry4 = new Entry();
entry4->setDefaultAutoTypeSequence(sequenceDisabled);
entry4->setGroup(group3);
// Entry from enabled group with disabled autotype
QPointer<Entry> entry5 = new Entry();
entry5->setGroup(group2);
entry5->setDefaultAutoTypeSequence(sequenceDisabled);
entry5->setAutoTypeEnabled(false);
// Entry with no parent
QScopedPointer<Entry> entry6(new Entry());
entry6->setDefaultAutoTypeSequence(sequenceOrphan);
QCOMPARE(entry1->defaultAutoTypeSequence(), QString());
QCOMPARE(entry1->effectiveAutoTypeSequence(), defaultSequence);
QCOMPARE(entry2->defaultAutoTypeSequence(), sequenceE2);
QCOMPARE(entry2->effectiveAutoTypeSequence(), sequenceE2);
QCOMPARE(entry3->defaultAutoTypeSequence(), QString());
QCOMPARE(entry3->effectiveAutoTypeSequence(), sequenceG1);
QCOMPARE(entry4->defaultAutoTypeSequence(), sequenceDisabled);
QCOMPARE(entry4->effectiveAutoTypeSequence(), QString());
QCOMPARE(entry5->defaultAutoTypeSequence(), sequenceDisabled);
QCOMPARE(entry5->effectiveAutoTypeSequence(), QString());
QCOMPARE(entry6->defaultAutoTypeSequence(), sequenceOrphan);
QCOMPARE(entry6->effectiveAutoTypeSequence(), QString());
} }

View File

@ -38,8 +38,7 @@ private slots:
void cleanup(); void cleanup();
void testInternal(); void testInternal();
void testAutoTypeWithoutSequence(); void testSingleAutoType();
void testAutoTypeWithSequence();
void testGlobalAutoTypeWithNoMatch(); void testGlobalAutoTypeWithNoMatch();
void testGlobalAutoTypeWithOneMatch(); void testGlobalAutoTypeWithOneMatch();
void testGlobalAutoTypeTitleMatch(); void testGlobalAutoTypeTitleMatch();
@ -48,6 +47,7 @@ private slots:
void testGlobalAutoTypeTitleMatchDisabled(); void testGlobalAutoTypeTitleMatchDisabled();
void testGlobalAutoTypeRegExp(); void testGlobalAutoTypeRegExp();
void testAutoTypeSyntaxChecks(); void testAutoTypeSyntaxChecks();
void testAutoTypeEffectiveSequences();
private: private:
AutoTypePlatformInterface* m_platform; AutoTypePlatformInterface* m_platform;