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