Skip to content

Commit

Permalink
feat: allow to configure search history (#364)
Browse files Browse the repository at this point in the history
  • Loading branch information
variar committed Jul 25, 2021
1 parent 02bf08e commit 09419c5
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
10 changes: 9 additions & 1 deletion src/ui/include/savedsearches.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,24 @@ class SavedSearches final : public Persistable<SavedSearches, session_settings>
// Returns a list of recent searches (newer first)
QStringList recentSearches() const;

int historySize() const;
void setHistorySize(int historySize);

void clear();

// Reads/writes the current config in the QSettings object passed
void saveToStorage( QSettings& settings ) const;
void retrieveFromStorage( QSettings& settings );

private:
void trim();

private:
static constexpr int SAVEDSEARCHES_VERSION = 1;
static constexpr int maxNumberOfRecentSearches = 50;

static constexpr int MaxNumberOfRecentSearches = 50;

int historySize_ = MaxNumberOfRecentSearches;
QStringList savedSearches_;
};

Expand Down
8 changes: 8 additions & 0 deletions src/ui/src/optionsdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#include "log.h"
#include "shortcuts.h"
#include "styles.h"
#include "savedsearches.h"

#include "optionsdialog.h"

Expand Down Expand Up @@ -300,6 +301,9 @@ void OptionsDialog::updateDialogFromConfig()
verifySslCheckBox->setChecked( config.verifySslPeers() );

buildShortcutsTable();

const auto& savedSearches = SavedSearches::get();
searchHistorySpinBox->setValue( savedSearches.historySize() );
}

//
Expand Down Expand Up @@ -442,6 +446,10 @@ void OptionsDialog::updateConfigFromDialog()

config.save();

auto& savedSearches = SavedSearches::get();
savedSearches.setHistorySize( searchHistorySpinBox->value() );
savedSearches.save();

emit optionsChanged();
}

Expand Down
26 changes: 20 additions & 6 deletions src/ui/src/savedsearches.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,39 @@ void SavedSearches::addRecent( const QString& text )
if ( text.isEmpty() )
return;

// Remove any copy of the about to be added text
savedSearches_.removeAll( text );

// Add at the front
savedSearches_.push_front( text );

// Trim the list if it's too long
while ( savedSearches_.size() > maxNumberOfRecentSearches )
savedSearches_.pop_back();
trim();
}

QStringList SavedSearches::recentSearches() const
{
return savedSearches_;
}

int SavedSearches::historySize() const
{
return historySize_;
}

void SavedSearches::setHistorySize( int historySize )
{
historySize_ = historySize;
trim();
}

void SavedSearches::clear()
{
savedSearches_.clear();
}

void SavedSearches::trim()
{
while ( savedSearches_.size() > historySize_ )
savedSearches_.pop_back();
}

//
// Persistable virtual functions implementation
//
Expand All @@ -69,6 +81,7 @@ void SavedSearches::saveToStorage( QSettings& settings ) const
settings.setValue( "string", savedSearches_.at( i ) );
}
settings.endArray();
settings.setValue( "historySize", historySize_ );
settings.endGroup();
}

Expand All @@ -88,6 +101,7 @@ void SavedSearches::retrieveFromStorage( QSettings& settings )
savedSearches_.append( search );
}
settings.endArray();
historySize_ = settings.value( "historySize", MaxNumberOfRecentSearches ).toInt();
}
else {
LOG_ERROR << "Unknown version of saved searches, ignoring it...";
Expand Down

0 comments on commit 09419c5

Please sign in to comment.