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"
22 #include <KFileItemListProperties>
23 #include <konq_operations.h>
26 #include <KActionCollection>
27 #include <KConfigGroup>
29 #include <KGlobalSettings>
30 #include <KIconLoader>
32 #include <KMessageBox>
33 #include <KPluginFactory>
35 #include <KToggleAction>
36 #include <KIO/NetAccess>
37 #include <KToolInvocation>
38 #include <kauthorized.h>
39 #include <KNewFileMenu>
41 #include <KInputDialog>
42 #include <KProtocolInfo>
44 #include "views/dolphinview.h"
45 #include "views/dolphinviewactionhandler.h"
46 #include "views/dolphinnewfilemenuobserver.h"
47 #include "views/dolphinremoteencoding.h"
49 #include <QActionGroup>
50 #include <QApplication>
54 K_PLUGIN_FACTORY(DolphinPartFactory
, registerPlugin
<DolphinPart
>();)
55 K_EXPORT_PLUGIN(DolphinPartFactory("dolphinpart", "dolphin"))
57 DolphinPart::DolphinPart(QWidget
* parentWidget
, QObject
* parent
, const QVariantList
& args
)
58 : KParts::ReadOnlyPart(parent
)
59 ,m_openTerminalAction(0)
62 setComponentData(DolphinPartFactory::componentData(), false);
63 m_extension
= new DolphinPartBrowserExtension(this);
64 new DolphinPartFileInfoExtension(this);
66 // make sure that other apps using this part find Dolphin's view-file-columns icons
67 KIconLoader::global()->addAppDir("dolphin");
69 m_view
= new DolphinView(KUrl(), parentWidget
);
70 m_view
->setTabsForFilesEnabled(true);
73 connect(m_view
, SIGNAL(directoryLoadingCompleted()), this, SIGNAL(completed()));
74 connect(m_view
, SIGNAL(directoryLoadingProgress(int)), this, SLOT(updateProgress(int)));
75 connect(m_view
, SIGNAL(errorMessage(QString
)), this, SLOT(slotErrorMessage(QString
)));
77 setXMLFile("dolphinpart.rc");
79 connect(m_view
, SIGNAL(infoMessage(QString
)),
80 this, SLOT(slotMessage(QString
)));
81 connect(m_view
, SIGNAL(operationCompletedMessage(QString
)),
82 this, SLOT(slotMessage(QString
)));
83 connect(m_view
, SIGNAL(errorMessage(QString
)),
84 this, SLOT(slotErrorMessage(QString
)));
85 connect(m_view
, SIGNAL(itemActivated(KFileItem
)),
86 this, SLOT(slotItemActivated(KFileItem
)));
87 connect(m_view
, SIGNAL(tabRequested(KUrl
)),
88 this, SLOT(createNewWindow(KUrl
)));
89 connect(m_view
, SIGNAL(requestContextMenu(QPoint
,KFileItem
,KUrl
,QList
<QAction
*>)),
90 this, SLOT(slotOpenContextMenu(QPoint
,KFileItem
,KUrl
,QList
<QAction
*>)));
91 connect(m_view
, SIGNAL(selectionChanged(KFileItemList
)),
92 m_extension
, SIGNAL(selectionInfo(KFileItemList
)));
93 connect(m_view
, SIGNAL(selectionChanged(KFileItemList
)),
94 this, SLOT(slotSelectionChanged(KFileItemList
)));
95 connect(m_view
, SIGNAL(requestItemInfo(KFileItem
)),
96 this, SLOT(slotRequestItemInfo(KFileItem
)));
97 connect(m_view
, SIGNAL(modeChanged(DolphinView::Mode
,DolphinView::Mode
)),
98 this, SIGNAL(viewModeChanged())); // relay signal
99 connect(m_view
, SIGNAL(redirection(KUrl
,KUrl
)),
100 this, SLOT(slotDirectoryRedirection(KUrl
,KUrl
)));
102 // Watch for changes that should result in updates to the
104 connect(m_view
, SIGNAL(itemCountChanged()), this, SLOT(updateStatusBar()));
105 connect(m_view
, SIGNAL(selectionChanged(KFileItemList
)), this, SLOT(updateStatusBar()));
107 m_actionHandler
= new DolphinViewActionHandler(actionCollection(), this);
108 m_actionHandler
->setCurrentView(m_view
);
109 connect(m_actionHandler
, SIGNAL(createDirectory()), SLOT(createDirectory()));
111 m_remoteEncoding
= new DolphinRemoteEncoding(this, m_actionHandler
);
112 connect(this, SIGNAL(aboutToOpenURL()),
113 m_remoteEncoding
, SLOT(slotAboutToOpenUrl()));
115 QClipboard
* clipboard
= QApplication::clipboard();
116 connect(clipboard
, SIGNAL(dataChanged()),
117 this, SLOT(updatePasteAction()));
120 m_actionHandler
->updateViewActions();
121 slotSelectionChanged(KFileItemList()); // initially disable selection-dependent actions
123 // TODO there was a "always open a new window" (when clicking on a directory) setting in konqueror
124 // (sort of spacial navigation)
126 loadPlugins(this, this, componentData());
130 DolphinPart::~DolphinPart()
132 DolphinNewFileMenuObserver::instance().detach(m_newFileMenu
);
135 void DolphinPart::createActions()
139 m_newFileMenu
= new KNewFileMenu(actionCollection(), "new_menu", this);
140 m_newFileMenu
->setParentWidget(widget());
141 DolphinNewFileMenuObserver::instance().attach(m_newFileMenu
);
142 connect(m_newFileMenu
->menu(), SIGNAL(aboutToShow()),
143 this, SLOT(updateNewMenu()));
145 KAction
*editMimeTypeAction
= actionCollection()->addAction( "editMimeType" );
146 editMimeTypeAction
->setText( i18nc("@action:inmenu Edit", "&Edit File Type..." ) );
147 connect(editMimeTypeAction
, SIGNAL(triggered()), SLOT(slotEditMimeType()));
149 KAction
* selectItemsMatching
= actionCollection()->addAction("select_items_matching");
150 selectItemsMatching
->setText(i18nc("@action:inmenu Edit", "Select Items Matching..."));
151 selectItemsMatching
->setShortcut(Qt::CTRL
| Qt::Key_S
);
152 connect(selectItemsMatching
, SIGNAL(triggered()), this, SLOT(slotSelectItemsMatchingPattern()));
154 KAction
* unselectItemsMatching
= actionCollection()->addAction("unselect_items_matching");
155 unselectItemsMatching
->setText(i18nc("@action:inmenu Edit", "Unselect Items Matching..."));
156 connect(unselectItemsMatching
, SIGNAL(triggered()), this, SLOT(slotUnselectItemsMatchingPattern()));
158 actionCollection()->addAction(KStandardAction::SelectAll
, "select_all", m_view
, SLOT(selectAll()));
160 KAction
* unselectAll
= actionCollection()->addAction("unselect_all");
161 unselectAll
->setText(i18nc("@action:inmenu Edit", "Unselect All"));
162 connect(unselectAll
, SIGNAL(triggered()), m_view
, SLOT(clearSelection()));
164 KAction
* invertSelection
= actionCollection()->addAction("invert_selection");
165 invertSelection
->setText(i18nc("@action:inmenu Edit", "Invert Selection"));
166 invertSelection
->setShortcut(Qt::CTRL
| Qt::SHIFT
| Qt::Key_A
);
167 connect(invertSelection
, SIGNAL(triggered()), m_view
, SLOT(invertSelection()));
169 // View menu: all done by DolphinViewActionHandler
173 QActionGroup
* goActionGroup
= new QActionGroup(this);
174 connect(goActionGroup
, SIGNAL(triggered(QAction
*)),
175 this, SLOT(slotGoTriggered(QAction
*)));
177 createGoAction("go_applications", "start-here-kde",
178 i18nc("@action:inmenu Go", "App&lications"), QString("programs:/"),
180 createGoAction("go_network_folders", "folder-remote",
181 i18nc("@action:inmenu Go", "&Network Folders"), QString("remote:/"),
183 createGoAction("go_settings", "preferences-system",
184 i18nc("@action:inmenu Go", "Sett&ings"), QString("settings:/"),
186 createGoAction("go_trash", "user-trash",
187 i18nc("@action:inmenu Go", "Trash"), QString("trash:/"),
189 createGoAction("go_autostart", "",
190 i18nc("@action:inmenu Go", "Autostart"), KGlobalSettings::autostartPath(),
194 m_findFileAction
= actionCollection()->addAction("find_file");
195 m_findFileAction
->setText(i18nc("@action:inmenu Tools", "Find File..."));
196 m_findFileAction
->setShortcut(Qt::CTRL
| Qt::Key_F
);
197 m_findFileAction
->setIcon(KIcon("edit-find"));
198 connect(m_findFileAction
, SIGNAL(triggered()), this, SLOT(slotFindFile()));
200 if (KAuthorized::authorizeKAction("shell_access")) {
201 m_openTerminalAction
= actionCollection()->addAction("open_terminal");
202 m_openTerminalAction
->setIcon(KIcon("utilities-terminal"));
203 m_openTerminalAction
->setText(i18nc("@action:inmenu Tools", "Open &Terminal"));
204 connect(m_openTerminalAction
, SIGNAL(triggered()), SLOT(slotOpenTerminal()));
205 m_openTerminalAction
->setShortcut(Qt::Key_F4
);
209 void DolphinPart::createGoAction(const char* name
, const char* iconName
,
210 const QString
& text
, const QString
& url
,
211 QActionGroup
* actionGroup
)
213 KAction
* action
= actionCollection()->addAction(name
);
214 action
->setIcon(KIcon(iconName
));
215 action
->setText(text
);
216 action
->setData(url
);
217 action
->setActionGroup(actionGroup
);
220 void DolphinPart::slotGoTriggered(QAction
* action
)
222 const QString url
= action
->data().toString();
223 emit m_extension
->openUrlRequest(KUrl(url
));
226 void DolphinPart::slotSelectionChanged(const KFileItemList
& selection
)
228 const bool hasSelection
= !selection
.isEmpty();
230 QAction
* renameAction
= actionCollection()->action("rename");
231 QAction
* moveToTrashAction
= actionCollection()->action("move_to_trash");
232 QAction
* deleteAction
= actionCollection()->action("delete");
233 QAction
* editMimeTypeAction
= actionCollection()->action("editMimeType");
234 QAction
* propertiesAction
= actionCollection()->action("properties");
235 QAction
* deleteWithTrashShortcut
= actionCollection()->action("delete_shortcut"); // see DolphinViewActionHandler
238 stateChanged("has_no_selection");
240 emit m_extension
->enableAction("cut", false);
241 emit m_extension
->enableAction("copy", false);
242 deleteWithTrashShortcut
->setEnabled(false);
243 editMimeTypeAction
->setEnabled(false);
245 stateChanged("has_selection");
247 // TODO share this code with DolphinMainWindow::updateEditActions (and the desktop code)
249 KFileItemListProperties
capabilities(selection
);
250 const bool enableMoveToTrash
= capabilities
.isLocal() && capabilities
.supportsMoving();
252 renameAction
->setEnabled(capabilities
.supportsMoving());
253 moveToTrashAction
->setEnabled(enableMoveToTrash
);
254 deleteAction
->setEnabled(capabilities
.supportsDeleting());
255 deleteWithTrashShortcut
->setEnabled(capabilities
.supportsDeleting() && !enableMoveToTrash
);
256 editMimeTypeAction
->setEnabled(true);
257 propertiesAction
->setEnabled(true);
258 emit m_extension
->enableAction("cut", capabilities
.supportsMoving());
259 emit m_extension
->enableAction("copy", true);
263 void DolphinPart::updatePasteAction()
265 QPair
<bool, QString
> pasteInfo
= m_view
->pasteInfo();
266 emit m_extension
->enableAction( "paste", pasteInfo
.first
);
267 emit m_extension
->setActionText( "paste", pasteInfo
.second
);
270 KAboutData
* DolphinPart::createAboutData()
272 return new KAboutData("dolphinpart", "dolphin", ki18nc("@title", "Dolphin Part"), "0.1");
275 bool DolphinPart::openUrl(const KUrl
& url
)
277 bool reload
= arguments().reload();
278 // A bit of a workaround so that changing the namefilter works: force reload.
279 // Otherwise DolphinView wouldn't relist the URL, so nothing would happen.
280 if (m_nameFilter
!= m_view
->nameFilter())
282 if (m_view
->url() == url
&& !reload
) { // DolphinView won't do anything in that case, so don't emit started
285 setUrl(url
); // remember it at the KParts level
286 KUrl
visibleUrl(url
);
287 if (!m_nameFilter
.isEmpty()) {
288 visibleUrl
.addPath(m_nameFilter
);
290 QString prettyUrl
= visibleUrl
.pathOrUrl();
291 emit
setWindowCaption(prettyUrl
);
292 emit m_extension
->setLocationBarUrl(prettyUrl
);
293 emit
started(0); // get the wheel to spin
294 m_view
->setNameFilter(m_nameFilter
);
297 emit
aboutToOpenURL();
300 // Disable "Find File" and "Open Terminal" actions for non-file URLs,
301 // e.g. ftp, smb, etc. #279283
302 const bool isLocalUrl
= url
.isLocalFile();
303 m_findFileAction
->setEnabled(isLocalUrl
);
304 if (m_openTerminalAction
) {
305 m_openTerminalAction
->setEnabled(isLocalUrl
);
310 void DolphinPart::slotMessage(const QString
& msg
)
312 emit
setStatusBarText(msg
);
315 void DolphinPart::slotErrorMessage(const QString
& msg
)
319 //KMessageBox::error(m_view, msg);
322 void DolphinPart::slotRequestItemInfo(const KFileItem
& item
)
324 emit m_extension
->mouseOverInfo(item
);
328 ReadOnlyPart::setStatusBarText(item
.getStatusBarInfo());
332 void DolphinPart::slotItemActivated(const KFileItem
& item
)
334 KParts::OpenUrlArguments args
;
335 // Forget about the known mimetype if a target URL is used.
336 // Testcase: network:/ with a item (mimetype "inode/some-foo-service") pointing to a http URL (html)
337 if (item
.targetUrl() == item
.url()) {
338 args
.setMimeType(item
.mimetype());
341 // Ideally, konqueror should be changed to not require trustedSource for directory views,
342 // since the idea was not to need BrowserArguments for non-browser stuff...
343 KParts::BrowserArguments browserArgs
;
344 browserArgs
.trustedSource
= true;
345 emit m_extension
->openUrlRequest(item
.targetUrl(), args
, browserArgs
);
348 void DolphinPart::createNewWindow(const KUrl
& url
)
350 // TODO: Check issue N176832 for the missing QAIV signal; task 177399 - maybe this code
351 // should be moved into DolphinPart::slotItemActivated()
352 emit m_extension
->createNewWindow(url
);
355 void DolphinPart::slotOpenContextMenu(const QPoint
& pos
,
356 const KFileItem
& _item
,
358 const QList
<QAction
*>& customActions
)
360 KParts::BrowserExtension::PopupFlags popupFlags
= KParts::BrowserExtension::DefaultPopupItems
361 | KParts::BrowserExtension::ShowProperties
362 | KParts::BrowserExtension::ShowUrlOperations
;
364 KFileItem
item(_item
);
366 if (item
.isNull()) { // viewport context menu
367 popupFlags
|= KParts::BrowserExtension::ShowNavigationItems
| KParts::BrowserExtension::ShowUp
;
368 item
= m_view
->rootItem();
370 item
= KFileItem( S_IFDIR
, (mode_t
)-1, url() );
372 item
.setUrl(url()); // ensure we use the view url, not the canonical path (#213799)
375 // TODO: We should change the signature of the slots (and signals) for being able
376 // to tell for which items we want a popup.
378 if (m_view
->selectedItems().isEmpty()) {
381 items
= m_view
->selectedItems();
384 KFileItemListProperties
capabilities(items
);
386 KParts::BrowserExtension::ActionGroupMap actionGroups
;
387 QList
<QAction
*> editActions
;
388 editActions
+= m_view
->versionControlActions(m_view
->selectedItems());
389 editActions
+= customActions
;
391 if (!_item
.isNull()) { // only for context menu on one or more items
392 bool supportsDeleting
= capabilities
.supportsDeleting();
393 bool supportsMoving
= capabilities
.supportsMoving();
395 if (!supportsDeleting
) {
396 popupFlags
|= KParts::BrowserExtension::NoDeletion
;
399 if (supportsMoving
) {
400 editActions
.append(actionCollection()->action("rename"));
403 bool addTrash
= capabilities
.isLocal() && supportsMoving
;
405 if (supportsDeleting
) {
406 if ( !item
.isLocalFile() )
408 else if (QApplication::keyboardModifiers() & Qt::ShiftModifier
) {
413 KSharedConfig::Ptr globalConfig
= KSharedConfig::openConfig("kdeglobals", KConfig::IncludeGlobals
);
414 KConfigGroup
configGroup(globalConfig
, "KDE");
415 addDel
= configGroup
.readEntry("ShowDeleteCommand", false);
420 editActions
.append(actionCollection()->action("move_to_trash"));
422 editActions
.append(actionCollection()->action("delete"));
424 // Normally KonqPopupMenu only shows the "Create new" submenu in the current view
425 // since otherwise the created file would not be visible.
426 // But in treeview mode we should allow it.
427 if (m_view
->itemsExpandable())
428 popupFlags
|= KParts::BrowserExtension::ShowCreateDirectory
;
432 actionGroups
.insert("editactions", editActions
);
434 emit m_extension
->popupMenu(pos
,
436 KParts::OpenUrlArguments(),
437 KParts::BrowserArguments(),
442 void DolphinPart::slotDirectoryRedirection(const KUrl
& oldUrl
, const KUrl
& newUrl
)
444 //kDebug() << oldUrl << newUrl << "currentUrl=" << url();
445 if (oldUrl
.equals(url(), KUrl::CompareWithoutTrailingSlash
/* #207572 */)) {
446 KParts::ReadOnlyPart::setUrl(newUrl
);
447 const QString prettyUrl
= newUrl
.pathOrUrl();
448 emit m_extension
->setLocationBarUrl(prettyUrl
);
453 void DolphinPart::slotEditMimeType()
455 const KFileItemList items
= m_view
->selectedItems();
456 if (!items
.isEmpty()) {
457 KonqOperations::editMimeType(items
.first().mimetype(), m_view
);
461 void DolphinPart::slotSelectItemsMatchingPattern()
463 openSelectionDialog(i18nc("@title:window", "Select"),
464 i18n("Select all items matching this pattern:"),
468 void DolphinPart::slotUnselectItemsMatchingPattern()
470 openSelectionDialog(i18nc("@title:window", "Unselect"),
471 i18n("Unselect all items matching this pattern:"),
475 void DolphinPart::openSelectionDialog(const QString
& title
, const QString
& text
, bool selectItems
)
478 QString pattern
= KInputDialog::getText(title
, text
, "*", &okClicked
, m_view
);
480 if (okClicked
&& !pattern
.isEmpty()) {
481 QRegExp
patternRegExp(pattern
, Qt::CaseSensitive
, QRegExp::Wildcard
);
482 m_view
->selectItems(patternRegExp
, selectItems
);
486 void DolphinPart::setCurrentViewMode(const QString
& viewModeName
)
488 QAction
* action
= actionCollection()->action(viewModeName
);
493 QString
DolphinPart::currentViewMode() const
495 return m_actionHandler
->currentViewModeActionName();
498 void DolphinPart::setNameFilter(const QString
& nameFilter
)
500 // This is the "/home/dfaure/*.diff" kind of name filter (KDirLister::setNameFilter)
501 // which is unrelated to DolphinView::setNameFilter which is substring filtering in a proxy.
502 m_nameFilter
= nameFilter
;
503 // TODO save/restore name filter in saveState/restoreState like KonqDirPart did in kde3?
506 void DolphinPart::slotOpenTerminal()
508 QString
dir(QDir::homePath());
512 // If the given directory is not local, it can still be the URL of an
513 // ioslave using UDS_LOCAL_PATH which to be converted first.
514 u
= KIO::NetAccess::mostLocalUrl(u
, widget());
516 //If the URL is local after the above conversion, set the directory.
517 if (u
.isLocalFile()) {
518 dir
= u
.toLocalFile();
521 KToolInvocation::invokeTerminal(QString(), dir
);
524 void DolphinPart::slotFindFile()
526 KRun::run("kfind", url(), widget());
529 void DolphinPart::updateNewMenu()
531 // As requested by KNewFileMenu :
532 m_newFileMenu
->checkUpToDate();
533 m_newFileMenu
->setViewShowsHiddenFiles(m_view
->hiddenFilesShown());
534 // And set the files that the menu apply on :
535 m_newFileMenu
->setPopupFiles(url());
538 void DolphinPart::updateStatusBar()
540 emit
ReadOnlyPart::setStatusBarText(m_view
->statusBarText());
543 void DolphinPart::updateProgress(int percent
)
545 m_extension
->loadingProgress(percent
);
548 void DolphinPart::createDirectory()
550 m_newFileMenu
->setViewShowsHiddenFiles(m_view
->hiddenFilesShown());
551 m_newFileMenu
->setPopupFiles(url());
552 m_newFileMenu
->createDirectory();
555 void DolphinPart::setFilesToSelect(const KUrl::List
& files
)
557 m_view
->markUrlsAsSelected(files
);
562 void DolphinPartBrowserExtension::restoreState(QDataStream
&stream
)
564 KParts::BrowserExtension::restoreState(stream
);
565 m_part
->view()->restoreState(stream
);
568 void DolphinPartBrowserExtension::saveState(QDataStream
&stream
)
570 KParts::BrowserExtension::saveState(stream
);
571 m_part
->view()->saveState(stream
);
574 void DolphinPartBrowserExtension::cut()
576 m_part
->view()->cutSelectedItems();
579 void DolphinPartBrowserExtension::copy()
581 m_part
->view()->copySelectedItems();
584 void DolphinPartBrowserExtension::paste()
586 m_part
->view()->paste();
589 void DolphinPartBrowserExtension::pasteTo(const KUrl
&)
591 m_part
->view()->pasteIntoFolder();
594 void DolphinPartBrowserExtension::reparseConfiguration()
596 m_part
->view()->readSettings();
601 DolphinPartFileInfoExtension::DolphinPartFileInfoExtension(DolphinPart
* part
)
602 : KParts::FileInfoExtension(part
)
606 DolphinPart
* DolphinPartFileInfoExtension::part() const
608 return static_cast<DolphinPart
*>(parent());
611 bool DolphinPartFileInfoExtension::hasSelection() const
613 return part()->view()->selectedItemsCount() > 0;
616 KParts::FileInfoExtension::QueryModes
DolphinPartFileInfoExtension::supportedQueryModes() const
618 return (KParts::FileInfoExtension::AllItems
| KParts::FileInfoExtension::SelectedItems
);
621 KFileItemList
DolphinPartFileInfoExtension::queryFor(KParts::FileInfoExtension::QueryMode mode
) const
625 if (mode
== KParts::FileInfoExtension::None
)
628 if (!(supportedQueryModes() & mode
))
632 case KParts::FileInfoExtension::SelectedItems
:
634 return part()->view()->selectedItems();
636 case KParts::FileInfoExtension::AllItems
:
637 return part()->view()->items();
645 #include "dolphinpart.moc"