add support for multiple autotype sequence, fix #559

This commit is contained in:
thez3ro 2018-01-16 22:05:58 +01:00
parent 065a85e05c
commit b5cabbeb43
No known key found for this signature in database
GPG key ID: F628F9E41DD7C073
13 changed files with 599 additions and 92 deletions

View file

@ -27,6 +27,7 @@
#include "autotype/AutoTypePlatformPlugin.h"
#include "autotype/AutoTypeSelectDialog.h"
#include "autotype/WildcardMatcher.h"
#include "core/AutoTypeMatch.h"
#include "core/Config.h"
#include "core/Database.h"
#include "core/Entry.h"
@ -136,7 +137,12 @@ void AutoType::executeAutoTypeActions(const Entry* entry, QWidget* hideWindow, c
QString sequence;
if (customSequence.isEmpty()) {
sequence = autoTypeSequence(entry);
QList<QString> sequences = autoTypeSequences(entry);
if(sequences.isEmpty()) {
sequence = "";
} else {
sequence = sequences.first();
}
} else {
sequence = customSequence;
}
@ -199,36 +205,36 @@ void AutoType::performGlobalAutoType(const QList<Database*>& dbList)
m_inAutoType = true;
QList<Entry*> entryList;
QHash<Entry*, QString> sequenceHash;
QList<AutoTypeMatch> matchList;
for (Database* db : dbList) {
const QList<Entry*> dbEntries = db->rootGroup()->entriesRecursive();
for (Entry* entry : dbEntries) {
QString sequence = autoTypeSequence(entry, windowTitle);
if (!sequence.isEmpty()) {
entryList << entry;
sequenceHash.insert(entry, sequence);
const QList<QString> sequences = autoTypeSequences(entry, windowTitle);
for (QString sequence : sequences) {
if (!sequence.isEmpty()) {
matchList << AutoTypeMatch(entry,sequence);
}
}
}
}
if (entryList.isEmpty()) {
if (matchList.isEmpty()) {
m_inAutoType = false;
QString message = tr("Couldn't find an entry that matches the window title:");
message.append("\n\n");
message.append(windowTitle);
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;
performAutoType(entryList.first(), nullptr, sequenceHash[entryList.first()]);
performAutoType(matchList.first().entry, nullptr, matchList.first().sequence);
} else {
m_windowFromGlobal = m_plugin->activeWindow();
AutoTypeSelectDialog* selectDialog = new AutoTypeSelectDialog();
connect(
selectDialog, SIGNAL(entryActivated(Entry*, QString)), SLOT(performAutoTypeFromGlobal(Entry*, QString)));
connect(selectDialog, SIGNAL(matchActivated(AutoTypeMatch)),
SLOT(performAutoTypeFromGlobal(AutoTypeMatch)));
connect(selectDialog, SIGNAL(rejected()), SLOT(resetInAutoType()));
selectDialog->setEntries(entryList, sequenceHash);
selectDialog->setMatchList(matchList);
#if defined(Q_OS_MAC)
m_plugin->raiseOwnWindow();
Tools::wait(500);
@ -239,7 +245,7 @@ void AutoType::performGlobalAutoType(const QList<Database*>& dbList)
}
}
void AutoType::performAutoTypeFromGlobal(Entry* entry, const QString& sequence)
void AutoType::performAutoTypeFromGlobal(AutoTypeMatch match)
{
Q_ASSERT(m_inAutoType);
@ -247,7 +253,7 @@ void AutoType::performAutoTypeFromGlobal(Entry* entry, const QString& sequence)
m_inAutoType = false;
performAutoType(entry, nullptr, sequence, m_windowFromGlobal);
performAutoType(match.entry, nullptr, match.sequence, m_windowFromGlobal);
}
void AutoType::resetInAutoType()
@ -506,78 +512,56 @@ QList<AutoTypeAction*> AutoType::createActionFromTemplate(const QString& tmpl, c
return list;
}
QString AutoType::autoTypeSequence(const Entry* entry, const QString& windowTitle)
QList<QString> AutoType::autoTypeSequences(const Entry* entry, const QString& windowTitle)
{
QList<QString> sequenceList;
if (!entry->autoTypeEnabled()) {
return QString();
return sequenceList;
}
bool enableSet = false;
QString sequence;
if (!windowTitle.isEmpty()) {
bool match = false;
const QList<AutoTypeAssociations::Association> assocList = entry->autoTypeAssociations()->getAll();
for (const AutoTypeAssociations::Association& assoc : assocList) {
const QString window = entry->resolveMultiplePlaceholders(assoc.window);
if (windowMatches(windowTitle, window)) {
if (!assoc.sequence.isEmpty()) {
sequence = assoc.sequence;
sequenceList.append(assoc.sequence);
} 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()))) {
sequence = entry->defaultAutoTypeSequence();
match = true;
sequenceList.append(entry->effectiveAutoTypeSequence());
}
if (!match && config()->get("AutoTypeEntryURLMatch").toBool() &&
if (config()->get("AutoTypeEntryURLMatch").toBool() &&
windowMatchesUrl(windowTitle, entry->resolvePlaceholder(entry->url()))) {
sequence = entry->defaultAutoTypeSequence();
match = true;
sequenceList.append(entry->effectiveAutoTypeSequence());
}
if (!match) {
return QString();
if (sequenceList.isEmpty()) {
return sequenceList;
}
} else {
sequence = entry->defaultAutoTypeSequence();
sequenceList.append(entry->effectiveAutoTypeSequence());
}
const Group* group = entry->group();
do {
if (!enableSet) {
if (group->autoTypeEnabled() == Group::Disable) {
return QString();
} else if (group->autoTypeEnabled() == Group::Enable) {
enableSet = true;
}
if (group->autoTypeEnabled() == Group::Disable) {
return QList<QString>();
} else if (group->autoTypeEnabled() == Group::Enable) {
return sequenceList;
}
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}";
}
}
} while (group);
return sequence;
return sequenceList;
}
bool AutoType::windowMatches(const QString& windowTitle, const QString& windowPattern)

View file

@ -23,6 +23,8 @@
#include <QStringList>
#include <QWidget>
#include "core/AutoTypeMatch.h"
class AutoTypeAction;
class AutoTypeExecutor;
class AutoTypePlatformInterface;
@ -69,7 +71,7 @@ signals:
void globalShortcutTriggered();
private slots:
void performAutoTypeFromGlobal(Entry* entry, const QString& sequence);
void performAutoTypeFromGlobal(AutoTypeMatch match);
void resetInAutoType();
void unloadPlugin();
@ -79,7 +81,7 @@ private:
void loadPlugin(const QString& pluginPath);
bool parseActions(const QString& sequence, const Entry* entry, QList<AutoTypeAction*>& actions);
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 windowMatchesUrl(const QString& windowTitle, const QString& resolvedUrl);
bool windowMatches(const QString& windowTitle, const QString& windowPattern);

View file

@ -1,5 +1,6 @@
/*
* 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
@ -25,14 +26,15 @@
#include <QVBoxLayout>
#include "autotype/AutoTypeSelectView.h"
#include "core/AutoTypeMatch.h"
#include "core/Config.h"
#include "core/FilePath.h"
#include "gui/entry/EntryModel.h"
#include "gui/entry/AutoTypeMatchModel.h"
AutoTypeSelectDialog::AutoTypeSelectDialog(QWidget* parent)
: QDialog(parent)
, m_view(new AutoTypeSelectView(this))
, m_entryActivatedEmitted(false)
, m_matchActivatedEmitted(false)
{
setAttribute(Qt::WA_DeleteOnClose);
// 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());
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.setHeight(qMin(size.height(), screenGeometry.height()));
resize(size);
@ -56,10 +58,10 @@ AutoTypeSelectDialog::AutoTypeSelectDialog(QWidget* parent)
QLabel* descriptionLabel = new QLabel(tr("Select entry to Auto-Type:"), this);
layout->addWidget(descriptionLabel);
connect(m_view, SIGNAL(activated(QModelIndex)), SLOT(emitEntryActivated(QModelIndex)));
connect(m_view, SIGNAL(clicked(QModelIndex)), SLOT(emitEntryActivated(QModelIndex)));
connect(m_view, SIGNAL(activated(QModelIndex)), SLOT(emitMatchActivated(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->model(), SIGNAL(rowsRemoved(QModelIndex,int,int)), SLOT(entryRemoved()));
layout->addWidget(m_view);
QDialogButtonBox* buttonBox = new QDialogButtonBox(QDialogButtonBox::Cancel, Qt::Horizontal, this);
@ -67,10 +69,9 @@ AutoTypeSelectDialog::AutoTypeSelectDialog(QWidget* parent)
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->setEntryList(entries);
m_view->setMatchList(matchList);
m_view->header()->resizeSections(QHeaderView::ResizeToContents);
}
@ -82,20 +83,20 @@ void AutoTypeSelectDialog::done(int 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
if (m_entryActivatedEmitted) {
if (m_matchActivatedEmitted) {
return;
}
m_entryActivatedEmitted = true;
m_matchActivatedEmitted = true;
Entry* entry = m_view->entryFromIndex(index);
AutoTypeMatch match = m_view->matchFromIndex(index);
accept();
emit entryActivated(entry, m_sequences[entry]);
emit matchActivated(match);
}
void AutoTypeSelectDialog::entryRemoved()
void AutoTypeSelectDialog::matchRemoved()
{
if (m_view->model()->rowCount() == 0) {
reject();

View file

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

View file

@ -21,15 +21,12 @@
#include <QMouseEvent>
AutoTypeSelectView::AutoTypeSelectView(QWidget* parent)
: EntryView(parent)
: AutoTypeMatchView(parent)
{
hideColumn(3);
setMouseTracking(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)
@ -44,10 +41,10 @@ void AutoTypeSelectView::mouseMoveEvent(QMouseEvent* event)
unsetCursor();
}
EntryView::mouseMoveEvent(event);
AutoTypeMatchView::mouseMoveEvent(event);
}
void AutoTypeSelectView::selectFirstEntry()
void AutoTypeSelectView::selectFirstMatch()
{
QModelIndex index = model()->index(0, 0);

View file

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