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
)
61 setComponentData(DolphinPartFactory::componentData(), false);
62 m_extension
= new DolphinPartBrowserExtension(this);
63 new DolphinPartFileInfoExtension(this);
65 // make sure that other apps using this part find Dolphin's view-file-columns icons
66 KIconLoader::global()->addAppDir("dolphin");
68 m_view
= new DolphinView(KUrl(), parentWidget
);
69 m_view
->setTabsForFilesEnabled(true);
72 connect(m_view
, SIGNAL(finishedDirLoading(KUrl
)), this, SLOT(slotCompleted(KUrl
)));
73 connect(m_view
, SIGNAL(directoryLoadingProgress(int)), this, SLOT(updateProgress(int)));
74 connect(m_view
, SIGNAL(errorMessage(QString
)), this, SLOT(slotErrorMessage(QString
)));
76 setXMLFile("dolphinpart.rc");
78 connect(m_view
, SIGNAL(infoMessage(QString
)),
79 this, SLOT(slotMessage(QString
)));
80 connect(m_view
, SIGNAL(operationCompletedMessage(QString
)),
81 this, SLOT(slotMessage(QString
)));
82 connect(m_view
, SIGNAL(errorMessage(QString
)),
83 this, SLOT(slotErrorMessage(QString
)));
84 connect(m_view
, SIGNAL(itemActivated(KFileItem
)),
85 this, SLOT(slotItemActivated(KFileItem
)));
86 connect(m_view
, SIGNAL(tabRequested(KUrl
)),
87 this, SLOT(createNewWindow(KUrl
)));
88 connect(m_view
, SIGNAL(requestContextMenu(QPoint
,KFileItem
,KUrl
,QList
<QAction
*>)),
89 this, SLOT(slotOpenContextMenu(QPoint
,KFileItem
,KUrl
,QList
<QAction
*>)));
90 connect(m_view
, SIGNAL(selectionChanged(KFileItemList
)),
91 m_extension
, SIGNAL(selectionInfo(KFileItemList
)));
92 connect(m_view
, SIGNAL(selectionChanged(KFileItemList
)),
93 this, SLOT(slotSelectionChanged(KFileItemList
)));
94 connect(m_view
, SIGNAL(requestItemInfo(KFileItem
)),
95 this, SLOT(slotRequestItemInfo(KFileItem
)));
96 connect(m_view
, SIGNAL(modeChanged(DolphinView::Mode
,DolphinView::Mode
)),
97 this, SIGNAL(viewModeChanged())); // relay signal
98 connect(m_view
, SIGNAL(redirection(KUrl
,KUrl
)),
99 this, SLOT(slotDirectoryRedirection(KUrl
,KUrl
)));
101 // Watch for changes that should result in updates to the
103 connect(m_view
, SIGNAL(itemCountChanged()), this, SLOT(updateStatusBar()));
104 connect(m_view
, SIGNAL(selectionChanged(KFileItemList
)), this, SLOT(updateStatusBar()));
106 m_actionHandler
= new DolphinViewActionHandler(actionCollection(), this);
107 m_actionHandler
->setCurrentView(m_view
);
108 connect(m_actionHandler
, SIGNAL(createDirectory()), SLOT(createDirectory()));
110 m_remoteEncoding
= new DolphinRemoteEncoding(this, m_actionHandler
);
111 connect(this, SIGNAL(aboutToOpenURL()),
112 m_remoteEncoding
, SLOT(slotAboutToOpenUrl()));
114 QClipboard
* clipboard
= QApplication::clipboard();
115 connect(clipboard
, SIGNAL(dataChanged()),
116 this, SLOT(updatePasteAction()));
119 m_actionHandler
->updateViewActions();
120 slotSelectionChanged(KFileItemList()); // initially disable selection-dependent actions
122 // TODO there was a "always open a new window" (when clicking on a directory) setting in konqueror
123 // (sort of spacial navigation)
125 loadPlugins(this, this, componentData());
129 DolphinPart::~DolphinPart()
131 DolphinNewFileMenuObserver::instance().detach(m_newFileMenu
);
134 void DolphinPart::createActions()
138 m_newFileMenu
= new KNewFileMenu(actionCollection(), "new_menu", this);
139 m_newFileMenu
->setParentWidget(widget());
140 DolphinNewFileMenuObserver::instance().attach(m_newFileMenu
);
141 connect(m_newFileMenu
->menu(), SIGNAL(aboutToShow()),
142 this, SLOT(updateNewMenu()));
144 KAction
*editMimeTypeAction
= actionCollection()->addAction( "editMimeType" );
145 editMimeTypeAction
->setText( i18nc("@action:inmenu Edit", "&Edit File Type..." ) );
146 connect(editMimeTypeAction
, SIGNAL(triggered()), SLOT(slotEditMimeType()));
148 KAction
* selectItemsMatching
= actionCollection()->addAction("select_items_matching");
149 selectItemsMatching
->setText(i18nc("@action:inmenu Edit", "Select Items Matching..."));
150 selectItemsMatching
->setShortcut(Qt::CTRL
| Qt::Key_S
);
151 connect(selectItemsMatching
, SIGNAL(triggered()), this, SLOT(slotSelectItemsMatchingPattern()));
153 KAction
* unselectItemsMatching
= actionCollection()->addAction("unselect_items_matching");
154 unselectItemsMatching
->setText(i18nc("@action:inmenu Edit", "Unselect Items Matching..."));
155 connect(unselectItemsMatching
, SIGNAL(triggered()), this, SLOT(slotUnselectItemsMatchingPattern()));
157 actionCollection()->addAction(KStandardAction::SelectAll
, "select_all", m_view
, SLOT(selectAll()));
159 KAction
* unselectAll
= actionCollection()->addAction("unselect_all");
160 unselectAll
->setText(i18nc("@action:inmenu Edit", "Unselect All"));
161 connect(unselectAll
, SIGNAL(triggered()), m_view
, SLOT(clearSelection()));
163 KAction
* invertSelection
= actionCollection()->addAction("invert_selection");
164 invertSelection
->setText(i18nc("@action:inmenu Edit", "Invert Selection"));
165 invertSelection
->setShortcut(Qt::CTRL
| Qt::SHIFT
| Qt::Key_A
);
166 connect(invertSelection
, SIGNAL(triggered()), m_view
, SLOT(invertSelection()));
168 // View menu: all done by DolphinViewActionHandler
172 QActionGroup
* goActionGroup
= new QActionGroup(this);
173 connect(goActionGroup
, SIGNAL(triggered(QAction
*)),
174 this, SLOT(slotGoTriggered(QAction
*)));
176 createGoAction("go_applications", "start-here-kde",
177 i18nc("@action:inmenu Go", "App&lications"), QString("programs:/"),
179 createGoAction("go_network_folders", "folder-remote",
180 i18nc("@action:inmenu Go", "&Network Folders"), QString("remote:/"),
182 createGoAction("go_settings", "preferences-system",
183 i18nc("@action:inmenu Go", "Sett&ings"), QString("settings:/"),
185 createGoAction("go_trash", "user-trash",
186 i18nc("@action:inmenu Go", "Trash"), QString("trash:/"),
188 createGoAction("go_autostart", "",
189 i18nc("@action:inmenu Go", "Autostart"), KGlobalSettings::autostartPath(),
193 KAction
* findFile
= actionCollection()->addAction("find_file");
194 findFile
->setText(i18nc("@action:inmenu Tools", "Find File..."));
195 findFile
->setShortcut(Qt::CTRL
| Qt::Key_F
);
196 findFile
->setIcon(KIcon("edit-find"));
197 connect(findFile
, SIGNAL(triggered()), this, SLOT(slotFindFile()));
199 if (KAuthorized::authorizeKAction("shell_access")) {
200 KAction
* action
= actionCollection()->addAction("open_terminal");
201 action
->setIcon(KIcon("utilities-terminal"));
202 action
->setText(i18nc("@action:inmenu Tools", "Open &Terminal"));
203 connect(action
, SIGNAL(triggered()), SLOT(slotOpenTerminal()));
204 action
->setShortcut(Qt::Key_F4
);
208 void DolphinPart::createGoAction(const char* name
, const char* iconName
,
209 const QString
& text
, const QString
& url
,
210 QActionGroup
* actionGroup
)
212 KAction
* action
= actionCollection()->addAction(name
);
213 action
->setIcon(KIcon(iconName
));
214 action
->setText(text
);
215 action
->setData(url
);
216 action
->setActionGroup(actionGroup
);
219 void DolphinPart::slotGoTriggered(QAction
* action
)
221 const QString url
= action
->data().toString();
222 emit m_extension
->openUrlRequest(KUrl(url
));
225 void DolphinPart::slotSelectionChanged(const KFileItemList
& selection
)
227 const bool hasSelection
= !selection
.isEmpty();
229 QAction
* renameAction
= actionCollection()->action("rename");
230 QAction
* moveToTrashAction
= actionCollection()->action("move_to_trash");
231 QAction
* deleteAction
= actionCollection()->action("delete");
232 QAction
* editMimeTypeAction
= actionCollection()->action("editMimeType");
233 QAction
* propertiesAction
= actionCollection()->action("properties");
234 QAction
* deleteWithTrashShortcut
= actionCollection()->action("delete_shortcut"); // see DolphinViewActionHandler
237 stateChanged("has_no_selection");
239 emit m_extension
->enableAction("cut", false);
240 emit m_extension
->enableAction("copy", false);
241 deleteWithTrashShortcut
->setEnabled(false);
242 editMimeTypeAction
->setEnabled(false);
244 stateChanged("has_selection");
246 // TODO share this code with DolphinMainWindow::updateEditActions (and the desktop code)
248 KFileItemListProperties
capabilities(selection
);
249 const bool enableMoveToTrash
= capabilities
.isLocal() && capabilities
.supportsMoving();
251 renameAction
->setEnabled(capabilities
.supportsMoving());
252 moveToTrashAction
->setEnabled(enableMoveToTrash
);
253 deleteAction
->setEnabled(capabilities
.supportsDeleting());
254 deleteWithTrashShortcut
->setEnabled(capabilities
.supportsDeleting() && !enableMoveToTrash
);
255 editMimeTypeAction
->setEnabled(true);
256 propertiesAction
->setEnabled(true);
257 emit m_extension
->enableAction("cut", capabilities
.supportsMoving());
258 emit m_extension
->enableAction("copy", true);
262 void DolphinPart::updatePasteAction()
264 QPair
<bool, QString
> pasteInfo
= m_view
->pasteInfo();
265 emit m_extension
->enableAction( "paste", pasteInfo
.first
);
266 emit m_extension
->setActionText( "paste", pasteInfo
.second
);
269 KAboutData
* DolphinPart::createAboutData()
271 return new KAboutData("dolphinpart", "dolphin", ki18nc("@title", "Dolphin Part"), "0.1");
274 bool DolphinPart::openUrl(const KUrl
& url
)
276 bool reload
= arguments().reload();
277 // A bit of a workaround so that changing the namefilter works: force reload.
278 // Otherwise DolphinView wouldn't relist the URL, so nothing would happen.
279 if (m_nameFilter
!= m_view
->nameFilter())
281 if (m_view
->url() == url
&& !reload
) { // DolphinView won't do anything in that case, so don't emit started
284 setUrl(url
); // remember it at the KParts level
285 KUrl
visibleUrl(url
);
286 if (!m_nameFilter
.isEmpty()) {
287 visibleUrl
.addPath(m_nameFilter
);
289 QString prettyUrl
= visibleUrl
.pathOrUrl();
290 emit
setWindowCaption(prettyUrl
);
291 emit m_extension
->setLocationBarUrl(prettyUrl
);
292 emit
started(0); // get the wheel to spin
293 m_view
->setNameFilter(m_nameFilter
);
296 emit
aboutToOpenURL();
302 void DolphinPart::slotCompleted(const KUrl
& url
)
308 void DolphinPart::slotMessage(const QString
& msg
)
310 emit
setStatusBarText(msg
);
313 void DolphinPart::slotErrorMessage(const QString
& msg
)
317 //KMessageBox::error(m_view, msg);
320 void DolphinPart::slotRequestItemInfo(const KFileItem
& item
)
322 emit m_extension
->mouseOverInfo(item
);
326 ReadOnlyPart::setStatusBarText(item
.getStatusBarInfo());
330 void DolphinPart::slotItemActivated(const KFileItem
& item
)
332 KParts::OpenUrlArguments args
;
333 // Forget about the known mimetype if a target URL is used.
334 // Testcase: network:/ with a item (mimetype "inode/some-foo-service") pointing to a http URL (html)
335 if (item
.targetUrl() == item
.url()) {
336 args
.setMimeType(item
.mimetype());
339 // Ideally, konqueror should be changed to not require trustedSource for directory views,
340 // since the idea was not to need BrowserArguments for non-browser stuff...
341 KParts::BrowserArguments browserArgs
;
342 browserArgs
.trustedSource
= true;
343 emit m_extension
->openUrlRequest(item
.targetUrl(), args
, browserArgs
);
346 void DolphinPart::createNewWindow(const KUrl
& url
)
348 // TODO: Check issue N176832 for the missing QAIV signal; task 177399 - maybe this code
349 // should be moved into DolphinPart::slotItemActivated()
350 emit m_extension
->createNewWindow(url
);
353 void DolphinPart::slotOpenContextMenu(const QPoint
& pos
,
354 const KFileItem
& _item
,
356 const QList
<QAction
*>& customActions
)
358 KParts::BrowserExtension::PopupFlags popupFlags
= KParts::BrowserExtension::DefaultPopupItems
359 | KParts::BrowserExtension::ShowProperties
360 | KParts::BrowserExtension::ShowUrlOperations
;
362 KFileItem
item(_item
);
364 if (item
.isNull()) { // viewport context menu
365 popupFlags
|= KParts::BrowserExtension::ShowNavigationItems
| KParts::BrowserExtension::ShowUp
;
366 item
= m_view
->rootItem();
368 item
= KFileItem( S_IFDIR
, (mode_t
)-1, url() );
370 item
.setUrl(url()); // ensure we use the view url, not the canonical path (#213799)
373 // TODO: We should change the signature of the slots (and signals) for being able
374 // to tell for which items we want a popup.
376 if (m_view
->selectedItems().isEmpty()) {
379 items
= m_view
->selectedItems();
382 KFileItemListProperties
capabilities(items
);
384 KParts::BrowserExtension::ActionGroupMap actionGroups
;
385 QList
<QAction
*> editActions
;
386 editActions
+= m_view
->versionControlActions(m_view
->selectedItems());
387 editActions
+= customActions
;
389 if (!_item
.isNull()) { // only for context menu on one or more items
390 bool supportsDeleting
= capabilities
.supportsDeleting();
391 bool supportsMoving
= capabilities
.supportsMoving();
393 if (!supportsDeleting
) {
394 popupFlags
|= KParts::BrowserExtension::NoDeletion
;
397 if (supportsMoving
) {
398 editActions
.append(actionCollection()->action("rename"));
401 bool addTrash
= capabilities
.isLocal() && supportsMoving
;
403 if (supportsDeleting
) {
404 if ( !item
.isLocalFile() )
406 else if (QApplication::keyboardModifiers() & Qt::ShiftModifier
) {
411 KSharedConfig::Ptr globalConfig
= KSharedConfig::openConfig("kdeglobals", KConfig::IncludeGlobals
);
412 KConfigGroup
configGroup(globalConfig
, "KDE");
413 addDel
= configGroup
.readEntry("ShowDeleteCommand", false);
418 editActions
.append(actionCollection()->action("move_to_trash"));
420 editActions
.append(actionCollection()->action("delete"));
422 // Normally KonqPopupMenu only shows the "Create new" submenu in the current view
423 // since otherwise the created file would not be visible.
424 // But in treeview mode we should allow it.
425 if (m_view
->itemsExpandable())
426 popupFlags
|= KParts::BrowserExtension::ShowCreateDirectory
;
430 actionGroups
.insert("editactions", editActions
);
432 emit m_extension
->popupMenu(pos
,
434 KParts::OpenUrlArguments(),
435 KParts::BrowserArguments(),
440 void DolphinPart::slotDirectoryRedirection(const KUrl
& oldUrl
, const KUrl
& newUrl
)
442 //kDebug() << oldUrl << newUrl << "currentUrl=" << url();
443 if (oldUrl
.equals(url(), KUrl::CompareWithoutTrailingSlash
/* #207572 */)) {
444 KParts::ReadOnlyPart::setUrl(newUrl
);
445 const QString prettyUrl
= newUrl
.pathOrUrl();
446 emit m_extension
->setLocationBarUrl(prettyUrl
);
451 void DolphinPart::slotEditMimeType()
453 const KFileItemList items
= m_view
->selectedItems();
454 if (!items
.isEmpty()) {
455 KonqOperations::editMimeType(items
.first().mimetype(), m_view
);
459 void DolphinPart::slotSelectItemsMatchingPattern()
461 openSelectionDialog(i18nc("@title:window", "Select"),
462 i18n("Select all items matching this pattern:"),
466 void DolphinPart::slotUnselectItemsMatchingPattern()
468 openSelectionDialog(i18nc("@title:window", "Unselect"),
469 i18n("Unselect all items matching this pattern:"),
473 void DolphinPart::openSelectionDialog(const QString
& title
, const QString
& text
, bool selectItems
)
476 QString pattern
= KInputDialog::getText(title
, text
, "*", &okClicked
, m_view
);
478 if (okClicked
&& !pattern
.isEmpty()) {
479 QRegExp
patternRegExp(pattern
, Qt::CaseSensitive
, QRegExp::Wildcard
);
480 m_view
->selectItems(patternRegExp
, selectItems
);
484 void DolphinPart::setCurrentViewMode(const QString
& viewModeName
)
486 QAction
* action
= actionCollection()->action(viewModeName
);
491 QString
DolphinPart::currentViewMode() const
493 return m_actionHandler
->currentViewModeActionName();
496 void DolphinPart::setNameFilter(const QString
& nameFilter
)
498 // This is the "/home/dfaure/*.diff" kind of name filter (KDirLister::setNameFilter)
499 // which is unrelated to DolphinView::setNameFilter which is substring filtering in a proxy.
500 m_nameFilter
= nameFilter
;
501 // TODO save/restore name filter in saveState/restoreState like KonqDirPart did in kde3?
504 void DolphinPart::slotOpenTerminal()
506 QString
dir(QDir::homePath());
510 // If the given directory is not local, it can still be the URL of an
511 // ioslave using UDS_LOCAL_PATH which to be converted first.
512 u
= KIO::NetAccess::mostLocalUrl(u
, widget());
514 //If the URL is local after the above conversion, set the directory.
515 if (u
.isLocalFile()) {
516 dir
= u
.toLocalFile();
519 KToolInvocation::invokeTerminal(QString(), dir
);
522 void DolphinPart::slotFindFile()
524 KRun::run("kfind", url(), widget());
527 void DolphinPart::updateNewMenu()
529 // As requested by KNewFileMenu :
530 m_newFileMenu
->checkUpToDate();
531 m_newFileMenu
->setViewShowsHiddenFiles(m_view
->hiddenFilesShown());
532 // And set the files that the menu apply on :
533 m_newFileMenu
->setPopupFiles(url());
536 void DolphinPart::updateStatusBar()
538 emit
ReadOnlyPart::setStatusBarText(m_view
->statusBarText());
541 void DolphinPart::updateProgress(int percent
)
543 m_extension
->loadingProgress(percent
);
546 void DolphinPart::createDirectory()
548 m_newFileMenu
->setViewShowsHiddenFiles(m_view
->hiddenFilesShown());
549 m_newFileMenu
->setPopupFiles(url());
550 m_newFileMenu
->createDirectory();
553 void DolphinPart::setFilesToSelect(const KUrl::List
& files
)
555 m_view
->markUrlsAsSelected(files
);
560 void DolphinPartBrowserExtension::restoreState(QDataStream
&stream
)
562 KParts::BrowserExtension::restoreState(stream
);
563 m_part
->view()->restoreState(stream
);
566 void DolphinPartBrowserExtension::saveState(QDataStream
&stream
)
568 KParts::BrowserExtension::saveState(stream
);
569 m_part
->view()->saveState(stream
);
572 void DolphinPartBrowserExtension::cut()
574 m_part
->view()->cutSelectedItems();
577 void DolphinPartBrowserExtension::copy()
579 m_part
->view()->copySelectedItems();
582 void DolphinPartBrowserExtension::paste()
584 m_part
->view()->paste();
587 void DolphinPartBrowserExtension::pasteTo(const KUrl
&)
589 m_part
->view()->pasteIntoFolder();
592 void DolphinPartBrowserExtension::reparseConfiguration()
594 m_part
->view()->readSettings();
599 DolphinPartFileInfoExtension::DolphinPartFileInfoExtension(DolphinPart
* part
)
600 : KParts::FileInfoExtension(part
)
604 DolphinPart
* DolphinPartFileInfoExtension::part() const
606 return static_cast<DolphinPart
*>(parent());
609 bool DolphinPartFileInfoExtension::hasSelection() const
611 return part()->view()->selectedItemsCount() > 0;
614 KParts::FileInfoExtension::QueryModes
DolphinPartFileInfoExtension::supportedQueryModes() const
616 return (KParts::FileInfoExtension::AllItems
| KParts::FileInfoExtension::SelectedItems
);
619 KFileItemList
DolphinPartFileInfoExtension::queryFor(KParts::FileInfoExtension::QueryMode mode
) const
623 if (mode
== KParts::FileInfoExtension::None
)
626 if (!(supportedQueryModes() & mode
))
630 case KParts::FileInfoExtension::SelectedItems
:
632 return part()->view()->selectedItems();
634 case KParts::FileInfoExtension::AllItems
:
635 return part()->view()->items();
643 #include "dolphinpart.moc"