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