]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinmainwindow.h
Implement "Focus Places Panel"
[dolphin.git] / src / dolphinmainwindow.h
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 #ifndef DOLPHIN_MAINWINDOW_H
10 #define DOLPHIN_MAINWINDOW_H
11
12 #include "config-dolphin.h"
13 #include "disabledactionnotifier.h"
14 #include "dolphintabwidget.h"
15 #include "selectionmode/bottombar.h"
16 #include <KActionMenu>
17 #include <KFileItemActions>
18 #include <kio/fileundomanager.h>
19 #include <kxmlguiwindow.h>
20
21 #if HAVE_BALOO
22 #include "panels/information/informationpanel.h"
23 #endif
24
25 #include <QFutureWatcher>
26 #include <QIcon>
27 #include <QList>
28 #include <QMenu>
29 #include <QPointer>
30 #include <QUrl>
31 #include <QVector>
32
33 typedef KIO::FileUndoManager::CommandType CommandType;
34
35 class DolphinBookmarkHandler;
36 class DolphinViewActionHandler;
37 class DolphinSettingsDialog;
38 class DolphinViewContainer;
39 class DolphinRemoteEncoding;
40 class DolphinTabWidget;
41 class KFileItem;
42 class KFileItemList;
43 class KJob;
44 class KNewFileMenu;
45 class KRecentFilesAction;
46 class KToolBarPopupAction;
47 class QToolButton;
48 class PlacesPanel;
49 class TerminalPanel;
50
51 /** Used to identify that a custom command should be triggered on a view background double-click.*/
52 constexpr QLatin1String customCommand{"CUSTOM_COMMAND"};
53
54 namespace KIO
55 {
56 class OpenUrlJob;
57 class CommandLauncherJob;
58 }
59 namespace SelectionMode
60 {
61 class ActionTextHelper;
62 }
63
64 /**
65 * @short Main window for Dolphin.
66 *
67 * Handles the menus, toolbars and Dolphin views.
68 */
69 class DolphinMainWindow : public KXmlGuiWindow
70 {
71 Q_OBJECT
72 Q_CLASSINFO("D-Bus Interface", "org.kde.dolphin.MainWindow")
73
74 public:
75 DolphinMainWindow();
76 ~DolphinMainWindow() override;
77
78 /**
79 * Returns the currently active view.
80 * All menu actions are applied to this view. When
81 * having a split view setup, the nonactive view
82 * is usually shown in darker colors.
83 */
84 DolphinViewContainer *activeViewContainer() const;
85
86 /**
87 * Returns view container for all tabs
88 */
89 QVector<DolphinViewContainer *> viewContainers() const;
90
91 /**
92 * Opens each directory in \p dirs in a separate tab. If \a splitView is set,
93 * 2 directories are collected within one tab.
94 * \pre \a dirs must contain at least one url.
95 */
96 void openDirectories(const QList<QUrl> &dirs, bool splitView);
97
98 /**
99 * Opens the directories which contain the files \p files and selects all files.
100 * If \a splitView is set, 2 directories are collected within one tab.
101 * \pre \a files must contain at least one url.
102 */
103 void openFiles(const QList<QUrl> &files, bool splitView);
104
105 /**
106 * Returns the 'Create New...' sub menu which also can be shared
107 * with other menus (e. g. a context menu).
108 */
109 KNewFileMenu *newFileMenu() const;
110
111 /**
112 * Augments Qt's build-in QMainWindow context menu to add
113 * Dolphin-specific actions, such as "unlock panels".
114 */
115 QMenu *createPopupMenu() override;
116
117 /**
118 * Switch the window's view containers' locations to display the home path
119 * for any which are currently displaying a location corresponding to or
120 * within mountPath.
121 *
122 * This typically done after unmounting a disk at mountPath to ensure that
123 * the window is not displaying an invalid location.
124 */
125 void setViewsToHomeIfMountPathOpen(const QString &mountPath);
126
127 /**
128 * Enables or disables the session autosaving feature.
129 *
130 * @param enable If true, saves the session automatically after a fixed
131 * time interval from the last state change.
132 */
133 void setSessionAutoSaveEnabled(bool enable);
134
135 bool isFoldersPanelEnabled() const;
136 bool isInformationPanelEnabled() const;
137 bool isSplitViewEnabledInCurrentTab() const;
138
139 /**
140 * Activates a user set action when double clicking the view's background.
141 */
142 void slotDoubleClickViewBackground(Qt::MouseButton button);
143
144 public Q_SLOTS:
145 /**
146 * Opens each directory in \p dirs in a separate tab. If \a splitView is set,
147 * 2 directories are collected within one tab.
148 * \pre \a dirs must contain at least one url.
149 *
150 * @note this function is overloaded so that it is callable via DBus.
151 */
152 void openDirectories(const QStringList &dirs, bool splitView);
153
154 /**
155 * Opens the directories which contain the files \p files and selects all files.
156 * If \a splitView is set, 2 directories are collected within one tab.
157 * \pre \a files must contain at least one url.
158 *
159 * @note this is overloaded so that this function is callable via DBus.
160 */
161 void openFiles(const QStringList &files, bool splitView);
162
163 /**
164 * Tries to raise/activate the Dolphin window.
165 */
166 void activateWindow(const QString &activationToken);
167
168 bool isActiveWindow();
169
170 /**
171 * Determines if a URL is open in any tab.
172 * @note Use of QString instead of QUrl is required to be callable via DBus.
173 *
174 * @param url URL to look for
175 * @returns true if url is currently open in a tab, false otherwise.
176 */
177 bool isUrlOpen(const QString &url);
178
179 /**
180 * @return Whether the item with @p url can be found in any view only by switching
181 * between already open tabs and scrolling in their primary or secondary view.
182 * @note Use of QString instead of QUrl is required to be callable via DBus.
183 */
184 bool isItemVisibleInAnyView(const QString &urlOfItem);
185
186 /**
187 * Pastes the clipboard data into the currently selected folder
188 * of the active view. If not exactly one folder is selected,
189 * no pasting is done at all.
190 */
191 void pasteIntoFolder();
192
193 /**
194 * Implementation of the MainWindowAdaptor/QDBusAbstractAdaptor interface.
195 * Inform all affected dolphin components (panels, views) of an URL
196 * change.
197 */
198 void changeUrl(const QUrl &url);
199
200 /**
201 * The current directory of the Terminal Panel has changed, probably because
202 * the user entered a 'cd' command. This slot calls changeUrl(url) and makes
203 * sure that the panel keeps the keyboard focus.
204 */
205 void slotTerminalDirectoryChanged(const QUrl &url);
206
207 /** Stores all settings and quits Dolphin. */
208 void quit();
209
210 /**
211 * Opens a new tab in the background showing the URL \a url.
212 */
213 void openNewTab(const QUrl &url);
214
215 /**
216 * Opens a new tab showing the URL \a url and activate it.
217 */
218 void openNewTabAndActivate(const QUrl &url);
219
220 /**
221 * Opens a new window showing the URL \a url.
222 */
223 void openNewWindow(const QUrl &url);
224
225 /** @see GeneralSettings::splitViewChanged() */
226 void slotSplitViewChanged();
227
228 Q_SIGNALS:
229 /**
230 * Is sent if the selection of the currently active view has
231 * been changed.
232 */
233 void selectionChanged(const KFileItemList &selection);
234
235 /**
236 * Is sent if the url of the currently active view has
237 * been changed.
238 */
239 void urlChanged(const QUrl &url);
240
241 /**
242 * Is emitted if information of an item is requested to be shown e. g. in the panel.
243 * If item is null, no item information request is pending.
244 */
245 void requestItemInfo(const KFileItem &item);
246
247 /**
248 * It is emitted when in the current view, files are changed,
249 * or dirs have files/removed from them.
250 */
251 void fileItemsChanged(const KFileItemList &changedFileItems);
252
253 /**
254 * Is emitted if the settings have been changed.
255 */
256 void settingsChanged();
257
258 protected:
259 /** @see QObject::event() */
260 bool event(QEvent *event) override;
261
262 /** @see QWidget::showEvent() */
263 void showEvent(QShowEvent *event) override;
264
265 /** @see QMainWindow::closeEvent() */
266 void closeEvent(QCloseEvent *event) override;
267
268 /** @see KMainWindow::saveProperties() */
269 void saveProperties(KConfigGroup &group) override;
270
271 /** @see KMainWindow::readProperties() */
272 void readProperties(const KConfigGroup &group) override;
273
274 /** Sets a sane initial window size **/
275 QSize sizeHint() const override;
276
277 protected Q_SLOTS:
278 /**
279 * Calls the base method KXmlGuiWindow::saveNewToolbarConfig().
280 * Is also used to set toolbar constraints and UrlNavigator position
281 * based on the newly changed toolbar configuration.
282 */
283 void saveNewToolbarConfig() override;
284
285 private Q_SLOTS:
286 /**
287 * Refreshes the views of the main window by recreating them according to
288 * the given Dolphin settings.
289 */
290 void refreshViews();
291
292 void clearStatusBar();
293
294 /** Updates the 'Create New...' sub menu. */
295 void updateNewMenu();
296
297 void createDirectory();
298
299 /** Shows the error message in the status bar of the active view. */
300 void showErrorMessage(const QString &message);
301
302 /**
303 * Updates the state of the 'Undo' menu action dependent
304 * on the parameter \a available.
305 */
306 void slotUndoAvailable(bool available);
307
308 /** Sets the text of the 'Undo' menu action to \a text. */
309 void slotUndoTextChanged(const QString &text);
310
311 /** Performs the current undo operation. */
312 void undo();
313
314 /**
315 * Copies all selected items to the clipboard and marks
316 * the items as cut.
317 */
318 void cut();
319
320 /** Copies all selected items to the clipboard. */
321 void copy();
322
323 /** Pastes the clipboard data to the active view. */
324 void paste();
325
326 /** Replaces the URL navigator by a search box to find files. */
327 void find();
328
329 /** Updates the state of the search action according to the view container. */
330 void updateSearchAction();
331
332 /**
333 * Updates the text of the paste action dependent on
334 * the number of items which are in the clipboard.
335 */
336 void updatePasteAction();
337
338 /** Calls DolphinViewContainer::setSelectionMode() for m_activeViewContainer. */
339 void slotSetSelectionMode(bool enabled, SelectionMode::BottomBar::Contents bottomBarContents);
340
341 /** Selects all items from the active view. */
342 void selectAll();
343
344 /**
345 * Inverts the selection of all items of the active view:
346 * Selected items get nonselected and nonselected items get
347 * selected.
348 */
349 void invertSelection();
350
351 /**
352 * Switches between one and two views:
353 * If one view is visible, it will get split into two views.
354 * If already two views are visible, the active view gets closed.
355 */
356 void toggleSplitView();
357
358 /**
359 * Pops out a split view.
360 * The active view will be popped out, unless the view is not split,
361 * in which case nothing will happen.
362 */
363 void popoutSplitView();
364
365 /** Dedicated action to open the stash:/ ioslave in split view. */
366 void toggleSplitStash();
367
368 /** Copies all selected items to the inactive view. */
369 void copyToInactiveSplitView();
370
371 /** Moves all selected items to the inactive view. */
372 void moveToInactiveSplitView();
373
374 /** Reloads the currently active view. */
375 void reloadView();
376
377 /** Stops the loading process for the currently active view. */
378 void stopLoading();
379
380 void enableStopAction();
381 void disableStopAction();
382
383 void toggleSelectionMode();
384
385 void showFilterBar();
386 void toggleFilterBar();
387
388 /**
389 * Toggles between edit and browse mode of the navigation bar.
390 */
391 void toggleEditLocation();
392
393 /**
394 * Switches to the edit mode of the navigation bar and selects
395 * the whole URL, so that it can be replaced by the user. If the edit mode is
396 * already active, it is assured that the navigation bar get focused.
397 */
398 void replaceLocation();
399
400 /**
401 * Toggles the state of the panels between a locked and unlocked layout.
402 */
403 void togglePanelLockState();
404
405 /**
406 * Is invoked whenever the Terminal panel visibility is changed by the user and then moves the focus
407 * to the active view if the panel was hidden.
408 * @note The opposite action (putting focus to the Terminal) is not handled
409 * here but in TerminalPanel::showEvent().
410 * @param visible the new visibility state of the terminal panel
411 */
412 void slotTerminalPanelVisibilityChanged(bool visible);
413
414 /**
415 * Is invoked whenever the Places panel visibility is changed by the user and then either moves the focus
416 * - to the Places panel if it was made visible, or
417 * - to the active view if the panel was hidden.
418 * @param visible the new visibility state of the Places panel
419 */
420 void slotPlacesPanelVisibilityChanged(bool visible);
421
422 /** Goes back one step of the URL history. */
423 void goBack();
424
425 /** Goes forward one step of the URL history. */
426 void goForward();
427
428 /** Goes up one hierarchy of the current URL. */
429 void goUp();
430
431 /** Changes the location to the home URL. */
432 void goHome();
433
434 /** Open the previous URL in the URL history in a new tab. */
435 void goBackInNewTab();
436
437 /** Open the next URL in the URL history in a new tab. */
438 void goForwardInNewTab();
439
440 /** Open the URL one hierarchy above the current URL in a new tab. */
441 void goUpInNewTab();
442
443 /** * Open the home URL in a new tab. */
444 void goHomeInNewTab();
445
446 /** Opens Kompare for 2 selected files. */
447 void compareFiles();
448
449 /**
450 * Hides the menu bar if it is visible, makes the menu bar
451 * visible if it is hidden.
452 */
453 void toggleShowMenuBar();
454
455 /** Updates "Open Preferred Search Tool" action. */
456 void updateOpenPreferredSearchToolAction();
457
458 /** Opens preferred search tool for the current location. */
459 void openPreferredSearchTool();
460
461 /** Opens a terminal window for the current location. */
462 void openTerminal();
463
464 /** Opens terminal windows for the selected items' locations. */
465 void openTerminalHere();
466
467 /** Opens a terminal window for the URL. */
468 void openTerminalJob(const QUrl &url);
469
470 /** Toggles focus to/from a Terminal Panel. */
471 void toggleTerminalPanelFocus();
472
473 /** Toggles focus to/from the Places Panel. */
474 void togglePlacesPanelFocus();
475
476 /** Opens the settings dialog for Dolphin. */
477 void editSettings();
478
479 /** Updates the state of the 'Show Full Location' action. */
480 void slotEditableStateChanged(bool editable);
481
482 /**
483 * Updates the state of the 'Edit' menu actions and emits
484 * the signal selectionChanged().
485 */
486 void slotSelectionChanged(const KFileItemList &selection);
487
488 /**
489 * Updates the state of the 'Back' and 'Forward' menu
490 * actions corresponding to the current history.
491 */
492 void updateHistory();
493
494 /** Updates the state of the 'Show filter bar' menu action. */
495 void updateFilterBarAction(bool show);
496
497 /** Open a new main window. */
498 void openNewMainWindow();
499
500 /**
501 * Opens a new view with the current URL that is part of a tab and
502 * activates it.
503 */
504 void openNewActivatedTab();
505
506 /**
507 * Adds the current URL as an entry to the Places panel
508 */
509 void addToPlaces();
510
511 /**
512 * Opens the selected folder in a new tab.
513 */
514 void openInNewTab();
515
516 /**
517 * Opens the selected folder in a new window.
518 */
519 void openInNewWindow();
520
521 /**
522 * Opens the selected folder in the other inactive split view, enables split view if necessary.
523 */
524 void openInSplitView(const QUrl &url);
525
526 /**
527 * Show the target of the selected symlink
528 */
529 void showTarget();
530
531 /**
532 * Indicates in the statusbar that the execution of the command \a command
533 * has been finished.
534 */
535 void showCommand(CommandType command);
536
537 /**
538 * If the URL can be listed, open it in the current view, otherwise
539 * run it through KRun.
540 */
541 void handleUrl(const QUrl &url);
542
543 /**
544 * Is invoked when the write state of a folder has been changed and
545 * enables/disables the "Create New..." menu entry.
546 */
547 void slotWriteStateChanged(bool isFolderWritable);
548
549 /**
550 * Opens the context menu on the current mouse position.
551 * @pos Position in screen coordinates.
552 * @item File item context. If item is null, the context menu
553 * should be applied to \a url.
554 * @selectedItems The selected items for which the context menu
555 * is opened. This list generally includes \a item.
556 * @url URL which contains \a item.
557 */
558 void openContextMenu(const QPoint &pos, const KFileItem &item, const KFileItemList &selectedItems, const QUrl &url);
559
560 /**
561 * Updates the menu that is by default at the right end of the toolbar.
562 *
563 * In true "simple by default" fashion, the menu only contains the most important
564 * and necessary actions to be able to use Dolphin. This is supposed to hold true even
565 * if the user does not know how to open a context menu. More advanced actions can be
566 * discovered through a sub-menu (@see KConfigWidgets::KHamburgerMenu::setMenuBarAdvertised()).
567 */
568 void updateHamburgerMenu();
569
570 /**
571 * Is called if the user clicked an item in the Places Panel.
572 * Reloads the view if \a url is the current URL already, and changes the
573 * current URL otherwise.
574 */
575 void slotPlaceActivated(const QUrl &url);
576
577 /**
578 * Is called if the another view has been activated by changing the current
579 * tab or activating another view in split-view mode.
580 *
581 * Activates the given view, which means that all menu actions are applied
582 * to this view. When having a split view setup, the nonactive view is
583 * usually shown in darker colors.
584 */
585 void activeViewChanged(DolphinViewContainer *viewContainer);
586
587 void closedTabsCountChanged(unsigned int count);
588
589 /**
590 * Is called if a new tab has been opened or a tab has been closed to
591 * enable/disable the tab actions.
592 */
593 void tabCountChanged(int count);
594
595 /**
596 * Updates the Window Title with the caption from the active view container
597 */
598 void updateWindowTitle();
599
600 /**
601 * This slot is called when the user requested to unmount a removable media
602 * from the places menu
603 */
604 void slotStorageTearDownFromPlacesRequested(const QString &mountPath);
605
606 /**
607 * This slot is called when the user requested to unmount a removable media
608 * _not_ from the dolphin's places menu (from the notification area for e.g.)
609 * This slot is basically connected to each removable device's
610 * Solid::StorageAccess::teardownRequested(const QString & udi)
611 * signal through the places panel.
612 */
613 void slotStorageTearDownExternallyRequested(const QString &mountPath);
614
615 /**
616 * Is called when the view has finished loading the directory.
617 */
618 void slotDirectoryLoadingCompleted();
619
620 /**
621 * Is called when the user middle clicks a toolbar button.
622 *
623 * Here middle clicking Back/Forward/Up/Home will open the resulting
624 * folder in a new tab.
625 */
626 void slotToolBarActionMiddleClicked(QAction *action);
627
628 /**
629 * Is called before the Back popup menu is shown. This slot will populate
630 * the menu with history data
631 */
632 void slotAboutToShowBackPopupMenu();
633
634 /**
635 * This slot is used by the Back Popup Menu to go back to a specific
636 * history index. The QAction::data will carry an int with the index
637 * to go to.
638 */
639 void slotGoBack(QAction *action);
640
641 /**
642 * Middle clicking Back/Forward will open the resulting folder in a new tab.
643 */
644 void slotBackForwardActionMiddleClicked(QAction *action);
645
646 /**
647 * Is called before the Forward popup menu is shown. This slot will populate
648 * the menu with history data
649 */
650 void slotAboutToShowForwardPopupMenu();
651
652 /**
653 * This slot is used by the Forward Popup Menu to go forward to a specific
654 * history index. The QAction::data will carry an int with the index
655 * to go to.
656 */
657 void slotGoForward(QAction *action);
658
659 /**
660 * Is called when configuring the keyboard shortcuts
661 */
662 void slotKeyBindings();
663
664 /**
665 * Saves the session.
666 */
667 void slotSaveSession();
668
669 private:
670 /**
671 * Sets up the various menus and actions and connects them.
672 */
673 void setupActions();
674
675 /**
676 * Sets up the dock widgets and their panels.
677 */
678 void setupDockWidgets();
679
680 void updateFileAndEditActions();
681 void updateViewActions();
682 void updateGoActions();
683
684 /**
685 * Connects the signals from the created DolphinView with
686 * the DolphinViewContainer \a container with the corresponding slots of
687 * the DolphinMainWindow. This method must be invoked each
688 * time a DolphinView has been created.
689 */
690 void connectViewSignals(DolphinViewContainer *container);
691
692 /**
693 * Updates the text of the split action:
694 * If two views are shown, the text is set to "Split",
695 * otherwise the text is set to "Join". The icon
696 * is updated to match with the text and the currently active view.
697 */
698 void updateSplitActions();
699
700 /**
701 * Sets the window sides the toolbar may be moved to based on toolbar contents.
702 */
703 void updateAllowedToolbarAreas();
704
705 bool isKompareInstalled() const;
706
707 /**
708 * Creates an action for showing/hiding a panel, that is accessible
709 * in "Configure toolbars..." and "Configure shortcuts...".
710 */
711 void createPanelAction(const QIcon &icon, const QKeySequence &shortcut, QDockWidget *dockAction, const QString &actionName);
712
713 /** Adds "What's This?" texts to many widgets and StandardActions. */
714 void setupWhatsThis();
715
716 /** Returns preferred search tool as configured in "More Search Tools" menu. */
717 QPointer<QAction> preferredSearchTool();
718
719 /**
720 * Adds this action to the mainWindow's toolbar and saves the change
721 * in the users ui configuration file.
722 * This method is only needed for migration and should be removed once we can expect
723 * that pretty much all users have been migrated. Remove in 2026 because that's when
724 * even the most risk-averse distros will already have been forced to upgrade.
725 * @return true if successful. Otherwise false.
726 */
727 bool addHamburgerMenuToToolbar();
728
729 /** Creates an action representing an item in the URL navigator history */
730 static QAction *urlNavigatorHistoryAction(const KUrlNavigator *urlNavigator, int historyIndex, QObject *parent = nullptr);
731
732 private:
733 /**
734 * Implements a custom error handling for the undo manager. This
735 * assures that all errors are shown in the status bar of Dolphin
736 * instead as modal error dialog with an OK button.
737 */
738 class UndoUiInterface : public KIO::FileUndoManager::UiInterface
739 {
740 public:
741 UndoUiInterface();
742 ~UndoUiInterface() override;
743 void jobError(KIO::Job *job) override;
744 };
745
746 KNewFileMenu *m_newFileMenu;
747 DolphinTabWidget *m_tabWidget;
748 DolphinViewContainer *m_activeViewContainer;
749
750 DolphinViewActionHandler *m_actionHandler;
751 DolphinRemoteEncoding *m_remoteEncoding;
752 QPointer<DolphinSettingsDialog> m_settingsDialog;
753 DolphinBookmarkHandler *m_bookmarkHandler;
754 SelectionMode::ActionTextHelper *m_actionTextHelper;
755 DisabledActionNotifier *m_disabledActionNotifier;
756
757 KIO::OpenUrlJob *m_lastHandleUrlOpenJob;
758
759 TerminalPanel *m_terminalPanel;
760 PlacesPanel *m_placesPanel;
761 bool m_tearDownFromPlacesRequested;
762
763 KToolBarPopupAction *m_backAction;
764 KToolBarPopupAction *m_forwardAction;
765 KActionMenu *m_splitViewAction;
766 QAction *m_splitViewMenuAction;
767
768 QMenu m_searchTools;
769 KFileItemActions m_fileItemActions;
770
771 QTimer *m_sessionSaveTimer;
772 QFutureWatcher<void> *m_sessionSaveWatcher;
773 bool m_sessionSaveScheduled;
774
775 KIO::CommandLauncherJob *m_job;
776
777 KRecentFilesAction *m_recentFiles = nullptr;
778
779 friend class DolphinMainWindowTest;
780 };
781
782 inline DolphinViewContainer *DolphinMainWindow::activeViewContainer() const
783 {
784 return m_activeViewContainer;
785 }
786
787 inline KNewFileMenu *DolphinMainWindow::newFileMenu() const
788 {
789 return m_newFileMenu;
790 }
791
792 #endif // DOLPHIN_MAINWINDOW_H