Http Version - Initial Commit
This commit is contained in:
commit
026ea97284
1 changed files with 978 additions and 0 deletions
53
configinterface.cpp
Normal file
53
configinterface.cpp
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
#include "configinterface.h"
|
||||
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
|
||||
ConfigInterface::ConfigInterface(QObject *parent)
|
||||
: QObject{parent}
|
||||
{
|
||||
}
|
||||
|
||||
QString ConfigInterface::fileName() const
|
||||
{
|
||||
return m_fileName;
|
||||
}
|
||||
|
||||
void ConfigInterface::setFileName(const QString &newFileName)
|
||||
{
|
||||
if (m_fileName == newFileName)
|
||||
return;
|
||||
m_fileName = newFileName;
|
||||
std::fstream input(newFileName.toStdString(), std::fstream::in);
|
||||
if(!input.is_open()) {
|
||||
return;
|
||||
}
|
||||
std::string data;
|
||||
std::string temp;
|
||||
while(std::getline(input, temp)) {
|
||||
data += temp;
|
||||
}
|
||||
input.flush();
|
||||
input.close();
|
||||
QByteArray arr_data(data);
|
||||
QJsonDocument doc = QJsonDocument::fromJson(arr_data);
|
||||
// The following works even the qml part is not initailzed/connected
|
||||
// do not change back to just emit config()
|
||||
QMetaObject::invokeMethod(this, &ConfigInterface::config, Qt::QueuedConnection, doc.object());
|
||||
emit fileNameChanged();
|
||||
}
|
||||
|
||||
void ConfigInterface::saveConfig(QJsonObject config)
|
||||
{
|
||||
QJsonDocument doc;
|
||||
doc.setObject(config);
|
||||
std::string data = doc.toJson().toStdString();
|
||||
std::fstream output(m_fileName.toStdString(), std::fstream::out);
|
||||
if(!output.is_open()) {
|
||||
return;
|
||||
}
|
||||
output << data;
|
||||
output.flush();
|
||||
output.close();
|
||||
QMetaObject::invokeMethod(this, &ConfigInterface::config, Qt::QueuedConnection, doc.object());
|
||||
}
|
||||
Loading…
Reference in a new issue