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>
24 #include <konq_operations.h>
27 #include <KActionCollection>
29 #include <KIconLoader>
30 #include <KLocalizedString>
31 #include <KMessageBox>
32 #include <KPluginFactory>
34 #include <KIO/NetAccess>
35 #include <KToolInvocation>
36 #include <kauthorized.h>
38 #include <KInputDialog>
39 #include <kdeversion.h>
40 #include <KSharedConfig>
41 #include <KConfigGroup>
42 #include <KMimeTypeEditor>
44 #include "dolphinpart_ext.h"
45 #include "dolphinnewfilemenu.h"
46 #include "views/dolphinview.h"
47 #include "views/dolphinviewactionhandler.h"
48 #include "views/dolphinnewfilemenuobserver.h"
49 #include "views/dolphinremoteencoding.h"
50 #include "kitemviews/kfileitemmodel.h"
51 #include "kitemviews/private/kfileitemmodeldirlister.h"
53 #include <QStandardPaths>
54 #include <QActionGroup>
55 #include <QTextDocument>
56 #include <QApplication>
62 K_PLUGIN_FACTORY(DolphinPartFactory
, registerPlugin
<DolphinPart
>();)
63 K_EXPORT_PLUGIN(DolphinPartFactory("dolphinpart", "dolphin"))
65 DolphinPart::DolphinPart(QWidget
* parentWidget
, QObject
* parent
, const QVariantList
& args
)
66 : KParts::ReadOnlyPart(parent
)
67 ,m_openTerminalAction(0)
71 setComponentData(*createAboutData(), false);
72 m_extension
= new DolphinPartBrowserExtension(this);
74 // make sure that other apps using this part find Dolphin's view-file-columns icons
75 KIconLoader::global()->addAppDir("dolphin");
77 m_view
= new DolphinView(QUrl(), parentWidget
);
78 m_view
->setTabsForFilesEnabled(true);
81 connect(&DolphinNewFileMenuObserver::instance(), &DolphinNewFileMenuObserver::errorMessage
,
82 this, &DolphinPart::slotErrorMessage
);
84 connect(m_view
, &DolphinView::directoryLoadingCompleted
, this, static_cast<void(DolphinPart::*)()>(&DolphinPart::completed
));
85 connect(m_view
, &DolphinView::directoryLoadingProgress
, this, &DolphinPart::updateProgress
);
86 connect(m_view
, &DolphinView::errorMessage
, this, &DolphinPart::slotErrorMessage
);
88 setXMLFile("dolphinpart.rc");
90 connect(m_view
, &DolphinView::infoMessage
,
91 this, &DolphinPart::slotMessage
);
92 connect(m_view
, &DolphinView::operationCompletedMessage
,
93 this, &DolphinPart::slotMessage
);
94 connect(m_view
, &DolphinView::errorMessage
,
95 this, &DolphinPart::slotErrorMessage
);
96 connect(m_view
, &DolphinView::itemActivated
,
97 this, &DolphinPart::slotItemActivated
);
98 connect(m_view
, &DolphinView::itemsActivated
,
99 this, &DolphinPart::slotItemsActivated
);
100 connect(m_view
, &DolphinView::tabRequested
,
101 this, &DolphinPart::createNewWindow
);
102 connect(m_view
, &DolphinView::requestContextMenu
,
103 this, &DolphinPart::slotOpenContextMenu
);
104 connect(m_view
, &DolphinView::selectionChanged
,
105 m_extension
, static_cast<void(DolphinPartBrowserExtension::*)(const KFileItemList
&)>(&DolphinPartBrowserExtension::selectionInfo
));
106 connect(m_view
, &DolphinView::selectionChanged
,
107 this, &DolphinPart::slotSelectionChanged
);
108 connect(m_view
, &DolphinView::requestItemInfo
,
109 this, &DolphinPart::slotRequestItemInfo
);
110 connect(m_view
, &DolphinView::modeChanged
,
111 this, &DolphinPart::viewModeChanged
); // relay signal
112 connect(m_view
, &DolphinView::redirection
,
113 this, &DolphinPart::slotDirectoryRedirection
);
115 // Watch for changes that should result in updates to the
117 connect(m_view
, &DolphinView::itemCountChanged
, this, &DolphinPart::updateStatusBar
);
118 connect(m_view
, &DolphinView::selectionChanged
, this, &DolphinPart::updateStatusBar
);
120 m_actionHandler
= new DolphinViewActionHandler(actionCollection(), this);
121 m_actionHandler
->setCurrentView(m_view
);
122 connect(m_actionHandler
, &DolphinViewActionHandler::createDirectory
, this, &DolphinPart::createDirectory
);
124 m_remoteEncoding
= new DolphinRemoteEncoding(this, m_actionHandler
);
125 connect(this, &DolphinPart::aboutToOpenURL
,
126 m_remoteEncoding
, &DolphinRemoteEncoding::slotAboutToOpenUrl
);
128 QClipboard
* clipboard
= QApplication::clipboard();
129 connect(clipboard
, &QClipboard::dataChanged
,
130 this, &DolphinPart::updatePasteAction
);
132 // Create file info and listing filter extensions.
133 // NOTE: Listing filter needs to be instantiated after the creation of the view.
134 new DolphinPartFileInfoExtension(this);
136 new DolphinPartListingFilterExtension(this);
138 KDirLister
* lister
= m_view
->m_model
->m_dirLister
;
140 DolphinPartListingNotificationExtension
* notifyExt
= new DolphinPartListingNotificationExtension(this);
141 connect(lister
, &KDirLister::newItems
, notifyExt
, &DolphinPartListingNotificationExtension::slotNewItems
);
142 connect(lister
, &KDirLister::itemsDeleted
, notifyExt
, &DolphinPartListingNotificationExtension::slotItemsDeleted
);
144 kWarning() << "NULL KDirLister object! KParts::ListingNotificationExtension will NOT be supported";
148 m_actionHandler
->updateViewActions();
149 slotSelectionChanged(KFileItemList()); // initially disable selection-dependent actions
151 // Listen to events from the app so we can update the remove key by
152 // checking for a Shift key press.
153 qApp
->installEventFilter(this);
155 // TODO there was a "always open a new window" (when clicking on a directory) setting in konqueror
156 // (sort of spacial navigation)
158 loadPlugins(this, this, componentData());
161 DolphinPart::~DolphinPart()
165 void DolphinPart::createActions()
169 m_newFileMenu
= new DolphinNewFileMenu(actionCollection(), this);
170 m_newFileMenu
->setParentWidget(widget());
171 connect(m_newFileMenu
->menu(), &QMenu::aboutToShow
,
172 this, &DolphinPart::updateNewMenu
);
174 QAction
*editMimeTypeAction
= actionCollection()->addAction( "editMimeType" );
175 editMimeTypeAction
->setText( i18nc("@action:inmenu Edit", "&Edit File Type..." ) );
176 connect(editMimeTypeAction
, &QAction::triggered
, this, &DolphinPart::slotEditMimeType
);
178 QAction
* selectItemsMatching
= actionCollection()->addAction("select_items_matching");
179 selectItemsMatching
->setText(i18nc("@action:inmenu Edit", "Select Items Matching..."));
180 actionCollection()->setDefaultShortcut(selectItemsMatching
, Qt::CTRL
| Qt::Key_S
);
181 connect(selectItemsMatching
, &QAction::triggered
, this, &DolphinPart::slotSelectItemsMatchingPattern
);
183 QAction
* unselectItemsMatching
= actionCollection()->addAction("unselect_items_matching");
184 unselectItemsMatching
->setText(i18nc("@action:inmenu Edit", "Unselect Items Matching..."));
185 connect(unselectItemsMatching
, &QAction::triggered
, this, &DolphinPart::slotUnselectItemsMatchingPattern
);
187 actionCollection()->addAction(KStandardAction::SelectAll
, "select_all", m_view
, SLOT(selectAll()));
189 QAction
* unselectAll
= actionCollection()->addAction("unselect_all");
190 unselectAll
->setText(i18nc("@action:inmenu Edit", "Unselect All"));
191 connect(unselectAll
, &QAction::triggered
, m_view
, &DolphinView::clearSelection
);
193 QAction
* invertSelection
= actionCollection()->addAction("invert_selection");
194 invertSelection
->setText(i18nc("@action:inmenu Edit", "Invert Selection"));
195 actionCollection()->setDefaultShortcut(invertSelection
, Qt::CTRL
| Qt::SHIFT
| Qt::Key_A
);
196 connect(invertSelection
, &QAction::triggered
, m_view
, &DolphinView::invertSelection
);
198 // View menu: all done by DolphinViewActionHandler
202 QActionGroup
* goActionGroup
= new QActionGroup(this);
203 connect(goActionGroup
, &QActionGroup::triggered
,
204 this, &DolphinPart::slotGoTriggered
);
206 createGoAction("go_applications", "start-here-kde",
207 i18nc("@action:inmenu Go", "App&lications"), QStringLiteral("programs:/"),
209 createGoAction("go_network_folders", "folder-remote",
210 i18nc("@action:inmenu Go", "&Network Folders"), QStringLiteral("remote:/"),
212 createGoAction("go_settings", "preferences-system",
213 i18nc("@action:inmenu Go", "Sett&ings"), QStringLiteral("settings:/"),
215 createGoAction("go_trash", "user-trash",
216 i18nc("@action:inmenu Go", "Trash"), QStringLiteral("trash:/"),
218 createGoAction("go_autostart", "",
219 i18nc("@action:inmenu Go", "Autostart"), QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation
) + "/autostart",
223 m_findFileAction
= actionCollection()->addAction("find_file");
224 m_findFileAction
->setText(i18nc("@action:inmenu Tools", "Find File..."));
225 actionCollection()->setDefaultShortcut(m_findFileAction
, Qt::CTRL
| Qt::Key_F
);
226 m_findFileAction
->setIcon(QIcon::fromTheme("edit-find"));
227 connect(m_findFileAction
, &QAction::triggered
, this, &DolphinPart::slotFindFile
);
229 if (KAuthorized::authorizeKAction("shell_access")) {
230 m_openTerminalAction
= actionCollection()->addAction("open_terminal");
231 m_openTerminalAction
->setIcon(QIcon::fromTheme("utilities-terminal"));
232 m_openTerminalAction
->setText(i18nc("@action:inmenu Tools", "Open &Terminal"));
233 connect(m_openTerminalAction
, &QAction::triggered
, this, &DolphinPart::slotOpenTerminal
);
234 actionCollection()->setDefaultShortcut(m_openTerminalAction
, Qt::Key_F4
);
238 void DolphinPart::createGoAction(const char* name
, const char* iconName
,
239 const QString
& text
, const QString
& url
,
240 QActionGroup
* actionGroup
)
242 QAction
* action
= actionCollection()->addAction(name
);
243 action
->setIcon(QIcon::fromTheme(iconName
));
244 action
->setText(text
);
245 action
->setData(url
);
246 action
->setActionGroup(actionGroup
);
249 void DolphinPart::slotGoTriggered(QAction
* action
)
251 const QString url
= action
->data().toString();
252 emit m_extension
->openUrlRequest(QUrl(url
));
255 void DolphinPart::slotSelectionChanged(const KFileItemList
& selection
)
257 const bool hasSelection
= !selection
.isEmpty();
259 QAction
* renameAction
= actionCollection()->action("rename");
260 QAction
* moveToTrashAction
= actionCollection()->action("move_to_trash");
261 QAction
* deleteAction
= actionCollection()->action("delete");
262 QAction
* editMimeTypeAction
= actionCollection()->action("editMimeType");
263 QAction
* propertiesAction
= actionCollection()->action("properties");
264 QAction
* deleteWithTrashShortcut
= actionCollection()->action("delete_shortcut"); // see DolphinViewActionHandler
267 stateChanged("has_no_selection");
269 emit m_extension
->enableAction("cut", false);
270 emit m_extension
->enableAction("copy", false);
271 deleteWithTrashShortcut
->setEnabled(false);
272 editMimeTypeAction
->setEnabled(false);
274 stateChanged("has_selection");
276 // TODO share this code with DolphinMainWindow::updateEditActions (and the desktop code)
278 KFileItemListProperties
capabilities(selection
);
279 const bool enableMoveToTrash
= capabilities
.isLocal() && capabilities
.supportsMoving();
281 renameAction
->setEnabled(capabilities
.supportsMoving());
282 moveToTrashAction
->setEnabled(enableMoveToTrash
);
283 deleteAction
->setEnabled(capabilities
.supportsDeleting());
284 deleteWithTrashShortcut
->setEnabled(capabilities
.supportsDeleting() && !enableMoveToTrash
);
285 editMimeTypeAction
->setEnabled(true);
286 propertiesAction
->setEnabled(true);
287 emit m_extension
->enableAction("cut", capabilities
.supportsMoving());
288 emit m_extension
->enableAction("copy", true);
292 void DolphinPart::updatePasteAction()
294 QPair
<bool, QString
> pasteInfo
= m_view
->pasteInfo();
295 emit m_extension
->enableAction( "paste", pasteInfo
.first
);
296 emit m_extension
->setActionText( "paste", pasteInfo
.second
);
299 KAboutData
* DolphinPart::createAboutData()
301 return new KAboutData("dolphinpart", i18nc("@title", "Dolphin Part"), "0.1");
304 bool DolphinPart::openUrl(const QUrl
&url
)
306 bool reload
= arguments().reload();
307 // A bit of a workaround so that changing the namefilter works: force reload.
308 // Otherwise DolphinView wouldn't relist the URL, so nothing would happen.
309 if (m_nameFilter
!= m_view
->nameFilter())
311 if (m_view
->url() == url
&& !reload
) { // DolphinView won't do anything in that case, so don't emit started
314 setUrl(url
); // remember it at the KParts level
315 QUrl
visibleUrl(url
);
316 if (!m_nameFilter
.isEmpty()) {
317 visibleUrl
.setPath(visibleUrl
.path() + '/' + m_nameFilter
);
319 QString prettyUrl
= visibleUrl
.toDisplayString(QUrl::PreferLocalFile
);
320 emit
setWindowCaption(prettyUrl
);
321 emit m_extension
->setLocationBarUrl(prettyUrl
);
322 emit
started(0); // get the wheel to spin
323 m_view
->setNameFilter(m_nameFilter
);
326 emit
aboutToOpenURL();
329 // Disable "Find File" and "Open Terminal" actions for non-file URLs,
330 // e.g. ftp, smb, etc. #279283
331 const bool isLocalUrl
= url
.isLocalFile();
332 m_findFileAction
->setEnabled(isLocalUrl
);
333 if (m_openTerminalAction
) {
334 m_openTerminalAction
->setEnabled(isLocalUrl
);
339 void DolphinPart::slotMessage(const QString
& msg
)
341 emit
setStatusBarText(msg
);
344 void DolphinPart::slotErrorMessage(const QString
& msg
)
348 //KMessageBox::error(m_view, msg);
351 void DolphinPart::slotRequestItemInfo(const KFileItem
& item
)
353 emit m_extension
->mouseOverInfo(item
);
357 const QString escapedText
= Qt::convertFromPlainText(item
.getStatusBarInfo());
358 ReadOnlyPart::setStatusBarText(QString("<qt>%1</qt>").arg(escapedText
));
362 void DolphinPart::slotItemActivated(const KFileItem
& item
)
364 KParts::OpenUrlArguments args
;
365 // Forget about the known mimetype if a target URL is used.
366 // Testcase: network:/ with a item (mimetype "inode/some-foo-service") pointing to a http URL (html)
367 if (item
.targetUrl() == item
.url()) {
368 args
.setMimeType(item
.mimetype());
371 // Ideally, konqueror should be changed to not require trustedSource for directory views,
372 // since the idea was not to need BrowserArguments for non-browser stuff...
373 KParts::BrowserArguments browserArgs
;
374 browserArgs
.trustedSource
= true;
375 emit m_extension
->openUrlRequest(item
.targetUrl(), args
, browserArgs
);
378 void DolphinPart::slotItemsActivated(const KFileItemList
& items
)
380 foreach (const KFileItem
& item
, items
) {
381 slotItemActivated(item
);
385 void DolphinPart::createNewWindow(const QUrl
& url
)
387 // TODO: Check issue N176832 for the missing QAIV signal; task 177399 - maybe this code
388 // should be moved into DolphinPart::slotItemActivated()
389 emit m_extension
->createNewWindow(url
);
392 void DolphinPart::slotOpenContextMenu(const QPoint
& pos
,
393 const KFileItem
& _item
,
395 const QList
<QAction
*>& customActions
)
397 KParts::BrowserExtension::PopupFlags popupFlags
= KParts::BrowserExtension::DefaultPopupItems
398 | KParts::BrowserExtension::ShowProperties
399 | KParts::BrowserExtension::ShowUrlOperations
;
401 KFileItem
item(_item
);
403 if (item
.isNull()) { // viewport context menu
404 popupFlags
|= KParts::BrowserExtension::ShowNavigationItems
| KParts::BrowserExtension::ShowUp
;
405 item
= m_view
->rootItem();
407 item
= KFileItem( S_IFDIR
, (mode_t
)-1, url() );
409 item
.setUrl(url()); // ensure we use the view url, not the canonical path (#213799)
412 // TODO: We should change the signature of the slots (and signals) for being able
413 // to tell for which items we want a popup.
415 if (m_view
->selectedItems().isEmpty()) {
418 items
= m_view
->selectedItems();
421 KFileItemListProperties
capabilities(items
);
423 KParts::BrowserExtension::ActionGroupMap actionGroups
;
424 QList
<QAction
*> editActions
;
425 editActions
+= m_view
->versionControlActions(m_view
->selectedItems());
426 editActions
+= customActions
;
428 if (!_item
.isNull()) { // only for context menu on one or more items
429 const bool supportsMoving
= capabilities
.supportsMoving();
431 if (capabilities
.supportsDeleting()) {
432 const bool showDeleteAction
= (KSharedConfig::openConfig()->group("KDE").readEntry("ShowDeleteCommand", false) ||
433 !item
.isLocalFile());
434 const bool showMoveToTrashAction
= capabilities
.isLocal() && supportsMoving
;
436 if (showDeleteAction
&& showMoveToTrashAction
) {
437 delete m_removeAction
;
439 editActions
.append(actionCollection()->action("move_to_trash"));
440 editActions
.append(actionCollection()->action("delete"));
441 } else if (showDeleteAction
&& !showMoveToTrashAction
) {
442 editActions
.append(actionCollection()->action("delete"));
445 m_removeAction
= new DolphinRemoveAction(this, actionCollection());
446 editActions
.append(m_removeAction
);
447 m_removeAction
->update();
450 popupFlags
|= KParts::BrowserExtension::NoDeletion
;
453 if (supportsMoving
) {
454 editActions
.append(actionCollection()->action("rename"));
457 // Normally KonqPopupMenu only shows the "Create new" submenu in the current view
458 // since otherwise the created file would not be visible.
459 // But in treeview mode we should allow it.
460 if (m_view
->itemsExpandable())
461 popupFlags
|= KParts::BrowserExtension::ShowCreateDirectory
;
465 actionGroups
.insert("editactions", editActions
);
467 emit m_extension
->popupMenu(pos
,
469 KParts::OpenUrlArguments(),
470 KParts::BrowserArguments(),
475 void DolphinPart::slotDirectoryRedirection(const QUrl
&oldUrl
, const QUrl
&newUrl
)
477 //kDebug() << oldUrl << newUrl << "currentUrl=" << url();
478 if (oldUrl
.matches(url(), QUrl::StripTrailingSlash
/* #207572 */)) {
479 KParts::ReadOnlyPart::setUrl(newUrl
);
480 const QString prettyUrl
= newUrl
.toDisplayString(QUrl::PreferLocalFile
);
481 emit m_extension
->setLocationBarUrl(prettyUrl
);
486 void DolphinPart::slotEditMimeType()
488 const KFileItemList items
= m_view
->selectedItems();
489 if (!items
.isEmpty()) {
490 KMimeTypeEditor::editMimeType(items
.first().mimetype(), m_view
);
494 void DolphinPart::slotSelectItemsMatchingPattern()
496 openSelectionDialog(i18nc("@title:window", "Select"),
497 i18n("Select all items matching this pattern:"),
501 void DolphinPart::slotUnselectItemsMatchingPattern()
503 openSelectionDialog(i18nc("@title:window", "Unselect"),
504 i18n("Unselect all items matching this pattern:"),
508 void DolphinPart::openSelectionDialog(const QString
& title
, const QString
& text
, bool selectItems
)
511 QString pattern
= KInputDialog::getText(title
, text
, "*", &okClicked
, m_view
);
513 if (okClicked
&& !pattern
.isEmpty()) {
514 QRegExp
patternRegExp(pattern
, Qt::CaseSensitive
, QRegExp::Wildcard
);
515 m_view
->selectItems(patternRegExp
, selectItems
);
519 void DolphinPart::setCurrentViewMode(const QString
& viewModeName
)
521 QAction
* action
= actionCollection()->action(viewModeName
);
526 QString
DolphinPart::currentViewMode() const
528 return m_actionHandler
->currentViewModeActionName();
531 void DolphinPart::setNameFilter(const QString
& nameFilter
)
533 // This is the "/home/dfaure/*.diff" kind of name filter (KDirLister::setNameFilter)
534 // which is unrelated to DolphinView::setNameFilter which is substring filtering in a proxy.
535 m_nameFilter
= nameFilter
;
536 // TODO save/restore name filter in saveState/restoreState like KonqDirPart did in kde3?
539 void DolphinPart::slotOpenTerminal()
541 QString
dir(QDir::homePath());
545 // If the given directory is not local, it can still be the URL of an
546 // ioslave using UDS_LOCAL_PATH which to be converted first.
547 u
= KIO::NetAccess::mostLocalUrl(u
, widget());
549 //If the URL is local after the above conversion, set the directory.
550 if (u
.isLocalFile()) {
551 dir
= u
.toLocalFile();
554 KToolInvocation::invokeTerminal(QString(), dir
);
557 void DolphinPart::slotFindFile()
559 KRun::run("kfind", QList
<QUrl
>() << url(), widget());
562 void DolphinPart::updateNewMenu()
564 // As requested by KNewFileMenu :
565 m_newFileMenu
->checkUpToDate();
566 m_newFileMenu
->setViewShowsHiddenFiles(m_view
->hiddenFilesShown());
567 // And set the files that the menu apply on :
568 m_newFileMenu
->setPopupFiles(url());
571 void DolphinPart::updateStatusBar()
573 const QString escapedText
= Qt::convertFromPlainText(m_view
->statusBarText());
574 emit
ReadOnlyPart::setStatusBarText(QString("<qt>%1</qt>").arg(escapedText
));
577 void DolphinPart::updateProgress(int percent
)
579 m_extension
->loadingProgress(percent
);
582 void DolphinPart::createDirectory()
584 m_newFileMenu
->setViewShowsHiddenFiles(m_view
->hiddenFilesShown());
585 m_newFileMenu
->setPopupFiles(url());
586 m_newFileMenu
->createDirectory();
589 void DolphinPart::setFilesToSelect(const QList
<QUrl
>& files
)
591 if (files
.isEmpty()) {
595 m_view
->markUrlsAsSelected(files
);
596 m_view
->markUrlAsCurrent(files
.at(0));
599 bool DolphinPart::eventFilter(QObject
* obj
, QEvent
* event
)
601 const int type
= event
->type();
603 if ((type
== QEvent::KeyPress
|| type
== QEvent::KeyRelease
) && m_removeAction
) {
604 QMenu
* menu
= qobject_cast
<QMenu
*>(obj
);
605 if (menu
&& menu
->parent() == m_view
) {
606 QKeyEvent
* ev
= static_cast<QKeyEvent
*>(event
);
607 if (ev
->key() == Qt::Key_Shift
) {
608 m_removeAction
->update();
613 return KParts::ReadOnlyPart::eventFilter(obj
, event
);
616 #include "dolphinpart.moc"