File indexing completed on 2024-04-28 05:34:26
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 Q_UNUSED(parentWidget) 0051 QAction *action = new QAction(icon, name, this); 0052 0053 connect(action, &QAction::triggered, this, [command, device]() { 0054 auto method = QDBusMessage::createMethodCall("org.kde.kded6", "/modules/plasmavault", "org.kde.plasmavault", command); 0055 method.setArguments({device}); 0056 0057 QDBusConnection::sessionBus().call(method, QDBus::NoBlock); 0058 }); 0059 0060 return action; 0061 }; 0062 0063 KConfig config("plasmavaultrc"); 0064 for (auto group : config.groupList()) { 0065 auto mountPoint = config.entryMap(group)["mountPoint"]; 0066 if (mountPoint == fileItem) { 0067 const auto currentMounts = KMountPoint::currentMountPoints(); 0068 0069 const bool mounted = std::any_of(currentMounts.begin(), currentMounts.end(), [mountPoint](const KMountPoint::Ptr &mount) { 0070 return mount->mountPoint() == mountPoint; 0071 }); 0072 0073 const QString command = mounted ? "closeVault" : "openVault"; 0074 const QString title = mounted ? i18nc("@action Action to unmount a vault", "Close this Plasma Vault") 0075 : i18nc("@action Action to mount a vault", "Open this Plasma Vault"); 0076 0077 return {createAction(icon, title, command, group, parentWidget)}; 0078 } 0079 } 0080 0081 return {}; 0082 } 0083 0084 #include "plasmavaultfileitemaction.moc" 0085 0086 #include "moc_plasmavaultfileitemaction.cpp"