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