]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/terminal/terminalpanel.cpp
Prevent accidental deletion of home directory in Terminal Panel
[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 <KService>
27 #include <kde_terminal_interface_v2.h>
28 #include <KParts/Part>
29 #include <KShell>
30 #include <KIO/Job>
31 #include <KIO/JobUiDelegate>
32
33 #include <QBoxLayout>
34 #include <QShowEvent>
35
36 TerminalPanel::TerminalPanel(QWidget* parent) :
37 Panel(parent),
38 m_clearTerminal(true),
39 m_mostLocalUrlJob(0),
40 m_layout(0),
41 m_terminal(0),
42 m_terminalWidget(0),
43 m_konsolePart(0),
44 m_konsolePartCurrentDirectory()
45 {
46 m_layout = new QVBoxLayout(this);
47 m_layout->setMargin(0);
48 }
49
50 TerminalPanel::~TerminalPanel()
51 {
52 }
53
54 void TerminalPanel::terminalExited()
55 {
56 m_terminal = 0;
57 emit hideTerminalPanel();
58 }
59
60 void TerminalPanel::dockVisibilityChanged()
61 {
62 // Only react when the DockWidget itself (not some parent) is hidden. This way we don't
63 // respond when e.g. Dolphin is minimized.
64 if (parentWidget() && parentWidget()->isHidden() &&
65 m_terminal && (m_terminal->foregroundProcessId() == -1)) {
66 // Make sure that the following "cd /" command will not affect the view.
67 disconnect(m_konsolePart, SIGNAL(currentDirectoryChanged(QString)),
68 this, SLOT(slotKonsolePartCurrentDirectoryChanged(QString)));
69
70 // Make sure this terminal does not prevent unmounting any removable drives
71 changeDir(KUrl::fromPath("/"));
72
73 // Because we have disconnected from the part's currentDirectoryChanged()
74 // signal, we have to update m_konsolePartCurrentDirectory manually. If this
75 // was not done, showing the panel again might not set the part's working
76 // directory correctly.
77 m_konsolePartCurrentDirectory = '/';
78 }
79 }
80
81 bool TerminalPanel::urlChanged()
82 {
83 if (!url().isValid()) {
84 return false;
85 }
86
87 const bool sendInput = m_terminal && (m_terminal->foregroundProcessId() == -1) && isVisible();
88 if (sendInput) {
89 changeDir(url());
90 }
91
92 return true;
93 }
94
95 void TerminalPanel::showEvent(QShowEvent* event)
96 {
97 if (event->spontaneous()) {
98 Panel::showEvent(event);
99 return;
100 }
101
102 if (!m_terminal) {
103 m_clearTerminal = true;
104 KPluginFactory* factory = 0;
105 KService::Ptr service = KService::serviceByDesktopName("konsolepart");
106 if (service) {
107 factory = KPluginLoader(service->library()).factory();
108 }
109 m_konsolePart = factory ? (factory->create<KParts::ReadOnlyPart>(this)) : 0;
110 if (m_konsolePart) {
111 connect(m_konsolePart, SIGNAL(destroyed(QObject*)), this, SLOT(terminalExited()));
112 m_terminalWidget = m_konsolePart->widget();
113 m_layout->addWidget(m_terminalWidget);
114 m_terminal = qobject_cast<TerminalInterfaceV2 *>(m_konsolePart);
115 }
116 }
117 if (m_terminal) {
118 connect(m_konsolePart, SIGNAL(currentDirectoryChanged(QString)),
119 this, SLOT(slotKonsolePartCurrentDirectoryChanged(QString)));
120 m_terminal->showShellInDir(url().toLocalFile());
121 changeDir(url());
122 m_terminalWidget->setFocus();
123 }
124
125 Panel::showEvent(event);
126 }
127
128 void TerminalPanel::changeDir(const KUrl& url)
129 {
130 delete m_mostLocalUrlJob;
131 m_mostLocalUrlJob = 0;
132
133 if (url.isLocalFile()) {
134 sendCdToTerminal(url.toLocalFile());
135 } else {
136 m_mostLocalUrlJob = KIO::mostLocalUrl(url, KIO::HideProgressInfo);
137 if (m_mostLocalUrlJob->ui()) {
138 m_mostLocalUrlJob->ui()->setWindow(this);
139 }
140 connect(m_mostLocalUrlJob, SIGNAL(result(KJob*)), this, SLOT(slotMostLocalUrlResult(KJob*)));
141 }
142 }
143
144 void TerminalPanel::sendCdToTerminal(const QString& dir)
145 {
146 if (dir == m_konsolePartCurrentDirectory) {
147 m_clearTerminal = false;
148 return;
149 }
150
151 if (!m_clearTerminal) {
152 // The TerminalV2 interface does not provide a way to delete the
153 // current line before sending a new input. This is mandatory,
154 // otherwise sending a 'cd x' to a existing 'rm -rf *' might
155 // result in data loss. As workaround SIGINT is send.
156 const int processId = m_terminal->terminalProcessId();
157 if (processId > 0) {
158 kill(processId, SIGINT);
159 }
160 }
161
162 m_terminal->sendInput(" cd " + KShell::quoteArg(dir) + '\n');
163 m_konsolePartCurrentDirectory = dir;
164
165 if (m_clearTerminal) {
166 m_terminal->sendInput(" clear\n");
167 m_clearTerminal = false;
168 }
169 }
170
171 void TerminalPanel::slotMostLocalUrlResult(KJob* job)
172 {
173 KIO::StatJob* statJob = static_cast<KIO::StatJob *>(job);
174 const KUrl url = statJob->mostLocalUrl();
175 if (url.isLocalFile()) {
176 sendCdToTerminal(url.toLocalFile());
177 }
178
179 m_mostLocalUrlJob = 0;
180 }
181
182 void TerminalPanel::slotKonsolePartCurrentDirectoryChanged(const QString& dir)
183 {
184 m_konsolePartCurrentDirectory = dir;
185
186 const KUrl newUrl(dir);
187 if (newUrl != url()) {
188 emit changeUrl(newUrl);
189 }
190 }
191
192 #include "terminalpanel.moc"