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"
26 #include <konq_fileitemcapabilities.h>
27 #include <konq_operations.h>
29 #include <kaboutdata.h>
30 #include <kactioncollection.h>
31 #include <kconfiggroup.h>
32 #include <kdirlister.h>
33 #include <kglobalsettings.h>
34 #include <kiconloader.h>
36 #include <kmessagebox.h>
37 #include <kpluginfactory.h>
38 #include <kpropertiesdialog.h>
39 #include <ktoggleaction.h>
40 #include <kio/netaccess.h>
41 #include <ktoolinvocation.h>
42 #include <kauthorized.h>
46 #include <QActionGroup>
47 #include <QApplication>
50 K_PLUGIN_FACTORY(DolphinPartFactory
, registerPlugin
<DolphinPart
>();)
51 K_EXPORT_PLUGIN(DolphinPartFactory("dolphinpart", "dolphin"))
53 DolphinPart::DolphinPart(QWidget
* parentWidget
, QObject
* parent
, const QVariantList
& args
)
54 : KParts::ReadOnlyPart(parent
)
57 setComponentData(DolphinPartFactory::componentData(), false);
58 m_extension
= new DolphinPartBrowserExtension(this);
60 // make sure that other apps using this part find Dolphin's view-file-columns icons
61 KIconLoader::global()->addAppDir("dolphin");
63 m_dirLister
= new KDirLister
;
64 m_dirLister
->setAutoUpdate(true);
65 m_dirLister
->setMainWindow(parentWidget
->window());
66 m_dirLister
->setDelayedMimeTypes(true);
68 //connect(m_dirLister, SIGNAL(started(KUrl)), this, SLOT(slotStarted()));
69 connect(m_dirLister
, SIGNAL(completed(KUrl
)), this, SLOT(slotCompleted(KUrl
)));
70 connect(m_dirLister
, SIGNAL(canceled(KUrl
)), this, SLOT(slotCanceled(KUrl
)));
72 m_dolphinModel
= new DolphinModel(this);
73 m_dolphinModel
->setDirLister(m_dirLister
);
75 m_proxyModel
= new DolphinSortFilterProxyModel(this);
76 m_proxyModel
->setSourceModel(m_dolphinModel
);
78 m_view
= new DolphinView(parentWidget
,
83 m_view
->setTabsForFilesEnabled(true);
86 setXMLFile("dolphinpart.rc");
88 connect(m_view
, SIGNAL(infoMessage(QString
)),
89 this, SLOT(slotInfoMessage(QString
)));
90 connect(m_view
, SIGNAL(errorMessage(QString
)),
91 this, SLOT(slotErrorMessage(QString
)));
92 connect(m_view
, SIGNAL(itemTriggered(KFileItem
)),
93 this, SLOT(slotItemTriggered(KFileItem
)));
94 connect(m_view
, SIGNAL(tabRequested(KUrl
)),
95 this, SLOT(createNewWindow(KUrl
)));
96 connect(m_view
, SIGNAL(requestContextMenu(KFileItem
,KUrl
)),
97 this, SLOT(slotOpenContextMenu(KFileItem
,KUrl
)));
98 connect(m_view
, SIGNAL(selectionChanged(KFileItemList
)),
99 m_extension
, SIGNAL(selectionInfo(KFileItemList
)));
100 connect(m_view
, SIGNAL(selectionChanged(KFileItemList
)),
101 this, SLOT(slotSelectionChanged(KFileItemList
)));
102 connect(m_view
, SIGNAL(requestItemInfo(KFileItem
)),
103 this, SLOT(slotRequestItemInfo(KFileItem
)));
104 connect(m_view
, SIGNAL(urlChanged(KUrl
)),
105 this, SLOT(slotUrlChanged(KUrl
)));
106 connect(m_view
, SIGNAL(requestUrlChange(KUrl
)),
107 this, SLOT(slotRequestUrlChange(KUrl
)));
108 connect(m_view
, SIGNAL(modeChanged()),
109 this, SIGNAL(viewModeChanged())); // relay signal
111 m_actionHandler
= new DolphinViewActionHandler(actionCollection(), this);
112 m_actionHandler
->setCurrentView(m_view
);
114 QClipboard
* clipboard
= QApplication::clipboard();
115 connect(clipboard
, SIGNAL(dataChanged()),
116 this, SLOT(updatePasteAction()));
119 m_actionHandler
->updateViewActions();
120 slotSelectionChanged(KFileItemList()); // initially disable selection-dependent actions
122 // TODO there was a "always open a new window" (when clicking on a directory) setting in konqueror
123 // (sort of spacial navigation)
125 loadPlugins(this, this, componentData());
128 DolphinPart::~DolphinPart()
133 void DolphinPart::createActions()
137 m_newMenu
= new KNewMenu(actionCollection(), widget(), "new_menu");
138 connect(m_newMenu
->menu(), SIGNAL(aboutToShow()),
139 this, SLOT(updateNewMenu()));
141 KAction
*editMimeTypeAction
= actionCollection()->addAction( "editMimeType" );
142 editMimeTypeAction
->setText( i18nc("@action:inmenu Edit", "&Edit File Type..." ) );
143 connect(editMimeTypeAction
, SIGNAL(triggered()), SLOT(slotEditMimeType()));
145 KAction
*propertiesAction
= actionCollection()->addAction( "properties" );
146 propertiesAction
->setText( i18nc("@action:inmenu Edit", "Properties") );
147 propertiesAction
->setShortcut(Qt::ALT
+Qt::Key_Return
);
148 connect(propertiesAction
, SIGNAL(triggered()), SLOT(slotProperties()));
150 // View menu: all done by DolphinViewActionHandler
154 QActionGroup
* goActionGroup
= new QActionGroup(this);
155 connect(goActionGroup
, SIGNAL(triggered(QAction
*)),
156 this, SLOT(slotGoTriggered(QAction
*)));
158 createGoAction("go_applications", "start-here-kde",
159 i18nc("@action:inmenu Go", "App&lications"), QString("programs:/"),
161 createGoAction("go_network_folders", "folder-remote",
162 i18nc("@action:inmenu Go", "&Network Folders"), QString("remote:/"),
164 createGoAction("go_settings", "preferences-system",
165 i18nc("@action:inmenu Go", "Sett&ings"), QString("settings:/"),
167 createGoAction("go_trash", "user-trash",
168 i18nc("@action:inmenu Go", "Trash"), QString("trash:/"),
170 createGoAction("go_autostart", "",
171 i18nc("@action:inmenu Go", "Autostart"), KGlobalSettings::autostartPath(),
175 if (KAuthorized::authorizeKAction("shell_access")) {
176 KAction
* action
= actionCollection()->addAction("open_terminal");
177 action
->setIcon(KIcon("utilities-terminal"));
178 action
->setText(i18nc("@action:inmenu Tools", "Open &Terminal"));
179 connect(action
, SIGNAL(triggered()), SLOT(slotOpenTerminal()));
180 action
->setShortcut(Qt::Key_F4
);
185 void DolphinPart::createGoAction(const char* name
, const char* iconName
,
186 const QString
& text
, const QString
& url
,
187 QActionGroup
* actionGroup
)
189 KAction
* action
= actionCollection()->addAction(name
);
190 action
->setIcon(KIcon(iconName
));
191 action
->setText(text
);
192 action
->setData(url
);
193 action
->setActionGroup(actionGroup
);
196 void DolphinPart::slotGoTriggered(QAction
* action
)
198 const QString url
= action
->data().toString();
199 emit m_extension
->openUrlRequest(KUrl(url
));
202 void DolphinPart::slotSelectionChanged(const KFileItemList
& selection
)
204 const bool hasSelection
= !selection
.isEmpty();
206 QAction
* renameAction
= actionCollection()->action("rename");
207 QAction
* moveToTrashAction
= actionCollection()->action("move_to_trash");
208 QAction
* deleteAction
= actionCollection()->action("delete");
209 QAction
* editMimeTypeAction
= actionCollection()->action("editMimeType");
210 QAction
* propertiesAction
= actionCollection()->action("properties");
211 QAction
* deleteWithTrashShortcut
= actionCollection()->action("delete_shortcut"); // see DolphinViewActionHandler
214 stateChanged("has_no_selection");
216 emit m_extension
->enableAction("cut", false);
217 emit m_extension
->enableAction("copy", false);
218 deleteWithTrashShortcut
->setEnabled(false);
219 editMimeTypeAction
->setEnabled(false);
221 stateChanged("has_selection");
223 // TODO share this code with DolphinMainWindow::updateEditActions (and the desktop code)
225 KonqFileItemCapabilities
capabilities(selection
);
226 const bool enableMoveToTrash
= capabilities
.isLocal() && capabilities
.supportsMoving();
228 renameAction
->setEnabled(capabilities
.supportsMoving());
229 moveToTrashAction
->setEnabled(enableMoveToTrash
);
230 deleteAction
->setEnabled(capabilities
.supportsDeleting());
231 deleteWithTrashShortcut
->setEnabled(capabilities
.supportsDeleting() && !enableMoveToTrash
);
232 editMimeTypeAction
->setEnabled(true);
233 propertiesAction
->setEnabled(true);
234 emit m_extension
->enableAction("cut", capabilities
.supportsMoving());
235 emit m_extension
->enableAction("copy", true);
239 void DolphinPart::updatePasteAction()
241 QPair
<bool, QString
> pasteInfo
= m_view
->pasteInfo();
242 emit m_extension
->enableAction( "paste", pasteInfo
.first
);
243 emit m_extension
->setActionText( "paste", pasteInfo
.second
);
246 KAboutData
* DolphinPart::createAboutData()
248 return new KAboutData("dolphinpart", "dolphin", ki18nc("@title", "Dolphin Part"), "0.1");
251 bool DolphinPart::openUrl(const KUrl
& url
)
253 bool reload
= arguments().reload();
254 // A bit of a workaround so that changing the namefilter works: force reload.
255 // Otherwise DolphinView wouldn't relist the URL, so nothing would happen.
256 if (m_nameFilter
!= m_dirLister
->nameFilter())
258 if (m_view
->url() == url
&& !reload
) { // DolphinView won't do anything in that case, so don't emit started
261 setUrl(url
); // remember it at the KParts level
262 KUrl
visibleUrl(url
);
263 if (!m_nameFilter
.isEmpty()) {
264 visibleUrl
.addPath(m_nameFilter
);
266 QString prettyUrl
= visibleUrl
.pathOrUrl();
267 emit
setWindowCaption(prettyUrl
);
268 emit m_extension
->setLocationBarUrl(prettyUrl
);
269 emit
started(0); // get the wheel to spin
270 m_dirLister
->setNameFilter(m_nameFilter
);
272 emit
aboutToOpenURL();
278 void DolphinPart::slotCompleted(const KUrl
& url
)
284 void DolphinPart::slotCanceled(const KUrl
& url
)
289 void DolphinPart::slotInfoMessage(const QString
& msg
)
291 emit
setStatusBarText(msg
);
294 void DolphinPart::slotErrorMessage(const QString
& msg
)
296 KMessageBox::error(m_view
, msg
);
299 void DolphinPart::slotRequestItemInfo(const KFileItem
& item
)
301 emit m_extension
->mouseOverInfo(item
);
304 void DolphinPart::slotItemTriggered(const KFileItem
& item
)
306 KParts::OpenUrlArguments args
;
307 args
.setMimeType(item
.mimetype());
309 // Ideally, konqueror should be changed to not require trustedSource for directory views,
310 // since the idea was not to need BrowserArguments for non-browser stuff...
311 KParts::BrowserArguments browserArgs
;
312 browserArgs
.trustedSource
= true;
313 emit m_extension
->openUrlRequest(item
.targetUrl(), args
, browserArgs
);
316 void DolphinPart::createNewWindow(const KUrl
& url
)
318 // TODO: Check issue N176832 for the missing QAIV signal; task 177399 - maybe this code
319 // should be moved into DolphinPart::slotItemTriggered()
320 KFileItem
item(S_IFDIR
, (mode_t
)-1, url
);
321 KParts::OpenUrlArguments args
;
322 args
.setMimeType(item
.mimetype());
323 emit m_extension
->createNewWindow(url
, args
);
326 void DolphinPart::slotOpenContextMenu(const KFileItem
& _item
, const KUrl
&)
328 KParts::BrowserExtension::PopupFlags popupFlags
= KParts::BrowserExtension::DefaultPopupItems
329 | KParts::BrowserExtension::ShowProperties
330 | KParts::BrowserExtension::ShowUrlOperations
;
332 KFileItem
item(_item
);
334 if (item
.isNull()) { // viewport context menu
335 popupFlags
|= KParts::BrowserExtension::ShowNavigationItems
| KParts::BrowserExtension::ShowUp
;
336 item
= m_dirLister
->rootItem();
338 item
= KFileItem( S_IFDIR
, (mode_t
)-1, url() );
341 KParts::BrowserExtension::ActionGroupMap actionGroups
;
342 QList
<QAction
*> editActions
;
344 if (!_item
.isNull()) { // only for context menu on one or more items
345 bool sDeleting
= true;
348 // If the parent directory of the selected item is writable, moving
349 // and deleting are possible.
350 KFileItem parentDir
= m_dirLister
->rootItem();
351 if (!parentDir
.isNull() && !parentDir
.isWritable()) {
352 popupFlags
|= KParts::BrowserExtension::NoDeletion
;
358 editActions
.append(actionCollection()->action("rename"));
360 bool addTrash
= false;
363 bool isIntoTrash
= _item
.url().protocol() == "trash";
365 if ( sMoving
&& !isIntoTrash
&& item
.isLocalFile() )
369 if ( !item
.isLocalFile() )
371 else if (QApplication::keyboardModifiers() & Qt::ShiftModifier
) {
376 KConfigGroup
configGroup( KGlobal::config(), "KDE" );
377 if ( configGroup
.readEntry( "ShowDeleteCommand", false) )
383 editActions
.append(actionCollection()->action("move_to_trash"));
385 editActions
.append(actionCollection()->action("delete"));
386 actionGroups
.insert("editactions", editActions
);
389 // TODO: We should change the signature of the slots (and signals) for being able
390 // to tell for which items we want a popup.
391 KFileItemList items
= (m_view
->selectedItems().count() ? m_view
->selectedItems()
392 : KFileItemList() << item
);
393 emit m_extension
->popupMenu(QCursor::pos(),
395 KParts::OpenUrlArguments(),
396 KParts::BrowserArguments(),
401 void DolphinPart::slotUrlChanged(const KUrl
& url
)
403 KParts::ReadOnlyPart::setUrl(url
);
404 QString prettyUrl
= url
.pathOrUrl();
405 emit m_extension
->setLocationBarUrl(prettyUrl
);
408 void DolphinPart::slotRequestUrlChange(const KUrl
& url
)
410 if (m_view
->url() != url
) {
411 // If the view URL is not equal to 'url', then an inner URL change has
412 // been done (e. g. by activating an existing column in the column view).
414 emit m_extension
->openUrlNotify();
420 void DolphinPartBrowserExtension::cut()
422 m_part
->view()->cutSelectedItems();
425 void DolphinPartBrowserExtension::copy()
427 m_part
->view()->copySelectedItems();
430 void DolphinPartBrowserExtension::paste()
432 m_part
->view()->paste();
435 void DolphinPartBrowserExtension::reparseConfiguration()
437 m_part
->view()->refresh();
442 void DolphinPart::slotEditMimeType()
444 const KFileItemList items
= m_view
->selectedItems();
445 if (!items
.isEmpty()) {
446 KonqOperations::editMimeType(items
.first().mimetype(), m_view
);
450 void DolphinPart::slotProperties()
452 const KFileItemList items
= m_view
->selectedItems();
453 if (!items
.isEmpty()) {
454 KPropertiesDialog
dialog(items
.first().url(), m_view
);
459 void DolphinPart::setCurrentViewMode(const QString
& viewModeName
)
461 QAction
* action
= actionCollection()->action(viewModeName
);
466 QString
DolphinPart::currentViewMode() const
468 return m_actionHandler
->currentViewModeActionName();
471 void DolphinPart::setNameFilter(const QString
& nameFilter
)
473 // This is the "/home/dfaure/*.diff" kind of name filter (KDirLister::setNameFilter)
474 // which is unrelated to DolphinView::setNameFilter which is substring filtering in a proxy.
475 m_nameFilter
= nameFilter
;
476 // TODO save/restore name filter in saveState/restoreState like KonqDirPart did in kde3?
479 void DolphinPart::slotOpenTerminal()
481 QString
dir(QDir::homePath());
485 // If the given directory is not local, it can still be the URL of an
486 // ioslave using UDS_LOCAL_PATH which to be converted first.
487 u
= KIO::NetAccess::mostLocalUrl(u
, widget());
489 //If the URL is local after the above conversion, set the directory.
490 if (u
.isLocalFile()) {
494 KToolInvocation::invokeTerminal(QString(), dir
);
497 void DolphinPart::updateNewMenu()
499 // As requested by KNewMenu :
500 m_newMenu
->slotCheckUpToDate();
501 // And set the files that the menu apply on :
502 m_newMenu
->setPopupFiles(url());
505 #include "dolphinpart.moc"