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(requestUrlChange(KUrl
)),
110 this, SLOT(slotRequestUrlChange(KUrl
)));
111 connect(m_view
, SIGNAL(modeChanged()),
112 this, SIGNAL(viewModeChanged())); // relay signal
113 connect(m_view
, SIGNAL(redirection(KUrl
, KUrl
)),
114 this, SLOT(slotRedirection(KUrl
, KUrl
)));
116 // Watch for changes that should result in updates to the
118 connect(m_dirLister
, SIGNAL(itemsDeleted(const KFileItemList
&)),
119 this, SLOT(updateStatusBar()));
120 connect(m_dirLister
, SIGNAL(clear()),
121 this, SLOT(updateStatusBar()));
122 connect(m_view
, SIGNAL(selectionChanged(const KFileItemList
)),
123 this, SLOT(updateStatusBar()));
125 m_actionHandler
= new DolphinViewActionHandler(actionCollection(), this);
126 m_actionHandler
->setCurrentView(m_view
);
127 connect(m_actionHandler
, SIGNAL(createDirectory()), SLOT(createDirectory()));
129 m_remoteEncoding
= new DolphinRemoteEncoding(this, m_actionHandler
);
130 connect(this, SIGNAL(aboutToOpenURL()),
131 m_remoteEncoding
, SLOT(slotAboutToOpenUrl()));
133 QClipboard
* clipboard
= QApplication::clipboard();
134 connect(clipboard
, SIGNAL(dataChanged()),
135 this, SLOT(updatePasteAction()));
138 m_actionHandler
->updateViewActions();
139 slotSelectionChanged(KFileItemList()); // initially disable selection-dependent actions
141 // TODO there was a "always open a new window" (when clicking on a directory) setting in konqueror
142 // (sort of spacial navigation)
144 loadPlugins(this, this, componentData());
148 DolphinPart::~DolphinPart()
150 DolphinSettings::instance().save();
151 DolphinNewMenuObserver::instance().detach(m_newMenu
);
155 void DolphinPart::createActions()
159 m_newMenu
= new KNewFileMenu(actionCollection(), "new_menu", widget());
160 DolphinNewMenuObserver::instance().attach(m_newMenu
);
161 connect(m_newMenu
->menu(), SIGNAL(aboutToShow()),
162 this, SLOT(updateNewMenu()));
164 KAction
*editMimeTypeAction
= actionCollection()->addAction( "editMimeType" );
165 editMimeTypeAction
->setText( i18nc("@action:inmenu Edit", "&Edit File Type..." ) );
166 connect(editMimeTypeAction
, SIGNAL(triggered()), SLOT(slotEditMimeType()));
168 KAction
* selectItemsMatching
= actionCollection()->addAction("select_items_matching");
169 selectItemsMatching
->setText(i18nc("@action:inmenu Edit", "Select Items Matching..."));
170 selectItemsMatching
->setShortcut(Qt::CTRL
| Qt::Key_S
);
171 connect(selectItemsMatching
, SIGNAL(triggered()), this, SLOT(slotSelectItemsMatchingPattern()));
173 KAction
* unselectItemsMatching
= actionCollection()->addAction("unselect_items_matching");
174 unselectItemsMatching
->setText(i18nc("@action:inmenu Edit", "Unselect Items Matching..."));
175 connect(unselectItemsMatching
, SIGNAL(triggered()), this, SLOT(slotUnselectItemsMatchingPattern()));
177 actionCollection()->addAction(KStandardAction::SelectAll
, "select_all", m_view
, SLOT(selectAll()));
179 KAction
* unselectAll
= actionCollection()->addAction("unselect_all");
180 unselectAll
->setText(i18nc("@action:inmenu Edit", "Unselect All"));
181 connect(unselectAll
, SIGNAL(triggered()), m_view
, SLOT(clearSelection()));
183 KAction
* invertSelection
= actionCollection()->addAction("invert_selection");
184 invertSelection
->setText(i18nc("@action:inmenu Edit", "Invert Selection"));
185 invertSelection
->setShortcut(Qt::CTRL
| Qt::SHIFT
| Qt::Key_A
);
186 connect(invertSelection
, SIGNAL(triggered()), m_view
, SLOT(invertSelection()));
188 // View menu: all done by DolphinViewActionHandler
192 QActionGroup
* goActionGroup
= new QActionGroup(this);
193 connect(goActionGroup
, SIGNAL(triggered(QAction
*)),
194 this, SLOT(slotGoTriggered(QAction
*)));
196 createGoAction("go_applications", "start-here-kde",
197 i18nc("@action:inmenu Go", "App&lications"), QString("programs:/"),
199 createGoAction("go_network_folders", "folder-remote",
200 i18nc("@action:inmenu Go", "&Network Folders"), QString("remote:/"),
202 createGoAction("go_settings", "preferences-system",
203 i18nc("@action:inmenu Go", "Sett&ings"), QString("settings:/"),
205 createGoAction("go_trash", "user-trash",
206 i18nc("@action:inmenu Go", "Trash"), QString("trash:/"),
208 createGoAction("go_autostart", "",
209 i18nc("@action:inmenu Go", "Autostart"), KGlobalSettings::autostartPath(),
213 if (KAuthorized::authorizeKAction("shell_access")) {
214 KAction
* action
= actionCollection()->addAction("open_terminal");
215 action
->setIcon(KIcon("utilities-terminal"));
216 action
->setText(i18nc("@action:inmenu Tools", "Open &Terminal"));
217 connect(action
, SIGNAL(triggered()), SLOT(slotOpenTerminal()));
218 action
->setShortcut(Qt::Key_F4
);
223 void DolphinPart::createGoAction(const char* name
, const char* iconName
,
224 const QString
& text
, const QString
& url
,
225 QActionGroup
* actionGroup
)
227 KAction
* action
= actionCollection()->addAction(name
);
228 action
->setIcon(KIcon(iconName
));
229 action
->setText(text
);
230 action
->setData(url
);
231 action
->setActionGroup(actionGroup
);
234 void DolphinPart::slotGoTriggered(QAction
* action
)
236 const QString url
= action
->data().toString();
237 emit m_extension
->openUrlRequest(KUrl(url
));
240 void DolphinPart::slotSelectionChanged(const KFileItemList
& selection
)
242 const bool hasSelection
= !selection
.isEmpty();
244 QAction
* renameAction
= actionCollection()->action("rename");
245 QAction
* moveToTrashAction
= actionCollection()->action("move_to_trash");
246 QAction
* deleteAction
= actionCollection()->action("delete");
247 QAction
* editMimeTypeAction
= actionCollection()->action("editMimeType");
248 QAction
* propertiesAction
= actionCollection()->action("properties");
249 QAction
* deleteWithTrashShortcut
= actionCollection()->action("delete_shortcut"); // see DolphinViewActionHandler
252 stateChanged("has_no_selection");
254 emit m_extension
->enableAction("cut", false);
255 emit m_extension
->enableAction("copy", false);
256 deleteWithTrashShortcut
->setEnabled(false);
257 editMimeTypeAction
->setEnabled(false);
259 stateChanged("has_selection");
261 // TODO share this code with DolphinMainWindow::updateEditActions (and the desktop code)
263 KFileItemListProperties
capabilities(selection
);
264 const bool enableMoveToTrash
= capabilities
.isLocal() && capabilities
.supportsMoving();
266 renameAction
->setEnabled(capabilities
.supportsMoving());
267 moveToTrashAction
->setEnabled(enableMoveToTrash
);
268 deleteAction
->setEnabled(capabilities
.supportsDeleting());
269 deleteWithTrashShortcut
->setEnabled(capabilities
.supportsDeleting() && !enableMoveToTrash
);
270 editMimeTypeAction
->setEnabled(true);
271 propertiesAction
->setEnabled(true);
272 emit m_extension
->enableAction("cut", capabilities
.supportsMoving());
273 emit m_extension
->enableAction("copy", true);
277 void DolphinPart::updatePasteAction()
279 QPair
<bool, QString
> pasteInfo
= m_view
->pasteInfo();
280 emit m_extension
->enableAction( "paste", pasteInfo
.first
);
281 emit m_extension
->setActionText( "paste", pasteInfo
.second
);
284 KAboutData
* DolphinPart::createAboutData()
286 return new KAboutData("dolphinpart", "dolphin", ki18nc("@title", "Dolphin Part"), "0.1");
289 bool DolphinPart::openUrl(const KUrl
& url
)
291 bool reload
= arguments().reload();
292 // A bit of a workaround so that changing the namefilter works: force reload.
293 // Otherwise DolphinView wouldn't relist the URL, so nothing would happen.
294 if (m_nameFilter
!= m_dirLister
->nameFilter())
296 if (m_view
->url() == url
&& !reload
) { // DolphinView won't do anything in that case, so don't emit started
299 setUrl(url
); // remember it at the KParts level
300 KUrl
visibleUrl(url
);
301 if (!m_nameFilter
.isEmpty()) {
302 visibleUrl
.addPath(m_nameFilter
);
304 QString prettyUrl
= visibleUrl
.pathOrUrl();
305 emit
setWindowCaption(prettyUrl
);
306 emit m_extension
->setLocationBarUrl(prettyUrl
);
307 emit
started(0); // get the wheel to spin
308 m_dirLister
->setNameFilter(m_nameFilter
);
311 emit
aboutToOpenURL();
317 void DolphinPart::slotCompleted(const KUrl
& url
)
323 void DolphinPart::slotCanceled(const KUrl
& url
)
328 void DolphinPart::slotMessage(const QString
& msg
)
330 emit
setStatusBarText(msg
);
333 void DolphinPart::slotErrorMessage(const QString
& msg
)
335 KMessageBox::error(m_view
, msg
);
338 void DolphinPart::slotRequestItemInfo(const KFileItem
& item
)
340 emit m_extension
->mouseOverInfo(item
);
344 ReadOnlyPart::setStatusBarText(item
.getStatusBarInfo());
348 void DolphinPart::slotItemTriggered(const KFileItem
& item
)
350 KParts::OpenUrlArguments args
;
351 args
.setMimeType(item
.mimetype());
353 // Ideally, konqueror should be changed to not require trustedSource for directory views,
354 // since the idea was not to need BrowserArguments for non-browser stuff...
355 KParts::BrowserArguments browserArgs
;
356 browserArgs
.trustedSource
= true;
357 emit m_extension
->openUrlRequest(item
.targetUrl(), args
, browserArgs
);
360 void DolphinPart::createNewWindow(const KUrl
& url
)
362 // TODO: Check issue N176832 for the missing QAIV signal; task 177399 - maybe this code
363 // should be moved into DolphinPart::slotItemTriggered()
364 emit m_extension
->createNewWindow(url
);
367 void DolphinPart::slotOpenContextMenu(const KFileItem
& _item
,
369 const QList
<QAction
*>& customActions
)
371 KParts::BrowserExtension::PopupFlags popupFlags
= KParts::BrowserExtension::DefaultPopupItems
372 | KParts::BrowserExtension::ShowProperties
373 | KParts::BrowserExtension::ShowUrlOperations
;
375 KFileItem
item(_item
);
377 if (item
.isNull()) { // viewport context menu
378 popupFlags
|= KParts::BrowserExtension::ShowNavigationItems
| KParts::BrowserExtension::ShowUp
;
379 item
= m_dirLister
->rootItem();
381 item
= KFileItem( S_IFDIR
, (mode_t
)-1, url() );
383 item
.setUrl(url()); // ensure we use the view url, not the canonical path (#213799)
386 // TODO: We should change the signature of the slots (and signals) for being able
387 // to tell for which items we want a popup.
388 const KFileItemList items
= (m_view
->selectedItems().count() ? m_view
->selectedItems()
389 : KFileItemList() << item
);
390 KFileItemListProperties
capabilities(items
);
392 KParts::BrowserExtension::ActionGroupMap actionGroups
;
393 QList
<QAction
*> editActions
;
394 editActions
+= m_view
->versionControlActions(m_view
->selectedItems());
395 editActions
+= customActions
;
397 if (!_item
.isNull()) { // only for context menu on one or more items
398 bool supportsDeleting
= capabilities
.supportsDeleting();
399 bool supportsMoving
= capabilities
.supportsMoving();
401 if (!supportsDeleting
) {
402 popupFlags
|= KParts::BrowserExtension::NoDeletion
;
405 if (supportsMoving
) {
406 editActions
.append(actionCollection()->action("rename"));
409 bool addTrash
= capabilities
.isLocal() && supportsMoving
;
411 if (supportsDeleting
) {
412 if ( !item
.isLocalFile() )
414 else if (QApplication::keyboardModifiers() & Qt::ShiftModifier
) {
419 KSharedConfig::Ptr globalConfig
= KSharedConfig::openConfig("kdeglobals", KConfig::IncludeGlobals
);
420 KConfigGroup
configGroup(globalConfig
, "KDE");
421 addDel
= configGroup
.readEntry("ShowDeleteCommand", false);
426 editActions
.append(actionCollection()->action("move_to_trash"));
428 editActions
.append(actionCollection()->action("delete"));
430 // Normally KonqPopupMenu only shows the "Create new" submenu in the current view
431 // since otherwise the created file would not be visible.
432 // But in treeview mode we should allow it.
433 if (m_view
->itemsExpandable())
434 popupFlags
|= KParts::BrowserExtension::ShowCreateDirectory
;
438 actionGroups
.insert("editactions", editActions
);
440 emit m_extension
->popupMenu(QCursor::pos(),
442 KParts::OpenUrlArguments(),
443 KParts::BrowserArguments(),
448 void DolphinPart::slotRedirection(const KUrl
& oldUrl
, const KUrl
& newUrl
)
450 //kDebug() << oldUrl << newUrl << "currentUrl=" << url();
451 if (oldUrl
.equals(url(), KUrl::CompareWithoutTrailingSlash
/* #207572 */)) {
452 KParts::ReadOnlyPart::setUrl(newUrl
);
453 const QString prettyUrl
= newUrl
.pathOrUrl();
454 emit m_extension
->setLocationBarUrl(prettyUrl
);
458 void DolphinPart::slotRequestUrlChange(const KUrl
& url
)
460 if (m_view
->url() != url
) {
461 // If the view URL is not equal to 'url', then an inner URL change has
462 // been done (e. g. by activating an existing column in the column view).
464 emit m_extension
->openUrlNotify();
470 void DolphinPartBrowserExtension::restoreState(QDataStream
&stream
)
472 KParts::BrowserExtension::restoreState(stream
);
473 m_part
->view()->restoreState(stream
);
476 void DolphinPartBrowserExtension::saveState(QDataStream
&stream
)
478 KParts::BrowserExtension::saveState(stream
);
479 m_part
->view()->saveState(stream
);
482 void DolphinPartBrowserExtension::cut()
484 m_part
->view()->cutSelectedItems();
487 void DolphinPartBrowserExtension::copy()
489 m_part
->view()->copySelectedItems();
492 void DolphinPartBrowserExtension::paste()
494 m_part
->view()->paste();
497 void DolphinPartBrowserExtension::pasteTo(const KUrl
&)
499 m_part
->view()->pasteIntoFolder();
502 void DolphinPartBrowserExtension::reparseConfiguration()
504 m_part
->view()->refresh();
509 void DolphinPart::slotEditMimeType()
511 const KFileItemList items
= m_view
->selectedItems();
512 if (!items
.isEmpty()) {
513 KonqOperations::editMimeType(items
.first().mimetype(), m_view
);
517 void DolphinPart::slotSelectItemsMatchingPattern()
519 openSelectionDialog(i18nc("@title:window", "Select"),
520 i18n("Select all items matching this pattern:"),
521 QItemSelectionModel::Select
);
524 void DolphinPart::slotUnselectItemsMatchingPattern()
526 openSelectionDialog(i18nc("@title:window", "Unselect"),
527 i18n("Unselect all items matching this pattern:"),
528 QItemSelectionModel::Deselect
);
531 void DolphinPart::openSelectionDialog(const QString
& title
, const QString
& text
, QItemSelectionModel::SelectionFlags command
)
534 QString pattern
= KInputDialog::getText(title
, text
, "*", &okClicked
, m_view
);
536 if (okClicked
&& !pattern
.isEmpty()) {
537 QRegExp
patternRegExp(pattern
, Qt::CaseSensitive
, QRegExp::Wildcard
);
538 QItemSelection matchingIndexes
= childrenMatchingPattern(QModelIndex(), patternRegExp
);
539 m_view
->selectionModel()->select(matchingIndexes
, command
);
543 QItemSelection
DolphinPart::childrenMatchingPattern(const QModelIndex
& parent
, const QRegExp
& patternRegExp
)
545 QItemSelection matchingIndexes
;
546 int numRows
= m_proxyModel
->rowCount(parent
);
548 for (int row
= 0; row
< numRows
; row
++) {
549 QModelIndex index
= m_proxyModel
->index(row
, 0, parent
);
550 QModelIndex sourceIndex
= m_proxyModel
->mapToSource(index
);
552 if (sourceIndex
.isValid() && patternRegExp
.exactMatch(m_dolphinModel
->data(sourceIndex
).toString())) {
553 matchingIndexes
+= QItemSelectionRange(index
);
556 if (m_proxyModel
->hasChildren(index
)) {
557 matchingIndexes
+= childrenMatchingPattern(index
, patternRegExp
);
561 return matchingIndexes
;
564 void DolphinPart::setCurrentViewMode(const QString
& viewModeName
)
566 QAction
* action
= actionCollection()->action(viewModeName
);
571 QString
DolphinPart::currentViewMode() const
573 return m_actionHandler
->currentViewModeActionName();
576 void DolphinPart::setNameFilter(const QString
& nameFilter
)
578 // This is the "/home/dfaure/*.diff" kind of name filter (KDirLister::setNameFilter)
579 // which is unrelated to DolphinView::setNameFilter which is substring filtering in a proxy.
580 m_nameFilter
= nameFilter
;
581 // TODO save/restore name filter in saveState/restoreState like KonqDirPart did in kde3?
584 void DolphinPart::slotOpenTerminal()
586 QString
dir(QDir::homePath());
590 // If the given directory is not local, it can still be the URL of an
591 // ioslave using UDS_LOCAL_PATH which to be converted first.
592 u
= KIO::NetAccess::mostLocalUrl(u
, widget());
594 //If the URL is local after the above conversion, set the directory.
595 if (u
.isLocalFile()) {
596 dir
= u
.toLocalFile();
599 KToolInvocation::invokeTerminal(QString(), dir
);
602 void DolphinPart::updateNewMenu()
604 // As requested by KNewFileMenu :
605 m_newMenu
->checkUpToDate();
606 m_newMenu
->setViewShowsHiddenFiles(m_view
->showHiddenFiles());
607 // And set the files that the menu apply on :
608 m_newMenu
->setPopupFiles(url());
611 void DolphinPart::updateStatusBar()
613 emit
ReadOnlyPart::setStatusBarText(m_view
->statusBarText());
616 void DolphinPart::updateProgress(int percent
)
618 m_extension
->loadingProgress(percent
);
621 void DolphinPart::createDirectory()
623 m_newMenu
->setViewShowsHiddenFiles(m_view
->showHiddenFiles());
624 m_newMenu
->setPopupFiles(url());
625 m_newMenu
->createDirectory();
628 #include "dolphinpart.moc"