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