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>
36 TerminalPanel::TerminalPanel(QWidget
* parent
) :
38 m_clearTerminal(true),
44 m_konsolePartCurrentDirectory()
46 m_layout
= new QVBoxLayout(this);
47 m_layout
->setMargin(0);
50 TerminalPanel::~TerminalPanel()
54 void TerminalPanel::terminalExited()
57 emit
hideTerminalPanel();
60 void TerminalPanel::dockVisibilityChanged()
62 // Only react when the DockWidget itself (not some parent) is hidden. This way we don't
63 // respond when e.g. Dolphin is minimized.
64 if (parentWidget() && parentWidget()->isHidden() &&
65 m_terminal
&& (m_terminal
->foregroundProcessId() == -1)) {
66 // Make sure that the following "cd /" command will not affect the view.
67 disconnect(m_konsolePart
, SIGNAL(currentDirectoryChanged(QString
)),
68 this, SLOT(slotKonsolePartCurrentDirectoryChanged(QString
)));
70 // Make sure this terminal does not prevent unmounting any removable drives
71 changeDir(KUrl::fromPath("/"));
73 // Because we have disconnected from the part's currentDirectoryChanged()
74 // signal, we have to update m_konsolePartCurrentDirectory manually. If this
75 // was not done, showing the panel again might not set the part's working
76 // directory correctly.
77 m_konsolePartCurrentDirectory
= '/';
81 bool TerminalPanel::urlChanged()
83 if (!url().isValid()) {
87 const bool sendInput
= m_terminal
&& (m_terminal
->foregroundProcessId() == -1) && isVisible();
95 void TerminalPanel::showEvent(QShowEvent
* event
)
97 if (event
->spontaneous()) {
98 Panel::showEvent(event
);
103 m_clearTerminal
= true;
104 KPluginFactory
* factory
= 0;
105 KService::Ptr service
= KService::serviceByDesktopName("konsolepart");
107 factory
= KPluginLoader(service
->library()).factory();
109 m_konsolePart
= factory
? (factory
->create
<KParts::ReadOnlyPart
>(this)) : 0;
111 connect(m_konsolePart
, SIGNAL(destroyed(QObject
*)), this, SLOT(terminalExited()));
112 m_terminalWidget
= m_konsolePart
->widget();
113 m_layout
->addWidget(m_terminalWidget
);
114 m_terminal
= qobject_cast
<TerminalInterfaceV2
*>(m_konsolePart
);
118 connect(m_konsolePart
, SIGNAL(currentDirectoryChanged(QString
)),
119 this, SLOT(slotKonsolePartCurrentDirectoryChanged(QString
)));
120 m_terminal
->showShellInDir(url().toLocalFile());
122 m_terminalWidget
->setFocus();
125 Panel::showEvent(event
);
128 void TerminalPanel::changeDir(const KUrl
& url
)
130 delete m_mostLocalUrlJob
;
131 m_mostLocalUrlJob
= 0;
133 if (url
.isLocalFile()) {
134 sendCdToTerminal(url
.toLocalFile());
136 m_mostLocalUrlJob
= KIO::mostLocalUrl(url
, KIO::HideProgressInfo
);
137 if (m_mostLocalUrlJob
->ui()) {
138 m_mostLocalUrlJob
->ui()->setWindow(this);
140 connect(m_mostLocalUrlJob
, SIGNAL(result(KJob
*)), this, SLOT(slotMostLocalUrlResult(KJob
*)));
144 void TerminalPanel::sendCdToTerminal(const QString
& dir
)
146 if (dir
== m_konsolePartCurrentDirectory
) {
147 m_clearTerminal
= false;
151 if (!m_clearTerminal
) {
152 // The TerminalV2 interface does not provide a way to delete the
153 // current line before sending a new input. This is mandatory,
154 // otherwise sending a 'cd x' to a existing 'rm -rf *' might
155 // result in data loss. As workaround SIGINT is send.
156 const int processId
= m_terminal
->terminalProcessId();
158 kill(processId
, SIGINT
);
162 m_terminal
->sendInput(" cd " + KShell::quoteArg(dir
) + '\n');
163 m_konsolePartCurrentDirectory
= dir
;
165 if (m_clearTerminal
) {
166 m_terminal
->sendInput(" clear\n");
167 m_clearTerminal
= false;
171 void TerminalPanel::slotMostLocalUrlResult(KJob
* job
)
173 KIO::StatJob
* statJob
= static_cast<KIO::StatJob
*>(job
);
174 const KUrl url
= statJob
->mostLocalUrl();
175 if (url
.isLocalFile()) {
176 sendCdToTerminal(url
.toLocalFile());
179 m_mostLocalUrlJob
= 0;
182 void TerminalPanel::slotKonsolePartCurrentDirectoryChanged(const QString
& dir
)
184 m_konsolePartCurrentDirectory
= dir
;
186 const KUrl
newUrl(dir
);
187 if (newUrl
!= url()) {
188 emit
changeUrl(newUrl
);
192 #include "terminalpanel.moc"