Changeset View
Changeset View
Standalone View
Standalone View
fileitemplugin/plasmavaultfileitemaction.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * Copyright (C) 2019 Ivan Cukic <ivan.cukic@kde.org> | ||||
3 | * | ||||
4 | * This program is free software; you can redistribute it and/or | ||||
5 | * modify it under the terms of the GNU General Public License | ||||
6 | * as published by the Free Software Foundation; either version 2 | ||||
7 | * of the License, or (at your option) any later version. | ||||
8 | * | ||||
9 | * This program is distributed in the hope that it will be useful, | ||||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
12 | * GNU General Public License for more details. | ||||
13 | * | ||||
14 | * You should have received a copy of the GNU General Public License | ||||
15 | * along with this program; if not, write to the Free Software | ||||
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||||
17 | * | ||||
18 | */ | ||||
19 | | ||||
20 | #include "plasmavaultfileitemaction.h" | ||||
21 | | ||||
22 | #include <QFileInfo> | ||||
23 | #include <QMenu> | ||||
24 | #include <QDBusInterface> | ||||
25 | #include <QDBusPendingCall> | ||||
26 | | ||||
27 | #include <KConfig> | ||||
28 | #include <KPluginFactory> | ||||
29 | #include <KMountPoint> | ||||
30 | #include <KLocalizedString> | ||||
31 | #include <KRun> | ||||
32 | | ||||
33 | K_PLUGIN_CLASS_WITH_JSON(PlasmaVaultFileItemAction, "plasmavaultfileitemaction.json") | ||||
34 | | ||||
35 | PlasmaVaultFileItemAction::PlasmaVaultFileItemAction(QObject* parent, const QVariantList&) | ||||
36 | : KAbstractFileItemActionPlugin(parent) | ||||
37 | {} | ||||
38 | | ||||
39 | QList<QAction*> PlasmaVaultFileItemAction::actions(const KFileItemListProperties& fileItemInfos, QWidget* parentWidget) | ||||
40 | { | ||||
41 | if (fileItemInfos.urlList().size() != 1 || | ||||
42 | !fileItemInfos.isDirectory()) return {}; | ||||
43 | | ||||
44 | QList<QAction*> actions; | ||||
45 | const QIcon icon = QIcon::fromTheme(QStringLiteral("plasmavault")); | ||||
46 | | ||||
47 | auto fileItem = fileItemInfos.urlList()[0].toLocalFile(); | ||||
48 | | ||||
49 | auto createAction = [this] (const QIcon& icon, const QString& name, | ||||
50 | QString command, QString device, | ||||
51 | QWidget* parentWidget) | ||||
52 | { | ||||
53 | QAction *action = new QAction(icon, name, parentWidget); | ||||
54 | | ||||
55 | connect(action, &QAction::triggered, this, [this,command,device]() { | ||||
56 | auto method = QDBusMessage::createMethodCall( | ||||
57 | "org.kde.kded5", | ||||
58 | "/modules/plasmavault", | ||||
59 | "org.kde.plasmavault", | ||||
60 | command); | ||||
61 | method.setArguments({ device }); | ||||
62 | | ||||
63 | QDBusConnection::sessionBus().call(method); | ||||
64 | }); | ||||
65 | | ||||
66 | return action; | ||||
67 | }; | ||||
68 | | ||||
69 | KConfig config("plasmavaultrc"); | ||||
70 | for (auto group: config.groupList()) { | ||||
71 | auto mountPoint = config.entryMap(group)["mountPoint"]; | ||||
72 | if (mountPoint == fileItem) { | ||||
73 | const auto currentMounts = KMountPoint::currentMountPoints(); | ||||
74 | | ||||
75 | const bool mounted = std::any_of(currentMounts.begin(), currentMounts.end(), | ||||
76 | [mountPoint] (const KMountPoint::Ptr& mount) { | ||||
77 | return mount->mountPoint() == mountPoint; | ||||
78 | }); | ||||
79 | | ||||
80 | const QString command = mounted ? "closeVault" : "openVault"; | ||||
81 | const QString title = mounted ? | ||||
82 | i18nc("@action Action to unmount a vault", "Close this Plasma Vault") : | ||||
83 | i18nc("@action Action to mount a vault", "Open this Plasma Vault"); | ||||
84 | | ||||
85 | return { | ||||
86 | createAction(icon, | ||||
87 | title, | ||||
88 | command, | ||||
89 | group, | ||||
90 | parentWidget) | ||||
91 | }; | ||||
92 | } | ||||
93 | } | ||||
94 | | ||||
95 | return {}; | ||||
96 | } | ||||
97 | | ||||
98 | #include "plasmavaultfileitemaction.moc" |