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