File indexing completed on 2024-09-22 05:16:04

0001 /*
0002     This file is part of the Kasten Framework, made within the KDE community.
0003 
0004     SPDX-FileCopyrightText: 2010 Friedrich W. H. Kossebau <kossebau@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0007 */
0008 
0009 #include "terminalview.hpp"
0010 
0011 // lib
0012 #include "terminaltool.hpp"
0013 // KF
0014 #include <KLocalizedString>
0015 #include <KPluginFactory>
0016 #include <KPluginMetaData>
0017 #include <KParts/ReadOnlyPart>
0018 #include <kde_terminal_interface.h>
0019 // Qt
0020 #include <QVBoxLayout>
0021 #include <QFrame>
0022 #include <QUrl>
0023 #include <QDir>
0024 
0025 namespace Kasten {
0026 
0027 TerminalView::TerminalView(TerminalTool* tool, QWidget* parent)
0028     : QWidget(parent)
0029     , mTool(tool)
0030 {
0031     auto* layout = new QVBoxLayout(this);
0032     layout->setContentsMargins(0, 0, 0, 0);
0033 
0034     connect(mTool, &TerminalTool::currentUrlChanged, this, &TerminalView::onCurrentUrlChanged);
0035     QMetaObject::invokeMethod(this, "createTerminalPart", Qt::QueuedConnection);
0036 }
0037 
0038 TerminalView::~TerminalView()
0039 {
0040     if (mTerminalPart) {
0041         disconnect(mTerminalPart, &QObject::destroyed,
0042                    this, &TerminalView::onTerminalPartDestroyed);
0043     }
0044 }
0045 
0046 void TerminalView::createTerminalPart()
0047 {
0048     KPluginFactory *factory = KPluginFactory::loadFactory(KPluginMetaData(QStringLiteral("konsolepart"))).plugin;
0049     mTerminalPart = factory ? (factory->create<KParts::ReadOnlyPart>(this)) : nullptr;
0050 
0051     if (mTerminalPart) {
0052         connect(mTerminalPart, &QObject::destroyed,
0053                 this, &TerminalView::onTerminalPartDestroyed);
0054 
0055         QWidget* terminalWidget = mTerminalPart->widget();
0056         terminalWidget->setFocusPolicy(Qt::WheelFocus);
0057         terminalWidget->setFocus();
0058         setFocusProxy(terminalWidget);
0059 
0060         auto* frame = qobject_cast<QFrame*>(terminalWidget);
0061         if (frame) {
0062             frame->setFrameStyle(QFrame::Panel | QFrame::Sunken);
0063         }
0064 
0065         auto* layout = static_cast<QVBoxLayout*>(this->layout());
0066         layout->addWidget(terminalWidget);
0067         terminalWidget->show();
0068 
0069         mTerminalInterface = qobject_cast<TerminalInterface*>(mTerminalPart);
0070         QUrl currentUrl = mTool->currentUrl();
0071         if (currentUrl.isEmpty()) {
0072             currentUrl = QUrl::fromLocalFile(QDir::homePath());
0073         }
0074         onCurrentUrlChanged(currentUrl);
0075     }
0076 }
0077 
0078 void TerminalView::onCurrentUrlChanged(const QUrl& currentUrl)
0079 {
0080     if (mTerminalInterface && currentUrl.isLocalFile()) {
0081         mTerminalInterface->showShellInDir(currentUrl.path());
0082     }
0083     // TODO: Konsole currently seems to ignore this call if shell is running?
0084 }
0085 
0086 void TerminalView::onTerminalPartDestroyed()
0087 {
0088     mTerminalPart = nullptr;
0089     mTerminalInterface = nullptr;
0090 
0091     createTerminalPart();
0092 }
0093 
0094 }
0095 
0096 #include "moc_terminalview.cpp"