UI: TaskZoneDownloader: Add local file source.

This commit is contained in:
Nodir Temirkhodjaev 2020-02-08 15:42:35 +05:00
parent 2d37db4f9c
commit e29d428ff7
13 changed files with 80 additions and 12 deletions

View File

@ -209,7 +209,7 @@ const char * const sqlUpdateZoneEnabled =
const char * const sqlUpdateZoneResult =
"UPDATE zone"
" SET text_checksum = ?2, bin_checksum = ?3,"
" last_run = ?4, last_success = ?5"
" source_modtime = ?4, last_run = ?5, last_success = ?6"
" WHERE zone_id = ?1;"
;
@ -746,6 +746,7 @@ bool ConfManager::updateZoneEnabled(qint64 zoneId, bool enabled)
bool ConfManager::updateZoneResult(qint64 zoneId, const QString &textChecksum,
const QString &binChecksum,
const QDateTime &sourceModTime,
const QDateTime &lastRun,
const QDateTime &lastSuccess)
{
@ -755,6 +756,7 @@ bool ConfManager::updateZoneResult(qint64 zoneId, const QString &textChecksum,
<< zoneId
<< textChecksum
<< binChecksum
<< sourceModTime
<< lastRun
<< lastSuccess
;

View File

@ -77,6 +77,7 @@ public:
bool updateZoneEnabled(qint64 zoneId, bool enabled);
bool updateZoneResult(qint64 zoneId, const QString &textChecksum,
const QString &binChecksum,
const QDateTime &sourceModTime,
const QDateTime &lastRun,
const QDateTime &lastSuccess);

View File

@ -9,6 +9,7 @@ CREATE TABLE zone(
form_data TEXT,
text_checksum TEXT,
bin_checksum TEXT,
source_modtime INTEGER,
last_run INTEGER,
last_success INTEGER
);

View File

@ -5,5 +5,12 @@
"zoneType": "bgp",
"url": "http://mrlg.tas-ix.uz/index.cgi",
"formData": "router=cisco&pass1=&query=1&arg="
},
{
"code": "file",
"title": "Addresses from Local File",
"zoneType": "file",
"url": "",
"formData": ""
}
]

View File

@ -6,5 +6,13 @@
"sort": true,
"pattern": "^\\*\\D{2,5}(\\S+)",
"emptyNetMask": 24
},
{
"code": "file",
"title": "File",
"description": "Local File",
"sort": true,
"pattern": "^\\D*(\\S+)",
"emptyNetMask": 32
}
]

View File

@ -336,6 +336,13 @@ void ZonesWindow::updateZoneEditForm(bool editCurrentZone)
bool ZonesWindow::saveZoneEditForm()
{
const auto zoneSource = ZoneSourceWrapper(m_comboSources->currentData());
const auto sourceCode = zoneSource.code();
if (zoneSource.url().isEmpty()) {
m_cbCustomUrl->setChecked(true);
}
const auto zoneName = m_editZoneName->text();
const bool enabled = m_cbEnabled->isChecked();
const bool storeText = m_cbStoreText->isChecked();
@ -343,9 +350,6 @@ bool ZonesWindow::saveZoneEditForm()
const auto url = m_editUrl->text();
const auto formData = m_editFormData->text();
const auto zoneSource = ZoneSourceWrapper(m_comboSources->currentData());
const auto sourceCode = zoneSource.code();
// Check custom url
if (customUrl && url.isEmpty()) {
m_editUrl->setText(zoneSource.url());

View File

@ -172,11 +172,12 @@ bool ZoneListModel::updateZoneEnabled(qint64 zoneId, bool enabled)
bool ZoneListModel::updateZoneResult(qint64 zoneId, const QString &textChecksum,
const QString &binChecksum,
const QDateTime &sourceModTime,
const QDateTime &lastRun,
const QDateTime &lastSuccess)
{
if (confManager()->updateZoneResult(zoneId, textChecksum, binChecksum,
lastRun, lastSuccess)) {
sourceModTime, lastRun, lastSuccess)) {
refresh();
return true;
}
@ -223,8 +224,9 @@ bool ZoneListModel::updateTableRow(int row) const
m_zoneRow.formData = stmt.columnText(7);
m_zoneRow.textChecksum = stmt.columnText(8);
m_zoneRow.binChecksum = stmt.columnText(9);
m_zoneRow.lastRun = stmt.columnDateTime(10);
m_zoneRow.lastSuccess = stmt.columnDateTime(11);
m_zoneRow.sourceModTime = stmt.columnDateTime(10);
m_zoneRow.lastRun = stmt.columnDateTime(11);
m_zoneRow.lastSuccess = stmt.columnDateTime(12);
return true;
}
@ -243,6 +245,7 @@ QString ZoneListModel::sqlBase() const
" form_data,"
" text_checksum,"
" bin_checksum,"
" source_modtime,"
" last_run,"
" last_success"
" FROM zone"

View File

@ -25,6 +25,7 @@ struct ZoneRow : TableRow {
QString textChecksum;
QString binChecksum;
QDateTime sourceModTime;
QDateTime lastRun;
QDateTime lastSuccess;
};
@ -64,6 +65,7 @@ public:
bool updateZoneEnabled(qint64 zoneId, bool enabled);
bool updateZoneResult(qint64 zoneId, const QString &textChecksum,
const QString &binChecksum,
const QDateTime &sourceModTime,
const QDateTime &lastRun,
const QDateTime &lastSuccess);
void deleteZone(qint64 zoneId, int row);

View File

@ -16,12 +16,15 @@ void TaskDownloader::run()
setupDownloader();
downloader()->start();
if (m_downloader != nullptr) {
m_downloader->start();
}
}
void TaskDownloader::abort(bool success)
{
if (!m_downloader) return;
if (m_downloader == nullptr)
return;
m_downloader->disconnect(this); // to avoid recursive call on abort()

View File

@ -40,7 +40,9 @@ bool TaskInfoZoneDownloader::processResult(bool success)
void TaskInfoZoneDownloader::setupTaskWorker()
{
const int rowCount = zoneListModel()->rowCount();
if (m_zoneIndex >= rowCount || aborted()) {
if (m_zoneIndex >= rowCount) {
m_zoneIndex = 0;
TaskInfo::handleFinished(m_success);
return;
}
@ -67,6 +69,7 @@ void TaskInfoZoneDownloader::setupTaskWorker()
worker->setPattern(zoneType.pattern());
worker->setTextChecksum(zoneRow.textChecksum);
worker->setCachePath(cachePath());
worker->setSourceModTime(zoneRow.sourceModTime);
worker->setLastSuccess(zoneRow.lastSuccess);
m_zoneIdSet.insert(zoneRow.zoneId);
@ -80,7 +83,11 @@ void TaskInfoZoneDownloader::handleFinished(bool success)
m_success = true;
}
++m_zoneIndex;
if (aborted()) {
m_zoneIndex = INT_MAX;
} else {
++m_zoneIndex;
}
setupTaskWorker();
runTaskWorker();
@ -97,11 +104,12 @@ void TaskInfoZoneDownloader::processSubResult(bool success)
const auto textChecksum = worker->textChecksum();
const auto binChecksum = worker->binChecksum();
const auto sourceModTime = worker->sourceModTime();
const auto now = QDateTime::currentDateTime();
const auto lastSuccess = success ? now : worker->lastSuccess();
zoneListModel()->updateZoneResult(zoneId, textChecksum, binChecksum,
now, lastSuccess);
sourceModTime, now, lastSuccess);
}
void TaskInfoZoneDownloader::removeOrphanCacheFiles()

View File

@ -2,6 +2,7 @@
#include <QCryptographicHash>
#include <QRegularExpression>
#include <QUrl>
#include "../util/conf/confutil.h"
#include "../util/fileutil.h"
@ -15,6 +16,12 @@ TaskZoneDownloader::TaskZoneDownloader(QObject *parent) :
void TaskZoneDownloader::setupDownloader()
{
if (QUrl::fromUserInput(url()).isLocalFile()) {
// Load addresses from local file
loadLocalFile();
return;
}
downloader()->setUrl(url());
downloader()->setData(formData().toUtf8());
}
@ -42,6 +49,20 @@ void TaskZoneDownloader::downloadFinished(bool success)
abort(success);
}
void TaskZoneDownloader::loadLocalFile()
{
bool success = false;
if (sourceModTime() != FileUtil::fileModTime(url())
|| !FileUtil::fileExists(cacheFileBinPath())) {
const auto buffer = FileUtil::readFileData(url());
downloader()->setBuffer(buffer);
success = true;
}
downloadFinished(success);
}
QVector<QStringRef> TaskZoneDownloader::parseAddresses(const QString &text,
QString &checksum) const
{

View File

@ -42,6 +42,9 @@ public:
QString cachePath() const { return m_cachePath; }
void setCachePath(const QString &v) { m_cachePath = v; }
QDateTime sourceModTime() const { return m_sourceModTime; }
void setSourceModTime(const QDateTime &v) { m_sourceModTime = v; }
QDateTime lastSuccess() const { return m_lastSuccess; }
void setLastSuccess(const QDateTime &v) { m_lastSuccess = v; }
@ -60,6 +63,9 @@ protected:
protected slots:
void downloadFinished(bool success) override;
private:
void loadLocalFile();
private:
bool m_storeText = false;
bool m_sort = false;
@ -78,6 +84,7 @@ private:
QString m_cachePath;
QDateTime m_sourceModTime;
QDateTime m_lastSuccess;
};

View File

@ -14,6 +14,7 @@ public:
void setData(const QByteArray &data) { m_data = data; }
QByteArray buffer() const { return m_buffer; }
void setBuffer(const QByteArray &buffer) { m_buffer = buffer; }
signals:
void finished(bool success);