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