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