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