2
* Copyright (C) 2019 KeePassXC Team <team@keepassxc.org>
4
* This program is free software: you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation, either version 2 or (at your option)
7
* version 3 of the License.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program. If not, see <http://www.gnu.org/licenses/>.
20
#include "DatabaseCreate.h"
23
#include <QCommandLineParser>
27
* Create a database file from an XML export of another database.
28
* A password can be specified to encrypt the database.
29
* If none is specified the function will fail.
31
* If the database is being saved in a non existent directory, the
34
* @return EXIT_SUCCESS on success, or EXIT_FAILURE on failure
39
name = QString("import");
40
description = QObject::tr("Import the contents of an XML database.");
41
positionalArguments.append({QString("xml"), QObject::tr("Path of the XML database export."), QString("")});
42
positionalArguments.append({QString("database"), QObject::tr("Path of the new database."), QString("")});
43
options.append(DatabaseCreate::SetKeyFileOption);
44
options.append(DatabaseCreate::SetKeyFileShortOption);
45
options.append(DatabaseCreate::SetPasswordOption);
46
options.append(DatabaseCreate::DecryptionTimeOption);
49
int Import::execute(const QStringList& arguments)
51
QSharedPointer<QCommandLineParser> parser = getCommandLineParser(arguments);
52
if (parser.isNull()) {
56
auto& out = parser->isSet(Command::QuietOption) ? Utils::DEVNULL : Utils::STDOUT;
57
auto& err = Utils::STDERR;
59
const QStringList args = parser->positionalArguments();
60
const QString& xmlExportPath = args.at(0);
61
const QString& dbPath = args.at(1);
63
if (QFileInfo::exists(dbPath)) {
64
err << QObject::tr("File %1 already exists.").arg(dbPath) << endl;
68
QSharedPointer<Database> db = DatabaseCreate::initializeDatabaseFromOptions(parser);
74
if (!db->import(xmlExportPath, &errorMessage)) {
75
err << QObject::tr("Unable to import XML database: %1").arg(errorMessage) << endl;
79
if (!db->saveAs(dbPath, Database::Atomic, {}, &errorMessage)) {
80
err << QObject::tr("Failed to save the database: %1.").arg(errorMessage) << endl;
84
out << QObject::tr("Successfully imported database.") << endl;