keepassxc

Форк
0
/
Export.cpp 
62 строки · 2.2 Кб
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 "Export.h"
19

20
#include "TextStream.h"
21
#include "Utils.h"
22
#include "format/CsvExporter.h"
23

24
#include <QCommandLineParser>
25

26
const QCommandLineOption Export::FormatOption = QCommandLineOption(
27
    QStringList() << "f"
28
                  << "format",
29
    QObject::tr("Format to use when exporting. Available choices are 'xml' or 'csv'. Defaults to 'xml'."),
30
    QStringLiteral("xml|csv"));
31

32
Export::Export()
33
{
34
    name = QStringLiteral("export");
35
    options.append(Export::FormatOption);
36
    description = QObject::tr("Exports the content of a database to standard output in the specified format.");
37
}
38

39
int Export::executeWithDatabase(QSharedPointer<Database> database, QSharedPointer<QCommandLineParser> parser)
40
{
41
    TextStream out(Utils::STDOUT.device());
42
    auto& err = Utils::STDERR;
43

44
    QString format = parser->value(Export::FormatOption);
45
    if (format.isEmpty() || format.startsWith(QStringLiteral("xml"), Qt::CaseInsensitive)) {
46
        QByteArray xmlData;
47
        QString errorMessage;
48
        if (!database->extract(xmlData, &errorMessage)) {
49
            err << QObject::tr("Unable to export database to XML: %1").arg(errorMessage) << endl;
50
            return EXIT_FAILURE;
51
        }
52
        out.write(xmlData.constData());
53
    } else if (format.startsWith(QStringLiteral("csv"), Qt::CaseInsensitive)) {
54
        CsvExporter csvExporter;
55
        out << csvExporter.exportDatabase(database);
56
    } else {
57
        err << QObject::tr("Unsupported format %1").arg(format) << endl;
58
        return EXIT_FAILURE;
59
    }
60

61
    return EXIT_SUCCESS;
62
}
63

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

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

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

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