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