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