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