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