Baloo Widgets

tagsfileitemaction.cpp
1 /*
2  * Copyright (C) 2018 Nicolas Fella <[email protected]>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) version 3, or any
8  * later version accepted by the membership of KDE e.V. (or its
9  * successor approved by the membership of KDE e.V.), which shall
10  * act as a proxy defined in Section 6 of version 3 of the license.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #include "tagsfileitemaction.h"
23 
24 #include <QList>
25 #include <QAction>
26 #include <QFileInfo>
27 #include <QWidget>
28 #include <QVariantList>
29 #include <QUrl>
30 #include <QIcon>
31 #include <QMenu>
32 #include <QInputDialog>
33 
34 #include <KPluginFactory>
35 #include <KLocalizedString>
36 
37 K_PLUGIN_FACTORY_WITH_JSON(TagsFileItemActionFactory, "tagsfileitemaction.json", registerPlugin<TagsFileItemAction>();)
38 
39 TagsFileItemAction::TagsFileItemAction(QObject* parent, const QVariantList&)
41  , m_tagsLister()
42 {
43  m_menu = new QMenu(i18n("Assign Tags"));
44  m_menu->setIcon(QIcon::fromTheme(QStringLiteral("tag")));
45 
46  connect(&m_tagsLister, &KCoreDirLister::itemsAdded, this, [this](const QUrl&, const KFileItemList& items) {
47  const QStringList fileTags = m_metaData->tags();
48 
49  // The file may be located outside an indexed path, or is not indexed yet
50  // Show the complete tag list, i.e. the union of file and index DB tags
51  QStringList allTags;
52  allTags.reserve(fileTags.size() + items.size());
53  allTags.append(fileTags);
54  for (const KFileItem &item: items) {
55  allTags.append(item.name());
56  }
57  allTags.sort(Qt::CaseInsensitive);
58  allTags.removeDuplicates();
59 
60  for (const QString name : qAsConst(allTags)) {
61  QAction* action = m_menu->addAction(QIcon::fromTheme(QStringLiteral("tag")), name);
62  action->setCheckable(true);
63  action->setChecked(fileTags.contains(name));
64 
65  connect(action, &QAction::triggered, this, [this, name](bool isChecked) {
66  QStringList newTags = m_metaData->tags();
67  if (isChecked) {
68  newTags.append(name);
69  } else {
70  newTags.removeAll(name);
71  }
72  m_metaData->setTags(newTags);
73  });
74  }
75  });
76 
77  newAction = new QAction(i18n("Create New..."));
78  newAction->setIcon(QIcon::fromTheme(QStringLiteral("tag-new")));
79 
80  connect(newAction, &QAction::triggered, this, [this] {
81  QString newTag = QInputDialog::getText(m_menu, i18n("New tag"), i18n("New tag:"), QLineEdit::Normal);
82  QStringList tags = m_metaData->tags();
83  if (!tags.contains(newTag)) {
84  tags.append(newTag);
85  m_metaData->setTags(tags);
86  }
87  });
88 }
89 
90 TagsFileItemAction::~TagsFileItemAction()
91 {
92  delete m_metaData;
93 }
94 
95 QList<QAction*> TagsFileItemAction::actions(const KFileItemListProperties& fileItemInfos, QWidget* parentWidget)
96 {
97 
98  if (fileItemInfos.urlList().size() > 1) {
99  return {};
100  }
101 
102  QString filePath = fileItemInfos.urlList()[0].toLocalFile();
103  if (!QFileInfo(filePath).isWritable()) {
104  return {};
105  }
106 
107  m_metaData = new KFileMetaData::UserMetaData(filePath);
108  if (!m_metaData->isSupported()) {
109  return {};
110  }
111 
112  m_tagsLister.openUrl(QUrl(QStringLiteral("tags:/")), KCoreDirLister::OpenUrlFlag::Reload);
113 
114  m_menu->clear();
115  m_menu->addAction(newAction);
116  m_menu->addSeparator();
117  m_menu->setParent(parentWidget, Qt::Popup);
118 
119  return {m_menu->menuAction()};
120 }
121 
122 #include "tagsfileitemaction.moc"
#define K_PLUGIN_FACTORY_WITH_JSON(name, jsonFile, pluginRegistrations)
void triggered(bool checked)
void reserve(int alloc)
void setChecked(bool)
int removeDuplicates()
bool contains(const QString &str, Qt::CaseSensitivity cs) const const
int size() const const
void append(const T &value)
CaseInsensitive
int removeAll(const T &value)
void itemsAdded(const QUrl &directoryUrl, const KFileItemList &items)
QString getText(QWidget *parent, const QString &title, const QString &label, QLineEdit::EchoMode mode, const QString &text, bool *ok, Qt::WindowFlags flags, Qt::InputMethodHints inputMethodHints)
QList< QUrl > urlList() const
void setCheckable(bool)
QString i18n(const char *text, const TYPE &arg...)
void sort(Qt::CaseSensitivity cs)
QIcon fromTheme(const QString &name)
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Thu Oct 21 2021 23:07:11 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.