2010-08-18 16:57:26 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 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/>.
|
|
|
|
*/
|
|
|
|
|
2010-08-31 08:39:35 -04:00
|
|
|
#include "KeePass2XmlWriter.h"
|
2010-08-18 16:57:26 -04:00
|
|
|
|
|
|
|
#include <QtCore/QBuffer>
|
2010-08-22 10:01:09 -04:00
|
|
|
#include <QtCore/QFile>
|
2010-08-18 16:57:26 -04:00
|
|
|
|
|
|
|
#include "core/Metadata.h"
|
2011-07-06 18:15:52 -04:00
|
|
|
#include "format/KeePass2RandomStream.h"
|
2012-04-21 10:45:46 -04:00
|
|
|
#include "streams/QtIOCompressor"
|
2010-08-18 16:57:26 -04:00
|
|
|
|
2010-08-31 10:18:45 -04:00
|
|
|
KeePass2XmlWriter::KeePass2XmlWriter()
|
2012-06-29 08:15:16 -04:00
|
|
|
: m_db(Q_NULLPTR)
|
|
|
|
, m_meta(Q_NULLPTR)
|
|
|
|
, m_randomStream(Q_NULLPTR)
|
2010-08-18 16:57:26 -04:00
|
|
|
{
|
|
|
|
m_xml.setAutoFormatting(true);
|
2010-08-25 07:52:59 -04:00
|
|
|
m_xml.setAutoFormattingIndent(-1); // 1 tab
|
2010-08-18 16:57:26 -04:00
|
|
|
m_xml.setCodec("UTF-8");
|
|
|
|
}
|
|
|
|
|
2012-09-25 16:33:36 -04:00
|
|
|
void KeePass2XmlWriter::writeDatabase(QIODevice* device, Database* db, KeePass2RandomStream* randomStream,
|
|
|
|
const QByteArray& headerHash)
|
2010-08-18 16:57:26 -04:00
|
|
|
{
|
2010-08-31 10:18:45 -04:00
|
|
|
m_db = db;
|
|
|
|
m_meta = db->metadata();
|
2011-07-06 18:15:52 -04:00
|
|
|
m_randomStream = randomStream;
|
2012-09-25 16:33:36 -04:00
|
|
|
m_headerHash = headerHash;
|
2010-08-31 10:18:45 -04:00
|
|
|
|
2012-04-21 10:45:46 -04:00
|
|
|
generateIdMap();
|
|
|
|
|
2010-08-22 10:01:09 -04:00
|
|
|
m_xml.setDevice(device);
|
|
|
|
|
2010-08-18 16:57:26 -04:00
|
|
|
m_xml.writeStartDocument("1.0", true);
|
|
|
|
|
|
|
|
m_xml.writeStartElement("KeePassFile");
|
|
|
|
|
|
|
|
writeMetadata();
|
|
|
|
writeRoot();
|
|
|
|
|
|
|
|
m_xml.writeEndElement();
|
|
|
|
|
|
|
|
m_xml.writeEndDocument();
|
2010-08-22 10:01:09 -04:00
|
|
|
}
|
2010-08-18 16:57:26 -04:00
|
|
|
|
2012-09-25 16:33:36 -04:00
|
|
|
void KeePass2XmlWriter::writeDatabase(const QString& filename, Database* db)
|
2010-08-22 10:01:09 -04:00
|
|
|
{
|
|
|
|
QFile file(filename);
|
2010-09-25 06:41:00 -04:00
|
|
|
file.open(QIODevice::WriteOnly|QIODevice::Truncate);
|
2012-09-25 16:33:36 -04:00
|
|
|
writeDatabase(&file, db);
|
2010-08-18 16:57:26 -04:00
|
|
|
}
|
|
|
|
|
2012-04-21 10:45:46 -04:00
|
|
|
void KeePass2XmlWriter::generateIdMap()
|
|
|
|
{
|
|
|
|
QList<Entry*> allEntries = m_db->rootGroup()->entriesRecursive(true);
|
|
|
|
int nextId = 0;
|
|
|
|
|
|
|
|
Q_FOREACH (Entry* entry, allEntries) {
|
|
|
|
Q_FOREACH (const QString& key, entry->attachments()->keys()) {
|
|
|
|
QByteArray data = entry->attachments()->value(key);
|
|
|
|
if (!m_idMap.contains(data)) {
|
|
|
|
m_idMap.insert(data, nextId++);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-31 08:39:35 -04:00
|
|
|
void KeePass2XmlWriter::writeMetadata()
|
2010-08-18 16:57:26 -04:00
|
|
|
{
|
|
|
|
m_xml.writeStartElement("Meta");
|
|
|
|
|
|
|
|
writeString("Generator", m_meta->generator());
|
2012-09-25 16:33:36 -04:00
|
|
|
if (!m_headerHash.isEmpty()) {
|
|
|
|
writeBinary("HeaderHash", m_headerHash);
|
|
|
|
}
|
2010-08-18 16:57:26 -04:00
|
|
|
writeString("DatabaseName", m_meta->name());
|
|
|
|
writeDateTime("DatabaseNameChanged", m_meta->nameChanged());
|
|
|
|
writeString("DatabaseDescription", m_meta->description());
|
|
|
|
writeDateTime("DatabaseDescriptionChanged", m_meta->descriptionChanged());
|
|
|
|
writeString("DefaultUserName", m_meta->defaultUserName());
|
|
|
|
writeDateTime("DefaultUserNameChanged", m_meta->defaultUserNameChanged());
|
|
|
|
writeNumber("MaintenanceHistoryDays", m_meta->maintenanceHistoryDays());
|
2012-04-21 10:45:46 -04:00
|
|
|
writeColor("Color", m_meta->color());
|
2010-09-25 06:41:00 -04:00
|
|
|
writeDateTime("MasterKeyChanged", m_meta->masterKeyChanged());
|
|
|
|
writeNumber("MasterKeyChangeRec", m_meta->masterKeyChangeRec());
|
|
|
|
writeNumber("MasterKeyChangeForce", m_meta->masterKeyChangeForce());
|
2010-08-18 16:57:26 -04:00
|
|
|
writeMemoryProtection();
|
|
|
|
writeCustomIcons();
|
|
|
|
writeBool("RecycleBinEnabled", m_meta->recycleBinEnabled());
|
|
|
|
writeUuid("RecycleBinUUID", m_meta->recycleBin());
|
|
|
|
writeDateTime("RecycleBinChanged", m_meta->recycleBinChanged());
|
|
|
|
writeUuid("EntryTemplatesGroup", m_meta->entryTemplatesGroup());
|
|
|
|
writeDateTime("EntryTemplatesGroupChanged", m_meta->entryTemplatesGroupChanged());
|
|
|
|
writeUuid("LastSelectedGroup", m_meta->lastSelectedGroup());
|
|
|
|
writeUuid("LastTopVisibleGroup", m_meta->lastTopVisibleGroup());
|
2012-04-21 10:45:46 -04:00
|
|
|
writeNumber("HistoryMaxItems", m_meta->historyMaxItems());
|
|
|
|
writeNumber("HistoryMaxSize", m_meta->historyMaxSize());
|
|
|
|
writeBinaries();
|
2010-08-18 16:57:26 -04:00
|
|
|
writeCustomData();
|
|
|
|
|
|
|
|
m_xml.writeEndElement();
|
|
|
|
}
|
|
|
|
|
2010-08-31 08:39:35 -04:00
|
|
|
void KeePass2XmlWriter::writeMemoryProtection()
|
2010-08-18 16:57:26 -04:00
|
|
|
{
|
|
|
|
m_xml.writeStartElement("MemoryProtection");
|
|
|
|
|
|
|
|
writeBool("ProtectTitle", m_meta->protectTitle());
|
|
|
|
writeBool("ProtectUserName", m_meta->protectUsername());
|
|
|
|
writeBool("ProtectPassword", m_meta->protectPassword());
|
|
|
|
writeBool("ProtectURL", m_meta->protectUrl());
|
|
|
|
writeBool("ProtectNotes", m_meta->protectNotes());
|
2012-04-21 10:45:46 -04:00
|
|
|
// writeBool("AutoEnableVisualHiding", m_meta->autoEnableVisualHiding());
|
2010-08-18 16:57:26 -04:00
|
|
|
|
|
|
|
m_xml.writeEndElement();
|
|
|
|
}
|
|
|
|
|
2010-08-31 08:39:35 -04:00
|
|
|
void KeePass2XmlWriter::writeCustomIcons()
|
2010-08-18 16:57:26 -04:00
|
|
|
{
|
|
|
|
m_xml.writeStartElement("CustomIcons");
|
|
|
|
|
2012-05-14 11:56:43 -04:00
|
|
|
Q_FOREACH (const Uuid& uuid, m_meta->customIconsOrder()) {
|
2012-05-13 14:50:41 -04:00
|
|
|
writeIcon(uuid, m_meta->customIcon(uuid));
|
2010-08-18 16:57:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
m_xml.writeEndElement();
|
|
|
|
}
|
|
|
|
|
2012-01-01 15:52:54 -05:00
|
|
|
void KeePass2XmlWriter::writeIcon(const Uuid& uuid, const QImage& icon)
|
2010-08-18 16:57:26 -04:00
|
|
|
{
|
|
|
|
m_xml.writeStartElement("Icon");
|
|
|
|
|
|
|
|
writeUuid("UUID", uuid);
|
|
|
|
|
|
|
|
QByteArray ba;
|
|
|
|
QBuffer buffer(&ba);
|
|
|
|
buffer.open(QIODevice::WriteOnly);
|
2012-05-11 06:39:06 -04:00
|
|
|
// TODO: check !icon.save()
|
2012-01-01 15:52:54 -05:00
|
|
|
icon.save(&buffer, "PNG");
|
2010-08-18 16:57:26 -04:00
|
|
|
buffer.close();
|
|
|
|
writeBinary("Data", ba);
|
|
|
|
|
|
|
|
m_xml.writeEndElement();
|
|
|
|
}
|
|
|
|
|
2012-04-21 10:45:46 -04:00
|
|
|
void KeePass2XmlWriter::writeBinaries()
|
|
|
|
{
|
|
|
|
m_xml.writeStartElement("Binaries");
|
|
|
|
|
|
|
|
QHash<QByteArray, int>::const_iterator i;
|
|
|
|
for (i = m_idMap.constBegin(); i != m_idMap.constEnd(); ++i) {
|
|
|
|
m_xml.writeStartElement("Binary");
|
|
|
|
|
|
|
|
m_xml.writeAttribute("ID", QString::number(i.value()));
|
|
|
|
|
2012-06-23 09:44:27 -04:00
|
|
|
QByteArray data;
|
2012-04-21 10:45:46 -04:00
|
|
|
if (m_db->compressionAlgo() == Database::CompressionGZip) {
|
|
|
|
m_xml.writeAttribute("Compressed", "True");
|
|
|
|
|
|
|
|
QBuffer buffer;
|
|
|
|
buffer.open(QIODevice::ReadWrite);
|
|
|
|
|
|
|
|
QtIOCompressor compressor(&buffer);
|
|
|
|
compressor.setStreamFormat(QtIOCompressor::GzipFormat);
|
|
|
|
compressor.open(QIODevice::WriteOnly);
|
|
|
|
|
|
|
|
qint64 bytesWritten = compressor.write(i.key());
|
|
|
|
Q_ASSERT(bytesWritten == i.key().size());
|
2012-07-03 06:39:03 -04:00
|
|
|
Q_UNUSED(bytesWritten);
|
2012-04-21 10:45:46 -04:00
|
|
|
compressor.close();
|
|
|
|
|
|
|
|
buffer.seek(0);
|
2012-06-23 09:44:27 -04:00
|
|
|
data = buffer.readAll();
|
2012-04-21 10:45:46 -04:00
|
|
|
}
|
|
|
|
else {
|
2012-06-23 09:44:27 -04:00
|
|
|
data = i.key();
|
2012-04-21 10:45:46 -04:00
|
|
|
}
|
|
|
|
|
2012-06-23 09:44:27 -04:00
|
|
|
if (!data.isEmpty()) {
|
|
|
|
m_xml.writeCharacters(QString::fromAscii(data.toBase64()));
|
|
|
|
}
|
2012-04-21 10:45:46 -04:00
|
|
|
m_xml.writeEndElement();
|
|
|
|
}
|
|
|
|
|
|
|
|
m_xml.writeEndElement();
|
|
|
|
}
|
|
|
|
|
2010-08-31 08:39:35 -04:00
|
|
|
void KeePass2XmlWriter::writeCustomData()
|
2010-08-18 16:57:26 -04:00
|
|
|
{
|
|
|
|
m_xml.writeStartElement("CustomData");
|
|
|
|
|
2010-08-25 18:31:07 -04:00
|
|
|
QHash<QString, QString> customFields = m_meta->customFields();
|
|
|
|
Q_FOREACH (const QString& key, customFields.keys()) {
|
|
|
|
writeCustomDataItem(key, customFields.value(key));
|
|
|
|
}
|
|
|
|
|
|
|
|
m_xml.writeEndElement();
|
|
|
|
}
|
|
|
|
|
2010-08-31 08:39:35 -04:00
|
|
|
void KeePass2XmlWriter::writeCustomDataItem(const QString& key, const QString& value)
|
2010-08-25 18:31:07 -04:00
|
|
|
{
|
|
|
|
m_xml.writeStartElement("Item");
|
|
|
|
|
|
|
|
writeString("Key", key);
|
|
|
|
writeString("Value", value);
|
2010-08-18 16:57:26 -04:00
|
|
|
|
|
|
|
m_xml.writeEndElement();
|
|
|
|
}
|
|
|
|
|
2010-08-31 08:39:35 -04:00
|
|
|
void KeePass2XmlWriter::writeRoot()
|
2010-08-18 16:57:26 -04:00
|
|
|
{
|
|
|
|
Q_ASSERT(m_db->rootGroup());
|
|
|
|
|
|
|
|
m_xml.writeStartElement("Root");
|
|
|
|
|
|
|
|
writeGroup(m_db->rootGroup());
|
2010-08-25 17:45:16 -04:00
|
|
|
writeDeletedObjects();
|
2010-08-18 16:57:26 -04:00
|
|
|
|
|
|
|
m_xml.writeEndElement();
|
|
|
|
}
|
|
|
|
|
2010-08-31 08:39:35 -04:00
|
|
|
void KeePass2XmlWriter::writeGroup(const Group* group)
|
2010-08-18 16:57:26 -04:00
|
|
|
{
|
2011-12-27 09:47:06 -05:00
|
|
|
Q_ASSERT(!group->uuid().isNull());
|
|
|
|
|
2010-08-18 16:57:26 -04:00
|
|
|
m_xml.writeStartElement("Group");
|
|
|
|
|
|
|
|
writeUuid("UUID", group->uuid());
|
|
|
|
writeString("Name", group->name());
|
2012-04-11 11:51:42 -04:00
|
|
|
writeString("Notes", group->notes());
|
2010-08-18 16:57:26 -04:00
|
|
|
writeNumber("IconID", group->iconNumber());
|
2010-08-22 10:01:09 -04:00
|
|
|
|
|
|
|
if (!group->iconUuid().isNull()) {
|
|
|
|
writeUuid("CustomIconUUID", group->iconUuid());
|
|
|
|
}
|
2010-08-18 16:57:26 -04:00
|
|
|
writeTimes(group->timeInfo());
|
|
|
|
writeBool("IsExpanded", group->isExpanded());
|
|
|
|
writeString("DefaultAutoTypeSequence", group->defaultAutoTypeSequence());
|
2010-08-19 08:03:54 -04:00
|
|
|
|
2011-07-07 06:45:14 -04:00
|
|
|
writeTriState("EnableAutoType", group->autoTypeEnabled());
|
2010-08-19 08:03:54 -04:00
|
|
|
|
2012-05-13 07:33:55 -04:00
|
|
|
writeTriState("EnableSearching", group->searchingEnabled());
|
2010-08-19 08:03:54 -04:00
|
|
|
|
2010-08-18 16:57:26 -04:00
|
|
|
writeUuid("LastTopVisibleEntry", group->lastTopVisibleEntry());
|
|
|
|
|
|
|
|
Q_FOREACH (const Entry* entry, group->entries()) {
|
|
|
|
writeEntry(entry);
|
|
|
|
}
|
|
|
|
|
2010-08-22 10:01:09 -04:00
|
|
|
Q_FOREACH (const Group* child, group->children()) {
|
|
|
|
writeGroup(child);
|
|
|
|
}
|
|
|
|
|
2010-08-18 16:57:26 -04:00
|
|
|
m_xml.writeEndElement();
|
|
|
|
}
|
|
|
|
|
2010-08-31 08:39:35 -04:00
|
|
|
void KeePass2XmlWriter::writeTimes(const TimeInfo& ti)
|
2010-08-18 16:57:26 -04:00
|
|
|
{
|
|
|
|
m_xml.writeStartElement("Times");
|
|
|
|
|
|
|
|
writeDateTime("LastModificationTime", ti.lastModificationTime());
|
|
|
|
writeDateTime("CreationTime", ti.creationTime());
|
|
|
|
writeDateTime("LastAccessTime", ti.lastAccessTime());
|
|
|
|
writeDateTime("ExpiryTime", ti.expiryTime());
|
|
|
|
writeBool("Expires", ti.expires());
|
|
|
|
writeNumber("UsageCount", ti.usageCount());
|
|
|
|
writeDateTime("LocationChanged", ti.locationChanged());
|
|
|
|
|
|
|
|
m_xml.writeEndElement();
|
|
|
|
}
|
|
|
|
|
2010-08-31 08:39:35 -04:00
|
|
|
void KeePass2XmlWriter::writeDeletedObjects()
|
2010-08-25 07:52:59 -04:00
|
|
|
{
|
|
|
|
m_xml.writeStartElement("DeletedObjects");
|
|
|
|
|
|
|
|
Q_FOREACH (const DeletedObject& delObj, m_db->deletedObjects()) {
|
|
|
|
writeDeletedObject(delObj);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_xml.writeEndElement();
|
|
|
|
}
|
|
|
|
|
2010-08-31 08:39:35 -04:00
|
|
|
void KeePass2XmlWriter::writeDeletedObject(const DeletedObject& delObj)
|
2010-08-25 07:52:59 -04:00
|
|
|
{
|
|
|
|
m_xml.writeStartElement("DeletedObject");
|
|
|
|
|
|
|
|
writeUuid("UUID", delObj.uuid);
|
|
|
|
writeDateTime("DeletionTime", delObj.deletionTime);
|
|
|
|
|
|
|
|
m_xml.writeEndElement();
|
|
|
|
}
|
|
|
|
|
2010-08-31 08:39:35 -04:00
|
|
|
void KeePass2XmlWriter::writeEntry(const Entry* entry)
|
2010-08-18 16:57:26 -04:00
|
|
|
{
|
2011-12-27 09:47:06 -05:00
|
|
|
Q_ASSERT(!entry->uuid().isNull());
|
|
|
|
|
2010-08-18 16:57:26 -04:00
|
|
|
m_xml.writeStartElement("Entry");
|
|
|
|
|
|
|
|
writeUuid("UUID", entry->uuid());
|
|
|
|
writeNumber("IconID", entry->iconNumber());
|
2010-08-22 10:01:09 -04:00
|
|
|
if (!entry->iconUuid().isNull()) {
|
|
|
|
writeUuid("CustomIconUUID", entry->iconUuid());
|
|
|
|
}
|
2010-08-18 16:57:26 -04:00
|
|
|
writeColor("ForegroundColor", entry->foregroundColor());
|
|
|
|
writeColor("BackgroundColor", entry->backgroundColor());
|
|
|
|
writeString("OverrideURL", entry->overrideUrl());
|
2010-09-25 06:41:00 -04:00
|
|
|
writeString("Tags", entry->tags());
|
2010-08-18 16:57:26 -04:00
|
|
|
writeTimes(entry->timeInfo());
|
|
|
|
|
2012-04-14 09:38:20 -04:00
|
|
|
Q_FOREACH (const QString& key, entry->attributes()->keys()) {
|
2010-08-18 16:57:26 -04:00
|
|
|
m_xml.writeStartElement("String");
|
2011-01-13 16:31:17 -05:00
|
|
|
|
|
|
|
bool protect = ( ((key == "Title") && m_meta->protectTitle()) ||
|
|
|
|
((key == "UserName") && m_meta->protectUsername()) ||
|
|
|
|
((key == "Password") && m_meta->protectPassword()) ||
|
|
|
|
((key == "URL") && m_meta->protectUrl()) ||
|
|
|
|
((key == "Notes") && m_meta->protectNotes()) ||
|
2012-04-21 10:45:46 -04:00
|
|
|
entry->attributes()->isProtected(key) );
|
2011-01-13 16:31:17 -05:00
|
|
|
|
2011-07-06 18:15:52 -04:00
|
|
|
writeString("Key", key);
|
|
|
|
|
|
|
|
m_xml.writeStartElement("Value");
|
|
|
|
QString value;
|
2011-01-13 16:31:17 -05:00
|
|
|
|
|
|
|
if (protect) {
|
2012-04-21 10:45:46 -04:00
|
|
|
if (m_randomStream) {
|
|
|
|
m_xml.writeAttribute("Protected", "True");
|
|
|
|
QByteArray rawData = m_randomStream->process(entry->attributes()->value(key).toUtf8());
|
|
|
|
value = QString::fromAscii(rawData.toBase64());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
m_xml.writeAttribute("ProtectInMemory", "True");
|
|
|
|
value = entry->attributes()->value(key);
|
|
|
|
}
|
2011-01-13 16:31:17 -05:00
|
|
|
}
|
|
|
|
else {
|
2012-04-14 09:38:20 -04:00
|
|
|
value = entry->attributes()->value(key);
|
2011-01-13 16:31:17 -05:00
|
|
|
}
|
|
|
|
|
2012-06-23 09:44:27 -04:00
|
|
|
if (!value.isEmpty()) {
|
|
|
|
m_xml.writeCharacters(value);
|
|
|
|
}
|
2011-07-06 18:15:52 -04:00
|
|
|
m_xml.writeEndElement();
|
|
|
|
|
2010-08-18 16:57:26 -04:00
|
|
|
m_xml.writeEndElement();
|
|
|
|
}
|
|
|
|
|
2012-04-14 09:38:20 -04:00
|
|
|
Q_FOREACH (const QString& key, entry->attachments()->keys()) {
|
2010-08-18 16:57:26 -04:00
|
|
|
m_xml.writeStartElement("Binary");
|
2011-01-13 16:31:17 -05:00
|
|
|
|
2011-07-06 18:15:52 -04:00
|
|
|
writeString("Key", key);
|
|
|
|
|
|
|
|
m_xml.writeStartElement("Value");
|
2012-04-21 10:45:46 -04:00
|
|
|
m_xml.writeAttribute("Ref", QString::number(m_idMap[entry->attachments()->value(key)]));
|
2011-07-06 18:15:52 -04:00
|
|
|
m_xml.writeEndElement();
|
|
|
|
|
2010-08-18 16:57:26 -04:00
|
|
|
m_xml.writeEndElement();
|
|
|
|
}
|
|
|
|
|
|
|
|
writeAutoType(entry);
|
2012-04-22 16:33:14 -04:00
|
|
|
// write history only for entries that are not history items
|
|
|
|
if (entry->parent()) {
|
|
|
|
writeEntryHistory(entry);
|
|
|
|
}
|
2010-08-18 16:57:26 -04:00
|
|
|
|
|
|
|
m_xml.writeEndElement();
|
|
|
|
}
|
|
|
|
|
2010-08-31 08:39:35 -04:00
|
|
|
void KeePass2XmlWriter::writeAutoType(const Entry* entry)
|
2010-08-18 16:57:26 -04:00
|
|
|
{
|
|
|
|
m_xml.writeStartElement("AutoType");
|
|
|
|
|
|
|
|
writeBool("Enabled", entry->autoTypeEnabled());
|
|
|
|
writeNumber("DataTransferObfuscation", entry->autoTypeObfuscation());
|
|
|
|
writeString("DefaultSequence", entry->defaultAutoTypeSequence());
|
|
|
|
|
2012-07-16 03:54:04 -04:00
|
|
|
Q_FOREACH (const AutoTypeAssociations::Association& assoc, entry->autoTypeAssociations()->getAll()) {
|
2010-08-18 16:57:26 -04:00
|
|
|
writeAutoTypeAssoc(assoc);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_xml.writeEndElement();
|
|
|
|
}
|
|
|
|
|
2012-07-16 03:54:04 -04:00
|
|
|
void KeePass2XmlWriter::writeAutoTypeAssoc(const AutoTypeAssociations::Association& assoc)
|
2010-08-18 16:57:26 -04:00
|
|
|
{
|
|
|
|
m_xml.writeStartElement("Association");
|
|
|
|
|
|
|
|
writeString("Window", assoc.window);
|
|
|
|
writeString("KeystrokeSequence", assoc.sequence);
|
|
|
|
|
|
|
|
m_xml.writeEndElement();
|
|
|
|
}
|
|
|
|
|
2010-08-31 08:39:35 -04:00
|
|
|
void KeePass2XmlWriter::writeEntryHistory(const Entry* entry)
|
2010-08-18 16:57:26 -04:00
|
|
|
{
|
|
|
|
m_xml.writeStartElement("History");
|
|
|
|
|
2010-08-25 07:52:59 -04:00
|
|
|
const QList<Entry*>& historyItems = entry->historyItems();
|
|
|
|
Q_FOREACH (const Entry* item, historyItems) {
|
|
|
|
writeEntry(item);
|
|
|
|
}
|
2010-08-18 16:57:26 -04:00
|
|
|
|
|
|
|
m_xml.writeEndElement();
|
|
|
|
}
|
|
|
|
|
2010-08-31 08:39:35 -04:00
|
|
|
void KeePass2XmlWriter::writeString(const QString& qualifiedName, const QString& string)
|
2010-08-18 16:57:26 -04:00
|
|
|
{
|
2012-06-23 09:44:27 -04:00
|
|
|
if (string.isEmpty()) {
|
|
|
|
m_xml.writeEmptyElement(qualifiedName);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
m_xml.writeTextElement(qualifiedName, string);
|
|
|
|
}
|
2010-08-18 16:57:26 -04:00
|
|
|
}
|
|
|
|
|
2010-08-31 08:39:35 -04:00
|
|
|
void KeePass2XmlWriter::writeNumber(const QString& qualifiedName, int number)
|
2010-08-18 16:57:26 -04:00
|
|
|
{
|
|
|
|
writeString(qualifiedName, QString::number(number));
|
|
|
|
}
|
|
|
|
|
2010-08-31 08:39:35 -04:00
|
|
|
void KeePass2XmlWriter::writeBool(const QString& qualifiedName, bool b)
|
2010-08-18 16:57:26 -04:00
|
|
|
{
|
|
|
|
if (b) {
|
|
|
|
writeString(qualifiedName, "True");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
writeString(qualifiedName, "False");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-31 08:39:35 -04:00
|
|
|
void KeePass2XmlWriter::writeDateTime(const QString& qualifiedName, const QDateTime& dateTime)
|
2010-08-18 16:57:26 -04:00
|
|
|
{
|
2011-06-29 10:33:23 -04:00
|
|
|
Q_ASSERT(dateTime.isValid());
|
2012-06-24 05:25:06 -04:00
|
|
|
Q_ASSERT(dateTime.timeSpec() == Qt::UTC);
|
2011-06-29 10:33:23 -04:00
|
|
|
|
2012-06-24 05:25:06 -04:00
|
|
|
QString dateTimeStr = dateTime.toString(Qt::ISODate);
|
|
|
|
|
|
|
|
// Qt < 4.8 doesn't append a 'Z' at the end
|
|
|
|
if (!dateTimeStr.isEmpty() && dateTimeStr[dateTimeStr.size() - 1] != 'Z') {
|
|
|
|
dateTimeStr.append('Z');
|
|
|
|
}
|
|
|
|
|
|
|
|
writeString(qualifiedName, dateTimeStr);
|
2010-08-18 16:57:26 -04:00
|
|
|
}
|
|
|
|
|
2010-08-31 08:39:35 -04:00
|
|
|
void KeePass2XmlWriter::writeUuid(const QString& qualifiedName, const Uuid& uuid)
|
2010-08-18 16:57:26 -04:00
|
|
|
{
|
|
|
|
writeString(qualifiedName, uuid.toBase64());
|
|
|
|
}
|
|
|
|
|
2010-08-31 08:39:35 -04:00
|
|
|
void KeePass2XmlWriter::writeUuid(const QString& qualifiedName, const Group* group)
|
2010-08-18 16:57:26 -04:00
|
|
|
{
|
|
|
|
if (group) {
|
|
|
|
writeUuid(qualifiedName, group->uuid());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
writeUuid(qualifiedName, Uuid());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-31 08:39:35 -04:00
|
|
|
void KeePass2XmlWriter::writeUuid(const QString& qualifiedName, const Entry* entry)
|
2010-08-18 16:57:26 -04:00
|
|
|
{
|
|
|
|
if (entry) {
|
|
|
|
writeUuid(qualifiedName, entry->uuid());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
writeUuid(qualifiedName, Uuid());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-31 08:39:35 -04:00
|
|
|
void KeePass2XmlWriter::writeBinary(const QString& qualifiedName, const QByteArray& ba)
|
2010-08-18 16:57:26 -04:00
|
|
|
{
|
|
|
|
writeString(qualifiedName, QString::fromAscii(ba.toBase64()));
|
|
|
|
}
|
|
|
|
|
2010-08-31 08:39:35 -04:00
|
|
|
void KeePass2XmlWriter::writeColor(const QString& qualifiedName, const QColor& color)
|
2010-08-18 16:57:26 -04:00
|
|
|
{
|
2011-06-29 10:33:23 -04:00
|
|
|
QString colorStr;
|
|
|
|
|
|
|
|
if (color.isValid()) {
|
|
|
|
colorStr = QString("#%1%2%3").arg(colorPartToString(color.red()))
|
|
|
|
.arg(colorPartToString(color.green()))
|
|
|
|
.arg(colorPartToString(color.blue()));
|
|
|
|
}
|
2010-08-18 16:57:26 -04:00
|
|
|
|
|
|
|
writeString(qualifiedName, colorStr);
|
|
|
|
}
|
|
|
|
|
2011-07-07 06:45:14 -04:00
|
|
|
void KeePass2XmlWriter::writeTriState(const QString& qualifiedName, Group::TriState triState)
|
|
|
|
{
|
|
|
|
QString value;
|
|
|
|
|
|
|
|
if (triState == Group::Inherit) {
|
|
|
|
value = "null";
|
|
|
|
}
|
|
|
|
else if (triState == Group::Enable) {
|
|
|
|
value = "true";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
value = "false";
|
|
|
|
}
|
|
|
|
|
|
|
|
writeString(qualifiedName, value);
|
|
|
|
}
|
|
|
|
|
2010-08-31 08:39:35 -04:00
|
|
|
QString KeePass2XmlWriter::colorPartToString(int value)
|
2010-08-18 16:57:26 -04:00
|
|
|
{
|
|
|
|
QString str = QString::number(value, 16).toUpper();
|
|
|
|
if (str.length() == 1) {
|
|
|
|
str.prepend("0");
|
|
|
|
}
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|