diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,6 +1,7 @@ add_definitions(-DTRANSLATION_DOMAIN=\"baloowidgets5\") add_subdirectory(filepropertiesplugin) +add_subdirectory(tagsfileitemactionplugin) set(widgets_SRCS kblocklayout.cpp diff --git a/src/tagsfileitemactionplugin/CMakeLists.txt b/src/tagsfileitemactionplugin/CMakeLists.txt new file mode 100644 --- /dev/null +++ b/src/tagsfileitemactionplugin/CMakeLists.txt @@ -0,0 +1,15 @@ +include_directories(${CMAKE_SOURCE_DIR}) + +set(tagsfileitemaction_debug_SRCS tagsfileitemaction.cpp) + +kcoreaddons_add_plugin(tagsfileitemaction + SOURCES ${tagsfileitemaction_debug_SRCS} + JSON tagsfileitemaction.json + INSTALL_NAMESPACE "kf5/kfileitemaction") + +target_link_libraries(tagsfileitemaction + KF5BalooWidgets + KF5::KIOWidgets + KF5::I18n + KF5::FileMetaData +) diff --git a/src/tagsfileitemactionplugin/Messages.sh b/src/tagsfileitemactionplugin/Messages.sh new file mode 100644 --- /dev/null +++ b/src/tagsfileitemactionplugin/Messages.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env bash + +$XGETTEXT `find . -name '*.cpp'` -o $podir/baloowidgets-tagsfileitemaction.pot diff --git a/src/tagsfileitemactionplugin/tagsfileitemaction.h b/src/tagsfileitemactionplugin/tagsfileitemaction.h new file mode 100644 --- /dev/null +++ b/src/tagsfileitemactionplugin/tagsfileitemaction.h @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2018 Nicolas Fella + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) version 3, or any + * later version accepted by the membership of KDE e.V. (or its + * successor approved by the membership of KDE e.V.), which shall + * act as a proxy defined in Section 6 of version 3 of the license. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see . + * + */ + +#ifndef TAGSFILEITEMACTION_H +#define TAGSFILEITEMACTION_H + +#include +#include +#include +#include + +class QAction; +class KFileItemListProperties; +class QWidget; + +class TagsFileItemAction : public KAbstractFileItemActionPlugin +{ +Q_OBJECT +public: + TagsFileItemAction(QObject* parent, const QVariantList& args); + virtual ~TagsFileItemAction(); + QList actions(const KFileItemListProperties& fileItemInfos, QWidget* parentWidget) override; + +private: + KFileMetaData::UserMetaData* m_metaData; + KCoreDirLister m_tagsLister; + QMenu* m_menu; +}; + +#endif // TAGSFILEITEMACTION_H diff --git a/src/tagsfileitemactionplugin/tagsfileitemaction.cpp b/src/tagsfileitemactionplugin/tagsfileitemaction.cpp new file mode 100644 --- /dev/null +++ b/src/tagsfileitemactionplugin/tagsfileitemaction.cpp @@ -0,0 +1,112 @@ +/* + * Copyright (C) 2018 Nicolas Fella + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) version 3, or any + * later version accepted by the membership of KDE e.V. (or its + * successor approved by the membership of KDE e.V.), which shall + * act as a proxy defined in Section 6 of version 3 of the license. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see . + * + */ + +#include "tagsfileitemaction.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +K_PLUGIN_FACTORY_WITH_JSON(TagsFileItemActionFactory, "tagsfileitemaction.json", registerPlugin();) + +TagsFileItemAction::TagsFileItemAction(QObject* parent, const QVariantList&) + : KAbstractFileItemActionPlugin(parent) + , m_tagsLister() +{ + m_menu = new QMenu(i18n("Tags")); + m_menu->setIcon(QIcon::fromTheme(QStringLiteral("tag"))); + + connect(&m_tagsLister, &KCoreDirLister::itemsAdded, this, [this](const QUrl&, const KFileItemList& items) { + + + for (const KFileItem &item: items) { + const QString name = item.name(); + + QAction* action = m_menu->addAction(QIcon::fromTheme(QStringLiteral("tag")), name); + action->setCheckable(true); + action->setChecked(m_metaData->tags().contains(name)); + + connect(action, &QAction::triggered, this, [this, action] { + if (action->isChecked()) { + QStringList newTags = m_metaData->tags(); + // HACK the first character of QAction::text is '&' + newTags.append(action->text().remove(0,1)); + m_metaData->setTags(newTags); + } else { + QStringList newTags = m_metaData->tags(); + // HACK the first character of QAction::text is '&' + newTags.removeAll(action->text().remove(0,1)); + m_metaData->setTags(newTags); + } + }); + + } + }); + + QAction* newAction = new QAction(i18n("Create new")); + newAction->setIcon(QIcon::fromTheme(QStringLiteral("tag"))); + + connect(newAction, &QAction::triggered, this, [this] { + QString newTag = QInputDialog::getText(m_menu, i18n("New tag"), i18n("New tag:"), QLineEdit::Normal); + QStringList tags = m_metaData->tags(); + tags.append(newTag); + m_metaData->setTags(tags); + }); + + m_menu->addAction(newAction); + m_menu->addSeparator(); + + m_tagsLister.openUrl(QUrl("tags:/"), KCoreDirLister::OpenUrlFlag::Reload); + + +} + +TagsFileItemAction::~TagsFileItemAction() +{ + delete m_metaData; +} + + +QList TagsFileItemAction::actions(const KFileItemListProperties& fileItemInfos, QWidget* parentWidget) +{ + Q_UNUSED(parentWidget); + + if (fileItemInfos.urlList().size() > 1) { + return {}; + } + + m_metaData = new KFileMetaData::UserMetaData(fileItemInfos.urlList()[0].toLocalFile()); + + return {m_menu->menuAction()}; + +} + + +#include "tagsfileitemaction.moc" diff --git a/src/tagsfileitemactionplugin/tagsfileitemaction.json b/src/tagsfileitemactionplugin/tagsfileitemaction.json new file mode 100644 --- /dev/null +++ b/src/tagsfileitemactionplugin/tagsfileitemaction.json @@ -0,0 +1,20 @@ +{ + "KPlugin": { + "Authors": [ + { + "Name": "Nicolas Fella" + } + ], + "Category": "Utilities", + "Description": "Change file tags", + "Icon": "tag", + "License": "GPL", + "MimeTypes": [ + "application/octet-stream" + ], + "Name": "Tags", + "ServiceTypes": [ + "KFileItemAction/Plugin" + ] + } +}