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 "settings/dolphinsettings.h"
45 #include "views/dolphinview.h"
46 #include "views/dolphinviewactionhandler.h"
47 #include "views/dolphinnewfilemenuobserver.h"
48 #include "views/dolphinremoteencoding.h"
49 #include "views/dolphindirlister.h"
51 #include <QActionGroup>
52 #include <QApplication>
55 K_PLUGIN_FACTORY(DolphinPartFactory
, registerPlugin
<DolphinPart
>();)
56 K_EXPORT_PLUGIN(DolphinPartFactory("dolphinpart", "dolphin"))
58 DolphinPart::DolphinPart(QWidget
* parentWidget
, QObject
* parent
, const QVariantList
& args
)
59 : KParts::ReadOnlyPart(parent
)
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(finishedPathLoading(KUrl
)), this, SLOT(slotCompleted(KUrl
)));
74 connect(m_view
, SIGNAL(pathLoadingProgress(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(KFileItem
,KUrl
,QList
<QAction
*>)),
90 this, SLOT(slotOpenContextMenu(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(slotRedirection(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 DolphinSettings::instance().save();
133 DolphinNewFileMenuObserver::instance().detach(m_newFileMenu
);
136 void DolphinPart::createActions()
140 m_newFileMenu
= new KNewFileMenu(actionCollection(), "new_menu", this);
141 m_newFileMenu
->setParentWidget(widget());
142 DolphinNewFileMenuObserver::instance().attach(m_newFileMenu
);
143 connect(m_newFileMenu
->menu(), SIGNAL(aboutToShow()),
144 this, SLOT(updateNewMenu()));
146 KAction
*editMimeTypeAction
= actionCollection()->addAction( "editMimeType" );
147 editMimeTypeAction
->setText( i18nc("@action:inmenu Edit", "&Edit File Type..." ) );
148 connect(editMimeTypeAction
, SIGNAL(triggered()), SLOT(slotEditMimeType()));
150 KAction
* selectItemsMatching
= actionCollection()->addAction("select_items_matching");
151 selectItemsMatching
->setText(i18nc("@action:inmenu Edit", "Select Items Matching..."));
152 selectItemsMatching
->setShortcut(Qt::CTRL
| Qt::Key_S
);
153 connect(selectItemsMatching
, SIGNAL(triggered()), this, SLOT(slotSelectItemsMatchingPattern()));
155 KAction
* unselectItemsMatching
= actionCollection()->addAction("unselect_items_matching");
156 unselectItemsMatching
->setText(i18nc("@action:inmenu Edit", "Unselect Items Matching..."));
157 connect(unselectItemsMatching
, SIGNAL(triggered()), this, SLOT(slotUnselectItemsMatchingPattern()));
159 actionCollection()->addAction(KStandardAction::SelectAll
, "select_all", m_view
, SLOT(selectAll()));
161 KAction
* unselectAll
= actionCollection()->addAction("unselect_all");
162 unselectAll
->setText(i18nc("@action:inmenu Edit", "Unselect All"));
163 connect(unselectAll
, SIGNAL(triggered()), m_view
, SLOT(clearSelection()));
165 KAction
* invertSelection
= actionCollection()->addAction("invert_selection");
166 invertSelection
->setText(i18nc("@action:inmenu Edit", "Invert Selection"));
167 invertSelection
->setShortcut(Qt::CTRL
| Qt::SHIFT
| Qt::Key_A
);
168 connect(invertSelection
, SIGNAL(triggered()), m_view
, SLOT(invertSelection()));
170 // View menu: all done by DolphinViewActionHandler
174 QActionGroup
* goActionGroup
= new QActionGroup(this);
175 connect(goActionGroup
, SIGNAL(triggered(QAction
*)),
176 this, SLOT(slotGoTriggered(QAction
*)));
178 createGoAction("go_applications", "start-here-kde",
179 i18nc("@action:inmenu Go", "App&lications"), QString("programs:/"),
181 createGoAction("go_network_folders", "folder-remote",
182 i18nc("@action:inmenu Go", "&Network Folders"), QString("remote:/"),
184 createGoAction("go_settings", "preferences-system",
185 i18nc("@action:inmenu Go", "Sett&ings"), QString("settings:/"),
187 createGoAction("go_trash", "user-trash",
188 i18nc("@action:inmenu Go", "Trash"), QString("trash:/"),
190 createGoAction("go_autostart", "",
191 i18nc("@action:inmenu Go", "Autostart"), KGlobalSettings::autostartPath(),
195 KAction
* findFile
= actionCollection()->addAction("find_file");
196 findFile
->setText(i18nc("@action:inmenu Tools", "Find File..."));
197 findFile
->setShortcut(Qt::CTRL
| Qt::Key_F
);
198 findFile
->setIcon(KIcon("edit-find"));
199 connect(findFile
, SIGNAL(triggered()), this, SLOT(slotFindFile()));
201 if (KAuthorized::authorizeKAction("shell_access")) {
202 KAction
* action
= actionCollection()->addAction("open_terminal");
203 action
->setIcon(KIcon("utilities-terminal"));
204 action
->setText(i18nc("@action:inmenu Tools", "Open &Terminal"));
205 connect(action
, SIGNAL(triggered()), SLOT(slotOpenTerminal()));
206 action
->setShortcut(Qt::Key_F4
);
210 void DolphinPart::createGoAction(const char* name
, const char* iconName
,
211 const QString
& text
, const QString
& url
,
212 QActionGroup
* actionGroup
)
214 KAction
* action
= actionCollection()->addAction(name
);
215 action
->setIcon(KIcon(iconName
));
216 action
->setText(text
);
217 action
->setData(url
);
218 action
->setActionGroup(actionGroup
);
221 void DolphinPart::slotGoTriggered(QAction
* action
)
223 const QString url
= action
->data().toString();
224 emit m_extension
->openUrlRequest(KUrl(url
));
227 void DolphinPart::slotSelectionChanged(const KFileItemList
& selection
)
229 const bool hasSelection
= !selection
.isEmpty();
231 QAction
* renameAction
= actionCollection()->action("rename");
232 QAction
* moveToTrashAction
= actionCollection()->action("move_to_trash");
233 QAction
* deleteAction
= actionCollection()->action("delete");
234 QAction
* editMimeTypeAction
= actionCollection()->action("editMimeType");
235 QAction
* propertiesAction
= actionCollection()->action("properties");
236 QAction
* deleteWithTrashShortcut
= actionCollection()->action("delete_shortcut"); // see DolphinViewActionHandler
239 stateChanged("has_no_selection");
241 emit m_extension
->enableAction("cut", false);
242 emit m_extension
->enableAction("copy", false);
243 deleteWithTrashShortcut
->setEnabled(false);
244 editMimeTypeAction
->setEnabled(false);
246 stateChanged("has_selection");
248 // TODO share this code with DolphinMainWindow::updateEditActions (and the desktop code)
250 KFileItemListProperties
capabilities(selection
);
251 const bool enableMoveToTrash
= capabilities
.isLocal() && capabilities
.supportsMoving();
253 renameAction
->setEnabled(capabilities
.supportsMoving());
254 moveToTrashAction
->setEnabled(enableMoveToTrash
);
255 deleteAction
->setEnabled(capabilities
.supportsDeleting());
256 deleteWithTrashShortcut
->setEnabled(capabilities
.supportsDeleting() && !enableMoveToTrash
);
257 editMimeTypeAction
->setEnabled(true);
258 propertiesAction
->setEnabled(true);
259 emit m_extension
->enableAction("cut", capabilities
.supportsMoving());
260 emit m_extension
->enableAction("copy", true);
264 void DolphinPart::updatePasteAction()
266 QPair
<bool, QString
> pasteInfo
= m_view
->pasteInfo();
267 emit m_extension
->enableAction( "paste", pasteInfo
.first
);
268 emit m_extension
->setActionText( "paste", pasteInfo
.second
);
271 KAboutData
* DolphinPart::createAboutData()
273 return new KAboutData("dolphinpart", "dolphin", ki18nc("@title", "Dolphin Part"), "0.1");
276 bool DolphinPart::openUrl(const KUrl
& url
)
278 bool reload
= arguments().reload();
279 // A bit of a workaround so that changing the namefilter works: force reload.
280 // Otherwise DolphinView wouldn't relist the URL, so nothing would happen.
281 if (m_nameFilter
!= m_view
->nameFilter())
283 if (m_view
->url() == url
&& !reload
) { // DolphinView won't do anything in that case, so don't emit started
286 setUrl(url
); // remember it at the KParts level
287 KUrl
visibleUrl(url
);
288 if (!m_nameFilter
.isEmpty()) {
289 visibleUrl
.addPath(m_nameFilter
);
291 QString prettyUrl
= visibleUrl
.pathOrUrl();
292 emit
setWindowCaption(prettyUrl
);
293 emit m_extension
->setLocationBarUrl(prettyUrl
);
294 emit
started(0); // get the wheel to spin
295 m_view
->setNameFilter(m_nameFilter
);
298 emit
aboutToOpenURL();
304 void DolphinPart::slotCompleted(const KUrl
& url
)
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 KFileItem
& _item
,
357 const QList
<QAction
*>& customActions
)
359 KParts::BrowserExtension::PopupFlags popupFlags
= KParts::BrowserExtension::DefaultPopupItems
360 | KParts::BrowserExtension::ShowProperties
361 | KParts::BrowserExtension::ShowUrlOperations
;
363 KFileItem
item(_item
);
365 if (item
.isNull()) { // viewport context menu
366 popupFlags
|= KParts::BrowserExtension::ShowNavigationItems
| KParts::BrowserExtension::ShowUp
;
367 item
= m_view
->rootItem();
369 item
= KFileItem( S_IFDIR
, (mode_t
)-1, url() );
371 item
.setUrl(url()); // ensure we use the view url, not the canonical path (#213799)
374 // TODO: We should change the signature of the slots (and signals) for being able
375 // to tell for which items we want a popup.
377 if (m_view
->selectedItems().isEmpty()) {
380 items
= m_view
->selectedItems();
383 KFileItemListProperties
capabilities(items
);
385 KParts::BrowserExtension::ActionGroupMap actionGroups
;
386 QList
<QAction
*> editActions
;
387 editActions
+= m_view
->versionControlActions(m_view
->selectedItems());
388 editActions
+= customActions
;
390 if (!_item
.isNull()) { // only for context menu on one or more items
391 bool supportsDeleting
= capabilities
.supportsDeleting();
392 bool supportsMoving
= capabilities
.supportsMoving();
394 if (!supportsDeleting
) {
395 popupFlags
|= KParts::BrowserExtension::NoDeletion
;
398 if (supportsMoving
) {
399 editActions
.append(actionCollection()->action("rename"));
402 bool addTrash
= capabilities
.isLocal() && supportsMoving
;
404 if (supportsDeleting
) {
405 if ( !item
.isLocalFile() )
407 else if (QApplication::keyboardModifiers() & Qt::ShiftModifier
) {
412 KSharedConfig::Ptr globalConfig
= KSharedConfig::openConfig("kdeglobals", KConfig::IncludeGlobals
);
413 KConfigGroup
configGroup(globalConfig
, "KDE");
414 addDel
= configGroup
.readEntry("ShowDeleteCommand", false);
419 editActions
.append(actionCollection()->action("move_to_trash"));
421 editActions
.append(actionCollection()->action("delete"));
423 // Normally KonqPopupMenu only shows the "Create new" submenu in the current view
424 // since otherwise the created file would not be visible.
425 // But in treeview mode we should allow it.
426 if (m_view
->itemsExpandable())
427 popupFlags
|= KParts::BrowserExtension::ShowCreateDirectory
;
431 actionGroups
.insert("editactions", editActions
);
433 emit m_extension
->popupMenu(QCursor::pos(),
435 KParts::OpenUrlArguments(),
436 KParts::BrowserArguments(),
441 void DolphinPart::slotRedirection(const KUrl
& oldUrl
, const KUrl
& newUrl
)
443 //kDebug() << oldUrl << newUrl << "currentUrl=" << url();
444 if (oldUrl
.equals(url(), KUrl::CompareWithoutTrailingSlash
/* #207572 */)) {
445 KParts::ReadOnlyPart::setUrl(newUrl
);
446 const QString prettyUrl
= newUrl
.pathOrUrl();
447 emit m_extension
->setLocationBarUrl(prettyUrl
);
452 void DolphinPart::slotEditMimeType()
454 const KFileItemList items
= m_view
->selectedItems();
455 if (!items
.isEmpty()) {
456 KonqOperations::editMimeType(items
.first().mimetype(), m_view
);
460 void DolphinPart::slotSelectItemsMatchingPattern()
462 openSelectionDialog(i18nc("@title:window", "Select"),
463 i18n("Select all items matching this pattern:"),
467 void DolphinPart::slotUnselectItemsMatchingPattern()
469 openSelectionDialog(i18nc("@title:window", "Unselect"),
470 i18n("Unselect all items matching this pattern:"),
474 void DolphinPart::openSelectionDialog(const QString
& title
, const QString
& text
, bool selectItems
)
477 QString pattern
= KInputDialog::getText(title
, text
, "*", &okClicked
, m_view
);
479 if (okClicked
&& !pattern
.isEmpty()) {
480 QRegExp
patternRegExp(pattern
, Qt::CaseSensitive
, QRegExp::Wildcard
);
481 m_view
->setItemSelectionEnabled(patternRegExp
, selectItems
);
485 void DolphinPart::setCurrentViewMode(const QString
& viewModeName
)
487 QAction
* action
= actionCollection()->action(viewModeName
);
492 QString
DolphinPart::currentViewMode() const
494 return m_actionHandler
->currentViewModeActionName();
497 void DolphinPart::setNameFilter(const QString
& nameFilter
)
499 // This is the "/home/dfaure/*.diff" kind of name filter (KDirLister::setNameFilter)
500 // which is unrelated to DolphinView::setNameFilter which is substring filtering in a proxy.
501 m_nameFilter
= nameFilter
;
502 // TODO save/restore name filter in saveState/restoreState like KonqDirPart did in kde3?
505 void DolphinPart::slotOpenTerminal()
507 QString
dir(QDir::homePath());
511 // If the given directory is not local, it can still be the URL of an
512 // ioslave using UDS_LOCAL_PATH which to be converted first.
513 u
= KIO::NetAccess::mostLocalUrl(u
, widget());
515 //If the URL is local after the above conversion, set the directory.
516 if (u
.isLocalFile()) {
517 dir
= u
.toLocalFile();
520 KToolInvocation::invokeTerminal(QString(), dir
);
523 void DolphinPart::slotFindFile()
525 KRun::run("kfind", url(), widget());
528 void DolphinPart::updateNewMenu()
530 // As requested by KNewFileMenu :
531 m_newFileMenu
->checkUpToDate();
532 m_newFileMenu
->setViewShowsHiddenFiles(m_view
->hiddenFilesShown());
533 // And set the files that the menu apply on :
534 m_newFileMenu
->setPopupFiles(url());
537 void DolphinPart::updateStatusBar()
539 emit
ReadOnlyPart::setStatusBarText(m_view
->statusBarText());
542 void DolphinPart::updateProgress(int percent
)
544 m_extension
->loadingProgress(percent
);
547 void DolphinPart::createDirectory()
549 m_newFileMenu
->setViewShowsHiddenFiles(m_view
->hiddenFilesShown());
550 m_newFileMenu
->setPopupFiles(url());
551 m_newFileMenu
->createDirectory();
554 void DolphinPart::setFilesToSelect(const KUrl::List
& files
)
556 m_view
->markUrlsAsSelected(files
);
561 void DolphinPartBrowserExtension::restoreState(QDataStream
&stream
)
563 KParts::BrowserExtension::restoreState(stream
);
564 m_part
->view()->restoreState(stream
);
567 void DolphinPartBrowserExtension::saveState(QDataStream
&stream
)
569 KParts::BrowserExtension::saveState(stream
);
570 m_part
->view()->saveState(stream
);
573 void DolphinPartBrowserExtension::cut()
575 m_part
->view()->cutSelectedItems();
578 void DolphinPartBrowserExtension::copy()
580 m_part
->view()->copySelectedItems();
583 void DolphinPartBrowserExtension::paste()
585 m_part
->view()->paste();
588 void DolphinPartBrowserExtension::pasteTo(const KUrl
&)
590 m_part
->view()->pasteIntoFolder();
593 void DolphinPartBrowserExtension::reparseConfiguration()
595 m_part
->view()->refresh();
600 DolphinPartFileInfoExtension::DolphinPartFileInfoExtension(DolphinPart
* part
)
601 : KParts::FileInfoExtension(part
)
605 DolphinPart
* DolphinPartFileInfoExtension::part() const
607 return static_cast<DolphinPart
*>(parent());
610 bool DolphinPartFileInfoExtension::hasSelection() const
612 return part()->view()->hasSelection();
615 KParts::FileInfoExtension::QueryModes
DolphinPartFileInfoExtension::supportedQueryModes() const
617 return (KParts::FileInfoExtension::AllItems
| KParts::FileInfoExtension::SelectedItems
);
620 KFileItemList
DolphinPartFileInfoExtension::queryFor(KParts::FileInfoExtension::QueryMode mode
) const
624 if (mode
== KParts::FileInfoExtension::None
)
627 if (!(supportedQueryModes() & mode
))
631 case KParts::FileInfoExtension::SelectedItems
:
633 return part()->view()->selectedItems();
635 case KParts::FileInfoExtension::AllItems
:
636 return part()->view()->items();
644 #include "dolphinpart.moc"