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/>.
*/
#include "ExportToModList.h"
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
namespace ExportToModList {
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");
}
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: {
return QString("unknown format:%1").arg(format);
}

View File

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

View File

@ -89,6 +89,20 @@ void ExportToModListDialog::formatChanged(int index)
break;
}
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->optionsGroup->setDisabled(true);
ui->resultText->hide();
@ -133,6 +147,12 @@ void ExportToModListDialog::triggerImp()
}
case ExportToModList::CUSTOM:
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 (ui->templateText->toPlainText() != exampleLine)
@ -146,7 +166,7 @@ void ExportToModListDialog::done(int result)
const QString filename = FS::RemoveInvalidFilenameChars(name);
const QString output =
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())
return;
@ -167,6 +187,10 @@ QString ExportToModListDialog::extension()
return ".txt";
case ExportToModList::CUSTOM:
return ".txt";
case ExportToModList::JSON:
return ".json";
case ExportToModList::CSV:
return ".csv";
}
return ".txt";
}

View File

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