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