Use a special enum for inherit/enable/disable variables.

This commit is contained in:
Felix Geyer 2011-07-07 12:45:14 +02:00
parent be934b2fce
commit afb285a476
6 changed files with 60 additions and 48 deletions

View file

@ -88,12 +88,12 @@ QString Group::defaultAutoTypeSequence() const
return m_defaultAutoTypeSequence; return m_defaultAutoTypeSequence;
} }
int Group::autoTypeEnabled() const Group::TriState Group::autoTypeEnabled() const
{ {
return m_autoTypeEnabled; return m_autoTypeEnabled;
} }
int Group::searchingEnabed() const Group::TriState Group::searchingEnabed() const
{ {
return m_searchingEnabled; return m_searchingEnabled;
} }
@ -155,17 +155,13 @@ void Group::setDefaultAutoTypeSequence(const QString& sequence)
m_defaultAutoTypeSequence = sequence; m_defaultAutoTypeSequence = sequence;
} }
void Group::setAutoTypeEnabled(int enable) void Group::setAutoTypeEnabled(TriState enable)
{ {
Q_ASSERT(enable >= -1 && enable <= 1);
m_autoTypeEnabled = enable; m_autoTypeEnabled = enable;
} }
void Group::setSearchingEnabled(int enable) void Group::setSearchingEnabled(TriState enable)
{ {
Q_ASSERT(enable >= -1 && enable <= 1);
m_searchingEnabled = enable; m_searchingEnabled = enable;
} }

View file

@ -31,6 +31,8 @@ class Group : public QObject
Q_OBJECT Q_OBJECT
public: public:
enum TriState { Inherit, Enable, Disable };
Group(); Group();
~Group(); ~Group();
Uuid uuid() const; Uuid uuid() const;
@ -42,8 +44,8 @@ public:
TimeInfo timeInfo() const; TimeInfo timeInfo() const;
bool isExpanded() const; bool isExpanded() const;
QString defaultAutoTypeSequence() const; QString defaultAutoTypeSequence() const;
int autoTypeEnabled() const; Group::TriState autoTypeEnabled() const;
int searchingEnabed() const; Group::TriState searchingEnabed() const;
Entry* lastTopVisibleEntry() const; Entry* lastTopVisibleEntry() const;
void setUuid(const Uuid& uuid); void setUuid(const Uuid& uuid);
@ -54,8 +56,8 @@ public:
void setTimeInfo(const TimeInfo& timeInfo); void setTimeInfo(const TimeInfo& timeInfo);
void setExpanded(bool expanded); void setExpanded(bool expanded);
void setDefaultAutoTypeSequence(const QString& sequence); void setDefaultAutoTypeSequence(const QString& sequence);
void setAutoTypeEnabled(int enable); void setAutoTypeEnabled(TriState enable);
void setSearchingEnabled(int enable); void setSearchingEnabled(TriState enable);
void setLastTopVisibleEntry(Entry* entry); void setLastTopVisibleEntry(Entry* entry);
Group* parentGroup(); Group* parentGroup();
@ -98,8 +100,8 @@ private:
TimeInfo m_timeInfo; TimeInfo m_timeInfo;
bool m_isExpanded; bool m_isExpanded;
QString m_defaultAutoTypeSequence; QString m_defaultAutoTypeSequence;
int m_autoTypeEnabled; TriState m_autoTypeEnabled;
int m_searchingEnabled; TriState m_searchingEnabled;
Entry* m_lastTopVisibleEntry; Entry* m_lastTopVisibleEntry;
QList<Group*> m_children; QList<Group*> m_children;
QList<Entry*> m_entries; QList<Entry*> m_entries;

View file

@ -340,13 +340,13 @@ Group* KeePass2XmlReader::parseGroup()
QString str = readString(); QString str = readString();
if (str.compare("null", Qt::CaseInsensitive) == 0) { if (str.compare("null", Qt::CaseInsensitive) == 0) {
group->setAutoTypeEnabled(-1); group->setAutoTypeEnabled(Group::Inherit);
} }
else if (str.compare("true", Qt::CaseInsensitive) == 0) { else if (str.compare("true", Qt::CaseInsensitive) == 0) {
group->setAutoTypeEnabled(1); group->setAutoTypeEnabled(Group::Enable);
} }
else if (str.compare("false", Qt::CaseInsensitive) == 0) { else if (str.compare("false", Qt::CaseInsensitive) == 0) {
group->setAutoTypeEnabled(0); group->setAutoTypeEnabled(Group::Disable);
} }
else { else {
raiseError(); raiseError();
@ -357,13 +357,13 @@ Group* KeePass2XmlReader::parseGroup()
QString str = readString(); QString str = readString();
if (str.compare("null", Qt::CaseInsensitive) == 0) { if (str.compare("null", Qt::CaseInsensitive) == 0) {
group->setSearchingEnabled(-1); group->setSearchingEnabled(Group::Inherit);
} }
else if (str.compare("true", Qt::CaseInsensitive) == 0) { else if (str.compare("true", Qt::CaseInsensitive) == 0) {
group->setSearchingEnabled(1); group->setSearchingEnabled(Group::Enable);
} }
else if (str.compare("false", Qt::CaseInsensitive) == 0) { else if (str.compare("false", Qt::CaseInsensitive) == 0) {
group->setSearchingEnabled(0); group->setSearchingEnabled(Group::Disable);
} }
else { else {
raiseError(); raiseError();

View file

@ -182,31 +182,9 @@ void KeePass2XmlWriter::writeGroup(const Group* group)
writeBool("IsExpanded", group->isExpanded()); writeBool("IsExpanded", group->isExpanded());
writeString("DefaultAutoTypeSequence", group->defaultAutoTypeSequence()); writeString("DefaultAutoTypeSequence", group->defaultAutoTypeSequence());
int autoTypeEnabled = group->autoTypeEnabled(); writeTriState("EnableAutoType", group->autoTypeEnabled());
QString autoTypeEnabledStr;
if (autoTypeEnabled == -1) {
autoTypeEnabledStr = "null";
}
else if (autoTypeEnabled == 0) {
autoTypeEnabledStr = "false";
}
else {
autoTypeEnabledStr = "true";
}
writeString("EnableAutoType", autoTypeEnabledStr);
int searchingEnabed = group->searchingEnabed(); writeTriState("EnableSearching", group->searchingEnabed());
QString searchingEnabedStr;
if (searchingEnabed == -1) {
searchingEnabedStr = "null";
}
else if (searchingEnabed == 0) {
searchingEnabedStr = "false";
}
else {
searchingEnabedStr = "true";
}
writeString("EnableSearching", searchingEnabedStr);
writeUuid("LastTopVisibleEntry", group->lastTopVisibleEntry()); writeUuid("LastTopVisibleEntry", group->lastTopVisibleEntry());
@ -441,6 +419,23 @@ void KeePass2XmlWriter::writeColor(const QString& qualifiedName, const QColor& c
writeString(qualifiedName, colorStr); writeString(qualifiedName, colorStr);
} }
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);
}
QString KeePass2XmlWriter::colorPartToString(int value) QString KeePass2XmlWriter::colorPartToString(int value)
{ {
QString str = QString::number(value, 16).toUpper(); QString str = QString::number(value, 16).toUpper();

View file

@ -67,6 +67,7 @@ private:
void writeUuid(const QString& qualifiedName, const Entry* entry); void writeUuid(const QString& qualifiedName, const Entry* entry);
void writeBinary(const QString& qualifiedName, const QByteArray& ba); void writeBinary(const QString& qualifiedName, const QByteArray& ba);
void writeColor(const QString& qualifiedName, const QColor& color); void writeColor(const QString& qualifiedName, const QColor& color);
void writeTriState(const QString& qualifiedName, Group::TriState triState);
QString colorPartToString(int value); QString colorPartToString(int value);
QXmlStreamWriter m_xml; QXmlStreamWriter m_xml;

View file

@ -33,6 +33,24 @@ namespace QTest {
ba += ")"; ba += ")";
return qstrdup(ba.data()); return qstrdup(ba.data());
} }
template<>
char *toString(const Group::TriState &triState)
{
QString value;
if (triState == Group::Inherit) {
value = "null";
}
else if (triState == Group::Enable) {
value = "true";
}
else {
value = "false";
}
return qstrdup(value.toLocal8Bit().constData());
}
} }
QDateTime TestKeePass2XmlReader::genDT(int year, int month, int day, int hour, int min, int second) QDateTime TestKeePass2XmlReader::genDT(int year, int month, int day, int hour, int min, int second)
@ -123,8 +141,8 @@ void TestKeePass2XmlReader::testGroupRoot()
QCOMPARE(ti.usageCount(), 52); QCOMPARE(ti.usageCount(), 52);
QCOMPARE(ti.locationChanged(), genDT(2010, 8, 8, 17, 24, 27)); QCOMPARE(ti.locationChanged(), genDT(2010, 8, 8, 17, 24, 27));
QCOMPARE(group->defaultAutoTypeSequence(), QString("")); QCOMPARE(group->defaultAutoTypeSequence(), QString(""));
QCOMPARE(group->autoTypeEnabled(), -1); QCOMPARE(group->autoTypeEnabled(), Group::Inherit);
QCOMPARE(group->searchingEnabed(), -1); QCOMPARE(group->searchingEnabed(), Group::Inherit);
QCOMPARE(group->lastTopVisibleEntry()->uuid().toBase64(), QString("+wSUOv6qf0OzW8/ZHAs2sA==")); QCOMPARE(group->lastTopVisibleEntry()->uuid().toBase64(), QString("+wSUOv6qf0OzW8/ZHAs2sA=="));
QCOMPARE(group->children().size(), 3); QCOMPARE(group->children().size(), 3);
@ -144,8 +162,8 @@ void TestKeePass2XmlReader::testGroup1()
QCOMPARE(group->iconUuid(), Uuid()); QCOMPARE(group->iconUuid(), Uuid());
QCOMPARE(group->isExpanded(), true); QCOMPARE(group->isExpanded(), true);
QCOMPARE(group->defaultAutoTypeSequence(), QString("{Password}{ENTER}")); QCOMPARE(group->defaultAutoTypeSequence(), QString("{Password}{ENTER}"));
QCOMPARE(group->autoTypeEnabled(), 1); QCOMPARE(group->autoTypeEnabled(), Group::Enable);
QCOMPARE(group->searchingEnabed(), 0); QCOMPARE(group->searchingEnabed(), Group::Disable);
QVERIFY(!group->lastTopVisibleEntry()); QVERIFY(!group->lastTopVisibleEntry());
} }