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