2021-12-26 23:18:28 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2020-2022 Roy Qu (royqh1979@gmail.com)
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2021-09-04 11:37:04 +08:00
|
|
|
#include "autolinkmanager.h"
|
|
|
|
#include "settings.h"
|
|
|
|
|
|
|
|
#include <QDir>
|
|
|
|
#include <QJsonArray>
|
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QJsonObject>
|
2021-10-06 23:19:18 +08:00
|
|
|
#include "systemconsts.h"
|
2021-09-04 11:37:04 +08:00
|
|
|
|
|
|
|
AutolinkManager* pAutolinkManager;
|
|
|
|
|
|
|
|
AutolinkManager::AutolinkManager()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
PAutolink AutolinkManager::getLink(const QString &header) const
|
|
|
|
{
|
|
|
|
return mLinks.value(header,PAutolink());
|
|
|
|
}
|
|
|
|
|
|
|
|
void AutolinkManager::load()
|
|
|
|
{
|
|
|
|
QDir dir(pSettings->dirs().config());
|
2021-10-06 23:19:18 +08:00
|
|
|
QString filename=dir.filePath(DEV_AUTOLINK_FILE);
|
2021-09-04 11:37:04 +08:00
|
|
|
QFile file(filename);
|
|
|
|
if (!file.exists()) {
|
2022-01-06 17:52:52 +08:00
|
|
|
#ifdef Q_OS_WIN
|
2021-09-04 19:27:44 +08:00
|
|
|
QFile preFile(":/config/autolink.json");
|
|
|
|
if (!preFile.open(QFile::ReadOnly)) {
|
|
|
|
throw FileError(QObject::tr("Can't open file '%1' for read.")
|
|
|
|
.arg(":/config/autolink.json"));
|
|
|
|
}
|
|
|
|
QByteArray content=preFile.readAll();
|
|
|
|
if (!file.open(QFile::WriteOnly|QFile::Truncate)) {
|
2021-09-04 11:37:04 +08:00
|
|
|
throw FileError(QObject::tr("Can't open file '%1' for write.")
|
|
|
|
.arg(filename));
|
|
|
|
}
|
2021-09-04 19:27:44 +08:00
|
|
|
file.write(content);
|
|
|
|
file.close();
|
|
|
|
preFile.close();
|
2022-01-06 17:52:52 +08:00
|
|
|
#else
|
|
|
|
return;
|
|
|
|
#endif
|
2021-09-04 11:37:04 +08:00
|
|
|
}
|
|
|
|
if (file.open(QFile::ReadOnly)) {
|
|
|
|
QByteArray content = file.readAll();
|
|
|
|
QJsonDocument doc(QJsonDocument::fromJson(content));
|
|
|
|
fromJson(doc.array());
|
|
|
|
file.close();
|
|
|
|
} else {
|
|
|
|
throw FileError(QObject::tr("Can't open file '%1' for read.")
|
|
|
|
.arg(filename));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AutolinkManager::save()
|
|
|
|
{
|
|
|
|
QDir dir(pSettings->dirs().config());
|
2021-10-06 23:19:18 +08:00
|
|
|
QString filename=dir.filePath(DEV_AUTOLINK_FILE);
|
2021-09-04 11:37:04 +08:00
|
|
|
QFile file(filename);
|
|
|
|
if (file.open(QFile::WriteOnly|QFile::Truncate)) {
|
|
|
|
QJsonDocument doc(toJson());
|
|
|
|
file.write(doc.toJson(QJsonDocument::Indented));
|
|
|
|
file.close();
|
|
|
|
} else {
|
|
|
|
throw FileError(QObject::tr("Can't open file '%1' for write.")
|
|
|
|
.arg(filename));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AutolinkManager::setLink(const QString &header, const QString &linkOption)
|
|
|
|
{
|
|
|
|
PAutolink link = mLinks.value(header,PAutolink());
|
|
|
|
if (link) {
|
|
|
|
link->linkOption = linkOption;
|
|
|
|
} else {
|
|
|
|
link = std::make_shared<Autolink>();
|
|
|
|
link->header = header;
|
|
|
|
link->linkOption = linkOption;
|
|
|
|
mLinks.insert(header,link);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const QMap<QString, PAutolink> &AutolinkManager::links() const
|
|
|
|
{
|
|
|
|
return mLinks;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AutolinkManager::clear()
|
|
|
|
{
|
|
|
|
mLinks.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
QJsonArray AutolinkManager::toJson()
|
|
|
|
{
|
|
|
|
QJsonArray result;
|
|
|
|
foreach (const QString& header, mLinks.keys()){
|
|
|
|
QJsonObject autolink;
|
|
|
|
autolink["header"]=header;
|
|
|
|
autolink["links"]=mLinks[header]->linkOption;
|
|
|
|
result.append(autolink);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AutolinkManager::fromJson(QJsonArray json)
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
for (int i=0;i<json.size();i++) {
|
|
|
|
QJsonObject obj = json[i].toObject();
|
|
|
|
setLink(obj["header"].toString(),obj["links"].toString());
|
|
|
|
}
|
|
|
|
}
|