Skip to content
This repository has been archived by the owner on Oct 10, 2021. It is now read-only.

Commit

Permalink
Merge pull request keepassxreboot#654 from weslly/feature/search-scop…
Browse files Browse the repository at this point in the history
…e-option

Add option to limit search to current group
  • Loading branch information
weslly authored Jun 21, 2017
2 parents a3020b6 + 719323e commit 3c2b4b1
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/core/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ void Config::init(const QString& fileName)
m_defaults.insert("AutoReloadOnChange", true);
m_defaults.insert("AutoSaveOnExit", false);
m_defaults.insert("ShowToolbar", true);
m_defaults.insert("SearchLimitGroup", false);
m_defaults.insert("MinimizeOnCopy", false);
m_defaults.insert("UseGroupIconOnEntryCreation", false);
m_defaults.insert("AutoTypeEntryTitleMatch", true);
Expand Down
11 changes: 10 additions & 1 deletion src/gui/DatabaseWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ DatabaseWidget::DatabaseWidget(Database* db, QWidget* parent)
m_ignoreAutoReload = false;

m_searchCaseSensitive = false;
m_searchLimitGroup = config()->get("SearchLimitGroup", false).toBool();

setCurrentWidget(m_mainWidget);
}
Expand Down Expand Up @@ -963,7 +964,9 @@ void DatabaseWidget::search(const QString& searchtext)

Qt::CaseSensitivity caseSensitive = m_searchCaseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive;

QList<Entry*> searchResult = EntrySearcher().search(searchtext, currentGroup(), caseSensitive);
Group* searchGroup = m_searchLimitGroup ? currentGroup() : m_db->rootGroup();

QList<Entry*> searchResult = EntrySearcher().search(searchtext, searchGroup, caseSensitive);

m_entryView->setEntryList(searchResult);
m_lastSearchText = searchtext;
Expand All @@ -987,6 +990,12 @@ void DatabaseWidget::setSearchCaseSensitive(bool state)
refreshSearch();
}

void DatabaseWidget::setSearchLimitGroup(bool state)
{
m_searchLimitGroup = state;
refreshSearch();
}

void DatabaseWidget::onGroupChanged(Group* group)
{
// Intercept group changes if in search mode
Expand Down
2 changes: 2 additions & 0 deletions src/gui/DatabaseWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ public slots:
// Search related slots
void search(const QString& searchtext);
void setSearchCaseSensitive(bool state);
void setSearchLimitGroup(bool state);
void endSearch();

void showMessage(const QString& text, MessageWidget::MessageType type);
Expand Down Expand Up @@ -221,6 +222,7 @@ private slots:
// Search state
QString m_lastSearchText;
bool m_searchCaseSensitive;
bool m_searchLimitGroup;

// Autoreload
QFileSystemWatcher m_fileWatcher;
Expand Down
21 changes: 21 additions & 0 deletions src/gui/SearchWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <QShortcut>
#include <QToolButton>

#include "core/Config.h"
#include "core/FilePath.h"

SearchWidget::SearchWidget(QWidget* parent)
Expand All @@ -50,6 +51,11 @@ SearchWidget::SearchWidget(QWidget* parent)
m_actionCaseSensitive->setObjectName("actionSearchCaseSensitive");
m_actionCaseSensitive->setCheckable(true);

m_actionLimitGroup = searchMenu->addAction(tr("Limit search to selected group"), this, SLOT(updateLimitGroup()));
m_actionLimitGroup->setObjectName("actionSearchLimitGroup");
m_actionLimitGroup->setCheckable(true);
m_actionLimitGroup->setChecked(config()->get("SearchLimitGroup", false).toBool());

m_ui->searchIcon->setIcon(filePath()->icon("actions", "system-search"));
m_ui->searchIcon->setMenu(searchMenu);
m_ui->searchEdit->addAction(m_ui->searchIcon, QLineEdit::LeadingPosition);
Expand Down Expand Up @@ -102,6 +108,7 @@ void SearchWidget::connectSignals(SignalMultiplexer& mx)
{
mx.connect(this, SIGNAL(search(QString)), SLOT(search(QString)));
mx.connect(this, SIGNAL(caseSensitiveChanged(bool)), SLOT(setSearchCaseSensitive(bool)));
mx.connect(this, SIGNAL(limitGroupChanged(bool)), SLOT(setSearchLimitGroup(bool)));
mx.connect(this, SIGNAL(copyPressed()), SLOT(copyPassword()));
mx.connect(this, SIGNAL(downPressed()), SLOT(setFocus()));
mx.connect(m_ui->searchEdit, SIGNAL(returnPressed()), SLOT(switchToEntryEdit()));
Expand All @@ -115,6 +122,7 @@ void SearchWidget::databaseChanged(DatabaseWidget* dbWidget)

// Enforce search policy
emit caseSensitiveChanged(m_actionCaseSensitive->isChecked());
emit limitGroupChanged(m_actionLimitGroup->isChecked());
} else {
m_ui->searchEdit->clear();
}
Expand Down Expand Up @@ -151,6 +159,19 @@ void SearchWidget::setCaseSensitive(bool state)
updateCaseSensitive();
}

void SearchWidget::updateLimitGroup()
{
config()->set("SearchLimitGroup", m_actionLimitGroup->isChecked());
emit limitGroupChanged(m_actionLimitGroup->isChecked());
}

void SearchWidget::setLimitGroup(bool state)
{
m_actionLimitGroup->setChecked(state);
updateLimitGroup();
}


void SearchWidget::searchFocus()
{
m_ui->searchEdit->setFocus();
Expand Down
4 changes: 4 additions & 0 deletions src/gui/SearchWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,15 @@ class SearchWidget : public QWidget

void connectSignals(SignalMultiplexer& mx);
void setCaseSensitive(bool state);
void setLimitGroup(bool state);

protected:
bool eventFilter(QObject* obj, QEvent* event);

signals:
void search(const QString& text);
void caseSensitiveChanged(bool state);
void limitGroupChanged(bool state);
void escapePressed();
void copyPressed();
void downPressed();
Expand All @@ -58,12 +60,14 @@ private slots:
void startSearchTimer();
void startSearch();
void updateCaseSensitive();
void updateLimitGroup();
void searchFocus();

private:
const QScopedPointer<Ui::SearchWidget> m_ui;
QTimer* m_searchTimer;
QAction* m_actionCaseSensitive;
QAction* m_actionLimitGroup;

Q_DISABLE_COPY(SearchWidget)
};
Expand Down
5 changes: 5 additions & 0 deletions tests/gui/TestGui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,12 @@ void TestGui::testSearch()
QModelIndex rootGroupIndex = groupView->model()->index(0, 0);
clickIndex(groupView->model()->index(0, 0, rootGroupIndex), groupView, Qt::LeftButton);
QCOMPARE(groupView->currentGroup()->name(), QString("General"));

searchWidget->setLimitGroup(false);
QTRY_COMPARE(entryView->model()->rowCount(), 2);
searchWidget->setLimitGroup(true);
QTRY_COMPARE(entryView->model()->rowCount(), 0);

// reset
clickIndex(rootGroupIndex, groupView, Qt::LeftButton);
QCOMPARE(groupView->currentGroup(), m_db->rootGroup());
Expand Down

0 comments on commit 3c2b4b1

Please sign in to comment.