UI: ZonesWindow: Add "Store Text" option.

This commit is contained in:
Nodir Temirkhodjaev 2020-02-06 13:48:09 +05:00
parent 02873331a3
commit 7a9f1b3ba3
13 changed files with 100 additions and 51 deletions

View File

@ -70,6 +70,7 @@ void Test::taskTasix()
QVERIFY(!buf.isEmpty());
TaskZoneDownloader tasix;
tasix.setZoneId(1);
tasix.setSort(true);
tasix.setEmptyNetMask(24);
tasix.setPattern("^\\*\\D{2,5}(\\S+)");
@ -79,5 +80,8 @@ void Test::taskTasix()
const auto list = tasix.parseAddresses(text, checksum);
QVERIFY(!list.isEmpty());
//QVERIFY(tasix.saveAddresses(list, QString(STR(PWD) "/data/tasix-mrlg.out"), true));
tasix.setStoreText(true);
tasix.setCachePath(QLatin1String(STR(PWD) "/data/tasix-mrlg/"));
//QVERIFY(tasix.storeAddresses(list));
}

View File

@ -178,8 +178,9 @@ const char * const sqlUpdateAppResetGroup =
;
const char * const sqlInsertZone =
"INSERT INTO zone(name, enabled, custom_url, source_code, url, form_data)"
" VALUES(?1, ?2, ?3, ?4, ?5, ?6);"
"INSERT INTO zone(name, enabled, store_text, custom_url,"
" source_code, url, form_data)"
" VALUES(?1, ?2, ?3, ?4, ?5, ?6, ?7);"
;
const char * const sqlDeleteZone =
@ -188,8 +189,8 @@ const char * const sqlDeleteZone =
const char * const sqlUpdateZone =
"UPDATE zone"
" SET name = ?2, enabled = ?3, custom_url = ?4,"
" source_code = ?5, url = ?6, form_data = ?7"
" SET name = ?2, enabled = ?3, store_text = ?4, custom_url = ?5,"
" source_code = ?6, url = ?7, form_data = ?8"
" WHERE zone_id = ?1;"
;
@ -646,13 +647,14 @@ void ConfManager::checkAppEndTimes()
bool ConfManager::addZone(const QString &zoneName, const QString &sourceCode,
const QString &url, const QString &formData,
bool enabled, bool customUrl)
bool enabled, bool storeText, bool customUrl)
{
bool ok = false;
const auto vars = QVariantList()
<< zoneName
<< enabled
<< storeText
<< customUrl
<< sourceCode
<< url
@ -683,7 +685,8 @@ bool ConfManager::deleteZone(qint64 zoneId)
bool ConfManager::updateZone(qint64 zoneId, const QString &zoneName,
const QString &sourceCode, const QString &url,
const QString &formData, bool enabled, bool customUrl)
const QString &formData, bool enabled,
bool storeText, bool customUrl)
{
bool ok = false;
@ -691,6 +694,7 @@ bool ConfManager::updateZone(qint64 zoneId, const QString &zoneName,
<< zoneId
<< zoneName
<< enabled
<< storeText
<< customUrl
<< sourceCode
<< url

View File

@ -67,11 +67,12 @@ public:
bool addZone(const QString &zoneName, const QString &sourceCode,
const QString &url, const QString &formData,
bool enabled, bool customUrl);
bool enabled, bool storeText, bool customUrl);
bool deleteZone(qint64 zoneId);
bool updateZone(qint64 zoneId, const QString &zoneName,
const QString &sourceCode, const QString &url,
const QString &formData, bool enabled, bool customUrl);
const QString &formData, bool enabled,
bool storeText, bool customUrl);
bool updateZoneName(qint64 zoneId, const QString &zoneName);
bool updateZoneEnabled(qint64 zoneId, bool enabled);
bool updateZoneResult(qint64 zoneId, const QString &checksum,

View File

@ -1,6 +1,7 @@
CREATE TABLE zone(
zone_id INTEGER PRIMARY KEY,
enabled BOOLEAN NOT NULL,
store_text BOOLEAN NOT NULL,
custom_url BOOLEAN NOT NULL,
name TEXT NOT NULL,
source_code TEXT NOT NULL,

View File

@ -78,6 +78,7 @@ void ZonesWindow::onRetranslateUi()
m_labelZoneName->setText(tr("Zone Name:"));
m_labelSource->setText(tr("Source:"));
m_cbEnabled->setText(tr("Enabled"));
m_cbStoreText->setText(tr("Store Text"));
m_cbCustomUrl->setText(tr("Custom URL"));
m_labelUrl->setText(tr("URL:"));
m_labelFormData->setText(tr("Form Data:"));
@ -146,6 +147,11 @@ void ZonesWindow::setupZoneEditForm()
formLayout->addRow(QString(), m_cbEnabled);
// Store Text
m_cbStoreText = new QCheckBox();
formLayout->addRow(QString(), m_cbStoreText);
// Custom URL
m_cbCustomUrl = new QCheckBox();
@ -318,6 +324,7 @@ void ZonesWindow::updateZoneEditForm(bool editCurrentZone)
m_editZoneName->setFocus();
m_comboSources->setCurrentIndex(zoneSource.index());
m_cbEnabled->setChecked(zoneRow.enabled);
m_cbStoreText->setChecked(zoneRow.storeText);
m_cbCustomUrl->setChecked(zoneRow.customUrl);
m_editUrl->setText(zoneRow.url);
m_editFormData->setText(zoneRow.formData);
@ -329,6 +336,7 @@ bool ZonesWindow::saveZoneEditForm()
{
const auto zoneName = m_editZoneName->text();
const bool enabled = m_cbEnabled->isChecked();
const bool storeText = m_cbStoreText->isChecked();
const bool customUrl = m_cbCustomUrl->isChecked();
const auto url = m_editUrl->text();
const auto formData = m_editFormData->text();
@ -348,7 +356,7 @@ bool ZonesWindow::saveZoneEditForm()
// Add new zone
if (m_formZoneIsNew) {
return zoneListModel()->addZone(zoneName, sourceCode, url, formData,
enabled, customUrl);
enabled, storeText, customUrl);
}
// Edit selected zone
@ -358,6 +366,7 @@ bool ZonesWindow::saveZoneEditForm()
const bool zoneNameEdited = (zoneName != zoneRow.zoneName);
const bool zoneEdited = (sourceCode != zoneRow.sourceCode
|| enabled != zoneRow.enabled
|| storeText != zoneRow.storeText
|| customUrl != zoneRow.customUrl
|| url != zoneRow.url
|| formData != zoneRow.formData);
@ -370,8 +379,8 @@ bool ZonesWindow::saveZoneEditForm()
}
return zoneListModel()->updateZone(zoneRow.zoneId, zoneName, sourceCode,
url, formData, enabled, customUrl,
zoneEdited);
url, formData, enabled, storeText,
customUrl, zoneEdited);
}
void ZonesWindow::updateZone(int row, bool enabled)
@ -380,7 +389,7 @@ void ZonesWindow::updateZone(int row, bool enabled)
zoneListModel()->updateZone(zoneRow.zoneId, zoneRow.zoneName,
zoneRow.sourceCode, zoneRow.url, zoneRow.formData,
enabled, zoneRow.customUrl);
enabled, zoneRow.storeText, zoneRow.customUrl);
}
void ZonesWindow::deleteZone(int row)

View File

@ -73,6 +73,7 @@ private:
QLineEdit *m_editZoneName = nullptr;
QLabel *m_labelSource = nullptr;
QCheckBox *m_cbEnabled = nullptr;
QCheckBox *m_cbStoreText = nullptr;
QCheckBox *m_cbCustomUrl = nullptr;
QComboBox *m_comboSources = nullptr;
QLabel *m_labelUrl = nullptr;

View File

@ -63,7 +63,9 @@ QVariant ZoneListModel::data(const QModelIndex &index, int role) const
const auto zoneRow = zoneRowAt(row);
switch (column) {
case 0: return zoneRow.zoneName;
case 0: return QString("(%1) %2")
.arg(QString::number(m_zoneRow.zoneId),
zoneRow.zoneName);
case 1: {
const auto zoneSource = ZoneSourceWrapper(
zoneSourceByCode(zoneRow.sourceCode));
@ -123,10 +125,10 @@ const ZoneRow &ZoneListModel::zoneRowAt(int row) const
bool ZoneListModel::addZone(const QString &zoneName, const QString &sourceCode,
const QString &url, const QString &formData,
bool enabled, bool customUrl)
bool enabled, bool storeText, bool customUrl)
{
if (confManager()->addZone(zoneName, sourceCode,
url, formData, enabled, customUrl)) {
if (confManager()->addZone(zoneName, sourceCode, url, formData,
enabled, storeText, customUrl)) {
reset();
return true;
}
@ -136,11 +138,11 @@ bool ZoneListModel::addZone(const QString &zoneName, const QString &sourceCode,
bool ZoneListModel::updateZone(qint64 zoneId, const QString &zoneName,
const QString &sourceCode, const QString &url,
const QString &formData, bool enabled, bool customUrl,
bool updateDriver)
const QString &formData, bool enabled, bool storeText,
bool customUrl, bool updateDriver)
{
if (confManager()->updateZone(zoneId, zoneName, sourceCode,
url, formData, enabled, customUrl)) {
if (confManager()->updateZone(zoneId, zoneName, sourceCode, url, formData,
enabled, storeText, customUrl)) {
refresh();
return true;
}
@ -211,14 +213,15 @@ bool ZoneListModel::updateTableRow(int row) const
m_zoneRow.zoneId = stmt.columnInt64(0);
m_zoneRow.enabled = stmt.columnBool(1);
m_zoneRow.customUrl = stmt.columnBool(2);
m_zoneRow.zoneName = stmt.columnText(3);
m_zoneRow.sourceCode = stmt.columnText(4);
m_zoneRow.url = stmt.columnText(5);
m_zoneRow.formData = stmt.columnText(6);
m_zoneRow.checksum = stmt.columnText(7);
m_zoneRow.lastRun = stmt.columnDateTime(8);
m_zoneRow.lastSuccess = stmt.columnDateTime(9);
m_zoneRow.storeText = stmt.columnBool(2);
m_zoneRow.customUrl = stmt.columnBool(3);
m_zoneRow.zoneName = stmt.columnText(4);
m_zoneRow.sourceCode = stmt.columnText(5);
m_zoneRow.url = stmt.columnText(6);
m_zoneRow.formData = stmt.columnText(7);
m_zoneRow.checksum = stmt.columnText(8);
m_zoneRow.lastRun = stmt.columnDateTime(9);
m_zoneRow.lastSuccess = stmt.columnDateTime(10);
return true;
}
@ -229,6 +232,7 @@ QString ZoneListModel::sqlBase() const
"SELECT"
" zone_id,"
" enabled,"
" store_text,"
" custom_url,"
" name,"
" source_code,"

View File

@ -11,6 +11,7 @@ QT_FORWARD_DECLARE_CLASS(ZoneSourceWrapper)
struct ZoneRow : TableRow {
bool enabled = true;
bool storeText = false;
bool customUrl = false;
qint64 zoneId = 0;
@ -53,11 +54,11 @@ public:
bool addZone(const QString &zoneName, const QString &sourceCode,
const QString &url, const QString &formData,
bool enabled, bool customUrl);
bool enabled, bool storeText, bool customUrl);
bool updateZone(qint64 zoneId, const QString &zoneName,
const QString &sourceCode, const QString &url,
const QString &formData, bool enabled, bool customUrl,
bool updateDriver = true);
const QString &formData, bool enabled, bool storeText,
bool customUrl, bool updateDriver = true);
bool updateZoneName(qint64 zoneId, const QString &zoneName);
bool updateZoneEnabled(qint64 zoneId, bool enabled);
bool updateZoneResult(qint64 zoneId, const QString &checksum,

View File

@ -28,7 +28,8 @@ bool TaskInfoZoneDownloader::processResult(bool success)
if (!success)
return false;
return fortManager()->saveOriginConf(tr("Zone Addresses Updated!"));
fortManager()->showTrayMessage(tr("Zone Addresses Updated!"));
return true;
}
void TaskInfoZoneDownloader::setupTaskWorker()
@ -50,6 +51,7 @@ void TaskInfoZoneDownloader::setupTaskWorker()
const auto zoneType = ZoneTypeWrapper(
zoneListModel()->zoneTypeByCode(zoneSource.zoneType()));
worker->setStoreText(zoneRow.storeText);
worker->setSort(zoneType.sort());
worker->setEmptyNetMask(zoneType.emptyNetMask());
worker->setZoneId(zoneRow.zoneId);

View File

@ -27,11 +27,12 @@ void TaskZoneDownloader::downloadFinished(bool success)
QString checksum;
const auto text = QString::fromLatin1(downloader()->buffer());
const auto list = parseAddresses(text, checksum);
if (!list.isEmpty()
&& this->checksum() != checksum) {
setChecksum(checksum);
success = saveAddresses(list, cacheFilePath());
if (!list.isEmpty()
&& (this->checksum() != checksum
|| !FileUtil::fileExists(cacheFileBinPath()))) {
setChecksum(checksum);
success = storeAddresses(list);
}
}
@ -64,9 +65,7 @@ QVector<QStringRef> TaskZoneDownloader::parseAddresses(const QString &text,
return list;
}
bool TaskZoneDownloader::saveAddresses(const QVector<QStringRef> &list,
const QString &filePath,
bool isOutputText) const
bool TaskZoneDownloader::storeAddresses(const QVector<QStringRef> &list) const
{
Ip4Range ip4Range;
if (!ip4Range.fromList(list, emptyNetMask(), sort()))
@ -74,16 +73,27 @@ bool TaskZoneDownloader::saveAddresses(const QVector<QStringRef> &list,
QByteArray data;
if (isOutputText) {
data = ip4Range.toText().toLatin1();
} else {
//ConfUtil::write();
if (storeText()) {
const auto text = ip4Range.toText();
FileUtil::writeFile(cacheFileTextPath(), text);
}
return FileUtil::writeFileData(filePath, data);
//ConfUtil::write();
return FileUtil::writeFileData(cacheFileBinPath(), data);
}
QString TaskZoneDownloader::cacheFilePath() const
QString TaskZoneDownloader::cacheFileBasePath() const
{
return cachePath() + '-' + QString::number(zoneId()) + ".bin";
return cachePath() + QString::number(zoneId());
}
QString TaskZoneDownloader::cacheFileBinPath() const
{
return cacheFileBasePath() + ".bin";
}
QString TaskZoneDownloader::cacheFileTextPath() const
{
return cacheFileBasePath() + ".txt";
}

View File

@ -12,6 +12,9 @@ class TaskZoneDownloader : public TaskDownloader
public:
explicit TaskZoneDownloader(QObject *parent = nullptr);
bool storeText() const { return m_storeText; }
void setStoreText(bool v) { m_storeText = v; }
bool sort() const { return m_sort; }
void setSort(bool v) { m_sort = v; }
@ -41,11 +44,11 @@ public:
QVector<QStringRef> parseAddresses(const QString &text,
QString &checksum) const;
bool saveAddresses(const QVector<QStringRef> &list,
const QString &filePath,
bool isOutputText = false) const;
bool storeAddresses(const QVector<QStringRef> &list) const;
QString cacheFilePath() const;
QString cacheFileBasePath() const;
QString cacheFileBinPath() const;
QString cacheFileTextPath() const;
protected:
void setupDownloader() override;
@ -54,6 +57,7 @@ protected slots:
void downloadFinished(bool success) override;
private:
bool m_storeText = false;
bool m_sort = false;
int m_emptyNetMask = 32;

View File

@ -117,6 +117,11 @@ bool FileUtil::makePath(const QString &path)
return QDir().mkpath(path);
}
bool FileUtil::makePathForFile(const QString &filePath)
{
return QFileInfo(filePath).dir().mkpath(".");
}
bool FileUtil::fileExists(const QString &filePath)
{
return QFileInfo::exists(filePath);
@ -165,6 +170,8 @@ bool FileUtil::writeFile(const QString &filePath, const QString &text)
bool FileUtil::writeFileData(const QString &filePath, const QByteArray &data)
{
makePathForFile(filePath); // create destination directory
QFile file(filePath);
if (!file.open(QFile::WriteOnly | QFile::Truncate))
return false;

View File

@ -29,6 +29,7 @@ public:
static QString toNativeSeparators(const QString &path);
static bool makePath(const QString &path);
static bool makePathForFile(const QString &filePath);
static bool fileExists(const QString &filePath);
static bool removeFile(const QString &filePath);
static bool renameFile(const QString &oldFilePath, const QString &newFilePath);