+void TerminalPanel::changeDir(const QUrl &url)
+{
+ delete m_mostLocalUrlJob;
+ m_mostLocalUrlJob = nullptr;
+
+ if (url.isLocalFile()) {
+ sendCdToTerminal(url.toLocalFile());
+ return;
+ }
+
+ // Try stat'ing the url; note that mostLocalUrl only works with ":local" protocols
+ if (KProtocolInfo::protocolClass(url.scheme()) == QLatin1String(":local")) {
+ m_mostLocalUrlJob = KIO::mostLocalUrl(url, KIO::HideProgressInfo);
+ if (m_mostLocalUrlJob->uiDelegate()) {
+ KJobWidgets::setWindow(m_mostLocalUrlJob, this);
+ }
+ connect(m_mostLocalUrlJob, &KIO::StatJob::result, this, &TerminalPanel::slotMostLocalUrlResult);
+ return;
+ }
+
+ // Last chance, try KIOFuse
+ sendCdToTerminalKIOFuse(url);
+}
+
+void TerminalPanel::sendCdToTerminal(const QString &dir, HistoryPolicy addToHistory)
+{
+ if (dir == m_konsolePartCurrentDirectory // We are already there
+ && m_sendCdToTerminalHistory.isEmpty() // …and that is not because the terminal couldn't keep up
+ ) {
+ m_clearTerminal = false;
+ return;
+ }
+
+ // Send prior Ctrl-E, Ctrl-U to ensure the line is empty. This is
+ // mandatory, otherwise sending a 'cd x\n' to a prompt with 'rm -rf *'
+ // would result in data loss.
+ m_terminal->sendInput(QStringLiteral("\x05\x15"));
+
+ // We want to ignore the currentDirectoryChanged(QString) signal, which we will receive after
+ // the directory change, because this directory change is not caused by a "cd" command that the
+ // user entered in the panel. Therefore, we have to remember 'dir'. Note that it could also be
+ // a symbolic link -> remember the 'canonical' path.
+ if (addToHistory == HistoryPolicy::AddToHistory)
+ m_sendCdToTerminalHistory.enqueue(QDir(dir).canonicalPath());
+
+ m_terminal->sendInput(" cd " + KShell::quoteArg(dir) + '\r');
+
+ if (m_clearTerminal) {
+ m_terminal->sendInput(QStringLiteral(" clear\r"));
+ m_clearTerminal = false;
+ }
+}
+
+void TerminalPanel::sendCdToTerminalKIOFuse(const QUrl &url)
+{
+ // URL isn't local, only hope for the terminal to be in sync with the
+ // DolphinView is to mount the remote URL in KIOFuse and point to it.
+ // If we can't do that for any reason, silently fail.
+ auto reply = m_kiofuseInterface.mountUrl(url.toString());
+ QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(reply, this);
+ QObject::connect(watcher, &QDBusPendingCallWatcher::finished, this, [=](QDBusPendingCallWatcher *watcher) {
+ watcher->deleteLater();
+ if (!reply.isError()) {
+ // Successfully mounted, point to the KIOFuse equivalent path.
+ sendCdToTerminal(reply.value());
+ }
+ });
+}
+
+void TerminalPanel::slotMostLocalUrlResult(KJob *job)
+{
+ KIO::StatJob *statJob = static_cast<KIO::StatJob *>(job);
+ const QUrl url = statJob->mostLocalUrl();
+ if (url.isLocalFile()) {
+ sendCdToTerminal(url.toLocalFile());
+ } else {
+ sendCdToTerminalKIOFuse(url);
+ }
+
+ m_mostLocalUrlJob = nullptr;
+}
+
+void TerminalPanel::slotKonsolePartCurrentDirectoryChanged(const QString &dir)
+{
+ m_konsolePartCurrentDirectory = QDir(dir).canonicalPath();
+
+ // Only emit a changeUrl signal if the directory change was caused by the user inside the
+ // terminal, and not by sendCdToTerminal(QString).
+ while (!m_sendCdToTerminalHistory.empty()) {
+ if (m_konsolePartCurrentDirectory == m_sendCdToTerminalHistory.dequeue()) {
+ return;
+ }
+ }
+
+ // User may potentially be browsing inside a KIOFuse mount.
+ // If so lets try and change the DolphinView to point to the remote URL equivalent.
+ // instead of into the KIOFuse mount itself (which can cause performance issues!)
+ const QUrl url(QUrl::fromLocalFile(dir));
+
+ KMountPoint::Ptr mountPoint = KMountPoint::currentMountPoints().findByPath(m_konsolePartCurrentDirectory);
+ if (mountPoint && mountPoint->mountType() != QStringLiteral("fuse.kio-fuse")) {
+ // Not in KIOFUse mount, so just switch to the corresponding URL.
+ Q_EMIT changeUrl(url);
+ return;
+ }
+
+ auto reply = m_kiofuseInterface.remoteUrl(m_konsolePartCurrentDirectory);
+ QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(reply, this);
+ QObject::connect(watcher, &QDBusPendingCallWatcher::finished, this, [=](QDBusPendingCallWatcher *watcher) {
+ watcher->deleteLater();
+ if (reply.isError()) {
+ // KIOFuse errored out... just show the normal URL
+ Q_EMIT changeUrl(url);
+ } else {
+ // Our location happens to be in a KIOFuse mount and is mounted.
+ // Let's change the DolphinView to point to the remote URL equivalent.
+ Q_EMIT changeUrl(QUrl::fromUserInput(reply.value()));
+ }
+ });
+}
+
+bool TerminalPanel::terminalHasFocus() const
+{
+ if (m_terminalWidget) {
+ return m_terminalWidget->hasFocus();
+ }
+
+ return hasFocus();
+}
+
+#include "moc_terminalpanel.cpp"