]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/terminal/terminalpanel.cpp
Ensure authentication data is cached properly
[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 {
43 m_layout = new QVBoxLayout(this);
44 m_layout->setMargin(0);
45 }
46
47 TerminalPanel::~TerminalPanel()
48 {
49 }
50
51 void TerminalPanel::terminalExited()
52 {
53 m_terminal = 0;
54 emit hideTerminalPanel();
55 }
56
57 void TerminalPanel::dockVisibilityChanged()
58 {
59 // Only react when the DockWidget itself (not some parent) is hidden. This way we don't
60 // respond when e.g. Dolphin is minimized.
61 if (parentWidget() && parentWidget()->isHidden() &&
62 m_terminal && (m_terminal->foregroundProcessId() == -1)) {
63 // Make sure this terminal does not prevent unmounting any removable drives
64 changeDir(KUrl::fromPath("/"));
65 }
66 }
67
68 bool TerminalPanel::urlChanged()
69 {
70 if (!url().isValid()) {
71 return false;
72 }
73
74 const bool sendInput = m_terminal && (m_terminal->foregroundProcessId() == -1) && isVisible();
75 if (sendInput) {
76 changeDir(url());
77 }
78
79 return true;
80 }
81
82 void TerminalPanel::showEvent(QShowEvent* event)
83 {
84 if (event->spontaneous()) {
85 Panel::showEvent(event);
86 return;
87 }
88
89 if (!m_terminal) {
90 m_clearTerminal = true;
91 KPluginFactory* factory = KPluginLoader("libkonsolepart").factory();
92 KParts::ReadOnlyPart* part = factory ? (factory->create<KParts::ReadOnlyPart>(this)) : 0;
93 if (part) {
94 connect(part, SIGNAL(destroyed(QObject*)), this, SLOT(terminalExited()));
95 m_terminalWidget = part->widget();
96 m_layout->addWidget(m_terminalWidget);
97 m_terminal = qobject_cast<TerminalInterfaceV2 *>(part);
98 }
99 }
100 if (m_terminal) {
101 m_terminal->showShellInDir(url().toLocalFile());
102 changeDir(url());
103 m_terminalWidget->setFocus();
104 }
105
106 Panel::showEvent(event);
107 }
108
109 void TerminalPanel::changeDir(const KUrl& url)
110 {
111 delete m_mostLocalUrlJob;
112 m_mostLocalUrlJob = 0;
113
114 if (url.isLocalFile()) {
115 sendCdToTerminal(url.toLocalFile());
116 } else {
117 m_mostLocalUrlJob = KIO::mostLocalUrl(url, KIO::HideProgressInfo);
118 if (m_mostLocalUrlJob->ui()) {
119 m_mostLocalUrlJob->ui()->setWindow(this);
120 }
121 connect(m_mostLocalUrlJob, SIGNAL(result(KJob*)), this, SLOT(slotMostLocalUrlResult(KJob*)));
122 }
123 }
124
125 void TerminalPanel::sendCdToTerminal(const QString& dir)
126 {
127 if (!m_clearTerminal) {
128 // The TerminalV2 interface does not provide a way to delete the
129 // current line before sending a new input. This is mandatory,
130 // otherwise sending a 'cd x' to a existing 'rm -rf *' might
131 // result in data loss. As workaround SIGINT is send.
132 const int processId = m_terminal->terminalProcessId();
133 if (processId > 0) {
134 kill(processId, SIGINT);
135 }
136 }
137
138 m_terminal->sendInput("cd " + KShell::quoteArg(dir) + '\n');
139
140 if (m_clearTerminal) {
141 m_terminal->sendInput("clear\n");
142 m_clearTerminal = false;
143 }
144 }
145
146 void TerminalPanel::slotMostLocalUrlResult(KJob* job)
147 {
148 KIO::StatJob* statJob = static_cast<KIO::StatJob *>(job);
149 const KUrl url = statJob->mostLocalUrl();
150 if (url.isLocalFile()) {
151 sendCdToTerminal(url.toLocalFile());
152 }
153
154 m_mostLocalUrlJob = 0;
155 }
156
157 #include "terminalpanel.moc"