Constification and some style fixes.

This commit is contained in:
Felix Geyer 2012-04-23 19:44:43 +02:00
parent b3ddfca2bd
commit 3df2ad35cb
28 changed files with 84 additions and 105 deletions

View File

@ -64,7 +64,7 @@ Config::Config()
userPath += "keepassx2.ini"; userPath += "keepassx2.ini";
m_settings = new QSettings(userPath, QSettings::IniFormat); m_settings.reset(new QSettings(userPath, QSettings::IniFormat));
} }
Config* config() Config* config()

View File

@ -18,7 +18,7 @@
#ifndef KEEPASSX_CONFIG_H #ifndef KEEPASSX_CONFIG_H
#define KEEPASSX_CONFIG_H #define KEEPASSX_CONFIG_H
#include <QtCore/QScopedPointer>
#include <QtCore/QVariant> #include <QtCore/QVariant>
class QSettings; class QSettings;
@ -32,7 +32,7 @@ public:
private: private:
Config(); Config();
QSettings* m_settings; QScopedPointer<QSettings> m_settings;
friend Config* config(); friend Config* config();
}; };

View File

@ -26,16 +26,15 @@
#include "format/KeePass2.h" #include "format/KeePass2.h"
Database::Database() Database::Database()
: m_metadata(new Metadata(this))
, m_cipher(KeePass2::CIPHER_AES)
, m_compressionAlgo(CompressionGZip)
, m_transformRounds(50000)
, m_hasKey(false)
{ {
m_hasKey = false;
m_metadata = new Metadata(this);
setRootGroup(new Group()); setRootGroup(new Group());
rootGroup()->setUuid(Uuid::random()); rootGroup()->setUuid(Uuid::random());
m_cipher = KeePass2::CIPHER_AES;
m_compressionAlgo = CompressionGZip;
m_transformRounds = 50000;
connect(m_metadata, SIGNAL(modified()), this, SIGNAL(modified())); connect(m_metadata, SIGNAL(modified()), this, SIGNAL(modified()));
} }

View File

@ -105,7 +105,7 @@ private:
void createRecycleBin(); void createRecycleBin();
Metadata* m_metadata; Metadata* const m_metadata;
Group* m_rootGroup; Group* m_rootGroup;
QList<DeletedObject> m_deletedObjects; QList<DeletedObject> m_deletedObjects;
@ -116,7 +116,6 @@ private:
QByteArray m_transformedMasterKey; QByteArray m_transformedMasterKey;
CompositeKey m_key; CompositeKey m_key;
bool m_hasKey; bool m_hasKey;
}; };

View File

@ -117,6 +117,7 @@ private Q_SLOTS:
private: private:
const Database* database() const; const Database* database() const;
template <class T> inline bool set(T& property, const T& value);
Uuid m_uuid; Uuid m_uuid;
int m_iconNumber; int m_iconNumber;
@ -137,8 +138,6 @@ private:
QPointer<Group> m_group; QPointer<Group> m_group;
QPixmapCache::Key m_pixmapCacheKey; QPixmapCache::Key m_pixmapCacheKey;
bool m_updateTimeinfo; bool m_updateTimeinfo;
template <class T> inline bool set(T& property, const T& value);
}; };
#endif // KEEPASSX_ENTRY_H #endif // KEEPASSX_ENTRY_H

View File

@ -109,7 +109,7 @@ private:
template <class P, class V> bool set(P& property, const V& value); template <class P, class V> bool set(P& property, const V& value);
template <class P, class V> bool set(P& property, const V& value, QDateTime& dateTime); template <class P, class V> bool set(P& property, const V& value, QDateTime& dateTime);
Database* m_parent; Database* const m_parent;
QString m_generator; QString m_generator;
QString m_name; QString m_name;

View File

@ -22,24 +22,8 @@
SymmetricCipher::SymmetricCipher(SymmetricCipher::Algorithm algo, SymmetricCipher::Mode mode, SymmetricCipher::SymmetricCipher(SymmetricCipher::Algorithm algo, SymmetricCipher::Mode mode,
SymmetricCipher::Direction direction, const QByteArray& key, const QByteArray& iv) SymmetricCipher::Direction direction, const QByteArray& key, const QByteArray& iv)
: m_backend(createBackend(algo, mode, direction))
{ {
switch (algo) {
case SymmetricCipher::Aes256:
m_backend = new SymmetricCipherGcrypt();
break;
case SymmetricCipher::Salsa20:
m_backend = new SymmetricCipherSalsa20();
break;
default:
Q_ASSERT(false);
break;
}
m_backend->setAlgorithm(algo);
m_backend->setMode(mode);
m_backend->setDirection(direction);
m_backend->init(); m_backend->init();
m_backend->setKey(key); m_backend->setKey(key);
m_backend->setIv(iv); m_backend->setIv(iv);
@ -47,7 +31,22 @@ SymmetricCipher::SymmetricCipher(SymmetricCipher::Algorithm algo, SymmetricCiphe
SymmetricCipher::~SymmetricCipher() SymmetricCipher::~SymmetricCipher()
{ {
delete m_backend; }
SymmetricCipherBackend* SymmetricCipher::createBackend(SymmetricCipher::Algorithm algo, SymmetricCipher::Mode mode,
SymmetricCipher::Direction direction)
{
switch (algo) {
case SymmetricCipher::Aes256:
return new SymmetricCipherGcrypt(algo, mode, direction);
case SymmetricCipher::Salsa20:
return new SymmetricCipherSalsa20(algo, mode, direction);
default:
Q_ASSERT(false);
return 0;
}
} }
QByteArray SymmetricCipher::process(const QByteArray& data) QByteArray SymmetricCipher::process(const QByteArray& data)

View File

@ -19,6 +19,7 @@
#define KEEPASSX_SYMMETRICCIPHER_H #define KEEPASSX_SYMMETRICCIPHER_H
#include <QtCore/QByteArray> #include <QtCore/QByteArray>
#include <QtCore/QScopedPointer>
class SymmetricCipherBackend; class SymmetricCipherBackend;
@ -55,7 +56,10 @@ public:
int blockSize() const; int blockSize() const;
private: private:
SymmetricCipherBackend* m_backend; static SymmetricCipherBackend* createBackend(SymmetricCipher::Algorithm algo, SymmetricCipher::Mode mode,
SymmetricCipher::Direction direction);
const QScopedPointer<SymmetricCipherBackend> m_backend;
Q_DISABLE_COPY(SymmetricCipher) Q_DISABLE_COPY(SymmetricCipher)
}; };

View File

@ -24,9 +24,6 @@ class SymmetricCipherBackend
{ {
public: public:
virtual ~SymmetricCipherBackend() {} virtual ~SymmetricCipherBackend() {}
virtual void setAlgorithm(SymmetricCipher::Algorithm algo) = 0;
virtual void setMode(SymmetricCipher::Mode mode) = 0;
virtual void setDirection(SymmetricCipher::Direction direction) = 0;
virtual void init() = 0; virtual void init() = 0;
virtual void setKey(const QByteArray& key) = 0; virtual void setKey(const QByteArray& key) = 0;
virtual void setIv(const QByteArray& iv) = 0; virtual void setIv(const QByteArray& iv) = 0;

View File

@ -19,9 +19,14 @@
#include "crypto/Crypto.h" #include "crypto/Crypto.h"
SymmetricCipherGcrypt::SymmetricCipherGcrypt() SymmetricCipherGcrypt::SymmetricCipherGcrypt(SymmetricCipher::Algorithm algo, SymmetricCipher::Mode mode,
SymmetricCipher::Direction direction)
: m_algo(GCRY_CIPHER_AES256)
, m_mode(gcryptMode(mode))
, m_direction(direction)
{ {
Q_ASSERT(Crypto::initalized()); Q_ASSERT(Crypto::initalized());
Q_ASSERT(algo == SymmetricCipher::Aes256);
} }
SymmetricCipherGcrypt::~SymmetricCipherGcrypt() SymmetricCipherGcrypt::~SymmetricCipherGcrypt()
@ -29,41 +34,21 @@ SymmetricCipherGcrypt::~SymmetricCipherGcrypt()
gcry_cipher_close(m_ctx); gcry_cipher_close(m_ctx);
} }
void SymmetricCipherGcrypt::setAlgorithm(SymmetricCipher::Algorithm algo) int SymmetricCipherGcrypt::gcryptMode(SymmetricCipher::Mode mode)
{
switch (algo) {
case SymmetricCipher::Aes256:
m_algo = GCRY_CIPHER_AES256;
break;
default:
Q_ASSERT(false);
break;
}
}
void SymmetricCipherGcrypt::setMode(SymmetricCipher::Mode mode)
{ {
switch (mode) { switch (mode) {
case SymmetricCipher::Ecb: case SymmetricCipher::Ecb:
m_mode = GCRY_CIPHER_MODE_ECB; return GCRY_CIPHER_MODE_ECB;
break;
case SymmetricCipher::Cbc: case SymmetricCipher::Cbc:
m_mode = GCRY_CIPHER_MODE_CBC; return GCRY_CIPHER_MODE_CBC;
break;
default: default:
Q_ASSERT(false); Q_ASSERT(false);
break; return -1;
} }
} }
void SymmetricCipherGcrypt::setDirection(SymmetricCipher::Direction direction)
{
m_direction = direction;
}
void SymmetricCipherGcrypt::init() void SymmetricCipherGcrypt::init()
{ {
gcry_error_t error; gcry_error_t error;

View File

@ -25,11 +25,9 @@
class SymmetricCipherGcrypt : public SymmetricCipherBackend class SymmetricCipherGcrypt : public SymmetricCipherBackend
{ {
public: public:
SymmetricCipherGcrypt(); SymmetricCipherGcrypt(SymmetricCipher::Algorithm algo, SymmetricCipher::Mode mode,
SymmetricCipher::Direction direction);
~SymmetricCipherGcrypt(); ~SymmetricCipherGcrypt();
void setAlgorithm(SymmetricCipher::Algorithm algo);
void setMode(SymmetricCipher::Mode mode);
void setDirection(SymmetricCipher::Direction direction);
void init(); void init();
void setKey(const QByteArray& key); void setKey(const QByteArray& key);
void setIv(const QByteArray& iv); void setIv(const QByteArray& iv);
@ -41,10 +39,12 @@ public:
int blockSize() const; int blockSize() const;
private: private:
static int gcryptMode(SymmetricCipher::Mode mode);
gcry_cipher_hd_t m_ctx; gcry_cipher_hd_t m_ctx;
int m_algo; const int m_algo;
int m_mode; const int m_mode;
SymmetricCipher::Direction m_direction; const SymmetricCipher::Direction m_direction;
QByteArray m_key; QByteArray m_key;
QByteArray m_iv; QByteArray m_iv;
int m_blockSize; int m_blockSize;

View File

@ -17,25 +17,20 @@
#include "SymmetricCipherSalsa20.h" #include "SymmetricCipherSalsa20.h"
SymmetricCipherSalsa20::~SymmetricCipherSalsa20() SymmetricCipherSalsa20::SymmetricCipherSalsa20(SymmetricCipher::Algorithm algo, SymmetricCipher::Mode mode,
{ SymmetricCipher::Direction direction)
}
void SymmetricCipherSalsa20::setAlgorithm(SymmetricCipher::Algorithm algo)
{ {
Q_ASSERT(algo == SymmetricCipher::Salsa20); Q_ASSERT(algo == SymmetricCipher::Salsa20);
Q_UNUSED(algo); Q_UNUSED(algo);
}
void SymmetricCipherSalsa20::setMode(SymmetricCipher::Mode mode)
{
Q_ASSERT(mode == SymmetricCipher::Stream); Q_ASSERT(mode == SymmetricCipher::Stream);
Q_UNUSED(mode); Q_UNUSED(mode);
Q_UNUSED(direction);
} }
void SymmetricCipherSalsa20::setDirection(SymmetricCipher::Direction direction) SymmetricCipherSalsa20::~SymmetricCipherSalsa20()
{ {
Q_UNUSED(direction);
} }
void SymmetricCipherSalsa20::init() void SymmetricCipherSalsa20::init()

View File

@ -24,6 +24,8 @@
class SymmetricCipherSalsa20 : public SymmetricCipherBackend class SymmetricCipherSalsa20 : public SymmetricCipherBackend
{ {
public: public:
SymmetricCipherSalsa20(SymmetricCipher::Algorithm algo, SymmetricCipher::Mode mode,
SymmetricCipher::Direction direction);
~SymmetricCipherSalsa20(); ~SymmetricCipherSalsa20();
void setAlgorithm(SymmetricCipher::Algorithm algo); void setAlgorithm(SymmetricCipher::Algorithm algo);
void setMode(SymmetricCipher::Mode mode); void setMode(SymmetricCipher::Mode mode);

View File

@ -33,7 +33,8 @@
#define CHECK_RETURN_FALSE(x) if (!(x)) return false; #define CHECK_RETURN_FALSE(x) if (!(x)) return false;
KeePass2Writer::KeePass2Writer() KeePass2Writer::KeePass2Writer()
: m_error(false) : m_device(0)
, m_error(false)
{ {
} }

View File

@ -48,7 +48,7 @@ private Q_SLOTS:
void togglePassword(bool checked); void togglePassword(bool checked);
private: private:
QScopedPointer<Ui::ChangeMasterKeyWidget> m_ui; const QScopedPointer<Ui::ChangeMasterKeyWidget> m_ui;
CompositeKey m_key; CompositeKey m_key;
Q_DISABLE_COPY(ChangeMasterKeyWidget) Q_DISABLE_COPY(ChangeMasterKeyWidget)

View File

@ -48,10 +48,10 @@ private Q_SLOTS:
void browseKeyFile(); void browseKeyFile();
private: private:
QScopedPointer<Ui::DatabaseOpenDialog> m_ui; const QScopedPointer<Ui::DatabaseOpenDialog> m_ui;
Database* m_db; Database* m_db;
QFile* m_file; QFile* const m_file;
QString m_filename; const QString m_filename;
Q_DISABLE_COPY(DatabaseOpenDialog) Q_DISABLE_COPY(DatabaseOpenDialog)
}; };

View File

@ -50,7 +50,7 @@ private Q_SLOTS:
void reject(); void reject();
private: private:
QScopedPointer<Ui::DatabaseSettingsWidget> m_ui; const QScopedPointer<Ui::DatabaseSettingsWidget> m_ui;
QString m_dbName; QString m_dbName;
QString m_dbDescription; QString m_dbDescription;

View File

@ -91,7 +91,7 @@ private:
Database* databaseFromDatabaseWidget(DatabaseWidget* dbWidget); Database* databaseFromDatabaseWidget(DatabaseWidget* dbWidget);
void insertDatabase(Database* db, const DatabaseManagerStruct& dbStruct); void insertDatabase(Database* db, const DatabaseManagerStruct& dbStruct);
QWidget* m_window; QWidget* const m_window;
KeePass2Writer m_writer; KeePass2Writer m_writer;
QHash<Database*, DatabaseManagerStruct> m_dbList; QHash<Database*, DatabaseManagerStruct> m_dbList;
DatabaseManagerStruct m_curDbStruct; DatabaseManagerStruct m_curDbStruct;

View File

@ -63,7 +63,7 @@ private Q_SLOTS:
void updateSettings(bool accepted); void updateSettings(bool accepted);
private: private:
Database* m_db; Database* const m_db;
QWidget* m_mainWidget; QWidget* m_mainWidget;
EditEntryWidget* m_editEntryWidget; EditEntryWidget* m_editEntryWidget;
EditGroupWidget* m_editGroupWidget; EditGroupWidget* m_editGroupWidget;

View File

@ -60,22 +60,22 @@ private Q_SLOTS:
void setPasswordCheckColors(); void setPasswordCheckColors();
private: private:
bool passwordsEqual();
Entry* m_entry; Entry* m_entry;
QScopedPointer<Ui::EditEntryWidget> m_ui; const QScopedPointer<Ui::EditEntryWidget> m_ui;
QScopedPointer<Ui::EditEntryWidgetMain> m_mainUi; const QScopedPointer<Ui::EditEntryWidgetMain> m_mainUi;
QScopedPointer<Ui::EditEntryWidgetNotes> m_notesUi; const QScopedPointer<Ui::EditEntryWidgetNotes> m_notesUi;
QScopedPointer<Ui::EditEntryWidgetAdvanced> m_advancedUi; const QScopedPointer<Ui::EditEntryWidgetAdvanced> m_advancedUi;
QWidget* m_mainWidget; QWidget* const m_mainWidget;
QWidget* m_notesWidget; QWidget* const m_notesWidget;
QWidget* m_advancedWidget; QWidget* const m_advancedWidget;
EntryAttachmentsModel* m_attachmentsModel; EntryAttachmentsModel* m_attachmentsModel;
EntryAttributesModel* m_attributesModel; EntryAttributesModel* m_attributesModel;
EntryAttachments* m_entryAttachments; EntryAttachments* m_entryAttachments;
EntryAttributes* m_entryAttributes; EntryAttributes* m_entryAttributes;
bool passwordsEqual();
Q_DISABLE_COPY(EditEntryWidget) Q_DISABLE_COPY(EditEntryWidget)
}; };

View File

@ -45,7 +45,7 @@ private Q_SLOTS:
void cancel(); void cancel();
private: private:
QScopedPointer<Ui::EditGroupWidget> m_ui; const QScopedPointer<Ui::EditGroupWidget> m_ui;
Group* m_group; Group* m_group;
Q_DISABLE_COPY(EditGroupWidget) Q_DISABLE_COPY(EditGroupWidget)

View File

@ -21,8 +21,8 @@
EntryView::EntryView(QWidget* parent) EntryView::EntryView(QWidget* parent)
: QTreeView(parent) : QTreeView(parent)
, m_model(new EntryModel(this))
{ {
m_model = new EntryModel(this);
QTreeView::setModel(m_model); QTreeView::setModel(m_model);
setUniformRowHeights(true); setUniformRowHeights(true);

View File

@ -45,7 +45,7 @@ Q_SIGNALS:
void entrySelectionChanged(); void entrySelectionChanged();
private: private:
EntryModel* m_model; EntryModel* const m_model;
}; };
#endif // KEEPASSX_ENTRYVIEW_H #endif // KEEPASSX_ENTRYVIEW_H

View File

@ -25,8 +25,8 @@
GroupView::GroupView(Database* db, QWidget* parent) GroupView::GroupView(Database* db, QWidget* parent)
: QTreeView(parent) : QTreeView(parent)
, m_model(new GroupModel(db, this))
{ {
m_model = new GroupModel(db, this);
QTreeView::setModel(m_model); QTreeView::setModel(m_model);
setHeaderHidden(true); setHeaderHidden(true);
setUniformRowHeights(true); setUniformRowHeights(true);

View File

@ -44,7 +44,7 @@ private Q_SLOTS:
private: private:
void recInitExpanded(Group* group); void recInitExpanded(Group* group);
GroupModel* m_model; GroupModel* const m_model;
}; };
#endif // KEEPASSX_GROUPVIEW_H #endif // KEEPASSX_GROUPVIEW_H

View File

@ -39,9 +39,8 @@ private Q_SLOTS:
void setMenuActionState(int index = -1); void setMenuActionState(int index = -1);
void updateWindowTitle(); void updateWindowTitle();
private: private:
QScopedPointer<Ui::MainWindow> m_ui; const QScopedPointer<Ui::MainWindow> m_ui;
static const QString m_baseWindowTitle; static const QString m_baseWindowTitle;
Q_DISABLE_COPY(MainWindow) Q_DISABLE_COPY(MainWindow)

View File

@ -37,9 +37,9 @@ public:
void addKey(const Key& key); void addKey(const Key& key);
private: private:
QList<Key*> m_keys;
static QByteArray transformKeyRaw(const QByteArray& key, const QByteArray& seed, int rounds); static QByteArray transformKeyRaw(const QByteArray& key, const QByteArray& seed, int rounds);
QList<Key*> m_keys;
}; };
#endif // KEEPASSX_COMPOSITEKEY_H #endif // KEEPASSX_COMPOSITEKEY_H

View File

@ -36,7 +36,7 @@ protected:
qint64 readData(char* data, qint64 maxSize); qint64 readData(char* data, qint64 maxSize);
qint64 writeData(const char* data, qint64 maxSize); qint64 writeData(const char* data, qint64 maxSize);
QIODevice* m_baseDevice; QIODevice* const m_baseDevice;
private Q_SLOTS: private Q_SLOTS:
void closeStream(); void closeStream();