1 /***************************************************************************
2 * Copyright (C) 2007-2010 by Peter Penz <peter.penz19@gmail.com> *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
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. *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
20 #include "terminalpanel.h"
22 #include <KIO/DesktopExecParser>
24 #include <KIO/JobUiDelegate>
25 #include <KJobWidgets>
26 #include <KLocalizedString>
27 #include <KMessageWidget>
28 #include <KParts/ReadOnlyPart>
29 #include <KPluginFactory>
30 #include <KPluginLoader>
33 #include <kde_terminal_interface.h>
36 #include <QDesktopServices>
41 #include <QVBoxLayout>
43 TerminalPanel::TerminalPanel(QWidget
* parent
) :
45 m_clearTerminal(true),
46 m_mostLocalUrlJob(nullptr),
49 m_terminalWidget(nullptr),
50 m_konsolePartMissingMessage(nullptr),
51 m_konsolePart(nullptr),
52 m_konsolePartCurrentDirectory(),
53 m_sendCdToTerminalHistory()
55 m_layout
= new QVBoxLayout(this);
56 m_layout
->setMargin(0);
59 TerminalPanel::~TerminalPanel()
63 void TerminalPanel::goHome()
65 sendCdToTerminal(QDir::homePath(), HistoryPolicy::SkipHistory
);
68 QString
TerminalPanel::currentWorkingDirectory()
71 return m_terminal
->currentWorkingDirectory();
76 void TerminalPanel::terminalExited()
79 emit
hideTerminalPanel();
82 bool TerminalPanel::isHiddenInVisibleWindow() const
85 && parentWidget()->isHidden()
87 && !hasProgramRunning();
90 void TerminalPanel::dockVisibilityChanged()
92 // Only react when the DockWidget itself (not some parent) is hidden. This way we don't
93 // respond when e.g. Dolphin is minimized.
94 if (isHiddenInVisibleWindow()) {
95 // Make sure that the following "cd /" command will not affect the view.
96 disconnect(m_konsolePart
, SIGNAL(currentDirectoryChanged(QString
)),
97 this, SLOT(slotKonsolePartCurrentDirectoryChanged(QString
)));
99 // Make sure this terminal does not prevent unmounting any removable drives
100 changeDir(QUrl::fromLocalFile(QStringLiteral("/")));
102 // Because we have disconnected from the part's currentDirectoryChanged()
103 // signal, we have to update m_konsolePartCurrentDirectory manually. If this
104 // was not done, showing the panel again might not set the part's working
105 // directory correctly.
106 m_konsolePartCurrentDirectory
= '/';
110 QString
TerminalPanel::runningProgramName() const
112 return m_terminal
? m_terminal
->foregroundProcessName() : QString();
115 bool TerminalPanel::hasProgramRunning() const
117 return m_terminal
&& (m_terminal
->foregroundProcessId() != -1);
120 bool TerminalPanel::urlChanged()
122 if (!url().isValid()) {
126 const bool sendInput
= m_terminal
&& !hasProgramRunning() && isVisible();
134 void TerminalPanel::showEvent(QShowEvent
* event
)
136 if (event
->spontaneous()) {
137 Panel::showEvent(event
);
142 m_clearTerminal
= true;
143 KPluginFactory
* factory
= nullptr;
144 KService::Ptr service
= KService::serviceByDesktopName(QStringLiteral("konsolepart"));
146 factory
= KPluginLoader(service
->library()).factory();
148 m_konsolePart
= factory
? (factory
->create
<KParts::ReadOnlyPart
>(this)) : nullptr;
150 connect(m_konsolePart
, &KParts::ReadOnlyPart::destroyed
, this, &TerminalPanel::terminalExited
);
151 m_terminalWidget
= m_konsolePart
->widget();
152 m_layout
->addWidget(m_terminalWidget
);
153 if (m_konsolePartMissingMessage
) {
154 m_layout
->removeWidget(m_konsolePartMissingMessage
);
156 m_terminal
= qobject_cast
<TerminalInterface
*>(m_konsolePart
);
157 } else if (!m_konsolePartMissingMessage
) {
158 const auto konsoleInstallUrl
= QUrl("appstream://org.kde.konsole.desktop");
159 const auto konsoleNotInstalledText
= i18n("Terminal cannot be shown because Konsole is not installed. "
160 "Please install it and then reopen the panel.");
161 m_konsolePartMissingMessage
= new KMessageWidget(konsoleNotInstalledText
, this);
162 m_konsolePartMissingMessage
->setCloseButtonVisible(false);
163 m_konsolePartMissingMessage
->hide();
164 if (KIO::DesktopExecParser::hasSchemeHandler(konsoleInstallUrl
)) {
165 auto installKonsoleAction
= new QAction(i18n("Install Konsole"), this);
166 connect(installKonsoleAction
, &QAction::triggered
, [konsoleInstallUrl
]() {
167 QDesktopServices::openUrl(konsoleInstallUrl
);
169 m_konsolePartMissingMessage
->addAction(installKonsoleAction
);
171 m_layout
->addWidget(m_konsolePartMissingMessage
);
172 m_layout
->addStretch();
173 QTimer::singleShot(0, m_konsolePartMissingMessage
, &KMessageWidget::animatedShow
);
175 m_konsolePartMissingMessage
->animatedShow();
179 m_terminal
->showShellInDir(url().toLocalFile());
181 m_terminalWidget
->setFocus();
182 connect(m_konsolePart
, SIGNAL(currentDirectoryChanged(QString
)),
183 this, SLOT(slotKonsolePartCurrentDirectoryChanged(QString
)));
186 Panel::showEvent(event
);
189 void TerminalPanel::changeDir(const QUrl
& url
)
191 delete m_mostLocalUrlJob
;
192 m_mostLocalUrlJob
= nullptr;
194 if (url
.isLocalFile()) {
195 sendCdToTerminal(url
.toLocalFile());
197 m_mostLocalUrlJob
= KIO::mostLocalUrl(url
, KIO::HideProgressInfo
);
198 if (m_mostLocalUrlJob
->uiDelegate()) {
199 KJobWidgets::setWindow(m_mostLocalUrlJob
, this);
201 connect(m_mostLocalUrlJob
, &KIO::StatJob::result
, this, &TerminalPanel::slotMostLocalUrlResult
);
205 void TerminalPanel::sendCdToTerminal(const QString
& dir
, HistoryPolicy addToHistory
)
207 if (dir
== m_konsolePartCurrentDirectory
) {
208 m_clearTerminal
= false;
213 if (!m_clearTerminal
) {
214 // The TerminalV2 interface does not provide a way to delete the
215 // current line before sending a new input. This is mandatory,
216 // otherwise sending a 'cd x' to a existing 'rm -rf *' might
217 // result in data loss. As workaround SIGINT is sent.
218 const int processId
= m_terminal
->terminalProcessId();
220 kill(processId
, SIGINT
);
225 m_terminal
->sendInput(" cd " + KShell::quoteArg(dir
) + '\n');
227 // We want to ignore the currentDirectoryChanged(QString) signal, which we will receive after
228 // the directory change, because this directory change is not caused by a "cd" command that the
229 // user entered in the panel. Therefore, we have to remember 'dir'. Note that it could also be
230 // a symbolic link -> remember the 'canonical' path.
231 if (addToHistory
== HistoryPolicy::AddToHistory
)
232 m_sendCdToTerminalHistory
.enqueue(QDir(dir
).canonicalPath());
234 if (m_clearTerminal
) {
235 m_terminal
->sendInput(QStringLiteral(" clear\n"));
236 m_clearTerminal
= false;
240 void TerminalPanel::slotMostLocalUrlResult(KJob
* job
)
242 KIO::StatJob
* statJob
= static_cast<KIO::StatJob
*>(job
);
243 const QUrl url
= statJob
->mostLocalUrl();
244 if (url
.isLocalFile()) {
245 sendCdToTerminal(url
.toLocalFile());
248 m_mostLocalUrlJob
= nullptr;
251 void TerminalPanel::slotKonsolePartCurrentDirectoryChanged(const QString
& dir
)
253 m_konsolePartCurrentDirectory
= QDir(dir
).canonicalPath();
255 // Only emit a changeUrl signal if the directory change was caused by the user inside the
256 // terminal, and not by sendCdToTerminal(QString).
257 while (!m_sendCdToTerminalHistory
.empty()) {
258 if (m_konsolePartCurrentDirectory
== m_sendCdToTerminalHistory
.dequeue()) {
263 const QUrl
url(QUrl::fromLocalFile(dir
));