File indexing completed on 2024-04-21 05:45:42

0001 /* This file is part of the KDE project
0002    SPDX-FileCopyrightText: 2007 David Faure <faure@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "dolphinpart.h"
0008 
0009 #include "dolphindebug.h"
0010 #include "dolphinnewfilemenu.h"
0011 #include "dolphinpart_ext.h"
0012 #include "dolphinremoveaction.h"
0013 #include "kitemviews/kfileitemmodel.h"
0014 #include "views/dolphinnewfilemenuobserver.h"
0015 #include "views/dolphinremoteencoding.h"
0016 #include "views/dolphinview.h"
0017 #include "views/dolphinviewactionhandler.h"
0018 
0019 #include <KActionCollection>
0020 #include <KAuthorized>
0021 #include <KConfigGroup>
0022 #include <KDialogJobUiDelegate>
0023 #include <KDirLister>
0024 #include <KFileItemListProperties>
0025 #include <KIO/CommandLauncherJob>
0026 #include <KIconLoader>
0027 #include <KJobWidgets>
0028 #include <KLocalizedString>
0029 #include <KMessageBox>
0030 #include <KMimeTypeEditor>
0031 #include <KPluginFactory>
0032 #include <KPluginMetaData>
0033 #include <KSharedConfig>
0034 #include <KTerminalLauncherJob>
0035 
0036 #include <QActionGroup>
0037 #include <QApplication>
0038 #include <QClipboard>
0039 #include <QDir>
0040 #include <QInputDialog>
0041 #include <QKeyEvent>
0042 #include <QMenu>
0043 #include <QRegularExpression>
0044 #include <QStandardPaths>
0045 #include <QTextDocument>
0046 
0047 #include <KPluginFactory>
0048 
0049 K_PLUGIN_CLASS_WITH_JSON(DolphinPart, "dolphinpart.json")
0050 
0051 DolphinPart::DolphinPart(QWidget *parentWidget, QObject *parent, const KPluginMetaData &metaData, const QVariantList &args)
0052     : KParts::ReadOnlyPart(parent, metaData)
0053     , m_openTerminalAction(nullptr)
0054     , m_removeAction(nullptr)
0055 {
0056     Q_UNUSED(args)
0057 
0058     m_extension = new DolphinPartBrowserExtension(this);
0059 
0060     // make sure that other apps using this part find Dolphin's view-file-columns icons
0061     KIconLoader::global()->addAppDir(QStringLiteral("dolphin"));
0062 
0063     m_view = new DolphinView(QUrl(), parentWidget);
0064     m_view->setTabsForFilesEnabled(true);
0065     setWidget(m_view);
0066 
0067     connect(&DolphinNewFileMenuObserver::instance(), &DolphinNewFileMenuObserver::errorMessage, this, &DolphinPart::slotErrorMessage);
0068 
0069     connect(m_view, &DolphinView::directoryLoadingCompleted, this, &KParts::ReadOnlyPart::completed);
0070     connect(m_view, &DolphinView::directoryLoadingCompleted, this, &DolphinPart::updatePasteAction);
0071     connect(m_view, &DolphinView::directoryLoadingProgress, this, &DolphinPart::updateProgress);
0072     connect(m_view, &DolphinView::errorMessage, this, &DolphinPart::slotErrorMessage);
0073 
0074     setXMLFile(QStringLiteral("dolphinpart.rc"));
0075 
0076     connect(m_view, &DolphinView::infoMessage, this, &DolphinPart::slotMessage);
0077     connect(m_view, &DolphinView::operationCompletedMessage, this, &DolphinPart::slotMessage);
0078     connect(m_view, &DolphinView::errorMessage, this, &DolphinPart::slotErrorMessage);
0079     connect(m_view, &DolphinView::itemActivated, this, &DolphinPart::slotItemActivated);
0080     connect(m_view, &DolphinView::itemsActivated, this, &DolphinPart::slotItemsActivated);
0081     connect(m_view, &DolphinView::statusBarTextChanged, this, [this](const QString &text) {
0082         const QString escapedText = Qt::convertFromPlainText(text);
0083         Q_EMIT ReadOnlyPart::setStatusBarText(QStringLiteral("<qt>%1</qt>").arg(escapedText));
0084     });
0085     connect(m_view, &DolphinView::tabRequested, this, &DolphinPart::createNewWindow);
0086     connect(m_view, &DolphinView::requestContextMenu, this, &DolphinPart::slotOpenContextMenu);
0087     connect(m_view, &DolphinView::selectionChanged, m_extension, &KParts::NavigationExtension::selectionInfo);
0088     connect(m_view, &DolphinView::selectionChanged, this, &DolphinPart::slotSelectionChanged);
0089     connect(m_view, &DolphinView::requestItemInfo, this, &DolphinPart::slotRequestItemInfo);
0090     connect(m_view, &DolphinView::modeChanged, this, &DolphinPart::viewModeChanged); // relay signal
0091     connect(m_view, &DolphinView::redirection, this, &DolphinPart::slotDirectoryRedirection);
0092 
0093     // Watch for changes that should result in updates to the
0094     // status bar text.
0095     connect(m_view, &DolphinView::itemCountChanged, this, &DolphinPart::updateStatusBar);
0096     connect(m_view, &DolphinView::selectionChanged, this, &DolphinPart::updateStatusBar);
0097 
0098     m_actionHandler = new DolphinViewActionHandler(actionCollection(), nullptr, this);
0099     m_actionHandler->setCurrentView(m_view);
0100     connect(m_actionHandler, &DolphinViewActionHandler::createDirectoryTriggered, this, &DolphinPart::createDirectory);
0101 
0102     m_remoteEncoding = new DolphinRemoteEncoding(this, m_actionHandler);
0103     connect(this, &DolphinPart::aboutToOpenURL, m_remoteEncoding, &DolphinRemoteEncoding::slotAboutToOpenUrl);
0104 
0105     QClipboard *clipboard = QApplication::clipboard();
0106     connect(clipboard, &QClipboard::dataChanged, this, &DolphinPart::updatePasteAction);
0107 
0108     // Create file info and listing filter extensions.
0109     // NOTE: Listing filter needs to be instantiated after the creation of the view.
0110     new DolphinPartFileInfoExtension(this);
0111 
0112     new DolphinPartListingFilterExtension(this);
0113 
0114     KDirLister *lister = m_view->m_model->m_dirLister;
0115     if (lister) {
0116         DolphinPartListingNotificationExtension *notifyExt = new DolphinPartListingNotificationExtension(this);
0117         connect(lister, &KDirLister::newItems, notifyExt, &DolphinPartListingNotificationExtension::slotNewItems);
0118         connect(lister, &KDirLister::itemsDeleted, notifyExt, &DolphinPartListingNotificationExtension::slotItemsDeleted);
0119     } else {
0120         qCWarning(DolphinDebug) << "NULL KDirLister object! KParts::ListingNotificationExtension will NOT be supported";
0121     }
0122 
0123     createActions();
0124     m_actionHandler->updateViewActions();
0125     slotSelectionChanged(KFileItemList()); // initially disable selection-dependent actions
0126 
0127     // Listen to events from the app so we can update the remove key by
0128     // checking for a Shift key press.
0129     qApp->installEventFilter(this);
0130 
0131     // TODO there was a "always open a new window" (when clicking on a directory) setting in konqueror
0132     // (sort of spacial navigation)
0133 }
0134 
0135 DolphinPart::~DolphinPart()
0136 {
0137 }
0138 
0139 void DolphinPart::createActions()
0140 {
0141     // Edit menu
0142 
0143     m_newFileMenu = new DolphinNewFileMenu(actionCollection()->action(QStringLiteral("create_dir")), this);
0144     m_newFileMenu->setParentWidget(widget());
0145     connect(m_newFileMenu->menu(), &QMenu::aboutToShow, this, &DolphinPart::updateNewMenu);
0146 
0147     QAction *editMimeTypeAction = actionCollection()->addAction(QStringLiteral("editMimeType"));
0148     editMimeTypeAction->setText(i18nc("@action:inmenu Edit", "&Edit File Type…"));
0149     connect(editMimeTypeAction, &QAction::triggered, this, &DolphinPart::slotEditMimeType);
0150 
0151     QAction *selectItemsMatching = actionCollection()->addAction(QStringLiteral("select_items_matching"));
0152     selectItemsMatching->setText(i18nc("@action:inmenu Edit", "Select Items Matching…"));
0153     actionCollection()->setDefaultShortcut(selectItemsMatching, Qt::CTRL | Qt::Key_S);
0154     connect(selectItemsMatching, &QAction::triggered, this, &DolphinPart::slotSelectItemsMatchingPattern);
0155 
0156     QAction *unselectItemsMatching = actionCollection()->addAction(QStringLiteral("unselect_items_matching"));
0157     unselectItemsMatching->setText(i18nc("@action:inmenu Edit", "Unselect Items Matching…"));
0158     connect(unselectItemsMatching, &QAction::triggered, this, &DolphinPart::slotUnselectItemsMatchingPattern);
0159 
0160     KStandardAction::selectAll(m_view, &DolphinView::selectAll, actionCollection());
0161 
0162     QAction *unselectAll = actionCollection()->addAction(QStringLiteral("unselect_all"));
0163     unselectAll->setText(i18nc("@action:inmenu Edit", "Unselect All"));
0164     connect(unselectAll, &QAction::triggered, m_view, &DolphinView::clearSelection);
0165 
0166     QAction *invertSelection = actionCollection()->addAction(QStringLiteral("invert_selection"));
0167     invertSelection->setText(i18nc("@action:inmenu Edit", "Invert Selection"));
0168     actionCollection()->setDefaultShortcut(invertSelection, Qt::CTRL | Qt::SHIFT | Qt::Key_A);
0169     connect(invertSelection, &QAction::triggered, m_view, &DolphinView::invertSelection);
0170 
0171     // View menu: all done by DolphinViewActionHandler
0172 
0173     // Go menu
0174 
0175     QActionGroup *goActionGroup = new QActionGroup(this);
0176     connect(goActionGroup, &QActionGroup::triggered, this, &DolphinPart::slotGoTriggered);
0177 
0178     createGoAction("go_applications", "start-here-kde", i18nc("@action:inmenu Go", "App&lications"), QStringLiteral("programs:/"), goActionGroup);
0179     createGoAction("go_network_folders", "folder-remote", i18nc("@action:inmenu Go", "&Network Folders"), QStringLiteral("remote:/"), goActionGroup);
0180     createGoAction("go_trash", "user-trash", i18nc("@action:inmenu Go", "Trash"), QStringLiteral("trash:/"), goActionGroup);
0181     createGoAction("go_autostart",
0182                    "",
0183                    i18nc("@action:inmenu Go", "Autostart"),
0184                    QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation) + "/autostart",
0185                    goActionGroup);
0186 
0187     // Tools menu
0188     m_findFileAction = KStandardAction::find(this, &DolphinPart::slotFindFile, actionCollection());
0189     m_findFileAction->setText(i18nc("@action:inmenu Tools", "Find File…"));
0190 
0191 #ifndef Q_OS_WIN
0192     if (KAuthorized::authorize(QStringLiteral("shell_access"))) {
0193         m_openTerminalAction = actionCollection()->addAction(QStringLiteral("open_terminal"));
0194         m_openTerminalAction->setIcon(QIcon::fromTheme(QStringLiteral("dialog-scripts")));
0195         m_openTerminalAction->setText(i18nc("@action:inmenu Tools", "Open &Terminal"));
0196         connect(m_openTerminalAction, &QAction::triggered, this, &DolphinPart::slotOpenTerminal);
0197         actionCollection()->setDefaultShortcut(m_openTerminalAction, Qt::Key_F4);
0198     }
0199 #endif
0200 }
0201 
0202 void DolphinPart::createGoAction(const char *name, const char *iconName, const QString &text, const QString &url, QActionGroup *actionGroup)
0203 {
0204     QAction *action = actionCollection()->addAction(name);
0205     action->setIcon(QIcon::fromTheme(iconName));
0206     action->setText(text);
0207     action->setData(url);
0208     action->setActionGroup(actionGroup);
0209 }
0210 
0211 void DolphinPart::slotGoTriggered(QAction *action)
0212 {
0213     const QString url = action->data().toString();
0214     Q_EMIT m_extension->openUrlRequest(QUrl(url));
0215 }
0216 
0217 void DolphinPart::slotSelectionChanged(const KFileItemList &selection)
0218 {
0219     const bool hasSelection = !selection.isEmpty();
0220 
0221     QAction *renameAction = actionCollection()->action(KStandardAction::name(KStandardAction::RenameFile));
0222     QAction *moveToTrashAction = actionCollection()->action(KStandardAction::name(KStandardAction::MoveToTrash));
0223     QAction *deleteAction = actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile));
0224     QAction *editMimeTypeAction = actionCollection()->action(QStringLiteral("editMimeType"));
0225     QAction *propertiesAction = actionCollection()->action(QStringLiteral("properties"));
0226     QAction *deleteWithTrashShortcut = actionCollection()->action(QStringLiteral("delete_shortcut")); // see DolphinViewActionHandler
0227 
0228     if (!hasSelection) {
0229         stateChanged(QStringLiteral("has_no_selection"));
0230 
0231         Q_EMIT m_extension->enableAction("cut", false);
0232         Q_EMIT m_extension->enableAction("copy", false);
0233         deleteWithTrashShortcut->setEnabled(false);
0234         editMimeTypeAction->setEnabled(false);
0235     } else {
0236         stateChanged(QStringLiteral("has_selection"));
0237 
0238         // TODO share this code with DolphinMainWindow::updateEditActions (and the desktop code)
0239         // in libkonq
0240         KFileItemListProperties capabilities(selection);
0241         const bool enableMoveToTrash = capabilities.isLocal() && capabilities.supportsMoving();
0242 
0243         renameAction->setEnabled(capabilities.supportsMoving());
0244         moveToTrashAction->setEnabled(enableMoveToTrash);
0245         deleteAction->setEnabled(capabilities.supportsDeleting());
0246         deleteWithTrashShortcut->setEnabled(capabilities.supportsDeleting() && !enableMoveToTrash);
0247         editMimeTypeAction->setEnabled(true);
0248         propertiesAction->setEnabled(true);
0249         Q_EMIT m_extension->enableAction("cut", capabilities.supportsMoving());
0250         Q_EMIT m_extension->enableAction("copy", true);
0251     }
0252 }
0253 
0254 void DolphinPart::updatePasteAction()
0255 {
0256     QPair<bool, QString> pasteInfo = m_view->pasteInfo();
0257     Q_EMIT m_extension->enableAction("paste", pasteInfo.first);
0258     Q_EMIT m_extension->setActionText("paste", pasteInfo.second);
0259 }
0260 
0261 QString DolphinPart::urlToLocalFilePath(const QUrl &url)
0262 {
0263     KIO::StatJob *statJob = KIO::mostLocalUrl(url);
0264     KJobWidgets::setWindow(statJob, widget());
0265     statJob->exec();
0266     QUrl localUrl = statJob->mostLocalUrl();
0267     if (localUrl.isLocalFile()) {
0268         return localUrl.toLocalFile();
0269     }
0270     return QString();
0271 }
0272 
0273 bool DolphinPart::openUrl(const QUrl &url)
0274 {
0275     bool reload = arguments().reload();
0276     // A bit of a workaround so that changing the namefilter works: force reload.
0277     // Otherwise DolphinView wouldn't relist the URL, so nothing would happen.
0278     if (m_nameFilter != m_view->nameFilter())
0279         reload = true;
0280     if (m_view->url() == url && !reload) { // DolphinView won't do anything in that case, so don't emit started
0281         return true;
0282     }
0283     setUrl(url); // remember url at the KParts level
0284     setLocalFilePath(urlToLocalFilePath(url)); // remember local path at the KParts level
0285     QUrl visibleUrl(url);
0286     if (!m_nameFilter.isEmpty()) {
0287         visibleUrl.setPath(visibleUrl.path() + '/' + m_nameFilter);
0288     }
0289     QString prettyUrl = visibleUrl.toDisplayString(QUrl::PreferLocalFile);
0290     Q_EMIT setWindowCaption(prettyUrl);
0291     Q_EMIT m_extension->setLocationBarUrl(prettyUrl);
0292     Q_EMIT started(nullptr); // get the wheel to spin
0293     m_view->setNameFilter(m_nameFilter);
0294     m_view->setUrl(url);
0295     updatePasteAction();
0296     Q_EMIT aboutToOpenURL();
0297     if (reload)
0298         m_view->reload();
0299     // Disable "Find File" and "Open Terminal" actions for non-file URLs,
0300     // e.g. ftp, smb, etc. #279283
0301     const bool isLocalUrl = !(localFilePath().isEmpty());
0302     m_findFileAction->setEnabled(isLocalUrl);
0303     if (m_openTerminalAction) {
0304         m_openTerminalAction->setEnabled(isLocalUrl);
0305     }
0306     return true;
0307 }
0308 
0309 void DolphinPart::slotMessage(const QString &msg)
0310 {
0311     Q_EMIT setStatusBarText(msg);
0312 }
0313 
0314 void DolphinPart::slotErrorMessage(const QString &msg)
0315 {
0316     qCDebug(DolphinDebug) << msg;
0317     Q_EMIT canceled(msg);
0318     //KMessageBox::error(m_view, msg);
0319 }
0320 
0321 void DolphinPart::slotRequestItemInfo(const KFileItem &item)
0322 {
0323     Q_EMIT m_extension->mouseOverInfo(item);
0324     if (item.isNull()) {
0325         updateStatusBar();
0326     } else {
0327         const QString escapedText = Qt::convertFromPlainText(item.getStatusBarInfo());
0328         Q_EMIT ReadOnlyPart::setStatusBarText(QStringLiteral("<qt>%1</qt>").arg(escapedText));
0329     }
0330 }
0331 
0332 void DolphinPart::slotItemActivated(const KFileItem &item)
0333 {
0334     KParts::OpenUrlArguments args;
0335     // Forget about the known mimetype if a target URL is used.
0336     // Testcase: network:/ with a item (mimetype "inode/some-foo-service") pointing to a http URL (html)
0337     if (item.targetUrl() == item.url()) {
0338         args.setMimeType(item.mimetype());
0339     }
0340 
0341     Q_EMIT m_extension->openUrlRequest(item.targetUrl(), args);
0342 }
0343 
0344 void DolphinPart::slotItemsActivated(const KFileItemList &items)
0345 {
0346     for (const KFileItem &item : items) {
0347         slotItemActivated(item);
0348     }
0349 }
0350 
0351 void DolphinPart::createNewWindow(const QUrl &url)
0352 {
0353     // TODO: Check issue N176832 for the missing QAIV signal; task 177399 - maybe this code
0354     // should be moved into DolphinPart::slotItemActivated()
0355     Q_EMIT m_extension->createNewWindow(url);
0356 }
0357 
0358 void DolphinPart::slotOpenContextMenu(const QPoint &pos, const KFileItem &_item, const KFileItemList &selectedItems, const QUrl &)
0359 {
0360     KParts::NavigationExtension::PopupFlags popupFlags =
0361         KParts::NavigationExtension::DefaultPopupItems | KParts::NavigationExtension::ShowProperties | KParts::NavigationExtension::ShowUrlOperations;
0362 
0363     KFileItem item(_item);
0364 
0365     if (item.isNull()) { // viewport context menu
0366         item = m_view->rootItem();
0367         if (item.isNull())
0368             item = KFileItem(url());
0369         else
0370             item.setUrl(url()); // ensure we use the view url, not the canonical path (#213799)
0371     }
0372 
0373     KFileItemList items;
0374     if (selectedItems.isEmpty()) {
0375         items.append(item);
0376     } else {
0377         items = selectedItems;
0378     }
0379 
0380     KFileItemListProperties capabilities(items);
0381 
0382     KParts::NavigationExtension::ActionGroupMap actionGroups;
0383     QList<QAction *> editActions;
0384     editActions += m_view->versionControlActions(m_view->selectedItems());
0385 
0386     if (!_item.isNull()) { // only for context menu on one or more items
0387         const bool supportsMoving = capabilities.supportsMoving();
0388 
0389         if (capabilities.supportsDeleting()) {
0390             const bool showDeleteAction =
0391                 (KSharedConfig::openConfig()->group(QStringLiteral("KDE")).readEntry("ShowDeleteCommand", false) || !item.isLocalFile());
0392             const bool showMoveToTrashAction = capabilities.isLocal() && supportsMoving;
0393 
0394             if (showDeleteAction && showMoveToTrashAction) {
0395                 delete m_removeAction;
0396                 m_removeAction = nullptr;
0397                 editActions.append(actionCollection()->action(KStandardAction::name(KStandardAction::MoveToTrash)));
0398                 editActions.append(actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile)));
0399             } else if (showDeleteAction && !showMoveToTrashAction) {
0400                 editActions.append(actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile)));
0401             } else {
0402                 if (!m_removeAction)
0403                     m_removeAction = new DolphinRemoveAction(this, actionCollection());
0404                 editActions.append(m_removeAction);
0405                 m_removeAction->update();
0406             }
0407         } else {
0408             popupFlags |= KParts::NavigationExtension::NoDeletion;
0409         }
0410 
0411         if (supportsMoving) {
0412             editActions.append(actionCollection()->action(KStandardAction::name(KStandardAction::RenameFile)));
0413         }
0414 
0415         // Normally KonqPopupMenu only shows the "Create new" submenu in the current view
0416         // since otherwise the created file would not be visible.
0417         // But in treeview mode we should allow it.
0418         if (m_view->itemsExpandable())
0419             popupFlags |= KParts::NavigationExtension::ShowCreateDirectory;
0420     }
0421 
0422     actionGroups.insert(QStringLiteral("editactions"), editActions);
0423 
0424     Q_EMIT m_extension->popupMenu(pos, items, KParts::OpenUrlArguments(), popupFlags, actionGroups);
0425 }
0426 
0427 void DolphinPart::slotDirectoryRedirection(const QUrl &oldUrl, const QUrl &newUrl)
0428 {
0429     qCDebug(DolphinDebug) << oldUrl << newUrl << "currentUrl=" << url();
0430     if (oldUrl.matches(url(), QUrl::StripTrailingSlash /* #207572 */)) {
0431         KParts::ReadOnlyPart::setUrl(newUrl);
0432         const QString prettyUrl = newUrl.toDisplayString(QUrl::PreferLocalFile);
0433         Q_EMIT m_extension->setLocationBarUrl(prettyUrl);
0434     }
0435 }
0436 
0437 void DolphinPart::slotEditMimeType()
0438 {
0439     const KFileItemList items = m_view->selectedItems();
0440     if (!items.isEmpty()) {
0441         KMimeTypeEditor::editMimeType(items.first().mimetype(), m_view);
0442     }
0443 }
0444 
0445 void DolphinPart::slotSelectItemsMatchingPattern()
0446 {
0447     openSelectionDialog(i18nc("@title:window", "Select"), i18n("Select all items matching this pattern:"), true);
0448 }
0449 
0450 void DolphinPart::slotUnselectItemsMatchingPattern()
0451 {
0452     openSelectionDialog(i18nc("@title:window", "Unselect"), i18n("Unselect all items matching this pattern:"), false);
0453 }
0454 
0455 void DolphinPart::openSelectionDialog(const QString &title, const QString &text, bool selectItems)
0456 {
0457     auto *dialog = new QInputDialog(m_view);
0458     dialog->setAttribute(Qt::WA_DeleteOnClose, true);
0459     dialog->setInputMode(QInputDialog::TextInput);
0460     dialog->setWindowTitle(title);
0461     dialog->setLabelText(text);
0462 
0463     const KConfigGroup group = KSharedConfig::openConfig("dolphinpartrc")->group(QStringLiteral("Select Dialog"));
0464     dialog->setComboBoxEditable(true);
0465     dialog->setComboBoxItems(group.readEntry("History", QStringList()));
0466 
0467     dialog->setTextValue(QStringLiteral("*"));
0468 
0469     connect(dialog, &QDialog::accepted, this, [=]() {
0470         const QString pattern = dialog->textValue();
0471         if (!pattern.isEmpty()) {
0472             QStringList items = dialog->comboBoxItems();
0473             items.removeAll(pattern);
0474             items.prepend(pattern);
0475 
0476             // Need to evaluate this again here, because the captured value is const
0477             // (even if the const were removed from 'const KConfigGroup group =' above).
0478             KConfigGroup group = KSharedConfig::openConfig("dolphinpartrc")->group(QStringLiteral("Select Dialog"));
0479             // Limit the size of the saved history.
0480             group.writeEntry("History", items.mid(0, 10));
0481             group.sync();
0482 
0483             const QRegularExpression patternRegExp(QRegularExpression::wildcardToRegularExpression(pattern));
0484             m_view->selectItems(patternRegExp, selectItems);
0485         }
0486     });
0487 
0488     dialog->open();
0489 }
0490 
0491 void DolphinPart::setCurrentViewMode(const QString &viewModeName)
0492 {
0493     QAction *action = actionCollection()->action(viewModeName);
0494     Q_ASSERT(action);
0495     action->trigger();
0496 }
0497 
0498 QString DolphinPart::currentViewMode() const
0499 {
0500     return m_actionHandler->currentViewModeActionName();
0501 }
0502 
0503 void DolphinPart::setNameFilter(const QString &nameFilter)
0504 {
0505     // This is the "/home/dfaure/*.diff" kind of name filter (KDirLister::setNameFilter)
0506     // which is unrelated to DolphinView::setNameFilter which is substring filtering in a proxy.
0507     m_nameFilter = nameFilter;
0508     // TODO save/restore name filter in saveState/restoreState like KonqDirPart did in kde3?
0509 }
0510 
0511 QString DolphinPart::localFilePathOrHome() const
0512 {
0513     const QString localPath = localFilePath();
0514     if (!localPath.isEmpty()) {
0515         return localPath;
0516     }
0517     return QDir::homePath();
0518 }
0519 
0520 void DolphinPart::slotOpenTerminal()
0521 {
0522     auto job = new KTerminalLauncherJob(QString());
0523     job->setWorkingDirectory(localFilePathOrHome());
0524     job->start();
0525 }
0526 
0527 void DolphinPart::slotFindFile()
0528 {
0529     KIO::CommandLauncherJob *job = new KIO::CommandLauncherJob(QStringLiteral("kfind"), {url().toString()}, this);
0530     job->setDesktopName(QStringLiteral("org.kde.kfind"));
0531     job->setUiDelegate(new KDialogJobUiDelegate(KJobUiDelegate::AutoHandlingEnabled, widget()));
0532     job->start();
0533 }
0534 
0535 void DolphinPart::updateNewMenu()
0536 {
0537     // As requested by KNewFileMenu :
0538     m_newFileMenu->checkUpToDate();
0539     // And set the files that the menu apply on :
0540     m_newFileMenu->setWorkingDirectory(url());
0541 }
0542 
0543 void DolphinPart::updateStatusBar()
0544 {
0545     m_view->requestStatusBarText();
0546 }
0547 
0548 void DolphinPart::updateProgress(int percent)
0549 {
0550     Q_EMIT m_extension->loadingProgress(percent);
0551 }
0552 
0553 void DolphinPart::createDirectory()
0554 {
0555     m_newFileMenu->setWorkingDirectory(url());
0556     m_newFileMenu->createDirectory();
0557 }
0558 
0559 void DolphinPart::setFilesToSelect(const QList<QUrl> &files)
0560 {
0561     if (files.isEmpty()) {
0562         return;
0563     }
0564 
0565     m_view->markUrlsAsSelected(files);
0566     m_view->markUrlAsCurrent(files.at(0));
0567 }
0568 
0569 bool DolphinPart::eventFilter(QObject *obj, QEvent *event)
0570 {
0571     using ShiftState = DolphinRemoveAction::ShiftState;
0572     const int type = event->type();
0573 
0574     if ((type == QEvent::KeyPress || type == QEvent::KeyRelease) && m_removeAction) {
0575         QMenu *menu = qobject_cast<QMenu *>(obj);
0576         if (menu && menu->parent() == m_view) {
0577             QKeyEvent *ev = static_cast<QKeyEvent *>(event);
0578             if (ev->key() == Qt::Key_Shift) {
0579                 m_removeAction->update(type == QEvent::KeyPress ? ShiftState::Pressed : ShiftState::Released);
0580             }
0581         }
0582     }
0583 
0584     return KParts::ReadOnlyPart::eventFilter(obj, event);
0585 }
0586 
0587 #include "dolphinpart.moc"
0588 #include "moc_dolphinpart.cpp"