]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinmainwindow.h
Apply 1 suggestion(s) to 1 file(s)
[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 * @return A pointer to the opened DolphinTabPage.
213 */
214 DolphinTabPage *openNewTab(const QUrl &url);
215
216 /**
217 * Opens a new tab showing the URL \a url and activate it.
218 */
219 void openNewTabAndActivate(const QUrl &url);
220
221 /**
222 * Opens a new window showing the URL \a url.
223 */
224 void openNewWindow(const QUrl &url);
225
226 /** @see GeneralSettings::splitViewChanged() */
227 void slotSplitViewChanged();
228
229 Q_SIGNALS:
230 /**
231 * Is sent if the selection of the currently active view has
232 * been changed.
233 */
234 void selectionChanged(const KFileItemList &selection);
235
236 /**
237 * Is sent if the url of the currently active view has
238 * been changed.
239 */
240 void urlChanged(const QUrl &url);
241
242 /**
243 * Is emitted if information of an item is requested to be shown e. g. in the panel.
244 * If item is null, no item information request is pending.
245 */
246 void requestItemInfo(const KFileItem &item);
247
248 /**
249 * It is emitted when in the current view, files are changed,
250 * or dirs have files/removed from them.
251 */
252 void fileItemsChanged(const KFileItemList &changedFileItems);
253
254 /**
255 * Is emitted if the settings have been changed.
256 */
257 void settingsChanged();
258
259 protected:
260 /** @see QObject::event() */
261 bool event(QEvent *event) override;
262
263 /** @see QWidget::showEvent() */
264 void showEvent(QShowEvent *event) override;
265
266 /** @see QMainWindow::closeEvent() */
267 void closeEvent(QCloseEvent *event) override;
268
269 /** @see KMainWindow::saveProperties() */
270 void saveProperties(KConfigGroup &group) override;
271
272 /** @see KMainWindow::readProperties() */
273 void readProperties(const KConfigGroup &group) override;
274
275 /** Sets a sane initial window size **/
276 QSize sizeHint() const override;
277
278 protected Q_SLOTS:
279 /**
280 * Calls the base method KXmlGuiWindow::saveNewToolbarConfig().
281 * Is also used to set toolbar constraints and UrlNavigator position
282 * based on the newly changed toolbar configuration.
283 */
284 void saveNewToolbarConfig() override;
285
286 private Q_SLOTS:
287 /**
288 * Refreshes the views of the main window by recreating them according to
289 * the given Dolphin settings.
290 */
291 void refreshViews();
292
293 void clearStatusBar();
294
295 /** Updates the 'Create New...' sub menu. */
296 void updateNewMenu();
297
298 void createDirectory();
299 void createFile();
300
301 /** Shows the error message in the status bar of the active view. */
302 void showErrorMessage(const QString &message);
303
304 /**
305 * Updates the state of the 'Undo' menu action dependent
306 * on the parameter \a available.
307 */
308 void slotUndoAvailable(bool available);
309
310 /** Sets the text of the 'Undo' menu action to \a text. */
311 void slotUndoTextChanged(const QString &text);
312
313 /** Performs the current undo operation. */
314 void undo();
315
316 /**
317 * Copies all selected items to the clipboard and marks
318 * the items as cut.
319 */
320 void cut();
321
322 /** Copies all selected items to the clipboard. */
323 void copy();
324
325 /** Pastes the clipboard data to the active view. */
326 void paste();
327
328 /** Replaces the URL navigator by a search box to find files. */
329 void find();
330
331 /** Updates the state of the search action according to the view container. */
332 void updateSearchAction();
333
334 /**
335 * Updates the text of the paste action dependent on
336 * the number of items which are in the clipboard.
337 */
338 void updatePasteAction();
339
340 /** Calls DolphinViewContainer::setSelectionMode() for m_activeViewContainer. */
341 void slotSetSelectionMode(bool enabled, SelectionMode::BottomBar::Contents bottomBarContents);
342
343 /** Selects all items from the active view. */
344 void selectAll();
345
346 /**
347 * Inverts the selection of all items of the active view:
348 * Selected items get nonselected and nonselected items get
349 * selected.
350 */
351 void invertSelection();
352
353 /**
354 * Switches between one and two views:
355 * If one view is visible, it will get split into two views.
356 * If already two views are visible, the active view gets closed.
357 */
358 void toggleSplitView();
359
360 /**
361 * Pops out a split view.
362 * The active view will be popped out, unless the view is not split,
363 * in which case nothing will happen.
364 */
365 void popoutSplitView();
366
367 /** Dedicated action to open the stash:/ ioslave in split view. */
368 void toggleSplitStash();
369
370 /** Copies all selected items to the inactive view. */
371 void copyToInactiveSplitView();
372
373 /** Moves all selected items to the inactive view. */
374 void moveToInactiveSplitView();
375
376 /** Reloads the currently active view. */
377 void reloadView();
378
379 /** Stops the loading process for the currently active view. */
380 void stopLoading();
381
382 void enableStopAction();
383 void disableStopAction();
384
385 void toggleSelectionMode();
386
387 void showFilterBar();
388 void toggleFilterBar();
389
390 /**
391 * Toggles between edit and browse mode of the navigation bar.
392 */
393 void toggleEditLocation();
394
395 /**
396 * Switches to the edit mode of the navigation bar and selects
397 * the whole URL, so that it can be replaced by the user. If the edit mode is
398 * already active, it is assured that the navigation bar get focused.
399 */
400 void replaceLocation();
401
402 /**
403 * Toggles the state of the panels between a locked and unlocked layout.
404 */
405 void togglePanelLockState();
406
407 /**
408 * Is invoked whenever the Terminal panel visibility is changed by the user and then moves the focus
409 * to the active view if the panel was hidden.
410 * @note The opposite action (putting focus to the Terminal) is not handled
411 * here but in TerminalPanel::showEvent().
412 * @param visible the new visibility state of the terminal panel
413 */
414 void slotTerminalPanelVisibilityChanged(bool visible);
415
416 /**
417 * Is invoked whenever the Places panel visibility is changed by the user and then either moves the focus
418 * - to the Places panel if it was made visible, or
419 * - to the active view if the panel was hidden.
420 * @param visible the new visibility state of the Places panel
421 */
422 void slotPlacesPanelVisibilityChanged(bool visible);
423
424 /** Goes back one step of the URL history. */
425 void goBack();
426
427 /** Goes forward one step of the URL history. */
428 void goForward();
429
430 /** Goes up one hierarchy of the current URL. */
431 void goUp();
432
433 /** Changes the location to the home URL. */
434 void goHome();
435
436 /** Open the previous URL in the URL history in a new tab. */
437 void goBackInNewTab();
438
439 /** Open the next URL in the URL history in a new tab. */
440 void goForwardInNewTab();
441
442 /** Open the URL one hierarchy above the current URL in a new tab. */
443 void goUpInNewTab();
444
445 /** * Open the home URL in a new tab. */
446 void goHomeInNewTab();
447
448 /** Opens Kompare for 2 selected files. */
449 void compareFiles();
450
451 /**
452 * Hides the menu bar if it is visible, makes the menu bar
453 * visible if it is hidden.
454 */
455 void toggleShowMenuBar();
456
457 /** Updates "Open Preferred Search Tool" action. */
458 void updateOpenPreferredSearchToolAction();
459
460 /** Opens preferred search tool for the current location. */
461 void openPreferredSearchTool();
462
463 /** Opens a terminal window for the current location. */
464 void openTerminal();
465
466 /** Opens terminal windows for the selected items' locations. */
467 void openTerminalHere();
468
469 /** Opens a terminal window for the URL. */
470 void openTerminalJob(const QUrl &url);
471
472 /** Toggles focus to/from a Terminal Panel. */
473 void toggleTerminalPanelFocus();
474
475 /** Toggles focus to/from the Places Panel. */
476 void togglePlacesPanelFocus();
477
478 /** Opens the settings dialog for Dolphin. */
479 void editSettings();
480
481 /** Updates the state of the 'Show Full Location' action. */
482 void slotEditableStateChanged(bool editable);
483
484 /**
485 * Updates the state of the 'Edit' menu actions and emits
486 * the signal selectionChanged().
487 */
488 void slotSelectionChanged(const KFileItemList &selection);
489
490 /**
491 * Updates the state of the 'Back' and 'Forward' menu
492 * actions corresponding to the current history.
493 */
494 void updateHistory();
495
496 /** Updates the state of the 'Show filter bar' menu action. */
497 void updateFilterBarAction(bool show);
498
499 /** Open a new main window. */
500 void openNewMainWindow();
501
502 /**
503 * Opens a new view with the current URL that is part of a tab and
504 * activates it.
505 */
506 void openNewActivatedTab();
507
508 /**
509 * Adds the current URL as an entry to the Places panel
510 */
511 void addToPlaces();
512
513 /**
514 * Opens the selected folder in a new tab.
515 */
516 void openInNewTab();
517
518 /**
519 * Opens the selected folder in a new window.
520 */
521 void openInNewWindow();
522
523 /**
524 * Opens the selected folder in the other inactive split view, enables split view if necessary.
525 */
526 void openInSplitView(const QUrl &url);
527
528 /**
529 * Show the target of the selected symlink
530 */
531 void showTarget();
532
533 /**
534 * Indicates in the statusbar that the execution of the command \a command
535 * has been finished.
536 */
537 void showCommand(CommandType command);
538
539 /**
540 * If the URL can be listed, open it in the current view, otherwise
541 * run it through KRun.
542 */
543 void handleUrl(const QUrl &url);
544
545 /**
546 * Is invoked when the write state of a folder has been changed and
547 * enables/disables the "Create New..." menu entry.
548 */
549 void slotWriteStateChanged(bool isFolderWritable);
550
551 /**
552 * Opens the context menu on the current mouse position.
553 * @pos Position in screen coordinates.
554 * @item File item context. If item is null, the context menu
555 * should be applied to \a url.
556 * @selectedItems The selected items for which the context menu
557 * is opened. This list generally includes \a item.
558 * @url URL which contains \a item.
559 */
560 void openContextMenu(const QPoint &pos, const KFileItem &item, const KFileItemList &selectedItems, const QUrl &url);
561
562 /**
563 * Updates the menu that is by default at the right end of the toolbar.
564 *
565 * In true "simple by default" fashion, the menu only contains the most important
566 * and necessary actions to be able to use Dolphin. This is supposed to hold true even
567 * if the user does not know how to open a context menu. More advanced actions can be
568 * discovered through a sub-menu (@see KConfigWidgets::KHamburgerMenu::setMenuBarAdvertised()).
569 */
570 void updateHamburgerMenu();
571
572 /**
573 * Is called if the user clicked an item in the Places Panel.
574 * Reloads the view if \a url is the current URL already, and changes the
575 * current URL otherwise.
576 */
577 void slotPlaceActivated(const QUrl &url);
578
579 /**
580 * Is called if the another view has been activated by changing the current
581 * tab or activating another view in split-view mode.
582 *
583 * Activates the given view, which means that all menu actions are applied
584 * to this view. When having a split view setup, the nonactive view is
585 * usually shown in darker colors.
586 */
587 void activeViewChanged(DolphinViewContainer *viewContainer);
588
589 void closedTabsCountChanged(unsigned int count);
590
591 /**
592 * Is called if a new tab has been opened or a tab has been closed to
593 * enable/disable the tab actions.
594 */
595 void tabCountChanged(int count);
596
597 /**
598 * Updates the Window Title with the caption from the active view container
599 */
600 void updateWindowTitle();
601
602 /**
603 * This slot is called when the user requested to unmount a removable media
604 * from the places menu
605 */
606 void slotStorageTearDownFromPlacesRequested(const QString &mountPath);
607
608 /**
609 * This slot is called when the user requested to unmount a removable media
610 * _not_ from the dolphin's places menu (from the notification area for e.g.)
611 * This slot is basically connected to each removable device's
612 * Solid::StorageAccess::teardownRequested(const QString & udi)
613 * signal through the places panel.
614 */
615 void slotStorageTearDownExternallyRequested(const QString &mountPath);
616
617 /**
618 * Is called when the view has finished loading the directory.
619 */
620 void slotDirectoryLoadingCompleted();
621
622 /**
623 * Is called when the user middle clicks a toolbar button.
624 *
625 * Here middle clicking Back/Forward/Up/Home will open the resulting
626 * folder in a new tab.
627 */
628 void slotToolBarActionMiddleClicked(QAction *action);
629
630 /**
631 * Is called before the Back popup menu is shown. This slot will populate
632 * the menu with history data
633 */
634 void slotAboutToShowBackPopupMenu();
635
636 /**
637 * This slot is used by the Back Popup Menu to go back to a specific
638 * history index. The QAction::data will carry an int with the index
639 * to go to.
640 */
641 void slotGoBack(QAction *action);
642
643 /**
644 * Middle clicking Back/Forward will open the resulting folder in a new tab.
645 */
646 void slotBackForwardActionMiddleClicked(QAction *action);
647
648 /**
649 * Is called before the Forward popup menu is shown. This slot will populate
650 * the menu with history data
651 */
652 void slotAboutToShowForwardPopupMenu();
653
654 /**
655 * This slot is used by the Forward Popup Menu to go forward to a specific
656 * history index. The QAction::data will carry an int with the index
657 * to go to.
658 */
659 void slotGoForward(QAction *action);
660
661 /**
662 * Is called when configuring the keyboard shortcuts
663 */
664 void slotKeyBindings();
665
666 /**
667 * Saves the session.
668 */
669 void slotSaveSession();
670
671 private:
672 /**
673 * Sets up the various menus and actions and connects them.
674 */
675 void setupActions();
676
677 /**
678 * Sets up the dock widgets and their panels.
679 */
680 void setupDockWidgets();
681
682 void updateFileAndEditActions();
683 void updateViewActions();
684 void updateGoActions();
685
686 /**
687 * Connects the signals from the created DolphinView with
688 * the DolphinViewContainer \a container with the corresponding slots of
689 * the DolphinMainWindow. This method must be invoked each
690 * time a DolphinView has been created.
691 */
692 void connectViewSignals(DolphinViewContainer *container);
693
694 /**
695 * Updates the text of the split action:
696 * If two views are shown, the text is set to "Split",
697 * otherwise the text is set to "Join". The icon
698 * is updated to match with the text and the currently active view.
699 */
700 void updateSplitActions();
701
702 /**
703 * Sets the window sides the toolbar may be moved to based on toolbar contents.
704 */
705 void updateAllowedToolbarAreas();
706
707 bool isKompareInstalled() const;
708
709 /**
710 * Creates an action for showing/hiding a panel, that is accessible
711 * in "Configure toolbars..." and "Configure shortcuts...".
712 */
713 void createPanelAction(const QIcon &icon, const QKeySequence &shortcut, QDockWidget *dockAction, const QString &actionName);
714
715 /** Adds "What's This?" texts to many widgets and StandardActions. */
716 void setupWhatsThis();
717
718 /** Returns preferred search tool as configured in "More Search Tools" menu. */
719 QPointer<QAction> preferredSearchTool();
720
721 /**
722 * Adds this action to the mainWindow's toolbar and saves the change
723 * in the users ui configuration file.
724 * This method is only needed for migration and should be removed once we can expect
725 * that pretty much all users have been migrated. Remove in 2026 because that's when
726 * even the most risk-averse distros will already have been forced to upgrade.
727 * @return true if successful. Otherwise false.
728 */
729 bool addHamburgerMenuToToolbar();
730
731 /** Creates an action representing an item in the URL navigator history */
732 static QAction *urlNavigatorHistoryAction(const KUrlNavigator *urlNavigator, int historyIndex, QObject *parent = nullptr);
733
734 private:
735 /**
736 * Implements a custom error handling for the undo manager. This
737 * assures that all errors are shown in the status bar of Dolphin
738 * instead as modal error dialog with an OK button.
739 */
740 class UndoUiInterface : public KIO::FileUndoManager::UiInterface
741 {
742 public:
743 UndoUiInterface();
744 ~UndoUiInterface() override;
745 void jobError(KIO::Job *job) override;
746 };
747
748 KNewFileMenu *m_newFileMenu;
749 DolphinTabWidget *m_tabWidget;
750 DolphinViewContainer *m_activeViewContainer;
751
752 DolphinViewActionHandler *m_actionHandler;
753 DolphinRemoteEncoding *m_remoteEncoding;
754 QPointer<DolphinSettingsDialog> m_settingsDialog;
755 DolphinBookmarkHandler *m_bookmarkHandler;
756 SelectionMode::ActionTextHelper *m_actionTextHelper;
757 DisabledActionNotifier *m_disabledActionNotifier;
758
759 KIO::OpenUrlJob *m_lastHandleUrlOpenJob;
760
761 TerminalPanel *m_terminalPanel;
762 PlacesPanel *m_placesPanel;
763 bool m_tearDownFromPlacesRequested;
764
765 KToolBarPopupAction *m_backAction;
766 KToolBarPopupAction *m_forwardAction;
767 KActionMenu *m_splitViewAction;
768 QAction *m_splitViewMenuAction;
769
770 QMenu m_searchTools;
771 KFileItemActions m_fileItemActions;
772
773 QTimer *m_sessionSaveTimer;
774 QFutureWatcher<void> *m_sessionSaveWatcher;
775 bool m_sessionSaveScheduled;
776
777 KIO::CommandLauncherJob *m_job;
778
779 KRecentFilesAction *m_recentFiles = nullptr;
780
781 friend class DolphinMainWindowTest;
782 };
783
784 inline DolphinViewContainer *DolphinMainWindow::activeViewContainer() const
785 {
786 return m_activeViewContainer;
787 }
788
789 inline KNewFileMenu *DolphinMainWindow::newFileMenu() const
790 {
791 return m_newFileMenu;
792 }
793
794 #endif // DOLPHIN_MAINWINDOW_H