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 K_EXPORT_PLUGIN(DolphinPartFactory("dolphinpart", "dolphin"))
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 there was a "always open a new window" (when clicking on a directory) setting in konqueror
124 // (sort of spacial navigation)
126 loadPlugins(this, this, componentData());
129 DolphinPart::~DolphinPart()
134 void DolphinPart::createActions()
138 m_newMenu
= new KNewMenu(actionCollection(), widget(), "new_menu");
139 connect(m_newMenu
->menu(), SIGNAL(aboutToShow()),
140 this, SLOT(updateNewMenu()));
142 KAction
*editMimeTypeAction
= actionCollection()->addAction( "editMimeType" );
143 editMimeTypeAction
->setText( i18nc("@action:inmenu Edit", "&Edit File Type..." ) );
144 connect(editMimeTypeAction
, SIGNAL(triggered()), SLOT(slotEditMimeType()));
146 KAction
*propertiesAction
= actionCollection()->addAction( "properties" );
147 propertiesAction
->setText( i18nc("@action:inmenu Edit", "Properties") );
148 propertiesAction
->setShortcut(Qt::ALT
+Qt::Key_Return
);
149 connect(propertiesAction
, SIGNAL(triggered()), SLOT(slotProperties()));
151 // View menu: all done by DolphinViewActionHandler
155 QActionGroup
* goActionGroup
= new QActionGroup(this);
156 connect(goActionGroup
, SIGNAL(triggered(QAction
*)),
157 this, SLOT(slotGoTriggered(QAction
*)));
159 createGoAction("go_applications", "start-here-kde",
160 i18nc("@action:inmenu Go", "App&lications"), QString("programs:/"),
162 createGoAction("go_network_folders", "folder-remote",
163 i18nc("@action:inmenu Go", "&Network Folders"), QString("remote:/"),
165 createGoAction("go_settings", "preferences-system",
166 i18nc("@action:inmenu Go", "Sett&ings"), QString("settings:/"),
168 createGoAction("go_trash", "user-trash",
169 i18nc("@action:inmenu Go", "Trash"), QString("trash:/"),
171 createGoAction("go_autostart", "",
172 i18nc("@action:inmenu Go", "Autostart"), KGlobalSettings::autostartPath(),
176 if (KAuthorized::authorizeKAction("shell_access")) {
177 KAction
* action
= actionCollection()->addAction("open_terminal");
178 action
->setIcon(KIcon("utilities-terminal"));
179 action
->setText(i18nc("@action:inmenu Tools", "Open &Terminal"));
180 connect(action
, SIGNAL(triggered()), SLOT(slotOpenTerminal()));
181 action
->setShortcut(Qt::Key_F4
);
186 void DolphinPart::createGoAction(const char* name
, const char* iconName
,
187 const QString
& text
, const QString
& url
,
188 QActionGroup
* actionGroup
)
190 KAction
* action
= actionCollection()->addAction(name
);
191 action
->setIcon(KIcon(iconName
));
192 action
->setText(text
);
193 action
->setData(url
);
194 action
->setActionGroup(actionGroup
);
197 void DolphinPart::slotGoTriggered(QAction
* action
)
199 const QString url
= action
->data().toString();
200 emit m_extension
->openUrlRequest(KUrl(url
));
203 void DolphinPart::slotSelectionChanged(const KFileItemList
& selection
)
205 const bool hasSelection
= !selection
.isEmpty();
207 QAction
* renameAction
= actionCollection()->action("rename");
208 QAction
* moveToTrashAction
= actionCollection()->action("move_to_trash");
209 QAction
* deleteAction
= actionCollection()->action("delete");
210 QAction
* editMimeTypeAction
= actionCollection()->action("editMimeType");
211 QAction
* propertiesAction
= actionCollection()->action("properties");
212 QAction
* deleteWithTrashShortcut
= actionCollection()->action("delete_shortcut"); // see DolphinViewActionHandler
215 stateChanged("has_no_selection");
217 emit m_extension
->enableAction("cut", false);
218 emit m_extension
->enableAction("copy", false);
219 deleteWithTrashShortcut
->setEnabled(false);
220 editMimeTypeAction
->setEnabled(false);
222 stateChanged("has_selection");
224 // TODO share this code with DolphinMainWindow::updateEditActions (and the desktop code)
226 KonqFileItemCapabilities
capabilities(selection
);
227 const bool enableMoveToTrash
= capabilities
.isLocal() && capabilities
.supportsMoving();
229 renameAction
->setEnabled(capabilities
.supportsMoving());
230 moveToTrashAction
->setEnabled(enableMoveToTrash
);
231 deleteAction
->setEnabled(capabilities
.supportsDeleting());
232 deleteWithTrashShortcut
->setEnabled(capabilities
.supportsDeleting() && !enableMoveToTrash
);
233 editMimeTypeAction
->setEnabled(true);
234 propertiesAction
->setEnabled(true);
235 emit m_extension
->enableAction("cut", capabilities
.supportsMoving());
236 emit m_extension
->enableAction("copy", true);
240 void DolphinPart::updatePasteAction()
242 QPair
<bool, QString
> pasteInfo
= m_view
->pasteInfo();
243 emit m_extension
->enableAction( "paste", pasteInfo
.first
);
244 emit m_extension
->setActionText( "paste", pasteInfo
.second
);
247 KAboutData
* DolphinPart::createAboutData()
249 return new KAboutData("dolphinpart", "dolphin", ki18nc("@title", "Dolphin Part"), "0.1");
252 bool DolphinPart::openUrl(const KUrl
& url
)
254 bool reload
= arguments().reload();
255 // A bit of a workaround so that changing the namefilter works: force reload.
256 // Otherwise DolphinView wouldn't relist the URL, so nothing would happen.
257 if (m_nameFilter
!= m_dirLister
->nameFilter())
259 if (m_view
->url() == url
&& !reload
) { // DolphinView won't do anything in that case, so don't emit started
262 setUrl(url
); // remember it at the KParts level
263 KUrl
visibleUrl(url
);
264 if (!m_nameFilter
.isEmpty()) {
265 visibleUrl
.addPath(m_nameFilter
);
267 QString prettyUrl
= visibleUrl
.pathOrUrl();
268 emit
setWindowCaption(prettyUrl
);
269 emit m_extension
->setLocationBarUrl(prettyUrl
);
270 emit
started(0); // get the wheel to spin
271 m_dirLister
->setNameFilter(m_nameFilter
);
273 emit
aboutToOpenURL();
279 void DolphinPart::slotCompleted(const KUrl
& url
)
285 void DolphinPart::slotCanceled(const KUrl
& url
)
290 void DolphinPart::slotInfoMessage(const QString
& msg
)
292 emit
setStatusBarText(msg
);
295 void DolphinPart::slotErrorMessage(const QString
& msg
)
297 KMessageBox::error(m_view
, msg
);
300 void DolphinPart::slotRequestItemInfo(const KFileItem
& item
)
302 emit m_extension
->mouseOverInfo(item
);
305 void DolphinPart::slotItemTriggered(const KFileItem
& item
)
307 KParts::OpenUrlArguments args
;
308 args
.setMimeType(item
.mimetype());
310 // Ideally, konqueror should be changed to not require trustedSource for directory views,
311 // since the idea was not to need BrowserArguments for non-browser stuff...
312 KParts::BrowserArguments browserArgs
;
313 browserArgs
.trustedSource
= true;
314 emit m_extension
->openUrlRequest(item
.targetUrl(), args
, browserArgs
);
317 void DolphinPart::createNewWindow(const KUrl
& url
)
319 // TODO: Check issue N176832 for the missing QAIV signal; task 177399 - maybe this code
320 // should be moved into DolphinPart::slotItemTriggered()
321 KFileItem
item(S_IFDIR
, (mode_t
)-1, url
);
322 KParts::OpenUrlArguments args
;
323 args
.setMimeType(item
.mimetype());
324 emit m_extension
->createNewWindow(url
, args
);
327 void DolphinPart::slotOpenContextMenu(const KFileItem
& _item
, const KUrl
&)
329 KParts::BrowserExtension::PopupFlags popupFlags
= KParts::BrowserExtension::DefaultPopupItems
330 | KParts::BrowserExtension::ShowProperties
331 | KParts::BrowserExtension::ShowUrlOperations
;
333 KFileItem
item(_item
);
335 if (item
.isNull()) { // viewport context menu
336 popupFlags
|= KParts::BrowserExtension::ShowNavigationItems
| KParts::BrowserExtension::ShowUp
;
337 item
= m_dirLister
->rootItem();
339 item
= KFileItem( S_IFDIR
, (mode_t
)-1, url() );
342 KParts::BrowserExtension::ActionGroupMap actionGroups
;
343 QList
<QAction
*> editActions
;
345 if (!_item
.isNull()) { // only for context menu on one or more items
346 bool sDeleting
= true;
349 // If the parent directory of the selected item is writable, moving
350 // and deleting are possible.
351 KFileItem parentDir
= m_dirLister
->rootItem();
352 if (!parentDir
.isNull() && !parentDir
.isWritable()) {
353 popupFlags
|= KParts::BrowserExtension::NoDeletion
;
359 editActions
.append(actionCollection()->action("rename"));
361 bool addTrash
= false;
364 bool isIntoTrash
= _item
.url().protocol() == "trash";
366 if ( sMoving
&& !isIntoTrash
&& item
.isLocalFile() )
370 if ( !item
.isLocalFile() )
372 else if (QApplication::keyboardModifiers() & Qt::ShiftModifier
) {
377 KConfigGroup
configGroup( KGlobal::config(), "KDE" );
378 if ( configGroup
.readEntry( "ShowDeleteCommand", false) )
384 editActions
.append(actionCollection()->action("move_to_trash"));
386 editActions
.append(actionCollection()->action("delete"));
387 actionGroups
.insert("editactions", editActions
);
390 // TODO: We should change the signature of the slots (and signals) for being able
391 // to tell for which items we want a popup.
392 KFileItemList items
= (m_view
->selectedItems().count() ? m_view
->selectedItems()
393 : KFileItemList() << item
);
394 emit m_extension
->popupMenu(QCursor::pos(),
396 KParts::OpenUrlArguments(),
397 KParts::BrowserArguments(),
402 void DolphinPart::slotUrlChanged(const KUrl
& url
)
404 KParts::ReadOnlyPart::setUrl(url
);
405 QString prettyUrl
= url
.pathOrUrl();
406 emit m_extension
->setLocationBarUrl(prettyUrl
);
409 void DolphinPart::slotRequestUrlChange(const KUrl
& url
)
411 if (m_view
->url() != url
) {
412 // If the view URL is not equal to 'url', then an inner URL change has
413 // been done (e. g. by activating an existing column in the column view).
415 emit m_extension
->openUrlNotify();
421 void DolphinPartBrowserExtension::cut()
423 m_part
->view()->cutSelectedItems();
426 void DolphinPartBrowserExtension::copy()
428 m_part
->view()->copySelectedItems();
431 void DolphinPartBrowserExtension::paste()
433 m_part
->view()->paste();
436 void DolphinPartBrowserExtension::reparseConfiguration()
438 m_part
->view()->refresh();
443 void DolphinPart::slotEditMimeType()
445 const KFileItemList items
= m_view
->selectedItems();
446 if (!items
.isEmpty()) {
447 KonqOperations::editMimeType(items
.first().mimetype(), m_view
);
451 void DolphinPart::slotProperties()
453 const KFileItemList items
= m_view
->selectedItems();
454 if (!items
.isEmpty()) {
455 KPropertiesDialog
dialog(items
.first().url(), m_view
);
460 void DolphinPart::setCurrentViewMode(const QString
& viewModeName
)
462 QAction
* action
= actionCollection()->action(viewModeName
);
467 QString
DolphinPart::currentViewMode() const
469 return m_actionHandler
->currentViewModeActionName();
472 void DolphinPart::setNameFilter(const QString
& nameFilter
)
474 // This is the "/home/dfaure/*.diff" kind of name filter (KDirLister::setNameFilter)
475 // which is unrelated to DolphinView::setNameFilter which is substring filtering in a proxy.
476 m_nameFilter
= nameFilter
;
477 // TODO save/restore name filter in saveState/restoreState like KonqDirPart did in kde3?
480 void DolphinPart::slotOpenTerminal()
482 QString
dir(QDir::homePath());
486 // If the given directory is not local, it can still be the URL of an
487 // ioslave using UDS_LOCAL_PATH which to be converted first.
488 u
= KIO::NetAccess::mostLocalUrl(u
, widget());
490 //If the URL is local after the above conversion, set the directory.
491 if (u
.isLocalFile()) {
495 KToolInvocation::invokeTerminal(QString(), dir
);
498 void DolphinPart::updateNewMenu()
500 // As requested by KNewMenu :
501 m_newMenu
->slotCheckUpToDate();
502 // And set the files that the menu apply on :
503 m_newMenu
->setPopupFiles(url());
506 #include "dolphinpart.moc"