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