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