keepassxc

Форк
0
/
CsvExporter.cpp 
120 строк · 3.4 Кб
1
/*
2
 *  Copyright (C) 2015 Florian Geyer <blueice@fobos.de>
3
 *  Copyright (C) 2015 Felix Geyer <debfx@fobos.de>
4
 *
5
 *  This program is free software: you can redistribute it and/or modify
6
 *  it under the terms of the GNU General Public License as published by
7
 *  the Free Software Foundation, either version 2 or (at your option)
8
 *  version 3 of the License.
9
 *
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU General Public License for more details.
14
 *
15
 *  You should have received a copy of the GNU General Public License
16
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18

19
#include "CsvExporter.h"
20

21
#include <QFile>
22

23
#include "core/Group.h"
24

25
bool CsvExporter::exportDatabase(const QString& filename, const QSharedPointer<const Database>& db)
26
{
27
    QFile file(filename);
28
    if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
29
        m_error = file.errorString();
30
        return false;
31
    }
32
    return exportDatabase(&file, db);
33
}
34

35
bool CsvExporter::exportDatabase(QIODevice* device, const QSharedPointer<const Database>& db)
36
{
37
    if (device->write(exportHeader().toUtf8()) == -1) {
38
        m_error = device->errorString();
39
        return false;
40
    }
41

42
    if (device->write(exportGroup(db->rootGroup()).toUtf8()) == -1) {
43
        m_error = device->errorString();
44
        return false;
45
    }
46

47
    return true;
48
}
49

50
QString CsvExporter::exportDatabase(const QSharedPointer<const Database>& db)
51
{
52
    return exportHeader() + exportGroup(db->rootGroup());
53
}
54

55
QString CsvExporter::errorString() const
56
{
57
    return m_error;
58
}
59

60
QString CsvExporter::exportHeader()
61
{
62
    QString header;
63
    addColumn(header, "Group");
64
    addColumn(header, "Title");
65
    addColumn(header, "Username");
66
    addColumn(header, "Password");
67
    addColumn(header, "URL");
68
    addColumn(header, "Notes");
69
    addColumn(header, "TOTP");
70
    addColumn(header, "Icon");
71
    addColumn(header, "Last Modified");
72
    addColumn(header, "Created");
73
    return header + QString("\n");
74
}
75

76
QString CsvExporter::exportGroup(const Group* group, QString groupPath)
77
{
78
    QString response;
79
    if (!groupPath.isEmpty()) {
80
        groupPath.append("/");
81
    }
82
    groupPath.append(group->name());
83

84
    const QList<Entry*>& entryList = group->entries();
85
    for (const Entry* entry : entryList) {
86
        QString line;
87

88
        addColumn(line, groupPath);
89
        addColumn(line, entry->title());
90
        addColumn(line, entry->username());
91
        addColumn(line, entry->password());
92
        addColumn(line, entry->url());
93
        addColumn(line, entry->notes());
94
        addColumn(line, entry->totpSettingsString());
95
        addColumn(line, QString::number(entry->iconNumber()));
96
        addColumn(line, entry->timeInfo().lastModificationTime().toString(Qt::ISODate));
97
        addColumn(line, entry->timeInfo().creationTime().toString(Qt::ISODate));
98

99
        line.append("\n");
100
        response.append(line);
101
    }
102

103
    const QList<Group*>& children = group->children();
104
    for (const Group* child : children) {
105
        response.append(exportGroup(child, groupPath));
106
    }
107

108
    return response;
109
}
110

111
void CsvExporter::addColumn(QString& str, const QString& column)
112
{
113
    if (!str.isEmpty()) {
114
        str.append(",");
115
    }
116

117
    str.append("\"");
118
    str.append(QString(column).replace("\"", "\"\""));
119
    str.append("\"");
120
}
121

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

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

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

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