]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/terminal/terminalpanel.cpp
861afebee0330fa2fd5c9ffc4917630a29809f6b
[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 <KIO/DesktopExecParser>
23 #include <KIO/Job>
24 #include <KIO/JobUiDelegate>
25 #include <KJobWidgets>
26 #include <KLocalizedString>
27 #include <KMessageWidget>
28 #include <KParts/ReadOnlyPart>
29 #include <KPluginFactory>
30 #include <KPluginLoader>
31 #include <KService>
32 #include <KShell>
33 #include <kde_terminal_interface.h>
34
35 #include <QAction>
36 #include <QDesktopServices>
37 #include <QDir>
38 #include <QLabel>
39 #include <QShowEvent>
40 #include <QTimer>
41 #include <QVBoxLayout>
42
43 TerminalPanel::TerminalPanel(QWidget* parent) :
44 Panel(parent),
45 m_clearTerminal(true),
46 m_mostLocalUrlJob(nullptr),
47 m_layout(nullptr),
48 m_terminal(nullptr),
49 m_terminalWidget(nullptr),
50 m_konsolePartMissingMessage(nullptr),
51 m_konsolePart(nullptr),
52 m_konsolePartCurrentDirectory(),
53 m_sendCdToTerminalHistory()
54 {
55 m_layout = new QVBoxLayout(this);
56 m_layout->setContentsMargins(0, 0, 0, 0);
57 }
58
59 TerminalPanel::~TerminalPanel()
60 {
61 }
62
63 void TerminalPanel::goHome()
64 {
65 sendCdToTerminal(QDir::homePath(), HistoryPolicy::SkipHistory);
66 }
67
68 QString TerminalPanel::currentWorkingDirectory()
69 {
70 if (m_terminal) {
71 return m_terminal->currentWorkingDirectory();
72 }
73 return QString();
74 }
75
76 void TerminalPanel::terminalExited()
77 {
78 m_terminal = nullptr;
79 emit hideTerminalPanel();
80 }
81
82 bool TerminalPanel::isHiddenInVisibleWindow() const
83 {
84 return parentWidget()
85 && parentWidget()->isHidden();
86 }
87
88 void TerminalPanel::dockVisibilityChanged()
89 {
90 // Only react when the DockWidget itself (not some parent) is hidden. This way we don't
91 // respond when e.g. Dolphin is minimized.
92 if (isHiddenInVisibleWindow() && m_terminal && !hasProgramRunning()) {
93 // Make sure that the following "cd /" command will not affect the view.
94 disconnect(m_konsolePart, SIGNAL(currentDirectoryChanged(QString)),
95 this, SLOT(slotKonsolePartCurrentDirectoryChanged(QString)));
96
97 // Make sure this terminal does not prevent unmounting any removable drives
98 changeDir(QUrl::fromLocalFile(QStringLiteral("/")));
99
100 // Because we have disconnected from the part's currentDirectoryChanged()
101 // signal, we have to update m_konsolePartCurrentDirectory manually. If this
102 // was not done, showing the panel again might not set the part's working
103 // directory correctly.
104 m_konsolePartCurrentDirectory = '/';
105 }
106 }
107
108 QString TerminalPanel::runningProgramName() const
109 {
110 return m_terminal ? m_terminal->foregroundProcessName() : QString();
111 }
112
113 bool TerminalPanel::hasProgramRunning() const
114 {
115 return m_terminal && (m_terminal->foregroundProcessId() != -1);
116 }
117
118 bool TerminalPanel::urlChanged()
119 {
120 if (!url().isValid()) {
121 return false;
122 }
123
124 const bool sendInput = m_terminal && !hasProgramRunning() && isVisible();
125 if (sendInput) {
126 changeDir(url());
127 }
128
129 return true;
130 }
131
132 void TerminalPanel::showEvent(QShowEvent* event)
133 {
134 if (event->spontaneous()) {
135 Panel::showEvent(event);
136 return;
137 }
138
139 if (!m_terminal) {
140 m_clearTerminal = true;
141 KPluginFactory* factory = nullptr;
142 KService::Ptr service = KService::serviceByDesktopName(QStringLiteral("konsolepart"));
143 if (service) {
144 factory = KPluginLoader(service->library()).factory();
145 }
146 m_konsolePart = factory ? (factory->create<KParts::ReadOnlyPart>(this)) : nullptr;
147 if (m_konsolePart) {
148 connect(m_konsolePart, &KParts::ReadOnlyPart::destroyed, this, &TerminalPanel::terminalExited);
149 m_terminalWidget = m_konsolePart->widget();
150 setFocusProxy(m_terminalWidget);
151 m_layout->addWidget(m_terminalWidget);
152 if (m_konsolePartMissingMessage) {
153 m_layout->removeWidget(m_konsolePartMissingMessage);
154 }
155 m_terminal = qobject_cast<TerminalInterface*>(m_konsolePart);
156 } else if (!m_konsolePartMissingMessage) {
157 const auto konsoleInstallUrl = QUrl("appstream://org.kde.konsole.desktop");
158 const auto konsoleNotInstalledText = i18n("Terminal cannot be shown because Konsole is not installed. "
159 "Please install it and then reopen the panel.");
160 m_konsolePartMissingMessage = new KMessageWidget(konsoleNotInstalledText, this);
161 m_konsolePartMissingMessage->setCloseButtonVisible(false);
162 m_konsolePartMissingMessage->hide();
163 if (KIO::DesktopExecParser::hasSchemeHandler(konsoleInstallUrl)) {
164 auto installKonsoleAction = new QAction(i18n("Install Konsole"), this);
165 connect(installKonsoleAction, &QAction::triggered, [konsoleInstallUrl]() {
166 QDesktopServices::openUrl(konsoleInstallUrl);
167 });
168 m_konsolePartMissingMessage->addAction(installKonsoleAction);
169 }
170 m_layout->addWidget(m_konsolePartMissingMessage);
171 m_layout->addStretch();
172 QTimer::singleShot(0, m_konsolePartMissingMessage, &KMessageWidget::animatedShow);
173 } else {
174 m_konsolePartMissingMessage->animatedShow();
175 }
176 }
177 if (m_terminal) {
178 m_terminal->showShellInDir(url().toLocalFile());
179 if(!hasProgramRunning()) {
180 changeDir(url());
181 }
182 m_terminalWidget->setFocus();
183 connect(m_konsolePart, SIGNAL(currentDirectoryChanged(QString)),
184 this, SLOT(slotKonsolePartCurrentDirectoryChanged(QString)));
185 }
186
187 Panel::showEvent(event);
188 }
189
190 void TerminalPanel::changeDir(const QUrl& url)
191 {
192 delete m_mostLocalUrlJob;
193 m_mostLocalUrlJob = nullptr;
194
195 if (url.isLocalFile()) {
196 sendCdToTerminal(url.toLocalFile());
197 } else {
198 m_mostLocalUrlJob = KIO::mostLocalUrl(url, KIO::HideProgressInfo);
199 if (m_mostLocalUrlJob->uiDelegate()) {
200 KJobWidgets::setWindow(m_mostLocalUrlJob, this);
201 }
202 connect(m_mostLocalUrlJob, &KIO::StatJob::result, this, &TerminalPanel::slotMostLocalUrlResult);
203 }
204 }
205
206 void TerminalPanel::sendCdToTerminal(const QString& dir, HistoryPolicy addToHistory)
207 {
208 if (dir == m_konsolePartCurrentDirectory) {
209 m_clearTerminal = false;
210 return;
211 }
212
213 #ifndef Q_OS_WIN
214 if (!m_clearTerminal) {
215 // The TerminalV2 interface does not provide a way to delete the
216 // current line before sending a new input. This is mandatory,
217 // otherwise sending a 'cd x' to a existing 'rm -rf *' might
218 // result in data loss. As workaround SIGINT is sent.
219 const int processId = m_terminal->terminalProcessId();
220 if (processId > 0) {
221 kill(processId, SIGINT);
222 }
223 }
224 #endif
225
226 m_terminal->sendInput(" cd " + KShell::quoteArg(dir) + '\n');
227
228 // We want to ignore the currentDirectoryChanged(QString) signal, which we will receive after
229 // the directory change, because this directory change is not caused by a "cd" command that the
230 // user entered in the panel. Therefore, we have to remember 'dir'. Note that it could also be
231 // a symbolic link -> remember the 'canonical' path.
232 if (addToHistory == HistoryPolicy::AddToHistory)
233 m_sendCdToTerminalHistory.enqueue(QDir(dir).canonicalPath());
234
235 if (m_clearTerminal) {
236 m_terminal->sendInput(QStringLiteral(" clear\n"));
237 m_clearTerminal = false;
238 }
239 }
240
241 void TerminalPanel::slotMostLocalUrlResult(KJob* job)
242 {
243 KIO::StatJob* statJob = static_cast<KIO::StatJob *>(job);
244 const QUrl url = statJob->mostLocalUrl();
245 if (url.isLocalFile()) {
246 sendCdToTerminal(url.toLocalFile());
247 }
248
249 m_mostLocalUrlJob = nullptr;
250 }
251
252 void TerminalPanel::slotKonsolePartCurrentDirectoryChanged(const QString& dir)
253 {
254 m_konsolePartCurrentDirectory = QDir(dir).canonicalPath();
255
256 // Only emit a changeUrl signal if the directory change was caused by the user inside the
257 // terminal, and not by sendCdToTerminal(QString).
258 while (!m_sendCdToTerminalHistory.empty()) {
259 if (m_konsolePartCurrentDirectory == m_sendCdToTerminalHistory.dequeue()) {
260 return;
261 }
262 }
263
264 const QUrl url(QUrl::fromLocalFile(dir));
265 emit changeUrl(url);
266 }
267
268 bool TerminalPanel::terminalHasFocus() const
269 {
270 return m_terminalWidget->hasFocus();
271 }