]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/terminal/terminalpanel.cpp
Cleanup KLibLoader use.
[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 <kpluginloader.h>
23 #include <kpluginfactory.h>
24 #include <kde_terminal_interface_v2.h>
25 #include <kparts/part.h>
26 #include <kshell.h>
27 #include <kio/job.h>
28 #include <KIO/JobUiDelegate>
29
30 #include <QBoxLayout>
31 #include <QShowEvent>
32
33 TerminalPanel::TerminalPanel(QWidget* parent) :
34 Panel(parent),
35 m_clearTerminal(true),
36 m_mostLocalUrlJob(0),
37 m_layout(0),
38 m_terminal(0),
39 m_terminalWidget(0)
40 {
41 m_layout = new QVBoxLayout(this);
42 m_layout->setMargin(0);
43 }
44
45 TerminalPanel::~TerminalPanel()
46 {
47 }
48
49 QSize TerminalPanel::sizeHint() const
50 {
51 QSize size = Panel::sizeHint();
52 size.setHeight(200);
53 return size;
54 }
55
56 void TerminalPanel::terminalExited()
57 {
58 emit hideTerminalPanel();
59 m_terminal = 0;
60 }
61
62 bool TerminalPanel::urlChanged()
63 {
64 if (!url().isValid()) {
65 return false;
66 }
67
68 const bool sendInput = (m_terminal != 0)
69 && (m_terminal->foregroundProcessId() == -1)
70 && isVisible();
71 if (sendInput) {
72 changeDir(url());
73 }
74
75 return true;
76 }
77
78 void TerminalPanel::showEvent(QShowEvent* event)
79 {
80 if (event->spontaneous()) {
81 Panel::showEvent(event);
82 return;
83 }
84
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;
89 if (part != 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);
94 }
95 }
96 if (m_terminal != 0) {
97 m_terminal->showShellInDir(url().toLocalFile());
98 changeDir(url());
99 m_terminalWidget->setFocus();
100 }
101
102 Panel::showEvent(event);
103 }
104
105 void TerminalPanel::changeDir(const KUrl& url)
106 {
107 delete m_mostLocalUrlJob;
108 m_mostLocalUrlJob = 0;
109
110 if (url.isLocalFile()) {
111 sendCdToTerminal(url.toLocalFile());
112 } else {
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*)));
116 }
117 }
118
119 void TerminalPanel::sendCdToTerminal(const QString& dir)
120 {
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.
126 QString cancel;
127 cancel.append(QChar(3));
128 cancel.append(QChar('c'));
129 m_terminal->sendInput(cancel);
130 }
131
132 m_terminal->sendInput("cd " + KShell::quoteArg(dir) + '\n');
133
134 if (m_clearTerminal) {
135 m_terminal->sendInput("clear\n");
136 m_clearTerminal = false;
137 }
138 }
139
140 void TerminalPanel::slotMostLocalUrlResult(KJob* job)
141 {
142 KIO::StatJob* statJob = static_cast<KIO::StatJob *>(job);
143 const KUrl url = statJob->mostLocalUrl();
144 if (url.isLocalFile()) {
145 sendCdToTerminal(url.toLocalFile());
146 }
147
148 m_mostLocalUrlJob = 0;
149 }
150
151 #include "terminalpanel.moc"