]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinpart.cpp
Fix Wayland window activation when attaching to an existing instance
[dolphin.git] / src / dolphinpart.cpp
1 /* This file is part of the KDE project
2 SPDX-FileCopyrightText: 2007 David Faure <faure@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6
7 #include "dolphinpart.h"
8
9 #include "dolphindebug.h"
10 #include "dolphinnewfilemenu.h"
11 #include "dolphinpart_ext.h"
12 #include "dolphinremoveaction.h"
13 #include "kitemviews/kfileitemmodel.h"
14 #include "views/dolphinnewfilemenuobserver.h"
15 #include "views/dolphinremoteencoding.h"
16 #include "views/dolphinview.h"
17 #include "views/dolphinviewactionhandler.h"
18
19 #include <KActionCollection>
20 #include <KAuthorized>
21 #include <KConfigGroup>
22 #include <KDialogJobUiDelegate>
23 #include <KDirLister>
24 #include <KFileItemListProperties>
25 #include <KIO/CommandLauncherJob>
26 #include <KIconLoader>
27 #include <KJobWidgets>
28 #include <KLocalizedString>
29 #include <KMessageBox>
30 #include <KMimeTypeEditor>
31 #include <KMoreToolsMenuFactory>
32 #include <KPluginFactory>
33 #include <KPluginMetaData>
34 #include <KSharedConfig>
35 #include <KTerminalLauncherJob>
36 #include <kio_version.h>
37
38 #include <QActionGroup>
39 #include <QApplication>
40 #include <QClipboard>
41 #include <QDir>
42 #include <QInputDialog>
43 #include <QKeyEvent>
44 #include <QMenu>
45 #include <QRegularExpression>
46 #include <QStandardPaths>
47 #include <QTextDocument>
48
49 K_PLUGIN_CLASS_WITH_JSON(DolphinPart, "dolphinpart.json")
50
51 DolphinPart::DolphinPart(QWidget* parentWidget, QObject* parent,
52 const KPluginMetaData& metaData, const QVariantList& args)
53 : KParts::ReadOnlyPart(parent)
54 ,m_openTerminalAction(nullptr)
55 ,m_removeAction(nullptr)
56 {
57 Q_UNUSED(args)
58 setMetaData(metaData);
59
60 m_extension = new DolphinPartBrowserExtension(this);
61
62 // make sure that other apps using this part find Dolphin's view-file-columns icons
63 KIconLoader::global()->addAppDir(QStringLiteral("dolphin"));
64
65 m_view = new DolphinView(QUrl(), parentWidget);
66 m_view->setTabsForFilesEnabled(true);
67 setWidget(m_view);
68
69 connect(&DolphinNewFileMenuObserver::instance(), &DolphinNewFileMenuObserver::errorMessage,
70 this, &DolphinPart::slotErrorMessage);
71
72 connect(m_view, &DolphinView::directoryLoadingCompleted, this, &KParts::ReadOnlyPart::completed);
73 connect(m_view, &DolphinView::directoryLoadingCompleted, this, &DolphinPart::updatePasteAction);
74 connect(m_view, &DolphinView::directoryLoadingProgress, this, &DolphinPart::updateProgress);
75 connect(m_view, &DolphinView::errorMessage, this, &DolphinPart::slotErrorMessage);
76
77 setXMLFile(QStringLiteral("dolphinpart.rc"));
78
79 connect(m_view, &DolphinView::infoMessage,
80 this, &DolphinPart::slotMessage);
81 connect(m_view, &DolphinView::operationCompletedMessage,
82 this, &DolphinPart::slotMessage);
83 connect(m_view, &DolphinView::errorMessage,
84 this, &DolphinPart::slotErrorMessage);
85 connect(m_view, &DolphinView::itemActivated,
86 this, &DolphinPart::slotItemActivated);
87 connect(m_view, &DolphinView::itemsActivated,
88 this, &DolphinPart::slotItemsActivated);
89 connect(m_view, &DolphinView::statusBarTextChanged, this, [this](const QString &text) {
90 const QString escapedText = Qt::convertFromPlainText(text);
91 Q_EMIT ReadOnlyPart::setStatusBarText(QStringLiteral("<qt>%1</qt>").arg(escapedText));
92 });
93 connect(m_view, &DolphinView::tabRequested,
94 this, &DolphinPart::createNewWindow);
95 connect(m_view, &DolphinView::requestContextMenu,
96 this, &DolphinPart::slotOpenContextMenu);
97 connect(m_view, &DolphinView::selectionChanged,
98 m_extension, &KParts::BrowserExtension::selectionInfo);
99 connect(m_view, &DolphinView::selectionChanged,
100 this, &DolphinPart::slotSelectionChanged);
101 connect(m_view, &DolphinView::requestItemInfo,
102 this, &DolphinPart::slotRequestItemInfo);
103 connect(m_view, &DolphinView::modeChanged,
104 this, &DolphinPart::viewModeChanged); // relay signal
105 connect(m_view, &DolphinView::redirection,
106 this, &DolphinPart::slotDirectoryRedirection);
107
108 // Watch for changes that should result in updates to the
109 // status bar text.
110 connect(m_view, &DolphinView::itemCountChanged, this, &DolphinPart::updateStatusBar);
111 connect(m_view, &DolphinView::selectionChanged, this, &DolphinPart::updateStatusBar);
112
113 m_actionHandler = new DolphinViewActionHandler(actionCollection(), nullptr, this);
114 m_actionHandler->setCurrentView(m_view);
115 connect(m_actionHandler, &DolphinViewActionHandler::createDirectoryTriggered, this, &DolphinPart::createDirectory);
116
117 m_remoteEncoding = new DolphinRemoteEncoding(this, m_actionHandler);
118 connect(this, &DolphinPart::aboutToOpenURL,
119 m_remoteEncoding, &DolphinRemoteEncoding::slotAboutToOpenUrl);
120
121 QClipboard* clipboard = QApplication::clipboard();
122 connect(clipboard, &QClipboard::dataChanged,
123 this, &DolphinPart::updatePasteAction);
124
125 // Create file info and listing filter extensions.
126 // NOTE: Listing filter needs to be instantiated after the creation of the view.
127 new DolphinPartFileInfoExtension(this);
128
129 new DolphinPartListingFilterExtension(this);
130
131 KDirLister* lister = m_view->m_model->m_dirLister;
132 if (lister) {
133 DolphinPartListingNotificationExtension* notifyExt = new DolphinPartListingNotificationExtension(this);
134 connect(lister, &KDirLister::newItems, notifyExt, &DolphinPartListingNotificationExtension::slotNewItems);
135 connect(lister, &KDirLister::itemsDeleted, notifyExt, &DolphinPartListingNotificationExtension::slotItemsDeleted);
136 } else {
137 qCWarning(DolphinDebug) << "NULL KDirLister object! KParts::ListingNotificationExtension will NOT be supported";
138 }
139
140 createActions();
141 m_actionHandler->updateViewActions();
142 slotSelectionChanged(KFileItemList()); // initially disable selection-dependent actions
143
144 // Listen to events from the app so we can update the remove key by
145 // checking for a Shift key press.
146 qApp->installEventFilter(this);
147
148 // TODO there was a "always open a new window" (when clicking on a directory) setting in konqueror
149 // (sort of spacial navigation)
150 }
151
152 DolphinPart::~DolphinPart()
153 {
154 }
155
156 void DolphinPart::createActions()
157 {
158 // Edit menu
159
160 m_newFileMenu = new DolphinNewFileMenu(actionCollection(), this);
161 m_newFileMenu->setParentWidget(widget());
162 connect(m_newFileMenu->menu(), &QMenu::aboutToShow,
163 this, &DolphinPart::updateNewMenu);
164
165 QAction *editMimeTypeAction = actionCollection()->addAction( QStringLiteral("editMimeType") );
166 editMimeTypeAction->setText( i18nc("@action:inmenu Edit", "&Edit File Type..." ) );
167 connect(editMimeTypeAction, &QAction::triggered, this, &DolphinPart::slotEditMimeType);
168
169 QAction* selectItemsMatching = actionCollection()->addAction(QStringLiteral("select_items_matching"));
170 selectItemsMatching->setText(i18nc("@action:inmenu Edit", "Select Items Matching..."));
171 actionCollection()->setDefaultShortcut(selectItemsMatching, Qt::CTRL | Qt::Key_S);
172 connect(selectItemsMatching, &QAction::triggered, this, &DolphinPart::slotSelectItemsMatchingPattern);
173
174 QAction* unselectItemsMatching = actionCollection()->addAction(QStringLiteral("unselect_items_matching"));
175 unselectItemsMatching->setText(i18nc("@action:inmenu Edit", "Unselect Items Matching..."));
176 connect(unselectItemsMatching, &QAction::triggered, this, &DolphinPart::slotUnselectItemsMatchingPattern);
177
178 KStandardAction::selectAll(m_view, &DolphinView::selectAll, actionCollection());
179
180 QAction* unselectAll = actionCollection()->addAction(QStringLiteral("unselect_all"));
181 unselectAll->setText(i18nc("@action:inmenu Edit", "Unselect All"));
182 connect(unselectAll, &QAction::triggered, m_view, &DolphinView::clearSelection);
183
184 QAction* invertSelection = actionCollection()->addAction(QStringLiteral("invert_selection"));
185 invertSelection->setText(i18nc("@action:inmenu Edit", "Invert Selection"));
186 actionCollection()->setDefaultShortcut(invertSelection, Qt::CTRL | Qt::SHIFT | Qt::Key_A);
187 connect(invertSelection, &QAction::triggered, m_view, &DolphinView::invertSelection);
188
189 // View menu: all done by DolphinViewActionHandler
190
191 // Go menu
192
193 QActionGroup* goActionGroup = new QActionGroup(this);
194 connect(goActionGroup, &QActionGroup::triggered,
195 this, &DolphinPart::slotGoTriggered);
196
197 createGoAction("go_applications", "start-here-kde",
198 i18nc("@action:inmenu Go", "App&lications"), QStringLiteral("programs:/"),
199 goActionGroup);
200 createGoAction("go_network_folders", "folder-remote",
201 i18nc("@action:inmenu Go", "&Network Folders"), QStringLiteral("remote:/"),
202 goActionGroup);
203 createGoAction("go_trash", "user-trash",
204 i18nc("@action:inmenu Go", "Trash"), QStringLiteral("trash:/"),
205 goActionGroup);
206 createGoAction("go_autostart", "",
207 i18nc("@action:inmenu Go", "Autostart"), QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation) + "/autostart",
208 goActionGroup);
209
210 // Tools menu
211 m_findFileAction = KStandardAction::find(this, &DolphinPart::slotFindFile, actionCollection());
212 m_findFileAction->setText(i18nc("@action:inmenu Tools", "Find File..."));
213
214 #ifndef Q_OS_WIN
215 if (KAuthorized::authorize(QStringLiteral("shell_access"))) {
216 m_openTerminalAction = actionCollection()->addAction(QStringLiteral("open_terminal"));
217 m_openTerminalAction->setIcon(QIcon::fromTheme(QStringLiteral("dialog-scripts")));
218 m_openTerminalAction->setText(i18nc("@action:inmenu Tools", "Open &Terminal"));
219 connect(m_openTerminalAction, &QAction::triggered, this, &DolphinPart::slotOpenTerminal);
220 actionCollection()->setDefaultShortcut(m_openTerminalAction, Qt::Key_F4);
221 }
222 #endif
223 }
224
225 void DolphinPart::createGoAction(const char* name, const char* iconName,
226 const QString& text, const QString& url,
227 QActionGroup* actionGroup)
228 {
229 QAction* action = actionCollection()->addAction(name);
230 action->setIcon(QIcon::fromTheme(iconName));
231 action->setText(text);
232 action->setData(url);
233 action->setActionGroup(actionGroup);
234 }
235
236 void DolphinPart::slotGoTriggered(QAction* action)
237 {
238 const QString url = action->data().toString();
239 Q_EMIT m_extension->openUrlRequest(QUrl(url));
240 }
241
242 void DolphinPart::slotSelectionChanged(const KFileItemList& selection)
243 {
244 const bool hasSelection = !selection.isEmpty();
245
246 QAction* renameAction = actionCollection()->action(KStandardAction::name(KStandardAction::RenameFile));
247 QAction* moveToTrashAction = actionCollection()->action(KStandardAction::name(KStandardAction::MoveToTrash));
248 QAction* deleteAction = actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile));
249 QAction* editMimeTypeAction = actionCollection()->action(QStringLiteral("editMimeType"));
250 QAction* propertiesAction = actionCollection()->action(QStringLiteral("properties"));
251 QAction* deleteWithTrashShortcut = actionCollection()->action(QStringLiteral("delete_shortcut")); // see DolphinViewActionHandler
252
253 if (!hasSelection) {
254 stateChanged(QStringLiteral("has_no_selection"));
255
256 Q_EMIT m_extension->enableAction("cut", false);
257 Q_EMIT m_extension->enableAction("copy", false);
258 deleteWithTrashShortcut->setEnabled(false);
259 editMimeTypeAction->setEnabled(false);
260 } else {
261 stateChanged(QStringLiteral("has_selection"));
262
263 // TODO share this code with DolphinMainWindow::updateEditActions (and the desktop code)
264 // in libkonq
265 KFileItemListProperties capabilities(selection);
266 const bool enableMoveToTrash = capabilities.isLocal() && capabilities.supportsMoving();
267
268 renameAction->setEnabled(capabilities.supportsMoving());
269 moveToTrashAction->setEnabled(enableMoveToTrash);
270 deleteAction->setEnabled(capabilities.supportsDeleting());
271 deleteWithTrashShortcut->setEnabled(capabilities.supportsDeleting() && !enableMoveToTrash);
272 editMimeTypeAction->setEnabled(true);
273 propertiesAction->setEnabled(true);
274 Q_EMIT m_extension->enableAction("cut", capabilities.supportsMoving());
275 Q_EMIT m_extension->enableAction("copy", true);
276 }
277 }
278
279 void DolphinPart::updatePasteAction()
280 {
281 QPair<bool, QString> pasteInfo = m_view->pasteInfo();
282 Q_EMIT m_extension->enableAction( "paste", pasteInfo.first );
283 Q_EMIT m_extension->setActionText( "paste", pasteInfo.second );
284 }
285
286 QString DolphinPart::urlToLocalFilePath(const QUrl &url)
287 {
288 KIO::StatJob* statJob = KIO::mostLocalUrl(url);
289 KJobWidgets::setWindow(statJob, widget());
290 statJob->exec();
291 QUrl localUrl = statJob->mostLocalUrl();
292 if (localUrl.isLocalFile()) {
293 return localUrl.toLocalFile();
294 }
295 return QString();
296 }
297
298 bool DolphinPart::openUrl(const QUrl &url)
299 {
300 bool reload = arguments().reload();
301 // A bit of a workaround so that changing the namefilter works: force reload.
302 // Otherwise DolphinView wouldn't relist the URL, so nothing would happen.
303 if (m_nameFilter != m_view->nameFilter())
304 reload = true;
305 if (m_view->url() == url && !reload) { // DolphinView won't do anything in that case, so don't emit started
306 return true;
307 }
308 setUrl(url); // remember url at the KParts level
309 setLocalFilePath(urlToLocalFilePath(url)); // remember local path at the KParts level
310 QUrl visibleUrl(url);
311 if (!m_nameFilter.isEmpty()) {
312 visibleUrl.setPath(visibleUrl.path() + '/' + m_nameFilter);
313 }
314 QString prettyUrl = visibleUrl.toDisplayString(QUrl::PreferLocalFile);
315 Q_EMIT setWindowCaption(prettyUrl);
316 Q_EMIT m_extension->setLocationBarUrl(prettyUrl);
317 Q_EMIT started(nullptr); // get the wheel to spin
318 m_view->setNameFilter(m_nameFilter);
319 m_view->setUrl(url);
320 updatePasteAction();
321 Q_EMIT aboutToOpenURL();
322 if (reload)
323 m_view->reload();
324 // Disable "Find File" and "Open Terminal" actions for non-file URLs,
325 // e.g. ftp, smb, etc. #279283
326 const bool isLocalUrl = !(localFilePath().isEmpty());
327 m_findFileAction->setEnabled(isLocalUrl);
328 if (m_openTerminalAction) {
329 m_openTerminalAction->setEnabled(isLocalUrl);
330 }
331 return true;
332 }
333
334 void DolphinPart::slotMessage(const QString& msg)
335 {
336 Q_EMIT setStatusBarText(msg);
337 }
338
339 void DolphinPart::slotErrorMessage(const QString& msg)
340 {
341 qCDebug(DolphinDebug) << msg;
342 Q_EMIT canceled(msg);
343 //KMessageBox::error(m_view, msg);
344 }
345
346 void DolphinPart::slotRequestItemInfo(const KFileItem& item)
347 {
348 Q_EMIT m_extension->mouseOverInfo(item);
349 if (item.isNull()) {
350 updateStatusBar();
351 } else {
352 const QString escapedText = Qt::convertFromPlainText(item.getStatusBarInfo());
353 Q_EMIT ReadOnlyPart::setStatusBarText(QStringLiteral("<qt>%1</qt>").arg(escapedText));
354 }
355 }
356
357 void DolphinPart::slotItemActivated(const KFileItem& item)
358 {
359 KParts::OpenUrlArguments args;
360 // Forget about the known mimetype if a target URL is used.
361 // Testcase: network:/ with a item (mimetype "inode/some-foo-service") pointing to a http URL (html)
362 if (item.targetUrl() == item.url()) {
363 args.setMimeType(item.mimetype());
364 }
365
366 // Ideally, konqueror should be changed to not require trustedSource for directory views,
367 // since the idea was not to need BrowserArguments for non-browser stuff...
368 KParts::BrowserArguments browserArgs;
369 browserArgs.trustedSource = true;
370 Q_EMIT m_extension->openUrlRequest(item.targetUrl(), args, browserArgs);
371 }
372
373 void DolphinPart::slotItemsActivated(const KFileItemList& items)
374 {
375 for (const KFileItem& item : items) {
376 slotItemActivated(item);
377 }
378 }
379
380 void DolphinPart::createNewWindow(const QUrl& url)
381 {
382 // TODO: Check issue N176832 for the missing QAIV signal; task 177399 - maybe this code
383 // should be moved into DolphinPart::slotItemActivated()
384 Q_EMIT m_extension->createNewWindow(url);
385 }
386
387 void DolphinPart::slotOpenContextMenu(const QPoint& pos,
388 const KFileItem& _item,
389 const KFileItemList &selectedItems,
390 const QUrl &)
391 {
392 KParts::BrowserExtension::PopupFlags popupFlags = KParts::BrowserExtension::DefaultPopupItems
393 | KParts::BrowserExtension::ShowProperties
394 | KParts::BrowserExtension::ShowUrlOperations;
395
396 KFileItem item(_item);
397
398 if (item.isNull()) { // viewport context menu
399 item = m_view->rootItem();
400 if (item.isNull())
401 item = KFileItem(url());
402 else
403 item.setUrl(url()); // ensure we use the view url, not the canonical path (#213799)
404 }
405
406 KFileItemList items;
407 if (selectedItems.isEmpty()) {
408 items.append(item);
409 } else {
410 items = selectedItems;
411 }
412
413 KFileItemListProperties capabilities(items);
414
415 KParts::BrowserExtension::ActionGroupMap actionGroups;
416 QList<QAction *> editActions;
417 editActions += m_view->versionControlActions(m_view->selectedItems());
418
419 if (!_item.isNull()) { // only for context menu on one or more items
420 const bool supportsMoving = capabilities.supportsMoving();
421
422 if (capabilities.supportsDeleting()) {
423 const bool showDeleteAction = (KSharedConfig::openConfig()->group("KDE").readEntry("ShowDeleteCommand", false) ||
424 !item.isLocalFile());
425 const bool showMoveToTrashAction = capabilities.isLocal() && supportsMoving;
426
427 if (showDeleteAction && showMoveToTrashAction) {
428 delete m_removeAction;
429 m_removeAction = nullptr;
430 editActions.append(actionCollection()->action(KStandardAction::name(KStandardAction::MoveToTrash)));
431 editActions.append(actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile)));
432 } else if (showDeleteAction && !showMoveToTrashAction) {
433 editActions.append(actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile)));
434 } else {
435 if (!m_removeAction)
436 m_removeAction = new DolphinRemoveAction(this, actionCollection());
437 editActions.append(m_removeAction);
438 m_removeAction->update();
439 }
440 } else {
441 popupFlags |= KParts::BrowserExtension::NoDeletion;
442 }
443
444 if (supportsMoving) {
445 editActions.append(actionCollection()->action(KStandardAction::name(KStandardAction::RenameFile)));
446 }
447
448 // Normally KonqPopupMenu only shows the "Create new" submenu in the current view
449 // since otherwise the created file would not be visible.
450 // But in treeview mode we should allow it.
451 if (m_view->itemsExpandable())
452 popupFlags |= KParts::BrowserExtension::ShowCreateDirectory;
453
454 }
455
456 actionGroups.insert(QStringLiteral("editactions"), editActions);
457
458 Q_EMIT m_extension->popupMenu(pos,
459 items,
460 KParts::OpenUrlArguments(),
461 KParts::BrowserArguments(),
462 popupFlags,
463 actionGroups);
464 }
465
466 void DolphinPart::slotDirectoryRedirection(const QUrl &oldUrl, const QUrl &newUrl)
467 {
468 qCDebug(DolphinDebug) << oldUrl << newUrl << "currentUrl=" << url();
469 if (oldUrl.matches(url(), QUrl::StripTrailingSlash /* #207572 */)) {
470 KParts::ReadOnlyPart::setUrl(newUrl);
471 const QString prettyUrl = newUrl.toDisplayString(QUrl::PreferLocalFile);
472 Q_EMIT m_extension->setLocationBarUrl(prettyUrl);
473 }
474 }
475
476
477 void DolphinPart::slotEditMimeType()
478 {
479 const KFileItemList items = m_view->selectedItems();
480 if (!items.isEmpty()) {
481 KMimeTypeEditor::editMimeType(items.first().mimetype(), m_view);
482 }
483 }
484
485 void DolphinPart::slotSelectItemsMatchingPattern()
486 {
487 openSelectionDialog(i18nc("@title:window", "Select"),
488 i18n("Select all items matching this pattern:"),
489 true);
490 }
491
492 void DolphinPart::slotUnselectItemsMatchingPattern()
493 {
494 openSelectionDialog(i18nc("@title:window", "Unselect"),
495 i18n("Unselect all items matching this pattern:"),
496 false);
497 }
498
499 void DolphinPart::openSelectionDialog(const QString& title, const QString& text, bool selectItems)
500 {
501 auto *dialog = new QInputDialog(m_view);
502 dialog->setAttribute(Qt::WA_DeleteOnClose, true);
503 dialog->setInputMode(QInputDialog::TextInput);
504 dialog->setWindowTitle(title);
505 dialog->setLabelText(text);
506
507 const KConfigGroup group = KSharedConfig::openConfig("dolphinpartrc")->group("Select Dialog");
508 dialog->setComboBoxEditable(true);
509 dialog->setComboBoxItems(group.readEntry("History", QStringList()));
510
511 dialog->setTextValue(QStringLiteral("*"));
512
513 connect(dialog, &QDialog::accepted, this, [=]() {
514 const QString pattern = dialog->textValue();
515 if (!pattern.isEmpty()) {
516 QStringList items = dialog->comboBoxItems();
517 items.removeAll(pattern);
518 items.prepend(pattern);
519
520 // Need to evaluate this again here, because the captured value is const
521 // (even if the const were removed from 'const KConfigGroup group =' above).
522 KConfigGroup group = KSharedConfig::openConfig("dolphinpartrc")->group("Select Dialog");
523 // Limit the size of the saved history.
524 group.writeEntry("History", items.mid(0, 10));
525 group.sync();
526
527 const QRegularExpression patternRegExp(QRegularExpression::wildcardToRegularExpression(pattern));
528 m_view->selectItems(patternRegExp, selectItems);
529 }
530 });
531
532 dialog->open();
533 }
534
535 void DolphinPart::setCurrentViewMode(const QString& viewModeName)
536 {
537 QAction* action = actionCollection()->action(viewModeName);
538 Q_ASSERT(action);
539 action->trigger();
540 }
541
542 QString DolphinPart::currentViewMode() const
543 {
544 return m_actionHandler->currentViewModeActionName();
545 }
546
547 void DolphinPart::setNameFilter(const QString& nameFilter)
548 {
549 // This is the "/home/dfaure/*.diff" kind of name filter (KDirLister::setNameFilter)
550 // which is unrelated to DolphinView::setNameFilter which is substring filtering in a proxy.
551 m_nameFilter = nameFilter;
552 // TODO save/restore name filter in saveState/restoreState like KonqDirPart did in kde3?
553 }
554
555 QString DolphinPart::localFilePathOrHome() const
556 {
557 const QString localPath = localFilePath();
558 if (!localPath.isEmpty()) {
559 return localPath;
560 }
561 return QDir::homePath();
562 }
563
564 void DolphinPart::slotOpenTerminal()
565 {
566 auto job = new KTerminalLauncherJob(QString());
567 job->setWorkingDirectory(localFilePathOrHome());
568 job->start();
569 }
570
571 void DolphinPart::slotFindFile()
572 {
573 QMenu searchTools;
574 KMoreToolsMenuFactory("dolphin/search-tools").fillMenuFromGroupingNames(
575 &searchTools, { "files-find" }, QUrl::fromLocalFile(localFilePathOrHome())
576 );
577 QList<QAction*> actions = searchTools.actions();
578 if (!(actions.isEmpty())) {
579 actions.first()->trigger();
580 } else {
581 KIO::CommandLauncherJob *job = new KIO::CommandLauncherJob(QStringLiteral("kfind"), {url().toString()}, this);
582 job->setDesktopName(QStringLiteral("org.kde.kfind"));
583 job->setUiDelegate(new KDialogJobUiDelegate(KJobUiDelegate::AutoHandlingEnabled, widget()));
584 job->start();
585 }
586 }
587
588 void DolphinPart::updateNewMenu()
589 {
590 // As requested by KNewFileMenu :
591 m_newFileMenu->checkUpToDate();
592 // And set the files that the menu apply on :
593 #if KIO_VERSION >= QT_VERSION_CHECK(5, 97, 0)
594 m_newFileMenu->setWorkingDirectory(url());
595 #else
596 m_newFileMenu->setPopupFiles(QList<QUrl>() << url());
597 #endif
598 }
599
600 void DolphinPart::updateStatusBar()
601 {
602 m_view->requestStatusBarText();
603 }
604
605 void DolphinPart::updateProgress(int percent)
606 {
607 Q_EMIT m_extension->loadingProgress(percent);
608 }
609
610 void DolphinPart::createDirectory()
611 {
612 #if KIO_VERSION >= QT_VERSION_CHECK(5, 97, 0)
613 m_newFileMenu->setWorkingDirectory(url());
614 #else
615 m_newFileMenu->setPopupFiles(QList<QUrl>() << url());
616 #endif
617 m_newFileMenu->createDirectory();
618 }
619
620 void DolphinPart::setFilesToSelect(const QList<QUrl>& files)
621 {
622 if (files.isEmpty()) {
623 return;
624 }
625
626 m_view->markUrlsAsSelected(files);
627 m_view->markUrlAsCurrent(files.at(0));
628 }
629
630 bool DolphinPart::eventFilter(QObject* obj, QEvent* event)
631 {
632 using ShiftState = DolphinRemoveAction::ShiftState;
633 const int type = event->type();
634
635 if ((type == QEvent::KeyPress || type == QEvent::KeyRelease) && m_removeAction) {
636 QMenu* menu = qobject_cast<QMenu*>(obj);
637 if (menu && menu->parent() == m_view) {
638 QKeyEvent* ev = static_cast<QKeyEvent*>(event);
639 if (ev->key() == Qt::Key_Shift) {
640 m_removeAction->update(type == QEvent::KeyPress ? ShiftState::Pressed : ShiftState::Released);
641 }
642 }
643 }
644
645 return KParts::ReadOnlyPart::eventFilter(obj, event);
646 }
647
648 #include "dolphinpart.moc"