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