File indexing completed on 2024-05-05 16:45:14

0001 // SPDX-FileCopyrightText: 2022 Gleb Popov <arrowd@FreeBSD.org>
0002 // SPDX-License-Identifier: BSD-3-Clause
0003 
0004 #include "craftplugin.h"
0005 #include "craftruntime.h"
0006 #include "debug_craft.h"
0007 
0008 #include <interfaces/icore.h>
0009 #include <interfaces/iproject.h>
0010 #include <interfaces/iprojectcontroller.h>
0011 #include <interfaces/iruntimecontroller.h>
0012 #include <interfaces/iuicontroller.h>
0013 
0014 #include <KParts/MainWindow>
0015 #include <KPluginFactory>
0016 #include <KLocalizedString>
0017 #include <KConfigGroup>
0018 #include <KMessageBox>
0019 #include <KMessageBox_KDevCompat>
0020 
0021 K_PLUGIN_FACTORY_WITH_JSON(KDevCraftFactory, "kdevcraft.json", registerPlugin<CraftPlugin>();)
0022 
0023 using namespace KDevelop;
0024 
0025 namespace {
0026 bool wantAutoEnable(KDevelop::IProject* project, const QString& craftRoot)
0027 {
0028     auto projectConfigGroup = project->projectConfiguration()->group("Project");
0029     const bool haveConfigEntry = projectConfigGroup.entryMap().contains(QLatin1String("AutoEnableCraftRuntime"));
0030 
0031     if (!haveConfigEntry) {
0032         const QString msgboxText = i18n(
0033             "The project being loaded (%1) is detected to reside\n"
0034             "under a Craft root [%2] .\nDo you want to automatically switch to the Craft runtime?",
0035             project->name(), craftRoot);
0036 
0037         auto answer = KMessageBox::questionTwoActions(
0038             ICore::self()->uiController()->activeMainWindow(), msgboxText, QString(),
0039             KGuiItem(i18nc("@action:button", "Switch to Craft Runtime"), QStringLiteral("dialog-ok")),
0040             KGuiItem(i18nc("@action:button", "Do not switch automatically"), QStringLiteral("dialog-cancel")));
0041         projectConfigGroup.writeEntry("AutoEnableCraftRuntime", answer == KMessageBox::PrimaryAction);
0042         return answer == KMessageBox::PrimaryAction;
0043     } else {
0044         return projectConfigGroup.readEntry("AutoEnableCraftRuntime", false);
0045     }
0046 }
0047 }
0048 
0049 CraftPlugin::CraftPlugin(QObject* parent, const QVariantList& /*args*/)
0050     : IPlugin(QStringLiteral("kdevcraft"), parent)
0051 {
0052     const QString pythonExecutable = CraftRuntime::findPython();
0053     if (pythonExecutable.isEmpty())
0054         return;
0055 
0056     // If KDevelop itself runs under Craft env, this plugin has nothing to do
0057     if (qEnvironmentVariableIsSet("KDEROOT"))
0058         return;
0059 
0060     connect(ICore::self()->projectController(), &IProjectController::projectAboutToBeOpened, this,
0061             [pythonExecutable](KDevelop::IProject* project) {
0062                 const QString craftRoot = CraftRuntime::findCraftRoot(project->path());
0063                 auto* currentCraftRuntime =
0064                     qobject_cast<CraftRuntime*>(ICore::self()->runtimeController()->currentRuntime());
0065 
0066                 if (craftRoot.isEmpty()) {
0067                     if (currentCraftRuntime)
0068                         qCDebug(CRAFT) << "Loading a non-Craft project while Craft runtime is enabled. This will cause "
0069                                           "the project to build and run under a Craft env!";
0070                     return;
0071                 }
0072 
0073                 qCDebug(CRAFT) << "Found Craft root at" << craftRoot;
0074                 if (currentCraftRuntime) {
0075                     qCDebug(CRAFT) << "A Craft runtime rooted at" << currentCraftRuntime->craftRoot()
0076                                    << "is already active. Will not create another one";
0077                     return;
0078                 }
0079 
0080                 auto* runtime = new CraftRuntime(craftRoot, pythonExecutable);
0081                 ICore::self()->runtimeController()->addRuntimes(runtime);
0082                 if (wantAutoEnable(project, craftRoot))
0083                     ICore::self()->runtimeController()->setCurrentRuntime(runtime);
0084             });
0085 }
0086 
0087 #include "craftplugin.moc"
0088 #include "moc_craftplugin.cpp"