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