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