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>
33 #include <kdirlister.h>
34 #include <kglobalsettings.h>
35 #include <kiconloader.h>
37 #include <kmessagebox.h>
38 #include <kpluginfactory.h>
39 #include <kpropertiesdialog.h>
40 #include <ktoggleaction.h>
41 #include <kio/netaccess.h>
42 #include <ktoolinvocation.h>
43 #include <kauthorized.h>
47 #include <QActionGroup>
48 #include <QApplication>
51 K_PLUGIN_FACTORY(DolphinPartFactory
, registerPlugin
<DolphinPart
>();)
52 // The componentdata name must be dolphinpart so that dolphinpart.rc is found
53 // Alternatively we would have to install it as dolphin/dolphinpart.rc
54 K_EXPORT_PLUGIN(DolphinPartFactory("dolphinpart"))
56 DolphinPart::DolphinPart(QWidget
* parentWidget
, QObject
* parent
, const QVariantList
& args
)
57 : KParts::ReadOnlyPart(parent
)
60 setComponentData(DolphinPartFactory::componentData(), false);
61 m_extension
= new DolphinPartBrowserExtension(this);
63 // make sure that other apps using this part find Dolphin's view-file-columns icons
64 KIconLoader::global()->addAppDir("dolphin");
66 m_dirLister
= new KDirLister
;
67 m_dirLister
->setAutoUpdate(true);
68 m_dirLister
->setMainWindow(parentWidget
->window());
69 m_dirLister
->setDelayedMimeTypes(true);
71 //connect(m_dirLister, SIGNAL(started(KUrl)), this, SLOT(slotStarted()));
72 connect(m_dirLister
, SIGNAL(completed(KUrl
)), this, SLOT(slotCompleted(KUrl
)));
73 connect(m_dirLister
, SIGNAL(canceled(KUrl
)), this, SLOT(slotCanceled(KUrl
)));
75 m_dolphinModel
= new DolphinModel(this);
76 m_dolphinModel
->setDirLister(m_dirLister
);
78 m_proxyModel
= new DolphinSortFilterProxyModel(this);
79 m_proxyModel
->setSourceModel(m_dolphinModel
);
81 m_view
= new DolphinView(parentWidget
,
86 m_view
->setTabsForFilesEnabled(true);
89 setXMLFile("dolphinpart.rc");
91 connect(m_view
, SIGNAL(infoMessage(QString
)),
92 this, SLOT(slotInfoMessage(QString
)));
93 connect(m_view
, SIGNAL(errorMessage(QString
)),
94 this, SLOT(slotErrorMessage(QString
)));
95 connect(m_view
, SIGNAL(itemTriggered(KFileItem
)),
96 this, SLOT(slotItemTriggered(KFileItem
)));
97 connect(m_view
, SIGNAL(tabRequested(KUrl
)),
98 this, SLOT(createNewWindow(KUrl
)));
99 connect(m_view
, SIGNAL(requestContextMenu(KFileItem
,KUrl
)),
100 this, SLOT(slotOpenContextMenu(KFileItem
,KUrl
)));
101 connect(m_view
, SIGNAL(selectionChanged(KFileItemList
)),
102 m_extension
, SIGNAL(selectionInfo(KFileItemList
)));
103 connect(m_view
, SIGNAL(selectionChanged(KFileItemList
)),
104 this, SLOT(slotSelectionChanged(KFileItemList
)));
105 connect(m_view
, SIGNAL(requestItemInfo(KFileItem
)),
106 this, SLOT(slotRequestItemInfo(KFileItem
)));
107 connect(m_view
, SIGNAL(urlChanged(KUrl
)),
108 this, SLOT(slotUrlChanged(KUrl
)));
109 connect(m_view
, SIGNAL(requestUrlChange(KUrl
)),
110 this, SLOT(slotRequestUrlChange(KUrl
)));
111 connect(m_view
, SIGNAL(modeChanged()),
112 this, SIGNAL(viewModeChanged())); // relay signal
114 m_actionHandler
= new DolphinViewActionHandler(actionCollection(), this);
115 m_actionHandler
->setCurrentView(m_view
);
117 QClipboard
* clipboard
= QApplication::clipboard();
118 connect(clipboard
, SIGNAL(dataChanged()),
119 this, SLOT(updatePasteAction()));
122 m_actionHandler
->updateViewActions();
123 slotSelectionChanged(KFileItemList()); // initially disable selection-dependent actions
125 // TODO there was a "always open a new window" (when clicking on a directory) setting in konqueror
126 // (sort of spacial navigation)
128 loadPlugins(this, this, componentData());
131 DolphinPart::~DolphinPart()
136 void DolphinPart::createActions()
140 m_newMenu
= new KNewMenu(actionCollection(), widget(), "new_menu");
141 connect(m_newMenu
->menu(), SIGNAL(aboutToShow()),
142 this, SLOT(updateNewMenu()));
144 KAction
*editMimeTypeAction
= actionCollection()->addAction( "editMimeType" );
145 editMimeTypeAction
->setText( i18nc("@action:inmenu Edit", "&Edit File Type..." ) );
146 connect(editMimeTypeAction
, SIGNAL(triggered()), SLOT(slotEditMimeType()));
148 KAction
*propertiesAction
= actionCollection()->addAction( "properties" );
149 propertiesAction
->setText( i18nc("@action:inmenu Edit", "Properties") );
150 propertiesAction
->setShortcut(Qt::ALT
+Qt::Key_Return
);
151 connect(propertiesAction
, SIGNAL(triggered()), SLOT(slotProperties()));
153 // View menu: all done by DolphinViewActionHandler
157 QActionGroup
* goActionGroup
= new QActionGroup(this);
158 connect(goActionGroup
, SIGNAL(triggered(QAction
*)),
159 this, SLOT(slotGoTriggered(QAction
*)));
161 createGoAction("go_applications", "start-here-kde",
162 i18nc("@action:inmenu Go", "App&lications"), QString("programs:/"),
164 createGoAction("go_network_folders", "folder-remote",
165 i18nc("@action:inmenu Go", "&Network Folders"), QString("remote:/"),
167 createGoAction("go_settings", "preferences-system",
168 i18nc("@action:inmenu Go", "Sett&ings"), QString("settings:/"),
170 createGoAction("go_trash", "user-trash",
171 i18nc("@action:inmenu Go", "Trash"), QString("trash:/"),
173 createGoAction("go_autostart", "",
174 i18nc("@action:inmenu Go", "Autostart"), KGlobalSettings::autostartPath(),
178 if (KAuthorized::authorizeKAction("shell_access")) {
179 KAction
* action
= actionCollection()->addAction("open_terminal");
180 action
->setIcon(KIcon("utilities-terminal"));
181 action
->setText(i18nc("@action:inmenu Tools", "Open &Terminal"));
182 connect(action
, SIGNAL(triggered()), SLOT(slotOpenTerminal()));
183 action
->setShortcut(Qt::Key_F4
);
188 void DolphinPart::createGoAction(const char* name
, const char* iconName
,
189 const QString
& text
, const QString
& url
,
190 QActionGroup
* actionGroup
)
192 KAction
* action
= actionCollection()->addAction(name
);
193 action
->setIcon(KIcon(iconName
));
194 action
->setText(text
);
195 action
->setData(url
);
196 action
->setActionGroup(actionGroup
);
199 void DolphinPart::slotGoTriggered(QAction
* action
)
201 const QString url
= action
->data().toString();
202 emit m_extension
->openUrlRequest(KUrl(url
));
205 void DolphinPart::slotSelectionChanged(const KFileItemList
& selection
)
207 const bool hasSelection
= !selection
.isEmpty();
209 QAction
* renameAction
= actionCollection()->action("rename");
210 QAction
* moveToTrashAction
= actionCollection()->action("move_to_trash");
211 QAction
* deleteAction
= actionCollection()->action("delete");
212 QAction
* editMimeTypeAction
= actionCollection()->action("editMimeType");
213 QAction
* propertiesAction
= actionCollection()->action("properties");
214 QAction
* deleteWithTrashShortcut
= actionCollection()->action("delete_shortcut"); // see DolphinViewActionHandler
217 stateChanged("has_no_selection");
219 emit m_extension
->enableAction("cut", false);
220 emit m_extension
->enableAction("copy", false);
221 deleteWithTrashShortcut
->setEnabled(false);
222 editMimeTypeAction
->setEnabled(false);
224 stateChanged("has_selection");
226 // TODO share this code with DolphinMainWindow::updateEditActions (and the desktop code)
228 KonqFileItemCapabilities
capabilities(selection
);
229 const bool enableMoveToTrash
= capabilities
.isLocal() && capabilities
.supportsMoving();
231 renameAction
->setEnabled(capabilities
.supportsMoving());
232 moveToTrashAction
->setEnabled(enableMoveToTrash
);
233 deleteAction
->setEnabled(capabilities
.supportsDeleting());
234 deleteWithTrashShortcut
->setEnabled(capabilities
.supportsDeleting() && !enableMoveToTrash
);
235 editMimeTypeAction
->setEnabled(true);
236 propertiesAction
->setEnabled(true);
237 emit m_extension
->enableAction("cut", capabilities
.supportsMoving());
238 emit m_extension
->enableAction("copy", true);
242 void DolphinPart::updatePasteAction()
244 QPair
<bool, QString
> pasteInfo
= m_view
->pasteInfo();
245 emit m_extension
->enableAction( "paste", pasteInfo
.first
);
246 emit m_extension
->setActionText( "paste", pasteInfo
.second
);
249 KAboutData
* DolphinPart::createAboutData()
251 return new KAboutData("dolphinpart", "dolphin", ki18nc("@title", "Dolphin Part"), "0.1");
254 bool DolphinPart::openUrl(const KUrl
& url
)
256 bool reload
= arguments().reload();
257 // A bit of a workaround so that changing the namefilter works: force reload.
258 // Otherwise DolphinView wouldn't relist the URL, so nothing would happen.
259 if (m_nameFilter
!= m_dirLister
->nameFilter())
261 if (m_view
->url() == url
&& !reload
) { // DolphinView won't do anything in that case, so don't emit started
264 setUrl(url
); // remember it at the KParts level
265 KUrl
visibleUrl(url
);
266 if (!m_nameFilter
.isEmpty()) {
267 visibleUrl
.addPath(m_nameFilter
);
269 QString prettyUrl
= visibleUrl
.pathOrUrl();
270 emit
setWindowCaption(prettyUrl
);
271 emit m_extension
->setLocationBarUrl(prettyUrl
);
272 emit
started(0); // get the wheel to spin
273 m_dirLister
->setNameFilter(m_nameFilter
);
275 emit
aboutToOpenURL();
281 void DolphinPart::slotCompleted(const KUrl
& url
)
287 void DolphinPart::slotCanceled(const KUrl
& url
)
292 void DolphinPart::slotInfoMessage(const QString
& msg
)
294 emit
setStatusBarText(msg
);
297 void DolphinPart::slotErrorMessage(const QString
& msg
)
299 KMessageBox::error(m_view
, msg
);
302 void DolphinPart::slotRequestItemInfo(const KFileItem
& item
)
304 emit m_extension
->mouseOverInfo(item
);
307 void DolphinPart::slotItemTriggered(const KFileItem
& item
)
309 KParts::OpenUrlArguments args
;
310 args
.setMimeType(item
.mimetype());
312 // Ideally, konqueror should be changed to not require trustedSource for directory views,
313 // since the idea was not to need BrowserArguments for non-browser stuff...
314 KParts::BrowserArguments browserArgs
;
315 browserArgs
.trustedSource
= true;
316 emit m_extension
->openUrlRequest(item
.targetUrl(), args
, browserArgs
);
319 void DolphinPart::createNewWindow(const KUrl
& url
)
321 // TODO: Check issue N176832 for the missing QAIV signal; task 177399 - maybe this code
322 // should be moved into DolphinPart::slotItemTriggered()
323 KFileItem
item(S_IFDIR
, (mode_t
)-1, url
);
324 KParts::OpenUrlArguments args
;
325 args
.setMimeType(item
.mimetype());
326 emit m_extension
->createNewWindow(url
, args
);
329 void DolphinPart::slotOpenContextMenu(const KFileItem
& _item
, const KUrl
&)
331 KParts::BrowserExtension::PopupFlags popupFlags
= KParts::BrowserExtension::DefaultPopupItems
332 | KParts::BrowserExtension::ShowProperties
333 | KParts::BrowserExtension::ShowUrlOperations
;
335 KFileItem
item(_item
);
337 if (item
.isNull()) { // viewport context menu
338 popupFlags
|= KParts::BrowserExtension::ShowNavigationItems
| KParts::BrowserExtension::ShowUp
;
339 item
= m_dirLister
->rootItem();
341 item
= KFileItem( S_IFDIR
, (mode_t
)-1, url() );
344 KParts::BrowserExtension::ActionGroupMap actionGroups
;
345 QList
<QAction
*> editActions
;
347 if (!_item
.isNull()) { // only for context menu on one or more items
348 bool sDeleting
= true;
351 // If the parent directory of the selected item is writable, moving
352 // and deleting are possible.
353 KFileItem parentDir
= m_dirLister
->rootItem();
354 if (!parentDir
.isNull() && !parentDir
.isWritable()) {
355 popupFlags
|= KParts::BrowserExtension::NoDeletion
;
361 editActions
.append(actionCollection()->action("rename"));
363 bool addTrash
= false;
366 bool isIntoTrash
= _item
.url().protocol() == "trash";
368 if ( sMoving
&& !isIntoTrash
&& item
.isLocalFile() )
372 if ( !item
.isLocalFile() )
374 else if (QApplication::keyboardModifiers() & Qt::ShiftModifier
) {
379 KConfigGroup
configGroup( KGlobal::config(), "KDE" );
380 if ( configGroup
.readEntry( "ShowDeleteCommand", false) )
386 editActions
.append(actionCollection()->action("move_to_trash"));
388 editActions
.append(actionCollection()->action("delete"));
389 actionGroups
.insert("editactions", editActions
);
392 // TODO: We should change the signature of the slots (and signals) for being able
393 // to tell for which items we want a popup.
394 KFileItemList items
= (m_view
->selectedItems().count() ? m_view
->selectedItems()
395 : KFileItemList() << item
);
396 emit m_extension
->popupMenu(QCursor::pos(),
398 KParts::OpenUrlArguments(),
399 KParts::BrowserArguments(),
404 void DolphinPart::slotUrlChanged(const KUrl
& url
)
406 QString prettyUrl
= url
.pathOrUrl();
407 emit m_extension
->setLocationBarUrl(prettyUrl
);
410 void DolphinPart::slotRequestUrlChange(const KUrl
& url
)
412 if (m_view
->url() != url
) {
413 // If the view URL is not equal to 'url', then an inner URL change has
414 // been done (e. g. by activating an existing column in the column view).
416 emit m_extension
->openUrlNotify();
422 void DolphinPartBrowserExtension::cut()
424 m_part
->view()->cutSelectedItems();
427 void DolphinPartBrowserExtension::copy()
429 m_part
->view()->copySelectedItems();
432 void DolphinPartBrowserExtension::paste()
434 m_part
->view()->paste();
437 void DolphinPartBrowserExtension::reparseConfiguration()
439 m_part
->view()->refresh();
444 void DolphinPart::slotEditMimeType()
446 const KFileItemList items
= m_view
->selectedItems();
447 if (!items
.isEmpty()) {
448 KonqOperations::editMimeType(items
.first().mimetype(), m_view
);
452 void DolphinPart::slotProperties()
454 const KFileItemList items
= m_view
->selectedItems();
455 if (!items
.isEmpty()) {
456 KPropertiesDialog
dialog(items
.first().url(), m_view
);
461 void DolphinPart::setCurrentViewMode(const QString
& viewModeName
)
463 QAction
* action
= actionCollection()->action(viewModeName
);
468 QString
DolphinPart::currentViewMode() const
470 return m_actionHandler
->currentViewModeActionName();
473 void DolphinPart::setNameFilter(const QString
& nameFilter
)
475 // This is the "/home/dfaure/*.diff" kind of name filter (KDirLister::setNameFilter)
476 // which is unrelated to DolphinView::setNameFilter which is substring filtering in a proxy.
477 m_nameFilter
= nameFilter
;
478 // TODO save/restore name filter in saveState/restoreState like KonqDirPart did in kde3?
481 void DolphinPart::slotOpenTerminal()
483 QString
dir(QDir::homePath());
487 // If the given directory is not local, it can still be the URL of an
488 // ioslave using UDS_LOCAL_PATH which to be converted first.
489 u
= KIO::NetAccess::mostLocalUrl(u
, widget());
491 //If the URL is local after the above conversion, set the directory.
492 if (u
.isLocalFile()) {
496 KToolInvocation::invokeTerminal(QString(), dir
);
499 void DolphinPart::updateNewMenu()
501 // As requested by KNewMenu :
502 m_newMenu
->slotCheckUpToDate();
503 // And set the files that the menu apply on :
504 m_newMenu
->setPopupFiles(url());
507 #include "dolphinpart.moc"