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>
37 #include <KIO/NetAccess>
38 #include <KToolInvocation>
39 #include <kauthorized.h>
41 #include <KInputDialog>
42 #include <KProtocolInfo>
43 #include <kdeversion.h>
45 #if KDE_IS_VERSION(4, 9, 2)
46 #include "dolphinpart_ext.h"
49 #include "dolphinnewfilemenu.h"
50 #include "views/dolphinview.h"
51 #include "views/dolphinviewactionhandler.h"
52 #include "views/dolphinnewfilemenuobserver.h"
53 #include "views/dolphinremoteencoding.h"
54 #include "kitemviews/kfileitemmodel.h"
55 #include "kitemviews/private/kfileitemmodeldirlister.h"
57 #include <QActionGroup>
58 #include <QApplication>
61 #include <QTextDocument>
63 K_PLUGIN_FACTORY(DolphinPartFactory
, registerPlugin
<DolphinPart
>();)
64 K_EXPORT_PLUGIN(DolphinPartFactory("dolphinpart", "dolphin"))
66 DolphinPart::DolphinPart(QWidget
* parentWidget
, QObject
* parent
, const QVariantList
& args
)
67 : KParts::ReadOnlyPart(parent
)
68 ,m_openTerminalAction(0)
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(), SIGNAL(errorMessage(QString
)),
83 this, SLOT(slotErrorMessage(QString
)));
85 connect(m_view
, SIGNAL(directoryLoadingCompleted()), this, SIGNAL(completed()));
86 connect(m_view
, SIGNAL(directoryLoadingProgress(int)), this, SLOT(updateProgress(int)));
87 connect(m_view
, SIGNAL(errorMessage(QString
)), this, SLOT(slotErrorMessage(QString
)));
89 setXMLFile("dolphinpart.rc");
91 connect(m_view
, SIGNAL(infoMessage(QString
)),
92 this, SLOT(slotMessage(QString
)));
93 connect(m_view
, SIGNAL(operationCompletedMessage(QString
)),
94 this, SLOT(slotMessage(QString
)));
95 connect(m_view
, SIGNAL(errorMessage(QString
)),
96 this, SLOT(slotErrorMessage(QString
)));
97 connect(m_view
, SIGNAL(itemActivated(KFileItem
)),
98 this, SLOT(slotItemActivated(KFileItem
)));
99 connect(m_view
, SIGNAL(itemsActivated(KFileItemList
)),
100 this, SLOT(slotItemsActivated(KFileItemList
)));
101 connect(m_view
, SIGNAL(tabRequested(KUrl
)),
102 this, SLOT(createNewWindow(KUrl
)));
103 connect(m_view
, SIGNAL(requestContextMenu(QPoint
,KFileItem
,KUrl
,QList
<QAction
*>)),
104 this, SLOT(slotOpenContextMenu(QPoint
,KFileItem
,KUrl
,QList
<QAction
*>)));
105 connect(m_view
, SIGNAL(selectionChanged(KFileItemList
)),
106 m_extension
, SIGNAL(selectionInfo(KFileItemList
)));
107 connect(m_view
, SIGNAL(selectionChanged(KFileItemList
)),
108 this, SLOT(slotSelectionChanged(KFileItemList
)));
109 connect(m_view
, SIGNAL(requestItemInfo(KFileItem
)),
110 this, SLOT(slotRequestItemInfo(KFileItem
)));
111 connect(m_view
, SIGNAL(modeChanged(DolphinView::Mode
,DolphinView::Mode
)),
112 this, SIGNAL(viewModeChanged())); // relay signal
113 connect(m_view
, SIGNAL(redirection(KUrl
,KUrl
)),
114 this, SLOT(slotDirectoryRedirection(KUrl
,KUrl
)));
116 // Watch for changes that should result in updates to the
118 connect(m_view
, SIGNAL(itemCountChanged()), this, SLOT(updateStatusBar()));
119 connect(m_view
, SIGNAL(selectionChanged(KFileItemList
)), this, SLOT(updateStatusBar()));
121 m_actionHandler
= new DolphinViewActionHandler(actionCollection(), this);
122 m_actionHandler
->setCurrentView(m_view
);
123 connect(m_actionHandler
, SIGNAL(createDirectory()), SLOT(createDirectory()));
125 m_remoteEncoding
= new DolphinRemoteEncoding(this, m_actionHandler
);
126 connect(this, SIGNAL(aboutToOpenURL()),
127 m_remoteEncoding
, SLOT(slotAboutToOpenUrl()));
129 QClipboard
* clipboard
= QApplication::clipboard();
130 connect(clipboard
, SIGNAL(dataChanged()),
131 this, SLOT(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 #if KDE_IS_VERSION(4, 9, 2)
138 new DolphinPartListingFilterExtension(this);
140 KDirLister
* lister
= m_view
->m_model
->m_dirLister
;
142 DolphinPartListingNotificationExtension
* notifyExt
= new DolphinPartListingNotificationExtension(this);
143 connect(lister
, SIGNAL(newItems(KFileItemList
)), notifyExt
, SLOT(slotNewItems(KFileItemList
)));
144 connect(lister
, SIGNAL(itemsDeleted(KFileItemList
)), notifyExt
, SLOT(slotItemsDeleted(KFileItemList
)));
146 kWarning() << "NULL KDirLister object! KParts::ListingNotificationExtension will NOT be supported";
151 m_actionHandler
->updateViewActions();
152 slotSelectionChanged(KFileItemList()); // initially disable selection-dependent actions
154 // Listen to events from the app so we can update the remove key by
155 // checking for a Shift key press.
156 qApp
->installEventFilter(this);
158 // TODO there was a "always open a new window" (when clicking on a directory) setting in konqueror
159 // (sort of spacial navigation)
161 loadPlugins(this, this, componentData());
164 DolphinPart::~DolphinPart()
168 void DolphinPart::createActions()
172 m_newFileMenu
= new DolphinNewFileMenu(actionCollection(), this);
173 m_newFileMenu
->setParentWidget(widget());
174 connect(m_newFileMenu
->menu(), SIGNAL(aboutToShow()),
175 this, SLOT(updateNewMenu()));
177 KAction
*editMimeTypeAction
= actionCollection()->addAction( "editMimeType" );
178 editMimeTypeAction
->setText( i18nc("@action:inmenu Edit", "&Edit File Type..." ) );
179 connect(editMimeTypeAction
, SIGNAL(triggered()), SLOT(slotEditMimeType()));
181 KAction
* selectItemsMatching
= actionCollection()->addAction("select_items_matching");
182 selectItemsMatching
->setText(i18nc("@action:inmenu Edit", "Select Items Matching..."));
183 selectItemsMatching
->setShortcut(Qt::CTRL
| Qt::Key_S
);
184 connect(selectItemsMatching
, SIGNAL(triggered()), this, SLOT(slotSelectItemsMatchingPattern()));
186 KAction
* unselectItemsMatching
= actionCollection()->addAction("unselect_items_matching");
187 unselectItemsMatching
->setText(i18nc("@action:inmenu Edit", "Unselect Items Matching..."));
188 connect(unselectItemsMatching
, SIGNAL(triggered()), this, SLOT(slotUnselectItemsMatchingPattern()));
190 actionCollection()->addAction(KStandardAction::SelectAll
, "select_all", m_view
, SLOT(selectAll()));
192 KAction
* unselectAll
= actionCollection()->addAction("unselect_all");
193 unselectAll
->setText(i18nc("@action:inmenu Edit", "Unselect All"));
194 connect(unselectAll
, SIGNAL(triggered()), m_view
, SLOT(clearSelection()));
196 KAction
* invertSelection
= actionCollection()->addAction("invert_selection");
197 invertSelection
->setText(i18nc("@action:inmenu Edit", "Invert Selection"));
198 invertSelection
->setShortcut(Qt::CTRL
| Qt::SHIFT
| Qt::Key_A
);
199 connect(invertSelection
, SIGNAL(triggered()), m_view
, SLOT(invertSelection()));
201 // View menu: all done by DolphinViewActionHandler
205 QActionGroup
* goActionGroup
= new QActionGroup(this);
206 connect(goActionGroup
, SIGNAL(triggered(QAction
*)),
207 this, SLOT(slotGoTriggered(QAction
*)));
209 createGoAction("go_applications", "start-here-kde",
210 i18nc("@action:inmenu Go", "App&lications"), QString("programs:/"),
212 createGoAction("go_network_folders", "folder-remote",
213 i18nc("@action:inmenu Go", "&Network Folders"), QString("remote:/"),
215 createGoAction("go_settings", "preferences-system",
216 i18nc("@action:inmenu Go", "Sett&ings"), QString("settings:/"),
218 createGoAction("go_trash", "user-trash",
219 i18nc("@action:inmenu Go", "Trash"), QString("trash:/"),
221 createGoAction("go_autostart", "",
222 i18nc("@action:inmenu Go", "Autostart"), KGlobalSettings::autostartPath(),
226 m_findFileAction
= actionCollection()->addAction("find_file");
227 m_findFileAction
->setText(i18nc("@action:inmenu Tools", "Find File..."));
228 m_findFileAction
->setShortcut(Qt::CTRL
| Qt::Key_F
);
229 m_findFileAction
->setIcon(KIcon("edit-find"));
230 connect(m_findFileAction
, SIGNAL(triggered()), this, SLOT(slotFindFile()));
232 if (KAuthorized::authorizeKAction("shell_access")) {
233 m_openTerminalAction
= actionCollection()->addAction("open_terminal");
234 m_openTerminalAction
->setIcon(KIcon("utilities-terminal"));
235 m_openTerminalAction
->setText(i18nc("@action:inmenu Tools", "Open &Terminal"));
236 connect(m_openTerminalAction
, SIGNAL(triggered()), SLOT(slotOpenTerminal()));
237 m_openTerminalAction
->setShortcut(Qt::Key_F4
);
241 void DolphinPart::createGoAction(const char* name
, const char* iconName
,
242 const QString
& text
, const QString
& url
,
243 QActionGroup
* actionGroup
)
245 KAction
* action
= actionCollection()->addAction(name
);
246 action
->setIcon(KIcon(iconName
));
247 action
->setText(text
);
248 action
->setData(url
);
249 action
->setActionGroup(actionGroup
);
252 void DolphinPart::slotGoTriggered(QAction
* action
)
254 const QString url
= action
->data().toString();
255 emit m_extension
->openUrlRequest(KUrl(url
));
258 void DolphinPart::slotSelectionChanged(const KFileItemList
& selection
)
260 const bool hasSelection
= !selection
.isEmpty();
262 QAction
* renameAction
= actionCollection()->action("rename");
263 QAction
* moveToTrashAction
= actionCollection()->action("move_to_trash");
264 QAction
* deleteAction
= actionCollection()->action("delete");
265 QAction
* editMimeTypeAction
= actionCollection()->action("editMimeType");
266 QAction
* propertiesAction
= actionCollection()->action("properties");
267 QAction
* deleteWithTrashShortcut
= actionCollection()->action("delete_shortcut"); // see DolphinViewActionHandler
270 stateChanged("has_no_selection");
272 emit m_extension
->enableAction("cut", false);
273 emit m_extension
->enableAction("copy", false);
274 deleteWithTrashShortcut
->setEnabled(false);
275 editMimeTypeAction
->setEnabled(false);
277 stateChanged("has_selection");
279 // TODO share this code with DolphinMainWindow::updateEditActions (and the desktop code)
281 KFileItemListProperties
capabilities(selection
);
282 const bool enableMoveToTrash
= capabilities
.isLocal() && capabilities
.supportsMoving();
284 renameAction
->setEnabled(capabilities
.supportsMoving());
285 moveToTrashAction
->setEnabled(enableMoveToTrash
);
286 deleteAction
->setEnabled(capabilities
.supportsDeleting());
287 deleteWithTrashShortcut
->setEnabled(capabilities
.supportsDeleting() && !enableMoveToTrash
);
288 editMimeTypeAction
->setEnabled(true);
289 propertiesAction
->setEnabled(true);
290 emit m_extension
->enableAction("cut", capabilities
.supportsMoving());
291 emit m_extension
->enableAction("copy", true);
295 void DolphinPart::updatePasteAction()
297 QPair
<bool, QString
> pasteInfo
= m_view
->pasteInfo();
298 emit m_extension
->enableAction( "paste", pasteInfo
.first
);
299 emit m_extension
->setActionText( "paste", pasteInfo
.second
);
302 KAboutData
* DolphinPart::createAboutData()
304 return new KAboutData("dolphinpart", "dolphin", ki18nc("@title", "Dolphin Part"), "0.1");
307 bool DolphinPart::openUrl(const KUrl
& url
)
309 bool reload
= arguments().reload();
310 // A bit of a workaround so that changing the namefilter works: force reload.
311 // Otherwise DolphinView wouldn't relist the URL, so nothing would happen.
312 if (m_nameFilter
!= m_view
->nameFilter())
314 if (m_view
->url() == url
&& !reload
) { // DolphinView won't do anything in that case, so don't emit started
317 setUrl(url
); // remember it at the KParts level
318 KUrl
visibleUrl(url
);
319 if (!m_nameFilter
.isEmpty()) {
320 visibleUrl
.addPath(m_nameFilter
);
322 QString prettyUrl
= visibleUrl
.pathOrUrl();
323 emit
setWindowCaption(prettyUrl
);
324 emit m_extension
->setLocationBarUrl(prettyUrl
);
325 emit
started(0); // get the wheel to spin
326 m_view
->setNameFilter(m_nameFilter
);
329 emit
aboutToOpenURL();
332 // Disable "Find File" and "Open Terminal" actions for non-file URLs,
333 // e.g. ftp, smb, etc. #279283
334 const bool isLocalUrl
= url
.isLocalFile();
335 m_findFileAction
->setEnabled(isLocalUrl
);
336 if (m_openTerminalAction
) {
337 m_openTerminalAction
->setEnabled(isLocalUrl
);
342 void DolphinPart::slotMessage(const QString
& msg
)
344 emit
setStatusBarText(msg
);
347 void DolphinPart::slotErrorMessage(const QString
& msg
)
351 //KMessageBox::error(m_view, msg);
354 void DolphinPart::slotRequestItemInfo(const KFileItem
& item
)
356 emit m_extension
->mouseOverInfo(item
);
360 const QString escapedText
= Qt::convertFromPlainText(item
.getStatusBarInfo());
361 ReadOnlyPart::setStatusBarText(QString("<qt>%1</qt>").arg(escapedText
));
365 void DolphinPart::slotItemActivated(const KFileItem
& item
)
367 KParts::OpenUrlArguments args
;
368 // Forget about the known mimetype if a target URL is used.
369 // Testcase: network:/ with a item (mimetype "inode/some-foo-service") pointing to a http URL (html)
370 if (item
.targetUrl() == item
.url()) {
371 args
.setMimeType(item
.mimetype());
374 // Ideally, konqueror should be changed to not require trustedSource for directory views,
375 // since the idea was not to need BrowserArguments for non-browser stuff...
376 KParts::BrowserArguments browserArgs
;
377 browserArgs
.trustedSource
= true;
378 emit m_extension
->openUrlRequest(item
.targetUrl(), args
, browserArgs
);
381 void DolphinPart::slotItemsActivated(const KFileItemList
& items
)
383 foreach (const KFileItem
& item
, items
) {
384 slotItemActivated(item
);
388 void DolphinPart::createNewWindow(const KUrl
& url
)
390 // TODO: Check issue N176832 for the missing QAIV signal; task 177399 - maybe this code
391 // should be moved into DolphinPart::slotItemActivated()
392 emit m_extension
->createNewWindow(url
);
395 void DolphinPart::slotOpenContextMenu(const QPoint
& pos
,
396 const KFileItem
& _item
,
398 const QList
<QAction
*>& customActions
)
400 KParts::BrowserExtension::PopupFlags popupFlags
= KParts::BrowserExtension::DefaultPopupItems
401 | KParts::BrowserExtension::ShowProperties
402 | KParts::BrowserExtension::ShowUrlOperations
;
404 KFileItem
item(_item
);
406 if (item
.isNull()) { // viewport context menu
407 popupFlags
|= KParts::BrowserExtension::ShowNavigationItems
| KParts::BrowserExtension::ShowUp
;
408 item
= m_view
->rootItem();
410 item
= KFileItem( S_IFDIR
, (mode_t
)-1, url() );
412 item
.setUrl(url()); // ensure we use the view url, not the canonical path (#213799)
415 // TODO: We should change the signature of the slots (and signals) for being able
416 // to tell for which items we want a popup.
418 if (m_view
->selectedItems().isEmpty()) {
421 items
= m_view
->selectedItems();
424 KFileItemListProperties
capabilities(items
);
426 KParts::BrowserExtension::ActionGroupMap actionGroups
;
427 QList
<QAction
*> editActions
;
428 editActions
+= m_view
->versionControlActions(m_view
->selectedItems());
429 editActions
+= customActions
;
431 if (!_item
.isNull()) { // only for context menu on one or more items
432 const bool supportsMoving
= capabilities
.supportsMoving();
434 if (capabilities
.supportsDeleting()) {
435 const bool showDeleteAction
= (KGlobal::config()->group("KDE").readEntry("ShowDeleteCommand", false) ||
436 !item
.isLocalFile());
437 const bool showMoveToTrashAction
= capabilities
.isLocal() && supportsMoving
;
439 if (showDeleteAction
&& showMoveToTrashAction
) {
440 delete m_removeAction
;
442 editActions
.append(actionCollection()->action("move_to_trash"));
443 editActions
.append(actionCollection()->action("delete"));
444 } else if (showDeleteAction
&& !showMoveToTrashAction
) {
445 editActions
.append(actionCollection()->action("delete"));
448 m_removeAction
= new DolphinRemoveAction(this, actionCollection());
449 editActions
.append(m_removeAction
);
450 m_removeAction
->update();
453 popupFlags
|= KParts::BrowserExtension::NoDeletion
;
456 if (supportsMoving
) {
457 editActions
.append(actionCollection()->action("rename"));
460 // Normally KonqPopupMenu only shows the "Create new" submenu in the current view
461 // since otherwise the created file would not be visible.
462 // But in treeview mode we should allow it.
463 if (m_view
->itemsExpandable())
464 popupFlags
|= KParts::BrowserExtension::ShowCreateDirectory
;
468 actionGroups
.insert("editactions", editActions
);
470 emit m_extension
->popupMenu(pos
,
472 KParts::OpenUrlArguments(),
473 KParts::BrowserArguments(),
478 void DolphinPart::slotDirectoryRedirection(const KUrl
& oldUrl
, const KUrl
& newUrl
)
480 //kDebug() << oldUrl << newUrl << "currentUrl=" << url();
481 if (oldUrl
.equals(url(), KUrl::CompareWithoutTrailingSlash
/* #207572 */)) {
482 KParts::ReadOnlyPart::setUrl(newUrl
);
483 const QString prettyUrl
= newUrl
.pathOrUrl();
484 emit m_extension
->setLocationBarUrl(prettyUrl
);
489 void DolphinPart::slotEditMimeType()
491 const KFileItemList items
= m_view
->selectedItems();
492 if (!items
.isEmpty()) {
493 KonqOperations::editMimeType(items
.first().mimetype(), m_view
);
497 void DolphinPart::slotSelectItemsMatchingPattern()
499 openSelectionDialog(i18nc("@title:window", "Select"),
500 i18n("Select all items matching this pattern:"),
504 void DolphinPart::slotUnselectItemsMatchingPattern()
506 openSelectionDialog(i18nc("@title:window", "Unselect"),
507 i18n("Unselect all items matching this pattern:"),
511 void DolphinPart::openSelectionDialog(const QString
& title
, const QString
& text
, bool selectItems
)
514 QString pattern
= KInputDialog::getText(title
, text
, "*", &okClicked
, m_view
);
516 if (okClicked
&& !pattern
.isEmpty()) {
517 QRegExp
patternRegExp(pattern
, Qt::CaseSensitive
, QRegExp::Wildcard
);
518 m_view
->selectItems(patternRegExp
, selectItems
);
522 void DolphinPart::setCurrentViewMode(const QString
& viewModeName
)
524 QAction
* action
= actionCollection()->action(viewModeName
);
529 QString
DolphinPart::currentViewMode() const
531 return m_actionHandler
->currentViewModeActionName();
534 void DolphinPart::setNameFilter(const QString
& nameFilter
)
536 // This is the "/home/dfaure/*.diff" kind of name filter (KDirLister::setNameFilter)
537 // which is unrelated to DolphinView::setNameFilter which is substring filtering in a proxy.
538 m_nameFilter
= nameFilter
;
539 // TODO save/restore name filter in saveState/restoreState like KonqDirPart did in kde3?
542 void DolphinPart::slotOpenTerminal()
544 QString
dir(QDir::homePath());
548 // If the given directory is not local, it can still be the URL of an
549 // ioslave using UDS_LOCAL_PATH which to be converted first.
550 u
= KIO::NetAccess::mostLocalUrl(u
, widget());
552 //If the URL is local after the above conversion, set the directory.
553 if (u
.isLocalFile()) {
554 dir
= u
.toLocalFile();
557 KToolInvocation::invokeTerminal(QString(), dir
);
560 void DolphinPart::slotFindFile()
562 KRun::run("kfind", url(), widget());
565 void DolphinPart::updateNewMenu()
567 // As requested by KNewFileMenu :
568 m_newFileMenu
->checkUpToDate();
569 m_newFileMenu
->setViewShowsHiddenFiles(m_view
->hiddenFilesShown());
570 // And set the files that the menu apply on :
571 m_newFileMenu
->setPopupFiles(url());
574 void DolphinPart::updateStatusBar()
576 const QString escapedText
= Qt::convertFromPlainText(m_view
->statusBarText());
577 emit
ReadOnlyPart::setStatusBarText(QString("<qt>%1</qt>").arg(escapedText
));
580 void DolphinPart::updateProgress(int percent
)
582 m_extension
->loadingProgress(percent
);
585 void DolphinPart::createDirectory()
587 m_newFileMenu
->setViewShowsHiddenFiles(m_view
->hiddenFilesShown());
588 m_newFileMenu
->setPopupFiles(url());
589 m_newFileMenu
->createDirectory();
592 void DolphinPart::setFilesToSelect(const KUrl::List
& files
)
594 if (files
.isEmpty()) {
598 m_view
->markUrlsAsSelected(files
);
599 m_view
->markUrlAsCurrent(files
.at(0));
602 bool DolphinPart::eventFilter(QObject
* obj
, QEvent
* event
)
604 const int type
= event
->type();
606 if ((type
== QEvent::KeyPress
|| type
== QEvent::KeyRelease
) && m_removeAction
) {
607 QMenu
* menu
= qobject_cast
<QMenu
*>(obj
);
608 if (menu
&& menu
->parent() == m_view
) {
609 QKeyEvent
* ev
= static_cast<QKeyEvent
*>(event
);
610 if (ev
->key() == Qt::Key_Shift
) {
611 m_removeAction
->update();
616 return KParts::ReadOnlyPart::eventFilter(obj
, event
);
619 #include "dolphinpart.moc"