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