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