+ openTerminalJob(m_activeViewContainer->url());
+}
+
+void DolphinMainWindow::openTerminalHere()
+{
+ QList<QUrl> urls = {};
+
+ for (const KFileItem& item : m_activeViewContainer->view()->selectedItems()) {
+ QUrl url = item.targetUrl();
+ if (item.isFile()) {
+ url.setPath(QFileInfo(url.path()).absolutePath());
+ }
+ if (!urls.contains(url)) {
+ urls << url;
+ }
+ }
+
+ // No items are selected. Open a terminal window for the current location.
+ if (urls.count() == 0) {
+ openTerminal();
+ return;
+ }
+
+ if (urls.count() > 5) {
+ QString question = i18np("Are you sure you want to open 1 terminal window?",
+ "Are you sure you want to open %1 terminal windows?", urls.count());
+#if KWIDGETSADDONS_VERSION >= QT_VERSION_CHECK(5, 100, 0)
+ const int answer = KMessageBox::warningTwoActions(this, question, {},
+#else
+ const int answer = KMessageBox::warningYesNo(this, question, {},
+#endif
+ KGuiItem(i18ncp("@action:button", "Open %1 Terminal", "Open %1 Terminals", urls.count()),
+ QStringLiteral("utilities-terminal")),
+ KStandardGuiItem::cancel());
+#if KWIDGETSADDONS_VERSION >= QT_VERSION_CHECK(5, 100, 0)
+ if (answer != KMessageBox::PrimaryAction) {
+#else
+ if (answer != KMessageBox::Yes) {
+#endif
+ return;
+ }
+ }
+
+ for (const QUrl& url : urls) {
+ openTerminalJob(url);
+ }
+}
+
+void DolphinMainWindow::openTerminalJob(const QUrl& url)
+{
+ if (url.isLocalFile()) {
+ auto job = new KTerminalLauncherJob(QString());
+ job->setWorkingDirectory(url.toLocalFile());
+ job->start();
+ return;
+ }
+
+ // Not a local file, with protocol Class ":local", try stat'ing
+ if (KProtocolInfo::protocolClass(url.scheme()) == QLatin1String(":local")) {
+ KIO::StatJob *job = KIO::mostLocalUrl(url);
+ KJobWidgets::setWindow(job, this);
+ connect(job, &KJob::result, this, [job]() {
+ QUrl statUrl;
+ if (!job->error()) {
+ statUrl = job->mostLocalUrl();
+ }
+
+ auto job = new KTerminalLauncherJob(QString());
+ job->setWorkingDirectory(statUrl.isLocalFile() ? statUrl.toLocalFile() : QDir::homePath());
+ job->start();
+ });
+
+ return;
+ }
+
+ // Nothing worked, just use $HOME
+ auto job = new KTerminalLauncherJob(QString());
+ job->setWorkingDirectory(QDir::homePath());
+ job->start();