aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorAqua-sama <aqua@iserlohn-fortress.net>2018-09-25 17:44:35 +0200
committerAqua-sama <aqua@iserlohn-fortress.net>2018-09-25 17:44:35 +0200
commita43c6108de52ca6a92866959a5fe2b0077d0d556 (patch)
tree156274f19a924f77abb8948d5d62a28b6119681f /lib
parentBookmarks: add tags and description fields to xbel (diff)
downloadsmolbote-a43c6108de52ca6a92866959a5fe2b0077d0d556.tar.xz
Bookmarks: add search by tag
- Tags are any words starting with ':'
Diffstat (limited to 'lib')
-rw-r--r--lib/bookmarks/bookmarkmodel.cpp51
1 files changed, 44 insertions, 7 deletions
diff --git a/lib/bookmarks/bookmarkmodel.cpp b/lib/bookmarks/bookmarkmodel.cpp
index 78e42b8..6e99331 100644
--- a/lib/bookmarks/bookmarkmodel.cpp
+++ b/lib/bookmarks/bookmarkmodel.cpp
@@ -11,6 +11,7 @@
#include "xbel.h"
#include <QBuffer>
#include <QMimeData>
+#include <QRegularExpression>
BookmarkModel::BookmarkModel(QObject *parent)
: QAbstractItemModel(parent)
@@ -166,23 +167,59 @@ QModelIndex BookmarkModel::parent(const QModelIndex &index) const
return createIndex(parentItem->row(), 0, parentItem);
}
-inline QStringList searchThrough(const QString &term, BookmarkItem *item)
+inline bool has(const QStringList &terms, const QStringList &where)
+{
+ for(const QString &term : terms) {
+ if(where.contains(term))
+ return true;
+ }
+ return false;
+}
+
+inline QStringList searchThrough(const QString &term, const QStringList &tags, BookmarkItem *item)
{
QStringList results;
+
for(int i = 0; i < item->childCount(); ++i) {
auto *child = item->child(i);
- if(child->type() == BookmarkItem::Bookmark && child->data(BookmarkItem::Href).toString().contains(term))
- results.append(child->data(BookmarkItem::Href).toString());
- else if(child->type() == BookmarkItem::Folder)
- results.append(searchThrough(term, child));
+
+ if(child->type() == BookmarkItem::Bookmark) {
+ if((!term.isEmpty() && child->data(BookmarkItem::Href).toString().contains(term)) || has(tags, child->data(BookmarkItem::Tags).toStringList()))
+ results.append(child->data(BookmarkItem::Href).toString());
+ }
+
+ else if(child->type() == BookmarkItem::Folder) {
+ if(has(tags, child->data(BookmarkItem::Tags).toStringList())) {
+
+ // append all bookmarks
+ for(int i = 0; i < child->childCount(); ++i) {
+ auto *subChild = child->child(i);
+ if(subChild->type() == BookmarkItem::Bookmark)
+ results.append(subChild->data(BookmarkItem::Href).toString());
+ }
+ }
+ results.append(searchThrough(term, tags, child));
+ }
}
return results;
}
QStringList BookmarkModel::search(const QString &term) const
{
- // TODO tag searching
- return searchThrough(term, rootItem);
+ QString searchTerm = term;
+ QStringList tags;
+
+ const QRegularExpression tagRE(":\\w+\\s?", QRegularExpression::CaseInsensitiveOption);
+ auto i = tagRE.globalMatch(term);
+ while(i.hasNext()) {
+ auto match = i.next();
+ QString tag = match.captured();
+ searchTerm.remove(tag);
+ tag = tag.remove(0, 1).trimmed();
+ tags.append(tag);
+ }
+
+ return searchThrough(searchTerm, tags, rootItem);
}
BookmarkItem *BookmarkModel::getItem(const QModelIndex &index) const