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