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