Added json and csv format

Signed-off-by: Trial97 <alexandru.tripon97@gmail.com>
This commit is contained in:
Trial97 2023-06-27 16:57:30 +03:00
parent 515197fba2
commit 9a3931dac6
No known key found for this signature in database
GPG Key ID: 55EF5DA53DB36318
4 changed files with 92 additions and 2 deletions

View File

@ -16,6 +16,9 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#include "ExportToModList.h" #include "ExportToModList.h"
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
namespace ExportToModList { namespace ExportToModList {
QString ExportToModList(QList<Mod*> mods, Formats format, OptionalData extraData) QString ExportToModList(QList<Mod*> mods, Formats format, OptionalData extraData)
@ -94,6 +97,59 @@ QString ExportToModList(QList<Mod*> mods, Formats format, OptionalData extraData
} }
return lines.join("\n"); return lines.join("\n");
} }
case JSON: {
QJsonArray lines;
for (auto mod : mods) {
auto meta = mod->metadata();
auto modName = mod->name();
QJsonObject line;
line["name"] = modName;
if (extraData & Url) {
auto url = mod->metaurl();
if (!url.isEmpty())
line["url"] = url;
}
if (extraData & Version) {
auto ver = mod->version();
if (ver.isEmpty() && meta != nullptr)
ver = meta->version().toString();
if (!ver.isEmpty())
line["version"] = ver;
}
if (extraData & Authors && !mod->authors().isEmpty())
line["authors"] = QJsonArray::fromStringList(mod->authors());
lines << line;
}
QJsonDocument doc;
doc.setArray(lines);
return doc.toJson();
}
case CSV: {
QStringList lines;
for (auto mod : mods) {
QStringList data;
auto meta = mod->metadata();
auto modName = mod->name();
data << modName;
if (extraData & Url) {
auto url = mod->metaurl();
if (!url.isEmpty())
data << url;
}
if (extraData & Version) {
auto ver = mod->version();
if (ver.isEmpty() && meta != nullptr)
ver = meta->version().toString();
if (!ver.isEmpty())
data << ver;
}
if (extraData & Authors && !mod->authors().isEmpty())
data << QString("\"%1\"").arg(mod->authors().join(","));
lines << data.join(",");
}
return lines.join("\n");
}
default: { default: {
return QString("unknown format:%1").arg(format); return QString("unknown format:%1").arg(format);
} }

View File

@ -22,7 +22,7 @@
namespace ExportToModList { namespace ExportToModList {
enum Formats { HTML, MARKDOWN, PLAINTXT, CUSTOM }; enum Formats { HTML, MARKDOWN, PLAINTXT, JSON, CSV, CUSTOM };
enum OptionalData { enum OptionalData {
Authors = 1 << 0, Authors = 1 << 0,
Url = 1 << 1, Url = 1 << 1,

View File

@ -89,6 +89,20 @@ void ExportToModListDialog::formatChanged(int index)
break; break;
} }
case 3: { case 3: {
ui->templateGroup->setDisabled(true);
ui->optionsGroup->setDisabled(false);
ui->resultText->hide();
format = ExportToModList::JSON;
break;
}
case 4: {
ui->templateGroup->setDisabled(true);
ui->optionsGroup->setDisabled(false);
ui->resultText->hide();
format = ExportToModList::CSV;
break;
}
case 5: {
ui->templateGroup->setDisabled(false); ui->templateGroup->setDisabled(false);
ui->optionsGroup->setDisabled(true); ui->optionsGroup->setDisabled(true);
ui->resultText->hide(); ui->resultText->hide();
@ -133,6 +147,12 @@ void ExportToModListDialog::triggerImp()
} }
case ExportToModList::CUSTOM: case ExportToModList::CUSTOM:
return; return;
case ExportToModList::JSON:
exampleLine = "{\"name\":\"{name}\",\"url\":\"{url}\",\"version\":\"{version}\",\"authors\":\"{authors}\"},";
break;
case ExportToModList::CSV:
exampleLine = "{name},{url},{version},\"{authors}\"";
break;
} }
if (!m_template_selected) { if (!m_template_selected) {
if (ui->templateText->toPlainText() != exampleLine) if (ui->templateText->toPlainText() != exampleLine)
@ -146,7 +166,7 @@ void ExportToModListDialog::done(int result)
const QString filename = FS::RemoveInvalidFilenameChars(name); const QString filename = FS::RemoveInvalidFilenameChars(name);
const QString output = const QString output =
QFileDialog::getSaveFileName(this, tr("Export %1").arg(name), FS::PathCombine(QDir::homePath(), filename + extension()), QFileDialog::getSaveFileName(this, tr("Export %1").arg(name), FS::PathCombine(QDir::homePath(), filename + extension()),
"File (*.txt *.html *.md)", nullptr); "File (*.txt *.html *.md *.json *.csv)", nullptr);
if (output.isEmpty()) if (output.isEmpty())
return; return;
@ -167,6 +187,10 @@ QString ExportToModListDialog::extension()
return ".txt"; return ".txt";
case ExportToModList::CUSTOM: case ExportToModList::CUSTOM:
return ".txt"; return ".txt";
case ExportToModList::JSON:
return ".json";
case ExportToModList::CSV:
return ".csv";
} }
return ".txt"; return ".txt";
} }

View File

@ -58,6 +58,16 @@
<string>Plaintext</string> <string>Plaintext</string>
</property> </property>
</item> </item>
<item>
<property name="text">
<string>JSON</string>
</property>
</item>
<item>
<property name="text">
<string>CSV</string>
</property>
</item>
<item> <item>
<property name="text"> <property name="text">
<string>Custom</string> <string>Custom</string>