]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinpart.cpp
Add cmake preset support
[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 "kitemviews/private/kfileitemmodeldirlister.h"
15 #include "views/dolphinnewfilemenuobserver.h"
16 #include "views/dolphinremoteencoding.h"
17 #include "views/dolphinview.h"
18 #include "views/dolphinviewactionhandler.h"
19
20 #include <KPluginMetaData>
21 #include <KActionCollection>
22 #include <KAuthorized>
23 #include <KConfigGroup>
24 #include <KDialogJobUiDelegate>
25 #include <KFileItemListProperties>
26 #include <KIconLoader>
27 #include <KJobWidgets>
28 #include <KLocalizedString>
29 #include <KMessageBox>
30 #include <KMimeTypeEditor>
31 #include <KNS3/KMoreToolsMenuFactory>
32 #include <KPluginFactory>
33 #include <KIO/CommandLauncherJob>
34 #include <KSharedConfig>
35 #include <KToolInvocation>
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, QOverload<>::of(&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, QOverload<const KFileItemList&>::of(&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 loadPlugins(this, this, componentName());
151 }
152
153 DolphinPart::~DolphinPart()
154 {
155 }
156
157 void DolphinPart::createActions()
158 {
159 // Edit menu
160
161 m_newFileMenu = new DolphinNewFileMenu(actionCollection(), this);
162 m_newFileMenu->setParentWidget(widget());
163 connect(m_newFileMenu->menu(), &QMenu::aboutToShow,
164 this, &DolphinPart::updateNewMenu);
165
166 QAction *editMimeTypeAction = actionCollection()->addAction( QStringLiteral("editMimeType") );
167 editMimeTypeAction->setText( i18nc("@action:inmenu Edit", "&Edit File Type..." ) );
168 connect(editMimeTypeAction, &QAction::triggered, this, &DolphinPart::slotEditMimeType);
169
170 QAction* selectItemsMatching = actionCollection()->addAction(QStringLiteral("select_items_matching"));
171 selectItemsMatching->setText(i18nc("@action:inmenu Edit", "Select Items Matching..."));
172 actionCollection()->setDefaultShortcut(selectItemsMatching, Qt::CTRL | Qt::Key_S);
173 connect(selectItemsMatching, &QAction::triggered, this, &DolphinPart::slotSelectItemsMatchingPattern);
174
175 QAction* unselectItemsMatching = actionCollection()->addAction(QStringLiteral("unselect_items_matching"));
176 unselectItemsMatching->setText(i18nc("@action:inmenu Edit", "Unselect Items Matching..."));
177 connect(unselectItemsMatching, &QAction::triggered, this, &DolphinPart::slotUnselectItemsMatchingPattern);
178
179 KStandardAction::selectAll(m_view, &DolphinView::selectAll, actionCollection());
180
181 QAction* unselectAll = actionCollection()->addAction(QStringLiteral("unselect_all"));
182 unselectAll->setText(i18nc("@action:inmenu Edit", "Unselect All"));
183 connect(unselectAll, &QAction::triggered, m_view, &DolphinView::clearSelection);
184
185 QAction* invertSelection = actionCollection()->addAction(QStringLiteral("invert_selection"));
186 invertSelection->setText(i18nc("@action:inmenu Edit", "Invert Selection"));
187 actionCollection()->setDefaultShortcut(invertSelection, Qt::CTRL | Qt::SHIFT | Qt::Key_A);
188 connect(invertSelection, &QAction::triggered, m_view, &DolphinView::invertSelection);
189
190 // View menu: all done by DolphinViewActionHandler
191
192 // Go menu
193
194 QActionGroup* goActionGroup = new QActionGroup(this);
195 connect(goActionGroup, &QActionGroup::triggered,
196 this, &DolphinPart::slotGoTriggered);
197
198 createGoAction("go_applications", "start-here-kde",
199 i18nc("@action:inmenu Go", "App&lications"), QStringLiteral("programs:/"),
200 goActionGroup);
201 createGoAction("go_network_folders", "folder-remote",
202 i18nc("@action:inmenu Go", "&Network Folders"), QStringLiteral("remote:/"),
203 goActionGroup);
204 createGoAction("go_settings", "preferences-system",
205 i18nc("@action:inmenu Go", "Sett&ings"), QStringLiteral("settings:/"),
206 goActionGroup);
207 createGoAction("go_trash", "user-trash",
208 i18nc("@action:inmenu Go", "Trash"), QStringLiteral("trash:/"),
209 goActionGroup);
210 createGoAction("go_autostart", "",
211 i18nc("@action:inmenu Go", "Autostart"), QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation) + "/autostart",
212 goActionGroup);
213
214 // Tools menu
215 m_findFileAction = KStandardAction::find(this, &DolphinPart::slotFindFile, actionCollection());
216 m_findFileAction->setText(i18nc("@action:inmenu Tools", "Find File..."));
217
218 #ifndef Q_OS_WIN
219 if (KAuthorized::authorize(QStringLiteral("shell_access"))) {
220 m_openTerminalAction = actionCollection()->addAction(QStringLiteral("open_terminal"));
221 m_openTerminalAction->setIcon(QIcon::fromTheme(QStringLiteral("dialog-scripts")));
222 m_openTerminalAction->setText(i18nc("@action:inmenu Tools", "Open &Terminal"));
223 connect(m_openTerminalAction, &QAction::triggered, this, &DolphinPart::slotOpenTerminal);
224 actionCollection()->setDefaultShortcut(m_openTerminalAction, Qt::Key_F4);
225 }
226 #endif
227 }
228
229 void DolphinPart::createGoAction(const char* name, const char* iconName,
230 const QString& text, const QString& url,
231 QActionGroup* actionGroup)
232 {
233 QAction* action = actionCollection()->addAction(name);
234 action->setIcon(QIcon::fromTheme(iconName));
235 action->setText(text);
236 action->setData(url);
237 action->setActionGroup(actionGroup);
238 }
239
240 void DolphinPart::slotGoTriggered(QAction* action)
241 {
242 const QString url = action->data().toString();
243 Q_EMIT m_extension->openUrlRequest(QUrl(url));
244 }
245
246 void DolphinPart::slotSelectionChanged(const KFileItemList& selection)
247 {
248 const bool hasSelection = !selection.isEmpty();
249
250 QAction* renameAction = actionCollection()->action(KStandardAction::name(KStandardAction::RenameFile));
251 QAction* moveToTrashAction = actionCollection()->action(KStandardAction::name(KStandardAction::MoveToTrash));
252 QAction* deleteAction = actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile));
253 QAction* editMimeTypeAction = actionCollection()->action(QStringLiteral("editMimeType"));
254 QAction* propertiesAction = actionCollection()->action(QStringLiteral("properties"));
255 QAction* deleteWithTrashShortcut = actionCollection()->action(QStringLiteral("delete_shortcut")); // see DolphinViewActionHandler
256
257 if (!hasSelection) {
258 stateChanged(QStringLiteral("has_no_selection"));
259
260 Q_EMIT m_extension->enableAction("cut", false);
261 Q_EMIT m_extension->enableAction("copy", false);
262 deleteWithTrashShortcut->setEnabled(false);
263 editMimeTypeAction->setEnabled(false);
264 } else {
265 stateChanged(QStringLiteral("has_selection"));
266
267 // TODO share this code with DolphinMainWindow::updateEditActions (and the desktop code)
268 // in libkonq
269 KFileItemListProperties capabilities(selection);
270 const bool enableMoveToTrash = capabilities.isLocal() && capabilities.supportsMoving();
271
272 renameAction->setEnabled(capabilities.supportsMoving());
273 moveToTrashAction->setEnabled(enableMoveToTrash);
274 deleteAction->setEnabled(capabilities.supportsDeleting());
275 deleteWithTrashShortcut->setEnabled(capabilities.supportsDeleting() && !enableMoveToTrash);
276 editMimeTypeAction->setEnabled(true);
277 propertiesAction->setEnabled(true);
278 Q_EMIT m_extension->enableAction("cut", capabilities.supportsMoving());
279 Q_EMIT m_extension->enableAction("copy", true);
280 }
281 }
282
283 void DolphinPart::updatePasteAction()
284 {
285 QPair<bool, QString> pasteInfo = m_view->pasteInfo();
286 Q_EMIT m_extension->enableAction( "paste", pasteInfo.first );
287 Q_EMIT m_extension->setActionText( "paste", pasteInfo.second );
288 }
289
290 QString DolphinPart::urlToLocalFilePath(const QUrl &url)
291 {
292 KIO::StatJob* statJob = KIO::mostLocalUrl(url);
293 KJobWidgets::setWindow(statJob, widget());
294 statJob->exec();
295 QUrl localUrl = statJob->mostLocalUrl();
296 if (localUrl.isLocalFile()) {
297 return localUrl.toLocalFile();
298 }
299 return QString();
300 }
301
302 bool DolphinPart::openUrl(const QUrl &url)
303 {
304 bool reload = arguments().reload();
305 // A bit of a workaround so that changing the namefilter works: force reload.
306 // Otherwise DolphinView wouldn't relist the URL, so nothing would happen.
307 if (m_nameFilter != m_view->nameFilter())
308 reload = true;
309 if (m_view->url() == url && !reload) { // DolphinView won't do anything in that case, so don't emit started
310 return true;
311 }
312 setUrl(url); // remember url at the KParts level
313 setLocalFilePath(urlToLocalFilePath(url)); // remember local path at the KParts level
314 QUrl visibleUrl(url);
315 if (!m_nameFilter.isEmpty()) {
316 visibleUrl.setPath(visibleUrl.path() + '/' + m_nameFilter);
317 }
318 QString prettyUrl = visibleUrl.toDisplayString(QUrl::PreferLocalFile);
319 Q_EMIT setWindowCaption(prettyUrl);
320 Q_EMIT m_extension->setLocationBarUrl(prettyUrl);
321 Q_EMIT started(nullptr); // get the wheel to spin
322 m_view->setNameFilter(m_nameFilter);
323 m_view->setUrl(url);
324 updatePasteAction();
325 Q_EMIT aboutToOpenURL();
326 if (reload)
327 m_view->reload();
328 // Disable "Find File" and "Open Terminal" actions for non-file URLs,
329 // e.g. ftp, smb, etc. #279283
330 const bool isLocalUrl = !(localFilePath().isEmpty());
331 m_findFileAction->setEnabled(isLocalUrl);
332 if (m_openTerminalAction) {
333 m_openTerminalAction->setEnabled(isLocalUrl);
334 }
335 return true;
336 }
337
338 void DolphinPart::slotMessage(const QString& msg)
339 {
340 Q_EMIT setStatusBarText(msg);
341 }
342
343 void DolphinPart::slotErrorMessage(const QString& msg)
344 {
345 qCDebug(DolphinDebug) << msg;
346 Q_EMIT canceled(msg);
347 //KMessageBox::error(m_view, msg);
348 }
349
350 void DolphinPart::slotRequestItemInfo(const KFileItem& item)
351 {
352 Q_EMIT m_extension->mouseOverInfo(item);
353 if (item.isNull()) {
354 updateStatusBar();
355 } else {
356 const QString escapedText = Qt::convertFromPlainText(item.getStatusBarInfo());
357 Q_EMIT ReadOnlyPart::setStatusBarText(QStringLiteral("<qt>%1</qt>").arg(escapedText));
358 }
359 }
360
361 void DolphinPart::slotItemActivated(const KFileItem& item)
362 {
363 KParts::OpenUrlArguments args;
364 // Forget about the known mimetype if a target URL is used.
365 // Testcase: network:/ with a item (mimetype "inode/some-foo-service") pointing to a http URL (html)
366 if (item.targetUrl() == item.url()) {
367 args.setMimeType(item.mimetype());
368 }
369
370 // Ideally, konqueror should be changed to not require trustedSource for directory views,
371 // since the idea was not to need BrowserArguments for non-browser stuff...
372 KParts::BrowserArguments browserArgs;
373 browserArgs.trustedSource = true;
374 Q_EMIT m_extension->openUrlRequest(item.targetUrl(), args, browserArgs);
375 }
376
377 void DolphinPart::slotItemsActivated(const KFileItemList& items)
378 {
379 for (const KFileItem& item : items) {
380 slotItemActivated(item);
381 }
382 }
383
384 void DolphinPart::createNewWindow(const QUrl& url)
385 {
386 // TODO: Check issue N176832 for the missing QAIV signal; task 177399 - maybe this code
387 // should be moved into DolphinPart::slotItemActivated()
388 Q_EMIT m_extension->createNewWindow(url);
389 }
390
391 void DolphinPart::slotOpenContextMenu(const QPoint& pos,
392 const KFileItem& _item,
393 const QUrl &,
394 const QList<QAction*>& customActions)
395 {
396 KParts::BrowserExtension::PopupFlags popupFlags = KParts::BrowserExtension::DefaultPopupItems
397 | KParts::BrowserExtension::ShowProperties
398 | KParts::BrowserExtension::ShowUrlOperations;
399
400 KFileItem item(_item);
401
402 if (item.isNull()) { // viewport context menu
403 item = m_view->rootItem();
404 if (item.isNull())
405 item = KFileItem(url());
406 else
407 item.setUrl(url()); // ensure we use the view url, not the canonical path (#213799)
408 }
409
410 // TODO: We should change the signature of the slots (and signals) for being able
411 // to tell for which items we want a popup.
412 KFileItemList items;
413 if (m_view->selectedItems().isEmpty()) {
414 items.append(item);
415 } else {
416 items = m_view->selectedItems();
417 }
418
419 KFileItemListProperties capabilities(items);
420
421 KParts::BrowserExtension::ActionGroupMap actionGroups;
422 QList<QAction *> editActions;
423 editActions += m_view->versionControlActions(m_view->selectedItems());
424 editActions += customActions;
425
426 if (!_item.isNull()) { // only for context menu on one or more items
427 const bool supportsMoving = capabilities.supportsMoving();
428
429 if (capabilities.supportsDeleting()) {
430 const bool showDeleteAction = (KSharedConfig::openConfig()->group("KDE").readEntry("ShowDeleteCommand", false) ||
431 !item.isLocalFile());
432 const bool showMoveToTrashAction = capabilities.isLocal() && supportsMoving;
433
434 if (showDeleteAction && showMoveToTrashAction) {
435 delete m_removeAction;
436 m_removeAction = nullptr;
437 editActions.append(actionCollection()->action(KStandardAction::name(KStandardAction::MoveToTrash)));
438 editActions.append(actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile)));
439 } else if (showDeleteAction && !showMoveToTrashAction) {
440 editActions.append(actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile)));
441 } else {
442 if (!m_removeAction)
443 m_removeAction = new DolphinRemoveAction(this, actionCollection());
444 editActions.append(m_removeAction);
445 m_removeAction->update();
446 }
447 } else {
448 popupFlags |= KParts::BrowserExtension::NoDeletion;
449 }
450
451 if (supportsMoving) {
452 editActions.append(actionCollection()->action(KStandardAction::name(KStandardAction::RenameFile)));
453 }
454
455 // Normally KonqPopupMenu only shows the "Create new" submenu in the current view
456 // since otherwise the created file would not be visible.
457 // But in treeview mode we should allow it.
458 if (m_view->itemsExpandable())
459 popupFlags |= KParts::BrowserExtension::ShowCreateDirectory;
460
461 }
462
463 actionGroups.insert(QStringLiteral("editactions"), editActions);
464
465 Q_EMIT m_extension->popupMenu(pos,
466 items,
467 KParts::OpenUrlArguments(),
468 KParts::BrowserArguments(),
469 popupFlags,
470 actionGroups);
471 }
472
473 void DolphinPart::slotDirectoryRedirection(const QUrl &oldUrl, const QUrl &newUrl)
474 {
475 qCDebug(DolphinDebug) << oldUrl << newUrl << "currentUrl=" << url();
476 if (oldUrl.matches(url(), QUrl::StripTrailingSlash /* #207572 */)) {
477 KParts::ReadOnlyPart::setUrl(newUrl);
478 const QString prettyUrl = newUrl.toDisplayString(QUrl::PreferLocalFile);
479 Q_EMIT m_extension->setLocationBarUrl(prettyUrl);
480 }
481 }
482
483
484 void DolphinPart::slotEditMimeType()
485 {
486 const KFileItemList items = m_view->selectedItems();
487 if (!items.isEmpty()) {
488 KMimeTypeEditor::editMimeType(items.first().mimetype(), m_view);
489 }
490 }
491
492 void DolphinPart::slotSelectItemsMatchingPattern()
493 {
494 openSelectionDialog(i18nc("@title:window", "Select"),
495 i18n("Select all items matching this pattern:"),
496 true);
497 }
498
499 void DolphinPart::slotUnselectItemsMatchingPattern()
500 {
501 openSelectionDialog(i18nc("@title:window", "Unselect"),
502 i18n("Unselect all items matching this pattern:"),
503 false);
504 }
505
506 void DolphinPart::openSelectionDialog(const QString& title, const QString& text, bool selectItems)
507 {
508 auto *dialog = new QInputDialog(m_view);
509 dialog->setAttribute(Qt::WA_DeleteOnClose, true);
510 dialog->setInputMode(QInputDialog::TextInput);
511 dialog->setWindowTitle(title);
512 dialog->setLabelText(text);
513
514 const KConfigGroup group = KSharedConfig::openConfig("dolphinpartrc")->group("Select Dialog");
515 dialog->setComboBoxEditable(true);
516 dialog->setComboBoxItems(group.readEntry("History", QStringList()));
517
518 dialog->setTextValue(QStringLiteral("*"));
519
520 connect(dialog, &QDialog::accepted, this, [=]() {
521 const QString pattern = dialog->textValue();
522 if (!pattern.isEmpty()) {
523 QStringList items = dialog->comboBoxItems();
524 items.removeAll(pattern);
525 items.prepend(pattern);
526
527 // Need to evaluate this again here, because the captured value is const
528 // (even if the const were removed from 'const KConfigGroup group =' above).
529 KConfigGroup group = KSharedConfig::openConfig("dolphinpartrc")->group("Select Dialog");
530 // Limit the size of the saved history.
531 group.writeEntry("History", items.mid(0, 10));
532 group.sync();
533
534 const QRegularExpression patternRegExp(QRegularExpression::wildcardToRegularExpression(pattern));
535 m_view->selectItems(patternRegExp, selectItems);
536 }
537 });
538
539 dialog->open();
540 }
541
542 void DolphinPart::setCurrentViewMode(const QString& viewModeName)
543 {
544 QAction* action = actionCollection()->action(viewModeName);
545 Q_ASSERT(action);
546 action->trigger();
547 }
548
549 QString DolphinPart::currentViewMode() const
550 {
551 return m_actionHandler->currentViewModeActionName();
552 }
553
554 void DolphinPart::setNameFilter(const QString& nameFilter)
555 {
556 // This is the "/home/dfaure/*.diff" kind of name filter (KDirLister::setNameFilter)
557 // which is unrelated to DolphinView::setNameFilter which is substring filtering in a proxy.
558 m_nameFilter = nameFilter;
559 // TODO save/restore name filter in saveState/restoreState like KonqDirPart did in kde3?
560 }
561
562 QString DolphinPart::localFilePathOrHome() const
563 {
564 const QString localPath = localFilePath();
565 if (!localPath.isEmpty()) {
566 return localPath;
567 }
568 return QDir::homePath();
569 }
570
571 void DolphinPart::slotOpenTerminal()
572 {
573 KToolInvocation::invokeTerminal(QString(), {}, localFilePathOrHome());
574 }
575
576 void DolphinPart::slotFindFile()
577 {
578 QMenu searchTools;
579 KMoreToolsMenuFactory("dolphin/search-tools").fillMenuFromGroupingNames(
580 &searchTools, { "files-find" }, QUrl::fromLocalFile(localFilePathOrHome())
581 );
582 QList<QAction*> actions = searchTools.actions();
583 if (!(actions.isEmpty())) {
584 actions.first()->trigger();
585 } else {
586 KIO::CommandLauncherJob *job = new KIO::CommandLauncherJob(QStringLiteral("kfind"), {url().toString()}, this);
587 job->setDesktopName(QStringLiteral("org.kde.kfind"));
588 job->setUiDelegate(new KDialogJobUiDelegate(KJobUiDelegate::AutoHandlingEnabled, widget()));
589 job->start();
590 }
591 }
592
593 void DolphinPart::updateNewMenu()
594 {
595 // As requested by KNewFileMenu :
596 m_newFileMenu->checkUpToDate();
597 m_newFileMenu->setViewShowsHiddenFiles(m_view->hiddenFilesShown());
598 // And set the files that the menu apply on :
599 m_newFileMenu->setPopupFiles(QList<QUrl>() << url());
600 }
601
602 void DolphinPart::updateStatusBar()
603 {
604 m_view->requestStatusBarText();
605 }
606
607 void DolphinPart::updateProgress(int percent)
608 {
609 Q_EMIT m_extension->loadingProgress(percent);
610 }
611
612 void DolphinPart::createDirectory()
613 {
614 m_newFileMenu->setViewShowsHiddenFiles(m_view->hiddenFilesShown());
615 m_newFileMenu->setPopupFiles(QList<QUrl>() << url());
616 m_newFileMenu->createDirectory();
617 }
618
619 void DolphinPart::setFilesToSelect(const QList<QUrl>& files)
620 {
621 if (files.isEmpty()) {
622 return;
623 }
624
625 m_view->markUrlsAsSelected(files);
626 m_view->markUrlAsCurrent(files.at(0));
627 }
628
629 bool DolphinPart::eventFilter(QObject* obj, QEvent* event)
630 {
631 using ShiftState = DolphinRemoveAction::ShiftState;
632 const int type = event->type();
633
634 if ((type == QEvent::KeyPress || type == QEvent::KeyRelease) && m_removeAction) {
635 QMenu* menu = qobject_cast<QMenu*>(obj);
636 if (menu && menu->parent() == m_view) {
637 QKeyEvent* ev = static_cast<QKeyEvent*>(event);
638 if (ev->key() == Qt::Key_Shift) {
639 m_removeAction->update(type == QEvent::KeyPress ? ShiftState::Pressed : ShiftState::Released);
640 }
641 }
642 }
643
644 return KParts::ReadOnlyPart::eventFilter(obj, event);
645 }
646
647 #include "dolphinpart.moc"