2019-06-16 21:33:13 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2019 KeePassXC Team <team@keepassxc.org>
|
|
|
|
*
|
|
|
|
* 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 2 or (at your option)
|
|
|
|
* version 3 of the License.
|
|
|
|
*
|
|
|
|
* 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 <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Export.h"
|
|
|
|
|
2021-02-20 08:41:47 -05:00
|
|
|
#include "TextStream.h"
|
|
|
|
#include "Utils.h"
|
2019-06-16 21:33:13 -04:00
|
|
|
#include "format/CsvExporter.h"
|
2025-02-23 14:43:06 +01:00
|
|
|
#include "format/HtmlExporter.h"
|
2019-06-16 21:33:13 -04:00
|
|
|
|
2021-07-11 22:10:29 -04:00
|
|
|
#include <QCommandLineParser>
|
|
|
|
|
2019-11-10 08:34:59 -05:00
|
|
|
const QCommandLineOption Export::FormatOption = QCommandLineOption(
|
2024-08-13 21:38:37 -04:00
|
|
|
QStringList() << "f" << "format",
|
2025-02-23 14:43:06 +01:00
|
|
|
QObject::tr("Format to use when exporting. Available choices are 'xml', 'csv' or 'html'. Defaults to 'xml'."),
|
|
|
|
QStringLiteral("xml|csv|html"));
|
2019-06-16 21:33:13 -04:00
|
|
|
|
|
|
|
Export::Export()
|
|
|
|
{
|
2019-10-20 22:16:19 -04:00
|
|
|
name = QStringLiteral("export");
|
2019-06-16 21:33:13 -04:00
|
|
|
options.append(Export::FormatOption);
|
|
|
|
description = QObject::tr("Exports the content of a database to standard output in the specified format.");
|
|
|
|
}
|
|
|
|
|
|
|
|
int Export::executeWithDatabase(QSharedPointer<Database> database, QSharedPointer<QCommandLineParser> parser)
|
|
|
|
{
|
2020-05-11 07:31:29 -04:00
|
|
|
TextStream out(Utils::STDOUT.device());
|
|
|
|
auto& err = Utils::STDERR;
|
2019-06-16 21:33:13 -04:00
|
|
|
|
|
|
|
QString format = parser->value(Export::FormatOption);
|
2019-11-10 08:34:59 -05:00
|
|
|
if (format.isEmpty() || format.startsWith(QStringLiteral("xml"), Qt::CaseInsensitive)) {
|
2019-06-16 21:33:13 -04:00
|
|
|
QByteArray xmlData;
|
|
|
|
QString errorMessage;
|
|
|
|
if (!database->extract(xmlData, &errorMessage)) {
|
2020-05-11 07:31:29 -04:00
|
|
|
err << QObject::tr("Unable to export database to XML: %1").arg(errorMessage) << endl;
|
2019-06-16 21:33:13 -04:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2020-05-11 07:31:29 -04:00
|
|
|
out.write(xmlData.constData());
|
2019-11-10 08:34:59 -05:00
|
|
|
} else if (format.startsWith(QStringLiteral("csv"), Qt::CaseInsensitive)) {
|
2019-06-16 21:33:13 -04:00
|
|
|
CsvExporter csvExporter;
|
2020-05-11 07:31:29 -04:00
|
|
|
out << csvExporter.exportDatabase(database);
|
2025-02-23 14:43:06 +01:00
|
|
|
} else if (format.startsWith(QStringLiteral("html"), Qt::CaseInsensitive)) {
|
|
|
|
HtmlExporter htmlExporter;
|
|
|
|
out << htmlExporter.exportDatabase(database);
|
2019-06-16 21:33:13 -04:00
|
|
|
} else {
|
2020-05-11 07:31:29 -04:00
|
|
|
err << QObject::tr("Unsupported format %1").arg(format) << endl;
|
2019-06-16 21:33:13 -04:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|