1 /* This file is part of the KDE project
2 Copyright (c) 2007 David Faure <faure@kde.org>
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
20 #include "dolphinpart.h"
21 #include "dolphinremoveaction.h"
23 #include <KFileItemListProperties>
26 #include <KActionCollection>
28 #include <KIconLoader>
29 #include <KLocalizedString>
30 #include <KMessageBox>
31 #include <KPluginFactory>
33 #include <KIO/NetAccess>
34 #include <KToolInvocation>
35 #include <kauthorized.h>
37 #include <kdeversion.h>
38 #include <KSharedConfig>
39 #include <KConfigGroup>
40 #include <KMimeTypeEditor>
42 #include "dolphinpart_ext.h"
43 #include "dolphinnewfilemenu.h"
44 #include "views/dolphinview.h"
45 #include "views/dolphinviewactionhandler.h"
46 #include "views/dolphinnewfilemenuobserver.h"
47 #include "views/dolphinremoteencoding.h"
48 #include "kitemviews/kfileitemmodel.h"
49 #include "kitemviews/private/kfileitemmodeldirlister.h"
51 #include <QStandardPaths>
52 #include <QActionGroup>
53 #include <QTextDocument>
54 #include <QApplication>
58 #include <QInputDialog>
60 K_PLUGIN_FACTORY(DolphinPartFactory
, registerPlugin
<DolphinPart
>();)
62 DolphinPart::DolphinPart(QWidget
* parentWidget
, QObject
* parent
, const QVariantList
& args
)
63 : KParts::ReadOnlyPart(parent
)
64 ,m_openTerminalAction(0)
68 setComponentData(*createAboutData(), false);
69 m_extension
= new DolphinPartBrowserExtension(this);
71 // make sure that other apps using this part find Dolphin's view-file-columns icons
72 KIconLoader::global()->addAppDir("dolphin");
74 m_view
= new DolphinView(QUrl(), parentWidget
);
75 m_view
->setTabsForFilesEnabled(true);
78 connect(&DolphinNewFileMenuObserver::instance(), &DolphinNewFileMenuObserver::errorMessage
,
79 this, &DolphinPart::slotErrorMessage
);
81 connect(m_view
, &DolphinView::directoryLoadingCompleted
, this, static_cast<void(DolphinPart::*)()>(&DolphinPart::completed
));
82 connect(m_view
, &DolphinView::directoryLoadingProgress
, this, &DolphinPart::updateProgress
);
83 connect(m_view
, &DolphinView::errorMessage
, this, &DolphinPart::slotErrorMessage
);
85 setXMLFile("dolphinpart.rc");
87 connect(m_view
, &DolphinView::infoMessage
,
88 this, &DolphinPart::slotMessage
);
89 connect(m_view
, &DolphinView::operationCompletedMessage
,
90 this, &DolphinPart::slotMessage
);
91 connect(m_view
, &DolphinView::errorMessage
,
92 this, &DolphinPart::slotErrorMessage
);
93 connect(m_view
, &DolphinView::itemActivated
,
94 this, &DolphinPart::slotItemActivated
);
95 connect(m_view
, &DolphinView::itemsActivated
,
96 this, &DolphinPart::slotItemsActivated
);
97 connect(m_view
, &DolphinView::tabRequested
,
98 this, &DolphinPart::createNewWindow
);
99 connect(m_view
, &DolphinView::requestContextMenu
,
100 this, &DolphinPart::slotOpenContextMenu
);
101 connect(m_view
, &DolphinView::selectionChanged
,
102 m_extension
, static_cast<void(DolphinPartBrowserExtension::*)(const KFileItemList
&)>(&DolphinPartBrowserExtension::selectionInfo
));
103 connect(m_view
, &DolphinView::selectionChanged
,
104 this, &DolphinPart::slotSelectionChanged
);
105 connect(m_view
, &DolphinView::requestItemInfo
,
106 this, &DolphinPart::slotRequestItemInfo
);
107 connect(m_view
, &DolphinView::modeChanged
,
108 this, &DolphinPart::viewModeChanged
); // relay signal
109 connect(m_view
, &DolphinView::redirection
,
110 this, &DolphinPart::slotDirectoryRedirection
);
112 // Watch for changes that should result in updates to the
114 connect(m_view
, &DolphinView::itemCountChanged
, this, &DolphinPart::updateStatusBar
);
115 connect(m_view
, &DolphinView::selectionChanged
, this, &DolphinPart::updateStatusBar
);
117 m_actionHandler
= new DolphinViewActionHandler(actionCollection(), this);
118 m_actionHandler
->setCurrentView(m_view
);
119 connect(m_actionHandler
, &DolphinViewActionHandler::createDirectory
, this, &DolphinPart::createDirectory
);
121 m_remoteEncoding
= new DolphinRemoteEncoding(this, m_actionHandler
);
122 connect(this, &DolphinPart::aboutToOpenURL
,
123 m_remoteEncoding
, &DolphinRemoteEncoding::slotAboutToOpenUrl
);
125 QClipboard
* clipboard
= QApplication::clipboard();
126 connect(clipboard
, &QClipboard::dataChanged
,
127 this, &DolphinPart::updatePasteAction
);
129 // Create file info and listing filter extensions.
130 // NOTE: Listing filter needs to be instantiated after the creation of the view.
131 new DolphinPartFileInfoExtension(this);
133 new DolphinPartListingFilterExtension(this);
135 KDirLister
* lister
= m_view
->m_model
->m_dirLister
;
137 DolphinPartListingNotificationExtension
* notifyExt
= new DolphinPartListingNotificationExtension(this);
138 connect(lister
, &KDirLister::newItems
, notifyExt
, &DolphinPartListingNotificationExtension::slotNewItems
);
139 connect(lister
, &KDirLister::itemsDeleted
, notifyExt
, &DolphinPartListingNotificationExtension::slotItemsDeleted
);
141 kWarning() << "NULL KDirLister object! KParts::ListingNotificationExtension will NOT be supported";
145 m_actionHandler
->updateViewActions();
146 slotSelectionChanged(KFileItemList()); // initially disable selection-dependent actions
148 // Listen to events from the app so we can update the remove key by
149 // checking for a Shift key press.
150 qApp
->installEventFilter(this);
152 // TODO there was a "always open a new window" (when clicking on a directory) setting in konqueror
153 // (sort of spacial navigation)
155 loadPlugins(this, this, componentData());
158 DolphinPart::~DolphinPart()
162 void DolphinPart::createActions()
166 m_newFileMenu
= new DolphinNewFileMenu(actionCollection(), this);
167 m_newFileMenu
->setParentWidget(widget());
168 connect(m_newFileMenu
->menu(), &QMenu::aboutToShow
,
169 this, &DolphinPart::updateNewMenu
);
171 QAction
*editMimeTypeAction
= actionCollection()->addAction( "editMimeType" );
172 editMimeTypeAction
->setText( i18nc("@action:inmenu Edit", "&Edit File Type..." ) );
173 connect(editMimeTypeAction
, &QAction::triggered
, this, &DolphinPart::slotEditMimeType
);
175 QAction
* selectItemsMatching
= actionCollection()->addAction("select_items_matching");
176 selectItemsMatching
->setText(i18nc("@action:inmenu Edit", "Select Items Matching..."));
177 actionCollection()->setDefaultShortcut(selectItemsMatching
, Qt::CTRL
| Qt::Key_S
);
178 connect(selectItemsMatching
, &QAction::triggered
, this, &DolphinPart::slotSelectItemsMatchingPattern
);
180 QAction
* unselectItemsMatching
= actionCollection()->addAction("unselect_items_matching");
181 unselectItemsMatching
->setText(i18nc("@action:inmenu Edit", "Unselect Items Matching..."));
182 connect(unselectItemsMatching
, &QAction::triggered
, this, &DolphinPart::slotUnselectItemsMatchingPattern
);
184 actionCollection()->addAction(KStandardAction::SelectAll
, "select_all", m_view
, SLOT(selectAll()));
186 QAction
* unselectAll
= actionCollection()->addAction("unselect_all");
187 unselectAll
->setText(i18nc("@action:inmenu Edit", "Unselect All"));
188 connect(unselectAll
, &QAction::triggered
, m_view
, &DolphinView::clearSelection
);
190 QAction
* invertSelection
= actionCollection()->addAction("invert_selection");
191 invertSelection
->setText(i18nc("@action:inmenu Edit", "Invert Selection"));
192 actionCollection()->setDefaultShortcut(invertSelection
, Qt::CTRL
| Qt::SHIFT
| Qt::Key_A
);
193 connect(invertSelection
, &QAction::triggered
, m_view
, &DolphinView::invertSelection
);
195 // View menu: all done by DolphinViewActionHandler
199 QActionGroup
* goActionGroup
= new QActionGroup(this);
200 connect(goActionGroup
, &QActionGroup::triggered
,
201 this, &DolphinPart::slotGoTriggered
);
203 createGoAction("go_applications", "start-here-kde",
204 i18nc("@action:inmenu Go", "App&lications"), QStringLiteral("programs:/"),
206 createGoAction("go_network_folders", "folder-remote",
207 i18nc("@action:inmenu Go", "&Network Folders"), QStringLiteral("remote:/"),
209 createGoAction("go_settings", "preferences-system",
210 i18nc("@action:inmenu Go", "Sett&ings"), QStringLiteral("settings:/"),
212 createGoAction("go_trash", "user-trash",
213 i18nc("@action:inmenu Go", "Trash"), QStringLiteral("trash:/"),
215 createGoAction("go_autostart", "",
216 i18nc("@action:inmenu Go", "Autostart"), QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation
) + "/autostart",
220 m_findFileAction
= actionCollection()->addAction("find_file");
221 m_findFileAction
->setText(i18nc("@action:inmenu Tools", "Find File..."));
222 actionCollection()->setDefaultShortcut(m_findFileAction
, Qt::CTRL
| Qt::Key_F
);
223 m_findFileAction
->setIcon(QIcon::fromTheme("edit-find"));
224 connect(m_findFileAction
, &QAction::triggered
, this, &DolphinPart::slotFindFile
);
226 if (KAuthorized::authorizeKAction("shell_access")) {
227 m_openTerminalAction
= actionCollection()->addAction("open_terminal");
228 m_openTerminalAction
->setIcon(QIcon::fromTheme("utilities-terminal"));
229 m_openTerminalAction
->setText(i18nc("@action:inmenu Tools", "Open &Terminal"));
230 connect(m_openTerminalAction
, &QAction::triggered
, this, &DolphinPart::slotOpenTerminal
);
231 actionCollection()->setDefaultShortcut(m_openTerminalAction
, Qt::Key_F4
);
235 void DolphinPart::createGoAction(const char* name
, const char* iconName
,
236 const QString
& text
, const QString
& url
,
237 QActionGroup
* actionGroup
)
239 QAction
* action
= actionCollection()->addAction(name
);
240 action
->setIcon(QIcon::fromTheme(iconName
));
241 action
->setText(text
);
242 action
->setData(url
);
243 action
->setActionGroup(actionGroup
);
246 void DolphinPart::slotGoTriggered(QAction
* action
)
248 const QString url
= action
->data().toString();
249 emit m_extension
->openUrlRequest(QUrl(url
));
252 void DolphinPart::slotSelectionChanged(const KFileItemList
& selection
)
254 const bool hasSelection
= !selection
.isEmpty();
256 QAction
* renameAction
= actionCollection()->action("rename");
257 QAction
* moveToTrashAction
= actionCollection()->action("move_to_trash");
258 QAction
* deleteAction
= actionCollection()->action("delete");
259 QAction
* editMimeTypeAction
= actionCollection()->action("editMimeType");
260 QAction
* propertiesAction
= actionCollection()->action("properties");
261 QAction
* deleteWithTrashShortcut
= actionCollection()->action("delete_shortcut"); // see DolphinViewActionHandler
264 stateChanged("has_no_selection");
266 emit m_extension
->enableAction("cut", false);
267 emit m_extension
->enableAction("copy", false);
268 deleteWithTrashShortcut
->setEnabled(false);
269 editMimeTypeAction
->setEnabled(false);
271 stateChanged("has_selection");
273 // TODO share this code with DolphinMainWindow::updateEditActions (and the desktop code)
275 KFileItemListProperties
capabilities(selection
);
276 const bool enableMoveToTrash
= capabilities
.isLocal() && capabilities
.supportsMoving();
278 renameAction
->setEnabled(capabilities
.supportsMoving());
279 moveToTrashAction
->setEnabled(enableMoveToTrash
);
280 deleteAction
->setEnabled(capabilities
.supportsDeleting());
281 deleteWithTrashShortcut
->setEnabled(capabilities
.supportsDeleting() && !enableMoveToTrash
);
282 editMimeTypeAction
->setEnabled(true);
283 propertiesAction
->setEnabled(true);
284 emit m_extension
->enableAction("cut", capabilities
.supportsMoving());
285 emit m_extension
->enableAction("copy", true);
289 void DolphinPart::updatePasteAction()
291 QPair
<bool, QString
> pasteInfo
= m_view
->pasteInfo();
292 emit m_extension
->enableAction( "paste", pasteInfo
.first
);
293 emit m_extension
->setActionText( "paste", pasteInfo
.second
);
296 KAboutData
* DolphinPart::createAboutData()
298 return new KAboutData("dolphinpart", i18nc("@title", "Dolphin Part"), "0.1");
301 bool DolphinPart::openUrl(const QUrl
&url
)
303 bool reload
= arguments().reload();
304 // A bit of a workaround so that changing the namefilter works: force reload.
305 // Otherwise DolphinView wouldn't relist the URL, so nothing would happen.
306 if (m_nameFilter
!= m_view
->nameFilter())
308 if (m_view
->url() == url
&& !reload
) { // DolphinView won't do anything in that case, so don't emit started
311 setUrl(url
); // remember it at the KParts level
312 QUrl
visibleUrl(url
);
313 if (!m_nameFilter
.isEmpty()) {
314 visibleUrl
.setPath(visibleUrl
.path() + '/' + m_nameFilter
);
316 QString prettyUrl
= visibleUrl
.toDisplayString(QUrl::PreferLocalFile
);
317 emit
setWindowCaption(prettyUrl
);
318 emit m_extension
->setLocationBarUrl(prettyUrl
);
319 emit
started(0); // get the wheel to spin
320 m_view
->setNameFilter(m_nameFilter
);
323 emit
aboutToOpenURL();
326 // Disable "Find File" and "Open Terminal" actions for non-file URLs,
327 // e.g. ftp, smb, etc. #279283
328 const bool isLocalUrl
= url
.isLocalFile();
329 m_findFileAction
->setEnabled(isLocalUrl
);
330 if (m_openTerminalAction
) {
331 m_openTerminalAction
->setEnabled(isLocalUrl
);
336 void DolphinPart::slotMessage(const QString
& msg
)
338 emit
setStatusBarText(msg
);
341 void DolphinPart::slotErrorMessage(const QString
& msg
)
345 //KMessageBox::error(m_view, msg);
348 void DolphinPart::slotRequestItemInfo(const KFileItem
& item
)
350 emit m_extension
->mouseOverInfo(item
);
354 const QString escapedText
= Qt::convertFromPlainText(item
.getStatusBarInfo());
355 ReadOnlyPart::setStatusBarText(QString("<qt>%1</qt>").arg(escapedText
));
359 void DolphinPart::slotItemActivated(const KFileItem
& item
)
361 KParts::OpenUrlArguments args
;
362 // Forget about the known mimetype if a target URL is used.
363 // Testcase: network:/ with a item (mimetype "inode/some-foo-service") pointing to a http URL (html)
364 if (item
.targetUrl() == item
.url()) {
365 args
.setMimeType(item
.mimetype());
368 // Ideally, konqueror should be changed to not require trustedSource for directory views,
369 // since the idea was not to need BrowserArguments for non-browser stuff...
370 KParts::BrowserArguments browserArgs
;
371 browserArgs
.trustedSource
= true;
372 emit m_extension
->openUrlRequest(item
.targetUrl(), args
, browserArgs
);
375 void DolphinPart::slotItemsActivated(const KFileItemList
& items
)
377 foreach (const KFileItem
& item
, items
) {
378 slotItemActivated(item
);
382 void DolphinPart::createNewWindow(const QUrl
& url
)
384 // TODO: Check issue N176832 for the missing QAIV signal; task 177399 - maybe this code
385 // should be moved into DolphinPart::slotItemActivated()
386 emit m_extension
->createNewWindow(url
);
389 void DolphinPart::slotOpenContextMenu(const QPoint
& pos
,
390 const KFileItem
& _item
,
392 const QList
<QAction
*>& customActions
)
394 KParts::BrowserExtension::PopupFlags popupFlags
= KParts::BrowserExtension::DefaultPopupItems
395 | KParts::BrowserExtension::ShowProperties
396 | KParts::BrowserExtension::ShowUrlOperations
;
398 KFileItem
item(_item
);
400 if (item
.isNull()) { // viewport context menu
401 popupFlags
|= KParts::BrowserExtension::ShowNavigationItems
| KParts::BrowserExtension::ShowUp
;
402 item
= m_view
->rootItem();
404 item
= KFileItem(url());
406 item
.setUrl(url()); // ensure we use the view url, not the canonical path (#213799)
409 // TODO: We should change the signature of the slots (and signals) for being able
410 // to tell for which items we want a popup.
412 if (m_view
->selectedItems().isEmpty()) {
415 items
= m_view
->selectedItems();
418 KFileItemListProperties
capabilities(items
);
420 KParts::BrowserExtension::ActionGroupMap actionGroups
;
421 QList
<QAction
*> editActions
;
422 editActions
+= m_view
->versionControlActions(m_view
->selectedItems());
423 editActions
+= customActions
;
425 if (!_item
.isNull()) { // only for context menu on one or more items
426 const bool supportsMoving
= capabilities
.supportsMoving();
428 if (capabilities
.supportsDeleting()) {
429 const bool showDeleteAction
= (KSharedConfig::openConfig()->group("KDE").readEntry("ShowDeleteCommand", false) ||
430 !item
.isLocalFile());
431 const bool showMoveToTrashAction
= capabilities
.isLocal() && supportsMoving
;
433 if (showDeleteAction
&& showMoveToTrashAction
) {
434 delete m_removeAction
;
436 editActions
.append(actionCollection()->action("move_to_trash"));
437 editActions
.append(actionCollection()->action("delete"));
438 } else if (showDeleteAction
&& !showMoveToTrashAction
) {
439 editActions
.append(actionCollection()->action("delete"));
442 m_removeAction
= new DolphinRemoveAction(this, actionCollection());
443 editActions
.append(m_removeAction
);
444 m_removeAction
->update();
447 popupFlags
|= KParts::BrowserExtension::NoDeletion
;
450 if (supportsMoving
) {
451 editActions
.append(actionCollection()->action("rename"));
454 // Normally KonqPopupMenu only shows the "Create new" submenu in the current view
455 // since otherwise the created file would not be visible.
456 // But in treeview mode we should allow it.
457 if (m_view
->itemsExpandable())
458 popupFlags
|= KParts::BrowserExtension::ShowCreateDirectory
;
462 actionGroups
.insert("editactions", editActions
);
464 emit m_extension
->popupMenu(pos
,
466 KParts::OpenUrlArguments(),
467 KParts::BrowserArguments(),
472 void DolphinPart::slotDirectoryRedirection(const QUrl
&oldUrl
, const QUrl
&newUrl
)
474 //kDebug() << oldUrl << newUrl << "currentUrl=" << url();
475 if (oldUrl
.matches(url(), QUrl::StripTrailingSlash
/* #207572 */)) {
476 KParts::ReadOnlyPart::setUrl(newUrl
);
477 const QString prettyUrl
= newUrl
.toDisplayString(QUrl::PreferLocalFile
);
478 emit m_extension
->setLocationBarUrl(prettyUrl
);
483 void DolphinPart::slotEditMimeType()
485 const KFileItemList items
= m_view
->selectedItems();
486 if (!items
.isEmpty()) {
487 KMimeTypeEditor::editMimeType(items
.first().mimetype(), m_view
);
491 void DolphinPart::slotSelectItemsMatchingPattern()
493 openSelectionDialog(i18nc("@title:window", "Select"),
494 i18n("Select all items matching this pattern:"),
498 void DolphinPart::slotUnselectItemsMatchingPattern()
500 openSelectionDialog(i18nc("@title:window", "Unselect"),
501 i18n("Unselect all items matching this pattern:"),
505 void DolphinPart::openSelectionDialog(const QString
& title
, const QString
& text
, bool selectItems
)
508 const QString pattern
= QInputDialog::getText(m_view
, title
, text
, QLineEdit::Normal
, "*", &okClicked
);
510 if (okClicked
&& !pattern
.isEmpty()) {
511 QRegExp
patternRegExp(pattern
, Qt::CaseSensitive
, QRegExp::Wildcard
);
512 m_view
->selectItems(patternRegExp
, selectItems
);
516 void DolphinPart::setCurrentViewMode(const QString
& viewModeName
)
518 QAction
* action
= actionCollection()->action(viewModeName
);
523 QString
DolphinPart::currentViewMode() const
525 return m_actionHandler
->currentViewModeActionName();
528 void DolphinPart::setNameFilter(const QString
& nameFilter
)
530 // This is the "/home/dfaure/*.diff" kind of name filter (KDirLister::setNameFilter)
531 // which is unrelated to DolphinView::setNameFilter which is substring filtering in a proxy.
532 m_nameFilter
= nameFilter
;
533 // TODO save/restore name filter in saveState/restoreState like KonqDirPart did in kde3?
536 void DolphinPart::slotOpenTerminal()
538 QString
dir(QDir::homePath());
542 // If the given directory is not local, it can still be the URL of an
543 // ioslave using UDS_LOCAL_PATH which to be converted first.
544 u
= KIO::NetAccess::mostLocalUrl(u
, widget());
546 //If the URL is local after the above conversion, set the directory.
547 if (u
.isLocalFile()) {
548 dir
= u
.toLocalFile();
551 KToolInvocation::invokeTerminal(QString(), dir
);
554 void DolphinPart::slotFindFile()
556 KRun::run("kfind", {url()}, widget());
559 void DolphinPart::updateNewMenu()
561 // As requested by KNewFileMenu :
562 m_newFileMenu
->checkUpToDate();
563 m_newFileMenu
->setViewShowsHiddenFiles(m_view
->hiddenFilesShown());
564 // And set the files that the menu apply on :
565 m_newFileMenu
->setPopupFiles(url());
568 void DolphinPart::updateStatusBar()
570 const QString escapedText
= Qt::convertFromPlainText(m_view
->statusBarText());
571 emit
ReadOnlyPart::setStatusBarText(QString("<qt>%1</qt>").arg(escapedText
));
574 void DolphinPart::updateProgress(int percent
)
576 m_extension
->loadingProgress(percent
);
579 void DolphinPart::createDirectory()
581 m_newFileMenu
->setViewShowsHiddenFiles(m_view
->hiddenFilesShown());
582 m_newFileMenu
->setPopupFiles(url());
583 m_newFileMenu
->createDirectory();
586 void DolphinPart::setFilesToSelect(const QList
<QUrl
>& files
)
588 if (files
.isEmpty()) {
592 m_view
->markUrlsAsSelected(files
);
593 m_view
->markUrlAsCurrent(files
.at(0));
596 bool DolphinPart::eventFilter(QObject
* obj
, QEvent
* event
)
598 const int type
= event
->type();
600 if ((type
== QEvent::KeyPress
|| type
== QEvent::KeyRelease
) && m_removeAction
) {
601 QMenu
* menu
= qobject_cast
<QMenu
*>(obj
);
602 if (menu
&& menu
->parent() == m_view
) {
603 QKeyEvent
* ev
= static_cast<QKeyEvent
*>(event
);
604 if (ev
->key() == Qt::Key_Shift
) {
605 m_removeAction
->update();
610 return KParts::ReadOnlyPart::eventFilter(obj
, event
);
613 #include "dolphinpart.moc"