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