Merge branch '2.0'

This commit is contained in:
Felix Geyer 2016-01-29 17:22:37 +01:00
commit 71d4cb781d
30 changed files with 569 additions and 40 deletions

View File

@ -73,6 +73,7 @@ set(keepassx_SOURCES
format/KeePass2.h
format/KeePass2RandomStream.cpp
format/KeePass2Reader.cpp
format/KeePass2Repair.cpp
format/KeePass2Writer.cpp
format/KeePass2XmlReader.cpp
format/KeePass2XmlWriter.cpp
@ -81,6 +82,7 @@ set(keepassx_SOURCES
gui/ChangeMasterKeyWidget.cpp
gui/Clipboard.cpp
gui/DatabaseOpenWidget.cpp
gui/DatabaseRepairWidget.cpp
gui/DatabaseSettingsWidget.cpp
gui/DatabaseTabWidget.cpp
gui/DatabaseWidget.cpp

View File

@ -113,13 +113,25 @@ QPixmap Entry::iconPixmap() const
else {
Q_ASSERT(database());
QPixmap pixmap;
if (database() && !QPixmapCache::find(m_pixmapCacheKey, &pixmap)) {
pixmap = QPixmap::fromImage(database()->metadata()->customIcon(m_data.customIcon));
m_pixmapCacheKey = QPixmapCache::insert(pixmap);
if (database()) {
return database()->metadata()->customIconPixmap(m_data.customIcon);
}
else {
return QPixmap();
}
}
}
return pixmap;
QPixmap Entry::iconScaledPixmap() const
{
if (m_data.customIcon.isNull()) {
// built-in icons are 16x16 so don't need to be scaled
return databaseIcons()->iconPixmap(m_data.iconNumber);
}
else {
Q_ASSERT(database());
return database()->metadata()->customIconScaledPixmap(m_data.customIcon);
}
}
@ -247,8 +259,6 @@ void Entry::setIcon(int iconNumber)
m_data.iconNumber = iconNumber;
m_data.customIcon = Uuid();
m_pixmapCacheKey = QPixmapCache::Key();
Q_EMIT modified();
emitDataChanged();
}
@ -262,8 +272,6 @@ void Entry::setIcon(const Uuid& uuid)
m_data.customIcon = uuid;
m_data.iconNumber = 0;
m_pixmapCacheKey = QPixmapCache::Key();
Q_EMIT modified();
emitDataChanged();
}

View File

@ -22,7 +22,6 @@
#include <QImage>
#include <QMap>
#include <QPixmap>
#include <QPixmapCache>
#include <QPointer>
#include <QSet>
#include <QUrl>
@ -60,6 +59,7 @@ public:
Uuid uuid() const;
QImage icon() const;
QPixmap iconPixmap() const;
QPixmap iconScaledPixmap() const;
int iconNumber() const;
Uuid iconUuid() const;
QColor foregroundColor() const;
@ -168,7 +168,6 @@ private:
Entry* m_tmpHistoryItem;
bool m_modifiedSinceBegin;
QPointer<Group> m_group;
mutable QPixmapCache::Key m_pixmapCacheKey;
bool m_updateTimeinfo;
};

View File

@ -133,13 +133,30 @@ QPixmap Group::iconPixmap() const
else {
Q_ASSERT(m_db);
QPixmap pixmap;
if (m_db && !QPixmapCache::find(m_pixmapCacheKey, &pixmap)) {
pixmap = QPixmap::fromImage(m_db->metadata()->customIcon(m_data.customIcon));
m_pixmapCacheKey = QPixmapCache::insert(pixmap);
if (m_db) {
return m_db->metadata()->customIconPixmap(m_data.customIcon);
}
else {
return QPixmap();
}
}
}
return pixmap;
QPixmap Group::iconScaledPixmap() const
{
if (m_data.customIcon.isNull()) {
// built-in icons are 16x16 so don't need to be scaled
return databaseIcons()->iconPixmap(m_data.iconNumber);
}
else {
Q_ASSERT(m_db);
if (m_db) {
return m_db->metadata()->customIconScaledPixmap(m_data.customIcon);
}
else {
return QPixmap();
}
}
}
@ -213,8 +230,6 @@ void Group::setIcon(int iconNumber)
m_data.iconNumber = iconNumber;
m_data.customIcon = Uuid();
m_pixmapCacheKey = QPixmapCache::Key();
updateTimeinfo();
Q_EMIT modified();
Q_EMIT dataChanged(this);
@ -229,8 +244,6 @@ void Group::setIcon(const Uuid& uuid)
m_data.customIcon = uuid;
m_data.iconNumber = 0;
m_pixmapCacheKey = QPixmapCache::Key();
updateTimeinfo();
Q_EMIT modified();
Q_EMIT dataChanged(this);

View File

@ -58,6 +58,7 @@ public:
QString notes() const;
QImage icon() const;
QPixmap iconPixmap() const;
QPixmap iconScaledPixmap() const;
int iconNumber() const;
Uuid iconUuid() const;
TimeInfo timeInfo() const;
@ -155,7 +156,6 @@ private:
QList<Entry*> m_entries;
QPointer<Group> m_parent;
mutable QPixmapCache::Key m_pixmapCacheKey;
bool m_updateTimeinfo;

View File

@ -167,6 +167,43 @@ QImage Metadata::customIcon(const Uuid& uuid) const
return m_customIcons.value(uuid);
}
QPixmap Metadata::customIconPixmap(const Uuid& uuid) const
{
QPixmap pixmap;
if (!m_customIcons.contains(uuid)) {
return pixmap;
}
QPixmapCache::Key& cacheKey = m_customIconCacheKeys[uuid];
if (!QPixmapCache::find(cacheKey, &pixmap)) {
pixmap = QPixmap::fromImage(m_customIcons.value(uuid));
cacheKey = QPixmapCache::insert(pixmap);
}
return pixmap;
}
QPixmap Metadata::customIconScaledPixmap(const Uuid& uuid) const
{
QPixmap pixmap;
if (!m_customIcons.contains(uuid)) {
return pixmap;
}
QPixmapCache::Key& cacheKey = m_customIconScaledCacheKeys[uuid];
if (!QPixmapCache::find(cacheKey, &pixmap)) {
QImage image = m_customIcons.value(uuid).scaled(16, 16, Qt::KeepAspectRatio, Qt::SmoothTransformation);
pixmap = QPixmap::fromImage(image);
cacheKey = QPixmapCache::insert(pixmap);
}
return pixmap;
}
bool Metadata::containsCustomIcon(const Uuid& uuid) const
{
return m_customIcons.contains(uuid);
@ -177,6 +214,17 @@ QHash<Uuid, QImage> Metadata::customIcons() const
return m_customIcons;
}
QHash<Uuid, QPixmap> Metadata::customIconsScaledPixmaps() const
{
QHash<Uuid, QPixmap> result;
Q_FOREACH (const Uuid& uuid, m_customIconsOrder) {
result.insert(uuid, customIconScaledPixmap(uuid));
}
return result;
}
QList<Uuid> Metadata::customIconsOrder() const
{
return m_customIconsOrder;
@ -338,6 +386,9 @@ void Metadata::addCustomIcon(const Uuid& uuid, const QImage& icon)
Q_ASSERT(!m_customIcons.contains(uuid));
m_customIcons.insert(uuid, icon);
// reset cache in case there is also an icon with that uuid
m_customIconCacheKeys[uuid] = QPixmapCache::Key();
m_customIconScaledCacheKeys[uuid] = QPixmapCache::Key();
m_customIconsOrder.append(uuid);
Q_ASSERT(m_customIcons.count() == m_customIconsOrder.count());
Q_EMIT modified();
@ -365,6 +416,10 @@ void Metadata::removeCustomIcon(const Uuid& uuid)
Q_ASSERT(m_customIcons.contains(uuid));
m_customIcons.remove(uuid);
QPixmapCache::remove(m_customIconCacheKeys.value(uuid));
m_customIconCacheKeys.remove(uuid);
QPixmapCache::remove(m_customIconScaledCacheKeys.value(uuid));
m_customIconScaledCacheKeys.remove(uuid);
m_customIconsOrder.removeAll(uuid);
Q_ASSERT(m_customIcons.count() == m_customIconsOrder.count());
Q_EMIT modified();

View File

@ -22,6 +22,8 @@
#include <QDateTime>
#include <QHash>
#include <QImage>
#include <QPixmap>
#include <QPixmapCache>
#include <QPointer>
#include "core/Uuid.h"
@ -77,10 +79,13 @@ public:
bool protectNotes() const;
// bool autoEnableVisualHiding() const;
QImage customIcon(const Uuid& uuid) const;
QPixmap customIconPixmap(const Uuid& uuid) const;
QPixmap customIconScaledPixmap(const Uuid& uuid) const;
bool containsCustomIcon(const Uuid& uuid) const;
QHash<Uuid, QImage> customIcons() const;
QList<Uuid> customIconsOrder() const;
bool recycleBinEnabled() const;
QHash<Uuid, QPixmap> customIconsScaledPixmaps() const;
Group* recycleBin();
const Group* recycleBin() const;
QDateTime recycleBinChanged() const;
@ -152,6 +157,8 @@ private:
MetadataData m_data;
QHash<Uuid, QImage> m_customIcons;
mutable QHash<Uuid, QPixmapCache::Key> m_customIconCacheKeys;
mutable QHash<Uuid, QPixmapCache::Key> m_customIconScaledCacheKeys;
QList<Uuid> m_customIconsOrder;
QPointer<Group> m_recycleBin;

View File

@ -43,7 +43,7 @@ KeePass2Reader::KeePass2Reader()
{
}
Database* KeePass2Reader::readDatabase(QIODevice* device, const CompositeKey& key)
Database* KeePass2Reader::readDatabase(QIODevice* device, const CompositeKey& key, bool keepDatabase)
{
QScopedPointer<Database> db(new Database());
m_db = db.data();
@ -178,7 +178,12 @@ Database* KeePass2Reader::readDatabase(QIODevice* device, const CompositeKey& ke
if (xmlReader.hasError()) {
raiseError(xmlReader.errorString());
return nullptr;
if (keepDatabase) {
return db.take();
}
else {
return nullptr;
}
}
Q_ASSERT(version < 0x00030001 || !xmlReader.headerHash().isEmpty());
@ -232,6 +237,11 @@ QByteArray KeePass2Reader::xmlData()
return m_xmlData;
}
QByteArray KeePass2Reader::streamKey()
{
return m_protectedStreamKey;
}
void KeePass2Reader::raiseError(const QString& errorMessage)
{
m_error = true;

View File

@ -31,12 +31,13 @@ class KeePass2Reader
public:
KeePass2Reader();
Database* readDatabase(QIODevice* device, const CompositeKey& key);
Database* readDatabase(QIODevice* device, const CompositeKey& key, bool keepDatabase = false);
Database* readDatabase(const QString& filename, const CompositeKey& key);
bool hasError();
QString errorString();
void setSaveXml(bool save);
QByteArray xmlData();
QByteArray streamKey();
private:
void raiseError(const QString& errorMessage);

View File

@ -0,0 +1,107 @@
/*
* Copyright (C) 2016 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 "KeePass2Repair.h"
#include <QBuffer>
#include <QRegExp>
#include "format/KeePass2RandomStream.h"
#include "format/KeePass2Reader.h"
#include "format/KeePass2XmlReader.h"
KeePass2Repair::KeePass2Repair()
: m_db(Q_NULLPTR)
{
}
KeePass2Repair::RepairResult KeePass2Repair::repairDatabase(QIODevice* device, const CompositeKey& key)
{
m_db = Q_NULLPTR;
m_errorStr.clear();
KeePass2Reader reader;
reader.setSaveXml(true);
Database* db = reader.readDatabase(device, key, true);
if (!reader.hasError()) {
delete db;
return NothingTodo;
}
QByteArray xmlData = reader.xmlData();
if (!db || xmlData.isEmpty()) {
delete db;
m_errorStr = reader.errorString();
return UnableToOpen;
}
bool repairAction = false;
QString xmlStart = QString::fromLatin1(xmlData.constData(), qMin(100, xmlData.size()));
QRegExp encodingRegExp("encoding=\"([^\"]+)\"", Qt::CaseInsensitive, QRegExp::RegExp2);
if (encodingRegExp.indexIn(xmlStart) != -1) {
if (encodingRegExp.cap(1).compare("utf-8", Qt::CaseInsensitive) != 0
&& encodingRegExp.cap(1).compare("utf8", Qt::CaseInsensitive) != 0)
{
// database is not utf-8 encoded, we don't support repairing that
delete db;
return RepairFailed;
}
}
// try to fix broken databases because of bug #392
for (int i = (xmlData.size() - 1); i >= 0; i--) {
char ch = xmlData.at(i);
if (ch < 0x20 && ch != 0x09 && ch != 0x0A && ch != 0x0D) {
xmlData.remove(i, 1);
repairAction = true;
}
}
if (!repairAction) {
// we were unable to find the problem
delete db;
return RepairFailed;
}
KeePass2RandomStream randomStream;
randomStream.init(reader.streamKey());
KeePass2XmlReader xmlReader;
QBuffer buffer(&xmlData);
buffer.open(QIODevice::ReadOnly);
xmlReader.readDatabase(&buffer, db, &randomStream);
if (xmlReader.hasError()) {
delete db;
return RepairFailed;
}
else {
m_db = db;
return RepairSuccess;
}
}
Database* KeePass2Repair::database() const
{
return m_db;
}
QString KeePass2Repair::errorString() const
{
return m_errorStr;
}

View File

@ -0,0 +1,50 @@
/*
* Copyright (C) 2016 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_KEEPASS2REPAIR_H
#define KEEPASSX_KEEPASS2REPAIR_H
#include <QCoreApplication>
#include <QIODevice>
#include "core/Database.h"
#include "keys/CompositeKey.h"
class KeePass2Repair
{
Q_DECLARE_TR_FUNCTIONS(KeePass2Repair)
public:
enum RepairResult
{
NothingTodo,
UnableToOpen,
RepairSuccess,
RepairFailed
};
KeePass2Repair();
RepairResult repairDatabase(QIODevice* device, const CompositeKey& key);
Database* database() const;
QString errorString() const;
private:
Database* m_db;
QString m_errorStr;
};
#endif // KEEPASSX_KEEPASS2REPAIR_H

View File

@ -372,7 +372,7 @@ void KeePass2XmlWriter::writeEntry(const Entry* entry)
}
if (!value.isEmpty()) {
m_xml.writeCharacters(value);
m_xml.writeCharacters(stripInvalidXml10Chars(value));
}
m_xml.writeEndElement();
@ -443,7 +443,7 @@ void KeePass2XmlWriter::writeString(const QString& qualifiedName, const QString&
m_xml.writeEmptyElement(qualifiedName);
}
else {
m_xml.writeTextElement(qualifiedName, string);
m_xml.writeTextElement(qualifiedName, stripInvalidXml10Chars(string));
}
}
@ -547,6 +547,23 @@ QString KeePass2XmlWriter::colorPartToString(int value)
return str;
}
QString KeePass2XmlWriter::stripInvalidXml10Chars(QString str)
{
for (int i = str.size() - 1; i >= 0; i--) {
const ushort uc = str.at(i).unicode();
if ((uc < 0x20 && uc != 0x09 && uc != 0x0A && uc != 0x0D)
|| (uc > 0xD7FF && uc < 0xE000)
|| (uc > 0xFFFD))
{
qWarning("Stripping invalid XML 1.0 codepoint %x", uc);
str.remove(i, 1);
}
}
return str;
}
void KeePass2XmlWriter::raiseError(const QString& errorMessage)
{
m_error = true;

View File

@ -73,6 +73,7 @@ private:
void writeColor(const QString& qualifiedName, const QColor& color);
void writeTriState(const QString& qualifiedName, Group::TriState triState);
QString colorPartToString(int value);
QString stripInvalidXml10Chars(QString str);
void raiseError(const QString& errorMessage);

View File

@ -0,0 +1,103 @@
/*
* Copyright (C) 2016 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 "DatabaseRepairWidget.h"
#include <QFile>
#include <QFileInfo>
#include "ui_DatabaseOpenWidget.h"
#include "core/Database.h"
#include "core/Metadata.h"
#include "format/KeePass2Repair.h"
#include "gui/MessageBox.h"
#include "keys/FileKey.h"
#include "keys/PasswordKey.h"
DatabaseRepairWidget::DatabaseRepairWidget(QWidget* parent)
: DatabaseOpenWidget(parent)
{
m_ui->labelHeadline->setText(tr("Repair database"));
connect(this, SIGNAL(editFinished(bool)), this, SLOT(processEditFinished(bool)));
}
void DatabaseRepairWidget::openDatabase()
{
CompositeKey masterKey;
if (m_ui->checkPassword->isChecked()) {
masterKey.addKey(PasswordKey(m_ui->editPassword->text()));
}
if (m_ui->checkKeyFile->isChecked()) {
FileKey key;
QString keyFilename = m_ui->comboKeyFile->currentText();
QString errorMsg;
if (!key.load(keyFilename, &errorMsg)) {
MessageBox::warning(this, tr("Error"), tr("Can't open key file").append(":\n").append(errorMsg));
Q_EMIT editFinished(false);
}
masterKey.addKey(key);
}
KeePass2Repair repair;
QFile file(m_filename);
if (!file.open(QIODevice::ReadOnly)) {
// TODO: error message
Q_EMIT editFinished(false);
return;
}
if (m_db) {
delete m_db;
}
QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
KeePass2Repair::RepairResult repairResult = repair.repairDatabase(&file, masterKey);
QApplication::restoreOverrideCursor();
switch (repairResult) {
case KeePass2Repair::NothingTodo:
MessageBox::information(this, tr("Error"), tr("Database opened fine. Nothing to do."));
Q_EMIT editFinished(false);
return;
case KeePass2Repair::UnableToOpen:
MessageBox::warning(this, tr("Error"), tr("Unable to open the database.").append("\n")
.append(repair.errorString()));
Q_EMIT editFinished(false);
return;
case KeePass2Repair::RepairSuccess:
m_db = repair.database();
MessageBox::warning(this, tr("Success"), tr("The database has been successfully repaired\nYou can now save it."));
Q_EMIT editFinished(true);
return;
case KeePass2Repair::RepairFailed:
MessageBox::warning(this, tr("Error"), tr("Unable to repair the database."));
Q_EMIT editFinished(false);
return;
}
}
void DatabaseRepairWidget::processEditFinished(bool result)
{
if (result) {
Q_EMIT success();
}
else {
Q_EMIT error();
}
}

View File

@ -0,0 +1,41 @@
/*
* Copyright (C) 2016 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_DATABASEREPAIRWIDGET_H
#define KEEPASSX_DATABASEREPAIRWIDGET_H
#include "gui/DatabaseOpenWidget.h"
class DatabaseRepairWidget : public DatabaseOpenWidget
{
Q_OBJECT
public:
explicit DatabaseRepairWidget(QWidget* parent = Q_NULLPTR);
Q_SIGNALS:
void success();
void error();
protected:
void openDatabase() Q_DECL_OVERRIDE;
private Q_SLOTS:
void processEditFinished(bool result);
};
#endif // KEEPASSX_DATABASEREPAIRWIDGET_H

View File

@ -98,7 +98,7 @@ void EditWidgetIcons::load(Uuid currentUuid, Database* database, IconStruct icon
m_database = database;
m_currentUuid = currentUuid;
m_customIconModel->setIcons(database->metadata()->customIcons(),
m_customIconModel->setIcons(database->metadata()->customIconsScaledPixmaps(),
database->metadata()->customIconsOrder());
Uuid iconUuid = iconStruct.uuid;
@ -133,7 +133,7 @@ void EditWidgetIcons::addCustomIcon()
if (!image.isNull()) {
Uuid uuid = Uuid::random();
m_database->metadata()->addCustomIconScaled(uuid, image);
m_customIconModel->setIcons(m_database->metadata()->customIcons(),
m_customIconModel->setIcons(m_database->metadata()->customIconsScaledPixmaps(),
m_database->metadata()->customIconsOrder());
QModelIndex index = m_customIconModel->indexFromUuid(uuid);
m_ui->customIconsView->setCurrentIndex(index);
@ -183,7 +183,7 @@ void EditWidgetIcons::removeCustomIcon()
}
m_database->metadata()->removeCustomIcon(iconUuid);
m_customIconModel->setIcons(m_database->metadata()->customIcons(),
m_customIconModel->setIcons(m_database->metadata()->customIconsScaledPixmaps(),
m_database->metadata()->customIconsOrder());
if (m_customIconModel->rowCount() > 0) {
m_ui->customIconsView->setCurrentIndex(m_customIconModel->index(0, 0));

View File

@ -54,7 +54,7 @@ CustomIconModel::CustomIconModel(QObject* parent)
{
}
void CustomIconModel::setIcons(const QHash<Uuid, QImage>& icons, const QList<Uuid>& iconsOrder)
void CustomIconModel::setIcons(const QHash<Uuid, QPixmap>& icons, const QList<Uuid>& iconsOrder)
{
beginResetModel();

View File

@ -19,7 +19,7 @@
#define KEEPASSX_ICONMODELS_H
#include <QAbstractListModel>
#include <QImage>
#include <QPixmap>
#include "core/Uuid.h"
@ -43,12 +43,12 @@ public:
int rowCount(const QModelIndex& parent = QModelIndex()) const override;
QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
void setIcons(const QHash<Uuid, QImage>& icons, const QList<Uuid>& iconsOrder);
void setIcons(const QHash<Uuid, QPixmap>& icons, const QList<Uuid>& iconsOrder);
Uuid uuidFromIndex(const QModelIndex& index) const;
QModelIndex indexFromUuid(const Uuid& uuid) const;
private:
QHash<Uuid, QImage> m_icons;
QHash<Uuid, QPixmap> m_icons;
QList<Uuid> m_iconsOrder;
};

View File

@ -27,8 +27,12 @@
#include "core/FilePath.h"
#include "core/InactivityTimer.h"
#include "core/Metadata.h"
#include "format/KeePass2Writer.h"
#include "gui/AboutDialog.h"
#include "gui/DatabaseWidget.h"
#include "gui/DatabaseRepairWidget.h"
#include "gui/FileDialog.h"
#include "gui/MessageBox.h"
const QString MainWindow::BaseWindowTitle = "KeePassX";
@ -163,6 +167,8 @@ MainWindow::MainWindow()
SLOT(changeDatabaseSettings()));
connect(m_ui->actionImportKeePass1, SIGNAL(triggered()), m_ui->tabWidget,
SLOT(importKeePass1Database()));
connect(m_ui->actionRepairDatabase, SIGNAL(triggered()), this,
SLOT(repairDatabase()));
connect(m_ui->actionExportCsv, SIGNAL(triggered()), m_ui->tabWidget,
SLOT(exportToCsv()));
connect(m_ui->actionLockDatabases, SIGNAL(triggered()), m_ui->tabWidget,
@ -367,6 +373,7 @@ void MainWindow::setMenuActionState(DatabaseWidget::Mode mode)
m_ui->actionDatabaseOpen->setEnabled(inDatabaseTabWidgetOrWelcomeWidget);
m_ui->menuRecentDatabases->setEnabled(inDatabaseTabWidgetOrWelcomeWidget);
m_ui->actionImportKeePass1->setEnabled(inDatabaseTabWidgetOrWelcomeWidget);
m_ui->actionRepairDatabase->setEnabled(inDatabaseTabWidgetOrWelcomeWidget);
m_ui->actionLockDatabases->setEnabled(m_ui->tabWidget->hasLockableDatabases());
}
@ -599,6 +606,36 @@ void MainWindow::lockDatabasesAfterInactivity()
m_ui->tabWidget->lockDatabases();
}
void MainWindow::repairDatabase()
{
QString filter = QString("%1 (*.kdbx);;%2 (*)").arg(tr("KeePass 2 Database"), tr("All files"));
QString fileName = fileDialog()->getOpenFileName(this, tr("Open database"), QString(),
filter);
if (fileName.isEmpty()) {
return;
}
QScopedPointer<QDialog> dialog(new QDialog(this));
DatabaseRepairWidget* dbRepairWidget = new DatabaseRepairWidget(dialog.data());
connect(dbRepairWidget, SIGNAL(success()), dialog.data(), SLOT(accept()));
connect(dbRepairWidget, SIGNAL(error()), dialog.data(), SLOT(reject()));
dbRepairWidget->load(fileName);
if (dialog->exec() == QDialog::Accepted && dbRepairWidget->database()) {
QString saveFileName = fileDialog()->getSaveFileName(this, tr("Save repaired database"), QString(),
tr("KeePass 2 Database").append(" (*.kdbx)"),
Q_NULLPTR, 0, "kdbx");
if (!saveFileName.isEmpty()) {
KeePass2Writer writer;
writer.writeDatabase(saveFileName, dbRepairWidget->database());
if (writer.hasError()) {
MessageBox::critical(this, tr("Error"), tr("Writing the database failed.") + "\n\n"
+ writer.errorString());
}
}
}
}
bool MainWindow::isTrayIconEnabled() const
{
#ifdef Q_OS_MAC

View File

@ -66,6 +66,7 @@ private Q_SLOTS:
void trayIconTriggered(QSystemTrayIcon::ActivationReason reason);
void toggleWindow();
void lockDatabasesAfterInactivity();
void repairDatabase();
private:
static void setShortcut(QAction* action, QKeySequence::StandardKey standard, int fallback = 0);

View File

@ -121,6 +121,7 @@
<addaction name="separator"/>
<addaction name="actionImportKeePass1"/>
<addaction name="actionExportCsv"/>
<addaction name="actionRepairDatabase"/>
<addaction name="separator"/>
<addaction name="actionQuit"/>
</widget>
@ -422,6 +423,11 @@
<string>Export to CSV file</string>
</property>
</action>
<action name="actionRepairDatabase">
<property name="text">
<string>Repair database</string>
</property>
</action>
</widget>
<customwidgets>
<customwidget>

View File

@ -295,6 +295,7 @@ void EditEntryWidget::setForms(const Entry* entry, bool restore)
m_mainUi->expireDatePicker->setReadOnly(m_history);
m_mainUi->notesEdit->setReadOnly(m_history);
m_mainUi->tooglePasswordGeneratorButton->setChecked(false);
m_mainUi->tooglePasswordGeneratorButton->setDisabled(m_history);
m_mainUi->passwordGenerator->reset();
m_advancedUi->addAttachmentButton->setEnabled(!m_history);
updateAttachmentButtonsEnabled(m_advancedUi->attachmentsView->currentIndex());
@ -672,6 +673,14 @@ void EditEntryWidget::openAttachment(const QModelIndex& index)
return;
}
if (!file->flush()) {
MessageBox::warning(this, tr("Error"),
tr("Unable to save the attachment:\n").append(file->errorString()));
return;
}
file->close();
QDesktopServices::openUrl(QUrl::fromLocalFile(file->fileName()));
}

View File

@ -145,7 +145,7 @@ QVariant EntryModel::data(const QModelIndex& index, int role) const
switch (index.column()) {
case ParentGroup:
if (entry->group()) {
return entry->group()->iconPixmap();
return entry->group()->iconScaledPixmap();
}
break;
case Title:
@ -153,7 +153,7 @@ QVariant EntryModel::data(const QModelIndex& index, int role) const
return databaseIcons()->iconPixmap(DatabaseIcons::ExpiredIconIndex);
}
else {
return entry->iconPixmap();
return entry->iconScaledPixmap();
}
}
}

View File

@ -136,7 +136,7 @@ QVariant GroupModel::data(const QModelIndex& index, int role) const
return databaseIcons()->iconPixmap(DatabaseIcons::ExpiredIconIndex);
}
else {
return group->iconPixmap();
return group->iconScaledPixmap();
}
}
else if (role == Qt::FontRole) {

View File

@ -209,17 +209,16 @@ void TestEntryModel::testCustomIconModel()
QCOMPARE(model->rowCount(), 0);
QHash<Uuid, QImage> icons;
QHash<Uuid, QPixmap> icons;
QList<Uuid> iconsOrder;
Uuid iconUuid(QByteArray(16, '2'));
QImage icon;
icons.insert(iconUuid, icon);
icons.insert(iconUuid, QPixmap());
iconsOrder << iconUuid;
Uuid iconUuid2(QByteArray(16, '1'));
QImage icon2;
icons.insert(iconUuid2, icon2);
icons.insert(iconUuid2, QPixmap());
iconsOrder << iconUuid2;
model->setIcons(icons, iconsOrder);

View File

@ -20,12 +20,14 @@
#include <QBuffer>
#include <QTest>
#include "config-keepassx-tests.h"
#include "FailDevice.h"
#include "core/Database.h"
#include "core/Group.h"
#include "core/Metadata.h"
#include "crypto/Crypto.h"
#include "format/KeePass2Reader.h"
#include "format/KeePass2Repair.h"
#include "format/KeePass2Writer.h"
#include "format/KeePass2XmlWriter.h"
#include "keys/PasswordKey.h"
@ -126,6 +128,34 @@ void TestKeePass2Writer::testDeviceFailure()
delete db;
}
void TestKeePass2Writer::testRepair()
{
QString brokenDbFilename = QString(KEEPASSX_TEST_DATA_DIR).append("/bug392.kdbx");
// master password = test
// entry username: testuser\x10
// entry password: testpw
CompositeKey key;
key.addKey(PasswordKey("test"));
// test that we can't open the broken database
KeePass2Reader reader;
Database* dbBroken = reader.readDatabase(brokenDbFilename, key);
QVERIFY(!dbBroken);
QVERIFY(reader.hasError());
// test if we can repair the database
KeePass2Repair repair;
QFile file(brokenDbFilename);
file.open(QIODevice::ReadOnly);
QCOMPARE(repair.repairDatabase(&file, key), KeePass2Repair::RepairSuccess);
Database* dbRepaired = repair.database();
QVERIFY(dbRepaired);
QCOMPARE(dbRepaired->rootGroup()->entries().size(), 1);
QCOMPARE(dbRepaired->rootGroup()->entries().at(0)->username(), QString("testuser"));
QCOMPARE(dbRepaired->rootGroup()->entries().at(0)->password(), QString("testpw"));
}
void TestKeePass2Writer::cleanupTestCase()
{
delete m_dbOrg;

View File

@ -33,6 +33,7 @@ private Q_SLOTS:
void testAttachments();
void testNonAsciiPasswords();
void testDeviceFailure();
void testRepair();
void cleanupTestCase();
private:

View File

@ -17,6 +17,7 @@
#include "TestKeePass2XmlReader.h"
#include <QBuffer>
#include <QFile>
#include <QTest>
@ -25,6 +26,7 @@
#include "core/Metadata.h"
#include "crypto/Crypto.h"
#include "format/KeePass2XmlReader.h"
#include "format/KeePass2XmlWriter.h"
#include "config-keepassx-tests.h"
QTEST_GUILESS_MAIN(TestKeePass2XmlReader)
@ -407,6 +409,35 @@ void TestKeePass2XmlReader::testEmptyUuids()
QVERIFY(!reader.hasError());
}
void TestKeePass2XmlReader::testInvalidXmlChars()
{
QScopedPointer<Database> dbWrite(new Database());
Entry* entry = new Entry();
entry->setUuid(Uuid::random());
entry->setNotes(QString("a %1 b %2 c %3").arg(QChar(0x02)).arg(QChar(0xD800)).arg(QChar(0xFFFE)));
entry->setGroup(dbWrite->rootGroup());
QBuffer buffer;
buffer.open(QIODevice::ReadWrite);
KeePass2XmlWriter writer;
writer.writeDatabase(&buffer, dbWrite.data());
QVERIFY(!writer.hasError());
buffer.seek(0);
KeePass2XmlReader reader;
reader.setStrictMode(true);
QScopedPointer<Database> dbRead(reader.readDatabase(&buffer));
if (reader.hasError()) {
qWarning("Database read error: %s", qPrintable(reader.errorString()));
}
QVERIFY(!reader.hasError());
QVERIFY(!dbRead.isNull());
QCOMPARE(dbRead->rootGroup()->entries().size(), 1);
// check that the invalid codepoints have been stripped
QCOMPARE(dbRead->rootGroup()->entries().first()->notes(), QString("a b c "));
}
void TestKeePass2XmlReader::cleanupTestCase()
{
delete m_db;

View File

@ -42,6 +42,7 @@ private Q_SLOTS:
void testBroken();
void testBroken_data();
void testEmptyUuids();
void testInvalidXmlChars();
void cleanupTestCase();
private:

BIN
tests/data/bug392.kdbx Normal file

Binary file not shown.