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