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