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_dirLister
= new DolphinDirLister
;
71 m_dirLister
->setAutoUpdate(true);
73 m_dirLister
->setMainWindow(parentWidget
->window());
75 m_dirLister
->setDelayedMimeTypes(true);
77 connect(m_dirLister
, SIGNAL(completed(KUrl
)), this, SLOT(slotCompleted(KUrl
)));
78 connect(m_dirLister
, SIGNAL(canceled(KUrl
)), this, SLOT(slotCanceled(KUrl
)));
79 connect(m_dirLister
, SIGNAL(percent(int)), this, SLOT(updateProgress(int)));
80 connect(m_dirLister
, SIGNAL(errorMessage(QString
)), this, SLOT(slotErrorMessage(QString
)));
82 m_dolphinModel
= new DolphinModel(this);
83 m_dolphinModel
->setDirLister(m_dirLister
); // m_dolphinModel takes ownership of m_dirLister
85 m_proxyModel
= new DolphinSortFilterProxyModel(this);
86 m_proxyModel
->setSourceModel(m_dolphinModel
);
88 m_view
= new DolphinView(parentWidget
, KUrl(), m_proxyModel
);
89 m_view
->setTabsForFilesEnabled(true);
92 setXMLFile("dolphinpart.rc");
94 connect(m_view
, SIGNAL(infoMessage(QString
)),
95 this, SLOT(slotMessage(QString
)));
96 connect(m_view
, SIGNAL(operationCompletedMessage(QString
)),
97 this, SLOT(slotMessage(QString
)));
98 connect(m_view
, SIGNAL(errorMessage(QString
)),
99 this, SLOT(slotErrorMessage(QString
)));
100 connect(m_view
, SIGNAL(itemTriggered(KFileItem
)),
101 this, SLOT(slotItemTriggered(KFileItem
)));
102 connect(m_view
, SIGNAL(tabRequested(KUrl
)),
103 this, SLOT(createNewWindow(KUrl
)));
104 connect(m_view
, SIGNAL(requestContextMenu(KFileItem
,KUrl
,QList
<QAction
*>)),
105 this, SLOT(slotOpenContextMenu(KFileItem
,KUrl
,QList
<QAction
*>)));
106 connect(m_view
, SIGNAL(selectionChanged(KFileItemList
)),
107 m_extension
, SIGNAL(selectionInfo(KFileItemList
)));
108 connect(m_view
, SIGNAL(selectionChanged(KFileItemList
)),
109 this, SLOT(slotSelectionChanged(KFileItemList
)));
110 connect(m_view
, SIGNAL(requestItemInfo(KFileItem
)),
111 this, SLOT(slotRequestItemInfo(KFileItem
)));
112 connect(m_view
, SIGNAL(modeChanged()),
113 this, SIGNAL(viewModeChanged())); // relay signal
114 connect(m_view
, SIGNAL(redirection(KUrl
, KUrl
)),
115 this, SLOT(slotRedirection(KUrl
, KUrl
)));
117 // Watch for changes that should result in updates to the
119 connect(m_dirLister
, SIGNAL(itemsDeleted(const KFileItemList
&)),
120 this, SLOT(updateStatusBar()));
121 connect(m_dirLister
, SIGNAL(clear()),
122 this, SLOT(updateStatusBar()));
123 connect(m_view
, SIGNAL(selectionChanged(const KFileItemList
)),
124 this, SLOT(updateStatusBar()));
126 m_actionHandler
= new DolphinViewActionHandler(actionCollection(), this);
127 m_actionHandler
->setCurrentView(m_view
);
128 connect(m_actionHandler
, SIGNAL(createDirectory()), SLOT(createDirectory()));
130 m_remoteEncoding
= new DolphinRemoteEncoding(this, m_actionHandler
);
131 connect(this, SIGNAL(aboutToOpenURL()),
132 m_remoteEncoding
, SLOT(slotAboutToOpenUrl()));
134 QClipboard
* clipboard
= QApplication::clipboard();
135 connect(clipboard
, SIGNAL(dataChanged()),
136 this, SLOT(updatePasteAction()));
139 m_actionHandler
->updateViewActions();
140 slotSelectionChanged(KFileItemList()); // initially disable selection-dependent actions
142 // TODO there was a "always open a new window" (when clicking on a directory) setting in konqueror
143 // (sort of spacial navigation)
145 loadPlugins(this, this, componentData());
149 DolphinPart::~DolphinPart()
151 DolphinSettings::instance().save();
152 DolphinNewFileMenuObserver::instance().detach(m_newFileMenu
);
155 void DolphinPart::createActions()
159 m_newFileMenu
= new KNewFileMenu(actionCollection(), "new_menu", this);
160 m_newFileMenu
->setParentWidget(widget());
161 DolphinNewFileMenuObserver::instance().attach(m_newFileMenu
);
162 connect(m_newFileMenu
->menu(), SIGNAL(aboutToShow()),
163 this, SLOT(updateNewMenu()));
165 KAction
*editMimeTypeAction
= actionCollection()->addAction( "editMimeType" );
166 editMimeTypeAction
->setText( i18nc("@action:inmenu Edit", "&Edit File Type..." ) );
167 connect(editMimeTypeAction
, SIGNAL(triggered()), SLOT(slotEditMimeType()));
169 KAction
* selectItemsMatching
= actionCollection()->addAction("select_items_matching");
170 selectItemsMatching
->setText(i18nc("@action:inmenu Edit", "Select Items Matching..."));
171 selectItemsMatching
->setShortcut(Qt::CTRL
| Qt::Key_S
);
172 connect(selectItemsMatching
, SIGNAL(triggered()), this, SLOT(slotSelectItemsMatchingPattern()));
174 KAction
* unselectItemsMatching
= actionCollection()->addAction("unselect_items_matching");
175 unselectItemsMatching
->setText(i18nc("@action:inmenu Edit", "Unselect Items Matching..."));
176 connect(unselectItemsMatching
, SIGNAL(triggered()), this, SLOT(slotUnselectItemsMatchingPattern()));
178 actionCollection()->addAction(KStandardAction::SelectAll
, "select_all", m_view
, SLOT(selectAll()));
180 KAction
* unselectAll
= actionCollection()->addAction("unselect_all");
181 unselectAll
->setText(i18nc("@action:inmenu Edit", "Unselect All"));
182 connect(unselectAll
, SIGNAL(triggered()), m_view
, SLOT(clearSelection()));
184 KAction
* invertSelection
= actionCollection()->addAction("invert_selection");
185 invertSelection
->setText(i18nc("@action:inmenu Edit", "Invert Selection"));
186 invertSelection
->setShortcut(Qt::CTRL
| Qt::SHIFT
| Qt::Key_A
);
187 connect(invertSelection
, SIGNAL(triggered()), m_view
, SLOT(invertSelection()));
189 // View menu: all done by DolphinViewActionHandler
193 QActionGroup
* goActionGroup
= new QActionGroup(this);
194 connect(goActionGroup
, SIGNAL(triggered(QAction
*)),
195 this, SLOT(slotGoTriggered(QAction
*)));
197 createGoAction("go_applications", "start-here-kde",
198 i18nc("@action:inmenu Go", "App&lications"), QString("programs:/"),
200 createGoAction("go_network_folders", "folder-remote",
201 i18nc("@action:inmenu Go", "&Network Folders"), QString("remote:/"),
203 createGoAction("go_settings", "preferences-system",
204 i18nc("@action:inmenu Go", "Sett&ings"), QString("settings:/"),
206 createGoAction("go_trash", "user-trash",
207 i18nc("@action:inmenu Go", "Trash"), QString("trash:/"),
209 createGoAction("go_autostart", "",
210 i18nc("@action:inmenu Go", "Autostart"), KGlobalSettings::autostartPath(),
214 if (KAuthorized::authorizeKAction("shell_access")) {
215 KAction
* action
= actionCollection()->addAction("open_terminal");
216 action
->setIcon(KIcon("utilities-terminal"));
217 action
->setText(i18nc("@action:inmenu Tools", "Open &Terminal"));
218 connect(action
, SIGNAL(triggered()), SLOT(slotOpenTerminal()));
219 action
->setShortcut(Qt::Key_F4
);
224 void DolphinPart::createGoAction(const char* name
, const char* iconName
,
225 const QString
& text
, const QString
& url
,
226 QActionGroup
* actionGroup
)
228 KAction
* action
= actionCollection()->addAction(name
);
229 action
->setIcon(KIcon(iconName
));
230 action
->setText(text
);
231 action
->setData(url
);
232 action
->setActionGroup(actionGroup
);
235 void DolphinPart::slotGoTriggered(QAction
* action
)
237 const QString url
= action
->data().toString();
238 emit m_extension
->openUrlRequest(KUrl(url
));
241 void DolphinPart::slotSelectionChanged(const KFileItemList
& selection
)
243 const bool hasSelection
= !selection
.isEmpty();
245 QAction
* renameAction
= actionCollection()->action("rename");
246 QAction
* moveToTrashAction
= actionCollection()->action("move_to_trash");
247 QAction
* deleteAction
= actionCollection()->action("delete");
248 QAction
* editMimeTypeAction
= actionCollection()->action("editMimeType");
249 QAction
* propertiesAction
= actionCollection()->action("properties");
250 QAction
* deleteWithTrashShortcut
= actionCollection()->action("delete_shortcut"); // see DolphinViewActionHandler
253 stateChanged("has_no_selection");
255 emit m_extension
->enableAction("cut", false);
256 emit m_extension
->enableAction("copy", false);
257 deleteWithTrashShortcut
->setEnabled(false);
258 editMimeTypeAction
->setEnabled(false);
260 stateChanged("has_selection");
262 // TODO share this code with DolphinMainWindow::updateEditActions (and the desktop code)
264 KFileItemListProperties
capabilities(selection
);
265 const bool enableMoveToTrash
= capabilities
.isLocal() && capabilities
.supportsMoving();
267 renameAction
->setEnabled(capabilities
.supportsMoving());
268 moveToTrashAction
->setEnabled(enableMoveToTrash
);
269 deleteAction
->setEnabled(capabilities
.supportsDeleting());
270 deleteWithTrashShortcut
->setEnabled(capabilities
.supportsDeleting() && !enableMoveToTrash
);
271 editMimeTypeAction
->setEnabled(true);
272 propertiesAction
->setEnabled(true);
273 emit m_extension
->enableAction("cut", capabilities
.supportsMoving());
274 emit m_extension
->enableAction("copy", true);
278 void DolphinPart::updatePasteAction()
280 QPair
<bool, QString
> pasteInfo
= m_view
->pasteInfo();
281 emit m_extension
->enableAction( "paste", pasteInfo
.first
);
282 emit m_extension
->setActionText( "paste", pasteInfo
.second
);
285 KAboutData
* DolphinPart::createAboutData()
287 return new KAboutData("dolphinpart", "dolphin", ki18nc("@title", "Dolphin Part"), "0.1");
290 bool DolphinPart::openUrl(const KUrl
& url
)
292 bool reload
= arguments().reload();
293 // A bit of a workaround so that changing the namefilter works: force reload.
294 // Otherwise DolphinView wouldn't relist the URL, so nothing would happen.
295 if (m_nameFilter
!= m_dirLister
->nameFilter())
297 if (m_view
->url() == url
&& !reload
) { // DolphinView won't do anything in that case, so don't emit started
300 setUrl(url
); // remember it at the KParts level
301 KUrl
visibleUrl(url
);
302 if (!m_nameFilter
.isEmpty()) {
303 visibleUrl
.addPath(m_nameFilter
);
305 QString prettyUrl
= visibleUrl
.pathOrUrl();
306 emit
setWindowCaption(prettyUrl
);
307 emit m_extension
->setLocationBarUrl(prettyUrl
);
308 emit
started(0); // get the wheel to spin
309 m_dirLister
->setNameFilter(m_nameFilter
);
312 emit
aboutToOpenURL();
318 void DolphinPart::slotCompleted(const KUrl
& url
)
324 void DolphinPart::slotCanceled(const KUrl
& url
)
329 void DolphinPart::slotMessage(const QString
& msg
)
331 emit
setStatusBarText(msg
);
334 void DolphinPart::slotErrorMessage(const QString
& msg
)
338 //KMessageBox::error(m_view, msg);
341 void DolphinPart::slotRequestItemInfo(const KFileItem
& item
)
343 emit m_extension
->mouseOverInfo(item
);
347 ReadOnlyPart::setStatusBarText(item
.getStatusBarInfo());
351 void DolphinPart::slotItemTriggered(const KFileItem
& item
)
353 KParts::OpenUrlArguments args
;
354 // Forget about the known mimetype if a target URL is used.
355 // Testcase: network:/ with a item (mimetype "inode/some-foo-service") pointing to a http URL (html)
356 if (item
.targetUrl() == item
.url()) {
357 args
.setMimeType(item
.mimetype());
360 // Ideally, konqueror should be changed to not require trustedSource for directory views,
361 // since the idea was not to need BrowserArguments for non-browser stuff...
362 KParts::BrowserArguments browserArgs
;
363 browserArgs
.trustedSource
= true;
364 emit m_extension
->openUrlRequest(item
.targetUrl(), args
, browserArgs
);
367 void DolphinPart::createNewWindow(const KUrl
& url
)
369 // TODO: Check issue N176832 for the missing QAIV signal; task 177399 - maybe this code
370 // should be moved into DolphinPart::slotItemTriggered()
371 emit m_extension
->createNewWindow(url
);
374 void DolphinPart::slotOpenContextMenu(const KFileItem
& _item
,
376 const QList
<QAction
*>& customActions
)
378 KParts::BrowserExtension::PopupFlags popupFlags
= KParts::BrowserExtension::DefaultPopupItems
379 | KParts::BrowserExtension::ShowProperties
380 | KParts::BrowserExtension::ShowUrlOperations
;
382 KFileItem
item(_item
);
384 if (item
.isNull()) { // viewport context menu
385 popupFlags
|= KParts::BrowserExtension::ShowNavigationItems
| KParts::BrowserExtension::ShowUp
;
386 item
= m_dirLister
->rootItem();
388 item
= KFileItem( S_IFDIR
, (mode_t
)-1, url() );
390 item
.setUrl(url()); // ensure we use the view url, not the canonical path (#213799)
393 // TODO: We should change the signature of the slots (and signals) for being able
394 // to tell for which items we want a popup.
396 if (m_view
->selectedItems().isEmpty()) {
399 items
= m_view
->selectedItems();
402 KFileItemListProperties
capabilities(items
);
404 KParts::BrowserExtension::ActionGroupMap actionGroups
;
405 QList
<QAction
*> editActions
;
406 editActions
+= m_view
->versionControlActions(m_view
->selectedItems());
407 editActions
+= customActions
;
409 if (!_item
.isNull()) { // only for context menu on one or more items
410 bool supportsDeleting
= capabilities
.supportsDeleting();
411 bool supportsMoving
= capabilities
.supportsMoving();
413 if (!supportsDeleting
) {
414 popupFlags
|= KParts::BrowserExtension::NoDeletion
;
417 if (supportsMoving
) {
418 editActions
.append(actionCollection()->action("rename"));
421 bool addTrash
= capabilities
.isLocal() && supportsMoving
;
423 if (supportsDeleting
) {
424 if ( !item
.isLocalFile() )
426 else if (QApplication::keyboardModifiers() & Qt::ShiftModifier
) {
431 KSharedConfig::Ptr globalConfig
= KSharedConfig::openConfig("kdeglobals", KConfig::IncludeGlobals
);
432 KConfigGroup
configGroup(globalConfig
, "KDE");
433 addDel
= configGroup
.readEntry("ShowDeleteCommand", false);
438 editActions
.append(actionCollection()->action("move_to_trash"));
440 editActions
.append(actionCollection()->action("delete"));
442 // Normally KonqPopupMenu only shows the "Create new" submenu in the current view
443 // since otherwise the created file would not be visible.
444 // But in treeview mode we should allow it.
445 if (m_view
->itemsExpandable())
446 popupFlags
|= KParts::BrowserExtension::ShowCreateDirectory
;
450 actionGroups
.insert("editactions", editActions
);
452 emit m_extension
->popupMenu(QCursor::pos(),
454 KParts::OpenUrlArguments(),
455 KParts::BrowserArguments(),
460 void DolphinPart::slotRedirection(const KUrl
& oldUrl
, const KUrl
& newUrl
)
462 //kDebug() << oldUrl << newUrl << "currentUrl=" << url();
463 if (oldUrl
.equals(url(), KUrl::CompareWithoutTrailingSlash
/* #207572 */)) {
464 KParts::ReadOnlyPart::setUrl(newUrl
);
465 const QString prettyUrl
= newUrl
.pathOrUrl();
466 emit m_extension
->setLocationBarUrl(prettyUrl
);
471 void DolphinPart::slotEditMimeType()
473 const KFileItemList items
= m_view
->selectedItems();
474 if (!items
.isEmpty()) {
475 KonqOperations::editMimeType(items
.first().mimetype(), m_view
);
479 void DolphinPart::slotSelectItemsMatchingPattern()
481 openSelectionDialog(i18nc("@title:window", "Select"),
482 i18n("Select all items matching this pattern:"),
486 void DolphinPart::slotUnselectItemsMatchingPattern()
488 openSelectionDialog(i18nc("@title:window", "Unselect"),
489 i18n("Unselect all items matching this pattern:"),
493 void DolphinPart::openSelectionDialog(const QString
& title
, const QString
& text
, bool selectItems
)
496 QString pattern
= KInputDialog::getText(title
, text
, "*", &okClicked
, m_view
);
498 if (okClicked
&& !pattern
.isEmpty()) {
499 QRegExp
patternRegExp(pattern
, Qt::CaseSensitive
, QRegExp::Wildcard
);
500 m_view
->setItemSelectionEnabled(patternRegExp
, selectItems
);
504 void DolphinPart::setCurrentViewMode(const QString
& viewModeName
)
506 QAction
* action
= actionCollection()->action(viewModeName
);
511 QString
DolphinPart::currentViewMode() const
513 return m_actionHandler
->currentViewModeActionName();
516 void DolphinPart::setNameFilter(const QString
& nameFilter
)
518 // This is the "/home/dfaure/*.diff" kind of name filter (KDirLister::setNameFilter)
519 // which is unrelated to DolphinView::setNameFilter which is substring filtering in a proxy.
520 m_nameFilter
= nameFilter
;
521 // TODO save/restore name filter in saveState/restoreState like KonqDirPart did in kde3?
524 void DolphinPart::slotOpenTerminal()
526 QString
dir(QDir::homePath());
530 // If the given directory is not local, it can still be the URL of an
531 // ioslave using UDS_LOCAL_PATH which to be converted first.
532 u
= KIO::NetAccess::mostLocalUrl(u
, widget());
534 //If the URL is local after the above conversion, set the directory.
535 if (u
.isLocalFile()) {
536 dir
= u
.toLocalFile();
539 KToolInvocation::invokeTerminal(QString(), dir
);
542 void DolphinPart::updateNewMenu()
544 // As requested by KNewFileMenu :
545 m_newFileMenu
->checkUpToDate();
546 m_newFileMenu
->setViewShowsHiddenFiles(m_view
->showHiddenFiles());
547 // And set the files that the menu apply on :
548 m_newFileMenu
->setPopupFiles(url());
551 void DolphinPart::updateStatusBar()
553 emit
ReadOnlyPart::setStatusBarText(m_view
->statusBarText());
556 void DolphinPart::updateProgress(int percent
)
558 m_extension
->loadingProgress(percent
);
561 void DolphinPart::createDirectory()
563 m_newFileMenu
->setViewShowsHiddenFiles(m_view
->showHiddenFiles());
564 m_newFileMenu
->setPopupFiles(url());
565 m_newFileMenu
->createDirectory();
568 void DolphinPart::setFilesToSelect(const KUrl::List
& files
)
570 m_view
->markUrlsAsSelected(files
);
575 void DolphinPartBrowserExtension::restoreState(QDataStream
&stream
)
577 KParts::BrowserExtension::restoreState(stream
);
578 m_part
->view()->restoreState(stream
);
581 void DolphinPartBrowserExtension::saveState(QDataStream
&stream
)
583 KParts::BrowserExtension::saveState(stream
);
584 m_part
->view()->saveState(stream
);
587 void DolphinPartBrowserExtension::cut()
589 m_part
->view()->cutSelectedItems();
592 void DolphinPartBrowserExtension::copy()
594 m_part
->view()->copySelectedItems();
597 void DolphinPartBrowserExtension::paste()
599 m_part
->view()->paste();
602 void DolphinPartBrowserExtension::pasteTo(const KUrl
&)
604 m_part
->view()->pasteIntoFolder();
607 void DolphinPartBrowserExtension::reparseConfiguration()
609 m_part
->view()->refresh();
614 DolphinPartFileInfoExtension::DolphinPartFileInfoExtension(DolphinPart
* part
)
615 : KParts::FileInfoExtension(part
)
619 DolphinPart
* DolphinPartFileInfoExtension::part() const
621 return static_cast<DolphinPart
*>(parent());
624 bool DolphinPartFileInfoExtension::hasSelection() const
626 return part()->view()->hasSelection();
629 KParts::FileInfoExtension::QueryModes
DolphinPartFileInfoExtension::supportedQueryModes() const
631 return (KParts::FileInfoExtension::AllItems
| KParts::FileInfoExtension::SelectedItems
);
634 KFileItemList
DolphinPartFileInfoExtension::queryFor(KParts::FileInfoExtension::QueryMode mode
) const
638 if (mode
== KParts::FileInfoExtension::None
)
641 if (!(supportedQueryModes() & mode
))
645 case KParts::FileInfoExtension::SelectedItems
:
647 return part()->view()->selectedItems();
649 case KParts::FileInfoExtension::AllItems
:
650 return part()->view()->items();
658 #include "dolphinpart.moc"