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