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_operations.h>
28 #include <kaboutdata.h>
29 #include <kactioncollection.h>
30 #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>
41 #include <QActionGroup>
42 #include <QApplication>
45 K_PLUGIN_FACTORY(DolphinPartFactory
, registerPlugin
<DolphinPart
>();)
46 // The componentdata name must be dolphinpart so that dolphinpart.rc is found
47 // Alternatively we would have to install it as dolphin/dolphinpart.rc
48 K_EXPORT_PLUGIN(DolphinPartFactory("dolphinpart"))
50 DolphinPart::DolphinPart(QWidget
* parentWidget
, QObject
* parent
, const QVariantList
& args
)
51 : KParts::ReadOnlyPart(parent
)
54 setComponentData(DolphinPartFactory::componentData(), false);
55 m_extension
= new DolphinPartBrowserExtension(this);
57 // make sure that other apps using this part find Dolphin's view-file-columns icons
58 KIconLoader::global()->addAppDir("dolphin");
60 m_dirLister
= new KDirLister
;
61 m_dirLister
->setAutoUpdate(true);
62 m_dirLister
->setMainWindow(parentWidget
->window());
63 m_dirLister
->setDelayedMimeTypes(true);
65 //connect(m_dirLister, SIGNAL(started(KUrl)), this, SLOT(slotStarted()));
66 connect(m_dirLister
, SIGNAL(completed(KUrl
)), this, SLOT(slotCompleted(KUrl
)));
67 connect(m_dirLister
, SIGNAL(canceled(KUrl
)), this, SLOT(slotCanceled(KUrl
)));
69 m_dolphinModel
= new DolphinModel(this);
70 m_dolphinModel
->setDirLister(m_dirLister
);
72 m_proxyModel
= new DolphinSortFilterProxyModel(this);
73 m_proxyModel
->setSourceModel(m_dolphinModel
);
75 m_view
= new DolphinView(parentWidget
,
80 m_view
->setTabsForFilesEnabled(true);
83 setXMLFile("dolphinpart.rc");
85 connect(m_view
, SIGNAL(infoMessage(QString
)),
86 this, SLOT(slotInfoMessage(QString
)));
87 connect(m_view
, SIGNAL(errorMessage(QString
)),
88 this, SLOT(slotErrorMessage(QString
)));
89 connect(m_view
, SIGNAL(itemTriggered(KFileItem
)),
90 this, SLOT(slotItemTriggered(KFileItem
)));
91 connect(m_view
, SIGNAL(tabRequested(KUrl
)),
92 this, SLOT(createNewWindow(KUrl
)));
93 connect(m_view
, SIGNAL(requestContextMenu(KFileItem
,KUrl
)),
94 this, SLOT(slotOpenContextMenu(KFileItem
,KUrl
)));
95 connect(m_view
, SIGNAL(selectionChanged(KFileItemList
)),
96 m_extension
, SIGNAL(selectionInfo(KFileItemList
)));
97 connect(m_view
, SIGNAL(selectionChanged(KFileItemList
)),
98 this, SLOT(slotSelectionChanged(KFileItemList
)));
99 connect(m_view
, SIGNAL(requestItemInfo(KFileItem
)),
100 this, SLOT(slotRequestItemInfo(KFileItem
)));
101 connect(m_view
, SIGNAL(urlChanged(KUrl
)),
102 this, SLOT(slotUrlChanged(KUrl
)));
103 connect(m_view
, SIGNAL(requestUrlChange(KUrl
)),
104 this, SLOT(slotRequestUrlChange(KUrl
)));
105 connect(m_view
, SIGNAL(modeChanged()),
106 this, SIGNAL(viewModeChanged())); // relay signal
108 m_actionHandler
= new DolphinViewActionHandler(actionCollection(), this);
109 m_actionHandler
->setCurrentView(m_view
);
111 QClipboard
* clipboard
= QApplication::clipboard();
112 connect(clipboard
, SIGNAL(dataChanged()),
113 this, SLOT(updatePasteAction()));
116 m_actionHandler
->updateViewActions();
117 slotSelectionChanged(KFileItemList()); // initially disable selection-dependent actions
119 // TODO sort_by_* actions
121 // TODO there was a "always open a new window" (when clicking on a directory) setting in konqueror
122 // (sort of spacial navigation)
124 loadPlugins(this, this, componentData());
127 DolphinPart::~DolphinPart()
132 void DolphinPart::createActions()
134 KAction
*editMimeTypeAction
= actionCollection()->addAction( "editMimeType" );
135 editMimeTypeAction
->setText( i18nc("@action:inmenu Edit", "&Edit File Type..." ) );
136 connect(editMimeTypeAction
, SIGNAL(triggered()), SLOT(slotEditMimeType()));
138 KAction
*propertiesAction
= actionCollection()->addAction( "properties" );
139 propertiesAction
->setText( i18nc("@action:inmenu Edit", "Properties") );
140 propertiesAction
->setShortcut(Qt::ALT
+Qt::Key_Return
);
141 connect(propertiesAction
, SIGNAL(triggered()), SLOT(slotProperties()));
143 // View menu: all done by DolphinViewActionHandler
147 QActionGroup
* goActionGroup
= new QActionGroup(this);
148 connect(goActionGroup
, SIGNAL(triggered(QAction
*)),
149 this, SLOT(slotGoTriggered(QAction
*)));
151 createGoAction("go_applications", "start-here-kde",
152 i18nc("@action:inmenu Go", "App&lications"), QString("programs:/"),
154 createGoAction("go_network_folders", "folder-remote",
155 i18nc("@action:inmenu Go", "&Network Folders"), QString("remote:/"),
157 createGoAction("go_settings", "preferences-system",
158 i18nc("@action:inmenu Go", "Sett&ings"), QString("settings:/"),
160 createGoAction("go_trash", "user-trash",
161 i18nc("@action:inmenu Go", "Trash"), QString("trash:/"),
163 createGoAction("go_autostart", "",
164 i18nc("@action:inmenu Go", "Autostart"), KGlobalSettings::autostartPath(),
168 void DolphinPart::createGoAction(const char* name
, const char* iconName
,
169 const QString
& text
, const QString
& url
,
170 QActionGroup
* actionGroup
)
172 KAction
* action
= actionCollection()->addAction(name
);
173 action
->setIcon(KIcon(iconName
));
174 action
->setText(text
);
175 action
->setData(url
);
176 action
->setActionGroup(actionGroup
);
179 void DolphinPart::slotGoTriggered(QAction
* action
)
181 const QString url
= action
->data().toString();
182 emit m_extension
->openUrlRequest(KUrl(url
));
185 void DolphinPart::slotSelectionChanged(const KFileItemList
& selection
)
187 const bool hasSelection
= !selection
.isEmpty();
189 stateChanged("has_no_selection");
191 stateChanged("has_selection");
195 actions
<< "rename" << "move_to_trash" << "delete" << "editMimeType" << "properties";
196 foreach(const QString
& actionName
, actions
) {
197 QAction
* action
= actionCollection()->action(actionName
);
200 action
->setEnabled(hasSelection
);
204 emit m_extension
->enableAction("cut", hasSelection
);
205 emit m_extension
->enableAction("copy", hasSelection
);
208 void DolphinPart::updatePasteAction()
210 QPair
<bool, QString
> pasteInfo
= m_view
->pasteInfo();
211 emit m_extension
->enableAction( "paste", pasteInfo
.first
);
212 emit m_extension
->setActionText( "paste", pasteInfo
.second
);
215 KAboutData
* DolphinPart::createAboutData()
217 return new KAboutData("dolphinpart", "dolphin", ki18nc("@title", "Dolphin Part"), "0.1");
220 bool DolphinPart::openUrl(const KUrl
& url
)
222 bool reload
= arguments().reload();
223 // A bit of a workaround so that changing the namefilter works: force reload.
224 // Otherwise DolphinView wouldn't relist the URL, so nothing would happen.
225 if (m_nameFilter
!= m_dirLister
->nameFilter())
227 if (m_view
->url() == url
&& !reload
) { // DolphinView won't do anything in that case, so don't emit started
230 setUrl(url
); // remember it at the KParts level
231 KUrl
visibleUrl(url
);
232 if (!m_nameFilter
.isEmpty()) {
233 visibleUrl
.addPath(m_nameFilter
);
235 QString prettyUrl
= visibleUrl
.pathOrUrl();
236 emit
setWindowCaption(prettyUrl
);
237 emit m_extension
->setLocationBarUrl(prettyUrl
);
238 emit
started(0); // get the wheel to spin
239 m_dirLister
->setNameFilter(m_nameFilter
);
241 emit
aboutToOpenURL();
247 void DolphinPart::slotCompleted(const KUrl
& url
)
253 void DolphinPart::slotCanceled(const KUrl
& url
)
258 void DolphinPart::slotInfoMessage(const QString
& msg
)
260 emit
setStatusBarText(msg
);
263 void DolphinPart::slotErrorMessage(const QString
& msg
)
265 KMessageBox::error(m_view
, msg
);
268 void DolphinPart::slotRequestItemInfo(const KFileItem
& item
)
270 emit m_extension
->mouseOverInfo(item
);
273 void DolphinPart::slotItemTriggered(const KFileItem
& item
)
275 KParts::OpenUrlArguments args
;
276 args
.setMimeType(item
.mimetype());
278 // Ideally, konqueror should be changed to not require trustedSource for directory views,
279 // since the idea was not to need BrowserArguments for non-browser stuff...
280 KParts::BrowserArguments browserArgs
;
281 browserArgs
.trustedSource
= true;
282 emit m_extension
->openUrlRequest(item
.targetUrl(), args
, browserArgs
);
285 void DolphinPart::createNewWindow(const KUrl
& url
)
287 // TODO: Check issue N176832 for the missing QAIV signal; task 177399 - maybe this code
288 // should be moved into DolphinPart::slotItemTriggered()
289 KFileItem
item(S_IFDIR
, (mode_t
)-1, url
);
290 KParts::OpenUrlArguments args
;
291 args
.setMimeType(item
.mimetype());
292 emit m_extension
->createNewWindow(url
, args
);
295 void DolphinPart::slotOpenContextMenu(const KFileItem
& _item
, const KUrl
&)
297 KParts::BrowserExtension::PopupFlags popupFlags
= KParts::BrowserExtension::DefaultPopupItems
298 | KParts::BrowserExtension::ShowProperties
299 | KParts::BrowserExtension::ShowUrlOperations
;
300 // TODO KonqKfmIconView had if ( !rootItem->isWritable() )
301 // popupFlags |= KParts::BrowserExtension::NoDeletion;
303 KFileItem
item(_item
);
305 if (item
.isNull()) { // viewport context menu
306 popupFlags
|= KParts::BrowserExtension::ShowNavigationItems
| KParts::BrowserExtension::ShowUp
;
307 // TODO get m_dirLister->rootItem if possible. or via kdirmodel?
308 // and use this as fallback:
309 item
= KFileItem( S_IFDIR
, (mode_t
)-1, url() );
312 KParts::BrowserExtension::ActionGroupMap actionGroups
;
313 QList
<QAction
*> editActions
;
315 if (!_item
.isNull()) { // only for context menu on one or more items
316 // TODO if ( sMoving )
317 editActions
.append(actionCollection()->action("rename"));
319 bool addTrash
= false;
322 bool isIntoTrash
= _item
.url().protocol() == "trash";
324 if ( /*TODO sMoving &&*/ !isIntoTrash
)
327 /* TODO if ( sDeleting ) */ {
328 if ( !item
.isLocalFile() )
330 else if (QApplication::keyboardModifiers() & Qt::ShiftModifier
) {
335 KConfigGroup
configGroup( KGlobal::config(), "KDE" );
336 if ( configGroup
.readEntry( "ShowDeleteCommand", false) )
342 editActions
.append(actionCollection()->action("move_to_trash"));
344 editActions
.append(actionCollection()->action("delete"));
345 actionGroups
.insert("editactions", editActions
);
348 // TODO: We should change the signature of the slots (and signals) for being able
349 // to tell for which items we want a popup.
350 KFileItemList items
= (m_view
->selectedItems().count() ? m_view
->selectedItems()
351 : KFileItemList() << item
);
352 emit m_extension
->popupMenu(QCursor::pos(),
354 KParts::OpenUrlArguments(),
355 KParts::BrowserArguments(),
360 void DolphinPart::slotUrlChanged(const KUrl
& url
)
362 QString prettyUrl
= url
.pathOrUrl();
363 emit m_extension
->setLocationBarUrl(prettyUrl
);
366 void DolphinPart::slotRequestUrlChange(const KUrl
& url
)
368 if (m_view
->url() != url
) {
369 // If the view URL is not equal to 'url', then an inner URL change has
370 // been done (e. g. by activating an existing column in the column view).
372 emit m_extension
->openUrlNotify();
378 void DolphinPartBrowserExtension::cut()
380 m_part
->view()->cutSelectedItems();
383 void DolphinPartBrowserExtension::copy()
385 m_part
->view()->copySelectedItems();
388 void DolphinPartBrowserExtension::paste()
390 m_part
->view()->paste();
393 void DolphinPartBrowserExtension::reparseConfiguration()
395 m_part
->view()->refresh();
400 void DolphinPart::slotEditMimeType()
402 const KFileItemList items
= m_view
->selectedItems();
403 if (!items
.isEmpty()) {
404 KonqOperations::editMimeType(items
.first().mimetype(), m_view
);
408 void DolphinPart::slotProperties()
410 const KFileItemList items
= m_view
->selectedItems();
411 if (!items
.isEmpty()) {
412 KPropertiesDialog
dialog(items
.first().url(), m_view
);
417 void DolphinPart::setCurrentViewMode(const QString
& viewModeName
)
419 QAction
* action
= actionCollection()->action(viewModeName
);
424 QString
DolphinPart::currentViewMode() const
426 return m_actionHandler
->currentViewModeActionName();
429 void DolphinPart::setNameFilter(const QString
& nameFilter
)
431 // This is the "/home/dfaure/*.diff" kind of name filter (KDirLister::setNameFilter)
432 // which is unrelated to DolphinView::setNameFilter which is substring filtering in a proxy.
433 m_nameFilter
= nameFilter
;
434 // TODO save/restore name filter in saveState/restoreState like KonqDirPart did in kde3?
437 #include "dolphinpart.moc"