Initial commit
This commit is contained in:
commit
b7d90b9afe
16245 files changed
+3396666
No files matched your search
Executable
+187
@@ -0,0 +1,187 @@
|
||||
#include "PresetManager.h"
|
||||
|
||||
#include <QInputDialog>
|
||||
#include <QApplication>
|
||||
#include <QDebug>
|
||||
|
||||
#ifdef Q_OS_WIN
|
||||
static QString newLine(QStringLiteral("\r\n"));
|
||||
#else
|
||||
static QString newLine(QStringLiteral("\n"));
|
||||
#endif
|
||||
|
||||
PresetManager::PresetManager()
|
||||
: QObject()
|
||||
{
|
||||
QString filename(QStringLiteral("%1.conf").arg(QApplication::arguments().at(0)));
|
||||
file.setFileName(filename);
|
||||
|
||||
bool goCreateFile = false;
|
||||
if (!file.exists())
|
||||
{
|
||||
goCreateFile = true;
|
||||
}
|
||||
|
||||
if (!file.open(QIODevice::ReadWrite))
|
||||
{
|
||||
//qWarning() << "cannot read/write conf file! ("<<filename<<")";
|
||||
return;
|
||||
}
|
||||
|
||||
if (goCreateFile)
|
||||
{
|
||||
addDefaultPreset();
|
||||
createFile();
|
||||
savePresets(); // add default preset to new file
|
||||
}
|
||||
|
||||
// read file...
|
||||
QString presetString(QString::fromUtf8(file.readLine(32000)));
|
||||
while (!presetString.isEmpty())
|
||||
{
|
||||
if (presetString.endsWith(newLine))
|
||||
{
|
||||
presetString.truncate(presetString.length() - newLine.length());
|
||||
}
|
||||
|
||||
if (presetString.at(0) == QLatin1Char('#')) // allow comments
|
||||
{
|
||||
//qWarning() << "line is comment line! next line..";
|
||||
presetString = QString::fromUtf8(file.readLine(32000));
|
||||
continue;
|
||||
}
|
||||
|
||||
QStringList presetStringList = presetString.split(QStringLiteral(":"));
|
||||
|
||||
if (presetStringList.count() != 2)
|
||||
{
|
||||
//qWarning() << "line ded! next line..";
|
||||
presetString = QString::fromUtf8(file.readLine(32000));
|
||||
continue;
|
||||
}
|
||||
|
||||
Preset preset;
|
||||
preset.name = presetStringList.at(0);
|
||||
|
||||
QStringList presetValueList = presetStringList.at(1).split(QStringLiteral(","));
|
||||
|
||||
if (presetValueList.count() < 5) // port is optional, as there is a default port !!
|
||||
{
|
||||
//qWarning() << "too few valz! next line..";
|
||||
presetString = QString::fromUtf8(file.readLine(32000));
|
||||
continue;
|
||||
}
|
||||
|
||||
preset.loginData.database = presetValueList.at(0);
|
||||
preset.loginData.username = presetValueList.at(1);
|
||||
preset.loginData.password = presetValueList.at(2);
|
||||
preset.loginData.hostname = presetValueList.at(3);
|
||||
preset.loginData.driver = presetValueList.at(4);
|
||||
|
||||
if (presetValueList.count() > 5) // is there a port?
|
||||
preset.loginData.port = presetValueList.at(5).toInt();
|
||||
|
||||
// keep compatibility
|
||||
if (presetValueList.count() > 6) // is there a filename?
|
||||
preset.loginData.filename = presetValueList.at(6);
|
||||
|
||||
presetList.append(preset);
|
||||
presetString = QString::fromUtf8(file.readLine(32000));
|
||||
}
|
||||
}
|
||||
|
||||
const PresetManager::PresetList &PresetManager::getPresetList() const
|
||||
{
|
||||
return presetList;
|
||||
}
|
||||
|
||||
|
||||
void PresetManager::savePreset(const LoginData &loginData, QWidget* parent)
|
||||
{
|
||||
bool ok;
|
||||
QString presetName = QInputDialog::getText(parent, tr("Save new Preset as.."),
|
||||
tr("Preset name:"), QLineEdit::Normal,
|
||||
QString(), &ok);
|
||||
|
||||
if (!ok || presetName.isEmpty())
|
||||
{
|
||||
//qWarning() << "doin nothin";
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 0; i < presetList.size(); ++i)
|
||||
{
|
||||
if (presetList.at(i).name == presetName)
|
||||
{
|
||||
presetList.removeAt(i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Preset preset;
|
||||
preset.name = presetName;
|
||||
preset.loginData = loginData;
|
||||
presetList.append(preset);
|
||||
|
||||
savePresets();
|
||||
}
|
||||
|
||||
|
||||
void PresetManager::savePresets()
|
||||
{
|
||||
createFile(); // recreate file
|
||||
|
||||
// save all presets to file
|
||||
for (const Preset &preset : qAsConst(presetList))
|
||||
{
|
||||
QString presetString = QStringLiteral("%1:%2%3");
|
||||
|
||||
QStringList presetValueList;
|
||||
|
||||
presetValueList << preset.loginData.database;
|
||||
presetValueList << preset.loginData.username;
|
||||
presetValueList << preset.loginData.password;
|
||||
presetValueList << preset.loginData.hostname;
|
||||
presetValueList << preset.loginData.driver;
|
||||
|
||||
if (preset.loginData.port > -1)
|
||||
presetValueList << QString::number(preset.loginData.port);
|
||||
|
||||
if (!preset.loginData.filename.isEmpty())
|
||||
presetValueList << preset.loginData.filename;
|
||||
|
||||
presetString = presetString.arg(preset.name, presetValueList.join(QLatin1Char(',')), newLine);
|
||||
|
||||
file.write(presetString.toUtf8());
|
||||
}
|
||||
|
||||
file.flush();
|
||||
}
|
||||
|
||||
|
||||
void PresetManager::addDefaultPreset()
|
||||
{
|
||||
Preset defaultPreset;
|
||||
|
||||
//defaultPreset.loginData.database = Settings::databaseName();
|
||||
defaultPreset.loginData.driver = QStringLiteral("PQSQL");
|
||||
defaultPreset.name = defaultPreset.loginData.database;
|
||||
defaultPreset.loginData.username = QStringLiteral("ctc_ws");
|
||||
defaultPreset.loginData.password = QStringLiteral("ctc11de");
|
||||
//defaultPreset.loginData.hostname = Settings::databaseHostname();
|
||||
defaultPreset.loginData.port = -1;
|
||||
// no need to set filename
|
||||
|
||||
presetList.insert(0, defaultPreset); // first item!
|
||||
}
|
||||
|
||||
|
||||
void PresetManager::createFile()
|
||||
{
|
||||
file.resize(0); // clear file
|
||||
file.flush();
|
||||
|
||||
file.write("#<PresetName>:<database><,username><,password><,hostname><,port(optional)><,filename(optional)>");
|
||||
file.write(newLine.toUtf8());
|
||||
file.flush();
|
||||
}
|
||||
Reference in new issue
Block a user