131 lines
4.3 KiB
C++
131 lines
4.3 KiB
C++
#include "kompanionkonsoleplugin.h"
|
|
|
|
#include "kompanionagentpanel.h"
|
|
|
|
#include "MainWindow.h"
|
|
#include "profile/ProfileManager.h"
|
|
#include "session/Session.h"
|
|
#include "session/SessionController.h"
|
|
|
|
#include <KActionCollection>
|
|
#include <KLocalizedString>
|
|
|
|
#include <QAction>
|
|
#include <QDockWidget>
|
|
#include <QHash>
|
|
#include <QKeySequence>
|
|
#include <QPointer>
|
|
|
|
K_PLUGIN_CLASS_WITH_JSON(KompanionKonsolePlugin, "kompanion_konsole.json")
|
|
|
|
struct KompanionKonsolePlugin::Private {
|
|
struct WindowUi {
|
|
QPointer<QDockWidget> dock;
|
|
QPointer<KompanionAgentPanel> panel;
|
|
};
|
|
|
|
QHash<Konsole::MainWindow *, WindowUi> uiPerWindow;
|
|
QPointer<Konsole::SessionController> activeController;
|
|
QString attachCommand = QStringLiteral(
|
|
"printf '\\033[1;35m[Kompanion] demo bridge engaged — shell handed to Kompanion.\\033[0m\\n'");
|
|
QString launchCommand =
|
|
QStringLiteral("printf '\\033[1;34m[Kompanion] launching demo agent shell...\\033[0m\\n'; "
|
|
"kom_mcp --list || echo \"[Kompanion] kom_mcp binary not found on PATH\"");
|
|
};
|
|
|
|
KompanionKonsolePlugin::KompanionKonsolePlugin(QObject *parent, const QVariantList &args)
|
|
: Konsole::IKonsolePlugin(parent, args)
|
|
, d(std::make_unique<Private>())
|
|
{
|
|
setName(QStringLiteral("KompanionKonsole"));
|
|
}
|
|
|
|
KompanionKonsolePlugin::~KompanionKonsolePlugin() = default;
|
|
|
|
void KompanionKonsolePlugin::createWidgetsForMainWindow(Konsole::MainWindow *mainWindow)
|
|
{
|
|
auto *dock = new QDockWidget(mainWindow);
|
|
dock->setWindowTitle(i18n("Kompanion Konsole Bridge"));
|
|
dock->setObjectName(QStringLiteral("KompanionKonsoleDock"));
|
|
dock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
|
|
dock->setVisible(false);
|
|
|
|
auto *panel = new KompanionAgentPanel(dock);
|
|
dock->setWidget(panel);
|
|
|
|
mainWindow->addDockWidget(Qt::RightDockWidgetArea, dock);
|
|
|
|
connect(panel, &KompanionAgentPanel::requestAttach, this, [this]() {
|
|
if (!d->activeController) {
|
|
return;
|
|
}
|
|
auto session = d->activeController->session();
|
|
if (!session) {
|
|
return;
|
|
}
|
|
session->sendTextToTerminal(d->attachCommand, QLatin1Char('\r'));
|
|
});
|
|
|
|
connect(panel, &KompanionAgentPanel::requestLaunch, this, [this, mainWindow]() {
|
|
auto profile = Konsole::ProfileManager::instance()->defaultProfile();
|
|
auto session = mainWindow->createSession(profile, QString());
|
|
if (!session) {
|
|
return;
|
|
}
|
|
session->sendTextToTerminal(d->launchCommand, QLatin1Char('\r'));
|
|
});
|
|
|
|
Private::WindowUi windowUi;
|
|
windowUi.dock = dock;
|
|
windowUi.panel = panel;
|
|
d->uiPerWindow.insert(mainWindow, windowUi);
|
|
}
|
|
|
|
void KompanionKonsolePlugin::activeViewChanged(Konsole::SessionController *controller, Konsole::MainWindow *mainWindow)
|
|
{
|
|
d->activeController = controller;
|
|
|
|
auto it = d->uiPerWindow.find(mainWindow);
|
|
if (it == d->uiPerWindow.end()) {
|
|
return;
|
|
}
|
|
|
|
const bool hasSession = controller && controller->session();
|
|
QString title;
|
|
QString directory;
|
|
|
|
if (hasSession) {
|
|
title = controller->userTitle();
|
|
if (title.isEmpty()) {
|
|
if (auto session = controller->session()) {
|
|
title = session->title(Konsole::Session::DisplayedTitleRole);
|
|
}
|
|
}
|
|
directory = controller->currentDir();
|
|
}
|
|
|
|
if (it->panel) {
|
|
it->panel->setActiveSessionInfo(title, directory);
|
|
it->panel->setAttachEnabled(hasSession);
|
|
}
|
|
}
|
|
|
|
QList<QAction *> KompanionKonsolePlugin::menuBarActions(Konsole::MainWindow *mainWindow) const
|
|
{
|
|
auto it = d->uiPerWindow.constFind(mainWindow);
|
|
if (it == d->uiPerWindow.constEnd() || !it->dock) {
|
|
return {};
|
|
}
|
|
|
|
QAction *toggleDock = new QAction(i18n("Show Kompanion Bridge"), mainWindow);
|
|
toggleDock->setCheckable(true);
|
|
mainWindow->actionCollection()->setDefaultShortcut(toggleDock, QKeySequence(Qt::CTRL | Qt::SHIFT | Qt::Key_K));
|
|
QObject::connect(toggleDock, &QAction::triggered, it->dock.data(), &QDockWidget::setVisible);
|
|
QObject::connect(it->dock.data(), &QDockWidget::visibilityChanged, toggleDock, &QAction::setChecked);
|
|
toggleDock->setChecked(it->dock->isVisible());
|
|
return {toggleDock};
|
|
}
|
|
|
|
#include "moc_kompanionkonsoleplugin.cpp"
|
|
#include "kompanionkonsoleplugin.moc"
|