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