#134 Added checking of a modified ServerModel, raising a prompt if modified. If "No" is selected, the model is reset. If "Yes", model is saved to file as if by pressing the "Save" button.

master
Tomatix 3 years ago
parent 8312170b71
commit 1bb74bf12c
  1. 24
      IConfig/IConfigServers.cpp
  2. 1
      IConfig/IConfigServers.h
  3. 51
      IConfig/ServerModel.cpp
  4. 4
      IConfig/ServerModel.h

@ -48,6 +48,7 @@ IConfigServers::IConfigServers(QWidget *parent) :
[this](const QModelIndex& sslIdx, const QModelIndex& passwordIdx) {
ui->servers->openPersistentEditor(sslIdx);
ui->servers->openPersistentEditor(passwordIdx);
serversModified = true;
});
ui->servers->setColumnWidth(2, 50); // SSL column
@ -67,7 +68,8 @@ bool IConfigServers::isChanged() const
|| cf_AltNickame != ui->edAltNickname->text()
|| cf_Server != ui->edServer->text()
|| cf_Password != ui->edServerPassword->text()
|| cf_SSL != ui->chkSSL->isChecked();
|| cf_SSL != ui->chkSSL->isChecked()
|| serversModified;
}
bool IConfigServers::connectToNewStatus() const
@ -101,6 +103,8 @@ void IConfigServers::save()
if (!smodel.saveToFile())
QMessageBox::warning(this, tr("Failed to save servers.json"), tr("Unable to open servers.json for writing, changes will be lost upon next restart!"));
else
serversModified = false;
}
void IConfigServers::reset()
@ -111,8 +115,15 @@ void IConfigServers::reset()
ui->edAltNickname->setText(cf_AltNickame);
ui->edServer->setText(cf_Server);
ui->edServerPassword->setText(cf_Password);
}
if (serversModified) {
smodel.reloadModel();
serversModified = false;
for (const auto& index : smodel.getEditorColumns()) {
ui->servers->openPersistentEditor(index);
}
}
}
void IConfigServers::on_btnShowPassword_toggled(bool checked)
{
@ -166,8 +177,11 @@ void IConfigServers::on_btnDeleteSelected_clicked()
tr("Delete server"),
tr("Confirm deletion of %1?").arg(item->name()));
if (btn == QMessageBox::No)
return;
else
if (btn == QMessageBox::Yes) {
smodel.deleteEntry(index);
serversModified = true;
}
else {
return;
}
}

@ -49,6 +49,7 @@ private:
QString cf_Server;
QString cf_Password;
bool cf_SSL;
bool serversModified{ false };
};
#endif // ICONFIGSERVERS_H

@ -43,25 +43,7 @@ QJsonObject createJsonObject(const QString& name, const QString& address, const
ServerModel::ServerModel(QObject* parent) :
QAbstractItemModel(parent)
{
QJsonDocument jsondoc;
const auto path{ QStringLiteral("%1/servers.json").arg(LOCAL_PATH) };
QFile f{ path };
if (f.open(QIODevice::ReadOnly)) {
const auto jsonData{ f.readAll() };
qDebug() << "Read" << jsonData.size() << "bytes from" << path;
jsondoc = QJsonDocument::fromJson(jsonData);
f.close();
if (!jsondoc.isObject())
qWarning() << "Parse error for servers.json";
else
createItems(jsondoc.object());
}
else
qWarning() << "Unable to open servers.json:" << path;
loadFromFile();
}
bool ServerModel::saveToFile()
@ -105,6 +87,14 @@ bool ServerModel::saveToFile()
return true;
}
void ServerModel::reloadModel()
{
beginResetModel();
m_items.clear();
loadFromFile();
endResetModel();
}
Qt::ItemFlags ServerModel::flags(const QModelIndex& index) const
{
/* Don't care about invalid indexes */
@ -408,3 +398,26 @@ void ServerModel::createItems(const QJsonObject& json)
}
}
}
void ServerModel::loadFromFile()
{
QJsonDocument jsondoc;
const auto path{ QStringLiteral("%1/servers.json").arg(LOCAL_PATH) };
QFile f{ path };
if (f.open(QIODevice::ReadOnly)) {
const auto jsonData{ f.readAll() };
qDebug() << "Read" << jsonData.size() << "bytes from" << path;
jsondoc = QJsonDocument::fromJson(jsonData);
f.close();
if (!jsondoc.isObject())
qWarning() << "Parse error for servers.json";
else
createItems(jsondoc.object());
}
else
qWarning() << "Unable to open servers.json:" << path;
}

@ -28,6 +28,9 @@ public:
/// Save model to servers.json
bool saveToFile();
/// Resets and reloads entire model
void reloadModel();
Qt::ItemFlags flags(const QModelIndex &index) const;
QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
@ -61,6 +64,7 @@ signals:
private:
QModelIndex networkIndex(int row, int col = 0);
void createItems(const QJsonObject& json);
void loadFromFile();
// I don't like this being mutable, but the nature of Qt's item model seems to have forced my hand.
// Or I've probably not done the correct implementation design. In any way, it works, but smells a bit...

Loading…
Cancel
Save