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_v2.h>
28 #include <KParts/Part>
31 #include <KIO/JobUiDelegate>
37 TerminalPanel::TerminalPanel(QWidget
* parent
) :
39 m_clearTerminal(true),
45 m_konsolePartCurrentDirectory(),
46 m_sendCdToTerminalHistory()
48 m_layout
= new QVBoxLayout(this);
49 m_layout
->setMargin(0);
52 TerminalPanel::~TerminalPanel()
56 void TerminalPanel::terminalExited()
59 emit
hideTerminalPanel();
62 void TerminalPanel::dockVisibilityChanged()
64 // Only react when the DockWidget itself (not some parent) is hidden. This way we don't
65 // respond when e.g. Dolphin is minimized.
66 if (parentWidget() && parentWidget()->isHidden() &&
67 m_terminal
&& (m_terminal
->foregroundProcessId() == -1)) {
68 // Make sure that the following "cd /" command will not affect the view.
69 disconnect(m_konsolePart
, SIGNAL(currentDirectoryChanged(QString
)),
70 this, SLOT(slotKonsolePartCurrentDirectoryChanged(QString
)));
72 // Make sure this terminal does not prevent unmounting any removable drives
73 changeDir(KUrl::fromPath("/"));
75 // Because we have disconnected from the part's currentDirectoryChanged()
76 // signal, we have to update m_konsolePartCurrentDirectory manually. If this
77 // was not done, showing the panel again might not set the part's working
78 // directory correctly.
79 m_konsolePartCurrentDirectory
= '/';
83 bool TerminalPanel::urlChanged()
85 if (!url().isValid()) {
89 const bool sendInput
= m_terminal
&& (m_terminal
->foregroundProcessId() == -1) && isVisible();
97 void TerminalPanel::showEvent(QShowEvent
* event
)
99 if (event
->spontaneous()) {
100 Panel::showEvent(event
);
105 m_clearTerminal
= true;
106 KPluginFactory
* factory
= 0;
107 KService::Ptr service
= KService::serviceByDesktopName("konsolepart");
109 factory
= KPluginLoader(service
->library()).factory();
111 m_konsolePart
= factory
? (factory
->create
<KParts::ReadOnlyPart
>(this)) : 0;
113 connect(m_konsolePart
, SIGNAL(destroyed(QObject
*)), this, SLOT(terminalExited()));
114 m_terminalWidget
= m_konsolePart
->widget();
115 m_layout
->addWidget(m_terminalWidget
);
116 m_terminal
= qobject_cast
<TerminalInterfaceV2
*>(m_konsolePart
);
120 m_terminal
->showShellInDir(url().toLocalFile());
122 m_terminalWidget
->setFocus();
123 connect(m_konsolePart
, SIGNAL(currentDirectoryChanged(QString
)),
124 this, SLOT(slotKonsolePartCurrentDirectoryChanged(QString
)));
127 Panel::showEvent(event
);
130 void TerminalPanel::changeDir(const KUrl
& url
)
132 delete m_mostLocalUrlJob
;
133 m_mostLocalUrlJob
= 0;
135 if (url
.isLocalFile()) {
136 sendCdToTerminal(url
.toLocalFile());
138 m_mostLocalUrlJob
= KIO::mostLocalUrl(url
, KIO::HideProgressInfo
);
139 if (m_mostLocalUrlJob
->ui()) {
140 m_mostLocalUrlJob
->ui()->setWindow(this);
142 connect(m_mostLocalUrlJob
, SIGNAL(result(KJob
*)), this, SLOT(slotMostLocalUrlResult(KJob
*)));
146 void TerminalPanel::sendCdToTerminal(const QString
& dir
)
148 if (dir
== m_konsolePartCurrentDirectory
) {
149 m_clearTerminal
= false;
153 if (!m_clearTerminal
) {
154 // The TerminalV2 interface does not provide a way to delete the
155 // current line before sending a new input. This is mandatory,
156 // otherwise sending a 'cd x' to a existing 'rm -rf *' might
157 // result in data loss. As workaround SIGINT is send.
158 const int processId
= m_terminal
->terminalProcessId();
160 kill(processId
, SIGINT
);
164 m_terminal
->sendInput(" cd " + KShell::quoteArg(dir
) + '\n');
166 // We want to ignore the currentDirectoryChanged(QString) signal, which we will receive after
167 // the directory change, because this directory change is not caused by a "cd" command that the
168 // user entered in the panel. Therefore, we have to remember 'dir'. Note that it could also be
169 // a symbolic link -> remember the 'canonical' path.
170 m_sendCdToTerminalHistory
.enqueue(QDir(dir
).canonicalPath());
172 if (m_clearTerminal
) {
173 m_terminal
->sendInput(" clear\n");
174 m_clearTerminal
= false;
178 void TerminalPanel::slotMostLocalUrlResult(KJob
* job
)
180 KIO::StatJob
* statJob
= static_cast<KIO::StatJob
*>(job
);
181 const KUrl url
= statJob
->mostLocalUrl();
182 if (url
.isLocalFile()) {
183 sendCdToTerminal(url
.toLocalFile());
186 m_mostLocalUrlJob
= 0;
189 void TerminalPanel::slotKonsolePartCurrentDirectoryChanged(const QString
& dir
)
191 m_konsolePartCurrentDirectory
= QDir(dir
).canonicalPath();
193 // Only emit a changeUrl signal if the directory change was caused by the user inside the
194 // terminal, and not by sendCdToTerminal(QString).
195 while (!m_sendCdToTerminalHistory
.empty()) {
196 if (m_konsolePartCurrentDirectory
== m_sendCdToTerminalHistory
.dequeue()) {
204 #include "terminalpanel.moc"