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