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"
21 #include "dolphinviewactionhandler.h"
22 #include "dolphinsortfilterproxymodel.h"
23 #include "dolphinview.h"
24 #include "dolphinmodel.h"
25 #include "dolphinnewmenuobserver.h"
26 #include "dolphinremoteencoding.h"
28 #include <kfileitemlistproperties.h>
29 #include <konq_operations.h>
31 #include <kaboutdata.h>
32 #include <kactioncollection.h>
33 #include <kconfiggroup.h>
35 #include <kdirlister.h>
36 #include <kglobalsettings.h>
37 #include <kiconloader.h>
39 #include <kmessagebox.h>
40 #include <kpluginfactory.h>
41 #include <ktoggleaction.h>
42 #include <kio/netaccess.h>
43 #include <ktoolinvocation.h>
44 #include <kauthorized.h>
45 #include <knewfilemenu.h>
47 #include <kinputdialog.h>
49 #include "settings/dolphinsettings.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);
65 // make sure that other apps using this part find Dolphin's view-file-columns icons
66 KIconLoader::global()->addAppDir("dolphin");
68 m_dirLister
= new KDirLister
;
69 m_dirLister
->setAutoUpdate(true);
71 m_dirLister
->setMainWindow(parentWidget
->window());
73 m_dirLister
->setDelayedMimeTypes(true);
75 connect(m_dirLister
, SIGNAL(completed(KUrl
)), this, SLOT(slotCompleted(KUrl
)));
76 connect(m_dirLister
, SIGNAL(canceled(KUrl
)), this, SLOT(slotCanceled(KUrl
)));
77 connect(m_dirLister
, SIGNAL(percent(int)), this, SLOT(updateProgress(int)));
79 m_dolphinModel
= new DolphinModel(this);
80 m_dolphinModel
->setDirLister(m_dirLister
);
82 m_proxyModel
= new DolphinSortFilterProxyModel(this);
83 m_proxyModel
->setSourceModel(m_dolphinModel
);
85 m_view
= new DolphinView(parentWidget
, KUrl(), m_proxyModel
);
86 m_view
->setTabsForFilesEnabled(true);
89 setXMLFile("dolphinpart.rc");
91 connect(m_view
, SIGNAL(infoMessage(QString
)),
92 this, SLOT(slotMessage(QString
)));
93 connect(m_view
, SIGNAL(operationCompletedMessage(QString
)),
94 this, SLOT(slotMessage(QString
)));
95 connect(m_view
, SIGNAL(errorMessage(QString
)),
96 this, SLOT(slotErrorMessage(QString
)));
97 connect(m_view
, SIGNAL(itemTriggered(KFileItem
)),
98 this, SLOT(slotItemTriggered(KFileItem
)));
99 connect(m_view
, SIGNAL(tabRequested(KUrl
)),
100 this, SLOT(createNewWindow(KUrl
)));
101 connect(m_view
, SIGNAL(requestContextMenu(KFileItem
,KUrl
,QList
<QAction
*>)),
102 this, SLOT(slotOpenContextMenu(KFileItem
,KUrl
,QList
<QAction
*>)));
103 connect(m_view
, SIGNAL(selectionChanged(KFileItemList
)),
104 m_extension
, SIGNAL(selectionInfo(KFileItemList
)));
105 connect(m_view
, SIGNAL(selectionChanged(KFileItemList
)),
106 this, SLOT(slotSelectionChanged(KFileItemList
)));
107 connect(m_view
, SIGNAL(requestItemInfo(KFileItem
)),
108 this, SLOT(slotRequestItemInfo(KFileItem
)));
109 connect(m_view
, SIGNAL(modeChanged()),
110 this, SIGNAL(viewModeChanged())); // relay signal
111 connect(m_view
, SIGNAL(redirection(KUrl
, KUrl
)),
112 this, SLOT(slotRedirection(KUrl
, KUrl
)));
114 // Watch for changes that should result in updates to the
116 connect(m_dirLister
, SIGNAL(itemsDeleted(const KFileItemList
&)),
117 this, SLOT(updateStatusBar()));
118 connect(m_dirLister
, SIGNAL(clear()),
119 this, SLOT(updateStatusBar()));
120 connect(m_view
, SIGNAL(selectionChanged(const KFileItemList
)),
121 this, SLOT(updateStatusBar()));
123 m_actionHandler
= new DolphinViewActionHandler(actionCollection(), this);
124 m_actionHandler
->setCurrentView(m_view
);
125 connect(m_actionHandler
, SIGNAL(createDirectory()), SLOT(createDirectory()));
127 m_remoteEncoding
= new DolphinRemoteEncoding(this, m_actionHandler
);
128 connect(this, SIGNAL(aboutToOpenURL()),
129 m_remoteEncoding
, SLOT(slotAboutToOpenUrl()));
131 QClipboard
* clipboard
= QApplication::clipboard();
132 connect(clipboard
, SIGNAL(dataChanged()),
133 this, SLOT(updatePasteAction()));
136 m_actionHandler
->updateViewActions();
137 slotSelectionChanged(KFileItemList()); // initially disable selection-dependent actions
139 // TODO there was a "always open a new window" (when clicking on a directory) setting in konqueror
140 // (sort of spacial navigation)
142 loadPlugins(this, this, componentData());
146 DolphinPart::~DolphinPart()
148 DolphinSettings::instance().save();
149 DolphinNewMenuObserver::instance().detach(m_newMenu
);
153 void DolphinPart::createActions()
157 m_newMenu
= new KNewFileMenu(actionCollection(), "new_menu", this);
158 m_newMenu
->setParentWidget(widget());
159 DolphinNewMenuObserver::instance().attach(m_newMenu
);
160 connect(m_newMenu
->menu(), SIGNAL(aboutToShow()),
161 this, SLOT(updateNewMenu()));
163 KAction
*editMimeTypeAction
= actionCollection()->addAction( "editMimeType" );
164 editMimeTypeAction
->setText( i18nc("@action:inmenu Edit", "&Edit File Type..." ) );
165 connect(editMimeTypeAction
, SIGNAL(triggered()), SLOT(slotEditMimeType()));
167 KAction
* selectItemsMatching
= actionCollection()->addAction("select_items_matching");
168 selectItemsMatching
->setText(i18nc("@action:inmenu Edit", "Select Items Matching..."));
169 selectItemsMatching
->setShortcut(Qt::CTRL
| Qt::Key_S
);
170 connect(selectItemsMatching
, SIGNAL(triggered()), this, SLOT(slotSelectItemsMatchingPattern()));
172 KAction
* unselectItemsMatching
= actionCollection()->addAction("unselect_items_matching");
173 unselectItemsMatching
->setText(i18nc("@action:inmenu Edit", "Unselect Items Matching..."));
174 connect(unselectItemsMatching
, SIGNAL(triggered()), this, SLOT(slotUnselectItemsMatchingPattern()));
176 actionCollection()->addAction(KStandardAction::SelectAll
, "select_all", m_view
, SLOT(selectAll()));
178 KAction
* unselectAll
= actionCollection()->addAction("unselect_all");
179 unselectAll
->setText(i18nc("@action:inmenu Edit", "Unselect All"));
180 connect(unselectAll
, SIGNAL(triggered()), m_view
, SLOT(clearSelection()));
182 KAction
* invertSelection
= actionCollection()->addAction("invert_selection");
183 invertSelection
->setText(i18nc("@action:inmenu Edit", "Invert Selection"));
184 invertSelection
->setShortcut(Qt::CTRL
| Qt::SHIFT
| Qt::Key_A
);
185 connect(invertSelection
, SIGNAL(triggered()), m_view
, SLOT(invertSelection()));
187 // View menu: all done by DolphinViewActionHandler
191 QActionGroup
* goActionGroup
= new QActionGroup(this);
192 connect(goActionGroup
, SIGNAL(triggered(QAction
*)),
193 this, SLOT(slotGoTriggered(QAction
*)));
195 createGoAction("go_applications", "start-here-kde",
196 i18nc("@action:inmenu Go", "App&lications"), QString("programs:/"),
198 createGoAction("go_network_folders", "folder-remote",
199 i18nc("@action:inmenu Go", "&Network Folders"), QString("remote:/"),
201 createGoAction("go_settings", "preferences-system",
202 i18nc("@action:inmenu Go", "Sett&ings"), QString("settings:/"),
204 createGoAction("go_trash", "user-trash",
205 i18nc("@action:inmenu Go", "Trash"), QString("trash:/"),
207 createGoAction("go_autostart", "",
208 i18nc("@action:inmenu Go", "Autostart"), KGlobalSettings::autostartPath(),
212 if (KAuthorized::authorizeKAction("shell_access")) {
213 KAction
* action
= actionCollection()->addAction("open_terminal");
214 action
->setIcon(KIcon("utilities-terminal"));
215 action
->setText(i18nc("@action:inmenu Tools", "Open &Terminal"));
216 connect(action
, SIGNAL(triggered()), SLOT(slotOpenTerminal()));
217 action
->setShortcut(Qt::Key_F4
);
222 void DolphinPart::createGoAction(const char* name
, const char* iconName
,
223 const QString
& text
, const QString
& url
,
224 QActionGroup
* actionGroup
)
226 KAction
* action
= actionCollection()->addAction(name
);
227 action
->setIcon(KIcon(iconName
));
228 action
->setText(text
);
229 action
->setData(url
);
230 action
->setActionGroup(actionGroup
);
233 void DolphinPart::slotGoTriggered(QAction
* action
)
235 const QString url
= action
->data().toString();
236 emit m_extension
->openUrlRequest(KUrl(url
));
239 void DolphinPart::slotSelectionChanged(const KFileItemList
& selection
)
241 const bool hasSelection
= !selection
.isEmpty();
243 QAction
* renameAction
= actionCollection()->action("rename");
244 QAction
* moveToTrashAction
= actionCollection()->action("move_to_trash");
245 QAction
* deleteAction
= actionCollection()->action("delete");
246 QAction
* editMimeTypeAction
= actionCollection()->action("editMimeType");
247 QAction
* propertiesAction
= actionCollection()->action("properties");
248 QAction
* deleteWithTrashShortcut
= actionCollection()->action("delete_shortcut"); // see DolphinViewActionHandler
251 stateChanged("has_no_selection");
253 emit m_extension
->enableAction("cut", false);
254 emit m_extension
->enableAction("copy", false);
255 deleteWithTrashShortcut
->setEnabled(false);
256 editMimeTypeAction
->setEnabled(false);
258 stateChanged("has_selection");
260 // TODO share this code with DolphinMainWindow::updateEditActions (and the desktop code)
262 KFileItemListProperties
capabilities(selection
);
263 const bool enableMoveToTrash
= capabilities
.isLocal() && capabilities
.supportsMoving();
265 renameAction
->setEnabled(capabilities
.supportsMoving());
266 moveToTrashAction
->setEnabled(enableMoveToTrash
);
267 deleteAction
->setEnabled(capabilities
.supportsDeleting());
268 deleteWithTrashShortcut
->setEnabled(capabilities
.supportsDeleting() && !enableMoveToTrash
);
269 editMimeTypeAction
->setEnabled(true);
270 propertiesAction
->setEnabled(true);
271 emit m_extension
->enableAction("cut", capabilities
.supportsMoving());
272 emit m_extension
->enableAction("copy", true);
276 void DolphinPart::updatePasteAction()
278 QPair
<bool, QString
> pasteInfo
= m_view
->pasteInfo();
279 emit m_extension
->enableAction( "paste", pasteInfo
.first
);
280 emit m_extension
->setActionText( "paste", pasteInfo
.second
);
283 KAboutData
* DolphinPart::createAboutData()
285 return new KAboutData("dolphinpart", "dolphin", ki18nc("@title", "Dolphin Part"), "0.1");
288 bool DolphinPart::openUrl(const KUrl
& url
)
290 bool reload
= arguments().reload();
291 // A bit of a workaround so that changing the namefilter works: force reload.
292 // Otherwise DolphinView wouldn't relist the URL, so nothing would happen.
293 if (m_nameFilter
!= m_dirLister
->nameFilter())
295 if (m_view
->url() == url
&& !reload
) { // DolphinView won't do anything in that case, so don't emit started
298 setUrl(url
); // remember it at the KParts level
299 KUrl
visibleUrl(url
);
300 if (!m_nameFilter
.isEmpty()) {
301 visibleUrl
.addPath(m_nameFilter
);
303 QString prettyUrl
= visibleUrl
.pathOrUrl();
304 emit
setWindowCaption(prettyUrl
);
305 emit m_extension
->setLocationBarUrl(prettyUrl
);
306 emit
started(0); // get the wheel to spin
307 m_dirLister
->setNameFilter(m_nameFilter
);
310 emit
aboutToOpenURL();
316 void DolphinPart::slotCompleted(const KUrl
& url
)
322 void DolphinPart::slotCanceled(const KUrl
& url
)
327 void DolphinPart::slotMessage(const QString
& msg
)
329 emit
setStatusBarText(msg
);
332 void DolphinPart::slotErrorMessage(const QString
& msg
)
334 KMessageBox::error(m_view
, msg
);
337 void DolphinPart::slotRequestItemInfo(const KFileItem
& item
)
339 emit m_extension
->mouseOverInfo(item
);
343 ReadOnlyPart::setStatusBarText(item
.getStatusBarInfo());
347 void DolphinPart::slotItemTriggered(const KFileItem
& item
)
349 KParts::OpenUrlArguments args
;
350 args
.setMimeType(item
.mimetype());
352 // Ideally, konqueror should be changed to not require trustedSource for directory views,
353 // since the idea was not to need BrowserArguments for non-browser stuff...
354 KParts::BrowserArguments browserArgs
;
355 browserArgs
.trustedSource
= true;
356 emit m_extension
->openUrlRequest(item
.targetUrl(), args
, browserArgs
);
359 void DolphinPart::createNewWindow(const KUrl
& url
)
361 // TODO: Check issue N176832 for the missing QAIV signal; task 177399 - maybe this code
362 // should be moved into DolphinPart::slotItemTriggered()
363 emit m_extension
->createNewWindow(url
);
366 void DolphinPart::slotOpenContextMenu(const KFileItem
& _item
,
368 const QList
<QAction
*>& customActions
)
370 KParts::BrowserExtension::PopupFlags popupFlags
= KParts::BrowserExtension::DefaultPopupItems
371 | KParts::BrowserExtension::ShowProperties
372 | KParts::BrowserExtension::ShowUrlOperations
;
374 KFileItem
item(_item
);
376 if (item
.isNull()) { // viewport context menu
377 popupFlags
|= KParts::BrowserExtension::ShowNavigationItems
| KParts::BrowserExtension::ShowUp
;
378 item
= m_dirLister
->rootItem();
380 item
= KFileItem( S_IFDIR
, (mode_t
)-1, url() );
382 item
.setUrl(url()); // ensure we use the view url, not the canonical path (#213799)
385 // TODO: We should change the signature of the slots (and signals) for being able
386 // to tell for which items we want a popup.
387 const KFileItemList items
= (m_view
->selectedItems().count() ? m_view
->selectedItems()
388 : KFileItemList() << item
);
389 KFileItemListProperties
capabilities(items
);
391 KParts::BrowserExtension::ActionGroupMap actionGroups
;
392 QList
<QAction
*> editActions
;
393 editActions
+= m_view
->versionControlActions(m_view
->selectedItems());
394 editActions
+= customActions
;
396 if (!_item
.isNull()) { // only for context menu on one or more items
397 bool supportsDeleting
= capabilities
.supportsDeleting();
398 bool supportsMoving
= capabilities
.supportsMoving();
400 if (!supportsDeleting
) {
401 popupFlags
|= KParts::BrowserExtension::NoDeletion
;
404 if (supportsMoving
) {
405 editActions
.append(actionCollection()->action("rename"));
408 bool addTrash
= capabilities
.isLocal() && supportsMoving
;
410 if (supportsDeleting
) {
411 if ( !item
.isLocalFile() )
413 else if (QApplication::keyboardModifiers() & Qt::ShiftModifier
) {
418 KSharedConfig::Ptr globalConfig
= KSharedConfig::openConfig("kdeglobals", KConfig::IncludeGlobals
);
419 KConfigGroup
configGroup(globalConfig
, "KDE");
420 addDel
= configGroup
.readEntry("ShowDeleteCommand", false);
425 editActions
.append(actionCollection()->action("move_to_trash"));
427 editActions
.append(actionCollection()->action("delete"));
429 // Normally KonqPopupMenu only shows the "Create new" submenu in the current view
430 // since otherwise the created file would not be visible.
431 // But in treeview mode we should allow it.
432 if (m_view
->itemsExpandable())
433 popupFlags
|= KParts::BrowserExtension::ShowCreateDirectory
;
437 actionGroups
.insert("editactions", editActions
);
439 emit m_extension
->popupMenu(QCursor::pos(),
441 KParts::OpenUrlArguments(),
442 KParts::BrowserArguments(),
447 void DolphinPart::slotRedirection(const KUrl
& oldUrl
, const KUrl
& newUrl
)
449 //kDebug() << oldUrl << newUrl << "currentUrl=" << url();
450 if (oldUrl
.equals(url(), KUrl::CompareWithoutTrailingSlash
/* #207572 */)) {
451 KParts::ReadOnlyPart::setUrl(newUrl
);
452 const QString prettyUrl
= newUrl
.pathOrUrl();
453 emit m_extension
->setLocationBarUrl(prettyUrl
);
459 void DolphinPartBrowserExtension::restoreState(QDataStream
&stream
)
461 KParts::BrowserExtension::restoreState(stream
);
462 m_part
->view()->restoreState(stream
);
465 void DolphinPartBrowserExtension::saveState(QDataStream
&stream
)
467 KParts::BrowserExtension::saveState(stream
);
468 m_part
->view()->saveState(stream
);
471 void DolphinPartBrowserExtension::cut()
473 m_part
->view()->cutSelectedItems();
476 void DolphinPartBrowserExtension::copy()
478 m_part
->view()->copySelectedItems();
481 void DolphinPartBrowserExtension::paste()
483 m_part
->view()->paste();
486 void DolphinPartBrowserExtension::pasteTo(const KUrl
&)
488 m_part
->view()->pasteIntoFolder();
491 void DolphinPartBrowserExtension::reparseConfiguration()
493 m_part
->view()->refresh();
498 void DolphinPart::slotEditMimeType()
500 const KFileItemList items
= m_view
->selectedItems();
501 if (!items
.isEmpty()) {
502 KonqOperations::editMimeType(items
.first().mimetype(), m_view
);
506 void DolphinPart::slotSelectItemsMatchingPattern()
508 openSelectionDialog(i18nc("@title:window", "Select"),
509 i18n("Select all items matching this pattern:"),
510 QItemSelectionModel::Select
);
513 void DolphinPart::slotUnselectItemsMatchingPattern()
515 openSelectionDialog(i18nc("@title:window", "Unselect"),
516 i18n("Unselect all items matching this pattern:"),
517 QItemSelectionModel::Deselect
);
520 void DolphinPart::openSelectionDialog(const QString
& title
, const QString
& text
, QItemSelectionModel::SelectionFlags command
)
523 QString pattern
= KInputDialog::getText(title
, text
, "*", &okClicked
, m_view
);
525 if (okClicked
&& !pattern
.isEmpty()) {
526 QRegExp
patternRegExp(pattern
, Qt::CaseSensitive
, QRegExp::Wildcard
);
527 QItemSelection matchingIndexes
= childrenMatchingPattern(QModelIndex(), patternRegExp
);
528 m_view
->selectionModel()->select(matchingIndexes
, command
);
532 QItemSelection
DolphinPart::childrenMatchingPattern(const QModelIndex
& parent
, const QRegExp
& patternRegExp
)
534 QItemSelection matchingIndexes
;
535 int numRows
= m_proxyModel
->rowCount(parent
);
537 for (int row
= 0; row
< numRows
; row
++) {
538 QModelIndex index
= m_proxyModel
->index(row
, 0, parent
);
539 QModelIndex sourceIndex
= m_proxyModel
->mapToSource(index
);
541 if (sourceIndex
.isValid() && patternRegExp
.exactMatch(m_dolphinModel
->data(sourceIndex
).toString())) {
542 matchingIndexes
+= QItemSelectionRange(index
);
545 if (m_proxyModel
->hasChildren(index
)) {
546 matchingIndexes
+= childrenMatchingPattern(index
, patternRegExp
);
550 return matchingIndexes
;
553 void DolphinPart::setCurrentViewMode(const QString
& viewModeName
)
555 QAction
* action
= actionCollection()->action(viewModeName
);
560 QString
DolphinPart::currentViewMode() const
562 return m_actionHandler
->currentViewModeActionName();
565 void DolphinPart::setNameFilter(const QString
& nameFilter
)
567 // This is the "/home/dfaure/*.diff" kind of name filter (KDirLister::setNameFilter)
568 // which is unrelated to DolphinView::setNameFilter which is substring filtering in a proxy.
569 m_nameFilter
= nameFilter
;
570 // TODO save/restore name filter in saveState/restoreState like KonqDirPart did in kde3?
573 void DolphinPart::slotOpenTerminal()
575 QString
dir(QDir::homePath());
579 // If the given directory is not local, it can still be the URL of an
580 // ioslave using UDS_LOCAL_PATH which to be converted first.
581 u
= KIO::NetAccess::mostLocalUrl(u
, widget());
583 //If the URL is local after the above conversion, set the directory.
584 if (u
.isLocalFile()) {
585 dir
= u
.toLocalFile();
588 KToolInvocation::invokeTerminal(QString(), dir
);
591 void DolphinPart::updateNewMenu()
593 // As requested by KNewFileMenu :
594 m_newMenu
->checkUpToDate();
595 m_newMenu
->setViewShowsHiddenFiles(m_view
->showHiddenFiles());
596 // And set the files that the menu apply on :
597 m_newMenu
->setPopupFiles(url());
600 void DolphinPart::updateStatusBar()
602 emit
ReadOnlyPart::setStatusBarText(m_view
->statusBarText());
605 void DolphinPart::updateProgress(int percent
)
607 m_extension
->loadingProgress(percent
);
610 void DolphinPart::createDirectory()
612 m_newMenu
->setViewShowsHiddenFiles(m_view
->showHiddenFiles());
613 m_newMenu
->setPopupFiles(url());
614 m_newMenu
->createDirectory();
617 void DolphinPart::setFilesToSelect(const KUrl::List
& files
)
619 m_view
->markUrlsAsSelected(files
);
622 #include "dolphinpart.moc"