]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinmainwindow.cpp
847301434b7439ddf7d6befb614cd2eda8bb7c08
[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 "dolphinapplication.h"
25 #include "dolphindockwidget.h"
26 #include "dolphincontextmenu.h"
27 #include "dolphinnewfilemenu.h"
28 #include "dolphinviewcontainer.h"
29 #include "panels/folders/folderspanel.h"
30 #include "panels/places/placespanel.h"
31 #include "panels/information/informationpanel.h"
32 #include "settings/dolphinsettingsdialog.h"
33 #include "statusbar/dolphinstatusbar.h"
34 #include "views/dolphinviewactionhandler.h"
35 #include "views/dolphinremoteencoding.h"
36 #include "views/draganddrophelper.h"
37 #include "views/viewproperties.h"
38 #include "views/dolphinnewfilemenuobserver.h"
39
40 #ifndef Q_OS_WIN
41 #include "panels/terminal/terminalpanel.h"
42 #endif
43
44 #include "dolphin_generalsettings.h"
45
46 #include <KAcceleratorManager>
47 #include <KAction>
48 #include <KActionCollection>
49 #include <KActionMenu>
50 #include <KConfig>
51 #include <KDesktopFile>
52 #include <kdeversion.h>
53 #include <kdualaction.h>
54 #include <KFileDialog>
55 #include <KGlobal>
56 #include <KLineEdit>
57 #include <KToolBar>
58 #include <KIcon>
59 #include <KIconLoader>
60 #include <KIO/NetAccess>
61 #include <KIO/JobUiDelegate>
62 #include <KInputDialog>
63 #include <KLocale>
64 #include <KProtocolManager>
65 #include <KMenu>
66 #include <KMenuBar>
67 #include <KMessageBox>
68 #include <KFileItemListProperties>
69 #include <konqmimedata.h>
70 #include <KProtocolInfo>
71 #include <KRun>
72 #include <KShell>
73 #include <KStandardDirs>
74 #include <kstatusbar.h>
75 #include <KStandardAction>
76 #include <ktabbar.h>
77 #include <KToggleAction>
78 #include <KUrlNavigator>
79 #include <KUrl>
80 #include <KUrlComboBox>
81 #include <KToolInvocation>
82
83 #include <QDesktopWidget>
84 #include <QDBusMessage>
85 #include <QKeyEvent>
86 #include <QClipboard>
87 #include <QToolButton>
88 #include <QSplitter>
89
90 namespace {
91 // Used for GeneralSettings::version() to determine whether
92 // an updated version of Dolphin is running.
93 const int CurrentDolphinVersion = 200;
94 };
95
96 /*
97 * Remembers the tab configuration if a tab has been closed.
98 * Each closed tab can be restored by the menu
99 * "Go -> Recently Closed Tabs".
100 */
101 struct ClosedTab
102 {
103 KUrl primaryUrl;
104 KUrl secondaryUrl;
105 bool isSplit;
106 };
107 Q_DECLARE_METATYPE(ClosedTab)
108
109 DolphinMainWindow::DolphinMainWindow() :
110 KXmlGuiWindow(0),
111 m_newFileMenu(0),
112 m_tabBar(0),
113 m_activeViewContainer(0),
114 m_centralWidgetLayout(0),
115 m_tabIndex(0),
116 m_viewTab(),
117 m_actionHandler(0),
118 m_remoteEncoding(0),
119 m_settingsDialog(),
120 m_controlButton(0),
121 m_updateToolBarTimer(0),
122 m_lastHandleUrlStatJob(0)
123 {
124 setObjectName("Dolphin#");
125
126 m_viewTab.append(ViewTab());
127 ViewTab& viewTab = m_viewTab[m_tabIndex];
128 viewTab.wasActive = true; // The first opened tab is automatically active
129
130 connect(&DolphinNewFileMenuObserver::instance(), SIGNAL(errorMessage(QString)),
131 this, SLOT(showErrorMessage(QString)));
132
133 KIO::FileUndoManager* undoManager = KIO::FileUndoManager::self();
134 undoManager->setUiInterface(new UndoUiInterface());
135
136 connect(undoManager, SIGNAL(undoAvailable(bool)),
137 this, SLOT(slotUndoAvailable(bool)));
138 connect(undoManager, SIGNAL(undoTextChanged(QString)),
139 this, SLOT(slotUndoTextChanged(QString)));
140 connect(undoManager, SIGNAL(jobRecordingStarted(CommandType)),
141 this, SLOT(clearStatusBar()));
142 connect(undoManager, SIGNAL(jobRecordingFinished(CommandType)),
143 this, SLOT(showCommand(CommandType)));
144
145 GeneralSettings* generalSettings = GeneralSettings::self();
146 const bool firstRun = (generalSettings->version() < 200);
147 if (firstRun) {
148 generalSettings->setViewPropsTimestamp(QDateTime::currentDateTime());
149 }
150
151 setAcceptDrops(true);
152
153 viewTab.splitter = new QSplitter(this);
154 viewTab.splitter->setChildrenCollapsible(false);
155
156 setupActions();
157
158 const KUrl homeUrl(generalSettings->homeUrl());
159 setUrlAsCaption(homeUrl);
160 m_actionHandler = new DolphinViewActionHandler(actionCollection(), this);
161 connect(m_actionHandler, SIGNAL(actionBeingHandled()), SLOT(clearStatusBar()));
162 connect(m_actionHandler, SIGNAL(createDirectory()), SLOT(createDirectory()));
163
164 viewTab.primaryView = createViewContainer(homeUrl, viewTab.splitter);
165
166 m_activeViewContainer = viewTab.primaryView;
167 connectViewSignals(m_activeViewContainer);
168 DolphinView* view = m_activeViewContainer->view();
169 m_activeViewContainer->show();
170 m_actionHandler->setCurrentView(view);
171
172 m_remoteEncoding = new DolphinRemoteEncoding(this, m_actionHandler);
173 connect(this, SIGNAL(urlChanged(KUrl)),
174 m_remoteEncoding, SLOT(slotAboutToOpenUrl()));
175
176 m_tabBar = new KTabBar(this);
177 m_tabBar->setMovable(true);
178 m_tabBar->setTabsClosable(true);
179 connect(m_tabBar, SIGNAL(currentChanged(int)),
180 this, SLOT(setActiveTab(int)));
181 connect(m_tabBar, SIGNAL(tabCloseRequested(int)),
182 this, SLOT(closeTab(int)));
183 connect(m_tabBar, SIGNAL(contextMenu(int,QPoint)),
184 this, SLOT(openTabContextMenu(int,QPoint)));
185 connect(m_tabBar, SIGNAL(newTabRequest()),
186 this, SLOT(openNewTab()));
187 connect(m_tabBar, SIGNAL(testCanDecode(const QDragMoveEvent*,bool&)),
188 this, SLOT(slotTestCanDecode(const QDragMoveEvent*,bool&)));
189 connect(m_tabBar, SIGNAL(mouseMiddleClick(int)),
190 this, SLOT(closeTab(int)));
191 connect(m_tabBar, SIGNAL(tabMoved(int,int)),
192 this, SLOT(slotTabMoved(int,int)));
193 connect(m_tabBar, SIGNAL(receivedDropEvent(int,QDropEvent*)),
194 this, SLOT(tabDropEvent(int,QDropEvent*)));
195
196 m_tabBar->blockSignals(true); // signals get unblocked after at least 2 tabs are open
197
198 QWidget* centralWidget = new QWidget(this);
199 m_centralWidgetLayout = new QVBoxLayout(centralWidget);
200 m_centralWidgetLayout->setSpacing(0);
201 m_centralWidgetLayout->setMargin(0);
202 m_centralWidgetLayout->addWidget(m_tabBar);
203 m_centralWidgetLayout->addWidget(viewTab.splitter, 1);
204
205 setCentralWidget(centralWidget);
206 setupDockWidgets();
207 emit urlChanged(homeUrl);
208
209 setupGUI(Keys | Save | Create | ToolBar);
210 stateChanged("new_file");
211
212 QClipboard* clipboard = QApplication::clipboard();
213 connect(clipboard, SIGNAL(dataChanged()),
214 this, SLOT(updatePasteAction()));
215
216 if (generalSettings->splitView()) {
217 toggleSplitView();
218 }
219 updateEditActions();
220 updateViewActions();
221 updateGoActions();
222
223 QAction* showFilterBarAction = actionCollection()->action("show_filter_bar");
224 showFilterBarAction->setChecked(generalSettings->filterBar());
225
226 if (firstRun) {
227 menuBar()->setVisible(false);
228 // Assure a proper default size if Dolphin runs the first time
229 resize(750, 500);
230 }
231
232 const bool showMenu = !menuBar()->isHidden();
233 QAction* showMenuBarAction = actionCollection()->action(KStandardAction::name(KStandardAction::ShowMenubar));
234 showMenuBarAction->setChecked(showMenu); // workaround for bug #171080
235 if (!showMenu) {
236 createControlButton();
237 }
238 }
239
240 DolphinMainWindow::~DolphinMainWindow()
241 {
242 }
243
244 void DolphinMainWindow::openDirectories(const QList<KUrl>& dirs)
245 {
246 if (dirs.isEmpty()) {
247 return;
248 }
249
250 if (dirs.count() == 1) {
251 m_activeViewContainer->setUrl(dirs.first());
252 return;
253 }
254
255 const int oldOpenTabsCount = m_viewTab.count();
256
257 const bool hasSplitView = GeneralSettings::splitView();
258
259 // Open each directory inside a new tab. If the "split view" option has been enabled,
260 // always show two directories within one tab.
261 QList<KUrl>::const_iterator it = dirs.constBegin();
262 while (it != dirs.constEnd()) {
263 openNewTab(*it);
264 ++it;
265
266 if (hasSplitView && (it != dirs.constEnd())) {
267 const int tabIndex = m_viewTab.count() - 1;
268 m_viewTab[tabIndex].secondaryView->setUrl(*it);
269 ++it;
270 }
271 }
272
273 // Remove the previously opened tabs
274 for (int i = 0; i < oldOpenTabsCount; ++i) {
275 closeTab(0);
276 }
277 }
278
279 void DolphinMainWindow::openFiles(const QList<KUrl>& files)
280 {
281 if (files.isEmpty()) {
282 return;
283 }
284
285 // Get all distinct directories from 'files' and open a tab
286 // for each directory. If the "split view" option is enabled, two
287 // directories are shown inside one tab (see openDirectories()).
288 QList<KUrl> dirs;
289 foreach (const KUrl& url, files) {
290 const KUrl dir(url.directory());
291 if (!dirs.contains(dir)) {
292 dirs.append(dir);
293 }
294 }
295
296 openDirectories(dirs);
297
298 // Select the files. Although the files can be split between several
299 // tabs, there is no need to split 'files' accordingly, as
300 // the DolphinView will just ignore invalid selections.
301 const int tabCount = m_viewTab.count();
302 for (int i = 0; i < tabCount; ++i) {
303 m_viewTab[i].primaryView->view()->markUrlsAsSelected(files);
304 m_viewTab[i].primaryView->view()->markUrlAsCurrent(files.at(0));
305 if (m_viewTab[i].secondaryView) {
306 m_viewTab[i].secondaryView->view()->markUrlsAsSelected(files);
307 m_viewTab[i].secondaryView->view()->markUrlAsCurrent(files.at(0));
308 }
309 }
310 }
311
312 void DolphinMainWindow::showCommand(CommandType command)
313 {
314 DolphinStatusBar* statusBar = m_activeViewContainer->statusBar();
315 switch (command) {
316 case KIO::FileUndoManager::Copy:
317 statusBar->setText(i18nc("@info:status", "Successfully copied."));
318 break;
319 case KIO::FileUndoManager::Move:
320 statusBar->setText(i18nc("@info:status", "Successfully moved."));
321 break;
322 case KIO::FileUndoManager::Link:
323 statusBar->setText(i18nc("@info:status", "Successfully linked."));
324 break;
325 case KIO::FileUndoManager::Trash:
326 statusBar->setText(i18nc("@info:status", "Successfully moved to trash."));
327 break;
328 case KIO::FileUndoManager::Rename:
329 statusBar->setText(i18nc("@info:status", "Successfully renamed."));
330 break;
331
332 case KIO::FileUndoManager::Mkdir:
333 statusBar->setText(i18nc("@info:status", "Created folder."));
334 break;
335
336 default:
337 break;
338 }
339 }
340
341 void DolphinMainWindow::pasteIntoFolder()
342 {
343 m_activeViewContainer->view()->pasteIntoFolder();
344 }
345
346 void DolphinMainWindow::changeUrl(const KUrl& url)
347 {
348 if (!KProtocolManager::supportsListing(url)) {
349 // The URL navigator only checks for validity, not
350 // if the URL can be listed. An error message is
351 // shown due to DolphinViewContainer::restoreView().
352 return;
353 }
354
355 DolphinViewContainer* view = activeViewContainer();
356 if (view) {
357 view->setUrl(url);
358 updateEditActions();
359 updateViewActions();
360 updateGoActions();
361 setUrlAsCaption(url);
362 if (m_viewTab.count() > 1) {
363 m_tabBar->setTabText(m_tabIndex, squeezedText(tabName(m_activeViewContainer->url())));
364 }
365 const QString iconName = KMimeType::iconNameForUrl(url);
366 m_tabBar->setTabIcon(m_tabIndex, KIcon(iconName));
367 emit urlChanged(url);
368 }
369 }
370
371 void DolphinMainWindow::slotTerminalDirectoryChanged(const KUrl& url)
372 {
373 m_activeViewContainer->setAutoGrabFocus(false);
374 changeUrl(url);
375 m_activeViewContainer->setAutoGrabFocus(true);
376 }
377
378 void DolphinMainWindow::slotEditableStateChanged(bool editable)
379 {
380 KToggleAction* editableLocationAction =
381 static_cast<KToggleAction*>(actionCollection()->action("editable_location"));
382 editableLocationAction->setChecked(editable);
383 }
384
385 void DolphinMainWindow::slotSelectionChanged(const KFileItemList& selection)
386 {
387 updateEditActions();
388
389 Q_ASSERT(m_viewTab[m_tabIndex].primaryView);
390 int selectedUrlsCount = m_viewTab[m_tabIndex].primaryView->view()->selectedItemsCount();
391 if (m_viewTab[m_tabIndex].secondaryView) {
392 selectedUrlsCount += m_viewTab[m_tabIndex].secondaryView->view()->selectedItemsCount();
393 }
394
395 QAction* compareFilesAction = actionCollection()->action("compare_files");
396 if (selectedUrlsCount == 2) {
397 compareFilesAction->setEnabled(isKompareInstalled());
398 } else {
399 compareFilesAction->setEnabled(false);
400 }
401
402 emit selectionChanged(selection);
403 }
404
405 void DolphinMainWindow::slotRequestItemInfo(const KFileItem& item)
406 {
407 emit requestItemInfo(item);
408 }
409
410 void DolphinMainWindow::updateHistory()
411 {
412 const KUrlNavigator* urlNavigator = m_activeViewContainer->urlNavigator();
413 const int index = urlNavigator->historyIndex();
414
415 QAction* backAction = actionCollection()->action("go_back");
416 if (backAction) {
417 backAction->setToolTip(i18nc("@info", "Go back"));
418 backAction->setEnabled(index < urlNavigator->historySize() - 1);
419 }
420
421 QAction* forwardAction = actionCollection()->action("go_forward");
422 if (forwardAction) {
423 forwardAction->setToolTip(i18nc("@info", "Go forward"));
424 forwardAction->setEnabled(index > 0);
425 }
426 }
427
428 void DolphinMainWindow::updateFilterBarAction(bool show)
429 {
430 QAction* showFilterBarAction = actionCollection()->action("show_filter_bar");
431 showFilterBarAction->setChecked(show);
432 }
433
434 void DolphinMainWindow::openNewMainWindow()
435 {
436 KRun::run("dolphin %u", KUrl::List(), this);
437 }
438
439 void DolphinMainWindow::openNewTab()
440 {
441 const bool isUrlEditable = m_activeViewContainer->urlNavigator()->isUrlEditable();
442
443 openNewTab(m_activeViewContainer->url());
444 m_tabBar->setCurrentIndex(m_viewTab.count() - 1);
445
446 // The URL navigator of the new tab should have the same editable state
447 // as the current tab
448 KUrlNavigator* navigator = m_activeViewContainer->urlNavigator();
449 navigator->setUrlEditable(isUrlEditable);
450
451 if (isUrlEditable) {
452 // If a new tab is opened and the URL is editable, assure that
453 // the user can edit the URL without manually setting the focus
454 navigator->setFocus();
455 }
456 }
457
458 void DolphinMainWindow::openNewTab(const KUrl& url)
459 {
460 QWidget* focusWidget = QApplication::focusWidget();
461
462 if (m_viewTab.count() == 1) {
463 // Only one view is open currently and hence no tab is shown at
464 // all. Before creating a tab for 'url', provide a tab for the current URL.
465 const KUrl currentUrl = m_activeViewContainer->url();
466 m_tabBar->addTab(KIcon(KMimeType::iconNameForUrl(currentUrl)),
467 squeezedText(tabName(currentUrl)));
468 m_tabBar->blockSignals(false);
469 }
470
471 m_tabBar->addTab(KIcon(KMimeType::iconNameForUrl(url)),
472 squeezedText(tabName(url)));
473
474 ViewTab viewTab;
475 viewTab.splitter = new QSplitter(this);
476 viewTab.splitter->setChildrenCollapsible(false);
477 viewTab.primaryView = createViewContainer(url, viewTab.splitter);
478 viewTab.primaryView->setActive(false);
479 connectViewSignals(viewTab.primaryView);
480
481 m_viewTab.append(viewTab);
482
483 actionCollection()->action("close_tab")->setEnabled(true);
484
485 // Provide a split view, if the startup settings are set this way
486 if (GeneralSettings::splitView()) {
487 const int newTabIndex = m_viewTab.count() - 1;
488 createSecondaryView(newTabIndex);
489 m_viewTab[newTabIndex].secondaryView->setActive(true);
490 m_viewTab[newTabIndex].isPrimaryViewActive = false;
491 }
492
493 if (focusWidget) {
494 // The DolphinViewContainer grabbed the keyboard focus. As the tab is opened
495 // in background, assure that the previous focused widget gets the focus back.
496 focusWidget->setFocus();
497 }
498 }
499
500 void DolphinMainWindow::openNewActivatedTab(const KUrl& url)
501 {
502 openNewTab(url);
503 m_tabBar->setCurrentIndex(m_viewTab.count() - 1);
504 }
505
506 void DolphinMainWindow::activateNextTab()
507 {
508 if (m_viewTab.count() >= 2) {
509 const int tabIndex = (m_tabBar->currentIndex() + 1) % m_tabBar->count();
510 m_tabBar->setCurrentIndex(tabIndex);
511 }
512 }
513
514 void DolphinMainWindow::activatePrevTab()
515 {
516 if (m_viewTab.count() >= 2) {
517 int tabIndex = m_tabBar->currentIndex() - 1;
518 if (tabIndex == -1) {
519 tabIndex = m_tabBar->count() - 1;
520 }
521 m_tabBar->setCurrentIndex(tabIndex);
522 }
523 }
524
525 void DolphinMainWindow::openInNewTab()
526 {
527 const KFileItemList& list = m_activeViewContainer->view()->selectedItems();
528 if (list.isEmpty()) {
529 openNewTab(m_activeViewContainer->url());
530 } else {
531 foreach (const KFileItem& item, list) {
532 const KUrl& url = DolphinView::openItemAsFolderUrl(item);
533 if (!url.isEmpty()) {
534 openNewTab(url);
535 }
536 }
537 }
538 }
539
540 void DolphinMainWindow::openInNewWindow()
541 {
542 KUrl newWindowUrl;
543
544 const KFileItemList list = m_activeViewContainer->view()->selectedItems();
545 if (list.isEmpty()) {
546 newWindowUrl = m_activeViewContainer->url();
547 } else if (list.count() == 1) {
548 const KFileItem& item = list.first();
549 newWindowUrl = DolphinView::openItemAsFolderUrl(item);
550 }
551
552 if (!newWindowUrl.isEmpty()) {
553 KRun::run("dolphin %u", KUrl::List() << newWindowUrl, this);
554 }
555 }
556
557 void DolphinMainWindow::toggleActiveView()
558 {
559 if (!m_viewTab[m_tabIndex].secondaryView) {
560 // only one view is available
561 return;
562 }
563
564 Q_ASSERT(m_activeViewContainer);
565 Q_ASSERT(m_viewTab[m_tabIndex].primaryView);
566
567 DolphinViewContainer* left = m_viewTab[m_tabIndex].primaryView;
568 DolphinViewContainer* right = m_viewTab[m_tabIndex].secondaryView;
569 setActiveViewContainer(m_activeViewContainer == right ? left : right);
570 }
571
572 void DolphinMainWindow::showEvent(QShowEvent* event)
573 {
574 KXmlGuiWindow::showEvent(event);
575 if (!event->spontaneous()) {
576 m_activeViewContainer->view()->setFocus();
577 }
578 }
579
580 void DolphinMainWindow::closeEvent(QCloseEvent* event)
581 {
582 // Find out if Dolphin is closed directly by the user or
583 // by the session manager because the session is closed
584 bool closedByUser = true;
585 DolphinApplication *application = qobject_cast<DolphinApplication*>(qApp);
586 if (application && application->sessionSaving()) {
587 closedByUser = false;
588 }
589
590 if (m_viewTab.count() > 1 && GeneralSettings::confirmClosingMultipleTabs() && closedByUser) {
591 // Ask the user if he really wants to quit and close all tabs.
592 // Open a confirmation dialog with 3 buttons:
593 // KDialog::Yes -> Quit
594 // KDialog::No -> Close only the current tab
595 // KDialog::Cancel -> do nothing
596 KDialog *dialog = new KDialog(this, Qt::Dialog);
597 dialog->setCaption(i18nc("@title:window", "Confirmation"));
598 dialog->setButtons(KDialog::Yes | KDialog::No | KDialog::Cancel);
599 dialog->setModal(true);
600 dialog->setButtonGuiItem(KDialog::Yes, KStandardGuiItem::quit());
601 dialog->setButtonGuiItem(KDialog::No, KGuiItem(i18n("C&lose Current Tab"), KIcon("tab-close")));
602 dialog->setButtonGuiItem(KDialog::Cancel, KStandardGuiItem::cancel());
603 dialog->setDefaultButton(KDialog::Yes);
604
605 bool doNotAskAgainCheckboxResult = false;
606
607 const int result = KMessageBox::createKMessageBox(dialog,
608 QMessageBox::Warning,
609 i18n("You have multiple tabs open in this window, are you sure you want to quit?"),
610 QStringList(),
611 i18n("Do not ask again"),
612 &doNotAskAgainCheckboxResult,
613 KMessageBox::Notify);
614
615 if (doNotAskAgainCheckboxResult) {
616 GeneralSettings::setConfirmClosingMultipleTabs(false);
617 }
618
619 switch (result) {
620 case KDialog::Yes:
621 // Quit
622 break;
623 case KDialog::No:
624 // Close only the current tab
625 closeTab();
626 default:
627 event->ignore();
628 return;
629 }
630 }
631
632 GeneralSettings::setVersion(CurrentDolphinVersion);
633 GeneralSettings::self()->writeConfig();
634
635 KXmlGuiWindow::closeEvent(event);
636 }
637
638 void DolphinMainWindow::saveProperties(KConfigGroup& group)
639 {
640 const int tabCount = m_viewTab.count();
641 group.writeEntry("Tab Count", tabCount);
642 group.writeEntry("Active Tab Index", m_tabBar->currentIndex());
643
644 for (int i = 0; i < tabCount; ++i) {
645 const DolphinViewContainer* cont = m_viewTab[i].primaryView;
646 group.writeEntry(tabProperty("Primary URL", i), cont->url().url());
647 group.writeEntry(tabProperty("Primary Editable", i),
648 cont->urlNavigator()->isUrlEditable());
649
650 cont = m_viewTab[i].secondaryView;
651 if (cont) {
652 group.writeEntry(tabProperty("Secondary URL", i), cont->url().url());
653 group.writeEntry(tabProperty("Secondary Editable", i),
654 cont->urlNavigator()->isUrlEditable());
655 }
656 }
657 }
658
659 void DolphinMainWindow::readProperties(const KConfigGroup& group)
660 {
661 const int tabCount = group.readEntry("Tab Count", 1);
662 for (int i = 0; i < tabCount; ++i) {
663 DolphinViewContainer* cont = m_viewTab[i].primaryView;
664
665 cont->setUrl(group.readEntry(tabProperty("Primary URL", i)));
666 const bool editable = group.readEntry(tabProperty("Primary Editable", i), false);
667 cont->urlNavigator()->setUrlEditable(editable);
668
669 cont = m_viewTab[i].secondaryView;
670 const QString secondaryUrl = group.readEntry(tabProperty("Secondary URL", i));
671 if (!secondaryUrl.isEmpty()) {
672 if (!cont) {
673 // a secondary view should be shown, but no one is available
674 // currently -> create a new view
675 toggleSplitView();
676 cont = m_viewTab[i].secondaryView;
677 Q_ASSERT(cont);
678 }
679
680 // The right view must be activated before the URL is set. Changing
681 // the URL in the right view will emit the right URL navigator's
682 // urlChanged(KUrl) signal, which is connected to the changeUrl(KUrl)
683 // slot. That slot will change the URL in the left view if it is still
684 // active. See https://bugs.kde.org/show_bug.cgi?id=330047.
685 setActiveViewContainer(cont);
686
687 cont->setUrl(secondaryUrl);
688 const bool editable = group.readEntry(tabProperty("Secondary Editable", i), false);
689 cont->urlNavigator()->setUrlEditable(editable);
690 } else if (cont) {
691 // no secondary view should be shown, but the default setting shows
692 // one already -> close the view
693 toggleSplitView();
694 }
695
696 // openNewTab() needs to be called only tabCount - 1 times
697 if (i != tabCount - 1) {
698 openNewTab();
699 }
700 }
701
702 const int index = group.readEntry("Active Tab Index", 0);
703 m_tabBar->setCurrentIndex(index);
704 }
705
706 void DolphinMainWindow::updateNewMenu()
707 {
708 m_newFileMenu->setViewShowsHiddenFiles(activeViewContainer()->view()->hiddenFilesShown());
709 m_newFileMenu->checkUpToDate();
710 m_newFileMenu->setPopupFiles(activeViewContainer()->url());
711 }
712
713 void DolphinMainWindow::createDirectory()
714 {
715 m_newFileMenu->setViewShowsHiddenFiles(activeViewContainer()->view()->hiddenFilesShown());
716 m_newFileMenu->setPopupFiles(activeViewContainer()->url());
717 m_newFileMenu->createDirectory();
718 }
719
720 void DolphinMainWindow::quit()
721 {
722 close();
723 }
724
725 void DolphinMainWindow::showErrorMessage(const QString& message)
726 {
727 m_activeViewContainer->showMessage(message, DolphinViewContainer::Error);
728 }
729
730 void DolphinMainWindow::slotUndoAvailable(bool available)
731 {
732 QAction* undoAction = actionCollection()->action(KStandardAction::name(KStandardAction::Undo));
733 if (undoAction) {
734 undoAction->setEnabled(available);
735 }
736 }
737
738 void DolphinMainWindow::restoreClosedTab(QAction* action)
739 {
740 if (action->data().toBool()) {
741 // clear all actions except the "Empty Recently Closed Tabs"
742 // action and the separator
743 QList<QAction*> actions = m_recentTabsMenu->menu()->actions();
744 const int count = actions.size();
745 for (int i = 2; i < count; ++i) {
746 m_recentTabsMenu->menu()->removeAction(actions.at(i));
747 }
748 } else {
749 const ClosedTab closedTab = action->data().value<ClosedTab>();
750 openNewTab(closedTab.primaryUrl);
751 m_tabBar->setCurrentIndex(m_viewTab.count() - 1);
752
753 if (closedTab.isSplit) {
754 // create secondary view
755 toggleSplitView();
756 m_viewTab[m_tabIndex].secondaryView->setUrl(closedTab.secondaryUrl);
757 }
758
759 m_recentTabsMenu->removeAction(action);
760 }
761
762 if (m_recentTabsMenu->menu()->actions().count() == 2) {
763 m_recentTabsMenu->setEnabled(false);
764 }
765 }
766
767 void DolphinMainWindow::slotUndoTextChanged(const QString& text)
768 {
769 QAction* undoAction = actionCollection()->action(KStandardAction::name(KStandardAction::Undo));
770 if (undoAction) {
771 undoAction->setText(text);
772 }
773 }
774
775 void DolphinMainWindow::undo()
776 {
777 clearStatusBar();
778 KIO::FileUndoManager::self()->uiInterface()->setParentWidget(this);
779 KIO::FileUndoManager::self()->undo();
780 }
781
782 void DolphinMainWindow::cut()
783 {
784 m_activeViewContainer->view()->cutSelectedItems();
785 }
786
787 void DolphinMainWindow::copy()
788 {
789 m_activeViewContainer->view()->copySelectedItems();
790 }
791
792 void DolphinMainWindow::paste()
793 {
794 m_activeViewContainer->view()->paste();
795 }
796
797 void DolphinMainWindow::find()
798 {
799 m_activeViewContainer->setSearchModeEnabled(true);
800 }
801
802 void DolphinMainWindow::updatePasteAction()
803 {
804 QAction* pasteAction = actionCollection()->action(KStandardAction::name(KStandardAction::Paste));
805 QPair<bool, QString> pasteInfo = m_activeViewContainer->view()->pasteInfo();
806 pasteAction->setEnabled(pasteInfo.first);
807 pasteAction->setText(pasteInfo.second);
808 }
809
810 void DolphinMainWindow::selectAll()
811 {
812 clearStatusBar();
813
814 // if the URL navigator is editable and focused, select the whole
815 // URL instead of all items of the view
816
817 KUrlNavigator* urlNavigator = m_activeViewContainer->urlNavigator();
818 QLineEdit* lineEdit = urlNavigator->editor()->lineEdit(); // krazy:exclude=qclasses
819 const bool selectUrl = urlNavigator->isUrlEditable() &&
820 lineEdit->hasFocus();
821 if (selectUrl) {
822 lineEdit->selectAll();
823 } else {
824 m_activeViewContainer->view()->selectAll();
825 }
826 }
827
828 void DolphinMainWindow::invertSelection()
829 {
830 clearStatusBar();
831 m_activeViewContainer->view()->invertSelection();
832 }
833
834 void DolphinMainWindow::toggleSplitView()
835 {
836 if (!m_viewTab[m_tabIndex].secondaryView) {
837 createSecondaryView(m_tabIndex);
838 setActiveViewContainer(m_viewTab[m_tabIndex].secondaryView);
839 } else if (m_activeViewContainer == m_viewTab[m_tabIndex].secondaryView) {
840 // remove secondary view
841 m_viewTab[m_tabIndex].secondaryView->close();
842 m_viewTab[m_tabIndex].secondaryView->deleteLater();
843 m_viewTab[m_tabIndex].secondaryView = 0;
844
845 setActiveViewContainer(m_viewTab[m_tabIndex].primaryView);
846 } else {
847 // The primary view is active and should be closed. Hence from a users point of view
848 // the content of the secondary view should be moved to the primary view.
849 // From an implementation point of view it is more efficient to close
850 // the primary view and exchange the internal pointers afterwards.
851
852 m_viewTab[m_tabIndex].primaryView->close();
853 m_viewTab[m_tabIndex].primaryView->deleteLater();
854 m_viewTab[m_tabIndex].primaryView = m_viewTab[m_tabIndex].secondaryView;
855 m_viewTab[m_tabIndex].secondaryView = 0;
856
857 setActiveViewContainer(m_viewTab[m_tabIndex].primaryView);
858 }
859
860 updateViewActions();
861 }
862
863 void DolphinMainWindow::reloadView()
864 {
865 clearStatusBar();
866 m_activeViewContainer->view()->reload();
867 }
868
869 void DolphinMainWindow::stopLoading()
870 {
871 m_activeViewContainer->view()->stopLoading();
872 }
873
874 void DolphinMainWindow::enableStopAction()
875 {
876 actionCollection()->action("stop")->setEnabled(true);
877 }
878
879 void DolphinMainWindow::disableStopAction()
880 {
881 actionCollection()->action("stop")->setEnabled(false);
882 }
883
884 void DolphinMainWindow::showFilterBar()
885 {
886 m_activeViewContainer->setFilterBarVisible(true);
887 }
888
889 void DolphinMainWindow::toggleEditLocation()
890 {
891 clearStatusBar();
892
893 QAction* action = actionCollection()->action("editable_location");
894 KUrlNavigator* urlNavigator = m_activeViewContainer->urlNavigator();
895 urlNavigator->setUrlEditable(action->isChecked());
896 }
897
898 void DolphinMainWindow::replaceLocation()
899 {
900 KUrlNavigator* navigator = m_activeViewContainer->urlNavigator();
901 navigator->setUrlEditable(true);
902 navigator->setFocus();
903
904 // select the whole text of the combo box editor
905 QLineEdit* lineEdit = navigator->editor()->lineEdit(); // krazy:exclude=qclasses
906 lineEdit->selectAll();
907 }
908
909 void DolphinMainWindow::togglePanelLockState()
910 {
911 const bool newLockState = !GeneralSettings::lockPanels();
912 foreach (QObject* child, children()) {
913 DolphinDockWidget* dock = qobject_cast<DolphinDockWidget*>(child);
914 if (dock) {
915 dock->setLocked(newLockState);
916 }
917 }
918
919 GeneralSettings::setLockPanels(newLockState);
920 }
921
922 void DolphinMainWindow::slotPlacesPanelVisibilityChanged(bool visible)
923 {
924 const int tabCount = m_viewTab.count();
925 for (int i = 0; i < tabCount; ++i) {
926 ViewTab& tab = m_viewTab[i];
927 Q_ASSERT(tab.primaryView);
928 tab.primaryView->urlNavigator()->setPlacesSelectorVisible(!visible);
929 if (tab.secondaryView) {
930 tab.secondaryView->urlNavigator()->setPlacesSelectorVisible(!visible);
931 }
932 }
933 }
934
935 void DolphinMainWindow::goBack()
936 {
937 KUrlNavigator* urlNavigator = m_activeViewContainer->urlNavigator();
938 urlNavigator->goBack();
939
940 if (urlNavigator->locationState().isEmpty()) {
941 // An empty location state indicates a redirection URL,
942 // which must be skipped too
943 urlNavigator->goBack();
944 }
945 }
946
947 void DolphinMainWindow::goForward()
948 {
949 m_activeViewContainer->urlNavigator()->goForward();
950 }
951
952 void DolphinMainWindow::goUp()
953 {
954 m_activeViewContainer->urlNavigator()->goUp();
955 }
956
957 void DolphinMainWindow::goHome()
958 {
959 m_activeViewContainer->urlNavigator()->goHome();
960 }
961
962 void DolphinMainWindow::goBack(Qt::MouseButtons buttons)
963 {
964 // The default case (left button pressed) is handled in goBack().
965 if (buttons == Qt::MidButton) {
966 KUrlNavigator* urlNavigator = activeViewContainer()->urlNavigator();
967 const int index = urlNavigator->historyIndex() + 1;
968 openNewTab(urlNavigator->locationUrl(index));
969 }
970 }
971
972 void DolphinMainWindow::goForward(Qt::MouseButtons buttons)
973 {
974 // The default case (left button pressed) is handled in goForward().
975 if (buttons == Qt::MidButton) {
976 KUrlNavigator* urlNavigator = activeViewContainer()->urlNavigator();
977 const int index = urlNavigator->historyIndex() - 1;
978 openNewTab(urlNavigator->locationUrl(index));
979 }
980 }
981
982 void DolphinMainWindow::goUp(Qt::MouseButtons buttons)
983 {
984 // The default case (left button pressed) is handled in goUp().
985 if (buttons == Qt::MidButton) {
986 openNewTab(activeViewContainer()->url().upUrl());
987 }
988 }
989
990 void DolphinMainWindow::goHome(Qt::MouseButtons buttons)
991 {
992 // The default case (left button pressed) is handled in goHome().
993 if (buttons == Qt::MidButton) {
994 openNewTab(GeneralSettings::self()->homeUrl());
995 }
996 }
997
998 void DolphinMainWindow::compareFiles()
999 {
1000 const DolphinViewContainer* primaryViewContainer = m_viewTab[m_tabIndex].primaryView;
1001 Q_ASSERT(primaryViewContainer);
1002 KFileItemList items = primaryViewContainer->view()->selectedItems();
1003
1004 const DolphinViewContainer* secondaryViewContainer = m_viewTab[m_tabIndex].secondaryView;
1005 if (secondaryViewContainer) {
1006 items.append(secondaryViewContainer->view()->selectedItems());
1007 }
1008
1009 if (items.count() != 2) {
1010 // The action is disabled in this case, but it could have been triggered
1011 // via D-Bus, see https://bugs.kde.org/show_bug.cgi?id=325517
1012 return;
1013 }
1014
1015 KUrl urlA = items.at(0).url();
1016 KUrl urlB = items.at(1).url();
1017
1018 QString command("kompare -c \"");
1019 command.append(urlA.pathOrUrl());
1020 command.append("\" \"");
1021 command.append(urlB.pathOrUrl());
1022 command.append('\"');
1023 KRun::runCommand(command, "Kompare", "kompare", this);
1024 }
1025
1026 void DolphinMainWindow::toggleShowMenuBar()
1027 {
1028 const bool visible = menuBar()->isVisible();
1029 menuBar()->setVisible(!visible);
1030 if (visible) {
1031 createControlButton();
1032 } else {
1033 deleteControlButton();
1034 }
1035 }
1036
1037 void DolphinMainWindow::openTerminal()
1038 {
1039 QString dir(QDir::homePath());
1040
1041 // If the given directory is not local, it can still be the URL of an
1042 // ioslave using UDS_LOCAL_PATH which to be converted first.
1043 KUrl url = KIO::NetAccess::mostLocalUrl(m_activeViewContainer->url(), this);
1044
1045 //If the URL is local after the above conversion, set the directory.
1046 if (url.isLocalFile()) {
1047 dir = url.toLocalFile();
1048 }
1049
1050 KToolInvocation::invokeTerminal(QString(), dir);
1051 }
1052
1053 void DolphinMainWindow::editSettings()
1054 {
1055 if (!m_settingsDialog) {
1056 DolphinViewContainer* container = activeViewContainer();
1057 container->view()->writeSettings();
1058
1059 const KUrl url = container->url();
1060 DolphinSettingsDialog* settingsDialog = new DolphinSettingsDialog(url, this);
1061 connect(settingsDialog, SIGNAL(settingsChanged()), this, SLOT(refreshViews()));
1062 settingsDialog->setAttribute(Qt::WA_DeleteOnClose);
1063 settingsDialog->show();
1064 m_settingsDialog = settingsDialog;
1065 } else {
1066 m_settingsDialog.data()->raise();
1067 }
1068 }
1069
1070 void DolphinMainWindow::setActiveTab(int index)
1071 {
1072 Q_ASSERT(index >= 0);
1073 Q_ASSERT(index < m_viewTab.count());
1074 if (index == m_tabIndex) {
1075 return;
1076 }
1077
1078 // hide current tab content
1079 ViewTab& hiddenTab = m_viewTab[m_tabIndex];
1080 hiddenTab.isPrimaryViewActive = hiddenTab.primaryView->isActive();
1081 hiddenTab.primaryView->setActive(false);
1082 if (hiddenTab.secondaryView) {
1083 hiddenTab.secondaryView->setActive(false);
1084 }
1085 QSplitter* splitter = m_viewTab[m_tabIndex].splitter;
1086 splitter->hide();
1087 m_centralWidgetLayout->removeWidget(splitter);
1088
1089 // show active tab content
1090 m_tabIndex = index;
1091
1092 ViewTab& viewTab = m_viewTab[index];
1093 m_centralWidgetLayout->addWidget(viewTab.splitter, 1);
1094 viewTab.primaryView->show();
1095 if (viewTab.secondaryView) {
1096 viewTab.secondaryView->show();
1097 }
1098 viewTab.splitter->show();
1099
1100 if (!viewTab.wasActive) {
1101 viewTab.wasActive = true;
1102
1103 // If the tab has not been activated yet the size of the KItemListView is
1104 // undefined and results in an unwanted animation. To prevent this a
1105 // reloading of the directory gets triggered.
1106 viewTab.primaryView->view()->reload();
1107 if (viewTab.secondaryView) {
1108 viewTab.secondaryView->view()->reload();
1109 }
1110 }
1111
1112 setActiveViewContainer(viewTab.isPrimaryViewActive ? viewTab.primaryView :
1113 viewTab.secondaryView);
1114 }
1115
1116 void DolphinMainWindow::closeTab()
1117 {
1118 closeTab(m_tabBar->currentIndex());
1119 }
1120
1121 void DolphinMainWindow::closeTab(int index)
1122 {
1123 Q_ASSERT(index >= 0);
1124 Q_ASSERT(index < m_viewTab.count());
1125 if (m_viewTab.count() == 1) {
1126 // the last tab may never get closed
1127 return;
1128 }
1129
1130 if (index == m_tabIndex) {
1131 // The tab that should be closed is the active tab. Activate the
1132 // previous tab before closing the tab.
1133 m_tabBar->setCurrentIndex((index > 0) ? index - 1 : 1);
1134 }
1135 rememberClosedTab(index);
1136
1137 // delete tab
1138 m_viewTab[index].primaryView->deleteLater();
1139 if (m_viewTab[index].secondaryView) {
1140 m_viewTab[index].secondaryView->deleteLater();
1141 }
1142 m_viewTab[index].splitter->deleteLater();
1143 m_viewTab.erase(m_viewTab.begin() + index);
1144
1145 m_tabBar->blockSignals(true);
1146 m_tabBar->removeTab(index);
1147
1148 if (m_tabIndex > index) {
1149 m_tabIndex--;
1150 Q_ASSERT(m_tabIndex >= 0);
1151 }
1152
1153 // if only one tab is left, also remove the tab entry so that
1154 // closing the last tab is not possible
1155 if (m_viewTab.count() == 1) {
1156 m_tabBar->removeTab(0);
1157 actionCollection()->action("close_tab")->setEnabled(false);
1158 } else {
1159 m_tabBar->blockSignals(false);
1160 }
1161 }
1162
1163 void DolphinMainWindow::openTabContextMenu(int index, const QPoint& pos)
1164 {
1165 KMenu menu(this);
1166
1167 QAction* newTabAction = menu.addAction(KIcon("tab-new"), i18nc("@action:inmenu", "New Tab"));
1168 newTabAction->setShortcut(actionCollection()->action("new_tab")->shortcut());
1169
1170 QAction* detachTabAction = menu.addAction(KIcon("tab-detach"), i18nc("@action:inmenu", "Detach Tab"));
1171
1172 QAction* closeOtherTabsAction = menu.addAction(KIcon("tab-close-other"), i18nc("@action:inmenu", "Close Other Tabs"));
1173
1174 QAction* closeTabAction = menu.addAction(KIcon("tab-close"), i18nc("@action:inmenu", "Close Tab"));
1175 closeTabAction->setShortcut(actionCollection()->action("close_tab")->shortcut());
1176 QAction* selectedAction = menu.exec(pos);
1177 if (selectedAction == newTabAction) {
1178 const ViewTab& tab = m_viewTab[index];
1179 Q_ASSERT(tab.primaryView);
1180 const KUrl url = tab.secondaryView && tab.secondaryView->isActive() ?
1181 tab.secondaryView->url() : tab.primaryView->url();
1182 openNewTab(url);
1183 m_tabBar->setCurrentIndex(m_viewTab.count() - 1);
1184 } else if (selectedAction == detachTabAction) {
1185 const QString separator(QLatin1Char(' '));
1186 QString command = QLatin1String("dolphin");
1187
1188 const ViewTab& tab = m_viewTab[index];
1189 Q_ASSERT(tab.primaryView);
1190
1191 command += separator + tab.primaryView->url().url();
1192 if (tab.secondaryView) {
1193 command += separator + tab.secondaryView->url().url();
1194 command += separator + QLatin1String("-split");
1195 }
1196
1197 KRun::runCommand(command, this);
1198
1199 closeTab(index);
1200 } else if (selectedAction == closeOtherTabsAction) {
1201 const int count = m_tabBar->count();
1202 for (int i = 0; i < index; ++i) {
1203 closeTab(0);
1204 }
1205 for (int i = index + 1; i < count; ++i) {
1206 closeTab(1);
1207 }
1208 } else if (selectedAction == closeTabAction) {
1209 closeTab(index);
1210 }
1211 }
1212
1213 void DolphinMainWindow::slotTabMoved(int from, int to)
1214 {
1215 m_viewTab.move(from, to);
1216 m_tabIndex = m_tabBar->currentIndex();
1217 }
1218
1219 void DolphinMainWindow::slotTestCanDecode(const QDragMoveEvent* event, bool& canDecode)
1220 {
1221 canDecode = KUrl::List::canDecode(event->mimeData());
1222 }
1223
1224 void DolphinMainWindow::handleUrl(const KUrl& url)
1225 {
1226 delete m_lastHandleUrlStatJob;
1227 m_lastHandleUrlStatJob = 0;
1228
1229 if (url.isLocalFile() && QFileInfo(url.toLocalFile()).isDir()) {
1230 activeViewContainer()->setUrl(url);
1231 } else if (KProtocolManager::supportsListing(url)) {
1232 // stat the URL to see if it is a dir or not
1233 m_lastHandleUrlStatJob = KIO::stat(url, KIO::HideProgressInfo);
1234 if (m_lastHandleUrlStatJob->ui()) {
1235 m_lastHandleUrlStatJob->ui()->setWindow(this);
1236 }
1237 connect(m_lastHandleUrlStatJob, SIGNAL(result(KJob*)),
1238 this, SLOT(slotHandleUrlStatFinished(KJob*)));
1239
1240 } else {
1241 new KRun(url, this); // Automatically deletes itself after being finished
1242 }
1243 }
1244
1245 void DolphinMainWindow::slotHandleUrlStatFinished(KJob* job)
1246 {
1247 m_lastHandleUrlStatJob = 0;
1248 const KIO::UDSEntry entry = static_cast<KIO::StatJob*>(job)->statResult();
1249 const KUrl url = static_cast<KIO::StatJob*>(job)->url();
1250 if (entry.isDir()) {
1251 activeViewContainer()->setUrl(url);
1252 } else {
1253 new KRun(url, this); // Automatically deletes itself after being finished
1254 }
1255 }
1256
1257 void DolphinMainWindow::tabDropEvent(int tab, QDropEvent* event)
1258 {
1259 const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
1260 if (!urls.isEmpty() && tab != -1) {
1261 const ViewTab& viewTab = m_viewTab[tab];
1262 const DolphinView* view = viewTab.isPrimaryViewActive ? viewTab.primaryView->view()
1263 : viewTab.secondaryView->view();
1264 QString error;
1265 DragAndDropHelper::dropUrls(view->rootItem(), view->url(), event, error);
1266 if (!error.isEmpty()) {
1267 activeViewContainer()->showMessage(error, DolphinViewContainer::Error);
1268 }
1269 }
1270 }
1271
1272 void DolphinMainWindow::slotWriteStateChanged(bool isFolderWritable)
1273 {
1274 newFileMenu()->setEnabled(isFolderWritable);
1275 }
1276
1277 void DolphinMainWindow::openContextMenu(const QPoint& pos,
1278 const KFileItem& item,
1279 const KUrl& url,
1280 const QList<QAction*>& customActions)
1281 {
1282 QWeakPointer<DolphinContextMenu> contextMenu = new DolphinContextMenu(this, pos, item, url);
1283 contextMenu.data()->setCustomActions(customActions);
1284 const DolphinContextMenu::Command command = contextMenu.data()->open();
1285
1286 switch (command) {
1287 case DolphinContextMenu::OpenParentFolderInNewWindow: {
1288 KRun::run("dolphin %u", KUrl::List() << item.url().upUrl(), this);
1289 break;
1290 }
1291
1292 case DolphinContextMenu::OpenParentFolderInNewTab:
1293 openNewTab(item.url().upUrl());
1294 break;
1295
1296 case DolphinContextMenu::None:
1297 default:
1298 break;
1299 }
1300
1301 delete contextMenu.data();
1302 }
1303
1304 void DolphinMainWindow::updateControlMenu()
1305 {
1306 KMenu* menu = qobject_cast<KMenu*>(sender());
1307 Q_ASSERT(menu);
1308
1309 // All actions get cleared by KMenu::clear(). The sub-menus are deleted
1310 // by connecting to the aboutToHide() signal from the parent-menu.
1311 menu->clear();
1312
1313 KActionCollection* ac = actionCollection();
1314
1315 // Add "Edit" actions
1316 bool added = addActionToMenu(ac->action(KStandardAction::name(KStandardAction::Undo)), menu) |
1317 addActionToMenu(ac->action(KStandardAction::name(KStandardAction::Find)), menu) |
1318 addActionToMenu(ac->action("select_all"), menu) |
1319 addActionToMenu(ac->action("invert_selection"), menu);
1320
1321 if (added) {
1322 menu->addSeparator();
1323 }
1324
1325 // Add "View" actions
1326 if (!GeneralSettings::showZoomSlider()) {
1327 addActionToMenu(ac->action(KStandardAction::name(KStandardAction::ZoomIn)), menu);
1328 addActionToMenu(ac->action(KStandardAction::name(KStandardAction::ZoomOut)), menu);
1329 menu->addSeparator();
1330 }
1331
1332 added = addActionToMenu(ac->action("view_mode"), menu) |
1333 addActionToMenu(ac->action("sort"), menu) |
1334 addActionToMenu(ac->action("additional_info"), menu) |
1335 addActionToMenu(ac->action("show_preview"), menu) |
1336 addActionToMenu(ac->action("show_in_groups"), menu) |
1337 addActionToMenu(ac->action("show_hidden_files"), menu);
1338
1339 if (added) {
1340 menu->addSeparator();
1341 }
1342
1343 added = addActionToMenu(ac->action("split_view"), menu) |
1344 addActionToMenu(ac->action("reload"), menu) |
1345 addActionToMenu(ac->action("view_properties"), menu);
1346 if (added) {
1347 menu->addSeparator();
1348 }
1349
1350 addActionToMenu(ac->action("panels"), menu);
1351 KMenu* locationBarMenu = new KMenu(i18nc("@action:inmenu", "Location Bar"), menu);
1352 locationBarMenu->addAction(ac->action("editable_location"));
1353 locationBarMenu->addAction(ac->action("replace_location"));
1354 menu->addMenu(locationBarMenu);
1355
1356 menu->addSeparator();
1357
1358 // Add "Go" menu
1359 KMenu* goMenu = new KMenu(i18nc("@action:inmenu", "Go"), menu);
1360 connect(menu, SIGNAL(aboutToHide()), goMenu, SLOT(deleteLater()));
1361 goMenu->addAction(ac->action(KStandardAction::name(KStandardAction::Back)));
1362 goMenu->addAction(ac->action(KStandardAction::name(KStandardAction::Forward)));
1363 goMenu->addAction(ac->action(KStandardAction::name(KStandardAction::Up)));
1364 goMenu->addAction(ac->action(KStandardAction::name(KStandardAction::Home)));
1365 goMenu->addAction(ac->action("closed_tabs"));
1366 menu->addMenu(goMenu);
1367
1368 // Add "Tool" menu
1369 KMenu* toolsMenu = new KMenu(i18nc("@action:inmenu", "Tools"), menu);
1370 connect(menu, SIGNAL(aboutToHide()), toolsMenu, SLOT(deleteLater()));
1371 toolsMenu->addAction(ac->action("show_filter_bar"));
1372 toolsMenu->addAction(ac->action("compare_files"));
1373 toolsMenu->addAction(ac->action("open_terminal"));
1374 toolsMenu->addAction(ac->action("change_remote_encoding"));
1375 menu->addMenu(toolsMenu);
1376
1377 // Add "Settings" menu entries
1378 addActionToMenu(ac->action(KStandardAction::name(KStandardAction::KeyBindings)), menu);
1379 addActionToMenu(ac->action(KStandardAction::name(KStandardAction::ConfigureToolbars)), menu);
1380 addActionToMenu(ac->action(KStandardAction::name(KStandardAction::Preferences)), menu);
1381
1382 // Add "Help" menu
1383 KMenu* helpMenu = new KMenu(i18nc("@action:inmenu", "Help"), menu);
1384 connect(menu, SIGNAL(aboutToHide()), helpMenu, SLOT(deleteLater()));
1385 helpMenu->addAction(ac->action(KStandardAction::name(KStandardAction::HelpContents)));
1386 helpMenu->addAction(ac->action(KStandardAction::name(KStandardAction::WhatsThis)));
1387 helpMenu->addSeparator();
1388 helpMenu->addAction(ac->action(KStandardAction::name(KStandardAction::ReportBug)));
1389 helpMenu->addSeparator();
1390 helpMenu->addAction(ac->action(KStandardAction::name(KStandardAction::SwitchApplicationLanguage)));
1391 helpMenu->addSeparator();
1392 helpMenu->addAction(ac->action(KStandardAction::name(KStandardAction::AboutApp)));
1393 helpMenu->addAction(ac->action(KStandardAction::name(KStandardAction::AboutKDE)));
1394 menu->addMenu(helpMenu);
1395
1396 menu->addSeparator();
1397 addActionToMenu(ac->action(KStandardAction::name(KStandardAction::ShowMenubar)), menu);
1398 }
1399
1400 void DolphinMainWindow::updateToolBar()
1401 {
1402 if (!menuBar()->isVisible()) {
1403 createControlButton();
1404 }
1405 }
1406
1407 void DolphinMainWindow::slotControlButtonDeleted()
1408 {
1409 m_controlButton = 0;
1410 m_updateToolBarTimer->start();
1411 }
1412
1413 void DolphinMainWindow::slotPanelErrorMessage(const QString& error)
1414 {
1415 activeViewContainer()->showMessage(error, DolphinViewContainer::Error);
1416 }
1417
1418 void DolphinMainWindow::slotPlaceActivated(const KUrl& url)
1419 {
1420 DolphinViewContainer* view = activeViewContainer();
1421
1422 if (view->url() == url) {
1423 // We can end up here if the user clicked a device in the Places Panel
1424 // which had been unmounted earlier, see https://bugs.kde.org/show_bug.cgi?id=161385.
1425 reloadView();
1426 } else {
1427 changeUrl(url);
1428 }
1429 }
1430
1431 void DolphinMainWindow::setActiveViewContainer(DolphinViewContainer* viewContainer)
1432 {
1433 Q_ASSERT(viewContainer);
1434 Q_ASSERT((viewContainer == m_viewTab[m_tabIndex].primaryView) ||
1435 (viewContainer == m_viewTab[m_tabIndex].secondaryView));
1436 if (m_activeViewContainer == viewContainer) {
1437 return;
1438 }
1439
1440 m_activeViewContainer->setActive(false);
1441 m_activeViewContainer = viewContainer;
1442
1443 // Activating the view container might trigger a recursive setActiveViewContainer() call
1444 // inside DolphinMainWindow::toggleActiveView() when having a split view. Temporary
1445 // disconnect the activated() signal in this case:
1446 disconnect(m_activeViewContainer->view(), SIGNAL(activated()), this, SLOT(toggleActiveView()));
1447 m_activeViewContainer->setActive(true);
1448 connect(m_activeViewContainer->view(), SIGNAL(activated()), this, SLOT(toggleActiveView()));
1449
1450 m_actionHandler->setCurrentView(viewContainer->view());
1451
1452 updateHistory();
1453 updateEditActions();
1454 updateViewActions();
1455 updateGoActions();
1456
1457 const KUrl url = m_activeViewContainer->url();
1458 setUrlAsCaption(url);
1459 if (m_viewTab.count() > 1) {
1460 m_tabBar->setTabText(m_tabIndex, tabName(url));
1461 m_tabBar->setTabIcon(m_tabIndex, KIcon(KMimeType::iconNameForUrl(url)));
1462 }
1463
1464 emit urlChanged(url);
1465 }
1466
1467 DolphinViewContainer* DolphinMainWindow::createViewContainer(const KUrl& url, QWidget* parent)
1468 {
1469 DolphinViewContainer* container = new DolphinViewContainer(url, parent);
1470
1471 // The places-selector from the URL navigator should only be shown
1472 // if the places dock is invisible
1473 QDockWidget* placesDock = findChild<QDockWidget*>("placesDock");
1474 container->urlNavigator()->setPlacesSelectorVisible(!placesDock || !placesDock->isVisible());
1475
1476 return container;
1477 }
1478
1479 void DolphinMainWindow::setupActions()
1480 {
1481 // setup 'File' menu
1482 m_newFileMenu = new DolphinNewFileMenu(actionCollection(), this);
1483 KMenu* menu = m_newFileMenu->menu();
1484 menu->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New"));
1485 menu->setIcon(KIcon("document-new"));
1486 m_newFileMenu->setDelayed(false);
1487 connect(menu, SIGNAL(aboutToShow()),
1488 this, SLOT(updateNewMenu()));
1489
1490 KAction* newWindow = actionCollection()->addAction("new_window");
1491 newWindow->setIcon(KIcon("window-new"));
1492 newWindow->setText(i18nc("@action:inmenu File", "New &Window"));
1493 newWindow->setShortcut(Qt::CTRL | Qt::Key_N);
1494 connect(newWindow, SIGNAL(triggered()), this, SLOT(openNewMainWindow()));
1495
1496 KAction* newTab = actionCollection()->addAction("new_tab");
1497 newTab->setIcon(KIcon("tab-new"));
1498 newTab->setText(i18nc("@action:inmenu File", "New Tab"));
1499 newTab->setShortcut(KShortcut(Qt::CTRL | Qt::Key_T, Qt::CTRL | Qt::SHIFT | Qt::Key_N));
1500 connect(newTab, SIGNAL(triggered()), this, SLOT(openNewTab()));
1501
1502 KAction* closeTab = actionCollection()->addAction("close_tab");
1503 closeTab->setIcon(KIcon("tab-close"));
1504 closeTab->setText(i18nc("@action:inmenu File", "Close Tab"));
1505 closeTab->setShortcut(Qt::CTRL | Qt::Key_W);
1506 closeTab->setEnabled(false);
1507 connect(closeTab, SIGNAL(triggered()), this, SLOT(closeTab()));
1508
1509 KStandardAction::quit(this, SLOT(quit()), actionCollection());
1510
1511 // setup 'Edit' menu
1512 KStandardAction::undo(this,
1513 SLOT(undo()),
1514 actionCollection());
1515
1516 // need to remove shift+del from cut action, else the shortcut for deletejob
1517 // doesn't work
1518 KAction* cut = KStandardAction::cut(this, SLOT(cut()), actionCollection());
1519 KShortcut cutShortcut = cut->shortcut();
1520 cutShortcut.remove(Qt::SHIFT | Qt::Key_Delete, KShortcut::KeepEmpty);
1521 cut->setShortcut(cutShortcut);
1522 KStandardAction::copy(this, SLOT(copy()), actionCollection());
1523 KAction* paste = KStandardAction::paste(this, SLOT(paste()), actionCollection());
1524 // The text of the paste-action is modified dynamically by Dolphin
1525 // (e. g. to "Paste One Folder"). To prevent that the size of the toolbar changes
1526 // due to the long text, the text "Paste" is used:
1527 paste->setIconText(i18nc("@action:inmenu Edit", "Paste"));
1528
1529 KStandardAction::find(this, SLOT(find()), actionCollection());
1530
1531 KAction* selectAll = actionCollection()->addAction("select_all");
1532 selectAll->setText(i18nc("@action:inmenu Edit", "Select All"));
1533 selectAll->setShortcut(Qt::CTRL | Qt::Key_A);
1534 connect(selectAll, SIGNAL(triggered()), this, SLOT(selectAll()));
1535
1536 KAction* invertSelection = actionCollection()->addAction("invert_selection");
1537 invertSelection->setText(i18nc("@action:inmenu Edit", "Invert Selection"));
1538 invertSelection->setShortcut(Qt::CTRL | Qt::SHIFT | Qt::Key_A);
1539 connect(invertSelection, SIGNAL(triggered()), this, SLOT(invertSelection()));
1540
1541 // setup 'View' menu
1542 // (note that most of it is set up in DolphinViewActionHandler)
1543
1544 KAction* split = actionCollection()->addAction("split_view");
1545 split->setShortcut(Qt::Key_F3);
1546 updateSplitAction();
1547 connect(split, SIGNAL(triggered()), this, SLOT(toggleSplitView()));
1548
1549 KAction* reload = actionCollection()->addAction("reload");
1550 reload->setText(i18nc("@action:inmenu View", "Reload"));
1551 reload->setShortcut(Qt::Key_F5);
1552 reload->setIcon(KIcon("view-refresh"));
1553 connect(reload, SIGNAL(triggered()), this, SLOT(reloadView()));
1554
1555 KAction* stop = actionCollection()->addAction("stop");
1556 stop->setText(i18nc("@action:inmenu View", "Stop"));
1557 stop->setToolTip(i18nc("@info", "Stop loading"));
1558 stop->setIcon(KIcon("process-stop"));
1559 connect(stop, SIGNAL(triggered()), this, SLOT(stopLoading()));
1560
1561 KToggleAction* editableLocation = actionCollection()->add<KToggleAction>("editable_location");
1562 editableLocation->setText(i18nc("@action:inmenu Navigation Bar", "Editable Location"));
1563 editableLocation->setShortcut(Qt::Key_F6);
1564 connect(editableLocation, SIGNAL(triggered()), this, SLOT(toggleEditLocation()));
1565
1566 KAction* replaceLocation = actionCollection()->addAction("replace_location");
1567 replaceLocation->setText(i18nc("@action:inmenu Navigation Bar", "Replace Location"));
1568 replaceLocation->setShortcut(Qt::CTRL | Qt::Key_L);
1569 connect(replaceLocation, SIGNAL(triggered()), this, SLOT(replaceLocation()));
1570
1571 // setup 'Go' menu
1572 KAction* backAction = KStandardAction::back(this, SLOT(goBack()), actionCollection());
1573 connect(backAction, SIGNAL(triggered(Qt::MouseButtons,Qt::KeyboardModifiers)), this, SLOT(goBack(Qt::MouseButtons)));
1574 KShortcut backShortcut = backAction->shortcut();
1575 backShortcut.setAlternate(Qt::Key_Backspace);
1576 backAction->setShortcut(backShortcut);
1577
1578 m_recentTabsMenu = new KActionMenu(i18n("Recently Closed Tabs"), this);
1579 m_recentTabsMenu->setIcon(KIcon("edit-undo"));
1580 m_recentTabsMenu->setDelayed(false);
1581 actionCollection()->addAction("closed_tabs", m_recentTabsMenu);
1582 connect(m_recentTabsMenu->menu(), SIGNAL(triggered(QAction*)),
1583 this, SLOT(restoreClosedTab(QAction*)));
1584
1585 QAction* action = new QAction(i18n("Empty Recently Closed Tabs"), m_recentTabsMenu);
1586 action->setIcon(KIcon("edit-clear-list"));
1587 action->setData(QVariant::fromValue(true));
1588 m_recentTabsMenu->addAction(action);
1589 m_recentTabsMenu->addSeparator();
1590 m_recentTabsMenu->setEnabled(false);
1591
1592 KAction* forwardAction = KStandardAction::forward(this, SLOT(goForward()), actionCollection());
1593 connect(forwardAction, SIGNAL(triggered(Qt::MouseButtons,Qt::KeyboardModifiers)), this, SLOT(goForward(Qt::MouseButtons)));
1594
1595 KAction* upAction = KStandardAction::up(this, SLOT(goUp()), actionCollection());
1596 connect(upAction, SIGNAL(triggered(Qt::MouseButtons,Qt::KeyboardModifiers)), this, SLOT(goUp(Qt::MouseButtons)));
1597
1598 KAction* homeAction = KStandardAction::home(this, SLOT(goHome()), actionCollection());
1599 connect(homeAction, SIGNAL(triggered(Qt::MouseButtons,Qt::KeyboardModifiers)), this, SLOT(goHome(Qt::MouseButtons)));
1600
1601 // setup 'Tools' menu
1602 KAction* showFilterBar = actionCollection()->addAction("show_filter_bar");
1603 showFilterBar->setText(i18nc("@action:inmenu Tools", "Show Filter Bar"));
1604 showFilterBar->setIcon(KIcon("view-filter"));
1605 showFilterBar->setShortcut(Qt::CTRL | Qt::Key_I);
1606 connect(showFilterBar, SIGNAL(triggered()), this, SLOT(showFilterBar()));
1607
1608 KAction* compareFiles = actionCollection()->addAction("compare_files");
1609 compareFiles->setText(i18nc("@action:inmenu Tools", "Compare Files"));
1610 compareFiles->setIcon(KIcon("kompare"));
1611 compareFiles->setEnabled(false);
1612 connect(compareFiles, SIGNAL(triggered()), this, SLOT(compareFiles()));
1613
1614 KAction* openTerminal = actionCollection()->addAction("open_terminal");
1615 openTerminal->setText(i18nc("@action:inmenu Tools", "Open Terminal"));
1616 openTerminal->setIcon(KIcon("utilities-terminal"));
1617 openTerminal->setShortcut(Qt::SHIFT | Qt::Key_F4);
1618 connect(openTerminal, SIGNAL(triggered()), this, SLOT(openTerminal()));
1619
1620 // setup 'Settings' menu
1621 KToggleAction* showMenuBar = KStandardAction::showMenubar(0, 0, actionCollection());
1622 connect(showMenuBar, SIGNAL(triggered(bool)), // Fixes #286822
1623 this, SLOT(toggleShowMenuBar()), Qt::QueuedConnection);
1624 KStandardAction::preferences(this, SLOT(editSettings()), actionCollection());
1625
1626 // not in menu actions
1627 QList<QKeySequence> nextTabKeys;
1628 nextTabKeys.append(KStandardShortcut::tabNext().primary());
1629 nextTabKeys.append(QKeySequence(Qt::CTRL | Qt::Key_Tab));
1630
1631 QList<QKeySequence> prevTabKeys;
1632 prevTabKeys.append(KStandardShortcut::tabPrev().primary());
1633 prevTabKeys.append(QKeySequence(Qt::CTRL | Qt::SHIFT | Qt::Key_Tab));
1634
1635 KAction* activateNextTab = actionCollection()->addAction("activate_next_tab");
1636 activateNextTab->setText(i18nc("@action:inmenu", "Activate Next Tab"));
1637 connect(activateNextTab, SIGNAL(triggered()), SLOT(activateNextTab()));
1638 activateNextTab->setShortcuts(QApplication::isRightToLeft() ? prevTabKeys : nextTabKeys);
1639
1640 KAction* activatePrevTab = actionCollection()->addAction("activate_prev_tab");
1641 activatePrevTab->setText(i18nc("@action:inmenu", "Activate Previous Tab"));
1642 connect(activatePrevTab, SIGNAL(triggered()), SLOT(activatePrevTab()));
1643 activatePrevTab->setShortcuts(QApplication::isRightToLeft() ? nextTabKeys : prevTabKeys);
1644
1645 // for context menu
1646 KAction* openInNewTab = actionCollection()->addAction("open_in_new_tab");
1647 openInNewTab->setText(i18nc("@action:inmenu", "Open in New Tab"));
1648 openInNewTab->setIcon(KIcon("tab-new"));
1649 connect(openInNewTab, SIGNAL(triggered()), this, SLOT(openInNewTab()));
1650
1651 KAction* openInNewTabs = actionCollection()->addAction("open_in_new_tabs");
1652 openInNewTabs->setText(i18nc("@action:inmenu", "Open in New Tabs"));
1653 openInNewTabs->setIcon(KIcon("tab-new"));
1654 connect(openInNewTabs, SIGNAL(triggered()), this, SLOT(openInNewTab()));
1655
1656 KAction* openInNewWindow = actionCollection()->addAction("open_in_new_window");
1657 openInNewWindow->setText(i18nc("@action:inmenu", "Open in New Window"));
1658 openInNewWindow->setIcon(KIcon("window-new"));
1659 connect(openInNewWindow, SIGNAL(triggered()), this, SLOT(openInNewWindow()));
1660 }
1661
1662 void DolphinMainWindow::setupDockWidgets()
1663 {
1664 const bool lock = GeneralSettings::lockPanels();
1665
1666 KDualAction* lockLayoutAction = actionCollection()->add<KDualAction>("lock_panels");
1667 lockLayoutAction->setActiveText(i18nc("@action:inmenu Panels", "Unlock Panels"));
1668 lockLayoutAction->setActiveIcon(KIcon("object-unlocked"));
1669 lockLayoutAction->setInactiveText(i18nc("@action:inmenu Panels", "Lock Panels"));
1670 lockLayoutAction->setInactiveIcon(KIcon("object-locked"));
1671 lockLayoutAction->setActive(lock);
1672 connect(lockLayoutAction, SIGNAL(triggered()), this, SLOT(togglePanelLockState()));
1673
1674 // Setup "Information"
1675 DolphinDockWidget* infoDock = new DolphinDockWidget(i18nc("@title:window", "Information"));
1676 infoDock->setLocked(lock);
1677 infoDock->setObjectName("infoDock");
1678 infoDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
1679 Panel* infoPanel = new InformationPanel(infoDock);
1680 infoPanel->setCustomContextMenuActions(QList<QAction*>() << lockLayoutAction);
1681 connect(infoPanel, SIGNAL(urlActivated(KUrl)), this, SLOT(handleUrl(KUrl)));
1682 infoDock->setWidget(infoPanel);
1683
1684 QAction* infoAction = infoDock->toggleViewAction();
1685 createPanelAction(KIcon("dialog-information"), Qt::Key_F11, infoAction, "show_information_panel");
1686
1687 addDockWidget(Qt::RightDockWidgetArea, infoDock);
1688 connect(this, SIGNAL(urlChanged(KUrl)),
1689 infoPanel, SLOT(setUrl(KUrl)));
1690 connect(this, SIGNAL(selectionChanged(KFileItemList)),
1691 infoPanel, SLOT(setSelection(KFileItemList)));
1692 connect(this, SIGNAL(requestItemInfo(KFileItem)),
1693 infoPanel, SLOT(requestDelayedItemInfo(KFileItem)));
1694
1695 // Setup "Folders"
1696 DolphinDockWidget* foldersDock = new DolphinDockWidget(i18nc("@title:window", "Folders"));
1697 foldersDock->setLocked(lock);
1698 foldersDock->setObjectName("foldersDock");
1699 foldersDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
1700 FoldersPanel* foldersPanel = new FoldersPanel(foldersDock);
1701 foldersPanel->setCustomContextMenuActions(QList<QAction*>() << lockLayoutAction);
1702 foldersDock->setWidget(foldersPanel);
1703
1704 QAction* foldersAction = foldersDock->toggleViewAction();
1705 createPanelAction(KIcon("folder"), Qt::Key_F7, foldersAction, "show_folders_panel");
1706
1707 addDockWidget(Qt::LeftDockWidgetArea, foldersDock);
1708 connect(this, SIGNAL(urlChanged(KUrl)),
1709 foldersPanel, SLOT(setUrl(KUrl)));
1710 connect(foldersPanel, SIGNAL(folderActivated(KUrl)),
1711 this, SLOT(changeUrl(KUrl)));
1712 connect(foldersPanel, SIGNAL(folderMiddleClicked(KUrl)),
1713 this, SLOT(openNewTab(KUrl)));
1714 connect(foldersPanel, SIGNAL(errorMessage(QString)),
1715 this, SLOT(slotPanelErrorMessage(QString)));
1716
1717 // Setup "Terminal"
1718 #ifndef Q_OS_WIN
1719 DolphinDockWidget* terminalDock = new DolphinDockWidget(i18nc("@title:window Shell terminal", "Terminal"));
1720 terminalDock->setLocked(lock);
1721 terminalDock->setObjectName("terminalDock");
1722 terminalDock->setAllowedAreas(Qt::TopDockWidgetArea | Qt::BottomDockWidgetArea);
1723 Panel* terminalPanel = new TerminalPanel(terminalDock);
1724 terminalPanel->setCustomContextMenuActions(QList<QAction*>() << lockLayoutAction);
1725 terminalDock->setWidget(terminalPanel);
1726
1727 connect(terminalPanel, SIGNAL(hideTerminalPanel()), terminalDock, SLOT(hide()));
1728 connect(terminalPanel, SIGNAL(changeUrl(KUrl)), this, SLOT(slotTerminalDirectoryChanged(KUrl)));
1729 connect(terminalDock, SIGNAL(visibilityChanged(bool)),
1730 terminalPanel, SLOT(dockVisibilityChanged()));
1731
1732 QAction* terminalAction = terminalDock->toggleViewAction();
1733 createPanelAction(KIcon("utilities-terminal"), Qt::Key_F4, terminalAction, "show_terminal_panel");
1734
1735 addDockWidget(Qt::BottomDockWidgetArea, terminalDock);
1736 connect(this, SIGNAL(urlChanged(KUrl)),
1737 terminalPanel, SLOT(setUrl(KUrl)));
1738 #endif
1739
1740 if (GeneralSettings::version() < 200) {
1741 infoDock->hide();
1742 foldersDock->hide();
1743 #ifndef Q_OS_WIN
1744 terminalDock->hide();
1745 #endif
1746 }
1747
1748 // Setup "Places"
1749 DolphinDockWidget* placesDock = new DolphinDockWidget(i18nc("@title:window", "Places"));
1750 placesDock->setLocked(lock);
1751 placesDock->setObjectName("placesDock");
1752 placesDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
1753
1754 PlacesPanel* placesPanel = new PlacesPanel(placesDock);
1755 placesPanel->setCustomContextMenuActions(QList<QAction*>() << lockLayoutAction);
1756 placesDock->setWidget(placesPanel);
1757
1758 QAction* placesAction = placesDock->toggleViewAction();
1759 createPanelAction(KIcon("bookmarks"), Qt::Key_F9, placesAction, "show_places_panel");
1760
1761 addDockWidget(Qt::LeftDockWidgetArea, placesDock);
1762 connect(placesPanel, SIGNAL(placeActivated(KUrl)),
1763 this, SLOT(slotPlaceActivated(KUrl)));
1764 connect(placesPanel, SIGNAL(placeMiddleClicked(KUrl)),
1765 this, SLOT(openNewTab(KUrl)));
1766 connect(placesPanel, SIGNAL(errorMessage(QString)),
1767 this, SLOT(slotPanelErrorMessage(QString)));
1768 connect(this, SIGNAL(urlChanged(KUrl)),
1769 placesPanel, SLOT(setUrl(KUrl)));
1770 connect(placesDock, SIGNAL(visibilityChanged(bool)),
1771 this, SLOT(slotPlacesPanelVisibilityChanged(bool)));
1772 connect(this, SIGNAL(settingsChanged()),
1773 placesPanel, SLOT(readSettings()));
1774
1775 // Add actions into the "Panels" menu
1776 KActionMenu* panelsMenu = new KActionMenu(i18nc("@action:inmenu View", "Panels"), this);
1777 actionCollection()->addAction("panels", panelsMenu);
1778 panelsMenu->setDelayed(false);
1779 const KActionCollection* ac = actionCollection();
1780 panelsMenu->addAction(ac->action("show_places_panel"));
1781 panelsMenu->addAction(ac->action("show_information_panel"));
1782 panelsMenu->addAction(ac->action("show_folders_panel"));
1783 #ifndef Q_OS_WIN
1784 panelsMenu->addAction(ac->action("show_terminal_panel"));
1785 #endif
1786 panelsMenu->addSeparator();
1787 panelsMenu->addAction(lockLayoutAction);
1788 }
1789
1790 void DolphinMainWindow::updateEditActions()
1791 {
1792 const KFileItemList list = m_activeViewContainer->view()->selectedItems();
1793 if (list.isEmpty()) {
1794 stateChanged("has_no_selection");
1795 } else {
1796 stateChanged("has_selection");
1797
1798 KActionCollection* col = actionCollection();
1799 QAction* renameAction = col->action("rename");
1800 QAction* moveToTrashAction = col->action("move_to_trash");
1801 QAction* deleteAction = col->action("delete");
1802 QAction* cutAction = col->action(KStandardAction::name(KStandardAction::Cut));
1803 QAction* deleteWithTrashShortcut = col->action("delete_shortcut"); // see DolphinViewActionHandler
1804
1805 KFileItemListProperties capabilities(list);
1806 const bool enableMoveToTrash = capabilities.isLocal() && capabilities.supportsMoving();
1807
1808 renameAction->setEnabled(capabilities.supportsMoving());
1809 moveToTrashAction->setEnabled(enableMoveToTrash);
1810 deleteAction->setEnabled(capabilities.supportsDeleting());
1811 deleteWithTrashShortcut->setEnabled(capabilities.supportsDeleting() && !enableMoveToTrash);
1812 cutAction->setEnabled(capabilities.supportsMoving());
1813 }
1814 updatePasteAction();
1815 }
1816
1817 void DolphinMainWindow::updateViewActions()
1818 {
1819 m_actionHandler->updateViewActions();
1820
1821 QAction* showFilterBarAction = actionCollection()->action("show_filter_bar");
1822 showFilterBarAction->setChecked(m_activeViewContainer->isFilterBarVisible());
1823
1824 updateSplitAction();
1825
1826 QAction* editableLocactionAction = actionCollection()->action("editable_location");
1827 const KUrlNavigator* urlNavigator = m_activeViewContainer->urlNavigator();
1828 editableLocactionAction->setChecked(urlNavigator->isUrlEditable());
1829 }
1830
1831 void DolphinMainWindow::updateGoActions()
1832 {
1833 QAction* goUpAction = actionCollection()->action(KStandardAction::name(KStandardAction::Up));
1834 const KUrl currentUrl = m_activeViewContainer->url();
1835 goUpAction->setEnabled(currentUrl.upUrl() != currentUrl);
1836 }
1837
1838 void DolphinMainWindow::createControlButton()
1839 {
1840 if (m_controlButton) {
1841 return;
1842 }
1843 Q_ASSERT(!m_controlButton);
1844
1845 m_controlButton = new QToolButton(this);
1846 m_controlButton->setIcon(KIcon("applications-system"));
1847 m_controlButton->setText(i18nc("@action", "Control"));
1848 m_controlButton->setPopupMode(QToolButton::InstantPopup);
1849 m_controlButton->setToolButtonStyle(toolBar()->toolButtonStyle());
1850
1851 KMenu* controlMenu = new KMenu(m_controlButton);
1852 connect(controlMenu, SIGNAL(aboutToShow()), this, SLOT(updateControlMenu()));
1853
1854 m_controlButton->setMenu(controlMenu);
1855
1856 toolBar()->addWidget(m_controlButton);
1857 connect(toolBar(), SIGNAL(iconSizeChanged(QSize)),
1858 m_controlButton, SLOT(setIconSize(QSize)));
1859 connect(toolBar(), SIGNAL(toolButtonStyleChanged(Qt::ToolButtonStyle)),
1860 m_controlButton, SLOT(setToolButtonStyle(Qt::ToolButtonStyle)));
1861
1862 // The added widgets are owned by the toolbar and may get deleted when e.g. the toolbar
1863 // gets edited. In this case we must add them again. The adding is done asynchronously by
1864 // m_updateToolBarTimer.
1865 connect(m_controlButton, SIGNAL(destroyed()), this, SLOT(slotControlButtonDeleted()));
1866 m_updateToolBarTimer = new QTimer(this);
1867 m_updateToolBarTimer->setInterval(500);
1868 connect(m_updateToolBarTimer, SIGNAL(timeout()), this, SLOT(updateToolBar()));
1869 }
1870
1871 void DolphinMainWindow::deleteControlButton()
1872 {
1873 delete m_controlButton;
1874 m_controlButton = 0;
1875
1876 delete m_updateToolBarTimer;
1877 m_updateToolBarTimer = 0;
1878 }
1879
1880 bool DolphinMainWindow::addActionToMenu(QAction* action, KMenu* menu)
1881 {
1882 Q_ASSERT(action);
1883 Q_ASSERT(menu);
1884
1885 const KToolBar* toolBarWidget = toolBar();
1886 foreach (const QWidget* widget, action->associatedWidgets()) {
1887 if (widget == toolBarWidget) {
1888 return false;
1889 }
1890 }
1891
1892 menu->addAction(action);
1893 return true;
1894 }
1895
1896 void DolphinMainWindow::rememberClosedTab(int index)
1897 {
1898 KMenu* tabsMenu = m_recentTabsMenu->menu();
1899
1900 const QString primaryPath = m_viewTab[index].primaryView->url().path();
1901 const QString iconName = KMimeType::iconNameForUrl(primaryPath);
1902
1903 QAction* action = new QAction(squeezedText(primaryPath), tabsMenu);
1904
1905 ClosedTab closedTab;
1906 closedTab.primaryUrl = m_viewTab[index].primaryView->url();
1907
1908 if (m_viewTab[index].secondaryView) {
1909 closedTab.secondaryUrl = m_viewTab[index].secondaryView->url();
1910 closedTab.isSplit = true;
1911 } else {
1912 closedTab.isSplit = false;
1913 }
1914
1915 action->setData(QVariant::fromValue(closedTab));
1916 action->setIcon(KIcon(iconName));
1917
1918 // add the closed tab menu entry after the separator and
1919 // "Empty Recently Closed Tabs" entry
1920 if (tabsMenu->actions().size() == 2) {
1921 tabsMenu->addAction(action);
1922 } else {
1923 tabsMenu->insertAction(tabsMenu->actions().at(2), action);
1924 }
1925
1926 // assure that only up to 8 closed tabs are shown in the menu
1927 if (tabsMenu->actions().size() > 8) {
1928 tabsMenu->removeAction(tabsMenu->actions().last());
1929 }
1930 actionCollection()->action("closed_tabs")->setEnabled(true);
1931 KAcceleratorManager::manage(tabsMenu);
1932 }
1933
1934 void DolphinMainWindow::refreshViews()
1935 {
1936 Q_ASSERT(m_viewTab[m_tabIndex].primaryView);
1937
1938 // remember the current active view, as because of
1939 // the refreshing the active view might change to
1940 // the secondary view
1941 DolphinViewContainer* activeViewContainer = m_activeViewContainer;
1942
1943 const int tabCount = m_viewTab.count();
1944 for (int i = 0; i < tabCount; ++i) {
1945 m_viewTab[i].primaryView->readSettings();
1946 if (m_viewTab[i].secondaryView) {
1947 m_viewTab[i].secondaryView->readSettings();
1948 }
1949 }
1950
1951 setActiveViewContainer(activeViewContainer);
1952
1953 if (GeneralSettings::modifiedStartupSettings()) {
1954 // The startup settings have been changed by the user (see bug #254947).
1955 // Synchronize the split-view setting with the active view:
1956 const bool splitView = GeneralSettings::splitView();
1957 const ViewTab& activeTab = m_viewTab[m_tabIndex];
1958 const bool toggle = ( splitView && !activeTab.secondaryView)
1959 || (!splitView && activeTab.secondaryView);
1960 if (toggle) {
1961 toggleSplitView();
1962 }
1963 }
1964
1965 emit settingsChanged();
1966 }
1967
1968 void DolphinMainWindow::clearStatusBar()
1969 {
1970 m_activeViewContainer->statusBar()->resetToDefaultText();
1971 }
1972
1973 void DolphinMainWindow::connectViewSignals(DolphinViewContainer* container)
1974 {
1975 connect(container, SIGNAL(showFilterBarChanged(bool)),
1976 this, SLOT(updateFilterBarAction(bool)));
1977 connect(container, SIGNAL(writeStateChanged(bool)),
1978 this, SLOT(slotWriteStateChanged(bool)));
1979
1980 DolphinView* view = container->view();
1981 connect(view, SIGNAL(selectionChanged(KFileItemList)),
1982 this, SLOT(slotSelectionChanged(KFileItemList)));
1983 connect(view, SIGNAL(requestItemInfo(KFileItem)),
1984 this, SLOT(slotRequestItemInfo(KFileItem)));
1985 connect(view, SIGNAL(activated()),
1986 this, SLOT(toggleActiveView()));
1987 connect(view, SIGNAL(tabRequested(KUrl)),
1988 this, SLOT(openNewTab(KUrl)));
1989 connect(view, SIGNAL(requestContextMenu(QPoint,KFileItem,KUrl,QList<QAction*>)),
1990 this, SLOT(openContextMenu(QPoint,KFileItem,KUrl,QList<QAction*>)));
1991 connect(view, SIGNAL(directoryLoadingStarted()),
1992 this, SLOT(enableStopAction()));
1993 connect(view, SIGNAL(directoryLoadingCompleted()),
1994 this, SLOT(disableStopAction()));
1995 connect(view, SIGNAL(goBackRequested()),
1996 this, SLOT(goBack()));
1997 connect(view, SIGNAL(goForwardRequested()),
1998 this, SLOT(goForward()));
1999
2000 const KUrlNavigator* navigator = container->urlNavigator();
2001 connect(navigator, SIGNAL(urlChanged(KUrl)),
2002 this, SLOT(changeUrl(KUrl)));
2003 connect(navigator, SIGNAL(historyChanged()),
2004 this, SLOT(updateHistory()));
2005 connect(navigator, SIGNAL(editableStateChanged(bool)),
2006 this, SLOT(slotEditableStateChanged(bool)));
2007 connect(navigator, SIGNAL(tabRequested(KUrl)),
2008 this, SLOT(openNewTab(KUrl)));
2009 }
2010
2011 void DolphinMainWindow::updateSplitAction()
2012 {
2013 QAction* splitAction = actionCollection()->action("split_view");
2014 if (m_viewTab[m_tabIndex].secondaryView) {
2015 if (m_activeViewContainer == m_viewTab[m_tabIndex].secondaryView) {
2016 splitAction->setText(i18nc("@action:intoolbar Close right view", "Close"));
2017 splitAction->setToolTip(i18nc("@info", "Close right view"));
2018 splitAction->setIcon(KIcon("view-right-close"));
2019 } else {
2020 splitAction->setText(i18nc("@action:intoolbar Close left view", "Close"));
2021 splitAction->setToolTip(i18nc("@info", "Close left view"));
2022 splitAction->setIcon(KIcon("view-left-close"));
2023 }
2024 } else {
2025 splitAction->setText(i18nc("@action:intoolbar Split view", "Split"));
2026 splitAction->setToolTip(i18nc("@info", "Split view"));
2027 splitAction->setIcon(KIcon("view-right-new"));
2028 }
2029 }
2030
2031 QString DolphinMainWindow::tabName(const KUrl& url) const
2032 {
2033 QString name;
2034 if (url.equals(KUrl("file:///"))) {
2035 name = '/';
2036 } else {
2037 name = url.fileName();
2038 if (name.isEmpty()) {
2039 name = url.protocol();
2040 } else {
2041 // Make sure that a '&' inside the directory name is displayed correctly
2042 // and not misinterpreted as a keyboard shortcut in QTabBar::setTabText()
2043 name.replace('&', "&&");
2044 }
2045 }
2046 return name;
2047 }
2048
2049 bool DolphinMainWindow::isKompareInstalled() const
2050 {
2051 static bool initialized = false;
2052 static bool installed = false;
2053 if (!initialized) {
2054 // TODO: maybe replace this approach later by using a menu
2055 // plugin like kdiff3plugin.cpp
2056 installed = !KGlobal::dirs()->findExe("kompare").isEmpty();
2057 initialized = true;
2058 }
2059 return installed;
2060 }
2061
2062 void DolphinMainWindow::createSecondaryView(int tabIndex)
2063 {
2064 ViewTab& viewTab = m_viewTab[tabIndex];
2065
2066 QSplitter* splitter = viewTab.splitter;
2067 const int newWidth = (viewTab.primaryView->width() - splitter->handleWidth()) / 2;
2068
2069 const DolphinView* view = viewTab.primaryView->view();
2070 // The final parent of the new view container will be set by adding it
2071 // to the splitter. However, we must make sure that the DolphinMainWindow
2072 // is a parent of the view container already when it is constructed
2073 // because this enables the container's KFileItemModel to assign its
2074 // dir lister to the right main window. The dir lister can then cache
2075 // authentication data.
2076 viewTab.secondaryView = createViewContainer(view->url(), this);
2077 splitter->addWidget(viewTab.secondaryView);
2078 splitter->setSizes(QList<int>() << newWidth << newWidth);
2079
2080 connectViewSignals(viewTab.secondaryView);
2081 viewTab.secondaryView->setActive(false);
2082 viewTab.secondaryView->resize(newWidth, viewTab.primaryView->height());
2083 viewTab.secondaryView->show();
2084 }
2085
2086 QString DolphinMainWindow::tabProperty(const QString& property, int tabIndex) const
2087 {
2088 return "Tab " + QString::number(tabIndex) + ' ' + property;
2089 }
2090
2091 void DolphinMainWindow::setUrlAsCaption(const KUrl& url)
2092 {
2093 QString caption;
2094 if (!url.isLocalFile()) {
2095 caption.append(url.protocol() + " - ");
2096 if (url.hasHost()) {
2097 caption.append(url.host() + " - ");
2098 }
2099 }
2100
2101 const QString fileName = url.fileName().isEmpty() ? "/" : url.fileName();
2102 caption.append(fileName);
2103
2104 setCaption(caption);
2105 }
2106
2107 QString DolphinMainWindow::squeezedText(const QString& text) const
2108 {
2109 const QFontMetrics fm = fontMetrics();
2110 return fm.elidedText(text, Qt::ElideMiddle, fm.maxWidth() * 10);
2111 }
2112
2113 void DolphinMainWindow::createPanelAction(const KIcon& icon,
2114 const QKeySequence& shortcut,
2115 QAction* dockAction,
2116 const QString& actionName)
2117 {
2118 KAction* panelAction = actionCollection()->addAction(actionName);
2119 panelAction->setCheckable(true);
2120 panelAction->setChecked(dockAction->isChecked());
2121 panelAction->setText(dockAction->text());
2122 panelAction->setIcon(icon);
2123 panelAction->setShortcut(shortcut);
2124
2125 connect(panelAction, SIGNAL(triggered()), dockAction, SLOT(trigger()));
2126 connect(dockAction, SIGNAL(toggled(bool)), panelAction, SLOT(setChecked(bool)));
2127 }
2128
2129 DolphinMainWindow::UndoUiInterface::UndoUiInterface() :
2130 KIO::FileUndoManager::UiInterface()
2131 {
2132 }
2133
2134 DolphinMainWindow::UndoUiInterface::~UndoUiInterface()
2135 {
2136 }
2137
2138 void DolphinMainWindow::UndoUiInterface::jobError(KIO::Job* job)
2139 {
2140 DolphinMainWindow* mainWin= qobject_cast<DolphinMainWindow *>(parentWidget());
2141 if (mainWin) {
2142 DolphinViewContainer* container = mainWin->activeViewContainer();
2143 container->showMessage(job->errorString(), DolphinViewContainer::Error);
2144 } else {
2145 KIO::FileUndoManager::UiInterface::jobError(job);
2146 }
2147 }
2148
2149 #include "dolphinmainwindow.moc"