mirror of
https://github.com/keepassxreboot/keepassxc.git
synced 2025-09-18 11:54:47 -04:00
Add ability to export databases to CSV files.
Based on implementation by Florian Geyer <blueice@fobos.de> Closes #57
This commit is contained in:
parent
0185b112e1
commit
ceb6a0383e
10 changed files with 337 additions and 0 deletions
|
@ -61,6 +61,7 @@ set(keepassx_SOURCES
|
|||
crypto/SymmetricCipher.cpp
|
||||
crypto/SymmetricCipherBackend.h
|
||||
crypto/SymmetricCipherGcrypt.cpp
|
||||
format/CsvExporter.cpp
|
||||
format/KeePass1.h
|
||||
format/KeePass1Reader.cpp
|
||||
format/KeePass2.h
|
||||
|
|
103
src/format/CsvExporter.cpp
Normal file
103
src/format/CsvExporter.cpp
Normal file
|
@ -0,0 +1,103 @@
|
|||
/*
|
||||
* Copyright (C) 2015 Florian Geyer <blueice@fobos.de>
|
||||
* Copyright (C) 2015 Felix Geyer <debfx@fobos.de>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 2 or (at your option)
|
||||
* version 3 of the License.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "CsvExporter.h"
|
||||
|
||||
#include <QFile>
|
||||
|
||||
#include "core/Database.h"
|
||||
#include "core/Group.h"
|
||||
|
||||
bool CsvExporter::exportDatabase(const QString& filename, const Database* db)
|
||||
{
|
||||
QFile file(filename);
|
||||
if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
|
||||
m_error = file.errorString();
|
||||
return false;
|
||||
}
|
||||
return exportDatabase(&file, db);
|
||||
}
|
||||
|
||||
bool CsvExporter::exportDatabase(QIODevice* device, const Database* db)
|
||||
{
|
||||
QString header;
|
||||
addColumn(header, "Group");
|
||||
addColumn(header, "Title");
|
||||
addColumn(header, "Username");
|
||||
addColumn(header, "Password");
|
||||
addColumn(header, "URL");
|
||||
addColumn(header, "Notes");
|
||||
header.append("\n");
|
||||
|
||||
if (device->write(header.toUtf8()) == -1) {
|
||||
m_error = device->errorString();
|
||||
return false;
|
||||
}
|
||||
|
||||
return writeGroup(device, db->rootGroup());
|
||||
}
|
||||
|
||||
QString CsvExporter::errorString() const
|
||||
{
|
||||
return m_error;
|
||||
}
|
||||
|
||||
bool CsvExporter::writeGroup(QIODevice* device, const Group* group, QString groupPath)
|
||||
{
|
||||
if (!groupPath.isEmpty()) {
|
||||
groupPath.append("/");
|
||||
}
|
||||
groupPath.append(group->name());
|
||||
|
||||
Q_FOREACH (const Entry* entry, group->entries()) {
|
||||
QString line;
|
||||
|
||||
addColumn(line, groupPath);
|
||||
addColumn(line, entry->title());
|
||||
addColumn(line, entry->username());
|
||||
addColumn(line, entry->password());
|
||||
addColumn(line, entry->url());
|
||||
addColumn(line, entry->notes());
|
||||
|
||||
line.append("\n");
|
||||
|
||||
if (device->write(line.toUtf8()) == -1) {
|
||||
m_error = device->errorString();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Q_FOREACH (const Group* child, group->children()) {
|
||||
if (!writeGroup(device, child, groupPath)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void CsvExporter::addColumn(QString& str, const QString& column)
|
||||
{
|
||||
if (!str.isEmpty()) {
|
||||
str.append(",");
|
||||
}
|
||||
|
||||
str.append("\"");
|
||||
str.append(QString(column).replace("\"", "\"\""));
|
||||
str.append("\"");
|
||||
}
|
42
src/format/CsvExporter.h
Normal file
42
src/format/CsvExporter.h
Normal file
|
@ -0,0 +1,42 @@
|
|||
/*
|
||||
* Copyright (C) 2015 Florian Geyer <blueice@fobos.de>
|
||||
* Copyright (C) 2015 Felix Geyer <debfx@fobos.de>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 2 or (at your option)
|
||||
* version 3 of the License.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef KEEPASSX_CSVEXPORTER_H
|
||||
#define KEEPASSX_CSVEXPORTER_H
|
||||
|
||||
#include <QString>
|
||||
|
||||
class Database;
|
||||
class Group;
|
||||
class QIODevice;
|
||||
|
||||
class CsvExporter
|
||||
{
|
||||
public:
|
||||
bool exportDatabase(const QString& filename, const Database* db);
|
||||
bool exportDatabase(QIODevice* device, const Database* db);
|
||||
QString errorString() const;
|
||||
|
||||
private:
|
||||
bool writeGroup(QIODevice* device, const Group* group, QString groupPath = QString());
|
||||
void addColumn(QString& str, const QString& column);
|
||||
|
||||
QString m_error;
|
||||
};
|
||||
|
||||
#endif // KEEPASSX_CSVEXPORTER_H
|
|
@ -26,6 +26,7 @@
|
|||
#include "core/Group.h"
|
||||
#include "core/Metadata.h"
|
||||
#include "core/qsavefile.h"
|
||||
#include "format/CsvExporter.h"
|
||||
#include "gui/DatabaseWidget.h"
|
||||
#include "gui/DatabaseWidgetStateSync.h"
|
||||
#include "gui/DragTabBar.h"
|
||||
|
@ -399,6 +400,27 @@ bool DatabaseTabWidget::saveDatabaseAs(int index)
|
|||
return saveDatabaseAs(indexDatabase(index));
|
||||
}
|
||||
|
||||
void DatabaseTabWidget::exportToCsv()
|
||||
{
|
||||
Database* db = indexDatabase(currentIndex());
|
||||
if (!db) {
|
||||
Q_ASSERT(false);
|
||||
return;
|
||||
}
|
||||
|
||||
QString fileName = fileDialog()->getSaveFileName(this, tr("Export database to CSV file"),
|
||||
QString(), tr("CSV file").append(" (*.csv)"));
|
||||
if (fileName.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
CsvExporter csvExporter;
|
||||
if (!csvExporter.exportDatabase(fileName, db)) {
|
||||
MessageBox::critical(this, tr("Error"), tr("Writing the CSV file failed.") + "\n\n"
|
||||
+ csvExporter.errorString());
|
||||
}
|
||||
}
|
||||
|
||||
void DatabaseTabWidget::changeMasterKey()
|
||||
{
|
||||
currentDatabaseWidget()->switchToMasterKeyChange();
|
||||
|
|
|
@ -66,6 +66,7 @@ public Q_SLOTS:
|
|||
void importKeePass1Database();
|
||||
bool saveDatabase(int index = -1);
|
||||
bool saveDatabaseAs(int index = -1);
|
||||
void exportToCsv();
|
||||
bool closeDatabase(int index = -1);
|
||||
void closeDatabaseFromSender();
|
||||
bool closeAllDatabases();
|
||||
|
|
|
@ -163,6 +163,8 @@ MainWindow::MainWindow()
|
|||
SLOT(changeDatabaseSettings()));
|
||||
connect(m_ui->actionImportKeePass1, SIGNAL(triggered()), m_ui->tabWidget,
|
||||
SLOT(importKeePass1Database()));
|
||||
connect(m_ui->actionExportCsv, SIGNAL(triggered()), m_ui->tabWidget,
|
||||
SLOT(exportToCsv()));
|
||||
connect(m_ui->actionLockDatabases, SIGNAL(triggered()), m_ui->tabWidget,
|
||||
SLOT(lockDatabases()));
|
||||
connect(m_ui->actionQuit, SIGNAL(triggered()), SLOT(close()));
|
||||
|
@ -303,6 +305,7 @@ void MainWindow::setMenuActionState(DatabaseWidget::Mode mode)
|
|||
m_ui->actionChangeDatabaseSettings->setEnabled(true);
|
||||
m_ui->actionDatabaseSave->setEnabled(true);
|
||||
m_ui->actionDatabaseSaveAs->setEnabled(true);
|
||||
m_ui->actionExportCsv->setEnabled(true);
|
||||
break;
|
||||
}
|
||||
case DatabaseWidget::EditMode:
|
||||
|
@ -326,6 +329,7 @@ void MainWindow::setMenuActionState(DatabaseWidget::Mode mode)
|
|||
m_ui->actionChangeDatabaseSettings->setEnabled(false);
|
||||
m_ui->actionDatabaseSave->setEnabled(false);
|
||||
m_ui->actionDatabaseSaveAs->setEnabled(false);
|
||||
m_ui->actionExportCsv->setEnabled(false);
|
||||
break;
|
||||
default:
|
||||
Q_ASSERT(false);
|
||||
|
@ -354,6 +358,7 @@ void MainWindow::setMenuActionState(DatabaseWidget::Mode mode)
|
|||
m_ui->actionDatabaseSaveAs->setEnabled(false);
|
||||
|
||||
m_ui->actionDatabaseClose->setEnabled(false);
|
||||
m_ui->actionExportCsv->setEnabled(false);
|
||||
}
|
||||
|
||||
bool inDatabaseTabWidgetOrWelcomeWidget = inDatabaseTabWidget || inWelcomeWidget;
|
||||
|
|
|
@ -120,6 +120,7 @@
|
|||
<addaction name="actionChangeDatabaseSettings"/>
|
||||
<addaction name="separator"/>
|
||||
<addaction name="actionImportKeePass1"/>
|
||||
<addaction name="actionExportCsv"/>
|
||||
<addaction name="separator"/>
|
||||
<addaction name="actionQuit"/>
|
||||
</widget>
|
||||
|
@ -413,6 +414,14 @@
|
|||
<string>Notes</string>
|
||||
</property>
|
||||
</action>
|
||||
<action name="actionExportCsv">
|
||||
<property name="enabled">
|
||||
<bool>false</bool>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>Export to CSV file</string>
|
||||
</property>
|
||||
</action>
|
||||
</widget>
|
||||
<customwidgets>
|
||||
<customwidget>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue