Changeset View
Changeset View
Standalone View
Standalone View
plugins/flatpak/flatpakplugin.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | Copyright 2017 Aleix Pol Gonzalez <aleixpol@kde.org> | ||||
3 | | ||||
4 | This library is free software; you can redistribute it and/or | ||||
5 | modify it under the terms of the GNU Library General Public | ||||
6 | License version 2 as published by the Free Software Foundation. | ||||
7 | | ||||
8 | This library is distributed in the hope that it will be useful, | ||||
9 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||
11 | Library General Public License for more details. | ||||
12 | | ||||
13 | You should have received a copy of the GNU Library General Public License | ||||
14 | along with this library; see the file COPYING.LIB. If not, write to | ||||
15 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||||
16 | Boston, MA 02110-1301, USA. | ||||
17 | */ | ||||
18 | | ||||
19 | #include "flatpakplugin.h" | ||||
20 | #include "flatpakruntime.h" | ||||
21 | #include <interfaces/icore.h> | ||||
22 | #include <interfaces/iruntimecontroller.h> | ||||
23 | #include <interfaces/iuicontroller.h> | ||||
24 | #include <interfaces/context.h> | ||||
25 | #include <interfaces/contextmenuextension.h> | ||||
26 | #include <project/projectmodel.h> | ||||
27 | #include <QDebug> | ||||
28 | #include <QStandardPaths> | ||||
29 | #include <QAction> | ||||
30 | #include <QProcess> | ||||
31 | #include <QRegularExpression> | ||||
32 | #include <QTemporaryDir> | ||||
33 | #include <QFileDialog> | ||||
34 | #include <KPluginFactory> | ||||
35 | #include <KActionCollection> | ||||
36 | #include <KLocalizedString> | ||||
37 | #include <KParts/MainWindow> | ||||
38 | #include <KJob> | ||||
39 | | ||||
40 | K_PLUGIN_FACTORY_WITH_JSON(KDevFlatpakFactory, "kdevflatpak.json", registerPlugin<FlatpakPlugin>();) | ||||
41 | | ||||
42 | using namespace KDevelop; | ||||
43 | | ||||
44 | FlatpakPlugin::FlatpakPlugin(QObject *parent, const QVariantList & /*args*/) | ||||
45 | : KDevelop::IPlugin( QStringLiteral("kdevflatpak"), parent ) | ||||
46 | { | ||||
47 | auto ac = actionCollection(); | ||||
48 | | ||||
49 | auto action = new QAction(QIcon::fromTheme(QStringLiteral("run-build-clean")), i18n("Rebuild environment"), this); | ||||
50 | action->setWhatsThis(i18n("Recompiles all dependencies for a fresh environment.")); | ||||
51 | action->setShortcut(Qt::CTRL | Qt::META | Qt::Key_X); | ||||
52 | connect(action, &QAction::triggered, this, &FlatpakPlugin::rebuildCurrent); | ||||
53 | ac->addAction(QStringLiteral("runtime_flatpak_rebuild"), action); | ||||
54 | | ||||
55 | auto exportAction = new QAction(QIcon::fromTheme(QStringLiteral("document-export")), i18n("Export flatpak environment..."), this); | ||||
56 | exportAction->setWhatsThis(i18n("Exports the current build into a 'bundle.flatpak' file.")); | ||||
57 | exportAction->setShortcut(Qt::CTRL | Qt::META | Qt::Key_E); | ||||
58 | connect(exportAction, &QAction::triggered, this, &FlatpakPlugin::exportCurrent); | ||||
59 | ac->addAction(QStringLiteral("runtime_flatpak_export"), exportAction); | ||||
60 | | ||||
61 | runtimeChanged(ICore::self()->runtimeController()->currentRuntime()); | ||||
62 | | ||||
63 | setXMLFile( QStringLiteral("kdevflatpakplugin.rc") ); | ||||
64 | connect(ICore::self()->runtimeController(), &IRuntimeController::currentRuntimeChanged, this, &FlatpakPlugin::runtimeChanged); | ||||
65 | } | ||||
66 | | ||||
67 | FlatpakPlugin::~FlatpakPlugin() = default; | ||||
68 | | ||||
69 | void FlatpakPlugin::runtimeChanged(KDevelop::IRuntime* newRuntime) | ||||
70 | { | ||||
71 | const bool isFlatpak = qobject_cast<FlatpakRuntime*>(newRuntime); | ||||
72 | | ||||
73 | for(auto action: actionCollection()->actions()) | ||||
74 | action->setEnabled(isFlatpak); | ||||
75 | } | ||||
76 | | ||||
77 | void FlatpakPlugin::rebuildCurrent() | ||||
78 | { | ||||
79 | const auto runtime = qobject_cast<FlatpakRuntime*>(ICore::self()->runtimeController()->currentRuntime()); | ||||
80 | Q_ASSERT(runtime); | ||||
81 | runtime->rebuild(); | ||||
82 | } | ||||
83 | | ||||
84 | void FlatpakPlugin::exportCurrent() | ||||
85 | { | ||||
86 | const auto runtime = qobject_cast<FlatpakRuntime*>(ICore::self()->runtimeController()->currentRuntime()); | ||||
87 | Q_ASSERT(runtime); | ||||
88 | | ||||
89 | const QString name = runtime->name(); | ||||
90 | const QString path = QFileDialog::getSaveFileName(ICore::self()->uiController()->activeMainWindow(), i18n("Export %1 to..."), {}, i18n("Flatpak Bundle (*.flatpak)")); | ||||
91 | if (!path.isEmpty()) { | ||||
92 | runtime->exportBundle(path); | ||||
93 | } | ||||
94 | } | ||||
95 | | ||||
96 | KDevelop::ContextMenuExtension FlatpakPlugin::contextMenuExtension(KDevelop::Context* context) | ||||
97 | { | ||||
98 | QList<QUrl> urls; | ||||
99 | | ||||
100 | if ( context->type() == KDevelop::Context::FileContext ) { | ||||
101 | KDevelop::FileContext* filectx = dynamic_cast<KDevelop::FileContext*>( context ); | ||||
102 | urls = filectx->urls(); | ||||
103 | } else if ( context->type() == KDevelop::Context::ProjectItemContext ) { | ||||
104 | KDevelop::ProjectItemContext* projctx = dynamic_cast<KDevelop::ProjectItemContext*>( context ); | ||||
105 | foreach( KDevelop::ProjectBaseItem* item, projctx->items() ) { | ||||
106 | if ( item->file() ) { | ||||
107 | urls << item->file()->path().toUrl(); | ||||
108 | } | ||||
109 | } | ||||
110 | } | ||||
111 | | ||||
112 | const QRegularExpression nameRx(".*\\..*\\.*.json$"); | ||||
113 | for(auto it = urls.begin(); it != urls.end(); ) { | ||||
114 | if (it->isLocalFile() && it->path().contains(nameRx)) { | ||||
115 | ++it; | ||||
116 | } else { | ||||
117 | it = urls.erase(it); | ||||
118 | } | ||||
119 | } | ||||
120 | | ||||
121 | if ( !urls.isEmpty() ) { | ||||
122 | KDevelop::ContextMenuExtension ext; | ||||
123 | foreach(const QUrl &url, urls) { | ||||
124 | const KDevelop::Path file(url); | ||||
125 | | ||||
126 | auto action = new QAction(i18n("Create flatpak environment for %1", file.lastPathSegment()), this); | ||||
127 | connect(action, &QAction::triggered, this, [this, file]() { | ||||
128 | QTemporaryDir dir(QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + QStringLiteral("/kdevelop-flatpak-")); | ||||
129 | dir.setAutoRemove(false); | ||||
130 | | ||||
131 | const KDevelop::Path path(dir.path()); | ||||
132 | | ||||
133 | auto process = FlatpakRuntime::createBuildDirectory(path, file); | ||||
134 | connect(process, &KJob::finished, this, [this, path, file] (KJob* job) { | ||||
135 | if (job->error() != 0) | ||||
136 | return; | ||||
137 | | ||||
138 | auto runtime = new FlatpakRuntime(path, file); | ||||
139 | ICore::self()->runtimeController()->addRuntimes({runtime}); | ||||
140 | }); | ||||
141 | process->start(); | ||||
142 | }); | ||||
143 | ext.addAction(KDevelop::ContextMenuExtension::RunGroup, action); | ||||
144 | } | ||||
145 | | ||||
146 | return ext; | ||||
147 | } | ||||
148 | | ||||
149 | return KDevelop::IPlugin::contextMenuExtension( context ); | ||||
150 | } | ||||
151 | | ||||
152 | | ||||
153 | #include "flatpakplugin.moc" |