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>
26 #include <kde_terminal_interface_v2.h>
27 #include <KParts/Part>
30 #include <KIO/JobUiDelegate>
35 TerminalPanel::TerminalPanel(QWidget
* parent
) :
37 m_clearTerminal(true),
43 m_konsolePartCurrentDirectory()
45 m_layout
= new QVBoxLayout(this);
46 m_layout
->setMargin(0);
49 TerminalPanel::~TerminalPanel()
53 void TerminalPanel::terminalExited()
56 emit
hideTerminalPanel();
59 void TerminalPanel::dockVisibilityChanged()
61 // Only react when the DockWidget itself (not some parent) is hidden. This way we don't
62 // respond when e.g. Dolphin is minimized.
63 if (parentWidget() && parentWidget()->isHidden() &&
64 m_terminal
&& (m_terminal
->foregroundProcessId() == -1)) {
65 // Make sure that the following "cd /" command will not affect the view.
66 disconnect(m_konsolePart
, SIGNAL(currentDirectoryChanged(QString
)),
67 this, SLOT(slotKonsolePartCurrentDirectoryChanged(QString
)));
69 // Make sure this terminal does not prevent unmounting any removable drives
70 changeDir(KUrl::fromPath("/"));
72 // Because we have disconnected from the part's currentDirectoryChanged()
73 // signal, we have to update m_konsolePartCurrentDirectory manually. If this
74 // was not done, showing the panel again might not set the part's working
75 // directory correctly.
76 m_konsolePartCurrentDirectory
= "/";
80 bool TerminalPanel::urlChanged()
82 if (!url().isValid()) {
86 const bool sendInput
= m_terminal
&& (m_terminal
->foregroundProcessId() == -1) && isVisible();
94 void TerminalPanel::showEvent(QShowEvent
* event
)
96 if (event
->spontaneous()) {
97 Panel::showEvent(event
);
102 m_clearTerminal
= true;
103 KPluginFactory
* factory
= KPluginLoader("libkonsolepart").factory();
104 m_konsolePart
= factory
? (factory
->create
<KParts::ReadOnlyPart
>(this)) : 0;
106 connect(m_konsolePart
, SIGNAL(destroyed(QObject
*)), this, SLOT(terminalExited()));
107 m_terminalWidget
= m_konsolePart
->widget();
108 m_layout
->addWidget(m_terminalWidget
);
109 m_terminal
= qobject_cast
<TerminalInterfaceV2
*>(m_konsolePart
);
113 connect(m_konsolePart
, SIGNAL(currentDirectoryChanged(QString
)),
114 this, SLOT(slotKonsolePartCurrentDirectoryChanged(QString
)));
115 m_terminal
->showShellInDir(url().toLocalFile());
117 m_terminalWidget
->setFocus();
120 Panel::showEvent(event
);
123 void TerminalPanel::changeDir(const KUrl
& url
)
125 delete m_mostLocalUrlJob
;
126 m_mostLocalUrlJob
= 0;
128 if (url
.isLocalFile()) {
129 sendCdToTerminal(url
.toLocalFile());
131 m_mostLocalUrlJob
= KIO::mostLocalUrl(url
, KIO::HideProgressInfo
);
132 if (m_mostLocalUrlJob
->ui()) {
133 m_mostLocalUrlJob
->ui()->setWindow(this);
135 connect(m_mostLocalUrlJob
, SIGNAL(result(KJob
*)), this, SLOT(slotMostLocalUrlResult(KJob
*)));
139 void TerminalPanel::sendCdToTerminal(const QString
& dir
)
141 if (dir
== m_konsolePartCurrentDirectory
) {
145 if (!m_clearTerminal
) {
146 // The TerminalV2 interface does not provide a way to delete the
147 // current line before sending a new input. This is mandatory,
148 // otherwise sending a 'cd x' to a existing 'rm -rf *' might
149 // result in data loss. As workaround SIGINT is send.
150 const int processId
= m_terminal
->terminalProcessId();
152 kill(processId
, SIGINT
);
156 m_terminal
->sendInput("cd " + KShell::quoteArg(dir
) + '\n');
158 if (m_clearTerminal
) {
159 m_terminal
->sendInput("clear\n");
160 m_clearTerminal
= false;
164 void TerminalPanel::slotMostLocalUrlResult(KJob
* job
)
166 KIO::StatJob
* statJob
= static_cast<KIO::StatJob
*>(job
);
167 const KUrl url
= statJob
->mostLocalUrl();
168 if (url
.isLocalFile()) {
169 sendCdToTerminal(url
.toLocalFile());
172 m_mostLocalUrlJob
= 0;
175 void TerminalPanel::slotKonsolePartCurrentDirectoryChanged(const QString
& dir
)
177 m_konsolePartCurrentDirectory
= dir
;
179 const KUrl
newUrl(dir
);
180 if (newUrl
!= url()) {
181 emit
changeUrl(newUrl
);
185 #include "terminalpanel.moc"