]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinmainwindow.cpp
Merge branch 'Applications/18.08'
[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
460 void DolphinMainWindow::createDirectory()
461 {
462 m_newFileMenu->setViewShowsHiddenFiles(activeViewContainer()->view()->hiddenFilesShown());
463 m_newFileMenu->setPopupFiles(activeViewContainer()->url());
464 m_newFileMenu->createDirectory();
465 }
466
467 void DolphinMainWindow::quit()
468 {
469 close();
470 }
471
472 void DolphinMainWindow::showErrorMessage(const QString& message)
473 {
474 m_activeViewContainer->showMessage(message, DolphinViewContainer::Error);
475 }
476
477 void DolphinMainWindow::slotUndoAvailable(bool available)
478 {
479 QAction* undoAction = actionCollection()->action(KStandardAction::name(KStandardAction::Undo));
480 if (undoAction) {
481 undoAction->setEnabled(available);
482 }
483 }
484
485 void DolphinMainWindow::slotUndoTextChanged(const QString& text)
486 {
487 QAction* undoAction = actionCollection()->action(KStandardAction::name(KStandardAction::Undo));
488 if (undoAction) {
489 undoAction->setText(text);
490 }
491 }
492
493 void DolphinMainWindow::undo()
494 {
495 clearStatusBar();
496 KIO::FileUndoManager::self()->uiInterface()->setParentWidget(this);
497 KIO::FileUndoManager::self()->undo();
498 }
499
500 void DolphinMainWindow::cut()
501 {
502 m_activeViewContainer->view()->cutSelectedItems();
503 }
504
505 void DolphinMainWindow::copy()
506 {
507 m_activeViewContainer->view()->copySelectedItems();
508 }
509
510 void DolphinMainWindow::paste()
511 {
512 m_activeViewContainer->view()->paste();
513 }
514
515 void DolphinMainWindow::find()
516 {
517 m_activeViewContainer->setSearchModeEnabled(true);
518 }
519
520 void DolphinMainWindow::updatePasteAction()
521 {
522 QAction* pasteAction = actionCollection()->action(KStandardAction::name(KStandardAction::Paste));
523 QPair<bool, QString> pasteInfo = m_activeViewContainer->view()->pasteInfo();
524 pasteAction->setEnabled(pasteInfo.first);
525 pasteAction->setText(pasteInfo.second);
526 }
527
528 void DolphinMainWindow::slotDirectoryLoadingCompleted()
529 {
530 updatePasteAction();
531 }
532
533 void DolphinMainWindow::slotToolBarActionMiddleClicked(QAction *action)
534 {
535 if (action == actionCollection()->action(KStandardAction::name(KStandardAction::Back))) {
536 goBackInNewTab();
537 } else if (action == actionCollection()->action(KStandardAction::name(KStandardAction::Forward))) {
538 goForwardInNewTab();
539 } else if (action == actionCollection()->action(QStringLiteral("go_up"))) {
540 goUpInNewTab();
541 } else if (action == actionCollection()->action(QStringLiteral("go_home"))) {
542 goHomeInNewTab();
543 }
544 }
545
546 void DolphinMainWindow::selectAll()
547 {
548 clearStatusBar();
549
550 // if the URL navigator is editable and focused, select the whole
551 // URL instead of all items of the view
552
553 KUrlNavigator* urlNavigator = m_activeViewContainer->urlNavigator();
554 QLineEdit* lineEdit = urlNavigator->editor()->lineEdit(); // krazy:exclude=qclasses
555 const bool selectUrl = urlNavigator->isUrlEditable() &&
556 lineEdit->hasFocus();
557 if (selectUrl) {
558 lineEdit->selectAll();
559 } else {
560 m_activeViewContainer->view()->selectAll();
561 }
562 }
563
564 void DolphinMainWindow::invertSelection()
565 {
566 clearStatusBar();
567 m_activeViewContainer->view()->invertSelection();
568 }
569
570 void DolphinMainWindow::toggleSplitView()
571 {
572 DolphinTabPage* tabPage = m_tabWidget->currentTabPage();
573 tabPage->setSplitViewEnabled(!tabPage->splitViewEnabled());
574
575 updateViewActions();
576 }
577
578 void DolphinMainWindow::toggleSplitStash()
579 {
580 DolphinTabPage* tabPage = m_tabWidget->currentTabPage();
581 tabPage->setSplitViewEnabled(false);
582 tabPage->setSplitViewEnabled(true, QUrl("stash:/"));
583 }
584
585 void DolphinMainWindow::reloadView()
586 {
587 clearStatusBar();
588 m_activeViewContainer->reload();
589 m_activeViewContainer->statusBar()->updateSpaceInfo();
590 }
591
592 void DolphinMainWindow::stopLoading()
593 {
594 m_activeViewContainer->view()->stopLoading();
595 }
596
597 void DolphinMainWindow::enableStopAction()
598 {
599 actionCollection()->action(QStringLiteral("stop"))->setEnabled(true);
600 }
601
602 void DolphinMainWindow::disableStopAction()
603 {
604 actionCollection()->action(QStringLiteral("stop"))->setEnabled(false);
605 }
606
607 void DolphinMainWindow::showFilterBar()
608 {
609 m_activeViewContainer->setFilterBarVisible(true);
610 }
611
612 void DolphinMainWindow::toggleEditLocation()
613 {
614 clearStatusBar();
615
616 QAction* action = actionCollection()->action(QStringLiteral("editable_location"));
617 KUrlNavigator* urlNavigator = m_activeViewContainer->urlNavigator();
618 urlNavigator->setUrlEditable(action->isChecked());
619 }
620
621 void DolphinMainWindow::replaceLocation()
622 {
623 KUrlNavigator* navigator = m_activeViewContainer->urlNavigator();
624 navigator->setUrlEditable(true);
625 navigator->setFocus();
626
627 // select the whole text of the combo box editor
628 QLineEdit* lineEdit = navigator->editor()->lineEdit(); // krazy:exclude=qclasses
629 lineEdit->selectAll();
630 }
631
632 void DolphinMainWindow::togglePanelLockState()
633 {
634 const bool newLockState = !GeneralSettings::lockPanels();
635 foreach (QObject* child, children()) {
636 DolphinDockWidget* dock = qobject_cast<DolphinDockWidget*>(child);
637 if (dock) {
638 dock->setLocked(newLockState);
639 }
640 }
641
642 GeneralSettings::setLockPanels(newLockState);
643 }
644
645 void DolphinMainWindow::slotTerminalPanelVisibilityChanged()
646 {
647 if (m_terminalPanel->isHiddenInVisibleWindow()) {
648 m_activeViewContainer->view()->setFocus();
649 }
650 }
651
652 void DolphinMainWindow::goBack()
653 {
654 KUrlNavigator* urlNavigator = m_activeViewContainer->urlNavigator();
655 urlNavigator->goBack();
656
657 if (urlNavigator->locationState().isEmpty()) {
658 // An empty location state indicates a redirection URL,
659 // which must be skipped too
660 urlNavigator->goBack();
661 }
662 }
663
664 void DolphinMainWindow::goForward()
665 {
666 m_activeViewContainer->urlNavigator()->goForward();
667 }
668
669 void DolphinMainWindow::goUp()
670 {
671 m_activeViewContainer->urlNavigator()->goUp();
672 }
673
674 void DolphinMainWindow::goHome()
675 {
676 m_activeViewContainer->urlNavigator()->goHome();
677 }
678
679 void DolphinMainWindow::goBackInNewTab()
680 {
681 KUrlNavigator* urlNavigator = activeViewContainer()->urlNavigator();
682 const int index = urlNavigator->historyIndex() + 1;
683 openNewTab(urlNavigator->locationUrl(index));
684 }
685
686 void DolphinMainWindow::goForwardInNewTab()
687 {
688 KUrlNavigator* urlNavigator = activeViewContainer()->urlNavigator();
689 const int index = urlNavigator->historyIndex() - 1;
690 openNewTab(urlNavigator->locationUrl(index));
691 }
692
693 void DolphinMainWindow::goUpInNewTab()
694 {
695 const QUrl currentUrl = activeViewContainer()->urlNavigator()->locationUrl();
696 openNewTab(KIO::upUrl(currentUrl));
697 }
698
699 void DolphinMainWindow::goHomeInNewTab()
700 {
701 openNewTab(Dolphin::homeUrl());
702 }
703
704 void DolphinMainWindow::compareFiles()
705 {
706 const KFileItemList items = m_tabWidget->currentTabPage()->selectedItems();
707 if (items.count() != 2) {
708 // The action is disabled in this case, but it could have been triggered
709 // via D-Bus, see https://bugs.kde.org/show_bug.cgi?id=325517
710 return;
711 }
712
713 QUrl urlA = items.at(0).url();
714 QUrl urlB = items.at(1).url();
715
716 QString command(QStringLiteral("kompare -c \""));
717 command.append(urlA.toDisplayString(QUrl::PreferLocalFile));
718 command.append("\" \"");
719 command.append(urlB.toDisplayString(QUrl::PreferLocalFile));
720 command.append('\"');
721 KRun::runCommand(command, QStringLiteral("Kompare"), QStringLiteral("kompare"), this);
722 }
723
724 void DolphinMainWindow::toggleShowMenuBar()
725 {
726 const bool visible = menuBar()->isVisible();
727 menuBar()->setVisible(!visible);
728 if (visible) {
729 createControlButton();
730 } else {
731 deleteControlButton();
732 }
733 }
734
735 void DolphinMainWindow::openTerminal()
736 {
737 QString dir(QDir::homePath());
738
739 // If the given directory is not local, it can still be the URL of an
740 // ioslave using UDS_LOCAL_PATH which to be converted first.
741 KIO::StatJob* statJob = KIO::mostLocalUrl(m_activeViewContainer->url());
742 KJobWidgets::setWindow(statJob, this);
743 statJob->exec();
744 QUrl url = statJob->mostLocalUrl();
745
746 //If the URL is local after the above conversion, set the directory.
747 if (url.isLocalFile()) {
748 dir = url.toLocalFile();
749 }
750
751 KToolInvocation::invokeTerminal(QString(), dir);
752 }
753
754 void DolphinMainWindow::editSettings()
755 {
756 if (!m_settingsDialog) {
757 DolphinViewContainer* container = activeViewContainer();
758 container->view()->writeSettings();
759
760 const QUrl url = container->url();
761 DolphinSettingsDialog* settingsDialog = new DolphinSettingsDialog(url, this);
762 connect(settingsDialog, &DolphinSettingsDialog::settingsChanged, this, &DolphinMainWindow::refreshViews);
763 settingsDialog->setAttribute(Qt::WA_DeleteOnClose);
764 settingsDialog->show();
765 m_settingsDialog = settingsDialog;
766 } else {
767 m_settingsDialog.data()->raise();
768 }
769 }
770
771 void DolphinMainWindow::handleUrl(const QUrl& url)
772 {
773 delete m_lastHandleUrlStatJob;
774 m_lastHandleUrlStatJob = nullptr;
775
776 if (url.isLocalFile() && QFileInfo(url.toLocalFile()).isDir()) {
777 activeViewContainer()->setUrl(url);
778 } else if (KProtocolManager::supportsListing(url)) {
779 // stat the URL to see if it is a dir or not
780 m_lastHandleUrlStatJob = KIO::stat(url, KIO::HideProgressInfo);
781 if (m_lastHandleUrlStatJob->uiDelegate()) {
782 KJobWidgets::setWindow(m_lastHandleUrlStatJob, this);
783 }
784 connect(m_lastHandleUrlStatJob, &KIO::Job::result,
785 this, &DolphinMainWindow::slotHandleUrlStatFinished);
786
787 } else {
788 new KRun(url, this); // Automatically deletes itself after being finished
789 }
790 }
791
792 void DolphinMainWindow::slotHandleUrlStatFinished(KJob* job)
793 {
794 m_lastHandleUrlStatJob = nullptr;
795 const KIO::UDSEntry entry = static_cast<KIO::StatJob*>(job)->statResult();
796 const QUrl url = static_cast<KIO::StatJob*>(job)->url();
797 if (entry.isDir()) {
798 activeViewContainer()->setUrl(url);
799 } else {
800 new KRun(url, this); // Automatically deletes itself after being finished
801 }
802 }
803
804 void DolphinMainWindow::slotWriteStateChanged(bool isFolderWritable)
805 {
806 // trash:/ is writable but we don't want to create new items in it.
807 // TODO: remove the trash check once https://phabricator.kde.org/T8234 is implemented
808 newFileMenu()->setEnabled(isFolderWritable && m_activeViewContainer->url().scheme() != QLatin1String("trash"));
809 }
810
811 void DolphinMainWindow::openContextMenu(const QPoint& pos,
812 const KFileItem& item,
813 const QUrl& url,
814 const QList<QAction*>& customActions)
815 {
816 QPointer<DolphinContextMenu> contextMenu = new DolphinContextMenu(this, pos, item, url);
817 contextMenu.data()->setCustomActions(customActions);
818 const DolphinContextMenu::Command command = contextMenu.data()->open();
819
820 switch (command) {
821 case DolphinContextMenu::OpenParentFolder:
822 changeUrl(KIO::upUrl(item.url()));
823 m_activeViewContainer->view()->markUrlsAsSelected({item.url()});
824 m_activeViewContainer->view()->markUrlAsCurrent(item.url());
825 break;
826
827 case DolphinContextMenu::OpenParentFolderInNewWindow:
828 Dolphin::openNewWindow({item.url()}, this, Dolphin::OpenNewWindowFlag::Select);
829 break;
830
831 case DolphinContextMenu::OpenParentFolderInNewTab:
832 openNewTab(KIO::upUrl(item.url()));
833 break;
834
835 case DolphinContextMenu::None:
836 default:
837 break;
838 }
839
840 // Delete the menu, unless it has been deleted in its own nested event loop already.
841 if (contextMenu) {
842 contextMenu->deleteLater();
843 }
844 }
845
846 void DolphinMainWindow::updateControlMenu()
847 {
848 QMenu* menu = qobject_cast<QMenu*>(sender());
849 Q_ASSERT(menu);
850
851 // All actions get cleared by QMenu::clear(). This includes the sub-menus
852 // because 'menu' is their parent.
853 menu->clear();
854
855 KActionCollection* ac = actionCollection();
856
857 // Add "Edit" actions
858 bool added = addActionToMenu(ac->action(KStandardAction::name(KStandardAction::Undo)), menu) |
859 addActionToMenu(ac->action(KStandardAction::name(KStandardAction::Find)), menu) |
860 addActionToMenu(ac->action(KStandardAction::name(KStandardAction::SelectAll)), menu) |
861 addActionToMenu(ac->action(QStringLiteral("invert_selection")), menu);
862
863 if (added) {
864 menu->addSeparator();
865 }
866
867 // Add "View" actions
868 if (!GeneralSettings::showZoomSlider()) {
869 addActionToMenu(ac->action(KStandardAction::name(KStandardAction::ZoomIn)), menu);
870 addActionToMenu(ac->action(KStandardAction::name(KStandardAction::ZoomOut)), menu);
871 menu->addSeparator();
872 }
873
874 added = addActionToMenu(ac->action(QStringLiteral("sort")), menu) |
875 addActionToMenu(ac->action(QStringLiteral("view_mode")), menu) |
876 addActionToMenu(ac->action(QStringLiteral("additional_info")), menu) |
877 addActionToMenu(ac->action(QStringLiteral("show_preview")), menu) |
878 addActionToMenu(ac->action(QStringLiteral("show_in_groups")), menu) |
879 addActionToMenu(ac->action(QStringLiteral("show_hidden_files")), menu);
880
881 if (added) {
882 menu->addSeparator();
883 }
884
885 added = addActionToMenu(ac->action(QStringLiteral("split_view")), menu) |
886 addActionToMenu(ac->action(KStandardAction::name(KStandardAction::Redisplay)), menu) |
887 addActionToMenu(ac->action(QStringLiteral("view_properties")), menu);
888 if (added) {
889 menu->addSeparator();
890 }
891
892 addActionToMenu(ac->action(QStringLiteral("panels")), menu);
893 QMenu* locationBarMenu = new QMenu(i18nc("@action:inmenu", "Location Bar"), menu);
894 locationBarMenu->addAction(ac->action(QStringLiteral("editable_location")));
895 locationBarMenu->addAction(ac->action(QStringLiteral("replace_location")));
896 menu->addMenu(locationBarMenu);
897
898 menu->addSeparator();
899
900 // Add "Go" menu
901 QMenu* goMenu = new QMenu(i18nc("@action:inmenu", "Go"), menu);
902 goMenu->addAction(ac->action(KStandardAction::name(KStandardAction::Back)));
903 goMenu->addAction(ac->action(KStandardAction::name(KStandardAction::Forward)));
904 goMenu->addAction(ac->action(KStandardAction::name(KStandardAction::Up)));
905 goMenu->addAction(ac->action(KStandardAction::name(KStandardAction::Home)));
906 goMenu->addAction(ac->action(QStringLiteral("closed_tabs")));
907 menu->addMenu(goMenu);
908
909 // Add "Tool" menu
910 QMenu* toolsMenu = new QMenu(i18nc("@action:inmenu", "Tools"), menu);
911 toolsMenu->addAction(ac->action(QStringLiteral("show_filter_bar")));
912 toolsMenu->addAction(ac->action(QStringLiteral("compare_files")));
913 toolsMenu->addAction(ac->action(QStringLiteral("open_terminal")));
914 toolsMenu->addAction(ac->action(QStringLiteral("change_remote_encoding")));
915 menu->addMenu(toolsMenu);
916
917 // Add "Settings" menu entries
918 addActionToMenu(ac->action(KStandardAction::name(KStandardAction::KeyBindings)), menu);
919 addActionToMenu(ac->action(KStandardAction::name(KStandardAction::ConfigureToolbars)), menu);
920 addActionToMenu(ac->action(KStandardAction::name(KStandardAction::Preferences)), menu);
921
922 // Add "Help" menu
923 auto helpMenu = new KHelpMenu(menu);
924 menu->addMenu(helpMenu->menu());
925
926 menu->addSeparator();
927 addActionToMenu(ac->action(KStandardAction::name(KStandardAction::ShowMenubar)), menu);
928 }
929
930 void DolphinMainWindow::updateToolBar()
931 {
932 if (!menuBar()->isVisible()) {
933 createControlButton();
934 }
935 }
936
937 void DolphinMainWindow::slotControlButtonDeleted()
938 {
939 m_controlButton = nullptr;
940 m_updateToolBarTimer->start();
941 }
942
943 void DolphinMainWindow::slotPlaceActivated(const QUrl& url)
944 {
945 DolphinViewContainer* view = activeViewContainer();
946
947 if (view->url() == url) {
948 // We can end up here if the user clicked a device in the Places Panel
949 // which had been unmounted earlier, see https://bugs.kde.org/show_bug.cgi?id=161385.
950 reloadView();
951 } else {
952 changeUrl(url);
953 }
954 }
955
956 void DolphinMainWindow::closedTabsCountChanged(unsigned int count)
957 {
958 actionCollection()->action(QStringLiteral("undo_close_tab"))->setEnabled(count > 0);
959 }
960
961 void DolphinMainWindow::activeViewChanged(DolphinViewContainer* viewContainer)
962 {
963 DolphinViewContainer* oldViewContainer = m_activeViewContainer;
964 Q_ASSERT(viewContainer);
965
966 m_activeViewContainer = viewContainer;
967
968 if (oldViewContainer) {
969 // Disconnect all signals between the old view container (container,
970 // view and url navigator) and main window.
971 oldViewContainer->disconnect(this);
972 oldViewContainer->view()->disconnect(this);
973 oldViewContainer->urlNavigator()->disconnect(this);
974 }
975
976 connectViewSignals(viewContainer);
977
978 m_actionHandler->setCurrentView(viewContainer->view());
979
980 updateHistory();
981 updateEditActions();
982 updatePasteAction();
983 updateViewActions();
984 updateGoActions();
985
986 const QUrl url = viewContainer->url();
987 emit urlChanged(url);
988 }
989
990 void DolphinMainWindow::tabCountChanged(int count)
991 {
992 const bool enableTabActions = (count > 1);
993 actionCollection()->action(KStandardAction::name(KStandardAction::Close))->setEnabled(enableTabActions);
994 actionCollection()->action(QStringLiteral("activate_next_tab"))->setEnabled(enableTabActions);
995 actionCollection()->action(QStringLiteral("activate_prev_tab"))->setEnabled(enableTabActions);
996 }
997
998 void DolphinMainWindow::updateWindowTitle()
999 {
1000 setWindowTitle(m_activeViewContainer->caption());
1001 }
1002
1003 void DolphinMainWindow::slotStorageTearDownFromPlacesRequested(const QString& mountPath)
1004 {
1005 if (m_terminalPanel->currentWorkingDirectory().startsWith(mountPath)) {
1006 m_tearDownFromPlacesRequested = true;
1007 m_terminalPanel->goHome();
1008 // m_placesPanel->proceedWithTearDown() will be called in slotTerminalDirectoryChanged
1009 } else {
1010 m_placesPanel->proceedWithTearDown();
1011 }
1012 }
1013
1014 void DolphinMainWindow::slotStorageTearDownExternallyRequested(const QString& mountPath)
1015 {
1016 if (m_terminalPanel->currentWorkingDirectory().startsWith(mountPath)) {
1017 m_tearDownFromPlacesRequested = false;
1018 m_terminalPanel->goHome();
1019 }
1020 }
1021
1022 void DolphinMainWindow::setupActions()
1023 {
1024 // setup 'File' menu
1025 m_newFileMenu = new DolphinNewFileMenu(actionCollection(), this);
1026 m_newFileMenu->setObjectName("newFileMenu");
1027 QMenu* menu = m_newFileMenu->menu();
1028 menu->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New"));
1029 menu->setIcon(QIcon::fromTheme(QStringLiteral("document-new")));
1030 m_newFileMenu->setDelayed(false);
1031 connect(menu, &QMenu::aboutToShow,
1032 this, &DolphinMainWindow::updateNewMenu);
1033
1034 QAction* newWindow = KStandardAction::openNew(this, &DolphinMainWindow::openNewMainWindow, actionCollection());
1035 newWindow->setText(i18nc("@action:inmenu File", "New &Window"));
1036
1037 QAction* newTab = actionCollection()->addAction(QStringLiteral("new_tab"));
1038 newTab->setIcon(QIcon::fromTheme(QStringLiteral("tab-new")));
1039 newTab->setText(i18nc("@action:inmenu File", "New Tab"));
1040 actionCollection()->setDefaultShortcuts(newTab, {Qt::CTRL + Qt::Key_T, Qt::CTRL + Qt::SHIFT + Qt::Key_N});
1041 connect(newTab, &QAction::triggered, this, static_cast<void(DolphinMainWindow::*)()>(&DolphinMainWindow::openNewActivatedTab));
1042
1043 QAction* closeTab = KStandardAction::close(
1044 m_tabWidget, static_cast<void(DolphinTabWidget::*)()>(&DolphinTabWidget::closeTab), actionCollection());
1045 closeTab->setText(i18nc("@action:inmenu File", "Close Tab"));
1046 closeTab->setEnabled(false);
1047
1048 KStandardAction::quit(this, &DolphinMainWindow::quit, actionCollection());
1049
1050 // setup 'Edit' menu
1051 KStandardAction::undo(this,
1052 &DolphinMainWindow::undo,
1053 actionCollection());
1054
1055
1056 KStandardAction::cut(this, &DolphinMainWindow::cut, actionCollection());
1057 KStandardAction::copy(this, &DolphinMainWindow::copy, actionCollection());
1058 QAction* paste = KStandardAction::paste(this, &DolphinMainWindow::paste, actionCollection());
1059 // The text of the paste-action is modified dynamically by Dolphin
1060 // (e. g. to "Paste One Folder"). To prevent that the size of the toolbar changes
1061 // due to the long text, the text "Paste" is used:
1062 paste->setIconText(i18nc("@action:inmenu Edit", "Paste"));
1063
1064 KStandardAction::find(this, &DolphinMainWindow::find, actionCollection());
1065
1066 KStandardAction::selectAll(this, &DolphinMainWindow::selectAll, actionCollection());
1067
1068 QAction* invertSelection = actionCollection()->addAction(QStringLiteral("invert_selection"));
1069 invertSelection->setText(i18nc("@action:inmenu Edit", "Invert Selection"));
1070 invertSelection->setIcon(QIcon::fromTheme(QStringLiteral("edit-select-invert")));
1071 actionCollection()->setDefaultShortcut(invertSelection, Qt::CTRL + Qt::SHIFT + Qt::Key_A);
1072 connect(invertSelection, &QAction::triggered, this, &DolphinMainWindow::invertSelection);
1073
1074 // setup 'View' menu
1075 // (note that most of it is set up in DolphinViewActionHandler)
1076
1077 QAction* split = actionCollection()->addAction(QStringLiteral("split_view"));
1078 actionCollection()->setDefaultShortcut(split, Qt::Key_F3);
1079 connect(split, &QAction::triggered, this, &DolphinMainWindow::toggleSplitView);
1080
1081 QAction* stashSplit = actionCollection()->addAction(QStringLiteral("split_stash"));
1082 actionCollection()->setDefaultShortcut(stashSplit, Qt::CTRL + Qt::Key_S);
1083 stashSplit->setText(i18nc("@action:intoolbar Stash", "Stash"));
1084 stashSplit->setToolTip(i18nc("@info", "Opens the stash virtual directory in a split window"));
1085 stashSplit->setIcon(QIcon::fromTheme(QStringLiteral("folder-stash")));
1086 stashSplit->setCheckable(false);
1087 stashSplit->setVisible(KProtocolInfo::isKnownProtocol("stash"));
1088 connect(stashSplit, &QAction::triggered, this, &DolphinMainWindow::toggleSplitStash);
1089
1090 KStandardAction::redisplay(this, &DolphinMainWindow::reloadView, actionCollection());
1091
1092 QAction* stop = actionCollection()->addAction(QStringLiteral("stop"));
1093 stop->setText(i18nc("@action:inmenu View", "Stop"));
1094 stop->setToolTip(i18nc("@info", "Stop loading"));
1095 stop->setIcon(QIcon::fromTheme(QStringLiteral("process-stop")));
1096 connect(stop, &QAction::triggered, this, &DolphinMainWindow::stopLoading);
1097
1098 KToggleAction* editableLocation = actionCollection()->add<KToggleAction>(QStringLiteral("editable_location"));
1099 editableLocation->setText(i18nc("@action:inmenu Navigation Bar", "Editable Location"));
1100 actionCollection()->setDefaultShortcut(editableLocation, Qt::Key_F6);
1101 connect(editableLocation, &KToggleAction::triggered, this, &DolphinMainWindow::toggleEditLocation);
1102
1103 QAction* replaceLocation = actionCollection()->addAction(QStringLiteral("replace_location"));
1104 replaceLocation->setText(i18nc("@action:inmenu Navigation Bar", "Replace Location"));
1105 actionCollection()->setDefaultShortcut(replaceLocation, Qt::CTRL + Qt::Key_L);
1106 connect(replaceLocation, &QAction::triggered, this, &DolphinMainWindow::replaceLocation);
1107
1108 // setup 'Go' menu
1109 QAction* backAction = KStandardAction::back(this, &DolphinMainWindow::goBack, actionCollection());
1110 auto backShortcuts = backAction->shortcuts();
1111 backShortcuts.append(QKeySequence(Qt::Key_Backspace));
1112 actionCollection()->setDefaultShortcuts(backAction, backShortcuts);
1113
1114 DolphinRecentTabsMenu* recentTabsMenu = new DolphinRecentTabsMenu(this);
1115 actionCollection()->addAction(QStringLiteral("closed_tabs"), recentTabsMenu);
1116 connect(m_tabWidget, &DolphinTabWidget::rememberClosedTab,
1117 recentTabsMenu, &DolphinRecentTabsMenu::rememberClosedTab);
1118 connect(recentTabsMenu, &DolphinRecentTabsMenu::restoreClosedTab,
1119 m_tabWidget, &DolphinTabWidget::restoreClosedTab);
1120 connect(recentTabsMenu, &DolphinRecentTabsMenu::closedTabsCountChanged,
1121 this, &DolphinMainWindow::closedTabsCountChanged);
1122
1123 QAction* undoCloseTab = actionCollection()->addAction(QStringLiteral("undo_close_tab"));
1124 undoCloseTab->setText(i18nc("@action:inmenu File", "Undo close tab"));
1125 actionCollection()->setDefaultShortcut(undoCloseTab, Qt::CTRL + Qt::SHIFT + Qt::Key_T);
1126 undoCloseTab->setIcon(QIcon::fromTheme(QStringLiteral("edit-undo")));
1127 undoCloseTab->setEnabled(false);
1128 connect(undoCloseTab, &QAction::triggered, recentTabsMenu, &DolphinRecentTabsMenu::undoCloseTab);
1129
1130 auto undoAction = actionCollection()->action(KStandardAction::name(KStandardAction::Undo));
1131 undoAction->setEnabled(false); // undo should be disabled by default
1132
1133 KStandardAction::forward(this, &DolphinMainWindow::goForward, actionCollection());
1134 KStandardAction::up(this, &DolphinMainWindow::goUp, actionCollection());
1135 KStandardAction::home(this, &DolphinMainWindow::goHome, actionCollection());
1136
1137 // setup 'Tools' menu
1138 QAction* showFilterBar = actionCollection()->addAction(QStringLiteral("show_filter_bar"));
1139 showFilterBar->setText(i18nc("@action:inmenu Tools", "Show Filter Bar"));
1140 showFilterBar->setIcon(QIcon::fromTheme(QStringLiteral("view-filter")));
1141 actionCollection()->setDefaultShortcuts(showFilterBar, {Qt::CTRL + Qt::Key_I, Qt::Key_Slash});
1142 connect(showFilterBar, &QAction::triggered, this, &DolphinMainWindow::showFilterBar);
1143
1144 QAction* compareFiles = actionCollection()->addAction(QStringLiteral("compare_files"));
1145 compareFiles->setText(i18nc("@action:inmenu Tools", "Compare Files"));
1146 compareFiles->setIcon(QIcon::fromTheme(QStringLiteral("kompare")));
1147 compareFiles->setEnabled(false);
1148 connect(compareFiles, &QAction::triggered, this, &DolphinMainWindow::compareFiles);
1149
1150 #ifndef Q_OS_WIN
1151 if (KAuthorized::authorize(QStringLiteral("shell_access"))) {
1152 QAction* openTerminal = actionCollection()->addAction(QStringLiteral("open_terminal"));
1153 openTerminal->setText(i18nc("@action:inmenu Tools", "Open Terminal"));
1154 openTerminal->setIcon(QIcon::fromTheme(QStringLiteral("utilities-terminal")));
1155 actionCollection()->setDefaultShortcut(openTerminal, Qt::SHIFT + Qt::Key_F4);
1156 connect(openTerminal, &QAction::triggered, this, &DolphinMainWindow::openTerminal);
1157 }
1158 #endif
1159
1160 // setup 'Settings' menu
1161 KToggleAction* showMenuBar = KStandardAction::showMenubar(nullptr, nullptr, actionCollection());
1162 connect(showMenuBar, &KToggleAction::triggered, // Fixes #286822
1163 this, &DolphinMainWindow::toggleShowMenuBar, Qt::QueuedConnection);
1164 KStandardAction::preferences(this, &DolphinMainWindow::editSettings, actionCollection());
1165
1166 // not in menu actions
1167 QList<QKeySequence> nextTabKeys = KStandardShortcut::tabNext();
1168 nextTabKeys.append(QKeySequence(Qt::CTRL + Qt::Key_Tab));
1169
1170 QList<QKeySequence> prevTabKeys = KStandardShortcut::tabPrev();
1171 prevTabKeys.append(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_Tab));
1172
1173 QAction* activateNextTab = actionCollection()->addAction(QStringLiteral("activate_next_tab"));
1174 activateNextTab->setIconText(i18nc("@action:inmenu", "Next Tab"));
1175 activateNextTab->setText(i18nc("@action:inmenu", "Activate Next Tab"));
1176 activateNextTab->setEnabled(false);
1177 connect(activateNextTab, &QAction::triggered, m_tabWidget, &DolphinTabWidget::activateNextTab);
1178 actionCollection()->setDefaultShortcuts(activateNextTab, nextTabKeys);
1179
1180 QAction* activatePrevTab = actionCollection()->addAction(QStringLiteral("activate_prev_tab"));
1181 activatePrevTab->setIconText(i18nc("@action:inmenu", "Previous Tab"));
1182 activatePrevTab->setText(i18nc("@action:inmenu", "Activate Previous Tab"));
1183 activatePrevTab->setEnabled(false);
1184 connect(activatePrevTab, &QAction::triggered, m_tabWidget, &DolphinTabWidget::activatePrevTab);
1185 actionCollection()->setDefaultShortcuts(activatePrevTab, prevTabKeys);
1186
1187 // for context menu
1188 QAction* showTarget = actionCollection()->addAction(QStringLiteral("show_target"));
1189 showTarget->setText(i18nc("@action:inmenu", "Show Target"));
1190 showTarget->setIcon(QIcon::fromTheme(QStringLiteral("document-open-folder")));
1191 showTarget->setEnabled(false);
1192 connect(showTarget, &QAction::triggered, this, &DolphinMainWindow::showTarget);
1193
1194 QAction* openInNewTab = actionCollection()->addAction(QStringLiteral("open_in_new_tab"));
1195 openInNewTab->setText(i18nc("@action:inmenu", "Open in New Tab"));
1196 openInNewTab->setIcon(QIcon::fromTheme(QStringLiteral("tab-new")));
1197 connect(openInNewTab, &QAction::triggered, this, &DolphinMainWindow::openInNewTab);
1198
1199 QAction* openInNewTabs = actionCollection()->addAction(QStringLiteral("open_in_new_tabs"));
1200 openInNewTabs->setText(i18nc("@action:inmenu", "Open in New Tabs"));
1201 openInNewTabs->setIcon(QIcon::fromTheme(QStringLiteral("tab-new")));
1202 connect(openInNewTabs, &QAction::triggered, this, &DolphinMainWindow::openInNewTab);
1203
1204 QAction* openInNewWindow = actionCollection()->addAction(QStringLiteral("open_in_new_window"));
1205 openInNewWindow->setText(i18nc("@action:inmenu", "Open in New Window"));
1206 openInNewWindow->setIcon(QIcon::fromTheme(QStringLiteral("window-new")));
1207 connect(openInNewWindow, &QAction::triggered, this, &DolphinMainWindow::openInNewWindow);
1208 }
1209
1210 void DolphinMainWindow::setupDockWidgets()
1211 {
1212 const bool lock = GeneralSettings::lockPanels();
1213
1214 KDualAction* lockLayoutAction = actionCollection()->add<KDualAction>(QStringLiteral("lock_panels"));
1215 lockLayoutAction->setActiveText(i18nc("@action:inmenu Panels", "Unlock Panels"));
1216 lockLayoutAction->setActiveIcon(QIcon::fromTheme(QStringLiteral("object-unlocked")));
1217 lockLayoutAction->setInactiveText(i18nc("@action:inmenu Panels", "Lock Panels"));
1218 lockLayoutAction->setInactiveIcon(QIcon::fromTheme(QStringLiteral("object-locked")));
1219 lockLayoutAction->setActive(lock);
1220 connect(lockLayoutAction, &KDualAction::triggered, this, &DolphinMainWindow::togglePanelLockState);
1221
1222 // Setup "Information"
1223 DolphinDockWidget* infoDock = new DolphinDockWidget(i18nc("@title:window", "Information"));
1224 infoDock->setLocked(lock);
1225 infoDock->setObjectName(QStringLiteral("infoDock"));
1226 infoDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
1227
1228 #ifdef HAVE_BALOO
1229 InformationPanel* infoPanel = new InformationPanel(infoDock);
1230 infoPanel->setCustomContextMenuActions({lockLayoutAction});
1231 connect(infoPanel, &InformationPanel::urlActivated, this, &DolphinMainWindow::handleUrl);
1232 infoDock->setWidget(infoPanel);
1233
1234 QAction* infoAction = infoDock->toggleViewAction();
1235 createPanelAction(QIcon::fromTheme(QStringLiteral("dialog-information")), Qt::Key_F11, infoAction, QStringLiteral("show_information_panel"));
1236
1237 addDockWidget(Qt::RightDockWidgetArea, infoDock);
1238 connect(this, &DolphinMainWindow::urlChanged,
1239 infoPanel, &InformationPanel::setUrl);
1240 connect(this, &DolphinMainWindow::selectionChanged,
1241 infoPanel, &InformationPanel::setSelection);
1242 connect(this, &DolphinMainWindow::requestItemInfo,
1243 infoPanel, &InformationPanel::requestDelayedItemInfo);
1244 #endif
1245
1246 // Setup "Folders"
1247 DolphinDockWidget* foldersDock = new DolphinDockWidget(i18nc("@title:window", "Folders"));
1248 foldersDock->setLocked(lock);
1249 foldersDock->setObjectName(QStringLiteral("foldersDock"));
1250 foldersDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
1251 FoldersPanel* foldersPanel = new FoldersPanel(foldersDock);
1252 foldersPanel->setCustomContextMenuActions({lockLayoutAction});
1253 foldersDock->setWidget(foldersPanel);
1254
1255 QAction* foldersAction = foldersDock->toggleViewAction();
1256 createPanelAction(QIcon::fromTheme(QStringLiteral("folder")), Qt::Key_F7, foldersAction, QStringLiteral("show_folders_panel"));
1257
1258 addDockWidget(Qt::LeftDockWidgetArea, foldersDock);
1259 connect(this, &DolphinMainWindow::urlChanged,
1260 foldersPanel, &FoldersPanel::setUrl);
1261 connect(foldersPanel, &FoldersPanel::folderActivated,
1262 this, &DolphinMainWindow::changeUrl);
1263 connect(foldersPanel, &FoldersPanel::folderMiddleClicked,
1264 this, &DolphinMainWindow::openNewTab);
1265 connect(foldersPanel, &FoldersPanel::errorMessage,
1266 this, &DolphinMainWindow::showErrorMessage);
1267
1268 // Setup "Terminal"
1269 #ifndef Q_OS_WIN
1270 if (KAuthorized::authorize(QStringLiteral("shell_access"))) {
1271 DolphinDockWidget* terminalDock = new DolphinDockWidget(i18nc("@title:window Shell terminal", "Terminal"));
1272 terminalDock->setLocked(lock);
1273 terminalDock->setObjectName(QStringLiteral("terminalDock"));
1274 m_terminalPanel = new TerminalPanel(terminalDock);
1275 m_terminalPanel->setCustomContextMenuActions({lockLayoutAction});
1276 terminalDock->setWidget(m_terminalPanel);
1277
1278 connect(m_terminalPanel, &TerminalPanel::hideTerminalPanel, terminalDock, &DolphinDockWidget::hide);
1279 connect(m_terminalPanel, &TerminalPanel::changeUrl, this, &DolphinMainWindow::slotTerminalDirectoryChanged);
1280 connect(terminalDock, &DolphinDockWidget::visibilityChanged,
1281 m_terminalPanel, &TerminalPanel::dockVisibilityChanged);
1282 connect(terminalDock, &DolphinDockWidget::visibilityChanged,
1283 this, &DolphinMainWindow::slotTerminalPanelVisibilityChanged);
1284
1285 QAction* terminalAction = terminalDock->toggleViewAction();
1286 createPanelAction(QIcon::fromTheme(QStringLiteral("utilities-terminal")), Qt::Key_F4, terminalAction, QStringLiteral("show_terminal_panel"));
1287
1288 addDockWidget(Qt::BottomDockWidgetArea, terminalDock);
1289 connect(this, &DolphinMainWindow::urlChanged,
1290 m_terminalPanel, &TerminalPanel::setUrl);
1291
1292 if (GeneralSettings::version() < 200) {
1293 terminalDock->hide();
1294 }
1295 }
1296 #endif
1297
1298 if (GeneralSettings::version() < 200) {
1299 infoDock->hide();
1300 foldersDock->hide();
1301 }
1302
1303 // Setup "Places"
1304 DolphinDockWidget* placesDock = new DolphinDockWidget(i18nc("@title:window", "Places"));
1305 placesDock->setLocked(lock);
1306 placesDock->setObjectName(QStringLiteral("placesDock"));
1307 placesDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
1308
1309 m_placesPanel = new PlacesPanel(placesDock);
1310 m_placesPanel->setCustomContextMenuActions({lockLayoutAction});
1311 placesDock->setWidget(m_placesPanel);
1312
1313 QAction *placesAction = placesDock->toggleViewAction();
1314 createPanelAction(QIcon::fromTheme(QStringLiteral("bookmarks")), Qt::Key_F9, placesAction, QStringLiteral("show_places_panel"));
1315
1316 addDockWidget(Qt::LeftDockWidgetArea, placesDock);
1317 connect(m_placesPanel, &PlacesPanel::placeActivated,
1318 this, &DolphinMainWindow::slotPlaceActivated);
1319 connect(m_placesPanel, &PlacesPanel::placeMiddleClicked,
1320 this, &DolphinMainWindow::openNewTab);
1321 connect(m_placesPanel, &PlacesPanel::errorMessage,
1322 this, &DolphinMainWindow::showErrorMessage);
1323 connect(this, &DolphinMainWindow::urlChanged,
1324 m_placesPanel, &PlacesPanel::setUrl);
1325 connect(placesDock, &DolphinDockWidget::visibilityChanged,
1326 m_tabWidget, &DolphinTabWidget::slotPlacesPanelVisibilityChanged);
1327 connect(this, &DolphinMainWindow::settingsChanged,
1328 m_placesPanel, &PlacesPanel::readSettings);
1329 connect(m_placesPanel, &PlacesPanel::storageTearDownRequested,
1330 this, &DolphinMainWindow::slotStorageTearDownFromPlacesRequested);
1331 connect(m_placesPanel, &PlacesPanel::storageTearDownExternallyRequested,
1332 this, &DolphinMainWindow::slotStorageTearDownExternallyRequested);
1333 m_tabWidget->slotPlacesPanelVisibilityChanged(m_placesPanel->isVisible());
1334
1335 // Add actions into the "Panels" menu
1336 KActionMenu* panelsMenu = new KActionMenu(i18nc("@action:inmenu View", "Panels"), this);
1337 actionCollection()->addAction(QStringLiteral("panels"), panelsMenu);
1338 panelsMenu->setDelayed(false);
1339 const KActionCollection* ac = actionCollection();
1340 panelsMenu->addAction(ac->action(QStringLiteral("show_places_panel")));
1341 #ifdef HAVE_BALOO
1342 panelsMenu->addAction(ac->action(QStringLiteral("show_information_panel")));
1343 #endif
1344 panelsMenu->addAction(ac->action(QStringLiteral("show_folders_panel")));
1345 panelsMenu->addAction(ac->action(QStringLiteral("show_terminal_panel")));
1346 panelsMenu->addSeparator();
1347 panelsMenu->addAction(lockLayoutAction);
1348 }
1349
1350 void DolphinMainWindow::updateEditActions()
1351 {
1352 const KFileItemList list = m_activeViewContainer->view()->selectedItems();
1353 if (list.isEmpty()) {
1354 stateChanged(QStringLiteral("has_no_selection"));
1355 } else {
1356 stateChanged(QStringLiteral("has_selection"));
1357
1358 KActionCollection* col = actionCollection();
1359 QAction* renameAction = col->action(KStandardAction::name(KStandardAction::RenameFile));
1360 QAction* moveToTrashAction = col->action(KStandardAction::name(KStandardAction::MoveToTrash));
1361 QAction* deleteAction = col->action(KStandardAction::name(KStandardAction::DeleteFile));
1362 QAction* cutAction = col->action(KStandardAction::name(KStandardAction::Cut));
1363 QAction* deleteWithTrashShortcut = col->action(QStringLiteral("delete_shortcut")); // see DolphinViewActionHandler
1364 QAction* showTarget = col->action(QStringLiteral("show_target"));
1365
1366 KFileItemListProperties capabilities(list);
1367 const bool enableMoveToTrash = capabilities.isLocal() && capabilities.supportsMoving();
1368
1369 renameAction->setEnabled(capabilities.supportsMoving());
1370 moveToTrashAction->setEnabled(enableMoveToTrash);
1371 deleteAction->setEnabled(capabilities.supportsDeleting());
1372 deleteWithTrashShortcut->setEnabled(capabilities.supportsDeleting() && !enableMoveToTrash);
1373 cutAction->setEnabled(capabilities.supportsMoving());
1374 showTarget->setEnabled(list.length() == 1 && list.at(0).isLink());
1375 }
1376 }
1377
1378 void DolphinMainWindow::updateViewActions()
1379 {
1380 m_actionHandler->updateViewActions();
1381
1382 QAction* showFilterBarAction = actionCollection()->action(QStringLiteral("show_filter_bar"));
1383 showFilterBarAction->setChecked(m_activeViewContainer->isFilterBarVisible());
1384
1385 updateSplitAction();
1386
1387 QAction* editableLocactionAction = actionCollection()->action(QStringLiteral("editable_location"));
1388 const KUrlNavigator* urlNavigator = m_activeViewContainer->urlNavigator();
1389 editableLocactionAction->setChecked(urlNavigator->isUrlEditable());
1390 }
1391
1392 void DolphinMainWindow::updateGoActions()
1393 {
1394 QAction* goUpAction = actionCollection()->action(KStandardAction::name(KStandardAction::Up));
1395 const QUrl currentUrl = m_activeViewContainer->url();
1396 goUpAction->setEnabled(KIO::upUrl(currentUrl) != currentUrl);
1397 }
1398
1399 void DolphinMainWindow::createControlButton()
1400 {
1401 if (m_controlButton) {
1402 return;
1403 }
1404 Q_ASSERT(!m_controlButton);
1405
1406 m_controlButton = new QToolButton(this);
1407 m_controlButton->setIcon(QIcon::fromTheme(QStringLiteral("application-menu")));
1408 m_controlButton->setText(i18nc("@action", "Control"));
1409 m_controlButton->setPopupMode(QToolButton::InstantPopup);
1410 m_controlButton->setToolButtonStyle(toolBar()->toolButtonStyle());
1411
1412 QMenu* controlMenu = new QMenu(m_controlButton);
1413 connect(controlMenu, &QMenu::aboutToShow, this, &DolphinMainWindow::updateControlMenu);
1414
1415 m_controlButton->setMenu(controlMenu);
1416
1417 toolBar()->addWidget(m_controlButton);
1418 connect(toolBar(), &KToolBar::iconSizeChanged,
1419 m_controlButton, &QToolButton::setIconSize);
1420 connect(toolBar(), &KToolBar::toolButtonStyleChanged,
1421 m_controlButton, &QToolButton::setToolButtonStyle);
1422
1423 // The added widgets are owned by the toolbar and may get deleted when e.g. the toolbar
1424 // gets edited. In this case we must add them again. The adding is done asynchronously by
1425 // m_updateToolBarTimer.
1426 connect(m_controlButton, &QToolButton::destroyed, this, &DolphinMainWindow::slotControlButtonDeleted);
1427 m_updateToolBarTimer = new QTimer(this);
1428 m_updateToolBarTimer->setInterval(500);
1429 connect(m_updateToolBarTimer, &QTimer::timeout, this, &DolphinMainWindow::updateToolBar);
1430 }
1431
1432 void DolphinMainWindow::deleteControlButton()
1433 {
1434 delete m_controlButton;
1435 m_controlButton = nullptr;
1436
1437 delete m_updateToolBarTimer;
1438 m_updateToolBarTimer = nullptr;
1439 }
1440
1441 bool DolphinMainWindow::addActionToMenu(QAction* action, QMenu* menu)
1442 {
1443 Q_ASSERT(action);
1444 Q_ASSERT(menu);
1445
1446 const KToolBar* toolBarWidget = toolBar();
1447 foreach (const QWidget* widget, action->associatedWidgets()) {
1448 if (widget == toolBarWidget) {
1449 return false;
1450 }
1451 }
1452
1453 menu->addAction(action);
1454 return true;
1455 }
1456
1457 void DolphinMainWindow::refreshViews()
1458 {
1459 m_tabWidget->refreshViews();
1460
1461 if (GeneralSettings::modifiedStartupSettings()) {
1462 // The startup settings have been changed by the user (see bug #254947).
1463 // Synchronize the split-view setting with the active view:
1464 const bool splitView = GeneralSettings::splitView();
1465 m_tabWidget->currentTabPage()->setSplitViewEnabled(splitView);
1466 updateSplitAction();
1467 updateWindowTitle();
1468 }
1469
1470 emit settingsChanged();
1471 }
1472
1473 void DolphinMainWindow::clearStatusBar()
1474 {
1475 m_activeViewContainer->statusBar()->resetToDefaultText();
1476 }
1477
1478 void DolphinMainWindow::connectViewSignals(DolphinViewContainer* container)
1479 {
1480 connect(container, &DolphinViewContainer::showFilterBarChanged,
1481 this, &DolphinMainWindow::updateFilterBarAction);
1482 connect(container, &DolphinViewContainer::writeStateChanged,
1483 this, &DolphinMainWindow::slotWriteStateChanged);
1484
1485 const DolphinView* view = container->view();
1486 connect(view, &DolphinView::selectionChanged,
1487 this, &DolphinMainWindow::slotSelectionChanged);
1488 connect(view, &DolphinView::requestItemInfo,
1489 this, &DolphinMainWindow::requestItemInfo);
1490 connect(view, &DolphinView::tabRequested,
1491 this, &DolphinMainWindow::openNewTab);
1492 connect(view, &DolphinView::requestContextMenu,
1493 this, &DolphinMainWindow::openContextMenu);
1494 connect(view, &DolphinView::directoryLoadingStarted,
1495 this, &DolphinMainWindow::enableStopAction);
1496 connect(view, &DolphinView::directoryLoadingCompleted,
1497 this, &DolphinMainWindow::disableStopAction);
1498 connect(view, &DolphinView::directoryLoadingCompleted,
1499 this, &DolphinMainWindow::slotDirectoryLoadingCompleted);
1500 connect(view, &DolphinView::goBackRequested,
1501 this, static_cast<void(DolphinMainWindow::*)()>(&DolphinMainWindow::goBack));
1502 connect(view, &DolphinView::goForwardRequested,
1503 this, static_cast<void(DolphinMainWindow::*)()>(&DolphinMainWindow::goForward));
1504 connect(view, &DolphinView::urlActivated,
1505 this, &DolphinMainWindow::handleUrl);
1506
1507 const KUrlNavigator* navigator = container->urlNavigator();
1508 connect(navigator, &KUrlNavigator::urlChanged,
1509 this, &DolphinMainWindow::changeUrl);
1510 connect(navigator, &KUrlNavigator::historyChanged,
1511 this, &DolphinMainWindow::updateHistory);
1512 connect(navigator, &KUrlNavigator::editableStateChanged,
1513 this, &DolphinMainWindow::slotEditableStateChanged);
1514 connect(navigator, &KUrlNavigator::tabRequested,
1515 this, &DolphinMainWindow::openNewTab);
1516 }
1517
1518 void DolphinMainWindow::updateSplitAction()
1519 {
1520 QAction* splitAction = actionCollection()->action(QStringLiteral("split_view"));
1521 const DolphinTabPage* tabPage = m_tabWidget->currentTabPage();
1522 if (tabPage->splitViewEnabled()) {
1523 if (tabPage->primaryViewActive()) {
1524 splitAction->setText(i18nc("@action:intoolbar Close left view", "Close"));
1525 splitAction->setToolTip(i18nc("@info", "Close left view"));
1526 splitAction->setIcon(QIcon::fromTheme(QStringLiteral("view-left-close")));
1527 } else {
1528 splitAction->setText(i18nc("@action:intoolbar Close right view", "Close"));
1529 splitAction->setToolTip(i18nc("@info", "Close right view"));
1530 splitAction->setIcon(QIcon::fromTheme(QStringLiteral("view-right-close")));
1531 }
1532 } else {
1533 splitAction->setText(i18nc("@action:intoolbar Split view", "Split"));
1534 splitAction->setToolTip(i18nc("@info", "Split view"));
1535 splitAction->setIcon(QIcon::fromTheme(QStringLiteral("view-right-new")));
1536 }
1537 }
1538
1539 bool DolphinMainWindow::isKompareInstalled() const
1540 {
1541 static bool initialized = false;
1542 static bool installed = false;
1543 if (!initialized) {
1544 // TODO: maybe replace this approach later by using a menu
1545 // plugin like kdiff3plugin.cpp
1546 installed = !QStandardPaths::findExecutable(QStringLiteral("kompare")).isEmpty();
1547 initialized = true;
1548 }
1549 return installed;
1550 }
1551
1552 void DolphinMainWindow::createPanelAction(const QIcon& icon,
1553 const QKeySequence& shortcut,
1554 QAction* dockAction,
1555 const QString& actionName)
1556 {
1557 QAction* panelAction = actionCollection()->addAction(actionName);
1558 panelAction->setCheckable(true);
1559 panelAction->setChecked(dockAction->isChecked());
1560 panelAction->setText(dockAction->text());
1561 panelAction->setIcon(icon);
1562 actionCollection()->setDefaultShortcut(panelAction, shortcut);
1563
1564 connect(panelAction, &QAction::triggered, dockAction, &QAction::trigger);
1565 connect(dockAction, &QAction::toggled, panelAction, &QAction::setChecked);
1566 }
1567
1568 DolphinMainWindow::UndoUiInterface::UndoUiInterface() :
1569 KIO::FileUndoManager::UiInterface()
1570 {
1571 }
1572
1573 DolphinMainWindow::UndoUiInterface::~UndoUiInterface()
1574 {
1575 }
1576
1577 void DolphinMainWindow::UndoUiInterface::jobError(KIO::Job* job)
1578 {
1579 DolphinMainWindow* mainWin= qobject_cast<DolphinMainWindow *>(parentWidget());
1580 if (mainWin) {
1581 DolphinViewContainer* container = mainWin->activeViewContainer();
1582 container->showMessage(job->errorString(), DolphinViewContainer::Error);
1583 } else {
1584 KIO::FileUndoManager::UiInterface::jobError(job);
1585 }
1586 }
1587