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