]>
cloud.milkyroute.net Git - dolphin.git/blob - src/panels/terminal/terminalpanel.cpp
1 /***************************************************************************
2 * Copyright (C) 2007-2010 by Peter Penz <peter.penz@gmx.at> *
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"
22 #include <kpluginloader.h>
23 #include <kpluginfactory.h>
24 #include <kde_terminal_interface_v2.h>
25 #include <kparts/part.h>
28 #include <KIO/JobUiDelegate>
33 TerminalPanel::TerminalPanel(QWidget
* parent
) :
35 m_clearTerminal(true),
41 m_layout
= new QVBoxLayout(this);
42 m_layout
->setMargin(0);
45 TerminalPanel::~TerminalPanel()
49 QSize
TerminalPanel::sizeHint() const
51 QSize size
= Panel::sizeHint();
56 void TerminalPanel::terminalExited()
58 emit
hideTerminalPanel();
62 bool TerminalPanel::urlChanged()
64 if (!url().isValid()) {
68 const bool sendInput
= (m_terminal
!= 0)
69 && (m_terminal
->foregroundProcessId() == -1)
78 void TerminalPanel::showEvent(QShowEvent
* event
)
80 if (event
->spontaneous()) {
81 Panel::showEvent(event
);
85 if (m_terminal
== 0) {
86 m_clearTerminal
= true;
87 KPluginFactory
* factory
= KPluginLoader("libkonsolepart").factory();
88 KParts::ReadOnlyPart
* part
= factory
? (factory
->create
<KParts::ReadOnlyPart
>(this)) : 0;
90 connect(part
, SIGNAL(destroyed(QObject
*)), this, SLOT(terminalExited()));
91 m_terminalWidget
= part
->widget();
92 m_layout
->addWidget(m_terminalWidget
);
93 m_terminal
= qobject_cast
<TerminalInterfaceV2
*>(part
);
96 if (m_terminal
!= 0) {
97 m_terminal
->showShellInDir(url().toLocalFile());
99 m_terminalWidget
->setFocus();
102 Panel::showEvent(event
);
105 void TerminalPanel::changeDir(const KUrl
& url
)
107 delete m_mostLocalUrlJob
;
108 m_mostLocalUrlJob
= 0;
110 if (url
.isLocalFile()) {
111 sendCdToTerminal(url
.toLocalFile());
113 m_mostLocalUrlJob
= KIO::mostLocalUrl(url
, KIO::HideProgressInfo
);
114 m_mostLocalUrlJob
->ui()->setWindow(this);
115 connect(m_mostLocalUrlJob
, SIGNAL(result(KJob
*)), this, SLOT(slotMostLocalUrlResult(KJob
*)));
119 void TerminalPanel::sendCdToTerminal(const QString
& dir
)
121 if (!m_clearTerminal
) {
122 // The TerminalV2 interface does not provide a way to delete the
123 // current line before sending a new input. This is mandatory,
124 // otherwise sending a 'cd x' to a existing 'rm -rf *' might
125 // result in data loss. As workaround Ctrl+C is send.
127 cancel
.append(QChar(3));
128 cancel
.append(QChar('c'));
129 m_terminal
->sendInput(cancel
);
132 m_terminal
->sendInput("cd " + KShell::quoteArg(dir
) + '\n');
134 if (m_clearTerminal
) {
135 m_terminal
->sendInput("clear\n");
136 m_clearTerminal
= false;
140 void TerminalPanel::slotMostLocalUrlResult(KJob
* job
)
142 KIO::StatJob
* statJob
= static_cast<KIO::StatJob
*>(job
);
143 const KUrl url
= statJob
->mostLocalUrl();
144 if (url
.isLocalFile()) {
145 sendCdToTerminal(url
.toLocalFile());
148 m_mostLocalUrlJob
= 0;
151 #include "terminalpanel.moc"