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