diff --git a/src/filewidgets/kfileplacesitem.cpp b/src/filewidgets/kfileplacesitem.cpp --- a/src/filewidgets/kfileplacesitem.cpp +++ b/src/filewidgets/kfileplacesitem.cpp @@ -121,6 +121,9 @@ case KFilePlacesModel::RemovableDevicesType: m_groupName = i18nc("@item", "Removable Devices"); break; + case KFilePlacesModel::TagsType: + m_groupName = i18nc("@item", "Tags"); + break; default: Q_UNREACHABLE(); break; @@ -162,6 +165,10 @@ return KFilePlacesModel::DevicesType; } + if (protocol == QLatin1String("tags")) { + return KFilePlacesModel::TagsType; + } + if (protocol == QLatin1String("remote") || KProtocolInfo::protocolClass(protocol) != QLatin1String(":local")) { return KFilePlacesModel::RemoteType; diff --git a/src/filewidgets/kfileplacesitem_p.h b/src/filewidgets/kfileplacesitem_p.h --- a/src/filewidgets/kfileplacesitem_p.h +++ b/src/filewidgets/kfileplacesitem_p.h @@ -51,7 +51,8 @@ RecentlySavedType, SearchForType, DevicesType, - RemovableDevicesType + RemovableDevicesType, + TagsType }; KFilePlacesItem(KBookmarkManager *manager, diff --git a/src/filewidgets/kfileplacesmodel.h b/src/filewidgets/kfileplacesmodel.h --- a/src/filewidgets/kfileplacesmodel.h +++ b/src/filewidgets/kfileplacesmodel.h @@ -62,7 +62,8 @@ SearchForType, DevicesType, RemovableDevicesType, - UnknownType + UnknownType, + TagsType }; explicit KFilePlacesModel(QObject *parent = nullptr); diff --git a/src/filewidgets/kfileplacesmodel.cpp b/src/filewidgets/kfileplacesmodel.cpp --- a/src/filewidgets/kfileplacesmodel.cpp +++ b/src/filewidgets/kfileplacesmodel.cpp @@ -50,6 +50,7 @@ #include #include +#include #include #include @@ -80,6 +81,8 @@ return QStringLiteral("GroupState-Devices-IsHidden"); case KFilePlacesModel::RemovableDevicesType: return QStringLiteral("GroupState-RemovableDevices-IsHidden"); + case KFilePlacesModel::TagsType: + return QStringLiteral("GroupState-Tags-IsHidden"); default: Q_UNREACHABLE(); } @@ -164,8 +167,54 @@ explicit Private(KFilePlacesModel *self) : q(self), bookmarkManager(nullptr), - fileIndexingEnabled(isFileIndexingEnabled()) + fileIndexingEnabled(isFileIndexingEnabled()), + tags(), + tagsLister(new KCoreDirLister()) { + if (KProtocolInfo::isKnownProtocol(QStringLiteral("tags"))) { + connect(tagsLister, &KCoreDirLister::itemsAdded, q, [this](const QUrl& directoryUrl, const KFileItemList& items) { + + Q_UNUSED(directoryUrl); + const int maxTags = 8; + + QList existingBookmarks; + + KBookmarkGroup root = bookmarkManager->root(); + KBookmark bookmark = root.first(); + QVector devices = availableDevices; + + while (!bookmark.isNull()) { + QUrl url = bookmark.url(); + + existingBookmarks.append(url); + + bookmark = root.next(bookmark); + } + + if (tags.isEmpty() && !existingBookmarks.contains(QUrl(tagsUrlBase))) { + KFilePlacesItem::createSystemBookmark(bookmarkManager, + QStringLiteral("All tags"), I18N_NOOP2("KFile System Bookmarks", "All tags"), + QUrl(tagsUrlBase), QStringLiteral("tag")); + } + + for (const KFileItem &item: items) { + const QString name = item.name(); + + if (!tags.contains(name) && tags.size() < maxTags) { + tags.append(name); + if (!existingBookmarks.contains(QUrl(item.url()))) { + KFilePlacesItem::createSystemBookmark(bookmarkManager,name, name, QUrl(item.url()), QStringLiteral("tag")); + } + } + } + + bookmarkManager->save(); + + _k_reloadBookmarks(); + }); + + tagsLister->openUrl(QUrl(tagsUrlBase), KCoreDirLister::OpenUrlFlag::Reload); + } } ~Private() @@ -191,6 +240,10 @@ QList loadBookmarkList(); int findNearestPosition(int source, int target); + QList tags; + const QString tagsUrlBase = QStringLiteral("tags:/"); + KCoreDirLister* tagsLister; + void _k_initDeviceList(); void _k_deviceAdded(const QString &udi); void _k_deviceRemoved(const QString &udi); @@ -270,6 +323,7 @@ setDefaultMetadataItemForGroup(RemoteType); setDefaultMetadataItemForGroup(DevicesType); setDefaultMetadataItemForGroup(RemovableDevicesType); + setDefaultMetadataItemForGroup(TagsType); // Force bookmarks to be saved. If on open/save dialog and the bookmarks are not saved, QFile::exists // will always return false, which opening/closing all the time the open/save dialog would case the @@ -1054,7 +1108,7 @@ if (isGroupHidden(type) == hidden) return; - d->bookmarkManager->root().setMetaDataItem(stateNameForGroupType(type), (hidden ? QStringLiteral("true") : QStringLiteral("false"))); + d->bookmarkManager->root().setMetaDataItem(stateNameForGroupType(type), (hidden ? QStringLiteral("true") : QStringLiteral("false"))); d->reloadAndSignal(); emit groupHiddenChanged(type, hidden); }