]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinpart.cpp
Fix icon resize animation
[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 <KPluginMetaData>
20 #include <KActionCollection>
21 #include <KAuthorized>
22 #include <KConfigGroup>
23 #include <KDialogJobUiDelegate>
24 #include <KDirLister>
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 <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 QUrl &,
389 const QList<QAction*>& customActions)
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 // TODO: We should change the signature of the slots (and signals) for being able
406 // to tell for which items we want a popup.
407 KFileItemList items;
408 if (m_view->selectedItems().isEmpty()) {
409 items.append(item);
410 } else {
411 items = m_view->selectedItems();
412 }
413
414 KFileItemListProperties capabilities(items);
415
416 KParts::BrowserExtension::ActionGroupMap actionGroups;
417 QList<QAction *> editActions;
418 editActions += m_view->versionControlActions(m_view->selectedItems());
419 editActions += customActions;
420
421 if (!_item.isNull()) { // only for context menu on one or more items
422 const bool supportsMoving = capabilities.supportsMoving();
423
424 if (capabilities.supportsDeleting()) {
425 const bool showDeleteAction = (KSharedConfig::openConfig()->group("KDE").readEntry("ShowDeleteCommand", false) ||
426 !item.isLocalFile());
427 const bool showMoveToTrashAction = capabilities.isLocal() && supportsMoving;
428
429 if (showDeleteAction && showMoveToTrashAction) {
430 delete m_removeAction;
431 m_removeAction = nullptr;
432 editActions.append(actionCollection()->action(KStandardAction::name(KStandardAction::MoveToTrash)));
433 editActions.append(actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile)));
434 } else if (showDeleteAction && !showMoveToTrashAction) {
435 editActions.append(actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile)));
436 } else {
437 if (!m_removeAction)
438 m_removeAction = new DolphinRemoveAction(this, actionCollection());
439 editActions.append(m_removeAction);
440 m_removeAction->update();
441 }
442 } else {
443 popupFlags |= KParts::BrowserExtension::NoDeletion;
444 }
445
446 if (supportsMoving) {
447 editActions.append(actionCollection()->action(KStandardAction::name(KStandardAction::RenameFile)));
448 }
449
450 // Normally KonqPopupMenu only shows the "Create new" submenu in the current view
451 // since otherwise the created file would not be visible.
452 // But in treeview mode we should allow it.
453 if (m_view->itemsExpandable())
454 popupFlags |= KParts::BrowserExtension::ShowCreateDirectory;
455
456 }
457
458 actionGroups.insert(QStringLiteral("editactions"), editActions);
459
460 Q_EMIT m_extension->popupMenu(pos,
461 items,
462 KParts::OpenUrlArguments(),
463 KParts::BrowserArguments(),
464 popupFlags,
465 actionGroups);
466 }
467
468 void DolphinPart::slotDirectoryRedirection(const QUrl &oldUrl, const QUrl &newUrl)
469 {
470 qCDebug(DolphinDebug) << oldUrl << newUrl << "currentUrl=" << url();
471 if (oldUrl.matches(url(), QUrl::StripTrailingSlash /* #207572 */)) {
472 KParts::ReadOnlyPart::setUrl(newUrl);
473 const QString prettyUrl = newUrl.toDisplayString(QUrl::PreferLocalFile);
474 Q_EMIT m_extension->setLocationBarUrl(prettyUrl);
475 }
476 }
477
478
479 void DolphinPart::slotEditMimeType()
480 {
481 const KFileItemList items = m_view->selectedItems();
482 if (!items.isEmpty()) {
483 KMimeTypeEditor::editMimeType(items.first().mimetype(), m_view);
484 }
485 }
486
487 void DolphinPart::slotSelectItemsMatchingPattern()
488 {
489 openSelectionDialog(i18nc("@title:window", "Select"),
490 i18n("Select all items matching this pattern:"),
491 true);
492 }
493
494 void DolphinPart::slotUnselectItemsMatchingPattern()
495 {
496 openSelectionDialog(i18nc("@title:window", "Unselect"),
497 i18n("Unselect all items matching this pattern:"),
498 false);
499 }
500
501 void DolphinPart::openSelectionDialog(const QString& title, const QString& text, bool selectItems)
502 {
503 auto *dialog = new QInputDialog(m_view);
504 dialog->setAttribute(Qt::WA_DeleteOnClose, true);
505 dialog->setInputMode(QInputDialog::TextInput);
506 dialog->setWindowTitle(title);
507 dialog->setLabelText(text);
508
509 const KConfigGroup group = KSharedConfig::openConfig("dolphinpartrc")->group("Select Dialog");
510 dialog->setComboBoxEditable(true);
511 dialog->setComboBoxItems(group.readEntry("History", QStringList()));
512
513 dialog->setTextValue(QStringLiteral("*"));
514
515 connect(dialog, &QDialog::accepted, this, [=]() {
516 const QString pattern = dialog->textValue();
517 if (!pattern.isEmpty()) {
518 QStringList items = dialog->comboBoxItems();
519 items.removeAll(pattern);
520 items.prepend(pattern);
521
522 // Need to evaluate this again here, because the captured value is const
523 // (even if the const were removed from 'const KConfigGroup group =' above).
524 KConfigGroup group = KSharedConfig::openConfig("dolphinpartrc")->group("Select Dialog");
525 // Limit the size of the saved history.
526 group.writeEntry("History", items.mid(0, 10));
527 group.sync();
528
529 const QRegularExpression patternRegExp(QRegularExpression::wildcardToRegularExpression(pattern));
530 m_view->selectItems(patternRegExp, selectItems);
531 }
532 });
533
534 dialog->open();
535 }
536
537 void DolphinPart::setCurrentViewMode(const QString& viewModeName)
538 {
539 QAction* action = actionCollection()->action(viewModeName);
540 Q_ASSERT(action);
541 action->trigger();
542 }
543
544 QString DolphinPart::currentViewMode() const
545 {
546 return m_actionHandler->currentViewModeActionName();
547 }
548
549 void DolphinPart::setNameFilter(const QString& nameFilter)
550 {
551 // This is the "/home/dfaure/*.diff" kind of name filter (KDirLister::setNameFilter)
552 // which is unrelated to DolphinView::setNameFilter which is substring filtering in a proxy.
553 m_nameFilter = nameFilter;
554 // TODO save/restore name filter in saveState/restoreState like KonqDirPart did in kde3?
555 }
556
557 QString DolphinPart::localFilePathOrHome() const
558 {
559 const QString localPath = localFilePath();
560 if (!localPath.isEmpty()) {
561 return localPath;
562 }
563 return QDir::homePath();
564 }
565
566 void DolphinPart::slotOpenTerminal()
567 {
568 auto job = new KTerminalLauncherJob(QString());
569 job->setWorkingDirectory(localFilePathOrHome());
570 job->start();
571 }
572
573 void DolphinPart::slotFindFile()
574 {
575 QMenu searchTools;
576 KMoreToolsMenuFactory("dolphin/search-tools").fillMenuFromGroupingNames(
577 &searchTools, { "files-find" }, QUrl::fromLocalFile(localFilePathOrHome())
578 );
579 QList<QAction*> actions = searchTools.actions();
580 if (!(actions.isEmpty())) {
581 actions.first()->trigger();
582 } else {
583 KIO::CommandLauncherJob *job = new KIO::CommandLauncherJob(QStringLiteral("kfind"), {url().toString()}, this);
584 job->setDesktopName(QStringLiteral("org.kde.kfind"));
585 job->setUiDelegate(new KDialogJobUiDelegate(KJobUiDelegate::AutoHandlingEnabled, widget()));
586 job->start();
587 }
588 }
589
590 void DolphinPart::updateNewMenu()
591 {
592 // As requested by KNewFileMenu :
593 m_newFileMenu->checkUpToDate();
594 // And set the files that the menu apply on :
595 m_newFileMenu->setPopupFiles(QList<QUrl>() << url());
596 }
597
598 void DolphinPart::updateStatusBar()
599 {
600 m_view->requestStatusBarText();
601 }
602
603 void DolphinPart::updateProgress(int percent)
604 {
605 Q_EMIT m_extension->loadingProgress(percent);
606 }
607
608 void DolphinPart::createDirectory()
609 {
610 m_newFileMenu->setPopupFiles(QList<QUrl>() << url());
611 m_newFileMenu->createDirectory();
612 }
613
614 void DolphinPart::setFilesToSelect(const QList<QUrl>& files)
615 {
616 if (files.isEmpty()) {
617 return;
618 }
619
620 m_view->markUrlsAsSelected(files);
621 m_view->markUrlAsCurrent(files.at(0));
622 }
623
624 bool DolphinPart::eventFilter(QObject* obj, QEvent* event)
625 {
626 using ShiftState = DolphinRemoveAction::ShiftState;
627 const int type = event->type();
628
629 if ((type == QEvent::KeyPress || type == QEvent::KeyRelease) && m_removeAction) {
630 QMenu* menu = qobject_cast<QMenu*>(obj);
631 if (menu && menu->parent() == m_view) {
632 QKeyEvent* ev = static_cast<QKeyEvent*>(event);
633 if (ev->key() == Qt::Key_Shift) {
634 m_removeAction->update(type == QEvent::KeyPress ? ShiftState::Pressed : ShiftState::Released);
635 }
636 }
637 }
638
639 return KParts::ReadOnlyPart::eventFilter(obj, event);
640 }
641
642 #include "dolphinpart.moc"