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