mirror of
https://github.com/keepassxreboot/keepassxc.git
synced 2024-12-25 15:29:44 -05:00
Add AsyncTask helper functions
This commit is contained in:
parent
0d6ca0945b
commit
d00ccd2eb5
@ -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
63
src/core/AsyncTask.h
Normal 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
|
@ -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));
|
||||
m_ui->transformRoundsSpinBox->setValue(AsyncTask::runAndWaitForFuture([this]() {
|
||||
int rounds = m_kdf->benchmark(1000);
|
||||
QApplication::restoreOverrideCursor();
|
||||
return rounds;
|
||||
}));
|
||||
}
|
||||
|
||||
void DatabaseSettingsWidget::truncateHistories()
|
||||
|
@ -28,7 +28,8 @@
|
||||
|
||||
class Database;
|
||||
|
||||
namespace Ui {
|
||||
namespace Ui
|
||||
{
|
||||
class DatabaseSettingsWidget;
|
||||
}
|
||||
|
||||
@ -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;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user