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>
28 #include <KConfigGroup>
30 #include <KGlobalSettings>
31 #include <KIconLoader>
33 #include <KMessageBox>
34 #include <KPluginFactory>
36 #include <KToggleAction>
38 #include <KIO/NetAccess>
39 #include <KToolInvocation>
40 #include <kauthorized.h>
42 #include <KInputDialog>
43 #include <KProtocolInfo>
44 #include <kdeversion.h>
46 #include "dolphinpart_ext.h"
48 #include "dolphinnewfilemenu.h"
49 #include "views/dolphinview.h"
50 #include "views/dolphinviewactionhandler.h"
51 #include "views/dolphinnewfilemenuobserver.h"
52 #include "views/dolphinremoteencoding.h"
53 #include "kitemviews/kfileitemmodel.h"
54 #include "kitemviews/private/kfileitemmodeldirlister.h"
56 #include <QActionGroup>
57 #include <QApplication>
60 #include <QTextDocument>
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 #pragma message("TODO: port to KF5")
72 //setComponentData(DolphinPartFactory::componentData(), false);
73 m_extension
= new DolphinPartBrowserExtension(this);
75 // make sure that other apps using this part find Dolphin's view-file-columns icons
76 KIconLoader::global()->addAppDir("dolphin");
78 m_view
= new DolphinView(KUrl(), parentWidget
);
79 m_view
->setTabsForFilesEnabled(true);
82 connect(&DolphinNewFileMenuObserver::instance(), &DolphinNewFileMenuObserver::errorMessage
,
83 this, &DolphinPart::slotErrorMessage
);
85 connect(m_view
, &DolphinView::directoryLoadingCompleted
, this, static_cast<void(DolphinPart::*)()>(&DolphinPart::completed
));
86 connect(m_view
, &DolphinView::directoryLoadingProgress
, this, &DolphinPart::updateProgress
);
87 connect(m_view
, &DolphinView::errorMessage
, this, &DolphinPart::slotErrorMessage
);
89 setXMLFile("dolphinpart.rc");
91 connect(m_view
, &DolphinView::infoMessage
,
92 this, &DolphinPart::slotMessage
);
93 connect(m_view
, &DolphinView::operationCompletedMessage
,
94 this, &DolphinPart::slotMessage
);
95 connect(m_view
, &DolphinView::errorMessage
,
96 this, &DolphinPart::slotErrorMessage
);
97 connect(m_view
, &DolphinView::itemActivated
,
98 this, &DolphinPart::slotItemActivated
);
99 connect(m_view
, &DolphinView::itemsActivated
,
100 this, &DolphinPart::slotItemsActivated
);
101 connect(m_view
, &DolphinView::tabRequested
,
102 this, &DolphinPart::createNewWindow
);
103 connect(m_view
, &DolphinView::requestContextMenu
,
104 this, &DolphinPart::slotOpenContextMenu
);
105 connect(m_view
, &DolphinView::selectionChanged
,
106 m_extension
, static_cast<void(DolphinPartBrowserExtension::*)(const KFileItemList
&)>(&DolphinPartBrowserExtension::selectionInfo
));
107 connect(m_view
, &DolphinView::selectionChanged
,
108 this, &DolphinPart::slotSelectionChanged
);
109 connect(m_view
, &DolphinView::requestItemInfo
,
110 this, &DolphinPart::slotRequestItemInfo
);
111 connect(m_view
, &DolphinView::modeChanged
,
112 this, &DolphinPart::viewModeChanged
); // relay signal
113 connect(m_view
, &DolphinView::redirection
,
114 this, &DolphinPart::slotDirectoryRedirection
);
116 // Watch for changes that should result in updates to the
118 connect(m_view
, &DolphinView::itemCountChanged
, this, &DolphinPart::updateStatusBar
);
119 connect(m_view
, &DolphinView::selectionChanged
, this, &DolphinPart::updateStatusBar
);
121 m_actionHandler
= new DolphinViewActionHandler(actionCollection(), this);
122 m_actionHandler
->setCurrentView(m_view
);
123 connect(m_actionHandler
, &DolphinViewActionHandler::createDirectory
, this, &DolphinPart::createDirectory
);
125 m_remoteEncoding
= new DolphinRemoteEncoding(this, m_actionHandler
);
126 connect(this, &DolphinPart::aboutToOpenURL
,
127 m_remoteEncoding
, &DolphinRemoteEncoding::slotAboutToOpenUrl
);
129 QClipboard
* clipboard
= QApplication::clipboard();
130 connect(clipboard
, &QClipboard::dataChanged
,
131 this, &DolphinPart::updatePasteAction
);
133 // Create file info and listing filter extensions.
134 // NOTE: Listing filter needs to be instantiated after the creation of the view.
135 new DolphinPartFileInfoExtension(this);
137 new DolphinPartListingFilterExtension(this);
139 KDirLister
* lister
= m_view
->m_model
->m_dirLister
;
141 DolphinPartListingNotificationExtension
* notifyExt
= new DolphinPartListingNotificationExtension(this);
142 connect(lister
, &KDirLister::newItems
, notifyExt
, &DolphinPartListingNotificationExtension::slotNewItems
);
143 connect(lister
, &KDirLister::itemsDeleted
, notifyExt
, &DolphinPartListingNotificationExtension::slotItemsDeleted
);
145 kWarning() << "NULL KDirLister object! KParts::ListingNotificationExtension will NOT be supported";
149 m_actionHandler
->updateViewActions();
150 slotSelectionChanged(KFileItemList()); // initially disable selection-dependent actions
152 // Listen to events from the app so we can update the remove key by
153 // checking for a Shift key press.
154 qApp
->installEventFilter(this);
156 // TODO there was a "always open a new window" (when clicking on a directory) setting in konqueror
157 // (sort of spacial navigation)
159 loadPlugins(this, this, componentData());
162 DolphinPart::~DolphinPart()
166 void DolphinPart::createActions()
170 m_newFileMenu
= new DolphinNewFileMenu(actionCollection(), this);
171 m_newFileMenu
->setParentWidget(widget());
172 connect(m_newFileMenu
->menu(), &QMenu::aboutToShow
,
173 this, &DolphinPart::updateNewMenu
);
175 QAction
*editMimeTypeAction
= actionCollection()->addAction( "editMimeType" );
176 editMimeTypeAction
->setText( i18nc("@action:inmenu Edit", "&Edit File Type..." ) );
177 connect(editMimeTypeAction
, &QAction::triggered
, this, &DolphinPart::slotEditMimeType
);
179 QAction
* selectItemsMatching
= actionCollection()->addAction("select_items_matching");
180 selectItemsMatching
->setText(i18nc("@action:inmenu Edit", "Select Items Matching..."));
181 selectItemsMatching
->setShortcut(Qt::CTRL
| Qt::Key_S
);
182 connect(selectItemsMatching
, &QAction::triggered
, this, &DolphinPart::slotSelectItemsMatchingPattern
);
184 QAction
* unselectItemsMatching
= actionCollection()->addAction("unselect_items_matching");
185 unselectItemsMatching
->setText(i18nc("@action:inmenu Edit", "Unselect Items Matching..."));
186 connect(unselectItemsMatching
, &QAction::triggered
, this, &DolphinPart::slotUnselectItemsMatchingPattern
);
188 actionCollection()->addAction(KStandardAction::SelectAll
, "select_all", m_view
, SLOT(selectAll()));
190 QAction
* unselectAll
= actionCollection()->addAction("unselect_all");
191 unselectAll
->setText(i18nc("@action:inmenu Edit", "Unselect All"));
192 connect(unselectAll
, &QAction::triggered
, m_view
, &DolphinView::clearSelection
);
194 QAction
* invertSelection
= actionCollection()->addAction("invert_selection");
195 invertSelection
->setText(i18nc("@action:inmenu Edit", "Invert Selection"));
196 invertSelection
->setShortcut(Qt::CTRL
| Qt::SHIFT
| Qt::Key_A
);
197 connect(invertSelection
, &QAction::triggered
, m_view
, &DolphinView::invertSelection
);
199 // View menu: all done by DolphinViewActionHandler
203 QActionGroup
* goActionGroup
= new QActionGroup(this);
204 connect(goActionGroup
, &QActionGroup::triggered
,
205 this, &DolphinPart::slotGoTriggered
);
207 createGoAction("go_applications", "start-here-kde",
208 i18nc("@action:inmenu Go", "App&lications"), QString("programs:/"),
210 createGoAction("go_network_folders", "folder-remote",
211 i18nc("@action:inmenu Go", "&Network Folders"), QString("remote:/"),
213 createGoAction("go_settings", "preferences-system",
214 i18nc("@action:inmenu Go", "Sett&ings"), QString("settings:/"),
216 createGoAction("go_trash", "user-trash",
217 i18nc("@action:inmenu Go", "Trash"), QString("trash:/"),
219 createGoAction("go_autostart", "",
220 i18nc("@action:inmenu Go", "Autostart"), QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation
) + "/autostart",
224 m_findFileAction
= actionCollection()->addAction("find_file");
225 m_findFileAction
->setText(i18nc("@action:inmenu Tools", "Find File..."));
226 m_findFileAction
->setShortcut(Qt::CTRL
| Qt::Key_F
);
227 m_findFileAction
->setIcon(QIcon::fromTheme("edit-find"));
228 connect(m_findFileAction
, &QAction::triggered
, this, &DolphinPart::slotFindFile
);
230 if (KAuthorized::authorizeKAction("shell_access")) {
231 m_openTerminalAction
= actionCollection()->addAction("open_terminal");
232 m_openTerminalAction
->setIcon(QIcon::fromTheme("utilities-terminal"));
233 m_openTerminalAction
->setText(i18nc("@action:inmenu Tools", "Open &Terminal"));
234 connect(m_openTerminalAction
, &QAction::triggered
, this, &DolphinPart::slotOpenTerminal
);
235 m_openTerminalAction
->setShortcut(Qt::Key_F4
);
239 void DolphinPart::createGoAction(const char* name
, const char* iconName
,
240 const QString
& text
, const QString
& url
,
241 QActionGroup
* actionGroup
)
243 QAction
* action
= actionCollection()->addAction(name
);
244 action
->setIcon(QIcon::fromTheme(iconName
));
245 action
->setText(text
);
246 action
->setData(url
);
247 action
->setActionGroup(actionGroup
);
250 void DolphinPart::slotGoTriggered(QAction
* action
)
252 const QString url
= action
->data().toString();
253 emit m_extension
->openUrlRequest(KUrl(url
));
256 void DolphinPart::slotSelectionChanged(const KFileItemList
& selection
)
258 const bool hasSelection
= !selection
.isEmpty();
260 QAction
* renameAction
= actionCollection()->action("rename");
261 QAction
* moveToTrashAction
= actionCollection()->action("move_to_trash");
262 QAction
* deleteAction
= actionCollection()->action("delete");
263 QAction
* editMimeTypeAction
= actionCollection()->action("editMimeType");
264 QAction
* propertiesAction
= actionCollection()->action("properties");
265 QAction
* deleteWithTrashShortcut
= actionCollection()->action("delete_shortcut"); // see DolphinViewActionHandler
268 stateChanged("has_no_selection");
270 emit m_extension
->enableAction("cut", false);
271 emit m_extension
->enableAction("copy", false);
272 deleteWithTrashShortcut
->setEnabled(false);
273 editMimeTypeAction
->setEnabled(false);
275 stateChanged("has_selection");
277 // TODO share this code with DolphinMainWindow::updateEditActions (and the desktop code)
279 KFileItemListProperties
capabilities(selection
);
280 const bool enableMoveToTrash
= capabilities
.isLocal() && capabilities
.supportsMoving();
282 renameAction
->setEnabled(capabilities
.supportsMoving());
283 moveToTrashAction
->setEnabled(enableMoveToTrash
);
284 deleteAction
->setEnabled(capabilities
.supportsDeleting());
285 deleteWithTrashShortcut
->setEnabled(capabilities
.supportsDeleting() && !enableMoveToTrash
);
286 editMimeTypeAction
->setEnabled(true);
287 propertiesAction
->setEnabled(true);
288 emit m_extension
->enableAction("cut", capabilities
.supportsMoving());
289 emit m_extension
->enableAction("copy", true);
293 void DolphinPart::updatePasteAction()
295 QPair
<bool, QString
> pasteInfo
= m_view
->pasteInfo();
296 emit m_extension
->enableAction( "paste", pasteInfo
.first
);
297 emit m_extension
->setActionText( "paste", pasteInfo
.second
);
300 KAboutData
* DolphinPart::createAboutData()
302 return new KAboutData("dolphinpart", i18nc("@title", "Dolphin Part"), "0.1");
305 bool DolphinPart::openUrl(const QUrl
&url
)
307 bool reload
= arguments().reload();
308 // A bit of a workaround so that changing the namefilter works: force reload.
309 // Otherwise DolphinView wouldn't relist the URL, so nothing would happen.
310 if (m_nameFilter
!= m_view
->nameFilter())
312 if (m_view
->url() == url
&& !reload
) { // DolphinView won't do anything in that case, so don't emit started
315 setUrl(url
); // remember it at the KParts level
316 KUrl
visibleUrl(url
);
317 if (!m_nameFilter
.isEmpty()) {
318 visibleUrl
.addPath(m_nameFilter
);
320 QString prettyUrl
= visibleUrl
.pathOrUrl();
321 emit
setWindowCaption(prettyUrl
);
322 emit m_extension
->setLocationBarUrl(prettyUrl
);
323 emit
started(0); // get the wheel to spin
324 m_view
->setNameFilter(m_nameFilter
);
327 emit
aboutToOpenURL();
330 // Disable "Find File" and "Open Terminal" actions for non-file URLs,
331 // e.g. ftp, smb, etc. #279283
332 const bool isLocalUrl
= url
.isLocalFile();
333 m_findFileAction
->setEnabled(isLocalUrl
);
334 if (m_openTerminalAction
) {
335 m_openTerminalAction
->setEnabled(isLocalUrl
);
340 void DolphinPart::slotMessage(const QString
& msg
)
342 emit
setStatusBarText(msg
);
345 void DolphinPart::slotErrorMessage(const QString
& msg
)
349 //KMessageBox::error(m_view, msg);
352 void DolphinPart::slotRequestItemInfo(const KFileItem
& item
)
354 emit m_extension
->mouseOverInfo(item
);
358 const QString escapedText
= Qt::convertFromPlainText(item
.getStatusBarInfo());
359 ReadOnlyPart::setStatusBarText(QString("<qt>%1</qt>").arg(escapedText
));
363 void DolphinPart::slotItemActivated(const KFileItem
& item
)
365 KParts::OpenUrlArguments args
;
366 // Forget about the known mimetype if a target URL is used.
367 // Testcase: network:/ with a item (mimetype "inode/some-foo-service") pointing to a http URL (html)
368 if (item
.targetUrl() == item
.url()) {
369 args
.setMimeType(item
.mimetype());
372 // Ideally, konqueror should be changed to not require trustedSource for directory views,
373 // since the idea was not to need BrowserArguments for non-browser stuff...
374 KParts::BrowserArguments browserArgs
;
375 browserArgs
.trustedSource
= true;
376 emit m_extension
->openUrlRequest(item
.targetUrl(), args
, browserArgs
);
379 void DolphinPart::slotItemsActivated(const KFileItemList
& items
)
381 foreach (const KFileItem
& item
, items
) {
382 slotItemActivated(item
);
386 void DolphinPart::createNewWindow(const KUrl
& url
)
388 // TODO: Check issue N176832 for the missing QAIV signal; task 177399 - maybe this code
389 // should be moved into DolphinPart::slotItemActivated()
390 emit m_extension
->createNewWindow(url
);
393 void DolphinPart::slotOpenContextMenu(const QPoint
& pos
,
394 const KFileItem
& _item
,
396 const QList
<QAction
*>& customActions
)
398 KParts::BrowserExtension::PopupFlags popupFlags
= KParts::BrowserExtension::DefaultPopupItems
399 | KParts::BrowserExtension::ShowProperties
400 | KParts::BrowserExtension::ShowUrlOperations
;
402 KFileItem
item(_item
);
404 if (item
.isNull()) { // viewport context menu
405 popupFlags
|= KParts::BrowserExtension::ShowNavigationItems
| KParts::BrowserExtension::ShowUp
;
406 item
= m_view
->rootItem();
408 item
= KFileItem( S_IFDIR
, (mode_t
)-1, url() );
410 item
.setUrl(url()); // ensure we use the view url, not the canonical path (#213799)
413 // TODO: We should change the signature of the slots (and signals) for being able
414 // to tell for which items we want a popup.
416 if (m_view
->selectedItems().isEmpty()) {
419 items
= m_view
->selectedItems();
422 KFileItemListProperties
capabilities(items
);
424 KParts::BrowserExtension::ActionGroupMap actionGroups
;
425 QList
<QAction
*> editActions
;
426 editActions
+= m_view
->versionControlActions(m_view
->selectedItems());
427 editActions
+= customActions
;
429 if (!_item
.isNull()) { // only for context menu on one or more items
430 const bool supportsMoving
= capabilities
.supportsMoving();
432 if (capabilities
.supportsDeleting()) {
433 const bool showDeleteAction
= (KGlobal::config()->group("KDE").readEntry("ShowDeleteCommand", false) ||
434 !item
.isLocalFile());
435 const bool showMoveToTrashAction
= capabilities
.isLocal() && supportsMoving
;
437 if (showDeleteAction
&& showMoveToTrashAction
) {
438 delete m_removeAction
;
440 editActions
.append(actionCollection()->action("move_to_trash"));
441 editActions
.append(actionCollection()->action("delete"));
442 } else if (showDeleteAction
&& !showMoveToTrashAction
) {
443 editActions
.append(actionCollection()->action("delete"));
446 m_removeAction
= new DolphinRemoveAction(this, actionCollection());
447 editActions
.append(m_removeAction
);
448 m_removeAction
->update();
451 popupFlags
|= KParts::BrowserExtension::NoDeletion
;
454 if (supportsMoving
) {
455 editActions
.append(actionCollection()->action("rename"));
458 // Normally KonqPopupMenu only shows the "Create new" submenu in the current view
459 // since otherwise the created file would not be visible.
460 // But in treeview mode we should allow it.
461 if (m_view
->itemsExpandable())
462 popupFlags
|= KParts::BrowserExtension::ShowCreateDirectory
;
466 actionGroups
.insert("editactions", editActions
);
468 emit m_extension
->popupMenu(pos
,
470 KParts::OpenUrlArguments(),
471 KParts::BrowserArguments(),
476 void DolphinPart::slotDirectoryRedirection(const KUrl
& oldUrl
, const KUrl
& newUrl
)
478 //kDebug() << oldUrl << newUrl << "currentUrl=" << url();
479 if (oldUrl
.equals(url(), KUrl::CompareWithoutTrailingSlash
/* #207572 */)) {
480 KParts::ReadOnlyPart::setUrl(newUrl
);
481 const QString prettyUrl
= newUrl
.pathOrUrl();
482 emit m_extension
->setLocationBarUrl(prettyUrl
);
487 void DolphinPart::slotEditMimeType()
489 const KFileItemList items
= m_view
->selectedItems();
490 if (!items
.isEmpty()) {
491 KonqOperations::editMimeType(items
.first().mimetype(), m_view
);
495 void DolphinPart::slotSelectItemsMatchingPattern()
497 openSelectionDialog(i18nc("@title:window", "Select"),
498 i18n("Select all items matching this pattern:"),
502 void DolphinPart::slotUnselectItemsMatchingPattern()
504 openSelectionDialog(i18nc("@title:window", "Unselect"),
505 i18n("Unselect all items matching this pattern:"),
509 void DolphinPart::openSelectionDialog(const QString
& title
, const QString
& text
, bool selectItems
)
512 QString pattern
= KInputDialog::getText(title
, text
, "*", &okClicked
, m_view
);
514 if (okClicked
&& !pattern
.isEmpty()) {
515 QRegExp
patternRegExp(pattern
, Qt::CaseSensitive
, QRegExp::Wildcard
);
516 m_view
->selectItems(patternRegExp
, selectItems
);
520 void DolphinPart::setCurrentViewMode(const QString
& viewModeName
)
522 QAction
* action
= actionCollection()->action(viewModeName
);
527 QString
DolphinPart::currentViewMode() const
529 return m_actionHandler
->currentViewModeActionName();
532 void DolphinPart::setNameFilter(const QString
& nameFilter
)
534 // This is the "/home/dfaure/*.diff" kind of name filter (KDirLister::setNameFilter)
535 // which is unrelated to DolphinView::setNameFilter which is substring filtering in a proxy.
536 m_nameFilter
= nameFilter
;
537 // TODO save/restore name filter in saveState/restoreState like KonqDirPart did in kde3?
540 void DolphinPart::slotOpenTerminal()
542 QString
dir(QDir::homePath());
546 // If the given directory is not local, it can still be the URL of an
547 // ioslave using UDS_LOCAL_PATH which to be converted first.
548 u
= KIO::NetAccess::mostLocalUrl(u
, widget());
550 //If the URL is local after the above conversion, set the directory.
551 if (u
.isLocalFile()) {
552 dir
= u
.toLocalFile();
555 KToolInvocation::invokeTerminal(QString(), dir
);
558 void DolphinPart::slotFindFile()
560 KRun::run("kfind", QList
<QUrl
>() << url(), widget());
563 void DolphinPart::updateNewMenu()
565 // As requested by KNewFileMenu :
566 m_newFileMenu
->checkUpToDate();
567 m_newFileMenu
->setViewShowsHiddenFiles(m_view
->hiddenFilesShown());
568 // And set the files that the menu apply on :
569 m_newFileMenu
->setPopupFiles(url());
572 void DolphinPart::updateStatusBar()
574 const QString escapedText
= Qt::convertFromPlainText(m_view
->statusBarText());
575 emit
ReadOnlyPart::setStatusBarText(QString("<qt>%1</qt>").arg(escapedText
));
578 void DolphinPart::updateProgress(int percent
)
580 m_extension
->loadingProgress(percent
);
583 void DolphinPart::createDirectory()
585 m_newFileMenu
->setViewShowsHiddenFiles(m_view
->hiddenFilesShown());
586 m_newFileMenu
->setPopupFiles(url());
587 m_newFileMenu
->createDirectory();
590 void DolphinPart::setFilesToSelect(const KUrl::List
& files
)
592 if (files
.isEmpty()) {
596 m_view
->markUrlsAsSelected(files
);
597 m_view
->markUrlAsCurrent(files
.at(0));
600 bool DolphinPart::eventFilter(QObject
* obj
, QEvent
* event
)
602 const int type
= event
->type();
604 if ((type
== QEvent::KeyPress
|| type
== QEvent::KeyRelease
) && m_removeAction
) {
605 QMenu
* menu
= qobject_cast
<QMenu
*>(obj
);
606 if (menu
&& menu
->parent() == m_view
) {
607 QKeyEvent
* ev
= static_cast<QKeyEvent
*>(event
);
608 if (ev
->key() == Qt::Key_Shift
) {
609 m_removeAction
->update();
614 return KParts::ReadOnlyPart::eventFilter(obj
, event
);
617 #include "dolphinpart.moc"