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