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()
47 m_layout
= new QVBoxLayout(this);
48 m_layout
->setMargin(0);
51 TerminalPanel::~TerminalPanel()
55 void TerminalPanel::terminalExited()
58 emit
hideTerminalPanel();
61 void TerminalPanel::dockVisibilityChanged()
63 // Only react when the DockWidget itself (not some parent) is hidden. This way we don't
64 // respond when e.g. Dolphin is minimized.
65 if (parentWidget() && parentWidget()->isHidden() &&
66 m_terminal
&& (m_terminal
->foregroundProcessId() == -1)) {
67 // Make sure that the following "cd /" command will not affect the view.
68 disconnect(m_konsolePart
, SIGNAL(currentDirectoryChanged(QString
)),
69 this, SLOT(slotKonsolePartCurrentDirectoryChanged(QString
)));
71 // Make sure this terminal does not prevent unmounting any removable drives
72 changeDir(KUrl::fromPath("/"));
74 // Because we have disconnected from the part's currentDirectoryChanged()
75 // signal, we have to update m_konsolePartCurrentDirectory manually. If this
76 // was not done, showing the panel again might not set the part's working
77 // directory correctly.
78 m_konsolePartCurrentDirectory
= '/';
82 bool TerminalPanel::urlChanged()
84 if (!url().isValid()) {
88 const bool sendInput
= m_terminal
&& (m_terminal
->foregroundProcessId() == -1) && isVisible();
96 void TerminalPanel::showEvent(QShowEvent
* event
)
98 if (event
->spontaneous()) {
99 Panel::showEvent(event
);
104 m_clearTerminal
= true;
105 KPluginFactory
* factory
= 0;
106 KService::Ptr service
= KService::serviceByDesktopName("konsolepart");
108 factory
= KPluginLoader(service
->library()).factory();
110 m_konsolePart
= factory
? (factory
->create
<KParts::ReadOnlyPart
>(this)) : 0;
112 connect(m_konsolePart
, SIGNAL(destroyed(QObject
*)), this, SLOT(terminalExited()));
113 m_terminalWidget
= m_konsolePart
->widget();
114 m_layout
->addWidget(m_terminalWidget
);
115 m_terminal
= qobject_cast
<TerminalInterfaceV2
*>(m_konsolePart
);
119 connect(m_konsolePart
, SIGNAL(currentDirectoryChanged(QString
)),
120 this, SLOT(slotKonsolePartCurrentDirectoryChanged(QString
)));
121 m_terminal
->showShellInDir(url().toLocalFile());
123 m_terminalWidget
->setFocus();
126 Panel::showEvent(event
);
129 void TerminalPanel::changeDir(const KUrl
& url
)
131 delete m_mostLocalUrlJob
;
132 m_mostLocalUrlJob
= 0;
134 if (url
.isLocalFile()) {
135 sendCdToTerminal(url
.toLocalFile());
137 m_mostLocalUrlJob
= KIO::mostLocalUrl(url
, KIO::HideProgressInfo
);
138 if (m_mostLocalUrlJob
->ui()) {
139 m_mostLocalUrlJob
->ui()->setWindow(this);
141 connect(m_mostLocalUrlJob
, SIGNAL(result(KJob
*)), this, SLOT(slotMostLocalUrlResult(KJob
*)));
145 void TerminalPanel::sendCdToTerminal(const QString
& dir
)
147 if (dir
== m_konsolePartCurrentDirectory
) {
148 m_clearTerminal
= false;
152 if (!m_clearTerminal
) {
153 // The TerminalV2 interface does not provide a way to delete the
154 // current line before sending a new input. This is mandatory,
155 // otherwise sending a 'cd x' to a existing 'rm -rf *' might
156 // result in data loss. As workaround SIGINT is send.
157 const int processId
= m_terminal
->terminalProcessId();
159 kill(processId
, SIGINT
);
163 m_terminal
->sendInput(" cd " + KShell::quoteArg(dir
) + '\n');
164 m_konsolePartCurrentDirectory
= dir
;
166 if (m_clearTerminal
) {
167 m_terminal
->sendInput(" clear\n");
168 m_clearTerminal
= false;
172 void TerminalPanel::slotMostLocalUrlResult(KJob
* job
)
174 KIO::StatJob
* statJob
= static_cast<KIO::StatJob
*>(job
);
175 const KUrl url
= statJob
->mostLocalUrl();
176 if (url
.isLocalFile()) {
177 sendCdToTerminal(url
.toLocalFile());
180 m_mostLocalUrlJob
= 0;
183 void TerminalPanel::slotKonsolePartCurrentDirectoryChanged(const QString
& dir
)
185 m_konsolePartCurrentDirectory
= dir
;
187 // Only change the view URL if 'dir' is different from the current view URL.
188 // Note that the current view URL could also be a symbolic link to 'dir'
189 // -> use QDir::canonicalPath() to check that.
190 const KUrl
oldUrl(url());
191 const KUrl
newUrl(dir
);
192 if (newUrl
!= oldUrl
&& dir
!= QDir(oldUrl
.path()).canonicalPath()) {
193 emit
changeUrl(newUrl
);
197 #include "terminalpanel.moc"