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>
42 #include <QActionGroup>
43 #include <QApplication>
46 K_PLUGIN_FACTORY(DolphinPartFactory
, registerPlugin
<DolphinPart
>();)
47 // The componentdata name must be dolphinpart so that dolphinpart.rc is found
48 // Alternatively we would have to install it as dolphin/dolphinpart.rc
49 K_EXPORT_PLUGIN(DolphinPartFactory("dolphinpart"))
51 DolphinPart::DolphinPart(QWidget
* parentWidget
, QObject
* parent
, const QVariantList
& args
)
52 : KParts::ReadOnlyPart(parent
)
55 setComponentData(DolphinPartFactory::componentData(), false);
56 m_extension
= new DolphinPartBrowserExtension(this);
58 // make sure that other apps using this part find Dolphin's view-file-columns icons
59 KIconLoader::global()->addAppDir("dolphin");
61 m_dirLister
= new KDirLister
;
62 m_dirLister
->setAutoUpdate(true);
63 m_dirLister
->setMainWindow(parentWidget
->window());
64 m_dirLister
->setDelayedMimeTypes(true);
66 //connect(m_dirLister, SIGNAL(started(KUrl)), this, SLOT(slotStarted()));
67 connect(m_dirLister
, SIGNAL(completed(KUrl
)), this, SLOT(slotCompleted(KUrl
)));
68 connect(m_dirLister
, SIGNAL(canceled(KUrl
)), this, SLOT(slotCanceled(KUrl
)));
70 m_dolphinModel
= new DolphinModel(this);
71 m_dolphinModel
->setDirLister(m_dirLister
);
73 m_proxyModel
= new DolphinSortFilterProxyModel(this);
74 m_proxyModel
->setSourceModel(m_dolphinModel
);
76 m_view
= new DolphinView(parentWidget
,
81 m_view
->setTabsForFilesEnabled(true);
84 setXMLFile("dolphinpart.rc");
86 connect(m_view
, SIGNAL(infoMessage(QString
)),
87 this, SLOT(slotInfoMessage(QString
)));
88 connect(m_view
, SIGNAL(errorMessage(QString
)),
89 this, SLOT(slotErrorMessage(QString
)));
90 connect(m_view
, SIGNAL(itemTriggered(KFileItem
)),
91 this, SLOT(slotItemTriggered(KFileItem
)));
92 connect(m_view
, SIGNAL(tabRequested(KUrl
)),
93 this, SLOT(createNewWindow(KUrl
)));
94 connect(m_view
, SIGNAL(requestContextMenu(KFileItem
,KUrl
)),
95 this, SLOT(slotOpenContextMenu(KFileItem
,KUrl
)));
96 connect(m_view
, SIGNAL(selectionChanged(KFileItemList
)),
97 m_extension
, SIGNAL(selectionInfo(KFileItemList
)));
98 connect(m_view
, SIGNAL(selectionChanged(KFileItemList
)),
99 this, SLOT(slotSelectionChanged(KFileItemList
)));
100 connect(m_view
, SIGNAL(requestItemInfo(KFileItem
)),
101 this, SLOT(slotRequestItemInfo(KFileItem
)));
102 connect(m_view
, SIGNAL(urlChanged(KUrl
)),
103 this, SLOT(slotUrlChanged(KUrl
)));
104 connect(m_view
, SIGNAL(requestUrlChange(KUrl
)),
105 this, SLOT(slotRequestUrlChange(KUrl
)));
106 connect(m_view
, SIGNAL(modeChanged()),
107 this, SIGNAL(viewModeChanged())); // relay signal
109 m_actionHandler
= new DolphinViewActionHandler(actionCollection(), this);
110 m_actionHandler
->setCurrentView(m_view
);
112 QClipboard
* clipboard
= QApplication::clipboard();
113 connect(clipboard
, SIGNAL(dataChanged()),
114 this, SLOT(updatePasteAction()));
117 m_actionHandler
->updateViewActions();
118 slotSelectionChanged(KFileItemList()); // initially disable selection-dependent actions
120 // TODO sort_by_* 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()
135 KAction
*editMimeTypeAction
= actionCollection()->addAction( "editMimeType" );
136 editMimeTypeAction
->setText( i18nc("@action:inmenu Edit", "&Edit File Type..." ) );
137 connect(editMimeTypeAction
, SIGNAL(triggered()), SLOT(slotEditMimeType()));
139 KAction
*propertiesAction
= actionCollection()->addAction( "properties" );
140 propertiesAction
->setText( i18nc("@action:inmenu Edit", "Properties") );
141 propertiesAction
->setShortcut(Qt::ALT
+Qt::Key_Return
);
142 connect(propertiesAction
, SIGNAL(triggered()), SLOT(slotProperties()));
144 // View menu: all done by DolphinViewActionHandler
148 QActionGroup
* goActionGroup
= new QActionGroup(this);
149 connect(goActionGroup
, SIGNAL(triggered(QAction
*)),
150 this, SLOT(slotGoTriggered(QAction
*)));
152 createGoAction("go_applications", "start-here-kde",
153 i18nc("@action:inmenu Go", "App&lications"), QString("programs:/"),
155 createGoAction("go_network_folders", "folder-remote",
156 i18nc("@action:inmenu Go", "&Network Folders"), QString("remote:/"),
158 createGoAction("go_settings", "preferences-system",
159 i18nc("@action:inmenu Go", "Sett&ings"), QString("settings:/"),
161 createGoAction("go_trash", "user-trash",
162 i18nc("@action:inmenu Go", "Trash"), QString("trash:/"),
164 createGoAction("go_autostart", "",
165 i18nc("@action:inmenu Go", "Autostart"), KGlobalSettings::autostartPath(),
169 void DolphinPart::createGoAction(const char* name
, const char* iconName
,
170 const QString
& text
, const QString
& url
,
171 QActionGroup
* actionGroup
)
173 KAction
* action
= actionCollection()->addAction(name
);
174 action
->setIcon(KIcon(iconName
));
175 action
->setText(text
);
176 action
->setData(url
);
177 action
->setActionGroup(actionGroup
);
180 void DolphinPart::slotGoTriggered(QAction
* action
)
182 const QString url
= action
->data().toString();
183 emit m_extension
->openUrlRequest(KUrl(url
));
186 void DolphinPart::slotSelectionChanged(const KFileItemList
& selection
)
188 const bool hasSelection
= !selection
.isEmpty();
190 QAction
* renameAction
= actionCollection()->action("rename");
191 QAction
* moveToTrashAction
= actionCollection()->action("move_to_trash");
192 QAction
* deleteAction
= actionCollection()->action("delete");
193 QAction
* editMimeTypeAction
= actionCollection()->action("editMimeType");
194 QAction
* propertiesAction
= actionCollection()->action("properties");
197 stateChanged("has_no_selection");
199 emit m_extension
->enableAction("cut", false);
200 emit m_extension
->enableAction("copy", false);
201 renameAction
->setEnabled(false);
202 moveToTrashAction
->setEnabled(false);
203 deleteAction
->setEnabled(false);
204 editMimeTypeAction
->setEnabled(false);
205 propertiesAction
->setEnabled(false);
207 stateChanged("has_selection");
209 KonqFileItemCapabilities
capabilities(selection
);
210 const bool enableMoveToTrash
= capabilities
.isLocal() && capabilities
.supportsMoving();
212 renameAction
->setEnabled(capabilities
.supportsMoving());
213 moveToTrashAction
->setEnabled(enableMoveToTrash
);
214 deleteAction
->setEnabled(capabilities
.supportsDeleting());
215 editMimeTypeAction
->setEnabled(true);
216 propertiesAction
->setEnabled(true);
217 emit m_extension
->enableAction("cut", capabilities
.supportsMoving());
218 emit m_extension
->enableAction("copy", true);
222 void DolphinPart::updatePasteAction()
224 QPair
<bool, QString
> pasteInfo
= m_view
->pasteInfo();
225 emit m_extension
->enableAction( "paste", pasteInfo
.first
);
226 emit m_extension
->setActionText( "paste", pasteInfo
.second
);
229 KAboutData
* DolphinPart::createAboutData()
231 return new KAboutData("dolphinpart", "dolphin", ki18nc("@title", "Dolphin Part"), "0.1");
234 bool DolphinPart::openUrl(const KUrl
& url
)
236 bool reload
= arguments().reload();
237 // A bit of a workaround so that changing the namefilter works: force reload.
238 // Otherwise DolphinView wouldn't relist the URL, so nothing would happen.
239 if (m_nameFilter
!= m_dirLister
->nameFilter())
241 if (m_view
->url() == url
&& !reload
) { // DolphinView won't do anything in that case, so don't emit started
244 setUrl(url
); // remember it at the KParts level
245 KUrl
visibleUrl(url
);
246 if (!m_nameFilter
.isEmpty()) {
247 visibleUrl
.addPath(m_nameFilter
);
249 QString prettyUrl
= visibleUrl
.pathOrUrl();
250 emit
setWindowCaption(prettyUrl
);
251 emit m_extension
->setLocationBarUrl(prettyUrl
);
252 emit
started(0); // get the wheel to spin
253 m_dirLister
->setNameFilter(m_nameFilter
);
255 emit
aboutToOpenURL();
261 void DolphinPart::slotCompleted(const KUrl
& url
)
267 void DolphinPart::slotCanceled(const KUrl
& url
)
272 void DolphinPart::slotInfoMessage(const QString
& msg
)
274 emit
setStatusBarText(msg
);
277 void DolphinPart::slotErrorMessage(const QString
& msg
)
279 KMessageBox::error(m_view
, msg
);
282 void DolphinPart::slotRequestItemInfo(const KFileItem
& item
)
284 emit m_extension
->mouseOverInfo(item
);
287 void DolphinPart::slotItemTriggered(const KFileItem
& item
)
289 KParts::OpenUrlArguments args
;
290 args
.setMimeType(item
.mimetype());
292 // Ideally, konqueror should be changed to not require trustedSource for directory views,
293 // since the idea was not to need BrowserArguments for non-browser stuff...
294 KParts::BrowserArguments browserArgs
;
295 browserArgs
.trustedSource
= true;
296 emit m_extension
->openUrlRequest(item
.targetUrl(), args
, browserArgs
);
299 void DolphinPart::createNewWindow(const KUrl
& url
)
301 // TODO: Check issue N176832 for the missing QAIV signal; task 177399 - maybe this code
302 // should be moved into DolphinPart::slotItemTriggered()
303 KFileItem
item(S_IFDIR
, (mode_t
)-1, url
);
304 KParts::OpenUrlArguments args
;
305 args
.setMimeType(item
.mimetype());
306 emit m_extension
->createNewWindow(url
, args
);
309 void DolphinPart::slotOpenContextMenu(const KFileItem
& _item
, const KUrl
&)
311 KParts::BrowserExtension::PopupFlags popupFlags
= KParts::BrowserExtension::DefaultPopupItems
312 | KParts::BrowserExtension::ShowProperties
313 | KParts::BrowserExtension::ShowUrlOperations
;
315 KFileItem
item(_item
);
317 if (item
.isNull()) { // viewport context menu
318 popupFlags
|= KParts::BrowserExtension::ShowNavigationItems
| KParts::BrowserExtension::ShowUp
;
319 item
= m_dirLister
->rootItem();
321 item
= KFileItem( S_IFDIR
, (mode_t
)-1, url() );
324 KParts::BrowserExtension::ActionGroupMap actionGroups
;
325 QList
<QAction
*> editActions
;
327 if (!_item
.isNull()) { // only for context menu on one or more items
328 bool sDeleting
= true;
331 // If the parent directory of the selected item is writable, moving
332 // and deleting are possible.
333 KFileItem parentDir
= m_dirLister
->rootItem();
334 if (!parentDir
.isWritable()) {
335 popupFlags
|= KParts::BrowserExtension::NoDeletion
;
341 editActions
.append(actionCollection()->action("rename"));
343 bool addTrash
= false;
346 bool isIntoTrash
= _item
.url().protocol() == "trash";
348 if ( sMoving
&& !isIntoTrash
&& item
.isLocalFile() )
352 if ( !item
.isLocalFile() )
354 else if (QApplication::keyboardModifiers() & Qt::ShiftModifier
) {
359 KConfigGroup
configGroup( KGlobal::config(), "KDE" );
360 if ( configGroup
.readEntry( "ShowDeleteCommand", false) )
366 editActions
.append(actionCollection()->action("move_to_trash"));
368 editActions
.append(actionCollection()->action("delete"));
369 actionGroups
.insert("editactions", editActions
);
372 // TODO: We should change the signature of the slots (and signals) for being able
373 // to tell for which items we want a popup.
374 KFileItemList items
= (m_view
->selectedItems().count() ? m_view
->selectedItems()
375 : KFileItemList() << item
);
376 emit m_extension
->popupMenu(QCursor::pos(),
378 KParts::OpenUrlArguments(),
379 KParts::BrowserArguments(),
384 void DolphinPart::slotUrlChanged(const KUrl
& url
)
386 QString prettyUrl
= url
.pathOrUrl();
387 emit m_extension
->setLocationBarUrl(prettyUrl
);
390 void DolphinPart::slotRequestUrlChange(const KUrl
& url
)
392 if (m_view
->url() != url
) {
393 // If the view URL is not equal to 'url', then an inner URL change has
394 // been done (e. g. by activating an existing column in the column view).
396 emit m_extension
->openUrlNotify();
402 void DolphinPartBrowserExtension::cut()
404 m_part
->view()->cutSelectedItems();
407 void DolphinPartBrowserExtension::copy()
409 m_part
->view()->copySelectedItems();
412 void DolphinPartBrowserExtension::paste()
414 m_part
->view()->paste();
417 void DolphinPartBrowserExtension::reparseConfiguration()
419 m_part
->view()->refresh();
424 void DolphinPart::slotEditMimeType()
426 const KFileItemList items
= m_view
->selectedItems();
427 if (!items
.isEmpty()) {
428 KonqOperations::editMimeType(items
.first().mimetype(), m_view
);
432 void DolphinPart::slotProperties()
434 const KFileItemList items
= m_view
->selectedItems();
435 if (!items
.isEmpty()) {
436 KPropertiesDialog
dialog(items
.first().url(), m_view
);
441 void DolphinPart::setCurrentViewMode(const QString
& viewModeName
)
443 QAction
* action
= actionCollection()->action(viewModeName
);
448 QString
DolphinPart::currentViewMode() const
450 return m_actionHandler
->currentViewModeActionName();
453 void DolphinPart::setNameFilter(const QString
& nameFilter
)
455 // This is the "/home/dfaure/*.diff" kind of name filter (KDirLister::setNameFilter)
456 // which is unrelated to DolphinView::setNameFilter which is substring filtering in a proxy.
457 m_nameFilter
= nameFilter
;
458 // TODO save/restore name filter in saveState/restoreState like KonqDirPart did in kde3?
461 #include "dolphinpart.moc"