]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinmainwindow.h
Merge branch '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 bool isOnActivity(const QString &activityId) const;
208 bool isOnCurrentDesktop() const;
209
210 Q_SIGNALS:
211 /**
212 * Is sent if the selection of the currently active view has
213 * been changed.
214 */
215 void selectionChanged(const KFileItemList &selection);
216
217 /**
218 * Is sent if the url of the currently active view has
219 * been changed.
220 */
221 void urlChanged(const QUrl &url);
222
223 /**
224 * Is emitted if information of an item is requested to be shown e. g. in the panel.
225 * If item is null, no item information request is pending.
226 */
227 void requestItemInfo(const KFileItem &item);
228
229 /**
230 * It is emitted when in the current view, files are changed,
231 * or dirs have files/removed from them.
232 */
233 void fileItemsChanged(const KFileItemList &changedFileItems);
234
235 /**
236 * Is emitted if the settings have been changed.
237 */
238 void settingsChanged();
239
240 protected:
241 /** @see QObject::event() */
242 bool event(QEvent *event) override;
243
244 /** @see QWidget::showEvent() */
245 void showEvent(QShowEvent *event) override;
246
247 /** @see QMainWindow::closeEvent() */
248 void closeEvent(QCloseEvent *event) override;
249
250 /** @see KMainWindow::saveProperties() */
251 void saveProperties(KConfigGroup &group) override;
252
253 /** @see KMainWindow::readProperties() */
254 void readProperties(const KConfigGroup &group) 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, const QKeySequence &shortcut, QAction *dockAction, const QString &actionName);
665
666 /** Adds "What's This?" texts to many widgets and StandardActions. */
667 void setupWhatsThis();
668
669 /** Returns preferred search tool as configured in "More Search Tools" menu. */
670 QPointer<QAction> preferredSearchTool();
671
672 /**
673 * Adds this action to the mainWindow's toolbar and saves the change
674 * in the users ui configuration file.
675 * This method is only needed for migration and should be removed once we can expect
676 * that pretty much all users have been migrated. Remove in 2026 because that's when
677 * even the most risk-averse distros will already have been forced to upgrade.
678 * @return true if successful. Otherwise false.
679 */
680 bool addHamburgerMenuToToolbar();
681
682 /** Creates an action representing an item in the URL navigator history */
683 static QAction *urlNavigatorHistoryAction(const KUrlNavigator *urlNavigator, int historyIndex, QObject *parent = nullptr);
684
685 private:
686 /**
687 * Implements a custom error handling for the undo manager. This
688 * assures that all errors are shown in the status bar of Dolphin
689 * instead as modal error dialog with an OK button.
690 */
691 class UndoUiInterface : public KIO::FileUndoManager::UiInterface
692 {
693 public:
694 UndoUiInterface();
695 ~UndoUiInterface() override;
696 void jobError(KIO::Job *job) override;
697 };
698
699 KNewFileMenu *m_newFileMenu;
700 DolphinTabWidget *m_tabWidget;
701 DolphinViewContainer *m_activeViewContainer;
702
703 DolphinViewActionHandler *m_actionHandler;
704 DolphinRemoteEncoding *m_remoteEncoding;
705 QPointer<DolphinSettingsDialog> m_settingsDialog;
706 DolphinBookmarkHandler *m_bookmarkHandler;
707 SelectionMode::ActionTextHelper *m_actionTextHelper;
708
709 KIO::OpenUrlJob *m_lastHandleUrlOpenJob;
710
711 TerminalPanel *m_terminalPanel;
712 PlacesPanel *m_placesPanel;
713 bool m_tearDownFromPlacesRequested;
714
715 KToolBarPopupAction *m_backAction;
716 KToolBarPopupAction *m_forwardAction;
717
718 QMenu m_searchTools;
719 KFileItemActions m_fileItemActions;
720
721 friend class DolphinMainWindowTest;
722 };
723
724 inline DolphinViewContainer *DolphinMainWindow::activeViewContainer() const
725 {
726 return m_activeViewContainer;
727 }
728
729 inline KNewFileMenu *DolphinMainWindow::newFileMenu() const
730 {
731 return m_newFileMenu;
732 }
733
734 #endif // DOLPHIN_MAINWINDOW_H