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