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