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