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 <kauthorized.h>
45 #include <QActionGroup>
46 #include <QApplication>
49 K_PLUGIN_FACTORY(DolphinPartFactory
, registerPlugin
<DolphinPart
>();)
50 // The componentdata name must be dolphinpart so that dolphinpart.rc is found
51 // Alternatively we would have to install it as dolphin/dolphinpart.rc
52 K_EXPORT_PLUGIN(DolphinPartFactory("dolphinpart"))
54 DolphinPart::DolphinPart(QWidget
* parentWidget
, QObject
* parent
, const QVariantList
& args
)
55 : KParts::ReadOnlyPart(parent
)
58 setComponentData(DolphinPartFactory::componentData(), false);
59 m_extension
= new DolphinPartBrowserExtension(this);
61 // make sure that other apps using this part find Dolphin's view-file-columns icons
62 KIconLoader::global()->addAppDir("dolphin");
64 m_dirLister
= new KDirLister
;
65 m_dirLister
->setAutoUpdate(true);
66 m_dirLister
->setMainWindow(parentWidget
->window());
67 m_dirLister
->setDelayedMimeTypes(true);
69 //connect(m_dirLister, SIGNAL(started(KUrl)), this, SLOT(slotStarted()));
70 connect(m_dirLister
, SIGNAL(completed(KUrl
)), this, SLOT(slotCompleted(KUrl
)));
71 connect(m_dirLister
, SIGNAL(canceled(KUrl
)), this, SLOT(slotCanceled(KUrl
)));
73 m_dolphinModel
= new DolphinModel(this);
74 m_dolphinModel
->setDirLister(m_dirLister
);
76 m_proxyModel
= new DolphinSortFilterProxyModel(this);
77 m_proxyModel
->setSourceModel(m_dolphinModel
);
79 m_view
= new DolphinView(parentWidget
,
84 m_view
->setTabsForFilesEnabled(true);
87 setXMLFile("dolphinpart.rc");
89 connect(m_view
, SIGNAL(infoMessage(QString
)),
90 this, SLOT(slotInfoMessage(QString
)));
91 connect(m_view
, SIGNAL(errorMessage(QString
)),
92 this, SLOT(slotErrorMessage(QString
)));
93 connect(m_view
, SIGNAL(itemTriggered(KFileItem
)),
94 this, SLOT(slotItemTriggered(KFileItem
)));
95 connect(m_view
, SIGNAL(tabRequested(KUrl
)),
96 this, SLOT(createNewWindow(KUrl
)));
97 connect(m_view
, SIGNAL(requestContextMenu(KFileItem
,KUrl
)),
98 this, SLOT(slotOpenContextMenu(KFileItem
,KUrl
)));
99 connect(m_view
, SIGNAL(selectionChanged(KFileItemList
)),
100 m_extension
, SIGNAL(selectionInfo(KFileItemList
)));
101 connect(m_view
, SIGNAL(selectionChanged(KFileItemList
)),
102 this, SLOT(slotSelectionChanged(KFileItemList
)));
103 connect(m_view
, SIGNAL(requestItemInfo(KFileItem
)),
104 this, SLOT(slotRequestItemInfo(KFileItem
)));
105 connect(m_view
, SIGNAL(urlChanged(KUrl
)),
106 this, SLOT(slotUrlChanged(KUrl
)));
107 connect(m_view
, SIGNAL(requestUrlChange(KUrl
)),
108 this, SLOT(slotRequestUrlChange(KUrl
)));
109 connect(m_view
, SIGNAL(modeChanged()),
110 this, SIGNAL(viewModeChanged())); // relay signal
112 m_actionHandler
= new DolphinViewActionHandler(actionCollection(), this);
113 m_actionHandler
->setCurrentView(m_view
);
115 QClipboard
* clipboard
= QApplication::clipboard();
116 connect(clipboard
, SIGNAL(dataChanged()),
117 this, SLOT(updatePasteAction()));
120 m_actionHandler
->updateViewActions();
121 slotSelectionChanged(KFileItemList()); // initially disable selection-dependent actions
123 // TODO sort_by_* 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()
138 KAction
*editMimeTypeAction
= actionCollection()->addAction( "editMimeType" );
139 editMimeTypeAction
->setText( i18nc("@action:inmenu Edit", "&Edit File Type..." ) );
140 connect(editMimeTypeAction
, SIGNAL(triggered()), SLOT(slotEditMimeType()));
142 KAction
*propertiesAction
= actionCollection()->addAction( "properties" );
143 propertiesAction
->setText( i18nc("@action:inmenu Edit", "Properties") );
144 propertiesAction
->setShortcut(Qt::ALT
+Qt::Key_Return
);
145 connect(propertiesAction
, SIGNAL(triggered()), SLOT(slotProperties()));
147 // View menu: all done by DolphinViewActionHandler
151 QActionGroup
* goActionGroup
= new QActionGroup(this);
152 connect(goActionGroup
, SIGNAL(triggered(QAction
*)),
153 this, SLOT(slotGoTriggered(QAction
*)));
155 createGoAction("go_applications", "start-here-kde",
156 i18nc("@action:inmenu Go", "App&lications"), QString("programs:/"),
158 createGoAction("go_network_folders", "folder-remote",
159 i18nc("@action:inmenu Go", "&Network Folders"), QString("remote:/"),
161 createGoAction("go_settings", "preferences-system",
162 i18nc("@action:inmenu Go", "Sett&ings"), QString("settings:/"),
164 createGoAction("go_trash", "user-trash",
165 i18nc("@action:inmenu Go", "Trash"), QString("trash:/"),
167 createGoAction("go_autostart", "",
168 i18nc("@action:inmenu Go", "Autostart"), KGlobalSettings::autostartPath(),
172 if (KAuthorized::authorizeKAction("shell_access")) {
173 KAction
* action
= actionCollection()->addAction("open_terminal");
174 action
->setIcon(KIcon("utilities-terminal"));
175 action
->setText(i18nc("@action:inmenu Tools", "Open &Terminal"));
176 connect(action
, SIGNAL(triggered()), SLOT(slotOpenTerminal()));
177 action
->setShortcut(Qt::Key_F4
);
182 void DolphinPart::createGoAction(const char* name
, const char* iconName
,
183 const QString
& text
, const QString
& url
,
184 QActionGroup
* actionGroup
)
186 KAction
* action
= actionCollection()->addAction(name
);
187 action
->setIcon(KIcon(iconName
));
188 action
->setText(text
);
189 action
->setData(url
);
190 action
->setActionGroup(actionGroup
);
193 void DolphinPart::slotGoTriggered(QAction
* action
)
195 const QString url
= action
->data().toString();
196 emit m_extension
->openUrlRequest(KUrl(url
));
199 void DolphinPart::slotSelectionChanged(const KFileItemList
& selection
)
201 const bool hasSelection
= !selection
.isEmpty();
203 QAction
* renameAction
= actionCollection()->action("rename");
204 QAction
* moveToTrashAction
= actionCollection()->action("move_to_trash");
205 QAction
* deleteAction
= actionCollection()->action("delete");
206 QAction
* editMimeTypeAction
= actionCollection()->action("editMimeType");
207 QAction
* propertiesAction
= actionCollection()->action("properties");
210 stateChanged("has_no_selection");
212 emit m_extension
->enableAction("cut", false);
213 emit m_extension
->enableAction("copy", false);
214 renameAction
->setEnabled(false);
215 moveToTrashAction
->setEnabled(false);
216 deleteAction
->setEnabled(false);
217 editMimeTypeAction
->setEnabled(false);
218 propertiesAction
->setEnabled(false);
220 stateChanged("has_selection");
222 KonqFileItemCapabilities
capabilities(selection
);
223 const bool enableMoveToTrash
= capabilities
.isLocal() && capabilities
.supportsMoving();
225 renameAction
->setEnabled(capabilities
.supportsMoving());
226 moveToTrashAction
->setEnabled(enableMoveToTrash
);
227 deleteAction
->setEnabled(capabilities
.supportsDeleting());
228 editMimeTypeAction
->setEnabled(true);
229 propertiesAction
->setEnabled(true);
230 emit m_extension
->enableAction("cut", capabilities
.supportsMoving());
231 emit m_extension
->enableAction("copy", true);
235 void DolphinPart::updatePasteAction()
237 QPair
<bool, QString
> pasteInfo
= m_view
->pasteInfo();
238 emit m_extension
->enableAction( "paste", pasteInfo
.first
);
239 emit m_extension
->setActionText( "paste", pasteInfo
.second
);
242 KAboutData
* DolphinPart::createAboutData()
244 return new KAboutData("dolphinpart", "dolphin", ki18nc("@title", "Dolphin Part"), "0.1");
247 bool DolphinPart::openUrl(const KUrl
& url
)
249 bool reload
= arguments().reload();
250 // A bit of a workaround so that changing the namefilter works: force reload.
251 // Otherwise DolphinView wouldn't relist the URL, so nothing would happen.
252 if (m_nameFilter
!= m_dirLister
->nameFilter())
254 if (m_view
->url() == url
&& !reload
) { // DolphinView won't do anything in that case, so don't emit started
257 setUrl(url
); // remember it at the KParts level
258 KUrl
visibleUrl(url
);
259 if (!m_nameFilter
.isEmpty()) {
260 visibleUrl
.addPath(m_nameFilter
);
262 QString prettyUrl
= visibleUrl
.pathOrUrl();
263 emit
setWindowCaption(prettyUrl
);
264 emit m_extension
->setLocationBarUrl(prettyUrl
);
265 emit
started(0); // get the wheel to spin
266 m_dirLister
->setNameFilter(m_nameFilter
);
268 emit
aboutToOpenURL();
274 void DolphinPart::slotCompleted(const KUrl
& url
)
280 void DolphinPart::slotCanceled(const KUrl
& url
)
285 void DolphinPart::slotInfoMessage(const QString
& msg
)
287 emit
setStatusBarText(msg
);
290 void DolphinPart::slotErrorMessage(const QString
& msg
)
292 KMessageBox::error(m_view
, msg
);
295 void DolphinPart::slotRequestItemInfo(const KFileItem
& item
)
297 emit m_extension
->mouseOverInfo(item
);
300 void DolphinPart::slotItemTriggered(const KFileItem
& item
)
302 KParts::OpenUrlArguments args
;
303 args
.setMimeType(item
.mimetype());
305 // Ideally, konqueror should be changed to not require trustedSource for directory views,
306 // since the idea was not to need BrowserArguments for non-browser stuff...
307 KParts::BrowserArguments browserArgs
;
308 browserArgs
.trustedSource
= true;
309 emit m_extension
->openUrlRequest(item
.targetUrl(), args
, browserArgs
);
312 void DolphinPart::createNewWindow(const KUrl
& url
)
314 // TODO: Check issue N176832 for the missing QAIV signal; task 177399 - maybe this code
315 // should be moved into DolphinPart::slotItemTriggered()
316 KFileItem
item(S_IFDIR
, (mode_t
)-1, url
);
317 KParts::OpenUrlArguments args
;
318 args
.setMimeType(item
.mimetype());
319 emit m_extension
->createNewWindow(url
, args
);
322 void DolphinPart::slotOpenContextMenu(const KFileItem
& _item
, const KUrl
&)
324 KParts::BrowserExtension::PopupFlags popupFlags
= KParts::BrowserExtension::DefaultPopupItems
325 | KParts::BrowserExtension::ShowProperties
326 | KParts::BrowserExtension::ShowUrlOperations
;
328 KFileItem
item(_item
);
330 if (item
.isNull()) { // viewport context menu
331 popupFlags
|= KParts::BrowserExtension::ShowNavigationItems
| KParts::BrowserExtension::ShowUp
;
332 item
= m_dirLister
->rootItem();
334 item
= KFileItem( S_IFDIR
, (mode_t
)-1, url() );
337 KParts::BrowserExtension::ActionGroupMap actionGroups
;
338 QList
<QAction
*> editActions
;
340 if (!_item
.isNull()) { // only for context menu on one or more items
341 bool sDeleting
= true;
344 // If the parent directory of the selected item is writable, moving
345 // and deleting are possible.
346 KFileItem parentDir
= m_dirLister
->rootItem();
347 if (!parentDir
.isWritable()) {
348 popupFlags
|= KParts::BrowserExtension::NoDeletion
;
354 editActions
.append(actionCollection()->action("rename"));
356 bool addTrash
= false;
359 bool isIntoTrash
= _item
.url().protocol() == "trash";
361 if ( sMoving
&& !isIntoTrash
&& item
.isLocalFile() )
365 if ( !item
.isLocalFile() )
367 else if (QApplication::keyboardModifiers() & Qt::ShiftModifier
) {
372 KConfigGroup
configGroup( KGlobal::config(), "KDE" );
373 if ( configGroup
.readEntry( "ShowDeleteCommand", false) )
379 editActions
.append(actionCollection()->action("move_to_trash"));
381 editActions
.append(actionCollection()->action("delete"));
382 actionGroups
.insert("editactions", editActions
);
385 // TODO: We should change the signature of the slots (and signals) for being able
386 // to tell for which items we want a popup.
387 KFileItemList items
= (m_view
->selectedItems().count() ? m_view
->selectedItems()
388 : KFileItemList() << item
);
389 emit m_extension
->popupMenu(QCursor::pos(),
391 KParts::OpenUrlArguments(),
392 KParts::BrowserArguments(),
397 void DolphinPart::slotUrlChanged(const KUrl
& url
)
399 QString prettyUrl
= url
.pathOrUrl();
400 emit m_extension
->setLocationBarUrl(prettyUrl
);
403 void DolphinPart::slotRequestUrlChange(const KUrl
& url
)
405 if (m_view
->url() != url
) {
406 // If the view URL is not equal to 'url', then an inner URL change has
407 // been done (e. g. by activating an existing column in the column view).
409 emit m_extension
->openUrlNotify();
415 void DolphinPartBrowserExtension::cut()
417 m_part
->view()->cutSelectedItems();
420 void DolphinPartBrowserExtension::copy()
422 m_part
->view()->copySelectedItems();
425 void DolphinPartBrowserExtension::paste()
427 m_part
->view()->paste();
430 void DolphinPartBrowserExtension::reparseConfiguration()
432 m_part
->view()->refresh();
437 void DolphinPart::slotEditMimeType()
439 const KFileItemList items
= m_view
->selectedItems();
440 if (!items
.isEmpty()) {
441 KonqOperations::editMimeType(items
.first().mimetype(), m_view
);
445 void DolphinPart::slotProperties()
447 const KFileItemList items
= m_view
->selectedItems();
448 if (!items
.isEmpty()) {
449 KPropertiesDialog
dialog(items
.first().url(), m_view
);
454 void DolphinPart::setCurrentViewMode(const QString
& viewModeName
)
456 QAction
* action
= actionCollection()->action(viewModeName
);
461 QString
DolphinPart::currentViewMode() const
463 return m_actionHandler
->currentViewModeActionName();
466 void DolphinPart::setNameFilter(const QString
& nameFilter
)
468 // This is the "/home/dfaure/*.diff" kind of name filter (KDirLister::setNameFilter)
469 // which is unrelated to DolphinView::setNameFilter which is substring filtering in a proxy.
470 m_nameFilter
= nameFilter
;
471 // TODO save/restore name filter in saveState/restoreState like KonqDirPart did in kde3?
474 void DolphinPart::slotOpenTerminal()
476 KConfigGroup
confGroup(KGlobal::config(), "General"); // set by componentchooser kcm
477 const QString preferredTerminal
= confGroup
.readPathEntry("TerminalApplication", "konsole");
479 QString
dir(QDir::homePath());
483 // If the given directory is not local, it can still be the URL of an
484 // ioslave using UDS_LOCAL_PATH which to be converted first.
485 u
= KIO::NetAccess::mostLocalUrl(u
, widget());
487 //If the URL is local after the above conversion, set the directory.
488 if (u
.isLocalFile()) {
492 // Compensate for terminal having arguments.
493 QStringList args
= KShell::splitArgs(preferredTerminal
);
494 if (args
.isEmpty()) {
497 const QString prog
= args
.takeFirst();
498 if (prog
== "konsole") {
502 QProcess::startDetached(prog
, args
);
505 #include "dolphinpart.moc"