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),
46 m_konsolePartCurrentDirectory(),
47 m_sendCdToTerminalHistory()
49 m_layout
= new QVBoxLayout(this);
50 m_layout
->setMargin(0);
53 TerminalPanel::~TerminalPanel()
57 void TerminalPanel::terminalExited()
60 emit
hideTerminalPanel();
63 void TerminalPanel::dockVisibilityChanged()
65 // Only react when the DockWidget itself (not some parent) is hidden. This way we don't
66 // respond when e.g. Dolphin is minimized.
67 if (parentWidget() && parentWidget()->isHidden() &&
68 m_terminal
&& (m_terminal
->foregroundProcessId() == -1)) {
69 // Make sure that the following "cd /" command will not affect the view.
70 disconnect(m_konsolePart
, SIGNAL(currentDirectoryChanged(QString
)),
71 this, SLOT(slotKonsolePartCurrentDirectoryChanged(QString
)));
73 // Make sure this terminal does not prevent unmounting any removable drives
74 changeDir(QUrl::fromLocalFile(QStringLiteral("/")));
76 // Because we have disconnected from the part's currentDirectoryChanged()
77 // signal, we have to update m_konsolePartCurrentDirectory manually. If this
78 // was not done, showing the panel again might not set the part's working
79 // directory correctly.
80 m_konsolePartCurrentDirectory
= '/';
84 bool TerminalPanel::urlChanged()
86 if (!url().isValid()) {
90 const bool sendInput
= m_terminal
&& (m_terminal
->foregroundProcessId() == -1) && isVisible();
98 void TerminalPanel::showEvent(QShowEvent
* event
)
100 if (event
->spontaneous()) {
101 Panel::showEvent(event
);
106 m_clearTerminal
= true;
107 KPluginFactory
* factory
= 0;
108 KService::Ptr service
= KService::serviceByDesktopName(QStringLiteral("konsolepart"));
110 factory
= KPluginLoader(service
->library()).factory();
112 m_konsolePart
= factory
? (factory
->create
<KParts::ReadOnlyPart
>(this)) : 0;
114 connect(m_konsolePart
, &KParts::ReadOnlyPart::destroyed
, this, &TerminalPanel::terminalExited
);
115 m_terminalWidget
= m_konsolePart
->widget();
116 m_layout
->addWidget(m_terminalWidget
);
117 m_terminal
= qobject_cast
<TerminalInterface
*>(m_konsolePart
);
121 m_terminal
->showShellInDir(url().toLocalFile());
123 m_terminalWidget
->setFocus();
124 connect(m_konsolePart
, SIGNAL(currentDirectoryChanged(QString
)),
125 this, SLOT(slotKonsolePartCurrentDirectoryChanged(QString
)));
128 Panel::showEvent(event
);
131 void TerminalPanel::changeDir(const QUrl
& url
)
133 delete m_mostLocalUrlJob
;
134 m_mostLocalUrlJob
= 0;
136 if (url
.isLocalFile()) {
137 sendCdToTerminal(url
.toLocalFile());
139 m_mostLocalUrlJob
= KIO::mostLocalUrl(url
, KIO::HideProgressInfo
);
140 if (m_mostLocalUrlJob
->ui()) {
141 KJobWidgets::setWindow(m_mostLocalUrlJob
, this);
143 connect(m_mostLocalUrlJob
, &KIO::StatJob::result
, this, &TerminalPanel::slotMostLocalUrlResult
);
147 void TerminalPanel::sendCdToTerminal(const QString
& dir
)
149 if (dir
== m_konsolePartCurrentDirectory
) {
150 m_clearTerminal
= false;
154 if (!m_clearTerminal
) {
155 // The TerminalV2 interface does not provide a way to delete the
156 // current line before sending a new input. This is mandatory,
157 // otherwise sending a 'cd x' to a existing 'rm -rf *' might
158 // result in data loss. As workaround SIGINT is send.
159 const int processId
= m_terminal
->terminalProcessId();
161 kill(processId
, SIGINT
);
165 m_terminal
->sendInput(" cd " + KShell::quoteArg(dir
) + '\n');
167 // We want to ignore the currentDirectoryChanged(QString) signal, which we will receive after
168 // the directory change, because this directory change is not caused by a "cd" command that the
169 // user entered in the panel. Therefore, we have to remember 'dir'. Note that it could also be
170 // a symbolic link -> remember the 'canonical' path.
171 m_sendCdToTerminalHistory
.enqueue(QDir(dir
).canonicalPath());
173 if (m_clearTerminal
) {
174 m_terminal
->sendInput(QStringLiteral(" clear\n"));
175 m_clearTerminal
= false;
179 void TerminalPanel::slotMostLocalUrlResult(KJob
* job
)
181 KIO::StatJob
* statJob
= static_cast<KIO::StatJob
*>(job
);
182 const QUrl url
= statJob
->mostLocalUrl();
183 if (url
.isLocalFile()) {
184 sendCdToTerminal(url
.toLocalFile());
187 m_mostLocalUrlJob
= 0;
190 void TerminalPanel::slotKonsolePartCurrentDirectoryChanged(const QString
& dir
)
192 m_konsolePartCurrentDirectory
= QDir(dir
).canonicalPath();
194 // Only emit a changeUrl signal if the directory change was caused by the user inside the
195 // terminal, and not by sendCdToTerminal(QString).
196 while (!m_sendCdToTerminalHistory
.empty()) {
197 if (m_konsolePartCurrentDirectory
== m_sendCdToTerminalHistory
.dequeue()) {
202 const QUrl
url(QUrl::fromLocalFile(dir
));