Baloo Widgets

tagsfileitemaction.cpp
1 /*
2  SPDX-FileCopyrightText: 2018 Nicolas Fella <[email protected]>
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 
22 K_PLUGIN_FACTORY_WITH_JSON(TagsFileItemActionFactory, "tagsfileitemaction.json", registerPlugin<TagsFileItemAction>();)
23 
24 TagsFileItemAction::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  }
42  allTags.sort(Qt::CaseInsensitive);
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 
75 TagsFileItemAction::~TagsFileItemAction()
76 {
77  delete m_metaData;
78 }
79 
80 QList<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"
void append(const T &value)
CaseInsensitive
QList< QUrl > urlList() const
int removeAll(const T &value)
bool contains(const QString &str, Qt::CaseSensitivity cs) const const
QIcon fromTheme(const QString &name)
void itemsAdded(const QUrl &directoryUrl, const KFileItemList &items)
void reserve(int alloc)
int size() const const
QString i18n(const char *text, const TYPE &arg...)
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 setCheckable(bool)
#define K_PLUGIN_FACTORY_WITH_JSON(name, jsonFile, pluginRegistrations)
int removeDuplicates()
void triggered(bool checked)
const T & constFirst() const const
void setChecked(bool)
void sort(Qt::CaseSensitivity cs)
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Fri Aug 19 2022 04:12:56 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.