2
* Copyright (C) 2015 Florian Geyer <blueice@fobos.de>
3
* Copyright (C) 2015 Felix Geyer <debfx@fobos.de>
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.
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.
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/>.
19
#include "CsvExporter.h"
23
#include "core/Group.h"
25
bool CsvExporter::exportDatabase(const QString& filename, const QSharedPointer<const Database>& db)
28
if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
29
m_error = file.errorString();
32
return exportDatabase(&file, db);
35
bool CsvExporter::exportDatabase(QIODevice* device, const QSharedPointer<const Database>& db)
37
if (device->write(exportHeader().toUtf8()) == -1) {
38
m_error = device->errorString();
42
if (device->write(exportGroup(db->rootGroup()).toUtf8()) == -1) {
43
m_error = device->errorString();
50
QString CsvExporter::exportDatabase(const QSharedPointer<const Database>& db)
52
return exportHeader() + exportGroup(db->rootGroup());
55
QString CsvExporter::errorString() const
60
QString CsvExporter::exportHeader()
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");
76
QString CsvExporter::exportGroup(const Group* group, QString groupPath)
79
if (!groupPath.isEmpty()) {
80
groupPath.append("/");
82
groupPath.append(group->name());
84
const QList<Entry*>& entryList = group->entries();
85
for (const Entry* entry : entryList) {
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));
100
response.append(line);
103
const QList<Group*>& children = group->children();
104
for (const Group* child : children) {
105
response.append(exportGroup(child, groupPath));
111
void CsvExporter::addColumn(QString& str, const QString& column)
113
if (!str.isEmpty()) {
118
str.append(QString(column).replace("\"", "\"\""));