diff --git a/CMakeLists.txt b/CMakeLists.txt --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -90,6 +90,7 @@ add_subdirectory (kded) add_subdirectory (plasma) add_subdirectory (icons) +add_subdirectory (fileitemplugin) # Write out the features feature_summary (WHAT ALL FATAL_ON_MISSING_REQUIRED_PACKAGES) diff --git a/fileitemplugin/CMakeLists.txt b/fileitemplugin/CMakeLists.txt new file mode 100644 --- /dev/null +++ b/fileitemplugin/CMakeLists.txt @@ -0,0 +1,10 @@ + +kcoreaddons_add_plugin( + plasmavaultfileitemaction + SOURCES plasmavaultfileitemaction.cpp + JSON plasmavaultfileitemaction.json + INSTALL_NAMESPACE "kf5/kfileitemaction") + +target_link_libraries(plasmavaultfileitemaction + KF5::I18n + KF5::KIOWidgets) diff --git a/fileitemplugin/plasmavaultfileitemaction.h b/fileitemplugin/plasmavaultfileitemaction.h new file mode 100644 --- /dev/null +++ b/fileitemplugin/plasmavaultfileitemaction.h @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2019 Ivan Cukic + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#ifndef PLASMAVAULTFILEITEMACTION_H +#define PLASMAVAULTFILEITEMACTION_H + +#include +#include + +class QAction; +class QWidget; + +class KFileItemListProperties; + +class PlasmaVaultFileItemAction : public KAbstractFileItemActionPlugin +{ + +Q_OBJECT + +public: + PlasmaVaultFileItemAction(QObject* parent, const QVariantList& args); + + QList actions(const KFileItemListProperties& fileItemInfos, + QWidget* parentWidget) override; +}; + +#endif diff --git a/fileitemplugin/plasmavaultfileitemaction.cpp b/fileitemplugin/plasmavaultfileitemaction.cpp new file mode 100644 --- /dev/null +++ b/fileitemplugin/plasmavaultfileitemaction.cpp @@ -0,0 +1,98 @@ +/* + * Copyright (C) 2019 Ivan Cukic + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#include "plasmavaultfileitemaction.h" + +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +K_PLUGIN_CLASS_WITH_JSON(PlasmaVaultFileItemAction, "plasmavaultfileitemaction.json") + +PlasmaVaultFileItemAction::PlasmaVaultFileItemAction(QObject* parent, const QVariantList&) + : KAbstractFileItemActionPlugin(parent) +{} + +QList PlasmaVaultFileItemAction::actions(const KFileItemListProperties& fileItemInfos, QWidget* parentWidget) +{ + if (fileItemInfos.urlList().size() != 1 || + !fileItemInfos.isDirectory()) return {}; + + QList actions; + const QIcon icon = QIcon::fromTheme(QStringLiteral("plasmavault")); + + auto fileItem = fileItemInfos.urlList()[0].toLocalFile(); + + auto createAction = [this] (const QIcon& icon, const QString& name, + QString command, QString device, + QWidget* parentWidget) + { + QAction *action = new QAction(icon, name, parentWidget); + + connect(action, &QAction::triggered, this, [this,command,device]() { + auto method = QDBusMessage::createMethodCall( + "org.kde.kded5", + "/modules/plasmavault", + "org.kde.plasmavault", + command); + method.setArguments({ device }); + + QDBusConnection::sessionBus().call(method); + }); + + return action; + }; + + KConfig config("plasmavaultrc"); + for (auto group: config.groupList()) { + auto mountPoint = config.entryMap(group)["mountPoint"]; + if (mountPoint == fileItem) { + const auto currentMounts = KMountPoint::currentMountPoints(); + + const bool mounted = std::any_of(currentMounts.begin(), currentMounts.end(), + [mountPoint] (const KMountPoint::Ptr& mount) { + return mount->mountPoint() == mountPoint; + }); + + const QString command = mounted ? "closeVault" : "openVault"; + const QString title = mounted ? + i18nc("@action Action to unmount a vault", "Close this Plasma Vault") : + i18nc("@action Action to mount a vault", "Open this Plasma Vault"); + + return { + createAction(icon, + title, + command, + group, + parentWidget) + }; + } + } + + return {}; +} + +#include "plasmavaultfileitemaction.moc" diff --git a/fileitemplugin/plasmavaultfileitemaction.json b/fileitemplugin/plasmavaultfileitemaction.json new file mode 100644 --- /dev/null +++ b/fileitemplugin/plasmavaultfileitemaction.json @@ -0,0 +1,13 @@ +{ + "KPlugin": { + "Icon": "plasmavault", + "MimeTypes": [ + "inode/directory" + ], + "Name": "Plasma Vault mount and unmount", + "ServiceTypes": [ + "KFileItemAction/Plugin" + ] + }, + "MimeType": "application/octet-stream;" +}