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