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