Add AsyncTask helper functions

This commit is contained in:
Janek Bevendorff 2017-12-17 14:49:46 +01:00 committed by Jonathan White
parent 0d6ca0945b
commit d00ccd2eb5
No known key found for this signature in database
GPG Key ID: 440FC65F2E0C6E01
4 changed files with 77 additions and 23 deletions

View File

@ -38,6 +38,7 @@ configure_file(version.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/version.h @ONLY)
set(keepassx_SOURCES
core/AutoTypeAssociations.cpp
core/AsyncTask.h
core/Config.cpp
core/CsvParser.cpp
core/Database.cpp

63
src/core/AsyncTask.h Normal file
View File

@ -0,0 +1,63 @@
/*
* 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 KEEPASSXC_ASYNCTASK_HPP
#define KEEPASSXC_ASYNCTASK_HPP
#include <QFuture>
#include <QFutureWatcher>
#include <QtConcurrent>
/**
* Asynchronously run computations outside the GUI thread.
*/
namespace AsyncTask
{
/**
* Wait for the given future without blocking the event loop.
*
* @param future future to wait for
* @return async task result
*/
template<typename FunctionObject>
typename std::result_of<FunctionObject()>::type waitForFuture(QFuture<typename std::result_of<FunctionObject()>::type> future)
{
QEventLoop loop;
QFutureWatcher<typename std::result_of<FunctionObject()>::type> watcher;
QObject::connect(&watcher, SIGNAL(finished()), &loop, SLOT(quit()));
watcher.setFuture(future);
loop.exec();
return future.result();
}
/**
* Run a given task and wait for it to finish without blocking the event loop.
*
* @param task std::function object to run
* @return async task result
*/
template<typename FunctionObject>
typename std::result_of<FunctionObject()>::type runAndWaitForFuture(FunctionObject task)
{
return waitForFuture<FunctionObject>(QtConcurrent::run(task));
}
}; // namespace AsyncTask
#endif //KEEPASSXC_ASYNCTASK_HPP

View File

@ -18,28 +18,18 @@
#include "DatabaseSettingsWidget.h"
#include "ui_DatabaseSettingsWidget.h"
#include <QList>
#include <QLabel>
#include <QPushButton>
#include <QHBoxLayout>
#include <QSpinBox>
#include <QMessageBox>
#include <QDialogButtonBox>
#include "core/AsyncTask.h"
#include "core/Database.h"
#include "core/Group.h"
#include "core/Metadata.h"
#include "crypto/SymmetricCipher.h"
#include "format/KeePass2.h"
#include "keys/CompositeKey.h"
#include "format/KeePass2.h"
#include "crypto/kdf/Kdf.h"
#include "MessageBox.h"
DatabaseSettingsWidget::DatabaseSettingsWidget(QWidget* parent)
: DialogyWidget(parent)
, m_ui(new Ui::DatabaseSettingsWidget())
, m_benchmarkField(nullptr)
, m_db(nullptr)
{
m_ui->setupUi(this);
@ -51,6 +41,7 @@ DatabaseSettingsWidget::DatabaseSettingsWidget(QWidget* parent)
connect(m_ui->historyMaxSizeCheckBox, SIGNAL(toggled(bool)),
m_ui->historyMaxSizeSpinBox, SLOT(setEnabled(bool)));
connect(m_ui->kdfComboBox, SIGNAL(currentIndexChanged(int)), SLOT(changeKdf(int)));
connect(m_ui->transformBenchmarkButton, SIGNAL(clicked()), SLOT(transformRoundsBenchmark()));
}
DatabaseSettingsWidget::~DatabaseSettingsWidget()
@ -111,6 +102,7 @@ void DatabaseSettingsWidget::load(Database* db)
}
displayKdf(*m_kdf);
m_ui->kdfComboBox->blockSignals(blockSignals);
m_ui->transformRoundsSpinBox->setValue(static_cast<unsigned>(m_kdf->rounds()));
m_ui->dbNameEdit->setFocus();
}
@ -191,13 +183,12 @@ void DatabaseSettingsWidget::reject()
void DatabaseSettingsWidget::transformRoundsBenchmark()
{
if (m_benchmarkField == nullptr) {
Q_ASSERT(false);
return;
}
QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
m_benchmarkField->setValue(m_kdf->benchmark(1000));
QApplication::restoreOverrideCursor();
m_ui->transformRoundsSpinBox->setValue(AsyncTask::runAndWaitForFuture([this]() {
int rounds = m_kdf->benchmark(1000);
QApplication::restoreOverrideCursor();
return rounds;
}));
}
void DatabaseSettingsWidget::truncateHistories()

View File

@ -28,13 +28,14 @@
class Database;
namespace Ui {
class DatabaseSettingsWidget;
namespace Ui
{
class DatabaseSettingsWidget;
}
class DatabaseSettingsWidget : public DialogyWidget
class DatabaseSettingsWidget: public DialogyWidget
{
Q_OBJECT
Q_OBJECT
public:
explicit DatabaseSettingsWidget(QWidget* parent = nullptr);
@ -58,8 +59,6 @@ private:
const QScopedPointer<Ui::DatabaseSettingsWidget> m_ui;
QList<QWidget*> m_kdfWidgets;
QList<QPair<quint32, QSpinBox*>> m_kdfFields;
QSpinBox* m_benchmarkField;
QScopedPointer<Kdf> m_kdf;
Database* m_db;