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