Fix issues with group functions (#2410)

This commit is contained in:
Jonathan White
2018-10-30 08:42:35 -04:00
committed by GitHub
parent 7263dcddfe
commit fa687f246e
6 changed files with 147 additions and 160 deletions

View File

@@ -84,7 +84,7 @@ int Clip::clipEntry(Database* database, QString entryPath, QString timeout)
}
TextStream outputTextStream(Utils::STDOUT, QIODevice::WriteOnly);
Entry* entry = database->rootGroup()->findEntry(entryPath);
Entry* entry = database->rootGroup()->findEntryByPath(entryPath);
if (!entry) {
err << QObject::tr("Entry %1 not found.").arg(entryPath) << endl;
return EXIT_FAILURE;

View File

@@ -82,7 +82,7 @@ int Show::showEntry(Database* database, QStringList attributes, const QString& e
TextStream out(Utils::STDOUT, QIODevice::WriteOnly);
TextStream err(Utils::STDERR, QIODevice::WriteOnly);
Entry* entry = database->rootGroup()->findEntry(entryPath);
Entry* entry = database->rootGroup()->findEntryByPath(entryPath);
if (!entry) {
err << QObject::tr("Could not find entry with path %1.").arg(entryPath) << endl;
return EXIT_FAILURE;

View File

@@ -321,9 +321,7 @@ void Group::setNotes(const QString& notes)
void Group::setIcon(int iconNumber)
{
Q_ASSERT(iconNumber >= 0);
if (m_data.iconNumber != iconNumber || !m_data.customIcon.isNull()) {
if (iconNumber >= 0 && (m_data.iconNumber != iconNumber || !m_data.customIcon.isNull())) {
m_data.iconNumber = iconNumber;
m_data.customIcon = QUuid();
emit modified();
@@ -333,9 +331,7 @@ void Group::setIcon(int iconNumber)
void Group::setIcon(const QUuid& uuid)
{
Q_ASSERT(!uuid.isNull());
if (m_data.customIcon != uuid) {
if (!uuid.isNull() && m_data.customIcon != uuid) {
m_data.customIcon = uuid;
m_data.iconNumber = 0;
emit modified();
@@ -552,36 +548,12 @@ QList<Entry*> Group::entriesRecursive(bool includeHistoryItems) const
return entryList;
}
Entry* Group::findEntry(QString entryId)
{
Q_ASSERT(!entryId.isNull());
Entry* entry;
QUuid entryUuid = QUuid::fromRfc4122(QByteArray::fromHex(entryId.toLatin1()));
if (!entryUuid.isNull()) {
entry = findEntryByUuid(entryUuid);
if (entry) {
return entry;
}
}
entry = findEntryByPath(entryId);
if (entry) {
return entry;
}
for (Entry* entry : entriesRecursive(false)) {
if (entry->title() == entryId) {
return entry;
}
}
return nullptr;
}
Entry* Group::findEntryByUuid(const QUuid& uuid) const
{
Q_ASSERT(!uuid.isNull());
if (uuid.isNull()) {
return nullptr;
}
for (Entry* entry : entriesRecursive(false)) {
if (entry->uuid() == uuid) {
return entry;
@@ -591,20 +563,34 @@ Entry* Group::findEntryByUuid(const QUuid& uuid) const
return nullptr;
}
Entry* Group::findEntryByPath(QString entryPath, QString basePath)
Entry* Group::findEntryByPath(QString entryPath)
{
if (entryPath.isEmpty()) {
return nullptr;
}
Q_ASSERT(!entryPath.isNull());
// Add a beginning slash if the search string contains a slash
// We don't add a slash by default to allow searching by entry title
QString normalizedEntryPath = entryPath;
if (!normalizedEntryPath.startsWith("/") && normalizedEntryPath.contains("/")) {
normalizedEntryPath = "/" + normalizedEntryPath;
}
return findEntryByPathRecursive(normalizedEntryPath, "/");
}
for (Entry* entry : asConst(m_entries)) {
QString currentEntryPath = basePath + entry->title();
if (entryPath == currentEntryPath || entryPath == QString("/" + currentEntryPath)) {
Entry* Group::findEntryByPathRecursive(QString entryPath, QString basePath)
{
// Return the first entry that matches the full path OR if there is no leading
// slash, return the first entry title that matches
for (Entry* entry : entries()) {
if (entryPath == (basePath + entry->title())
|| (!entryPath.startsWith("/") && entry->title() == entryPath)) {
return entry;
}
}
for (Group* group : asConst(m_children)) {
Entry* entry = group->findEntryByPath(entryPath, basePath + group->name() + QString("/"));
for (Group* group : children()) {
Entry* entry = group->findEntryByPathRecursive(entryPath, basePath + group->name() + "/");
if (entry != nullptr) {
return entry;
}
@@ -615,22 +601,20 @@ Entry* Group::findEntryByPath(QString entryPath, QString basePath)
Group* Group::findGroupByPath(QString groupPath)
{
Q_ASSERT(!groupPath.isNull());
// normalize the groupPath by adding missing front and rear slashes. once.
QString normalizedGroupPath;
if (groupPath == "") {
if (groupPath.isEmpty()) {
normalizedGroupPath = QString("/"); // root group
} else {
normalizedGroupPath = ((groupPath.startsWith("/"))? "" : "/")
normalizedGroupPath = (groupPath.startsWith("/") ? "" : "/")
+ groupPath
+ ((groupPath.endsWith("/") )? "" : "/");
+ (groupPath.endsWith("/") ? "" : "/");
}
return findGroupByPathRecursion(normalizedGroupPath, "/");
return findGroupByPathRecursive(normalizedGroupPath, "/");
}
Group* Group::findGroupByPathRecursion(QString groupPath, QString basePath)
Group* Group::findGroupByPathRecursive(QString groupPath, QString basePath)
{
// paths must be normalized
Q_ASSERT(groupPath.startsWith("/") && groupPath.endsWith("/"));
@@ -642,7 +626,7 @@ Group* Group::findGroupByPathRecursion(QString groupPath, QString basePath)
for (Group* innerGroup : children()) {
QString innerBasePath = basePath + innerGroup->name() + "/";
Group* group = innerGroup->findGroupByPathRecursion(groupPath, innerBasePath);
Group* group = innerGroup->findGroupByPathRecursive(groupPath, innerBasePath);
if (group != nullptr) {
return group;
}
@@ -683,7 +667,7 @@ QList<const Group*> Group::groupsRecursive(bool includeSelf) const
groupList.append(this);
}
for (const Group* group : m_children) {
for (const Group* group : asConst(m_children)) {
groupList.append(group->groupsRecursive(true));
}
@@ -728,7 +712,10 @@ QSet<QUuid> Group::customIconsRecursive() const
Group* Group::findGroupByUuid(const QUuid& uuid)
{
Q_ASSERT(!uuid.isNull());
if (uuid.isNull()) {
return nullptr;
}
for (Group* group : groupsRecursive(true)) {
if (group->uuid() == uuid) {
return group;
@@ -749,6 +736,11 @@ Group* Group::findChildByName(const QString& name)
return nullptr;
}
/**
* Creates a duplicate of this group.
* Note that you need to copy the custom icons manually when inserting the
* new group into another database.
*/
Group* Group::clone(Entry::CloneFlags entryFlags, Group::CloneFlags groupFlags) const
{
Group* clonedGroup = new Group();
@@ -936,8 +928,11 @@ bool Group::resolveAutoTypeEnabled() const
QStringList Group::locate(QString locateTerm, QString currentPath)
{
Q_ASSERT(!locateTerm.isNull());
// TODO: Replace with EntrySearcher
QStringList response;
if (locateTerm.isEmpty()) {
return response;
}
for (Entry* entry : asConst(m_entries)) {
QString entryPath = currentPath + entry->title();
@@ -957,20 +952,15 @@ QStringList Group::locate(QString locateTerm, QString currentPath)
Entry* Group::addEntryWithPath(QString entryPath)
{
Q_ASSERT(!entryPath.isNull());
if (this->findEntryByPath(entryPath)) {
if (entryPath.isEmpty() || findEntryByPath(entryPath)) {
return nullptr;
}
QStringList groups = entryPath.split("/");
QString entryTitle = groups.takeLast();
QString groupPath = groups.join("/");
if (groupPath.isNull()) {
groupPath = QString("");
}
Q_ASSERT(!groupPath.isNull());
Group* group = this->findGroupByPath(groupPath);
Group* group = findGroupByPath(groupPath);
if (!group) {
return nullptr;
}

View File

@@ -114,12 +114,11 @@ public:
static const QString RootAutoTypeSequence;
Group* findChildByName(const QString& name);
Entry* findEntry(QString entryId);
Entry* findEntryByUuid(const QUuid& uuid) const;
Entry* findEntryByPath(QString entryPath, QString basePath = QString(""));
Entry* findEntryByPath(QString entryPath);
Group* findGroupByUuid(const QUuid& uuid);
Group* findGroupByPath(QString groupPath);
QStringList locate(QString locateTerm, QString currentPath = QString("/"));
QStringList locate(QString locateTerm, QString currentPath = {"/"});
Entry* addEntryWithPath(QString entryPath);
void setUuid(const QUuid& uuid);
void setName(const QString& name);
@@ -154,11 +153,7 @@ public:
QList<const Group*> groupsRecursive(bool includeSelf) const;
QList<Group*> groupsRecursive(bool includeSelf);
QSet<QUuid> customIconsRecursive() const;
/**
* Creates a duplicate of this group.
* Note that you need to copy the custom icons manually when inserting the
* new group into another database.
*/
Group* clone(Entry::CloneFlags entryFlags = DefaultEntryCloneFlags,
CloneFlags groupFlags = DefaultCloneFlags) const;
@@ -167,28 +162,22 @@ public:
void addEntry(Entry* entry);
void removeEntry(Entry* entry);
signals:
void dataChanged(Group* group);
void aboutToAdd(Group* group, int index);
void added();
void aboutToRemove(Group* group);
void removed();
/**
* Group moved within the database.
*/
void aboutToMove(Group* group, Group* toGroup, int index);
void moved();
void modified();
void entryAboutToAdd(Entry* entry);
void entryAdded(Entry* entry);
void entryAboutToRemove(Entry* entry);
void entryRemoved(Entry* entry);
void entryDataChanged(Entry* entry);
void modified();
private slots:
void updateTimeinfo();
@@ -201,7 +190,8 @@ private:
void cleanupParent();
void recCreateDelObjects();
Group* findGroupByPathRecursion(QString groupPath, QString basePath);
Entry* findEntryByPathRecursive(QString entryPath, QString basePath);
Group* findGroupByPathRecursive(QString groupPath, QString basePath);
QPointer<Database> m_db;
QUuid m_uuid;