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/Part>
31 #include <KIO/JobUiDelegate>
32 #include <KJobWidgets>
38 TerminalPanel::TerminalPanel(QWidget
* parent
) :
40 m_clearTerminal(true),
46 m_konsolePartCurrentDirectory()
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
, &KParts::ReadOnlyPart::destroyed
, this, &TerminalPanel::terminalExited
);
114 m_terminalWidget
= m_konsolePart
->widget();
115 m_layout
->addWidget(m_terminalWidget
);
116 m_terminal
= qobject_cast
<TerminalInterface
*>(m_konsolePart
);
120 connect(m_konsolePart
, SIGNAL(currentDirectoryChanged(QString
)),
121 this, SLOT(slotKonsolePartCurrentDirectoryChanged(QString
)));
122 m_terminal
->showShellInDir(url().toLocalFile());
124 m_terminalWidget
->setFocus();
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 KJobWidgets::setWindow(m_mostLocalUrlJob
, this);
142 connect(m_mostLocalUrlJob
, &KIO::StatJob::result
, this, &TerminalPanel::slotMostLocalUrlResult
);
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');
165 m_konsolePartCurrentDirectory
= dir
;
167 if (m_clearTerminal
) {
168 m_terminal
->sendInput(" clear\n");
169 m_clearTerminal
= false;
173 void TerminalPanel::slotMostLocalUrlResult(KJob
* job
)
175 KIO::StatJob
* statJob
= static_cast<KIO::StatJob
*>(job
);
176 const KUrl url
= statJob
->mostLocalUrl();
177 if (url
.isLocalFile()) {
178 sendCdToTerminal(url
.toLocalFile());
181 m_mostLocalUrlJob
= 0;
184 void TerminalPanel::slotKonsolePartCurrentDirectoryChanged(const QString
& dir
)
186 m_konsolePartCurrentDirectory
= dir
;
188 // Only change the view URL if 'dir' is different from the current view URL.
189 // Note that the current view URL could also be a symbolic link to 'dir'
190 // -> use QDir::canonicalPath() to check that.
191 const KUrl
oldUrl(url());
192 const KUrl
newUrl(dir
);
193 if (newUrl
!= oldUrl
&& dir
!= QDir(oldUrl
.path()).canonicalPath()) {
194 emit
changeUrl(newUrl
);