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.h>
23 #include <konq_operations.h>
25 #include <kaboutdata.h>
26 #include <kactioncollection.h>
27 #include <kconfiggroup.h>
29 #include <kglobalsettings.h>
30 #include <kiconloader.h>
32 #include <kmessagebox.h>
33 #include <kpluginfactory.h>
34 #include <ktoggleaction.h>
35 #include <kio/netaccess.h>
36 #include <ktoolinvocation.h>
37 #include <kauthorized.h>
38 #include <knewfilemenu.h>
40 #include <kinputdialog.h>
41 #include <kprotocolinfo.h>
43 #include "settings/dolphinsettings.h"
44 #include "views/dolphinview.h"
45 #include "views/dolphinviewactionhandler.h"
46 #include "views/dolphinsortfilterproxymodel.h"
47 #include "views/dolphinmodel.h"
48 #include "views/dolphinnewfilemenuobserver.h"
49 #include "views/dolphinremoteencoding.h"
50 #include "views/dolphindirlister.h"
52 #include <QActionGroup>
53 #include <QApplication>
56 K_PLUGIN_FACTORY(DolphinPartFactory
, registerPlugin
<DolphinPart
>();)
57 K_EXPORT_PLUGIN(DolphinPartFactory("dolphinpart", "dolphin"))
59 DolphinPart::DolphinPart(QWidget
* parentWidget
, QObject
* parent
, const QVariantList
& args
)
60 : KParts::ReadOnlyPart(parent
)
63 setComponentData(DolphinPartFactory::componentData(), false);
64 m_extension
= new DolphinPartBrowserExtension(this);
65 new DolphinPartFileInfoExtension(this);
67 // make sure that other apps using this part find Dolphin's view-file-columns icons
68 KIconLoader::global()->addAppDir("dolphin");
70 m_view
= new DolphinView(KUrl(), parentWidget
);
71 m_view
->setTabsForFilesEnabled(true);
74 connect(m_view
, SIGNAL(finishedPathLoading(KUrl
)), this, SLOT(slotCompleted(KUrl
)));
75 connect(m_view
, SIGNAL(pathLoadingProgress(int)), this, SLOT(updateProgress(int)));
76 connect(m_view
, SIGNAL(errorMessage(QString
)), this, SLOT(slotErrorMessage(QString
)));
78 setXMLFile("dolphinpart.rc");
80 connect(m_view
, SIGNAL(infoMessage(QString
)),
81 this, SLOT(slotMessage(QString
)));
82 connect(m_view
, SIGNAL(operationCompletedMessage(QString
)),
83 this, SLOT(slotMessage(QString
)));
84 connect(m_view
, SIGNAL(errorMessage(QString
)),
85 this, SLOT(slotErrorMessage(QString
)));
86 connect(m_view
, SIGNAL(itemTriggered(KFileItem
)),
87 this, SLOT(slotItemTriggered(KFileItem
)));
88 connect(m_view
, SIGNAL(tabRequested(KUrl
)),
89 this, SLOT(createNewWindow(KUrl
)));
90 connect(m_view
, SIGNAL(requestContextMenu(KFileItem
,KUrl
,QList
<QAction
*>)),
91 this, SLOT(slotOpenContextMenu(KFileItem
,KUrl
,QList
<QAction
*>)));
92 connect(m_view
, SIGNAL(selectionChanged(KFileItemList
)),
93 m_extension
, SIGNAL(selectionInfo(KFileItemList
)));
94 connect(m_view
, SIGNAL(selectionChanged(KFileItemList
)),
95 this, SLOT(slotSelectionChanged(KFileItemList
)));
96 connect(m_view
, SIGNAL(requestItemInfo(KFileItem
)),
97 this, SLOT(slotRequestItemInfo(KFileItem
)));
98 connect(m_view
, SIGNAL(modeChanged()),
99 this, SIGNAL(viewModeChanged())); // relay signal
100 connect(m_view
, SIGNAL(redirection(KUrl
, KUrl
)),
101 this, SLOT(slotRedirection(KUrl
, KUrl
)));
103 // Watch for changes that should result in updates to the
105 connect(m_view
, SIGNAL(itemCountChanged()), this, SLOT(updateStatusBar()));
106 connect(m_view
, SIGNAL(selectionChanged(const KFileItemList
)), this, SLOT(updateStatusBar()));
108 m_actionHandler
= new DolphinViewActionHandler(actionCollection(), this);
109 m_actionHandler
->setCurrentView(m_view
);
110 connect(m_actionHandler
, SIGNAL(createDirectory()), SLOT(createDirectory()));
112 m_remoteEncoding
= new DolphinRemoteEncoding(this, m_actionHandler
);
113 connect(this, SIGNAL(aboutToOpenURL()),
114 m_remoteEncoding
, SLOT(slotAboutToOpenUrl()));
116 QClipboard
* clipboard
= QApplication::clipboard();
117 connect(clipboard
, SIGNAL(dataChanged()),
118 this, SLOT(updatePasteAction()));
121 m_actionHandler
->updateViewActions();
122 slotSelectionChanged(KFileItemList()); // initially disable selection-dependent actions
124 // TODO there was a "always open a new window" (when clicking on a directory) setting in konqueror
125 // (sort of spacial navigation)
127 loadPlugins(this, this, componentData());
131 DolphinPart::~DolphinPart()
133 DolphinSettings::instance().save();
134 DolphinNewFileMenuObserver::instance().detach(m_newFileMenu
);
137 void DolphinPart::createActions()
141 m_newFileMenu
= new KNewFileMenu(actionCollection(), "new_menu", this);
142 m_newFileMenu
->setParentWidget(widget());
143 DolphinNewFileMenuObserver::instance().attach(m_newFileMenu
);
144 connect(m_newFileMenu
->menu(), SIGNAL(aboutToShow()),
145 this, SLOT(updateNewMenu()));
147 KAction
*editMimeTypeAction
= actionCollection()->addAction( "editMimeType" );
148 editMimeTypeAction
->setText( i18nc("@action:inmenu Edit", "&Edit File Type..." ) );
149 connect(editMimeTypeAction
, SIGNAL(triggered()), SLOT(slotEditMimeType()));
151 KAction
* selectItemsMatching
= actionCollection()->addAction("select_items_matching");
152 selectItemsMatching
->setText(i18nc("@action:inmenu Edit", "Select Items Matching..."));
153 selectItemsMatching
->setShortcut(Qt::CTRL
| Qt::Key_S
);
154 connect(selectItemsMatching
, SIGNAL(triggered()), this, SLOT(slotSelectItemsMatchingPattern()));
156 KAction
* unselectItemsMatching
= actionCollection()->addAction("unselect_items_matching");
157 unselectItemsMatching
->setText(i18nc("@action:inmenu Edit", "Unselect Items Matching..."));
158 connect(unselectItemsMatching
, SIGNAL(triggered()), this, SLOT(slotUnselectItemsMatchingPattern()));
160 actionCollection()->addAction(KStandardAction::SelectAll
, "select_all", m_view
, SLOT(selectAll()));
162 KAction
* unselectAll
= actionCollection()->addAction("unselect_all");
163 unselectAll
->setText(i18nc("@action:inmenu Edit", "Unselect All"));
164 connect(unselectAll
, SIGNAL(triggered()), m_view
, SLOT(clearSelection()));
166 KAction
* invertSelection
= actionCollection()->addAction("invert_selection");
167 invertSelection
->setText(i18nc("@action:inmenu Edit", "Invert Selection"));
168 invertSelection
->setShortcut(Qt::CTRL
| Qt::SHIFT
| Qt::Key_A
);
169 connect(invertSelection
, SIGNAL(triggered()), m_view
, SLOT(invertSelection()));
171 // View menu: all done by DolphinViewActionHandler
175 QActionGroup
* goActionGroup
= new QActionGroup(this);
176 connect(goActionGroup
, SIGNAL(triggered(QAction
*)),
177 this, SLOT(slotGoTriggered(QAction
*)));
179 createGoAction("go_applications", "start-here-kde",
180 i18nc("@action:inmenu Go", "App&lications"), QString("programs:/"),
182 createGoAction("go_network_folders", "folder-remote",
183 i18nc("@action:inmenu Go", "&Network Folders"), QString("remote:/"),
185 createGoAction("go_settings", "preferences-system",
186 i18nc("@action:inmenu Go", "Sett&ings"), QString("settings:/"),
188 createGoAction("go_trash", "user-trash",
189 i18nc("@action:inmenu Go", "Trash"), QString("trash:/"),
191 createGoAction("go_autostart", "",
192 i18nc("@action:inmenu Go", "Autostart"), KGlobalSettings::autostartPath(),
196 if (KAuthorized::authorizeKAction("shell_access")) {
197 KAction
* action
= actionCollection()->addAction("open_terminal");
198 action
->setIcon(KIcon("utilities-terminal"));
199 action
->setText(i18nc("@action:inmenu Tools", "Open &Terminal"));
200 connect(action
, SIGNAL(triggered()), SLOT(slotOpenTerminal()));
201 action
->setShortcut(Qt::Key_F4
);
206 void DolphinPart::createGoAction(const char* name
, const char* iconName
,
207 const QString
& text
, const QString
& url
,
208 QActionGroup
* actionGroup
)
210 KAction
* action
= actionCollection()->addAction(name
);
211 action
->setIcon(KIcon(iconName
));
212 action
->setText(text
);
213 action
->setData(url
);
214 action
->setActionGroup(actionGroup
);
217 void DolphinPart::slotGoTriggered(QAction
* action
)
219 const QString url
= action
->data().toString();
220 emit m_extension
->openUrlRequest(KUrl(url
));
223 void DolphinPart::slotSelectionChanged(const KFileItemList
& selection
)
225 const bool hasSelection
= !selection
.isEmpty();
227 QAction
* renameAction
= actionCollection()->action("rename");
228 QAction
* moveToTrashAction
= actionCollection()->action("move_to_trash");
229 QAction
* deleteAction
= actionCollection()->action("delete");
230 QAction
* editMimeTypeAction
= actionCollection()->action("editMimeType");
231 QAction
* propertiesAction
= actionCollection()->action("properties");
232 QAction
* deleteWithTrashShortcut
= actionCollection()->action("delete_shortcut"); // see DolphinViewActionHandler
235 stateChanged("has_no_selection");
237 emit m_extension
->enableAction("cut", false);
238 emit m_extension
->enableAction("copy", false);
239 deleteWithTrashShortcut
->setEnabled(false);
240 editMimeTypeAction
->setEnabled(false);
242 stateChanged("has_selection");
244 // TODO share this code with DolphinMainWindow::updateEditActions (and the desktop code)
246 KFileItemListProperties
capabilities(selection
);
247 const bool enableMoveToTrash
= capabilities
.isLocal() && capabilities
.supportsMoving();
249 renameAction
->setEnabled(capabilities
.supportsMoving());
250 moveToTrashAction
->setEnabled(enableMoveToTrash
);
251 deleteAction
->setEnabled(capabilities
.supportsDeleting());
252 deleteWithTrashShortcut
->setEnabled(capabilities
.supportsDeleting() && !enableMoveToTrash
);
253 editMimeTypeAction
->setEnabled(true);
254 propertiesAction
->setEnabled(true);
255 emit m_extension
->enableAction("cut", capabilities
.supportsMoving());
256 emit m_extension
->enableAction("copy", true);
260 void DolphinPart::updatePasteAction()
262 QPair
<bool, QString
> pasteInfo
= m_view
->pasteInfo();
263 emit m_extension
->enableAction( "paste", pasteInfo
.first
);
264 emit m_extension
->setActionText( "paste", pasteInfo
.second
);
267 KAboutData
* DolphinPart::createAboutData()
269 return new KAboutData("dolphinpart", "dolphin", ki18nc("@title", "Dolphin Part"), "0.1");
272 bool DolphinPart::openUrl(const KUrl
& url
)
274 bool reload
= arguments().reload();
275 // A bit of a workaround so that changing the namefilter works: force reload.
276 // Otherwise DolphinView wouldn't relist the URL, so nothing would happen.
277 if (m_nameFilter
!= m_view
->nameFilter())
279 if (m_view
->url() == url
&& !reload
) { // DolphinView won't do anything in that case, so don't emit started
282 setUrl(url
); // remember it at the KParts level
283 KUrl
visibleUrl(url
);
284 if (!m_nameFilter
.isEmpty()) {
285 visibleUrl
.addPath(m_nameFilter
);
287 QString prettyUrl
= visibleUrl
.pathOrUrl();
288 emit
setWindowCaption(prettyUrl
);
289 emit m_extension
->setLocationBarUrl(prettyUrl
);
290 emit
started(0); // get the wheel to spin
291 m_view
->setNameFilter(m_nameFilter
);
294 emit
aboutToOpenURL();
300 void DolphinPart::slotCompleted(const KUrl
& url
)
306 void DolphinPart::slotMessage(const QString
& msg
)
308 emit
setStatusBarText(msg
);
311 void DolphinPart::slotErrorMessage(const QString
& msg
)
315 //KMessageBox::error(m_view, msg);
318 void DolphinPart::slotRequestItemInfo(const KFileItem
& item
)
320 emit m_extension
->mouseOverInfo(item
);
324 ReadOnlyPart::setStatusBarText(item
.getStatusBarInfo());
328 void DolphinPart::slotItemTriggered(const KFileItem
& item
)
330 KParts::OpenUrlArguments args
;
331 // Forget about the known mimetype if a target URL is used.
332 // Testcase: network:/ with a item (mimetype "inode/some-foo-service") pointing to a http URL (html)
333 if (item
.targetUrl() == item
.url()) {
334 args
.setMimeType(item
.mimetype());
337 // Ideally, konqueror should be changed to not require trustedSource for directory views,
338 // since the idea was not to need BrowserArguments for non-browser stuff...
339 KParts::BrowserArguments browserArgs
;
340 browserArgs
.trustedSource
= true;
341 emit m_extension
->openUrlRequest(item
.targetUrl(), args
, browserArgs
);
344 void DolphinPart::createNewWindow(const KUrl
& url
)
346 // TODO: Check issue N176832 for the missing QAIV signal; task 177399 - maybe this code
347 // should be moved into DolphinPart::slotItemTriggered()
348 emit m_extension
->createNewWindow(url
);
351 void DolphinPart::slotOpenContextMenu(const KFileItem
& _item
,
353 const QList
<QAction
*>& customActions
)
355 KParts::BrowserExtension::PopupFlags popupFlags
= KParts::BrowserExtension::DefaultPopupItems
356 | KParts::BrowserExtension::ShowProperties
357 | KParts::BrowserExtension::ShowUrlOperations
;
359 KFileItem
item(_item
);
361 if (item
.isNull()) { // viewport context menu
362 popupFlags
|= KParts::BrowserExtension::ShowNavigationItems
| KParts::BrowserExtension::ShowUp
;
363 item
= m_view
->rootItem();
365 item
= KFileItem( S_IFDIR
, (mode_t
)-1, url() );
367 item
.setUrl(url()); // ensure we use the view url, not the canonical path (#213799)
370 // TODO: We should change the signature of the slots (and signals) for being able
371 // to tell for which items we want a popup.
373 if (m_view
->selectedItems().isEmpty()) {
376 items
= m_view
->selectedItems();
379 KFileItemListProperties
capabilities(items
);
381 KParts::BrowserExtension::ActionGroupMap actionGroups
;
382 QList
<QAction
*> editActions
;
383 editActions
+= m_view
->versionControlActions(m_view
->selectedItems());
384 editActions
+= customActions
;
386 if (!_item
.isNull()) { // only for context menu on one or more items
387 bool supportsDeleting
= capabilities
.supportsDeleting();
388 bool supportsMoving
= capabilities
.supportsMoving();
390 if (!supportsDeleting
) {
391 popupFlags
|= KParts::BrowserExtension::NoDeletion
;
394 if (supportsMoving
) {
395 editActions
.append(actionCollection()->action("rename"));
398 bool addTrash
= capabilities
.isLocal() && supportsMoving
;
400 if (supportsDeleting
) {
401 if ( !item
.isLocalFile() )
403 else if (QApplication::keyboardModifiers() & Qt::ShiftModifier
) {
408 KSharedConfig::Ptr globalConfig
= KSharedConfig::openConfig("kdeglobals", KConfig::IncludeGlobals
);
409 KConfigGroup
configGroup(globalConfig
, "KDE");
410 addDel
= configGroup
.readEntry("ShowDeleteCommand", false);
415 editActions
.append(actionCollection()->action("move_to_trash"));
417 editActions
.append(actionCollection()->action("delete"));
419 // Normally KonqPopupMenu only shows the "Create new" submenu in the current view
420 // since otherwise the created file would not be visible.
421 // But in treeview mode we should allow it.
422 if (m_view
->itemsExpandable())
423 popupFlags
|= KParts::BrowserExtension::ShowCreateDirectory
;
427 actionGroups
.insert("editactions", editActions
);
429 emit m_extension
->popupMenu(QCursor::pos(),
431 KParts::OpenUrlArguments(),
432 KParts::BrowserArguments(),
437 void DolphinPart::slotRedirection(const KUrl
& oldUrl
, const KUrl
& newUrl
)
439 //kDebug() << oldUrl << newUrl << "currentUrl=" << url();
440 if (oldUrl
.equals(url(), KUrl::CompareWithoutTrailingSlash
/* #207572 */)) {
441 KParts::ReadOnlyPart::setUrl(newUrl
);
442 const QString prettyUrl
= newUrl
.pathOrUrl();
443 emit m_extension
->setLocationBarUrl(prettyUrl
);
448 void DolphinPart::slotEditMimeType()
450 const KFileItemList items
= m_view
->selectedItems();
451 if (!items
.isEmpty()) {
452 KonqOperations::editMimeType(items
.first().mimetype(), m_view
);
456 void DolphinPart::slotSelectItemsMatchingPattern()
458 openSelectionDialog(i18nc("@title:window", "Select"),
459 i18n("Select all items matching this pattern:"),
463 void DolphinPart::slotUnselectItemsMatchingPattern()
465 openSelectionDialog(i18nc("@title:window", "Unselect"),
466 i18n("Unselect all items matching this pattern:"),
470 void DolphinPart::openSelectionDialog(const QString
& title
, const QString
& text
, bool selectItems
)
473 QString pattern
= KInputDialog::getText(title
, text
, "*", &okClicked
, m_view
);
475 if (okClicked
&& !pattern
.isEmpty()) {
476 QRegExp
patternRegExp(pattern
, Qt::CaseSensitive
, QRegExp::Wildcard
);
477 m_view
->setItemSelectionEnabled(patternRegExp
, selectItems
);
481 void DolphinPart::setCurrentViewMode(const QString
& viewModeName
)
483 QAction
* action
= actionCollection()->action(viewModeName
);
488 QString
DolphinPart::currentViewMode() const
490 return m_actionHandler
->currentViewModeActionName();
493 void DolphinPart::setNameFilter(const QString
& nameFilter
)
495 // This is the "/home/dfaure/*.diff" kind of name filter (KDirLister::setNameFilter)
496 // which is unrelated to DolphinView::setNameFilter which is substring filtering in a proxy.
497 m_nameFilter
= nameFilter
;
498 // TODO save/restore name filter in saveState/restoreState like KonqDirPart did in kde3?
501 void DolphinPart::slotOpenTerminal()
503 QString
dir(QDir::homePath());
507 // If the given directory is not local, it can still be the URL of an
508 // ioslave using UDS_LOCAL_PATH which to be converted first.
509 u
= KIO::NetAccess::mostLocalUrl(u
, widget());
511 //If the URL is local after the above conversion, set the directory.
512 if (u
.isLocalFile()) {
513 dir
= u
.toLocalFile();
516 KToolInvocation::invokeTerminal(QString(), dir
);
519 void DolphinPart::updateNewMenu()
521 // As requested by KNewFileMenu :
522 m_newFileMenu
->checkUpToDate();
523 m_newFileMenu
->setViewShowsHiddenFiles(m_view
->showHiddenFiles());
524 // And set the files that the menu apply on :
525 m_newFileMenu
->setPopupFiles(url());
528 void DolphinPart::updateStatusBar()
530 emit
ReadOnlyPart::setStatusBarText(m_view
->statusBarText());
533 void DolphinPart::updateProgress(int percent
)
535 m_extension
->loadingProgress(percent
);
538 void DolphinPart::createDirectory()
540 m_newFileMenu
->setViewShowsHiddenFiles(m_view
->showHiddenFiles());
541 m_newFileMenu
->setPopupFiles(url());
542 m_newFileMenu
->createDirectory();
545 void DolphinPart::setFilesToSelect(const KUrl::List
& files
)
547 m_view
->markUrlsAsSelected(files
);
552 void DolphinPartBrowserExtension::restoreState(QDataStream
&stream
)
554 KParts::BrowserExtension::restoreState(stream
);
555 m_part
->view()->restoreState(stream
);
558 void DolphinPartBrowserExtension::saveState(QDataStream
&stream
)
560 KParts::BrowserExtension::saveState(stream
);
561 m_part
->view()->saveState(stream
);
564 void DolphinPartBrowserExtension::cut()
566 m_part
->view()->cutSelectedItems();
569 void DolphinPartBrowserExtension::copy()
571 m_part
->view()->copySelectedItems();
574 void DolphinPartBrowserExtension::paste()
576 m_part
->view()->paste();
579 void DolphinPartBrowserExtension::pasteTo(const KUrl
&)
581 m_part
->view()->pasteIntoFolder();
584 void DolphinPartBrowserExtension::reparseConfiguration()
586 m_part
->view()->refresh();
591 DolphinPartFileInfoExtension::DolphinPartFileInfoExtension(DolphinPart
* part
)
592 : KParts::FileInfoExtension(part
)
596 DolphinPart
* DolphinPartFileInfoExtension::part() const
598 return static_cast<DolphinPart
*>(parent());
601 bool DolphinPartFileInfoExtension::hasSelection() const
603 return part()->view()->hasSelection();
606 KParts::FileInfoExtension::QueryModes
DolphinPartFileInfoExtension::supportedQueryModes() const
608 return (KParts::FileInfoExtension::AllItems
| KParts::FileInfoExtension::SelectedItems
);
611 KFileItemList
DolphinPartFileInfoExtension::queryFor(KParts::FileInfoExtension::QueryMode mode
) const
615 if (mode
== KParts::FileInfoExtension::None
)
618 if (!(supportedQueryModes() & mode
))
622 case KParts::FileInfoExtension::SelectedItems
:
624 return part()->view()->selectedItems();
626 case KParts::FileInfoExtension::AllItems
:
627 return part()->view()->items();
635 #include "dolphinpart.moc"