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