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"
23 #include <KIO/JobUiDelegate>
24 #include <KJobWidgets>
25 #include <KParts/ReadOnlyPart>
26 #include <KPluginFactory>
27 #include <KPluginLoader>
30 #include <kde_terminal_interface.h>
34 #include <QVBoxLayout>
36 TerminalPanel::TerminalPanel(QWidget
* parent
) :
38 m_clearTerminal(true),
39 m_mostLocalUrlJob(nullptr),
42 m_terminalWidget(nullptr),
43 m_konsolePart(nullptr),
44 m_konsolePartCurrentDirectory(),
45 m_sendCdToTerminalHistory()
47 m_layout
= new QVBoxLayout(this);
48 m_layout
->setMargin(0);
51 TerminalPanel::~TerminalPanel()
55 void TerminalPanel::goHome()
57 sendCdToTerminal(QDir::homePath(), HistoryPolicy::SkipHistory
);
60 QString
TerminalPanel::currentWorkingDirectory()
63 return m_terminal
->currentWorkingDirectory();
68 void TerminalPanel::terminalExited()
71 emit
hideTerminalPanel();
74 bool TerminalPanel::isHiddenInVisibleWindow()
77 && parentWidget()->isHidden()
79 && (m_terminal
->foregroundProcessId() == -1);
82 void TerminalPanel::dockVisibilityChanged()
84 // Only react when the DockWidget itself (not some parent) is hidden. This way we don't
85 // respond when e.g. Dolphin is minimized.
86 if (isHiddenInVisibleWindow()) {
87 // Make sure that the following "cd /" command will not affect the view.
88 disconnect(m_konsolePart
, SIGNAL(currentDirectoryChanged(QString
)),
89 this, SLOT(slotKonsolePartCurrentDirectoryChanged(QString
)));
91 // Make sure this terminal does not prevent unmounting any removable drives
92 changeDir(QUrl::fromLocalFile(QStringLiteral("/")));
94 // Because we have disconnected from the part's currentDirectoryChanged()
95 // signal, we have to update m_konsolePartCurrentDirectory manually. If this
96 // was not done, showing the panel again might not set the part's working
97 // directory correctly.
98 m_konsolePartCurrentDirectory
= '/';
102 bool TerminalPanel::urlChanged()
104 if (!url().isValid()) {
108 const bool sendInput
= m_terminal
&& (m_terminal
->foregroundProcessId() == -1) && isVisible();
116 void TerminalPanel::showEvent(QShowEvent
* event
)
118 if (event
->spontaneous()) {
119 Panel::showEvent(event
);
124 m_clearTerminal
= true;
125 KPluginFactory
* factory
= nullptr;
126 KService::Ptr service
= KService::serviceByDesktopName(QStringLiteral("konsolepart"));
128 factory
= KPluginLoader(service
->library()).factory();
130 m_konsolePart
= factory
? (factory
->create
<KParts::ReadOnlyPart
>(this)) : nullptr;
132 connect(m_konsolePart
, &KParts::ReadOnlyPart::destroyed
, this, &TerminalPanel::terminalExited
);
133 m_terminalWidget
= m_konsolePart
->widget();
134 m_layout
->addWidget(m_terminalWidget
);
135 m_terminal
= qobject_cast
<TerminalInterface
*>(m_konsolePart
);
139 m_terminal
->showShellInDir(url().toLocalFile());
141 m_terminalWidget
->setFocus();
142 connect(m_konsolePart
, SIGNAL(currentDirectoryChanged(QString
)),
143 this, SLOT(slotKonsolePartCurrentDirectoryChanged(QString
)));
146 Panel::showEvent(event
);
149 void TerminalPanel::changeDir(const QUrl
& url
)
151 delete m_mostLocalUrlJob
;
152 m_mostLocalUrlJob
= nullptr;
154 if (url
.isLocalFile()) {
155 sendCdToTerminal(url
.toLocalFile());
157 m_mostLocalUrlJob
= KIO::mostLocalUrl(url
, KIO::HideProgressInfo
);
158 if (m_mostLocalUrlJob
->uiDelegate()) {
159 KJobWidgets::setWindow(m_mostLocalUrlJob
, this);
161 connect(m_mostLocalUrlJob
, &KIO::StatJob::result
, this, &TerminalPanel::slotMostLocalUrlResult
);
165 void TerminalPanel::sendCdToTerminal(const QString
& dir
, HistoryPolicy addToHistory
)
167 if (dir
== m_konsolePartCurrentDirectory
) {
168 m_clearTerminal
= false;
173 if (!m_clearTerminal
) {
174 // The TerminalV2 interface does not provide a way to delete the
175 // current line before sending a new input. This is mandatory,
176 // otherwise sending a 'cd x' to a existing 'rm -rf *' might
177 // result in data loss. As workaround SIGINT is sent.
178 const int processId
= m_terminal
->terminalProcessId();
180 kill(processId
, SIGINT
);
185 m_terminal
->sendInput(" cd " + KShell::quoteArg(dir
) + '\n');
187 // We want to ignore the currentDirectoryChanged(QString) signal, which we will receive after
188 // the directory change, because this directory change is not caused by a "cd" command that the
189 // user entered in the panel. Therefore, we have to remember 'dir'. Note that it could also be
190 // a symbolic link -> remember the 'canonical' path.
191 if (addToHistory
== HistoryPolicy::AddToHistory
)
192 m_sendCdToTerminalHistory
.enqueue(QDir(dir
).canonicalPath());
194 if (m_clearTerminal
) {
195 m_terminal
->sendInput(QStringLiteral(" clear\n"));
196 m_clearTerminal
= false;
200 void TerminalPanel::slotMostLocalUrlResult(KJob
* job
)
202 KIO::StatJob
* statJob
= static_cast<KIO::StatJob
*>(job
);
203 const QUrl url
= statJob
->mostLocalUrl();
204 if (url
.isLocalFile()) {
205 sendCdToTerminal(url
.toLocalFile());
208 m_mostLocalUrlJob
= nullptr;
211 void TerminalPanel::slotKonsolePartCurrentDirectoryChanged(const QString
& dir
)
213 m_konsolePartCurrentDirectory
= QDir(dir
).canonicalPath();
215 // Only emit a changeUrl signal if the directory change was caused by the user inside the
216 // terminal, and not by sendCdToTerminal(QString).
217 while (!m_sendCdToTerminalHistory
.empty()) {
218 if (m_konsolePartCurrentDirectory
== m_sendCdToTerminalHistory
.dequeue()) {
223 const QUrl
url(QUrl::fromLocalFile(dir
));