1 /*
2  * SPDX-FileCopyrightText: 2006 Peter Penz <peter.penz19@gmail.com>
3  * SPDX-FileCopyrightText: 2006 Stefan Monov <logixoul@gmail.com>
4  * SPDX-FileCopyrightText: 2006 Cvetoslav Ludmiloff <ludmiloff@gmail.com>
5  *
6  * SPDX-License-Identifier: GPL-2.0-or-later
7  */
8 
9 #include "dolphinmainwindow.h"
10 
11 #include "dolphinmainwindowadaptor.h"
12 #include "config-terminal.h"
13 #include "global.h"
14 #include "dolphinbookmarkhandler.h"
15 #include "dolphindockwidget.h"
16 #include "dolphincontextmenu.h"
17 #include "dolphinnavigatorswidgetaction.h"
18 #include "dolphinnewfilemenu.h"
19 #include "dolphinrecenttabsmenu.h"
20 #include "dolphinurlnavigatorscontroller.h"
21 #include "dolphinviewcontainer.h"
22 #include "dolphintabpage.h"
23 #include "middleclickactioneventfilter.h"
24 #include "panels/folders/folderspanel.h"
25 #include "panels/places/placesitemmodel.h"
26 #include "panels/places/placespanel.h"
27 #include "panels/terminal/terminalpanel.h"
28 #include "settings/dolphinsettingsdialog.h"
29 #include "statusbar/dolphinstatusbar.h"
30 #include "views/dolphinviewactionhandler.h"
31 #include "views/dolphinremoteencoding.h"
32 #include "views/draganddrophelper.h"
33 #include "views/viewproperties.h"
34 #include "views/dolphinnewfilemenuobserver.h"
35 #include "dolphin_generalsettings.h"
36 
37 #include <KActionCollection>
38 #include <KActionMenu>
39 #include <KAuthorized>
40 #include <KConfig>
41 #include <KConfigGui>
42 #include <KDualAction>
43 #include <KFileItemListProperties>
44 #include <KHelpMenu>
45 #include <KIO/CommandLauncherJob>
46 #include <KIO/JobUiDelegate>
47 #include <KIO/OpenFileManagerWindowJob>
48 #include <KIO/OpenUrlJob>
49 #include <KJobWidgets>
50 #include <KLocalizedString>
51 #include <KMessageBox>
52 #include <KNS3/KMoreToolsMenuFactory>
53 #include <KProtocolInfo>
54 #include <KProtocolManager>
55 #include <KShell>
56 #include <KStandardAction>
57 #include <KStartupInfo>
58 #include <KSycoca>
59 #include <KTerminalLauncherJob>
60 #include <KToggleAction>
61 #include <KToolBar>
62 #include <KToolBarPopupAction>
63 #include <KUrlComboBox>
64 #include <KUrlNavigator>
65 #include <KWindowSystem>
66 #include <KXMLGUIFactory>
67 #include <kxmlgui_version.h>
68 
69 #include <kio_version.h>
70 
71 #include <QApplication>
72 #include <QClipboard>
73 #include <QCloseEvent>
74 #include <QDesktopServices>
75 #include <QDialog>
76 #include <QDomDocument>
77 #include <QFileInfo>
78 #include <QLineEdit>
79 #include <QMenuBar>
80 #include <QPushButton>
81 #include <QShowEvent>
82 #include <QStandardPaths>
83 #include <QTimer>
84 #include <QToolButton>
85 #include <QWhatsThisClickedEvent>
86 
87 namespace {
88     // Used for GeneralSettings::version() to determine whether
89     // an updated version of Dolphin is running.
90     const int CurrentDolphinVersion = 201;
91     // The maximum number of entries in the back/forward popup menu
92     const int MaxNumberOfNavigationentries = 12;
93     // The maximum number of "Activate Tab" shortcuts
94     const int MaxActivateTabShortcuts = 9;
95 }
96 
DolphinMainWindow()97 DolphinMainWindow::DolphinMainWindow() :
98     KXmlGuiWindow(nullptr),
99     m_newFileMenu(nullptr),
100     m_helpMenu(nullptr),
101     m_tabWidget(nullptr),
102     m_activeViewContainer(nullptr),
103     m_actionHandler(nullptr),
104     m_remoteEncoding(nullptr),
105     m_settingsDialog(),
106     m_bookmarkHandler(nullptr),
107     m_controlButton(nullptr),
108     m_updateToolBarTimer(nullptr),
109     m_lastHandleUrlOpenJob(nullptr),
110     m_terminalPanel(nullptr),
111     m_placesPanel(nullptr),
112     m_tearDownFromPlacesRequested(false),
113     m_backAction(nullptr),
114     m_forwardAction(nullptr)
115 {
116     Q_INIT_RESOURCE(dolphin);
117 
118     new MainWindowAdaptor(this);
119 
120 #ifndef Q_OS_WIN
121 	setWindowFlags(Qt::WindowContextHelpButtonHint);
122 #endif
123     setComponentName(QStringLiteral("dolphin"), QGuiApplication::applicationDisplayName());
124     setObjectName(QStringLiteral("Dolphin#"));
125 
126 #if KXMLGUI_VERSION >= QT_VERSION_CHECK(5, 88, 0)
127     setStateConfigGroup("State");
128 #endif
129 
130     connect(&DolphinNewFileMenuObserver::instance(), &DolphinNewFileMenuObserver::errorMessage,
131             this, &DolphinMainWindow::showErrorMessage);
132 
133     KIO::FileUndoManager* undoManager = KIO::FileUndoManager::self();
134     undoManager->setUiInterface(new UndoUiInterface());
135 
136     connect(undoManager, QOverload<bool>::of(&KIO::FileUndoManager::undoAvailable),
137             this, &DolphinMainWindow::slotUndoAvailable);
138     connect(undoManager, &KIO::FileUndoManager::undoTextChanged,
139             this, &DolphinMainWindow::slotUndoTextChanged);
140     connect(undoManager, &KIO::FileUndoManager::jobRecordingStarted,
141             this, &DolphinMainWindow::clearStatusBar);
142     connect(undoManager, &KIO::FileUndoManager::jobRecordingFinished,
143             this, &DolphinMainWindow::showCommand);
144 
145     const bool firstRun = (GeneralSettings::version() < 200);
146     if (firstRun) {
147         GeneralSettings::setViewPropsTimestamp(QDateTime::currentDateTime());
148     }
149 
150     setAcceptDrops(true);
151 
152     auto *navigatorsWidgetAction = new DolphinNavigatorsWidgetAction(this);
153     actionCollection()->addAction(QStringLiteral("url_navigators"), navigatorsWidgetAction);
154     m_tabWidget = new DolphinTabWidget(navigatorsWidgetAction, this);
155     m_tabWidget->setObjectName("tabWidget");
156     connect(m_tabWidget, &DolphinTabWidget::activeViewChanged,
157             this, &DolphinMainWindow::activeViewChanged);
158     connect(m_tabWidget, &DolphinTabWidget::tabCountChanged,
159             this, &DolphinMainWindow::tabCountChanged);
160     connect(m_tabWidget, &DolphinTabWidget::currentUrlChanged,
161             this, &DolphinMainWindow::updateWindowTitle);
162     setCentralWidget(m_tabWidget);
163 
164     setupActions();
165 
166     m_actionHandler = new DolphinViewActionHandler(actionCollection(), this);
167     connect(m_actionHandler, &DolphinViewActionHandler::actionBeingHandled, this, &DolphinMainWindow::clearStatusBar);
168     connect(m_actionHandler, &DolphinViewActionHandler::createDirectoryTriggered, this, &DolphinMainWindow::createDirectory);
169 
170     m_remoteEncoding = new DolphinRemoteEncoding(this, m_actionHandler);
171     connect(this, &DolphinMainWindow::urlChanged,
172             m_remoteEncoding, &DolphinRemoteEncoding::slotAboutToOpenUrl);
173 
174     setupDockWidgets();
175 
176     setupGUI(Keys | Save | Create | ToolBar);
177     stateChanged(QStringLiteral("new_file"));
178 
179     QClipboard* clipboard = QApplication::clipboard();
180     connect(clipboard, &QClipboard::dataChanged,
181             this, &DolphinMainWindow::updatePasteAction);
182 
183     QAction* toggleFilterBarAction = actionCollection()->action(QStringLiteral("toggle_filter"));
184     toggleFilterBarAction->setChecked(GeneralSettings::filterBar());
185 
186     if (firstRun) {
187         menuBar()->setVisible(false);
188     }
189 
190     const bool showMenu = !menuBar()->isHidden();
191     QAction* showMenuBarAction = actionCollection()->action(KStandardAction::name(KStandardAction::ShowMenubar));
192     showMenuBarAction->setChecked(showMenu);  // workaround for bug #171080
193 
194     auto hamburgerMenu = static_cast<KHamburgerMenu *>(actionCollection()->action(
195                                     KStandardAction::name(KStandardAction::HamburgerMenu)));
196     hamburgerMenu->setMenuBar(menuBar());
197     hamburgerMenu->setShowMenuBarAction(showMenuBarAction);
198     connect(hamburgerMenu, &KHamburgerMenu::aboutToShowMenu,
199             this, &DolphinMainWindow::updateHamburgerMenu);
200     hamburgerMenu->hideActionsOf(toolBar());
201     if (GeneralSettings::version() < 201 && !toolBar()->actions().contains(hamburgerMenu)) {
202         addHamburgerMenuToToolbar();
203     }
204 
205     updateAllowedToolbarAreas();
206 
207     // enable middle-click on back/forward/up to open in a new tab
208     auto *middleClickEventFilter = new MiddleClickActionEventFilter(this);
209     connect(middleClickEventFilter, &MiddleClickActionEventFilter::actionMiddleClicked, this, &DolphinMainWindow::slotToolBarActionMiddleClicked);
210     toolBar()->installEventFilter(middleClickEventFilter);
211 
212     setupWhatsThis();
213 
214     connect(KSycoca::self(), QOverload<>::of(&KSycoca::databaseChanged), this, &DolphinMainWindow::updateOpenPreferredSearchToolAction);
215 
216     QTimer::singleShot(0, this, &DolphinMainWindow::updateOpenPreferredSearchToolAction);
217 
218     m_fileItemActions.setParentWidget(this);
219     connect(&m_fileItemActions, &KFileItemActions::error, this, [this](const QString &errorMessage) {
220         showErrorMessage(errorMessage);
221     });
222 
223     connect(GeneralSettings::self(), &GeneralSettings::splitViewChanged,
224             this, &DolphinMainWindow::slotSplitViewChanged);
225 }
226 
~DolphinMainWindow()227 DolphinMainWindow::~DolphinMainWindow()
228 {
229     // This fixes a crash on Wayland when closing the mainwindow while another dialog is open.
230     disconnect(QGuiApplication::clipboard(), &QClipboard::dataChanged, this, &DolphinMainWindow::updatePasteAction);
231 }
232 
viewContainers() const233 QVector<DolphinViewContainer*> DolphinMainWindow::viewContainers() const
234 {
235     QVector<DolphinViewContainer*> viewContainers;
236 
237     for (int i = 0; i < m_tabWidget->count(); ++i) {
238         DolphinTabPage *tabPage = m_tabWidget->tabPageAt(i);
239 
240         viewContainers << tabPage->primaryViewContainer();
241         if (tabPage->splitViewEnabled()) {
242             viewContainers << tabPage->secondaryViewContainer();
243         }
244     }
245     return viewContainers;
246 }
247 
openDirectories(const QList<QUrl> & dirs,bool splitView)248 void DolphinMainWindow::openDirectories(const QList<QUrl>& dirs, bool splitView)
249 {
250     m_tabWidget->openDirectories(dirs, splitView);
251 }
252 
openDirectories(const QStringList & dirs,bool splitView)253 void DolphinMainWindow::openDirectories(const QStringList& dirs, bool splitView)
254 {
255     openDirectories(QUrl::fromStringList(dirs), splitView);
256 }
257 
openFiles(const QList<QUrl> & files,bool splitView)258 void DolphinMainWindow::openFiles(const QList<QUrl>& files, bool splitView)
259 {
260     m_tabWidget->openFiles(files, splitView);
261 }
262 
isFoldersPanelEnabled() const263 bool DolphinMainWindow::isFoldersPanelEnabled() const
264 {
265     return actionCollection()->action(QStringLiteral("show_folders_panel"))->isChecked();
266 }
267 
isInformationPanelEnabled() const268 bool DolphinMainWindow::isInformationPanelEnabled() const
269 {
270 #ifdef HAVE_BALOO
271     return actionCollection()->action(QStringLiteral("show_information_panel"))->isChecked();
272 #else
273     return false;
274 #endif
275 }
276 
openFiles(const QStringList & files,bool splitView)277 void DolphinMainWindow::openFiles(const QStringList& files, bool splitView)
278 {
279     openFiles(QUrl::fromStringList(files), splitView);
280 }
281 
activateWindow()282 void DolphinMainWindow::activateWindow()
283 {
284     window()->setAttribute(Qt::WA_NativeWindow, true);
285     KStartupInfo::setNewStartupId(window()->windowHandle(), KStartupInfo::startupId());
286     KWindowSystem::activateWindow(window()->effectiveWinId());
287 }
288 
showCommand(CommandType command)289 void DolphinMainWindow::showCommand(CommandType command)
290 {
291     DolphinStatusBar* statusBar = m_activeViewContainer->statusBar();
292     switch (command) {
293     case KIO::FileUndoManager::Copy:
294         statusBar->setText(i18nc("@info:status", "Successfully copied."));
295         break;
296     case KIO::FileUndoManager::Move:
297         statusBar->setText(i18nc("@info:status", "Successfully moved."));
298         break;
299     case KIO::FileUndoManager::Link:
300         statusBar->setText(i18nc("@info:status", "Successfully linked."));
301         break;
302     case KIO::FileUndoManager::Trash:
303         statusBar->setText(i18nc("@info:status", "Successfully moved to trash."));
304         break;
305     case KIO::FileUndoManager::Rename:
306         statusBar->setText(i18nc("@info:status", "Successfully renamed."));
307         break;
308 
309     case KIO::FileUndoManager::Mkdir:
310         statusBar->setText(i18nc("@info:status", "Created folder."));
311         break;
312 
313     default:
314         break;
315     }
316 }
317 
pasteIntoFolder()318 void DolphinMainWindow::pasteIntoFolder()
319 {
320     m_activeViewContainer->view()->pasteIntoFolder();
321 }
322 
changeUrl(const QUrl & url)323 void DolphinMainWindow::changeUrl(const QUrl &url)
324 {
325     if (!KProtocolManager::supportsListing(url)) {
326         // The URL navigator only checks for validity, not
327         // if the URL can be listed. An error message is
328         // shown due to DolphinViewContainer::restoreView().
329         return;
330     }
331 
332     m_activeViewContainer->setUrl(url);
333     updateFileAndEditActions();
334     updatePasteAction();
335     updateViewActions();
336     updateGoActions();
337 
338     Q_EMIT urlChanged(url);
339 }
340 
slotTerminalDirectoryChanged(const QUrl & url)341 void DolphinMainWindow::slotTerminalDirectoryChanged(const QUrl& url)
342 {
343     if (m_tearDownFromPlacesRequested && url == QUrl::fromLocalFile(QDir::homePath())) {
344         m_placesPanel->proceedWithTearDown();
345         m_tearDownFromPlacesRequested = false;
346     }
347 
348     m_activeViewContainer->setAutoGrabFocus(false);
349     changeUrl(url);
350     m_activeViewContainer->setAutoGrabFocus(true);
351 }
352 
slotEditableStateChanged(bool editable)353 void DolphinMainWindow::slotEditableStateChanged(bool editable)
354 {
355     KToggleAction* editableLocationAction =
356         static_cast<KToggleAction*>(actionCollection()->action(QStringLiteral("editable_location")));
357     editableLocationAction->setChecked(editable);
358 }
359 
slotSelectionChanged(const KFileItemList & selection)360 void DolphinMainWindow::slotSelectionChanged(const KFileItemList& selection)
361 {
362     updateFileAndEditActions();
363 
364     const int selectedUrlsCount = m_tabWidget->currentTabPage()->selectedItemsCount();
365 
366     QAction* compareFilesAction = actionCollection()->action(QStringLiteral("compare_files"));
367     if (selectedUrlsCount == 2) {
368         compareFilesAction->setEnabled(isKompareInstalled());
369     } else {
370         compareFilesAction->setEnabled(false);
371     }
372 
373     Q_EMIT selectionChanged(selection);
374 }
375 
updateHistory()376 void DolphinMainWindow::updateHistory()
377 {
378     const KUrlNavigator *urlNavigator = m_activeViewContainer->urlNavigatorInternalWithHistory();
379     const int index = urlNavigator->historyIndex();
380 
381     QAction* backAction = actionCollection()->action(KStandardAction::name(KStandardAction::Back));
382     if (backAction) {
383         backAction->setToolTip(i18nc("@info", "Go back"));
384         backAction->setWhatsThis(i18nc("@info:whatsthis go back", "Return to the previously viewed folder."));
385         backAction->setEnabled(index < urlNavigator->historySize() - 1);
386     }
387 
388     QAction* forwardAction = actionCollection()->action(KStandardAction::name(KStandardAction::Forward));
389     if (forwardAction) {
390         forwardAction->setToolTip(i18nc("@info", "Go forward"));
391         forwardAction->setWhatsThis(xi18nc("@info:whatsthis go forward",
392             "This undoes a <interface>Go|Back</interface> action."));
393         forwardAction->setEnabled(index > 0);
394     }
395 }
396 
updateFilterBarAction(bool show)397 void DolphinMainWindow::updateFilterBarAction(bool show)
398 {
399     QAction* toggleFilterBarAction = actionCollection()->action(QStringLiteral("toggle_filter"));
400     toggleFilterBarAction->setChecked(show);
401 }
402 
openNewMainWindow()403 void DolphinMainWindow::openNewMainWindow()
404 {
405     Dolphin::openNewWindow({m_activeViewContainer->url()}, this);
406 }
407 
openNewActivatedTab()408 void DolphinMainWindow::openNewActivatedTab()
409 {
410     // keep browsers compatibility, new tab is always after last one
411     auto openNewTabAfterLastTabConfigured = GeneralSettings::openNewTabAfterLastTab();
412     GeneralSettings::setOpenNewTabAfterLastTab(true);
413     m_tabWidget->openNewActivatedTab();
414     GeneralSettings::setOpenNewTabAfterLastTab(openNewTabAfterLastTabConfigured);
415 }
416 
addToPlaces()417 void DolphinMainWindow::addToPlaces()
418 {
419     QUrl url;
420     QString name;
421 
422     // If nothing is selected, act on the current dir
423     if (m_activeViewContainer->view()->selectedItems().isEmpty()) {
424         url = m_activeViewContainer->url();
425         name = m_activeViewContainer->placesText();
426     } else {
427         const auto dirToAdd = m_activeViewContainer->view()->selectedItems().first();
428         url = dirToAdd.url();
429         name = dirToAdd.name();
430     }
431     if (url.isValid()) {
432         PlacesItemModel model;
433         QString icon;
434         if (m_activeViewContainer->isSearchModeEnabled()) {
435             icon = QStringLiteral("folder-saved-search-symbolic");
436         } else {
437             icon = KIO::iconNameForUrl(url);
438         }
439         model.createPlacesItem(name, url, icon);
440     }
441 }
442 
openNewTab(const QUrl & url)443 void DolphinMainWindow::openNewTab(const QUrl& url)
444 {
445     m_tabWidget->openNewTab(url, QUrl());
446 }
447 
slotSplitViewChanged()448 void DolphinMainWindow::slotSplitViewChanged()
449 {
450     m_tabWidget->currentTabPage()->setSplitViewEnabled(GeneralSettings::splitView(), WithAnimation);
451     updateSplitAction();
452 }
453 
openInNewTab()454 void DolphinMainWindow::openInNewTab()
455 {
456     const KFileItemList& list = m_activeViewContainer->view()->selectedItems();
457     bool tabCreated = false;
458 
459     for (const KFileItem& item : list) {
460         const QUrl& url = DolphinView::openItemAsFolderUrl(item);
461         if (!url.isEmpty()) {
462             openNewTab(url);
463             tabCreated = true;
464         }
465     }
466 
467     // if no new tab has been created from the selection
468     // open the current directory in a new tab
469     if (!tabCreated) {
470         openNewTab(m_activeViewContainer->url());
471     }
472 }
473 
openInNewWindow()474 void DolphinMainWindow::openInNewWindow()
475 {
476     QUrl newWindowUrl;
477 
478     const KFileItemList list = m_activeViewContainer->view()->selectedItems();
479     if (list.isEmpty()) {
480         newWindowUrl = m_activeViewContainer->url();
481     } else if (list.count() == 1) {
482         const KFileItem& item = list.first();
483         newWindowUrl = DolphinView::openItemAsFolderUrl(item);
484     }
485 
486     if (!newWindowUrl.isEmpty()) {
487         Dolphin::openNewWindow({newWindowUrl}, this);
488     }
489 }
490 
showTarget()491 void DolphinMainWindow::showTarget()
492 {
493     const auto link = m_activeViewContainer->view()->selectedItems().at(0);
494     const auto linkLocationDir = QFileInfo(link.localPath()).absoluteDir();
495     auto linkDestination = link.linkDest();
496     if (QFileInfo(linkDestination).isRelative()) {
497         linkDestination = linkLocationDir.filePath(linkDestination);
498     }
499     if (QFileInfo::exists(linkDestination)) {
500         KIO::highlightInFileManager({QUrl::fromLocalFile(linkDestination).adjusted(QUrl::StripTrailingSlash)});
501     } else {
502         m_activeViewContainer->showMessage(xi18nc("@info", "Could not access <filename>%1</filename>.", linkDestination),
503                                            DolphinViewContainer::Warning);
504     }
505 }
506 
showEvent(QShowEvent * event)507 void DolphinMainWindow::showEvent(QShowEvent* event)
508 {
509     KXmlGuiWindow::showEvent(event);
510 
511     if (!event->spontaneous()) {
512         m_activeViewContainer->view()->setFocus();
513     }
514 }
515 
closeEvent(QCloseEvent * event)516 void DolphinMainWindow::closeEvent(QCloseEvent* event)
517 {
518     // Find out if Dolphin is closed directly by the user or
519     // by the session manager because the session is closed
520     bool closedByUser = true;
521     if (qApp->isSavingSession()) {
522         closedByUser = false;
523     }
524 
525     if (m_tabWidget->count() > 1
526         && GeneralSettings::confirmClosingMultipleTabs()
527         && !GeneralSettings::rememberOpenedTabs()
528         && closedByUser) {
529         // Ask the user if he really wants to quit and close all tabs.
530         // Open a confirmation dialog with 3 buttons:
531         // QDialogButtonBox::Yes    -> Quit
532         // QDialogButtonBox::No     -> Close only the current tab
533         // QDialogButtonBox::Cancel -> do nothing
534         QDialog *dialog = new QDialog(this, Qt::Dialog);
535         dialog->setWindowTitle(i18nc("@title:window", "Confirmation"));
536         dialog->setModal(true);
537         QDialogButtonBox* buttons = new QDialogButtonBox(QDialogButtonBox::Yes | QDialogButtonBox::No | QDialogButtonBox::Cancel);
538         KGuiItem::assign(buttons->button(QDialogButtonBox::Yes), KGuiItem(i18nc("@action:button 'Quit Dolphin' button", "&Quit %1", QGuiApplication::applicationDisplayName()), QIcon::fromTheme(QStringLiteral("application-exit"))));
539         KGuiItem::assign(buttons->button(QDialogButtonBox::No), KGuiItem(i18n("C&lose Current Tab"), QIcon::fromTheme(QStringLiteral("tab-close"))));
540         KGuiItem::assign(buttons->button(QDialogButtonBox::Cancel), KStandardGuiItem::cancel());
541         buttons->button(QDialogButtonBox::Yes)->setDefault(true);
542 
543         bool doNotAskAgainCheckboxResult = false;
544 
545         const auto result = KMessageBox::createKMessageBox(dialog,
546             buttons,
547             QMessageBox::Warning,
548             i18n("You have multiple tabs open in this window, are you sure you want to quit?"),
549             QStringList(),
550             i18n("Do not ask again"),
551             &doNotAskAgainCheckboxResult,
552             KMessageBox::Notify);
553 
554         if (doNotAskAgainCheckboxResult) {
555             GeneralSettings::setConfirmClosingMultipleTabs(false);
556         }
557 
558         switch (result) {
559             case QDialogButtonBox::Yes:
560                 // Quit
561                 break;
562             case QDialogButtonBox::No:
563                 // Close only the current tab
564                 m_tabWidget->closeTab();
565                 Q_FALLTHROUGH();
566             default:
567                 event->ignore();
568                 return;
569         }
570     }
571 
572     if (m_terminalPanel && m_terminalPanel->hasProgramRunning() && GeneralSettings::confirmClosingTerminalRunningProgram() && closedByUser) {
573         // Ask if the user really wants to quit Dolphin with a program that is still running in the Terminal panel
574         // Open a confirmation dialog with 3 buttons:
575         // QDialogButtonBox::Yes    -> Quit
576         // QDialogButtonBox::No     -> Show Terminal Panel
577         // QDialogButtonBox::Cancel -> do nothing
578         QDialog *dialog = new QDialog(this, Qt::Dialog);
579         dialog->setWindowTitle(i18nc("@title:window", "Confirmation"));
580         dialog->setModal(true);
581         auto standardButtons = QDialogButtonBox::Yes | QDialogButtonBox::Cancel;
582         if (!m_terminalPanel->isVisible()) {
583             standardButtons |= QDialogButtonBox::No;
584         }
585         QDialogButtonBox *buttons = new QDialogButtonBox(standardButtons);
586         KGuiItem::assign(buttons->button(QDialogButtonBox::Yes), KStandardGuiItem::quit());
587         if (!m_terminalPanel->isVisible()) {
588             KGuiItem::assign(
589                     buttons->button(QDialogButtonBox::No),
590                     KGuiItem(i18n("Show &Terminal Panel"), QIcon::fromTheme(QStringLiteral("dialog-scripts"))));
591         }
592         KGuiItem::assign(buttons->button(QDialogButtonBox::Cancel), KStandardGuiItem::cancel());
593 
594         bool doNotAskAgainCheckboxResult = false;
595 
596         const auto result = KMessageBox::createKMessageBox(
597                 dialog,
598                 buttons,
599                 QMessageBox::Warning,
600                 i18n("The program '%1' is still running in the Terminal panel. Are you sure you want to quit?", m_terminalPanel->runningProgramName()),
601                 QStringList(),
602                 i18n("Do not ask again"),
603                 &doNotAskAgainCheckboxResult,
604                 KMessageBox::Dangerous);
605 
606         if (doNotAskAgainCheckboxResult) {
607             GeneralSettings::setConfirmClosingTerminalRunningProgram(false);
608         }
609 
610         switch (result) {
611             case QDialogButtonBox::Yes:
612                 // Quit
613                 break;
614             case QDialogButtonBox::No:
615                 actionCollection()->action("show_terminal_panel")->trigger();
616                 // Do not quit, ignore quit event
617                 Q_FALLTHROUGH();
618             default:
619                 event->ignore();
620                 return;
621         }
622     }
623 
624     if (GeneralSettings::rememberOpenedTabs())  {
625         KConfigGui::setSessionConfig(QStringLiteral("dolphin"), QStringLiteral("dolphin"));
626         KConfig *config = KConfigGui::sessionConfig();
627         saveGlobalProperties(config);
628         savePropertiesInternal(config, 1);
629         config->sync();
630     }
631 
632     GeneralSettings::setVersion(CurrentDolphinVersion);
633     GeneralSettings::self()->save();
634 
635     KXmlGuiWindow::closeEvent(event);
636 }
637 
saveProperties(KConfigGroup & group)638 void DolphinMainWindow::saveProperties(KConfigGroup& group)
639 {
640     m_tabWidget->saveProperties(group);
641 }
642 
readProperties(const KConfigGroup & group)643 void DolphinMainWindow::readProperties(const KConfigGroup& group)
644 {
645     m_tabWidget->readProperties(group);
646 }
647 
updateNewMenu()648 void DolphinMainWindow::updateNewMenu()
649 {
650     m_newFileMenu->setViewShowsHiddenFiles(activeViewContainer()->view()->hiddenFilesShown());
651     m_newFileMenu->checkUpToDate();
652     m_newFileMenu->setPopupFiles(QList<QUrl>() << activeViewContainer()->url());
653 }
654 
createDirectory()655 void DolphinMainWindow::createDirectory()
656 {
657     m_newFileMenu->setViewShowsHiddenFiles(activeViewContainer()->view()->hiddenFilesShown());
658     m_newFileMenu->setPopupFiles(QList<QUrl>() << activeViewContainer()->url());
659     m_newFileMenu->createDirectory();
660 }
661 
quit()662 void DolphinMainWindow::quit()
663 {
664     close();
665 }
666 
showErrorMessage(const QString & message)667 void DolphinMainWindow::showErrorMessage(const QString& message)
668 {
669     m_activeViewContainer->showMessage(message, DolphinViewContainer::Error);
670 }
671 
slotUndoAvailable(bool available)672 void DolphinMainWindow::slotUndoAvailable(bool available)
673 {
674     QAction* undoAction = actionCollection()->action(KStandardAction::name(KStandardAction::Undo));
675     if (undoAction) {
676         undoAction->setEnabled(available);
677     }
678 }
679 
slotUndoTextChanged(const QString & text)680 void DolphinMainWindow::slotUndoTextChanged(const QString& text)
681 {
682     QAction* undoAction = actionCollection()->action(KStandardAction::name(KStandardAction::Undo));
683     if (undoAction) {
684         undoAction->setText(text);
685     }
686 }
687 
undo()688 void DolphinMainWindow::undo()
689 {
690     clearStatusBar();
691     KIO::FileUndoManager::self()->uiInterface()->setParentWidget(this);
692     KIO::FileUndoManager::self()->undo();
693 }
694 
cut()695 void DolphinMainWindow::cut()
696 {
697     m_activeViewContainer->view()->cutSelectedItemsToClipboard();
698 }
699 
copy()700 void DolphinMainWindow::copy()
701 {
702     m_activeViewContainer->view()->copySelectedItemsToClipboard();
703 }
704 
paste()705 void DolphinMainWindow::paste()
706 {
707     m_activeViewContainer->view()->paste();
708 }
709 
find()710 void DolphinMainWindow::find()
711 {
712     m_activeViewContainer->setSearchModeEnabled(true);
713 }
714 
updateSearchAction()715 void DolphinMainWindow::updateSearchAction()
716 {
717     QAction* toggleSearchAction = actionCollection()->action(QStringLiteral("toggle_search"));
718     toggleSearchAction->setChecked(m_activeViewContainer->isSearchModeEnabled());
719 }
720 
updatePasteAction()721 void DolphinMainWindow::updatePasteAction()
722 {
723     QAction* pasteAction = actionCollection()->action(KStandardAction::name(KStandardAction::Paste));
724     QPair<bool, QString> pasteInfo = m_activeViewContainer->view()->pasteInfo();
725     pasteAction->setEnabled(pasteInfo.first);
726     pasteAction->setText(pasteInfo.second);
727 }
728 
slotDirectoryLoadingCompleted()729 void DolphinMainWindow::slotDirectoryLoadingCompleted()
730 {
731     updatePasteAction();
732 }
733 
slotToolBarActionMiddleClicked(QAction * action)734 void DolphinMainWindow::slotToolBarActionMiddleClicked(QAction *action)
735 {
736     if (action == actionCollection()->action(KStandardAction::name(KStandardAction::Back))) {
737         goBackInNewTab();
738     } else if (action == actionCollection()->action(KStandardAction::name(KStandardAction::Forward))) {
739         goForwardInNewTab();
740     } else if (action == actionCollection()->action(QStringLiteral("go_up"))) {
741         goUpInNewTab();
742     } else if (action == actionCollection()->action(QStringLiteral("go_home"))) {
743         goHomeInNewTab();
744     }
745 }
746 
slotAboutToShowBackPopupMenu()747 void DolphinMainWindow::slotAboutToShowBackPopupMenu()
748 {
749     const KUrlNavigator *urlNavigator = m_activeViewContainer->urlNavigatorInternalWithHistory();
750     int entries = 0;
751     m_backAction->menu()->clear();
752     for (int i = urlNavigator->historyIndex() + 1; i < urlNavigator->historySize() && entries < MaxNumberOfNavigationentries; ++i, ++entries) {
753         QAction* action = new QAction(urlNavigator->locationUrl(i).toDisplayString(QUrl::PreferLocalFile), m_backAction->menu());
754         action->setData(i);
755         m_backAction->menu()->addAction(action);
756     }
757 }
758 
slotGoBack(QAction * action)759 void DolphinMainWindow::slotGoBack(QAction* action)
760 {
761     int gotoIndex = action->data().value<int>();
762     const KUrlNavigator *urlNavigator = m_activeViewContainer->urlNavigatorInternalWithHistory();
763     for (int i = gotoIndex - urlNavigator->historyIndex(); i > 0; --i) {
764         goBack();
765     }
766 }
767 
slotBackForwardActionMiddleClicked(QAction * action)768 void DolphinMainWindow::slotBackForwardActionMiddleClicked(QAction* action)
769 {
770     if (action) {
771         const KUrlNavigator *urlNavigator = activeViewContainer()->urlNavigatorInternalWithHistory();
772         openNewTab(urlNavigator->locationUrl(action->data().value<int>()));
773     }
774 }
775 
slotAboutToShowForwardPopupMenu()776 void DolphinMainWindow::slotAboutToShowForwardPopupMenu()
777 {
778     const KUrlNavigator *urlNavigator = m_activeViewContainer->urlNavigatorInternalWithHistory();
779     int entries = 0;
780     m_forwardAction->menu()->clear();
781     for (int i = urlNavigator->historyIndex() - 1; i >= 0 && entries < MaxNumberOfNavigationentries; --i, ++entries) {
782         QAction* action = new QAction(urlNavigator->locationUrl(i).toDisplayString(QUrl::PreferLocalFile), m_forwardAction->menu());
783         action->setData(i);
784         m_forwardAction->menu()->addAction(action);
785     }
786 }
787 
slotGoForward(QAction * action)788 void DolphinMainWindow::slotGoForward(QAction* action)
789 {
790     int gotoIndex = action->data().value<int>();
791     const KUrlNavigator *urlNavigator = m_activeViewContainer->urlNavigatorInternalWithHistory();
792     for (int i = urlNavigator->historyIndex() - gotoIndex; i > 0; --i) {
793         goForward();
794     }
795 }
796 
selectAll()797 void DolphinMainWindow::selectAll()
798 {
799     clearStatusBar();
800 
801     // if the URL navigator is editable and focused, select the whole
802     // URL instead of all items of the view
803 
804     KUrlNavigator* urlNavigator = m_activeViewContainer->urlNavigator();
805     QLineEdit* lineEdit = urlNavigator->editor()->lineEdit();
806     const bool selectUrl = urlNavigator->isUrlEditable() &&
807                            lineEdit->hasFocus();
808     if (selectUrl) {
809         lineEdit->selectAll();
810     } else {
811         m_activeViewContainer->view()->selectAll();
812     }
813 }
814 
invertSelection()815 void DolphinMainWindow::invertSelection()
816 {
817     clearStatusBar();
818     m_activeViewContainer->view()->invertSelection();
819 }
820 
toggleSplitView()821 void DolphinMainWindow::toggleSplitView()
822 {
823     DolphinTabPage* tabPage = m_tabWidget->currentTabPage();
824     tabPage->setSplitViewEnabled(!tabPage->splitViewEnabled(), WithAnimation);
825 
826     updateViewActions();
827 }
828 
toggleSplitStash()829 void DolphinMainWindow::toggleSplitStash()
830 {
831     DolphinTabPage* tabPage = m_tabWidget->currentTabPage();
832     tabPage->setSplitViewEnabled(false, WithAnimation);
833     tabPage->setSplitViewEnabled(true, WithAnimation, QUrl("stash:/"));
834 }
835 
reloadView()836 void DolphinMainWindow::reloadView()
837 {
838     clearStatusBar();
839     m_activeViewContainer->reload();
840     m_activeViewContainer->statusBar()->updateSpaceInfo();
841 }
842 
stopLoading()843 void DolphinMainWindow::stopLoading()
844 {
845     m_activeViewContainer->view()->stopLoading();
846 }
847 
enableStopAction()848 void DolphinMainWindow::enableStopAction()
849 {
850     actionCollection()->action(QStringLiteral("stop"))->setEnabled(true);
851 }
852 
disableStopAction()853 void DolphinMainWindow::disableStopAction()
854 {
855     actionCollection()->action(QStringLiteral("stop"))->setEnabled(false);
856 }
857 
showFilterBar()858 void DolphinMainWindow::showFilterBar()
859 {
860     m_activeViewContainer->setFilterBarVisible(true);
861 }
862 
toggleFilterBar()863 void DolphinMainWindow::toggleFilterBar()
864 {
865     const bool checked = !m_activeViewContainer->isFilterBarVisible();
866     m_activeViewContainer->setFilterBarVisible(checked);
867 
868     QAction* toggleFilterBarAction = actionCollection()->action(QStringLiteral("toggle_filter"));
869     toggleFilterBarAction->setChecked(checked);
870 }
871 
toggleEditLocation()872 void DolphinMainWindow::toggleEditLocation()
873 {
874     clearStatusBar();
875 
876     QAction* action = actionCollection()->action(QStringLiteral("editable_location"));
877     KUrlNavigator* urlNavigator = m_activeViewContainer->urlNavigator();
878     urlNavigator->setUrlEditable(action->isChecked());
879 }
880 
replaceLocation()881 void DolphinMainWindow::replaceLocation()
882 {
883     KUrlNavigator* navigator = m_activeViewContainer->urlNavigator();
884     QLineEdit* lineEdit = navigator->editor()->lineEdit();
885 
886     // If the text field currently has focus and everything is selected,
887     // pressing the keyboard shortcut returns the whole thing to breadcrumb mode
888     if (navigator->isUrlEditable()
889         && lineEdit->hasFocus()
890         && lineEdit->selectedText() == lineEdit->text() ) {
891         navigator->setUrlEditable(false);
892     } else {
893         navigator->setUrlEditable(true);
894         navigator->setFocus();
895         lineEdit->selectAll();
896     }
897 }
898 
togglePanelLockState()899 void DolphinMainWindow::togglePanelLockState()
900 {
901     const bool newLockState = !GeneralSettings::lockPanels();
902     const auto childrenObjects = children();
903     for (QObject* child : childrenObjects) {
904         DolphinDockWidget* dock = qobject_cast<DolphinDockWidget*>(child);
905         if (dock) {
906             dock->setLocked(newLockState);
907         }
908     }
909 
910     GeneralSettings::setLockPanels(newLockState);
911 }
912 
slotTerminalPanelVisibilityChanged()913 void DolphinMainWindow::slotTerminalPanelVisibilityChanged()
914 {
915     if (m_terminalPanel->isHiddenInVisibleWindow() && m_activeViewContainer) {
916         m_activeViewContainer->view()->setFocus();
917     }
918 }
919 
goBack()920 void DolphinMainWindow::goBack()
921 {
922     DolphinUrlNavigator *urlNavigator = m_activeViewContainer->urlNavigatorInternalWithHistory();
923     urlNavigator->goBack();
924 
925     if (urlNavigator->locationState().isEmpty()) {
926         // An empty location state indicates a redirection URL,
927         // which must be skipped too
928         urlNavigator->goBack();
929     }
930 }
931 
goForward()932 void DolphinMainWindow::goForward()
933 {
934     m_activeViewContainer->urlNavigatorInternalWithHistory()->goForward();
935 }
936 
goUp()937 void DolphinMainWindow::goUp()
938 {
939     m_activeViewContainer->urlNavigatorInternalWithHistory()->goUp();
940 }
941 
goHome()942 void DolphinMainWindow::goHome()
943 {
944     m_activeViewContainer->urlNavigatorInternalWithHistory()->goHome();
945 }
946 
goBackInNewTab()947 void DolphinMainWindow::goBackInNewTab()
948 {
949     const KUrlNavigator* urlNavigator = activeViewContainer()->urlNavigatorInternalWithHistory();
950     const int index = urlNavigator->historyIndex() + 1;
951     openNewTab(urlNavigator->locationUrl(index));
952 }
953 
goForwardInNewTab()954 void DolphinMainWindow::goForwardInNewTab()
955 {
956     const KUrlNavigator* urlNavigator = activeViewContainer()->urlNavigatorInternalWithHistory();
957     const int index = urlNavigator->historyIndex() - 1;
958     openNewTab(urlNavigator->locationUrl(index));
959 }
960 
goUpInNewTab()961 void DolphinMainWindow::goUpInNewTab()
962 {
963     const QUrl currentUrl = activeViewContainer()->urlNavigator()->locationUrl();
964     openNewTab(KIO::upUrl(currentUrl));
965 }
966 
goHomeInNewTab()967 void DolphinMainWindow::goHomeInNewTab()
968 {
969     openNewTab(Dolphin::homeUrl());
970 }
971 
compareFiles()972 void DolphinMainWindow::compareFiles()
973 {
974     const KFileItemList items = m_tabWidget->currentTabPage()->selectedItems();
975     if (items.count() != 2) {
976         // The action is disabled in this case, but it could have been triggered
977         // via D-Bus, see https://bugs.kde.org/show_bug.cgi?id=325517
978         return;
979     }
980 
981     QUrl urlA = items.at(0).url();
982     QUrl urlB = items.at(1).url();
983 
984     QString command(QStringLiteral("kompare -c \""));
985     command.append(urlA.toDisplayString(QUrl::PreferLocalFile));
986     command.append("\" \"");
987     command.append(urlB.toDisplayString(QUrl::PreferLocalFile));
988     command.append('\"');
989 
990     KIO::CommandLauncherJob *job = new KIO::CommandLauncherJob(command, this);
991     job->setDesktopName(QStringLiteral("org.kde.kompare"));
992     job->start();
993 }
994 
toggleShowMenuBar()995 void DolphinMainWindow::toggleShowMenuBar()
996 {
997     const bool visible = menuBar()->isVisible();
998     menuBar()->setVisible(!visible);
999 }
1000 
preferredSearchTool()1001 QPointer<QAction> DolphinMainWindow::preferredSearchTool()
1002 {
1003     m_searchTools.clear();
1004     KMoreToolsMenuFactory("dolphin/search-tools").fillMenuFromGroupingNames(
1005         &m_searchTools, { "files-find" }, m_activeViewContainer->url()
1006     );
1007     QList<QAction*> actions = m_searchTools.actions();
1008     if (actions.isEmpty()) {
1009         return nullptr;
1010     }
1011     QAction* action = actions.first();
1012     if (action->isSeparator()) {
1013         return nullptr;
1014     }
1015     return action;
1016 }
1017 
updateOpenPreferredSearchToolAction()1018 void DolphinMainWindow::updateOpenPreferredSearchToolAction()
1019 {
1020     QAction* openPreferredSearchTool = actionCollection()->action(QStringLiteral("open_preferred_search_tool"));
1021     if (!openPreferredSearchTool) {
1022         return;
1023     }
1024     QPointer<QAction> tool = preferredSearchTool();
1025     if (tool) {
1026         openPreferredSearchTool->setVisible(true);
1027         openPreferredSearchTool->setText(i18nc("@action:inmenu Tools", "Open %1", tool->text()));
1028         // Only override with the app icon if it is the default, i.e. the user hasn't configured one manually
1029         // https://bugs.kde.org/show_bug.cgi?id=442815
1030         if (openPreferredSearchTool->icon().name() == QLatin1String("search")) {
1031             openPreferredSearchTool->setIcon(tool->icon());
1032         }
1033     } else {
1034         openPreferredSearchTool->setVisible(false);
1035         // still visible in Shortcuts configuration window
1036         openPreferredSearchTool->setText(i18nc("@action:inmenu Tools", "Open Preferred Search Tool"));
1037         openPreferredSearchTool->setIcon(QIcon::fromTheme(QStringLiteral("search")));
1038     }
1039 }
1040 
openPreferredSearchTool()1041 void DolphinMainWindow::openPreferredSearchTool()
1042 {
1043     QPointer<QAction> tool = preferredSearchTool();
1044     if (tool) {
1045         tool->trigger();
1046     }
1047 }
1048 
openTerminal()1049 void DolphinMainWindow::openTerminal()
1050 {
1051     const QUrl url = m_activeViewContainer->url();
1052 
1053     if (url.isLocalFile()) {
1054         auto job = new KTerminalLauncherJob(QString());
1055         job->setWorkingDirectory(url.toLocalFile());
1056         job->start();
1057         return;
1058     }
1059 
1060      // Not a local file, with protocol Class ":local", try stat'ing
1061     if (KProtocolInfo::protocolClass(url.scheme()) == QLatin1String(":local")) {
1062         KIO::StatJob *job = KIO::mostLocalUrl(url);
1063         KJobWidgets::setWindow(job, this);
1064         connect(job, &KJob::result, this, [job]() {
1065             QUrl statUrl;
1066             if (!job->error()) {
1067                 statUrl = job->mostLocalUrl();
1068             }
1069 
1070             auto job = new KTerminalLauncherJob(QString());
1071             job->setWorkingDirectory(statUrl.isLocalFile() ? statUrl.toLocalFile() : QDir::homePath());
1072             job->start();
1073         });
1074 
1075         return;
1076     }
1077 
1078     // Nothing worked, just use $HOME
1079     auto job = new KTerminalLauncherJob(QString());
1080     job->setWorkingDirectory(QDir::homePath());
1081     job->start();
1082 }
1083 
editSettings()1084 void DolphinMainWindow::editSettings()
1085 {
1086     if (!m_settingsDialog) {
1087         DolphinViewContainer* container = activeViewContainer();
1088         container->view()->writeSettings();
1089 
1090         const QUrl url = container->url();
1091         DolphinSettingsDialog* settingsDialog = new DolphinSettingsDialog(url, this, actionCollection());
1092         connect(settingsDialog, &DolphinSettingsDialog::settingsChanged, this, &DolphinMainWindow::refreshViews);
1093         connect(settingsDialog, &DolphinSettingsDialog::settingsChanged,
1094                 &DolphinUrlNavigatorsController::slotReadSettings);
1095         settingsDialog->setAttribute(Qt::WA_DeleteOnClose);
1096         settingsDialog->show();
1097         m_settingsDialog = settingsDialog;
1098     } else {
1099         m_settingsDialog.data()->raise();
1100     }
1101 }
1102 
handleUrl(const QUrl & url)1103 void DolphinMainWindow::handleUrl(const QUrl& url)
1104 {
1105     delete m_lastHandleUrlOpenJob;
1106     m_lastHandleUrlOpenJob = nullptr;
1107 
1108     if (url.isLocalFile() && QFileInfo(url.toLocalFile()).isDir()) {
1109         activeViewContainer()->setUrl(url);
1110     } else {
1111         m_lastHandleUrlOpenJob = new KIO::OpenUrlJob(url);
1112         m_lastHandleUrlOpenJob->setUiDelegate(new KIO::JobUiDelegate(KJobUiDelegate::AutoHandlingEnabled, this));
1113         m_lastHandleUrlOpenJob->setShowOpenOrExecuteDialog(true);
1114 
1115         connect(m_lastHandleUrlOpenJob, &KIO::OpenUrlJob::mimeTypeFound, this,
1116                 [this, url](const QString &mimetype) {
1117                     if (mimetype == QLatin1String("inode/directory")) {
1118                         // If it's a dir, we'll take it from here
1119                         m_lastHandleUrlOpenJob->kill();
1120                         m_lastHandleUrlOpenJob = nullptr;
1121                         activeViewContainer()->setUrl(url);
1122                     }
1123         });
1124 
1125         connect(m_lastHandleUrlOpenJob, &KIO::OpenUrlJob::result, this, [this]() {
1126             m_lastHandleUrlOpenJob = nullptr;
1127         });
1128 
1129         m_lastHandleUrlOpenJob->start();
1130     }
1131 }
1132 
slotWriteStateChanged(bool isFolderWritable)1133 void DolphinMainWindow::slotWriteStateChanged(bool isFolderWritable)
1134 {
1135     // trash:/ is writable but we don't want to create new items in it.
1136     // TODO: remove the trash check once https://phabricator.kde.org/T8234 is implemented
1137     newFileMenu()->setEnabled(isFolderWritable && m_activeViewContainer->url().scheme() != QLatin1String("trash"));
1138 }
1139 
openContextMenu(const QPoint & pos,const KFileItem & item,const QUrl & url,const QList<QAction * > & customActions)1140 void DolphinMainWindow::openContextMenu(const QPoint& pos,
1141                                         const KFileItem& item,
1142                                         const QUrl& url,
1143                                         const QList<QAction*>& customActions)
1144 {
1145     QPointer<DolphinContextMenu> contextMenu = new DolphinContextMenu(this, pos, item, url, &m_fileItemActions);
1146     contextMenu.data()->setCustomActions(customActions);
1147     const DolphinContextMenu::Command command = contextMenu.data()->open();
1148 
1149     switch (command) {
1150     case DolphinContextMenu::OpenParentFolder:
1151         changeUrl(KIO::upUrl(item.url()));
1152         m_activeViewContainer->view()->markUrlsAsSelected({item.url()});
1153         m_activeViewContainer->view()->markUrlAsCurrent(item.url());
1154         break;
1155 
1156     case DolphinContextMenu::OpenParentFolderInNewWindow:
1157         Dolphin::openNewWindow({item.url()}, this, Dolphin::OpenNewWindowFlag::Select);
1158         break;
1159 
1160     case DolphinContextMenu::OpenParentFolderInNewTab:
1161         openNewTab(KIO::upUrl(item.url()));
1162         break;
1163 
1164     case DolphinContextMenu::None:
1165     default:
1166         break;
1167     }
1168 
1169     // Delete the menu, unless it has been deleted in its own nested event loop already.
1170     if (contextMenu) {
1171         contextMenu->deleteLater();
1172     }
1173 }
1174 
updateHamburgerMenu()1175 void DolphinMainWindow::updateHamburgerMenu()
1176 {
1177     KActionCollection* ac = actionCollection();
1178     auto hamburgerMenu = static_cast<KHamburgerMenu *>(
1179                     ac->action(KStandardAction::name(KStandardAction::HamburgerMenu)));
1180     auto menu = hamburgerMenu->menu();
1181     if (!menu) {
1182         menu = new QMenu(this);
1183         hamburgerMenu->setMenu(menu);
1184         hamburgerMenu->hideActionsOf(ac->action(QStringLiteral("basic_actions"))->menu());
1185         hamburgerMenu->hideActionsOf(ac->action(QStringLiteral("zoom"))->menu());
1186     } else {
1187         menu->clear();
1188     }
1189     const QList<QAction *> toolbarActions = toolBar()->actions();
1190 
1191     if (!toolBar()->isVisible()) {
1192         // If neither the menu bar nor the toolbar are visible, these actions should be available.
1193         menu->addAction(ac->action(KStandardAction::name(KStandardAction::ShowMenubar)));
1194         menu->addAction(toolBarMenuAction());
1195         menu->addSeparator();
1196     }
1197 
1198     // This group of actions (until the next separator) contains all the most basic actions
1199     // necessary to use Dolphin effectively.
1200     menu->addAction(ac->action(QStringLiteral("go_back")));
1201     menu->addAction(ac->action(QStringLiteral("go_forward")));
1202 
1203     menu->addMenu(m_newFileMenu->menu());
1204     menu->addAction(ac->action(QStringLiteral("basic_actions")));
1205     menu->addAction(ac->action(KStandardAction::name(KStandardAction::Undo)));
1206     if (!toolBar()->isVisible()
1207         || (!toolbarActions.contains(ac->action(QStringLiteral("toggle_search")))
1208             && !toolbarActions.contains(ac->action(QStringLiteral("open_preferred_search_tool"))))
1209     ) {
1210         menu->addAction(ac->action(KStandardAction::name(KStandardAction::Find)));
1211         // This way a search action will only be added if none of the three available
1212         // search actions is present on the toolbar.
1213     }
1214     if (!toolBar()->isVisible()
1215         || !toolbarActions.contains(ac->action(QStringLiteral("toggle_filter")))
1216     ) {
1217         menu->addAction(ac->action(QStringLiteral("show_filter_bar")));
1218         // This way a filter action will only be added if none of the two available
1219         // filter actions is present on the toolbar.
1220     }
1221     menu->addSeparator();
1222 
1223     // The second group of actions (up until the next separator) contains actions for opening
1224     // additional views to interact with the file system.
1225     menu->addAction(ac->action(QStringLiteral("file_new")));
1226     menu->addAction(ac->action(QStringLiteral("new_tab")));
1227     if (ac->action(QStringLiteral("undo_close_tab"))->isEnabled()) {
1228         menu->addAction(ac->action(QStringLiteral("closed_tabs")));
1229     }
1230     menu->addAction(ac->action(QStringLiteral("open_terminal")));
1231     menu->addSeparator();
1232 
1233     // The third group contains actions to change what one sees in the view
1234     // and to change the more general UI.
1235     if (!toolBar()->isVisible()
1236         || (!toolbarActions.contains(ac->action(QStringLiteral("icons")))
1237             && !toolbarActions.contains(ac->action(QStringLiteral("compact")))
1238             && !toolbarActions.contains(ac->action(QStringLiteral("details")))
1239             && !toolbarActions.contains(ac->action(QStringLiteral("view_mode"))))
1240     ) {
1241         menu->addAction(ac->action(QStringLiteral("view_mode")));
1242     }
1243     menu->addAction(ac->action(QStringLiteral("show_hidden_files")));
1244     menu->addAction(ac->action(QStringLiteral("sort")));
1245     menu->addAction(ac->action(QStringLiteral("additional_info")));
1246     if (!GeneralSettings::showStatusBar() || !GeneralSettings::showZoomSlider()) {
1247         menu->addAction(ac->action(QStringLiteral("zoom")));
1248     }
1249     menu->addAction(ac->action(QStringLiteral("panels")));
1250 
1251     // The "Configure" menu is not added to the actionCollection() because there is hardly
1252     // a good reason for users to put it on their toolbar.
1253     auto configureMenu = menu->addMenu(QIcon::fromTheme(QStringLiteral("configure")),
1254                             i18nc("@action:inmenu menu for configure actions", "Configure"));
1255     configureMenu->addAction(ac->action(KStandardAction::name(KStandardAction::SwitchApplicationLanguage)));
1256     configureMenu->addAction(ac->action(KStandardAction::name(KStandardAction::KeyBindings)));
1257     configureMenu->addAction(ac->action(KStandardAction::name(KStandardAction::ConfigureToolbars)));
1258     configureMenu->addAction(ac->action(KStandardAction::name(KStandardAction::Preferences)));
1259     hamburgerMenu->hideActionsOf(configureMenu);
1260 }
1261 
slotPlaceActivated(const QUrl & url)1262 void DolphinMainWindow::slotPlaceActivated(const QUrl& url)
1263 {
1264     DolphinViewContainer* view = activeViewContainer();
1265 
1266     if (view->url() == url) {
1267         // We can end up here if the user clicked a device in the Places Panel
1268         // which had been unmounted earlier, see https://bugs.kde.org/show_bug.cgi?id=161385.
1269         reloadView();
1270     } else {
1271         view->disableUrlNavigatorSelectionRequests();
1272         changeUrl(url);
1273         view->enableUrlNavigatorSelectionRequests();
1274     }
1275 }
1276 
closedTabsCountChanged(unsigned int count)1277 void DolphinMainWindow::closedTabsCountChanged(unsigned int count)
1278 {
1279     actionCollection()->action(QStringLiteral("undo_close_tab"))->setEnabled(count > 0);
1280 }
1281 
activeViewChanged(DolphinViewContainer * viewContainer)1282 void DolphinMainWindow::activeViewChanged(DolphinViewContainer* viewContainer)
1283 {
1284     DolphinViewContainer* oldViewContainer = m_activeViewContainer;
1285     Q_ASSERT(viewContainer);
1286 
1287     m_activeViewContainer = viewContainer;
1288 
1289     if (oldViewContainer) {
1290         const QAction* toggleSearchAction = actionCollection()->action(QStringLiteral("toggle_search"));
1291         toggleSearchAction->disconnect(oldViewContainer);
1292 
1293         // Disconnect all signals between the old view container (container,
1294         // view and url navigator) and main window.
1295         oldViewContainer->disconnect(this);
1296         oldViewContainer->view()->disconnect(this);
1297         oldViewContainer->urlNavigatorInternalWithHistory()->disconnect(this);
1298         auto navigators = static_cast<DolphinNavigatorsWidgetAction *>
1299                           (actionCollection()->action(QStringLiteral("url_navigators")));
1300         navigators->primaryUrlNavigator()->disconnect(this);
1301         if (auto secondaryUrlNavigator = navigators->secondaryUrlNavigator()) {
1302             secondaryUrlNavigator->disconnect(this);
1303         }
1304 
1305         // except the requestItemInfo so that on hover the information panel can still be updated
1306         connect(oldViewContainer->view(), &DolphinView::requestItemInfo,
1307                 this, &DolphinMainWindow::requestItemInfo);
1308     }
1309 
1310     connectViewSignals(viewContainer);
1311 
1312     m_actionHandler->setCurrentView(viewContainer->view());
1313 
1314     updateHistory();
1315     updateFileAndEditActions();
1316     updatePasteAction();
1317     updateViewActions();
1318     updateGoActions();
1319     updateSearchAction();
1320 
1321     const QUrl url = viewContainer->url();
1322     Q_EMIT urlChanged(url);
1323 }
1324 
tabCountChanged(int count)1325 void DolphinMainWindow::tabCountChanged(int count)
1326 {
1327     const bool enableTabActions = (count > 1);
1328     for (int i = 0; i < MaxActivateTabShortcuts; ++i) {
1329         actionCollection()->action(QStringLiteral("activate_tab_%1").arg(i))->setEnabled(enableTabActions);
1330     }
1331     actionCollection()->action(QStringLiteral("activate_last_tab"))->setEnabled(enableTabActions);
1332     actionCollection()->action(QStringLiteral("activate_next_tab"))->setEnabled(enableTabActions);
1333     actionCollection()->action(QStringLiteral("activate_prev_tab"))->setEnabled(enableTabActions);
1334 }
1335 
updateWindowTitle()1336 void DolphinMainWindow::updateWindowTitle()
1337 {
1338     const QString newTitle = m_activeViewContainer->captionWindowTitle();
1339     if (windowTitle() != newTitle) {
1340         setWindowTitle(newTitle);
1341     }
1342 }
1343 
slotStorageTearDownFromPlacesRequested(const QString & mountPath)1344 void DolphinMainWindow::slotStorageTearDownFromPlacesRequested(const QString& mountPath)
1345 {
1346     connect(m_placesPanel, &PlacesPanel::storageTearDownSuccessful, this, [this, mountPath]() {
1347         setViewsToHomeIfMountPathOpen(mountPath);
1348     });
1349 
1350     if (m_terminalPanel && m_terminalPanel->currentWorkingDirectory().startsWith(mountPath)) {
1351         m_tearDownFromPlacesRequested = true;
1352         m_terminalPanel->goHome();
1353         // m_placesPanel->proceedWithTearDown() will be called in slotTerminalDirectoryChanged
1354     } else {
1355         m_placesPanel->proceedWithTearDown();
1356     }
1357 }
1358 
slotStorageTearDownExternallyRequested(const QString & mountPath)1359 void DolphinMainWindow::slotStorageTearDownExternallyRequested(const QString& mountPath)
1360 {
1361     connect(m_placesPanel, &PlacesPanel::storageTearDownSuccessful, this, [this, mountPath]() {
1362         setViewsToHomeIfMountPathOpen(mountPath);
1363     });
1364 
1365     if (m_terminalPanel && m_terminalPanel->currentWorkingDirectory().startsWith(mountPath)) {
1366         m_tearDownFromPlacesRequested = false;
1367         m_terminalPanel->goHome();
1368     }
1369 }
1370 
setViewsToHomeIfMountPathOpen(const QString & mountPath)1371 void DolphinMainWindow::setViewsToHomeIfMountPathOpen(const QString& mountPath)
1372 {
1373     const QVector<DolphinViewContainer*> theViewContainers = viewContainers();
1374     for (DolphinViewContainer *viewContainer : theViewContainers) {
1375         if (viewContainer && viewContainer->url().toLocalFile().startsWith(mountPath)) {
1376             viewContainer->setUrl(QUrl::fromLocalFile(QDir::homePath()));
1377         }
1378     }
1379     disconnect(m_placesPanel, &PlacesPanel::storageTearDownSuccessful, nullptr, nullptr);
1380 }
1381 
setupActions()1382 void DolphinMainWindow::setupActions()
1383 {
1384     KStandardAction::hamburgerMenu(nullptr, nullptr, actionCollection());
1385 
1386     // setup 'File' menu
1387     m_newFileMenu = new DolphinNewFileMenu(actionCollection(), this);
1388     QMenu* menu = m_newFileMenu->menu();
1389     menu->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New"));
1390     menu->setIcon(QIcon::fromTheme(QStringLiteral("list-add")));
1391     m_newFileMenu->setPopupMode(QToolButton::InstantPopup);
1392     connect(menu, &QMenu::aboutToShow,
1393             this, &DolphinMainWindow::updateNewMenu);
1394 
1395     QAction* newWindow = KStandardAction::openNew(this, &DolphinMainWindow::openNewMainWindow, actionCollection());
1396     newWindow->setText(i18nc("@action:inmenu File", "New &Window"));
1397     newWindow->setToolTip(i18nc("@info", "Open a new Dolphin window"));
1398     newWindow->setWhatsThis(xi18nc("@info:whatsthis", "This opens a new "
1399         "window just like this one with the current location and view."
1400         "<nl/>You can drag and drop items between windows."));
1401     newWindow->setIcon(QIcon::fromTheme(QStringLiteral("window-new")));
1402 
1403     QAction* newTab = actionCollection()->addAction(QStringLiteral("new_tab"));
1404     newTab->setIcon(QIcon::fromTheme(QStringLiteral("tab-new")));
1405     newTab->setText(i18nc("@action:inmenu File", "New Tab"));
1406     newTab->setWhatsThis(xi18nc("@info:whatsthis", "This opens a new "
1407         "<emphasis>Tab</emphasis> with the current location and view.<nl/>"
1408         "A tab is an additional view within this window. "
1409         "You can drag and drop items between tabs."));
1410     actionCollection()->setDefaultShortcuts(newTab, {Qt::CTRL | Qt::Key_T, Qt::CTRL | Qt::SHIFT | Qt::Key_N});
1411     connect(newTab, &QAction::triggered, this, &DolphinMainWindow::openNewActivatedTab);
1412 
1413     QAction* addToPlaces = actionCollection()->addAction(QStringLiteral("add_to_places"));
1414     addToPlaces->setIcon(QIcon::fromTheme(QStringLiteral("bookmark-new")));
1415     addToPlaces->setText(i18nc("@action:inmenu Add current folder to places", "Add to Places"));
1416     addToPlaces->setWhatsThis(xi18nc("@info:whatsthis", "This adds the selected folder "
1417         "to the Places panel."));
1418     connect(addToPlaces, &QAction::triggered, this, &DolphinMainWindow::addToPlaces);
1419 
1420     QAction* closeTab = KStandardAction::close(m_tabWidget, QOverload<>::of(&DolphinTabWidget::closeTab), actionCollection());
1421     closeTab->setText(i18nc("@action:inmenu File", "Close Tab"));
1422     closeTab->setWhatsThis(i18nc("@info:whatsthis", "This closes the "
1423         "currently viewed tab. If no more tabs are left this window "
1424         "will close instead."));
1425 
1426     QAction* quitAction = KStandardAction::quit(this, &DolphinMainWindow::quit, actionCollection());
1427     quitAction->setWhatsThis(i18nc("@info:whatsthis quit", "This closes this window."));
1428 
1429     // setup 'Edit' menu
1430     KStandardAction::undo(this,
1431                           &DolphinMainWindow::undo,
1432                           actionCollection());
1433 
1434     // i18n: This will be the last paragraph for the whatsthis for all three:
1435     // Cut, Copy and Paste
1436     const QString cutCopyPastePara = xi18nc("@info:whatsthis", "<para><emphasis>Cut, "
1437         "Copy</emphasis> and <emphasis>Paste</emphasis> work between many "
1438         "applications and are among the most used commands. That's why their "
1439         "<emphasis>keyboard shortcuts</emphasis> are prominently placed right "
1440         "next to each other on the keyboard: <shortcut>Ctrl+X</shortcut>, "
1441         "<shortcut>Ctrl+C</shortcut> and <shortcut>Ctrl+V</shortcut>.</para>");
1442     QAction* cutAction = KStandardAction::cut(this, &DolphinMainWindow::cut, actionCollection());
1443     cutAction->setWhatsThis(xi18nc("@info:whatsthis cut", "This copies the items "
1444         "in your current selection to the <emphasis>clipboard</emphasis>.<nl/>"
1445         "Use the <emphasis>Paste</emphasis> action afterwards to copy them from "
1446         "the clipboard to a new location. The items will be removed from their "
1447         "initial location.") + cutCopyPastePara);
1448     QAction* copyAction = KStandardAction::copy(this, &DolphinMainWindow::copy, actionCollection());
1449     copyAction->setWhatsThis(xi18nc("@info:whatsthis copy", "This copies the "
1450         "items in your current selection to the <emphasis>clipboard</emphasis>."
1451         "<nl/>Use the <emphasis>Paste</emphasis> action afterwards to copy them "
1452         "from the clipboard to a new location.") +  cutCopyPastePara);
1453     QAction* paste = KStandardAction::paste(this, &DolphinMainWindow::paste, actionCollection());
1454     // The text of the paste-action is modified dynamically by Dolphin
1455     // (e. g. to "Paste One Folder"). To prevent that the size of the toolbar changes
1456     // due to the long text, the text "Paste" is used:
1457     paste->setIconText(i18nc("@action:inmenu Edit", "Paste"));
1458     paste->setWhatsThis(xi18nc("@info:whatsthis paste", "This copies the items from "
1459         "your <emphasis>clipboard</emphasis> to the currently viewed folder.<nl/>"
1460         "If the items were added to the clipboard by the <emphasis>Cut</emphasis> "
1461         "action they are removed from their old location.") +  cutCopyPastePara);
1462 
1463     QAction* copyToOtherViewAction = actionCollection()->addAction(QStringLiteral("copy_to_inactive_split_view"));
1464     copyToOtherViewAction->setText(i18nc("@action:inmenu", "Copy to Inactive Split View"));
1465     copyToOtherViewAction->setWhatsThis(xi18nc("@info:whatsthis Copy", "This copies the selected items from "
1466         "the <emphasis>active</emphasis> view to the inactive split view."));
1467     copyToOtherViewAction->setIcon(QIcon::fromTheme(QStringLiteral("edit-copy")));
1468     copyToOtherViewAction->setIconText(i18nc("@action:inmenu Edit", "Copy to Inactive Split View"));
1469     actionCollection()->setDefaultShortcut(copyToOtherViewAction, Qt::SHIFT | Qt::Key_F5 );
1470     connect(copyToOtherViewAction, &QAction::triggered, m_tabWidget, &DolphinTabWidget::copyToInactiveSplitView);
1471 
1472     QAction* moveToOtherViewAction = actionCollection()->addAction(QStringLiteral("move_to_inactive_split_view"));
1473     moveToOtherViewAction->setText(i18nc("@action:inmenu", "Move to Inactive Split View"));
1474     moveToOtherViewAction->setWhatsThis(xi18nc("@info:whatsthis Move", "This moves the selected items from "
1475         "the <emphasis>active</emphasis> view to the inactive split view."));
1476     moveToOtherViewAction->setIcon(QIcon::fromTheme(QStringLiteral("edit-cut")));
1477     moveToOtherViewAction->setIconText(i18nc("@action:inmenu Edit", "Move to Inactive Split View"));
1478     actionCollection()->setDefaultShortcut(moveToOtherViewAction, Qt::SHIFT | Qt::Key_F6 );
1479     connect(moveToOtherViewAction, &QAction::triggered, m_tabWidget, &DolphinTabWidget::moveToInactiveSplitView);
1480 
1481     QAction* showFilterBar = actionCollection()->addAction(QStringLiteral("show_filter_bar"));
1482     showFilterBar->setText(i18nc("@action:inmenu Tools", "Filter..."));
1483     showFilterBar->setToolTip(i18nc("@info:tooltip", "Toggle Filter Bar"));
1484     showFilterBar->setWhatsThis(xi18nc("@info:whatsthis", "This opens the "
1485         "<emphasis>Filter Bar</emphasis> at the bottom of the window.<nl/> "
1486         "There you can enter a text to filter the files and folders currently displayed. "
1487         "Only those that contain the text in their name will be kept in view."));
1488     showFilterBar->setIcon(QIcon::fromTheme(QStringLiteral("view-filter")));
1489     actionCollection()->setDefaultShortcuts(showFilterBar, {Qt::CTRL | Qt::Key_I, Qt::Key_Slash});
1490     connect(showFilterBar, &QAction::triggered, this, &DolphinMainWindow::showFilterBar);
1491 
1492     // toggle_filter acts as a copy of the main showFilterBar to be used mainly
1493     // in the toolbar, with no default shortcut attached, to avoid messing with
1494     // existing workflows (filter bar always open and Ctrl-I to focus)
1495     QAction *toggleFilter = actionCollection()->addAction(QStringLiteral("toggle_filter"));
1496     toggleFilter->setText(i18nc("@action:inmenu", "Toggle Filter Bar"));
1497     toggleFilter->setIconText(i18nc("@action:intoolbar", "Filter"));
1498     toggleFilter->setIcon(showFilterBar->icon());
1499     toggleFilter->setToolTip(showFilterBar->toolTip());
1500     toggleFilter->setWhatsThis(showFilterBar->whatsThis());
1501     toggleFilter->setCheckable(true);
1502     connect(toggleFilter, &QAction::triggered, this, &DolphinMainWindow::toggleFilterBar);
1503 
1504     QAction *searchAction = KStandardAction::find(this, &DolphinMainWindow::find, actionCollection());
1505     searchAction->setText(i18n("Search..."));
1506     searchAction->setToolTip(i18nc("@info:tooltip", "Search for files and folders"));
1507     searchAction->setWhatsThis(xi18nc("@info:whatsthis find", "<para>This helps you "
1508         "find files and folders by opening a <emphasis>find bar</emphasis>. "
1509         "There you can enter search terms and specify settings to find the "
1510         "objects you are looking for.</para><para>Use this help again on "
1511         "the find bar so we can have a look at it while the settings are "
1512         "explained.</para>"));
1513 
1514     // toggle_search acts as a copy of the main searchAction to be used mainly
1515     // in the toolbar, with no default shortcut attached, to avoid messing with
1516     // existing workflows (search bar always open and Ctrl-F to focus)
1517     QAction *toggleSearchAction = actionCollection()->addAction(QStringLiteral("toggle_search"));
1518     toggleSearchAction->setText(i18nc("@action:inmenu", "Toggle Search Bar"));
1519     toggleSearchAction->setIconText(i18nc("@action:intoolbar", "Search"));
1520     toggleSearchAction->setIcon(searchAction->icon());
1521     toggleSearchAction->setToolTip(searchAction->toolTip());
1522     toggleSearchAction->setWhatsThis(searchAction->whatsThis());
1523     toggleSearchAction->setCheckable(true);
1524 
1525     QAction* selectAllAction = KStandardAction::selectAll(this, &DolphinMainWindow::selectAll, actionCollection());
1526     selectAllAction->setWhatsThis(xi18nc("@info:whatsthis", "This selects all "
1527         "files and folders in the current location."));
1528 
1529     QAction* invertSelection = actionCollection()->addAction(QStringLiteral("invert_selection"));
1530     invertSelection->setText(i18nc("@action:inmenu Edit", "Invert Selection"));
1531     invertSelection->setWhatsThis(xi18nc("@info:whatsthis invert", "This selects all "
1532         "objects that you have currently <emphasis>not</emphasis> selected instead."));
1533     invertSelection->setIcon(QIcon::fromTheme(QStringLiteral("edit-select-invert")));
1534     actionCollection()->setDefaultShortcut(invertSelection, Qt::CTRL | Qt::SHIFT | Qt::Key_A);
1535     connect(invertSelection, &QAction::triggered, this, &DolphinMainWindow::invertSelection);
1536 
1537     // setup 'View' menu
1538     // (note that most of it is set up in DolphinViewActionHandler)
1539 
1540     QAction* split = actionCollection()->addAction(QStringLiteral("split_view"));
1541     split->setWhatsThis(xi18nc("@info:whatsthis find", "<para>This splits "
1542         "the folder view below into two autonomous views.</para><para>This "
1543         "way you can see two locations at once and move items between them "
1544         "quickly.</para>Click this again afterwards to recombine the views."));
1545     actionCollection()->setDefaultShortcut(split, Qt::Key_F3);
1546     connect(split, &QAction::triggered, this, &DolphinMainWindow::toggleSplitView);
1547 
1548     QAction* stashSplit = actionCollection()->addAction(QStringLiteral("split_stash"));
1549     actionCollection()->setDefaultShortcut(stashSplit, Qt::CTRL | Qt::Key_S);
1550     stashSplit->setText(i18nc("@action:intoolbar Stash", "Stash"));
1551     stashSplit->setToolTip(i18nc("@info", "Opens the stash virtual directory in a split window"));
1552     stashSplit->setIcon(QIcon::fromTheme(QStringLiteral("folder-stash")));
1553     stashSplit->setCheckable(false);
1554     QDBusConnectionInterface *sessionInterface = QDBusConnection::sessionBus().interface();
1555     stashSplit->setVisible(sessionInterface && sessionInterface->isServiceRegistered(QStringLiteral("org.kde.kio.StashNotifier")));
1556     connect(stashSplit, &QAction::triggered, this, &DolphinMainWindow::toggleSplitStash);
1557 
1558     KStandardAction::redisplay(this, &DolphinMainWindow::reloadView, actionCollection());
1559 
1560     QAction* stop = actionCollection()->addAction(QStringLiteral("stop"));
1561     stop->setText(i18nc("@action:inmenu View", "Stop"));
1562     stop->setToolTip(i18nc("@info", "Stop loading"));
1563     stop->setWhatsThis(i18nc("@info", "This stops the loading of the contents of the current folder."));
1564     stop->setIcon(QIcon::fromTheme(QStringLiteral("process-stop")));
1565     connect(stop, &QAction::triggered, this, &DolphinMainWindow::stopLoading);
1566 
1567     KToggleAction* editableLocation = actionCollection()->add<KToggleAction>(QStringLiteral("editable_location"));
1568     editableLocation->setText(i18nc("@action:inmenu Navigation Bar", "Editable Location"));
1569     editableLocation->setWhatsThis(xi18nc("@info:whatsthis",
1570         "This toggles the <emphasis>Location Bar</emphasis> to be "
1571         "editable so you can directly enter a location you want to go to.<nl/>"
1572         "You can also switch to editing by clicking to the right of the "
1573         "location and switch back by confirming the edited location."));
1574     actionCollection()->setDefaultShortcut(editableLocation, Qt::Key_F6);
1575     connect(editableLocation, &KToggleAction::triggered, this, &DolphinMainWindow::toggleEditLocation);
1576 
1577     QAction* replaceLocation = actionCollection()->addAction(QStringLiteral("replace_location"));
1578     replaceLocation->setText(i18nc("@action:inmenu Navigation Bar", "Replace Location"));
1579     // i18n: "enter" is used both in the meaning of "writing" and "going to" a new location here.
1580     // Both meanings are useful but not necessary to understand the use of "Replace Location".
1581     // So you might want to be more verbose in your language to convey the meaning but it's up to you.
1582     replaceLocation->setWhatsThis(xi18nc("@info:whatsthis",
1583         "This switches to editing the location and selects it "
1584         "so you can quickly enter a different location."));
1585     actionCollection()->setDefaultShortcut(replaceLocation, Qt::CTRL | Qt::Key_L);
1586     connect(replaceLocation, &QAction::triggered, this, &DolphinMainWindow::replaceLocation);
1587 
1588     // setup 'Go' menu
1589     {
1590         QScopedPointer<QAction> backAction(KStandardAction::back(nullptr, nullptr, nullptr));
1591         m_backAction = new KToolBarPopupAction(backAction->icon(), backAction->text(), actionCollection());
1592         m_backAction->setObjectName(backAction->objectName());
1593         m_backAction->setShortcuts(backAction->shortcuts());
1594     }
1595     m_backAction->setPopupMode(QToolButton::DelayedPopup);
1596     connect(m_backAction, &QAction::triggered, this, &DolphinMainWindow::goBack);
1597     connect(m_backAction->menu(), &QMenu::aboutToShow, this, &DolphinMainWindow::slotAboutToShowBackPopupMenu);
1598     connect(m_backAction->menu(), &QMenu::triggered, this, &DolphinMainWindow::slotGoBack);
1599     actionCollection()->addAction(m_backAction->objectName(), m_backAction);
1600 
1601     auto backShortcuts = m_backAction->shortcuts();
1602     backShortcuts.append(QKeySequence(Qt::Key_Backspace));
1603     actionCollection()->setDefaultShortcuts(m_backAction, backShortcuts);
1604 
1605     DolphinRecentTabsMenu* recentTabsMenu = new DolphinRecentTabsMenu(this);
1606     actionCollection()->addAction(QStringLiteral("closed_tabs"), recentTabsMenu);
1607     connect(m_tabWidget, &DolphinTabWidget::rememberClosedTab,
1608             recentTabsMenu, &DolphinRecentTabsMenu::rememberClosedTab);
1609     connect(recentTabsMenu, &DolphinRecentTabsMenu::restoreClosedTab,
1610             m_tabWidget, &DolphinTabWidget::restoreClosedTab);
1611     connect(recentTabsMenu, &DolphinRecentTabsMenu::closedTabsCountChanged,
1612             this, &DolphinMainWindow::closedTabsCountChanged);
1613 
1614     QAction* undoCloseTab = actionCollection()->addAction(QStringLiteral("undo_close_tab"));
1615     undoCloseTab->setText(i18nc("@action:inmenu File", "Undo close tab"));
1616     undoCloseTab->setWhatsThis(i18nc("@info:whatsthis undo close tab",
1617         "This returns you to the previously closed tab."));
1618     actionCollection()->setDefaultShortcut(undoCloseTab, Qt::CTRL | Qt::SHIFT | Qt::Key_T);
1619     undoCloseTab->setIcon(QIcon::fromTheme(QStringLiteral("edit-undo")));
1620     undoCloseTab->setEnabled(false);
1621     connect(undoCloseTab, &QAction::triggered, recentTabsMenu, &DolphinRecentTabsMenu::undoCloseTab);
1622 
1623     auto undoAction = actionCollection()->action(KStandardAction::name(KStandardAction::Undo));
1624     undoAction->setWhatsThis(xi18nc("@info:whatsthis", "This undoes "
1625         "the last change you made to files or folders.<nl/>"
1626         "Such changes include <interface>creating, renaming</interface> "
1627         "and <interface>moving</interface> them to a different location "
1628         "or to the <filename>Trash</filename>. <nl/>Changes that can't "
1629         "be undone will ask for your confirmation."));
1630     undoAction->setEnabled(false); // undo should be disabled by default
1631 
1632     {
1633         QScopedPointer<QAction> forwardAction(KStandardAction::forward(nullptr, nullptr, nullptr));
1634         m_forwardAction = new KToolBarPopupAction(forwardAction->icon(), forwardAction->text(), actionCollection());
1635         m_forwardAction->setObjectName(forwardAction->objectName());
1636         m_forwardAction->setShortcuts(forwardAction->shortcuts());
1637     }
1638     m_forwardAction->setPopupMode(QToolButton::DelayedPopup);
1639     connect(m_forwardAction, &QAction::triggered, this, &DolphinMainWindow::goForward);
1640     connect(m_forwardAction->menu(), &QMenu::aboutToShow, this, &DolphinMainWindow::slotAboutToShowForwardPopupMenu);
1641     connect(m_forwardAction->menu(), &QMenu::triggered, this, &DolphinMainWindow::slotGoForward);
1642     actionCollection()->addAction(m_forwardAction->objectName(), m_forwardAction);
1643     actionCollection()->setDefaultShortcuts(m_forwardAction, m_forwardAction->shortcuts());
1644 
1645     // enable middle-click to open in a new tab
1646     auto *middleClickEventFilter = new MiddleClickActionEventFilter(this);
1647     connect(middleClickEventFilter, &MiddleClickActionEventFilter::actionMiddleClicked, this, &DolphinMainWindow::slotBackForwardActionMiddleClicked);
1648     m_backAction->menu()->installEventFilter(middleClickEventFilter);
1649     m_forwardAction->menu()->installEventFilter(middleClickEventFilter);
1650     KStandardAction::up(this, &DolphinMainWindow::goUp, actionCollection());
1651     QAction* homeAction = KStandardAction::home(this, &DolphinMainWindow::goHome, actionCollection());
1652     homeAction->setWhatsThis(xi18nc("@info:whatsthis", "Go to your "
1653         "<filename>Home</filename> folder.<nl/>Every user account "
1654         "has their own <filename>Home</filename> that contains their data "
1655         "including folders that contain personal application data."));
1656 
1657     // setup 'Tools' menu
1658     QAction* compareFiles = actionCollection()->addAction(QStringLiteral("compare_files"));
1659     compareFiles->setText(i18nc("@action:inmenu Tools", "Compare Files"));
1660     compareFiles->setIcon(QIcon::fromTheme(QStringLiteral("kompare")));
1661     compareFiles->setEnabled(false);
1662     connect(compareFiles, &QAction::triggered, this, &DolphinMainWindow::compareFiles);
1663 
1664     QAction* openPreferredSearchTool = actionCollection()->addAction(QStringLiteral("open_preferred_search_tool"));
1665     openPreferredSearchTool->setText(i18nc("@action:inmenu Tools", "Open Preferred Search Tool"));
1666     openPreferredSearchTool->setWhatsThis(xi18nc("@info:whatsthis",
1667         "<para>This opens a preferred search tool for the viewed location.</para>"
1668         "<para>Use <emphasis>More Search Tools</emphasis> menu to configure it.</para>"));
1669     openPreferredSearchTool->setIcon(QIcon::fromTheme(QStringLiteral("search")));
1670     actionCollection()->setDefaultShortcut(openPreferredSearchTool, Qt::CTRL | Qt::SHIFT | Qt::Key_F);
1671     connect(openPreferredSearchTool, &QAction::triggered, this, &DolphinMainWindow::openPreferredSearchTool);
1672 
1673     if (KAuthorized::authorize(QStringLiteral("shell_access"))) {
1674         QAction* openTerminal = actionCollection()->addAction(QStringLiteral("open_terminal"));
1675         openTerminal->setText(i18nc("@action:inmenu Tools", "Open Terminal"));
1676         openTerminal->setWhatsThis(xi18nc("@info:whatsthis",
1677             "<para>This opens a <emphasis>terminal</emphasis> application for the viewed location.</para>"
1678             "<para>To learn more about terminals use the help in the terminal application.</para>"));
1679         openTerminal->setIcon(QIcon::fromTheme(QStringLiteral("utilities-terminal")));
1680         actionCollection()->setDefaultShortcut(openTerminal, Qt::SHIFT | Qt::Key_F4);
1681         connect(openTerminal, &QAction::triggered, this, &DolphinMainWindow::openTerminal);
1682 
1683 #ifdef HAVE_TERMINAL
1684         QAction* focusTerminalPanel = actionCollection()->addAction(QStringLiteral("focus_terminal_panel"));
1685         focusTerminalPanel->setText(i18nc("@action:inmenu Tools", "Focus Terminal Panel"));
1686         focusTerminalPanel->setIcon(QIcon::fromTheme(QStringLiteral("swap-panels")));
1687         actionCollection()->setDefaultShortcut(focusTerminalPanel, Qt::CTRL | Qt::SHIFT | Qt::Key_F4);
1688         connect(focusTerminalPanel, &QAction::triggered, this, &DolphinMainWindow::focusTerminalPanel);
1689 #endif
1690     }
1691 
1692     // setup 'Bookmarks' menu
1693     KActionMenu *bookmarkMenu = new KActionMenu(i18nc("@title:menu", "&Bookmarks"), this);
1694     bookmarkMenu->setIcon(QIcon::fromTheme(QStringLiteral("bookmarks")));
1695     // Make the toolbar button version work properly on click
1696     bookmarkMenu->setPopupMode(QToolButton::InstantPopup);
1697     m_bookmarkHandler = new DolphinBookmarkHandler(this, actionCollection(), bookmarkMenu->menu(), this);
1698     actionCollection()->addAction(QStringLiteral("bookmarks"), bookmarkMenu);
1699 
1700     // setup 'Settings' menu
1701     KToggleAction* showMenuBar = KStandardAction::showMenubar(nullptr, nullptr, actionCollection());
1702     showMenuBar->setWhatsThis(xi18nc("@info:whatsthis",
1703             "This switches between having a <emphasis>Menubar</emphasis> "
1704             "and having a <interface>Control</interface> button. Both "
1705             "contain mostly the same commands and configuration options."));
1706     connect(showMenuBar, &KToggleAction::triggered,                   // Fixes #286822
1707             this, &DolphinMainWindow::toggleShowMenuBar, Qt::QueuedConnection);
1708     KStandardAction::preferences(this, &DolphinMainWindow::editSettings, actionCollection());
1709 
1710     // setup 'Help' menu for the m_controlButton. The other one is set up in the base class.
1711     m_helpMenu = new KHelpMenu(nullptr);
1712     m_helpMenu->menu()->installEventFilter(this);
1713     // remove duplicate shortcuts
1714     auto removeHelpActionShortcut = [this](KHelpMenu::MenuId menuId) {
1715         if (auto *action = m_helpMenu->action(menuId)) {
1716             action->setShortcut(QKeySequence());
1717         }
1718     };
1719     removeHelpActionShortcut(KHelpMenu::menuHelpContents);
1720     removeHelpActionShortcut(KHelpMenu::menuWhatsThis);
1721 
1722     // not in menu actions
1723     QList<QKeySequence> nextTabKeys = KStandardShortcut::tabNext();
1724     nextTabKeys.append(QKeySequence(Qt::CTRL | Qt::Key_Tab));
1725 
1726     QList<QKeySequence> prevTabKeys = KStandardShortcut::tabPrev();
1727     prevTabKeys.append(QKeySequence(Qt::CTRL | Qt::SHIFT | Qt::Key_Tab));
1728 
1729     for (int i = 0; i < MaxActivateTabShortcuts; ++i) {
1730         QAction* activateTab = actionCollection()->addAction(QStringLiteral("activate_tab_%1").arg(i));
1731         activateTab->setText(i18nc("@action:inmenu", "Activate Tab %1", i + 1));
1732         activateTab->setEnabled(false);
1733         connect(activateTab, &QAction::triggered, this, [this, i]() { m_tabWidget->activateTab(i); });
1734 
1735         // only add default shortcuts for the first 9 tabs regardless of MaxActivateTabShortcuts
1736         if (i < 9) {
1737             actionCollection()->setDefaultShortcut(activateTab, QStringLiteral("Alt+%1").arg(i + 1));
1738         }
1739     }
1740 
1741     QAction* activateLastTab = actionCollection()->addAction(QStringLiteral("activate_last_tab"));
1742     activateLastTab->setText(i18nc("@action:inmenu", "Activate Last Tab"));
1743     activateLastTab->setEnabled(false);
1744     connect(activateLastTab, &QAction::triggered, m_tabWidget, &DolphinTabWidget::activateLastTab);
1745     actionCollection()->setDefaultShortcut(activateLastTab, Qt::ALT | Qt::Key_0);
1746 
1747     QAction* activateNextTab = actionCollection()->addAction(QStringLiteral("activate_next_tab"));
1748     activateNextTab->setIconText(i18nc("@action:inmenu", "Next Tab"));
1749     activateNextTab->setText(i18nc("@action:inmenu", "Activate Next Tab"));
1750     activateNextTab->setEnabled(false);
1751     connect(activateNextTab, &QAction::triggered, m_tabWidget, &DolphinTabWidget::activateNextTab);
1752     actionCollection()->setDefaultShortcuts(activateNextTab, nextTabKeys);
1753 
1754     QAction* activatePrevTab = actionCollection()->addAction(QStringLiteral("activate_prev_tab"));
1755     activatePrevTab->setIconText(i18nc("@action:inmenu", "Previous Tab"));
1756     activatePrevTab->setText(i18nc("@action:inmenu", "Activate Previous Tab"));
1757     activatePrevTab->setEnabled(false);
1758     connect(activatePrevTab, &QAction::triggered, m_tabWidget, &DolphinTabWidget::activatePrevTab);
1759     actionCollection()->setDefaultShortcuts(activatePrevTab, prevTabKeys);
1760 
1761     // for context menu
1762     QAction* showTarget = actionCollection()->addAction(QStringLiteral("show_target"));
1763     showTarget->setText(i18nc("@action:inmenu", "Show Target"));
1764     showTarget->setIcon(QIcon::fromTheme(QStringLiteral("document-open-folder")));
1765     showTarget->setEnabled(false);
1766     connect(showTarget, &QAction::triggered, this, &DolphinMainWindow::showTarget);
1767 
1768     QAction* openInNewTab = actionCollection()->addAction(QStringLiteral("open_in_new_tab"));
1769     openInNewTab->setText(i18nc("@action:inmenu", "Open in New Tab"));
1770     openInNewTab->setIcon(QIcon::fromTheme(QStringLiteral("tab-new")));
1771     connect(openInNewTab, &QAction::triggered, this, &DolphinMainWindow::openInNewTab);
1772 
1773     QAction* openInNewTabs = actionCollection()->addAction(QStringLiteral("open_in_new_tabs"));
1774     openInNewTabs->setText(i18nc("@action:inmenu", "Open in New Tabs"));
1775     openInNewTabs->setIcon(QIcon::fromTheme(QStringLiteral("tab-new")));
1776     connect(openInNewTabs, &QAction::triggered, this, &DolphinMainWindow::openInNewTab);
1777 
1778     QAction* openInNewWindow = actionCollection()->addAction(QStringLiteral("open_in_new_window"));
1779     openInNewWindow->setText(i18nc("@action:inmenu", "Open in New Window"));
1780     openInNewWindow->setIcon(QIcon::fromTheme(QStringLiteral("window-new")));
1781     connect(openInNewWindow, &QAction::triggered, this, &DolphinMainWindow::openInNewWindow);
1782 }
1783 
setupDockWidgets()1784 void DolphinMainWindow::setupDockWidgets()
1785 {
1786     const bool lock = GeneralSettings::lockPanels();
1787 
1788     KDualAction* lockLayoutAction = actionCollection()->add<KDualAction>(QStringLiteral("lock_panels"));
1789     lockLayoutAction->setActiveText(i18nc("@action:inmenu Panels", "Unlock Panels"));
1790     lockLayoutAction->setActiveIcon(QIcon::fromTheme(QStringLiteral("object-unlocked")));
1791     lockLayoutAction->setInactiveText(i18nc("@action:inmenu Panels", "Lock Panels"));
1792     lockLayoutAction->setInactiveIcon(QIcon::fromTheme(QStringLiteral("object-locked")));
1793     lockLayoutAction->setWhatsThis(xi18nc("@info:whatsthis", "This "
1794         "switches between having panels <emphasis>locked</emphasis> or "
1795         "<emphasis>unlocked</emphasis>.<nl/>Unlocked panels can be "
1796         "dragged to the other side of the window and have a close "
1797         "button.<nl/>Locked panels are embedded more cleanly."));
1798     lockLayoutAction->setActive(lock);
1799     connect(lockLayoutAction, &KDualAction::triggered, this, &DolphinMainWindow::togglePanelLockState);
1800 
1801     // Setup "Information"
1802     DolphinDockWidget* infoDock = new DolphinDockWidget(i18nc("@title:window", "Information"));
1803     infoDock->setLocked(lock);
1804     infoDock->setObjectName(QStringLiteral("infoDock"));
1805     infoDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
1806 
1807 #ifdef HAVE_BALOO
1808     InformationPanel* infoPanel = new InformationPanel(infoDock);
1809     infoPanel->setCustomContextMenuActions({lockLayoutAction});
1810     connect(infoPanel, &InformationPanel::urlActivated, this, &DolphinMainWindow::handleUrl);
1811     infoDock->setWidget(infoPanel);
1812 
1813     QAction* infoAction = infoDock->toggleViewAction();
1814     createPanelAction(QIcon::fromTheme(QStringLiteral("dialog-information")), Qt::Key_F11, infoAction, QStringLiteral("show_information_panel"));
1815 
1816     addDockWidget(Qt::RightDockWidgetArea, infoDock);
1817     connect(this, &DolphinMainWindow::urlChanged,
1818             infoPanel, &InformationPanel::setUrl);
1819     connect(this, &DolphinMainWindow::selectionChanged,
1820             infoPanel, &InformationPanel::setSelection);
1821     connect(this, &DolphinMainWindow::requestItemInfo,
1822             infoPanel, &InformationPanel::requestDelayedItemInfo);
1823     connect(this, &DolphinMainWindow::fileItemsChanged,
1824             infoPanel, &InformationPanel::slotFilesItemChanged);
1825 #endif
1826 
1827     // i18n: This is the last paragraph for the "What's This"-texts of all four panels.
1828     const QString panelWhatsThis = xi18nc("@info:whatsthis", "<para>To show or "
1829         "hide panels like this go to <interface>Control|Panels</interface> "
1830         "or <interface>View|Panels</interface>.</para>");
1831 #ifdef HAVE_BALOO
1832     actionCollection()->action(QStringLiteral("show_information_panel"))
1833         ->setWhatsThis(xi18nc("@info:whatsthis", "<para> This toggles the "
1834         "<emphasis>information</emphasis> panel at the right side of the "
1835         "window.</para><para>The panel provides in-depth information "
1836         "about the items your mouse is hovering over or about the selected "
1837         "items. Otherwise it informs you about the currently viewed folder.<nl/>"
1838         "For single items a preview of their contents is provided.</para>"));
1839 #endif
1840     infoDock->setWhatsThis(xi18nc("@info:whatsthis", "<para>This panel "
1841         "provides in-depth information about the items your mouse is "
1842         "hovering over or about the selected items. Otherwise it informs "
1843         "you about the currently viewed folder.<nl/>For single items a "
1844         "preview of their contents is provided.</para><para>You can configure "
1845         "which and how details are given here by right-clicking.</para>") + panelWhatsThis);
1846 
1847     // Setup "Folders"
1848     DolphinDockWidget* foldersDock = new DolphinDockWidget(i18nc("@title:window", "Folders"));
1849     foldersDock->setLocked(lock);
1850     foldersDock->setObjectName(QStringLiteral("foldersDock"));
1851     foldersDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
1852     FoldersPanel* foldersPanel = new FoldersPanel(foldersDock);
1853     foldersPanel->setCustomContextMenuActions({lockLayoutAction});
1854     foldersDock->setWidget(foldersPanel);
1855 
1856     QAction* foldersAction = foldersDock->toggleViewAction();
1857     createPanelAction(QIcon::fromTheme(QStringLiteral("folder")), Qt::Key_F7, foldersAction, QStringLiteral("show_folders_panel"));
1858 
1859     addDockWidget(Qt::LeftDockWidgetArea, foldersDock);
1860     connect(this, &DolphinMainWindow::urlChanged,
1861             foldersPanel, &FoldersPanel::setUrl);
1862     connect(foldersPanel, &FoldersPanel::folderActivated,
1863             this, &DolphinMainWindow::changeUrl);
1864     connect(foldersPanel, &FoldersPanel::folderMiddleClicked,
1865             this, &DolphinMainWindow::openNewTab);
1866     connect(foldersPanel, &FoldersPanel::errorMessage,
1867             this, &DolphinMainWindow::showErrorMessage);
1868 
1869     actionCollection()->action(QStringLiteral("show_folders_panel"))
1870         ->setWhatsThis(xi18nc("@info:whatsthis", "This toggles the "
1871         "<emphasis>folders</emphasis> panel at the left side of the window."
1872         "<nl/><nl/>It shows the folders of the <emphasis>file system"
1873         "</emphasis> in a <emphasis>tree view</emphasis>."));
1874     foldersDock->setWhatsThis(xi18nc("@info:whatsthis", "<para>This panel "
1875         "shows the folders of the <emphasis>file system</emphasis> in a "
1876         "<emphasis>tree view</emphasis>.</para><para>Click a folder to go "
1877         "there. Click the arrow to the left of a folder to see its subfolders. "
1878         "This allows quick switching between any folders.</para>") + panelWhatsThis);
1879 
1880     // Setup "Terminal"
1881 #ifdef HAVE_TERMINAL
1882     if (KAuthorized::authorize(QStringLiteral("shell_access"))) {
1883         DolphinDockWidget* terminalDock = new DolphinDockWidget(i18nc("@title:window Shell terminal", "Terminal"));
1884         terminalDock->setLocked(lock);
1885         terminalDock->setObjectName(QStringLiteral("terminalDock"));
1886         m_terminalPanel = new TerminalPanel(terminalDock);
1887         m_terminalPanel->setCustomContextMenuActions({lockLayoutAction});
1888         terminalDock->setWidget(m_terminalPanel);
1889 
1890         connect(m_terminalPanel, &TerminalPanel::hideTerminalPanel, terminalDock, &DolphinDockWidget::hide);
1891         connect(m_terminalPanel, &TerminalPanel::changeUrl, this, &DolphinMainWindow::slotTerminalDirectoryChanged);
1892         connect(terminalDock, &DolphinDockWidget::visibilityChanged,
1893                 m_terminalPanel, &TerminalPanel::dockVisibilityChanged);
1894         connect(terminalDock, &DolphinDockWidget::visibilityChanged,
1895                 this, &DolphinMainWindow::slotTerminalPanelVisibilityChanged);
1896 
1897         QAction* terminalAction = terminalDock->toggleViewAction();
1898         createPanelAction(QIcon::fromTheme(QStringLiteral("dialog-scripts")), Qt::Key_F4, terminalAction, QStringLiteral("show_terminal_panel"));
1899 
1900         addDockWidget(Qt::BottomDockWidgetArea, terminalDock);
1901         connect(this, &DolphinMainWindow::urlChanged,
1902                 m_terminalPanel, &TerminalPanel::setUrl);
1903 
1904         if (GeneralSettings::version() < 200) {
1905             terminalDock->hide();
1906         }
1907 
1908         actionCollection()->action(QStringLiteral("show_terminal_panel"))
1909             ->setWhatsThis(xi18nc("@info:whatsthis", "<para>This toggles the "
1910             "<emphasis>terminal</emphasis> panel at the bottom of the window."
1911             "<nl/>The location in the terminal will always match the folder "
1912             "view so you can navigate using either.</para><para>The terminal "
1913             "panel is not needed for basic computer usage but can be useful "
1914             "for advanced tasks. To learn more about terminals use the help "
1915             "in a standalone terminal application like Konsole.</para>"));
1916         terminalDock->setWhatsThis(xi18nc("@info:whatsthis", "<para>This is "
1917             "the <emphasis>terminal</emphasis> panel. It behaves like a "
1918             "normal terminal but will match the location of the folder view "
1919             "so you can navigate using either.</para><para>The terminal panel "
1920             "is not needed for basic computer usage but can be useful for "
1921             "advanced tasks. To learn more about terminals use the help in a "
1922             "standalone terminal application like Konsole.</para>") + panelWhatsThis);
1923     }
1924 #endif
1925 
1926     if (GeneralSettings::version() < 200) {
1927         infoDock->hide();
1928         foldersDock->hide();
1929     }
1930 
1931     // Setup "Places"
1932     DolphinDockWidget* placesDock = new DolphinDockWidget(i18nc("@title:window", "Places"));
1933     placesDock->setLocked(lock);
1934     placesDock->setObjectName(QStringLiteral("placesDock"));
1935     placesDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
1936 
1937     m_placesPanel = new PlacesPanel(placesDock);
1938     m_placesPanel->setCustomContextMenuActions({lockLayoutAction});
1939     placesDock->setWidget(m_placesPanel);
1940 
1941     QAction *placesAction = placesDock->toggleViewAction();
1942     createPanelAction(QIcon::fromTheme(QStringLiteral("compass")), Qt::Key_F9, placesAction, QStringLiteral("show_places_panel"));
1943 
1944     addDockWidget(Qt::LeftDockWidgetArea, placesDock);
1945     connect(m_placesPanel, &PlacesPanel::placeActivated,
1946             this, &DolphinMainWindow::slotPlaceActivated);
1947     connect(m_placesPanel, &PlacesPanel::placeMiddleClicked,
1948             this, &DolphinMainWindow::openNewTab);
1949     connect(m_placesPanel, &PlacesPanel::errorMessage,
1950             this, &DolphinMainWindow::showErrorMessage);
1951     connect(this, &DolphinMainWindow::urlChanged,
1952             m_placesPanel, &PlacesPanel::setUrl);
1953     connect(placesDock, &DolphinDockWidget::visibilityChanged,
1954             &DolphinUrlNavigatorsController::slotPlacesPanelVisibilityChanged);
1955     connect(this, &DolphinMainWindow::settingsChanged,
1956         m_placesPanel, &PlacesPanel::readSettings);
1957     connect(m_placesPanel, &PlacesPanel::storageTearDownRequested,
1958             this, &DolphinMainWindow::slotStorageTearDownFromPlacesRequested);
1959     connect(m_placesPanel, &PlacesPanel::storageTearDownExternallyRequested,
1960             this, &DolphinMainWindow::slotStorageTearDownExternallyRequested);
1961     DolphinUrlNavigatorsController::slotPlacesPanelVisibilityChanged(m_placesPanel->isVisible());
1962 
1963     auto actionShowAllPlaces = new QAction(QIcon::fromTheme(QStringLiteral("view-hidden")), i18nc("@item:inmenu", "Show Hidden Places"), this);
1964     actionShowAllPlaces->setCheckable(true);
1965     actionShowAllPlaces->setDisabled(true);
1966     actionShowAllPlaces->setWhatsThis(i18nc("@info:whatsthis", "This displays "
1967         "all places in the places panel that have been hidden. They will "
1968         "appear semi-transparent unless you uncheck their hide property."));
1969 
1970     connect(actionShowAllPlaces, &QAction::triggered, this, [actionShowAllPlaces, this](bool checked){
1971         actionShowAllPlaces->setIcon(QIcon::fromTheme(checked ? QStringLiteral("view-visible") : QStringLiteral("view-hidden")));
1972         m_placesPanel->showHiddenEntries(checked);
1973     });
1974 
1975     connect(m_placesPanel, &PlacesPanel::showHiddenEntriesChanged, this, [actionShowAllPlaces] (bool checked){
1976         actionShowAllPlaces->setChecked(checked);
1977         actionShowAllPlaces->setIcon(QIcon::fromTheme(checked ? QStringLiteral("view-visible") : QStringLiteral("view-hidden")));
1978    });
1979 
1980     actionCollection()->action(QStringLiteral("show_places_panel"))
1981         ->setWhatsThis(xi18nc("@info:whatsthis", "<para>This toggles the "
1982         "<emphasis>places</emphasis> panel at the left side of the window."
1983         "</para><para>It allows you to go to locations you have "
1984         "bookmarked and to access disk or media attached to the computer "
1985         "or to the network. It also contains sections to find recently "
1986         "saved files or files of a certain type.</para>"));
1987     placesDock->setWhatsThis(xi18nc("@info:whatsthis", "<para>This is the "
1988         "<emphasis>Places</emphasis> panel. It allows you to go to locations "
1989         "you have bookmarked and to access disk or media attached to the "
1990         "computer or to the network. It also contains sections to find "
1991         "recently saved files or files of a certain type.</para><para>"
1992         "Click on an entry to go there. Click with the right mouse button "
1993         "instead to open any entry in a new tab or new window.</para>"
1994         "<para>New entries can be added by dragging folders onto this panel. "
1995         "Right-click any section or entry to hide it. Right-click an empty "
1996         "space on this panel and select <interface>Show Hidden Places"
1997         "</interface> to display it again.</para>") + panelWhatsThis);
1998 
1999     // Add actions into the "Panels" menu
2000     KActionMenu* panelsMenu = new KActionMenu(i18nc("@action:inmenu View", "Show Panels"), this);
2001     actionCollection()->addAction(QStringLiteral("panels"), panelsMenu);
2002     panelsMenu->setIcon(QIcon::fromTheme(QStringLiteral("view-sidetree")));
2003     panelsMenu->setPopupMode(QToolButton::InstantPopup);
2004     const KActionCollection* ac = actionCollection();
2005     panelsMenu->addAction(ac->action(QStringLiteral("show_places_panel")));
2006 #ifdef HAVE_BALOO
2007     panelsMenu->addAction(ac->action(QStringLiteral("show_information_panel")));
2008 #endif
2009     panelsMenu->addAction(ac->action(QStringLiteral("show_folders_panel")));
2010     panelsMenu->addAction(ac->action(QStringLiteral("show_terminal_panel")));
2011     panelsMenu->addSeparator();
2012     panelsMenu->addAction(actionShowAllPlaces);
2013     panelsMenu->addAction(lockLayoutAction);
2014 
2015     connect(panelsMenu->menu(), &QMenu::aboutToShow, this, [actionShowAllPlaces, this]{
2016         actionShowAllPlaces->setEnabled(m_placesPanel->hiddenListCount());
2017     });
2018 }
2019 
2020 
updateFileAndEditActions()2021 void DolphinMainWindow::updateFileAndEditActions()
2022 {
2023     const KFileItemList list = m_activeViewContainer->view()->selectedItems();
2024     const KActionCollection* col = actionCollection();
2025     KFileItemListProperties capabilitiesSource(list);
2026 
2027     QAction* addToPlacesAction = col->action(QStringLiteral("add_to_places"));
2028     QAction* copyToOtherViewAction   = col->action(QStringLiteral("copy_to_inactive_split_view"));
2029     QAction* moveToOtherViewAction   = col->action(QStringLiteral("move_to_inactive_split_view"));
2030     QAction* copyLocation            = col->action(QString("copy_location"));
2031 
2032     if (list.isEmpty()) {
2033         stateChanged(QStringLiteral("has_no_selection"));
2034 
2035         addToPlacesAction->setEnabled(true);
2036         copyToOtherViewAction->setEnabled(false);
2037         moveToOtherViewAction->setEnabled(false);
2038         copyLocation->setEnabled(false);
2039     } else {
2040         stateChanged(QStringLiteral("has_selection"));
2041 
2042         QAction* renameAction            = col->action(KStandardAction::name(KStandardAction::RenameFile));
2043         QAction* moveToTrashAction       = col->action(KStandardAction::name(KStandardAction::MoveToTrash));
2044         QAction* deleteAction            = col->action(KStandardAction::name(KStandardAction::DeleteFile));
2045         QAction* cutAction               = col->action(KStandardAction::name(KStandardAction::Cut));
2046         QAction* deleteWithTrashShortcut = col->action(QStringLiteral("delete_shortcut")); // see DolphinViewActionHandler
2047         QAction* showTarget              = col->action(QStringLiteral("show_target"));
2048         QAction* duplicateAction         = col->action(QStringLiteral("duplicate")); // see DolphinViewActionHandler
2049 
2050         if (list.length() == 1 && list.first().isDir()) {
2051             addToPlacesAction->setEnabled(true);
2052         } else {
2053             addToPlacesAction->setEnabled(false);
2054         }
2055 
2056         if (m_tabWidget->currentTabPage()->splitViewEnabled()) {
2057             DolphinTabPage* tabPage = m_tabWidget->currentTabPage();
2058             KFileItem capabilitiesDestination;
2059 
2060             if (tabPage->primaryViewActive()) {
2061                 capabilitiesDestination = tabPage->secondaryViewContainer()->url();
2062             } else {
2063                 capabilitiesDestination = tabPage->primaryViewContainer()->url();
2064             }
2065 
2066             copyToOtherViewAction->setEnabled(capabilitiesDestination.isWritable());
2067             moveToOtherViewAction->setEnabled(capabilitiesSource.supportsMoving() && capabilitiesDestination.isWritable());
2068         } else {
2069             copyToOtherViewAction->setEnabled(false);
2070             moveToOtherViewAction->setEnabled(false);
2071         }
2072 
2073         const bool enableMoveToTrash = capabilitiesSource.isLocal() && capabilitiesSource.supportsMoving();
2074 
2075         renameAction->setEnabled(capabilitiesSource.supportsMoving());
2076         moveToTrashAction->setEnabled(enableMoveToTrash);
2077         deleteAction->setEnabled(capabilitiesSource.supportsDeleting());
2078         deleteWithTrashShortcut->setEnabled(capabilitiesSource.supportsDeleting() && !enableMoveToTrash);
2079         cutAction->setEnabled(capabilitiesSource.supportsMoving());
2080         copyLocation->setEnabled(list.length() == 1);
2081         showTarget->setEnabled(list.length() == 1 && list.at(0).isLink());
2082         duplicateAction->setEnabled(capabilitiesSource.supportsWriting());
2083     }
2084 }
2085 
updateViewActions()2086 void DolphinMainWindow::updateViewActions()
2087 {
2088     m_actionHandler->updateViewActions();
2089 
2090     QAction* toggleFilterBarAction = actionCollection()->action(QStringLiteral("toggle_filter"));
2091     toggleFilterBarAction->setChecked(m_activeViewContainer->isFilterBarVisible());
2092 
2093     updateSplitAction();
2094 }
2095 
updateGoActions()2096 void DolphinMainWindow::updateGoActions()
2097 {
2098     QAction* goUpAction = actionCollection()->action(KStandardAction::name(KStandardAction::Up));
2099     const QUrl currentUrl = m_activeViewContainer->url();
2100     // I think this is one of the best places to firstly be confronted
2101     // with a file system and its hierarchy. Talking about the root
2102     // directory might seem too much here but it is the question that
2103     // naturally arises in this context.
2104     goUpAction->setWhatsThis(xi18nc("@info:whatsthis", "<para>Go to "
2105         "the folder that contains the currently viewed one.</para>"
2106         "<para>All files and folders are organized in a hierarchical "
2107         "<emphasis>file system</emphasis>. At the top of this hierarchy is "
2108         "a directory that contains all data connected to this computer"
2109         "—the <emphasis>root directory</emphasis>.</para>"));
2110     goUpAction->setEnabled(KIO::upUrl(currentUrl) != currentUrl);
2111 }
2112 
refreshViews()2113 void DolphinMainWindow::refreshViews()
2114 {
2115     m_tabWidget->refreshViews();
2116 
2117     if (GeneralSettings::modifiedStartupSettings()) {
2118         updateWindowTitle();
2119     }
2120 
2121     Q_EMIT settingsChanged();
2122 }
2123 
clearStatusBar()2124 void DolphinMainWindow::clearStatusBar()
2125 {
2126     m_activeViewContainer->statusBar()->resetToDefaultText();
2127 }
2128 
connectViewSignals(DolphinViewContainer * container)2129 void DolphinMainWindow::connectViewSignals(DolphinViewContainer* container)
2130 {
2131     connect(container, &DolphinViewContainer::showFilterBarChanged,
2132             this, &DolphinMainWindow::updateFilterBarAction);
2133     connect(container, &DolphinViewContainer::writeStateChanged,
2134             this, &DolphinMainWindow::slotWriteStateChanged);
2135     connect(container, &DolphinViewContainer::searchModeEnabledChanged,
2136             this, &DolphinMainWindow::updateSearchAction);
2137     connect(container, &DolphinViewContainer::captionChanged,
2138             this, &DolphinMainWindow::updateWindowTitle);
2139 
2140     const QAction* toggleSearchAction = actionCollection()->action(QStringLiteral("toggle_search"));
2141     connect(toggleSearchAction, &QAction::triggered, container, &DolphinViewContainer::setSearchModeEnabled);
2142 
2143     const DolphinView* view = container->view();
2144     connect(view, &DolphinView::selectionChanged,
2145             this, &DolphinMainWindow::slotSelectionChanged);
2146     connect(view, &DolphinView::requestItemInfo,
2147             this, &DolphinMainWindow::requestItemInfo);
2148     connect(view, &DolphinView::fileItemsChanged,
2149             this, &DolphinMainWindow::fileItemsChanged);
2150     connect(view, &DolphinView::tabRequested,
2151             this, &DolphinMainWindow::openNewTab);
2152     connect(view, &DolphinView::requestContextMenu,
2153             this, &DolphinMainWindow::openContextMenu);
2154     connect(view, &DolphinView::directoryLoadingStarted,
2155             this, &DolphinMainWindow::enableStopAction);
2156     connect(view, &DolphinView::directoryLoadingCompleted,
2157             this, &DolphinMainWindow::disableStopAction);
2158     connect(view, &DolphinView::directoryLoadingCompleted,
2159             this, &DolphinMainWindow::slotDirectoryLoadingCompleted);
2160     connect(view, &DolphinView::goBackRequested,
2161             this, &DolphinMainWindow::goBack);
2162     connect(view, &DolphinView::goForwardRequested,
2163             this, &DolphinMainWindow::goForward);
2164     connect(view, &DolphinView::urlActivated,
2165             this, &DolphinMainWindow::handleUrl);
2166     connect(view, &DolphinView::goUpRequested,
2167             this, &DolphinMainWindow::goUp);
2168 
2169     connect(container->urlNavigatorInternalWithHistory(), &KUrlNavigator::urlChanged,
2170             this, &DolphinMainWindow::changeUrl);
2171     connect(container->urlNavigatorInternalWithHistory(), &KUrlNavigator::historyChanged,
2172             this, &DolphinMainWindow::updateHistory);
2173 
2174     auto navigators = static_cast<DolphinNavigatorsWidgetAction *>
2175         (actionCollection()->action(QStringLiteral("url_navigators")));
2176     const KUrlNavigator *navigator = m_tabWidget->currentTabPage()->primaryViewActive() ?
2177                                      navigators->primaryUrlNavigator() :
2178                                      navigators->secondaryUrlNavigator();
2179 
2180     QAction *editableLocactionAction = actionCollection()->action(QStringLiteral("editable_location"));
2181     editableLocactionAction->setChecked(navigator->isUrlEditable());
2182     connect(navigator, &KUrlNavigator::editableStateChanged,
2183             this, &DolphinMainWindow::slotEditableStateChanged);
2184     connect(navigator, &KUrlNavigator::tabRequested,
2185             this, &DolphinMainWindow::openNewTab);
2186 
2187 }
2188 
updateSplitAction()2189 void DolphinMainWindow::updateSplitAction()
2190 {
2191     QAction* splitAction = actionCollection()->action(QStringLiteral("split_view"));
2192     const DolphinTabPage* tabPage = m_tabWidget->currentTabPage();
2193     if (tabPage->splitViewEnabled()) {
2194         if (GeneralSettings::closeActiveSplitView() ? tabPage->primaryViewActive() : !tabPage->primaryViewActive()) {
2195             splitAction->setText(i18nc("@action:intoolbar Close left view", "Close"));
2196             splitAction->setToolTip(i18nc("@info", "Close left view"));
2197             splitAction->setIcon(QIcon::fromTheme(QStringLiteral("view-left-close")));
2198         } else {
2199             splitAction->setText(i18nc("@action:intoolbar Close right view", "Close"));
2200             splitAction->setToolTip(i18nc("@info", "Close right view"));
2201             splitAction->setIcon(QIcon::fromTheme(QStringLiteral("view-right-close")));
2202         }
2203     } else {
2204         splitAction->setText(i18nc("@action:intoolbar Split view", "Split"));
2205         splitAction->setToolTip(i18nc("@info", "Split view"));
2206         splitAction->setIcon(QIcon::fromTheme(QStringLiteral("view-right-new")));
2207     }
2208 }
2209 
updateAllowedToolbarAreas()2210 void DolphinMainWindow::updateAllowedToolbarAreas()
2211 {
2212     auto navigators = static_cast<DolphinNavigatorsWidgetAction *>
2213                         (actionCollection()->action(QStringLiteral("url_navigators")));
2214     if (toolBar()->actions().contains(navigators)) {
2215         toolBar()->setAllowedAreas(Qt::TopToolBarArea | Qt::BottomToolBarArea);
2216         if (toolBarArea(toolBar()) == Qt::LeftToolBarArea ||
2217             toolBarArea(toolBar()) == Qt::RightToolBarArea) {
2218             addToolBar(Qt::TopToolBarArea, toolBar());
2219         }
2220     } else {
2221         toolBar()->setAllowedAreas(Qt::AllToolBarAreas);
2222     }
2223 }
2224 
isKompareInstalled() const2225 bool DolphinMainWindow::isKompareInstalled() const
2226 {
2227     static bool initialized = false;
2228     static bool installed = false;
2229     if (!initialized) {
2230         // TODO: maybe replace this approach later by using a menu
2231         // plugin like kdiff3plugin.cpp
2232         installed = !QStandardPaths::findExecutable(QStringLiteral("kompare")).isEmpty();
2233         initialized = true;
2234     }
2235     return installed;
2236 }
2237 
createPanelAction(const QIcon & icon,const QKeySequence & shortcut,QAction * dockAction,const QString & actionName)2238 void DolphinMainWindow::createPanelAction(const QIcon& icon,
2239                                           const QKeySequence& shortcut,
2240                                           QAction* dockAction,
2241                                           const QString& actionName)
2242 {
2243     QAction* panelAction = actionCollection()->addAction(actionName);
2244     panelAction->setCheckable(true);
2245     panelAction->setChecked(dockAction->isChecked());
2246     panelAction->setText(dockAction->text());
2247     panelAction->setIcon(icon);
2248     actionCollection()->setDefaultShortcut(panelAction, shortcut);
2249 
2250     connect(panelAction, &QAction::triggered, dockAction, &QAction::trigger);
2251     connect(dockAction, &QAction::toggled, panelAction, &QAction::setChecked);
2252 }
2253 
setupWhatsThis()2254 void DolphinMainWindow::setupWhatsThis()
2255 {
2256     // main widgets
2257     menuBar()->setWhatsThis(xi18nc("@info:whatsthis", "<para>This is the "
2258         "<emphasis>Menubar</emphasis>. It provides access to commands and "
2259         "configuration options. Left-click on any of the menus on this "
2260         "bar to see its contents.</para><para>The Menubar can be hidden "
2261         "by unchecking <interface>Settings|Show Menubar</interface>. Then "
2262         "most of its contents become available through a <interface>Control"
2263         "</interface> button on the <emphasis>Toolbar</emphasis>.</para>"));
2264     toolBar()->setWhatsThis(xi18nc("@info:whatsthis", "<para>This is the "
2265         "<emphasis>Toolbar</emphasis>. It allows quick access to "
2266         "frequently used actions.</para><para>It is highly customizable. "
2267         "All items you see in the <interface>Control</interface> menu or "
2268         "in the <interface>Menubar</interface> can be placed on the "
2269         "Toolbar. Just right-click on it and select <interface>Configure "
2270         "Toolbars…</interface> or find this action in the <interface>"
2271         "Control</interface> or <interface>Settings</interface> menu."
2272         "</para><para>The location of the bar and the style of its "
2273         "buttons can also be changed in the right-click menu. Right-click "
2274         "a button if you want to show or hide its text.</para>"));
2275     m_tabWidget->setWhatsThis(xi18nc("@info:whatsthis main view",
2276         "<para>Here you can see the <emphasis>folders</emphasis> and "
2277         "<emphasis>files</emphasis> that are at the location described in "
2278         "the <interface>Location Bar</interface> above. This area is the "
2279         "central part of this application where you navigate to the files "
2280         "you want to use.</para><para>For an elaborate and general "
2281         "introduction to this application <link "
2282         "url='https://userbase.kde.org/Dolphin/File_Management#Introduction_to_Dolphin'>"
2283         "click here</link>. This will open an introductory article from "
2284         "the <emphasis>KDE UserBase Wiki</emphasis>.</para><para>For brief "
2285         "explanations of all the features of this <emphasis>view</emphasis> "
2286         "<link url='help:/dolphin/dolphin-view.html'>click here</link> "
2287         "instead. This will open a page from the <emphasis>Handbook"
2288         "</emphasis> that covers the basics.</para>"));
2289 
2290     // Settings menu
2291     actionCollection()->action(KStandardAction::name(KStandardAction::KeyBindings))
2292         ->setWhatsThis(xi18nc("@info:whatsthis","<para>This opens a window "
2293         "that lists the <emphasis>keyboard shortcuts</emphasis>.<nl/>"
2294         "There you can set up key combinations to trigger an action when "
2295         "they are pressed simultaneously. All commands in this application can "
2296         "be triggered this way.</para>"));
2297     actionCollection()->action(KStandardAction::name(KStandardAction::ConfigureToolbars))
2298         ->setWhatsThis(xi18nc("@info:whatsthis","<para>This opens a window in which "
2299         "you can change which buttons appear on the <emphasis>Toolbar</emphasis>.</para>"
2300         "<para>All items you see in the <interface>Control</interface> menu "
2301         "or in the <interface>Menubar</interface> can also be placed on the Toolbar.</para>"));
2302     actionCollection()->action(KStandardAction::name(KStandardAction::Preferences))
2303         ->setWhatsThis(xi18nc("@info:whatsthis","This opens a window where you can "
2304         "change a multitude of settings for this application. For an explanation "
2305         "of the various settings go to the chapter <emphasis>Configuring Dolphin"
2306         "</emphasis> in <interface>Help|Dolphin Handbook</interface>."));
2307 
2308     // Help menu
2309     // The whatsthis has to be set for the m_helpMenu and for the
2310     // StandardAction separately because both are used in different locations.
2311     // m_helpMenu is only used for createControlButton() button.
2312 
2313     auto setStandardActionWhatsThis = [this](KStandardAction::StandardAction actionId,
2314                                              const QString &whatsThis) {
2315         if (auto *action = actionCollection()->action(KStandardAction::name(actionId))) {
2316             action->setWhatsThis(whatsThis);
2317         }
2318     };
2319     auto setHelpActionWhatsThis = [this](KHelpMenu::MenuId menuId, const QString &whatsThis) {
2320         if (auto *action = m_helpMenu->action(menuId)) {
2321             action->setWhatsThis(whatsThis);
2322         }
2323     };
2324 
2325     // Links do not work within the Menubar so texts without links are provided there.
2326 
2327     // i18n: If the external link isn't available in your language you should
2328     // probably state the external link language at least in brackets to not
2329     // frustrate the user. If there are multiple languages that the user might
2330     // know with a reasonable chance you might want to have 2 external links.
2331     // The same is in my opinion true for every external link you translate.
2332     const QString whatsThisHelpContents = xi18nc("@info:whatsthis handbook",
2333         "<para>This opens the Handbook for this application. It provides "
2334         "explanations for every part of <emphasis>Dolphin</emphasis>.</para>");
2335     setStandardActionWhatsThis(KStandardAction::HelpContents, whatsThisHelpContents
2336         + xi18nc("@info:whatsthis second half of handbook hb text without link",
2337         "<para>If you want more elaborate introductions to the "
2338         "different features of <emphasis>Dolphin</emphasis> "
2339         "go to the KDE UserBase Wiki.</para>"));
2340     setHelpActionWhatsThis(KHelpMenu::menuHelpContents, whatsThisHelpContents
2341         + xi18nc("@info:whatsthis second half of handbook text with link",
2342         "<para>If you want more elaborate introductions to the "
2343         "different features of <emphasis>Dolphin</emphasis> "
2344         "<link url='https://userbase.kde.org/Dolphin/File_Management'>click here</link>. "
2345         "It will open the dedicated page in the KDE UserBase Wiki.</para>"));
2346 
2347     const QString whatsThisWhatsThis = xi18nc("@info:whatsthis whatsthis button",
2348         "<para>This is the button that invokes the help feature you are "
2349         "using right now! Click it, then click any component of this "
2350         "application to ask \"What's this?\" about it. The mouse cursor "
2351         "will change appearance if no help is available for a spot.</para>");
2352     setStandardActionWhatsThis(KStandardAction::WhatsThis, whatsThisWhatsThis
2353         + xi18nc("@info:whatsthis second half of whatsthis button text without link",
2354         "<para>There are two other ways to get help for this application: The "
2355         "<interface>Dolphin Handbook</interface> in the <interface>Help"
2356         "</interface> menu and the <emphasis>KDE UserBase Wiki</emphasis> "
2357         "article about <emphasis>File Management</emphasis> online."
2358         "</para><para>The \"What's this?\" help is "
2359         "missing in most other windows so don't get too used to this.</para>"));
2360     setHelpActionWhatsThis(KHelpMenu::menuWhatsThis, whatsThisWhatsThis
2361         + xi18nc("@info:whatsthis second half of whatsthis button text with link",
2362         "<para>There are two other ways to get help: "
2363         "The <link url='help:/dolphin/index.html'>Dolphin Handbook</link> and "
2364         "the <link url='https://userbase.kde.org/Dolphin/File_Management'>KDE "
2365         "UserBase Wiki</link>.</para><para>The \"What's this?\" help is "
2366         "missing in most other windows so don't get too used to this.</para>"));
2367 
2368     const QString whatsThisReportBug = xi18nc("@info:whatsthis","<para>This opens a "
2369         "window that will guide you through reporting errors or flaws "
2370         "in this application or in other KDE software.</para>");
2371     setStandardActionWhatsThis(KStandardAction::ReportBug, whatsThisReportBug);
2372     setHelpActionWhatsThis(KHelpMenu::menuReportBug, whatsThisReportBug
2373         + xi18nc("@info:whatsthis second half of reportbug text with link",
2374         "<para>High-quality bug reports are much appreciated. To learn "
2375         "how to make your bug report as effective as possible "
2376         "<link url='https://community.kde.org/Get_Involved/Bug_Reporting'>"
2377         "click here</link>.</para>"));
2378 
2379     const QString whatsThisDonate = xi18nc("@info:whatsthis","<para>This opens a "
2380         "<emphasis>web page</emphasis> where you can donate to "
2381         "support the continued work on this application and many "
2382         "other projects by the <emphasis>KDE</emphasis> community.</para>"
2383         "<para>Donating is the easiest and fastest way to efficiently "
2384         "support KDE and its projects. KDE projects are available for "
2385         "free therefore your donation is needed to cover things that "
2386         "require money like servers, contributor meetings, etc.</para>"
2387         "<para><emphasis>KDE e.V.</emphasis> is the non-profit "
2388         "organization behind the KDE community.</para>");
2389     setStandardActionWhatsThis(KStandardAction::Donate, whatsThisDonate);
2390     setHelpActionWhatsThis(KHelpMenu::menuDonate, whatsThisDonate);
2391 
2392     const QString whatsThisSwitchLanguage = xi18nc("@info:whatsthis",
2393         "With this you can change the language this application uses."
2394         "<nl/>You can even set secondary languages which will be used "
2395         "if texts are not available in your preferred language.");
2396     setStandardActionWhatsThis(KStandardAction::SwitchApplicationLanguage,
2397                                whatsThisSwitchLanguage);
2398     setHelpActionWhatsThis(KHelpMenu::menuSwitchLanguage, whatsThisSwitchLanguage);
2399 
2400     const QString whatsThisAboutApp = xi18nc("@info:whatsthis","This opens a "
2401         "window that informs you about the version, license, "
2402         "used libraries and maintainers of this application.");
2403     setStandardActionWhatsThis(KStandardAction::AboutApp, whatsThisAboutApp);
2404     setHelpActionWhatsThis(KHelpMenu::menuAboutApp, whatsThisAboutApp);
2405 
2406     const QString whatsThisAboutKDE = xi18nc("@info:whatsthis","This opens a "
2407         "window with information about <emphasis>KDE</emphasis>. "
2408         "The KDE community are the people behind this free software."
2409         "<nl/>If you like using this application but don't know "
2410         "about KDE or want to see a cute dragon have a look!");
2411     setStandardActionWhatsThis(KStandardAction::AboutKDE, whatsThisAboutKDE);
2412     setHelpActionWhatsThis(KHelpMenu::menuAboutKDE, whatsThisAboutKDE);
2413 }
2414 
addHamburgerMenuToToolbar()2415 bool DolphinMainWindow::addHamburgerMenuToToolbar()
2416 {
2417     QDomDocument domDocument = KXMLGUIClient::domDocument();
2418     if (domDocument.isNull()) {
2419         return false;
2420     }
2421     QDomNode toolbar = domDocument.elementsByTagName(QStringLiteral("ToolBar")).at(0);
2422     if (toolbar.isNull()) {
2423         return false;
2424     }
2425 
2426     QDomElement hamburgerMenuElement = domDocument.createElement(QStringLiteral("Action"));
2427     hamburgerMenuElement.setAttribute(QStringLiteral("name"), QStringLiteral("hamburger_menu"));
2428     toolbar.appendChild(hamburgerMenuElement);
2429 
2430     KXMLGUIFactory::saveConfigFile(domDocument, xmlFile());
2431     reloadXML();
2432     createGUI();
2433     return true;
2434     // Make sure to also remove the <KXMLGUIFactory> and <QDomDocument> include
2435     // whenever this method is removed (maybe in the year ~2026).
2436 }
2437 
event(QEvent * event)2438 bool DolphinMainWindow::event(QEvent *event)
2439 {
2440     if (event->type() == QEvent::WhatsThisClicked) {
2441         event->accept();
2442         QWhatsThisClickedEvent* whatsThisEvent = dynamic_cast<QWhatsThisClickedEvent*>(event);
2443         QDesktopServices::openUrl(QUrl(whatsThisEvent->href()));
2444         return true;
2445     }
2446     return KXmlGuiWindow::event(event);
2447 }
2448 
eventFilter(QObject * obj,QEvent * event)2449 bool DolphinMainWindow::eventFilter(QObject* obj, QEvent* event)
2450 {
2451     Q_UNUSED(obj)
2452     if (event->type() == QEvent::WhatsThisClicked) {
2453         event->accept();
2454         QWhatsThisClickedEvent* whatsThisEvent = dynamic_cast<QWhatsThisClickedEvent*>(event);
2455         QDesktopServices::openUrl(QUrl(whatsThisEvent->href()));
2456         return true;
2457     }
2458     return false;
2459 }
2460 
2461 // Set a sane initial window size
sizeHint() const2462 QSize DolphinMainWindow::sizeHint() const
2463 {
2464     return KXmlGuiWindow::sizeHint().expandedTo(QSize(760, 550));
2465 }
2466 
saveNewToolbarConfig()2467 void DolphinMainWindow::saveNewToolbarConfig()
2468 {
2469     KXmlGuiWindow::saveNewToolbarConfig(); // Applies the new config. This has to be called first
2470                                            // because the rest of this method decides things
2471                                            // based on the new config.
2472     auto navigators = static_cast<DolphinNavigatorsWidgetAction *>
2473                         (actionCollection()->action(QStringLiteral("url_navigators")));
2474     if (!toolBar()->actions().contains(navigators)) {
2475         m_tabWidget->currentTabPage()->insertNavigatorsWidget(navigators);
2476     }
2477     updateAllowedToolbarAreas();
2478     (static_cast<KHamburgerMenu *>(actionCollection()->action(KStandardAction::name(
2479                             KStandardAction::HamburgerMenu))))->hideActionsOf(toolBar());
2480 }
2481 
focusTerminalPanel()2482 void DolphinMainWindow::focusTerminalPanel()
2483 {
2484     if (m_terminalPanel->isVisible()) {
2485         if (m_terminalPanel->terminalHasFocus()) {
2486             m_activeViewContainer->view()->setFocus(Qt::FocusReason::ShortcutFocusReason);
2487             actionCollection()->action(QStringLiteral("focus_terminal_panel"))->setText(i18nc("@action:inmenu Tools", "Focus Terminal Panel"));
2488         } else {
2489             m_terminalPanel->setFocus(Qt::FocusReason::ShortcutFocusReason);
2490             actionCollection()->action(QStringLiteral("focus_terminal_panel"))->setText(i18nc("@action:inmenu Tools", "Defocus Terminal Panel"));
2491         }
2492     } else {
2493         actionCollection()->action(QStringLiteral("show_terminal_panel"))->trigger();
2494         actionCollection()->action(QStringLiteral("focus_terminal_panel"))->setText(i18nc("@action:inmenu Tools", "Defocus Terminal Panel"));
2495     }
2496 }
2497 
UndoUiInterface()2498 DolphinMainWindow::UndoUiInterface::UndoUiInterface() :
2499     KIO::FileUndoManager::UiInterface()
2500 {
2501 }
2502 
~UndoUiInterface()2503 DolphinMainWindow::UndoUiInterface::~UndoUiInterface()
2504 {
2505 }
2506 
jobError(KIO::Job * job)2507 void DolphinMainWindow::UndoUiInterface::jobError(KIO::Job* job)
2508 {
2509     DolphinMainWindow* mainWin= qobject_cast<DolphinMainWindow *>(parentWidget());
2510     if (mainWin) {
2511         DolphinViewContainer* container = mainWin->activeViewContainer();
2512         container->showMessage(job->errorString(), DolphinViewContainer::Error);
2513     } else {
2514         KIO::FileUndoManager::UiInterface::jobError(job);
2515     }
2516 }
2517 
isUrlOpen(const QString & url)2518 bool DolphinMainWindow::isUrlOpen(const QString& url)
2519 {
2520     return m_tabWidget->isUrlOpen(QUrl::fromUserInput((url)));
2521 }
2522 
2523