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