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