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