File indexing completed on 2024-04-21 16:20:42

0001 /*
0002  * Copyright (C) 2019 Ivan Cukic <ivan.cukic@kde.org>
0003  *
0004  * This program is free software; you can redistribute it and/or
0005  * modify it under the terms of the GNU General Public License
0006  * as published by the Free Software Foundation; either version 2
0007  * of the License, or (at your option) any later version.
0008  *
0009  * This program is distributed in the hope that it will be useful,
0010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0012  * GNU General Public License for more details.
0013  *
0014  * You should have received a copy of the GNU General Public License
0015  * along with this program; if not, write to the Free Software
0016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
0017  *
0018  */
0019 
0020 #include "plasmavaultfileitemaction.h"
0021 
0022 #include <QAction>
0023 #include <QDBusInterface>
0024 #include <QDBusPendingCall>
0025 #include <QIcon>
0026 
0027 #include <KConfig>
0028 #include <KLocalizedString>
0029 #include <KMountPoint>
0030 #include <KPluginFactory>
0031 
0032 K_PLUGIN_CLASS_WITH_JSON(PlasmaVaultFileItemAction, "plasmavaultfileitemaction.json")
0033 
0034 PlasmaVaultFileItemAction::PlasmaVaultFileItemAction(QObject *parent, const QVariantList &)
0035     : KAbstractFileItemActionPlugin(parent)
0036 {
0037 }
0038 
0039 QList<QAction *> PlasmaVaultFileItemAction::actions(const KFileItemListProperties &fileItemInfos, QWidget *parentWidget)
0040 {
0041     if (fileItemInfos.urlList().size() != 1 || !fileItemInfos.isDirectory() || !fileItemInfos.isLocal())
0042         return {};
0043 
0044     QList<QAction *> actions;
0045     const QIcon icon = QIcon::fromTheme(QStringLiteral("plasmavault"));
0046 
0047     auto fileItem = fileItemInfos.urlList()[0].toLocalFile();
0048 
0049     auto createAction = [this](const QIcon &icon, const QString &name, QString command, QString device, QWidget *parentWidget) {
0050         QAction *action = new QAction(icon, name, this);
0051 
0052         connect(action, &QAction::triggered, this, [command, device]() {
0053             auto method = QDBusMessage::createMethodCall("org.kde.kded5", "/modules/plasmavault", "org.kde.plasmavault", command);
0054             method.setArguments({device});
0055 
0056             QDBusConnection::sessionBus().call(method, QDBus::NoBlock);
0057         });
0058 
0059         return action;
0060     };
0061 
0062     KConfig config("plasmavaultrc");
0063     for (auto group : config.groupList()) {
0064         auto mountPoint = config.entryMap(group)["mountPoint"];
0065         if (mountPoint == fileItem) {
0066             const auto currentMounts = KMountPoint::currentMountPoints();
0067 
0068             const bool mounted = std::any_of(currentMounts.begin(), currentMounts.end(), [mountPoint](const KMountPoint::Ptr &mount) {
0069                 return mount->mountPoint() == mountPoint;
0070             });
0071 
0072             const QString command = mounted ? "closeVault" : "openVault";
0073             const QString title = mounted ? i18nc("@action Action to unmount a vault", "Close this Plasma Vault")
0074                                           : i18nc("@action Action to mount a vault", "Open this Plasma Vault");
0075 
0076             return {createAction(icon, title, command, group, parentWidget)};
0077         }
0078     }
0079 
0080     return {};
0081 }
0082 
0083 #include "plasmavaultfileitemaction.moc"