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/dolphinsortfilterproxymodel.h"
48 #include "views/dolphinmodel.h"
49 #include "views/dolphinnewfilemenuobserver.h"
50 #include "views/dolphinremoteencoding.h"
51 #include "views/dolphindirlister.h"
53 #include <QActionGroup>
54 #include <QApplication>
57 K_PLUGIN_FACTORY(DolphinPartFactory
, registerPlugin
<DolphinPart
>();)
58 K_EXPORT_PLUGIN(DolphinPartFactory("dolphinpart", "dolphin"))
60 DolphinPart::DolphinPart(QWidget
* parentWidget
, QObject
* parent
, const QVariantList
& args
)
61 : KParts::ReadOnlyPart(parent
)
64 setComponentData(DolphinPartFactory::componentData(), false);
65 m_extension
= new DolphinPartBrowserExtension(this);
66 new DolphinPartFileInfoExtension(this);
68 // make sure that other apps using this part find Dolphin's view-file-columns icons
69 KIconLoader::global()->addAppDir("dolphin");
71 m_view
= new DolphinView(KUrl(), parentWidget
);
72 m_view
->setTabsForFilesEnabled(true);
75 connect(m_view
, SIGNAL(finishedPathLoading(KUrl
)), this, SLOT(slotCompleted(KUrl
)));
76 connect(m_view
, SIGNAL(pathLoadingProgress(int)), this, SLOT(updateProgress(int)));
77 connect(m_view
, SIGNAL(errorMessage(QString
)), this, SLOT(slotErrorMessage(QString
)));
79 setXMLFile("dolphinpart.rc");
81 connect(m_view
, SIGNAL(infoMessage(QString
)),
82 this, SLOT(slotMessage(QString
)));
83 connect(m_view
, SIGNAL(operationCompletedMessage(QString
)),
84 this, SLOT(slotMessage(QString
)));
85 connect(m_view
, SIGNAL(errorMessage(QString
)),
86 this, SLOT(slotErrorMessage(QString
)));
87 connect(m_view
, SIGNAL(itemTriggered(KFileItem
)),
88 this, SLOT(slotItemTriggered(KFileItem
)));
89 connect(m_view
, SIGNAL(tabRequested(KUrl
)),
90 this, SLOT(createNewWindow(KUrl
)));
91 connect(m_view
, SIGNAL(requestContextMenu(KFileItem
,KUrl
,QList
<QAction
*>)),
92 this, SLOT(slotOpenContextMenu(KFileItem
,KUrl
,QList
<QAction
*>)));
93 connect(m_view
, SIGNAL(selectionChanged(KFileItemList
)),
94 m_extension
, SIGNAL(selectionInfo(KFileItemList
)));
95 connect(m_view
, SIGNAL(selectionChanged(KFileItemList
)),
96 this, SLOT(slotSelectionChanged(KFileItemList
)));
97 connect(m_view
, SIGNAL(requestItemInfo(KFileItem
)),
98 this, SLOT(slotRequestItemInfo(KFileItem
)));
99 connect(m_view
, SIGNAL(modeChanged()),
100 this, SIGNAL(viewModeChanged())); // relay signal
101 connect(m_view
, SIGNAL(redirection(KUrl
, KUrl
)),
102 this, SLOT(slotRedirection(KUrl
, KUrl
)));
104 // Watch for changes that should result in updates to the
106 connect(m_view
, SIGNAL(itemCountChanged()), this, SLOT(updateStatusBar()));
107 connect(m_view
, SIGNAL(selectionChanged(const KFileItemList
)), this, SLOT(updateStatusBar()));
109 m_actionHandler
= new DolphinViewActionHandler(actionCollection(), this);
110 m_actionHandler
->setCurrentView(m_view
);
111 connect(m_actionHandler
, SIGNAL(createDirectory()), SLOT(createDirectory()));
113 m_remoteEncoding
= new DolphinRemoteEncoding(this, m_actionHandler
);
114 connect(this, SIGNAL(aboutToOpenURL()),
115 m_remoteEncoding
, SLOT(slotAboutToOpenUrl()));
117 QClipboard
* clipboard
= QApplication::clipboard();
118 connect(clipboard
, SIGNAL(dataChanged()),
119 this, SLOT(updatePasteAction()));
122 m_actionHandler
->updateViewActions();
123 slotSelectionChanged(KFileItemList()); // initially disable selection-dependent actions
125 // TODO there was a "always open a new window" (when clicking on a directory) setting in konqueror
126 // (sort of spacial navigation)
128 loadPlugins(this, this, componentData());
132 DolphinPart::~DolphinPart()
134 DolphinSettings::instance().save();
135 DolphinNewFileMenuObserver::instance().detach(m_newFileMenu
);
138 void DolphinPart::createActions()
142 m_newFileMenu
= new KNewFileMenu(actionCollection(), "new_menu", this);
143 m_newFileMenu
->setParentWidget(widget());
144 DolphinNewFileMenuObserver::instance().attach(m_newFileMenu
);
145 connect(m_newFileMenu
->menu(), SIGNAL(aboutToShow()),
146 this, SLOT(updateNewMenu()));
148 KAction
*editMimeTypeAction
= actionCollection()->addAction( "editMimeType" );
149 editMimeTypeAction
->setText( i18nc("@action:inmenu Edit", "&Edit File Type..." ) );
150 connect(editMimeTypeAction
, SIGNAL(triggered()), SLOT(slotEditMimeType()));
152 KAction
* selectItemsMatching
= actionCollection()->addAction("select_items_matching");
153 selectItemsMatching
->setText(i18nc("@action:inmenu Edit", "Select Items Matching..."));
154 selectItemsMatching
->setShortcut(Qt::CTRL
| Qt::Key_S
);
155 connect(selectItemsMatching
, SIGNAL(triggered()), this, SLOT(slotSelectItemsMatchingPattern()));
157 KAction
* unselectItemsMatching
= actionCollection()->addAction("unselect_items_matching");
158 unselectItemsMatching
->setText(i18nc("@action:inmenu Edit", "Unselect Items Matching..."));
159 connect(unselectItemsMatching
, SIGNAL(triggered()), this, SLOT(slotUnselectItemsMatchingPattern()));
161 actionCollection()->addAction(KStandardAction::SelectAll
, "select_all", m_view
, SLOT(selectAll()));
163 KAction
* unselectAll
= actionCollection()->addAction("unselect_all");
164 unselectAll
->setText(i18nc("@action:inmenu Edit", "Unselect All"));
165 connect(unselectAll
, SIGNAL(triggered()), m_view
, SLOT(clearSelection()));
167 KAction
* invertSelection
= actionCollection()->addAction("invert_selection");
168 invertSelection
->setText(i18nc("@action:inmenu Edit", "Invert Selection"));
169 invertSelection
->setShortcut(Qt::CTRL
| Qt::SHIFT
| Qt::Key_A
);
170 connect(invertSelection
, SIGNAL(triggered()), m_view
, SLOT(invertSelection()));
172 // View menu: all done by DolphinViewActionHandler
176 QActionGroup
* goActionGroup
= new QActionGroup(this);
177 connect(goActionGroup
, SIGNAL(triggered(QAction
*)),
178 this, SLOT(slotGoTriggered(QAction
*)));
180 createGoAction("go_applications", "start-here-kde",
181 i18nc("@action:inmenu Go", "App&lications"), QString("programs:/"),
183 createGoAction("go_network_folders", "folder-remote",
184 i18nc("@action:inmenu Go", "&Network Folders"), QString("remote:/"),
186 createGoAction("go_settings", "preferences-system",
187 i18nc("@action:inmenu Go", "Sett&ings"), QString("settings:/"),
189 createGoAction("go_trash", "user-trash",
190 i18nc("@action:inmenu Go", "Trash"), QString("trash:/"),
192 createGoAction("go_autostart", "",
193 i18nc("@action:inmenu Go", "Autostart"), KGlobalSettings::autostartPath(),
197 KAction
* findFile
= actionCollection()->addAction("find_file");
198 findFile
->setText(i18nc("@action:inmenu Tools", "Find File..."));
199 findFile
->setShortcut(Qt::CTRL
| Qt::Key_F
);
200 findFile
->setIcon(KIcon("edit-find"));
201 connect(findFile
, SIGNAL(triggered()), this, SLOT(slotFindFile()));
203 if (KAuthorized::authorizeKAction("shell_access")) {
204 KAction
* action
= actionCollection()->addAction("open_terminal");
205 action
->setIcon(KIcon("utilities-terminal"));
206 action
->setText(i18nc("@action:inmenu Tools", "Open &Terminal"));
207 connect(action
, SIGNAL(triggered()), SLOT(slotOpenTerminal()));
208 action
->setShortcut(Qt::Key_F4
);
212 void DolphinPart::createGoAction(const char* name
, const char* iconName
,
213 const QString
& text
, const QString
& url
,
214 QActionGroup
* actionGroup
)
216 KAction
* action
= actionCollection()->addAction(name
);
217 action
->setIcon(KIcon(iconName
));
218 action
->setText(text
);
219 action
->setData(url
);
220 action
->setActionGroup(actionGroup
);
223 void DolphinPart::slotGoTriggered(QAction
* action
)
225 const QString url
= action
->data().toString();
226 emit m_extension
->openUrlRequest(KUrl(url
));
229 void DolphinPart::slotSelectionChanged(const KFileItemList
& selection
)
231 const bool hasSelection
= !selection
.isEmpty();
233 QAction
* renameAction
= actionCollection()->action("rename");
234 QAction
* moveToTrashAction
= actionCollection()->action("move_to_trash");
235 QAction
* deleteAction
= actionCollection()->action("delete");
236 QAction
* editMimeTypeAction
= actionCollection()->action("editMimeType");
237 QAction
* propertiesAction
= actionCollection()->action("properties");
238 QAction
* deleteWithTrashShortcut
= actionCollection()->action("delete_shortcut"); // see DolphinViewActionHandler
241 stateChanged("has_no_selection");
243 emit m_extension
->enableAction("cut", false);
244 emit m_extension
->enableAction("copy", false);
245 deleteWithTrashShortcut
->setEnabled(false);
246 editMimeTypeAction
->setEnabled(false);
248 stateChanged("has_selection");
250 // TODO share this code with DolphinMainWindow::updateEditActions (and the desktop code)
252 KFileItemListProperties
capabilities(selection
);
253 const bool enableMoveToTrash
= capabilities
.isLocal() && capabilities
.supportsMoving();
255 renameAction
->setEnabled(capabilities
.supportsMoving());
256 moveToTrashAction
->setEnabled(enableMoveToTrash
);
257 deleteAction
->setEnabled(capabilities
.supportsDeleting());
258 deleteWithTrashShortcut
->setEnabled(capabilities
.supportsDeleting() && !enableMoveToTrash
);
259 editMimeTypeAction
->setEnabled(true);
260 propertiesAction
->setEnabled(true);
261 emit m_extension
->enableAction("cut", capabilities
.supportsMoving());
262 emit m_extension
->enableAction("copy", true);
266 void DolphinPart::updatePasteAction()
268 QPair
<bool, QString
> pasteInfo
= m_view
->pasteInfo();
269 emit m_extension
->enableAction( "paste", pasteInfo
.first
);
270 emit m_extension
->setActionText( "paste", pasteInfo
.second
);
273 KAboutData
* DolphinPart::createAboutData()
275 return new KAboutData("dolphinpart", "dolphin", ki18nc("@title", "Dolphin Part"), "0.1");
278 bool DolphinPart::openUrl(const KUrl
& url
)
280 bool reload
= arguments().reload();
281 // A bit of a workaround so that changing the namefilter works: force reload.
282 // Otherwise DolphinView wouldn't relist the URL, so nothing would happen.
283 if (m_nameFilter
!= m_view
->nameFilter())
285 if (m_view
->url() == url
&& !reload
) { // DolphinView won't do anything in that case, so don't emit started
288 setUrl(url
); // remember it at the KParts level
289 KUrl
visibleUrl(url
);
290 if (!m_nameFilter
.isEmpty()) {
291 visibleUrl
.addPath(m_nameFilter
);
293 QString prettyUrl
= visibleUrl
.pathOrUrl();
294 emit
setWindowCaption(prettyUrl
);
295 emit m_extension
->setLocationBarUrl(prettyUrl
);
296 emit
started(0); // get the wheel to spin
297 m_view
->setNameFilter(m_nameFilter
);
300 emit
aboutToOpenURL();
306 void DolphinPart::slotCompleted(const KUrl
& url
)
312 void DolphinPart::slotMessage(const QString
& msg
)
314 emit
setStatusBarText(msg
);
317 void DolphinPart::slotErrorMessage(const QString
& msg
)
321 //KMessageBox::error(m_view, msg);
324 void DolphinPart::slotRequestItemInfo(const KFileItem
& item
)
326 emit m_extension
->mouseOverInfo(item
);
330 ReadOnlyPart::setStatusBarText(item
.getStatusBarInfo());
334 void DolphinPart::slotItemTriggered(const KFileItem
& item
)
336 KParts::OpenUrlArguments args
;
337 // Forget about the known mimetype if a target URL is used.
338 // Testcase: network:/ with a item (mimetype "inode/some-foo-service") pointing to a http URL (html)
339 if (item
.targetUrl() == item
.url()) {
340 args
.setMimeType(item
.mimetype());
343 // Ideally, konqueror should be changed to not require trustedSource for directory views,
344 // since the idea was not to need BrowserArguments for non-browser stuff...
345 KParts::BrowserArguments browserArgs
;
346 browserArgs
.trustedSource
= true;
347 emit m_extension
->openUrlRequest(item
.targetUrl(), args
, browserArgs
);
350 void DolphinPart::createNewWindow(const KUrl
& url
)
352 // TODO: Check issue N176832 for the missing QAIV signal; task 177399 - maybe this code
353 // should be moved into DolphinPart::slotItemTriggered()
354 emit m_extension
->createNewWindow(url
);
357 void DolphinPart::slotOpenContextMenu(const KFileItem
& _item
,
359 const QList
<QAction
*>& customActions
)
361 KParts::BrowserExtension::PopupFlags popupFlags
= KParts::BrowserExtension::DefaultPopupItems
362 | KParts::BrowserExtension::ShowProperties
363 | KParts::BrowserExtension::ShowUrlOperations
;
365 KFileItem
item(_item
);
367 if (item
.isNull()) { // viewport context menu
368 popupFlags
|= KParts::BrowserExtension::ShowNavigationItems
| KParts::BrowserExtension::ShowUp
;
369 item
= m_view
->rootItem();
371 item
= KFileItem( S_IFDIR
, (mode_t
)-1, url() );
373 item
.setUrl(url()); // ensure we use the view url, not the canonical path (#213799)
376 // TODO: We should change the signature of the slots (and signals) for being able
377 // to tell for which items we want a popup.
379 if (m_view
->selectedItems().isEmpty()) {
382 items
= m_view
->selectedItems();
385 KFileItemListProperties
capabilities(items
);
387 KParts::BrowserExtension::ActionGroupMap actionGroups
;
388 QList
<QAction
*> editActions
;
389 editActions
+= m_view
->versionControlActions(m_view
->selectedItems());
390 editActions
+= customActions
;
392 if (!_item
.isNull()) { // only for context menu on one or more items
393 bool supportsDeleting
= capabilities
.supportsDeleting();
394 bool supportsMoving
= capabilities
.supportsMoving();
396 if (!supportsDeleting
) {
397 popupFlags
|= KParts::BrowserExtension::NoDeletion
;
400 if (supportsMoving
) {
401 editActions
.append(actionCollection()->action("rename"));
404 bool addTrash
= capabilities
.isLocal() && supportsMoving
;
406 if (supportsDeleting
) {
407 if ( !item
.isLocalFile() )
409 else if (QApplication::keyboardModifiers() & Qt::ShiftModifier
) {
414 KSharedConfig::Ptr globalConfig
= KSharedConfig::openConfig("kdeglobals", KConfig::IncludeGlobals
);
415 KConfigGroup
configGroup(globalConfig
, "KDE");
416 addDel
= configGroup
.readEntry("ShowDeleteCommand", false);
421 editActions
.append(actionCollection()->action("move_to_trash"));
423 editActions
.append(actionCollection()->action("delete"));
425 // Normally KonqPopupMenu only shows the "Create new" submenu in the current view
426 // since otherwise the created file would not be visible.
427 // But in treeview mode we should allow it.
428 if (m_view
->itemsExpandable())
429 popupFlags
|= KParts::BrowserExtension::ShowCreateDirectory
;
433 actionGroups
.insert("editactions", editActions
);
435 emit m_extension
->popupMenu(QCursor::pos(),
437 KParts::OpenUrlArguments(),
438 KParts::BrowserArguments(),
443 void DolphinPart::slotRedirection(const KUrl
& oldUrl
, const KUrl
& newUrl
)
445 //kDebug() << oldUrl << newUrl << "currentUrl=" << url();
446 if (oldUrl
.equals(url(), KUrl::CompareWithoutTrailingSlash
/* #207572 */)) {
447 KParts::ReadOnlyPart::setUrl(newUrl
);
448 const QString prettyUrl
= newUrl
.pathOrUrl();
449 emit m_extension
->setLocationBarUrl(prettyUrl
);
454 void DolphinPart::slotEditMimeType()
456 const KFileItemList items
= m_view
->selectedItems();
457 if (!items
.isEmpty()) {
458 KonqOperations::editMimeType(items
.first().mimetype(), m_view
);
462 void DolphinPart::slotSelectItemsMatchingPattern()
464 openSelectionDialog(i18nc("@title:window", "Select"),
465 i18n("Select all items matching this pattern:"),
469 void DolphinPart::slotUnselectItemsMatchingPattern()
471 openSelectionDialog(i18nc("@title:window", "Unselect"),
472 i18n("Unselect all items matching this pattern:"),
476 void DolphinPart::openSelectionDialog(const QString
& title
, const QString
& text
, bool selectItems
)
479 QString pattern
= KInputDialog::getText(title
, text
, "*", &okClicked
, m_view
);
481 if (okClicked
&& !pattern
.isEmpty()) {
482 QRegExp
patternRegExp(pattern
, Qt::CaseSensitive
, QRegExp::Wildcard
);
483 m_view
->setItemSelectionEnabled(patternRegExp
, selectItems
);
487 void DolphinPart::setCurrentViewMode(const QString
& viewModeName
)
489 QAction
* action
= actionCollection()->action(viewModeName
);
494 QString
DolphinPart::currentViewMode() const
496 return m_actionHandler
->currentViewModeActionName();
499 void DolphinPart::setNameFilter(const QString
& nameFilter
)
501 // This is the "/home/dfaure/*.diff" kind of name filter (KDirLister::setNameFilter)
502 // which is unrelated to DolphinView::setNameFilter which is substring filtering in a proxy.
503 m_nameFilter
= nameFilter
;
504 // TODO save/restore name filter in saveState/restoreState like KonqDirPart did in kde3?
507 void DolphinPart::slotOpenTerminal()
509 QString
dir(QDir::homePath());
513 // If the given directory is not local, it can still be the URL of an
514 // ioslave using UDS_LOCAL_PATH which to be converted first.
515 u
= KIO::NetAccess::mostLocalUrl(u
, widget());
517 //If the URL is local after the above conversion, set the directory.
518 if (u
.isLocalFile()) {
519 dir
= u
.toLocalFile();
522 KToolInvocation::invokeTerminal(QString(), dir
);
525 void DolphinPart::slotFindFile()
527 KRun::run("kfind", url(), widget());
530 void DolphinPart::updateNewMenu()
532 // As requested by KNewFileMenu :
533 m_newFileMenu
->checkUpToDate();
534 m_newFileMenu
->setViewShowsHiddenFiles(m_view
->showHiddenFiles());
535 // And set the files that the menu apply on :
536 m_newFileMenu
->setPopupFiles(url());
539 void DolphinPart::updateStatusBar()
541 emit
ReadOnlyPart::setStatusBarText(m_view
->statusBarText());
544 void DolphinPart::updateProgress(int percent
)
546 m_extension
->loadingProgress(percent
);
549 void DolphinPart::createDirectory()
551 m_newFileMenu
->setViewShowsHiddenFiles(m_view
->showHiddenFiles());
552 m_newFileMenu
->setPopupFiles(url());
553 m_newFileMenu
->createDirectory();
556 void DolphinPart::setFilesToSelect(const KUrl::List
& files
)
558 m_view
->markUrlsAsSelected(files
);
563 void DolphinPartBrowserExtension::restoreState(QDataStream
&stream
)
565 KParts::BrowserExtension::restoreState(stream
);
566 m_part
->view()->restoreState(stream
);
569 void DolphinPartBrowserExtension::saveState(QDataStream
&stream
)
571 KParts::BrowserExtension::saveState(stream
);
572 m_part
->view()->saveState(stream
);
575 void DolphinPartBrowserExtension::cut()
577 m_part
->view()->cutSelectedItems();
580 void DolphinPartBrowserExtension::copy()
582 m_part
->view()->copySelectedItems();
585 void DolphinPartBrowserExtension::paste()
587 m_part
->view()->paste();
590 void DolphinPartBrowserExtension::pasteTo(const KUrl
&)
592 m_part
->view()->pasteIntoFolder();
595 void DolphinPartBrowserExtension::reparseConfiguration()
597 m_part
->view()->refresh();
602 DolphinPartFileInfoExtension::DolphinPartFileInfoExtension(DolphinPart
* part
)
603 : KParts::FileInfoExtension(part
)
607 DolphinPart
* DolphinPartFileInfoExtension::part() const
609 return static_cast<DolphinPart
*>(parent());
612 bool DolphinPartFileInfoExtension::hasSelection() const
614 return part()->view()->hasSelection();
617 KParts::FileInfoExtension::QueryModes
DolphinPartFileInfoExtension::supportedQueryModes() const
619 return (KParts::FileInfoExtension::AllItems
| KParts::FileInfoExtension::SelectedItems
);
622 KFileItemList
DolphinPartFileInfoExtension::queryFor(KParts::FileInfoExtension::QueryMode mode
) const
626 if (mode
== KParts::FileInfoExtension::None
)
629 if (!(supportedQueryModes() & mode
))
633 case KParts::FileInfoExtension::SelectedItems
:
635 return part()->view()->selectedItems();
637 case KParts::FileInfoExtension::AllItems
:
638 return part()->view()->items();
646 #include "dolphinpart.moc"