Baloo Widgets

tagsfileitemaction.cpp
1/*
2 SPDX-FileCopyrightText: 2018 Nicolas Fella <nicolas.fella@gmx.de>
3
4 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5*/
6
7#include "tagsfileitemaction.h"
8
9#include <QAction>
10#include <QFileInfo>
11#include <QIcon>
12#include <QInputDialog>
13#include <QList>
14#include <QMenu>
15#include <QUrl>
16#include <QVariantList>
17#include <QWidget>
18
19#include <KLocalizedString>
20#include <KPluginFactory>
21
22K_PLUGIN_FACTORY_WITH_JSON(TagsFileItemActionFactory, "tagsfileitemaction.json", registerPlugin<TagsFileItemAction>();)
23
24TagsFileItemAction::TagsFileItemAction(QObject *parent, const QVariantList &)
26 , m_tagsLister()
27{
28 m_menu = new QMenu(i18n("Assign Tags"));
29 m_menu->setIcon(QIcon::fromTheme(QStringLiteral("tag")));
30
31 connect(&m_tagsLister, &KCoreDirLister::itemsAdded, this, [this](const QUrl &, const KFileItemList &items) {
32 const QStringList fileTags = m_metaData->tags();
33
34 // The file may be located outside an indexed path, or is not indexed yet
35 // Show the complete tag list, i.e. the union of file and index DB tags
36 QStringList allTags;
37 allTags.reserve(fileTags.size() + items.size());
38 allTags.append(fileTags);
39 for (const KFileItem &item : items) {
40 allTags.append(item.name());
41 }
43 allTags.removeDuplicates();
44
45 for (const QString &name : std::as_const(allTags)) {
46 QAction *action = m_menu->addAction(QIcon::fromTheme(QStringLiteral("tag")), name);
47 action->setCheckable(true);
48 action->setChecked(fileTags.contains(name));
49
50 connect(action, &QAction::triggered, this, [this, name](bool isChecked) {
51 QStringList newTags = m_metaData->tags();
52 if (isChecked) {
53 newTags.append(name);
54 } else {
55 newTags.removeAll(name);
56 }
57 m_metaData->setTags(newTags);
58 });
59 }
60 });
61
62 newAction = new QAction(i18n("Create New..."));
63 newAction->setIcon(QIcon::fromTheme(QStringLiteral("tag-new")));
64
65 connect(newAction, &QAction::triggered, this, [this] {
66 QString newTag = QInputDialog::getText(m_menu, i18n("New tag"), i18n("New tag:"), QLineEdit::Normal);
67 QStringList tags = m_metaData->tags();
68 if (!tags.contains(newTag)) {
69 tags.append(newTag);
70 m_metaData->setTags(tags);
71 }
72 });
73}
74
75TagsFileItemAction::~TagsFileItemAction()
76{
77 delete m_metaData;
78}
79
80QList<QAction *> TagsFileItemAction::actions(const KFileItemListProperties &fileItemInfos, QWidget *parentWidget)
81{
82 if (fileItemInfos.urlList().size() > 1) {
83 return {};
84 }
85
86 const QString filePath = fileItemInfos.urlList().constFirst().toLocalFile();
87 if (!QFileInfo(filePath).isWritable()) {
88 return {};
89 }
90
91 m_metaData = new KFileMetaData::UserMetaData(filePath);
92 if (!m_metaData->isSupported()) {
93 return {};
94 }
95
96 m_tagsLister.openUrl(QUrl(QStringLiteral("tags:/")), KCoreDirLister::OpenUrlFlag::Reload);
97
98 m_menu->clear();
99 m_menu->addAction(newAction);
100 m_menu->addSeparator();
101 m_menu->setParent(parentWidget, Qt::Popup);
102
103 return {m_menu->menuAction()};
104}
105
106#include "tagsfileitemaction.moc"
107
108#include "moc_tagsfileitemaction.cpp"
bool openUrl(const QUrl &dirUrl, OpenUrlFlags flags=NoFlags)
void itemsAdded(const QUrl &directoryUrl, const KFileItemList &items)
QList< QUrl > urlList() const
#define K_PLUGIN_FACTORY_WITH_JSON(name, jsonFile, pluginRegistrations)
QString i18n(const char *text, const TYPE &arg...)
void setCheckable(bool)
void setChecked(bool)
void triggered(bool checked)
QIcon fromTheme(const QString &name)
QString getText(QWidget *parent, const QString &title, const QString &label, QLineEdit::EchoMode mode, const QString &text, bool *ok, Qt::WindowFlags flags, Qt::InputMethodHints inputMethodHints)
void append(QList< T > &&value)
qsizetype removeAll(const AT &t)
void reserve(qsizetype size)
qsizetype size() const const
QAction * addAction(const QIcon &icon, const QString &text, Functor functor, const QKeySequence &shortcut)
QAction * addSeparator()
void clear()
QAction * menuAction() const const
bool contains(QLatin1StringView str, Qt::CaseSensitivity cs) const const
qsizetype removeDuplicates()
void sort(Qt::CaseSensitivity cs)
CaseInsensitive
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
void setParent(QWidget *parent)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:20:22 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.