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"
24 #include <KPluginLoader>
25 #include <KPluginFactory>
27 #include <kde_terminal_interface.h>
28 #include <KParts/ReadOnlyPart>
31 #include <KIO/JobUiDelegate>
32 #include <KJobWidgets>
36 #include <QVBoxLayout>
38 TerminalPanel::TerminalPanel(QWidget
* parent
) :
40 m_clearTerminal(true),
41 m_mostLocalUrlJob(nullptr),
44 m_terminalWidget(nullptr),
45 m_konsolePart(nullptr),
46 m_konsolePartCurrentDirectory(),
47 m_sendCdToTerminalHistory()
49 m_layout
= new QVBoxLayout(this);
50 m_layout
->setMargin(0);
53 TerminalPanel::~TerminalPanel()
57 void TerminalPanel::goHome()
59 sendCdToTerminal(QDir::homePath(), HistoryPolicy::SkipHistory
);
62 QString
TerminalPanel::currentWorkingDirectory()
65 return m_terminal
->currentWorkingDirectory();
70 void TerminalPanel::terminalExited()
73 emit
hideTerminalPanel();
76 bool TerminalPanel::isHiddenInVisibleWindow()
79 && parentWidget()->isHidden()
81 && (m_terminal
->foregroundProcessId() == -1);
84 void TerminalPanel::dockVisibilityChanged()
86 // Only react when the DockWidget itself (not some parent) is hidden. This way we don't
87 // respond when e.g. Dolphin is minimized.
88 if (isHiddenInVisibleWindow()) {
89 // Make sure that the following "cd /" command will not affect the view.
90 disconnect(m_konsolePart
, SIGNAL(currentDirectoryChanged(QString
)),
91 this, SLOT(slotKonsolePartCurrentDirectoryChanged(QString
)));
93 // Make sure this terminal does not prevent unmounting any removable drives
94 changeDir(QUrl::fromLocalFile(QStringLiteral("/")));
96 // Because we have disconnected from the part's currentDirectoryChanged()
97 // signal, we have to update m_konsolePartCurrentDirectory manually. If this
98 // was not done, showing the panel again might not set the part's working
99 // directory correctly.
100 m_konsolePartCurrentDirectory
= '/';
104 bool TerminalPanel::urlChanged()
106 if (!url().isValid()) {
110 const bool sendInput
= m_terminal
&& (m_terminal
->foregroundProcessId() == -1) && isVisible();
118 void TerminalPanel::showEvent(QShowEvent
* event
)
120 if (event
->spontaneous()) {
121 Panel::showEvent(event
);
126 m_clearTerminal
= true;
127 KPluginFactory
* factory
= nullptr;
128 KService::Ptr service
= KService::serviceByDesktopName(QStringLiteral("konsolepart"));
130 factory
= KPluginLoader(service
->library()).factory();
132 m_konsolePart
= factory
? (factory
->create
<KParts::ReadOnlyPart
>(this)) : nullptr;
134 connect(m_konsolePart
, &KParts::ReadOnlyPart::destroyed
, this, &TerminalPanel::terminalExited
);
135 m_terminalWidget
= m_konsolePart
->widget();
136 m_layout
->addWidget(m_terminalWidget
);
137 m_terminal
= qobject_cast
<TerminalInterface
*>(m_konsolePart
);
141 m_terminal
->showShellInDir(url().toLocalFile());
143 m_terminalWidget
->setFocus();
144 connect(m_konsolePart
, SIGNAL(currentDirectoryChanged(QString
)),
145 this, SLOT(slotKonsolePartCurrentDirectoryChanged(QString
)));
148 Panel::showEvent(event
);
151 void TerminalPanel::changeDir(const QUrl
& url
)
153 delete m_mostLocalUrlJob
;
154 m_mostLocalUrlJob
= nullptr;
156 if (url
.isLocalFile()) {
157 sendCdToTerminal(url
.toLocalFile());
159 m_mostLocalUrlJob
= KIO::mostLocalUrl(url
, KIO::HideProgressInfo
);
160 if (m_mostLocalUrlJob
->uiDelegate()) {
161 KJobWidgets::setWindow(m_mostLocalUrlJob
, this);
163 connect(m_mostLocalUrlJob
, &KIO::StatJob::result
, this, &TerminalPanel::slotMostLocalUrlResult
);
167 void TerminalPanel::sendCdToTerminal(const QString
& dir
, HistoryPolicy addToHistory
)
169 if (dir
== m_konsolePartCurrentDirectory
) {
170 m_clearTerminal
= false;
174 if (!m_clearTerminal
) {
175 // The TerminalV2 interface does not provide a way to delete the
176 // current line before sending a new input. This is mandatory,
177 // otherwise sending a 'cd x' to a existing 'rm -rf *' might
178 // result in data loss. As workaround SIGINT is sent.
179 const int processId
= m_terminal
->terminalProcessId();
181 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
));