Refactor database extraction (#2698)

Previously, extracting the XML from a database was done with the
`saveXml` attribute in the `KeePass2Reader` class.

This had several unfortunate consequences:
* The `KdbxReader` class had to import the `KdbxXmlWriter` class
in order to perform the export (bad separation of concerns);
* The CLI database unlocking logic had to be duplicated only
for the `Extract` command;
* The `xmlData` had to be stored in the `KeePass2Reader` as
a temporary result.
* Lots of `setSaveXml` functions were implemented only
to trickle down this functionality.

Also, the naming of the `saveXml` variable was not really
helpful to understand it's role.

Overall, this change will make it easier to maintain and expand
the CLI database unlocking logic (for example, adding a `--no-password`
option as requested in https://github.com/keepassxreboot/keepassxc/issues/1873)
It also opens to door to other types of extraction/exporting (for
example exporting to CSV, as requested in
https://github.com/keepassxreboot/keepassxc/issues/2572)
This commit is contained in:
louib 2019-02-13 13:24:55 -05:00 committed by Janek Bevendorff
parent 8bfc539234
commit 504904a414
15 changed files with 79 additions and 130 deletions

View file

@ -281,7 +281,6 @@ bool Database::writeDatabase(QIODevice* device, QString* error)
setEmitModified(true);
if (writer.hasError()) {
// the writer failed
if (error) {
*error = writer.errorString();
}
@ -292,6 +291,20 @@ bool Database::writeDatabase(QIODevice* device, QString* error)
return true;
}
bool Database::extract(QByteArray& xmlOutput, QString* error)
{
KeePass2Writer writer;
writer.extractDatabase(this, xmlOutput);
if (writer.hasError()) {
if (error) {
*error = writer.errorString();
}
return false;
}
return true;
}
/**
* Remove the old backup and replace it with a new one
* backups are named <filename>.old.kdbx