Skip to content

Commit

Permalink
Filter notes correctly by tag even if notes with same name are in dif…
Browse files Browse the repository at this point in the history
…ferent note sub-folders (#1686)
  • Loading branch information
pbek committed Apr 13, 2020
1 parent 00f16e3 commit 92981d4
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 34 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
sub-folders (for [#1683](https://github.com/pbek/QOwnNotes/issues/1683))
- the note folder will now be reloaded if a note sub-folder was deleted in the *Subfolders panel*
- fixed optional automatic database closing under Windows (for [#926](https://github.com/pbek/QOwnNotes/issues/926))
- notes are now correctly filtered by tag even if notes with the same name are
in different note sub-folders (for [#1686](https://github.com/pbek/QOwnNotes/issues/1686))

## 20.4.5
- fixed the time formats link in the *Editor settings*
Expand Down
24 changes: 19 additions & 5 deletions src/entities/note.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,13 @@ Note Note::fetchByFileName(const QString &fileName, int noteSubFolderId) {
return note;
}

Note Note::fetchByFileName(const QString &fileName,
const QString &noteSubFolderPathData) {
auto noteSubFolder = NoteSubFolder::fetchByPathData(noteSubFolderPathData);

return fetchByFileName(fileName, noteSubFolder.getId());
}

bool Note::fillByFileName(const QString &fileName, int noteSubFolderId) {
const QSqlDatabase db = QSqlDatabase::database(QStringLiteral("memory"));
QSqlQuery query(db);
Expand Down Expand Up @@ -506,6 +513,13 @@ Note Note::fetchByShareId(int shareId) {
return Note();
}

Note Note::fetchByName(const QString &name,
const QString &noteSubFolderPathData) {
auto noteSubFolder = NoteSubFolder::fetchByPathData(noteSubFolderPathData);

return fetchByName(name, noteSubFolder.getId());
}

Note Note::fetchByName(const QString &name, int noteSubFolderId) {
const QSqlDatabase db = QSqlDatabase::database(QStringLiteral("memory"));
QSqlQuery query(db);
Expand Down Expand Up @@ -688,19 +702,19 @@ QVector<Note> Note::fetchAllNotTagged(int activeNoteSubFolderId) {
/**
* Returns all notes names that are not tagged
*/
QStringList Note::fetchAllNotTaggedNames() {
QVector<int> Note::fetchAllNotTaggedIds() {
QVector<Note> noteList = Note::fetchAll();
QStringList untaggedNoteFileNameList;
untaggedNoteFileNameList.reserve(noteList.size());
QVector<int> untaggedNoteIdList;
untaggedNoteIdList.reserve(noteList.size());

QVectorIterator<Note> itr(noteList);
QVector<Note>::const_iterator it = noteList.constBegin();
for (; it != noteList.constEnd(); ++it) {
const int tagCount = Tag::countAllOfNote(*it);
if (tagCount == 0) untaggedNoteFileNameList << it->getName();
if (tagCount == 0) untaggedNoteIdList << it->getId();
}

return untaggedNoteFileNameList;
return untaggedNoteIdList;
}

/**
Expand Down
8 changes: 7 additions & 1 deletion src/entities/note.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,19 @@ class Note {
static Note fetchByFileName(const QString &fileName,
int noteSubFolderId = -1);

static Note fetchByFileName(const QString &fileName,
const QString &noteSubFolderPathData);

static Note fetchByName(const QString &name, int noteSubFolderId = -1);

static Note fetchByName(const QString &name,
const QString &noteSubFolderPathData);

static QVector<Note> fetchAll(int limit = -1);

static QVector<Note> fetchAllNotTagged(int activeNoteSubFolderId);

static QStringList fetchAllNotTaggedNames();
static QVector<int> fetchAllNotTaggedIds();

static int countAllNotTagged(int activeNoteSubFolderId = -1);

Expand Down
53 changes: 34 additions & 19 deletions src/entities/tag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -574,22 +574,22 @@ QList<Tag> Tag::fetchAllWithLinkToNoteNames(const QStringList &noteNameList) {
}

/**
* Fetches all linked note file names
* Fetches all linked note ids
*/

QStringList Tag::fetchAllLinkedNoteFileNames(
const bool fromAllSubfolders) const {
QVector<int> Tag::fetchAllLinkedNoteIds(const bool fromAllSubfolders) const {
QSqlDatabase db = DatabaseService::getNoteFolderDatabase();
QSqlQuery query(db);
QStringList fileNameList;
QVector<int> noteIdList;

if (fromAllSubfolders) {
// 'All notes' selected in note subfolder panel
query.prepare(QStringLiteral(
"SELECT note_file_name FROM noteTagLink WHERE tag_id = :id"));
"SELECT note_file_name, note_sub_folder_path "
"FROM noteTagLink WHERE tag_id = :id"));
} else {
query.prepare(QStringLiteral(
"SELECT note_file_name FROM noteTagLink WHERE tag_id = :id "
"SELECT note_file_name, note_sub_folder_path "
"FROM noteTagLink WHERE tag_id = :id "
"AND note_sub_folder_path LIKE :noteSubFolderPath"));
query.bindValue(
QStringLiteral(":noteSubFolderPath"),
Expand All @@ -602,33 +602,41 @@ QStringList Tag::fetchAllLinkedNoteFileNames(
qWarning() << __func__ << ": " << query.lastError();
} else {
for (int r = 0; query.next(); r++) {
fileNameList.append(
query.value(QStringLiteral("note_file_name")).toString());
// always keep in mind that note_file_name is no file name,
// but the base name (so "my-note", instead of "my-note.md")
const QString &name = query.value(
QStringLiteral("note_file_name")).toString();
const QString &noteSubFolderPathData = query.value(
QStringLiteral("note_sub_folder_path")).toString();
const Note &note = Note::fetchByName(name, noteSubFolderPathData);

noteIdList.append(note.getId());
}
}

DatabaseService::closeDatabaseConnection(db, query);

return fileNameList;
return noteIdList;
}

/**
* Fetches all linked note file names for a given subfolder
* Fetches all linked note ids for a given subfolder
*/

QStringList Tag::fetchAllLinkedNoteFileNamesForFolder(
QVector<int> Tag::fetchAllLinkedNoteIdsForFolder(
const NoteSubFolder &noteSubFolder, bool fromAllSubfolders) const {
QSqlDatabase db = DatabaseService::getNoteFolderDatabase();
QSqlQuery query(db);
QStringList fileNameList;
QVector<int> noteIdList;

if (fromAllSubfolders) {
// 'All notes' selected in note subfolder panel
query.prepare(QStringLiteral(
"SELECT note_file_name FROM noteTagLink WHERE tag_id = :id"));
"SELECT note_file_name, note_sub_folder_path "
"FROM noteTagLink WHERE tag_id = :id"));
} else {
query.prepare(QStringLiteral(
"SELECT note_file_name FROM noteTagLink WHERE tag_id = :id "
"SELECT note_file_name, note_sub_folder_path "
"FROM noteTagLink WHERE tag_id = :id "
"AND note_sub_folder_path LIKE :noteSubFolderPath"));
query.bindValue(QStringLiteral(":noteSubFolderPath"),
noteSubFolder.relativePath() + "%");
Expand All @@ -640,14 +648,21 @@ QStringList Tag::fetchAllLinkedNoteFileNamesForFolder(
qWarning() << __func__ << ": " << query.lastError();
} else {
for (int r = 0; query.next(); r++) {
fileNameList.append(
query.value(QStringLiteral("note_file_name")).toString());
// always keep in mind that note_file_name is no file name,
// but the base name (so "my-note", instead of "my-note.md")
const QString &name = query.value(
QStringLiteral("note_file_name")).toString();
const QString &noteSubFolderPathData = query.value(
QStringLiteral("note_sub_folder_path")).toString();
const Note &note = Note::fetchByName(name, noteSubFolderPathData);

noteIdList.append(note.getId());
}
}

DatabaseService::closeDatabaseConnection(db, query);

return fileNameList;
return noteIdList;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/entities/tag.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ class Tag {

bool removeLinkToNote(const Note &note) const;

QStringList fetchAllLinkedNoteFileNames(const bool fromAllSubfolders) const;
QVector<int> fetchAllLinkedNoteIds(const bool fromAllSubfolders) const;

QStringList fetchAllLinkedNoteFileNamesForFolder(
QVector<int> fetchAllLinkedNoteIdsForFolder(
const NoteSubFolder &noteSubFolder, bool fromAllSubfolders) const;

QList<Note> fetchAllLinkedNotes() const;
Expand Down
14 changes: 7 additions & 7 deletions src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5638,15 +5638,15 @@ void MainWindow::filterNotesByTag() {
}

const int tagId = Tag::activeTagId();
QStringList fileNameList;
QVector<int> noteIdList;

switch (tagId) {
case Tag::AllNotesId:
// don't do any additional filtering here
return;
case Tag::AllUntaggedNotesId:
// get all note names that are not tagged
fileNameList = Note::fetchAllNotTaggedNames();
noteIdList = Note::fetchAllNotTaggedIds();
break;
default:
// check for multiple active;
Expand Down Expand Up @@ -5697,20 +5697,20 @@ void MainWindow::filterNotesByTag() {
for (const QTreeWidgetItem *i : selectedFolderItems) {
const int id = i->data(0, Qt::UserRole).toInt();
const NoteSubFolder folder = NoteSubFolder::fetch(id);
fileNameList
<< tag.fetchAllLinkedNoteFileNamesForFolder(

noteIdList << tag.fetchAllLinkedNoteIdsForFolder(
folder, _showNotesFromAllNoteSubFolders);
}
} else {
fileNameList << tag.fetchAllLinkedNoteFileNames(
noteIdList << tag.fetchAllLinkedNoteIds(
_showNotesFromAllNoteSubFolders);
}
}

break;
}

qDebug() << __func__ << " - 'fileNameList': " << fileNameList;
qDebug() << __func__ << " - 'noteIdList': " << noteIdList;

// omit the already hidden notes
QTreeWidgetItemIterator it(ui->noteTreeWidget,
Expand All @@ -5727,7 +5727,7 @@ void MainWindow::filterNotesByTag() {
// note subfolder are not taken into account here (note names are now
// not unique), but it should be ok because they are filtered by
// filterNotesByNoteSubFolders
if (!fileNameList.contains((*it)->text(0))) {
if (!noteIdList.contains((*it)->data(0, Qt::UserRole).toInt())) {
(*it)->setHidden(true);
}

Expand Down

0 comments on commit 92981d4

Please sign in to comment.