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::goHome()
59 sendCdToTerminal(QDir::homePath(), HistoryPolicy::SkipHistory
);
62 QString
TerminalPanel::currentWorkingDirectory()
65 return m_terminal
->currentWorkingDirectory();
70 void TerminalPanel::terminalExited()
73 emit
hideTerminalPanel();
76 void TerminalPanel::dockVisibilityChanged()
78 // Only react when the DockWidget itself (not some parent) is hidden. This way we don't
79 // respond when e.g. Dolphin is minimized.
80 if (parentWidget() && parentWidget()->isHidden() &&
81 m_terminal
&& (m_terminal
->foregroundProcessId() == -1)) {
82 // Make sure that the following "cd /" command will not affect the view.
83 disconnect(m_konsolePart
, SIGNAL(currentDirectoryChanged(QString
)),
84 this, SLOT(slotKonsolePartCurrentDirectoryChanged(QString
)));
86 // Make sure this terminal does not prevent unmounting any removable drives
87 changeDir(QUrl::fromLocalFile(QStringLiteral("/")));
89 // Because we have disconnected from the part's currentDirectoryChanged()
90 // signal, we have to update m_konsolePartCurrentDirectory manually. If this
91 // was not done, showing the panel again might not set the part's working
92 // directory correctly.
93 m_konsolePartCurrentDirectory
= '/';
97 bool TerminalPanel::urlChanged()
99 if (!url().isValid()) {
103 const bool sendInput
= m_terminal
&& (m_terminal
->foregroundProcessId() == -1) && isVisible();
111 void TerminalPanel::showEvent(QShowEvent
* event
)
113 if (event
->spontaneous()) {
114 Panel::showEvent(event
);
119 m_clearTerminal
= true;
120 KPluginFactory
* factory
= 0;
121 KService::Ptr service
= KService::serviceByDesktopName(QStringLiteral("konsolepart"));
123 factory
= KPluginLoader(service
->library()).factory();
125 m_konsolePart
= factory
? (factory
->create
<KParts::ReadOnlyPart
>(this)) : 0;
127 connect(m_konsolePart
, &KParts::ReadOnlyPart::destroyed
, this, &TerminalPanel::terminalExited
);
128 m_terminalWidget
= m_konsolePart
->widget();
129 m_layout
->addWidget(m_terminalWidget
);
130 m_terminal
= qobject_cast
<TerminalInterface
*>(m_konsolePart
);
134 m_terminal
->showShellInDir(url().toLocalFile());
136 m_terminalWidget
->setFocus();
137 connect(m_konsolePart
, SIGNAL(currentDirectoryChanged(QString
)),
138 this, SLOT(slotKonsolePartCurrentDirectoryChanged(QString
)));
141 Panel::showEvent(event
);
144 void TerminalPanel::changeDir(const QUrl
& url
)
146 delete m_mostLocalUrlJob
;
147 m_mostLocalUrlJob
= 0;
149 if (url
.isLocalFile()) {
150 sendCdToTerminal(url
.toLocalFile());
152 m_mostLocalUrlJob
= KIO::mostLocalUrl(url
, KIO::HideProgressInfo
);
153 if (m_mostLocalUrlJob
->uiDelegate()) {
154 KJobWidgets::setWindow(m_mostLocalUrlJob
, this);
156 connect(m_mostLocalUrlJob
, &KIO::StatJob::result
, this, &TerminalPanel::slotMostLocalUrlResult
);
160 void TerminalPanel::sendCdToTerminal(const QString
& dir
, HistoryPolicy addToHistory
)
162 if (dir
== m_konsolePartCurrentDirectory
) {
163 m_clearTerminal
= false;
167 if (!m_clearTerminal
) {
168 // The TerminalV2 interface does not provide a way to delete the
169 // current line before sending a new input. This is mandatory,
170 // otherwise sending a 'cd x' to a existing 'rm -rf *' might
171 // result in data loss. As workaround SIGINT is send.
172 const int processId
= m_terminal
->terminalProcessId();
174 kill(processId
, SIGINT
);
178 m_terminal
->sendInput(" cd " + KShell::quoteArg(dir
) + '\n');
180 // We want to ignore the currentDirectoryChanged(QString) signal, which we will receive after
181 // the directory change, because this directory change is not caused by a "cd" command that the
182 // user entered in the panel. Therefore, we have to remember 'dir'. Note that it could also be
183 // a symbolic link -> remember the 'canonical' path.
184 if (addToHistory
== HistoryPolicy::AddToHistory
)
185 m_sendCdToTerminalHistory
.enqueue(QDir(dir
).canonicalPath());
187 if (m_clearTerminal
) {
188 m_terminal
->sendInput(QStringLiteral(" clear\n"));
189 m_clearTerminal
= false;
193 void TerminalPanel::slotMostLocalUrlResult(KJob
* job
)
195 KIO::StatJob
* statJob
= static_cast<KIO::StatJob
*>(job
);
196 const QUrl url
= statJob
->mostLocalUrl();
197 if (url
.isLocalFile()) {
198 sendCdToTerminal(url
.toLocalFile());
201 m_mostLocalUrlJob
= 0;
204 void TerminalPanel::slotKonsolePartCurrentDirectoryChanged(const QString
& dir
)
206 m_konsolePartCurrentDirectory
= QDir(dir
).canonicalPath();
208 // Only emit a changeUrl signal if the directory change was caused by the user inside the
209 // terminal, and not by sendCdToTerminal(QString).
210 while (!m_sendCdToTerminalHistory
.empty()) {
211 if (m_konsolePartCurrentDirectory
== m_sendCdToTerminalHistory
.dequeue()) {
216 const QUrl
url(QUrl::fromLocalFile(dir
));