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