]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/terminal/terminalpanel.cpp
Internal cleanup for panels: Let the panel-implementations decide whether they accept...
[dolphin.git] / src / panels / terminal / terminalpanel.cpp
1 /***************************************************************************
2 * Copyright (C) 2007-2010 by Peter Penz <peter.penz@gmx.at> *
3 * *
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. *
8 * *
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. *
13 * *
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 ***************************************************************************/
19
20 #include "terminalpanel.h"
21
22 #include <klibloader.h>
23 #include <kde_terminal_interface_v2.h>
24 #include <kparts/part.h>
25 #include <kshell.h>
26 #include <kio/job.h>
27 #include <KIO/JobUiDelegate>
28
29 #include <QBoxLayout>
30 #include <QShowEvent>
31
32 TerminalPanel::TerminalPanel(QWidget* parent) :
33 Panel(parent),
34 m_clearTerminal(true),
35 m_mostLocalUrlJob(0),
36 m_layout(0),
37 m_terminal(0),
38 m_terminalWidget(0)
39 {
40 m_layout = new QVBoxLayout(this);
41 m_layout->setMargin(0);
42 }
43
44 TerminalPanel::~TerminalPanel()
45 {
46 }
47
48 QSize TerminalPanel::sizeHint() const
49 {
50 QSize size = Panel::sizeHint();
51 size.setHeight(200);
52 return size;
53 }
54
55 void TerminalPanel::terminalExited()
56 {
57 emit hideTerminalPanel();
58 m_terminal = 0;
59 }
60
61 bool TerminalPanel::urlChanged()
62 {
63 if (!url().isValid()) {
64 return false;
65 }
66
67 const bool sendInput = (m_terminal != 0)
68 && (m_terminal->foregroundProcessId() == -1)
69 && isVisible();
70 if (sendInput) {
71 changeDir(url());
72 }
73
74 return true;
75 }
76
77 void TerminalPanel::showEvent(QShowEvent* event)
78 {
79 if (event->spontaneous()) {
80 Panel::showEvent(event);
81 return;
82 }
83
84 if (m_terminal == 0) {
85 m_clearTerminal = true;
86 KPluginFactory* factory = KPluginLoader("libkonsolepart").factory();
87 KParts::ReadOnlyPart* part = factory ? (factory->create<KParts::ReadOnlyPart>(this)) : 0;
88 if (part != 0) {
89 connect(part, SIGNAL(destroyed(QObject*)), this, SLOT(terminalExited()));
90 m_terminalWidget = part->widget();
91 m_layout->addWidget(m_terminalWidget);
92 m_terminal = qobject_cast<TerminalInterfaceV2 *>(part);
93 }
94 }
95 if (m_terminal != 0) {
96 m_terminal->showShellInDir(url().toLocalFile());
97 changeDir(url());
98 m_terminalWidget->setFocus();
99 }
100
101 Panel::showEvent(event);
102 }
103
104 void TerminalPanel::changeDir(const KUrl& url)
105 {
106 delete m_mostLocalUrlJob;
107 m_mostLocalUrlJob = 0;
108
109 if (url.isLocalFile()) {
110 sendCdToTerminal(url.toLocalFile());
111 } else {
112 m_mostLocalUrlJob = KIO::mostLocalUrl(url, KIO::HideProgressInfo);
113 m_mostLocalUrlJob->ui()->setWindow(this);
114 connect(m_mostLocalUrlJob, SIGNAL(result(KJob*)), this, SLOT(slotMostLocalUrlResult(KJob*)));
115 }
116 }
117
118 void TerminalPanel::sendCdToTerminal(const QString& dir)
119 {
120 if (!m_clearTerminal) {
121 // The TerminalV2 interface does not provide a way to delete the
122 // current line before sending a new input. This is mandatory,
123 // otherwise sending a 'cd x' to a existing 'rm -rf *' might
124 // result in data loss. As workaround Ctrl+C is send.
125 QString cancel;
126 cancel.append(QChar(3));
127 cancel.append(QChar('c'));
128 m_terminal->sendInput(cancel);
129 }
130
131 m_terminal->sendInput("cd " + KShell::quoteArg(dir) + '\n');
132
133 if (m_clearTerminal) {
134 m_terminal->sendInput("clear\n");
135 m_clearTerminal = false;
136 }
137 }
138
139 void TerminalPanel::slotMostLocalUrlResult(KJob* job)
140 {
141 KIO::StatJob* statJob = static_cast<KIO::StatJob *>(job);
142 const KUrl url = statJob->mostLocalUrl();
143 if (url.isLocalFile()) {
144 sendCdToTerminal(url.toLocalFile());
145 }
146
147 m_mostLocalUrlJob = 0;
148 }
149
150 #include "terminalpanel.moc"