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 <ktoggleaction.h>
39 #include <kio/netaccess.h>
40 #include <ktoolinvocation.h>
41 #include <kauthorized.h>
45 #include <QActionGroup>
46 #include <QApplication>
49 K_PLUGIN_FACTORY(DolphinPartFactory
, registerPlugin
<DolphinPart
>();)
50 K_EXPORT_PLUGIN(DolphinPartFactory("dolphinpart", "dolphin"))
52 DolphinPart::DolphinPart(QWidget
* parentWidget
, QObject
* parent
, const QVariantList
& args
)
53 : KParts::ReadOnlyPart(parent
)
56 setComponentData(DolphinPartFactory::componentData(), false);
57 m_extension
= new DolphinPartBrowserExtension(this);
59 // make sure that other apps using this part find Dolphin's view-file-columns icons
60 KIconLoader::global()->addAppDir("dolphin");
62 m_dirLister
= new KDirLister
;
63 m_dirLister
->setAutoUpdate(true);
64 m_dirLister
->setMainWindow(parentWidget
->window());
65 m_dirLister
->setDelayedMimeTypes(true);
67 //connect(m_dirLister, SIGNAL(started(KUrl)), this, SLOT(slotStarted()));
68 connect(m_dirLister
, SIGNAL(completed(KUrl
)), this, SLOT(slotCompleted(KUrl
)));
69 connect(m_dirLister
, SIGNAL(canceled(KUrl
)), this, SLOT(slotCanceled(KUrl
)));
71 m_dolphinModel
= new DolphinModel(this);
72 m_dolphinModel
->setDirLister(m_dirLister
);
74 m_proxyModel
= new DolphinSortFilterProxyModel(this);
75 m_proxyModel
->setSourceModel(m_dolphinModel
);
77 m_view
= new DolphinView(parentWidget
,
82 m_view
->setTabsForFilesEnabled(true);
85 setXMLFile("dolphinpart.rc");
87 connect(m_view
, SIGNAL(infoMessage(QString
)),
88 this, SLOT(slotInfoMessage(QString
)));
89 connect(m_view
, SIGNAL(errorMessage(QString
)),
90 this, SLOT(slotErrorMessage(QString
)));
91 connect(m_view
, SIGNAL(itemTriggered(KFileItem
)),
92 this, SLOT(slotItemTriggered(KFileItem
)));
93 connect(m_view
, SIGNAL(tabRequested(KUrl
)),
94 this, SLOT(createNewWindow(KUrl
)));
95 connect(m_view
, SIGNAL(requestContextMenu(KFileItem
,KUrl
)),
96 this, SLOT(slotOpenContextMenu(KFileItem
,KUrl
)));
97 connect(m_view
, SIGNAL(selectionChanged(KFileItemList
)),
98 m_extension
, SIGNAL(selectionInfo(KFileItemList
)));
99 connect(m_view
, SIGNAL(selectionChanged(KFileItemList
)),
100 this, SLOT(slotSelectionChanged(KFileItemList
)));
101 connect(m_view
, SIGNAL(requestItemInfo(KFileItem
)),
102 this, SLOT(slotRequestItemInfo(KFileItem
)));
103 connect(m_view
, SIGNAL(urlChanged(KUrl
)),
104 this, SLOT(slotUrlChanged(KUrl
)));
105 connect(m_view
, SIGNAL(requestUrlChange(KUrl
)),
106 this, SLOT(slotRequestUrlChange(KUrl
)));
107 connect(m_view
, SIGNAL(modeChanged()),
108 this, SIGNAL(viewModeChanged())); // relay signal
110 m_actionHandler
= new DolphinViewActionHandler(actionCollection(), this);
111 m_actionHandler
->setCurrentView(m_view
);
113 QClipboard
* clipboard
= QApplication::clipboard();
114 connect(clipboard
, SIGNAL(dataChanged()),
115 this, SLOT(updatePasteAction()));
118 m_actionHandler
->updateViewActions();
119 slotSelectionChanged(KFileItemList()); // initially disable selection-dependent 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()
136 m_newMenu
= new KNewMenu(actionCollection(), widget(), "new_menu");
137 connect(m_newMenu
->menu(), SIGNAL(aboutToShow()),
138 this, SLOT(updateNewMenu()));
140 KAction
*editMimeTypeAction
= actionCollection()->addAction( "editMimeType" );
141 editMimeTypeAction
->setText( i18nc("@action:inmenu Edit", "&Edit File Type..." ) );
142 connect(editMimeTypeAction
, SIGNAL(triggered()), SLOT(slotEditMimeType()));
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 if (KAuthorized::authorizeKAction("shell_access")) {
170 KAction
* action
= actionCollection()->addAction("open_terminal");
171 action
->setIcon(KIcon("utilities-terminal"));
172 action
->setText(i18nc("@action:inmenu Tools", "Open &Terminal"));
173 connect(action
, SIGNAL(triggered()), SLOT(slotOpenTerminal()));
174 action
->setShortcut(Qt::Key_F4
);
179 void DolphinPart::createGoAction(const char* name
, const char* iconName
,
180 const QString
& text
, const QString
& url
,
181 QActionGroup
* actionGroup
)
183 KAction
* action
= actionCollection()->addAction(name
);
184 action
->setIcon(KIcon(iconName
));
185 action
->setText(text
);
186 action
->setData(url
);
187 action
->setActionGroup(actionGroup
);
190 void DolphinPart::slotGoTriggered(QAction
* action
)
192 const QString url
= action
->data().toString();
193 emit m_extension
->openUrlRequest(KUrl(url
));
196 void DolphinPart::slotSelectionChanged(const KFileItemList
& selection
)
198 const bool hasSelection
= !selection
.isEmpty();
200 QAction
* renameAction
= actionCollection()->action("rename");
201 QAction
* moveToTrashAction
= actionCollection()->action("move_to_trash");
202 QAction
* deleteAction
= actionCollection()->action("delete");
203 QAction
* editMimeTypeAction
= actionCollection()->action("editMimeType");
204 QAction
* propertiesAction
= actionCollection()->action("properties");
205 QAction
* deleteWithTrashShortcut
= actionCollection()->action("delete_shortcut"); // see DolphinViewActionHandler
208 stateChanged("has_no_selection");
210 emit m_extension
->enableAction("cut", false);
211 emit m_extension
->enableAction("copy", false);
212 deleteWithTrashShortcut
->setEnabled(false);
213 editMimeTypeAction
->setEnabled(false);
215 stateChanged("has_selection");
217 // TODO share this code with DolphinMainWindow::updateEditActions (and the desktop code)
219 KonqFileItemCapabilities
capabilities(selection
);
220 const bool enableMoveToTrash
= capabilities
.isLocal() && capabilities
.supportsMoving();
222 renameAction
->setEnabled(capabilities
.supportsMoving());
223 moveToTrashAction
->setEnabled(enableMoveToTrash
);
224 deleteAction
->setEnabled(capabilities
.supportsDeleting());
225 deleteWithTrashShortcut
->setEnabled(capabilities
.supportsDeleting() && !enableMoveToTrash
);
226 editMimeTypeAction
->setEnabled(true);
227 propertiesAction
->setEnabled(true);
228 emit m_extension
->enableAction("cut", capabilities
.supportsMoving());
229 emit m_extension
->enableAction("copy", true);
233 void DolphinPart::updatePasteAction()
235 QPair
<bool, QString
> pasteInfo
= m_view
->pasteInfo();
236 emit m_extension
->enableAction( "paste", pasteInfo
.first
);
237 emit m_extension
->setActionText( "paste", pasteInfo
.second
);
240 KAboutData
* DolphinPart::createAboutData()
242 return new KAboutData("dolphinpart", "dolphin", ki18nc("@title", "Dolphin Part"), "0.1");
245 bool DolphinPart::openUrl(const KUrl
& url
)
247 bool reload
= arguments().reload();
248 // A bit of a workaround so that changing the namefilter works: force reload.
249 // Otherwise DolphinView wouldn't relist the URL, so nothing would happen.
250 if (m_nameFilter
!= m_dirLister
->nameFilter())
252 if (m_view
->url() == url
&& !reload
) { // DolphinView won't do anything in that case, so don't emit started
255 setUrl(url
); // remember it at the KParts level
256 KUrl
visibleUrl(url
);
257 if (!m_nameFilter
.isEmpty()) {
258 visibleUrl
.addPath(m_nameFilter
);
260 QString prettyUrl
= visibleUrl
.pathOrUrl();
261 emit
setWindowCaption(prettyUrl
);
262 emit m_extension
->setLocationBarUrl(prettyUrl
);
263 emit
started(0); // get the wheel to spin
264 m_dirLister
->setNameFilter(m_nameFilter
);
266 emit
aboutToOpenURL();
272 void DolphinPart::slotCompleted(const KUrl
& url
)
278 void DolphinPart::slotCanceled(const KUrl
& url
)
283 void DolphinPart::slotInfoMessage(const QString
& msg
)
285 emit
setStatusBarText(msg
);
288 void DolphinPart::slotErrorMessage(const QString
& msg
)
290 KMessageBox::error(m_view
, msg
);
293 void DolphinPart::slotRequestItemInfo(const KFileItem
& item
)
295 emit m_extension
->mouseOverInfo(item
);
298 void DolphinPart::slotItemTriggered(const KFileItem
& item
)
300 KParts::OpenUrlArguments args
;
301 args
.setMimeType(item
.mimetype());
303 // Ideally, konqueror should be changed to not require trustedSource for directory views,
304 // since the idea was not to need BrowserArguments for non-browser stuff...
305 KParts::BrowserArguments browserArgs
;
306 browserArgs
.trustedSource
= true;
307 emit m_extension
->openUrlRequest(item
.targetUrl(), args
, browserArgs
);
310 void DolphinPart::createNewWindow(const KUrl
& url
)
312 // TODO: Check issue N176832 for the missing QAIV signal; task 177399 - maybe this code
313 // should be moved into DolphinPart::slotItemTriggered()
314 KFileItem
item(S_IFDIR
, (mode_t
)-1, url
);
315 KParts::OpenUrlArguments args
;
316 args
.setMimeType(item
.mimetype());
317 emit m_extension
->createNewWindow(url
, args
);
320 void DolphinPart::slotOpenContextMenu(const KFileItem
& _item
, const KUrl
&)
322 KParts::BrowserExtension::PopupFlags popupFlags
= KParts::BrowserExtension::DefaultPopupItems
323 | KParts::BrowserExtension::ShowProperties
324 | KParts::BrowserExtension::ShowUrlOperations
;
326 KFileItem
item(_item
);
328 if (item
.isNull()) { // viewport context menu
329 popupFlags
|= KParts::BrowserExtension::ShowNavigationItems
| KParts::BrowserExtension::ShowUp
;
330 item
= m_dirLister
->rootItem();
332 item
= KFileItem( S_IFDIR
, (mode_t
)-1, url() );
335 KParts::BrowserExtension::ActionGroupMap actionGroups
;
336 QList
<QAction
*> editActions
;
338 if (!_item
.isNull()) { // only for context menu on one or more items
339 bool sDeleting
= true;
342 // If the parent directory of the selected item is writable, moving
343 // and deleting are possible.
344 KFileItem parentDir
= m_dirLister
->rootItem();
345 if (!parentDir
.isNull() && !parentDir
.isWritable()) {
346 popupFlags
|= KParts::BrowserExtension::NoDeletion
;
352 editActions
.append(actionCollection()->action("rename"));
354 bool addTrash
= false;
357 bool isIntoTrash
= _item
.url().protocol() == "trash";
359 if ( sMoving
&& !isIntoTrash
&& item
.isLocalFile() )
363 if ( !item
.isLocalFile() )
365 else if (QApplication::keyboardModifiers() & Qt::ShiftModifier
) {
370 KConfigGroup
configGroup( KGlobal::config(), "KDE" );
371 if ( configGroup
.readEntry( "ShowDeleteCommand", false) )
377 editActions
.append(actionCollection()->action("move_to_trash"));
379 editActions
.append(actionCollection()->action("delete"));
380 actionGroups
.insert("editactions", editActions
);
383 // TODO: We should change the signature of the slots (and signals) for being able
384 // to tell for which items we want a popup.
385 KFileItemList items
= (m_view
->selectedItems().count() ? m_view
->selectedItems()
386 : KFileItemList() << item
);
387 emit m_extension
->popupMenu(QCursor::pos(),
389 KParts::OpenUrlArguments(),
390 KParts::BrowserArguments(),
395 void DolphinPart::slotUrlChanged(const KUrl
& url
)
397 KParts::ReadOnlyPart::setUrl(url
);
398 QString prettyUrl
= url
.pathOrUrl();
399 emit m_extension
->setLocationBarUrl(prettyUrl
);
402 void DolphinPart::slotRequestUrlChange(const KUrl
& url
)
404 if (m_view
->url() != url
) {
405 // If the view URL is not equal to 'url', then an inner URL change has
406 // been done (e. g. by activating an existing column in the column view).
408 emit m_extension
->openUrlNotify();
414 void DolphinPartBrowserExtension::cut()
416 m_part
->view()->cutSelectedItems();
419 void DolphinPartBrowserExtension::copy()
421 m_part
->view()->copySelectedItems();
424 void DolphinPartBrowserExtension::paste()
426 m_part
->view()->paste();
429 void DolphinPartBrowserExtension::reparseConfiguration()
431 m_part
->view()->refresh();
436 void DolphinPart::slotEditMimeType()
438 const KFileItemList items
= m_view
->selectedItems();
439 if (!items
.isEmpty()) {
440 KonqOperations::editMimeType(items
.first().mimetype(), m_view
);
444 void DolphinPart::setCurrentViewMode(const QString
& viewModeName
)
446 QAction
* action
= actionCollection()->action(viewModeName
);
451 QString
DolphinPart::currentViewMode() const
453 return m_actionHandler
->currentViewModeActionName();
456 void DolphinPart::setNameFilter(const QString
& nameFilter
)
458 // This is the "/home/dfaure/*.diff" kind of name filter (KDirLister::setNameFilter)
459 // which is unrelated to DolphinView::setNameFilter which is substring filtering in a proxy.
460 m_nameFilter
= nameFilter
;
461 // TODO save/restore name filter in saveState/restoreState like KonqDirPart did in kde3?
464 void DolphinPart::slotOpenTerminal()
466 QString
dir(QDir::homePath());
470 // If the given directory is not local, it can still be the URL of an
471 // ioslave using UDS_LOCAL_PATH which to be converted first.
472 u
= KIO::NetAccess::mostLocalUrl(u
, widget());
474 //If the URL is local after the above conversion, set the directory.
475 if (u
.isLocalFile()) {
479 KToolInvocation::invokeTerminal(QString(), dir
);
482 void DolphinPart::updateNewMenu()
484 // As requested by KNewMenu :
485 m_newMenu
->slotCheckUpToDate();
486 // And set the files that the menu apply on :
487 m_newMenu
->setPopupFiles(url());
490 #include "dolphinpart.moc"