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