]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/terminal/terminalpanel.cpp
Coding style update for pointer comparison
[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 <KPluginLoader>
23 #include <KPluginFactory>
24 #include <kde_terminal_interface_v2.h>
25 #include <KParts/Part>
26 #include <KShell>
27 #include <KIO/Job>
28 #include <KIO/JobUiDelegate>
29
30 #include <QBoxLayout>
31 #include <QShowEvent>
32
33 TerminalPanel::TerminalPanel(QWidget* parent) :
34 Panel(parent),
35 m_clearTerminal(true),
36 m_mostLocalUrlJob(0),
37 m_layout(0),
38 m_terminal(0),
39 m_terminalWidget(0)
40 {
41 m_layout = new QVBoxLayout(this);
42 m_layout->setMargin(0);
43 }
44
45 TerminalPanel::~TerminalPanel()
46 {
47 }
48
49 void TerminalPanel::terminalExited()
50 {
51 emit hideTerminalPanel();
52 m_terminal = 0;
53 }
54
55 bool TerminalPanel::urlChanged()
56 {
57 if (!url().isValid()) {
58 return false;
59 }
60
61 const bool sendInput = m_terminal && (m_terminal->foregroundProcessId() == -1) && isVisible();
62 if (sendInput) {
63 changeDir(url());
64 }
65
66 return true;
67 }
68
69 void TerminalPanel::showEvent(QShowEvent* event)
70 {
71 if (event->spontaneous()) {
72 Panel::showEvent(event);
73 return;
74 }
75
76 if (!m_terminal) {
77 m_clearTerminal = true;
78 KPluginFactory* factory = KPluginLoader("libkonsolepart").factory();
79 KParts::ReadOnlyPart* part = factory ? (factory->create<KParts::ReadOnlyPart>(this)) : 0;
80 if (part) {
81 connect(part, SIGNAL(destroyed(QObject*)), this, SLOT(terminalExited()));
82 m_terminalWidget = part->widget();
83 m_layout->addWidget(m_terminalWidget);
84 m_terminal = qobject_cast<TerminalInterfaceV2 *>(part);
85 }
86 }
87 if (m_terminal) {
88 m_terminal->showShellInDir(url().toLocalFile());
89 changeDir(url());
90 m_terminalWidget->setFocus();
91 }
92
93 Panel::showEvent(event);
94 }
95
96 void TerminalPanel::changeDir(const KUrl& url)
97 {
98 delete m_mostLocalUrlJob;
99 m_mostLocalUrlJob = 0;
100
101 if (url.isLocalFile()) {
102 sendCdToTerminal(url.toLocalFile());
103 } else {
104 m_mostLocalUrlJob = KIO::mostLocalUrl(url, KIO::HideProgressInfo);
105 m_mostLocalUrlJob->ui()->setWindow(this);
106 connect(m_mostLocalUrlJob, SIGNAL(result(KJob*)), this, SLOT(slotMostLocalUrlResult(KJob*)));
107 }
108 }
109
110 void TerminalPanel::sendCdToTerminal(const QString& dir)
111 {
112 if (!m_clearTerminal) {
113 // The TerminalV2 interface does not provide a way to delete the
114 // current line before sending a new input. This is mandatory,
115 // otherwise sending a 'cd x' to a existing 'rm -rf *' might
116 // result in data loss. As workaround Ctrl+C is send.
117 QString cancel;
118 cancel.append(QChar(3));
119 cancel.append(QChar('c'));
120 m_terminal->sendInput(cancel);
121 }
122
123 m_terminal->sendInput("cd " + KShell::quoteArg(dir) + '\n');
124
125 if (m_clearTerminal) {
126 m_terminal->sendInput("clear\n");
127 m_clearTerminal = false;
128 }
129 }
130
131 void TerminalPanel::slotMostLocalUrlResult(KJob* job)
132 {
133 KIO::StatJob* statJob = static_cast<KIO::StatJob *>(job);
134 const KUrl url = statJob->mostLocalUrl();
135 if (url.isLocalFile()) {
136 sendCdToTerminal(url.toLocalFile());
137 }
138
139 m_mostLocalUrlJob = 0;
140 }
141
142 #include "terminalpanel.moc"