feat: import rules from JSON via Rule::fromJson + RuleDao::insert
This commit is contained in:
@@ -12,6 +12,7 @@
|
||||
#include <QFileDialog>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QJsonArray>
|
||||
#include <QLabel>
|
||||
#include <QMenu>
|
||||
#include <QSqlDatabase>
|
||||
@@ -273,9 +274,32 @@ void RulesManagerDialog::onImportExport(bool import)
|
||||
if (import) {
|
||||
QString file = QFileDialog::getOpenFileName(this, tr("Importar reglas"), QString(), tr("JSON (*.json)"));
|
||||
if (file.isEmpty()) return;
|
||||
|
||||
// TODO: Parse JSON and create rules
|
||||
QMessageBox::information(this, tr("Importar"), tr("Función de importación en desarrollo."));
|
||||
|
||||
QFile f(file);
|
||||
if (!f.open(QIODevice::ReadOnly)) {
|
||||
QMessageBox::warning(this, tr("Importar"), tr("No se pudo abrir el archivo."));
|
||||
return;
|
||||
}
|
||||
QJsonParseError err;
|
||||
QJsonDocument doc = QJsonDocument::fromJson(f.readAll(), &err);
|
||||
if (err.error != QJsonParseError::NoError || !doc.isArray()) {
|
||||
QMessageBox::warning(this, tr("Importar"), tr("Archivo JSON inválido."));
|
||||
return;
|
||||
}
|
||||
QJsonArray arr = doc.array();
|
||||
int imported = 0;
|
||||
int skipped = 0;
|
||||
for (const QJsonValue &v : arr) {
|
||||
if (!v.isObject()) { ++skipped; continue; }
|
||||
Rule rule = Rule::fromJson(v.toObject());
|
||||
if (rule.name.isEmpty()) { ++skipped; continue; }
|
||||
rule.accountId = m_accountId; // scope to the dialog's account (-1 = global)
|
||||
if (RuleDao::insert(rule)) ++imported;
|
||||
else ++skipped;
|
||||
}
|
||||
QMessageBox::information(this, tr("Importar"),
|
||||
tr("Se importaron %1 regla(s). %2 omitida(s).").arg(imported).arg(skipped));
|
||||
refresh();
|
||||
} else {
|
||||
QString file = QFileDialog::getSaveFileName(this, tr("Exportar reglas"), "rules.json", tr("JSON (*.json)"));
|
||||
if (file.isEmpty()) return;
|
||||
|
||||
Reference in New Issue
Block a user