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