Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More 2.4.0 Code Improvements #2731

Merged
merged 4 commits into from
Feb 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/core/EntryAttributes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@ QString EntryAttributes::value(const QString& key) const
return m_attributes.value(key);
}

QList<QString> EntryAttributes::values(const QList<QString>& keys) const
{
QList<QString> values;
for (const QString& key : keys) {
values.append(m_attributes.value(key));
}
return values;
}

bool EntryAttributes::contains(const QString& key) const
{
return m_attributes.contains(key);
Expand Down
1 change: 1 addition & 0 deletions src/core/EntryAttributes.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class EntryAttributes : public QObject
bool hasKey(const QString& key) const;
QList<QString> customKeys() const;
QString value(const QString& key) const;
QList<QString> values(const QList<QString>& keys) const;
bool contains(const QString& key) const;
bool containsValue(const QString& value) const;
bool isProtected(const QString& key) const;
Expand Down
78 changes: 63 additions & 15 deletions src/core/EntrySearcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,31 +28,83 @@ EntrySearcher::EntrySearcher(bool caseSensitive)
{
}

/**
* Search group, and its children, by parsing the provided search
* string for search terms.
*
* @param searchString search terms
* @param baseGroup group to start search from, cannot be null
* @param forceSearch ignore group search settings
* @return list of entries that match the search terms
*/
QList<Entry*> EntrySearcher::search(const QString& searchString, const Group* baseGroup, bool forceSearch)
{
Q_ASSERT(baseGroup);

parseSearchTerms(searchString);
return repeat(baseGroup, forceSearch);
}

/**
* Repeat the last search starting from the given group
*
* @param baseGroup group to start search from, cannot be null
* @param forceSearch ignore group search settings
* @return list of entries that match the search terms
*/
QList<Entry*> EntrySearcher::repeat(const Group* baseGroup, bool forceSearch)
droidmonkey marked this conversation as resolved.
Show resolved Hide resolved
{
Q_ASSERT(baseGroup);

QList<Entry*> results;
for (const auto group : baseGroup->groupsRecursive(true)) {
if (forceSearch || group->resolveSearchingEnabled()) {
results.append(searchEntries(searchString, group->entries()));
for (auto* entry : group->entries()) {
if (searchEntryImpl(entry)) {
results.append(entry);
}
}
}
}

return results;
}

/**
* Search provided entries by parsing the search string
* for search terms.
*
* @param searchString search terms
* @param entries list of entries to include in the search
* @return list of entries that match the search terms
*/
QList<Entry*> EntrySearcher::searchEntries(const QString& searchString, const QList<Entry*>& entries)
{
parseSearchTerms(searchString);
return repeatEntries(entries);
}

/**
* Repeat the last search on the given entries
*
* @param entries list of entries to include in the search
* @return list of entries that match the search terms
*/
QList<Entry*> EntrySearcher::repeatEntries(const QList<Entry*>& entries)
{
QList<Entry*> results;
for (Entry* entry : entries) {
if (searchEntryImpl(searchString, entry)) {
for (auto* entry : entries) {
if (searchEntryImpl(entry)) {
results.append(entry);
}
}
return results;
}

/**
* Set the next search to be case sensitive or not
*
* @param state
*/
void EntrySearcher::setCaseSensitive(bool state)
{
m_caseSensitive = state;
Expand All @@ -63,16 +115,15 @@ bool EntrySearcher::isCaseSensitive()
return m_caseSensitive;
}

bool EntrySearcher::searchEntryImpl(const QString& searchString, Entry* entry)
bool EntrySearcher::searchEntryImpl(Entry* entry)
{
// Pre-load in case they are needed
auto attributes = QStringList(entry->attributes()->keys());
auto attributes_keys = entry->attributes()->customKeys();
auto attributes = QStringList(attributes_keys + entry->attributes()->values(attributes_keys));
auto attachments = QStringList(entry->attachments()->keys());

bool found;
auto searchTerms = parseSearchTerms(searchString);

for (const auto& term : searchTerms) {
for (const auto& term : m_searchTerms) {
switch (term->field) {
case Field::Title:
found = term->regex.match(entry->resolvePlaceholder(entry->title())).hasMatch();
Expand Down Expand Up @@ -112,10 +163,9 @@ bool EntrySearcher::searchEntryImpl(const QString& searchString, Entry* entry)
return true;
}

QList<QSharedPointer<EntrySearcher::SearchTerm>> EntrySearcher::parseSearchTerms(const QString& searchString)
void EntrySearcher::parseSearchTerms(const QString& searchString)
{
auto terms = QList<QSharedPointer<SearchTerm>>();

m_searchTerms.clear();
auto results = m_termParser.globalMatch(searchString);
while (results.hasNext()) {
auto result = results.next();
Expand Down Expand Up @@ -165,8 +215,6 @@ QList<QSharedPointer<EntrySearcher::SearchTerm>> EntrySearcher::parseSearchTerms
}
}

terms.append(term);
m_searchTerms.append(term);
}

return terms;
}
9 changes: 6 additions & 3 deletions src/core/EntrySearcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@ class EntrySearcher
explicit EntrySearcher(bool caseSensitive = false);

QList<Entry*> search(const QString& searchString, const Group* baseGroup, bool forceSearch = false);
QList<Entry*> repeat(const Group* baseGroup, bool forceSearch = false);

QList<Entry*> searchEntries(const QString& searchString, const QList<Entry*>& entries);
QList<Entry*> repeatEntries(const QList<Entry*>& entries);

void setCaseSensitive(bool state);
bool isCaseSensitive();

private:
bool searchEntryImpl(const QString& searchString, Entry* entry);

enum class Field
{
Undefined,
Expand All @@ -59,10 +60,12 @@ class EntrySearcher
bool exclude;
};

QList<QSharedPointer<SearchTerm>> parseSearchTerms(const QString& searchString);
bool searchEntryImpl(Entry* entry);
void parseSearchTerms(const QString& searchString);

bool m_caseSensitive;
QRegularExpression m_termParser;
QList<QSharedPointer<SearchTerm>> m_searchTerms;

friend class TestEntrySearcher;
};
Expand Down
1 change: 1 addition & 0 deletions src/core/FileWatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ BulkFileWatcher::BulkFileWatcher(QObject* parent)
connect(&m_fileWatchUnblockTimer, SIGNAL(timeout()), this, SLOT(observeFileChanges()));
connect(&m_pendingSignalsTimer, SIGNAL(timeout()), this, SLOT(emitSignals()));
m_fileWatchUnblockTimer.setSingleShot(true);
m_pendingSignalsTimer.setSingleShot(true);
}

void BulkFileWatcher::clear()
Expand Down
6 changes: 5 additions & 1 deletion src/gui/DatabaseOpenWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,11 @@ QSharedPointer<CompositeKey> DatabaseOpenWidget::databaseKey()

#ifdef WITH_XC_TOUCHID
// check if TouchID is available and enabled for unlocking the database
if (m_ui->checkTouchID->isChecked() && TouchID::getInstance().isAvailable() && masterKey->isEmpty()) {
if (m_ui->checkTouchID->isChecked() && TouchID::getInstance().isAvailable()
&& m_ui->editPassword->text().isEmpty()) {
// clear empty password from composite key
masterKey->clear();

// try to get, decrypt and use PasswordKey
QSharedPointer<QByteArray> passwordKey = TouchID::getInstance().getKey(m_filename);
if (passwordKey != NULL) {
Expand Down
1 change: 1 addition & 0 deletions src/gui/EditWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ void EditWidget::addPage(const QString& labelText, const QIcon& icon, QWidget* w
*/
auto* scrollArea = new QScrollArea(m_ui->stackedWidget);
scrollArea->setFrameShape(QFrame::NoFrame);
scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
scrollArea->setWidget(widget);
scrollArea->setWidgetResizable(true);
m_ui->stackedWidget->addWidget(scrollArea);
Expand Down
1 change: 0 additions & 1 deletion src/gui/EditWidgetIcons.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ EditWidgetIcons::EditWidgetIcons(QWidget* parent)
connect(m_ui->deleteButton, SIGNAL(clicked()), SLOT(removeCustomIcon()));
connect(m_ui->faviconButton, SIGNAL(clicked()), SLOT(downloadFavicon()));

connect(m_ui->defaultIconsRadio, SIGNAL(toggled(bool)), this, SIGNAL(widgetUpdated()));
connect(m_ui->defaultIconsRadio, SIGNAL(toggled(bool)), this, SIGNAL(widgetUpdated()));
connect(m_ui->defaultIconsView->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
this, SIGNAL(widgetUpdated()));
Expand Down
2 changes: 1 addition & 1 deletion src/gui/entry/EditEntryWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ void EditEntryWidget::setupMain()
connect(m_mainUi->togglePasswordGeneratorButton, SIGNAL(toggled(bool)), SLOT(togglePasswordGeneratorButton(bool)));
#ifdef WITH_XC_NETWORKING
connect(m_mainUi->fetchFaviconButton, SIGNAL(clicked()), m_iconsWidget, SLOT(downloadFavicon()));
connect(m_mainUi->urlEdit, SIGNAL(textChanged(QString)), m_iconsWidget, SLOT(setUrl(QString)));
#endif
connect(m_mainUi->expireCheck, SIGNAL(toggled(bool)), m_mainUi->expireDatePicker, SLOT(setEnabled(bool)));
connect(m_mainUi->notesEnabled, SIGNAL(toggled(bool)), this, SLOT(toggleHideNotes(bool)));
Expand Down Expand Up @@ -764,7 +765,6 @@ void EditEntryWidget::setForms(Entry* entry, bool restore)
iconStruct.uuid = entry->iconUuid();
iconStruct.number = entry->iconNumber();
m_iconsWidget->load(entry->uuid(), m_db, iconStruct, entry->webUrl());
connect(m_mainUi->urlEdit, SIGNAL(textChanged(QString)), m_iconsWidget, SLOT(setUrl(QString)));

m_autoTypeUi->enableButton->setChecked(entry->autoTypeEnabled());
if (entry->defaultAutoTypeSequence().isEmpty()) {
Expand Down
7 changes: 4 additions & 3 deletions tests/TestEntrySearcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,8 @@ void TestEntrySearcher::testAllAttributesAreSearched()
void TestEntrySearcher::testSearchTermParser()
{
// Test standard search terms
auto terms =
m_entrySearcher.parseSearchTerms("-test \"quoted \\\"string\\\"\" user:user pass:\"test me\" noquote ");
m_entrySearcher.parseSearchTerms("-test \"quoted \\\"string\\\"\" user:user pass:\"test me\" noquote ");
auto terms = m_entrySearcher.m_searchTerms;

QCOMPARE(terms.length(), 5);

Expand All @@ -200,7 +200,8 @@ void TestEntrySearcher::testSearchTermParser()
QCOMPARE(terms[4]->word, QString("noquote"));

// Test wildcard and regex search terms
terms = m_entrySearcher.parseSearchTerms("+url:*.google.com *user:\\d+\\w{2}");
m_entrySearcher.parseSearchTerms("+url:*.google.com *user:\\d+\\w{2}");
terms = m_entrySearcher.m_searchTerms;

QCOMPARE(terms.length(), 2);

Expand Down