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