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