]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinmainwindow.cpp
Renames DolphinViewContainer::getCaption to caption
[dolphin.git] / src / dolphinmainwindow.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz <peter.penz19@gmail.com> *
3 * Copyright (C) 2006 by Stefan Monov <logixoul@gmail.com> *
4 * Copyright (C) 2006 by Cvetoslav Ludmiloff <ludmiloff@gmail.com> *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the *
18 * Free Software Foundation, Inc., *
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
20 ***************************************************************************/
21
22 #include "dolphinmainwindow.h"
23
24 #include "global.h"
25 #include "dolphindockwidget.h"
26 #include "dolphincontextmenu.h"
27 #include "dolphinnewfilemenu.h"
28 #include "dolphinrecenttabsmenu.h"
29 #include "dolphintabwidget.h"
30 #include "dolphinviewcontainer.h"
31 #include "dolphintabpage.h"
32 #include "middleclickactioneventfilter.h"
33 #include "panels/folders/folderspanel.h"
34 #include "panels/places/placespanel.h"
35 #include "panels/information/informationpanel.h"
36 #include "panels/terminal/terminalpanel.h"
37 #include "settings/dolphinsettingsdialog.h"
38 #include "statusbar/dolphinstatusbar.h"
39 #include "views/dolphinviewactionhandler.h"
40 #include "views/dolphinremoteencoding.h"
41 #include "views/draganddrophelper.h"
42 #include "views/viewproperties.h"
43 #include "views/dolphinnewfilemenuobserver.h"
44 #include "dolphin_generalsettings.h"
45
46 #include <KActionCollection>
47 #include <KActionMenu>
48 #include <KAuthorized>
49 #include <KConfig>
50 #include <KFileItemListProperties>
51 #include <KHelpMenu>
52 #include <KIO/JobUiDelegate>
53 #include <KIO/OpenFileManagerWindowJob>
54 #include <KJobWidgets>
55 #include <KLocalizedString>
56 #include <KMessageBox>
57 #include <KProtocolInfo>
58 #include <KProtocolManager>
59 #include <KRun>
60 #include <KShell>
61 #include <KStandardAction>
62 #include <KToggleAction>
63 #include <KToolBar>
64 #include <KToolInvocation>
65 #include <KUrlComboBox>
66 #include <KUrlNavigator>
67
68 #include <QApplication>
69 #include <QClipboard>
70 #include <QCloseEvent>
71 #include <QDialog>
72 #include <QFileInfo>
73 #include <QLineEdit>
74 #include <QMenu>
75 #include <QMenuBar>
76 #include <QPushButton>
77 #include <QShowEvent>
78 #include <QStandardPaths>
79 #include <QTimer>
80 #include <QToolButton>
81 #include <kdualaction.h>
82
83 namespace {
84 // Used for GeneralSettings::version() to determine whether
85 // an updated version of Dolphin is running.
86 const int CurrentDolphinVersion = 200;
87 }
88
89 DolphinMainWindow::DolphinMainWindow() :
90 KXmlGuiWindow(nullptr),
91 m_newFileMenu(nullptr),
92 m_tabWidget(nullptr),
93 m_activeViewContainer(nullptr),
94 m_actionHandler(nullptr),
95 m_remoteEncoding(nullptr),
96 m_settingsDialog(),
97 m_controlButton(nullptr),
98 m_updateToolBarTimer(nullptr),
99 m_lastHandleUrlStatJob(nullptr),
100 m_terminalPanel(nullptr),
101 m_placesPanel(nullptr),
102 m_tearDownFromPlacesRequested(false)
103 {
104 Q_INIT_RESOURCE(dolphin);
105 setComponentName(QStringLiteral("dolphin"), QGuiApplication::applicationDisplayName());
106 setObjectName(QStringLiteral("Dolphin#"));
107
108 connect(&DolphinNewFileMenuObserver::instance(), &DolphinNewFileMenuObserver::errorMessage,
109 this, &DolphinMainWindow::showErrorMessage);
110
111 KIO::FileUndoManager* undoManager = KIO::FileUndoManager::self();
112 undoManager->setUiInterface(new UndoUiInterface());
113
114 connect(undoManager, static_cast<void(KIO::FileUndoManager::*)(bool)>(&KIO::FileUndoManager::undoAvailable),
115 this, &DolphinMainWindow::slotUndoAvailable);
116 connect(undoManager, &KIO::FileUndoManager::undoTextChanged,
117 this, &DolphinMainWindow::slotUndoTextChanged);
118 connect(undoManager, &KIO::FileUndoManager::jobRecordingStarted,
119 this, &DolphinMainWindow::clearStatusBar);
120 connect(undoManager, &KIO::FileUndoManager::jobRecordingFinished,
121 this, &DolphinMainWindow::showCommand);
122
123 GeneralSettings* generalSettings = GeneralSettings::self();
124 const bool firstRun = (generalSettings->version() < 200);
125 if (firstRun) {
126 generalSettings->setViewPropsTimestamp(QDateTime::currentDateTime());
127 }
128
129 setAcceptDrops(true);
130
131 m_tabWidget = new DolphinTabWidget(this);
132 m_tabWidget->setObjectName("tabWidget");
133 connect(m_tabWidget, &DolphinTabWidget::activeViewChanged,
134 this, &DolphinMainWindow::activeViewChanged);
135 connect(m_tabWidget, &DolphinTabWidget::tabCountChanged,
136 this, &DolphinMainWindow::tabCountChanged);
137 connect(m_tabWidget, &DolphinTabWidget::currentUrlChanged,
138 this, &DolphinMainWindow::updateWindowTitle);
139 setCentralWidget(m_tabWidget);
140
141 setupActions();
142
143 m_actionHandler = new DolphinViewActionHandler(actionCollection(), this);
144 connect(m_actionHandler, &DolphinViewActionHandler::actionBeingHandled, this, &DolphinMainWindow::clearStatusBar);
145 connect(m_actionHandler, &DolphinViewActionHandler::createDirectory, this, &DolphinMainWindow::createDirectory);
146
147 m_remoteEncoding = new DolphinRemoteEncoding(this, m_actionHandler);
148 connect(this, &DolphinMainWindow::urlChanged,
149 m_remoteEncoding, &DolphinRemoteEncoding::slotAboutToOpenUrl);
150
151 setupDockWidgets();
152
153 setupGUI(Keys | Save | Create | ToolBar);
154 stateChanged(QStringLiteral("new_file"));
155
156 QClipboard* clipboard = QApplication::clipboard();
157 connect(clipboard, &QClipboard::dataChanged,
158 this, &DolphinMainWindow::updatePasteAction);
159
160 QAction* showFilterBarAction = actionCollection()->action(QStringLiteral("show_filter_bar"));
161 showFilterBarAction->setChecked(generalSettings->filterBar());
162
163 if (firstRun) {
164 menuBar()->setVisible(false);
165 // Assure a proper default size if Dolphin runs the first time
166 resize(750, 500);
167 }
168
169 const bool showMenu = !menuBar()->isHidden();
170 QAction* showMenuBarAction = actionCollection()->action(KStandardAction::name(KStandardAction::ShowMenubar));
171 showMenuBarAction->setChecked(showMenu); // workaround for bug #171080
172 if (!showMenu) {
173 createControlButton();
174 }
175
176 // enable middle-click on back/forward/up to open in a new tab
177 auto *middleClickEventFilter = new MiddleClickActionEventFilter(this);
178 connect(middleClickEventFilter, &MiddleClickActionEventFilter::actionMiddleClicked, this, &DolphinMainWindow::slotToolBarActionMiddleClicked);
179 toolBar()->installEventFilter(middleClickEventFilter);
180 }
181
182 DolphinMainWindow::~DolphinMainWindow()
183 {
184 }
185
186 void DolphinMainWindow::openDirectories(const QList<QUrl>& dirs, bool splitView)
187 {
188 m_tabWidget->openDirectories(dirs, splitView);
189 }
190
191 void DolphinMainWindow::openFiles(const QList<QUrl>& files, bool splitView)
192 {
193 m_tabWidget->openFiles(files, splitView);
194 }
195
196 void DolphinMainWindow::showCommand(CommandType command)
197 {
198 DolphinStatusBar* statusBar = m_activeViewContainer->statusBar();
199 switch (command) {
200 case KIO::FileUndoManager::Copy:
201 statusBar->setText(i18nc("@info:status", "Successfully copied."));
202 break;
203 case KIO::FileUndoManager::Move:
204 statusBar->setText(i18nc("@info:status", "Successfully moved."));
205 break;
206 case KIO::FileUndoManager::Link:
207 statusBar->setText(i18nc("@info:status", "Successfully linked."));
208 break;
209 case KIO::FileUndoManager::Trash:
210 statusBar->setText(i18nc("@info:status", "Successfully moved to trash."));
211 break;
212 case KIO::FileUndoManager::Rename:
213 statusBar->setText(i18nc("@info:status", "Successfully renamed."));
214 break;
215
216 case KIO::FileUndoManager::Mkdir:
217 statusBar->setText(i18nc("@info:status", "Created folder."));
218 break;
219
220 default:
221 break;
222 }
223 }
224
225 void DolphinMainWindow::pasteIntoFolder()
226 {
227 m_activeViewContainer->view()->pasteIntoFolder();
228 }
229
230 void DolphinMainWindow::changeUrl(const QUrl &url)
231 {
232 if (!KProtocolManager::supportsListing(url)) {
233 // The URL navigator only checks for validity, not
234 // if the URL can be listed. An error message is
235 // shown due to DolphinViewContainer::restoreView().
236 return;
237 }
238
239 m_activeViewContainer->setUrl(url);
240 updateEditActions();
241 updatePasteAction();
242 updateViewActions();
243 updateGoActions();
244
245 emit urlChanged(url);
246 }
247
248 void DolphinMainWindow::slotTerminalDirectoryChanged(const QUrl& url)
249 {
250 if (m_tearDownFromPlacesRequested && url == QUrl::fromLocalFile(QDir::homePath())) {
251 m_placesPanel->proceedWithTearDown();
252 m_tearDownFromPlacesRequested = false;
253 }
254
255 m_activeViewContainer->setAutoGrabFocus(false);
256 changeUrl(url);
257 m_activeViewContainer->setAutoGrabFocus(true);
258 }
259
260 void DolphinMainWindow::slotEditableStateChanged(bool editable)
261 {
262 KToggleAction* editableLocationAction =
263 static_cast<KToggleAction*>(actionCollection()->action(QStringLiteral("editable_location")));
264 editableLocationAction->setChecked(editable);
265 }
266
267 void DolphinMainWindow::slotSelectionChanged(const KFileItemList& selection)
268 {
269 updateEditActions();
270
271 const int selectedUrlsCount = m_tabWidget->currentTabPage()->selectedItemsCount();
272
273 QAction* compareFilesAction = actionCollection()->action(QStringLiteral("compare_files"));
274 if (selectedUrlsCount == 2) {
275 compareFilesAction->setEnabled(isKompareInstalled());
276 } else {
277 compareFilesAction->setEnabled(false);
278 }
279
280 emit selectionChanged(selection);
281 }
282
283 void DolphinMainWindow::updateHistory()
284 {
285 const KUrlNavigator* urlNavigator = m_activeViewContainer->urlNavigator();
286 const int index = urlNavigator->historyIndex();
287
288 QAction* backAction = actionCollection()->action(KStandardAction::name(KStandardAction::Back));
289 if (backAction) {
290 backAction->setToolTip(i18nc("@info", "Go back"));
291 backAction->setEnabled(index < urlNavigator->historySize() - 1);
292 }
293
294 QAction* forwardAction = actionCollection()->action(KStandardAction::name(KStandardAction::Forward));
295 if (forwardAction) {
296 forwardAction->setToolTip(i18nc("@info", "Go forward"));
297 forwardAction->setEnabled(index > 0);
298 }
299 }
300
301 void DolphinMainWindow::updateFilterBarAction(bool show)
302 {
303 QAction* showFilterBarAction = actionCollection()->action(QStringLiteral("show_filter_bar"));
304 showFilterBarAction->setChecked(show);
305 }
306
307 void DolphinMainWindow::openNewMainWindow()
308 {
309 Dolphin::openNewWindow({m_activeViewContainer->url()}, this);
310 }
311
312 void DolphinMainWindow::openNewActivatedTab()
313 {
314 m_tabWidget->openNewActivatedTab();
315 }
316
317 void DolphinMainWindow::openNewTab(const QUrl& url)
318 {
319 m_tabWidget->openNewTab(url);
320 }
321
322 void DolphinMainWindow::openInNewTab()
323 {
324 const KFileItemList& list = m_activeViewContainer->view()->selectedItems();
325 bool tabCreated = false;
326
327 foreach (const KFileItem& item, list) {
328 const QUrl& url = DolphinView::openItemAsFolderUrl(item);
329 if (!url.isEmpty()) {
330 openNewTab(url);
331 tabCreated = true;
332 }
333 }
334
335 // if no new tab has been created from the selection
336 // open the current directory in a new tab
337 if (!tabCreated) {
338 openNewTab(m_activeViewContainer->url());
339 }
340 }
341
342 void DolphinMainWindow::openInNewWindow()
343 {
344 QUrl newWindowUrl;
345
346 const KFileItemList list = m_activeViewContainer->view()->selectedItems();
347 if (list.isEmpty()) {
348 newWindowUrl = m_activeViewContainer->url();
349 } else if (list.count() == 1) {
350 const KFileItem& item = list.first();
351 newWindowUrl = DolphinView::openItemAsFolderUrl(item);
352 }
353
354 if (!newWindowUrl.isEmpty()) {
355 Dolphin::openNewWindow({newWindowUrl}, this);
356 }
357 }
358
359 void DolphinMainWindow::showTarget()
360 {
361 const auto link = m_activeViewContainer->view()->selectedItems().at(0);
362 const auto linkLocationDir = QFileInfo(link.localPath()).absoluteDir();
363 auto linkDestination = link.linkDest();
364 if (QFileInfo(linkDestination).isRelative()) {
365 linkDestination = linkLocationDir.filePath(linkDestination);
366 }
367 if (QFileInfo::exists(linkDestination)) {
368 KIO::highlightInFileManager({QUrl::fromLocalFile(linkDestination).adjusted(QUrl::StripTrailingSlash)});
369 } else {
370 m_activeViewContainer->showMessage(xi18nc("@info", "Could not access <filename>%1</filename>.", linkDestination),
371 DolphinViewContainer::Warning);
372 }
373 }
374
375 void DolphinMainWindow::showEvent(QShowEvent* event)
376 {
377 KXmlGuiWindow::showEvent(event);
378
379 if (!event->spontaneous()) {
380 m_activeViewContainer->view()->setFocus();
381 }
382 }
383
384 void DolphinMainWindow::closeEvent(QCloseEvent* event)
385 {
386 // Find out if Dolphin is closed directly by the user or
387 // by the session manager because the session is closed
388 bool closedByUser = true;
389 if (qApp->isSavingSession()) {
390 closedByUser = false;
391 }
392
393 if (m_tabWidget->count() > 1 && GeneralSettings::confirmClosingMultipleTabs() && closedByUser) {
394 // Ask the user if he really wants to quit and close all tabs.
395 // Open a confirmation dialog with 3 buttons:
396 // QDialogButtonBox::Yes -> Quit
397 // QDialogButtonBox::No -> Close only the current tab
398 // QDialogButtonBox::Cancel -> do nothing
399 QDialog *dialog = new QDialog(this, Qt::Dialog);
400 dialog->setWindowTitle(i18nc("@title:window", "Confirmation"));
401 dialog->setModal(true);
402 QDialogButtonBox* buttons = new QDialogButtonBox(QDialogButtonBox::Yes | QDialogButtonBox::No | QDialogButtonBox::Cancel);
403 KGuiItem::assign(buttons->button(QDialogButtonBox::Yes), KGuiItem(i18nc("@action:button 'Quit Dolphin' button", "&Quit %1", QGuiApplication::applicationDisplayName()), QIcon::fromTheme(QStringLiteral("application-exit"))));
404 KGuiItem::assign(buttons->button(QDialogButtonBox::No), KGuiItem(i18n("C&lose Current Tab"), QIcon::fromTheme(QStringLiteral("tab-close"))));
405 KGuiItem::assign(buttons->button(QDialogButtonBox::Cancel), KStandardGuiItem::cancel());
406 buttons->button(QDialogButtonBox::Yes)->setDefault(true);
407
408 bool doNotAskAgainCheckboxResult = false;
409
410 const int result = KMessageBox::createKMessageBox(dialog,
411 buttons,
412 QMessageBox::Warning,
413 i18n("You have multiple tabs open in this window, are you sure you want to quit?"),
414 QStringList(),
415 i18n("Do not ask again"),
416 &doNotAskAgainCheckboxResult,
417 KMessageBox::Notify);
418
419 if (doNotAskAgainCheckboxResult) {
420 GeneralSettings::setConfirmClosingMultipleTabs(false);
421 }
422
423 switch (result) {
424 case QDialogButtonBox::Yes:
425 // Quit
426 break;
427 case QDialogButtonBox::No:
428 // Close only the current tab
429 m_tabWidget->closeTab();
430 Q_FALLTHROUGH();
431 default:
432 event->ignore();
433 return;
434 }
435 }
436
437 GeneralSettings::setVersion(CurrentDolphinVersion);
438 GeneralSettings::self()->save();
439
440 KXmlGuiWindow::closeEvent(event);
441 }
442
443 void DolphinMainWindow::saveProperties(KConfigGroup& group)
444 {
445 m_tabWidget->saveProperties(group);
446 }
447
448 void DolphinMainWindow::readProperties(const KConfigGroup& group)
449 {
450 m_tabWidget->readProperties(group);
451 }
452
453 void DolphinMainWindow::updateNewMenu()
454 {
455 m_newFileMenu->setViewShowsHiddenFiles(activeViewContainer()->view()->hiddenFilesShown());
456 m_newFileMenu->checkUpToDate();
457 m_newFileMenu->setPopupFiles(activeViewContainer()->url());
458
459 // If we're in the trash, also disable all the 'create new' items
460 // TODO: remove this once https://phabricator.kde.org/T8234 is implemented
461 slotWriteStateChanged(m_activeViewContainer->view()->url().scheme() != QLatin1String("trash"));
462 }
463
464 void DolphinMainWindow::createDirectory()
465 {
466 m_newFileMenu->setViewShowsHiddenFiles(activeViewContainer()->view()->hiddenFilesShown());
467 m_newFileMenu->setPopupFiles(activeViewContainer()->url());
468 m_newFileMenu->createDirectory();
469 }
470
471 void DolphinMainWindow::quit()
472 {
473 close();
474 }
475
476 void DolphinMainWindow::showErrorMessage(const QString& message)
477 {
478 m_activeViewContainer->showMessage(message, DolphinViewContainer::Error);
479 }
480
481 void DolphinMainWindow::slotUndoAvailable(bool available)
482 {
483 QAction* undoAction = actionCollection()->action(KStandardAction::name(KStandardAction::Undo));
484 if (undoAction) {
485 undoAction->setEnabled(available);
486 }
487 }
488
489 void DolphinMainWindow::slotUndoTextChanged(const QString& text)
490 {
491 QAction* undoAction = actionCollection()->action(KStandardAction::name(KStandardAction::Undo));
492 if (undoAction) {
493 undoAction->setText(text);
494 }
495 }
496
497 void DolphinMainWindow::undo()
498 {
499 clearStatusBar();
500 KIO::FileUndoManager::self()->uiInterface()->setParentWidget(this);
501 KIO::FileUndoManager::self()->undo();
502 }
503
504 void DolphinMainWindow::cut()
505 {
506 m_activeViewContainer->view()->cutSelectedItems();
507 }
508
509 void DolphinMainWindow::copy()
510 {
511 m_activeViewContainer->view()->copySelectedItems();
512 }
513
514 void DolphinMainWindow::paste()
515 {
516 m_activeViewContainer->view()->paste();
517 }
518
519 void DolphinMainWindow::find()
520 {
521 m_activeViewContainer->setSearchModeEnabled(true);
522 }
523
524 void DolphinMainWindow::updatePasteAction()
525 {
526 QAction* pasteAction = actionCollection()->action(KStandardAction::name(KStandardAction::Paste));
527 QPair<bool, QString> pasteInfo = m_activeViewContainer->view()->pasteInfo();
528 pasteAction->setEnabled(pasteInfo.first);
529 pasteAction->setText(pasteInfo.second);
530 }
531
532 void DolphinMainWindow::slotDirectoryLoadingCompleted()
533 {
534 updatePasteAction();
535 }
536
537 void DolphinMainWindow::slotToolBarActionMiddleClicked(QAction *action)
538 {
539 if (action == actionCollection()->action(KStandardAction::name(KStandardAction::Back))) {
540 goBackInNewTab();
541 } else if (action == actionCollection()->action(KStandardAction::name(KStandardAction::Forward))) {
542 goForwardInNewTab();
543 } else if (action == actionCollection()->action(QStringLiteral("go_up"))) {
544 goUpInNewTab();
545 } else if (action == actionCollection()->action(QStringLiteral("go_home"))) {
546 goHomeInNewTab();
547 }
548 }
549
550 void DolphinMainWindow::selectAll()
551 {
552 clearStatusBar();
553
554 // if the URL navigator is editable and focused, select the whole
555 // URL instead of all items of the view
556
557 KUrlNavigator* urlNavigator = m_activeViewContainer->urlNavigator();
558 QLineEdit* lineEdit = urlNavigator->editor()->lineEdit(); // krazy:exclude=qclasses
559 const bool selectUrl = urlNavigator->isUrlEditable() &&
560 lineEdit->hasFocus();
561 if (selectUrl) {
562 lineEdit->selectAll();
563 } else {
564 m_activeViewContainer->view()->selectAll();
565 }
566 }
567
568 void DolphinMainWindow::invertSelection()
569 {
570 clearStatusBar();
571 m_activeViewContainer->view()->invertSelection();
572 }
573
574 void DolphinMainWindow::toggleSplitView()
575 {
576 DolphinTabPage* tabPage = m_tabWidget->currentTabPage();
577 tabPage->setSplitViewEnabled(!tabPage->splitViewEnabled());
578
579 updateViewActions();
580 }
581
582 void DolphinMainWindow::toggleSplitStash()
583 {
584 DolphinTabPage* tabPage = m_tabWidget->currentTabPage();
585 tabPage->setSplitViewEnabled(false);
586 tabPage->setSplitViewEnabled(true, QUrl("stash:/"));
587 }
588
589 void DolphinMainWindow::reloadView()
590 {
591 clearStatusBar();
592 m_activeViewContainer->reload();
593 }
594
595 void DolphinMainWindow::stopLoading()
596 {
597 m_activeViewContainer->view()->stopLoading();
598 }
599
600 void DolphinMainWindow::enableStopAction()
601 {
602 actionCollection()->action(QStringLiteral("stop"))->setEnabled(true);
603 }
604
605 void DolphinMainWindow::disableStopAction()
606 {
607 actionCollection()->action(QStringLiteral("stop"))->setEnabled(false);
608 }
609
610 void DolphinMainWindow::showFilterBar()
611 {
612 m_activeViewContainer->setFilterBarVisible(true);
613 }
614
615 void DolphinMainWindow::toggleEditLocation()
616 {
617 clearStatusBar();
618
619 QAction* action = actionCollection()->action(QStringLiteral("editable_location"));
620 KUrlNavigator* urlNavigator = m_activeViewContainer->urlNavigator();
621 urlNavigator->setUrlEditable(action->isChecked());
622 }
623
624 void DolphinMainWindow::replaceLocation()
625 {
626 KUrlNavigator* navigator = m_activeViewContainer->urlNavigator();
627 navigator->setUrlEditable(true);
628 navigator->setFocus();
629
630 // select the whole text of the combo box editor
631 QLineEdit* lineEdit = navigator->editor()->lineEdit(); // krazy:exclude=qclasses
632 lineEdit->selectAll();
633 }
634
635 void DolphinMainWindow::togglePanelLockState()
636 {
637 const bool newLockState = !GeneralSettings::lockPanels();
638 foreach (QObject* child, children()) {
639 DolphinDockWidget* dock = qobject_cast<DolphinDockWidget*>(child);
640 if (dock) {
641 dock->setLocked(newLockState);
642 }
643 }
644
645 GeneralSettings::setLockPanels(newLockState);
646 }
647
648 void DolphinMainWindow::slotTerminalPanelVisibilityChanged()
649 {
650 if (m_terminalPanel->isHiddenInVisibleWindow()) {
651 m_activeViewContainer->view()->setFocus();
652 }
653 }
654
655 void DolphinMainWindow::goBack()
656 {
657 KUrlNavigator* urlNavigator = m_activeViewContainer->urlNavigator();
658 urlNavigator->goBack();
659
660 if (urlNavigator->locationState().isEmpty()) {
661 // An empty location state indicates a redirection URL,
662 // which must be skipped too
663 urlNavigator->goBack();
664 }
665 }
666
667 void DolphinMainWindow::goForward()
668 {
669 m_activeViewContainer->urlNavigator()->goForward();
670 }
671
672 void DolphinMainWindow::goUp()
673 {
674 m_activeViewContainer->urlNavigator()->goUp();
675 }
676
677 void DolphinMainWindow::goHome()
678 {
679 m_activeViewContainer->urlNavigator()->goHome();
680 }
681
682 void DolphinMainWindow::goBackInNewTab()
683 {
684 KUrlNavigator* urlNavigator = activeViewContainer()->urlNavigator();
685 const int index = urlNavigator->historyIndex() + 1;
686 openNewTab(urlNavigator->locationUrl(index));
687 }
688
689 void DolphinMainWindow::goForwardInNewTab()
690 {
691 KUrlNavigator* urlNavigator = activeViewContainer()->urlNavigator();
692 const int index = urlNavigator->historyIndex() - 1;
693 openNewTab(urlNavigator->locationUrl(index));
694 }
695
696 void DolphinMainWindow::goUpInNewTab()
697 {
698 const QUrl currentUrl = activeViewContainer()->urlNavigator()->locationUrl();
699 openNewTab(KIO::upUrl(currentUrl));
700 }
701
702 void DolphinMainWindow::goHomeInNewTab()
703 {
704 openNewTab(Dolphin::homeUrl());
705 }
706
707 void DolphinMainWindow::compareFiles()
708 {
709 const KFileItemList items = m_tabWidget->currentTabPage()->selectedItems();
710 if (items.count() != 2) {
711 // The action is disabled in this case, but it could have been triggered
712 // via D-Bus, see https://bugs.kde.org/show_bug.cgi?id=325517
713 return;
714 }
715
716 QUrl urlA = items.at(0).url();
717 QUrl urlB = items.at(1).url();
718
719 QString command(QStringLiteral("kompare -c \""));
720 command.append(urlA.toDisplayString(QUrl::PreferLocalFile));
721 command.append("\" \"");
722 command.append(urlB.toDisplayString(QUrl::PreferLocalFile));
723 command.append('\"');
724 KRun::runCommand(command, QStringLiteral("Kompare"), QStringLiteral("kompare"), this);
725 }
726
727 void DolphinMainWindow::toggleShowMenuBar()
728 {
729 const bool visible = menuBar()->isVisible();
730 menuBar()->setVisible(!visible);
731 if (visible) {
732 createControlButton();
733 } else {
734 deleteControlButton();
735 }
736 }
737
738 void DolphinMainWindow::openTerminal()
739 {
740 QString dir(QDir::homePath());
741
742 // If the given directory is not local, it can still be the URL of an
743 // ioslave using UDS_LOCAL_PATH which to be converted first.
744 KIO::StatJob* statJob = KIO::mostLocalUrl(m_activeViewContainer->url());
745 KJobWidgets::setWindow(statJob, this);
746 statJob->exec();
747 QUrl url = statJob->mostLocalUrl();
748
749 //If the URL is local after the above conversion, set the directory.
750 if (url.isLocalFile()) {
751 dir = url.toLocalFile();
752 }
753
754 KToolInvocation::invokeTerminal(QString(), dir);
755 }
756
757 void DolphinMainWindow::editSettings()
758 {
759 if (!m_settingsDialog) {
760 DolphinViewContainer* container = activeViewContainer();
761 container->view()->writeSettings();
762
763 const QUrl url = container->url();
764 DolphinSettingsDialog* settingsDialog = new DolphinSettingsDialog(url, this);
765 connect(settingsDialog, &DolphinSettingsDialog::settingsChanged, this, &DolphinMainWindow::refreshViews);
766 settingsDialog->setAttribute(Qt::WA_DeleteOnClose);
767 settingsDialog->show();
768 m_settingsDialog = settingsDialog;
769 } else {
770 m_settingsDialog.data()->raise();
771 }
772 }
773
774 void DolphinMainWindow::handleUrl(const QUrl& url)
775 {
776 delete m_lastHandleUrlStatJob;
777 m_lastHandleUrlStatJob = nullptr;
778
779 if (url.isLocalFile() && QFileInfo(url.toLocalFile()).isDir()) {
780 activeViewContainer()->setUrl(url);
781 } else if (KProtocolManager::supportsListing(url)) {
782 // stat the URL to see if it is a dir or not
783 m_lastHandleUrlStatJob = KIO::stat(url, KIO::HideProgressInfo);
784 if (m_lastHandleUrlStatJob->uiDelegate()) {
785 KJobWidgets::setWindow(m_lastHandleUrlStatJob, this);
786 }
787 connect(m_lastHandleUrlStatJob, &KIO::Job::result,
788 this, &DolphinMainWindow::slotHandleUrlStatFinished);
789
790 } else {
791 new KRun(url, this); // Automatically deletes itself after being finished
792 }
793 }
794
795 void DolphinMainWindow::slotHandleUrlStatFinished(KJob* job)
796 {
797 m_lastHandleUrlStatJob = nullptr;
798 const KIO::UDSEntry entry = static_cast<KIO::StatJob*>(job)->statResult();
799 const QUrl url = static_cast<KIO::StatJob*>(job)->url();
800 if (entry.isDir()) {
801 activeViewContainer()->setUrl(url);
802 } else {
803 new KRun(url, this); // Automatically deletes itself after being finished
804 }
805 }
806
807 void DolphinMainWindow::slotWriteStateChanged(bool isFolderWritable)
808 {
809 const auto actions = m_newFileMenu->menu()->actions();
810 for (auto menuItem : actions) {
811 menuItem->setEnabled(isFolderWritable);
812 }
813 }
814
815 void DolphinMainWindow::openContextMenu(const QPoint& pos,
816 const KFileItem& item,
817 const QUrl& url,
818 const QList<QAction*>& customActions)
819 {
820 QPointer<DolphinContextMenu> contextMenu = new DolphinContextMenu(this, pos, item, url);
821 contextMenu.data()->setCustomActions(customActions);
822 const DolphinContextMenu::Command command = contextMenu.data()->open();
823
824 switch (command) {
825 case DolphinContextMenu::OpenParentFolder:
826 changeUrl(KIO::upUrl(item.url()));
827 m_activeViewContainer->view()->markUrlsAsSelected({item.url()});
828 m_activeViewContainer->view()->markUrlAsCurrent(item.url());
829 break;
830
831 case DolphinContextMenu::OpenParentFolderInNewWindow:
832 Dolphin::openNewWindow({item.url()}, this, Dolphin::OpenNewWindowFlag::Select);
833 break;
834
835 case DolphinContextMenu::OpenParentFolderInNewTab:
836 openNewTab(KIO::upUrl(item.url()));
837 break;
838
839 case DolphinContextMenu::None:
840 default:
841 break;
842 }
843
844 // Delete the menu, unless it has been deleted in its own nested event loop already.
845 if (contextMenu) {
846 contextMenu->deleteLater();
847 }
848 }
849
850 void DolphinMainWindow::updateControlMenu()
851 {
852 QMenu* menu = qobject_cast<QMenu*>(sender());
853 Q_ASSERT(menu);
854
855 // All actions get cleared by QMenu::clear(). This includes the sub-menus
856 // because 'menu' is their parent.
857 menu->clear();
858
859 KActionCollection* ac = actionCollection();
860
861 // Add "Edit" actions
862 bool added = addActionToMenu(ac->action(KStandardAction::name(KStandardAction::Undo)), menu) |
863 addActionToMenu(ac->action(KStandardAction::name(KStandardAction::Find)), menu) |
864 addActionToMenu(ac->action(KStandardAction::name(KStandardAction::SelectAll)), menu) |
865 addActionToMenu(ac->action(QStringLiteral("invert_selection")), menu);
866
867 if (added) {
868 menu->addSeparator();
869 }
870
871 // Add "View" actions
872 if (!GeneralSettings::showZoomSlider()) {
873 addActionToMenu(ac->action(KStandardAction::name(KStandardAction::ZoomIn)), menu);
874 addActionToMenu(ac->action(KStandardAction::name(KStandardAction::ZoomOut)), menu);
875 menu->addSeparator();
876 }
877
878 added = addActionToMenu(ac->action(QStringLiteral("sort")), menu) |
879 addActionToMenu(ac->action(QStringLiteral("view_mode")), menu) |
880 addActionToMenu(ac->action(QStringLiteral("additional_info")), menu) |
881 addActionToMenu(ac->action(QStringLiteral("show_preview")), menu) |
882 addActionToMenu(ac->action(QStringLiteral("show_in_groups")), menu) |
883 addActionToMenu(ac->action(QStringLiteral("show_hidden_files")), menu);
884
885 if (added) {
886 menu->addSeparator();
887 }
888
889 added = addActionToMenu(ac->action(QStringLiteral("split_view")), menu) |
890 addActionToMenu(ac->action(KStandardAction::name(KStandardAction::Redisplay)), menu) |
891 addActionToMenu(ac->action(QStringLiteral("view_properties")), menu);
892 if (added) {
893 menu->addSeparator();
894 }
895
896 addActionToMenu(ac->action(QStringLiteral("panels")), menu);
897 QMenu* locationBarMenu = new QMenu(i18nc("@action:inmenu", "Location Bar"), menu);
898 locationBarMenu->addAction(ac->action(QStringLiteral("editable_location")));
899 locationBarMenu->addAction(ac->action(QStringLiteral("replace_location")));
900 menu->addMenu(locationBarMenu);
901
902 menu->addSeparator();
903
904 // Add "Go" menu
905 QMenu* goMenu = new QMenu(i18nc("@action:inmenu", "Go"), menu);
906 goMenu->addAction(ac->action(KStandardAction::name(KStandardAction::Back)));
907 goMenu->addAction(ac->action(KStandardAction::name(KStandardAction::Forward)));
908 goMenu->addAction(ac->action(KStandardAction::name(KStandardAction::Up)));
909 goMenu->addAction(ac->action(KStandardAction::name(KStandardAction::Home)));
910 goMenu->addAction(ac->action(QStringLiteral("closed_tabs")));
911 menu->addMenu(goMenu);
912
913 // Add "Tool" menu
914 QMenu* toolsMenu = new QMenu(i18nc("@action:inmenu", "Tools"), menu);
915 toolsMenu->addAction(ac->action(QStringLiteral("show_filter_bar")));
916 toolsMenu->addAction(ac->action(QStringLiteral("compare_files")));
917 toolsMenu->addAction(ac->action(QStringLiteral("open_terminal")));
918 toolsMenu->addAction(ac->action(QStringLiteral("change_remote_encoding")));
919 menu->addMenu(toolsMenu);
920
921 // Add "Settings" menu entries
922 addActionToMenu(ac->action(KStandardAction::name(KStandardAction::KeyBindings)), menu);
923 addActionToMenu(ac->action(KStandardAction::name(KStandardAction::ConfigureToolbars)), menu);
924 addActionToMenu(ac->action(KStandardAction::name(KStandardAction::Preferences)), menu);
925
926 // Add "Help" menu
927 auto helpMenu = new KHelpMenu(menu);
928 menu->addMenu(helpMenu->menu());
929
930 menu->addSeparator();
931 addActionToMenu(ac->action(KStandardAction::name(KStandardAction::ShowMenubar)), menu);
932 }
933
934 void DolphinMainWindow::updateToolBar()
935 {
936 if (!menuBar()->isVisible()) {
937 createControlButton();
938 }
939 }
940
941 void DolphinMainWindow::slotControlButtonDeleted()
942 {
943 m_controlButton = nullptr;
944 m_updateToolBarTimer->start();
945 }
946
947 void DolphinMainWindow::slotPlaceActivated(const QUrl& url)
948 {
949 DolphinViewContainer* view = activeViewContainer();
950
951 if (view->url() == url) {
952 // We can end up here if the user clicked a device in the Places Panel
953 // which had been unmounted earlier, see https://bugs.kde.org/show_bug.cgi?id=161385.
954 reloadView();
955 } else {
956 changeUrl(url);
957 }
958 }
959
960 void DolphinMainWindow::closedTabsCountChanged(unsigned int count)
961 {
962 actionCollection()->action(QStringLiteral("undo_close_tab"))->setEnabled(count > 0);
963 }
964
965 void DolphinMainWindow::activeViewChanged(DolphinViewContainer* viewContainer)
966 {
967 DolphinViewContainer* oldViewContainer = m_activeViewContainer;
968 Q_ASSERT(viewContainer);
969
970 m_activeViewContainer = viewContainer;
971
972 if (oldViewContainer) {
973 // Disconnect all signals between the old view container (container,
974 // view and url navigator) and main window.
975 oldViewContainer->disconnect(this);
976 oldViewContainer->view()->disconnect(this);
977 oldViewContainer->urlNavigator()->disconnect(this);
978 }
979
980 connectViewSignals(viewContainer);
981
982 m_actionHandler->setCurrentView(viewContainer->view());
983
984 updateHistory();
985 updateEditActions();
986 updatePasteAction();
987 updateViewActions();
988 updateGoActions();
989
990 const QUrl url = viewContainer->url();
991 emit urlChanged(url);
992 }
993
994 void DolphinMainWindow::tabCountChanged(int count)
995 {
996 const bool enableTabActions = (count > 1);
997 actionCollection()->action(KStandardAction::name(KStandardAction::Close))->setEnabled(enableTabActions);
998 actionCollection()->action(QStringLiteral("activate_next_tab"))->setEnabled(enableTabActions);
999 actionCollection()->action(QStringLiteral("activate_prev_tab"))->setEnabled(enableTabActions);
1000 }
1001
1002 void DolphinMainWindow::updateWindowTitle()
1003 {
1004 setWindowTitle(m_activeViewContainer->caption());
1005 }
1006
1007 void DolphinMainWindow::slotStorageTearDownFromPlacesRequested(const QString& mountPath)
1008 {
1009 if (m_terminalPanel->currentWorkingDirectory().startsWith(mountPath)) {
1010 m_tearDownFromPlacesRequested = true;
1011 m_terminalPanel->goHome();
1012 // m_placesPanel->proceedWithTearDown() will be called in slotTerminalDirectoryChanged
1013 } else {
1014 m_placesPanel->proceedWithTearDown();
1015 }
1016 }
1017
1018 void DolphinMainWindow::slotStorageTearDownExternallyRequested(const QString& mountPath)
1019 {
1020 if (m_terminalPanel->currentWorkingDirectory().startsWith(mountPath)) {
1021 m_tearDownFromPlacesRequested = false;
1022 m_terminalPanel->goHome();
1023 }
1024 }
1025
1026 void DolphinMainWindow::setupActions()
1027 {
1028 // setup 'File' menu
1029 m_newFileMenu = new DolphinNewFileMenu(actionCollection(), this);
1030 QMenu* menu = m_newFileMenu->menu();
1031 menu->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New"));
1032 menu->setIcon(QIcon::fromTheme(QStringLiteral("document-new")));
1033 m_newFileMenu->setDelayed(false);
1034 connect(menu, &QMenu::aboutToShow,
1035 this, &DolphinMainWindow::updateNewMenu);
1036
1037 QAction* newWindow = KStandardAction::openNew(this, &DolphinMainWindow::openNewMainWindow, actionCollection());
1038 newWindow->setText(i18nc("@action:inmenu File", "New &Window"));
1039
1040 QAction* newTab = actionCollection()->addAction(QStringLiteral("new_tab"));
1041 newTab->setIcon(QIcon::fromTheme(QStringLiteral("tab-new")));
1042 newTab->setText(i18nc("@action:inmenu File", "New Tab"));
1043 actionCollection()->setDefaultShortcuts(newTab, {Qt::CTRL + Qt::Key_T, QKeySequence::AddTab});
1044 connect(newTab, &QAction::triggered, this, static_cast<void(DolphinMainWindow::*)()>(&DolphinMainWindow::openNewActivatedTab));
1045
1046 QAction* closeTab = KStandardAction::close(
1047 m_tabWidget, static_cast<void(DolphinTabWidget::*)()>(&DolphinTabWidget::closeTab), actionCollection());
1048 closeTab->setText(i18nc("@action:inmenu File", "Close Tab"));
1049 closeTab->setEnabled(false);
1050
1051 KStandardAction::quit(this, &DolphinMainWindow::quit, actionCollection());
1052
1053 // setup 'Edit' menu
1054 KStandardAction::undo(this,
1055 &DolphinMainWindow::undo,
1056 actionCollection());
1057
1058
1059 KStandardAction::cut(this, &DolphinMainWindow::cut, actionCollection());
1060 KStandardAction::copy(this, &DolphinMainWindow::copy, actionCollection());
1061 QAction* paste = KStandardAction::paste(this, &DolphinMainWindow::paste, actionCollection());
1062 // The text of the paste-action is modified dynamically by Dolphin
1063 // (e. g. to "Paste One Folder"). To prevent that the size of the toolbar changes
1064 // due to the long text, the text "Paste" is used:
1065 paste->setIconText(i18nc("@action:inmenu Edit", "Paste"));
1066
1067 KStandardAction::find(this, &DolphinMainWindow::find, actionCollection());
1068
1069 KStandardAction::selectAll(this, &DolphinMainWindow::selectAll, actionCollection());
1070
1071 QAction* invertSelection = actionCollection()->addAction(QStringLiteral("invert_selection"));
1072 invertSelection->setText(i18nc("@action:inmenu Edit", "Invert Selection"));
1073 invertSelection->setIcon(QIcon::fromTheme(QStringLiteral("edit-select-invert")));
1074 actionCollection()->setDefaultShortcut(invertSelection, Qt::CTRL + Qt::SHIFT + Qt::Key_A);
1075 connect(invertSelection, &QAction::triggered, this, &DolphinMainWindow::invertSelection);
1076
1077 // setup 'View' menu
1078 // (note that most of it is set up in DolphinViewActionHandler)
1079
1080 QAction* split = actionCollection()->addAction(QStringLiteral("split_view"));
1081 actionCollection()->setDefaultShortcut(split, Qt::Key_F3);
1082 connect(split, &QAction::triggered, this, &DolphinMainWindow::toggleSplitView);
1083
1084 QAction* stashSplit = actionCollection()->addAction(QStringLiteral("split_stash"));
1085 actionCollection()->setDefaultShortcut(stashSplit, Qt::CTRL + Qt::Key_S);
1086 stashSplit->setText(i18nc("@action:intoolbar Stash", "Stash"));
1087 stashSplit->setToolTip(i18nc("@info", "Opens the stash virtual directory in a split window"));
1088 stashSplit->setIcon(QIcon::fromTheme(QStringLiteral("folder-stash")));
1089 stashSplit->setCheckable(false);
1090 stashSplit->setVisible(KProtocolInfo::isKnownProtocol("stash"));
1091 connect(stashSplit, &QAction::triggered, this, &DolphinMainWindow::toggleSplitStash);
1092
1093 KStandardAction::redisplay(this, &DolphinMainWindow::reloadView, actionCollection());
1094
1095 QAction* stop = actionCollection()->addAction(QStringLiteral("stop"));
1096 stop->setText(i18nc("@action:inmenu View", "Stop"));
1097 stop->setToolTip(i18nc("@info", "Stop loading"));
1098 stop->setIcon(QIcon::fromTheme(QStringLiteral("process-stop")));
1099 connect(stop, &QAction::triggered, this, &DolphinMainWindow::stopLoading);
1100
1101 KToggleAction* editableLocation = actionCollection()->add<KToggleAction>(QStringLiteral("editable_location"));
1102 editableLocation->setText(i18nc("@action:inmenu Navigation Bar", "Editable Location"));
1103 actionCollection()->setDefaultShortcut(editableLocation, Qt::Key_F6);
1104 connect(editableLocation, &KToggleAction::triggered, this, &DolphinMainWindow::toggleEditLocation);
1105
1106 QAction* replaceLocation = actionCollection()->addAction(QStringLiteral("replace_location"));
1107 replaceLocation->setText(i18nc("@action:inmenu Navigation Bar", "Replace Location"));
1108 actionCollection()->setDefaultShortcut(replaceLocation, Qt::CTRL + Qt::Key_L);
1109 connect(replaceLocation, &QAction::triggered, this, &DolphinMainWindow::replaceLocation);
1110
1111 // setup 'Go' menu
1112 QAction* backAction = KStandardAction::back(this, &DolphinMainWindow::goBack, actionCollection());
1113 auto backShortcuts = backAction->shortcuts();
1114 backShortcuts.append(QKeySequence(Qt::Key_Backspace));
1115 actionCollection()->setDefaultShortcuts(backAction, backShortcuts);
1116
1117 DolphinRecentTabsMenu* recentTabsMenu = new DolphinRecentTabsMenu(this);
1118 actionCollection()->addAction(QStringLiteral("closed_tabs"), recentTabsMenu);
1119 connect(m_tabWidget, &DolphinTabWidget::rememberClosedTab,
1120 recentTabsMenu, &DolphinRecentTabsMenu::rememberClosedTab);
1121 connect(recentTabsMenu, &DolphinRecentTabsMenu::restoreClosedTab,
1122 m_tabWidget, &DolphinTabWidget::restoreClosedTab);
1123 connect(recentTabsMenu, &DolphinRecentTabsMenu::closedTabsCountChanged,
1124 this, &DolphinMainWindow::closedTabsCountChanged);
1125
1126 QAction* undoCloseTab = actionCollection()->addAction(QStringLiteral("undo_close_tab"));
1127 undoCloseTab->setText(i18nc("@action:inmenu File", "Undo close tab"));
1128 actionCollection()->setDefaultShortcut(undoCloseTab, Qt::CTRL + Qt::SHIFT + Qt::Key_T);
1129 undoCloseTab->setIcon(QIcon::fromTheme(QStringLiteral("edit-undo")));
1130 undoCloseTab->setEnabled(false);
1131 connect(undoCloseTab, &QAction::triggered, recentTabsMenu, &DolphinRecentTabsMenu::undoCloseTab);
1132
1133 auto undoAction = actionCollection()->action(KStandardAction::name(KStandardAction::Undo));
1134 undoAction->setEnabled(false); // undo should be disabled by default
1135
1136 KStandardAction::forward(this, &DolphinMainWindow::goForward, actionCollection());
1137 KStandardAction::up(this, &DolphinMainWindow::goUp, actionCollection());
1138 KStandardAction::home(this, &DolphinMainWindow::goHome, actionCollection());
1139
1140 // setup 'Tools' menu
1141 QAction* showFilterBar = actionCollection()->addAction(QStringLiteral("show_filter_bar"));
1142 showFilterBar->setText(i18nc("@action:inmenu Tools", "Show Filter Bar"));
1143 showFilterBar->setIcon(QIcon::fromTheme(QStringLiteral("view-filter")));
1144 actionCollection()->setDefaultShortcuts(showFilterBar, {Qt::CTRL + Qt::Key_I, Qt::Key_Slash});
1145 connect(showFilterBar, &QAction::triggered, this, &DolphinMainWindow::showFilterBar);
1146
1147 QAction* compareFiles = actionCollection()->addAction(QStringLiteral("compare_files"));
1148 compareFiles->setText(i18nc("@action:inmenu Tools", "Compare Files"));
1149 compareFiles->setIcon(QIcon::fromTheme(QStringLiteral("kompare")));
1150 compareFiles->setEnabled(false);
1151 connect(compareFiles, &QAction::triggered, this, &DolphinMainWindow::compareFiles);
1152
1153 #ifndef Q_OS_WIN
1154 if (KAuthorized::authorize(QStringLiteral("shell_access"))) {
1155 QAction* openTerminal = actionCollection()->addAction(QStringLiteral("open_terminal"));
1156 openTerminal->setText(i18nc("@action:inmenu Tools", "Open Terminal"));
1157 openTerminal->setIcon(QIcon::fromTheme(QStringLiteral("utilities-terminal")));
1158 actionCollection()->setDefaultShortcut(openTerminal, Qt::SHIFT + Qt::Key_F4);
1159 connect(openTerminal, &QAction::triggered, this, &DolphinMainWindow::openTerminal);
1160 }
1161 #endif
1162
1163 // setup 'Settings' menu
1164 KToggleAction* showMenuBar = KStandardAction::showMenubar(nullptr, nullptr, actionCollection());
1165 connect(showMenuBar, &KToggleAction::triggered, // Fixes #286822
1166 this, &DolphinMainWindow::toggleShowMenuBar, Qt::QueuedConnection);
1167 KStandardAction::preferences(this, &DolphinMainWindow::editSettings, actionCollection());
1168
1169 // not in menu actions
1170 QList<QKeySequence> nextTabKeys = KStandardShortcut::tabNext();
1171 nextTabKeys.append(QKeySequence(Qt::CTRL + Qt::Key_Tab));
1172
1173 QList<QKeySequence> prevTabKeys = KStandardShortcut::tabPrev();
1174 prevTabKeys.append(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_Tab));
1175
1176 QAction* activateNextTab = actionCollection()->addAction(QStringLiteral("activate_next_tab"));
1177 activateNextTab->setIconText(i18nc("@action:inmenu", "Next Tab"));
1178 activateNextTab->setText(i18nc("@action:inmenu", "Activate Next Tab"));
1179 activateNextTab->setEnabled(false);
1180 connect(activateNextTab, &QAction::triggered, m_tabWidget, &DolphinTabWidget::activateNextTab);
1181 actionCollection()->setDefaultShortcuts(activateNextTab, nextTabKeys);
1182
1183 QAction* activatePrevTab = actionCollection()->addAction(QStringLiteral("activate_prev_tab"));
1184 activatePrevTab->setIconText(i18nc("@action:inmenu", "Previous Tab"));
1185 activatePrevTab->setText(i18nc("@action:inmenu", "Activate Previous Tab"));
1186 activatePrevTab->setEnabled(false);
1187 connect(activatePrevTab, &QAction::triggered, m_tabWidget, &DolphinTabWidget::activatePrevTab);
1188 actionCollection()->setDefaultShortcuts(activatePrevTab, prevTabKeys);
1189
1190 // for context menu
1191 QAction* showTarget = actionCollection()->addAction(QStringLiteral("show_target"));
1192 showTarget->setText(i18nc("@action:inmenu", "Show Target"));
1193 showTarget->setIcon(QIcon::fromTheme(QStringLiteral("document-open-folder")));
1194 showTarget->setEnabled(false);
1195 connect(showTarget, &QAction::triggered, this, &DolphinMainWindow::showTarget);
1196
1197 QAction* openInNewTab = actionCollection()->addAction(QStringLiteral("open_in_new_tab"));
1198 openInNewTab->setText(i18nc("@action:inmenu", "Open in New Tab"));
1199 openInNewTab->setIcon(QIcon::fromTheme(QStringLiteral("tab-new")));
1200 connect(openInNewTab, &QAction::triggered, this, &DolphinMainWindow::openInNewTab);
1201
1202 QAction* openInNewTabs = actionCollection()->addAction(QStringLiteral("open_in_new_tabs"));
1203 openInNewTabs->setText(i18nc("@action:inmenu", "Open in New Tabs"));
1204 openInNewTabs->setIcon(QIcon::fromTheme(QStringLiteral("tab-new")));
1205 connect(openInNewTabs, &QAction::triggered, this, &DolphinMainWindow::openInNewTab);
1206
1207 QAction* openInNewWindow = actionCollection()->addAction(QStringLiteral("open_in_new_window"));
1208 openInNewWindow->setText(i18nc("@action:inmenu", "Open in New Window"));
1209 openInNewWindow->setIcon(QIcon::fromTheme(QStringLiteral("window-new")));
1210 connect(openInNewWindow, &QAction::triggered, this, &DolphinMainWindow::openInNewWindow);
1211 }
1212
1213 void DolphinMainWindow::setupDockWidgets()
1214 {
1215 const bool lock = GeneralSettings::lockPanels();
1216
1217 KDualAction* lockLayoutAction = actionCollection()->add<KDualAction>(QStringLiteral("lock_panels"));
1218 lockLayoutAction->setActiveText(i18nc("@action:inmenu Panels", "Unlock Panels"));
1219 lockLayoutAction->setActiveIcon(QIcon::fromTheme(QStringLiteral("object-unlocked")));
1220 lockLayoutAction->setInactiveText(i18nc("@action:inmenu Panels", "Lock Panels"));
1221 lockLayoutAction->setInactiveIcon(QIcon::fromTheme(QStringLiteral("object-locked")));
1222 lockLayoutAction->setActive(lock);
1223 connect(lockLayoutAction, &KDualAction::triggered, this, &DolphinMainWindow::togglePanelLockState);
1224
1225 // Setup "Information"
1226 DolphinDockWidget* infoDock = new DolphinDockWidget(i18nc("@title:window", "Information"));
1227 infoDock->setLocked(lock);
1228 infoDock->setObjectName(QStringLiteral("infoDock"));
1229 infoDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
1230
1231 #ifdef HAVE_BALOO
1232 InformationPanel* infoPanel = new InformationPanel(infoDock);
1233 infoPanel->setCustomContextMenuActions({lockLayoutAction});
1234 connect(infoPanel, &InformationPanel::urlActivated, this, &DolphinMainWindow::handleUrl);
1235 infoDock->setWidget(infoPanel);
1236
1237 QAction* infoAction = infoDock->toggleViewAction();
1238 createPanelAction(QIcon::fromTheme(QStringLiteral("dialog-information")), Qt::Key_F11, infoAction, QStringLiteral("show_information_panel"));
1239
1240 addDockWidget(Qt::RightDockWidgetArea, infoDock);
1241 connect(this, &DolphinMainWindow::urlChanged,
1242 infoPanel, &InformationPanel::setUrl);
1243 connect(this, &DolphinMainWindow::selectionChanged,
1244 infoPanel, &InformationPanel::setSelection);
1245 connect(this, &DolphinMainWindow::requestItemInfo,
1246 infoPanel, &InformationPanel::requestDelayedItemInfo);
1247 #endif
1248
1249 // Setup "Folders"
1250 DolphinDockWidget* foldersDock = new DolphinDockWidget(i18nc("@title:window", "Folders"));
1251 foldersDock->setLocked(lock);
1252 foldersDock->setObjectName(QStringLiteral("foldersDock"));
1253 foldersDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
1254 FoldersPanel* foldersPanel = new FoldersPanel(foldersDock);
1255 foldersPanel->setCustomContextMenuActions({lockLayoutAction});
1256 foldersDock->setWidget(foldersPanel);
1257
1258 QAction* foldersAction = foldersDock->toggleViewAction();
1259 createPanelAction(QIcon::fromTheme(QStringLiteral("folder")), Qt::Key_F7, foldersAction, QStringLiteral("show_folders_panel"));
1260
1261 addDockWidget(Qt::LeftDockWidgetArea, foldersDock);
1262 connect(this, &DolphinMainWindow::urlChanged,
1263 foldersPanel, &FoldersPanel::setUrl);
1264 connect(foldersPanel, &FoldersPanel::folderActivated,
1265 this, &DolphinMainWindow::changeUrl);
1266 connect(foldersPanel, &FoldersPanel::folderMiddleClicked,
1267 this, &DolphinMainWindow::openNewTab);
1268 connect(foldersPanel, &FoldersPanel::errorMessage,
1269 this, &DolphinMainWindow::showErrorMessage);
1270
1271 // Setup "Terminal"
1272 #ifndef Q_OS_WIN
1273 if (KAuthorized::authorize(QStringLiteral("shell_access"))) {
1274 DolphinDockWidget* terminalDock = new DolphinDockWidget(i18nc("@title:window Shell terminal", "Terminal"));
1275 terminalDock->setLocked(lock);
1276 terminalDock->setObjectName(QStringLiteral("terminalDock"));
1277 m_terminalPanel = new TerminalPanel(terminalDock);
1278 m_terminalPanel->setCustomContextMenuActions({lockLayoutAction});
1279 terminalDock->setWidget(m_terminalPanel);
1280
1281 connect(m_terminalPanel, &TerminalPanel::hideTerminalPanel, terminalDock, &DolphinDockWidget::hide);
1282 connect(m_terminalPanel, &TerminalPanel::changeUrl, this, &DolphinMainWindow::slotTerminalDirectoryChanged);
1283 connect(terminalDock, &DolphinDockWidget::visibilityChanged,
1284 m_terminalPanel, &TerminalPanel::dockVisibilityChanged);
1285 connect(terminalDock, &DolphinDockWidget::visibilityChanged,
1286 this, &DolphinMainWindow::slotTerminalPanelVisibilityChanged);
1287
1288 QAction* terminalAction = terminalDock->toggleViewAction();
1289 createPanelAction(QIcon::fromTheme(QStringLiteral("utilities-terminal")), Qt::Key_F4, terminalAction, QStringLiteral("show_terminal_panel"));
1290
1291 addDockWidget(Qt::BottomDockWidgetArea, terminalDock);
1292 connect(this, &DolphinMainWindow::urlChanged,
1293 m_terminalPanel, &TerminalPanel::setUrl);
1294
1295 if (GeneralSettings::version() < 200) {
1296 terminalDock->hide();
1297 }
1298 }
1299 #endif
1300
1301 if (GeneralSettings::version() < 200) {
1302 infoDock->hide();
1303 foldersDock->hide();
1304 }
1305
1306 // Setup "Places"
1307 DolphinDockWidget* placesDock = new DolphinDockWidget(i18nc("@title:window", "Places"));
1308 placesDock->setLocked(lock);
1309 placesDock->setObjectName(QStringLiteral("placesDock"));
1310 placesDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
1311
1312 m_placesPanel = new PlacesPanel(placesDock);
1313 m_placesPanel->setCustomContextMenuActions({lockLayoutAction});
1314 placesDock->setWidget(m_placesPanel);
1315
1316 QAction *placesAction = placesDock->toggleViewAction();
1317 createPanelAction(QIcon::fromTheme(QStringLiteral("bookmarks")), Qt::Key_F9, placesAction, QStringLiteral("show_places_panel"));
1318
1319 addDockWidget(Qt::LeftDockWidgetArea, placesDock);
1320 connect(m_placesPanel, &PlacesPanel::placeActivated,
1321 this, &DolphinMainWindow::slotPlaceActivated);
1322 connect(m_placesPanel, &PlacesPanel::placeMiddleClicked,
1323 this, &DolphinMainWindow::openNewTab);
1324 connect(m_placesPanel, &PlacesPanel::errorMessage,
1325 this, &DolphinMainWindow::showErrorMessage);
1326 connect(this, &DolphinMainWindow::urlChanged,
1327 m_placesPanel, &PlacesPanel::setUrl);
1328 connect(placesDock, &DolphinDockWidget::visibilityChanged,
1329 m_tabWidget, &DolphinTabWidget::slotPlacesPanelVisibilityChanged);
1330 connect(this, &DolphinMainWindow::settingsChanged,
1331 m_placesPanel, &PlacesPanel::readSettings);
1332 connect(m_placesPanel, &PlacesPanel::storageTearDownRequested,
1333 this, &DolphinMainWindow::slotStorageTearDownFromPlacesRequested);
1334 connect(m_placesPanel, &PlacesPanel::storageTearDownExternallyRequested,
1335 this, &DolphinMainWindow::slotStorageTearDownExternallyRequested);
1336 m_tabWidget->slotPlacesPanelVisibilityChanged(m_placesPanel->isVisible());
1337
1338 // Add actions into the "Panels" menu
1339 KActionMenu* panelsMenu = new KActionMenu(i18nc("@action:inmenu View", "Panels"), this);
1340 actionCollection()->addAction(QStringLiteral("panels"), panelsMenu);
1341 panelsMenu->setDelayed(false);
1342 const KActionCollection* ac = actionCollection();
1343 panelsMenu->addAction(ac->action(QStringLiteral("show_places_panel")));
1344 #ifdef HAVE_BALOO
1345 panelsMenu->addAction(ac->action(QStringLiteral("show_information_panel")));
1346 #endif
1347 panelsMenu->addAction(ac->action(QStringLiteral("show_folders_panel")));
1348 panelsMenu->addAction(ac->action(QStringLiteral("show_terminal_panel")));
1349 panelsMenu->addSeparator();
1350 panelsMenu->addAction(lockLayoutAction);
1351 }
1352
1353 void DolphinMainWindow::updateEditActions()
1354 {
1355 const KFileItemList list = m_activeViewContainer->view()->selectedItems();
1356 if (list.isEmpty()) {
1357 stateChanged(QStringLiteral("has_no_selection"));
1358 } else {
1359 stateChanged(QStringLiteral("has_selection"));
1360
1361 KActionCollection* col = actionCollection();
1362 QAction* renameAction = col->action(KStandardAction::name(KStandardAction::RenameFile));
1363 QAction* moveToTrashAction = col->action(KStandardAction::name(KStandardAction::MoveToTrash));
1364 QAction* deleteAction = col->action(KStandardAction::name(KStandardAction::DeleteFile));
1365 QAction* cutAction = col->action(KStandardAction::name(KStandardAction::Cut));
1366 QAction* deleteWithTrashShortcut = col->action(QStringLiteral("delete_shortcut")); // see DolphinViewActionHandler
1367 QAction* showTarget = col->action(QStringLiteral("show_target"));
1368
1369 KFileItemListProperties capabilities(list);
1370 const bool enableMoveToTrash = capabilities.isLocal() && capabilities.supportsMoving();
1371
1372 renameAction->setEnabled(capabilities.supportsMoving());
1373 moveToTrashAction->setEnabled(enableMoveToTrash);
1374 deleteAction->setEnabled(capabilities.supportsDeleting());
1375 deleteWithTrashShortcut->setEnabled(capabilities.supportsDeleting() && !enableMoveToTrash);
1376 cutAction->setEnabled(capabilities.supportsMoving());
1377 showTarget->setEnabled(list.length() == 1 && list.at(0).isLink());
1378 }
1379 }
1380
1381 void DolphinMainWindow::updateViewActions()
1382 {
1383 m_actionHandler->updateViewActions();
1384
1385 QAction* showFilterBarAction = actionCollection()->action(QStringLiteral("show_filter_bar"));
1386 showFilterBarAction->setChecked(m_activeViewContainer->isFilterBarVisible());
1387
1388 updateSplitAction();
1389
1390 QAction* editableLocactionAction = actionCollection()->action(QStringLiteral("editable_location"));
1391 const KUrlNavigator* urlNavigator = m_activeViewContainer->urlNavigator();
1392 editableLocactionAction->setChecked(urlNavigator->isUrlEditable());
1393 }
1394
1395 void DolphinMainWindow::updateGoActions()
1396 {
1397 QAction* goUpAction = actionCollection()->action(KStandardAction::name(KStandardAction::Up));
1398 const QUrl currentUrl = m_activeViewContainer->url();
1399 goUpAction->setEnabled(KIO::upUrl(currentUrl) != currentUrl);
1400 }
1401
1402 void DolphinMainWindow::createControlButton()
1403 {
1404 if (m_controlButton) {
1405 return;
1406 }
1407 Q_ASSERT(!m_controlButton);
1408
1409 m_controlButton = new QToolButton(this);
1410 m_controlButton->setIcon(QIcon::fromTheme(QStringLiteral("application-menu")));
1411 m_controlButton->setText(i18nc("@action", "Control"));
1412 m_controlButton->setPopupMode(QToolButton::InstantPopup);
1413 m_controlButton->setToolButtonStyle(toolBar()->toolButtonStyle());
1414
1415 QMenu* controlMenu = new QMenu(m_controlButton);
1416 connect(controlMenu, &QMenu::aboutToShow, this, &DolphinMainWindow::updateControlMenu);
1417
1418 m_controlButton->setMenu(controlMenu);
1419
1420 toolBar()->addWidget(m_controlButton);
1421 connect(toolBar(), &KToolBar::iconSizeChanged,
1422 m_controlButton, &QToolButton::setIconSize);
1423 connect(toolBar(), &KToolBar::toolButtonStyleChanged,
1424 m_controlButton, &QToolButton::setToolButtonStyle);
1425
1426 // The added widgets are owned by the toolbar and may get deleted when e.g. the toolbar
1427 // gets edited. In this case we must add them again. The adding is done asynchronously by
1428 // m_updateToolBarTimer.
1429 connect(m_controlButton, &QToolButton::destroyed, this, &DolphinMainWindow::slotControlButtonDeleted);
1430 m_updateToolBarTimer = new QTimer(this);
1431 m_updateToolBarTimer->setInterval(500);
1432 connect(m_updateToolBarTimer, &QTimer::timeout, this, &DolphinMainWindow::updateToolBar);
1433 }
1434
1435 void DolphinMainWindow::deleteControlButton()
1436 {
1437 delete m_controlButton;
1438 m_controlButton = nullptr;
1439
1440 delete m_updateToolBarTimer;
1441 m_updateToolBarTimer = nullptr;
1442 }
1443
1444 bool DolphinMainWindow::addActionToMenu(QAction* action, QMenu* menu)
1445 {
1446 Q_ASSERT(action);
1447 Q_ASSERT(menu);
1448
1449 const KToolBar* toolBarWidget = toolBar();
1450 foreach (const QWidget* widget, action->associatedWidgets()) {
1451 if (widget == toolBarWidget) {
1452 return false;
1453 }
1454 }
1455
1456 menu->addAction(action);
1457 return true;
1458 }
1459
1460 void DolphinMainWindow::refreshViews()
1461 {
1462 m_tabWidget->refreshViews();
1463
1464 if (GeneralSettings::modifiedStartupSettings()) {
1465 // The startup settings have been changed by the user (see bug #254947).
1466 // Synchronize the split-view setting with the active view:
1467 const bool splitView = GeneralSettings::splitView();
1468 m_tabWidget->currentTabPage()->setSplitViewEnabled(splitView);
1469 updateSplitAction();
1470 updateWindowTitle();
1471 }
1472
1473 emit settingsChanged();
1474 }
1475
1476 void DolphinMainWindow::clearStatusBar()
1477 {
1478 m_activeViewContainer->statusBar()->resetToDefaultText();
1479 }
1480
1481 void DolphinMainWindow::connectViewSignals(DolphinViewContainer* container)
1482 {
1483 connect(container, &DolphinViewContainer::showFilterBarChanged,
1484 this, &DolphinMainWindow::updateFilterBarAction);
1485 connect(container, &DolphinViewContainer::writeStateChanged,
1486 this, &DolphinMainWindow::slotWriteStateChanged);
1487
1488 const DolphinView* view = container->view();
1489 connect(view, &DolphinView::selectionChanged,
1490 this, &DolphinMainWindow::slotSelectionChanged);
1491 connect(view, &DolphinView::requestItemInfo,
1492 this, &DolphinMainWindow::requestItemInfo);
1493 connect(view, &DolphinView::tabRequested,
1494 this, &DolphinMainWindow::openNewTab);
1495 connect(view, &DolphinView::requestContextMenu,
1496 this, &DolphinMainWindow::openContextMenu);
1497 connect(view, &DolphinView::directoryLoadingStarted,
1498 this, &DolphinMainWindow::enableStopAction);
1499 connect(view, &DolphinView::directoryLoadingCompleted,
1500 this, &DolphinMainWindow::disableStopAction);
1501 connect(view, &DolphinView::directoryLoadingCompleted,
1502 this, &DolphinMainWindow::slotDirectoryLoadingCompleted);
1503 connect(view, &DolphinView::goBackRequested,
1504 this, static_cast<void(DolphinMainWindow::*)()>(&DolphinMainWindow::goBack));
1505 connect(view, &DolphinView::goForwardRequested,
1506 this, static_cast<void(DolphinMainWindow::*)()>(&DolphinMainWindow::goForward));
1507 connect(view, &DolphinView::urlActivated,
1508 this, &DolphinMainWindow::handleUrl);
1509
1510 const KUrlNavigator* navigator = container->urlNavigator();
1511 connect(navigator, &KUrlNavigator::urlChanged,
1512 this, &DolphinMainWindow::changeUrl);
1513 connect(navigator, &KUrlNavigator::historyChanged,
1514 this, &DolphinMainWindow::updateHistory);
1515 connect(navigator, &KUrlNavigator::editableStateChanged,
1516 this, &DolphinMainWindow::slotEditableStateChanged);
1517 connect(navigator, &KUrlNavigator::tabRequested,
1518 this, &DolphinMainWindow::openNewTab);
1519 }
1520
1521 void DolphinMainWindow::updateSplitAction()
1522 {
1523 QAction* splitAction = actionCollection()->action(QStringLiteral("split_view"));
1524 const DolphinTabPage* tabPage = m_tabWidget->currentTabPage();
1525 if (tabPage->splitViewEnabled()) {
1526 if (tabPage->primaryViewActive()) {
1527 splitAction->setText(i18nc("@action:intoolbar Close left view", "Close"));
1528 splitAction->setToolTip(i18nc("@info", "Close left view"));
1529 splitAction->setIcon(QIcon::fromTheme(QStringLiteral("view-left-close")));
1530 } else {
1531 splitAction->setText(i18nc("@action:intoolbar Close right view", "Close"));
1532 splitAction->setToolTip(i18nc("@info", "Close right view"));
1533 splitAction->setIcon(QIcon::fromTheme(QStringLiteral("view-right-close")));
1534 }
1535 } else {
1536 splitAction->setText(i18nc("@action:intoolbar Split view", "Split"));
1537 splitAction->setToolTip(i18nc("@info", "Split view"));
1538 splitAction->setIcon(QIcon::fromTheme(QStringLiteral("view-right-new")));
1539 }
1540 }
1541
1542 bool DolphinMainWindow::isKompareInstalled() const
1543 {
1544 static bool initialized = false;
1545 static bool installed = false;
1546 if (!initialized) {
1547 // TODO: maybe replace this approach later by using a menu
1548 // plugin like kdiff3plugin.cpp
1549 installed = !QStandardPaths::findExecutable(QStringLiteral("kompare")).isEmpty();
1550 initialized = true;
1551 }
1552 return installed;
1553 }
1554
1555 void DolphinMainWindow::createPanelAction(const QIcon& icon,
1556 const QKeySequence& shortcut,
1557 QAction* dockAction,
1558 const QString& actionName)
1559 {
1560 QAction* panelAction = actionCollection()->addAction(actionName);
1561 panelAction->setCheckable(true);
1562 panelAction->setChecked(dockAction->isChecked());
1563 panelAction->setText(dockAction->text());
1564 panelAction->setIcon(icon);
1565 actionCollection()->setDefaultShortcut(panelAction, shortcut);
1566
1567 connect(panelAction, &QAction::triggered, dockAction, &QAction::trigger);
1568 connect(dockAction, &QAction::toggled, panelAction, &QAction::setChecked);
1569 }
1570
1571 DolphinMainWindow::UndoUiInterface::UndoUiInterface() :
1572 KIO::FileUndoManager::UiInterface()
1573 {
1574 }
1575
1576 DolphinMainWindow::UndoUiInterface::~UndoUiInterface()
1577 {
1578 }
1579
1580 void DolphinMainWindow::UndoUiInterface::jobError(KIO::Job* job)
1581 {
1582 DolphinMainWindow* mainWin= qobject_cast<DolphinMainWindow *>(parentWidget());
1583 if (mainWin) {
1584 DolphinViewContainer* container = mainWin->activeViewContainer();
1585 container->showMessage(job->errorString(), DolphinViewContainer::Error);
1586 } else {
1587 KIO::FileUndoManager::UiInterface::jobError(job);
1588 }
1589 }
1590