Skip to content

Commit b4ad134

Browse files
committed
#1717 backlinks: fix duplicate item issue
Signed-off-by: Patrizio Bekerle <[email protected]>
1 parent b78b798 commit b4ad134

File tree

4 files changed

+29
-15
lines changed

4 files changed

+29
-15
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# QOwnNotes Changelog
22

3+
## 24.9.4
4+
- An issue with the **backlink widget** showing duplicate items panel was fixed
5+
(for [#1717](https://github.com/pbek/QOwnNotes/issues/1717))
36
## 24.9.3
47
- In the **backlink widget** and the automatic link transformation, when notes are
58
renamed, now relative links with `/` and urlencoded `/` (as `%2F`) are both supported

src/entities/note.cpp

+8-8
Original file line numberDiff line numberDiff line change
@@ -3144,9 +3144,9 @@ BacklinkHit Note::findAndReturnBacklinkHit(const QString &text, const QString &p
31443144
return text.contains(pattern) ? BacklinkHit(pattern, pattern) : BacklinkHit("", "");
31453145
}
31463146

3147-
QList<BacklinkHit> Note::findAndReturnBacklinkHit(const QString &text,
3148-
const QRegularExpression &regex) {
3149-
QList<BacklinkHit> backlinkHits;
3147+
QSet<BacklinkHit> Note::findAndReturnBacklinkHit(const QString &text,
3148+
const QRegularExpression &regex) {
3149+
QSet<BacklinkHit> backlinkHits;
31503150
QRegularExpressionMatchIterator iterator = regex.globalMatch(text);
31513151

31523152
while (iterator.hasNext()) {
@@ -3155,7 +3155,7 @@ QList<BacklinkHit> Note::findAndReturnBacklinkHit(const QString &text,
31553155
QString linkUrl = match.captured(2);
31563156
// Do something with the link text and URL
31573157

3158-
backlinkHits.append(BacklinkHit(match.captured(0), match.captured(1)));
3158+
backlinkHits.insert(BacklinkHit(match.captured(0), match.captured(1)));
31593159
}
31603160

31613161
return backlinkHits;
@@ -3180,12 +3180,12 @@ void Note::addTextToBacklinkNoteHashIfFound(const Note &note, const QRegularExpr
31803180
if (!_backlinkNoteHash.contains(note)) {
31813181
_backlinkNoteHash.insert(note, backlinkHits);
31823182
} else {
3183-
_backlinkNoteHash[note] << backlinkHits;
3183+
_backlinkNoteHash[note] = backlinkHits;
31843184
}
31853185
}
31863186
}
31873187

3188-
QHash<Note, QList<BacklinkHit>> Note::findReverseLinkNotes() {
3188+
QHash<Note, QSet<BacklinkHit>> Note::findReverseLinkNotes() {
31893189
const QVector<int> noteIdList = this->findLinkedNoteIds();
31903190
const int noteCount = noteIdList.count();
31913191

@@ -3501,15 +3501,15 @@ bool Note::handleNoteMoving(const Note &oldNote) {
35013501
return noteIdList.contains(_id);
35023502
}
35033503

3504-
QList<Note> Note::findBacklinks() const {
3504+
QSet<Note> Note::findBacklinks() const {
35053505
const QVector<int> noteIdList = this->findLinkedNoteIds();
35063506
const int noteCount = noteIdList.count();
35073507

35083508
if (noteCount == 0) {
35093509
return {};
35103510
}
35113511

3512-
QList<Note> notes;
3512+
QSet<Note> notes;
35133513

35143514
for (const int noteId : noteIdList) {
35153515
const Note linkedNote = Note::fetch(noteId);

src/entities/note.h

+16-5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <utils/misc.h>
55

66
#include <QDateTime>
7+
#include <QSet>
78

89
class Bookmark;
910
class CommandSnippet;
@@ -19,10 +20,20 @@ struct BacklinkHit {
1920

2021
bool isEmpty() const noexcept { return markdown.isEmpty() && text.isEmpty(); }
2122

23+
// Add operator== for comparison
24+
bool operator==(const BacklinkHit &other) const {
25+
return markdown == other.markdown && text == other.text;
26+
}
27+
2228
QString markdown;
2329
QString text;
2430
};
2531

32+
// Add hash function for BacklinkHit
33+
inline uint qHash(const BacklinkHit &hit, uint seed = 0) {
34+
return qHash(hit.markdown, seed) ^ qHash(hit.text, seed);
35+
}
36+
2637
#define NOTE_TEXT_ENCRYPTION_PRE_STRING "<!-- BEGIN ENCRYPTED TEXT --"
2738
#define NOTE_TEXT_ENCRYPTION_POST_STRING "-- END ENCRYPTED TEXT -->"
2839
#define BOTAN_SALT "Gj3%36/SmPoe12$snNAs-A-_.),?faQ1@!f32"
@@ -370,9 +381,9 @@ class Note {
370381

371382
static bool applyIgnoredNotesSetting(QStringList &fileNames);
372383

373-
QList<Note> findBacklinks() const;
384+
QSet<Note> findBacklinks() const;
374385

375-
QHash<Note, QList<BacklinkHit>> findReverseLinkNotes();
386+
QHash<Note, QSet<BacklinkHit>> findReverseLinkNotes();
376387

377388
protected:
378389
int _id;
@@ -394,7 +405,7 @@ class Note {
394405
int _shareId;
395406
unsigned int _sharePermissions;
396407
bool _hasDirtyData;
397-
QHash<Note, QList<BacklinkHit>> _backlinkNoteHash;
408+
QHash<Note, QSet<BacklinkHit>> _backlinkNoteHash;
398409

399410
static QRegularExpression getEncryptedNoteTextRegularExpression();
400411
QString getEncryptedNoteText() const;
@@ -406,8 +417,8 @@ class Note {
406417
void restoreCreatedDate();
407418

408419
static BacklinkHit findAndReturnBacklinkHit(const QString &text, const QString &pattern);
409-
static QList<BacklinkHit> findAndReturnBacklinkHit(const QString &text,
410-
const QRegularExpression &regex);
420+
static QSet<BacklinkHit> findAndReturnBacklinkHit(const QString &text,
421+
const QRegularExpression &regex);
411422

412423
void addTextToBacklinkNoteHashIfFound(const Note &note, const QString &pattern);
413424
void addTextToBacklinkNoteHashIfFound(const Note &note, const QRegularExpression &pattern);

src/widgets/backlinkwidget.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,12 @@ void BacklinkWidget::onItemClicked(QTreeWidgetItem *current, int column) {
6060
*/
6161
void BacklinkWidget::findBacklinks(Note note) {
6262
clear();
63-
QHash<Note, QList<BacklinkHit>> reverseLinkNotes = note.findReverseLinkNotes();
63+
auto reverseLinkNotes = note.findReverseLinkNotes();
6464

6565
// Iterate over reverseLinkNotes
6666
for (auto it = reverseLinkNotes.begin(); it != reverseLinkNotes.end(); ++it) {
6767
const Note &backlinkNote = it.key();
68-
const QList<BacklinkHit> &linkTextList = it.value();
68+
const QSet<BacklinkHit> &linkTextList = it.value();
6969

7070
auto *topItem = new QTreeWidgetItem();
7171

0 commit comments

Comments
 (0)