keepassxc

Форк
0
/
Import.cpp 
86 строк · 2.9 Кб
1
/*
2
 *  Copyright (C) 2019 KeePassXC Team <team@keepassxc.org>
3
 *
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.
8
 *
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.
13
 *
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/>.
16
 */
17

18
#include "Import.h"
19

20
#include "DatabaseCreate.h"
21
#include "Utils.h"
22

23
#include <QCommandLineParser>
24
#include <QFileInfo>
25

26
/**
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.
30
 *
31
 * If the database is being saved in a non existent directory, the
32
 * function will fail.
33
 *
34
 * @return EXIT_SUCCESS on success, or EXIT_FAILURE on failure
35
 */
36

37
Import::Import()
38
{
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);
47
}
48

49
int Import::execute(const QStringList& arguments)
50
{
51
    QSharedPointer<QCommandLineParser> parser = getCommandLineParser(arguments);
52
    if (parser.isNull()) {
53
        return EXIT_FAILURE;
54
    }
55

56
    auto& out = parser->isSet(Command::QuietOption) ? Utils::DEVNULL : Utils::STDOUT;
57
    auto& err = Utils::STDERR;
58

59
    const QStringList args = parser->positionalArguments();
60
    const QString& xmlExportPath = args.at(0);
61
    const QString& dbPath = args.at(1);
62

63
    if (QFileInfo::exists(dbPath)) {
64
        err << QObject::tr("File %1 already exists.").arg(dbPath) << endl;
65
        return EXIT_FAILURE;
66
    }
67

68
    QSharedPointer<Database> db = DatabaseCreate::initializeDatabaseFromOptions(parser);
69
    if (!db) {
70
        return EXIT_FAILURE;
71
    }
72

73
    QString errorMessage;
74
    if (!db->import(xmlExportPath, &errorMessage)) {
75
        err << QObject::tr("Unable to import XML database: %1").arg(errorMessage) << endl;
76
        return EXIT_FAILURE;
77
    }
78

79
    if (!db->saveAs(dbPath, Database::Atomic, {}, &errorMessage)) {
80
        err << QObject::tr("Failed to save the database: %1.").arg(errorMessage) << endl;
81
        return EXIT_FAILURE;
82
    }
83

84
    out << QObject::tr("Successfully imported database.") << endl;
85
    return EXIT_SUCCESS;
86
}
87

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.